I am of the opinion that, safety being good trait of a language, Rust-level safety is sometimes not even worth. You can achieve a very high level of safety without going the Rust way because there are alternative ways to do things in many occassions that obviate the need for a full-blown borrow checker.
I find Rust people or Rust proposers highly academic but the truth is that I question how much value a Rust-lile borrow checker would bring. Value as in real-world safety delta.
Also, Rust people insist that exposing safe code with unsafe inside is safe. I will say again: no, it is not. It is trusted code anyway and saying otherwise is marketing. We could cinsider std lib safe, but going to Rust crates and finding all code that uses unsafe and pretends it is safe just bc you can hide it behind a safe interface does not make that code safe.
Let's start to talk in honest terms to get the highest value: how safe is Rust safe code? What would be the practical delta in safety between Rust-level checking and code written in a safer'-by-default subset?
The rest looks to me like everyone pushing their own wishes or overselling. Particularly I find Rust is highly oversold in the safety department.
Rust is good at isolating potential unsafety and you are ok as long as you do not use unsafe. Once unsafe enters the picture, Rust code can advertise itself as safe, but that is not going to chsnge the fact that the code is not completely guaranteed to be safe. There have been CVEs related to it. If it was safe, that would not be even a possibility. And with this I am not saying C++ is safer. Of course it is not right now.
I am just saying that let us measure things and look at them without cheating.
unsafe acknowledges that the safe subset is overly strict, and that there are safe interfaces to other operations that would otherwise be illegal. unsafe is not mechanically checked, but it makes the safe subset more useful, as long as someone didn't make a mistake and accidentally violate the safe interface. CVEs are either due to mistakes with unsafe, or due to bugs in the Rust compiler.
Any systems language with a safe subset by design is going to benefit from escape hatches for efficiency, because modelling safety perfectly in a systems language is a hard problem, which (if even solvable) would probably lead to too much complexity. D's safe subset is more permissive than Rust, but also less general (at least without D's unsafe equivalents).
You're right that one alternative to a safe subset is to have a partially-safe subset, but then even if all the safety enforcement in the compiler and libraries is perfect, it's still not going to detect some cases where ordinary users mess up even when they wouldn't have used unsafe (most users shouldn't use unsafe anyway, and it helps a lot in code reviews and can be grepped for in automated tests). A safe subset can only be messed up by people writing unsafe or by bugs in the compiler.
unsafe acknowledges that the safe subset is overly strict, and that there are safe interfaces to other operations that would otherwise be illegal.
It also acknowledges that you must trust the code as correctly reviewed. That is not safe. It is trusted code.
CVEs are either due to mistakes with unsafe, or due to bugs in the Rust compiler.
Exactly making my point: was trusted code and it was not safe in those cases.
Any systems language with a safe subset by design is going to benefit from escape hatches for efficiency
I agree, but that is a trade-off: you will lose the safety.
You're right that one alternative to a safe subset is to have a partially-safe subset, but then even if all the safety enforcement in the compiler and libraries is perfect, it's still not going to detect some cases where ordinary users mess up even when they wouldn't have used unsafe (most users shouldn't use unsafe anyway, and it helps a lot in code reviews and can be grepped for in automated tests)
Agreed, most users should not use unsafe. But Rust has crates with unsafe advertising safe interfaces. That is, plainly speaking, cheating. If you told me: std lib is special, you can rely on it, I could buy that. Going to crates and expecting all safe interfaces that use unsafe (not std lib unsafe but their own blocks) is a matter of... trust.
A safe subset can only be messed up by people writing unsafe or by bugs in the compiler
I assume that most seasoned C++ developers would have no problem writing a correct implementation of reverse() for std::vector, while as mentioned above the Rust standard library had a UB bug in its implementation of reverse() as recently as 3 years ago.
I'm not entirely sure you aren't comparing apples and oranges here. Writing a correct implementation of reverse() is one thing; writing an implementation of reverse() that also handles the optimization issues described in the original implementation is another.
To expand on this, I think the normal path for the Rust implementation isn't particularly unreasonable?
pub fn reverse(&mut self) {
let mut i: usize = 0;
let ln = self.len();
while i < ln / 2 {
// SAFETY: `i` is inferior to half the length of the slice so
// accessing `i` and `ln - i - 1` is safe (`i` starts at 0 and
// will not go further than `ln / 2 - 1`).
// The resulting pointers `pa` and `pb` are therefore valid and
// aligned, and can be read from and written to.
unsafe {
self.swap_unchecked(i, ln - i - 1);
}
i += 1;
}
}
I don't think it's that different from one possible way reverse() could be written in C++ (hopefully didn't goof the implementation):
template<typename T>
void std::vector<T>::reverse() {
if (this->size() <= 1) { return; } // Not sure this is necessary?
auto front = this->begin();
auto back = this->end() - 1;
while (front < back) {
std::iter_swap(front, back);
++front;
--back;
}
}
And indeed, the UB in reverse() was not in the simpler bits here - it was in the fun parts that were there to try to deal with the optimization issues described in the original implementation. If you don't care about those optimization issues, then there's no need to complicate these implementations further. If you do care, then I'm not sure it's possible to have a "very simple and easy to get correct" implementation any more, whether you're writing in Rust, C++, or another language that uses LLVM.
I guess another way of putting it is that the UB you linked isn't necessarily because Rust had to use unsafe to efficiently implement reverse(). It's because the devs decided that an optimizer bug was worth working around. I think this makes it not a particularly great example of a "kind[] of simple functionality [that is] apparently surprisingly hard to write correctly and efficiently in Rust without UB".
All that being said, this is basically quibbling over a specific example and I wouldn't be too surprised if there were others you knew of. I'd certainly like to learn from them, at any rate.
I'm kind of curious whether a C++ port of the initial Rust implementation would have experienced UB as well. First thing that comes to mind is potentially running afoul of the strict aliasing rule for the 2-byte specialization, and I'm not really sure how padding/object lifetimes are treated if you use a char*.
That comment you replied to just showed what we already know: there is trusted code and it can fail. That is misleading.
What you have actually in Rust is a very well partitioned area of safe and unsafe parts of the language. The composition does not make it safe as long as you rely on unsafe. That said, I would consider (even if in the past it failed) a std lib and the core as "trustworthy" and assume it is safe (even if it is trusted). But for random crates that use unsafe on top of safe interfaces this is potentially misleading IMHO.
It is a safer language if you will, a more fenced, systematic way of classification of safe/unsafe. And it is not me who says that the language is more fenced but not 100% safe (though the result should be better than with alternatives), it would be simply impossible to have a CVE in a function like reverse() if the code was as safe as advertised. I do not care it is bc of an optimization or not. It is just what it is: a CVE in something advertised as safe.
1
u/germandiago Nov 03 '24 edited Nov 03 '24
I am of the opinion that, safety being good trait of a language, Rust-level safety is sometimes not even worth. You can achieve a very high level of safety without going the Rust way because there are alternative ways to do things in many occassions that obviate the need for a full-blown borrow checker.
I find Rust people or Rust proposers highly academic but the truth is that I question how much value a Rust-lile borrow checker would bring. Value as in real-world safety delta.
Also, Rust people insist that exposing safe code with unsafe inside is safe. I will say again: no, it is not. It is trusted code anyway and saying otherwise is marketing. We could cinsider std lib safe, but going to Rust crates and finding all code that uses unsafe and pretends it is safe just bc you can hide it behind a safe interface does not make that code safe.
Let's start to talk in honest terms to get the highest value: how safe is Rust safe code? What would be the practical delta in safety between Rust-level checking and code written in a safer'-by-default subset?
The rest looks to me like everyone pushing their own wishes or overselling. Particularly I find Rust is highly oversold in the safety department.
Rust is good at isolating potential unsafety and you are ok as long as you do not use unsafe. Once unsafe enters the picture, Rust code can advertise itself as safe, but that is not going to chsnge the fact that the code is not completely guaranteed to be safe. There have been CVEs related to it. If it was safe, that would not be even a possibility. And with this I am not saying C++ is safer. Of course it is not right now.
I am just saying that let us measure things and look at them without cheating.