Sometimes, instead of trying to detect every possible error, we want a more reliable compiler that, in case we missed something, it will prevent the program from crashing. DieHard addresses this issue via probabilistic memory safety, thus reducing the likelihood of problems caused by:
- Dangling pointers (using a pointer to a previously freed space)
- Buffer overflows (writing beyond the space allocated to an object – very typical with strings)
- Heap metadata overwrites (out-of-bounds write to the heap metadata area)
- Uninitialized reads (read from unallocated memory, or allocated but not yet initialized)
- Invalid frees (trying to free a space not allocated)
- Double frees (self-explanatory)
To try DieHard, I downloaded it from github and compiled it.
Following the instructions caused the following error:
In file included from source/libdieharder.cpp:29: In file included from Heap-Layers/heaplayers.h:107: In file included from Heap-Layers/locks/all.h:1: Heap-Layers/locks/maclock.h:39:10: fatal error: 'os/lock.h' file not found #include <os/lock.h> ^ 1 error generated. make: *** [macos] Error 1
Therefore, before explaining how to use DieHard with some examples, I’ll explain how to get rid of this error.
- Download DieHard according to instructions
git clone --recursive https://github.com/emeryberger/DieHard
- Enter the file with the error
- Copy the problematic file to have as backup
- Edit maclock.h and remove the following lines
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1012
#define USE_UNFAIR_LOCKS 1
#define USE_UNFAIR_LOCKS 0
- Get back to src:
The compiled library libdiehard.dylib should now appear within src.