-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathcommon.py
More file actions
1601 lines (1352 loc) · 56.9 KB
/
common.py
File metadata and controls
1601 lines (1352 loc) · 56.9 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 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.
import contextlib
import difflib
import hashlib
import io
import json
import logging
import os
import re
import shlex
import shutil
import stat
import string
import subprocess
import sys
import tempfile
import textwrap
from functools import wraps
from pathlib import Path
from subprocess import PIPE, STDOUT
import clang_native
import jsrun
import line_endings
from retryable_unittest import RetryableTestCase
from tools import building, config, feature_matrix, shared, utils
from tools.feature_matrix import Feature
from tools.settings import COMPILE_TIME_SETTINGS
from tools.shared import DEBUG, EMCC, EMXX, get_canonical_temp_dir
from tools.utils import (
WINDOWS,
exe_path_from_root,
exit_with_error,
path_from_root,
read_binary,
read_file,
write_binary,
)
logger = logging.getLogger('common')
# If we are drawing a parallel swimlane graph of test output, we need to use a temp
# file to track which tests were flaky so they can be graphed in orange color to
# visually stand out.
flaky_tests_log_filename = os.path.join(path_from_root('out/flaky_tests.txt'))
EMTEST_DETECT_TEMPFILE_LEAKS = None
EMTEST_SAVE_DIR = None
# generally js engines are equivalent, testing 1 is enough. set this
# to force testing on all js engines, good to find js engine bugs
EMTEST_ALL_ENGINES = None
EMTEST_SKIP_SLOW = None
EMTEST_SKIP_FLAKY = None
EMTEST_RETRY_FLAKY = None
EMTEST_LACKS_NATIVE_CLANG = None
EMTEST_VERBOSE = None
EMTEST_REBASELINE = None
# Verbosity level control for subprocess calls to configure + make.
# 0: disabled.
# 1: Log stderr of configure/make.
# 2: Log stdout and stderr configure/make. Print out subprocess commands that were executed.
# 3: Log stdout and stderr, and pass VERBOSE=1 to CMake/configure/make steps.
EMTEST_BUILD_VERBOSE = int(os.getenv('EMTEST_BUILD_VERBOSE', '0'))
if 'EM_BUILD_VERBOSE' in os.environ:
exit_with_error('EM_BUILD_VERBOSE has been renamed to EMTEST_BUILD_VERBOSE')
# Special value for passing to assert_returncode which means we expect that program
# to fail with non-zero return code, but we don't care about specifically which one.
NON_ZERO = -1
TEST_ROOT = path_from_root('test')
LAST_TEST = path_from_root('out/last_test.txt')
PREVIOUS_TEST_RUN_RESULTS_FILE = path_from_root('out/previous_test_run_results.json')
WEBIDL_BINDER = exe_path_from_root('tools/webidl_binder')
EMBUILDER = exe_path_from_root('embuilder')
EMMAKE = exe_path_from_root('emmake')
EMCMAKE = exe_path_from_root('emcmake')
EMCONFIGURE = exe_path_from_root('emconfigure')
EMRUN = exe_path_from_root('emrun')
WASM_DIS = os.path.join(building.get_binaryen_bin(), 'wasm-dis')
LLVM_OBJDUMP = shared.llvm_tool_path('llvm-objdump')
PYTHON = sys.executable
assert config.NODE_JS # assert for mypy's benefit
# By default we run the tests in the same version of node as emscripten itself used.
if not config.NODE_JS_TEST:
config.NODE_JS_TEST = config.NODE_JS
# The default set of JS_ENGINES contains just node.
if not config.JS_ENGINES:
config.JS_ENGINES = [config.NODE_JS_TEST]
def errlog(*args):
"""Shorthand for print with file=sys.stderr
Use this for all internal test framework logging..
"""
print(*args, file=sys.stderr)
def load_previous_test_run_results():
try:
return json.load(open(PREVIOUS_TEST_RUN_RESULTS_FILE))
except FileNotFoundError:
return {}
def test_file(*path_components):
"""Construct a path relative to the emscripten "tests" directory."""
return str(Path(TEST_ROOT, *path_components))
def maybe_test_file(filename):
if not os.path.exists(filename) and os.path.exists(test_file(filename)):
filename = test_file(filename)
return filename
def copytree(src, dest):
shutil.copytree(src, dest, dirs_exist_ok=True)
def exe_suffix(cmd):
return cmd + '.exe' if WINDOWS else cmd
def compiler_for(filename, force_c=False):
if utils.suffix(filename) in ('.cc', '.cxx', '.cpp') and not force_c:
return EMXX
else:
return EMCC
def record_flaky_test(test_name, attempt_count, max_attempts, exception_msg):
logger.info(f'Retrying flaky test "{test_name}" (attempt {attempt_count}/{max_attempts} failed):\n{exception_msg}')
open(flaky_tests_log_filename, 'a').write(f'{test_name}\n')
def node_bigint_flags(node_version):
# The --experimental-wasm-bigint flag was added in v12, and then removed (enabled by default)
# in v16.
if node_version and node_version < (16, 0, 0):
return ['--experimental-wasm-bigint']
else:
return []
@contextlib.contextmanager
def env_modify(updates):
"""A context manager that updates os.environ."""
# This could also be done with mock.patch.dict() but taking a dependency
# on the mock library is probably not worth the benefit.
old_env = os.environ.copy()
print("env_modify: " + str(updates))
# Setting a value to None means clear the environment variable
clears = [key for key, value in updates.items() if value is None]
updates = {key: value for key, value in updates.items() if value is not None}
os.environ.update(updates)
for key in clears:
if key in os.environ:
del os.environ[key]
try:
yield
finally:
os.environ.clear()
os.environ.update(old_env)
def ensure_dir(dirname):
dirname = Path(dirname)
dirname.mkdir(parents=True, exist_ok=True)
def limit_size(string):
maxbytes = 800000 * 20
if sys.stdout.isatty():
maxlines = 500
max_line = 500
else:
max_line = 5000
maxlines = 1000
lines = string.splitlines()
for i, line in enumerate(lines):
if len(line) > max_line:
lines[i] = line[:max_line] + '[..]'
if len(lines) > maxlines:
lines = lines[0:maxlines // 2] + ['[..]'] + lines[-maxlines // 2:]
lines.append('(not all output shown. See `limit_size`)')
string = '\n'.join(lines) + '\n'
if len(string) > maxbytes:
string = string[0:maxbytes // 2] + '\n[..]\n' + string[-maxbytes // 2:]
return string
def create_file(name, contents, binary=False, absolute=False):
name = Path(name)
assert absolute or not name.is_absolute(), name
if binary:
name.write_bytes(contents)
else:
# Dedent the contents of text files so that the files on disc all
# start in column 1, even if they are indented when embedded in the
# python test code.
contents = textwrap.dedent(contents)
name.write_text(contents, encoding='utf-8')
@contextlib.contextmanager
def chdir(dir):
"""A context manager that performs actions in the given directory."""
orig_cwd = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(orig_cwd)
def make_executable(name):
Path(name).chmod(stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
def make_dir_writeable(dirname):
# Some tests make files and subdirectories read-only, so rmtree/unlink will not delete
# them. Force-make everything writable in the subdirectory to make it
# removable and re-attempt.
os.chmod(dirname, 0o777)
for directory, subdirs, files in os.walk(dirname):
for item in files + subdirs:
i = os.path.join(directory, item)
if not os.path.islink(i):
os.chmod(i, 0o777)
def force_delete_dir(dirname):
"""Deletes a directory. Returns whether deletion succeeded."""
if not os.path.exists(dirname):
return True
assert not os.path.isfile(dirname)
try:
make_dir_writeable(dirname)
utils.delete_dir(dirname)
except PermissionError as e:
# This issue currently occurs on Windows when running browser tests e.g.
# on Firefox browser. Killing Firefox browser is not 100% watertight, and
# occassionally a Firefox browser process can be left behind, holding on
# to a file handle, preventing the deletion from succeeding.
# We expect this issue to only occur on Windows.
if not WINDOWS:
raise e
print(f'Warning: Failed to delete directory "{dirname}"\n{e}')
return False
return True
def force_delete_contents(dirname):
make_dir_writeable(dirname)
utils.delete_contents(dirname)
def get_output_suffix(args):
if any(a in args for a in ('-sEXPORT_ES6', '-sWASM_ESM_INTEGRATION', '-sMODULARIZE=instance')):
return '.mjs'
else:
return '.js'
class RunnerMeta(type):
@classmethod
def make_test(mcs, name, func, suffix, args):
"""
This is a helper function to create new test functions for each parameterized form.
:param name: the original name of the function
:param func: the original function that we are parameterizing
:param suffix: the suffix to append to the name of the function for this parameterization
:param args: the positional arguments to pass to the original function for this parameterization
:returns: a tuple of (new_function_name, new_function_object)
"""
# Create the new test function. It calls the original function with the specified args.
# We use @functools.wraps to copy over all the function attributes.
@wraps(func)
def resulting_test(self):
return func(self, *args)
# Add suffix to the function name so that it displays correctly.
if suffix:
resulting_test.__name__ = f'{name}_{suffix}'
else:
resulting_test.__name__ = name
# On python 3, functions have __qualname__ as well. This is a full dot-separated path to the
# function. We add the suffix to it as well.
resulting_test.__qualname__ = f'{func.__qualname__}_{suffix}'
return resulting_test.__name__, resulting_test
def __new__(mcs, name, bases, attrs):
# This metaclass expands parameterized methods from `attrs` into separate ones in `new_attrs`.
new_attrs = {}
for attr_name, value in attrs.items():
# Check if a member of the new class has _parameterize, the tag inserted by @parameterized.
if hasattr(value, '_parameterize'):
# If it does, we extract the parameterization information, build new test functions.
for suffix, args in value._parameterize.items():
new_name, func = mcs.make_test(attr_name, value, suffix, args)
assert new_name not in new_attrs, 'Duplicate attribute name generated when parameterizing %s' % attr_name
new_attrs[new_name] = func
else:
# If not, we just copy it over to new_attrs verbatim.
assert attr_name not in new_attrs, '%s collided with an attribute from parameterization' % attr_name
new_attrs[attr_name] = value
# We invoke type, the default metaclass, to actually create the new class, with new_attrs.
return type.__new__(mcs, name, bases, new_attrs)
class RunnerCore(RetryableTestCase, metaclass=RunnerMeta):
# default temporary directory settings. set_temp_dir may be called later to
# override these
temp_dir = shared.TEMP_DIR
canonical_temp_dir = get_canonical_temp_dir(shared.TEMP_DIR)
# This avoids cluttering the test runner output, which is stderr too, with compiler warnings etc.
# Change this to None to get stderr reporting, for debugging purposes
stderr_redirect = STDOUT
def is_wasm(self):
return self.get_setting('WASM') != 0
def is_wasm2js(self):
return not self.is_wasm()
def is_browser_test(self):
return False
def is_wasm64(self):
return self.get_setting('MEMORY64')
def is_4gb(self):
return self.get_setting('INITIAL_MEMORY') == '4200mb'
def is_2gb(self):
return self.get_setting('INITIAL_MEMORY') == '2200mb'
def check_dylink(self):
if self.get_setting('WASM_ESM_INTEGRATION'):
self.skipTest('dynamic linking not supported with WASM_ESM_INTEGRATION')
if '-lllvmlibc' in self.cflags:
self.skipTest('dynamic linking not supported with llvm-libc')
if self.is_wasm2js():
self.skipTest('dynamic linking not supported with wasm2js')
# MEMORY64=2 mode doesn't currently support dynamic linking because
# The side modules are lowered to wasm32 when they are built, making
# them unlinkable with wasm64 binaries.
if self.get_setting('MEMORY64') == 2:
self.skipTest('dynamic linking not supported with MEMORY64=2')
def get_v8(self):
"""Return v8 engine, if one is configured, otherwise None"""
if not config.V8_ENGINE or config.V8_ENGINE not in config.JS_ENGINES:
return None
return config.V8_ENGINE
def require_v8(self):
if 'EMTEST_SKIP_V8' in os.environ:
self.skipTest('test requires v8 and EMTEST_SKIP_V8 is set')
v8 = self.get_v8()
if not v8:
self.fail('d8 required to run this test. Use EMTEST_SKIP_V8 to skip')
self.require_engine(v8)
self.cflags.append('-sENVIRONMENT=shell')
def get_nodejs(self):
"""Return nodejs engine, if one is configured, otherwise None"""
if config.NODE_JS_TEST not in config.JS_ENGINES:
return None
return config.NODE_JS_TEST
def require_node(self):
if 'EMTEST_SKIP_NODE' in os.environ:
self.skipTest('test requires node and EMTEST_SKIP_NODE is set')
nodejs = self.get_nodejs()
if not nodejs:
self.fail('node required to run this test. Use EMTEST_SKIP_NODE to skip')
self.require_engine(nodejs)
return nodejs
def node_is_canary(self, nodejs):
return nodejs and nodejs[0] and ('canary' in nodejs[0] or 'nightly' in nodejs[0])
def require_node_canary(self):
if 'EMTEST_SKIP_NODE_CANARY' in os.environ:
self.skipTest('test requires node canary and EMTEST_SKIP_NODE_CANARY is set')
nodejs = self.get_nodejs()
if self.node_is_canary(nodejs):
self.require_engine(nodejs)
return
self.fail('node canary required to run this test. Use EMTEST_SKIP_NODE_CANARY to skip')
def require_engine(self, engine):
logger.debug(f'require_engine: {engine}')
if self.required_engine and self.required_engine != engine:
self.skipTest(f'Skipping test that requires `{engine}` when `{self.required_engine}` was previously required')
self.required_engine = engine
self.js_engines = [engine]
self.wasm_engines = []
def require_wasm64(self):
if 'EMTEST_SKIP_WASM64' in os.environ:
self.skipTest('test requires node >= 24 or d8 (and EMTEST_SKIP_WASM64 is set)')
if self.is_browser_test():
return
if self.try_require_node_version(24):
return
v8 = self.get_v8()
if v8:
self.cflags.append('-sENVIRONMENT=shell')
self.js_engines = [v8]
return
self.fail('either d8 or node >= 24 required to run wasm64 tests. Use EMTEST_SKIP_WASM64 to skip')
def try_require_node_version(self, major, minor = 0, revision = 0):
nodejs = self.get_nodejs()
if not nodejs:
self.skipTest('Test requires nodejs to run')
version = shared.get_node_version(nodejs)
if version < (major, minor, revision):
return False
self.js_engines = [nodejs]
return True
def require_simd(self):
if 'EMTEST_SKIP_SIMD' in os.environ:
self.skipTest('test requires node >= 16 or d8 (and EMTEST_SKIP_SIMD is set)')
if self.is_browser_test():
return
if self.try_require_node_version(16):
return
v8 = self.get_v8()
if v8:
self.cflags.append('-sENVIRONMENT=shell')
self.js_engines = [v8]
return
self.fail('either d8 or node >= 16 required to run wasm64 tests. Use EMTEST_SKIP_SIMD to skip')
def require_wasm_legacy_eh(self):
if 'EMTEST_SKIP_WASM_LEGACY_EH' in os.environ:
self.skipTest('test requires node >= 17 or d8 (and EMTEST_SKIP_WASM_LEGACY_EH is set)')
self.set_setting('WASM_LEGACY_EXCEPTIONS')
if self.is_browser_test():
self.check_browser_feature('EMTEST_SKIP_WASM_LEGACY_EH', Feature.WASM_LEGACY_EXCEPTIONS, 'test requires Wasm Legacy EH')
return
if self.try_require_node_version(17):
return
v8 = self.get_v8()
if v8:
self.cflags.append('-sENVIRONMENT=shell')
self.js_engines = [v8]
return
self.fail('either d8 or node >= 17 required to run legacy wasm-eh tests. Use EMTEST_SKIP_WASM_LEGACY_EH to skip')
def require_wasm_eh(self):
if 'EMTEST_SKIP_WASM_EH' in os.environ:
self.skipTest('test requires node v24 or d8 (and EMTEST_SKIP_WASM_EH is set)')
self.set_setting('WASM_LEGACY_EXCEPTIONS', 0)
if self.is_browser_test():
self.check_browser_feature('EMTEST_SKIP_WASM_EH', Feature.WASM_EXCEPTIONS, 'test requires Wasm EH')
return
if self.try_require_node_version(22):
self.node_args.append('--experimental-wasm-exnref')
return
v8 = self.get_v8()
if v8:
self.cflags.append('-sENVIRONMENT=shell')
self.js_engines = [v8]
self.v8_args.append('--experimental-wasm-exnref')
return
self.fail('either d8 or node v24 required to run wasm-eh tests. Use EMTEST_SKIP_WASM_EH to skip')
def require_jspi(self):
if 'EMTEST_SKIP_JSPI' in os.environ:
self.skipTest('skipping JSPI (EMTEST_SKIP_JSPI is set)')
# emcc warns about stack switching being experimental, and we build with
# warnings-as-errors, so disable that warning
self.cflags += ['-Wno-experimental']
self.set_setting('JSPI')
if self.is_wasm2js():
self.skipTest('JSPI is not currently supported for WASM2JS')
if self.get_setting('WASM_ESM_INTEGRATION'):
self.skipTest('WASM_ESM_INTEGRATION is not compatible with JSPI')
if self.is_browser_test():
return
exp_args = ['--experimental-wasm-stack-switching', '--experimental-wasm-type-reflection']
# Support for JSPI came earlier than 22, but the new API changes require v24
if self.try_require_node_version(24):
self.node_args += exp_args
return
v8 = self.get_v8()
if v8:
self.cflags.append('-sENVIRONMENT=shell')
self.js_engines = [v8]
self.v8_args += exp_args
return
self.fail('either d8 or node v24 required to run JSPI tests. Use EMTEST_SKIP_JSPI to skip')
def require_wasm2js(self):
if self.is_wasm64():
self.skipTest('wasm2js is not compatible with MEMORY64')
if self.is_2gb() or self.is_4gb():
self.skipTest('wasm2js does not support over 2gb of memory')
if self.get_setting('WASM_ESM_INTEGRATION'):
self.skipTest('wasm2js is not compatible with WASM_ESM_INTEGRATION')
def setup_nodefs_test(self):
self.require_node()
if self.get_setting('WASMFS'):
# without this the JS setup code in setup_nodefs.js doesn't work
self.set_setting('FORCE_FILESYSTEM')
self.cflags += ['-DNODEFS', '-lnodefs.js', '--pre-js', test_file('setup_nodefs.js'), '-sINCOMING_MODULE_JS_API=onRuntimeInitialized']
def setup_noderawfs_test(self):
self.require_node()
self.cflags += ['-DNODERAWFS']
self.set_setting('NODERAWFS')
def setup_wasmfs_test(self):
self.set_setting('WASMFS')
self.cflags += ['-DWASMFS']
def setup_node_pthreads(self):
self.require_node()
self.cflags += ['-Wno-pthreads-mem-growth', '-pthread']
if self.get_setting('MINIMAL_RUNTIME'):
self.skipTest('node pthreads not yet supported with MINIMAL_RUNTIME')
nodejs = self.get_nodejs()
self.js_engines = [nodejs]
self.node_args += shared.node_pthread_flags(nodejs)
def set_temp_dir(self, temp_dir):
self.temp_dir = temp_dir
self.canonical_temp_dir = get_canonical_temp_dir(self.temp_dir)
# Explicitly set dedicated temporary directory for parallel tests
os.environ['EMCC_TEMP_DIR'] = self.temp_dir
def parse_wasm(self, filename):
wat = self.get_wasm_text(filename)
imports = []
exports = []
funcs = []
for line in wat.splitlines():
line = line.strip()
if line.startswith('(import '):
line = line.strip('()')
parts = line.split()
module = parts[1].strip('"')
name = parts[2].strip('"')
imports.append('%s.%s' % (module, name))
if line.startswith('(export '):
line = line.strip('()')
name = line.split()[1].strip('"')
exports.append(name)
if line.startswith('(func '):
line = line.strip('()')
name = line.split()[1].strip('"')
funcs.append(name)
return imports, exports, funcs
def output_name(self, basename):
suffix = get_output_suffix(self.get_cflags())
return basename + suffix
@classmethod
def setUpClass(cls):
super().setUpClass()
shared.check_sanity(force=True, quiet=True)
def setUp(self):
super().setUp()
self.js_engines = config.JS_ENGINES.copy()
self.settings_mods = {}
self.skip_exec = None
self.flaky = False
self.cflags = ['-Wclosure', '-Werror', '-Wno-limited-postlink-optimizations']
# TODO(https://github.com/emscripten-core/emscripten/issues/11121)
# For historical reasons emcc compiles and links as C++ by default.
# However we want to run our tests in a more strict manner. We can
# remove this if the issue above is ever fixed.
self.set_setting('NO_DEFAULT_TO_CXX')
self.ldflags = []
# Increase the stack trace limit to maximise usefulness of test failure reports.
# Also, include backtrace for all uncuaght exceptions (not just Error).
self.node_args = ['--stack-trace-limit=50', '--trace-uncaught']
self.spidermonkey_args = ['-w']
nodejs = self.get_nodejs()
if nodejs:
node_version = shared.get_node_version(nodejs)
if node_version < (13, 0, 0):
self.node_args.append('--unhandled-rejections=strict')
elif node_version < (15, 0, 0):
# Opt in to node v15 default behaviour:
# https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode
self.node_args.append('--unhandled-rejections=throw')
self.node_args += node_bigint_flags(node_version)
# If the version we are running tests in is lower than the version that
# emcc targets then we need to tell emcc to target that older version.
emcc_min_node_version_str = str(shared.settings.MIN_NODE_VERSION)
emcc_min_node_version = (
int(emcc_min_node_version_str[0:2]),
int(emcc_min_node_version_str[2:4]),
int(emcc_min_node_version_str[4:6]),
)
if node_version < emcc_min_node_version:
self.cflags += building.get_emcc_node_flags(node_version)
self.cflags.append('-Wno-transpile')
# This allows much of the test suite to be run on older versions of node that don't
# support wasm bigint integration
if node_version[0] < feature_matrix.min_browser_versions[feature_matrix.Feature.JS_BIGINT_INTEGRATION]['node'] / 10000:
self.cflags.append('-sWASM_BIGINT=0')
self.v8_args = ['--wasm-staging']
self.env = {}
self.temp_files_before_run = []
self.required_engine = None
self.wasm_engines = config.WASM_ENGINES.copy()
self.use_all_engines = EMTEST_ALL_ENGINES
if self.get_current_js_engine() != config.NODE_JS_TEST:
# If our primary JS engine is something other than node then enable
# shell support.
default_envs = 'web,webview,worker,node'
self.set_setting('ENVIRONMENT', default_envs + ',shell')
if EMTEST_DETECT_TEMPFILE_LEAKS:
for root, dirnames, filenames in os.walk(self.temp_dir):
for dirname in dirnames:
self.temp_files_before_run.append(os.path.normpath(os.path.join(root, dirname)))
for filename in filenames:
self.temp_files_before_run.append(os.path.normpath(os.path.join(root, filename)))
if self.runningInParallel():
self.working_dir = tempfile.mkdtemp(prefix='emscripten_test_' + self.__class__.__name__ + '_', dir=self.temp_dir)
else:
self.working_dir = path_from_root('out/test')
if os.path.exists(self.working_dir):
if EMTEST_SAVE_DIR == 2:
print('Not clearing existing test directory')
else:
logger.debug('Clearing existing test directory: %s', self.working_dir)
# Even when --save-dir is used we still try to start with an empty directory as many tests
# expect this. --no-clean can be used to keep the old contents for the new test
# run. This can be useful when iterating on a given test with extra files you want to keep
# around in the output directory.
force_delete_contents(self.working_dir)
else:
logger.debug('Creating new test output directory: %s', self.working_dir)
ensure_dir(self.working_dir)
utils.write_file(LAST_TEST, self.id() + '\n')
os.chdir(self.working_dir)
def runningInParallel(self):
return getattr(self, 'is_parallel', False)
def tearDown(self):
if self.runningInParallel() and not EMTEST_SAVE_DIR:
# rmtree() fails on Windows if the current working directory is inside the tree.
os.chdir(os.path.dirname(self.get_dir()))
force_delete_dir(self.get_dir())
if EMTEST_DETECT_TEMPFILE_LEAKS:
temp_files_after_run = []
for root, dirnames, filenames in os.walk(self.temp_dir):
for dirname in dirnames:
temp_files_after_run.append(os.path.normpath(os.path.join(root, dirname)))
for filename in filenames:
temp_files_after_run.append(os.path.normpath(os.path.join(root, filename)))
# Our leak detection will pick up *any* new temp files in the temp dir.
# They may not be due to us, but e.g. the browser when running browser
# tests. Until we figure out a proper solution, ignore some temp file
# names that we see on our CI infrastructure.
ignorable_file_prefixes = [
'/tmp/tmpaddon',
'/tmp/circleci-no-output-timeout',
'/tmp/wasmer',
]
left_over_files = set(temp_files_after_run) - set(self.temp_files_before_run)
left_over_files = [f for f in left_over_files if not any(f.startswith(p) for p in ignorable_file_prefixes)]
if left_over_files:
errlog(f'ERROR: After running test, there are {len(left_over_files)} new temporary files/directories left behind:')
for f in left_over_files:
errlog('leaked file: ', f)
self.fail(f'Test leaked {len(left_over_files)} temporary files!')
def get_setting(self, key, default=None):
return self.settings_mods.get(key, default)
def set_setting(self, key, value=1):
if value is None:
self.clear_setting(key)
if type(value) is bool:
value = int(value)
self.settings_mods[key] = value
def has_changed_setting(self, key):
return key in self.settings_mods
def clear_setting(self, key):
self.settings_mods.pop(key, None)
def serialize_settings(self, compile_only=False):
ret = []
for key, value in self.settings_mods.items():
if compile_only and key not in COMPILE_TIME_SETTINGS:
continue
if value == 1:
ret.append(f'-s{key}')
elif type(value) is list:
ret.append(f'-s{key}={",".join(value)}')
else:
ret.append(f'-s{key}={value}')
return ret
def get_dir(self):
return self.working_dir
def in_dir(self, *pathelems):
return os.path.join(self.get_dir(), *pathelems)
def add_pre_run(self, code):
assert not self.get_setting('MINIMAL_RUNTIME')
create_file('prerun.js', 'Module.preRun = function() { %s }\n' % code)
self.cflags += ['--pre-js', 'prerun.js', '-sINCOMING_MODULE_JS_API=preRun']
def add_post_run(self, code):
assert not self.get_setting('MINIMAL_RUNTIME')
create_file('postrun.js', 'Module.postRun = function() { %s }\n' % code)
self.cflags += ['--pre-js', 'postrun.js', '-sINCOMING_MODULE_JS_API=postRun']
def add_on_exit(self, code):
assert not self.get_setting('MINIMAL_RUNTIME')
create_file('onexit.js', 'Module.onExit = function() { %s }\n' % code)
self.cflags += ['--pre-js', 'onexit.js', '-sINCOMING_MODULE_JS_API=onExit']
# returns the full list of arguments to pass to emcc
# param @main_file whether this is the main file of the test. some arguments
# (like --pre-js) do not need to be passed when building
# libraries, for example
def get_cflags(self, main_file=False, compile_only=False, asm_only=False):
def is_ldflag(f):
return f.startswith('-l') or any(f.startswith(s) for s in ['-sEXPORT_ES6', '-sGL_TESTING', '-sPROXY_TO_PTHREAD', '-sENVIRONMENT=', '--pre-js=', '--post-js=', '-sPTHREAD_POOL_SIZE='])
args = self.serialize_settings(compile_only or asm_only) + self.cflags
if asm_only:
args = [a for a in args if not a.startswith('-O')]
if compile_only or asm_only:
args = [a for a in args if not is_ldflag(a)]
else:
args += self.ldflags
if not main_file:
for i, arg in enumerate(args):
if arg in ('--pre-js', '--post-js'):
args[i] = None
args[i + 1] = None
args = [arg for arg in args if arg is not None]
return args
def verify_es5(self, filename):
es_check = shared.get_npm_cmd('es-check')
# use --quiet once its available
# See: https://github.com/dollarshaveclub/es-check/pull/126/
es_check_env = os.environ.copy()
# Use NODE_JS here (the version of node that the compiler uses) rather then NODE_JS_TEST (the
# version of node being used to run the tests) since we only care about having something that
# can run the es-check tool.
es_check_env['PATH'] = os.path.dirname(config.NODE_JS[0]) + os.pathsep + es_check_env['PATH']
inputfile = os.path.abspath(filename)
# For some reason es-check requires unix paths, even on windows
if WINDOWS:
inputfile = utils.normalize_path(inputfile)
try:
# es-check prints the details of the errors to stdout, but it also prints
# stuff in the case there are no errors:
# ES-Check: there were no ES version matching errors!
# pipe stdout and stderr so that we can choose if/when to print this
# output and avoid spamming stdout when tests are successful.
utils.run_process(es_check + ['es5', inputfile], stdout=PIPE, stderr=STDOUT, env=es_check_env)
except subprocess.CalledProcessError as e:
print(e.stdout)
self.fail('es-check failed to verify ES5 output compliance')
# Build JavaScript code from source code
def build(self, filename, libraries=None, includes=None, force_c=False, cflags=None, output_basename=None, output_suffix=None):
filename = maybe_test_file(filename)
compiler = [compiler_for(filename, force_c)]
if force_c:
assert utils.suffix(filename) != '.c', 'force_c is not needed for source files ending in .c'
compiler.append('-xc')
all_cflags = self.get_cflags(main_file=True)
if cflags:
all_cflags += cflags
if not output_suffix:
output_suffix = get_output_suffix(all_cflags)
if output_basename:
output = output_basename + output_suffix
else:
output = utils.unsuffixed_basename(filename) + output_suffix
cmd = compiler + [str(filename), '-o', output] + all_cflags
if libraries:
cmd += libraries
if includes:
cmd += ['-I' + str(include) for include in includes]
self.run_process(cmd, stderr=self.stderr_redirect if not DEBUG else None)
self.assertExists(output)
if output_suffix in ('.js', '.mjs'):
# Make sure we produced correct line endings
self.assertEqual(line_endings.check_line_endings(output), 0)
return output
def get_func(self, src, name):
start = src.index('function ' + name + '(')
t = start
n = 0
while True:
if src[t] == '{':
n += 1
elif src[t] == '}':
n -= 1
if n == 0:
return src[start:t + 1]
t += 1
assert t < len(src)
def get_wasm_text(self, wasm_binary):
return self.run_process([WASM_DIS, wasm_binary], stdout=PIPE).stdout
def is_exported_in_wasm(self, name, wasm):
wat = self.get_wasm_text(wasm)
return ('(export "%s"' % name) in wat
def measure_wasm_code_lines(self, wasm):
wat_lines = self.get_wasm_text(wasm).splitlines()
non_data_lines = [line for line in wat_lines if '(data ' not in line]
return len(non_data_lines)
def clean_js_output(self, output):
"""Cleaup the JS output prior to running verification steps on it.
Due to minification, when we get a crash report from JS it can sometimes
contains the entire program in the output (since the entire program is
on a single line). In this case we can sometimes get false positives
when checking for strings in the output. To avoid these false positives
and the make the output easier to read in such cases we attempt to remove
such lines from the JS output.
"""
lines = output.splitlines()
long_lines = []
def cleanup(line):
if len(line) > 2048 and line.startswith('var Module=typeof Module!="undefined"'):
long_lines.append(line)
line = '<REPLACED ENTIRE PROGRAM ON SINGLE LINE>'
return line
lines = [cleanup(l) for l in lines]
if not long_lines:
# No long lines found just return the unmodified output
return output
# Sanity check that we only a single long line.
assert len(long_lines) == 1
return '\n'.join(lines)
def get_current_js_engine(self):
"""Return the default JS engine to run tests under"""
return self.js_engines[0]
def get_engine_with_args(self, engine=None):
if not engine:
engine = self.get_current_js_engine()
# Make a copy of the engine command before we modify/extend it.
engine = list(engine)
if engine == config.NODE_JS_TEST:
engine += self.node_args
elif engine == config.V8_ENGINE:
engine += self.v8_args
elif engine == config.SPIDERMONKEY_ENGINE:
engine += self.spidermonkey_args
return engine
def run_js(self, filename, engine=None, args=None,
assert_returncode=0,
interleaved_output=True,
input=None):
# use files, as PIPE can get too full and hang us
stdout_file = self.in_dir('stdout')
stderr_file = None
if interleaved_output:
stderr = STDOUT
else:
stderr_file = self.in_dir('stderr')
stderr = open(stderr_file, 'w')
stdout = open(stdout_file, 'w')
error = None
timeout_error = None
engine = self.get_engine_with_args(engine)
try:
jsrun.run_js(filename, engine, args,
stdout=stdout,
stderr=stderr,
assert_returncode=assert_returncode,
input=input)
except subprocess.TimeoutExpired as e:
timeout_error = e
except subprocess.CalledProcessError as e:
error = e
finally:
stdout.close()
if stderr != STDOUT:
stderr.close()
ret = read_file(stdout_file)
if not interleaved_output:
ret += read_file(stderr_file)
if assert_returncode != 0:
ret = self.clean_js_output(ret)
if error or timeout_error or EMTEST_VERBOSE:
print('-- begin program output --')
print(limit_size(read_file(stdout_file)), end='')
print('-- end program output --')
if not interleaved_output:
print('-- begin program stderr --')
print(limit_size(read_file(stderr_file)), end='')
print('-- end program stderr --')
if timeout_error:
raise timeout_error
if error:
ret = limit_size(ret)
if assert_returncode == NON_ZERO:
self.fail('JS subprocess unexpectedly succeeded (%s): Output:\n%s' % (error.cmd, ret))
else:
self.fail('JS subprocess failed (%s): %s (expected=%s). Output:\n%s' % (error.cmd, error.returncode, assert_returncode, ret))
return ret
def assertExists(self, filename, msg=None):
if not msg:
msg = f'Expected file not found: {filename}'
self.assertTrue(os.path.exists(filename), msg)
def assertNotExists(self, filename, msg=None):
if not msg:
msg = 'Unexpected file exists: ' + filename
self.assertFalse(os.path.exists(filename), msg)
# Tests that the given two paths are identical, modulo path delimiters. E.g. "C:/foo" is equal to "C:\foo".
def assertPathsIdentical(self, path1, path2):
path1 = utils.normalize_path(path1)
path2 = utils.normalize_path(path2)