Skip to content

Commit 6b30209

Browse files
authored
Remove assert from being imported into asm.js/wasm (#8000)
We should not call out into JS to do a check, as that is silly and slow. The users were the async code - rewrote those to do the checks in asm/wasm.
1 parent 3e23711 commit 6b30209

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

emscripten.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ def setup_function_pointers(function_table_sigs):
14501450

14511451

14521452
def create_basic_funcs(function_table_sigs, invoke_function_names):
1453-
basic_funcs = ['abort', 'assert', 'setTempRet0', 'getTempRet0']
1453+
basic_funcs = ['abort', 'setTempRet0', 'getTempRet0']
14541454
if shared.Settings.STACK_OVERFLOW_CHECK:
14551455
basic_funcs += ['abortStackOverflow']
14561456
if shared.Settings.EMTERPRETIFY:
@@ -2160,7 +2160,7 @@ def create_em_js(forwarded_json, metadata):
21602160

21612161

21622162
def create_sending_wasm(invoke_funcs, jscall_sigs, forwarded_json, metadata):
2163-
basic_funcs = ['assert']
2163+
basic_funcs = []
21642164
if shared.Settings.SAFE_HEAP:
21652165
basic_funcs += ['segfault', 'alignfault']
21662166

src/library_async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mergeInto(LibraryManager.library, {
9898
// just undo a recent emscripten_alloc_async_context
9999
ctx = ctx|0;
100100
#if ASSERTIONS
101-
assert((((___async_cur_frame + 8)|0) == (ctx|0))|0);
101+
if ((((___async_cur_frame + 8)|0) != (ctx|0))|0) abort();
102102
#endif
103103
stackRestore(___async_cur_frame | 0);
104104
___async_cur_frame = {{{ makeGetValueAsm('___async_cur_frame', 0, 'i32') }}};

tests/test_other.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7870,70 +7870,70 @@ def test(filename, expectations, size_slack):
78707870

78717871
print('test on hello world')
78727872
test(path_from_root('tests', 'hello_world.cpp'), [
7873-
([], 21, ['assert'], ['waka'], 46505, 22, 15, 58), # noqa
7874-
(['-O1'], 16, ['assert'], ['waka'], 12630, 14, 13, 30), # noqa
7875-
(['-O2'], 16, ['assert'], ['waka'], 12616, 14, 13, 30), # noqa
7876-
(['-O3'], 6, [], [], 2690, 9, 2, 21), # noqa; in -O3, -Os and -Oz we metadce
7877-
(['-Os'], 6, [], [], 2690, 9, 2, 21), # noqa
7878-
(['-Oz'], 6, [], [], 2690, 9, 2, 21), # noqa
7873+
([], 20, ['abort'], ['waka'], 46505, 22, 15, 58), # noqa
7874+
(['-O1'], 15, ['abort'], ['waka'], 12630, 14, 13, 30), # noqa
7875+
(['-O2'], 15, ['abort'], ['waka'], 12616, 14, 13, 30), # noqa
7876+
(['-O3'], 6, [], [], 2690, 9, 2, 21), # noqa; in -O3, -Os and -Oz we metadce
7877+
(['-Os'], 6, [], [], 2690, 9, 2, 21), # noqa
7878+
(['-Oz'], 6, [], [], 2690, 9, 2, 21), # noqa
78797879
# finally, check what happens when we export nothing. wasm should be almost empty
78807880
(['-Os', '-s', 'EXPORTED_FUNCTIONS=[]'],
7881-
0, [], [], 8, 0, 0, 0), # noqa; totally empty!
7881+
0, [], [], 8, 0, 0, 0), # noqa; totally empty!
78827882
# we don't metadce with linkable code! other modules may want stuff
78837883
(['-O3', '-s', 'MAIN_MODULE=1'],
7884-
1557, [], [], 226057, 28, 75, None), # noqa; don't compare the # of functions in a main module, which changes a lot
7884+
1556, [], [], 226057, 28, 75, None), # noqa; don't compare the # of functions in a main module, which changes a lot
78857885
], size_slack) # noqa
78867886

78877887
print('test on a minimal pure computational thing')
78887888
test('minimal.c', [
7889-
([], 21, ['assert'], ['waka'], 22712, 22, 14, 27), # noqa
7890-
(['-O1'], 11, ['assert'], ['waka'], 10450, 7, 11, 11), # noqa
7891-
(['-O2'], 11, ['assert'], ['waka'], 10440, 7, 11, 11), # noqa
7889+
([], 20, ['abort'], ['waka'], 22712, 22, 14, 27), # noqa
7890+
(['-O1'], 10, ['abort'], ['waka'], 10450, 7, 11, 11), # noqa
7891+
(['-O2'], 10, ['abort'], ['waka'], 10440, 7, 11, 11), # noqa
78927892
# in -O3, -Os and -Oz we metadce, and they shrink it down to the minimal output we want
7893-
(['-O3'], 0, [], [], 55, 0, 1, 1), # noqa
7894-
(['-Os'], 0, [], [], 55, 0, 1, 1), # noqa
7895-
(['-Oz'], 0, [], [], 55, 0, 1, 1), # noqa
7893+
(['-O3'], 0, [], [], 55, 0, 1, 1), # noqa
7894+
(['-Os'], 0, [], [], 55, 0, 1, 1), # noqa
7895+
(['-Oz'], 0, [], [], 55, 0, 1, 1), # noqa
78967896
], size_slack)
78977897

78987898
print('test on libc++: see effects of emulated function pointers')
78997899
test(path_from_root('tests', 'hello_libcxx.cpp'), [
7900-
(['-O2'], 35, ['assert'], ['waka'], 196709, 28, 39, 659), # noqa
7900+
(['-O2'], 34, ['abort'], ['waka'], 196709, 28, 39, 659), # noqa
79017901
(['-O2', '-s', 'EMULATED_FUNCTION_POINTERS=1'],
7902-
35, ['assert'], ['waka'], 196709, 28, 20, 620), # noqa
7902+
34, ['abort'], ['waka'], 196709, 28, 20, 620), # noqa
79037903
], size_slack) # noqa
79047904
else:
79057905
# wasm-backend
79067906
size_slack = 0.5 # for now, don't look carefully at code size
79077907

