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 }})

coolreader18

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...

@rustbot

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 rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

T-libs

Relevant to the library team, which will review and decide on the PR/issue.

labels

Feb 22, 2024

coolreader18

@coolreader18

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.

bjorn3

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.

bjorn3

bjorn3

@rustbot rustbot added the T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

label

Feb 22, 2024

@rustbot

This PR changes how LLVM is built. Consider updating src/bootstrap/download-ci-llvm-stamp.

@coolreader18

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.

@coolreader18

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.

@coolreader18

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.

@coolreader18

I've convinced myself I think, lol

@coolreader18

bjorn3

@coolreader18

This was referenced

Feb 28, 2024

@cuviper

I'm not clear, is this waiting on the stdarch change, or vice versa?

@coolreader18

Vice versa - the stdarch PR will fail until this is in nightly.

@cuviper

@bors

📌 Commit c7fcf43 has been approved by cuviper

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

label

Mar 4, 2024

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Mar 5, 2024

@matthiaskrgr

…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

Mar 5, 2024

@bors

This was referenced

Mar 11, 2024

bors added a commit to rust-lang-ci/rust that referenced this pull request

Mar 11, 2024

@bors

…kingjubilee

Rollup of 15 pull requests

Successful merges:

r? @ghost @rustbot modify labels: rollup

rust-timer added a commit to rust-lang-ci/rust that referenced this pull request

Mar 11, 2024

@rust-timer

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

Oct 12, 2024

@tgross35

… 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

Oct 13, 2024

@tgross35

… 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

Oct 13, 2024

@rust-timer

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

S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

T-libs

Relevant to the library team, which will review and decide on the PR/issue.