Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-106213: Make Emscripten trampolines work with JSPI#106219
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
hoodmane commented Jun 29, 2023 • edited by bedevere-bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-bot
Uh oh!
There was an error while loading. Please reload this page.
There is a WIP proposal to enable webassembly stack switching which have been implemented in v8: https://github.com/WebAssembly/js-promise-integration It is not possible to switch stacks that contain JS frames so the Emscripten JS trampolines that allow calling functions with the wrong number of arguments don't work in this case. However, the js-promise-integration proposal requires the [type reflection for Wasm/JS API](https://github.com/WebAssembly/js-types) proposal, which allows us to actually count the number of arguments a function expects. For better compatibility with stack switching, this PR checks if type reflection is available, and if so we use a switch block to decide the appropriate signature. If type reflection is unavailable, we should use the current EMJS trampoline. We cache the function argument counts since when I didn't cache them performance was negatively affected.
erlend-aasland left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build changes LGTM; I cannot comment on the Emscripten peculiarities, though :)
hoodmane commented Jun 29, 2023
I guess I will add a news entry. |
hoodmane commented Jun 29, 2023
Thanks for the review @erlend-aasland! Emscripten is indeed peculiar... |
erlend-aasland commented Jun 29, 2023
If you have the time to guide me, I'll be happy to review the rest of the PR, just for the fun of learning! :) |
hoodmane commented Jun 29, 2023
Sure! Thank you for your time. I'll write a more detailed explanation. |
hoodmane left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the background of these trampolines and why this PR.
The purpose of these trampolines
Each WebAssembly function has a signature known to the runtime: how many arguments of what types and how many return values of what types. For security reasons, WebAssembly wants to ensure that every function is always invoked with the expected signature. It has two different instructions for calling a function: a call instruction for calling a function whose signature is validated at load time, and a call_indirect instruction for calling a function pointer whose signature is validated at runtime. So call_indirect is a bit more expensive. If runtime validation fails, then there is a trap.
Section 6.3.2.3, paragraph 8 of the C spec says:
A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the pointed-to type, the behavior is undefined.
But in every architecture + ABI other than wasm, it just works: if you cast a function that takes 2 arguments to a function that takes 3 and then call it with 3 arguments, everything goes fine. In wasm, it traps.
People frequently write getters and setters that don't take a void* closure argument and METH_NOARGS functions that don't take the always NULL second argument. So we need some way to fix it.
How the JS trampoline works
Point is simply that calling a JS function is relaxed about arguments. If you give a JS function extra arguments, they are just ignored. So we call wasm --> Js --> Wasm. The Wasm --> Js call is strict about wanting 4 arguments (the function pointer and the original three arguments) and the Js --> Wasm call drops any extra arguments.
What is JSPI (Wasm/JavaScript Promise integration)
The point is that most system calls in Posix are synchronous, whereas most browser APIs are asynchronous. So for instance, people want to use input() to get input from the user but the only ways of getting input are async. JSPI allows switching the stack, waiting for user input, then unswitching the stack and continuing. So that stdin can work correctly again. The JSPI is an implementation-stage proposal which allows a very specific sort of stack switching. There is a more general wasm stack switching API which is still under discussion.
What is the problem
JSPI can only unwind wasm stack frames not JS stack frames. This is because there was concern that the interaction between asyncio unwinding and a second stack switching mechanism could cause subtle bugs in the JS runtime state.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
erlend-aasland commented Jul 5, 2023
@brandtbucher, would you mind taking a look at these two comments? |
erlend-aasland commented Jul 6, 2023
Thanks for chiming in, @kumaraditya303! |
hoodmane commented Jul 7, 2023
Okay I think I made the changes suggested by @kumaraditya303 and @erlend-aasland. Could someone trigger the Emscripten build bot on this PR? If it passes then I think it should be ready to merge (unless of course anyone has further feedback). |
vstinner commented Sep 11, 2023
I'm sorry, but I broke wasm32 with my libregrtest refactoring work. I modified the code to spawn test worker processes in a different working directory: the temporary directory created by the main test process. |
vstinner commented Sep 11, 2023
Wasm32 Python can only be run in the Python source code directory? |
brettcannon commented Sep 11, 2023 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Currently the build looks for everything in See cpython/Tools/wasm/build_wasi.sh Line 28 in fbaf77e
Long-term I want to freeze the stdlib into the binary so this sort of thing isn't an issue. |
vstinner commented Sep 11, 2023
I wrote PR #109290 to fix Emscripten and WASI buildbot workers. |
vstinner commented Sep 12, 2023
I merged a first regrtest fix for wasm/wasi: #109313 (comment) Sadly, I didn't notice but my other regrtest json file descriptor change also broke wasm/wasi! Apparently, passing a file descriptor with |
vstinner commented Sep 12, 2023
I wrote PR #109326 to fix it: use a filename, instead of a file descriptor, on WASM/WASI. |
vstinner commented Sep 14, 2023
Emscripten and WASI buildbot workers are back to green. Sorry for having broken them, I was in the middle of a large refactoring of the regrtest code ;-) |
brettcannon commented Sep 14, 2023
!buildbot wasm32-emscripten * |
bedevere-bot commented Sep 14, 2023
🤖 New build scheduled with the buildbot fleet by @brettcannon for commit 6781a48 🤖 The command will test the builders whose names match following regular expression: The builders matched are:
|
brettcannon commented Sep 15, 2023
Thanks for the help, everyone! |
bedevere-bot commented Sep 15, 2023
|
bedevere-bot commented Sep 15, 2023
|
bedevere-bot commented Sep 15, 2023
|
bedevere-bot commented Sep 15, 2023
|
hoodmane commented Sep 15, 2023
Thanks so much @brettcannon@erlend-aasland@vstinner for all your help with this! |
bedevere-bot commented Sep 15, 2023
|
bedevere-bot commented Sep 15, 2023
|
…-106219) There is a WIP proposal to enable webassembly stack switching which have been implemented in v8: https://github.com/WebAssembly/js-promise-integration It is not possible to switch stacks that contain JS frames so the Emscripten JS trampolines that allow calling functions with the wrong number of arguments don't work in this case. However, the js-promise-integration proposal requires the [type reflection for Wasm/JS API](https://github.com/WebAssembly/js-types) proposal, which allows us to actually count the number of arguments a function expects. For better compatibility with stack switching, this PR checks if type reflection is available, and if so we use a switch block to decide the appropriate signature. If type reflection is unavailable, we should use the current EMJS trampoline. We cache the function argument counts since when I didn't cache them performance was negatively affected. Co-authored-by: T. Wouters <thomas@python.org> Co-authored-by: Brett Cannon <brett@python.org>
…-106219 (pythonGH-121701) (cherry picked from commit 3086b86) Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
There is a WIP proposal to enable webassembly stack switching which have been implemented in v8: https://github.com/WebAssembly/js-promise-integration It is not possible to switch stacks that contain JS frames so the Emscripten JS trampolines that allow calling functions with the wrong number of arguments don't work in this case. However, the js-promise-integration proposal requires the [type reflection for Wasm/JS API](https://github.com/WebAssembly/js-types) proposal, which allows us to actually count the number of arguments a function expects. For better compatibility with stack switching, this PR checks if type reflection is available, and if so we use a switch block to decide the appropriate signature. If type reflection is unavailable, we should use the current EMJS trampoline. We cache the function argument counts since when I didn't cache them performance was negatively affected. Upstreamed here: python#106219
There is a WIP proposal to enable webassembly stack switching which have been implemented in v8:
https://github.com/WebAssembly/js-promise-integration
It is not possible to switch stacks that contain JS frames so the Emscripten JS trampolines that allow calling functions with the wrong number of arguments don't work in this case. However, the js-promise-integration proposal requires the type reflection for Wasm/JS API proposal, which allows us to actually count the number of arguments a function expects.
For better compatibility with stack switching, this PR checks if type reflection is available, and if so we use a switch block to decide the appropriate signature. If type reflection is unavailable, we should use the current EMJS trampoline.
We cache the function argument counts since when I didn't cache them performance was negatively affected.
A backport of this patch to v3.11.x is tested against Pyodide and passes our tests:
pyodide/pyodide#3964