-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathtest_core.py
9951 lines (8486 loc) · 333 KB
/
test_core.py
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 2013 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 hashlib
import json
import logging
import os
import random
import re
import shutil
import sys
import time
import unittest
from pathlib import Path
from functools import wraps
if __name__ == '__main__':
raise Exception('do not run this file directly; do something like: test/runner')
from tools.shared import PIPE
from tools.shared import EMCC, EMAR, FILE_PACKAGER
from tools.utils import WINDOWS, MACOS, write_file, delete_file
from tools import shared, building, config, webassembly
import common
from common import RunnerCore, path_from_root, requires_native_clang, test_file, create_file
from common import skip_if, needs_dylink, no_windows, no_mac, is_slow_test, parameterized
from common import env_modify, with_env_modify, disabled, flaky, node_pthreads, also_with_wasm_bigint
from common import read_file, read_binary, requires_v8, requires_node, requires_node_canary, compiler_for, crossplatform
from common import with_both_sjlj, also_with_standalone_wasm, can_do_standalone
from common import NON_ZERO, WEBIDL_BINDER, EMBUILDER, PYTHON
import clang_native
# decorators for limiting which modes a test can run in
logger = logging.getLogger("test_core")
def wasm_simd(f):
@wraps(f)
def decorated(self, *args, **kwargs):
self.require_simd()
if self.get_setting('MEMORY64') == 2:
self.skipTest('https://github.com/WebAssembly/binaryen/issues/4638')
if not self.is_wasm():
self.skipTest('wasm2js only supports MVP for now')
if '-O3' in self.emcc_args:
self.skipTest('SIMD tests are too slow with -O3 in the new LLVM pass manager, https://github.com/emscripten-core/emscripten/issues/13427')
self.emcc_args.append('-msimd128')
self.emcc_args.append('-fno-lax-vector-conversions')
self.v8_args.append('--experimental-wasm-simd')
f(self, *args, **kwargs)
return decorated
def wasm_relaxed_simd(f):
def decorated(self):
if self.get_setting('MEMORY64') == 2:
self.skipTest('https://github.com/WebAssembly/binaryen/issues/4638')
# We don't actually run any tests yet, so don't require any engines.
if not self.is_wasm():
self.skipTest('wasm2js only supports MVP for now')
self.emcc_args.append('-mrelaxed-simd')
f(self)
return decorated
def needs_non_trapping_float_to_int(f):
def decorated(self):
if not self.is_wasm():
self.skipTest('wasm2js only supports MVP for now')
f(self)
return decorated
# without EMTEST_ALL_ENGINES set we only run tests in a single VM by
# default. in some tests we know that cross-VM differences may happen and
# so are worth testing, and they should be marked with this decorator
def all_engines(f):
def decorated(self):
old = self.use_all_engines
self.use_all_engines = True
self.set_setting('ENVIRONMENT', 'web,node,shell')
try:
f(self)
finally:
self.use_all_engines = old
return decorated
# Tests exception handling / setjmp/longjmp handling in Emscripten EH/SjLj mode
# and if possible, new wasm EH/SjLj mode. This tests two combinations:
# - Emscripten EH + Emscripten SjLj
# - Wasm EH + Wasm SjLj
def with_both_eh_sjlj(f):
assert callable(f)
def metafunc(self, is_native):
if is_native:
# Wasm EH is currently supported only in wasm backend and V8
if not self.is_wasm():
self.skipTest('wasm2js does not support wasm EH/SjLj')
self.require_wasm_eh()
# FIXME Temporarily disabled. Enable this later when the bug is fixed.
if '-fsanitize=address' in self.emcc_args:
self.skipTest('Wasm EH does not work with asan yet')
self.emcc_args.append('-fwasm-exceptions')
self.set_setting('SUPPORT_LONGJMP', 'wasm')
f(self)
else:
self.set_setting('DISABLE_EXCEPTION_CATCHING', 0)
self.set_setting('SUPPORT_LONGJMP', 'emscripten')
# DISABLE_EXCEPTION_CATCHING=0 exports __cxa_can_catch and
# __cxa_is_pointer_type, so if we don't build in C++ mode, wasm-ld will
# error out because libc++abi is not included. See
# https://github.com/emscripten-core/emscripten/pull/14192 for details.
self.set_setting('DEFAULT_TO_CXX')
f(self)
metafunc._parameterize = {'': (False,),
'wasm': (True,)}
return metafunc
def no_wasm2js(note=''):
assert not callable(note)
def decorated(f):
return skip_if(f, 'is_wasm2js', note)
return decorated
def no_wasm64(note=''):
assert not callable(note)
def decorated(f):
return skip_if(f, 'is_wasm64', note)
return decorated
def also_with_noderawfs(func):
assert callable(func)
def metafunc(self, rawfs):
if rawfs:
self.require_node()
self.emcc_args += ['-DNODERAWFS']
self.set_setting('NODERAWFS')
func(self)
metafunc._parameterize = {'': (False,),
'rawfs': (True,)}
return metafunc
def also_with_wasmfs(func):
def decorated(self):
func(self)
if self.get_setting('WASMFS'):
# Nothing more to test.
return
print('wasmfs')
if self.get_setting('STANDALONE_WASM'):
self.skipTest("test currently cannot run both with WASMFS and STANDALONE_WASM")
self.set_setting('WASMFS')
self.emcc_args = self.emcc_args.copy() + ['-DWASMFS']
func(self)
return decorated
# Similar to also_with_wasmfs, but also enables the full JS API
def also_with_wasmfs_js(func):
def decorated(self):
func(self)
print('wasmfs')
if self.get_setting('STANDALONE_WASM'):
self.skipTest("test currently cannot run both with WASMFS and STANDALONE_WASM")
self.set_setting('WASMFS')
self.set_setting('FORCE_FILESYSTEM')
self.emcc_args = self.emcc_args.copy() + ['-DWASMFS']
func(self)
return decorated
def with_asyncify_and_jspi(f):
assert callable(f)
def metafunc(self, jspi):
if jspi:
self.set_setting('ASYNCIFY', 2)
self.require_jspi()
f(self)
else:
self.set_setting('ASYNCIFY')
f(self)
metafunc._parameterize = {'': (False,),
'jspi': (True,)}
return metafunc
def no_optimize(note=''):
assert not callable(note)
def decorator(func):
assert callable(func)
def decorated(self):
if self.is_optimizing():
self.skipTest(note)
func(self)
return decorated
return decorator
def needs_make(note=''):
assert not callable(note)
if WINDOWS:
return unittest.skip('Tool not available on Windows bots (%s)' % note)
return lambda f: f
def no_asan(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if '-fsanitize=address' in self.emcc_args:
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_lsan(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if '-fsanitize=leak' in self.emcc_args:
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_4gb(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if self.get_setting('INITIAL_MEMORY') == '4200mb':
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_2gb(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
# 2200mb is the value used by the core_2gb test mode
if self.get_setting('INITIAL_MEMORY') == '2200mb':
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_ubsan(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if '-fsanitize=undefined' in self.emcc_args:
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_sanitize(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if any(a.startswith('-fsanitize=') for a in self.emcc_args):
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def no_wasmfs(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if self.get_setting('WASMFS'):
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
def make_no_decorator_for_setting(name):
def outer_decorator(note):
assert not callable(note)
def decorator(f):
assert callable(f)
@wraps(f)
def decorated(self, *args, **kwargs):
if (name + '=1') in self.emcc_args or self.get_setting(name):
self.skipTest(note)
f(self, *args, **kwargs)
return decorated
return decorator
return outer_decorator
no_minimal_runtime = make_no_decorator_for_setting('MINIMAL_RUNTIME')
no_safe_heap = make_no_decorator_for_setting('SAFE_HEAP')
def is_sanitizing(args):
return '-fsanitize=' in str(args)
class TestCoreBase(RunnerCore):
def is_wasm2js(self):
return self.get_setting('WASM') == 0
def is_wasm64(self):
return self.get_setting('MEMORY64')
# A simple check whether the compiler arguments cause optimization.
def is_optimizing(self):
return '-O' in str(self.emcc_args) and '-O0' not in self.emcc_args
def should_use_closure(self):
# Don't run closure in all test modes, just a couple, since it slows
# the tests down quite a bit.
required = ('-O2', '-Os')
prohibited = ('-g', '--profiling')
return all(f not in self.emcc_args for f in prohibited) and any(f in self.emcc_args for f in required)
# Use closure in some tests for some additional coverage
def maybe_closure(self):
if '--closure=1' not in self.emcc_args and self.should_use_closure():
self.emcc_args += ['--closure=1']
logger.debug('using closure compiler..')
return True
return False
def assertStartswith(self, output, prefix):
self.assertEqual(prefix, output[:len(prefix)])
def verify_in_strict_mode(self, filename):
js = read_file(filename)
filename += '.strict.js'
write_file(filename, '"use strict";\n' + js)
self.run_js(filename)
def do_core_test(self, testname, **kwargs):
self.do_run_in_out_file_test(Path('core', testname), **kwargs)
def get_bullet_library(self, use_cmake):
if use_cmake:
configure_commands = ['cmake', '.']
configure_args = ['-DBUILD_DEMOS=OFF', '-DBUILD_EXTRAS=OFF', '-DUSE_GLUT=OFF',
'-DCMAKE_CXX_STANDARD=14']
# Depending on whether 'configure' or 'cmake' is used to build, Bullet
# places output files in different directory structures.
generated_libs = [Path('src/BulletDynamics/libBulletDynamics.a'),
Path('src/BulletCollision/libBulletCollision.a'),
Path('src/LinearMath/libLinearMath.a')]
else:
configure_commands = ['sh', './configure']
# Force a nondefault --host= so that the configure script will interpret
# that we are doing cross-compilation
# and skip attempting to run the generated executable with './a.out',
# which would fail since we are building a .js file.
configure_args = ['--disable-shared', '--host=i686-pc-linux-gnu',
'--disable-demos', '--disable-dependency-tracking']
generated_libs = [Path('src/.libs/libBulletDynamics.a'),
Path('src/.libs/libBulletCollision.a'),
Path('src/.libs/libLinearMath.a')]
return self.get_library('third_party/bullet', generated_libs,
configure=configure_commands,
configure_args=configure_args,
cache_name_extra=configure_commands[0])
@also_with_standalone_wasm()
@also_with_wasmfs
def test_hello_world(self):
self.do_core_test('test_hello_world.c')
def test_wasm_synchronous_compilation(self):
self.set_setting('STRICT_JS')
self.set_setting('WASM_ASYNC_COMPILATION', 0)
self.do_core_test('test_hello_world.c')
@also_with_standalone_wasm()
def test_hello_argc(self):
self.do_core_test('test_hello_argc.c', args=['hello', 'world'])
@node_pthreads
def test_hello_argc_pthreads(self):
self.set_setting('PROXY_TO_PTHREAD')
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_hello_argc.c', args=['hello', 'world'])
@also_with_wasmfs
def test_intvars(self):
self.do_core_test('test_intvars.cpp')
@also_with_wasmfs
def test_sintvars(self):
self.do_core_test('test_sintvars.c')
def test_int53(self):
if common.EMTEST_REBASELINE:
self.run_process([EMCC, test_file('core/test_int53.c'), '-o', 'a.js', '-DGENERATE_ANSWERS'] + self.emcc_args)
ret = self.run_process(config.NODE_JS + ['a.js'], stdout=PIPE).stdout
write_file(test_file('core/test_int53.out'), ret)
else:
self.do_core_test('test_int53.c', interleaved_output=False)
def test_int53_convertI32PairToI53Checked(self):
if common.EMTEST_REBASELINE:
self.run_process([EMCC, test_file('core/test_convertI32PairToI53Checked.cpp'), '-o', 'a.js', '-DGENERATE_ANSWERS'] + self.emcc_args)
ret = self.run_process(config.NODE_JS + ['a.js'], stdout=PIPE).stdout
write_file(test_file('core/test_convertI32PairToI53Checked.out'), ret)
else:
self.do_core_test('test_convertI32PairToI53Checked.cpp', interleaved_output=False)
def test_i64(self):
self.do_core_test('test_i64.c')
def test_i64_2(self):
self.do_core_test('test_i64_2.cpp')
def test_i64_3(self):
self.do_core_test('test_i64_3.cpp')
def test_i64_4(self):
# stuff that also needs sign corrections
self.do_core_test('test_i64_4.c')
def test_i64_b(self):
self.do_core_test('test_i64_b.cpp')
def test_i64_cmp(self):
self.do_core_test('test_i64_cmp.cpp')
def test_i64_cmp2(self):
self.do_core_test('test_i64_cmp2.c')
def test_i64_double(self):
self.do_core_test('test_i64_double.cpp')
def test_i64_umul(self):
self.do_core_test('test_i64_umul.c')
@also_with_standalone_wasm()
@no_ubsan('contains UB')
def test_i64_precise(self):
self.do_core_test('test_i64_precise.c')
def test_i64_precise_needed(self):
self.do_core_test('test_i64_precise_needed.c')
def test_i64_llabs(self):
self.do_core_test('test_i64_llabs.c')
def test_i64_zextneg(self):
self.do_core_test('test_i64_zextneg.c')
def test_i64_7z(self):
# needs to flush stdio streams
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_i64_7z.c', args=['hallo'])
def test_i64_i16(self):
self.do_core_test('test_i64_i16.c')
def test_i64_qdouble(self):
self.do_core_test('test_i64_qdouble.c')
def test_i64_varargs(self):
self.do_core_test('test_i64_varargs.c', args='waka fleefl asdfasdfasdfasdf'.split())
@no_wasm2js('wasm_bigint')
@requires_node
def test_i64_invoke_bigint(self):
self.set_setting('WASM_BIGINT')
self.emcc_args += ['-fexceptions']
self.node_args += shared.node_bigint_flags()
self.do_core_test('test_i64_invoke_bigint.cpp')
def test_vararg_copy(self):
self.do_run_in_out_file_test('va_arg/test_va_copy.c')
def test_llvm_fabs(self):
self.do_core_test('test_llvm_fabs.c')
def test_double_varargs(self):
self.do_core_test('test_double_varargs.c')
def test_trivial_struct_varargs(self):
self.do_core_test('test_trivial_struct_varargs.c')
def test_struct_varargs(self):
self.do_core_test('test_struct_varargs.c')
def test_zero_struct_varargs(self):
self.do_core_test('test_zero_struct_varargs.c')
def zzztest_nested_struct_varargs(self):
self.do_core_test('test_nested_struct_varargs.c')
def test_i32_mul_precise(self):
self.do_core_test('test_i32_mul_precise.c')
def test_i16_emcc_intrinsic(self):
# needs to flush stdio streams
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_i16_emcc_intrinsic.c')
def test_double_i64_conversion(self):
self.do_core_test('test_double_i64_conversion.c')
def test_float32_precise(self):
self.do_core_test('test_float32_precise.c')
def test_negative_zero(self):
self.do_core_test('test_negative_zero.c')
def test_literal_negative_zero(self):
self.do_core_test('test_literal_negative_zero.c')
@also_with_standalone_wasm()
def test_bswap64(self):
self.do_core_test('test_bswap64.cpp')
def test_sha1(self):
self.do_runf(test_file('sha1.c'), 'SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6')
def test_core_types(self):
self.do_runf(test_file('core/test_core_types.c'))
def test_cube2md5(self):
self.emcc_args += ['--embed-file', 'cube2md5.txt']
shutil.copyfile(test_file('cube2md5.txt'), 'cube2md5.txt')
self.do_run_from_file(test_file('cube2md5.cpp'), test_file('cube2md5.ok'), assert_returncode=NON_ZERO)
@also_with_standalone_wasm()
@needs_make('make')
def test_cube2hash(self):
# A good test of i64 math
self.do_run('// empty file', 'Usage: hashstring <seed>',
libraries=self.get_library('third_party/cube2hash', ['libcube2hash.a'], configure=None),
includes=[test_file('third_party/cube2hash')], assert_returncode=NON_ZERO)
for text, output in [('fleefl', '892BDB6FD3F62E863D63DA55851700FDE3ACF30204798CE9'),
('fleefl2', 'AA2CC5F96FC9D540CA24FDAF1F71E2942753DB83E8A81B61'),
('64bitisslow', '64D8470573635EC354FEE7B7F87C566FCAF1EFB491041670')]:
self.do_run('src.js', 'hash value: ' + output, args=[text], no_build=True)
def test_unaligned(self):
self.skipTest('LLVM marks the reads of s as fully aligned, making this test invalid')
src = r'''
#include <stdio.h>
struct S {
double x;
int y;
};
int main() {
// the 64-bit value here will not be 8-byte aligned
S s0[3] = { {0x12a751f430142, 22}, {0x17a5c85bad144, 98}, {1, 1}};
char buffer[10*sizeof(S)];
int b = int(buffer);
S *s = (S*)(b + 4-b%8);
s[0] = s0[0];
s[1] = s0[1];
s[2] = s0[2];
printf("*%d : %d : %d\n", sizeof(S), ((unsigned long)&s[0]) % 8 != ((unsigned long)&s[1]) % 8,
((unsigned long)&s[1]) - ((unsigned long)&s[0]));
s[0].x++;
s[0].y++;
s[1].x++;
s[1].y++;
printf("%.1f,%d,%.1f,%d\n", s[0].x, s[0].y, s[1].x, s[1].y);
return 0;
}
'''
# TODO: A version of this with int64s as well
self.do_run(src, '*12 : 1 : 12\n328157500735811.0,23,416012775903557.0,99\n')
return # TODO: continue to the next part here
# Test for undefined behavior in C. This is not legitimate code, but does exist
src = r'''
#include <stdio.h>
int main()
{
int x[10];
char *p = (char*)&x[0];
p++;
short *q = (short*)p;
*q = 300;
printf("*%d:%ld*\n", *q, ((long)q)%2);
int *r = (int*)p;
*r = 515559;
printf("*%d*\n", *r);
long long *t = (long long*)p;
*t = 42949672960;
printf("*%lld*\n", *t);
return 0;
}
'''
try:
self.do_run(src, '*300:1*\n*515559*\n*42949672960*\n')
except Exception as e:
assert 'must be aligned' in str(e), e # expected to fail without emulation
def test_align64(self):
src = r'''
#include <stdio.h>
// inspired by poppler
enum Type {
A = 10,
B = 20
};
struct Object {
Type type;
union {
int intg;
double real;
char *name;
};
};
struct Principal {
double x;
Object a;
double y;
};
int main(int argc, char **argv) {
int base = argc-1;
Object o[10];
printf("%zu,%zu\n", sizeof(Object), sizeof(Principal));
printf("%ld,%ld,%ld,%ld\n", (long)&o[base].type - (long)o, (long)&o[base].intg - (long)o, (long)&o[base].real - (long)o, (long)&o[base].name - (long)o);
printf("%ld,%ld,%ld,%ld\n", (long)&o[base+1].type - (long)o, (long)&o[base+1].intg - (long)o, (long)&o[base+1].real - (long)o, (long)&o[base+1].name - (long)o);
Principal p, q;
p.x = p.y = q.x = q.y = 0;
p.a.type = A;
p.a.real = 123.456;
*(&q.a) = p.a;
printf("%.2f,%d,%.2f,%.2f : %.2f,%d,%.2f,%.2f\n", p.x, p.a.type, p.a.real, p.y, q.x, q.a.type, q.a.real, q.y);
return 0;
}
'''
self.do_run(src, '''16,32
0,8,8,8
16,24,24,24
0.00,10,123.46,0.00 : 0.00,10,123.46,0.00
''')
@no_asan('asan errors on corner cases we check')
@no_lsan('lsan errors on corner cases we check')
def test_aligned_alloc(self):
self.do_runf(test_file('test_aligned_alloc.c'), '',
emcc_args=['-Wno-non-power-of-two-alignment'])
def test_unsigned(self):
src = '''
#include <stdio.h>
const signed char cvals[2] = { -1, -2 }; // compiler can store this is a string, so -1 becomes \\FF, and needs re-signing
int main()
{
{
unsigned char x = 200;
printf("*%d*\\n", x);
unsigned char y = -22;
printf("*%d*\\n", y);
}
int varey = 100;
unsigned int MAXEY = -1, MAXEY2 = -77;
printf("*%u,%d,%u*\\n", MAXEY, varey >= MAXEY, MAXEY2); // 100 >= -1? not in unsigned!
int y = cvals[0];
printf("*%d,%d,%d,%d*\\n", cvals[0], cvals[0] < 0, y, y < 0);
y = cvals[1];
printf("*%d,%d,%d,%d*\\n", cvals[1], cvals[1] < 0, y, y < 0);
// zext issue - see mathop in jsifier
unsigned char x8 = -10;
unsigned long hold = 0;
hold += x8;
int y32 = hold+50;
printf("*%lu,%d*\\n", hold, y32);
// Comparisons
x8 = 0;
for (int i = 0; i < 254; i++) x8++; // make it an actual 254 in JS - not a -2
printf("*%d,%d*\\n", x8+1 == 0xff, x8+1 != 0xff); // 0xff may be '-1' in the bitcode
return 0;
}
'''
self.do_run(src, '*4294967295,0,4294967219*\n*-1,1,-1,1*\n*-2,1,-2,1*\n*246,296*\n*1,0*')
self.emcc_args.append('-Wno-constant-conversion')
src = '''
#include <stdio.h>
int main()
{
{
unsigned char x;
unsigned char *y = &x;
*y = -1;
printf("*%d*\\n", x);
}
{
unsigned short x;
unsigned short *y = &x;
*y = -1;
printf("*%d*\\n", x);
}
/*{ // This case is not checked. The hint for unsignedness is just the %u in printf, and we do not analyze that
unsigned int x;
unsigned int *y = &x;
*y = -1;
printf("*%u*\\n", x);
}*/
{
char x;
char *y = &x;
*y = 255;
printf("*%d*\\n", x);
}
{
char x;
char *y = &x;
*y = 65535;
printf("*%d*\\n", x);
}
{
char x;
char *y = &x;
*y = 0xffffffff;
printf("*%d*\\n", x);
}
return 0;
}
'''
self.do_run(src, '*255*\n*65535*\n*-1*\n*-1*\n*-1*')
def test_bitfields(self):
self.do_core_test('test_bitfields.c')
def test_floatvars(self):
self.do_core_test('test_floatvars.cpp')
def test_closebitcasts(self):
self.do_core_test('closebitcasts.c')
def test_fast_math(self):
self.emcc_args += ['-ffast-math']
self.do_core_test('test_fast_math.c', args=['5', '6', '8'])
def test_zerodiv(self):
self.do_core_test('test_zerodiv.c')
def test_zero_multiplication(self):
# needs to flush stdio streams
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_zero_multiplication.c')
def test_isnan(self):
self.do_core_test('test_isnan.c')
def test_globaldoubles(self):
self.do_core_test('test_globaldoubles.c')
def test_math(self):
self.do_core_test('test_math.c')
def test_erf(self):
self.do_core_test('test_erf.c')
def test_math_hyperbolic(self):
self.do_core_test('test_math_hyperbolic.c')
def test_math_lgamma(self):
self.do_run_in_out_file_test('math/lgamma.c', assert_returncode=NON_ZERO)
def test_math_fmodf(self):
self.do_run_in_out_file_test('math/fmodf.c')
def test_frexp(self):
self.do_core_test('test_frexp.c')
def test_rounding(self):
# needs to flush stdio streams
self.set_setting('EXIT_RUNTIME')
self.do_core_test('test_rounding.c')
def test_fcvt(self):
self.do_core_test('test_fcvt.cpp')
def test_llrint(self):
self.do_core_test('test_llrint.c')
def test_getgep(self):
# Generated code includes getelementptr (getelementptr, 0, 1), i.e., GEP as the first param to GEP
self.do_core_test('test_getgep.c')
def test_multiply_defined_symbols(self):
create_file('a1.c', 'int f() { return 1; }')
create_file('a2.c', 'void x() {}')
create_file('b1.c', 'int f() { return 2; }')
create_file('b2.c', 'void y() {}')
create_file('main.c', r'''
#include <stdio.h>
int f();
int main() {
printf("result: %d\n", f());
return 0;
}
''')
self.emcc('a1.c', ['-c'])
self.emcc('a2.c', ['-c'])
self.emcc('b1.c', ['-c'])
self.emcc('b2.c', ['-c'])
self.emcc('main.c', ['-c'])
building.emar('cr', 'liba.a', ['a1.o', 'a2.o'])
building.emar('cr', 'libb.a', ['b1.o', 'b2.o'])
# Add -Wno-deprecated to avoid warning about bitcode linking in the LTO
# version of this test.
self.run_process([EMCC, '-r', '-o', 'all.o', 'main.o', 'liba.a', 'libb.a',
'-Wno-deprecated'] + self.get_emcc_args())
self.emcc('all.o', output_filename='all.js')
self.do_run('all.js', 'result: 1', no_build=True)
def test_if(self):
self.do_core_test('test_if.c')
def test_if_else(self):
self.do_core_test('test_if_else.c')
def test_loop(self):
self.do_core_test('test_loop.c')
def test_stack(self):
self.set_setting('INLINING_LIMIT')
# some extra coverage in all test suites for stack checks
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.do_core_test('test_stack.c')
def test_stack_align(self):
src = test_file('core/test_stack_align.cpp')
def test():
self.do_runf(src, ['''align 4: 0
align 8: 0
align 16: 0
align 32: 0
base align: 0, 0, 0, 0'''])
test()
@no_asan('stack size is too low for asan to work properly')
def test_stack_placement(self):
self.set_setting('STACK_SIZE', 1024)
self.do_core_test('test_stack_placement.c')
self.set_setting('GLOBAL_BASE', '100kb')
self.do_core_test('test_stack_placement.c')
@no_sanitize('sanitizers do not yet support dynamic linking')
@no_wasm2js('MAIN_MODULE support')
@needs_dylink
def test_stack_placement_pic(self):
self.set_setting('STACK_SIZE', 1024)
self.set_setting('MAIN_MODULE')
self.do_core_test('test_stack_placement.c')
self.set_setting('GLOBAL_BASE', '100kb')
self.do_core_test('test_stack_placement.c')
def test_strings(self):
self.do_core_test('test_strings.c', args=['wowie', 'too', '74'])
def test_strcmp_uni(self):
self.do_core_test('test_strcmp_uni.c')
def test_strndup(self):
self.do_core_test('test_strndup.c')
def test_errar(self):
self.do_core_test('test_errar.c')
def test_mainenv(self):
self.do_core_test('test_mainenv.c')
def test_funcs(self):
self.do_core_test('test_funcs.c')
def test_structs(self):
self.do_core_test('test_structs.c')
gen_struct_src = '''
#include <stdio.h>
#include <stdlib.h>
#include "emscripten.h"
struct S
{
int x, y;
};
int main()
{
S* a = {{gen_struct}};
a->x = 51; a->y = 62;
printf("*%d,%d*\\n", a->x, a->y);
{{del_struct}}(a);
return 0;
}
'''
def test_mallocstruct(self):
self.do_run(self.gen_struct_src.replace('{{gen_struct}}', '(S*)malloc(sizeof(S))').replace('{{del_struct}}', 'free'), '*51,62*')
@no_asan('ASan does not support custom memory allocators')
@no_lsan('LSan does not support custom memory allocators')
@parameterized({
'normal': [],
'memvalidate': ['-DEMMALLOC_MEMVALIDATE'],
'memvalidate_verbose': ['-DEMMALLOC_MEMVALIDATE', '-DEMMALLOC_VERBOSE', '-DRANDOM_ITERS=130'],
})
def test_emmalloc(self, *args):
if '-DEMMALLOC_VERBOSE' in args and self.is_wasm64():
self.skipTest('EMMALLOC_VERBOSE is not compatible with wasm64')
# in newer clang+llvm, the internal calls to malloc in emmalloc may be optimized under
# the assumption that they are external, so like in system_libs.py where we build
# malloc, we need to disable builtin here too
self.set_setting('MALLOC', 'none')
self.emcc_args += [
'-fno-builtin',
path_from_root('system/lib/libc/sbrk.c'),
path_from_root('system/lib/emmalloc.c')
]
self.emcc_args += args