-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Fix late-binding symbols with JSPI #23619
base: main
Are you sure you want to change the base?
Conversation
55ff7e0
to
33707ea
Compare
Late-binding symbols get a JS stub import that resolves the symbol and then makes an onward call. This breaks JSPI. (It also canonicalizes NaNs by round tripping them through JS). To fix this, we have to walk the import table and make wasm module stubs for each of the missing function-type symbols. These stubs will call a JS function to resolve the symbol but then insert it into a table and return control back to the wasm stub which has to make the onward call. This currently relies on --experimental-wasm-type-reflection. To avoid that, we need to parse the import table of the wasm binary manually. Also, without wasm-type-reflection, we have no way to handle the case where someone calls `loadWebAssemblyModule()` on a WebAssembly module instead of a Uint8Array.
33707ea
to
ed49619
Compare
Why do you say this? Is that simply because wasm type reflection is not standardized yet? Presumably if it was standardized this would be good tool to solve this problem? |
return WebAssembly.instantiate(binary, info).then( | ||
(result) => postInstantiation(result.module, result.instance) | ||
); | ||
return (async function() { |
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.
Hmm.. nice trick to use await
inside a function that is not async
..
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.
Oh wow, we really have to jump through a bunch of hoops to avoid JS thunks huh?
sig += lookup[v]; | ||
} | ||
return sig; | ||
}, |
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.
Can any of this new code be shared with the existing code for binary module creation in in libraryadd_function.js?
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.
Perhaps it would make sense to factor out the wasm prelude (magic + version) as an export from libaddfunction
. Other than that, the only code that's really shared is generateFuncType
which we already extracted as a separate function.
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.
wasmSigToEmscripten
is the inverse function of sigToWasmTypes
in libaddfunction.js
. In Pyodide I have a runtime_wasm
file for producing wasm trampolines like this:
https://github.com/pyodide/pyodide/blob/main/src/core/stack_switching/runtime_wasm.mjs
I could eventually upstream a bit more of this. It's not great for code size if we only have a few trampolines but it certainly leads to more readable code. Here's an example where I make invoke_sig
functions with it:
https://github.com/pyodide/pyodide/blob/main/src/core/stack_switching/create_invokes.mjs
I think I might copy this code into libffi because I've been intending to make libffi compatible with stack switching.
Yes in particular my concern is that I expect jspi to become available without a feature flag before wasm-type-reflection so if we depend on it there will be engines that otherwise would be able to use our jspi but can't because they haven't yet shipped type reflection.
Absolutely it's the right tool. |
Late-binding symbols get a JS stub import that resolves the symbol and then makes an onward call. This breaks JSPI. (It also canonicalizes NaNs by round tripping them through JS).
To fix this, we have to walk the import table and make wasm module stubs for each of the missing function-type symbols. These stubs will call a JS function to resolve the symbol but then insert it into a table and return control back to the wasm stub which has to make the onward call.
This currently relies on --experimental-wasm-type-reflection. To avoid that, we will need to parse the import table of the wasm binary manually. Also, without wasm-type-reflection, we have no way to handle the case where someone calls
loadWebAssemblyModule()
on a WebAssembly module instead of a Uint8Array.This half way fixes #23395. I say half way because to be truly fixed the solution shouldn't rely on wasm type reflection. See also #23600.