r/cpp • • Jan 11 '26

CppCon Making C++ Safe, Healthy, and Efficient - CppCon 2025

https://youtu.be/p52mNWsh-qs?si=Bz6pyW6d0SMLCZxV

Now with some updated content since the ACCU talk, and the Q&A is nonetheless interesting.

55 Upvotes

103 comments sorted by

View all comments

Show parent comment

37

u/seanbaxter Jan 11 '26 edited Jan 11 '26

asan would be the more relevant sanitizer here, but this isn't related to asan or ubsan. The claim that John makes is that you can achieve Rust-like lifetime safety by enforcing exclusivity at runtime. It's perfectly robust against use-after-free defects, you can use any algorithm you want, and it doesn't require modifying existing code. It would be high-performance because where the compiler can statically prove properties, the ghost data would be optimized out.

There won't even be a proposal or a working prototype for this. Problem is that runtime exclusivity is logically impossible. It's profiles redux. One way to look at it is that runtime exclusivity enforcement requires time travel. Borrow checking enforces the invariant that there is only one mutable xor multiple shared live references to a place. What's a "live reference?" It's a reference which is to be loaded or stored from in the future. Borrow checking can propagate uses of regions backwards: start at the terminals of the CFG and walk backwards. When a region is used in a load/store/function call, it becomes live at the preceding instruction. When a reference is assigned a new address, then its no longer live. There are forward propagating algorithms as well, but they also require the CFG be available.

How do you compute liveness in the runtime case? You would have to predict if a reference is used in the future to know if it is live now. That's time travel. Compile-time borrow checking feels restrictive because it has to account for all possible paths in the CFG. This runtime exclusivity would be much more lenient because it would only restrict you to the paths that are actually executed.. but at the cost of being unimplementable.

The authors of this keep claiming it performs runtime exclusivity enforcement. I think they should describe how that is achieved. I've asked them many times and never got anything approaching an answer.

Gasper gave a talk on this topic in October: https://www.youtube.com/watch?v=bGht0Pu5heo

7

u/QuaternionsRoll Jan 12 '26

Unless I’m misunderstanding you,

enforcing exclusivity at runtime

you can use any algorithm you want, and it doesn't require modifying existing code

you can’t have it both ways. Many algorithms and data structures rely on non-exclusivity. Doubly-linked lists are famously difficult to implement in safe Rust, and impossible to implement without incurring a substantial runtime overhead à la RwLock (you can’t even use RefCell if you want to replicate the fact that LinkedList implements Sync).

3

u/servermeta_net Jan 12 '26

There are plenty of efficient double linked lists in rust. Even idiomatic ones (no unsafe) that are more efficient than idomatic CPP ones. I personally really like arena allocated DLLs.

1

u/QuaternionsRoll Jan 12 '26

arena allocated DLLs

look inside

Vec<Option<(T, usize, usize)>>

Unless you’re saying “just use an arena allocator library”, to which I say “just use LinkedList”.

1

u/servermeta_net Jan 13 '26

No that's not how I would do it, you can move more optimizations to compile time. But what's wrong in your opinion? You still think that performance would be problematic?

In any case if you are using x64 you can achieve a very similar result by tagging pointers in single threaded apps, and using capabilities in multithreaded apps.

22

u/James20k P2005R0 Jan 11 '26

The authors of this keep claiming it performs runtime exclusivity enforcement. I think they should describe how that is achieved. I've asked them many times and never got anything approaching an answer.

Its always struck me as odd how much of safety in C++ revolves around making large scale promises without a technology demonstrator or theoretical background to show that its viable. The profiles proposal is also promising memory safety with no-minimal code changes, which makes this latest approach a somewhat strange direction given that profiles were promised to solve exactly the same thing

3

u/pjmlp Jan 11 '26

A consequence of having proposals without implementations being a common thing in WG21 papers.

However, John Lakos addresses this at the beginning of the talk, so I am at least hopeful that something like this would indeed have a prototype in place of some kind before having a paper without implementation.

16

u/seanbaxter Jan 11 '26

There is no paper or implementation, because the idea doesn't work.

2

u/pjmlp Jan 12 '26

I agree that something like Safe C++ would be a much better path to be on, but since we're now here, if they manage to provide a prototype that at least partially works, it is better than nothing at all.

In a way I appreciate there are still some people left trying to do something on WG21 even if it isn't the way it should be, versus no one at WG14, where no one really cares beyond keeping C as a pseudo portable assembler.

However as acknowledged on the Motivation slide, Google, Microsoft and Adobe have downramped their investment, additionally Apple isn't racing to keep up with C++, other than what their own needs, so even if the solution would work, there needs to be interest to make it happen on the remaining C++ compilers still having active development.

13

u/seanbaxter Jan 12 '26

