You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am working on a Rust project that we compile to wasm32 which uses atomics. The compiled wasm is then used in a browser by multiple web workers. I was seeing strange issues with return values from different workers where the return values got mixed up, e.g. worker 1 received a return value from another worker. After drilling into this I found that the JS binding that is generated by wasm-bindgen is not thread-safe. In particular, I ran into a lot of issues when returning Option's to JS. The generated code for this is the following:
When using synchronization primitives the order in which threads / workers are unlocked is not always guaranteed, and that's why this can easily mix up return values because all threads use the same stack space for return values.
Possible Solution
After talking to @alexcrichton, one possible solution could be to increment a stack pointer per thread and have each thread allocate a unique stack space (a thread-local area).
The text was updated successfully, but these errors were encountered:
This commit updates the implementation of passing return pointers from
JS to wasm to actually modify the wasm's shadow stack pointer instead of
manufacturing the arbitrary address of 8. The purpose of this is to
correctly handle threaded scenarios where each thread will write to its
own area instead of everyone trying to compete at address 8.
The implementation here will lazily, if necessary, export the stack
pointer we find the module and modify it as necessary.
Closesrustwasm#2218
* Pass actual stack pointers around instead of address 8
This commit updates the implementation of passing return pointers from
JS to wasm to actually modify the wasm's shadow stack pointer instead of
manufacturing the arbitrary address of 8. The purpose of this is to
correctly handle threaded scenarios where each thread will write to its
own area instead of everyone trying to compete at address 8.
The implementation here will lazily, if necessary, export the stack
pointer we find the module and modify it as necessary.
Closes#2218
* Fix benchmarks build
Describe the Bug
I am working on a Rust project that we compile to wasm32 which uses atomics. The compiled wasm is then used in a browser by multiple web workers. I was seeing strange issues with return values from different workers where the return values got mixed up, e.g. worker 1 received a return value from another worker. After drilling into this I found that the JS binding that is generated by wasm-bindgen is not thread-safe. In particular, I ran into a lot of issues when returning Option's to JS. The generated code for this is the following:
When using synchronization primitives the order in which threads / workers are unlocked is not always guaranteed, and that's why this can easily mix up return values because all threads use the same stack space for return values.
Possible Solution
After talking to @alexcrichton, one possible solution could be to increment a stack pointer per thread and have each thread allocate a unique stack space (a thread-local area).
The text was updated successfully, but these errors were encountered: