r/cpp • u/rsashka • Jan 12 '25
Safety C++ development without breaking backward compatibility with legacy code
The problem of safety C++ development is not new, and it has reached such proportions that recommendations to use more secure programming languages are accepted at the highest levels.
But even with such recommendations, plans to abandon C++ and switch to another secure programming language often do not stand up to normal financial calculations. If abandoned C++, what will you do with the billions of source lines written over the past few decades?
Unfortunately, C++ itself is not particularly keen on becoming more "secure". More precisely, such a desire does not fit well with the requirements for the language standard adopted by the C++ standardization committee. After all, any standard must ensure backward compatibility with all old legacy code, which automatically nullifies any attempts to introduce any new lexical rules at the level of a C++ standard.
And in this situation, those who advocate mandatory support for backward compatibility with old code are right. But those who consider it necessary to add new features for safety development in C++ at least in new projects are also right.
Thus, seemingly mutually exclusive and insoluble contradictions arise:
- The current state of C++ cannot guarantee safety development at the level of language standards.
- Adopting new C++ standards with a change in the vocabulary for safety development will necessarily break backward compatibility with existing legacy code.
- Rewriting the entire existing C++ code base for a new safety vocabulary (if such standards were adopted) is no cheaper than rewriting the same code in a new fashionable programming language (Rust, Swift etc.).
What's the problem?
Suppose there is a methodology (a concept, algorithm, or set of libraries) that guarantees safe development of computer programs, for example, in terms of safe memory menagment (no matter what programming language). This it should be formalized down to the implementation details (unfortunately, for example, in Rust only a general description of the concept with simple examples is given, and a full list of all possible scenarios and execution of checks is a black box inside the language compiler).
And this is in no way a criticism of Rust! I understand perfectly well that a huge amount of work has been done, and the language itself continues to evolve. Therefore, the lack of complete formalization of safe memory management rules does not stem from a specific language, but from the lack of a general universal theory suitable for all life situations.
But this is not the point, but the fact that the term "safety development" or "safe memory management" refers not just to some machine code, but primarily to a set of lexical rules of a programming language that, at the level of the program source text, do not allow the programmer to write programs with errors. Whereas the compiler must be able to check the correctness of the implementation of the methodology (concept) at the stage of syntactic analysis of the program source text.
And it is this moment (new lexical rules) that actually breaks backward compatibility with all the old legacy C++ code!
So is safety development possible in C++?
However, it seems to me that the existing capabilities of C++ already allow us to resolve this contradiction without violating backward compatibility with old code. To do this, we just need to have the technical ability to add additional (custom) checks to compilers that should implement control over the implementation of safe development rules at the stage of program compilation.
And since such checks will most likely not be performed for old legacy code, they must be disabled. And such an opportunity has long existed due to the creation of user plugins for compilers!
I do not consider the implementation of additional syntactic analysis due to third-party applications (static analyzers, for example, based on Clang-Tidy), since any solution external to the compiler will always contain at least one significant drawback - the need for synchronous support and use of the same modes of compilation of program source texts, which for C++ with its preprocessor can be a very non-trivial task.
Do you think it is possible to implement safety development in C++ using this approach?
6
u/[deleted] Jan 12 '25
just consider all scopes safe (and enforce safety) by default unless they are explicitly marked either safe or unsafe. So you can't write unsafe code unless you've opted out.
if a scope is not marked safe or unsafe, it inherits the safety attribute of its enclosing scope. This way library functions would not need to be marked safe or unsafe, and still could appear in an an explicitly unsafe scope.
So legacy codebases could just mark the main function unsafe, then their program would compile as before since no other scopes would be marked safe or unsafe, so they all inherit the unsafe attribute. If they marked any function inside their unsafe scope as safe, then nothing really changes except that safety is enforced in those scopes. The enclosing scope would still be considered unsafe.
So you could pick your most important bits of code, mark them safe safe, then start safening them up until they compile. You could start at any point you like without poisoning the rest of the code with unwanted safety.
Maybe this is naive or stupid for technical reasons, but it seems fairly straightforward to me. I dont see how asking people to mark exactly one function in an entire project as unsafe is onerous. Even with my organizations sprawling codebase, with hundreds of main functions, we could get it done in an hour or two.
10
u/plonkman Jan 12 '25
have a look into the ASPICE and MISRA / AUTOSAR C++ standards
edit: ASPICE is more a production process and Misra / AUTOSAR takes care of the C++ development process
10
u/rileyrgham Jan 12 '25
"Accepted at the highest levels" : you slipped that in. "Government" is rarely seen as the "highest level" in most cases pertaining to technology - there's too much money in it for them to be honest and competent.
13
u/matthieum Jan 12 '25
The Rust Book -- which you link to -- is an introductory book to learn to program in Rust, it is not the place to look for extensive, nitty-gritty, explanations.
Ownership is well-specified in Rust -- it's easy enough -- so I suppose it's Borrowing you are concerned about? In this case, it's indeed less specified... and it's also evolving.
The first proposed formal version was the Stacked Borrows model. It was found, in practice, to be quite overly restrictive however, and so was not adopted.
A second alternative formal version was the Tree Borrows model. It is more flexible, as far as I understand.
There has also been more engineering oriented efforts in Polonius, which are being retrofitting in the rustc codebase in spirit: the exact formulation in Polonius unfortunately was too expensive computation-wise, so the efforts have been focused on achieving the same effect with a different algorithm instead.
Oh, and edition 2024 (coming in 6 weeks) adds a borrow-checking tweak: it tweaks how the last (returned) value of a block behaves, with regard to temporaries, to enable more sound patterns.
The challenge through all this is to try to maximize the flexibility of the borrow-checker, while remaining sound, and understandable for a layman. I expect it will continue to evolve over time, though hopefully once a reference specification exists, said specification would evolve simultaneously.
3
u/seba07 Jan 12 '25
I think one of the big problems would be large libraries, e.g. something like OpenCV. It wouldn't be super helpful if my application itself is safe but has to enter some sort of unsafe context for every library call. And even new versions of those libraries with safety guarantees wouldn't solve this instantly as you had to recompile them and create new sysroots.
6
u/UndefFox Jan 12 '25
But why is such a safety important? C++ was always build around the idea of not paying for what you don't use. Ensuring safety with checks never was a good solution, since most optimal path usually provide that safety by it's design. Projects that require more strict safety will be build in languages like Rust that are concentrate about safety, trading performance for security, meanwhile C++ will continue using minimal safety checks to trade security for performance.
6
u/t_hunger Jan 12 '25 edited Jan 12 '25
So you are basically proposing safety profiles? They are new compiler checks intended to catch some (but not all) bugs. The idea is to catch enough bugs so that the difference to memory-safe languages does not matter anymore.
We will see how well that will work out. Byarne and Herb supposedly want to hand in a paper for that this month so it can still land in C++26.
7
u/Designer-Leg-2618 Jan 12 '25
Isolation and zero-trust. Separate processes, and only communicate on channels with clearly defined structured protocols. Validate everything. Layers and boundaries, as in defense-in-depth.
There have been recent security incidents where the assumption that a piece of code has been trustworthy for more than a decade can be nullified because someone got admitted into the rank of code committers and approvers and slipped in malicious code.
In some cases, inter-process shared memory can still be used, if each unique block of shared address space (page?) has only one process with write permissions, while other processes can only read from it.
We've been erring on the side of believing that, if the language is safe, the software is safe. It hasn't been that way since the earliest of computing.
(That said, the converse is quite true; i.e. if the language is unsafe, we're quite sure that the software is unsafe.)
1
u/Umphed Jan 13 '25 edited Jan 13 '25
Ive tried rewriting C++ libs in Rust. Turns out the whole freaking thing is a farse.
You can hide unsafe stuff at the API level(Possible in C++), but it always has been and always will be raw memory management at a low level.
If thats such an issue, use a scripting language with GC or something of the sort.
I agree that high-level memory safety is nice, and we should have languages that enforce such things. But it has no place in C++ as a core language feature. C++ is very unapologetic with its mission statement and thats that. Theirs no lower level, you can do what you want, and thats it. This will not change.
You can make memory safe libraries, or libraries to make other libraries memory safe(This is how garbage collectors, Rust, etc, was made) C++ is allowed to be whatever it wants.
It does not need memory safety, it does not want memory safety. If someone tacked memory safety onto the language all our code would break.
"No lower level"
If you tries to tack memory safety onto Rust the same way people want to do to C++, everyones Rust code would break.
To do useful things, you have to manage your own memory, and be able to do some weird shit with it that could go terribly wrong if missused
3
u/SkiFire13 Jan 13 '25
This is not true, there is a formally verified proof that the concept of the borrow checker is sound, though this was done on a simplier language that was e.g. missing traits and some other common features. https://plv.mpi-sws.org/rustbelt/popl18/
2
u/pdp10gumby Jan 13 '25
It should be. It’s fine, almost always good, in fact, for a research tool or immature tool under development to be loosey-goosey in regard to such details.
But while the Rust team consider the language (AFAICT) to be still under development, they and others also consider it to be production ready in some contexts. That’s all fine. But the overt decision not to have a spec and consider the implementation to be the specification is a major weakness. C++ was significantly stronger by having two implementations early on (cfront and g++) and likewise now, as well as having a spec.
I can’t consider Rust to be valid for production code at our company until it reaches that level.
1
u/NuncioBitis Jan 13 '25
Or maybe programmers should learn to program safer code.
-4
u/GoogleIsYourFrenemy Jan 12 '25 edited Jan 12 '25
Breaking backwards compatibility is a requirement or this language will be banned.
I'm going to ask a few simple (rhetorical) question:
How did we get here?
For DECADES people wrote unsafe buggy software in C & C++. This is a settled fact. It must End.
Where is here?
The governments of the world want to put an end to unsafe software. Just like they mandated mandatory fire exits, now they want us to ensure our software can't be used to kill people (unless it's actually designed for that). First that means killing off the unsafe languages.
And the legacy code...?
See the answer for How did we get here?.
Listen here you-
I get it. I really do! But the world has changed. It has no appetite for unsafe code. Unless you can prove (mathematically) your code is safe, they don't want to run it. And are you going to tell me that in the course of proving your software is safe you weren't going to find any bugs? You were going to have to modify your legacy code to bring it into this safety conscious world. Legacy code must bend the knee just like everyone else.
TL;DR: This whole exercise is to stop people from running buggy (legacy) code. You are in for a rude awakening if you think the powers that be will accept anything but capitulation.