forked from csmith-project/csmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_prog.py
78 lines (64 loc) · 1.9 KB
/
gen_prog.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import subprocess
import tempfile
import sys
import shutil
command_writer = result_reader = None
pipeout = pipein = None
def write_result(str):
global pipeout
if pipeout is None:
pipeout = open(result_reader, "w")
pipeout.write(str + "\n")
pipeout.flush()
def read_command():
global pipein
if pipein is None:
pipein = open(command_writer, "rb")
while True:
c = pipein.read(1)
if not c:
continue
n = c[0]
return pipein.read(n).decode('ascii')
def ack():
write_result("ACK")
def gen(data, output_name):
global command_writer, result_reader
env = dict(os.environ)
pipe_dir = tempfile.mkdtemp()
command_writer = os.path.join(pipe_dir, "hypothesisfifo.commands")
result_reader = os.path.join(pipe_dir, "hypothesisfifo.results")
env["HYPOTHESISFIFOCOMMANDS"] = command_writer
env["HYPOTHESISFIFORESULTS"] = result_reader
os.mkfifo(command_writer)
os.mkfifo(result_reader)
proc = subprocess.Popen(["./src/csmith", "-o", output_name], env=env, stdout=sys.stdout, stderr=sys.stderr)
try:
while True:
line = read_command()
if line == "TERMINATE":
ack()
break
elif line == "RAND":
value = str(data.draw_bits(31))
write_result(value)
elif line.startswith("START "):
_, label = line.split()
data.start_example(label.strip())
ack()
elif line == "END":
data.stop_example()
ack()
# Terminated improperly
elif not line:
break
else:
raise Exception("Unknown command %r" % (line,))
proc.wait()
finally:
try:
proc.kill()
except OSError:
pass
shutil.rmtree(pipe_dir)