Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] Fix backtraces through empty sequences of Wasm frames #9418

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

alexcrichton
Copy link
Member

This fixes a bug where we would not properly handle contiguous sequences of Wasm frames that are empty. This was mistakenly believed to be an impossible scenario, and before the tail-calls proposal it was impossible, however it can now happen after the following series of events:

  • Host calls into Wasm, pushing the entry trampoline frame.

  • Entry trampoline calls the actual Wasm function, pushing a Wasm frame.

  • Wasm function tail calls to an imported host function, replacing the Wasm frame with the exit trampoline's frame.

Now we have a stack like [host, entry trampoline, exit trampoline], which has zero Wasm frames between the entry and exit trampolines. If the host function that the exit trampoline calls out to attempts to capture a backtrace, then -- before this commit -- we would fail an internal assertion and panic. That panic would then unwind to the first Rust frame that is called by Wasm. With Rust 1.81 and later, Rust automatically inserts a panic handler that prevents the unwind from continuing into external/foreign code, which is undefined behavior, and aborts the process. Rust versions before 1.81 would attempt to continue unwinding, hitting undefined behavior.

This commit fixes the backtrace capturing machinery to handle empty sequences of Wasm frames, passes the assertion, and avoids unwinding into external/foreign code.

This fixes a bug where we would not properly handle contiguous sequences
of Wasm frames that are empty. This was mistakenly believed to be an impossible
scenario, and before the tail-calls proposal it was impossible, however it can
now happen after the following series of events:

* Host calls into Wasm, pushing the entry trampoline frame.

* Entry trampoline calls the actual Wasm function, pushing a Wasm frame.

* Wasm function tail calls to an imported host function, *replacing*
  the Wasm frame with the exit trampoline's frame.

Now we have a stack like `[host, entry trampoline, exit trampoline]`, which has
zero Wasm frames between the entry and exit trampolines. If the host function
that the exit trampoline calls out to attempts to capture a backtrace, then --
before this commit -- we would fail an internal assertion and panic. That panic
would then unwind to the first Rust frame that is called by Wasm. With Rust 1.81
and later, Rust automatically inserts a panic handler that prevents the unwind
from continuing into external/foreign code, which is undefined behavior, and
aborts the process. Rust versions before 1.81 would attempt to continue
unwinding, hitting undefined behavior.

This commit fixes the backtrace capturing machinery to handle empty sequences of
Wasm frames, passes the assertion, and avoids unwinding into external/foreign
code.

Co-Authored-By: Alex Crichton <alex@alexcrichton.com>
@alexcrichton alexcrichton requested a review from a team as a code owner October 9, 2024 15:13
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team October 9, 2024 15:13
@fitzgen fitzgen enabled auto-merge October 9, 2024 15:21
@fitzgen fitzgen added this pull request to the merge queue Oct 9, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Oct 9, 2024
@alexcrichton alexcrichton added this pull request to the merge queue Oct 9, 2024
Merged via the queue into bytecodealliance:main with commit 75a10e1 Oct 9, 2024
39 checks passed
@alexcrichton alexcrichton deleted the fix-main branch October 9, 2024 16:13
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