This module provides various building blocks for a dynamic allocator. This is composed of the following parts:
block: An implementation of a linked list of memory blocks, supporting splitting and merging of blocks.
freelist: A freelist, suitable for fast lookups of available memory chunks (i.e.
Heap Integrity Check#
Block class provides two check functions:
bool Block::IsValid(): Returns
trueis the given block is valid and
void Block::CrashIfInvalid(): Crash the program and output the reason why the check fails using
By default, this module disables heap poisoning since it requires extra space.
User can enable heap poisoning by enabling the
$ gn args out # Modify and save the args file to use heap poison. pw_allocator_POISON_HEAP = true
When heap poisoning is enabled,
pw_allocator will add
bytes before and after the usable space of each
Block, and paint the space
with a hard-coded randomized pattern. During each check,
will check if the painted space still remains the pattern, and return
if the pattern is damaged.
pw_allocator supplies a pw command
pw heap-viewer to help visualize
the state of the heap at the end of a dump file. The heap is represented by
ASCII characters, where each character represents 4 bytes in the heap.
The heap visualizer can be launched from a shell using the Pigweed environment.
$ pw heap-viewer --dump-file <directory of dump file> --heap-low-address <hex address of heap lower address> --heap-high-address <hex address of heap lower address> [options]
The required arguments are:
--dump-fileis the path of a file that contains
malloc/freeinformation. Each line in the dump file represents a
mallocis represented as
m <size> <memory address>and
freeis represented as
f <memory address>. For example, a dump file should look like:
m 20 0x20004450 # malloc 20 bytes, the pointer is 0x20004450 m 8 0x2000447c # malloc 8 bytes, the pointer is 0x2000447c f 0x2000447c # free the pointer at 0x2000447c ...
Any line not formatted as the above will be ignored.
--heap-low-addressis the start of the heap. For example:
--heap-high-addressis the end of the heap. For example:
Options include the following:
--poison-enable: If heap poisoning is enabled during the allocation or not. The value is
Falseif the option is not specified and
--pointer-size <integer of pointer size>: The size of a pointer on the machine where
malloc/freeis called. The default value is
Note, this module, and its documentation, is currently incomplete and experimental.