News for hackers. Stuff that matters.
Re:What Is Rust's Unsafe? (2019) by pjmlp on Sunday April 10, 2022 @04:15PM (#30981231)
This is a complete misuse of the unsafe as language concept in high integrity computing.
Re:What Is Rust's Unsafe? (2019) by vitno on Sunday April 10, 2022 @04:34PM (#30981422)
This person isn't wrong. A lot of serious Rust users don't agree with what the GP is suggesting. `unsafe` has an explicit meaning: the user must uphold some invariant or check something about the environment, otherwise it is memory unsafe.

I have several times in code review prevented people from marking safe interfaces as "unsafe" because they are "special and concerning", overloading the usage of unsafe is itself dangerous.

Re:What Is Rust's Unsafe? (2019) by zozbot234 on Sunday April 10, 2022 @05:44PM (#30982033)
True, but I think allowing potentially UB-invoking code to not use "unsafe" (e.g. because the use is in the context of FFI, so the unsafety is thought to be "obvious" and not worth marking as such) might be even less advisable. This makes it harder to ensure the "social rule" mentioned by GP, that every potential UB should be endowed with a "Safety" annotation describing the conditions for it to be safe.
Re:What Is Rust's Unsafe? (2019) by ______-_-______ on Sunday April 10, 2022 @05:57PM (#30982122)
Your comment gave me an idea for a lint that might help prevent those mistakes. Right now rustc flags `unsafe {}` with an "unused_unsafe" warning. However it doesn't warn for `unsafe fn foo() {}`. Maybe it should.
Re:What Is Rust's Unsafe? (2019) by gpm on Sunday April 10, 2022 @06:17PM (#30982287)
I think as described you would get false positives, because `unsafe fn foo() { body that performs no unsafe operations }` can be unsafe to call if it interacts with private fields on datastructures used by safe (to call) functions that perform unsafe operations... I expect you would end up with a reasonably high number of false positives.

For an example, consider Vec::set_len in the standard library. Which only contains safe code, but lets you access uninitialized memory and beyond the length of your allocation by modifying the length field of vector: https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1264

You might be able to fix this with a lint that looked at a bit more context though, `unsafe fn foo()` in a module (or even crate) with no actually unsafe operations is very likely wrong. Likewise `unsafe fn foo()` which performs no unsafe operations and only accesses fields, statics, functions, and methods that are public.

Re:What Is Rust's Unsafe? (2019) by aliceryhl on Monday April 11, 2022 @04:55AM (#30986267)
The Rust Vec type has an unsafe function called `set_len` that changes the length of the vector without checking whether the new length is in bounds, or whether the memory containing any new values is initialized. The body of the function is the following:

    self.len = new_len;
No unsafe operations in sight. Should the compiler emit a warning here?
Re:What Is Rust's Unsafe? (2019) by stormbrew on Monday April 11, 2022 @12:18AM (#30984622)
This made me think about what you could potentially do to get a similar sort of thing using the type system instead of the built-in effect of unsafe.

Seems like you could create a sort of userland-unsafe by using a closed trait[1] and requiring its use on a 'dangerous' method or struct or whatever:

   mod my_unsafe {
      pub trait MyUnsafe {}
   }
   pub struct MyUnsafe;
   impl my_unsafe::MyUnsafe for MyUnsafe;

   pub fn dangerous_function<U: my_unsafe::MyUnsafe>() {}

   fn main() {
     dangerous_function::<MyUnsafe>()
   }
