Repository navigation
[dylink] Don't walk the main module's table in pthread workers - #28105
clementperon wants to merge 10 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request optimizes worker initialization in pthreads builds with MAIN_MODULE by passing only the table slots of the main module's exported functions to workers, rather than having them walk the entire function table. Feedback suggests adding a defensive check in src/lib/libaddfunction.js to ensure getWasmTableEntry(slot) returns a valid object before adding it to the WeakMap, preventing potential runtime crashes in workers.
|
@sbc100 is this something you want to merge upstream ? |
|
Mostly likely yes, I'm just a little review constrained right now. I'll try to get to it. |
|
Thank you, take your time and don't hesitate if you want me to perform more test. |
509af0f to
12b8416
Compare
The first call to getFunctionAddress walks the whole table, which creates a JS function object for every entry. With MAIN_MODULE and pthreads, every worker does this when it instantiates the main module, and keeps those objects for its lifetime: about 8 MiB per worker for a 50k-entry table. The dynamic linker only looks up the main module's exported functions, so the main thread now records their table slots and passes them to workers, which add just those to the map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Note in getFunctionAddress and the ChangeLog that a non-exported main-module function passed to addFunction in a worker now gets a new slot. Add an ASSERTIONS=2 variant of test_pthread_dylink, whose addFunction check catches export slots missing from a worker's map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Instead of special-casing workers in getFunctionAddress, fill their functionsInTableMap with the main module's export slots right before updateGOT. At that point the table only holds the main module, and side modules add their own slots when they are loaded, so workers no longer need the main module's table size. The main thread's recording and the worker's seeding are now one function, initFunctionsInTableMap. Drop the ASSERTIONS=2 variant of test_pthread_dylink: the &bar_main assertion already fails if workers don't get the export slots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12b8416 to
0b49138
Compare
|
@sbc100 just to add more information I retest this PR on top of 7.0.0 addFunction(getWasmTableEntry(ptr)) in a worker on a non-exported main-module function allocates a new slot, and if another thread later dlopens a library, a side-module function pointer with that index calls the old function in that worker => not nice :( But, the same happens today on 7.0.0 without this PR for any function a worker adds with addFunction before a dlopen on another thread, for example a JS callback: the table layouts diverge and the side module's pointer calls the callback. So the PR does not introduce a new failure mode; it moves one case (re-adding a function that is already in the table but not exported) into the existing one. ASSERTIONS=2 reports it. Keeping the old result exactly needs the full walk in every worker, which is what costs 7–8 MiB of JS heap per worker in our app (12.3 → 4.7 MiB with 7.0.0). I can reword the ChangeLog entry to state the consequence precisely. Would that be acceptable, or do you want workers to fall back to the walk on a miss from user code? |
Yes, directly calling We should probably have made this more clear.. I had thought there was an error message for such cases but maybe not?
I'm not quite sure exactly that would look like. Perhaps you could draft that changelog entry here so we could see exactly what the semantic differences is that you are proposing .
|
|
On the error message: I did not find one. AI: addFunction has no pthread check in libaddfunction.js, and the addFunction section of Interacting-with-code.rst does not mention threads. The dynamic linker itself calls addFunction in workers (updateGOT, reportUndefinedSymbols, dlsym), so a check would have to sit on the user-facing path only. I can look at that in a separate PR if you want. Here is the ChangeLog entry I propose: |
|
That sounds reasonable to me. I don't think direct calls to |
Follow-up to the discussion in #28105: each thread has its own table, so a function pointer returned by `addFunction` is only valid in the thread that called it which break C/C++ semantics. In builds with assertions and shared memory (pthreads or Wasm Workers), `addFunction` now warns once when it has to add a function to the table.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@sbc100 shorten the Changelog |
|
@sbc100 if you have a bit of time to review 🙏 |
sbc100
left a comment
There was a problem hiding this comment.
lgtm % nits
I like how clean this change is now.
- Assert that workers received mainModuleExportSlots instead of silently falling back to the full table walk. - Reword the initFunctionsInTableMap comment. - Drop the ChangeLog entry. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@sbc100 done, thanks for the review 👍 |
Move the worker half of initFunctionsInTableMap into the lazy init in getFunctionAddress, so the map is only created in one place, and rename the remaining main-thread half to initMainModuleExportSlots. Workers still seed the map during the main module's updateGOT, before any side module is loaded. Assert that when a side module is loaded. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
| for (var slot of mainModuleExportSlots) { | ||
| functionsInTableMap.set(getWasmTableEntry(slot), slot); | ||
| } | ||
| } else |
There was a problem hiding this comment.
Hmm.. now I'm little worried we might not fully initialize the functionsInTableMap if this is called late (i.e. after one or more side modules are loaded).
Could that happen? Then the differed updateTableMap entries for the side module wouldn't make it into the final functionsInTableMap.
I guess there is some tension here between wanting to defer the creation of functionsInTableMap.. and this new optimization?
There was a problem hiding this comment.
AI review It can't happen today: in a worker, updateGOT calls getFunctionAddress right after the main module is instantiated, before any side module is loaded, and this commit asserts that in loadWebAssemblyModule. But it does depend on updateGOT staying eager (#27993 would change that).
That tension is why the previous revision created the map up front in workers. I'd rather go back to that and live with the duplicated new WeakMap().
$initFunctionsInTableMap: (exports) => {
if (!ENVIRONMENT_IS_PTHREAD) {
mainModuleExportSlots = [];
for (var value of Object.values(exports)) {
var addr = typeof value == 'function' && getFunctionAddress(value);
if (addr) {
mainModuleExportSlots.push(addr);
}
}
} else {
#if ASSERTIONS
assert(mainModuleExportSlots);
#endif
functionsInTableMap = new WeakMap(); // <- the duplicated line
for (var slot of mainModuleExportSlots) {
functionsInTableMap.set(getWasmTableEntry(slot), slot);
}
}
}
Should i rollback to this version ?
This reverts commit 4ee63af. The lazy version only works if the first getFunctionAddress call in a worker happens before any side module is loaded, which depends on updateGOT calling it right after the main module is instantiated. Go back to creating the map up front in workers, so that it does not depend on that ordering. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Can you come up with a final commit message here? The full PR description I think is a little too much information for this. You can either post it here as a comment or update the commit message. |
The first call to
getFunctionAddresswalks the whole table and keeps a JS function object for every entry. WithMAIN_MODULEand pthreads, every worker makes that call when it instantiates the main module (updateGOT), so each worker holds a wrapper for every address-taken function of the main module for its whole lifetime.The dynamic linker only looks up the main module's exported functions. So the main thread now records their table slots, and passes them to workers in the
loadmessage next tosharedModules. Right beforeupdateGOT, a worker createsfunctionsInTableMapwith just those slots. At that point the table only holds the main module. Side modules add their own slots when they are loaded, as before.Both halves are in one function,
initFunctionsInTableMapinlibdylink.js.addFunctionandgetFunctionAddressare unchanged.Impact
Kodi's WebAssembly port links Kodi as
MAIN_MODULE=2so binary add-ons can bedlopened. Its table has 50,476 slots, and 76 of its exported functions are in the table.Measured in headless Chrome 157 with Emscripten 7.0.0 and with this change applied to 7.0.0. The wasm is byte-identical in both builds; only the JS differs. Kodi sits on its home screen with 11 to 13 workers, three runs each:
For reference, the same app linked without
MAIN_MODULEhas 3.0–3.7 MiB of JS heap per worker.An earlier revision of this change, applied to the Emscripten 6.0.9 glue, took the 17 workers Kodi has during playback from 257 MiB to 117 MiB of JS heap.
Behavior
Lookups by the dynamic linker in workers return the same slots as before. A function that appears in several slots still resolves to its last slot, as the walk did.
The difference is a non-exported main-module function passed to
addFunctionin a worker. JS can only reach such a function through the table itself (for example withgetWasmTableEntry), so it already has its address. Before, the walk found its existing slot. Now, the worker gives it a new slot at the end of its own table.That makes the worker's table differ from the other threads' tables. If another thread later loads a library, the library's functions get the same indices, and in that worker a pointer to one of them calls the function that was added instead. I checked this with a small program: after the
dlopen, the worker's call through the side module's pointer ran the main-module function.This is not a new failure mode. Emscripten 7.0.0 without this change does the same for any function that a worker adds with
addFunctionbefore another thread callsdlopen, for example a JS callback: in the same program, the side module's pointer then called the JS callback. This PR moves one more case into it: re-adding a function that is already in the table but not exported.Keeping the old result for that case would need the full walk this PR removes. With
ASSERTIONS=2,addFunctionreports it ("function in Table but not functionsInTableMap"). The limitation is noted in a comment oninitFunctionsInTableMapand in the ChangeLog entry.The main thread still walks the table once. #27993 creates the main module's GOT entries lazily and doesn't change the walk, so the two changes are independent.
Testing
test_pthread_dylinknow asserts that a side module sees the same&bar_mainas the main module, on the main thread and in a worker. That assertion fails if workers don't add the recorded slots.Run locally on this revision, with the LLVM of the 7.0.0 release: all
test_pthread_dl*,test_pthread_dylink*andtest_pthread_tls_dylinktests incore0,core2andwasm64, andother.test_dylink_pthread*. 56 pass and 2 are skipped by the test suite itself (wasm64.test_pthread_dlopen_many,other.test_dylink_pthread_comdat). I did not run the codesize tests locally; the Codesize check on this PR passes.No test checks that workers skip the walk.
🤖 Generated with Claude Code