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
View all comments
Show parent comment
1
u/koflerdavid Jan 03 '26 edited Jan 03 '26
Correctness is a distinct concept from memory safety and type safety. Of course merely having the latter two is not enough to guarantee correctness. But nobody claims that.
Java could have easily forbidden data races, but then there would be a huge performance impact since allowing out-of-order execution would require expensive analysis and a global, serialized view of all memory would have to be enforced, which would kill performance for NUMA architectures. Also, new concurrency primitives would probably be impossible to provide without implementing them in the JVM. (More importantly, it would also have broken existing applications, but that's irrelevant for the sake of this discussion, which is about how a clean room design should have looked like). But also in this case the impact is correctness, not memory safety.
Java already provides a feature to protect against unrestricted reflection usage: the JPMS. Furthermore, in the near future it will become impossible to modify non-static final fields via reflection (I also can't believe how long this took), and other integrity measures are coming too. For example the
sun.misc.Unsafeloose end will be tied up. Access to JNA and the new FFM will be restricted.Circumventing these integrity measures is possible, but it happens at the behest of those that are responsible for the application: the people that control the startup flags of the JVM. They also control which instrumentation tools are deployed. If the JVM didn't make it possible to circumvent integrity measures and add instrumentation tools then there would quickly be a fork of the JVM that does so. You might argue that many applications require it in practice, but that's the ecosystem's fault.