Skip to content

Commit 5c29ad5

Browse files
committed
Do not change zend_alloc_sizes
1 parent 5f4cee3 commit 5c29ad5

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

Zend/zend_alloc.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -367,29 +367,19 @@ struct _zend_mm_huge_list {
367367
#define ZEND_MM_PAGE_ADDR(chunk, page_num) \
368368
((void*)(((zend_mm_page*)(chunk)) + (page_num)))
369369

370-
#define _BIN_DATA_SIZE(num, size, elements, pages, x, y) \
371-
/* Need two words for free slot pointer and shadow */ \
372-
(ZEND_MM_HEAP_PROTECTION ? MAX(size, sizeof(zend_mm_free_slot*)*2) : size)
373-
#define _BIN_DATA_SIZE_C(num, size, elements, pages, x, y) \
374-
_BIN_DATA_SIZE(num, size, elements, pages, x, y),
370+
#define _BIN_DATA_SIZE(num, size, elements, pages, x, y) size,
375371
static const uint32_t bin_data_size[] = {
376-
ZEND_MM_BINS_INFO(_BIN_DATA_SIZE_C, x, y)
372+
ZEND_MM_BINS_INFO(_BIN_DATA_SIZE, x, y)
377373
};
378374

379-
#define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) \
380-
/* Adjusting size requires adjusting elements */ \
381-
(elements / (_BIN_DATA_SIZE(num, size, elements, pages, x, y) / size))
382-
#define _BIN_DATA_ELEMENTS_C(num, size, elements, pages, x, y) \
383-
_BIN_DATA_ELEMENTS(num, size, elements, pages, x, y),
375+
#define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) elements,
384376
static const uint32_t bin_elements[] = {
385-
ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS_C, x, y)
377+
ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS, x, y)
386378
};
387379

388-
#define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages
389-
#define _BIN_DATA_PAGES_C(num, size, elements, pages, x, y) \
390-
_BIN_DATA_PAGES(num, size, elements, pages, x, y),
380+
#define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages,
391381
static const uint32_t bin_pages[] = {
392-
ZEND_MM_BINS_INFO(_BIN_DATA_PAGES_C, x, y)
382+
ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y)
393383
};
394384

