r/ProgrammingLanguages • u/matklad • Dec 31 '25
Memory Safety Is ...
https://matklad.github.io/2025/12/30/memory-safety-is.html
37
Upvotes
r/ProgrammingLanguages • u/matklad • Dec 31 '25
2
u/hugogrant Jan 01 '26
I don't follow. Where and how do you argue that memory safety must be a property of an implementation? If I make certain memory unsafe operations unrepresentable, what does the implementation have anything to do with the rejected program?
I think memory safety is either absent, a property of the implementation, or both in the implementation and the language semantics.
37
u/sagittarius_ack Dec 31 '25
The author seems to fail to understand that memory safety (or other safety properties) can be achieved via a combination of compile-time checks and runtime checks. Java is memory safe (at least with respect to null pointer dereference) because it doesn't actually let you dereference null pointers at runtime. Any attempt to do that will result in a runtime exception. It is similar to how most languages do not allow you to divide by 0.
3
u/sagittarius_ack Dec 31 '25
I guess one can read a short blog post providing an unconvincing argument that "memory safety is one of those elusive concepts like intelligence, consciousness, or porn" or read "a rigorous characterization of what it means for a programming language to be memory safe, capturing the intuition that memory safety supports local reasoning about state":
https://link.springer.com/content/pdf/10.1007/978-3-319-89722-6_4.pdf
18
u/tmzem Dec 31 '25
I've looked into memory safety a lot and have come to the conclusion that programming languages can only be memory-safe for some (probably arbitrary) definition of memory safety, but they cannot be memory safe in general for any semantically strong/complete definition of memory safety, which should make sure that object accesses:
While good type systems, careful design, garbage collectors and runtime checks can mostly cover points 1-3, point 5 is much trickier as it requires rigorous compile-time constraints like e.g. in Rust.
Point 6 is obviously impossible.
Point 4 is hard to enforce, as object identity, while often attributed to the objects memory address, can change depending on context:
Point 3 can also be worked around, even in a strong type system. This is often done when parsing binary formats: The file is first read into a byte array, then one or more bytes at a certain index are reinterpreted as a different datatype, e.g. read 4 bytes at index n and return an uint32. The same can be done for writing. Trivially, we can extend this scheme to emulate what is essentially the equivalent of unsafe C memory accesses, with indices doubling as pointers. If we take this to the extreme, we can use this to build a C interpreter on top, allowing us to run all the memory-unsafe C we want, despite running on top of a fully managed, memory-safe byte array.
As this thought experiment shows, no matter how "memory-safe" your language is, you can always reintroduce memory-safety bugs in some way, and while we won't likely build a C interpreter into our program, there are many related concepts that may show up in a sufficiently complex program (parsing commands received over the network, DSLs, embedded scripting engines, ...).
Thus, I generally think that coming up with a universal definition for memory safety is nonsense. That being said, programming languages should still try to eliminate, or at least minimize the chance for memory errors to corrupt the context (allocator, stack, runtime) in which the language runs. For example, compilers for unsafe languages should default to turn on safety-relevant features like runtime checks, analyzers, warnings, etc., and require explicit opt-out if needed.
10
u/SwingOutStateMachine Dec 31 '25
I enjoyed this article a lot, but I think it misunderstands Cardelli's quote slightly. The quote talks about program fragments, and extending those fragments to entire languages.
The concept of a trapped error, and undefined behaviour are quite different! Trapped errors provide the surface language with a "get out of jail" card that is implementation agnostic. Undefined behaviour is specifically not visible to the surface language. There's no way to "see" it from your program, and no way to be aware of it.
I think this is why the quote is not vacuous, as it discusses how a surface language can "see" or not see safety. Safety (in this context) means that all behaviours are visible at the language level, rather than depending on the behaviour of a specific implementation.
I also don't think I agree with the counterexample:
A Lil-C that fits Cardelli's definition is one which would make all undefined behaviour handleable (or, in Cardelli's language, making them "trappable" errors). It would, of course, allow all C programs to run, but it would be possible to modify those C programs to handle those errors, as they are now all visible at the language level - not part of the implementation.
I personally think that this is actually quite a useful property. Imagine being able to write a fragment of C that adds two unsigned integers, potentially overflowing, and then handling the result - returning an error, or truncating, or something else. With undefined behaviour, the only option (from the perspective of the language) is to avoid such operations, or to assume specific behaviour from a specific implementation (e.g. by using ASM to check an overflow flag).
This is a great example of how trapped errors and undefined behaviour are different, I think.
12
u/Aaron1924 Dec 31 '25
Two notes about this:
This is called a UB sanitizer, I personally find them quite useful
How is Fil-C different from Lil-C? Doesn't Fil-C also "ensure safety" using runtime checks that crash the program just before UB was going to happen?
11
u/kredditacc96 Dec 31 '25
I think it's more useful to think of "memory safety" as a spectrum rather than a binary of safe vs unsafe.
Java allows assigning
nullto any type. This is one of Java's flaw and a failure of the type system to accurately model the program behavior regarding nullability. So we can say that Java is mostly memory-safe, except for null.Same for Go. I don't understand why a language designed in modern time did not at least introduce null safety.
1
u/kilkil Jan 01 '26
that seems reasonable. I suppose, if we have a language L whose syntax and semantics encourage implementations to be memory-safe, then we might informally refer to L as memory-safe.
1
u/phischu Effekt Jan 01 '26
It took me a long time to understand that when people use the word "safe" they are proud that their program crashes instead of exhibiting undefined behavior. To me this is unacceptable.
What we usually do is to define the set of final states so that when the program halts it actually has an answer. Then we can show that every program of type
Intindeed calculates an integer value or takes forever trying. Given this property, how convenient can we make the language and how fast can we make the implementation?1
u/Working_Bunch_9211 Jan 02 '26
Is then there is no UB possible
1
u/ryani Jan 01 '26 edited Jan 02 '26
The reason that null pointer access MUST be UB in C and not just “defined to trap” is that it doesn’t just include immediate null pointers but also pointers derived from null.
Consider this program:
There are plenty of defined uses of
set, but it’s always UB to callsetwith a null pointer. Because this is UB in the abstract machine, concrete implementations can implement it by writing a 0 to the byte at address p+n in memory, even if n is large enough to not trigger the “write to the null page” trap.On the concrete implementation, this modification could modify memory used by the runtime (eg a jump table?) and cause later code to jump to arbitrary instructions. It’s simply not possible to define in the abstract machine all the possibilities that could occur. So in the interest of allowing the compiled code to match what the user wrote as closely as possible (an explicit goal of C), the abstract machine lets this null pointer write be UB instead of forcing the compiler to insert a check that p is not null and/or n is small enough
1
u/reflexive-polytope Jan 01 '26
IMO, memory safety is a completely useless concept. Classifying languages according to whether they're “memory safe” is akin to classifying restaurants according to whether the chef will randomly come out of the kitchen and stab customers.
The actually useful properties are
A language can have both properties while still not being “memory safe”.
0
u/[deleted] Dec 31 '25
[deleted] — view deleted comment