Skip to content

Fix out-of-bounds dltab in the WASI dlopen shim - #86

Open
bendytree wants to merge 1 commit into
electric-sql:REL_17_5_WASM-pglitefrom
bendytree:fix/wasi-dlopen-dltab
Open

Fix out-of-bounds dltab in the WASI dlopen shim#86
bendytree wants to merge 1 commit into
electric-sql:REL_17_5_WASM-pglitefrom
bendytree:fix/wasi-dlopen-dltab

Conversation

@bendytree

Copy link
Copy Markdown

The WASI dlopen shim's library table (dltab in wasm-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 at dltab[1] while the lookup loop reads dltab[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 len reads 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 first CREATE EXTENSION issued over CMA.

This PR makes dltab a 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant