r/cpp • • Oct 22 '26

Revisit Safe C++

Everyone is aware of the deluge of vulnerabilities now hitting critical software. It even makes the mainstream news, so software safety is coming onto politician's radars.

If this gets worse, I think it is not inconceivable that at some point memory safe software becomes mandatory. Or at least for software that is "reachable" from the internet.

So is it time to revisit Sean Baxter's Safe C++? Start out with a safe context ("safeexpr") and expand from there, just like constexpr.

37 Upvotes

181 comments sorted by

View all comments

Show parent comment

-2

u/arthurno1 Oct 24 '26

Common Lisp has been in the use for 30+ years, and completely hides the hardware below. I have recently being playing with trying to get Common Lisp to run as fast as C++, and it seems to be fully possible. But: I need an escape clutch from the managed runtime, which SBCL has (I can emit assembly and get unchecked access to memory via raw pointer).

Now, I am not sure about this, but to achive the bare-metal speed, we do need unrestricted access (as much as possible) to the hardware below. Rust also has a clutch to escape into the "unsafe" code. I am affraid anyone who want to run 100% speed on bare-metal will want a cluth. However, I also believe 95% or 99% of bare-metal speed if more than enough for most of the applications.

I think any language with dynamicism, i.e. not completely static programs, will have to restrict access to raw hardware in order to ensure safety; at least somewhere. C++ is designed to allow creating of objects at runtime, so not everything can be fully statically determined, so some form or "management" will always be needed, and that is where some tradeoff will be made. In other words: yes- and no-sayers would have to agree somewhere and make a trade-off.

7

u/HommeMusical Oct 25 '26

we do need unrestricted access (as much as possible) to the hardware below.

Safe C++ proposals are checked at compile time and there would always be a way to say, "Store this word at this pointer" - you would simply have to mark that access as unsafe somehow.

I think any language with dynamicism, i.e. not completely static programs, will have to restrict access to raw hardware in order to ensure safety;

Strong disagree. The type checking proves to the compiler that your code is correct but you are in no way restricted at runtime.

4

u/arthurno1 Oct 25 '26

Safe C++ proposals are checked at compile time and there would always be a way to say, "Store this word at this pointer" - you would simply have to mark that access as unsafe somehow.

Wasn't that exactly what I said?

Strong disagree. The type checking proves to the compiler that your code is correct but you are in no way restricted at runtime.

Ok, you have a cristall ball? Disagree as much as you want but I don't think you understand the problem. There is no mathematics that can prove that a future datum (a piece of data), you have never seen will have certain property say max-length = L. As a concrete example: you are reading a string from Internet or from a file and have a limited size buffer to store it in. It means you have to check it's length at the runtime. Unless you have a cristall ball to see into the future, there is no way for you to prove a property of some data you have never seen at compile time . But downvote me and strongly disagree :).

3

u/frediku Oct 26 '26

I believe his argument is that the code parts that use no unsafe escape hatch can be proven to be safe, for some definition of safe. I think this indeed works. Without unsafe you cannot access the network and thus one can statically prove how many bytes you can receive via the network in pure safe code. The answer is 0. It's not a helpful answer but it can be proven.

Are such proofs then worthless? I believe no, as quite large chunks of code can be made to not use unsafe stuff and knowing that if there is a safety issue, it does not reside there, has worth. In your example, there will likely be way more code that moves the network response around and parses it than there is code that directly interfaces with the network.

1

u/arthurno1 Oct 27 '26 edited Oct 27 '26

I think we are in agreement for the most part.

I am just saying that you will not be able to always statically prove that you don't need bound checking. As much as I am familiar with compilers, it is a halting problem. I/O, dynamic array indirections, when calculating from functions on which you can't assert exact interval for return values, in some loops, etc. In those situation when you can't prove it you will want to emit bounds checking. Some people might wish to use that escape clutch in some form, for example a raw pointer, and dereference themselves in those cases to get bare metal speed. Of course programming languages vary whether they want to give the programmer that escape or not, or in which degree. Honestly, I don't think that is controversial.

here will likely be way more code that moves the network response around and parses it than there is code that directly interfaces with the network.

When you design a programming language, you don't know in which layer it runs; you design it to be general.

My point was: if you want completely safe programming language, than you should perhaps not give people that escape clutch to go bare metal. "Portable" Common Lisp is one such language. SBCL is an optimizing compiler that does expose raw memory with foreign pointers, but that is out of the standard. However, if I am gonna stay withing "portable" Common Lisp, i.e. standard, that does not give me raw pointers, and does not exposre raw memory modell as C/C++, than I have to live with the fact that the compiler will emit bound checking in cases where it can't assure I am within bounds. That is the compromize, the tradeoff for more safety and more automation I am probably willing to pay in say 99% of cases (unless I am demonstrating Lisp can be fast as C++ if you give me that clutch :)).

1

u/HommeMusical Oct 30 '26

Disagree as much as you want but I don't think you understand the problem. There is no mathematics that can prove that a future datum (a piece of data), you have never seen will have certain property say max-length = L.

This statement is true, but completely and utterly irrelevant to the matter at hand.

1

u/arthurno1 Oct 30 '26

