Skip to content

Drop zend_mm_set_custom_debug_handlers() #13457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,44 @@ PHP 8.4 INTERNALS UPGRADE NOTES
* The inet_aton() and win32/inet.h on Windows have been removed. Use Windows
native inet_pton() from ws2tcpip.h.

* zend_mm_set_custom_debug_handlers() has been removed from ZendMM, use
zend_mm_set_custom_handlers() instead which now supports DEBUG builds

* zend_mm_set_custom_handlers() has changed its signature from
void()(zend_mm_heap *heap,
void* (*_malloc)(size_t),
void (*_free)(void*),
void* (*_realloc)(void*, size_t))
to
void()(zend_mm_heap *heap,
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))

* zend_mm_get_custom_handlers() has changed its signature from
void()(zend_mm_heap *heap,
void* (**_malloc)(size_t),
void (**_free)(void*),
void* (**_realloc)(void*, size_t))
to
void()(zend_mm_heap *heap,
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))

* __zend_malloc() has changed their signature from
void(*)(size_t) to
void(*)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)

* __zend_calloc() has changed their signature from
void(*)(size_t, size_t) to
void(*)(size_t, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)

* __zend_realloc() has changed their signature from
void(*)(void *, size_t) to
void(*)(void *, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)


========================
2. Build system changes
========================
Expand Down
148 changes: 48 additions & 100 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,10 @@ struct _zend_mm_heap {
int last_chunks_delete_boundary; /* number of chunks after last deletion */
int last_chunks_delete_count; /* number of deletion over the last boundary */
#if ZEND_MM_CUSTOM
union {
struct {
void *(*_malloc)(size_t);
void (*_free)(void*);
void *(*_realloc)(void*, size_t);
} std;
struct {
void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
} debug;
struct {
void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
} custom_heap;
HashTable *tracked_allocs;
#endif
Expand Down Expand Up @@ -2261,7 +2254,7 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
#endif

#if ZEND_MM_CUSTOM
static void *tracked_malloc(size_t size);
static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
static void tracked_free_all(void);
#endif

Expand All @@ -2272,7 +2265,7 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)

#if ZEND_MM_CUSTOM
if (heap->use_custom_heap) {
if (heap->custom_heap.std._malloc == tracked_malloc) {
if (heap->custom_heap._malloc == tracked_malloc) {
if (silent) {
tracked_free_all();
}
Expand All @@ -2281,17 +2274,13 @@ void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
zend_hash_destroy(heap->tracked_allocs);
free(heap->tracked_allocs);
/* Make sure the heap free below does not use tracked_free(). */
heap->custom_heap.std._free = free;
heap->custom_heap._free = __zend_free;
}
heap->size = 0;
}

if (full) {
if (ZEND_DEBUG && heap->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
heap->custom_heap.debug._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
} else {
heap->custom_heap.std._free(heap);
}
heap->custom_heap._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC);
}
return;
}
Expand Down Expand Up @@ -2412,7 +2401,7 @@ ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr
{
#if ZEND_MM_CUSTOM
if (UNEXPECTED(heap->use_custom_heap)) {
if (heap->custom_heap.std._malloc == tracked_malloc) {
if (heap->custom_heap._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) {
Expand Down Expand Up @@ -2455,7 +2444,7 @@ ZEND_API bool is_zend_ptr(const void *ptr)
{
#if ZEND_MM_CUSTOM
if (AG(mm_heap)->use_custom_heap) {
if (AG(mm_heap)->custom_heap.std._malloc == tracked_malloc) {
if (AG(mm_heap)->custom_heap._malloc == tracked_malloc) {
zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2;
zval *size_zv = zend_hash_index_find(AG(mm_heap)->tracked_allocs, h);
if (size_zv) {
Expand Down Expand Up @@ -2492,48 +2481,18 @@ ZEND_API bool is_zend_ptr(const void *ptr)
return 0;
}

#if ZEND_MM_CUSTOM

static ZEND_COLD void* ZEND_FASTCALL _malloc_custom(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
return AG(mm_heap)->custom_heap.debug._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
} else {
return AG(mm_heap)->custom_heap.std._malloc(size);
}
}

static ZEND_COLD void ZEND_FASTCALL _efree_custom(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
AG(mm_heap)->custom_heap.debug._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
} else {
AG(mm_heap)->custom_heap.std._free(ptr);
}
}

static ZEND_COLD void* ZEND_FASTCALL _realloc_custom(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
if (ZEND_DEBUG && AG(mm_heap)->use_custom_heap == ZEND_MM_CUSTOM_HEAP_DEBUG) {
return AG(mm_heap)->custom_heap.debug._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
} else {
return AG(mm_heap)->custom_heap.std._realloc(ptr, size);
}
}
#endif

#if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
#undef _emalloc

#if ZEND_MM_CUSTOM
# define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
} \
} while (0)
# define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \
_efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
return; \
} \
} while (0)
Expand Down Expand Up @@ -2618,7 +2577,7 @@ ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LI
{
#if ZEND_MM_CUSTOM
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
return _malloc_custom(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
}
#endif
return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
Expand All @@ -2628,7 +2587,7 @@ ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_OR
{
#if ZEND_MM_CUSTOM
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
_efree_custom(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
return;
}
#endif
Expand All @@ -2639,7 +2598,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC
{
#if ZEND_MM_CUSTOM
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}
#endif
return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
Expand All @@ -2649,7 +2608,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size
{
#if ZEND_MM_CUSTOM
if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) {
return _realloc_custom(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
}
#endif
return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
Expand Down Expand Up @@ -2844,7 +2803,7 @@ static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t ad
}
}

static void *tracked_malloc(size_t size)
static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
zend_mm_heap *heap = AG(mm_heap);
tracked_check_limit(heap, size);
Expand All @@ -2859,7 +2818,7 @@ static void *tracked_malloc(size_t size)
return ptr;
}

static void tracked_free(void *ptr) {
static void tracked_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {
if (!ptr) {
return;
}
Expand All @@ -2871,7 +2830,7 @@ static void tracked_free(void *ptr) {
free(ptr);
}

static void *tracked_realloc(void *ptr, size_t new_size) {
static void *tracked_realloc(void *ptr, size_t new_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) {
zend_mm_heap *heap = AG(mm_heap);
zval *old_size_zv = NULL;
size_t old_size = 0;
Expand All @@ -2889,7 +2848,7 @@ static void *tracked_realloc(void *ptr, size_t new_size) {
zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) old_size_zv);
}

ptr = __zend_realloc(ptr, new_size);
ptr = __zend_realloc(ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
tracked_add(heap, ptr, new_size);
heap->size += new_size - old_size;
return ptr;
Expand Down Expand Up @@ -2921,14 +2880,14 @@ static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)

if (!tracked) {
/* Use system allocator. */
mm_heap->custom_heap.std._malloc = __zend_malloc;
mm_heap->custom_heap.std._free = free;
mm_heap->custom_heap.std._realloc = __zend_realloc;
mm_heap->custom_heap._malloc = __zend_malloc;
mm_heap->custom_heap._free = __zend_free;
mm_heap->custom_heap._realloc = __zend_realloc;
} else {
/* Use system allocator and track allocations for auto-free. */
mm_heap->custom_heap.std._malloc = tracked_malloc;
mm_heap->custom_heap.std._free = tracked_free;
mm_heap->custom_heap.std._realloc = tracked_realloc;
mm_heap->custom_heap._malloc = tracked_malloc;
mm_heap->custom_heap._free = tracked_free;
mm_heap->custom_heap._realloc = tracked_realloc;
mm_heap->tracked_allocs = malloc(sizeof(HashTable));
zend_hash_init(mm_heap->tracked_allocs, 1024, NULL, NULL, 1);
}
Expand Down Expand Up @@ -2990,9 +2949,9 @@ ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap)
}

ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
void* (*_malloc)(size_t),
void (*_free)(void*),
void* (*_realloc)(void*, size_t))
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
{
#if ZEND_MM_CUSTOM
zend_mm_heap *_heap = (zend_mm_heap*)heap;
Expand All @@ -3001,25 +2960,25 @@ ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE;
} else {
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
_heap->custom_heap.std._malloc = _malloc;
_heap->custom_heap.std._free = _free;
_heap->custom_heap.std._realloc = _realloc;
_heap->custom_heap._malloc = _malloc;
_heap->custom_heap._free = _free;
_heap->custom_heap._realloc = _realloc;
}
#endif
}

ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
void* (**_malloc)(size_t),
void (**_free)(void*),
void* (**_realloc)(void*, size_t))
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
{
#if ZEND_MM_CUSTOM
zend_mm_heap *_heap = (zend_mm_heap*)heap;

if (heap->use_custom_heap) {
*_malloc = _heap->custom_heap.std._malloc;
*_free = _heap->custom_heap.std._free;
*_realloc = _heap->custom_heap.std._realloc;
*_malloc = _heap->custom_heap._malloc;
*_free = _heap->custom_heap._free;
*_realloc = _heap->custom_heap._realloc;
} else {
*_malloc = NULL;
*_free = NULL;
Expand All @@ -3032,23 +2991,6 @@ ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
#endif
}

#if ZEND_DEBUG
ZEND_API void zend_mm_set_custom_debug_handlers(zend_mm_heap *heap,
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
{
#if ZEND_MM_CUSTOM
zend_mm_heap *_heap = (zend_mm_heap*)heap;

_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_DEBUG;
_heap->custom_heap.debug._malloc = _malloc;
_heap->custom_heap.debug._free = _free;
_heap->custom_heap.debug._realloc = _realloc;
#endif
}
#endif

ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap)
{
#if ZEND_MM_STORAGE
Expand Down Expand Up @@ -3134,7 +3076,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
#endif
}

ZEND_API void * __zend_malloc(size_t len)
ZEND_API void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
void *tmp = malloc(len);
if (EXPECTED(tmp || !len)) {
Expand All @@ -3143,17 +3085,17 @@ ZEND_API void * __zend_malloc(size_t len)
zend_out_of_memory();
}

ZEND_API void * __zend_calloc(size_t nmemb, size_t len)
ZEND_API void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
void *tmp;

len = zend_safe_address_guarded(nmemb, len, 0);
tmp = __zend_malloc(len);
tmp = __zend_malloc(len ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
memset(tmp, 0, len);
return tmp;
}

ZEND_API void * __zend_realloc(void *p, size_t len)
ZEND_API void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
p = realloc(p, len);
if (EXPECTED(p || !len)) {
Expand All @@ -3162,6 +3104,12 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
zend_out_of_memory();
}

ZEND_API void __zend_free(void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
{
free(p);
return;
}

ZEND_API char * __zend_strdup(const char *s)
{
char *tmp = strdup(s);
Expand Down
Loading