decy-verify 2.2.0

Safety property verification for transpiled Rust code
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Differential testing against GCC semantics (S5).
//!
//! Compiles original C with gcc, compiles transpiled Rust with rustc,
//! runs both binaries, and compares stdout + exit codes to prove
//! behavioral equivalence.
//!
//! # Example
//!
//! ```no_run
//! use decy_verify::diff_test::{diff_test, DiffTestConfig};
//!
//! let c_code = "int main() { return 0; }";
//! let rust_code = "fn main() {}";
//! let config = DiffTestConfig::default();
//! let result = diff_test(c_code, rust_code, &config).unwrap();
//! assert!(result.stdout_matches);
//! assert!(result.exit_code_matches);
//! ```

use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;

/// Output captured from running a compiled binary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecutionOutput {
    /// Standard output
    pub stdout: String,
    /// Standard error
    pub stderr: String,
    /// Process exit code (0 = success)
    pub exit_code: i32,
}

/// Result of a differential test comparing C and Rust execution.
#[derive(Debug, Clone)]
pub struct DiffTestResult {
    /// Output from the compiled C binary
    pub c_output: ExecutionOutput,
    /// Output from the compiled Rust binary
    pub rust_output: ExecutionOutput,
    /// Whether stdout is identical
    pub stdout_matches: bool,
    /// Whether exit codes are identical
    pub exit_code_matches: bool,
    /// List of specific divergences found
    pub divergences: Vec<String>,
}

impl DiffTestResult {
    /// Returns true when both stdout and exit code match.
    pub fn passed(&self) -> bool {
        self.stdout_matches && self.exit_code_matches
    }
}

/// Configuration for differential testing.
#[derive(Debug, Clone)]
pub struct DiffTestConfig {
    /// Timeout in seconds for each binary execution
    pub timeout_secs: u64,
    /// Path to the gcc compiler
    pub gcc_path: String,
    /// Path to the rustc compiler
    pub rustc_path: String,
    /// Whether to also compare stderr output
    pub compare_stderr: bool,
}

impl Default for DiffTestConfig {
    fn default() -> Self {
        Self {
            timeout_secs: 5,
            gcc_path: "gcc".to_string(),
            rustc_path: "rustc".to_string(),
            compare_stderr: false,
        }
    }
}

/// Compile C source code with gcc and return the temp directory + binary path.
///
/// The caller owns the returned `TempDir`; dropping it cleans up all files.
pub fn compile_c(c_code: &str, config: &DiffTestConfig) -> Result<(TempDir, PathBuf)> {
    let tmp = TempDir::new().context("Failed to create temp directory for C compilation")?;
    let src = tmp.path().join("input.c");
    let bin = tmp.path().join("c_binary");

    std::fs::write(&src, c_code).context("Failed to write C source to temp file")?;

    let output = Command::new(&config.gcc_path)
        .arg("-o")
        .arg(&bin)
        .arg("-x")
        .arg("c")
        .arg("-std=c99")
        .arg("-lm")
        .arg(&src)
        .output()
        .with_context(|| format!("Failed to run gcc ({})", config.gcc_path))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("gcc compilation failed:\n{}", stderr);
    }

    Ok((tmp, bin))
}

/// Compile Rust source code with rustc and return the temp directory + binary path.
///
/// The caller owns the returned `TempDir`; dropping it cleans up all files.
pub fn compile_rust(rust_code: &str, config: &DiffTestConfig) -> Result<(TempDir, PathBuf)> {
    let tmp = TempDir::new().context("Failed to create temp directory for Rust compilation")?;
    let src = tmp.path().join("input.rs");
    let bin = tmp.path().join("rust_binary");

    std::fs::write(&src, rust_code).context("Failed to write Rust source to temp file")?;

    let output = Command::new(&config.rustc_path)
        .arg("--edition=2021")
        .arg("-o")
        .arg(&bin)
        .arg(&src)
        .output()
        .with_context(|| format!("Failed to run rustc ({})", config.rustc_path))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("rustc compilation failed:\n{}", stderr);
    }

    Ok((tmp, bin))
}

/// Run a compiled binary and capture its output.
pub fn run_binary(binary: &Path) -> Result<ExecutionOutput> {
    let output = Command::new(binary)
        .output()
        .with_context(|| format!("Failed to execute binary: {}", binary.display()))?;

    Ok(ExecutionOutput {
        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
        stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        exit_code: output.status.code().unwrap_or(-1),
    })
}

