Skip to content

Commit e66df63

Browse files
structclaude
andcommitted
Fix memory tagging, heap profiler, aligned allocation, and interface bugs
Memory tagging: fix the tag verification comparison which never matched a valid tag, fix the whole zone tag refresh condition and write through the unmasked user pages pointer, reject one-past-the-end pointers, and use modulo for the chunk offset check since chunk sizes are not always a power of 2. Freed chunks now always receive a tag that differs from the previous one so a stale tagged pointer can never verify. Heap profiler: size the zone profiler map for SMALL_SIZE_MAX inclusive, copy the full trace arrays, compute usage percentages with float math, check dladdr results, and call the free profiler under the root lock. Walk frame pointers directly for backtraces since gcc returns 0 for __builtin_return_address(n > 0) on aarch64. Declare profiler globals extern in the header so the profiler links under -fno-common. Aligned allocation: posix_memalign, memalign, aligned_alloc, and the aligned C++ new operators now honor the requested alignment through _iso_alloc_aligned. Alignments up to a page come from an exact fit zone, larger ones from an over-mapped and trimmed big zone. Hooks validate alignment per POSIX. Throwing operator new retries via the new handler and throws bad_alloc rather than returning NULL. Interfaces: iso_alloc_new_zone returns NULL instead of a masked NULL, iso_zone_chunk_count handles a NULL handle. Sanity: only check the sampled chunk canary when it was written. Tests: add aligned_alloc_test to the test suite, aligned new coverage to the C++ tests, valid_tag_ptr_test and tag_refresh_test to the tagging suite, and a profiler_tests target with profiler_test. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent f55c8ca commit e66df63

19 files changed

Lines changed: 1212 additions & 149 deletions

‎Makefile‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PERM_FREE_REALLOC = -DPERM_FREE_REALLOC=0
2121
## 1 byte tag per addressable chunk of memory. These tags can
2222
## be retrieved and verified. This feature will likely interfere
2323
## with ARM MTE and PAC. See MEMORY_TAGGING.md for more information
24-
MEMORY_TAGGING = -DMEMORY_TAGGING=0
24+
MEMORY_TAGGING = -DMEMORY_TAGGING=1
2525

2626
## Enable abort() when isoalloc can't gather enough entropy.
2727
ABORT_NO_ENTROPY = -DABORT_NO_ENTROPY=1
@@ -90,8 +90,12 @@ THP_PAGES = -DTHP_PAGES=1
9090
## program. This file encodes the heap usage patterns of
9191
## the target. This file can be consumed by the profiler
9292
## CLI utility. See PROFILER.md for the format of this file
93-
#HEAP_PROFILER = -DHEAP_PROFILER=1 -fno-omit-frame-pointer \
94-
# -fno-optimize-sibling-calls -ldl
93+
#HEAP_PROFILER = $(HEAP_PROFILER_FLAGS)
94+
95+
## Flags required to build with the heap profiler. Backtraces
96+
## are collected by walking frame pointers so they must be kept
97+
HEAP_PROFILER_FLAGS = -DHEAP_PROFILER=1 -fno-omit-frame-pointer \
98+
-fno-optimize-sibling-calls
9599