But they won't provide a prototype that partially works. It's not better than nothing at all. The ghost data thing is a make-believe solution. May as well say the tooth fairy makes your program memory safe. Next time, before someone gives keynotes on how they made C++ memory safe, they need to write down how the technique works, because extraordinary claims do require some evidence.

1

u/Wonderful-Wind-905 Jan 13 '26

Might Clang's lifetime annotations count as to something that partially works?

1

u/ts826848 Jan 16 '26

Not in the context of this subthread, I think, since the focus is generally on runtime enforcement. Clang's lifetime annotations are purely compile-time from what I understand.

1

u/pjmlp Jan 15 '26

Nope, because they go all the way back to 2015, alongside VC++ ones.

They require quite a few annotations, that go against the famous "no annotations" paper, and even then, they cannot cover all C++ semantics.

3

u/Wonderful-Wind-905 Jan 15 '26 edited Jan 15 '26

But if one relaxes the "no annotations" requirement, and then focuses on "partially works" regarding "they cannot cover all C++ semantics", might Clang's lifetime annotations count? There have been development on them over the years, including as recently as 3 days ago

https://github.com/llvm/llvm-project/pull/170007

https://github.com/llvm/llvm-project/issues/152520

3

u/ts826848 Jan 16 '26

But if one relaxes the "no annotations" requirement

Obviously relaxing restrictions will make more solutions more viable. That doesn't necessarily tell you anything about what would work for the non-relaxed-restrictions scenario, though.

In any case, that you can implement new features/checks/etc. using new annotations/keywords/syntax/etc. is entirely uncontroversial. If anything, doing such a thing is bog-standard part of language development and evolution. It's the claims that those new features/checks can be implemented without (or effectively without, if you want to be more pedantic) new annotations/keywords/etc. that grabs headlines, and it's those claims that subsequently garner skepticism when there's no good explanation nor examples of how they would work.

In this case, the "no annotations" part of the claim is basically the entire reason for the claims being discussed to be considered extraordinary, so relaxing that requirement kind of renders the entire conversation moot.

-3

u/Wonderful-Wind-905 Jan 13 '26

But even rust region checking is unsound, as per googling 'blazingly fast memory vulnerabilities, written in 100% safe rust.' The brilliant rustc developers have wrestled with this for years, it seems really difficult. Polonius may help a bit, but it's still WIP.

And if diving into the lake of unsafe, it can become tricky. Like this code

    pub struct Vec<T> {
        ptr: *mut T,
        len: usize,
        cap: usize,
    }


    impl<T> Vec<T> {
        pub fn push(&mut self, elem: T) {
            if self.len == self.cap {
                self.reallocate();
            }
            unsafe {
                ptr::write(self.ptr.add(self.len), elem);
                self.len += 1;
            }
        }
    }


    pub fn make_room(&mut self) {
        self.cap += 1;
    }


This has undefined behavior, due to a bug in make_room(&mut self), even though it is not marked unsafe.

6

u/ts826848 Jan 13 '26

But even rust region checking is unsound, as per googling 'blazingly fast memory vulnerabilities, written in 100% safe rust.'

It would probably be wise to avoid conflating the soundness of Rust's implementation of borrow checking with the soundness of borrow checking in general. That the concept itself is sound is quite undisputable, I think - consider Ada SPARK's adoption of a borrow checker-like feature (emphasis in original):

The main scientific contributions of this report are the following: the design of rules for access types in SPARK, inspired by Rust’s borrow-checker and affine typing, enforcing Concurrent Read, Exclusive Write (CREW ) and an intra-procedural move semantics.

Or the formalizations of various subsets of Rust that include the borrow checker such as RustBelt and extensions thereof like RefinedRust.

And from a different perspective, I think if one can claim that their borrow checking implementation has "Rust-like lifetime safety" due to having the same soundness holes that would probably be considered a success for most use cases given that those holes basically never show up in real-world use. And if the implementation doesn't have those holes? Even better!

And if diving into the lake of unsafe, it can become tricky. Like this code [snip] This has undefined behavior, due to a bug in make_room(&mut self), even though it is not marked unsafe.

This seems like a non-sequitur to me? The peculiarities of unsafe in Rust are completely unrelated to what Sean is talking about (i.e., whether runtime borrow checking is possible at all). On top of that, the claim Sean discusses would obviate the need for unsafe in the first place ("you can use any algorithm you want, and it doesn't require modifying existing code").

-1

u/Wonderful-Wind-905 Jan 13 '26

Sorry, a question: u/stl .

Was the sibling post made by ts826848 created using AI? And do you like or dislike that sibling post?

4

u/STL MSVC STL Dev Jan 13 '26

Doesn't sound like AI at all. I don't care about what you're talking about at all.