~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/relooper/fuzzer.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import random, subprocess, difflib
 
3
 
 
4
while True:
 
5
  # Random decisions
 
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)]
 
9
  branches = [0]*num
 
10
  defaults = [0]*num
 
11
  for i in range(num):
 
12
    b = set([])
 
13
    bs = random.randint(1, max(1, round(density*random.random()*(num-1))))
 
14
    for j in range(bs):
 
15
      b.add(random.randint(1, num-1))
 
16
    b = list(b)
 
17
    defaults[i] = random.choice(b)
 
18
    b.remove(defaults[i])
 
19
    branches[i] = b
 
20
  print num, density
 
21
 
 
22
  for temp in ['fuzz', 'fuzz.fast.js', 'fuzz.slow.js', 'fuzz.cpp']:
 
23
    try:
 
24
      os.unlink(temp)
 
25
    except:
 
26
      pass
 
27
 
 
28
  # parts
 
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)
 
30
 
 
31
  slow = entry + '\n'
 
32
  for i in range(len(branches[0])):
 
33
    if i > 0: slow += 'else '
 
34
    b = branches[0]
 
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]
 
38
 
 
39
  slow += '''
 
40
while(1) switch(label) {
 
41
'''
 
42
 
 
43
  fast = '''
 
44
 
 
45
#include <stdlib.h>
 
46
#include "Relooper.h"
 
47
 
 
48
int main() {
 
49
  char *buffer = (char*)malloc(10*1024*1024);
 
50
  Relooper::SetOutputBuffer(buffer, 10*1024*1024);
 
51
'''
 
52
 
 
53
  for i in range(1, num):
 
54
    slow += '  case %d: print(%d); state = check(); \n' % (i, i)
 
55
    b = branches[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]
 
59
 
 
60
  for i in range(num):
 
61
    if i == 0:
 
62
      fast += '''
 
63
  Block *b%d = new Block("%s");
 
64
''' % (i, entry)
 
65
    else:
 
66
      fast += '''  Block *b%d = new Block("print(%d); state = check();%s");
 
67
''' % (i, i, '// ' + ('.' * int(random.expovariate(0.5/num))))
 
68
 
 
69
  for i in range(num):
 
70
    b = branches[i]
 
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])
 
76
 
 
77
  fast += '''
 
78
  Relooper r;
 
79
'''
 
80
 
 
81
  for i in range(num):
 
82
    fast += '''  r.AddBlock(b%d);
 
83
''' % i
 
84
 
 
85
  fast += '''
 
86
  r.Calculate(b0);
 
87
  printf("\\n\\n");
 
88
  r.Render();
 
89
 
 
90
  puts(buffer);
 
91
 
 
92
  return 1;
 
93
}
 
94
'''
 
95
 
 
96
  slow += '}'
 
97
 
 
98
  open('fuzz.slow.js', 'w').write(slow)
 
99
  open('fuzz.cpp', 'w').write(fast)
 
100
  print '_'
 
101
  slow_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.slow.js'], stdout=subprocess.PIPE).communicate()[0]
 
102
 
 
103
  print '.'
 
104
  subprocess.call(['g++', 'fuzz.cpp', 'Relooper.o', '-o', 'fuzz', '-g'])
 
105
  print '*'
 
106
  subprocess.call(['./fuzz'], stdout=open('fuzz.fast.js', 'w'))
 
107
  print '-'
 
108
  fast_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.fast.js'], stdout=subprocess.PIPE).communicate()[0]
 
109
  print
 
110
 
 
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')])
 
113
    assert False
 
114
 
 
115
  #break
 
116