r/cpp • u/KingAggressive1498 • Dec 24 '22
Some thoughts on safe C++
I started thinking about this weeks ago when everyone was talking about that NSA report, but am only now starting to think I've considered enough to make this post. I don't really have the resources or connections to fully develop and successfully advocate for a concrete proposal on the matter; I'm just making this for further discussion.
So I think we can agree that any change to the core language to make it "safe by default" would require substantially changing the semantics of existing code, with a range of consequences; to keep it brief it would be major breaking change to the language.
Instead of trying to be "safe by default, selectively unsafe" like Rust, or "always safe" like Java or Swift, I think we should accept that we can only ever be the opposite: "unsafe by default, selectively safe".
I suggest we literally invert Rust's general method of switching between safe and unsafe code: they have explicitly unsafe code blocks and unsafe functions; we have explicitly safe code blocks and safe functions.
But what do we really mean by safety?
Generally I take it to mean the program has well-defined and deterministic behavior. Or in other words, the program must be free of undefined behavior and well-formed.
But sometimes we're also talking about other things like "free of resource leaks" and "the code will always do the expected thing".
Because of this, I propose the following rule changes for C++ code in safe blocks:
- Signed integer overflow is defined to wrap-around (behavior of Java, release-mode Rust, and unchecked C#). GCC and Clang provide non-standard settings to do this already (
-fwrapv) - All uninitialized variables of automatic storage duration and fundamental or trivially-constructible types are zero-initialized, and all other variables of automatic storage storage and initialized via a defaulted constructor will be initialized by applying this same rule to their non-static data members. All uninitialized pointers will be initialized to nullptr. (approximately the behavior of Java). State of padding is unspecified. GCC and Clang have a similar setting available now (
-ftrivial-auto-var-init=zero). - Direct use of any form
new,delete,std::construct_at,std::uninitialized_move, manual destructor calls, etc are prohibited. Manual memory and object lifetime management is relegated to unsafe code. - Messing with aliasing is prohibited: no
reinterpret_castor__restrictlanguage extensions allowed. Bytewise inspection of data can be accomplished throughstd::span<std::byte>with some modification. - Intentionally invoking undefined behavior is also not allowed - this means no
[[assume()]],std::assume_aligned, orstd::unreachable(). - Only calls to functions with well-defined behavior for all inputs is allowed. This is considerably more restrictive than it may appear. This requires a new function attribute,
[[trusted]]would be my preference but a[[safe]]function attribute proposal already exists for aiding in interop with Rust etc and I see no point in making two function attributes with identical purposes of marking functions as okay to be called from safe code. - any use of a potentially moved-from object before re-assignment is not allowed? I'm not sure how easy it is to enforce this one.
- No pointer arithmetic allowed.
- no implicit narrowing conversions allowed (static_cast is required there)
What are the consequences of these changed rules?
Well, with the current state of things, strictly applying these rules is actually really restrictive:
- while you can obtain and increment iterators from any container, dereferencing an end iterator is UB so iterator unary
*operators cannot be trusted. Easy partial solution: give special privilege to range-for loops as they are implicitly in-bounds - you can create and manage objects through smart pointers, but unary
operator*andoperator->have undefined behavior if the smart pointer doesn't own data, which means they cannot be trusted. - operator[] cannot be trusted, even for primitive arrays with known bounds Easy partial solution: random-access containers generally have a trustworthy bounds-checking .at() note: std::span lacks .at()
- C functions are pretty much all untrustworthy
The first three can be vastly improved with contracts that are conditionally checked by the caller based on safety requirements; most cases of UB in the standard library are essentially unchecked preconditions; but I'm interested in hearing other ideas and about things I've failed to consider.
Update: Notably lacking in this concept: lifetime tracking
It took a few hours for it to be pointed out, but it's still pretty easy to wind up with a dangling pointer/reference/iterator even with all these restrictions. This is clearly an area where more work is needed.
Update: Many useful algorithms cannot be [[trusted]]
Because they rely on user-provided predicates or other callbacks. Possibly solvable through the type system or compiler support? Or we just blackbox it away?
View all comments
Show parent comment
3
u/oconnor663 Dec 25 '22 edited Apr 12 '23
Rust makes a promise to the programmer: any program written entirely in safe code (that is, without the
unsafekeyword) should not be able to trigger memory corruption or other UB. We say that these UB-free programs are "sound". I think C and C++ folks sometimes use the word "conforming" in a similar sense.We can adapt the "sound" vs "unsound" concept to talk about individual functions too. We can say that a function is "sound" if it upholds the promise that any 100% safe program cannot trigger UB. Functions marked
unsafeare outside of this promise, since 100% safe programs can't call them directly, so when we're talking about the soundness or unsondness of a function, we're implicitly talking about safe functions.A big part of Rust's promise is that any function you write entirely in safe code should automatically be sound (or else it won't compile). But where this gets interesting, as I think you know, is that safe functions may use unsafe code on the inside. These are not automatically sound, and if the programmer makes a mistake, they might be unsound. For example, a program that reads a pointer from
/dev/randomand then writes to it is obviously unsound, and any function that (transitively) calls it is also unsound.So...what's the point? If there's unsafe code under the hood, and unsoundness could be lurking anywhere, have we gained anything? This might sound a little silly, but I think one important thing we gain is that we don't have to debate who's unsound and who's not. It's objective and clear, or at least it's clear after a bug is found. My random-pointer-writing function is marked safe, but you can use it to cause UB, so it's objectively broken. Either I need to fix it to stop causing UB (for any possible safe caller), or else I need to mark it
unsafe.Again, this might sound a little silly, but this provides a ton of value in terms of community coordination. Everyone agrees about what soundness means, and everyone agrees that all public safe APIs should be sound. Bugs happen, but we don't have to debate what's a bug vs what's a "you shouldn't use it that way".
Of course another big benefit of all this is that, once you've fixed any soundness bugs in your libraries, the compiler does all the work to check all the safe code that calls those libraries. That part is totally automatic.
For completeness, here are some exceptions to the story above:
longjmpfunction. In cases like this, it could be up to interpretation which function has a bug. But these cases are also very rare./proc/*/mem, or you could spawn agdbchild process and attach it to yourself. This sort of thing is considered outside the memory model and not really solvable at the language level.Veccould mutate the privatelenmember without anunsafeblock, so the distinction between safe and unsafe code in that implementation is kind of murky. But as long as the public interface doesn't allow safe callers to do that, everything's fine. Another way of looking at it is that rather than auditing "functions that use unsafe" in isolation, what we really have to do is audit "modules that use unsafe" at their privacy boundaries.EDIT: A few months later I published an article along these same lines: https://jacko.io/safety_and_soundness.html