Skip to content

WASM threads don't seem to share memory (if the memory isn't imported) #12219

@fatlotus

Description

@fatlotus

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:

main.wat.txt

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
After

Actual 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
After

In 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior in the current implementation that needs fixing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions