News for hackers. Stuff that matters.
Re:What Is Rust's Unsafe? (2019) by furyofantares on Sunday April 10, 2022 @03:05PM (#30980542)
Would you be able to provide a real example of that HellPortal thing? I'm not really following
Re:What Is Rust's Unsafe? (2019) by tialaramex on Sunday April 10, 2022 @03:54PM (#30981045)
I'm assuming "real example" means of such unsafe-means-actually-unsafe behaviour in embedded Rust, as opposed to a real example of summoning demons?

For example volatile_register is a crate for representing some sort of MMIO hardware registers. It will do the actual MMIO for you, just tell it where your registers are in "memory" and say whether they're read-write, read-only, or write-only just once, and it provides the nice Rust interface to the registers.

https://docs.rs/volatile-register/0.2.1/volatile_register/st...

The low-level stuff it's doing is inherently unsafe, but it is wrapping that. So when you call register.read() that's safe, and it will... read the register. However even though it's a wrapper it chooses to label the register.write() call as unsafe, reminding you that this is a hardware register and that's on you.

In many cases you'd add a further wrapper, e.g. maybe there's a register for controlling clock frequency of another part, you know the part malfunctions below 5kHz and is not warrantied above 60kHz, so, your wrapper can take a value, check it's between 5 and 60 inclusive and then do the arithmetic and set the frequency register using that unsafe register.write() function. You would probably decide that your wrapper is now actually safe.

Re:What Is Rust's Unsafe? (2019) by GolDDranks on Monday April 11, 2022 @02:54AM (#30985512)
So, I peeked into the documentation of volatile_register. The unsafe is there for a clear reason: the compiler can't verify that you aren't creating a mapping to some memory location that is used by the program itself. If you are allowed to safely create a mapping to, for example, a local variable and safely modify it using `register.write()`, you have UB, and that's all in safe code!

So, this isn't a case that the `unsafe` is there just as a warning lint, it has an actual meaning, protecting the memory safety invariants.

Re:What Is Rust's Unsafe? (2019) by furyofantares on Sunday April 10, 2022 @04:01PM (#30981123)
> I'm assuming "real example" means of such unsafe-means-actually-unsafe behaviour in embedded Rust, as opposed to a real example of summoning demons?

That was what I meant, thanks for the answer! Though if you have an example of the other thing I'd be open to that too

Re:What Is Rust's Unsafe? (2019) by mikepurvis on Sunday April 10, 2022 @03:18PM (#30980671)
Similar to the sibling, stuff where you're dealing with parallel state in hardware, like talking to a device over i2c or something, where you know certain things are supposed to happen but you don't, like, know know.
Re:What Is Rust's Unsafe? (2019) by seba_dos1 on Sunday April 10, 2022 @03:12PM (#30980618)
I'm not convinced that it's a great use of Rust's `unsafe`, but since you want an example... dealing with voltage regulators maybe? Where an invalid value put into some register could fry your hardware? There's a ton of such cases in embedded.
Re:What Is Rust's Unsafe? (2019) by oconnor663 on Sunday April 10, 2022 @04:37PM (#30981453)
You could probably make the case that any function that might physically destroy your memory is memory unsafe :)
Re:What Is Rust's Unsafe? (2019) by wheelerof4te on Sunday April 10, 2022 @03:50PM (#30981012)
Imagine the code screaming

"Rip and tear, until it's done!"