10
10
"--max-block-depth" , "1" ,
11
11
]
12
12
13
+ def cat (path , title = None ):
14
+ if not title :
15
+ title = path
16
+ print ("-------------------- {} --------------------" .format (title ))
17
+ run (["cat" , path ])
18
+
13
19
def run_logged (cmd ):
14
20
with NamedTemporaryFile () as stdout , NamedTemporaryFile () as stderr :
15
21
result = run (cmd , stdin = DEVNULL , stdout = stdout , stderr = stderr )
16
22
if result .returncode != 0 :
17
23
print ()
18
- print ("Error: {} exited with code {}" .format (str (cmd ), result .returncode ))
19
- print ("-------------------- stdout --------------------" )
20
- run (["cat" , stdout .name ])
21
- print ("-------------------- stderr --------------------" )
22
- run (["cat" , stderr .name ])
24
+ print ("Error: '{}' exited with code {}" .format (" " .join (cmd ), result .returncode ))
25
+ cat (stdout .name , title = "stdout" )
26
+ cat (stdout .name , title = "stderr" )
23
27
return result
24
28
25
29
def run_bindgen (input , output ):
@@ -33,6 +37,15 @@ def run_bindgen(input, output):
33
37
"-I" , os .path .abspath (os .path .dirname (sys .argv [0 ])),
34
38
])
35
39
40
+ def run_rustc (output , test ):
41
+ return run_logged ([
42
+ "rustc" ,
43
+ "--crate-type" , "lib" ,
44
+ "--test" ,
45
+ output .name ,
46
+ "-o" , test .name ,
47
+ ])
48
+
36
49
def main ():
37
50
print ("Fuzzing `bindgen` with C-Smith...\n " )
38
51
@@ -41,21 +54,36 @@ def main():
41
54
print ("\r Iteration: {}" .format (iterations ), end = "" , flush = True )
42
55
43
56
input = NamedTemporaryFile (delete = False , prefix = "input-" , suffix = ".h" )
57
+ input .close ()
44
58
result = run_logged (csmith_command + ["-o" , input .name ])
45
59
if result .returncode != 0 :
46
60
exit (1 )
47
61
48
62
output = NamedTemporaryFile (delete = False , prefix = "output-" , suffix = ".rs" )
63
+ output .close ()
49
64
result = run_bindgen (input , output )
50
65
if result .returncode != 0 :
51
- print ("-------------------- {} --------------------" .format (input .name ))
52
- run (["cat" , input .name ])
53
- print ("-------------------- {} --------------------" .format (output .name ))
54
- run (["cat" , output .name ])
66
+ cat (input .name )
67
+ cat (output .name )
68
+ exit (1 )
69
+
70
+ test = NamedTemporaryFile (delete = False , prefix = "test-" )
71
+ test .close ()
72
+ result = run_rustc (output , test )
73
+ if result .returncode != 0 :
74
+ cat (input .name )
75
+ cat (output .name )
76
+ exit (1 )
77
+
78
+ result = run_logged ([test .name ])
79
+ if result .returncode != 0 :
80
+ cat (input .name )
81
+ cat (output .name )
55
82
exit (1 )
56
83
57
84
os .remove (input .name )
58
85
os .remove (output .name )
86
+ os .remove (test .name )
59
87
60
88
iterations += 1
61
89
0 commit comments