Skip to content

Commit 8b7148f

Browse files
Add -fstack-protector support to wasi-libc (#351)
Inlcude `__stack_chk_fail.c` and initialize `__stack_chk_guard` in ctor. ``` $ cat main.c char input[] = "0123456789012345"; int main(void) { char buf[8]; for (char *sp = input, *dp = buf; *sp != '\0'; sp++, dp++) { *dp = *sp; } return 0; } $ clang main.c -fstack-protector $ wasmtime ./a.out Error: failed to run main module `./a.out` Caused by: 0: failed to invoke command default 1: wasm trap: wasm `unreachable` instruction executed wasm backtrace: 0: 0x258 - <unknown>!__stack_chk_fail 1: 0x21e - <unknown>!__original_main 2: 0xca - <unknown>!_start ```
1 parent 6cd1be1 commit 8b7148f

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
141141
fcntl/creat.c \
142142
dirent/alphasort.c \
143143
dirent/versionsort.c \
144+
env/__stack_chk_fail.c \
144145
env/clearenv.c \
145146
env/getenv.c \
146147
env/putenv.c \

expected/wasm32-wasi/posix/defined-symbols.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ __getopt_msg
9191
__gmtime_r
9292
__hwcap
9393
__inet_aton
94+
__init_ssp
9495
__init_tp
9596
__intscan
9697
__invtrigl_R
@@ -236,6 +237,9 @@ __sin
236237
__sindf
237238
__sinl
238239
__small_printf
240+
__stack_chk_fail
241+
__stack_chk_fail_local
242+
__stack_chk_guard
239243
__stderr_FILE
240244
__stderr_used
241245
__stdin_FILE

expected/wasm32-wasi/single/defined-symbols.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ __getopt_msg
8181
__gmtime_r
8282
__hwcap
8383
__inet_aton
84+
__init_ssp
8485
__intscan
8586
__invtrigl_R
8687
__isalnum_l
@@ -193,6 +194,9 @@ __sin
193194
__sindf
194195
__sinl
195196
__small_printf
197+
__stack_chk_fail
198+
__stack_chk_fail_local
199+
__stack_chk_guard
196200
__stderr_FILE
197201
__stderr_used
198202
__stdin_FILE

libc-top-half/musl/src/env/__stack_chk_fail.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include <string.h>
22
#include <stdint.h>
3+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
34
#include "pthread_impl.h"
5+
#else
6+
// In non-_REENTRANT, include it for `a_crash`
7+
# include "atomic.h"
8+
#endif
49

510
uintptr_t __stack_chk_guard;
611

@@ -18,7 +23,9 @@ void __init_ssp(void *entropy)
1823
((char *)&__stack_chk_guard)[1] = 0;
1924
#endif
2025

26+
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
2127
__pthread_self()->canary = __stack_chk_guard;
28+
#endif
2229
}
2330

2431
void __stack_chk_fail(void)
@@ -29,3 +36,14 @@ void __stack_chk_fail(void)
2936
hidden void __stack_chk_fail_local(void);
3037

3138
weak_alias(__stack_chk_fail, __stack_chk_fail_local);
39+
40+
#ifndef __wasilibc_unmodified_upstream
41+
# include <wasi/api.h>
42+
43+
__attribute__((constructor(60)))
44+
static void __wasilibc_init_ssp(void) {
45+
uintptr_t entropy;
46+
int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t));
47+
__init_ssp(r ? NULL : &entropy);
48+
}
49+
#endif

0 commit comments

Comments
 (0)