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.
3
u/ntrel2 Nov 03 '24 edited Nov 03 '24
unsafeacknowledges that the safe subset is overly strict, and that there are safe interfaces to other operations that would otherwise be illegal.unsafeis 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 withunsafe, 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
unsafeequivalents).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 useunsafeanyway, 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 writingunsafeor by bugs in the compiler.