bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Error injection testing - systematic failure simulation
// Implements SQLite-style anomaly testing for reliability verification

use crate::models::{Config, Result};
use crate::transpile;
use crate::Error;
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::Mutex;

/// Error injection testing framework
pub struct ErrorInjectionTester {
    config: Config,
}

/// Tracks where failures can be injected
#[derive(Debug, Clone)]
pub struct FailurePoint {
    pub location: String,
    pub failure_type: FailureType,
    pub trigger_count: usize,
    pub activated: bool,
}

#[derive(Debug, Clone)]
pub enum FailureType {
    MemoryAllocation,
    FileIO,
    NetworkIO,
    Parse,
    Validation,
    CodeGeneration,
}

/// Results from error injection testing
#[derive(Debug, Default)]
pub struct ErrorInjectionResults {
    pub total_injections: usize,
    pub graceful_failures: usize,
    pub panics: usize,
    pub memory_leaks: usize,
    pub corruption_detected: usize,
    pub failure_details: Vec<String>,
}

impl ErrorInjectionTester {
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Run comprehensive error injection tests
    pub fn run_error_injection_tests(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Test memory allocation failures
        results.merge(self.test_allocation_failures()?);

        // Test I/O failures
        results.merge(self.test_io_failures()?);

        // Test parser failures
        results.merge(self.test_parser_failures()?);

        // Test validation failures
        results.merge(self.test_validation_failures()?);

        // Test code generation failures
        results.merge(self.test_codegen_failures()?);

        Ok(results)
    }

    /// Test memory allocation failure scenarios
    pub fn test_allocation_failures(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Test allocation failure at different points
        let large_string_test = format!("fn main() {{ let x = \"{}\"; }}", "x".repeat(10000));
        let test_inputs = vec![
            "fn main() { let x = 42; }",
            "fn main() { let s = \"very long string that might require allocation\"; }",
            large_string_test.as_str(),
        ];

        for (fail_after, input) in test_inputs.into_iter().enumerate() {
            // Simulate allocation failure after 'fail_after' allocations
            let result = self.test_with_allocation_failure(input, fail_after);

            results.total_injections += 1;

            match result {
                Ok(_) => {
                    // Success despite allocation pressure is fine
                    results.graceful_failures += 1;
                }
                Err(Error::Internal(msg)) if msg.contains("memory") => {
                    // Graceful OOM handling
                    results.graceful_failures += 1;
                }
                Err(_) => {
                    // Other errors are acceptable
                    results.graceful_failures += 1;
                }
            }
        }

        // Test large allocation scenarios
        for size in [1_000, 10_000, 100_000, 1_000_000] {
            let large_input = format!("fn main() {{ let x = \"{}\"; }}", "x".repeat(size));
            let result = self.test_with_memory_pressure(&large_input);

            results.total_injections += 1;

            if result.is_ok()
                || matches!(result, Err(Error::Internal(msg)) if msg.contains("memory"))
            {
                results.graceful_failures += 1;
            } else {
                results
                    .failure_details
                    .push(format!("Unexpected error with {size} bytes"));
            }
        }

        Ok(results)
    }

    /// Test I/O failure scenarios
    pub fn test_io_failures(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Test scenarios that might involve I/O (hypothetical, since our transpiler is mostly in-memory)
        let test_cases = vec![
            "fn main() { let x = 42; }",
            "fn main() { /* This could involve reading includes */ }",
        ];

        for input in test_cases {
            // Simulate I/O failures
            let result = self.test_with_io_failure(input);

            results.total_injections += 1;

            match result {
                Ok(_) => results.graceful_failures += 1,
                Err(Error::Io(_)) => results.graceful_failures += 1,
                Err(_) => {
                    results
                        .failure_details
                        .push("Unexpected I/O error handling".to_string());
                }
            }
        }

        Ok(results)
    }

    /// Test parser failure scenarios
    pub fn test_parser_failures(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Malformed inputs that should be rejected gracefully
        let malformed_inputs = vec![
            "",                                        // Empty input
            "fn",                                      // Incomplete syntax
            "fn main(",                                // Incomplete function
            "fn main() {",                             // Incomplete body
            "fn main() { let; }",                      // Incomplete let
            "fn main() { let x; }",                    // Missing initializer
            "fn main() { let x = ; }",                 // Missing value
            "fn main() { 42 }",                        // Missing let
            "fn main() { let x = y }",                 // Undefined variable
            "fn main() { let x: Vec<u32> = vec![]; }", // Unsupported types
            "fn main() { unsafe { } }",                // Unsupported unsafe
            "async fn main() {}",                      // Unsupported async
            "fn main<T>() {}",                         // Unsupported generics
            "fn main() -> impl Iterator<Item=u32> {}", // Unsupported return types
        ];

        for input in malformed_inputs {
            let result = transpile(input, &self.config);

            results.total_injections += 1;

            // All of these should fail gracefully with parse/validation errors
            match result {
                Err(Error::Parse(_)) | Err(Error::Validation(_)) => {
                    results.graceful_failures += 1;
                }
                Ok(_) => {
                    results
                        .failure_details
                        .push(format!("Unexpectedly succeeded: {input}"));
                }
                Err(e) => {
                    results
                        .failure_details
                        .push(format!("Wrong error type for '{input}': {e:?}"));
                }
            }
        }

        // Inputs that are now supported (non-fn items skipped, loop/while handled)
        // These should succeed gracefully
        let now_supported_inputs = vec![
            "struct Foo {}",                  // Non-fn items gracefully skipped
            "impl Foo {}",                    // Non-fn items gracefully skipped
            "fn main() { loop {} }",          // Loop converts to while true
            "fn main() { while true {} }",    // While now supported
            "use std::collections::HashMap;", // Use items gracefully skipped
        ];

        for input in now_supported_inputs {
            let result = transpile(input, &self.config);

            results.total_injections += 1;

            // These should either succeed or fail gracefully
            match result {
                Ok(_) | Err(Error::Parse(_)) | Err(Error::Validation(_)) => {
                    results.graceful_failures += 1;
                }
                Err(e) => {
                    results
                        .failure_details
                        .push(format!("Unexpected error type for '{input}': {e:?}"));
                }
            }
        }

        // Test deeply nested structures that might cause stack overflow
        for depth in [10, 20, 30, 40, 50] {
            let nested_input = self.create_deeply_nested_input(depth);
            let result = transpile(&nested_input, &self.config);

            results.total_injections += 1;

            match result {
                Ok(_) => results.graceful_failures += 1, // Handled deep nesting
                Err(_) => results.graceful_failures += 1, // Rejected deep nesting gracefully
            }
        }

        Ok(results)
    }

