Skip to content

Commit 8cc29af

Browse files
committed
Fixes
1 parent f94fb29 commit 8cc29af

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Zend/zend_alloc.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ static size_t _real_page_size = ZEND_MM_PAGE_SIZE;
182182
if (UNEXPECTED(((size_t) (_ptr)) & ((size_t)7))) { \
183183
zend_mm_panic("Wrong alignment"); \
184184
} \
185+
fprintf(stderr, "Poisoning %p - %p in %d\n", (_ptr), ((size_t)_ptr)+((size_t)_size), __LINE__); \
186+
fflush(stderr); \
185187
ASAN_POISON_MEMORY_REGION((_ptr), (_size));\
186188
} while (0);
187189
#define ZEND_MM_UNPOISON(_ptr, _size) do { \
188190
if (UNEXPECTED(((size_t) (_ptr)) & ((size_t)7))) { \
189191
zend_mm_panic("Wrong alignment"); \
190192
} \
193+
fprintf(stderr, "Unpoisoning %p - %p in %d\n", (_ptr), ((size_t)_ptr)+((size_t)_size), __LINE__); \
194+
fflush(stderr); \
191195
ASAN_UNPOISON_MEMORY_REGION((_ptr), (_size));\
192196
} while (0);
193197

@@ -928,6 +932,7 @@ static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_ch
928932
chunk->heap = heap;
929933
chunk->next = heap->main_chunk;
930934
ZEND_MM_UNPOISON_CHUNK_HDR(heap->main_chunk);
935+
ZEND_MM_UNPOISON_CHUNK_HDR(heap->main_chunk->prev);
931936
chunk->prev = heap->main_chunk->prev;
932937
chunk->prev->next = chunk;
933938
chunk->next->prev = chunk;
@@ -939,6 +944,7 @@ static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_ch
939944
/* mark first pages as allocated */
940945
chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
941946
chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
947+
ZEND_MM_POISON_CHUNK_HDR(heap->main_chunk->prev, heap);
942948
ZEND_MM_POISON_CHUNK_HDR(heap->main_chunk, heap);
943949
}
944950

@@ -1169,17 +1175,19 @@ static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_F
11691175
/* move chunk into the head of the linked-list */
11701176
chunk->prev->next = chunk->next;
11711177
chunk->next->prev = chunk->prev;
1172-
ZEND_MM_POISON_CHUNK_HDR(chunk->next, heap);
1173-
ZEND_MM_POISON_CHUNK_HDR(chunk->prev, heap);
11741178

11751179
ZEND_MM_UNPOISON_CHUNK_HDR(heap->main_chunk);
11761180
ZEND_MM_UNPOISON_CHUNK_HDR(heap->main_chunk->next);
11771181
chunk->next = heap->main_chunk->next;
11781182
chunk->prev = heap->main_chunk;
11791183
chunk->prev->next = chunk;
11801184
chunk->next->prev = chunk;
1181-
ZEND_MM_POISON_CHUNK_HDR(heap->main_chunk, heap);
11821185
ZEND_MM_POISON_CHUNK_HDR(heap->main_chunk->next, heap);
1186+
ZEND_MM_POISON_CHUNK_HDR(heap->main_chunk, heap);
1187+
1188+
ZEND_MM_UNPOISON_CHUNK_HDR(chunk);
1189+
ZEND_MM_POISON_CHUNK_HDR(chunk->next, heap);
1190+
ZEND_MM_POISON_CHUNK_HDR(chunk->prev, heap);
11831191
}
11841192
/* mark run as allocated */
11851193
chunk->free_pages -= pages_count;
@@ -1603,6 +1611,8 @@ static zend_always_inline void *zend_mm_alloc_heap(zend_mm_heap *heap, size_t si
16031611
dbg->lineno = __zend_lineno;
16041612
dbg->orig_lineno = __zend_orig_lineno;
16051613
#endif
1614+
ZEND_MM_UNPOISON(ptr, size);
1615+
return ptr;
16061616
} else if (EXPECTED(size <= ZEND_MM_MAX_LARGE_SIZE)) {
16071617
ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
16081618
#if ZEND_DEBUG
@@ -1613,14 +1623,16 @@ static zend_always_inline void *zend_mm_alloc_heap(zend_mm_heap *heap, size_t si
16131623
dbg->lineno = __zend_lineno;
16141624
dbg->orig_lineno = __zend_orig_lineno;
16151625
#endif
1626+
ZEND_MM_UNPOISON(ptr, size);
1627+
return ptr;
16161628
} else {
16171629
#if ZEND_DEBUG
16181630
size = real_size;
16191631
#endif
16201632
ptr = zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
1633+
ZEND_MM_UNPOISON(ptr, size);
1634+
return ptr;
16211635
}
1622-
ZEND_MM_UNPOISON(ptr, size);
1623-
return ptr;
16241636
}
16251637

16261638
static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
@@ -1807,6 +1819,7 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
18071819
} else {
18081820
ret = zend_mm_realloc_huge(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
18091821
}
1822+
ZEND_MM_UNPOISON(ret, size);
18101823
return ret;
18111824
} else {
18121825
zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE);
@@ -1911,7 +1924,6 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
19111924
chunk->free_pages += rest_pages_count;
19121925
zend_mm_bitset_reset_range(chunk->free_map, page_num + new_pages_count, rest_pages_count);
19131926
ZEND_MM_POISON(ZEND_MM_PAGE_ADDR(chunk, page_num + new_pages_count), rest_pages_count * ZEND_MM_PAGE_SIZE);
1914-
19151927
#if ZEND_DEBUG
19161928
dbg = zend_mm_get_debug_info(heap, ptr);
19171929
dbg->size = real_size;
@@ -1921,6 +1933,8 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
19211933
dbg->orig_lineno = __zend_orig_lineno;
19221934
#endif
19231935
ZEND_MM_POISON_CHUNK_HDR(chunk, heap);
1936+
ZEND_MM_POISON(ptr, old_size);
1937+
ZEND_MM_UNPOISON(ptr, size);
19241938
return ptr;
19251939
} else /* if (new_size > old_size) */ {
19261940
int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE);
@@ -1940,7 +1954,6 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
19401954
chunk->free_pages -= new_pages_count - old_pages_count;
19411955
zend_mm_bitset_set_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count);
19421956
chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count);
1943-
ZEND_MM_UNPOISON(ZEND_MM_PAGE_ADDR(chunk, page_num + old_pages_count), (new_pages_count - old_pages_count) * ZEND_MM_PAGE_SIZE);
19441957
#if ZEND_DEBUG
19451958
dbg = zend_mm_get_debug_info(heap, ptr);
19461959
dbg->size = real_size;
@@ -1950,6 +1963,7 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
19501963
dbg->orig_lineno = __zend_orig_lineno;
19511964
#endif
19521965
ZEND_MM_POISON_CHUNK_HDR(chunk, heap);
1966+
ZEND_MM_UNPOISON(ptr, size);
19531967
return ptr;
19541968
}
19551969
}
@@ -1963,6 +1977,7 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p
19631977

19641978
copy_size = MIN(old_size, copy_size);
19651979
ret = zend_mm_realloc_slow(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
1980+
ZEND_MM_UNPOISON(ret, size);
19661981
return ret;
19671982
}
19681983

0 commit comments

Comments
 (0)