Skip to content

Conversation

@Grazfather
Copy link
Collaborator

@Grazfather Grazfather commented Jan 19, 2026

While testing out @tact1m4n3 's wifi work in #861, I got suspicious that we were relying on an allocator that hadn't actually been properly vetted. I used Claude to write a bunch of tests.

These test cases passed!

I then noticed that we could initialize the heap with a size too small for the metadata. I changed init to return an error (API change).

Finally, with Claude I looked at how dbg_integrity_check works, and noticed some issues. Fixing that pointed to extra bugs which we fixed:

  • Chunk constants calculation
    • header_size now uses @offsetOf(Chunk, "prior_free") to account for padding and to not hardcode types.
    • min_size now uses @sizeOf(Chunk) so also account for potential padding, plus to not hardcode e.g. how many pointers a chunk contains.
    • alignment now uses @alignOf(Chunk) instead of @alignOf(Alloc), this was just straight up incorrect, but the alignment was probably the same.
  • do_alloc and do_resize:
    • Both functions had a bug where trailing trim could create a chunk smaller than min_size
    • Added check: our_new_size >= Chunk.min_size before trimming
    • Also changed < to <= in the trim chunk size check for correctness

1. Improve `dbg_integrity_check`:
- Add mutex protection
- Fix boundary check: != instead of >
- Add zero-size chunk check (prevents infinite loop)
- Add minimum chunk size check
- Add alignment checks for addresses and sizes
- Add free list doubly-linked list integrity check
- Add free list correct binning check
- Fix misleading error message

2. Cleanup `Chunk` constants calculation
- header_size now uses `@offsetOf(Chunk, "prior_free")`
- min_size now uses `@sizeOf(Chunk)`
- alignment now uses `@alignOf(Chunk)` instead of `@alignOf(Alloc)`

3. Fix bug fixes in `do_alloc` and `do_resize`:
- Both functions had a bug where trailing trim could create a chunk smaller than min_size
- Added check: our_new_size >= Chunk.min_size before trimming
- Also changed < to <= in the trim chunk size check for correctness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants