Skip to content

Commit

Permalink
emterpreter: explicit error message on emterpreter stack overflow (#6754
Browse files Browse the repository at this point in the history
) (#6774)
  • Loading branch information
Beuc authored and kripken committed Jul 5, 2018
1 parent c39a45d commit 9579837
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions emscripten.py
Expand Up @@ -1224,6 +1224,8 @@ def create_basic_funcs(function_table_sigs):
basic_funcs += ['abortOnCannotGrowMemory']
if shared.Settings.STACK_OVERFLOW_CHECK:
basic_funcs += ['abortStackOverflow']
if shared.Settings.EMTERPRETIFY:
basic_funcs += ['abortStackOverflowEmterpreter']
if shared.Settings.SAFE_HEAP:
if asm_safe_heap():
basic_funcs += ['segfault', 'alignfault', 'ftfault']
Expand Down
6 changes: 6 additions & 0 deletions src/preamble.js
Expand Up @@ -938,6 +938,12 @@ function abortStackOverflow(allocSize) {
}
#endif

#if EMTERPRETIFY
function abortStackOverflowEmterpreter() {
abort("Emterpreter stack overflow! Decrease the recursion level or increase EMT_STACK_MAX in tools/emterpretify.py (current value " + EMT_STACK_MAX + ").");
}
#endif

#if ABORTING_MALLOC
function abortOnCannotGrowMemory() {
#if WASM
Expand Down
10 changes: 6 additions & 4 deletions tools/emterpretify.py
Expand Up @@ -479,8 +479,9 @@ def handle_async_post_call():

CASES[ROPCODES['INTCALL']] = '''
lz = HEAPU8[(HEAP32[pc + 4 >> 2] | 0) + 1 | 0] | 0; // FUNC inst, see definition above; we read params here
ly = 0;
assert(((EMTSTACKTOP + 8|0) <= (EMT_STACK_MAX|0))|0); // for return value
ly = 0;''' + ('''
if (((EMTSTACKTOP + 8|0) > (EMT_STACK_MAX|0))|0) // for return value
abortStackOverflowEmterpreter(); ''' if ASSERTIONS else '') + '''
%s
%s
while ((ly|0) < (lz|0)) {
Expand Down Expand Up @@ -701,8 +702,9 @@ def process(code):
'' if not ASYNC else 'HEAP32[EMTSTACKTOP>>2] = pc;\n',
push_stacktop(zero),
ROPCODES['FUNC'],
(''' EMTSTACKTOP = EMTSTACKTOP + (lx ''' + (' + 1 ' if ASYNC else '') + '''<< 3) | 0;
assert(((EMTSTACKTOP|0) <= (EMT_STACK_MAX|0))|0);\n''' + (' if ((asyncState|0) != 2) {' if ASYNC else '')) if not zero else '',
(''' EMTSTACKTOP = EMTSTACKTOP + (lx ''' + (' + 1 ' if ASYNC else '') + '''<< 3) | 0;\n''' +
(''' if (((EMTSTACKTOP|0) > (EMT_STACK_MAX|0))|0) abortStackOverflowEmterpreter();\n''' if ASSERTIONS else '') +
(' if ((asyncState|0) != 2) {' if ASYNC else '')) if not zero else '',
' } else { pc = (HEAP32[sp - 4 >> 2] | 0) - 8 | 0; }' if ASYNC else '',
main_loop,
))
Expand Down
4 changes: 3 additions & 1 deletion tools/js-optimizer.js
Expand Up @@ -7574,7 +7574,9 @@ function emterpretify(ast) {
func[3].push(srcToStat('sp = EMTSTACKTOP;'));
var stackBytes = finalLocals*8;
func[3].push(srcToStat('EMTSTACKTOP = EMTSTACKTOP + ' + stackBytes + ' | 0;'));
func[3].push(srcToStat('assert(((EMTSTACKTOP|0) <= (EMT_STACK_MAX|0))|0);'));
if (ASSERTIONS) {
func[3].push(srcToStat('if (((EMTSTACKTOP|0) > (EMT_STACK_MAX|0))|0) abortStackOverflowEmterpreter();'));
}
asmData.vars['x'] = ASM_INT;
func[3].push(srcToStat('while ((x | 0) < ' + stackBytes + ') { HEAP32[sp + x >> 2] = HEAP32[x >> 2] | 0; x = x + 4 | 0; }'));
}
Expand Down

0 comments on commit 9579837

Please sign in to comment.