Skip to content

Commit a754765

Browse files
committed
fix bug claude found where we modify the big zone list unintentionally
1 parent 664d6cf commit a754765

4 files changed

Lines changed: 60 additions & 10 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ tests: clean library_debug_unit_tests
428428
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/thread_tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/thread_tests $(LDFLAGS)
429429
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) $(UNIT_TESTING) tests/big_canary_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/big_canary_test $(LDFLAGS)
430430
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/big_tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/big_tests $(LDFLAGS)
431+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/big_zone_chunksz.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/big_zone_chunksz $(LDFLAGS)
431432
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/double_free.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/double_free $(LDFLAGS)
432433
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/big_double_free.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/big_double_free $(LDFLAGS)
433434
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/heap_overflow.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/heap_overflow $(LDFLAGS)

‎src/iso_alloc.c‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,19 +1779,23 @@ INTERNAL_HIDDEN iso_alloc_big_zone_t *iso_find_big_zone(void *p, bool remove) {
17791779
check_big_canary(big_zone);
17801780
/* Only an exact match of the address is valid */
17811781
if(p == big_zone->user_pages_start) {
1782-
if(remove == true && prev != NULL) {
1783-
prev->next = big_zone->next;
1784-
}
1782+
/* A lookup (remove == false) must not modify the list */
1783+
if(remove == true) {
1784+
if(prev != NULL) {
1785+
prev->next = big_zone->next;
1786+
}
17851787

1786-
_root->big_zone_used_count--;
1788+
_root->big_zone_used_count--;
1789+
1790+
/* If this is our first iteration then we are returning the
1791+
* used list head. Set its new value to the next entry */
1792+
if(prev == NULL) {
1793+
_root->big_zone_used = big_zone->next;
1794+
}
17871795

1788-
/* If this is our first iteration then we are returning the
1789-
* used list head. Set its new value to the next entry */
1790-
if(prev == NULL) {
1791-
_root->big_zone_used = big_zone->next;
1796+
big_zone->next = NULL;
17921797
}
17931798

1794-
big_zone->next = NULL;
17951799
UNLOCK_BIG_ZONE_USED();
17961800
return big_zone;
17971801
}

‎tests/big_zone_chunksz.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* iso_alloc big_zone_chunksz.c
2+
* Copyright 2026 - chris.rohlf@gmail.com */
3+
4+
/* Regression test for iso_find_big_zone(p, remove=false). A size
5+
* lookup via iso_chunksz() must not modify the big zone used list.
6+
* Previously a single lookup unlinked the matched zone (and severed
7+
* the list tail on a mid-list match) which made the subsequent
8+
* iso_free() of those chunks abort */
9+
#include "iso_alloc.h"
10+
#include "iso_alloc_internal.h"
11+
12+
int main(int argc, char *argv[]) {
13+
const size_t big_sz = SMALL_SIZE_MAX * 2;
14+
15+
/* Used list after these allocations: c -> b -> a */
16+
void *a = iso_alloc(big_sz);
17+
void *b = iso_alloc(big_sz);
18+
void *c = iso_alloc(big_sz);
19+
20+
if(a == NULL || b == NULL || c == NULL) {
21+
return ERR;
22+
}
23+
24+
/* Lookup the head of the used list */
25+
if(iso_chunksz(c) < big_sz) {
26+
return ERR;
27+
}
28+
29+
/* Lookup a mid-list entry */
30+
if(iso_chunksz(b) < big_sz) {
31+
return ERR;
32+
}
33+
34+
/* Repeated lookups should still find every entry */
35+
if(iso_chunksz(a) < big_sz || iso_chunksz(b) < big_sz || iso_chunksz(c) < big_sz) {
36+
return ERR;
37+
}
38+
39+
/* With the bug these frees abort with "Could not find any zone" */
40+
iso_free(a);
41+
iso_free(b);
42+
iso_free(c);
43+
44+
return OK;
45+
}

‎utils/run_tests.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# examples of code that should crash
44
$(echo '' > test_output.txt)
55

6-
tests=("tests" "big_tests" "interfaces_test" "thread_tests" "pool_test"
6+
tests=("tests" "big_tests" "big_zone_chunksz" "interfaces_test" "thread_tests" "pool_test"
77
"rand_freelist")
88
failure=0
99
succeeded=0

0 commit comments

Comments
 (0)