-
Notifications
You must be signed in to change notification settings - Fork 13k
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
book: use abort() over loop {} for panic #38138
Conversation
I would like to hear what @rust-lang/libs thinks about this, and possibly other teams. |
How is |
Maybe for producing a
I don't know where you get that from. All the examples in that thread involve a
I have multiple problem with this perspective:
|
In all my experiments, Quoting the bug report:
And the relevant bug for pure infinite loops https://llvm.org/bugs/show_bug.cgi?id=965 is still open, thus I assume it should not be ub You are right of course, inline is not the culprit, but it's still a bug that moving the function into another compilation unit changes behaviour. |
Your experiments might have been too simplistic (e.g., no surrounding code that could be optimized further by replacing an infinite loop with
I don't think that deserves to be called a separate bug. We wouldn't even be asking the question if not for the principal bug (infinite loop == UB). But this is mostly a semantic quibble. In any case, it's not worth a separate issue because the problem disappears if the infinite loop == UB issue is fixed, and the general principle that splitting code into several crates can influence optimizations is well known and unavoidable. |
Your playground link does not use while true |
Ooops, sorry. Fixed. |
See #37088 for an example that used to do ub with while true and no ub with loop. Edit: i guess the mir optimizations changed that. |
There are multiple programs in #37088, all of them UB if I read the description right, some using |
I'm on mobile right now and can't reproduce it on the stable playground, so something has changed. I'll try with an older compiler once I'm back on a pc |
Almost every implementation and bit of docs I've ever seen on this use |
What exactly do you mean by "this" here? |
Documentation and examples around |
Ideally these intrinsics are forgotten details of the compiler as no std transitions to panic=abort. Unfortunately though doc tests are hard to write as such so we're forced to do something here. Can we just hide these functions and recommend panic=abort? I don't really like working around this llvm bug but a bug is a bug and we should strive to document working code here. |
@alexcrichton I tried to build a no_std executable with panic=abort in place of these intrinsics and couldn't get it to work. Is that option supported today? If so, that indeed sounds like what we should be recommending, even if we can't doctest it (we could still have a run-pass test). |
Yeah, you still have to implement at least |
@rkruppe to get it to truly work you'll need to recompile libcore/libstd as well because all libraries need to be compiled with |
Oh right, of course. Didn't notice that because I'm actually cross-compiling to a custom target so I'm building core as a normal dependency anyway (i.e., I have |
Alternatively, shouldn't one in principle be able to link to |
In theory, yes, linking |
So: what to do with this patch? |
First of all, I tried panic=abort + linking panic_abort and it still missed a lang item (panic_fmt), as @alexcrichton feared. There are also further issues with recommending panic=abort right now: As far as I can tell, panic=abort is not explained anywhere in the book. I couldn't even find and detailed explanation of unwinding or panics in general. This makes it problematic to just write 'use panic=abort'. In conclusion I'm inclined to argue this patch as-is (or at least a close variation, e.g. using libc abort instead of the intrinsic) represents the best we're gonna get for a while, i.e. until someone writes a chapter on panics and extends rustdoc to allow passing -C options to tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay here, @rkruppe . r=me after this nit.
#![no_std] | ||
use core::intrinsics::abort; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be use core::intrinsics
...
@@ -58,7 +55,7 @@ fn main(argc: isize, argv: *const *const u8) -> isize { | |||
} | |||
|
|||
#[lang = "eh_personality"] extern fn rust_eh_personality() {} | |||
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { loop {} } | |||
#[lang = "panic_fmt"] extern fn rust_begin_panic() -> ! { unsafe { abort() } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here, intrinsics::abort
? This is the standard style for importing functions.
Due to rust-lang#28728 loop {} is very risky and can lead to fun debugging experiences like in rust-lang#38136. Besides, aborting is probably better behavior than an infinite loop.
6687dcc
to
893f42a
Compare
Addressed nit. |
@bors: r+ rollup thanks! |
📌 Commit 893f42a has been approved by |
book: use abort() over loop {} for panic Due to rust-lang#28728 `loop {}` is very risky and can lead to fun debugging experiences such as rust-lang#38136. Besides, aborting is probably better behavior than an infinite loop. r? @steveklabnik
⌛ Testing commit 893f42a with merge ec9ae8c... |
💔 Test failed - status-travis |
@bors: retry
* sccache hang?
…On Mon, Jan 9, 2017 at 5:55 AM, bors ***@***.***> wrote:
💔 Test failed - status-travis
<https://travis-ci.org/rust-lang/rust/builds/190240467>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#38138 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95IclnyXzaeBvC1Fy3lOpByw1R_38ks5rQjxdgaJpZM4LDJdg>
.
|
⌛ Testing commit 893f42a with merge aa3ae13... |
💔 Test failed - status-appveyor |
@bors: retry
* network failure
…On Mon, Jan 9, 2017 at 9:18 AM, bors ***@***.***> wrote:
💔 Test failed - status-appveyor
<https://ci.appveyor.com/project/rust-lang/rust/build/1.0.1476>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#38138 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAD95GMB1s8kuickq2bzw6vL72jnvwT1ks5rQmv-gaJpZM4LDJdg>
.
|
book: use abort() over loop {} for panic Due to #28728 `loop {}` is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop. r? @steveklabnik
☀️ Test successful - status-appveyor, status-travis |
Due to #28728
loop {}
is very risky and can lead to fun debugging experiences such as #38136. Besides, aborting is probably better behavior than an infinite loop.r? @steveklabnik