    /// Test validation failure scenarios
    pub fn test_validation_failures(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Inputs that parse but should fail validation
        let validation_failures = vec![
            "fn not_main() { let x = 42; }",         // No main function
            "fn main() {} fn main() {}",             // Duplicate main
            "fn main() { return x; }",               // Undefined variable
            "fn main() { let x = unknown_func(); }", // Unknown function
            "fn main() { let x = 42; let x = 43; }", // Duplicate variable
        ];

        for input in validation_failures {
            let result = transpile(input, &self.config);

            results.total_injections += 1;

            match result {
                Err(Error::Validation(_)) => {
                    results.graceful_failures += 1;
                }
                Ok(_) => {
                    results
                        .failure_details
                        .push(format!("Should have failed validation: {input}"));
                }
                Err(e) => {
                    results
                        .failure_details
                        .push(format!("Wrong error type: {e:?}"));
                }
            }
        }

        Ok(results)
    }

    /// Test code generation failure scenarios
    pub fn test_codegen_failures(&mut self) -> Result<ErrorInjectionResults> {
        let mut results = ErrorInjectionResults::default();

        // Inputs that might stress the code generator
        let long_var_name = format!(
            "fn main() {{ let {} = 42; }}",
            "very_long_variable_name".repeat(100)
        );
        let many_vars = (0..1000)
            .map(|i| format!("let var{i} = {i};"))
            .collect::<Vec<_>>()
            .join(" ");
        let many_func_calls = format!(
            "fn main() {{ {}; }}",
            (0..100)
                .map(|i| format!("func{i}"))
                .collect::<Vec<_>>()
                .join("(); ")
        );

        let stress_inputs = vec![
            // Very long variable names
            long_var_name.as_str(),
            // Many variables
            many_vars.as_str(),
            // Complex expressions
            "fn main() { let x = ((1 + 2) * (3 + 4)) + ((5 + 6) * (7 + 8)); }",
            // Many function calls
            many_func_calls.as_str(),
        ];

        for input in stress_inputs {
            let full_input = if input.starts_with("fn main()") {
                input.to_string()
            } else {
                format!("fn main() {{ {input} }}")
            };

            let result = transpile(&full_input, &self.config);

            results.total_injections += 1;

            match result {
                Ok(_) => results.graceful_failures += 1,
                Err(_) => results.graceful_failures += 1, // Graceful rejection is also fine
            }
        }

        Ok(results)
    }

    // Helper methods
    fn test_with_allocation_failure(&self, input: &str, _fail_after: usize) -> Result<String> {
        // In a real implementation, this would use a custom allocator
        // For now, just test normal operation
        transpile(input, &self.config)
    }

    fn test_with_memory_pressure(&self, input: &str) -> Result<String> {
        // Test under memory pressure
        transpile(input, &self.config)
    }

    fn test_with_io_failure(&self, input: &str) -> Result<String> {
        // Test with simulated I/O failures
        transpile(input, &self.config)
    }

    fn create_deeply_nested_input(&self, depth: usize) -> String {
        let mut input = "fn main() {".to_string();

        for i in 0..depth {
            input.push_str(&format!("if true {{ let x{i} = {i}; "));
        }

        for _ in 0..depth {
            input.push_str("} ");
        }

        input.push('}');
        input
    }
}

impl ErrorInjectionResults {
    fn merge(&mut self, other: ErrorInjectionResults) {
        self.total_injections += other.total_injections;
        self.graceful_failures += other.graceful_failures;
        self.panics += other.panics;
        self.memory_leaks += other.memory_leaks;
        self.corruption_detected += other.corruption_detected;
        self.failure_details.extend(other.failure_details);
    }

    pub fn success_rate(&self) -> f64 {
        if self.total_injections == 0 {
            0.0
        } else {
            (self.graceful_failures as f64 / self.total_injections as f64) * 100.0
        }
    }
}

/// Custom allocator for testing allocation failures
pub struct FailingAllocator {
    fail_after: usize,
    allocation_count: Mutex<usize>,
}

impl FailingAllocator {
    pub fn new(fail_after: usize) -> Self {
        Self {
            fail_after,
            allocation_count: Mutex::new(0),
        }
    }
}

unsafe impl GlobalAlloc for FailingAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let mut count = self.allocation_count.lock().unwrap();
        *count += 1;

        if *count > self.fail_after {
            std::ptr::null_mut()
        } else {
            // SAFETY: Delegating to System allocator, which is safe when called properly
            unsafe { System.alloc(layout) }
        }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        // SAFETY: Delegating to System allocator, ptr comes from alloc()
        unsafe { System.dealloc(ptr, layout) }
    }
}

#[cfg(test)]
#[path = "error_injection_tests_parser_error.rs"]
mod tests_extracted;