diff --git a/docs/contribute/toolchain.md b/docs/contribute/toolchain.md index 6760a6df8..7f3204d1a 100644 --- a/docs/contribute/toolchain.md +++ b/docs/contribute/toolchain.md @@ -43,7 +43,6 @@ options. - `lind_syscall/lind_syscall.c` - `lind_syscall/addr_translation.c` - `csu/wasm32/wasi_thread_start.s` - - `csu/wasm32/set_stack_pointer.s` 3. Generate sysroot diff --git a/scripts/lind_compile b/scripts/lind_compile index f84ae6c7d..5aecd4fe6 100755 --- a/scripts/lind_compile +++ b/scripts/lind_compile @@ -396,7 +396,7 @@ do_dynamic_compile() { fi run_cmd "${CLANG_BIN}" "${default_clang_flags[@]}" "${mandatory_clang_flags[@]}"\ - "${EXTRA_CLANG_ARGS[@]}" "${SRC}" "$SYSROOT/lib/wasm32-wasi/set_stack_pointer.o" "$SYSROOT/lib/wasm32-wasi/crt1_shared.o" "$SYSROOT/lib/wasm32-wasi/lind_utils.o" -o "${OUT_WASM}" + "${EXTRA_CLANG_ARGS[@]}" "${SRC}" "$SYSROOT/lib/wasm32-wasi/crt1_shared.o" "$SYSROOT/lib/wasm32-wasi/lind_utils.o" -o "${OUT_WASM}" run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_tls_relocs func __wasm_apply_tls_relocs optional run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_global_relocs func __wasm_apply_global_relocs optional diff --git a/scripts/make_glibc_and_sysroot.sh b/scripts/make_glibc_and_sysroot.sh index a440c94ef..eea0b3e3f 100755 --- a/scripts/make_glibc_and_sysroot.sh +++ b/scripts/make_glibc_and_sysroot.sh @@ -173,10 +173,6 @@ $CC --target=wasm32-wasi-threads -matomics \ -o $BUILD/csu/wasi_thread_start.o \ -c $GLIBC/csu/wasm32/wasi_thread_start.s -$CC --target=wasm32-wasi-threads -matomics \ - -o $BUILD/csu/set_stack_pointer.o \ - -c $GLIBC/csu/wasm32/set_stack_pointer.s - # Generate sysroot # First, remove the existing sysroot directory to start cleanly rm -rf "$SYSROOT" @@ -185,7 +181,6 @@ rm -rf "$SYSROOT" # Create the sysroot directory structure mkdir -p "$SYSROOT/include/wasm32-wasi" "$SYSROOT/lib/wasm32-wasi" cp "$BUILD/lind_utils.o" "$SYSROOT/lib/wasm32-wasi/" -cp "$BUILD/csu/set_stack_pointer.o" "$SYSROOT/lib/wasm32-wasi/" "$SCRIPT_DIR/make_archive.sh" cd $SCRIPT_DIR diff --git a/scripts/object_lists/libc_objects.txt b/scripts/object_lists/libc_objects.txt index 2fb3119c5..a7e378088 100755 --- a/scripts/object_lists/libc_objects.txt +++ b/scripts/object_lists/libc_objects.txt @@ -2,7 +2,6 @@ lind_syscall.o addr_translation.o lind_debug.o csu/wasi_thread_start.o -csu/set_stack_pointer.o csu/__init_tls.o csu/init-first.o csu/libc-start.o diff --git a/scripts/object_lists/libc_objects_shared.txt b/scripts/object_lists/libc_objects_shared.txt index 34eae7e93..20116a9a2 100755 --- a/scripts/object_lists/libc_objects_shared.txt +++ b/scripts/object_lists/libc_objects_shared.txt @@ -2,7 +2,6 @@ lind_syscall.os addr_translation.os lind_debug.os csu/wasi_thread_start.os -csu/set_stack_pointer.os csu/libc-start.os csu/sysdep.os csu/version.os diff --git a/src/glibc/csu/wasm32/set_stack_pointer.s b/src/glibc/csu/wasm32/set_stack_pointer.s deleted file mode 100644 index 251f3e06b..000000000 --- a/src/glibc/csu/wasm32/set_stack_pointer.s +++ /dev/null @@ -1,23 +0,0 @@ -# function to set the stack pointer -# used by pthread to set thread stack pointer -# takes one argument (i32), and set __stack_pointer to the value -# technically this could be done directly in wasmtime, but I havn't dived into -# the way to manually set this yet. Using a helper wasm asm seems fine currently. - .text - - .export_name set_stack_pointer, set_stack_pointer - - .globaltype __stack_pointer, i32 - - .hidden set_stack_pointer - .globl set_stack_pointer - .type set_stack_pointer,@function - -set_stack_pointer: - .functype set_stack_pointer (i32) -> () - - local.get 0 # start_arg - global.set __stack_pointer - - end_function - \ No newline at end of file diff --git a/src/glibc/nptl/pthread_create.c b/src/glibc/nptl/pthread_create.c index 85526c6b4..104a7d92b 100644 --- a/src/glibc/nptl/pthread_create.c +++ b/src/glibc/nptl/pthread_create.c @@ -241,9 +241,6 @@ late_init (void) the user code (*PD->start_routine). */ -void set_stack_pointer(int stack_addr); -void *__dummy_reference2 = set_stack_pointer; - static int _Noreturn start_thread (void *arg); static int create_thread (struct pthread *pd, const struct pthread_attr *attr, diff --git a/src/wasmtime/crates/lind-multi-process/src/lib.rs b/src/wasmtime/crates/lind-multi-process/src/lib.rs index 0e88c800d..7c10e98fb 100644 --- a/src/wasmtime/crates/lind-multi-process/src/lib.rs +++ b/src/wasmtime/crates/lind-multi-process/src/lib.rs @@ -1139,10 +1139,9 @@ impl< // we might also want to perserve the offset of current stack pointer to stack bottom // not very sure if this is required, but just keep everything the same from parent seems to be good let offset = parent_stack_high_usr as u32 - stack_pointer; - let stack_pointer_setter = instance - .get_typed_func::(&mut store, "set_stack_pointer") + instance + .set_stack_pointer(&mut store, (stack_addr - offset) as i32) .unwrap(); - let _ = stack_pointer_setter.call(&mut store, (stack_addr - offset) as i32); // TODO: set up __stack_low and __stack_high // TODO: should share the imported wasm global diff --git a/src/wasmtime/crates/wasmtime/src/runtime/instance.rs b/src/wasmtime/crates/wasmtime/src/runtime/instance.rs index 86c9b5b42..d49076345 100644 --- a/src/wasmtime/crates/wasmtime/src/runtime/instance.rs +++ b/src/wasmtime/crates/wasmtime/src/runtime/instance.rs @@ -795,6 +795,26 @@ impl Instance { return Err(()); } + /// Updates the exported `__stack_pointer` global for this instance. + /// Returns `Err(())` if `__stack_pointer` is not exported, is not a global, + /// or cannot be updated. + pub fn set_stack_pointer(&self, mut store: impl AsContextMut, sp: i32) -> Result<(), ()> { + if let Some(sp_extern) = self.get_export(store.as_context_mut(), "__stack_pointer") { + match sp_extern { + Extern::Global(sp_global) => { + match sp_global.set(store.as_context_mut(), Val::I32(sp)) { + Ok(()) => return Ok(()), + Err(_) => return Err(()), + } + } + _ => { + return Err(()); + } + } + } + Err(()) + } + pub fn get_stack_low(&self, mut store: impl AsContextMut) -> Result { if let Some(sp_extern) = self.get_export(store.as_context_mut(), "__stack_low") { match sp_extern {