Files
hakmem/docs/FREE_SAFETY.md

32 lines
1.5 KiB
Markdown
Raw Normal View History

Free Safety (Debug Guards)
==========================
Goal
- Detect invalid/double free and class mismatches early, with minimal intrusion on hot paths (debugonly).
Envs
- `HAKMEM_SAFE_FREE=1`
- Enable freetime validations (SS range, block alignment/size/capacity, light freelist scan for duplicates).
- `HAKMEM_SAFE_FREE_STRICT=1`
- FailFast (emit Tiny Ring + raise SIGUSR2) when invalid free is detected.
Checks (SuperSlab path)
- Pointer must map to a registered `SuperSlab` (registry lookup + `magic` check).
- Block alignment: `(ptr - slab_base) % block_size == 0` and `< block_size * capacity`.
- Optional duplicate scan: traverse up to 64 nodes of `meta->freelist` to see if `ptr` is already present.
- Cross lookup note: the same virtual base may be reused by a different class after SS free/cache reuse.
- Two consecutive `free_enter` with different classes on the same pointer likely means double free, not freelist corruption.
Checks (TinySlab path)
- Registry lookup + membership (ptr in `[base, base+64KB)`).
- Optional duplicate scan (same as above; beware of cost).
Ring Guidance
- Record `free_enter` before validation.
- On invalid free: record ring with pointer/class and boundary that failed; if STRICT then raise SIGUSR2.
Limitations
- No perblock headers: we avoid storing tags in user memory; detection focuses on boundary and freelist duplication checks.
- Reallocatethenfree(UAF) after the pointer is reused may evade duplicate scan; STRICT mode is recommended when chasing crashes.