Skip to content

Commit

Permalink
Add support for -fstack-protector
Browse files Browse the repository at this point in the history
Fixes: #9780
  • Loading branch information
sbc100 committed Jul 12, 2024
1 parent 7b466b4 commit 19d0b81
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions system/lib/libc/musl/src/env/__stack_chk_fail.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void __init_ssp(void *entropy)

void __stack_chk_fail(void)
{
#if defined(__EMSCRIPTEN__) && !defined(NDEBUG)
// Report the reason for the crash, at least in debug builds.
// This is the same message that glibc outputs (even in release builds).
emscripten_err("*** stack smashing detected ***");
#endif
a_crash();
}

Expand Down
14 changes: 14 additions & 0 deletions test/other/test_stack_protector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <alloca.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>

char mystring[] = "Hello world";

int main() {
char buf[10];
// This is not valid since mystring is larger than `buf`
strcpy(buf, mystring);
printf("buf: %s\n", buf);
return 0;
}
2 changes: 2 additions & 0 deletions test/other/test_stack_protector.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
buf: Hello world
*** stack smashing detected ***
3 changes: 3 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14937,3 +14937,6 @@ def test_std_promise_link(self, *args):
}
''')
self.run_process([EMXX, 'src.cpp', '-pthread'] + list(args))

def test_stack_protector(self):
self.do_other_test('test_stack_protector.c', emcc_args=['-fstack-protector'], assert_returncode=NON_ZERO)
2 changes: 1 addition & 1 deletion tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ def get_files(self):
'fork.c', 'vfork.c', 'posix_spawn.c', 'posix_spawnp.c', 'execve.c', 'waitid.c', 'system.c',
'_Fork.c',
# 'env' exclusion
'__reset_tls.c', '__init_tls.c', '__libc_start_main.c', '__stack_chk_fail.c',
'__reset_tls.c', '__init_tls.c', '__libc_start_main.c',
]

ignore += LIBC_SOCKETS
Expand Down

0 comments on commit 19d0b81

Please sign in to comment.