2
import random, subprocess, difflib
6
num = random.randint(2, 250)
7
density = random.random() * random.random()
8
decisions = [random.randint(1, num*20) for x in range(num*3)]
13
bs = random.randint(1, max(1, round(density*random.random()*(num-1))))
15
b.add(random.randint(1, num-1))
17
defaults[i] = random.choice(b)
22
for temp in ['fuzz', 'fuzz.fast.js', 'fuzz.slow.js', 'fuzz.cpp']:
29
entry = '''print('entry'); var label; var state; var decisions = %s; var index = 0; function check() { if (index == decisions.length) throw 'HALT'; return decisions[index++] }''' % str(decisions)
32
for i in range(len(branches[0])):
33
if i > 0: slow += 'else '
35
slow += 'if (state %% %d == %d) { label = %d; }\n' % (len(b)+1, i, b[i]) # TODO: split range 1-n into these options
36
if len(branches[0]): slow += 'else '
37
slow += 'label = %d;\n' % defaults[0]
40
while(1) switch(label) {
49
char *buffer = (char*)malloc(10*1024*1024);
50
Relooper::SetOutputBuffer(buffer, 10*1024*1024);
53
for i in range(1, num):
54
slow += ' case %d: print(%d); state = check(); \n' % (i, i)
56
for j in range(len(b)):
57
slow += ' if (state %% %d == %d) { label = %d; break }\n' % (len(b)+1, j, b[j]) # TODO: split range 1-n into these options
58
slow += ' label = %d; break\n' % defaults[i]
63
Block *b%d = new Block("%s");
66
fast += ''' Block *b%d = new Block("print(%d); state = check();%s");
67
''' % (i, i, '// ' + ('.' * int(random.expovariate(0.5/num))))
71
for j in range(len(b)):
72
fast += ''' b%d->AddBranchTo(b%d, "state %% %d == %d");
73
''' % (i, b[j], len(b)+1, j)
74
fast += ''' b%d->AddBranchTo(b%d, NULL);
75
''' % (i, defaults[i])
82
fast += ''' r.AddBlock(b%d);
98
open('fuzz.slow.js', 'w').write(slow)
99
open('fuzz.cpp', 'w').write(fast)
101
slow_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.slow.js'], stdout=subprocess.PIPE).communicate()[0]
104
subprocess.call(['g++', 'fuzz.cpp', 'Relooper.o', '-o', 'fuzz', '-g'])
106
subprocess.call(['./fuzz'], stdout=open('fuzz.fast.js', 'w'))
108
fast_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.fast.js'], stdout=subprocess.PIPE).communicate()[0]
111
if slow_out != fast_out:
112
print ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(slow_out.split('\n'), fast_out.split('\n'), fromfile='slow', tofile='fast')])