Fix out-of-bounds dltab in the WASI dlopen shim - #86
Open
bendytree wants to merge 1 commit into
Open
Conversation
dltab was a tentative one-element array ("dict_t* dltab[]") indexed past
its end, and slot 0 was never assigned: the first library was stored at
dltab[1] while the lookup loop read dltab[0..dltab_index). Every dlopen
lookup therefore dereferenced a NULL dict, i.e. guest address 0.
That happens to be harmless while address 0 holds zeros (the dict's len
field reads as 0), which is why the socket-file transport never noticed.
With the CMA wire channel, live request bytes sit at the bottom of linear
memory, so the NULL-dict read picks up garbage lengths and entry pointers
and traps — reproducible as a crash on the first CREATE EXTENSION issued
over CMA.
Make dltab a real fixed-size array, keep every slot initialized, check
the existing-library branch against index >= 0 (slot 0 is valid), and
return stable 1-based handles.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The WASI dlopen shim's library table (
dltabinwasm-build/sdk_port-wasi/sdk_port-wasi-dlfcn.c) is a tentative one-element array indexed past its end, and slot 0 is never assigned — the first library is stored atdltab[1]while the lookup loop readsdltab[0..dltab_index). Every lookup therefore dereferences a NULL dict, i.e. guest address 0.This is invisible with the socket-file transport (address 0 holds zeros, so the dict's
lenreads as 0 and the loop is skipped), but with the CMA wire channel live request bytes sit at the bottom of linear memory, so the NULL-dict read picks up garbage lengths/pointers and traps. Reproducible as a crash on the firstCREATE EXTENSIONissued over CMA.This PR makes
dltaba real fixed-size array, keeps every slot initialized, fixes the existing-library check (>= 0— index 0 is a valid hit), and returns stable 1-based handles.Found while building a native (wasm2c) iOS embedding of this branch — https://github.com/bendytree/pglite-ios — where the fix is verified by test suites on macOS, iOS simulator, and iPhone hardware.