forked from csmith-project/csmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_warning_examples.py
60 lines (47 loc) · 1.62 KB
/
find_warning_examples.py
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
import os
from random import Random
from hypothesis.internal.conjecture.data import ConjectureData
import sys
from gen_prog import gen
import hashlib
import subprocess
EXAMPLES = os.path.join(os.path.dirname(__file__), "examples")
LIMIT = 5
def compiler_emits_warning_and_program_terminates(warning, sourcename):
try:
proc = subprocess.Popen(["gcc", "-Wall", "-pedantic", "-I", "runtime", sourcename], encoding="utf-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
if not warning in stderr:
return False
proc = subprocess.Popen(["./a.out"], encoding="utf-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate(timeout=3)
except subprocess.SubprocessError:
return False
return True
if __name__ == '__main__':
warning = sys.argv[1]
try:
os.makedirs(EXAMPLES)
except FileExistsError:
pass
rnd = Random()
found = 0
i = 0
while found < LIMIT:
i += 1
print("Iter", i)
data = ConjectureData(
max_length=2**29,
draw_bytes=lambda self, n: rnd.getrandbits(8 * n).to_bytes(
n, byteorder='big'
)
)
sourcename = "example.c"
gen(data, sourcename)
if compiler_emits_warning_and_program_terminates(warning, sourcename):
found += 1
print("Found", found)
result = bytes(data.buffer)
name = hashlib.sha1(result).hexdigest()[:16]
with open(os.path.join(EXAMPLES, name), 'wb') as outfile:
outfile.write(result)