This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
DieHard is an error-resistant memory allocator that provides probabilistic memory safety for C/C++ programs. It tolerates memory errors (buffer overflows, dangling pointers, double frees, invalid frees) that would crash programs using standard allocators.
The repository contains three related systems:
- DieHard: Increases reliability by probabilistically avoiding memory errors
- DieHarder: Security-hardened variant with stronger randomization (CCS 2010)
- Exterminator: Automatically fixes memory errors using statistical inference (PLDI 2007)
# Standard build (scalable mode is enabled by default)
mkdir build && cd build
cmake ..
make
# Build with DieHarder (security-focused variant)
cmake -DBUILD_DIEHARDER=ON ..
make
# Build without scalable mode (single global heap, not recommended)
cmake -DBUILD_SCALABLE=OFF ..
make
# Build with replicated mode support
cmake -DBUILD_DIEHARDER=ON -DBUILD_REPLICATED=ON ..
makeOutput libraries:
libdiehard.so/libdiehard.dylib- Main DieHard librarylibdieharder.so- Security-hardened variant (when enabled)libdieharder_r.so- Replicated variant for multi-replica execution
# Linux: preload to replace malloc
LD_PRELOAD=$PWD/libdiehard.so ./your_application
# macOS: preload to replace malloc
DYLD_INSERT_LIBRARIES=$PWD/libdiehard.dylib ./your_application
# Replicated mode (Linux): run with N replicas for higher reliability
diehard 3 /path/to/libdiehard_r.so yourappThe allocator approximates "infinite-heap semantics" through randomization:
- Segregated size classes: Heap partitioned into regions for power-of-two sizes (16B to 1MB on 64-bit; larger objects use mmap)
- Bitmap allocation: One bit per object slot tracks allocation state (no per-object headers)
- Random placement: Objects placed randomly within their size class region
- Metadata separation: All heap metadata stored separately from heap objects (prevents heap corruption)
The current implementation uses an adaptive algorithm (different from the static heap in the original PLDI 2006 paper):
- Each size class maintains a collection of miniheaps that grow dynamically
- Miniheaps double in size: if allocation would exceed 1/M fullness, a new miniheap is created with 2x the capacity of the previous largest
- Objects are allocated randomly across all miniheaps for a given size class
- The heap multiplier M (default: Numerator=8, Denominator=7) ensures total capacity is M times the maximum live memory
- Random probing finds free slots in O(1) expected time since heap is at most 1/M full
This adaptive approach (from the Exterminator/PLDI 2007 work) avoids the need to pre-specify heap size while maintaining probabilistic safety guarantees.
src/source/libdieharder.cpp- Main library entry point, definesxxmalloc/xxfreesrc/include/diehardheap.h-DieHardHeapclass managing multiple size classessrc/include/randomheap.h-RandomHeapmanaging mini-heaps for a specific sizesrc/include/randomminiheap.h- Dispatches to DieHard or DieHarder mini-heap implementationssrc/include/randomminiheap-diehard.h- DieHard-specific allocation logicsrc/include/randomminiheap-dieharder.h- DieHarder-specific allocation with page tablesrc/include/util/bitmap.h- Standard bitmap for allocation trackingsrc/include/util/atomicbitmap.h- Lock-free atomic bitmap (for scalable mode)src/include/heapregistry.h- Registry of per-thread heap instances (for scalable mode)src/include/globalfreepool.h- Cross-thread free pool (for scalable mode)src/include/objectownership.h- Ownership tracking heap wrapper (for scalable mode)
The heap is parameterized by:
Numerator/Denominator- Heap expansion ratio (default 8/7, meaning heap is 8/7x max live size)MaxSize- Maximum object size for small object heap (default 1MB)DieFastOn- Enable fill patterns for overflow detectionDieHarderOn- Enable security hardening with page tables
Controlled by compile definitions:
DIEHARD_DIEHARDER=0- Standard DieHard (reliability focused)DIEHARD_DIEHARDER=1- DieHarder (security focused)DIEHARD_MULTITHREADED=1- Thread-safe allocationDIEHARD_REPLICATED=1- Support for replica-based executionDIEHARD_SCALABLE=1- Scalable per-thread heap design
The default build uses a scalable design that replaces the single global locked heap with per-thread heaps for improved multi-core performance:
Architecture:
Thread[0] Thread[1] Thread[N]
| | |
v v v
PerThreadHeap[0] PerThreadHeap[1] PerThreadHeap[N]
(lock-free alloc) (lock-free alloc) (lock-free alloc)
| | |
+----------------------------+----------------------------+
|
v
HeapRegistry
(tracks heap instances for
cross-thread ownership lookup)
|
v
GlobalFreePool (batched)
(cross-thread frees collected here,
redistributed to owner heaps periodically)
Key Components:
src/include/util/atomicbitmap.h- Lock-free bitmap using CAS operationssrc/include/heapregistry.h- Registry of per-thread heap instances for ownership detectionsrc/include/globalfreepool.h- Per-owner-thread queues for cross-thread freessrc/include/objectownership.h- Heap wrapper that routes cross-thread frees
How it works:
- Each thread has its own
DieHardHeapinstance viaThreadSpecificHeap - Allocation uses
AtomicBitMapwith CAS for lock-free slot claiming RandomHeapcounters are atomic with cache-line alignment to prevent false sharing- On free, ownership is determined by querying each heap's
getSize()(fast path checks own heap first) - Cross-thread frees are queued in the owner's
GlobalFreePoolqueue - Owner thread drains its queue periodically (on allocation or when threshold reached)
Performance: The scalable mode achieves ~9x speedup over non-scalable at 8 threads.
Probabilistic guarantees are preserved: Each per-thread heap maintains the same randomization properties as the original design
- Heap-Layers - Composable heap infrastructure
- printf - Heap-safe printf implementation
DieHard provides probabilistic guarantees against memory errors:
- Buffer overflows: Random placement means overflows likely hit empty space. With heap 1/2 full, ~50% chance single-object overflow is benign; with 3 replicas, >99% protection.
- Dangling pointers: Random reuse means freed objects unlikely to be immediately overwritten. For 8-byte objects with 10,000 intervening allocations, >99.5% safe.
- Double/invalid frees: Bitmap checks make these no-ops (completely eliminated).
- Heap metadata corruption: Impossible since metadata is segregated from heap data.
The replicated mode runs multiple processes with different random seeds; output is voted on to detect/mask errors including uninitialized reads.
- Supported: Linux, macOS (arm64, arm64e, x86_64)
- Not supported: Windows (currently)
- C++14 required