r/rust • 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..)

0 Upvotes

36 comments sorted by

35

u/Tastaturtaste 2d ago

Ran this on the playground with miri. Miri actually detects this as undefined behavior.

-2

u/koczurekk 2d ago

Cool, but miri, as per its own documentation, is a tool to detect unsafe code that fails to uphold its safety requirements.

The fact that the Rust compiler passes this is a bug, which is why it has an open issue on GitHub. The fact that miri catches it is a byproduct of what it's actually meant to do, and in no way diminishes the underlying problem. I'm honestly confused why you're framing a confirmation of the bug as a reason to dismiss it, unless I misunderstood your wording.

10

u/Tastaturtaste 1d ago

In what way did I dismiss the problem? I think unsoundness bugs (like this one) need to be and are the biggest priority for the rust project. I did not in any way mean to imply that this bug is lower priority because of miri.

The fact that miri is intended to check unsafe code is a byproduct of the fact that safe code is expected to not be able to cause UB. 

-11

u/Impossible-Track3875 2d ago

It's nice that miri caught it, really that's the best thing about this whole thing. It shows that the unsafe code guidelines are tight enough that miri can catch it. It's fine that the compiler doesn't, as it's not something people would ever write accidentally, the inner expression is so cursed.

-12

u/dobkeratops rustfind 2d ago

"miri detects it" .. is that a seperate tool ? something in the cargo tools ecosystem?
is that an extra optional check based on rust's IR
is this used to try to check unsafe code or something ?

4

u/noop_noob 2d ago

It's an interpreter for rust. It runs a rust program, and checks at run time if UB happened. It's not used in production because it's ridiculously slow.

1

u/dobkeratops rustfind 2d ago

so something like "valgrind" in the C++ ecosystem ?

5

u/noop_noob 2d ago

Similar. But miri is much more reliable at detecting UB.

9

u/yodal_ 2d ago

Miri is a separate tool used to detect undefined behavior just like this.

-8

u/danted002 2d ago

Manual drops should really be marked as unsafe

11

u/garagedragon 2d ago

That doesn't really make sense. std::mem::drop is just an empty body that consumes its argument by normal language rules, you can't make that unsafe because it's not actually doing anything magic.

27

u/rundevelopment 2d ago

4

u/olegispe 2d ago

With the same code verbatim. OP seems to be karma farming

7

u/dobkeratops rustfind 2d ago

"OP" is just curious, posting questions in forums to get replies is "Farming" ?!

it's nice to talk about programming languages with other programmers on the web, isn't it ?

4

u/olegispe 2d ago

Fair enough. u/noop_noob was right, I did think you claimed to find it

That’s my bad. It’s good to ask questions. Genuinely sick of people copy pasting ChatGPT/Claude and thinking it makes them a rust programmer, or a programmer in general

Asking pseudo-complicated questions

Good to find other rustaceans 🙌

3

u/dobkeratops rustfind 2d ago

heh. Yeah I dont use claude at all , I code for personal satisfaction and mental exercise primarily. I've been using Rust on and off since 2014 (and it became my main language maybe 5 years ago). i make one of the "50 game engines" and a boomer shooter tech demo of sorts in it.

8

u/noop_noob 2d ago

They misunderstood what you wrote. They thought you said "I used claude and it found rust compiler bugs".

5

u/moltonel 2d ago

If you're curious about this bug, you might want to look at the other current soundness bugs too. Despite the somewhat hyped reactions to this announcement, it's business as usual for most compilers.

0

u/[deleted] 2d ago

[deleted] — view deleted comment

1

u/noop_noob 2d ago edited 2d ago

No, it's actually found by Claude, according to a blog post by Anthropic. Not OP's claude though.

https://www.anthropic.com/research/finding-logic-bugs

-1

u/dobkeratops rustfind 2d ago

I've never used claude myself.
I found this on X where I usually defend rust.

4

u/matthieum [he/him] 1d ago

In particular, as mentioned in the issue, the bug is specifically about built-in indexing.

For any regular types, using the Index trait, the array is borrowed from the point &mut arr[0] is written, preventing the move in the second index expression.

It just happens that indexing on arrays (slices?) is built-in, instead, and the lowering "fails" to borrow.

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.

-3

u/dobkeratops rustfind 2d ago

if there's nothing to discuss why reply..

i'm just curious e.g. deeper explanation of where this came from
i do remember that intiially rust required you to split expresions up more to pass the checker. Is this something that wouldn't have passed an earlier version ? when did it get introduced ?etc.

someone else suggests there's already a tool that can catch it ("miri") again i hadn't heard of that.

5

u/Zde-G 2d ago

There is “nothing to discuss”, which is not the same as “nothing to do”.

Soundness bugs existed before and would exist in the future. That's the price we pay for an attempt to make Rust more ergonomic: every time rules of borrow checker are altered to accept more patterns there are a small chance of introducing the soundness bug… if that happens it should be reported to be, eventually, fixed.

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 of arr later in arr[0] ought to throw an error.

At least, that's how I read it.

Conceptually I'd expect the compiler to do:

let __1 = 0;
let __2 = { let [row] = arr; drop(row); 0};
let alias = &mut arr[__1][__2];

so it should be clear that the arr cannot be re-used.

Unless I'm mistaken, which is completely possible.

1

u/dobkeratops rustfind 2d ago

yes when you extract the inner expression as "__2" as you have here.. the compiler does infact give you 'use of partially moved value' and rejects it.. I think this is something arrising from being able to destructure array elements and move independentyly? its something that would happen with struct fields more frequently perhaps .. but the bug here relies on array indexing. It's so obscure i doubt anyone has ever done this deliberately, but it does mean someone could hide something nefarious in an otherwise safe looking block if they had malicious intent

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/panstromek 2d ago

There's a bisection and a lot more info in the linked issue

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

5

u/lenscas 2d ago

It is a real bug in the compiler that allow you to accidentally "escape" the borrowchecker and run straight into UB without writing any unsafe code. And while this exact code is weird and probably won't be written (at least, not on purpose). This is the smallest example that triggers it, there might be other ways to run into this same bug which are much closer to code people would actually write.

So, why would it matter who or what found it?

0

u/dobkeratops rustfind 2d ago

dropping the anti-AI edge (yes I am irritated by AI hyperscalers) it's useful information, i.e. *yes there is something that a bad actor could sneak into a safe looking rust repo*
.. it's worth paying attention to,
.. and besides the AI aspect , it's ammo that anti-Rust people can use.