Obviously this doesn't let you do a block of unsafe without having to repeat it like `unsafe {}` does, but it doesn't leave you much room to do the dangerous things without the shrinkwrap agreement either (and turbofish are so ugly at least for me they'd be a deterrent).

That said I find the named use-case kind of weird. The whole point of the library is to do these unsafe things, so it's kind of silly to be like "don't forget it's dangerous!"

Re:What Is Rust's Unsafe? (2019) by nextaccountic on Tuesday April 12, 2022 @07:46PM (#31009597)
Oh that's just the suggestion I mentioned here https://news.ycombinator.com/item?id=31009572 (I saw it on some internals.rust-lang.org thread), but with no new syntax. It also mirrors the usage of existing beyond-UB safety markers like UnwindSafe https://doc.rust-lang.org/std/panic/trait.UnwindSafe.html but with inverted polarity (instead of asserting something is safe, it asserts that the programmer acknowledges the new kind of unsafety)

I think an interesting part of unsafe Rust is the interplay between positive and negative polarities (unsafe/safe fn vs unsafe blocks basically) and I think that adding new syntax is the way to leverage this kind of idiom in the new kind of unsafe

Re:What Is Rust's Unsafe? (2019) by infogulch on Sunday April 10, 2022 @06:55PM (#30982567)
Real formal verification is clearly a step up from rust's meaning of "safe", but I don't think it's wrong to try to add another rung to the verification ladder at a different height. Verification technologies have a lot of space to improve in the UX department, here Rust trades off a some verification guarantees for a practical system that is still meaningful.
Re:What Is Rust's Unsafe? (2019) by zozbot234 on Sunday April 10, 2022 @07:06PM (#30982664)
Real formal verification in Rust is basically waiting for a proper formal model of Rust semantics (including semantics of "unsafe"). This is one of the most unappreciated things about Rust's future potential, AIUI; the only language in common use that supports both systems-level programming and formal proof is ATS.
Re:What Is Rust's Unsafe? (2019) by pcwalton on Sunday April 10, 2022 @07:55PM (#30982993)
Eh. You can write, and people have written, formal proofs about C code too. In fact, the difference between Rust and C in terms of safety isn't very large if you have an infinite budget to put toward proving all the code that you write correct.

The safety benefits of Rust appear when you aren't willing to formally prove all the code you write correct. That's because you only have to prove the unsafe code, plus the memory model, correct in order to guarantee memory safety for the entire program. This is less burdensome than in C, where to do the same you have to prove correctness properties about the specific code that makes up the whole program. Rust makes it easier to prove certain properties about the program (far easier in the case of memory safety), but it was always possible.

Re:What Is Rust's Unsafe? (2019) by xavxav on Monday April 11, 2022 @03:10AM (#30985605)
I’m doing my PhD on the formal verification of Rust, and while you’re right that safe code provides a lot of informal advantages it also dramatically simplified form reasoning.

In particular, the dirty secret of C verifiers is that they don’t handle pointers all that well. Either you find yourself doing a lot of manual proof work or you have to dramatically simplify the memory model.

In contrast, when verifying safe Rust, the rules of the borrow checker allow us to dramatically simplify the verification work. All of a sudden verifying a manual memory program with pointers (borrows) becomes as simple as verifying a basic imperative language. I’ve been working on a tool: https://github.com/xldenis/creusot to put this into practice

On the other hand, the moment you dive into unsafe, all bets are off and you find yourself wading through the marshes of (weak) memory models with your favorite CSL as your only friend.

Re:What Is Rust's Unsafe? (2019) by zozbot234 on Monday April 11, 2022 @07:39AM (#30987332)
> I’ve been working on a tool: https://github.com/xldenis/creusot to put this into practice

Note that there are other tools trying to deal with formal statements about Rust programs. AIUI, Rust developers are working on forming a proper team or working group for pursuing these issues. We might get a RFC-standardized way of expressing formal/logical conditions about Rust code, which would be a meaningful first step towards supporting proof-carrying code directly within Rust.

Re:What Is Rust's Unsafe? (2019) by zozbot234 on Sunday April 10, 2022 @08:50PM (#30983335)
> Eh. You can write, and people have written, formal proofs about C code too.

Not without a formal model of C, and the C standard is only an informal, natural-language text. Rust having memory safety as its express goal (which is basically table stakes for any sort of workable language semantics) means that it's at least realistic to think about a formal semantics for Safe Rust. Then you "just" need to deal with the uncomfortable reality that lots of Safe Rust facilities actually bottom out into Unsafe Rust, which is why it turns out you must care about its semantics too. But the hope is ultimately that the small portion of real-world Rust codebases that's Unsafe Rust might not "infect" the Safe Rust to the point of making verification as practically unworkable as in C.

Re:What Is Rust's Unsafe? (2019) by nullc on Monday April 11, 2022 @12:34AM (#30984716)
There are formalization of C, e.g. compcert, which also compiles to machine code in a way that should guarantee proofs all the way though execution (assuming the machine modeling is correct).

It seems like it is a long way off from being possible to do that for rust.

Re:What Is Rust's Unsafe? (2019) by pjmlp on Monday April 11, 2022 @12:48AM (#30984774)
Nope, Ada/SPARK and Frama-C, both with plenty of experience in high integrity systems deployments into production than Rust, currently.