96100
## Enable CPU pinning support on a per-zone basis. This is
97101
## a minor security feature which introduces an allocation
@@ -425,6 +429,7 @@ tests: clean library_debug_unit_tests
425429
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/tests.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tests $(LDFLAGS)
426430
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) $(UNIT_TESTING) tests/uaf.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf $(LDFLAGS)
427431
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/interfaces_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/interfaces_test $(LDFLAGS)
432+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/aligned_alloc_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/aligned_alloc_test $(LDFLAGS)
428433
$(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)
429434
$(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)
430435
$(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)
@@ -463,9 +468,19 @@ tagging_tests: clean cpp_library_debug
463468
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/uaf_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/uaf_tag_ptr_test $(LDFLAGS)
464469
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/bad_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/bad_tag_ptr_test $(LDFLAGS)
465470
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/verify_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/verify_tag_ptr_test $(LDFLAGS)
471+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/valid_tag_ptr_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/valid_tag_ptr_test $(LDFLAGS)
472+
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/tag_refresh_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tag_refresh_test $(LDFLAGS)
466473
$(CXX) -DMEMORY_TAGGING=1 $(CXXFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(EXE_CFLAGS) $(OS_FLAGS) tests/tagged_ptr_test.cpp $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/tagged_ptr_test_cpp $(LDFLAGS)
467474
utils/run_tagging_tests.sh
468475

476+
## Build the library with the heap profiler and run its test.
477+
## The profiler data file is checked by the runner script
478+
profiler_tests: clean
479+
@echo "make profiler_tests"
480+
$(CC) $(CFLAGS) $(HEAP_PROFILER_FLAGS) $(LIBRARY) $(OS_FLAGS) $(UNIT_TESTING) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(C_SRCS) -o $(BUILD_DIR)/$(LIBNAME) -ldl
481+
$(CC) $(CFLAGS) $(HEAP_PROFILER_FLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/profiler_test.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/profiler_test $(LDFLAGS) -ldl
482+
utils/run_profiler_tests.sh
483+
469484
init_test: clean library_debug_unit_tests
470485
@echo "make init_test"
471486
$(CC) $(CFLAGS) $(EXE_CFLAGS) $(DEBUG_LOG_FLAGS) $(GDB_FLAGS) $(OS_FLAGS) tests/init_destroy.c $(ISO_ALLOC_PRINTF_SRC) -o $(BUILD_DIR)/init_destroy $(LDFLAGS)
@@ -530,6 +545,6 @@ format-ci:
530545

531546
clean:
532547
rm -rf build/* tests_perf_analysis.txt big_tests_perf_analysis.txt gmon.out \
533-
tagging_test_output.txt test_output.txt *.dSYM core* iso_alloc_profiler.data
548+
tagging_test_output.txt test_output.txt profiler_test_output.txt *.dSYM core* iso_alloc_profiler.data
534549
rm -rf android/libs android/obj
535550
mkdir -p build/

‎include/iso_alloc_internal.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ extern pthread_mutex_t root_busy_mutex;
367367
/* The global root */
368368
extern iso_alloc_root *_root;
369369

370+
/* These are C functions and may be called from iso_alloc.cpp */
371+
#ifdef __cplusplus
372+
extern "C" {
373+
#endif
370374
INTERNAL_HIDDEN INLINE void check_big_canary(iso_alloc_big_zone_t *big);
371375
INTERNAL_HIDDEN INLINE void check_canary(iso_alloc_zone_t *zone, const void *p);
372376
INTERNAL_HIDDEN INLINE void iso_clear_user_chunk(uint8_t *p, size_t size);
@@ -377,6 +381,7 @@ INTERNAL_HIDDEN INLINE void flush_chunk_quarantine(void);
377381
INTERNAL_HIDDEN INLINE void clear_zone_cache(void);
378382
INTERNAL_HIDDEN iso_alloc_big_zone_t *iso_find_big_zone(void *p, bool remove);
379383
INTERNAL_HIDDEN FLATTEN iso_alloc_zone_t *is_zone_usable(iso_alloc_zone_t *zone, size_t size);
384+
INTERNAL_HIDDEN iso_alloc_zone_t *find_exact_fit_zone(size_t size);
380385
INTERNAL_HIDDEN iso_alloc_zone_t *find_suitable_zone(size_t size);
381386
INTERNAL_HIDDEN iso_alloc_zone_t *iso_new_zone(size_t size, bool internal);
382387
INTERNAL_HIDDEN iso_alloc_zone_t *_iso_new_zone(size_t size, bool internal, int32_t index);
@@ -389,6 +394,7 @@ INTERNAL_HIDDEN INLINE bit_slot_t get_next_free_bit_slot(iso_alloc_zone_t *zone)
389394
INTERNAL_HIDDEN iso_alloc_root *iso_alloc_new_root(void);
390395
INTERNAL_HIDDEN bool is_pow2(uint64_t sz);
391396
INTERNAL_HIDDEN bool _is_zone_retired(iso_alloc_zone_t *zone);
397+
INTERNAL_HIDDEN void _iso_alloc_refresh_chunk_tag(void *p, iso_alloc_zone_t *zone);
392398
INTERNAL_HIDDEN bool _refresh_zone_mem_tags(iso_alloc_zone_t *zone);
393399
INTERNAL_HIDDEN iso_alloc_zone_t *_iso_free_internal_unlocked(void *p, bool permanent, iso_alloc_zone_t *zone);
394400
INTERNAL_HIDDEN void fill_free_bit_slots(iso_alloc_zone_t *zone);
@@ -414,8 +420,9 @@ INTERNAL_HIDDEN INLINE void dont_need_pages(void *p, size_t size);
414420
INTERNAL_HIDDEN void *_tag_ptr(void *p, iso_alloc_zone_t *zone);
415421
INTERNAL_HIDDEN void *_untag_ptr(void *p, iso_alloc_zone_t *zone);
416422
INTERNAL_HIDDEN void _free_big_zone_list(iso_alloc_big_zone_t *head);
417-
INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_big_alloc(size_t size);
423+
INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_big_alloc(size_t size, size_t alignment);
418424
INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_alloc(iso_alloc_zone_t *zone, size_t size);
425+
INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_alloc_aligned(size_t size, size_t alignment);
419426
INTERNAL_HIDDEN INLINE ASSUME_ALIGNED void *_iso_alloc_bitslot_from_zone(bit_slot_t bitslot, iso_alloc_zone_t *zone);
420427
INTERNAL_HIDDEN ASSUME_ALIGNED void *_iso_calloc(size_t nmemb, size_t size);
421428
#if UAF_PTR_PAGE
@@ -430,6 +437,9 @@ INTERNAL_HIDDEN size_t _iso_chunk_size(void *p);
430437
INTERNAL_HIDDEN int64_t check_canary_no_abort(iso_alloc_zone_t *zone, const void *p);
431438
INTERNAL_HIDDEN void _iso_alloc_initialize(void);
432439
INTERNAL_HIDDEN void _iso_alloc_destroy(void);
440+
#ifdef __cplusplus
441+
}
442+
#endif
433443

434444
#if ARM_MTE
435445
INLINE void *iso_mte_untag_ptr(void *p);

‎include/iso_alloc_profiler.h‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,33 @@
1818
/* The IsoAlloc profiler is not thread local but these
1919
* globals should only ever be touched by internal
2020
* allocator functions when the root is locked */
21-
uint64_t _alloc_count;
22-
uint64_t _free_count;
23-
uint64_t _alloc_sampled_count;
24-
uint64_t _free_sampled_count;
25-
26-
int32_t profiler_fd;
21+
extern uint64_t _alloc_count;
22+
extern uint64_t _free_count;
23+
extern uint64_t _alloc_sampled_count;
24+
extern uint64_t _free_sampled_count;
2725

2826
typedef struct {
2927
uint64_t total;
3028
uint64_t count;
3129
} zone_profiler_map_t;
3230

33-
zone_profiler_map_t _zone_profiler_map[SMALL_SIZE_MAX];
31+
/* These are defined once in iso_alloc_profiler.c. This
32+
* header is included by several translation units so
33+
* defining them here fails to link under -fno-common */
34+
extern int32_t profiler_fd;
35+
36+
/* Indexed by chunk size, which can be exactly SMALL_SIZE_MAX */
37+
extern zone_profiler_map_t _zone_profiler_map[SMALL_SIZE_MAX + 1];
3438

3539
/* iso_alloc_traces_t is a public structure, and
3640
* is defined in the public header iso_alloc.h */
37-
iso_alloc_traces_t _alloc_bts[BACKTRACE_DEPTH_SZ];
38-
size_t _alloc_bts_count;
41+
extern iso_alloc_traces_t _alloc_bts[BACKTRACE_DEPTH_SZ];
42+
extern size_t _alloc_bts_count;
3943

4044
/* iso_free_traces_t is a public structure, and
4145
* is defined in the public header iso_alloc.h */
42-
iso_free_traces_t _free_bts[BACKTRACE_DEPTH_SZ];
43-
size_t _free_bts_count;
46+
extern iso_free_traces_t _free_bts[BACKTRACE_DEPTH_SZ];
47+
extern size_t _free_bts_count;
4448

4549
INTERNAL_HIDDEN INLINE uint64_t _get_backtrace_hash(void);
4650
INTERNAL_HIDDEN INLINE void _save_backtrace(iso_alloc_traces_t *abts);

0 commit comments

Comments
 (0)