r/rust • u/dobkeratops rustfind • 2d ago
🎙️ discussion Rust Compiler bug found by claude (use-after-free via partial move), discuss
"compiler bugs found by claude"
fn main() {
let mut arr = [[Box::new(42)]];
let alias = &mut arr[0][{ let [row] = arr; drop(row); 0}];
println!("{}"; **alias); // use-after-free. of arr's dead stack slot
}
.. now i'm guessing this turns up in real code so infrequently that it doesn't show up in an empirical analysis of program bugs and vulnerabilities (i've certainly never ran into it).
the problem here appears to be a partial move and reuse of a value in the same expression, and when you pull that inner expression out, the borrow checker catches it.
i recall how earlier past v1.0 there were many occasions of the opposite situation where inner expressions wouldn't pass the borrow checker and you needed to sometimes split code up more than you would in C++ just to get past the checker, then at some point the borrow checker got more sophisticated and let those cases through. I'm guessing this logic bug might have gotten introduced around that time?
naturally i think that people yelling about this probably either 'just dont like rust' or 'have a lot of c++ that they do not want to rewrite' (both are valid reasons to not use it, but peolpe tend to not want to admit that, and come up with technical arguments.. i've seen far worse..) ..
nonetheless is there a plan toward fixing this r.e. the propoganda effects ..
maybe as an interim a warning could be introduced that could be rolled out quicker (wont change what programs are accepted/rejected, but will guide people away from writing this hazard)
i must say the example looks particularly contrived and I'm not surprised that i've never ran into it in 12 years or so of using the langauge on and off..
can anyone shed more light on this? "partial move.." .. might something like this happen with stuct member access aswell? (partial moves of struct members is probably more common, i haven't needed to do such a thing for array elements..)
35
u/Tastaturtaste 2d ago
Ran this on the playground with miri. Miri actually detects this as undefined behavior.
27
u/rundevelopment 2d ago
Compiler issue: https://github.com/rust-lang/rust/issues/160525
4
u/lfairy 2d ago
What is there to discuss? This is just a soundness bug, which Rust has had before.
You have a minimal example. File a bug and someone will fix it.
2
u/dgkimpton 2d ago
I'm not sure it's fair to call that a partial move (although that's indeed the error the compiler gives if you re-order the code, in itself that's weird) - after the destructuring
let [row] = arr;the arr has (should've) been consumed. The re-use ofarrlater inarr[0]ought to throw an error.At least, that's how I read it.
Conceptually I'd expect the compiler to do:
so it should be clear that the arr cannot be re-used.
Unless I'm mistaken, which is completely possible.
13
u/noop_noob 2d ago edited 2d ago
> maybe as an interim a warning could be introduced that could be rolled out quicker (wont change what programs are accepted/rejected, but will guide people away from writing this hazard)
There's a mechanism to do this in the compiler, known as a "future compatibility warning". This has been done when a lot of code relies on a compiler bug.
That's not the problem though. The hard part is modifying the compiler's check so that it rejects code that is actually problematic, and not just any code.
Anthropic found a bunch of bugs. Some of them are already fixed. https://github.com/rust-lang/rust/issues?q=is%3Aissue+involves%3Acarlini
So, it's a matter of "not enough people to do the work of fixing everything as quickly as you seem to want".
Anyway, here are all the currently unfixed ways to cause UB in safe code: https://github.com/rust-lang/rust/issues?q=state%3Aopen%20label%3A%22I-unsound%22 (some of them require unstable features though)
2
u/ProgrammingLanguager 2d ago
I wonder when this was introduced, running a regression test would be of use
3
u/pine_ary 2d ago
It’s very niche. But I’m glad we’re finding the soundness bugs. Not just in case anyone triggers this specific bug, but to prompt us to evaluate if there’s an underlying unsoundness that goes deeper than just the example.
1
u/ProgrammingLanguager 2d ago
Have you filed a rust-land/rustc issue? That's the appropriate space for this sort of conversation
-8
u/[deleted] 2d ago
Dude, its all about pumping their stock prices. It is a garbage info