-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Please take this report with a grain of salt as I'm fairly new with WASM.
Test Case
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <unistd.h>
extern "C" {
__attribute__((import_module("wasi"), import_name("thread-spawn")))
int32_t spawn_a_thread(void *thread_data);
}
int magic_number = 0;
[[clang::export_name("wasi_thread_start")]]
void wasi_thread_start(int32_t tid, int *arg) {
printf("Running on a thread. tid=%d, arg=%p, magic_number=%d\n", tid, arg,
magic_number);
}
int main() {
magic_number += 1;
sleep(1);
int thread_data;
printf("thread_data=%p\n", &thread_data);
int tid = spawn_a_thread(&thread_data);
assert(tid > 0);
sleep(1);
printf("After\n");
}Here's what this compiles to (apologies, it's pretty long) with Homebrew Clang 21.1.8:
Steps to Reproduce
- wasmtime:
$ wasmtime run --wasi threads=y --wasm threads=y main.wat - WAMR:
$ iwasm --max-threads=2 main.wasm
Expected Results
$ iwasm --max-threads=2 main.wat
thread_data=0x113b8
Running on a thread. tid=1, arg=0x113b8, magic_number=1
AfterActual Results
$ wasmtime run --wasi threads=y --wasm threads=y main.wat
thread_data=0x113b8
Running on a thread. tid=1, arg=0x113b8, magic_number=0
AfterIn particular, I think the thread should see a magic_number of 1, since it has been incremented by the main program before the thread started.
Versions and Environment
Wasmtime version or commit: wasmtime 39.0.1
Operating system: macOS 14.6.1
Architecture: M1
Extra Info
There's some funny logic here (https://github.com/bytecodealliance/wasmtime/blob/main/crates/wasi-threads/src/lib.rs#L165-L179) in the wasi threads crate, where the shared memory is somehow re-injected into the linker. In the clang-generated code, the shared memory isn't imported — maybe that means the linker doesn't inject the same shared memory region to each thread?