79087908
print('test on hello world')
79097909
test(path_from_root('tests', 'hello_world.cpp'), [
7910-
([], 17, ['assert'], ['waka'], 33171, 10, 15, 70), # noqa
7911-
(['-O1'], 15, ['assert'], ['waka'], 14720, 8, 14, 29), # noqa
7912-
(['-O2'], 15, ['assert'], ['waka'], 14569, 8, 14, 24), # noqa
7913-
(['-O3'], 5, [], [], 3395, 7, 3, 14), # noqa; in -O3, -Os and -Oz we metadce
7914-
(['-Os'], 5, [], [], 3350, 7, 3, 15), # noqa
7915-
(['-Oz'], 5, [], [], 3309, 7, 2, 14), # noqa
7910+
([], 16, [], ['waka'], 33171, 10, 15, 70), # noqa
7911+
(['-O1'], 14, [], ['waka'], 14720, 8, 14, 29), # noqa
7912+
(['-O2'], 14, [], ['waka'], 14569, 8, 14, 24), # noqa
7913+
(['-O3'], 5, [], [], 3395, 7, 3, 14), # noqa; in -O3, -Os and -Oz we metadce
7914+
(['-Os'], 5, [], [], 3350, 7, 3, 15), # noqa
7915+
(['-Oz'], 5, [], [], 3309, 7, 2, 14), # noqa
79167916
# finally, check what happens when we export nothing. wasm should be almost empty
79177917
(['-Os', '-s', 'EXPORTED_FUNCTIONS=[]'],
7918-
0, [], [], 61, 0, 1, 1), # noqa
7918+
0, [], [], 61, 0, 1, 1), # noqa
79197919
], size_slack) # noqa
79207920

79217921
print('test on a minimal pure computational thing')
79227922
test('minimal.c', [
7923-
([], 17, ['assert'], ['waka'], 14567, 9, 15, 24), # noqa
7924-
(['-O1'], 10, ['assert'], ['waka'], 11255, 2, 12, 10), # noqa
7925-
(['-O2'], 10, ['assert'], ['waka'], 11255, 2, 12, 10), # noqa
7923+
([], 16, [], ['waka'], 14567, 9, 15, 24), # noqa
7924+
(['-O1'], 9, [], ['waka'], 11255, 2, 12, 10), # noqa
7925+
(['-O2'], 9, [], ['waka'], 11255, 2, 12, 10), # noqa
79267926
# in -O3, -Os and -Oz we metadce, and they shrink it down to the minimal output we want
7927-
(['-O3'], 0, [], [], None, 0, 1, 1), # noqa FIXME see https://github.com/WebAssembly/binaryen/pull/1875
7928-
(['-Os'], 0, [], [], None, 0, 1, 1), # noqa FIXME see https://github.com/WebAssembly/binaryen/pull/1875
7929-
(['-Oz'], 0, [], [], None, 0, 0, 0), # noqa XXX wasm backend ignores EMSCRIPTEN_KEEPALIVE https://github.com/emscripten-core/emscripten/issues/6233
7927+
(['-O3'], 0, [], [], None, 0, 1, 1), # noqa FIXME see https://github.com/WebAssembly/binaryen/pull/1875
7928+
(['-Os'], 0, [], [], None, 0, 1, 1), # noqa FIXME see https://github.com/WebAssembly/binaryen/pull/1875
7929+
(['-Oz'], 0, [], [], None, 0, 0, 0), # noqa XXX wasm backend ignores EMSCRIPTEN_KEEPALIVE https://github.com/emscripten-core/emscripten/issues/6233
79307930
], size_slack)
79317931

