Skip to content

Commit c576965

Browse files
committed
Fixes
1 parent 835810c commit c576965

File tree

1 file changed

+91
-68
lines changed

1 file changed

+91
-68
lines changed

Zend/zend_alloc.c

Lines changed: 91 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,71 @@ static void stderr_last_error(char *msg)
514514
}
515515
#endif
516516

517+
static void _zend_mm_set_custom_handlers_ex(zend_mm_heap *heap,
518+
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
519+
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
520+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
521+
size_t (*_gc)(void),
522+
void (*_shutdown)(bool, bool))
523+
{
524+
#if ZEND_MM_CUSTOM
525+
zend_mm_heap *_heap = (zend_mm_heap*)heap;
526+
527+
if (!_malloc && !_free && !_realloc) {
528+
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE;
529+
} else {
530+
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
531+
_heap->custom_heap._malloc = _malloc;
532+
_heap->custom_heap._free = _free;
533+
_heap->custom_heap._realloc = _realloc;
534+
_heap->custom_heap._gc = _gc;
535+
_heap->custom_heap._shutdown = _shutdown;
536+
}
537+
#endif
538+
}
539+
540+
541+
static void _zend_mm_get_custom_handlers_ex(zend_mm_heap *heap,
542+
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
543+
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
544+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
545+
size_t (**_gc)(void),
546+
void (**_shutdown)(bool, bool))
547+
{
548+
#if ZEND_MM_CUSTOM
549+
zend_mm_heap *_heap = (zend_mm_heap*)heap;
550+
551+
if (heap->use_custom_heap) {
552+
*_malloc = _heap->custom_heap._malloc;
553+
*_free = _heap->custom_heap._free;
554+
*_realloc = _heap->custom_heap._realloc;
555+
if (_gc != NULL) {
556+
*_gc = _heap->custom_heap._gc;
557+
}
558+
if (_shutdown != NULL) {
559+
*_shutdown = _heap->custom_heap._shutdown;
560+
}
561+
} else {
562+
*_malloc = NULL;
563+
*_free = NULL;
564+
*_realloc = NULL;
565+
if (_gc != NULL) {
566+
*_gc = NULL;
567+
}
568+
if (_shutdown != NULL) {
569+
*_shutdown = NULL;
570+
}
571+
}
572+
#else
573+
*_malloc = NULL;
574+
*_free = NULL;
575+
*_realloc = NULL;
576+
*_gc = NULL;
577+
*_shutdown = NULL;
578+
#endif
579+
}
580+
581+
517582
/*****************/
518583
/* OS Allocation */
519584
/*****************/
@@ -3509,6 +3574,7 @@ static void* poison_realloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_L
35093574
oldsize -= sizeof(zend_mm_debug_info);
35103575
#endif
35113576

3577+
ZEND_MM_UNPOISON(ptr, MIN(oldsize, size));
35123578
memcpy(new, ptr, MIN(oldsize, size));
35133579
poison_free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
35143580
}
@@ -3526,12 +3592,12 @@ static size_t poison_gc(void)
35263592
size_t (*_gc)(void);
35273593
void (*_shutdown)(bool, bool);
35283594

3529-
zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown);
3530-
zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL);
3595+
_zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown);
3596+
_zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL);
35313597

35323598
size_t collected = _zend_mm_gc(heap);
35333599

3534-
zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
3600+
_zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
35353601

35363602
return collected;
35373603
}
@@ -3546,17 +3612,18 @@ static void poison_shutdown(bool full, bool silent)
35463612
size_t (*_gc)(void);
35473613
void (*_shutdown)(bool, bool);
35483614

3549-
zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown);
3550-
zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL);
3615+
_zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown);
3616+
_zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL);
35513617

35523618
if (heap->debug.check_freelists_on_shutdown) {
35533619
zend_mm_check_freelists(heap);
35543620
}
35553621

35563622
zend_mm_shutdown(heap, full, silent);
3623+
ZEND_MM_UNPOISON_HEAP(heap);
35573624

35583625
if (!full) {
3559-
zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
3626+
_zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
35603627
}
35613628
}
35623629

@@ -3637,7 +3704,7 @@ static void poison_enable(zend_mm_heap *heap, char *parameters)
36373704
tmp++;
36383705
}
36393706

