Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated JS binding is not thread-safe #2218

Closed
d3lm opened this issue Jun 29, 2020 · 0 comments · Fixed by #2249
Closed

Generated JS binding is not thread-safe #2218

d3lm opened this issue Jun 29, 2020 · 0 comments · Fixed by #2249
Labels

Comments

@d3lm
Copy link
Contributor

d3lm commented Jun 29, 2020

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:

var r0 = getInt32Memory0()[8 / 4 + 0];
var r1 = getInt32Memory0()[8 / 4 + 1];

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).

@d3lm d3lm added the bug label Jun 29, 2020
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Jul 22, 2020
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 rustwasm#2218
alexcrichton added a commit that referenced this issue Jul 23, 2020
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant