Skip to content

Enable and improve test_stack_overflow_check #12095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 3, 2020
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
4 changes: 0 additions & 4 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ function callMain(args) {
var start = Date.now();
#endif

#if STACK_OVERFLOW_CHECK >= 2
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/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function run() {
emscriptenMemoryProfiler.onPreloadComplete();
#endif

#if STACK_OVERFLOW_CHECK >= 2
___set_stack_limits(STACK_BASE, 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
3 changes: 3 additions & 0 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ function initRuntime() {
assert(!runtimeInitialized);
#endif
runtimeInitialized = true;
#if STACK_OVERFLOW_CHECK >= 2
Module['___set_stack_limits'](STACK_BASE, STACK_MAX);
#endif
{{{ getQuoted('ATINITS') }}}
callRuntimeCallbacks(__ATINIT__);
}
Expand Down
39 changes: 22 additions & 17 deletions tests/stack_overflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@

void __attribute__((noinline)) InteropString(char *staticBuffer)
{
char *string = (char*)EM_ASM_INT({
var str = "hello, this is a string! ";
for(var i = 0; i < 15; ++i)
str = str + str;
var stringOnTheStack = allocate(intArrayFromString(str), 'i8', ALLOC_STACK);
return stringOnTheStack;
});

int stringLength = strlen(string);
printf("Got string: %s\n", string);
printf("Received a string of length %d.\n", stringLength);
strcpy(staticBuffer, string);
char *string = (char*)EM_ASM_INT({
var str = "hello, this is a string! ";
#if ONE_BIG_STRING
// double it until it is bigger than the stack
for (var i = 0; i < 15; ++i) {
str = str + str;
}
allocate(intArrayFromString(str), "i8", ALLOC_STACK);
#else
// allocate as many times as we need to overflow
for (var i = 0; i < 1024 * 1024; i++) {
allocate(intArrayFromString(str), "i8", ALLOC_STACK);
}
abort("we should never get here!");
#endif
});
}

int main()
{
char staticBuffer[512288] = {}; // Make asm.js side consume a large portion of the stack, before bumping the rest with C++<->JS interop.
InteropString(staticBuffer);
int stringLength = strlen(staticBuffer);
printf("Got string: %s\n", staticBuffer);
printf("Received a string of length %d.\n", stringLength);
// Make C side consume a large portion of the stack, before bumping the rest with C++<->JS interop.
char staticBuffer[512288] = {};
InteropString(staticBuffer);
int stringLength = strlen(staticBuffer);
printf("Got string: %s\n", staticBuffer);
printf("Received a string of length %d.\n", stringLength);
}
17 changes: 8 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def decorated(self):

def can_do_standalone(self):
return self.get_setting('WASM') and \
not self.get_setting('SAFE_STACK') and \
self.get_setting('STACK_OVERFLOW_CHECK') < 2 and \
not self.get_setting('MINIMAL_RUNTIME') and \
'-fsanitize=address' not in self.emcc_args

Expand Down Expand Up @@ -786,6 +786,8 @@ def test_loop(self):

def test_stack(self):
self.set_setting('INLINING_LIMIT', 50)
# some extra coverage in all test suites for stack checks
self.set_setting('STACK_OVERFLOW_CHECK', 2)

self.do_run_in_out_file_test('tests', 'core', 'test_stack.c')

Expand Down Expand Up @@ -7810,15 +7812,13 @@ def test_fs_dict_none(self):
self.do_run('int main() { return 0; }', expected)

@sync
@no_wasm_backend("https://github.com/emscripten-core/emscripten/issues/9039")
def test_stack_overflow_check(self):
args = self.emcc_args + ['-s', 'TOTAL_STACK=1048576']

self.emcc_args = args + ['-s', 'STACK_OVERFLOW_CHECK=2', '-s', 'ASSERTIONS=0']
self.do_runf(path_from_root('tests', 'stack_overflow.cpp'), 'Stack overflow! Attempted to allocate', assert_returncode=NON_ZERO)
self.set_setting('TOTAL_STACK', 1048576)
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.do_runf(path_from_root('tests', 'stack_overflow.cpp'), 'stack overflow', assert_returncode=NON_ZERO)

self.emcc_args = args + ['-s', 'ASSERTIONS=1']
self.do_runf(path_from_root('tests', 'stack_overflow.cpp'), 'Stack overflow! Attempted to allocate', assert_returncode=NON_ZERO)
self.emcc_args += ['-DONE_BIG_STRING']
self.do_runf(path_from_root('tests', 'stack_overflow.cpp'), 'stack overflow', assert_returncode=NON_ZERO)

@node_pthreads
def test_binaryen_2170_emscripten_atomic_cas_u8(self):
Expand Down Expand Up @@ -8206,7 +8206,6 @@ def test_safe_stack_alloca(self):
expected_output=['abort(stack overflow)', '__handle_stack_overflow'], assert_returncode=NON_ZERO)

@needs_dlfcn
@unittest.skip('allow binaryen change to roll in')
def test_safe_stack_dylink(self):
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.set_setting('TOTAL_STACK', 65536)
Expand Down