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

Stabilize std::arch::wasm32::unreachable #61119

Closed
pepyakin opened this issue May 24, 2019 · 9 comments
Closed

Stabilize std::arch::wasm32::unreachable #61119

pepyakin opened this issue May 24, 2019 · 9 comments
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@pepyakin
Copy link
Contributor

This is a tracking issue to stabilize std::arch::wasm32::unreachable
The tentative signature is:

fn unreachable() -> !;

Semantics

This intrinsic represents instruction that is present in the MVP of WebAssembly: unreachable.

From the surface it is a simple instruction: it doesn't take any arguments nor return anything. Upon the execution it generates a trap, which terminates the execution of wasm immediately returning control to the embedder with an error. There are no way to catch a trap at the moment.

Note that technically a WebAssembly instance can be used after a trap. However, Rust doesn't guarantee that such a module is usable after a trap. For example, there is no guarantee that the stack will be reset to initial values or that any destructors be executed.

Stabilization in Rust

In a way, this intrinsic is similar to the currently stabilized std::process::abort function. In fact, std::process::abort is lowered to the unreachable instruction for wasm32-unknown-unknown - this is totally makes sense since wasm32-unknown-unknown doesn't make any assumptions of the host environment. However, there is a problem with std::process::abort, it is only available in std. Consider how you would implement a panic handler in WebAssembly for no_std mode when a trap is required:

#[panic_handler]
fn panic_handler(_: &PanicInfo) -> ! {
    unsafe {
        intrinsics::abort();
    }
}

This works, however, it requires #![feature(core_intrinsics)]. Here are other ways and why they are not appropriate:

  1. e.g. safe division by zero - requires panic, thus recursive. Unsafe division might work, but then it would require loop { } which might be a problem.
  2. e.g. make an access to the address 0xFFFFFFFF assuming that it is not accessible. Doesn't work if the module genuinely allocates 4GiBs. I guess that optimizer also might be able to figure out that it is being tricked with respective consequences.
  3. e.g. call_indirect suffer from the similar issues.
  4. If the host is known (which is likely), it might be possible to introduce a special host function sole function of which is just trap. But that feels hacky and clumsy as well.

Stabilization of unreachable will allow implementing the panic handler in no_std environments on stable.

cc @alexcrichton @sunfishcode

@jonas-schievink jonas-schievink added O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 24, 2019
@alexcrichton
Copy link
Member

@rfcbot fcp merge

Seems reasonable to me to stabilize!

@rfcbot
Copy link

rfcbot commented May 24, 2019

Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 24, 2019
@SimonSapin
Copy link
Contributor

Is this the same as core::intrinsics::abort? Should we have a stable portable function for this in libcore?

@alexcrichton
Copy link
Member

It is indeed the same, but there have been concerns about stabilizing intrinsics::abort historically due to some architectures not actually having an abort instruction (I think it was like msp430 or something like that)

@pepyakin
Copy link
Contributor Author

I guess almost all of MCUs don't have such an instruction. Maybe @japaric can confirm this?

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label May 24, 2019
@rfcbot
Copy link

rfcbot commented May 24, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label May 24, 2019
@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Jun 3, 2019
@rfcbot
Copy link

rfcbot commented Jun 3, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jun 3, 2019
@alexcrichton
Copy link
Member

Ok great! @pepyakin want to send a PR to stabilize to stdsimd?

Centril added a commit to Centril/rust that referenced this issue Jun 6, 2019
…hton

Update stdsimd

Ref rust-lang#61119,  for stabilization of wasm32 `unreachable`
Centril added a commit to Centril/rust that referenced this issue Jun 6, 2019
…hton

Update stdsimd

Ref rust-lang#61119,  for stabilization of wasm32 `unreachable`
@est31
Copy link
Member

est31 commented Aug 3, 2019

I think this can be closed @alexcrichton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants