Skip to content

Commit

Permalink
pw_allocator: Check for room to split the first block
Browse files Browse the repository at this point in the history
Changing the check in Block::AllocLast(). Usually, during an
allocation, a free block is checked for enough space for the allocation.
If there more than enough space, but not enough to hold another block,
the excess is appended to the previous Block. This is not possible with
the first Block since there isn't a previous Block. In the case of the
first Block, the extra space will become part of the block and the Block
will be returned to the caller. Before this change, even when there was
not enough room for the first Block to be split into two, it was. This
caused heap corruption and eventually a crash.

As part of this fix, the unit tests were made less brittle and more
exhaustive by adding test utilities that manage aligned and unaligned
buffers, and then ensuring unit test coverage of every path though
AllocFirst, CanAllocLast, and AllocLast.

Bug: 366175024
Change-Id: Ic3c9538e5e33eaf507f08f0d3ac54361b5dcf998
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/235312
Pigweed-Auto-Submit: Aaron Green <[email protected]>
Reviewed-by: John Comito <[email protected]>
Reviewed-by: Ewout van Bekkum <[email protected]>
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Keir Mierle <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Docs-Not-Needed: Aaron Green <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
  • Loading branch information
jpcomito authored and CQ Bot Account committed Sep 13, 2024
1 parent 62d79c9 commit 82759cc
Show file tree
Hide file tree
Showing 8 changed files with 685 additions and 212 deletions.
1 change: 1 addition & 0 deletions pw_allocator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ pw_cc_test(
"freelist_heap_test.cc",
],
deps = [
":block_testing",
":freelist_heap",
"//pw_bytes:alignment",
],
Expand Down
1 change: 1 addition & 0 deletions pw_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ pw_test("fragmentation_test") {

pw_test("freelist_heap_test") {
deps = [
":block_testing",
":freelist_heap",
"$dir_pw_bytes:alignment",
]
Expand Down
1 change: 1 addition & 0 deletions pw_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ pw_add_test(pw_allocator.freelist_heap_test
SOURCES
freelist_heap_test.cc
PRIVATE_DEPS
pw_allocator.block_testing
pw_allocator.freelist_heap
pw_bytes.alignment
GROUPS
Expand Down
Loading

0 comments on commit 82759cc

Please sign in to comment.