/// Run a full differential test: compile C with gcc, compile Rust with rustc,
/// execute both, and compare outputs.
pub fn diff_test(c_code: &str, rust_code: &str, config: &DiffTestConfig) -> Result<DiffTestResult> {
    // Compile both
    let (_c_dir, c_bin) =
        compile_c(c_code, config).context("C compilation failed during diff test")?;
    let (_rs_dir, rs_bin) =
        compile_rust(rust_code, config).context("Rust compilation failed during diff test")?;

    // Run both
    let c_output = run_binary(&c_bin).context("Failed to run C binary")?;
    let rust_output = run_binary(&rs_bin).context("Failed to run Rust binary")?;

    // Compare
    let stdout_matches = c_output.stdout == rust_output.stdout;
    let exit_code_matches = c_output.exit_code == rust_output.exit_code;

    let mut divergences = Vec::new();

    if !stdout_matches {
        divergences.push(format!(
            "stdout differs:\n  C:    {:?}\n  Rust: {:?}",
            c_output.stdout, rust_output.stdout
        ));
    }

    if !exit_code_matches {
        divergences.push(format!(
            "exit code differs: C={}, Rust={}",
            c_output.exit_code, rust_output.exit_code
        ));
    }

    if config.compare_stderr && c_output.stderr != rust_output.stderr {
        divergences.push(format!(
            "stderr differs:\n  C:    {:?}\n  Rust: {:?}",
            c_output.stderr, rust_output.stderr
        ));
    }

    Ok(DiffTestResult { c_output, rust_output, stdout_matches, exit_code_matches, divergences })
}

#[cfg(test)]
mod tests {
    use super::*;

    // ========================================================================
    // Compilation tests
    // ========================================================================

    #[test]
    fn test_compile_c_valid() {
        let config = DiffTestConfig::default();
        let result = compile_c("int main() { return 0; }", &config);
        assert!(result.is_ok(), "Valid C should compile: {:?}", result.err());
        let (_dir, bin) = result.unwrap();
        assert!(bin.exists(), "Binary should exist after compilation");
    }

