# \`core::intrinsics::abort\` in no\_std?

**URL:** <https://internals.rust-lang.org/t/core-abort-in-no-std/8772>\
**Category:** libs\
**Created:** [November 5, 2018, 1:43pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772 "2018-11-05T13:43:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepyakin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pepyakin/32/1581_2.png) [@pepyakin](https://internals.rust-lang.org/u/pepyakin)\
**Post date:** [November 5, 2018, 1:43pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/1 "2018-11-05T13:43:39Z")

</div>

I’m working on a project that uses Rust to create a WebAssembly binary which will be executed in a non-web (and even non-JS) environment. I have been using a nightly compiler but with the recent release, I started considering switching to stable. Unfortunately, I discovered some roadblocks preventing me from doing that.

My use-case is as follows:

I need to implement `#[panic_handler]` which performs some debug logging (which in my case is pretty much host-environment specific) and then just terminates execution via trap (ideally, with wasm’s `unreachable` instruction). For the moment the code is using [`core::intrinsics::unreachable`](https://doc.rust-lang.org/1.30.0/std/intrinsics/fn.unreachable.html) and it requires nightly `feature(core_intrinsics)`. Technically, it’s possible to change it to [`std::hint::unreachable_unchecked`](https://doc.rust-lang.org/1.30.0/std/hint/fn.unreachable_unchecked.html) and get rid of the nightly feature. But this is still not a good idea since it’s an UB if the control flow reaches this function. It works for now because the optimizer is tied by immediately preceding calls to external functions I suppose, but this still looks too fragile.

I could try to abort the execution by different means like dividing by zero or adding a special function to the host environment sole purpose of which is just to trap. However, I think that those are awkward solutions.

What I think I really need is to use [`core::intrinsics::abort`](https://doc.rust-lang.org/1.30.0/core/intrinsics/fn.abort.html). As far as I understood it is lowered down to [`ud2`](https://mudongliang.github.io/x86/html/file_module_x86_id_318.html), [`unreachable`](https://webassembly.github.io/spec/core/exec/instructions.html#exec-unreachable) or similar instructions in non-std environments, and seems like this is exactly what I need.

The problem is that the stabilized version is [`std::process::abort`](https://doc.rust-lang.org/1.30.0/std/process/fn.abort.html) which implies the existence of the standard library around which is not my case.

What do you think is the best way forward here? Does this use-case sound legit for you? If so is there any chance of stabilizing of [`core::intrinsics::abort`](https://doc.rust-lang.org/1.30.0/core/intrinsics/fn.abort.html) for `no_std` environments separately?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 5, 2018, 3:10pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/2 "2018-11-05T15:10:39Z")

</div>

> [@pepyakin](#):
>
> For the moment the code is using [`core::intrinsics::unreachable`](https://doc.rust-lang.org/1.30.0/std/intrinsics/fn.unreachable.html) and it requires nightly `feature(core_intrinsics)` . Technically, it’s possible to change it to [`std::hint::unreachable_unchecked`](https://doc.rust-lang.org/1.30.0/std/hint/fn.unreachable_unchecked.html) and get rid of the nightly feature. But this is still not a good idea since it’s an UB if the control flow reaches this function.

The two have the exact same semantics.

I do agree that `abort` is definitely the semantics that you want here, however.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [November 5, 2018, 5:25pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/3 "2018-11-05T17:25:18Z")

</div>

Can you even abort in a free-standing context? `abort` is normally a syscall, but you can’t syscall in a kernel.

Unless this `abort` does something different?

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [November 5, 2018, 6:09pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/4 "2018-11-05T18:09:37Z")

</div>

The abort intrinsic is more “lightweight” - it turns into a `ud2` on x86\_64 for example. I wouldn’t be surprised if it still generated a call to libc abort on some architectures though.

---

<div class="post-metadata">

**Author:** ![pepyakin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pepyakin/32/1581_2.png) [@pepyakin](https://internals.rust-lang.org/u/pepyakin)\
**Post date:** [November 5, 2018, 6:15pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/5 "2018-11-05T18:15:38Z")

</div>

> The two have the exact same semantics.

Yes, and they both suffer from the mentioned problem!

> Can you even abort in a free-standing context? `abort` is normally a syscall, but you can’t syscall in a kernel. Unless this `abort` does something different?

Good questions. Indeed, `abort` is usually used as a name of a syscall, but here we have something different. As far as I understand, `core::intrinsics::abort` is lowered to `llvm.trap` which is [defined as follows](https://llvm.org/docs/LangRef.html#llvm-trap-intrinsic):

> This intrinsic is lowered to the target dependent trap instruction. If the target does not have a trap instruction, this intrinsic will be lowered to a call of the `abort()` function.

It seems it is **not exactly** what I need. I'm not familiar with LLVM inner workings, but I wonder what LLVM would do if the target platform doesn't have a special instruction for trap nor libc's `abort` function : )

Another option would be to consider going via platform specific `core::arch::wasm32::unreachable`?

---

<div class="post-metadata">

**Author:** ![pepyakin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pepyakin/32/1581_2.png) [@pepyakin](https://internals.rust-lang.org/u/pepyakin)\
**Post date:** [December 4, 2018, 3:02pm UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/6 "2018-12-04T15:02:17Z")

</div>

This solution could help in my case: [https://github.com/rust-lang/rust/issues/54981#issuecomment-443369450](https://github.com/rust-lang/rust/issues/54981#issuecomment-443369450)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/core-abort-in-no-std/8772/7 "2019-03-25T08:31:06Z")

</div>

Closed on March 25, 2019, 8:31am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
