News for hackers. Stuff that matters.
Re:Achievement unlocked: rustc segfault by kelnos on Sunday April 10, 2022 @06:50PM (#30982530)
Perhaps I missed it, but too bad OP didn't submit a fix to LLVM as well, or at least file a bug report there. Sure, rustc was emitting bad IR, but LLVM shouldn't crash! It should catch the issue and exit cleanly with an error message. Probably would have been easier to debug this issue if LLVM hadn't crashed in the first place, too.

Either way, a really fun read. For some reason I enjoy reading debugging stories for bugs that I almost certainly wouldn't be able to solve myself.

Re:Achievement unlocked: rustc segfault by mshockwave on Sunday April 10, 2022 @07:17PM (#30982739)
> It should catch the issue and exit cleanly with an error message.

IIRC LLVM's IR verification is not enabled in release build. In other words, if you're using rustc with debug version of LLVM the error message should pop up.

EDIT: "...LLVM's IR verification is not enabled in release build..." this is wrong, LLVM doesn't turn off verification based on its build mode. It is up to the user of LLVM, namely rustc in this case, to enable verification. For instance, you can verify IR after each optimization Pass (which is pretty expensive) by configuring `llvm::StandardInstrumentation` properly. Or you can verify the IR before codegen by switching up one of `llvm::TargetMachine`'s options. Clang always enables the latter (verification) by default but disables the former regardless of the optimization level or its build mode.

Re:Achievement unlocked: rustc segfault by est31 on Sunday April 10, 2022 @08:30PM (#30983228)
The rust compiler has an option to enable llvm assertions, but it needs to be set at compile time (of the compiler) in config.toml: https://github.com/rust-lang/rust/blob/1f7fb6413d6d6c0c929b2...

I don't know how these checks compare to what clang is doing.

Re:Achievement unlocked: rustc segfault by mshockwave on Sunday April 10, 2022 @09:29PM (#30983550)
the root cause of the original problem is caused by invalid IR, which can be caught by IR verification -- which is actually different from assertion.
Re:Achievement unlocked: rustc segfault by jackosdev on Monday April 11, 2022 @03:12AM (#30985617)
So would something like miri have caught this if it was in their CI?
Re:Achievement unlocked: rustc segfault by rob74 on Monday April 11, 2022 @02:57AM (#30985529)
...and it's probably not enabled because the Rust compiler is already slow enough as it is? But yeah, I guess it's a fair trade-off having 0.0001% of builds crash if it makes the other 99.9999% a bit faster...
Re:Achievement unlocked: rustc segfault by EE84M3i on Sunday April 10, 2022 @08:13PM (#30983120)
In either case dereferencing a null pointer would still be a bug, right? Or is it kind of "all bets are off" if you feed LLVM bad IR and don't enable verification?
Re:Achievement unlocked: rustc segfault by tedunangst on Sunday April 10, 2022 @09:50PM (#30983669)
If you call free((void *)-1) in C code, it will very likely crash, but it will crash in libc, not your program. Is that a bug in libc?
Re:Achievement unlocked: rustc segfault by MaxBarraclough on Monday April 11, 2022 @03:47PM (#30993835)
I think even deriving the argument might invoke undefined behaviour here, but I'm not certain.

If I understand correctly, it's undefined behaviour to do this:

    int *ptr = (int*)42;
As, in order to avoid undefined behaviour, ptr should only be assigned a valid address of an int, or the 'address' one past the end of an array of int, or NULL (logical zero).

It's possible things are different when the type is void*, I'm not certain.

Re:Achievement unlocked: rustc segfault by tedunangst on Monday April 11, 2022 @04:19PM (#30994317)
Make it free(free) if you want it to be a valid pointer.
Re:Achievement unlocked: rustc segfault by kelnos on Friday April 15, 2022 @12:51AM (#31036691)
No, because the defined contract for free() is that you pass it a pointer that was previously returned from malloc(), that you don't try to free() it twice, etc.
Re:Achievement unlocked: rustc segfault by Thorrez on Monday April 11, 2022 @12:15AM (#30984598)
That's documented undefined behavior.

Is it documented undefined behavior to feed bad IR into LLVM?

Re:Achievement unlocked: rustc segfault by scatters on Monday April 11, 2022 @02:51AM (#30985487)
Of course. If there's an input checking mode, and you disable the checks, then you're guaranteeing that you won't supply invalid input.
Re:Achievement unlocked: rustc segfault by mshockwave on Sunday April 10, 2022 @09:39PM (#30983605)
LLVM makes trade off on enabling/disabling certain checks, including assertions and IR verification, primarily to keep compilation time acceptable in release build. The idea is that we catch as many bugs as possible in debug version of LLVM such that release version can run fast.
Re:Achievement unlocked: rustc segfault by theresistor on Sunday April 10, 2022 @08:53PM (#30983351)
LLVM makes use of assertions to validate things like this, but many users of LLVM, including rustc, turn them off for performance reasons.
Re:Achievement unlocked: rustc segfault by comex on Sunday April 10, 2022 @08:23PM (#30983180)
It is.
Re:Achievement unlocked: rustc segfault by eyelidlessness on Sunday April 10, 2022 @08:48PM (#30983328)
Terse affirmative is ambiguous.
Re:Achievement unlocked: rustc segfault by nlewycky on Sunday April 10, 2022 @09:44PM (#30983634)
If you feed LLVM bad IR, all bets are off. LLVM's assertions and IR verifier are impressively comprehensive, but not a guarantee.

For example, LLVM has a pointer type and a void type, but you may not make a pointer-to-void type, see https://llvm.org/docs/LangRef.html#pointer-type . If you do call `Type::getVoidTy(C)->getPointerTo()` then LLVM will hit an assertion only in a build with assertions enabled. Without assertions LLVM may silently execute UB.

Re:Achievement unlocked: rustc segfault by akimball on Monday April 11, 2022 @08:15AM (#30987637)
As are all tautologies;)
Re:Achievement unlocked: rustc segfault by pwr-electronics on Monday April 11, 2022 @10:00AM (#30988793)
> It should catch the issue and exit cleanly with an error message.

Probably not. As the author describes, LLVM has to tool to check for invalid IR, which they used to investigate the issue and generate an explanatory error message.