395385
#if ZEND_DEBUG
@@ -1343,6 +1333,8 @@ static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot(zend_mm_he
13431333

13441334
static zend_always_inline void zend_mm_set_next_free_slot(zend_mm_heap *heap, uint32_t bin_num, zend_mm_free_slot *slot, zend_mm_free_slot *next)
13451335
{
1336+
ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_SMALL_SIZE);
1337+
13461338
slot->next_free_slot = next;
13471339
ZEND_MM_FREE_SLOT_PTR_SHADOW(slot, bin_num) = zend_mm_encode_free_slot(heap, next);
13481340
}
@@ -1424,6 +1416,8 @@ static zend_never_inline void *zend_mm_alloc_small_slow(zend_mm_heap *heap, uint
14241416

14251417
static zend_always_inline void *zend_mm_alloc_small(zend_mm_heap *heap, int bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
14261418
{
1419+
ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_SMALL_SIZE);
1420+
14271421
#if ZEND_MM_STAT
14281422
do {
14291423
size_t size = heap->size + bin_data_size[bin_num];
@@ -1444,6 +1438,8 @@ static zend_always_inline void *zend_mm_alloc_small(zend_mm_heap *heap, int bin_
14441438

14451439
static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, int bin_num)
14461440
{
1441+
ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_SMALL_SIZE);
1442+
14471443
zend_mm_free_slot *p;
14481444

14491445
#if ZEND_MM_STAT
@@ -1493,6 +1489,11 @@ static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_hea
14931489
static zend_always_inline void *zend_mm_alloc_heap(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
14941490
{
14951491
void *ptr;
1492+
#if ZEND_MM_HEAP_PROTECTION
1493+
if (size < ZEND_MM_MIN_SMALL_SIZE) {
1494+
size = ZEND_MM_MIN_SMALL_SIZE;
1495+
}
1496+
#endif /* ZEND_MM_HEAP_PROTECTION */
14961497
#if ZEND_DEBUG
14971498
size_t real_size = size;
14981499
zend_mm_debug_info *dbg;
@@ -1714,6 +1715,11 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
17141715
zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
17151716
int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE);
17161717
zend_mm_page_info info = chunk->map[page_num];
1718+
#if ZEND_MM_HEAP_PROTECTION
1719+
if (size < ZEND_MM_MIN_SMALL_SIZE) {
1720+
size = ZEND_MM_MIN_SMALL_SIZE;
1721+
}
1722+
#endif /* ZEND_MM_HEAP_PROTECTION */
17171723
#if ZEND_DEBUG
17181724
size_t real_size = size;
17191725

@@ -2677,6 +2683,7 @@ ZEND_API bool is_zend_ptr(const void *ptr)
26772683

26782684
# define _ZEND_BIN_ALLOCATOR(_num, _size, _elements, _pages, x, y) \
26792685
ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) { \
2686+
ZEND_ASSERT(_size >= ZEND_MM_MIN_SMALL_SIZE); \
26802687
ZEND_MM_CUSTOM_ALLOCATOR(_size); \
26812688
return zend_mm_alloc_small(AG(mm_heap), _num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \
26822689
}
@@ -2698,6 +2705,7 @@ ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size)
26982705
#if ZEND_DEBUG
26992706
# define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
27002707
ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
2708+
ZEND_ASSERT(_size >= ZEND_MM_MIN_SMALL_SIZE); \
27012709
ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
27022710
{ \
27032711
size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); \
@@ -2712,6 +2720,7 @@ ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size)
27122720
#else
27132721
# define _ZEND_BIN_FREE(_num, _size, _elements, _pages, x, y) \
27142722
ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \
2723+
ZEND_ASSERT(_size >= ZEND_MM_MIN_SMALL_SIZE); \
27152724
ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \
27162725
{ \
27172726
zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \

Zend/zend_alloc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_large(size_t size) Z
9090
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc_huge(size_t size) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
9191

9292
# define _ZEND_BIN_ALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, size, y) \
93-
((size <= _size) ? _emalloc_ ## _size() :
93+
((size <= _size && _size >= ZEND_MM_MIN_SMALL_SIZE) ? _emalloc_ ## _size() :
9494
# define _ZEND_BIN_ALLOCATOR_SELECTOR_END(_num, _size, _elements, _pages, size, y) \
9595
)
9696

@@ -115,7 +115,7 @@ ZEND_API void ZEND_FASTCALL _efree_large(void *, size_t size);
115115
ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
116116

117117
# define _ZEND_BIN_DEALLOCATOR_SELECTOR_START(_num, _size, _elements, _pages, ptr, size) \
118-
if (size <= _size) { _efree_ ## _size(ptr); } else
118+
if (size <= _size && _size >= ZEND_MM_MIN_SMALL_SIZE) { _efree_ ## _size(ptr); } else
119119

120120
# define ZEND_DEALLOCATOR(ptr, size) \
121121
ZEND_MM_BINS_INFO(_ZEND_BIN_DEALLOCATOR_SELECTOR_START, ptr, size) \

Zend/zend_alloc_sizes.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@
1919
#ifndef ZEND_ALLOC_SIZES_H
2020
#define ZEND_ALLOC_SIZES_H
2121

22+
#ifndef ZEND_MM_HEAP_PROTECTION
23+
# define ZEND_MM_HEAP_PROTECTION 1 /* protect heap against corruptions */
24+
#endif
25+
2226
#define ZEND_MM_CHUNK_SIZE ((size_t) (2 * 1024 * 1024)) /* 2 MB */
2327
#define ZEND_MM_PAGE_SIZE (4 * 1024) /* 4 KB */
2428
#define ZEND_MM_PAGES (ZEND_MM_CHUNK_SIZE / ZEND_MM_PAGE_SIZE) /* 512 */
2529
#define ZEND_MM_FIRST_PAGE (1)
2630

27-
#define ZEND_MM_MIN_SMALL_SIZE 8
31+
#if ZEND_MM_HEAP_PROTECTION
32+
# define ZEND_MM_MIN_SMALL_SIZE (sizeof(void*) * 2)
33+
#else
34+
# define ZEND_MM_MIN_SMALL_SIZE 8
35+
#endif
2836
#define ZEND_MM_MAX_SMALL_SIZE 3072
2937
#define ZEND_MM_MAX_LARGE_SIZE (ZEND_MM_CHUNK_SIZE - (ZEND_MM_PAGE_SIZE * ZEND_MM_FIRST_PAGE))
3038

0 commit comments

Comments
 (0)