Skip to content

Commit

Permalink
Define __stack_base and __stack_limit globals in debug mode for s…
Browse files Browse the repository at this point in the history
…tack overflow detection

That enables VMs to implement stack overflow detection or using passes like https://github.com/WebAssembly/binaryen/blob/main/src/passes/StackCheck.cpp
  • Loading branch information
loganek committed Jan 10, 2023
1 parent 16a6940 commit 95a609e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
thread/sem_timedwait.c \
thread/sem_trywait.c \
thread/sem_wait.c \
thread/wasm32/wasi_thread_start.s \
thread/wasm32/wasi_thread_start.S \
)
endif

Expand Down Expand Up @@ -341,7 +341,7 @@ CFLAGS += -isystem "$(SYSROOT_INC)"
# These variables describe the locations of various files and directories in
# the build tree.
objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
asmobjs = $(patsubst $(CURDIR)/%.s,$(OBJDIR)/%.o,$(1))
asmobjs = $(patsubst $(CURDIR)/%.S,$(OBJDIR)/%.o,$(1))
DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
EMMALLOC_OBJS = $(call objs,$(EMMALLOC_SOURCES))
LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
Expand Down Expand Up @@ -520,7 +520,7 @@ $(OBJDIR)/%.o: $(CURDIR)/%.c include_dirs
@mkdir -p "$(@D)"
$(CC) $(CFLAGS) -MD -MP -o $@ -c $<

$(OBJDIR)/%.o: $(CURDIR)/%.s include_dirs
$(OBJDIR)/%.o: $(CURDIR)/%.S include_dirs
@mkdir -p "$(@D)"
$(CC) $(ASMFLAGS) -o $@ -c $<

Expand Down
10 changes: 8 additions & 2 deletions libc-top-half/musl/src/thread/pthread_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,14 @@ struct start_args {
unsigned long sig_mask[_NSIG/8/sizeof(long)];
#else
/*
* Note: the offset of the "stack" and "tls_base" members
* Note: the offset of the "stack", "stack_limit" and "tls_base" members
* in this structure is hardcoded in wasi_thread_start.
*/
void *stack;
void *tls_base;
#ifndef NDEBUG
void *stack_limit;
#endif
void *(*start_func)(void *);
void *start_arg;
#endif
Expand Down Expand Up @@ -501,7 +504,10 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
/* Correct the stack size */
new->stack_size = stack - stack_limit;

args->stack = new->stack; /* just for convenience of asm trampoline */
/* just for convenience of asm trampoline */
args->stack = new->stack;
args->stack_limit = stack_limit;

args->start_func = entry;
args->start_arg = arg;
args->tls_base = (void*)new_tls_base;
Expand Down
64 changes: 64 additions & 0 deletions libc-top-half/musl/src/thread/wasm32/wasi_thread_start.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#define PTR i32
#define PTRSIZE 4
# Preprocessor's define directive doesn't resolve math expressions,
# so we hardcode the result of PTRSIZE * 2 operation
#define DOUBLE_PTRSIZE 8

.text

.export_name wasi_thread_start, wasi_thread_start

.globaltype __stack_pointer, PTR
.globaltype __tls_base, PTR

#ifndef NDEBUG
.globaltype __stack_base, PTR
.globaltype __stack_limit, PTR
#endif

.functype __wasi_thread_start_C (i32, PTR) -> ()

.hidden wasi_thread_start
.globl wasi_thread_start
.type wasi_thread_start,@function

wasi_thread_start:
.functype wasi_thread_start (i32, PTR) -> ()

# Set up the minimum C environment.
# Note: offsetof(start_arg, stack) == 0
local.get 1 # start_arg
PTR.load 0 # stack
global.set __stack_pointer

local.get 1 # start_arg
PTR.load PTRSIZE # tls_base
global.set __tls_base

#ifndef NDEBUG
# configure __stack_base and __stack_limit in debug mode
# to allow for stack overflow detection
local.get 1 # start_arg
PTR.load 0 # stack
global.set __stack_base

local.get 1 # start_arg
PTR.load DOUBLE_PTRSIZE # stack_limit
global.set __stack_limit
#endif

# Make the C function do the rest of work.
local.get 0 # tid
local.get 1 # start_arg
call __wasi_thread_start_C

end_function

#ifndef NDEBUG
.section .data,"",@
__stack_base:
.size __stack_base, PTRSIZE

__stack_limit:
.size __stack_limit, PTRSIZE
#endif
31 changes: 0 additions & 31 deletions libc-top-half/musl/src/thread/wasm32/wasi_thread_start.s

This file was deleted.

0 comments on commit 95a609e

Please sign in to comment.