Skip to content

Commit dfd3e71

Browse files
authored
Nondeterminism checker tool (emscripten-core#6263) [ci skip]
1 parent ffc375b commit dfd3e71

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tools/determinism_checker.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Runs a build command many times to search for any nondeterminism.
3+
'''
4+
5+
import os
6+
import random
7+
import shutil
8+
import subprocess
9+
import time
10+
11+
def run():
12+
subprocess.check_call(['emcc', 'src.cpp', '-O2', '-s', 'WASM=1'])
13+
ret = {}
14+
for relevant_file in os.listdir('.'):
15+
ret[relevant_file] = open(relevant_file).read()
16+
return ret
17+
18+
def write(data, subdir):
19+
if not os.path.exists(subdir):
20+
os.mkdir(subdir)
21+
for relevant_file in data.keys():
22+
open(os.path.join(subdir, relevant_file), 'w').write(data[relevant_file])
23+
24+
old = os.getcwd()
25+
try:
26+
os.chdir('/tmp/emscripten_temp')
27+
assert len(os.listdir('.')) == 0, 'temp dir should start out empty, after that, everything there looks important to us'
28+
open('src.cpp', 'w').write('''
29+
#include <iostream>
30+
31+
int main()
32+
{
33+
std::cout << "hello world" << std::endl << 77 << "." << std::endl;
34+
return 0;
35+
}
36+
''')
37+
38+
os.environ['EMCC_DEBUG'] = '1'
39+
#os.environ['BINARYEN_PASS_DEBUG'] = '3'
40+
41+
first = run()
42+
43+
i = 0
44+
while 1:
45+
print(i)
46+
i += 1
47+
time.sleep(random.random()/(10*5)) # add some timing nondeterminism here, not that we need it, but whatever
48+
curr = run()
49+
if first != curr:
50+
print('NONDETERMINISM!!!1')
51+
write(first, 'first')
52+
write(curr, 'second')
53+
break
54+
55+
finally:
56+
os.chdir(old)
57+

0 commit comments

Comments
 (0)