3640-
zend_mm_set_custom_handlers_ex(heap, poison_malloc, poison_free,
3707+
_zend_mm_set_custom_handlers_ex(heap, poison_malloc, poison_free,
36413708
poison_realloc, poison_gc, poison_shutdown);
36423709
}
36433710
#endif
@@ -3737,95 +3804,51 @@ ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap)
37373804
#endif
37383805
}
37393806

3740-
ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
3807+
ZEND_API void zend_mm_set_custom_handlers_ex(zend_mm_heap *heap,
37413808
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
37423809
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3743-
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
3810+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3811+
size_t (*_gc)(void),
3812+
void (*_shutdown)(bool, bool))
37443813
{
3745-
#if ZEND_MM_CUSTOM
37463814
ZEND_MM_UNPOISON_HEAP(heap);
3747-
zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL);
3748-
ZEND_MM_POISON_HEAP(heap);
3749-
#endif
3815+
_zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
3816+
ZEND_MM_UNPOISON_HEAP(heap);
37503817
}
37513818

3752-
ZEND_API void zend_mm_set_custom_handlers_ex(zend_mm_heap *heap,
3819+
ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
37533820
void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
37543821
void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3755-
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3756-
size_t (*_gc)(void),
3757-
void (*_shutdown)(bool, bool))
3822+
void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
37583823
{
37593824
#if ZEND_MM_CUSTOM
37603825
ZEND_MM_UNPOISON_HEAP(heap);
3761-
zend_mm_heap *_heap = (zend_mm_heap*)heap;
3762-
3763-
if (!_malloc && !_free && !_realloc) {
3764-
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE;
3765-
} else {
3766-
_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
3767-
_heap->custom_heap._malloc = _malloc;
3768-
_heap->custom_heap._free = _free;
3769-
_heap->custom_heap._realloc = _realloc;
3770-
_heap->custom_heap._gc = _gc;
3771-
_heap->custom_heap._shutdown = _shutdown;
3772-
}
3826+
_zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL);
37733827
ZEND_MM_POISON_HEAP(heap);
37743828
#endif
37753829
}
37763830

3777-
ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
3831+
ZEND_API void zend_mm_get_custom_handlers_ex(zend_mm_heap *heap,
37783832
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
37793833
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3780-
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
3834+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3835+
size_t (**_gc)(void),
3836+
void (**_shutdown)(bool, bool))
37813837
{
3782-
#if ZEND_MM_CUSTOM
37833838
ZEND_MM_UNPOISON_HEAP(heap);
3784-
zend_mm_get_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL);
3839+
_zend_mm_get_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown);
37853840
ZEND_MM_POISON_HEAP(heap);
3786-
#endif
37873841
}
37883842

3789-
ZEND_API void zend_mm_get_custom_handlers_ex(zend_mm_heap *heap,
3843+
ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap,
37903844
void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
37913845
void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3792-
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC),
3793-
size_t (**_gc)(void),
3794-
void (**_shutdown)(bool, bool))
3846+
void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC))
37953847
{
37963848
#if ZEND_MM_CUSTOM
37973849
ZEND_MM_UNPOISON_HEAP(heap);
3798-
3799-
zend_mm_heap *_heap = (zend_mm_heap*)heap;
3800-
3801-
if (heap->use_custom_heap) {
3802-
*_malloc = _heap->custom_heap._malloc;
3803-
*_free = _heap->custom_heap._free;
3804-
*_realloc = _heap->custom_heap._realloc;
3805-
if (_gc != NULL) {
3806-
*_gc = _heap->custom_heap._gc;
3807-
}
3808-
if (_shutdown != NULL) {
3809-
*_shutdown = _heap->custom_heap._shutdown;
3810-
}
3811-
} else {
3812-
*_malloc = NULL;
3813-
*_free = NULL;
3814-
*_realloc = NULL;
3815-
if (_gc != NULL) {
3816-
*_gc = NULL;
3817-
}
3818-
if (_shutdown != NULL) {
3819-
*_shutdown = NULL;
3820-
}
3821-
}
3850+
_zend_mm_get_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL);
38223851
ZEND_MM_POISON_HEAP(heap);
3823-
#else
3824-
*_malloc = NULL;
3825-
*_free = NULL;
3826-
*_realloc = NULL;
3827-
*_gc = NULL;
3828-
*_shutdown = NULL;
38293852
#endif
38303853
}
38313854

0 commit comments

Comments
 (0)