From 3148da8ee1d997a1494d5b478c0be22c5f1980fc Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Thu, 3 Aug 2023 10:08:41 +0200 Subject: [PATCH] Add block size support for tracked_malloc (#11856) This does still deviate from USE_ZEND_ALLOC=0 in that we're not rounding up the size of the allocation to fixed sizes. Doing so would suppress some out-of-bounds errors checked by ASAN. Rounding up the size in _zend_mm_block_size would not be good either as it would break code like memset(ptr, 0 _zend_mm_block_size(ptr)). --- Zend/zend_alloc.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 7e83f067a4f..5dcf7ce5d71 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2401,6 +2401,18 @@ void* ZEND_FASTCALL _zend_mm_realloc2(zend_mm_heap *heap, void *ptr, size_t size ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { +#if ZEND_MM_CUSTOM + if (UNEXPECTED(heap->use_custom_heap)) { + if (heap->custom_heap.std._malloc == tracked_malloc) { + zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; + zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h); + if (size_zv) { + return Z_LVAL_P(size_zv); + } + } + return 0; + } +#endif return zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); } @@ -2636,12 +2648,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { -#if ZEND_MM_CUSTOM - if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { - return 0; - } -#endif - return zend_mm_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); + return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); } ZEND_API void* ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)