Skip to content

Run linker callback finalizers on invalid names - #14126

Merged
dicej merged 1 commit into
bytecodealliance:mainfrom
grandpig:fix-linker-callback-finalizers
Aug 12, 2026
Merged

Run linker callback finalizers on invalid names#14126
dicej merged 1 commit into
bytecodealliance:mainfrom
grandpig:fix-linker-callback-finalizers

Conversation

@grandpig

Copy link
Copy Markdown
Contributor

Follow-up to bytecodealliance/wasmtime-go#293.

When one of the linker function APIs receives an invalid UTF-8 module or function name, to_str! returns an error before the Rust callback closure is constructed.

The closure owns the C-provided data and finalizer through ForeignData.

Constructing it after parsing the names therefore means that the finalizer is not called when name parsing fails.

Move callback construction before UTF-8 validation in:

  • wasmtime_linker_define_func
  • wasmtime_linker_define_func_unchecked
  • wasmtime_linker_define_async_func

This transfers ownership of data to ForeignData before any fallible name parsing. If parsing fails, normal Rust drop behavior invokes the finalizer.

Regression tests verify this behavior for the checked, unchecked, and async linker function APIs.

Testing

  • C API test suite: 137/137 passed
  • cargo fmt --all -- --check
  • clang-format dry run on the modified C++ test files

@grandpig
grandpig requested a review from a team as a code owner August 12, 2026 15:44
@grandpig
grandpig requested review from dicej and removed request for a team August 12, 2026 15:44

@dicej dicej left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dicej
dicej enabled auto-merge August 12, 2026 16:01
@dicej
dicej added this pull request to the merge queue Aug 12, 2026
Merged via the queue into bytecodealliance:main with commit 0b9b72d Aug 12, 2026
57 checks passed
pull Bot pushed a commit to Haofei/wasmtime that referenced this pull request Aug 13, 2026
* Shuffle more finalizers in the C API

This implements a similar refactoring to bytecodealliance#14126 but for the component
linker as well.

* Clang-format
alexcrichton added a commit that referenced this pull request Aug 14, 2026
* Fix panic compiling an empty component with debug info (#14130)

generate_simulated_dwarf unwrapped the first core-module translation to
name its compilation unit, but a component with no core modules has no
translations, so `wasmtime compile -D debug-info=y` panicked on a valid
`(component)` input. Return early instead: with no translations there are
no functions to describe.

* Run linker callback finalizers on invalid names (#14126)

* Shuffle more finalizers in the C API (#14133)

* Shuffle more finalizers in the C API

This implements a similar refactoring to #14126 but for the component
linker as well.

* Clang-format

* fix(wasmtime-cli): generic eio error thrown for wasip2 (#14107)

* Cranelift: unwind last-store state after removing a dead store (#14111)

Alias analysis's dead-store elimination removed the dead store's `mem_values`
entry, but left the region's last-store slot naming the instruction it had just
deleted. Leaving the removed-store meant that when we then reprocess the
overwriting store, we keyed its lookup on a removed instruction, found nothing,
and failed to notice that (for example) the overwriting store became idempotent
and could also be removed.

With this commit, each store now records the memory version it displaced, and
eliminating a dead store rolls that version back, so a chain like

    v1 = load.i32 region0 v0
    store region0 v2, v0  ;; dead
    store region0 v1, v0  ;; idempotent once the dead store is gone

collapses in the single pass we actually make, rather than removing only one
link in the chain and requiring that we do N passes to fully clean up a chain of
N dead/idempotent stores. This code pattern the shape fused sync adapters emit
around the `MAY_LEAVE` flag and the relevant disas tests each lose a store as a
result.

* Alias analysis: do not restore the last-fence into a region slot (#14134)

* Alias analysis: do not restore the last-fence into a region slot

When we eliminate a dead store, we undo the effects that the dead store had on
the `LastStore` state. However, querying the last store for a particular region
falls back to the last fence, and we were incorrectly restoring that last fence
into the region slot, rather than resetting the region slot to `None`. While
technically incorrect, it was generally benign, but it did lead to "observing"
instructions that we didn't mark observed during our initial observation pass,
which ultimately led to debug assertion failures.

Fixes #14131

* untrim whitespace in filetests

* Return is-directory when a directory fd is used as a file (#14135)

* wasip2: return is-directory when a directory fd is used as a file

Descriptor::file() treated a directory as a bad descriptor. POSIX
read/write on a directory is EISDIR, and wasi:filesystem already has
is-directory. Preview1 guests still get EBADF (separate match and
adapter).

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>

* wasip2: map is-directory only on read-via-stream

Descriptor::file() must stay bad-descriptor for directories.
wasi-testsuite filesystem-advise expects that for advise.
Return is-directory from read-via-stream only (p2 result, p3
result future) so a directory read matches POSIX EISDIR.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>

---------

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>

* Fix test expectations

---------

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Co-authored-by: m0g3r <87276771+m0g3r@users.noreply.github.com>
Co-authored-by: grandpig <grandpig@outlook.com>
Co-authored-by: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com>
Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
Co-authored-by: Sebastien Tardif <SebTardif@ncf.ca>
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.

2 participants