Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,8 @@ def debug_copy(src, dst):
cmd.append('--global-base=0')
else:
cmd.append('--global-base=%s' % shared.Settings.GLOBAL_BASE)
if shared.Settings.SAFE_STACK:
cmd.append('--check-stack-overflow')
shared.print_compiler_stage(cmd)
stdout = shared.check_call(cmd, stdout=subprocess.PIPE).stdout
if write_source_map:
Expand Down
3 changes: 3 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -5170,6 +5170,9 @@ LibraryManager.library = {
},
// =======================================================================

__handle_stack_overflow: function() {
Copy link
Copy Markdown
Collaborator

@juj juj Aug 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__handle_stack_overflow -> __abort_stack_overflow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if __abort_stack_overflow is really a good name for the handler function. Binaryen gives the runtime flexibility to do whatever in face of stack overflow. Hypothetically, a runtime could implement a mechanism similar to sigaltstack(2) to handle stack overflows.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, now that I look closer at this - does the wasm backend directly generate a call to this function? If so, then the name is very generic. How about __wasm_stack_overflow? Having compiler backend construct a function call with such a name that does not connect to an established API would make the symbol look somewhat floating.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WASM backend does not generate a call to this. wasm-emscripten-finalize in binaryen inserts a call to this function when passed a certain flag. So the __wasm prefix is inappropriate.

abort('stack overflow')
},
};

function autoAddDeps(object, name) {
Expand Down
4 changes: 4 additions & 0 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ Module['callMain'] = function callMain(args) {
var start = Date.now();
#endif

#if SAFE_STACK
Module['___set_stack_limit'](STACK_MAX);
#endif

#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which pthread_create()s a new thread
// that will call the user's real main() for the application.
Expand Down
4 changes: 4 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ var SAFE_HEAP = 0;
// Log out all SAFE_HEAP operations
var SAFE_HEAP_LOG = 0;

// Check each stack pointer decrement on WASM backend to ensure that the stack
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (not worth a PR): wasm isn't an acronym, so just "wasm" is fine.

// does not overflow.
var SAFE_STACK = 0;

// In asm.js mode, we cannot simply add function pointers to function tables, so
// we reserve some slots for them. An alternative to this is to use
// EMULATED_FUNCTION_POINTERS, in which case we don't need to reserve.
Expand Down
3 changes: 3 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ this.onmessage = function(e) {
#if WASM_BACKEND
Module['_emscripten_tls_init']();
#endif
#if SAFE_STACK
Module['___set_stack_limit'](STACK_MAX);
#endif
Comment thread
quantum5 marked this conversation as resolved.
#if STACK_OVERFLOW_CHECK
{{{ makeAsmGlobalAccessInPthread('writeStackCookie') }}}();
#endif
Expand Down
12 changes: 12 additions & 0 deletions tests/core/test_safe_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string.h>

int f(int *b) {
int a[64];
memset(b, 0, 64 * sizeof(int));
return f(a);
}

int main() {
int a[64];
f(a);
}
13 changes: 13 additions & 0 deletions tests/core/test_safe_stack_alloca.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <alloca.h>
#include <string.h>
#include <stdlib.h>

int f(int *ptr) {
for (int i = 0; i < 16384; ++i)
ptr[i] = rand();
return ptr[16383];
}

int main() {
return f((int*) alloca(65536));
}
5 changes: 5 additions & 0 deletions tests/pthread/test_safe_stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Module['onAbort'] = function (what) {
if (what == 'stack overflow') {
reportResultToServer(1);
}
}
1 change: 1 addition & 0 deletions tests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def create_test_file(name, contents, binary=False):
'benchmark',
'asan',
'lsan',
'wasm2ss',
]

test_index = 0
Expand Down
5 changes: 5 additions & 0 deletions tests/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3902,6 +3902,11 @@ def test_pthread_tls(self):
def test_pthread_tls_main(self):
self.btest(path_from_root('tests', 'pthread', 'test_pthread_tls_main.cpp'), expected='1337', args=['-s', 'USE_PTHREADS', '-std=c++11'])

@no_fastcomp('-s SAFE_STACK is only supported on WASM backend')
@requires_threads
def test_pthread_safe_stack(self):
self.btest(path_from_root('tests', 'core', 'test_safe_stack.c'), expected='1', args=['-s', 'USE_PTHREADS', '-s', 'PROXY_TO_PTHREAD', '-s', 'SAFE_STACK', '-s', 'DEFAULT_PTHREAD_STACK_SIZE=64KB', '--pre-js', path_from_root('tests', 'pthread', 'test_safe_stack.js')])

@parameterized({
'leak': ['test_pthread_lsan_leak', ['-g4']],
'no_leak': ['test_pthread_lsan_no_leak'],
Expand Down
41 changes: 41 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8039,6 +8039,46 @@ def test_asan_js_stack_op(self):
self.do_run(open(path_from_root('tests', 'core', 'test_asan_js_stack_op.c')).read(),
basename='src.c', expected_output='Hello, World!')

@no_fastcomp('SAFE_STACK not supported on fastcomp')
def test_safe_stack(self):
self.set_setting('SAFE_STACK', 1)
self.set_setting('TOTAL_STACK', 65536)
self.do_run(open(path_from_root('tests', 'core', 'test_safe_stack.c')).read(),
expected_output=['abort(stack overflow)', '__handle_stack_overflow'])

@no_fastcomp('SAFE_STACK not supported on fastcomp')
def test_safe_stack_alloca(self):
self.set_setting('SAFE_STACK', 1)
self.set_setting('TOTAL_STACK', 65536)
self.do_run(open(path_from_root('tests', 'core', 'test_safe_stack_alloca.c')).read(),
expected_output=['abort(stack overflow)', '__handle_stack_overflow'])

@needs_dlfcn
@no_fastcomp('SAFE_STACK not supported on fastcomp')
def test_safe_stack_dylink(self):
self.set_setting('SAFE_STACK', 1)
self.set_setting('TOTAL_STACK', 65536)
self.dylink_test(r'''
#include <stdio.h>
extern void sidey();
int main() {
sidey();
}
''', '''
#include <string.h>

int f(int *b) {
int a[64];
memset(b, 0, 2048 * sizeof(int));
return f(a);
}

void sidey() {
int a[2048];
f(a);
}
''', ['abort(stack overflow)', '__handle_stack_overflow'])


# Generate tests for everything
def make_run(name, emcc_args, settings=None, env=None):
Expand Down Expand Up @@ -8128,6 +8168,7 @@ def setUp(self):

# wasm
wasm2s = make_run('wasm2s', emcc_args=['-O2'], settings={'SAFE_HEAP': 1})
wasm2ss = make_run('wasm2ss', emcc_args=['-O2'], settings={'SAFE_STACK': 1})

# emterpreter
asmi = make_run('asmi', emcc_args=[], settings={'ASM_JS': 2, 'EMTERPRETIFY': 1, 'WASM': 0})
Expand Down