Where can I find a summary about how Cppfront compares to Rust in terms of memory safety? Will it stop this avalanche of recommendation of different organs to stop using C++?
how Cppfront compares to Rust in terms of memory safety
safety doc link Invalid comparison. It does change defaults to be safer and adds some extra features for helping you write better/correct code, but it only solves the easy problems for now (just like profiles).
avalanche of recommendation of different organs to stop using C++?
The current C++ will still be an unsafe language regardless of cpp2, so nothing changes for C++. Iif cpp2 manages to be [mostly] safe , it may be recommended as a possible upgrade path for current C++ code.
EDIT: More importantly, cpp folks need to be convinced to actually adopt the successor language. It adds a bunch of runtime checks for safety, and this will trigger the "Muh Performance" folks because THIS IS C++ (referencing this talk).
nothing changes for C++. Iif cpp2 manages to be [mostly] safe , it may be recommended as a possible upgrade path for current C++ code.
Actually I'm bringing most of the things I'm trying out in Cpp2 to ISO C++ as proposals to evolve C++ itself, such as metafunctions, type-safe is/as queries and casts, pattern matching, safe chained comparison, bounds-safe automatic call-site subscript checking, and more. The only things I can't easily directly propose to ISO C++ as an extension to today's syntax are those parts of the 10x simplification that are specifically about syntax, but those are actually a minority even though understandably most people fixate on syntax.
I've said that the major difference between Rust/Carbon/Val/Circle and Cpp2 is that the former are on what I call the "Dart plan" and Cpp2 is on the "TypeScript plan"... that is, of those only Cpp2 is designed to be still inherently C++ (compiles to normal ISO C++, has seamless interop with zero thunking/marshaling/wrapping) and cooperate with C++ evolution (bring standards proposals to ISO C++ as evolutions of today's C++). In the past month or so several of the others' designers have publicly said here that their project is seeking to serve as an off-ramp from C++, which is a natural part of being on the Dart plan. But Cpp2 is definitely not that, and I hope that the constant stream of Cpp2-derived proposals flowing to ISO C++ for evolving ISO C++ is evidence that I'm personally only interested in the opposite direction.
That said, I encourage others to bring papers based on their experience to ISO C++ and help improve ISO C++'s own evolution. Besides my papers, the only one such I'm aware of is Sean's current paper to bring his Rust-based lifetime safety he's experimented with in Circle as a proposal to ISO C++, and I look forward to discussing that at our meeting in Poland in a few weeks. I wish more would do that, but I'm not aware of any examples of contributions to ISO C++ evolution from other groups. And I also caution that it's important to have reasonable expectations: Most proposals (including mine) do not succeed right away or at all, all of us have had proposals rejected, and in the best case if the proposal does succeed it will need at least several meetings of iteration and refinement to incorporate committee feedback, and that work falls squarely on the proposal author to go do. Progressing an ISO C++ proposal is not easy and is not guaranteed to succeed for any of us, but those of us who are interested in improving ISO C++ do keep putting in the blood sweat and tears, not just once but sustained effort over time, because we love the language and we think it's worth it to try.
Actually I'm bringing most of the things I'm trying out in Cpp2 to ISO C++ as proposals to evolve C++ itself, such as metafunctions, type-safe is/as queries and casts, pattern matching, safe chained comparison, bounds-safe automatic call-site subscript checking, and more.
These are nice features that will help us write safer code, but there's nothing in your comment that will change C++ memory unsafety story (which the parent comment was asking about) as shown in seans' criticism of profiles. It will just be another "modern cpp features are safer" argument.
Your comparison of circle with dart and cpp2 with typescript is unfair too. Circle actually fixes the safety issue by safe/unsafe coloring, restricted aliasing and lifetimes (borrow checker). But cpp2 just pushes the question further down the road (just like profiles).
Carbon is definitely like Dart though. Google making its own language ignoring the committee.
EDIT: The typescript argument doesn't apply to cpp2 either. JS was the only choice for browsers, TS was a superset of JS and it actually addressed the issues people cared about. But C++ has Rust as competition, cpp2 is a different syntax and it hasn't fixed the main issue yet.
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.
5
u/Occase Boost.Redis Nov 02 '24
Where can I find a summary about how Cppfront compares to Rust in terms of memory safety? Will it stop this avalanche of recommendation of different organs to stop using C++?