    #[test]
    fn test_compile_c_invalid() {
        let config = DiffTestConfig::default();
        let result = compile_c("int main( { }", &config);
        assert!(result.is_err(), "Invalid C should fail compilation");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("gcc compilation failed"),
            "Error should mention gcc: {}",
            err_msg
        );
    }

    #[test]
    fn test_compile_c_bad_gcc_path() {
        let config =
            DiffTestConfig { gcc_path: "/nonexistent/gcc".to_string(), ..Default::default() };
        let result = compile_c("int main() { return 0; }", &config);
        assert!(result.is_err(), "Bad gcc path should error");
    }

    #[test]
    fn test_compile_rust_valid() {
        let config = DiffTestConfig::default();
        let result = compile_rust("fn main() {}", &config);
        assert!(result.is_ok(), "Valid Rust should compile: {:?}", result.err());
        let (_dir, bin) = result.unwrap();
        assert!(bin.exists(), "Binary should exist after compilation");
    }

    #[test]
    fn test_compile_rust_invalid() {
        let config = DiffTestConfig::default();
        let result = compile_rust("fn main( {}", &config);
        assert!(result.is_err(), "Invalid Rust should fail compilation");
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("rustc compilation failed"),
            "Error should mention rustc: {}",
            err_msg
        );
    }

    #[test]
    fn test_compile_rust_bad_rustc_path() {
        let config =
            DiffTestConfig { rustc_path: "/nonexistent/rustc".to_string(), ..Default::default() };
        let result = compile_rust("fn main() {}", &config);
        assert!(result.is_err(), "Bad rustc path should error");
    }

    // ========================================================================
    // S5 Prediction tests: behavioral equivalence
    // ========================================================================

    #[test]
    fn test_s5_p1_integer_arithmetic() {
        let c_code = r#"
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main() {
    printf("%d\n", add(2, 3));
    return 0;
}
"#;
        let rust_code = r#"
fn add(a: i32, b: i32) -> i32 { a + b }
fn main() {
    println!("{}", add(2, 3));
}
"#;
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(
            result.stdout_matches,
            "Integer arithmetic stdout should match: {:?}",
            result.divergences
        );
        assert!(result.exit_code_matches, "Exit codes should match: {:?}", result.divergences);
        assert!(result.passed());
    }

    #[test]
    fn test_s5_p2_array_indexing() {
        let c_code = r#"
#include <stdio.h>
int main() {
    int arr[] = {10, 20, 30};
    printf("%d\n", arr[1]);
    return 0;
}
"#;
        let rust_code = r#"
fn main() {
    let arr = [10, 20, 30];
    println!("{}", arr[1]);
}
"#;
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(
            result.stdout_matches,
            "Array indexing stdout should match: {:?}",
            result.divergences
        );
        assert!(result.passed());
    }

    #[test]
    fn test_s5_p3_string_output() {
        let c_code = r#"
#include <stdio.h>
int main() {
    printf("hello world\n");
    return 0;
}
"#;
        let rust_code = r#"
fn main() {
    println!("hello world");
}
"#;
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(result.stdout_matches, "String output should match: {:?}", result.divergences);
        assert!(result.passed());
    }

    // ========================================================================
    // Edge cases
    // ========================================================================

    #[test]
    fn test_empty_stdout() {
        let c_code = "int main() { return 0; }";
        let rust_code = "fn main() {}";
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(result.stdout_matches, "Empty stdout should match");
        assert_eq!(result.c_output.stdout, "");
        assert_eq!(result.rust_output.stdout, "");
        assert!(result.passed());
    }

    #[test]
    fn test_multiline_output() {
        let c_code = r#"
#include <stdio.h>
int main() {
    printf("line1\n");
    printf("line2\n");
    printf("line3\n");
    return 0;
}
"#;
        let rust_code = r#"
fn main() {
    println!("line1");
    println!("line2");
    println!("line3");
}
"#;
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(result.stdout_matches, "Multiline output should match: {:?}", result.divergences);
        assert!(result.passed());
    }

    #[test]
    fn test_nonzero_exit_code() {
        let c_code = "int main() { return 42; }";
        let rust_code = "fn main() { std::process::exit(42); }";
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert_eq!(result.c_output.exit_code, 42);
        assert_eq!(result.rust_output.exit_code, 42);
        assert!(result.exit_code_matches);
        assert!(result.passed());
    }

    #[test]
    fn test_stdout_divergence_detected() {
        let c_code = r#"
#include <stdio.h>
int main() { printf("from C\n"); return 0; }
"#;
        let rust_code = r#"
fn main() { println!("from Rust"); }
"#;
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(!result.stdout_matches, "Different outputs should diverge");
        assert!(!result.divergences.is_empty());
        assert!(!result.passed());
    }

    #[test]
    fn test_exit_code_divergence_detected() {
        let c_code = "int main() { return 0; }";
        let rust_code = "fn main() { std::process::exit(1); }";
        let config = DiffTestConfig::default();
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(!result.exit_code_matches, "Different exit codes should diverge");
        assert!(!result.passed());
    }

    // ========================================================================
    // Config tests
    // ========================================================================

    #[test]
    fn test_default_config() {
        let config = DiffTestConfig::default();
        assert_eq!(config.timeout_secs, 5);
        assert_eq!(config.gcc_path, "gcc");
        assert_eq!(config.rustc_path, "rustc");
        assert!(!config.compare_stderr);
    }

    #[test]
    fn test_compare_stderr_flag() {
        let c_code = "int main() { return 0; }";
        let rust_code = "fn main() {}";
        let config = DiffTestConfig { compare_stderr: true, ..Default::default() };
        let result = diff_test(c_code, rust_code, &config).expect("diff_test should succeed");
        assert!(result.passed());
    }

    #[test]
    fn test_diff_test_result_passed() {
        let result = DiffTestResult {
            c_output: ExecutionOutput {
                stdout: "ok\n".to_string(),
                stderr: String::new(),
                exit_code: 0,
            },
            rust_output: ExecutionOutput {
                stdout: "ok\n".to_string(),
                stderr: String::new(),
                exit_code: 0,
            },
            stdout_matches: true,
            exit_code_matches: true,
            divergences: vec![],
        };
        assert!(result.passed());
    }

    #[test]
    fn test_diff_test_result_failed() {
        let result = DiffTestResult {
            c_output: ExecutionOutput {
                stdout: "a\n".to_string(),
                stderr: String::new(),
                exit_code: 0,
            },
            rust_output: ExecutionOutput {
                stdout: "b\n".to_string(),
                stderr: String::new(),
                exit_code: 0,
            },
            stdout_matches: false,
            exit_code_matches: true,
            divergences: vec!["stdout differs".to_string()],
        };
        assert!(!result.passed());
    }
}