Modules

Overwriting_Allocator

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Overwriting_Allocator";

A debugging allocator that fills memory with a recognizable byte on allocation and on free.

get_overwriting_allocator wraps another allocator. Use it with push_allocator while hunting for code that reads memory before writing it or after freeing it.

Example↗
allocator := get_overwriting_allocator();
{
    push_allocator(allocator);
    bytes := cast(*u8) alloc(4);
    assert(bytes[0] == 0xcd);
    free(bytes);
}
deinit_overwriting_allocator(allocator);

Procedures#

(base_allocator: Allocator = .{}, overwrite_byte: u8 = 0xcd) -> Allocator

Returns an allocator that wraps base_allocator (the current allocator when unset) and fills memory with overwrite_byte when it is allocated and again when it is freed, so reads of uninitialized or freed memory show an obvious pattern. Freeing a pointer it did not hand out, or twice, is reported. Free it with deinit_overwriting_allocator.

allocator := get_overwriting_allocator();
memory := cast(*u8) allocator.proc(.ALLOCATE, 4, 0, null, allocator.data);
assert(memory[0] == 0xcd);
memory[0] = 1;
allocator.proc(.FREE, 0, 4, memory, allocator.data);
deinit_overwriting_allocator(allocator);
↗

Types#

:: struct

The state of an overwriting allocator: the allocator it wraps, a record of live allocations and the fill byte.

base_allocator: Allocator
table: H.Table(*void, u64)
overwrite_byte: u8
mutex: Lock