It is relevant because you have to decide whether you should decide the language so it always emit safer option with some extra computations, or whether you should trust programmer and let them emit unsafe code.

0

u/HommeMusical Oct 25 '26

As a concrete example: you are reading a string from Internet or from a file and have a limited size buffer to store it in. It means you have to check it's length at the runtime.

Yes, this will always be possible. I'm not seeing your objection.

0

u/arthurno1 Oct 25 '26

What will always be possible and which objection you don't see?

1

u/HommeMusical Oct 30 '26

you are reading a string from Internet or from a file and have a limited size buffer to store it in. It means you have to check it's length at the runtime.

Yes, this will always be possible. I'm not seeing your objection.

What will always be possible and which objection you don't see?

It will always be possible to read a string from the Internet or a file, check its length at runtime, and then put it into the right type, so that's why I don't see your objection at all.

0

u/arthurno1 Oct 30 '26

Of course you can check the lenght at the runtime; that is the exact point. I took the example because everyone understand that one. Here we talk about code that would like to skip that runtime check. It does not have to be a string, and does not have to be from the internet. Look at the recently posted blog article by Lemire, where he overshoots reading from a buffer. I have done similar for the exact same reason when I used a mmap-ed file to implement a wordcount program: I relied on system zeroing mmap, and that mmap is a multiple of 64, and that system would always allocate an entire piage , even if it is only one extra byte needed, so I could "overshoot" and didn't had to pay for the extra loop tail processing. Compiler can't deduce that and eliminate bounds checks in such situation. I have since than switched to pread because I spent too much time in system mmap-ing, but the technique is useful.

-1

u/SV-97 Oct 28 '26

As a concrete example: you are reading a string from Internet or from a file and have a limited size buffer to store it in. It means you have to check it's length at the runtime. Unless you have a cristall ball to see into the future, there is no way for you to prove a property of some data you have never seen at compile time .

What's the point you're trying to make here? If you prove something about a natural number in math you also don't know a bound on its value (that does not depend on the number itself) but can of course still prove highly nontrivial stuff about it.

Note that you can also write functions that dynamically certify the data to have certain properties (e.g. being bounded by some value or whatever) and only then enter code-paths that are proven correct for that size. A proof can also account for (potentially failing) allocation etc. etc. --- it's just a matter of where you draw the line with what you're modeling / what you want to prove.

It's honestly baffling that in an age where dependently typed languages are relatively established someone would still try and make such an argument.

-1

u/arthurno1 Oct 28 '26

someone would still try and make such an argument.

"Such" as what exactly?

What is baffling is that people can't pay 5 seconds of mental gymnastics to read and reflect over what is written, if one does not explicitly writes in exact words what they have in their own mind. Social media in a nutshell.

it's just a matter of where you draw the line with what you're modeling / what you want to prove.

Yes, it is about how much and what you and what you want to prove; and were you want to draw the line, that is exactly the point I am making there.

Proving that you can remove all bound checks is a halting problem. Do you have a solution? If you can't prove you can remove bounds check than you have to keep them. There are languages that do let you to remove bound checks, at your responsibility. That is the line to draw: if you want maximum performance, than perhaps you need to allow that explicit, clutch. If you want perfectly safe language, than you don't want to allow that hatch. What is baffling about that one?

1

u/SV-97 Oct 28 '26

"Such" as what exactly?

"Such" a nonsense argument about how you can't prove nontrivial stuff without access to a crystal ball. You're just entirely and fundamentally missing the point about how formalization is done today.

Proving that you can remove all bound checks is a halting problem. Do you have a solution?

ANY nontrivial property about the semantics of a program is equivalent to a halting problem (Rice's theorem) --- yet we still routinely solve such problems in practice by simply accepting some false negatives (i.e. restricting ourselves to the class of problems that we can actually handle) / requiring users to manually prove correctness for cases that can't be automatically deduced. That's how every compiler today works.

However that's really a straw-man (nobody was talking about "removing all bounds checks" or anything like that) and also totally misses the point of what I was saying, which was that you can have parts of your program that are statically proven correct under the premise of having a bound --- and then other parts that dynamically establish that this premise actually holds before entering that codepath. As you say: it is baffling that people can't pay 5 seconds of mental gymnastics to read and reflect over what is written.

If you can't prove you can remove bounds check than you have to keep them.

Which is a perfectly valid solution: omit what you can prove to be unnecessary, and otherwise just keep the check or (if preferred) error out and require manual proof. It's really not that complicated, and its standard practice.

Just for completeness: safety XOR performance is a false dichotomy. We've seen this time and time again at this point; see for example the case study by Lukas Bergdoll on sorting algorithms.

-1

u/arthurno1 Oct 28 '26

nobody was talking about "removing all bounds checks" or anything like that) and also totally misses the point of what I was saying, which was that you can have parts of your program that are statically proven correct under the premise of having a bound --- and then other parts that dynamically establish that this premise actually holds before entering that codepath.

So you are in agreement with me the: if you want perfectly safe program, you can't remove bounds checking always.

it is baffling that people can't pay 5 seconds of mental gymnastics to read and reflect over what is written.

Yes, it is. So go away now, because you seem to accuse me for talking against type checking or whatever, which I certainly don't.