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
{{ message }}
Repository navigation
Emscripten calls null GOT entry when side module loaded with allowUndefined: true #22052
The following code example looks up GOT.__cxa_throw which is 0 and then calls it which throws TypeError: getWasmTableEntry(...) is not a function. Calling reportUndefinedSymbols or removing allowUndefined: true resolves the crash.
We should probably add a test for this? I wonder if there is some way to build a test that doesn't depend on using EM_JS?
Do you know exactly which types of symbols are effected by this? Is __cxa_throw effected because its a JS symbol? Or would any symbol be similarly effected?
I wonder if there is some way to build a test that doesn't depend on using EM_JS?
loadDylibs sets allowUndefined: true and doesn't ever seem to call reportUndefinedSymbols(). I bet we can use that. I assume loadDylibs() is the code path we go through if we pass libsomething.so on the command line?
Do you know exactly which types of symbols are effected by this? would any symbol be similarly effected?
I am not sure. Many symbols don't seem to have this problem. I will investigate further and get back to you.
It seems like the issue was likely always there but somehow the C++ std library changes the set of undefined symbols present in the shared libraries. This does sounds like something really should be fixed.
On the other hand, only uses who load code directly through the loadDynamicLibrary will be effected. Since this is essentially a helper function for implementing dlopen we don't have as much testing for the use of this function directly like this. @hoodmane is there any way we could get you to transition to more traditional dlopen? I'm guessing that answer is no.. otherwise you would already have done it :)
Do you know about that async version of dlopen? We have dlopen_emscripten (which take callbacks) and emscripten_dlopen_promise (which returns a promise)
No I was not aware of those. We're trying to call into this from JavaScript not from C so there would be some extra code in order to use these C apis, but if they work better that could be a reason to switch. I think there are some other subtle differences between the shared library dependency search paths in native dlopen vs emscripten's dlopen that we're currently working around. Perhaps if we can figure out a few more bugfixes we can switch.
It would be nice to see typedef __externref_t em_promise_t; some day so we wouldn't need this HandleAllocator stuff.
Do you know about that async version of dlopen? We have dlopen_emscripten (which take callbacks) and emscripten_dlopen_promise (which returns a promise)
This would be worth taking a look at, thanks. Another reason we use the JS function directly is to locate shared libraries. I opened a separate issue about it #22126.
No I was not aware of those. We're trying to call into this from JavaScript not from C so there would be some extra code in order to use these C apis, but if they work better that could be a reason to switch. I think there are some other subtle differences between the shared library dependency search paths in native dlopen vs emscripten's dlopen that we're currently working around. Perhaps if we can figure out a few more bugfixes we can switch.
It would be nice to see typedef __externref_t em_promise_t; some day so we wouldn't need this HandleAllocator stuff.
That is very unlikely to happen sadly, since __externref_t cannot be stored to memory, and cannot live within a struct, so it would make the API way less useful for normal C/C++ programmers. Sadly C/C++ is just not good at holding onto non-store-able things.
A lot of the time people just use return values immediately and don't need to store them, so in all of those cases this saves work. The JavaScript functions don't need to do extra steps to lookup the handle value; if the handle lookup needs to happen it occurs in C. And then we don't need special case destroy functions since they are all shared. When I switched to this approach it eliminated a lot of boilerplate in all the EM_JS functions in Pyodide.
A lot of the time people just use return values immediately and don't need to store them, so in all of those cases this saves work. The JavaScript functions don't need to do extra steps to lookup the handle value; if the handle lookup needs to happen it occurs in C. And then we don't need special case destroy functions since they are all shared. When I switched to this approach it eliminated a lot of boilerplate in all the EM_JS functions in Pyodide.
This is a bit of a sidetrack though =)
Sure that sounds like a good way of layering it. I would still probably want a simple variant all such function that just return a handle though, and perhaps a lower/level alternative one that returns the raw externref (which, as I say, I imagine very few folks would want in practice).
IIRC, we have looked, for example, and moving the handle allocator to native code, but IIRC is was strictly worse since the JS side is extremely good and doing fast hashmaps and doing that in C has some overhead. I guess there are somethings that JS really is better at.
a lower/level alternative one that returns the raw externref
I imagine very few folks would want in practice
Of course different applications are different, but in Pyodide there are only 8 places we explicitly create a handle now, whereas there were 87 places before we switched to externref, so we only need to make a handle under 10% of the time. This made a pretty large difference in performance for us.
FWIW, I think when I changed the implementation from JavaScript to C I didn't see any change in performance. We don't implement a hash map in C, we just have the table and an array of metadata. You could also do it with a free list.
Activity
FIX Prevent segfaults in CLI runner
hoodmane commentedon Jun 4, 2024
I think this patch to
GOTHandler.getfixes the problem:FIX Don't leave GOT entries null when libs are loaded with `allowUnde…
FIX Don't leave GOT entries null when libs are loaded with `allowUnde…
sbc100 commentedon Jun 4, 2024
We should probably add a test for this? I wonder if there is some way to build a test that doesn't depend on using EM_JS?
Do you know exactly which types of symbols are effected by this? Is
__cxa_throweffected because its a JS symbol? Or would any symbol be similarly effected?hoodmane commentedon Jun 5, 2024
+1 I'll add a test to #22053.
loadDylibssetsallowUndefined: trueand doesn't ever seem to callreportUndefinedSymbols(). I bet we can use that. I assumeloadDylibs()is the code path we go through if we passlibsomething.soon the command line?I am not sure. Many symbols don't seem to have this problem. I will investigate further and get back to you.
hoodmane commentedon Jun 6, 2024
Nevermind,
loadDylibs()does callreportUndefinedSymbols()so that doesn't work.FIX Prevent segfaults in CLI runner (#4836)
FIX Prevent segfaults in CLI runner (pyodide#4836)
hoodmane commentedon Jun 9, 2024
I think the first bad commit is emscripten-releases e4ea05676d38c899d486aeb1553af4a0b5432317
hoodmane commentedon Jun 9, 2024
No wait that one is e4ea05676d38c899d486aeb1553af4a0b5432317 good first bad is the next commit 9063e25ecf39470a96689f3896ca5d4462928eb6:
which is:
hoodmane commentedon Jun 21, 2024
Reverting #21638 on top of the main branch has no conflicts and makes the problem go away. Any ideas why that might be @aheejin?
sbc100 commentedon Jun 21, 2024
It seems like the issue was likely always there but somehow the C++ std library changes the set of undefined symbols present in the shared libraries. This does sounds like something really should be fixed.
On the other hand, only uses who load code directly through the
loadDynamicLibrarywill be effected. Since this is essentially a helper function for implementingdlopenwe don't have as much testing for the use of this function directly like this. @hoodmane is there any way we could get you to transition to more traditionaldlopen? I'm guessing that answer is no.. otherwise you would already have done it :)hoodmane commentedon Jun 21, 2024
We'd like to transition to
dlopen, I think the main barrier is that it is a sync API and there are quite a few reasons to prefer asynchronous loading.cc @ryanking13 who might have a better idea than I do about this.
sbc100 commentedon Jun 21, 2024
Do you know about that async version of dlopen? We have
dlopen_emscripten(which take callbacks) andemscripten_dlopen_promise(which returns a promise)hoodmane commentedon Jun 21, 2024
No I was not aware of those. We're trying to call into this from JavaScript not from C so there would be some extra code in order to use these C apis, but if they work better that could be a reason to switch. I think there are some other subtle differences between the shared library dependency search paths in native dlopen vs emscripten's dlopen that we're currently working around. Perhaps if we can figure out a few more bugfixes we can switch.
It would be nice to see
typedef __externref_t em_promise_t;some day so we wouldn't need thisHandleAllocatorstuff.ryanking13 commentedon Jun 21, 2024
This would be worth taking a look at, thanks. Another reason we use the JS function directly is to locate shared libraries. I opened a separate issue about it #22126.
sbc100 commentedon Jun 21, 2024
That is very unlikely to happen sadly, since
__externref_tcannot be stored to memory, and cannot live within a struct, so it would make the API way less useful for normal C/C++ programmers. Sadly C/C++ is just not good at holding onto non-store-able things.hoodmane commentedon Jun 21, 2024
I made this library to deal with adapting between
__externref_tand addressable memory:https://github.com/hoodmane/hiwire
I think it's beneficial to split up the steps of
__externref_tandA lot of the time people just use return values immediately and don't need to store them, so in all of those cases this saves work. The JavaScript functions don't need to do extra steps to lookup the handle value; if the handle lookup needs to happen it occurs in C. And then we don't need special case
destroyfunctions since they are all shared. When I switched to this approach it eliminated a lot of boilerplate in all theEM_JSfunctions in Pyodide.This is a bit of a sidetrack though =)
sbc100 commentedon Jun 21, 2024
Sure that sounds like a good way of layering it. I would still probably want a simple variant all such function that just return a handle though, and perhaps a lower/level alternative one that returns the raw externref (which, as I say, I imagine very few folks would want in practice).
IIRC, we have looked, for example, and moving the handle allocator to native code, but IIRC is was strictly worse since the JS side is extremely good and doing fast hashmaps and doing that in C has some overhead. I guess there are somethings that JS really is better at.
hoodmane commentedon Jun 21, 2024
Of course different applications are different, but in Pyodide there are only 8 places we explicitly create a handle now, whereas there were 87 places before we switched to externref, so we only need to make a handle under 10% of the time. This made a pretty large difference in performance for us.
FWIW, I think when I changed the implementation from JavaScript to C I didn't see any change in performance. We don't implement a hash map in C, we just have the table and an array of metadata. You could also do it with a free list.