How to COMPLETELY disable all Rust safety checks?

And this is undefined behavior, so your program is free to summon some nasal demons to go with your eaten laundry.

It is, and always will be, undefined behavior to use multiple aliasing &mut to the same data.

4 Likes

Sure, but in practice is it any more dangerous than doing the same in C?

If you have a &mut, yes it’s more dangerous than C. Rust takes advantage of its rules to tell LLVM more things about a &mut than a C compiler does about a pointer. (Violating the &mut aliasing rules in Rust is roughly like violating the restrict aliasing rules in C: extremely dangerous.)

4 Likes

That makes sense… in theory. Is there an example where rustc/llvm will actually perform an optimization that breaks multiple mutable references?

Here is a simple example where “broken” aliasing information leads to an optimization that will return incorrect results.

In foo, the compiler assumes that x and y don’t alias, and as a result it simply forwards the register value holding x as the function’s result - it assumes the store to y has no effect on x, but in reality it does here. The assembly of foo shows the issue:

playground::foo:
	movl	$2, (%rsi)
	movl	%edi, %eax
	retq

If you instead make foo use raw pointers, you’ll see that the compiler no longer makes aliasing assumptions, and the assembly is different as a result:

playground::foo:
	movl	$2, (%rsi)
	movl	(%rdi), %eax <-- this now reads/derefs the %rdi reg, holding `x`'s address
	retq

This is probably as basic of an example as one can show, but if you couple these types of “misoptimizations”, you can get all sorts of fun stuff to happen.

12 Likes

Undefined behavior is pretty much ‘catastrophic badness’ from a programmers’ perspective. It’s one thing to play fast and loose with your memory or types, but you’re on a whole different level when you invoke undefined behavior.

When it winds up being a middle-aged dude with mental health issues who nonetheless would like to learn to program, you wind up feeling like a jerk, though. Always worth keeping in mind the story on the other side of the Internet probably isn’t as simple as the story of our frustrations.

1 Like

We are drifting off-topic here people.

There is a simple question in the first post.
The premise of this forum, and our code of conduct, is to assume good intent in the user’s asking.
If you feel the question is worthwhile, answer it. If you feel it is not, move along to other threads where you’ll feel better.

In case you doubt the good intentions, flag it for our tireless moderators, who have thicker skin and more overview on the user. It’s not worth your good temper at best, and troll-feeding at worst.


As for the original question:
@Joe232, the thing you are asking is the programming equivalent if “how do I take the wheels away from my car, but still race it on the highway”. The answer is “you don’t, and you shouldn’t” (not even to “try something out for my own learning experience”)
Rust’s safety guarantees are it’s primary justification to exist, and an essential requirement for the compiler to make sense of your code. Without it, there is no point to using Rust.

12 Likes

You know, that’s a VERY GOOD point. Thanks for that. It’s always good to be reminded that things aren’t always what they at first seem to be. Jumping to conclusions is never good.

2 Likes

Locking this thread as it is largely off-topic and I don’t think there’s any actual Rust-related question remaining to be solved. Please start a new more focused thread if there are specific unanswered Rust questions to ask.

5 Likes