|
70 | 70 | # include <wincrypt.h>
|
71 | 71 | # include <process.h>
|
72 | 72 | # include "win32/winutil.h"
|
73 | | -# define getpid _getpid |
74 | | -typedef int pid_t; |
75 | 73 | #endif
|
76 | 74 |
|
77 | 75 | #include <stdio.h>
|
@@ -317,7 +315,6 @@ struct _zend_mm_heap {
|
317 | 315 | } debug;
|
318 | 316 | };
|
319 | 317 | #endif
|
320 | | - pid_t pid; |
321 | 318 | zend_random_bytes_insecure_state rand_state;
|
322 | 319 | };
|
323 | 320 |
|
@@ -1310,15 +1307,20 @@ static zend_always_inline zend_mm_free_slot* zend_mm_encode_free_slot(const zend
|
1310 | 1307 | #endif
|
1311 | 1308 | }
|
1312 | 1309 |
|
1313 | | -static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot(zend_mm_heap*heap, zend_mm_free_slot *slot) |
| 1310 | +static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot_key(uintptr_tshadow_key, zend_mm_free_slot *slot) |
1314 | 1311 | {
|
1315 | 1312 | #ifdef WORDS_BIGENDIAN
|
1316 | | - return (zend_mm_free_slot*)((uintptr_t)slot ^ heap->shadow_key); |
| 1313 | + return (zend_mm_free_slot*)((uintptr_t)slot ^ shadow_key); |
1317 | 1314 | #else
|
1318 | | - return (zend_mm_free_slot*)(BSWAPPTR((uintptr_t)slot ^ heap->shadow_key)); |
| 1315 | + return (zend_mm_free_slot*)(BSWAPPTR((uintptr_t)slot ^ shadow_key)); |
1319 | 1316 | #endif
|
1320 | 1317 | }
|
1321 | 1318 |
|
| 1319 | +static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot(zend_mm_heap *heap, zend_mm_free_slot *slot) |
| 1320 | +{ |
| 1321 | + return zend_mm_decode_free_slot_key(heap->shadow_key, slot); |
| 1322 | +} |
| 1323 | + |
1322 | 1324 | 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)
|
1323 | 1325 | {
|
1324 | 1326 | ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_USEABLE_BIN_SIZE);
|
@@ -2027,6 +2029,30 @@ static void zend_mm_init_key(zend_mm_heap *heap)
|
2027 | 2029 | zend_mm_refresh_key(heap);
|
2028 | 2030 | }
|
2029 | 2031 |
|
| 2032 | +static void zend_mm_refresh_key_child(zend_mm_heap *heap) |
| 2033 | +{ |
| 2034 | + uintptr_t old_key = heap->shadow_key; |
| 2035 | + |
| 2036 | + zend_mm_init_key(heap); |
| 2037 | + |
| 2038 | + /* Update shadow pointers with new key */ |
| 2039 | + for (int i = 0; i < ZEND_MM_BINS; i++) { |
| 2040 | + zend_mm_free_slot *slot = heap->free_slot[i]; |
| 2041 | + if (!slot) { |
| 2042 | + continue; |
| 2043 | + } |
| 2044 | + zend_mm_free_slot *next; |
| 2045 | + while ((next = slot->next_free_slot)) { |
| 2046 | + zend_mm_free_slot *shadow = ZEND_MM_FREE_SLOT_PTR_SHADOW(slot, i); |
| 2047 | + if (UNEXPECTED(next != zend_mm_decode_free_slot_key(old_key, shadow))) { |
| 2048 | + zend_mm_panic("zend_mm_heap corrupted"); |
| 2049 | + } |
| 2050 | + zend_mm_set_next_free_slot(heap, i, slot, next); |
| 2051 | + slot = next; |
| 2052 | + } |
| 2053 | + } |
| 2054 | +} |
| 2055 | + |
2030 | 2056 | static zend_mm_heap *zend_mm_init(void)
|
2031 | 2057 | {
|
2032 | 2058 | zend_mm_chunk *chunk = (zend_mm_chunk*)zend_mm_chunk_alloc_int(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
|
@@ -2075,7 +2101,6 @@ static zend_mm_heap *zend_mm_init(void)
|
2075 | 2101 | heap->storage = NULL;
|
2076 | 2102 | #endif
|
2077 | 2103 | heap->huge_list = NULL;
|
2078 | | - heap->pid = getpid(); |
2079 | 2104 | return heap;
|
2080 | 2105 | }
|
2081 | 2106 |
|
@@ -2535,13 +2560,7 @@ ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent)
|
2535 | 2560 | p->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1;
|
2536 | 2561 | p->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE);
|
2537 | 2562 |
|
2538 | | - pid_t pid = getpid(); |
2539 | | - if (heap->pid != pid) { |
2540 | | - zend_mm_init_key(heap); |
2541 | | - heap->pid = pid; |
2542 | | - } else { |
2543 | | - zend_mm_refresh_key(heap); |
2544 | | - } |
| 2563 | + zend_mm_refresh_key(heap); |
2545 | 2564 | }
|
2546 | 2565 | }
|
2547 | 2566 |
|
@@ -2949,6 +2968,11 @@ ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown)
|
2949 | 2968 | zend_mm_shutdown(AG(mm_heap), full_shutdown, silent);
|
2950 | 2969 | }
|
2951 | 2970 |
|
| 2971 | +ZEND_API void refresh_memory_manager(void) |
| 2972 | +{ |
| 2973 | + zend_mm_refresh_key_child(AG(mm_heap)); |
| 2974 | +} |
| 2975 | + |
2952 | 2976 | static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void)
|
2953 | 2977 | {
|
2954 | 2978 | fprintf(stderr, "Out of memory\n");
|
@@ -3506,7 +3530,6 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
|
3506 | 3530 | memcpy(storage->data, data, data_size);
|
3507 | 3531 | }
|
3508 | 3532 | heap->storage = storage;
|
3509 | | - heap->pid = getpid(); |
3510 | 3533 | return heap;
|
3511 | 3534 | #else
|
3512 | 3535 | return NULL;
|
|
0 commit comments