DieHard: An error-resistant memory allocator for Windows, Linux, and Mac OS X
github.com
github.com
That this is useful is a good argument to get out of C/C++ and into Go, or Rust, or just about anything with subscript checking.
I've been using C since ~ 1980 (sic), loathe C++, and, yeah, I'm going to investigate Rust in due course.
The memory errors in C++ are not exactly memory error, they are, like other errors, a misconception on the state of the object graph. And it's hard to find and fix.
Both rely on a carefully designed semantics under which one can provide a reasonable interpretation for erroneous computations. They employ sophisticated randomized algorithms and/or statistical inference procedures and contain proofs of their effectiveness. That's not exactly a hack.
The fact is that managed languages that provide memory safety guarantees -- through strong type systems, mandatory bounds checks and automatic memory management -- are of course preferable to C/C++/Objective-C from a software engineering perspective.
But the fact also remains that most of the code we use today is written in unsafe languages, including the runtime systems that undergird managed languages. Since these programs almost certainly contain errors, it makes sense to accept that fact and work to provide solutions.
I'd encourage you to read this article, which appeared in Communications of the ACM - "Software Needs Seatbelts and Airbags", which contains an in-depth discussion of these issues.
http://cacm.acm.org/magazines/2012/9/154577-software-needs-s...
That content, which initially appeared in ACM Queue, is freely available here on my blog:
http://emeryblogger.com/2012/05/31/software-needs-seatbelts-...
Follow-on work from DieHard, which probabilistically tolerates memory errors, includes the following: Archipelago, which trades virtual address space for reliability; Exterminator, which automatically corrects memory errors with high probability; and DieHarder, which secures the heap against attack. The Github repo contains the code for all of these.
More info here -
http://emeryberger.com/research/diehard/
http://emeryberger.com/research/archipelago/
http://emeryberger.com/research/exterminator/
http://emeryberger.com/research/dieharder/
DieHarder talk at Woot 2011: https://www.usenix.org/conference/woot11/dieharder-securing-...
However the vast majority of all C code ever written does not; most code is written by mere mortals and contains various bugs, including memory bugs and undefined behavior.
Why can't the C standards committee introduce a memory safe mode for C? Make it opt-in. Prohibit aliasing without special overriding casts. Bounds check array accesses.
I'm willing to trade some performance to prevent the next Heartbleed; I can throw VMs at my problems very cheaply. Having to tell everyone on the Internet to change their passwords (and every server op to get a new SSL key) is a multi-billion dollar waste of human capital for no reason. And that's just one C memory error. New zero day vulnerabilities, many due to C memory errors are continuously discovered. How many more Heartbleeds does it take?
When talking about C it's important to understand why the standard is the way it is. C was designed to be a portable language. Not in the sense that code is easily portable, but that the language itself is easily portable by writing a new compiler for it on whatever OS you're using. As such, the C standard is written to make C an easy language to write a compiler for, which is why it has so much undefined behavior.
Otherwise Java, Rust, Go et al. wouldn't need to incur the runtime overhead of the extra bookkeeping int per array.
https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Code-Gen-Option...
There have been bounds-checked implementations of C, but the runtime cost is very large. The problem is that to do it, you need to check not only explicit array references, but pointer dereferences as well -- and in order to make that work, each pointer needs to carry around with it its associated bounds. That, in turn, means that your pointer representation has to be something more (or other) than just a plain machine address.
Wrong people. It would be the compiler writers who would do that, since they know the target machine.