std support for wasm32 panic=unwind by coolreader18 · Pull Request #121438 · rust-lang/rust (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation34 Commits5 Checks0 Files changed
Conversation
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
[ Show hidden characters]({{ revealButtonHref }})
Tracking issue: #118168
This adds std support for -Cpanic=unwind
on wasm, and with it slightly more fleshed out rustc support. Now, the stable default is still panic=abort without exception-handling, but if you -Zbuild-std
with RUSTFLAGS=-Cpanic=unwind
, you get wasm exception-handling try/catch blocks in the binary:
#[no_mangle] pub fn foo_bar(x: bool) -> *mut u8 { let s = Box::::from("hello"); maybe_panic(x); Box::into_raw(s).cast() }
#[inline(never)] #[no_mangle] fn maybe_panic(x: bool) { if x { panic!("AAAAA"); } }
;; snip... (try labellabellabel5 (do (call $maybe_panic (local.get $0) ) (br labellabellabel1) ) (catch_all (global.set $__stack_pointer (local.get $1) ) (call $__rust_dealloc (local.get $2) (i32.const 5) (i32.const 1) ) (rethrow labellabellabel5) ) ) ;; snip...
r? @cuviper
rustbot has assigned @cuviper.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.
Use r? to explicitly pick a reviewer
rustbot added S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
Relevant to the compiler team, which will review and decide on the PR/issue.
Relevant to the library team, which will review and decide on the PR/issue.
labels
Also, though this is more of a future concern, idk if a panic=unwind
std would be able to run nicely in a panic=abort
binary atm - if the user is trying to keep backwards compatibility to wasm MVP, llvm or something would have to strip the try
instructions from the end result.
extern "C" { |
---|
/// LLVM lowers this intrinsic to the `throw` instruction. |
#[link_name = "llvm.wasm.throw"] |
fn wasm_throw(tag: i32, ptr: *mut u8) -> !; |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a new rust intrinsic which lowers to this LLVM intrinsic when using the LLVM backend?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I wanna put it in stdarch, alongside the other wasm instructions. A full extern "rust-intrinsic"
intrinsic shouldn't be necessary.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustbot added the T-bootstrap
Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
label
This PR changes how LLVM is built. Consider updating src/bootstrap/download-ci-llvm-stamp.
Ok, now wasi uses libunwind, while -unknown still uses wasm.throw directly. It does seem like the scheme that LLVM implements is a tooling convention (so, not just LLVM's specific implementation) but it does say it's for C++ specifically, and says other languages should have distinct tags. However, doing that means (unless I'm reading the exception-handling proposal wrong) that exceptions caught in a different language can't be cleaned up. But maybe that's fine, for now.
Oh, actually, if we had a distinct tag for panics it would just mean that a C++ exception would skip catch_unwind
. Which is fine, if I'm reading the C-unwind rfc correctly.
As noted in the summary, if a Rust frame containing a pending catch_unwind call is unwound by a foreign exception, the behavior is undefined for now.
If we really want to process a C++ exception with catch_unwind in the future, we could just implement the itanium wasm EH scheme in library/unwind and codegen a catch $cpp_exc_tag
for r#try
. Once llvm supports that.
Actually, knowing that, do you think wasi even needs llvm's libunwind? We can handle it just fine ourselves, obviously, and it will indeed cause UB at catch_unwind, but that's fine, and that'll be fixable if we want to once LLVM lets you specify other tags.
I've convinced myself I think, lol
This was referenced
Feb 28, 2024
I'm not clear, is this waiting on the stdarch change, or vice versa?
Vice versa - the stdarch PR will fail until this is in nightly.
📌 Commit c7fcf43 has been approved by cuviper
It is now in the queue for this repository.
bors added the S-waiting-on-bors
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
label
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request
…r=cuviper
std support for wasm32 panic=unwind
Tracking issue: rust-lang#118168
This adds std support for -Cpanic=unwind
on wasm, and with it slightly more fleshed out rustc support. Now, the stable default is still panic=abort without exception-handling, but if you -Zbuild-std
with RUSTFLAGS=-Cpanic=unwind
, you get wasm exception-handling try/catch blocks in the binary:
#[no_mangle]
pub fn foo_bar(x: bool) -> *mut u8 {
let s = Box::<str>::from("hello");
maybe_panic(x);
Box::into_raw(s).cast()
}
#[inline(never)]
#[no_mangle]
fn maybe_panic(x: bool) {
if x {
panic!("AAAAA");
}
}
;; snip...
(try <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>5
(do
(call $maybe_panic
(local.get $0)
)
(br <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>1)
)
(catch_all
(global.set $__stack_pointer
(local.get $1)
)
(call $__rust_dealloc
(local.get $2)
(i32.const 5)
(i32.const 1)
)
(rethrow <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>5)
)
)
;; snip...
bors added a commit to rust-lang-ci/rust that referenced this pull request
This was referenced
Mar 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request
…kingjubilee
Rollup of 15 pull requests
Successful merges:
- rust-lang#116791 (Allow codegen backends to opt-out of parallel codegen)
- rust-lang#116793 (Allow targets to override default codegen backend)
- rust-lang#117458 (LLVM Bitcode Linker: A self contained linker for nvptx and other targets)
- rust-lang#119385 (Fix type resolution of associated const equality bounds (take 2))
- rust-lang#121438 (std support for wasm32 panic=unwind)
- rust-lang#121893 (Add tests (and a bit of cleanup) for interior mut handling in promotion and const-checking)
- rust-lang#122080 (Clarity improvements to
DropTree
) - rust-lang#122152 (Improve diagnostics for parenthesized type arguments)
- rust-lang#122166 (Remove the unused
field_remapping
field fromTypeLowering
) - rust-lang#122249 (interpret: do not call machine read hooks during validation)
- rust-lang#122299 (Store backtrace for
must_produce_diag
) - rust-lang#122318 (Revision-related tweaks for next-solver tests)
- rust-lang#122320 (Use ptradd for vtable indexing)
- rust-lang#122328 (unix_sigpipe: Replace
inherit
withsig_dfl
in syntax tests) - rust-lang#122330 (bootstrap readme: fix, improve, update)
r? @ghost
@rustbot
modify labels: rollup
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request
Rollup merge of rust-lang#121438 - coolreader18:wasm32-panic-unwind, r=cuviper
std support for wasm32 panic=unwind
Tracking issue: rust-lang#118168
This adds std support for -Cpanic=unwind
on wasm, and with it slightly more fleshed out rustc support. Now, the stable default is still panic=abort without exception-handling, but if you -Zbuild-std
with RUSTFLAGS=-Cpanic=unwind
, you get wasm exception-handling try/catch blocks in the binary:
#[no_mangle]
pub fn foo_bar(x: bool) -> *mut u8 {
let s = Box::<str>::from("hello");
maybe_panic(x);
Box::into_raw(s).cast()
}
#[inline(never)]
#[no_mangle]
fn maybe_panic(x: bool) {
if x {
panic!("AAAAA");
}
}
;; snip...
(try <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>5
(do
(call $maybe_panic
(local.get $0)
)
(br <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>1)
)
(catch_all
(global.set $__stack_pointer
(local.get $1)
)
(call $__rust_dealloc
(local.get $2)
(i32.const 5)
(i32.const 1)
)
(rethrow <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>a</mi><mi>b</mi><mi>e</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">label</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>5)
)
)
;; snip...
tgross35 added a commit to tgross35/rust that referenced this pull request
… r=bjorn3
Use throw intrinsic from stdarch in wasm libunwind
Tracking issue: rust-lang#118168
This is a very belated followup to rust-lang#121438; now that rust-lang/stdarch#1542 is merged, we can use the intrinsic exported from core::arch
instead of defining it inline. I also cleaned up the cfgs a bit and added a more detailed comment.
tgross35 added a commit to tgross35/rust that referenced this pull request
… r=bjorn3
Use throw intrinsic from stdarch in wasm libunwind
Tracking issue: rust-lang#118168
This is a very belated followup to rust-lang#121438; now that rust-lang/stdarch#1542 is merged, we can use the intrinsic exported from core::arch
instead of defining it inline. I also cleaned up the cfgs a bit and added a more detailed comment.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request
Rollup merge of rust-lang#131418 - coolreader18:wasm-exc-use-stdarch, r=bjorn3
Use throw intrinsic from stdarch in wasm libunwind
Tracking issue: rust-lang#118168
This is a very belated followup to rust-lang#121438; now that rust-lang/stdarch#1542 is merged, we can use the intrinsic exported from core::arch
instead of defining it inline. I also cleaned up the cfgs a bit and added a more detailed comment.
Labels
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Relevant to the compiler team, which will review and decide on the PR/issue.
Relevant to the library team, which will review and decide on the PR/issue.