Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd `segfault` keyword #2446
Comments
This comment has been minimized.
This comment has been minimized.
|
How is this different from https://doc.rust-lang.org/stable/std/process/fn.abort.html ? |
This comment has been minimized.
This comment has been minimized.
CryZe
commented
May 24, 2018
•
|
It reserves a keyword for it integrating it further into the language. |
This comment has been minimized.
This comment has been minimized.
|
What's the benefit of that? |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
•
|
|
This comment has been minimized.
This comment has been minimized.
CryZe
commented
May 24, 2018
•
|
What about platforms that don't have segfaults? Like wasm. Would that just abort then? Also in case this isn't just bait: Is there an actual benefit to segfaulting manually? |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
Surely wasm has a null dereference? |
This comment has been minimized.
This comment has been minimized.
CryZe
commented
May 24, 2018
|
It just reads from the 0 address just fine without trapping / panicking or anything. |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
can you set the 0 address as an abort trap? I doubt rust would be able to use the 0 address anyway, because of how references work... |
This comment has been minimized.
This comment has been minimized.
|
So... I don't exactly understand what the point of intentionally causing a segfault is... why would you want to do that? |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
Sometimes, a program is written expecting another program to segfault. |
This comment has been minimized.
This comment has been minimized.
Oh man, this happens all the time if you are working on validation of legacy x86 features, just... not with Rust and not with keywords |
This comment has been minimized.
This comment has been minimized.
|
@SoniEx2 @petrochenkov Interesting; Got any resources I can study re. this? (I think a keyword for something surely so niche is not warranted) |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
I have a program at work that "expects" the target program to segfault, and restarts it when it does so. A non-segfaulting exit is treated as success. |
This comment has been minimized.
This comment has been minimized.
|
EDIT: On my platform (x86_64 Linux) and on Playground, this program segment causes segfault with debug mode. |
This comment has been minimized.
This comment has been minimized.
warlord500
commented
May 24, 2018
•
|
@ordovicia that only works on some platforms and some times, its techinically UB which isnt what @SoniEx2 is asking for. @SoniEx2, why not change the program so that panics or aborts work as errors in your program at work?, I am not sure what would the most effective way to cause a segfault intentionally on all platforms, but I think the most reliable to way to force an error exit would be via panic or abort. segfault as a keyword simply wouldnt pull its weight,a function maybe but you would have to more percise as in why abort wouldnt work |
This comment has been minimized.
This comment has been minimized.
|
@warlord500 Oh, you are quite right. And that's why optimization changes the behavior of my program above. Thanks! |
This comment has been minimized.
This comment has been minimized.
|
@Centril |
This comment has been minimized.
This comment has been minimized.
|
There's no way this justifies a new keyword. I don't think it even justifies a place in the standard library. This could easily be implemented in a crate using OS-specific system calls to trigger the desired behaviour. Segfaults are not a portable concept to begin with, and AFAIK you can't generate a segfault with portable LLVM IR without invoking undefined behaviour, so it needs to be solved at the OS level anyway. |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
•
|
Oh please there are very good backwards-compatibility (as in, "reimplementing things in rust in a way that's backwards compatible with the original C code, including equivalent failure modes") reasons to have this. LLVM can be patched, or you can use inline asm. |
This comment has been minimized.
This comment has been minimized.
CryZe
commented
May 24, 2018
•
|
How does explicitly segfaulting work in C? |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
It just... happens. |
This comment has been minimized.
This comment has been minimized.
CryZe
commented
May 24, 2018
|
So just like in Rust? Why does Rust need a keyword / library function if C doesn't have that either? |
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
Rust doesn't segfault, unless something else does. Rust can segfault if a C library you're using segfaults, or if your unsafe code is broken, etc. But when reimplementing something in safe Rust, it really doesn't segfault. It may panic, but that's still different from a segfault. |
This comment has been minimized.
This comment has been minimized.
|
Safe Rust doesn't segfault but unsafe certainly can.
|
This comment has been minimized.
This comment has been minimized.
SoniEx2
commented
May 24, 2018
|
No it doesn't. |
This comment has been minimized.
This comment has been minimized.
|
@SoniEx2 it doesn't in this case because it invokes undefined behaviour which the compiler optimises away. Note that if you compiled the exact equivalent C/C++ code with clang and the same optimisation level, it would likely do the exact same thing, and would not be guaranteed to segfault. |
This comment has been minimized.
This comment has been minimized.
|
You should use the use nix::sys::signal;
fn main() {
signal::raise(signal::Signal::SIGSEGV);
} |
This comment has been minimized.
This comment has been minimized.
barzamin
commented
May 24, 2018
•
|
Note that there is not a standardized way to segfault across every arch/OS combination. In certain settings, 'segfaulting' isn't even a thing you can do that makes sense. What's a 'segfault' on ring0 x86_64? no-std Cortex M0? wasm? To make 'segfault' a keyword would definitely fall under the heading of language bloat, in addition to it being an extremely context-dependent operation. Additionally, note that segfaulting on Linux, SIGSEGV notwithstanding, must necessarily invoke UB of some kind - eg, dereffing an invalid pointer. |
This comment has been minimized.
This comment has been minimized.
|
On x86_64 dereferencing But also, I'm not sure how much I should interpret this as trolling and how much I should interpret it as "I intentionally want segfaults, but I'm exaggerating the desire for a keyword." |
This comment has been minimized.
This comment has been minimized.
|
@Diggsey's suggestion seems to be the solution here, so I'm going to close. |
SoniEx2 commentedMay 24, 2018
There should be a keyword for easy segfaultation. The main use-case is to make your program segfault at runtime.
It's not one of the most common use-cases, but every other systems programming language has an easy to use segfault operation. It seems fairly reasonable to add it to Rust so that Rust stays relevant in the systems programming language category.
I propose
segfaultbe reserved in Rust 2018.