Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/determinism_checker.py
4128 views
1
# Copyright 2018 The Emscripten Authors. All rights reserved.
2
# Emscripten is available under two separate licenses, the MIT license and the
3
# University of Illinois/NCSA Open Source License. Both these licenses can be
4
# found in the LICENSE file.
5
6
"""Runs a build command many times to search for any nondeterminism.
7
"""
8
9
import os
10
import random
11
import subprocess
12
import time
13
from pathlib import Path
14
15
16
def run():
17
subprocess.check_call(['emcc', 'src.cpp', '-O2'])
18
ret = {}
19
for relevant_file in os.listdir('.'):
20
ret[relevant_file] = Path(relevant_file).read_text()
21
return ret
22
23
24
def write(data, subdir):
25
if not os.path.exists(subdir):
26
os.mkdir(subdir)
27
for relevant_file in data:
28
Path(os.path.join(subdir, relevant_file)).write_text(data[relevant_file])
29
30
31
os.chdir('/tmp/emscripten_temp')
32
assert len(os.listdir('.')) == 0, 'temp dir should start out empty, after that, everything there looks important to us'
33
Path('src.cpp').write_text('''
34
#include <iostream>
35
36
int main()
37
{
38
std::cout << "hello world" << std::endl << 77 << "." << std::endl;
39
return 0;
40
}
41
''')
42
43
os.environ['EMCC_DEBUG'] = '1'
44
# os.environ['BINARYEN_PASS_DEBUG'] = '3'
45
46
first = run()
47
48
i = 0
49
while 1:
50
print(i)
51
i += 1
52
time.sleep(random.random() / (10 * 5)) # add some timing nondeterminism here, not that we need it, but whatever
53
curr = run()
54
if first != curr:
55
print('NONDETERMINISM!!!1')
56
write(first, 'first')
57
write(curr, 'second')
58
break
59
60