r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
2
u/MEaster Oct 30 '24 edited Oct 30 '24
It could be. Miri is analogous to C++'s sanatizers, and I'm going to assume not perfect.
That's a neat solution. I assume you have some sort of flag on the dynamic container that tracks these fixed borrows, and gets checked when doing an invalidating operation?
UnsafeCell is the language primitive that makes shared mutation through a reference not be UB. So Cell, RefCell, Mutex, and RwLock all contain an UnsafeCell. Note that raw pointers are allowed to have shared mutation, as long as you are very careful about how you source the pointers. You can still end up with UB if there are live references to the same memory and you violate the borrow checker rules. You are also opened up to potential data races in a multithreaded environment.
That is an example of an assumption the optimizer can make. Rust's shared XOR unique references enforce that exact requirement, which means it can prove this. This doesn't apply just to &mut Ts, it also applies to &T if T doesn't contain an UnsafeCell, which is actually the vast majority of references.
It also doesn't just apply to references being passed to called functions, it applies to references in function arguments. Compare how these two (albeit contrived) functions compile. Raw pointers are never marked noalias. This means that the optimizer cannot assume that writing to
cwon't invalidateaorb, and so it's forced to do the operation one at a time. With the references, it can make that assumption, and so vectorises it.One thing to consider here is how much the compiler is currently taking advantage compared to how much it could. From what I gather, prior to rustc the only user of noalias was C's restrict pointers. Restrict pointers only seem to have been used in a very controlled manner, in very specific situations, in very hot loops, because you're basically planting a footgun mine. Then along comes rustc and marks 99% of pointers noalias. There were so many bugs in LLVM around noalias, because it was so unused, that the rust devs had to try like half a dozen times before the miscompilations stopped.
You're absolutely right that not having them does limit expressivity. And it probably does result in more unsafe code where you may be able to avoid it with a move constructor. I think the problem is that the existence of move constructors would make unsafe code harder. It would mean that any unsafe which handles user-defined types must now assume that even simply doing
a = b;can panic. That now means things that are relatively simple and easy currently must be panic-safe, which is more difficult.And you also have to consider where the thing you just moved is. Is it in
a, inb, or are both partially initialized? Unsafe Rust is hard enough as it is, and this would make it even harder. I think that could be partly why there's little desire to add them to Rust.I think here it's another matter of tradeoffs. There are a lot of memory safe languages, but most of them have various degrees of runtime mechanism for managing that safety. And these aren't bad choices, they very often have the advantages of making the language easier to use.
And I think the same is true to an extent of scpptools. From what you've described, there are certain operations that are free in Rust but have a runtime cost (however small) in your implementation. But on the other hand, certain things could be made easier for the user, or provide more expressivity, by having that runtime cost.
Whether the costs, either in expressivity, ease of use, or runtime, are worth it comes down to what you are trying to achieve and want to prioritize. There's absolutely room for C++ to make different choices, and make different tradeoffs, and still achieve memory safety.
That's actually a good example of something you need to be careful of. When you do
*ptr = val;, it will call the Drop implementation of what's behind the pointer, which creates a &mut. It gets even worse if the ptr is pointing at uninitialized memory. Now, this is OK if you know the pointer is initialized, aligned, and that it's valid to drop (e.g. you got your pointer from a reference), but if you don't then you need to doptr.write(val).Yeah, I would never be so daft as to claim that Rust's model is the only viable one. I think Hylo's model, which has also been suggested, could also work. And I'm sure there are others worth exploring. But, to an extent, there's also a bit of a time pressure. The US government has already made comments about memory safety, and the EU is bringing in liability for software vendors. How long can C++ afford to spend exploring different models? Especially when you have things like Google's recent article demonstrating that the reduction of newly-written memory-unsafe code significantly reduced their most severe security vulnerabilities.
Rust's model does have two plusses in general, and one specifically for C++. The model itself is proven to be sound by Ralf Jung's work, so we know it's good on that front, but it's also been proven in practice by Rust itself that it is a workable model for non-trivial software. I don't know where Hylo's model stands on these fronts. I understand that Swift uses Hylo's model, but Swift I believe also has reference types, which I think are ref-counted, and this changes things a bit. Is this model, without the reference types, workable for non-trivial software? Maybe it is, and it's even easier to use than Rust's. I hope Hylo and others continue to explore it, but that exploration and experience will take time.
For C++ specifically, I think Rust's (and sccptools) model (at least compared to Hylo's), has the advantage that it's closer to how C++ is currently written. C++ codebases commonly use references/pointers in way that is pretty close to how Rust codebases do, and I think this kind of model would likely better integrate into C++ and be easier to adapt to than something that requires a different way of approaching the problem.
It certainly does! No need to apologize, I've been enjoying this greatly.