Skip to content

Commit 88cbee2

Browse files
committed
Add additional information of the memory_limit exceeded error messages
1 parent 2209c1a commit 88cbee2

File tree

1 file changed

+64
-40
lines changed

1 file changed

+64
-40
lines changed

Zend/zend_alloc.c

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,17 @@ static ZEND_COLD ZEND_NORETURN void zend_mm_panic(const char *message)
368368
abort();
369369
}
370370

371-
static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap,
372-
const char *format,
373-
size_t limit,
374-
#if ZEND_DEBUG
375-
const char *filename,
376-
uint32_t lineno,
377-
#endif
378-
size_t size)
371+
static ZEND_COLD ZEND_NORETURN void zend_mm_safe_error(zend_mm_heap *heap, char* format, ...)
379372
{
373+
va_list args;
380374

381375
heap->overflow = 1;
382376
zend_try {
383-
zend_error_noreturn(E_ERROR,
384-
format,
385-
limit,
386-
#if ZEND_DEBUG
387-
filename,
388-
lineno,
389-
#endif
390-
size);
377+
va_start(args, format);
378+
zend_string *message = zend_vstrpprintf(0, format, args);
379+
zend_error_noreturn(E_ERROR, "%s", ZSTR_VAL(message));
380+
zend_string_release(message);
381+
va_end(args);
391382
} zend_catch {
392383
} zend_end_try();
393384
heap->overflow = 0;
@@ -1009,11 +1000,21 @@ static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_F
10091000
if (zend_mm_gc(heap)) {
10101001
goto get_chunk;
10111002
} else if (heap->overflow == 0) {
1012-
#if ZEND_DEBUG
1013-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, ZEND_MM_CHUNK_SIZE);
1014-
#else
1015-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, ZEND_MM_CHUNK_SIZE);
1016-
#endif
1003+
zend_mm_safe_error(
1004+
heap,
1005+
"Allowed memory size of %zu bytes exhausted by %zu bytes"
1006+
# if ZEND_DEBUG
1007+
" at %s:%d"
1008+
# endif
1009+
". Allocated %zu bytes and need to allocate %zu bytes to satisfy a request for %zu bytes",
1010+
1011+
heap->limit, (ZEND_MM_CHUNK_SIZE - (heap->limit - heap->real_size)),
1012+
# if ZEND_DEBUG
1013+
__zend_filename, __zend_lineno,
1014+
# endif
1015+
heap->real_size, ZEND_MM_CHUNK_SIZE, (ZEND_DEBUG ? size : ZEND_MM_PAGE_SIZE * pages_count)
1016+
);
1017+
10171018
return NULL;
10181019
}
10191020
}
@@ -1535,11 +1536,20 @@ static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *pt
15351536
if (zend_mm_gc(heap) && new_size - old_size <= heap->limit - heap->real_size) {
15361537
/* pass */
15371538
} else if (heap->overflow == 0) {
1538-
#if ZEND_DEBUG
1539-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, (new_size - old_size));
1540-
#else
1541-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, (new_size - old_size));
1542-
#endif
1539+
zend_mm_safe_error(
1540+
heap,
1541+
"Allowed memory size of %zu bytes exhausted by %zu bytes"
1542+
# if ZEND_DEBUG
1543+
" at %s:%d"
1544+
# endif
1545+
". Allocated %zu bytes and need to allocate %zu bytes to satisfy a reallocation from %zu to %zu bytes",
1546+
1547+
heap->limit, ((new_size - old_size) - (heap->limit - heap->real_size)),
1548+
# if ZEND_DEBUG
1549+
__zend_filename, __zend_lineno,
1550+
# endif
1551+
heap->real_size, (new_size - old_size), old_size, new_size
1552+
);
15431553
return NULL;
15441554
}
15451555
}
@@ -1827,11 +1837,20 @@ static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
18271837
if (zend_mm_gc(heap) && new_size <= heap->limit - heap->real_size) {
18281838
/* pass */
18291839
} else if (heap->overflow == 0) {
1830-
#if ZEND_DEBUG
1831-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", heap->limit, __zend_filename, __zend_lineno, new_size);
1832-
#else
1833-
zend_mm_safe_error(heap, "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", heap->limit, new_size);
1834-
#endif
1840+
zend_mm_safe_error(
1841+
heap,
1842+
"Allowed memory size of %zu bytes exhausted by %zu bytes"
1843+
# if ZEND_DEBUG
1844+
" at %s:%d"
1845+
# endif
1846+
". Allocated %zu bytes and need to allocate %zu bytes",
1847+
1848+
heap->limit, (new_size - (heap->limit - heap->real_size)),
1849+
# if ZEND_DEBUG
1850+
__zend_filename, __zend_lineno,
1851+
# endif
1852+
heap->real_size, new_size
1853+
);
18351854
return NULL;
18361855
}
18371856
}
@@ -2823,15 +2842,20 @@ static zend_always_inline zval *tracked_get_size_zv(zend_mm_heap *heap, void *pt
28232842

28242843
static zend_always_inline void tracked_check_limit(zend_mm_heap *heap, size_t add_size) {
28252844
if (add_size > heap->limit - heap->size && !heap->overflow) {
2826-
#if ZEND_DEBUG
2827-
zend_mm_safe_error(heap,
2828-
"Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)",
2829-
heap->limit, "file", 0, add_size);
2830-
#else
2831-
zend_mm_safe_error(heap,
2832-
"Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)",
2833-
heap->limit, add_size);
2834-
#endif
2845+
zend_mm_safe_error(
2846+
heap,
2847+
"Allowed memory size of %zu bytes exhausted by %zu bytes"
2848+
# if ZEND_DEBUG
2849+
" at %s:%d"
2850+
# endif
2851+
". Allocated %zu bytes and need to allocate %zu bytes",
2852+
2853+
heap->limit, (add_size - (heap->limit - heap->real_size)),
2854+
# if ZEND_DEBUG
2855+
"file", 0,
2856+
# endif
2857+
heap->real_size, add_size
2858+
);
28352859
}
28362860
}
28372861

0 commit comments

Comments
 (0)