File: determinism_checker.py

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (58 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (3)
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
# Copyright 2018 The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.

"""Runs a build command many times to search for any nondeterminism.
"""

import os
import random
import subprocess
import time
from pathlib import Path


def run():
  subprocess.check_call(['emcc', 'src.cpp', '-O2'])
  ret = {}
  for relevant_file in os.listdir('.'):
    ret[relevant_file] = Path(relevant_file).read_text()
  return ret


def write(data, subdir):
  if not os.path.exists(subdir):
    os.mkdir(subdir)
  for relevant_file in data.keys():
    Path(os.path.join(subdir, relevant_file)).write_text(data[relevant_file])


os.chdir('/tmp/emscripten_temp')
assert len(os.listdir('.')) == 0, 'temp dir should start out empty, after that, everything there looks important to us'
Path('src.cpp').write_text('''
  #include <iostream>

  int main()
  {
    std::cout << "hello world" << std::endl << 77 << "." << std::endl;
    return 0;
  }
''')

os.environ['EMCC_DEBUG'] = '1'
# os.environ['BINARYEN_PASS_DEBUG'] = '3'

first = run()

i = 0
while 1:
  print(i)
  i += 1
  time.sleep(random.random() / (10 * 5)) # add some timing nondeterminism here, not that we need it, but whatever
  curr = run()
  if first != curr:
    print('NONDETERMINISM!!!1')
    write(first, 'first')
    write(curr, 'second')
    break