r/cpp • u/pedersenk • Mar 20 '26
C++/sys - A Standard Library Projection to Facilitate the Verification of Run-time Memory Safety
Hi all,
A while ago I was looking for a relevant C++ conference to demonstrate the capabilities of C++/sys. A pure C++ mechanism which offers a new approach to memory safety via debug-time verification.
My talk was accepted into C++Online 2026 which concluded a couple of weeks ago. The discussions were great, I received some really useful feedback (and the little 2D gather town was pretty fun!).
The slides are available here and the release video should appear on YouTube in the following weeks.
Excited to share the tech with the wider community. Much of it is discussed in the slides; some highlights:
- Offers improvements over AddressSanitizer in terms of C++ error detection rate
- Tracks access lifespan rather than blocks of data
- Has contextual knowledge of C++ objects and containers so generally quite performant
- 100% detection rate for executed paths
- Memory pinning and locking mechanism tracks dangling 'this' and references
- Zero overhead in release builds
- Compatible with any standard C++ compiler (from hosted C++98 running on MS-DOS to freestanding C++${LATEST} on Zephyr)
- Doesn't interfere with C code/libraries. Just like Java, Rust, Python thin-bindings, these are treated as unchecked.
Some of the above sounds like magic but the core mechanism allowing for this is quite simple. Temporaries created by operator->, operator\* and operator[] lock memory for the lifetime of access and pointers are entirely prevented from dangling in a coarse-grained manner. Obviously this has an impact on what designs are possible with some discussion in the slides.
Link to the open-source (BSD 3-clause) reference implementation is here.
https://codeberg.org/kpedersen/sys_public
https://research.thamessoftware.co.uk/sys
Very happy to discuss further. It has some rough areas but I personally love the tech and find it makes writing and testing C++ considerably more satisfying knowing that there is virtually zero chance of a memory error lurking in the tested branches.
Update: Video of the talk is now released and available here.
4
u/duneroadrunner Mar 22 '26
I can quite appreciate what you're doing here. The SaferCPlusPlus library (my project) incorporates some similar techniques. The SaferCPlusPlus library provides a variety of "safe" versions of (the commonly used) standard library elements with various tradeoffs between compatibility/flexibility, performance, and dependence on static analysis/enforcement, but I don't think it (yet) covers the tradeoff set that C++/sys seems to, which I gather is roughly:
i) basically full lifetime and bounds safety if you stick to only using the library's "owning" pointers and containers, and non-owning reference types (i.e. pointers and iterators), with the exception that you can use raw references as function parameter types
ii) some run-time performance and modest run-time size cost
iii) some limitation in flexibility (eg. no dangling references even if they are not actually dereferenced when dangling)
iv) some modest divergence from the standard library interface (eg. non-owning pointer targets need to derive from
sys::base)Is that a reasonable characterization?
The SaferCPlusPlus library provides options for basically three tradeoff sets (that can be mixed-and-matched):
a) high-performance, somewhat divergent interface for accessing the contents of dynamic owning pointers and containers, limitations on raw references and pointers (can't be null, can't point to anything that doesn't outlive them), significant dependence on a static analyzer/enforcer for safety
b) high-performance, significantly divergent interface, less dependence on a static analyzer/enforcer for safety, but if not using the static analyzer/enforcer then native references and pointers are unsafe and there will be significant limitations on the library's zero-overhead pointers
c) interface-replicating drop-in replacements for standard C++ elements (including raw pointers (that can dangle), but not raw references), not high-performance, any interaction with raw pointers or references remain unsafe
One key thing that C++/sys does is implicit locking of the owning pointers and containers for the lifetime of the expression in which the dereferencing or content accessing occurs. This makes it safe to use references as function parameters because function parameters will never outlive any temporary argument they bind to.
Currently the dereference and content access operators and methods of the owning pointers and containers in SaferCPlusPlus' option a) listed above also return a "proxy reference" object that locks the owning pointer or container (like
sys::auto_ptr<>::operator*()does). But for some reason they currently do not support conversion to raw references. (They only support conversion to value and assignment.) I'll have to change that and teach the static analyzer that the reference is valid for the (temporary) lifetime of the "proxy reference" object.And the "trick" of using implicit conversion of the subscript operator argument to unlock the vector at the end of the expression instead of relying on a custom return object is rather slick. But I notice your vector doesn't seem to have
front()orback()member functions. You could implement those functions with a default argument and use the same trick, no? Unfortunately C++ doesn't seem to allow default arguments for dereference operators for some reason, so you can't use the same technique with the pointers I guess?While C++/sys does implicit locking well, options a) and b) of the SaferCPlusPlus library provide the option for the programmer to explicitly lock the owning pointer/container. I think this is key to achieving the performance necessary for release builds. With explicit locking, the programmer can "hoist" the structure locking (and its run-time cost) outside of any hot inner loops, even though the actual dereferencing or content accessing happens inside the loop. This makes the overall performance cost of structure locking effectively negligible.
You have the option of structure locking with "exclusive" or non-exclusive access.
Structure locking with non-exclusive access basically corresponds to what happens in C++/sys, I think. Structure locking with exclusive access (aka "borrowing a fixed object" in SaferCPlusPlus parlance) theoretically allows for potentially more optimal implementations, but also enables the programmer to explicitly express their intent (i.e. whether or not they expect anyone else to be accessing the container) and helps catch bugs when those expectations are violated. It can also be applied to standard library smart pointers and containers, like
std::vector<>s. When applied to standard library pointers and containers, it protects the (owned) contents by temporarily moving the contents out of the pointer/container when the "borrow" is initiated and returns the (possibly modified) contents when the borrow ends.The explicit structure locking interface is a departure from the standard interface, but I mean, it doesn't strike me as necessarily more egregious than, for example, requiring objects to inherit from
sys::base<>(more on that below), right? And I think the payoff of achieving the performance necessary for release builds is well worth it.And at this point I don't necessarily consider this explicit locking of dynamic owning pointers and containers to be "unfortunate noise". The way I see it, it provides more information about the programmer's intent and makes it clear where lifetime safety mechanisms are needed and where the cost is being incurred. I see it as arguably increasing the quality and clarity of the code.
Ok, I also notice that C++/sys seems to require the target of an "observer" pointer to inherit from
sys::base. The webkit guys do the same thing. I guess it's intuitive, but it seems intrusive and limits the set of types that can be targeted, right? The SaferCPlusPlus version instead uses the target type as a public base class. This let's you use essentially any existing type as the (base) target type. (Even types that cannot act as a base class, via template specialization.) These sorts of "observer" pointers are the most flexible "safe" pointers we have, so I think it's important to avoid unnecessarily limiting their functionality. For example, the flexibility of the SaferCPlusPlus "observer"/non-owning pointers enables the scpptool to support a feature that can auto-translate C code to the memory-safe subset of C++ it enforces.Speaking of auto-translation, while other commenters point out the sensible reticence of developers to adopt solutions with non-standard interfaces, I can't help wondering if auto-translation and AI-assisted translation might sufficiently lower the adoption barrier. I mean it's one thing for a developer to adopt a non-standard interface. It may be another thing for the developer to stick to using the standard interface in a way that's amenable to being auto-translated to the (safe) non-standard interface. I guess my point is that even if the non-standard interface is an insurmountable barrier right now, it may not be as much the case for long.
Anyway, it's nice to see another project participating in the "safer C++" library space and demonstrating innovative techniques. Good work! :)
1
u/thelvhishow Mar 20 '26
I watched briefly the slides. Are you suggesting to have a completely separate standard library?
Wouldn’t be possible to have the standard library already doing this? libc++ and libstdc++ are open source already (not sure about msvc)
-1
u/t_hunger Mar 21 '26
So it is quantum memory-safe? The safety level offered changes depending on whether I look at the results (debug mode) or not (release mode)?
I build up an impressive test suite and fix all the bugs C++/sys uncovers. I am sure there will be a lot, it seems to cover quite some problematic areas. I then deploy the code and somebody triggers the next heartbleed my tests missed. While there is financial motivation to find memory safety issues out there in the wild, people will spot all the issues my test suite missed -- sooner or later.
It is a interesting approach and will definitely catch bugs, which is always a good thing. It is just some strange use of "memory-safe". There is no guarantee that a program using this is memory-safe (it just was not proven to be memory-unsafe in debug mode)... much less so the language used to write that program as a whole.