How grapheneos/hardened_malloc works
hardened_malloc is a hardened general-purpose allocator that replaces the standard malloc family on Android or Linux. The principal workflow routes application allocation and release requests through allocator entry points, which service small requests from slabs and large requests from guarded page regions. It hardens those paths with randomized allocation state, quarantine, and optional Arm memory tagging, while exposing compatibility and statistics extensions. The source excerpts show key allocator and memory interfaces; several documented features are described in the README but their implementation paths are not fully sampled, so those links are omitted where unverified.
Main entry points: h_malloc.c Allocator core · h_malloc.h C allocation API · new.cc C++ new/delete · pages.c Guarded pages
Components 13
Allocator API
- C allocation API
include/h_malloc.h - C++ new/delete
new.cc - Stats and extensions
h_malloc.c
Allocation engine
- Allocator core
h_malloc.c - Slab allocations
h_malloc.c - Large allocations
h_malloc.c
Memory platform
- Guarded pages
pages.c - Virtual memory operations
memory.c
Hardening primitives
- Random state
random.c - ChaCha stream
chacha.c - Memory tagging
memtag.h - Arm MTE operations
arm_mte.h
Other components
- Application
Connections 13
- Application → C allocation API: requests allocation
README.md - C allocation API → Allocator core: dispatches API
h_malloc.c - C++ new/delete → Allocator core: calls allocation
new.cc - Allocator core → Slab allocations: allocates small
h_malloc.c - Allocator core → Large allocations: allocates large
h_malloc.c - Allocator core → Guarded pages: requests pages
h_malloc.c - Guarded pages → Virtual memory operations: maps and protects
pages.c - Allocator core → Random state: uses randomness
h_malloc.c - Random state → ChaCha stream: generates stream
random.c - Allocator core → Memory tagging: handles tagged pointers
h_malloc.c - Memory tagging → Arm MTE operations: uses MTE primitives
memtag.h - Application → C++ new/delete: uses C++ allocation
README.md - C allocation API → Stats and extensions: exposes extensions
include/h_malloc.h