79327932
print('test on libc++: see effects of emulated function pointers')
79337933
test(path_from_root('tests', 'hello_libcxx.cpp'), [
7934-
(['-O2'], 40, ['assert'], ['waka'], 348370, 27, 224, 728), # noqa
7934+
(['-O2'], 39, [], ['waka'], 348370, 27, 224, 728), # noqa
79357935
(['-O2', '-s', 'EMULATED_FUNCTION_POINTERS=1'],
7936-
40, ['assert'], ['waka'], 348249, 27, 224, 728), # noqa
7936+
39, [], ['waka'], 348249, 27, 224, 728), # noqa
79377937
], size_slack) # noqa
79387938

79397939
# ensures runtime exports work, even with metadce

tools/emterpretify.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def fix_coercion(value, s):
603603

604604
CASES[ROPCODES['EXTCALL']] = 'switch ((inst>>>16)|0) {\n' + \
605605
'\n'.join([' case %d: {\n%s\n }' % (i, make_target_call(i)) for i in range(global_func_id)]) + \
606-
'\n default: assert(0);' + \
606+
'\n default: emterpAssert(0);' + \
607607
'\n }'
608608

609609
if ROPCODES['GETGLBI'] not in CASES:
@@ -614,7 +614,7 @@ def make_load(i, t):
614614
def make_getglb(suffix, t):
615615
CASES[ROPCODES['GETGLB' + suffix]] = 'switch (ly|0) {\n' + \
616616
'\n'.join([' case %d: {\n%s\n }' % (i, make_load(i, t)) for i in range(global_var_id) if global_var_types[rglobal_vars[i]] == t]) + \
617-
'\n default: assert(0);' + \
617+
'\n default: emterpAssert(0);' + \
618618
'\n }'
619619

620620
make_getglb('I', 'i')
@@ -627,7 +627,7 @@ def make_store(i, t):
627627
def make_setglb(suffix, t):
628628
CASES[ROPCODES['SETGLB' + suffix]] = 'switch ((inst >> 8)&255) {\n' + \
629629
'\n'.join([' case %d: {\n%s\n }' % (i, make_store(i, t)) for i in range(global_var_id) if global_var_types[rglobal_vars[i]] == t]) + \
630-
'\n default: assert(0);' + \
630+
'\n default: emterpAssert(0);' + \
631631
'\n }'
632632

633633
make_setglb('I', 'i')
@@ -638,7 +638,7 @@ def fix_case(case):
638638
return case.replace('PROCEED_WITH_PC_BUMP', 'continue').replace('PROCEED_WITHOUT_PC_BUMP', 'pc = pc - 4 | 0; continue').replace('continue; continue;', 'continue;')
639639

640640
def process(code):
641-
if not ASSERTIONS: code = code.replace(' assert(', ' //assert(')
641+
if not ASSERTIONS: code = code.replace(' emterpAssert(', ' //emterpAssert(')
642642
if zero: code = code.replace('sp + ', '')
643643
return code
644644

@@ -655,7 +655,7 @@ def process(code):
655655
main_loop = main_loop_prefix + r'''
656656
switch (inst&255) {
657657
%s
658-
default: assert(0);
658+
default: emterpAssert(0);
659659
}
660660
''' % ('\n'.join([fix_case(' case %d: %s break;' % (k, CASES[k])) for k in sorted(CASES.keys()) if opcode_used[OPCODES[k]]]))
661661
else:
@@ -671,7 +671,7 @@ def process(code):
671671
}
672672
switch (inst&255) {
673673
%s
674-
default: assert(0);
674+
default: emterpAssert(0);
675675
}
676676
''' % (
677677
' ' + '\n '.join(main_loop_prefix.split('\n')),
@@ -688,7 +688,7 @@ def process(code):
688688
%s
689689
%s
690690
%s
691-
assert(((HEAPU8[pc>>0]>>>0) == %d)|0);
691+
emterpAssert(((HEAPU8[pc>>0]>>>0) == %d)|0);
692692
lx = HEAPU16[pc + 2 >> 1] | 0; // num locals
693693
%s
694694
%s
@@ -698,7 +698,7 @@ def process(code):
698698
while (1) {
699699
%s
700700
}
701-
assert(0);
701+
emterpAssert(0);
702702
}''' % (
703703
'' if not zero else '_z',
704704
'sp = 0, ' if not zero else '',
@@ -987,6 +987,13 @@ def post_process_code(code):
987987

988988
# finalize funcs JS (first line has the marker, add emterpreters right after that)
989989
asm.funcs_js = '\n'.join([lines[0], make_emterpreter(), make_emterpreter(zero=True) if ZERO else '', '\n'.join([line for line in lines[1:] if len(line)])]) + '\n'
990+
if ASSERTIONS:
991+
asm.funcs_js += '''
992+
function emterpAssert(x) {
993+
x = x | 0;
994+
if (!x) abort();
995+
}
996+
'''
990997
lines = None
991998

992999
# set up emterpreter stack top (note we must use malloc if in a shared lib, or other enviroment where static memory is sealed)

0 commit comments

Comments
 (0)