-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix pointer arguments in addFunction + wasm64 #24693
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
Open
RReverser
wants to merge
1
commit into
emscripten-core:main
Choose a base branch
from
RReverser:add-function-p-wasm64
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm a little surprised we don't have any existing tests of addFunction being used with JS functions with 'p' in their signatures.
I'd like to take a moment to try and figure out why no existing test covered this.
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.
Okay, as long as it doesn't block the fix too long.
Uh oh!
There was an error while loading. Please reload this page.
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.
I suspect it's possible that even if there were any tests, they 1) weren't doing pointer arithmetic and 2) weren't returning a pointer from JS to Wasm, only the other way around.
Without these conditions, e.g. if you have a simple tests that only logs the passed argument, the issues wouldn't trigger.
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.
I'm just a little suprised we don't run into this issue with our exist dynamic linking tests. The main user or
addFunction
for JS function with a signature passed are in dynamic linking, firstly indlsym
and secondly (which I imagine occurs way more often) inreportUndefinedSymbols
which will fill in missing GOT entries using JS functions. I'm surprised we don't run into this there.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 wait.. I think its because JS libraries functions are modified in place when they are constructed, right? So this change is not necessary for JS library functions at all, only for JS functions that come from elsewhere?
Is that right?
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.
Ok, fair enough. Given that it's possible to use
addFunction
from user code, and that it lives in its ownlibaddfunction.js
, I assumed it's part of public API, but it sounds like it's just one of many functions that isn't marked as__internal: true
for backward compat / legacy reasons rather than intended to be used by external developers?If so, I'm happy to also close this and fix just my individual usage of
addFunction
in the Embind PR where I needed it.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.
Yeah, I think its does have some users, but I'd like to discourage its usage if possible. I'd rather see dynamic dispatch mechanisms be built on the JS side instead of by injecting into (modifying the wasm table at runtime). (I think only the dynamic linker should be doing that really, but I've never really articulated that in any official docs anywhere).
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 I'm finding even more usecases for
addFunction
where it would save quite a bit of JS code as well as make dispatch faster, pretty much win-win.So I think I'll spend some more time sorting this out after all.
Pretty much the opposite preference here, the more I play with it - if we can avoid extra JS work and perform "direct" typed calls, why not do that?
I also remembered that it's actually been a documented API for invoking JS callbacks from Wasm: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-javascript-functions-as-function-pointers-from-c
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.
But is there any real performance advantage over having an additional lookup table on the JS side?
Also, remember that
addFunction
does not currently work with threading, is that a limitation you are OK with?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.
There is for variadic functions, anywhere where you otherwise have to encode type info and number of arguments out of band.
Yeah that's fine, I'm only lazily storing it in thread-locals.