/src/php-src/Zend/zend_alloc.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Andi Gutmans <andi@php.net> | |
15 | | | Zeev Suraski <zeev@php.net> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | /* |
21 | | * zend_alloc is designed to be a modern CPU cache friendly memory manager |
22 | | * for PHP. Most ideas are taken from jemalloc and tcmalloc implementations. |
23 | | * |
24 | | * All allocations are split into 3 categories: |
25 | | * |
26 | | * Huge - the size is greater than CHUNK size (~2M by default), allocation is |
27 | | * performed using mmap(). The result is aligned on 2M boundary. |
28 | | * |
29 | | * Large - a number of 4096K pages inside a CHUNK. Large blocks |
30 | | * are always aligned on page boundary. |
31 | | * |
32 | | * Small - less than 3/4 of page size. Small sizes are rounded up to nearest |
33 | | * greater predefined small size (there are 30 predefined sizes: |
34 | | * 8, 16, 24, 32, ... 3072). Small blocks are allocated from |
35 | | * RUNs. Each RUN is allocated as a single or few following pages. |
36 | | * Allocation inside RUNs implemented using linked list of free |
37 | | * elements. The result is aligned to 8 bytes. |
38 | | * |
39 | | * zend_alloc allocates memory from OS by CHUNKs, these CHUNKs and huge memory |
40 | | * blocks are always aligned to CHUNK boundary. So it's very easy to determine |
41 | | * the CHUNK owning the certain pointer. Regular CHUNKs reserve a single |
42 | | * page at start for special purpose. It contains bitset of free pages, |
43 | | * few bitset for available runs of predefined small sizes, map of pages that |
44 | | * keeps information about usage of each page in this CHUNK, etc. |
45 | | * |
46 | | * zend_alloc provides familiar emalloc/efree/erealloc API, but in addition it |
47 | | * provides specialized and optimized routines to allocate blocks of predefined |
48 | | * sizes (e.g. emalloc_2(), emallc_4(), ..., emalloc_large(), etc) |
49 | | * The library uses C preprocessor tricks that substitute calls to emalloc() |
50 | | * with more specialized routines when the requested size is known. |
51 | | */ |
52 | | |
53 | | #include "zend.h" |
54 | | #include "zend_alloc.h" |
55 | | #include "zend_API.h" |
56 | | #include "zend_globals.h" |
57 | | #include "zend_hrtime.h" |
58 | | #include "zend_operators.h" |
59 | | #include "zend_multiply.h" |
60 | | #include "zend_bitset.h" |
61 | | #include "zend_mmap.h" |
62 | | #include "zend_portability.h" |
63 | | #include "zend_string.h" |
64 | | #include "zend_exceptions.h" |
65 | | #include <signal.h> |
66 | | |
67 | | #ifdef HAVE_UNISTD_H |
68 | | # include <unistd.h> |
69 | | #endif |
70 | | |
71 | | #ifdef ZEND_WIN32 |
72 | | # include <wincrypt.h> |
73 | | # include <process.h> |
74 | | # include "win32/winutil.h" |
75 | | # define getpid _getpid |
76 | | typedef int pid_t; |
77 | | #endif |
78 | | |
79 | | #include <stdio.h> |
80 | | #include <stdlib.h> |
81 | | #include <string.h> |
82 | | |
83 | | #include <sys/types.h> |
84 | | #include <sys/stat.h> |
85 | | #include <limits.h> |
86 | | #include <fcntl.h> |
87 | | #include <errno.h> |
88 | | #ifdef __SANITIZE_ADDRESS__ |
89 | | # include <sanitizer/asan_interface.h> |
90 | | #endif |
91 | | |
92 | | #ifndef _WIN32 |
93 | | # include <sys/mman.h> |
94 | | # ifndef MAP_ANON |
95 | | # ifdef MAP_ANONYMOUS |
96 | | # define MAP_ANON MAP_ANONYMOUS |
97 | | # endif |
98 | | # endif |
99 | | # ifndef MAP_FAILED |
100 | | # define MAP_FAILED ((void*)-1) |
101 | | # endif |
102 | | # ifndef MAP_POPULATE |
103 | | # define MAP_POPULATE 0 |
104 | | # endif |
105 | | # if defined(_SC_PAGESIZE) || (_SC_PAGE_SIZE) |
106 | 16 | # define REAL_PAGE_SIZE _real_page_size |
107 | | static size_t _real_page_size = ZEND_MM_PAGE_SIZE; |
108 | | # endif |
109 | | # ifdef MAP_ALIGNED_SUPER |
110 | | # define MAP_HUGETLB MAP_ALIGNED_SUPER |
111 | | # endif |
112 | | #endif |
113 | | |
114 | | #ifndef REAL_PAGE_SIZE |
115 | | # define REAL_PAGE_SIZE ZEND_MM_PAGE_SIZE |
116 | | #endif |
117 | | |
118 | | /* NetBSD has an mremap() function with a signature that is incompatible with Linux (WTF?), |
119 | | * so pretend it doesn't exist. */ |
120 | | #ifndef __linux__ |
121 | | # undef HAVE_MREMAP |
122 | | #endif |
123 | | |
124 | | #ifndef __APPLE__ |
125 | 0 | # define ZEND_MM_FD -1 |
126 | | #else |
127 | | # include <mach/vm_statistics.h> |
128 | | /* Mac allows to track anonymous page via vmmap per TAG id. |
129 | | * user land applications are allowed to take from 240 to 255. |
130 | | */ |
131 | | # define ZEND_MM_FD VM_MAKE_TAG(250U) |
132 | | #endif |
133 | | |
134 | | #ifndef ZEND_MM_STAT |
135 | | # define ZEND_MM_STAT 1 /* track current and peak memory usage */ |
136 | | #endif |
137 | | #ifndef ZEND_MM_LIMIT |
138 | | # define ZEND_MM_LIMIT 1 /* support for user-defined memory limit */ |
139 | | #endif |
140 | | #ifndef ZEND_MM_CUSTOM |
141 | | # define ZEND_MM_CUSTOM 1 /* support for custom memory allocator */ |
142 | | /* USE_ZEND_ALLOC=0 may switch to system malloc() */ |
143 | | #endif |
144 | | #ifndef ZEND_MM_STORAGE |
145 | | # define ZEND_MM_STORAGE 1 /* support for custom memory storage */ |
146 | | #endif |
147 | | #ifndef ZEND_MM_ERROR |
148 | | # define ZEND_MM_ERROR 1 /* report system errors */ |
149 | | #endif |
150 | | #ifndef ZEND_MM_HEAP_PROTECTION |
151 | | # define ZEND_MM_HEAP_PROTECTION 1 /* protect heap against corruptions */ |
152 | | #endif |
153 | | |
154 | | #if ZEND_MM_HEAP_PROTECTION |
155 | | /* Define ZEND_MM_MIN_USEABLE_BIN_SIZE to the size of two pointers */ |
156 | | # if UINTPTR_MAX == UINT64_MAX |
157 | 0 | # define ZEND_MM_MIN_USEABLE_BIN_SIZE 16 |
158 | | # elif UINTPTR_MAX == UINT32_MAX |
159 | | # define ZEND_MM_MIN_USEABLE_BIN_SIZE 8 |
160 | | # else |
161 | | # error |
162 | | # endif |
163 | | # if ZEND_MM_MIN_USEABLE_BIN_SIZE < ZEND_MM_MIN_SMALL_SIZE |
164 | | # error |
165 | | # endif |
166 | | #else /* ZEND_MM_HEAP_PROTECTION */ |
167 | | # define ZEND_MM_MIN_USEABLE_BIN_SIZE ZEND_MM_MIN_SMALL_SIZE |
168 | | #endif /* ZEND_MM_HEAP_PROTECTION */ |
169 | | |
170 | | #ifndef ZEND_MM_CHECK |
171 | 0 | # define ZEND_MM_CHECK(condition, message) do { \ |
172 | 0 | if (UNEXPECTED(!(condition))) { \ |
173 | 0 | zend_mm_panic(message); \ |
174 | 0 | } \ |
175 | 0 | } while (0) |
176 | | #endif |
177 | | |
178 | | typedef uint32_t zend_mm_page_info; /* 4-byte integer */ |
179 | | typedef zend_ulong zend_mm_bitset; /* 4-byte or 8-byte integer */ |
180 | | |
181 | | #define ZEND_MM_ALIGNED_OFFSET(size, alignment) \ |
182 | 0 | (((size_t)(size)) & ((alignment) - 1)) |
183 | | #define ZEND_MM_ALIGNED_BASE(size, alignment) \ |
184 | 0 | (((size_t)(size)) & ~((alignment) - 1)) |
185 | | #define ZEND_MM_SIZE_TO_NUM(size, alignment) \ |
186 | 0 | (((size_t)(size) + ((alignment) - 1)) / (alignment)) |
187 | | |
188 | 0 | #define ZEND_MM_BITSET_LEN (sizeof(zend_mm_bitset) * 8) /* 32 or 64 */ |
189 | | #define ZEND_MM_PAGE_MAP_LEN (ZEND_MM_PAGES / ZEND_MM_BITSET_LEN) /* 16 or 8 */ |
190 | | |
191 | | typedef zend_mm_bitset zend_mm_page_map[ZEND_MM_PAGE_MAP_LEN]; /* 64B */ |
192 | | |
193 | | #define ZEND_MM_IS_FRUN 0x00000000 |
194 | 0 | #define ZEND_MM_IS_LRUN 0x40000000 |
195 | 0 | #define ZEND_MM_IS_SRUN 0x80000000 |
196 | | |
197 | 0 | #define ZEND_MM_LRUN_PAGES_MASK 0x000003ff |
198 | 0 | #define ZEND_MM_LRUN_PAGES_OFFSET 0 |
199 | | |
200 | 0 | #define ZEND_MM_SRUN_BIN_NUM_MASK 0x0000001f |
201 | 0 | #define ZEND_MM_SRUN_BIN_NUM_OFFSET 0 |
202 | | |
203 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER_MASK 0x03ff0000 |
204 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER_OFFSET 16 |
205 | | |
206 | 0 | #define ZEND_MM_NRUN_OFFSET_MASK 0x01ff0000 |
207 | 0 | #define ZEND_MM_NRUN_OFFSET_OFFSET 16 |
208 | | |
209 | 0 | #define ZEND_MM_LRUN_PAGES(info) (((info) & ZEND_MM_LRUN_PAGES_MASK) >> ZEND_MM_LRUN_PAGES_OFFSET) |
210 | 0 | #define ZEND_MM_SRUN_BIN_NUM(info) (((info) & ZEND_MM_SRUN_BIN_NUM_MASK) >> ZEND_MM_SRUN_BIN_NUM_OFFSET) |
211 | 0 | #define ZEND_MM_SRUN_FREE_COUNTER(info) (((info) & ZEND_MM_SRUN_FREE_COUNTER_MASK) >> ZEND_MM_SRUN_FREE_COUNTER_OFFSET) |
212 | 0 | #define ZEND_MM_NRUN_OFFSET(info) (((info) & ZEND_MM_NRUN_OFFSET_MASK) >> ZEND_MM_NRUN_OFFSET_OFFSET) |
213 | | |
214 | | #define ZEND_MM_FRUN() ZEND_MM_IS_FRUN |
215 | 0 | #define ZEND_MM_LRUN(count) (ZEND_MM_IS_LRUN | ((count) << ZEND_MM_LRUN_PAGES_OFFSET)) |
216 | 0 | #define ZEND_MM_SRUN(bin_num) (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET)) |
217 | 0 | #define ZEND_MM_SRUN_EX(bin_num, count) (ZEND_MM_IS_SRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET) | ((count) << ZEND_MM_SRUN_FREE_COUNTER_OFFSET)) |
218 | 0 | #define ZEND_MM_NRUN(bin_num, offset) (ZEND_MM_IS_SRUN | ZEND_MM_IS_LRUN | ((bin_num) << ZEND_MM_SRUN_BIN_NUM_OFFSET) | ((offset) << ZEND_MM_NRUN_OFFSET_OFFSET)) |
219 | | |
220 | 0 | #define ZEND_MM_BINS 30 |
221 | | |
222 | | #if ZEND_MM_LIMIT |
223 | 627k | # define ZEND_MM_LIMIT_MAX ((size_t)Z_L(-1) >> 1) |
224 | | #endif |
225 | | |
226 | | #if UINTPTR_MAX == UINT64_MAX |
227 | 0 | # define BSWAPPTR(u) ZEND_BYTES_SWAP64(u) |
228 | | #else |
229 | | # define BSWAPPTR(u) ZEND_BYTES_SWAP32(u) |
230 | | #endif |
231 | | |
232 | | typedef struct _zend_mm_page zend_mm_page; |
233 | | typedef struct _zend_mm_bin zend_mm_bin; |
234 | | typedef struct _zend_mm_free_slot zend_mm_free_slot; |
235 | | typedef struct _zend_mm_chunk zend_mm_chunk; |
236 | | typedef struct _zend_mm_huge_list zend_mm_huge_list; |
237 | | |
238 | | static bool zend_mm_use_huge_pages = false; |
239 | | static bool zend_mm_max_allocs_reached = false; |
240 | | |
241 | | /* |
242 | | * Memory is retrieved from OS by chunks of fixed size 2MB. |
243 | | * Inside chunk it's managed by pages of fixed size 4096B. |
244 | | * So each chunk consists from 512 pages. |
245 | | * The first page of each chunk is reserved for chunk header. |
246 | | * It contains service information about all pages. |
247 | | * |
248 | | * free_pages - current number of free pages in this chunk |
249 | | * |
250 | | * free_tail - number of continuous free pages at the end of chunk |
251 | | * |
252 | | * free_map - bitset (a bit for each page). The bit is set if the corresponding |
253 | | * page is allocated. Allocator for "large sizes" may easily find a |
254 | | * free page (or a continuous number of pages) searching for zero |
255 | | * bits. |
256 | | * |
257 | | * map - contains service information for each page. (32-bits for each |
258 | | * page). |
259 | | * usage: |
260 | | * (2 bits) |
261 | | * FRUN - free page, |
262 | | * LRUN - first page of "large" allocation |
263 | | * SRUN - first page of a bin used for "small" allocation |
264 | | * |
265 | | * lrun_pages: |
266 | | * (10 bits) number of allocated pages |
267 | | * |
268 | | * srun_bin_num: |
269 | | * (5 bits) bin number (e.g. 0 for sizes 0-2, 1 for 3-4, |
270 | | * 2 for 5-8, 3 for 9-16 etc) see zend_alloc_sizes.h |
271 | | */ |
272 | | |
273 | | struct _zend_mm_heap { |
274 | | #if ZEND_MM_CUSTOM |
275 | | int use_custom_heap; |
276 | | #endif |
277 | | #if ZEND_MM_STORAGE |
278 | | zend_mm_storage *storage; |
279 | | #endif |
280 | | #if ZEND_MM_STAT |
281 | | size_t size; /* current memory usage */ |
282 | | size_t peak; /* peak memory usage */ |
283 | | #endif |
284 | | uintptr_t shadow_key; /* free slot shadow ptr xor key */ |
285 | | zend_mm_free_slot *free_slot[ZEND_MM_BINS]; /* free lists for small sizes */ |
286 | | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
287 | | size_t real_size; /* current size of allocated pages */ |
288 | | #endif |
289 | | #if ZEND_MM_STAT |
290 | | size_t real_peak; /* peak size of allocated pages */ |
291 | | #endif |
292 | | #if ZEND_MM_LIMIT |
293 | | size_t limit; /* memory limit */ |
294 | | size_t hard_limit; |
295 | | zend_long hard_limit_percent; |
296 | | int overflow; /* memory overflow flag */ |
297 | | #endif |
298 | | bool fallible; |
299 | | |
300 | | zend_mm_huge_list *huge_list; /* list of huge allocated blocks */ |
301 | | |
302 | | zend_mm_chunk *main_chunk; |
303 | | zend_mm_chunk *cached_chunks; /* list of unused chunks */ |
304 | | int chunks_count; /* number of allocated chunks */ |
305 | | int peak_chunks_count; /* peak number of allocated chunks for current request */ |
306 | | int cached_chunks_count; /* number of cached chunks */ |
307 | | double avg_chunks_count; /* average number of chunks allocated per request */ |
308 | | int last_chunks_delete_boundary; /* number of chunks after last deletion */ |
309 | | int last_chunks_delete_count; /* number of deletion over the last boundary */ |
310 | | #if ZEND_MM_CUSTOM |
311 | | struct { |
312 | | void *(*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
313 | | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
314 | | void *(*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
315 | | size_t (*_gc)(void); |
316 | | void (*_shutdown)(bool full, bool silent); |
317 | | } custom_heap; |
318 | | union { |
319 | | HashTable *tracked_allocs; |
320 | | struct { |
321 | | bool poison_alloc; |
322 | | uint8_t poison_alloc_value; |
323 | | bool poison_free; |
324 | | uint8_t poison_free_value; |
325 | | uint8_t padding; |
326 | | bool check_freelists_on_shutdown; |
327 | | } debug; |
328 | | }; |
329 | | uint64_t max_allocs; |
330 | | #endif |
331 | | #if ZEND_DEBUG |
332 | | pid_t pid; |
333 | | #endif |
334 | | zend_random_bytes_insecure_state rand_state; |
335 | | }; |
336 | | |
337 | | struct _zend_mm_chunk { |
338 | | zend_mm_heap *heap; |
339 | | zend_mm_chunk *next; |
340 | | zend_mm_chunk *prev; |
341 | | zend_mm_chunk *next_shadow; /* shadow of "next" while the chunk is cached */ |
342 | | uint32_t free_pages; /* number of free pages */ |
343 | | uint32_t free_tail; /* number of free pages at the end of chunk */ |
344 | | uint32_t num; |
345 | | char reserve[64 - (sizeof(void*) * 4 + sizeof(uint32_t) * 3)]; |
346 | | zend_mm_heap heap_slot; /* used only in main chunk */ |
347 | | zend_mm_page_map free_map; /* 512 bits or 64 bytes */ |
348 | | zend_mm_page_info map[ZEND_MM_PAGES]; /* 2 KB = 512 * 4 */ |
349 | | }; |
350 | | |
351 | | struct _zend_mm_page { |
352 | | char bytes[ZEND_MM_PAGE_SIZE]; |
353 | | }; |
354 | | |
355 | | /* |
356 | | * bin - is one or few continuous pages (up to 8) used for allocation of |
357 | | * a particular "small size". |
358 | | */ |
359 | | struct _zend_mm_bin { |
360 | | char bytes[ZEND_MM_PAGE_SIZE * 8]; |
361 | | }; |
362 | | |
363 | | struct _zend_mm_free_slot { |
364 | | zend_mm_free_slot *next_free_slot; |
365 | | }; |
366 | | |
367 | | struct _zend_mm_huge_list { |
368 | | void *ptr; |
369 | | size_t size; |
370 | | zend_mm_huge_list *next; |
371 | | zend_mm_huge_list *prev; |
372 | | #if ZEND_DEBUG |
373 | | zend_mm_debug_info dbg; |
374 | | #endif |
375 | | }; |
376 | | |
377 | | #define ZEND_MM_PAGE_ADDR(chunk, page_num) \ |
378 | 0 | ((void*)(((zend_mm_page*)(chunk)) + (page_num))) |
379 | | |
380 | | #define _BIN_DATA_SIZE(num, size, elements, pages, x, y) size, |
381 | | static const uint32_t bin_data_size[] = { |
382 | | ZEND_MM_BINS_INFO(_BIN_DATA_SIZE, x, y) |
383 | | }; |
384 | | |
385 | | #define _BIN_DATA_ELEMENTS(num, size, elements, pages, x, y) elements, |
386 | | static const uint32_t bin_elements[] = { |
387 | | ZEND_MM_BINS_INFO(_BIN_DATA_ELEMENTS, x, y) |
388 | | }; |
389 | | |
390 | | #define _BIN_DATA_PAGES(num, size, elements, pages, x, y) pages, |
391 | | static const uint32_t bin_pages[] = { |
392 | | ZEND_MM_BINS_INFO(_BIN_DATA_PAGES, x, y) |
393 | | }; |
394 | | |
395 | | ZEND_NORETURN static ZEND_COLD void zend_mm_panic(const char *message) |
396 | 0 | { |
397 | 0 | fprintf(stderr, "%s\n", message); |
398 | | /* See http://support.microsoft.com/kb/190351 */ |
399 | | #ifdef ZEND_WIN32 |
400 | | fflush(stderr); |
401 | | #endif |
402 | 0 | #if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID) |
403 | 0 | kill(getpid(), SIGSEGV); |
404 | 0 | #endif |
405 | 0 | abort(); |
406 | 0 | } |
407 | | |
408 | | ZEND_NORETURN static ZEND_COLD void zend_out_of_memory(void); |
409 | | |
410 | | static ZEND_COLD void zend_mm_delayed_verror(const char *format, va_list args) |
411 | 835 | { |
412 | 835 | if (ZEND_HAS_OOM()) { |
413 | 0 | return; |
414 | 0 | } |
415 | | |
416 | 835 | zend_string *script_filename = NULL; |
417 | 835 | uint32_t script_lineno = 0; |
418 | | |
419 | 835 | if (zend_is_executing()) { |
420 | 764 | script_filename = zend_get_executed_filename_ex(); |
421 | 764 | script_lineno = zend_get_executed_lineno(); |
422 | 764 | } else if (zend_is_compiling()) { |
423 | 36 | script_filename = zend_get_compiled_filename(); |
424 | 36 | script_lineno = zend_get_compiled_lineno(); |
425 | 36 | } |
426 | | |
427 | 835 | if (script_filename == NULL) { |
428 | 35 | script_filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED); |
429 | 35 | script_lineno = 0; |
430 | 35 | } |
431 | | |
432 | 835 | char buf[1024]; |
433 | 835 | vsnprintf(buf, sizeof(buf), format, args); |
434 | | |
435 | 835 | EG(delayed_oom).message = zend_string_init(buf, strlen(buf), /* persistent */ true); |
436 | 835 | GC_MAKE_PERSISTENT_LOCAL(EG(delayed_oom).message); |
437 | 835 | zend_string_addref(script_filename); |
438 | 835 | EG(delayed_oom).filename = script_filename; |
439 | 835 | EG(delayed_oom).lineno = script_lineno; |
440 | 835 | EG(delayed_oom).state = ZEND_OOM_STATE_PENDING; |
441 | | |
442 | 835 | atomic_store(&EG(vm_interrupt), true); |
443 | 835 | } |
444 | | |
445 | | ZEND_ATTRIBUTE_FORMAT(printf, 1, 2) |
446 | | static ZEND_COLD void zend_mm_delayed_error(const char *format, ...) |
447 | 835 | { |
448 | 835 | va_list args; |
449 | 835 | va_start(args, format); |
450 | 835 | zend_mm_delayed_verror(format, args); |
451 | 835 | va_end(args); |
452 | 835 | } |
453 | | |
454 | | ZEND_ATTRIBUTE_FORMAT(printf, 2, 3) |
455 | | ZEND_API ZEND_COLD void zend_mm_delayed_error_or_panic(bool delay, const char *format, ...) |
456 | 0 | { |
457 | 0 | va_list args; |
458 | 0 | va_start(args, format); |
459 | |
|
460 | 0 | if (delay) { |
461 | 0 | zend_mm_delayed_verror(format, args); |
462 | 0 | } else { |
463 | 0 | char buf[1024]; |
464 | 0 | vsnprintf(buf, sizeof(buf), format, args); |
465 | 0 | zend_mm_panic(buf); |
466 | 0 | } |
467 | |
|
468 | 0 | va_end(args); |
469 | 0 | } |
470 | | |
471 | | ZEND_COLD void zend_mm_oom_info_dtor(zend_oom_info *info) |
472 | 600k | { |
473 | 600k | if (info->message) { |
474 | 835 | zend_string_release(info->message); |
475 | 835 | info->message = NULL; |
476 | 835 | } |
477 | 600k | if (info->filename) { |
478 | 835 | zend_string_release(info->filename); |
479 | 835 | info->filename = NULL; |
480 | 835 | } |
481 | 600k | } |
482 | | |
483 | | #ifdef ZEND_MM_LIMIT |
484 | | ZEND_NORETURN static ZEND_COLD void zend_mm_hard_limit_error(zend_mm_heap *heap, |
485 | | size_t add_size ZEND_FILE_LINE_DC) |
486 | 0 | { |
487 | 0 | zend_string *script_filename = NULL; |
488 | 0 | uint32_t script_lineno = 0; |
489 | |
|
490 | 0 | if (zend_is_executing()) { |
491 | 0 | script_filename = zend_get_executed_filename_ex(); |
492 | 0 | script_lineno = zend_get_executed_lineno(); |
493 | 0 | } else if (zend_is_compiling()) { |
494 | 0 | script_filename = zend_get_compiled_filename(); |
495 | 0 | script_lineno = zend_get_compiled_lineno(); |
496 | 0 | } |
497 | |
|
498 | 0 | if (script_filename == NULL) { |
499 | 0 | script_filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED); |
500 | 0 | script_lineno = 0; |
501 | 0 | } |
502 | |
|
503 | 0 | fprintf(stderr, |
504 | 0 | # if ZEND_DEBUG |
505 | 0 | "Hard memory limit of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes) in %s on line %d\n", |
506 | | # else |
507 | | "Hard memory limit of %zu bytes exhausted (tried to allocate %zu bytes) in %s on line %d\n", |
508 | | # endif |
509 | 0 | heap->hard_limit |
510 | 0 | ZEND_FILE_LINE_RELAY_CC, |
511 | 0 | add_size, |
512 | 0 | ZSTR_VAL(script_filename), |
513 | 0 | script_lineno); |
514 | | |
515 | | /* See http://support.microsoft.com/kb/190351 */ |
516 | | # ifdef ZEND_WIN32 |
517 | | fflush(stderr); |
518 | | # endif |
519 | |
|
520 | 0 | _exit(137); |
521 | 0 | } |
522 | | #endif /* ZEND_MM_LIMIT */ |
523 | | |
524 | | /* Returns FAILURE if allocation should fail (in case the heap is fallible). |
525 | | * When the soft limit is to be exceeded, a delayed OOM error is queued. |
526 | | * When the hard limit is to be exceeded, the function does not return. |
527 | | */ |
528 | | static zend_always_inline zend_result zend_mm_check_limit(zend_mm_heap *heap, size_t add_size, bool gc, bool *did_gc ZEND_FILE_LINE_DC) |
529 | 133M | { |
530 | 133M | #if ZEND_MM_LIMIT |
531 | | /* Is the soft limit exceeded or going to be exceeded? */ |
532 | 133M | if (UNEXPECTED(heap->limit < heap->real_size || add_size > heap->limit - heap->real_size)) { |
533 | 11.1M | if (gc && zend_mm_gc(heap)) { |
534 | 0 | if (did_gc) { |
535 | 0 | *did_gc = true; |
536 | 0 | } |
537 | 0 | } |
538 | 11.1M | ZEND_ASSERT(heap->hard_limit >= heap->limit && heap->hard_limit >= heap->real_size); |
539 | 11.1M | if (!heap->fallible && add_size > heap->hard_limit - heap->real_size) { |
540 | 0 | zend_mm_hard_limit_error(heap, add_size ZEND_FILE_LINE_RELAY_CC); |
541 | 0 | } |
542 | | /* Check soft limit again as gc may have reduced memory usage */ |
543 | 11.1M | if (!gc || (heap->limit < heap->real_size || add_size > heap->limit - heap->real_size)) { |
544 | 11.1M | if (!ZEND_HAS_OOM()) { |
545 | 835 | zend_mm_delayed_error( |
546 | 835 | # if ZEND_DEBUG |
547 | 835 | "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", |
548 | | # else |
549 | | "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", |
550 | | # endif |
551 | 835 | heap->limit |
552 | 835 | ZEND_FILE_LINE_RELAY_CC, |
553 | 835 | add_size); |
554 | 835 | } |
555 | | |
556 | 11.1M | return heap->fallible ? FAILURE : SUCCESS; |
557 | 11.1M | } |
558 | 11.1M | } |
559 | 122M | #endif /* ZEND_MM_LIMIT */ |
560 | | |
561 | 122M | return SUCCESS; |
562 | 133M | } |
563 | | |
564 | | #ifdef _WIN32 |
565 | | static void stderr_last_error(char *msg) |
566 | | { |
567 | | DWORD err = GetLastError(); |
568 | | char *buf = php_win32_error_to_msg(err); |
569 | | |
570 | | if (!buf[0]) { |
571 | | fprintf(stderr, "\n%s: [0x%08lx]\n", msg, err); |
572 | | } |
573 | | else { |
574 | | fprintf(stderr, "\n%s: [0x%08lx] %s\n", msg, err, buf); |
575 | | } |
576 | | |
577 | | php_win32_error_msg_free(buf); |
578 | | } |
579 | | #endif |
580 | | |
581 | | /*****************/ |
582 | | /* OS Allocation */ |
583 | | /*****************/ |
584 | | |
585 | | static void zend_mm_munmap(void *addr, size_t size) |
586 | 0 | { |
587 | | #ifdef _WIN32 |
588 | | if (VirtualFree(addr, 0, MEM_RELEASE) == 0) { |
589 | | /** ERROR_INVALID_ADDRESS is expected when addr is not range start address */ |
590 | | if (GetLastError() != ERROR_INVALID_ADDRESS) { |
591 | | #if ZEND_MM_ERROR |
592 | | stderr_last_error("VirtualFree() failed"); |
593 | | #endif |
594 | | return; |
595 | | } |
596 | | SetLastError(0); |
597 | | |
598 | | MEMORY_BASIC_INFORMATION mbi; |
599 | | if (VirtualQuery(addr, &mbi, sizeof(mbi)) == 0) { |
600 | | #if ZEND_MM_ERROR |
601 | | stderr_last_error("VirtualQuery() failed"); |
602 | | #endif |
603 | | return; |
604 | | } |
605 | | addr = mbi.AllocationBase; |
606 | | |
607 | | if (VirtualFree(addr, 0, MEM_RELEASE) == 0) { |
608 | | #if ZEND_MM_ERROR |
609 | | stderr_last_error("VirtualFree() failed"); |
610 | | #endif |
611 | | } |
612 | | } |
613 | | #else |
614 | 0 | if (munmap(addr, size) != 0) { |
615 | 0 | #if ZEND_MM_ERROR |
616 | 0 | fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno)); |
617 | 0 | #endif |
618 | 0 | } |
619 | 0 | #endif |
620 | 0 | } |
621 | | |
622 | | #ifndef HAVE_MREMAP |
623 | | static void *zend_mm_mmap_fixed(void *addr, size_t size) |
624 | | { |
625 | | #ifdef _WIN32 |
626 | | void *ptr = VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
627 | | |
628 | | if (ptr == NULL) { |
629 | | /** ERROR_INVALID_ADDRESS is expected when fixed addr range is not free */ |
630 | | if (GetLastError() != ERROR_INVALID_ADDRESS) { |
631 | | #if ZEND_MM_ERROR |
632 | | stderr_last_error("VirtualAlloc() fixed failed"); |
633 | | #endif |
634 | | } |
635 | | SetLastError(0); |
636 | | return NULL; |
637 | | } |
638 | | ZEND_ASSERT(ptr == addr); |
639 | | return ptr; |
640 | | #else |
641 | | int flags = MAP_PRIVATE | MAP_ANON; |
642 | | #if defined(MAP_EXCL) |
643 | | flags |= MAP_FIXED | MAP_EXCL; |
644 | | #elif defined(MAP_TRYFIXED) |
645 | | flags |= MAP_TRYFIXED; |
646 | | #endif |
647 | | /* MAP_FIXED leads to discarding of the old mapping, so it can't be used. */ |
648 | | void *ptr = mmap(addr, size, PROT_READ | PROT_WRITE, flags /*| MAP_POPULATE | MAP_HUGETLB*/, ZEND_MM_FD, 0); |
649 | | |
650 | | if (ptr == MAP_FAILED) { |
651 | | #if ZEND_MM_ERROR && !defined(MAP_EXCL) && !defined(MAP_TRYFIXED) |
652 | | fprintf(stderr, "\nmmap() fixed failed: [%d] %s\n", errno, strerror(errno)); |
653 | | #endif |
654 | | return NULL; |
655 | | } else if (ptr != addr) { |
656 | | zend_mm_munmap(ptr, size); |
657 | | return NULL; |
658 | | } |
659 | | return ptr; |
660 | | #endif |
661 | | } |
662 | | #endif |
663 | | |
664 | | static void *zend_mm_mmap(size_t size) |
665 | 0 | { |
666 | | #ifdef _WIN32 |
667 | | void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
668 | | |
669 | | if (ptr == NULL) { |
670 | | #if ZEND_MM_ERROR |
671 | | stderr_last_error("VirtualAlloc() failed"); |
672 | | #endif |
673 | | return NULL; |
674 | | } |
675 | | return ptr; |
676 | | #else |
677 | 0 | void *ptr; |
678 | |
|
679 | 0 | #if defined(MAP_HUGETLB) || defined(VM_FLAGS_SUPERPAGE_SIZE_2MB) |
680 | 0 | if (zend_mm_use_huge_pages && size == ZEND_MM_CHUNK_SIZE) { |
681 | 0 | int fd = -1; |
682 | 0 | int mflags = MAP_PRIVATE | MAP_ANON; |
683 | 0 | #if defined(MAP_HUGETLB) |
684 | 0 | mflags |= MAP_HUGETLB; |
685 | | #else |
686 | | fd = VM_FLAGS_SUPERPAGE_SIZE_2MB; |
687 | | #endif |
688 | 0 | ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, mflags, fd, 0); |
689 | 0 | if (ptr != MAP_FAILED) { |
690 | 0 | zend_mmap_set_name(ptr, size, "zend_alloc"); |
691 | 0 | return ptr; |
692 | 0 | } |
693 | 0 | } |
694 | 0 | #endif |
695 | | |
696 | 0 | ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, ZEND_MM_FD, 0); |
697 | |
|
698 | 0 | if (ptr == MAP_FAILED) { |
699 | 0 | #if ZEND_MM_ERROR |
700 | 0 | fprintf(stderr, "\nmmap() failed: [%d] %s\n", errno, strerror(errno)); |
701 | 0 | #endif |
702 | 0 | return NULL; |
703 | 0 | } |
704 | 0 | zend_mmap_set_name(ptr, size, "zend_alloc"); |
705 | 0 | return ptr; |
706 | 0 | #endif |
707 | 0 | } |
708 | | |
709 | | /***********/ |
710 | | /* Bitmask */ |
711 | | /***********/ |
712 | | |
713 | | /* number of trailing set (1) bits */ |
714 | | ZEND_ATTRIBUTE_CONST static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset) |
715 | 0 | { |
716 | 0 | #if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) && SIZEOF_ZEND_LONG == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL) |
717 | 0 | return __builtin_ctzl(~bitset); |
718 | | #elif (defined(__GNUC__) || __has_builtin(__builtin_ctzll)) && defined(PHP_HAVE_BUILTIN_CTZLL) |
719 | | return __builtin_ctzll(~bitset); |
720 | | #elif defined(_WIN32) |
721 | | unsigned long index; |
722 | | |
723 | | #if defined(_WIN64) |
724 | | if (!BitScanForward64(&index, ~bitset)) { |
725 | | #else |
726 | | if (!BitScanForward(&index, ~bitset)) { |
727 | | #endif |
728 | | /* undefined behavior */ |
729 | | return 32; |
730 | | } |
731 | | |
732 | | return (int)index; |
733 | | #else |
734 | | int n; |
735 | | |
736 | | if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN; |
737 | | |
738 | | n = 0; |
739 | | #if SIZEOF_ZEND_LONG == 8 |
740 | | if (sizeof(zend_mm_bitset) == 8) { |
741 | | if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);} |
742 | | } |
743 | | #endif |
744 | | if ((bitset & 0x0000ffff) == 0x0000ffff) {n += 16; bitset = bitset >> 16;} |
745 | | if ((bitset & 0x000000ff) == 0x000000ff) {n += 8; bitset = bitset >> 8;} |
746 | | if ((bitset & 0x0000000f) == 0x0000000f) {n += 4; bitset = bitset >> 4;} |
747 | | if ((bitset & 0x00000003) == 0x00000003) {n += 2; bitset = bitset >> 2;} |
748 | | return n + (bitset & 1); |
749 | | #endif |
750 | 0 | } |
751 | | |
752 | | static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int bit) |
753 | 0 | { |
754 | 0 | return ZEND_BIT_TEST(bitset, bit); |
755 | 0 | } |
756 | | |
757 | | static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit) |
758 | 0 | { |
759 | 0 | bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1))); |
760 | 0 | } |
761 | | |
762 | | static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit) |
763 | 0 | { |
764 | 0 | bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1))); |
765 | 0 | } |
766 | | |
767 | | static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len) |
768 | 0 | { |
769 | 0 | if (len == 1) { |
770 | 0 | zend_mm_bitset_set_bit(bitset, start); |
771 | 0 | } else { |
772 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
773 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
774 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
775 | 0 | zend_mm_bitset tmp; |
776 | |
|
777 | 0 | if (pos != end) { |
778 | | /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
779 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
780 | 0 | bitset[pos++] |= tmp; |
781 | 0 | while (pos != end) { |
782 | | /* set all bits */ |
783 | 0 | bitset[pos++] = (zend_mm_bitset)-1; |
784 | 0 | } |
785 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
786 | | /* set bits from "0" to "end" */ |
787 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
788 | 0 | bitset[pos] |= tmp; |
789 | 0 | } else { |
790 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
791 | | /* set bits from "bit" to "end" */ |
792 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
793 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
794 | 0 | bitset[pos] |= tmp; |
795 | 0 | } |
796 | 0 | } |
797 | 0 | } |
798 | | |
799 | | static zend_always_inline void zend_mm_bitset_reset_range(zend_mm_bitset *bitset, int start, int len) |
800 | 0 | { |
801 | 0 | if (len == 1) { |
802 | 0 | zend_mm_bitset_reset_bit(bitset, start); |
803 | 0 | } else { |
804 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
805 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
806 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
807 | 0 | zend_mm_bitset tmp; |
808 | |
|
809 | 0 | if (pos != end) { |
810 | | /* reset bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
811 | 0 | tmp = ~((Z_UL(1) << bit) - 1); |
812 | 0 | bitset[pos++] &= ~tmp; |
813 | 0 | while (pos != end) { |
814 | | /* set all bits */ |
815 | 0 | bitset[pos++] = 0; |
816 | 0 | } |
817 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
818 | | /* reset bits from "0" to "end" */ |
819 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
820 | 0 | bitset[pos] &= ~tmp; |
821 | 0 | } else { |
822 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
823 | | /* reset bits from "bit" to "end" */ |
824 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
825 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
826 | 0 | bitset[pos] &= ~tmp; |
827 | 0 | } |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | | static zend_always_inline int zend_mm_bitset_is_free_range(zend_mm_bitset *bitset, int start, int len) |
832 | 0 | { |
833 | 0 | if (len == 1) { |
834 | 0 | return !zend_mm_bitset_is_set(bitset, start); |
835 | 0 | } else { |
836 | 0 | int pos = start / ZEND_MM_BITSET_LEN; |
837 | 0 | int end = (start + len - 1) / ZEND_MM_BITSET_LEN; |
838 | 0 | int bit = start & (ZEND_MM_BITSET_LEN - 1); |
839 | 0 | zend_mm_bitset tmp; |
840 | |
|
841 | 0 | if (pos != end) { |
842 | | /* set bits from "bit" to ZEND_MM_BITSET_LEN-1 */ |
843 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
844 | 0 | if ((bitset[pos++] & tmp) != 0) { |
845 | 0 | return 0; |
846 | 0 | } |
847 | 0 | while (pos != end) { |
848 | | /* set all bits */ |
849 | 0 | if (bitset[pos++] != 0) { |
850 | 0 | return 0; |
851 | 0 | } |
852 | 0 | } |
853 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
854 | | /* set bits from "0" to "end" */ |
855 | 0 | tmp = (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
856 | 0 | return (bitset[pos] & tmp) == 0; |
857 | 0 | } else { |
858 | 0 | end = (start + len - 1) & (ZEND_MM_BITSET_LEN - 1); |
859 | | /* set bits from "bit" to "end" */ |
860 | 0 | tmp = (zend_mm_bitset)-1 << bit; |
861 | 0 | tmp &= (zend_mm_bitset)-1 >> ((ZEND_MM_BITSET_LEN - 1) - end); |
862 | 0 | return (bitset[pos] & tmp) == 0; |
863 | 0 | } |
864 | 0 | } |
865 | 0 | } |
866 | | |
867 | | /**********/ |
868 | | /* Chunks */ |
869 | | /**********/ |
870 | | |
871 | | static zend_always_inline void zend_mm_hugepage(void* ptr, size_t size) |
872 | 0 | { |
873 | 0 | #if defined(MADV_HUGEPAGE) |
874 | 0 | (void)madvise(ptr, size, MADV_HUGEPAGE); |
875 | | #elif defined(HAVE_MEMCNTL) |
876 | | struct memcntl_mha m = {.mha_cmd = MHA_MAPSIZE_VA, .mha_pagesize = ZEND_MM_CHUNK_SIZE, .mha_flags = 0}; |
877 | | (void)memcntl(ptr, size, MC_HAT_ADVISE, (char *)&m, 0, 0); |
878 | | #elif !defined(VM_FLAGS_SUPERPAGE_SIZE_2MB) && !defined(MAP_ALIGNED_SUPER) |
879 | | zend_error_noreturn(E_ERROR, "huge_pages: thp unsupported on this platform"); |
880 | | #endif |
881 | 0 | } |
882 | | |
883 | | static void *zend_mm_chunk_alloc_int(size_t size, size_t alignment) |
884 | 0 | { |
885 | 0 | void *ptr = zend_mm_mmap(size); |
886 | |
|
887 | 0 | if (ptr == NULL) { |
888 | 0 | return NULL; |
889 | 0 | } else if (ZEND_MM_ALIGNED_OFFSET(ptr, alignment) == 0) { |
890 | 0 | if (zend_mm_use_huge_pages) { |
891 | 0 | zend_mm_hugepage(ptr, size); |
892 | 0 | } |
893 | | #ifdef __SANITIZE_ADDRESS__ |
894 | | ASAN_UNPOISON_MEMORY_REGION(ptr, size); |
895 | | #endif |
896 | 0 | return ptr; |
897 | 0 | } else { |
898 | 0 | size_t offset; |
899 | | |
900 | | /* chunk has to be aligned */ |
901 | 0 | zend_mm_munmap(ptr, size); |
902 | 0 | ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE); |
903 | 0 | if (ptr == NULL) { |
904 | 0 | return NULL; |
905 | 0 | } |
906 | | #ifdef _WIN32 |
907 | | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
908 | | if (offset != 0) { |
909 | | offset = alignment - offset; |
910 | | } |
911 | | zend_mm_munmap(ptr, size + alignment - REAL_PAGE_SIZE); |
912 | | ptr = zend_mm_mmap_fixed((void*)((char*)ptr + offset), size); |
913 | | if (ptr == NULL) { // fix GH-9650, fixed addr range is not free |
914 | | ptr = zend_mm_mmap(size + alignment - REAL_PAGE_SIZE); |
915 | | if (ptr == NULL) { |
916 | | return NULL; |
917 | | } |
918 | | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
919 | | if (offset != 0) { |
920 | | ptr = (void*)((char*)ptr + alignment - offset); |
921 | | } |
922 | | } |
923 | | return ptr; |
924 | | #else |
925 | 0 | offset = ZEND_MM_ALIGNED_OFFSET(ptr, alignment); |
926 | 0 | if (offset != 0) { |
927 | 0 | offset = alignment - offset; |
928 | 0 | zend_mm_munmap(ptr, offset); |
929 | 0 | ptr = (char*)ptr + offset; |
930 | 0 | alignment -= offset; |
931 | 0 | } |
932 | 0 | if (alignment > REAL_PAGE_SIZE) { |
933 | 0 | zend_mm_munmap((char*)ptr + size, alignment - REAL_PAGE_SIZE); |
934 | 0 | } |
935 | 0 | if (zend_mm_use_huge_pages) { |
936 | 0 | zend_mm_hugepage(ptr, size); |
937 | 0 | } |
938 | | # ifdef __SANITIZE_ADDRESS__ |
939 | | ASAN_UNPOISON_MEMORY_REGION(ptr, size); |
940 | | # endif |
941 | 0 | #endif |
942 | 0 | return ptr; |
943 | 0 | } |
944 | 0 | } |
945 | | |
946 | | static void *zend_mm_chunk_alloc(zend_mm_heap *heap, size_t size, size_t alignment) |
947 | 0 | { |
948 | 0 | #if ZEND_MM_STORAGE |
949 | 0 | if (UNEXPECTED(heap->storage)) { |
950 | 0 | void *ptr = heap->storage->handlers.chunk_alloc(heap->storage, size, alignment); |
951 | 0 | ZEND_ASSERT(((uintptr_t)((char*)ptr + (alignment-1)) & (alignment-1)) == (uintptr_t)ptr); |
952 | 0 | return ptr; |
953 | 0 | } |
954 | 0 | #endif |
955 | 0 | return zend_mm_chunk_alloc_int(size, alignment); |
956 | 0 | } |
957 | | |
958 | | static void zend_mm_chunk_free(zend_mm_heap *heap, void *addr, size_t size) |
959 | 0 | { |
960 | 0 | #if ZEND_MM_STORAGE |
961 | 0 | if (UNEXPECTED(heap->storage)) { |
962 | 0 | heap->storage->handlers.chunk_free(heap->storage, addr, size); |
963 | 0 | return; |
964 | 0 | } |
965 | 0 | #endif |
966 | 0 | zend_mm_munmap(addr, size); |
967 | 0 | } |
968 | | |
969 | | static int zend_mm_chunk_truncate(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size) |
970 | 0 | { |
971 | 0 | #if ZEND_MM_STORAGE |
972 | 0 | if (UNEXPECTED(heap->storage)) { |
973 | 0 | if (heap->storage->handlers.chunk_truncate) { |
974 | 0 | return heap->storage->handlers.chunk_truncate(heap->storage, addr, old_size, new_size); |
975 | 0 | } else { |
976 | 0 | return 0; |
977 | 0 | } |
978 | 0 | } |
979 | 0 | #endif |
980 | 0 | #ifndef _WIN32 |
981 | 0 | zend_mm_munmap((char*)addr + new_size, old_size - new_size); |
982 | 0 | return 1; |
983 | | #else |
984 | | return 0; |
985 | | #endif |
986 | 0 | } |
987 | | |
988 | | static int zend_mm_chunk_extend(zend_mm_heap *heap, void *addr, size_t old_size, size_t new_size) |
989 | 0 | { |
990 | 0 | #if ZEND_MM_STORAGE |
991 | 0 | if (UNEXPECTED(heap->storage)) { |
992 | 0 | if (heap->storage->handlers.chunk_extend) { |
993 | 0 | return heap->storage->handlers.chunk_extend(heap->storage, addr, old_size, new_size); |
994 | 0 | } else { |
995 | 0 | return 0; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | #endif |
999 | 0 | #ifdef HAVE_MREMAP |
1000 | | /* We don't use MREMAP_MAYMOVE due to alignment requirements. */ |
1001 | 0 | void *ptr = mremap(addr, old_size, new_size, 0); |
1002 | 0 | if (ptr == MAP_FAILED) { |
1003 | 0 | return 0; |
1004 | 0 | } |
1005 | | /* Sanity check: The mapping shouldn't have moved. */ |
1006 | 0 | ZEND_ASSERT(ptr == addr); |
1007 | 0 | return 1; |
1008 | | #elif !defined(_WIN32) |
1009 | | return (zend_mm_mmap_fixed((char*)addr + old_size, new_size - old_size) != NULL); |
1010 | | #else |
1011 | | return 0; |
1012 | | #endif |
1013 | 0 | } |
1014 | | |
1015 | | static zend_always_inline void zend_mm_chunk_init(zend_mm_heap *heap, zend_mm_chunk *chunk) |
1016 | 0 | { |
1017 | 0 | chunk->heap = heap; |
1018 | 0 | chunk->next = heap->main_chunk; |
1019 | 0 | chunk->prev = heap->main_chunk->prev; |
1020 | 0 | chunk->prev->next = chunk; |
1021 | 0 | chunk->next->prev = chunk; |
1022 | | /* mark first pages as allocated */ |
1023 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
1024 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
1025 | | /* the younger chunks have bigger number */ |
1026 | 0 | chunk->num = chunk->prev->num + 1; |
1027 | | /* mark first pages as allocated */ |
1028 | 0 | chunk->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1; |
1029 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
1030 | 0 | } |
1031 | | |
1032 | | /* Cached chunks are linked through their headers, which live in memory a heap |
1033 | | * overflow can reach, so the link is mirrored in an encoded shadow. The shadow |
1034 | | * is byte-swapped, so that small overwrites hit the most significant bytes of |
1035 | | * the address, XOR'ed with the heap key, and XOR'ed with its own address so |
1036 | | * that a valid (link, shadow) pair cannot be replayed into another chunk. */ |
1037 | | static zend_always_inline zend_mm_chunk *zend_mm_encode_cached_chunk(const zend_mm_heap *heap, const void *holder, const zend_mm_chunk *next) |
1038 | 0 | { |
1039 | | #ifdef WORDS_BIGENDIAN |
1040 | | return (zend_mm_chunk*)((uintptr_t)next ^ heap->shadow_key ^ (uintptr_t)holder); |
1041 | | #else |
1042 | 0 | return (zend_mm_chunk*)(BSWAPPTR((uintptr_t)next) ^ heap->shadow_key ^ (uintptr_t)holder); |
1043 | 0 | #endif |
1044 | 0 | } |
1045 | | |
1046 | | static zend_always_inline zend_mm_chunk *zend_mm_decode_cached_chunk_key(uintptr_t key, const void *holder, const zend_mm_chunk *encoded) |
1047 | 0 | { |
1048 | | #ifdef WORDS_BIGENDIAN |
1049 | | zend_mm_chunk *next = (zend_mm_chunk*)((uintptr_t)encoded ^ key ^ (uintptr_t)holder); |
1050 | | #else |
1051 | 0 | zend_mm_chunk *next = (zend_mm_chunk*)(BSWAPPTR((uintptr_t)encoded ^ key ^ (uintptr_t)holder)); |
1052 | 0 | #endif |
1053 | |
|
1054 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(next, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted"); |
1055 | 0 | return next; |
1056 | 0 | } |
1057 | | |
1058 | | static zend_always_inline void zend_mm_set_next_cached_chunk(zend_mm_heap *heap, zend_mm_chunk *chunk, zend_mm_chunk *next) |
1059 | 0 | { |
1060 | 0 | chunk->next = next; |
1061 | 0 | chunk->next_shadow = zend_mm_encode_cached_chunk(heap, &chunk->next_shadow, next); |
1062 | 0 | } |
1063 | | |
1064 | | static zend_always_inline zend_mm_chunk *zend_mm_get_next_cached_chunk_key(uintptr_t key, const zend_mm_chunk *chunk) |
1065 | 0 | { |
1066 | 0 | zend_mm_chunk *next = zend_mm_decode_cached_chunk_key(key, &chunk->next_shadow, chunk->next_shadow); |
1067 | |
|
1068 | 0 | ZEND_MM_CHECK(chunk->next == next, "zend_mm_heap corrupted"); |
1069 | 0 | return next; |
1070 | 0 | } |
1071 | | |
1072 | | static zend_always_inline zend_mm_chunk *zend_mm_get_next_cached_chunk(const zend_mm_heap *heap, const zend_mm_chunk *chunk) |
1073 | 0 | { |
1074 | 0 | return zend_mm_get_next_cached_chunk_key(heap->shadow_key, chunk); |
1075 | 0 | } |
1076 | | |
1077 | | /* Re-encode the cached links after the heap key changed. */ |
1078 | | static zend_always_inline void zend_mm_rekey_cached_chunks(zend_mm_heap *heap, uintptr_t old_key) |
1079 | 0 | { |
1080 | 0 | zend_mm_chunk *chunk = heap->cached_chunks; |
1081 | |
|
1082 | 0 | while (chunk != NULL) { |
1083 | 0 | zend_mm_chunk *next = zend_mm_get_next_cached_chunk_key(old_key, chunk); |
1084 | |
|
1085 | 0 | zend_mm_set_next_cached_chunk(heap, chunk, next); |
1086 | 0 | chunk = next; |
1087 | 0 | } |
1088 | 0 | } |
1089 | | |
1090 | | /***********************/ |
1091 | | /* Huge Runs (forward) */ |
1092 | | /***********************/ |
1093 | | |
1094 | | static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
1095 | | static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
1096 | | static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
1097 | | |
1098 | | #if ZEND_DEBUG |
1099 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
1100 | | #else |
1101 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
1102 | | #endif |
1103 | | |
1104 | | /**************/ |
1105 | | /* Large Runs */ |
1106 | | /**************/ |
1107 | | |
1108 | | #if ZEND_DEBUG |
1109 | | static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1110 | | #else |
1111 | | static void *zend_mm_alloc_pages(zend_mm_heap *heap, uint32_t pages_count ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1112 | | #endif |
1113 | 0 | { |
1114 | 0 | zend_mm_chunk *chunk = heap->main_chunk; |
1115 | 0 | uint32_t page_num, len; |
1116 | 0 | int steps = 0; |
1117 | |
|
1118 | 0 | while (1) { |
1119 | 0 | if (UNEXPECTED(chunk->free_pages < pages_count)) { |
1120 | 0 | goto not_found; |
1121 | | #if 0 |
1122 | | } else if (UNEXPECTED(chunk->free_pages + chunk->free_tail == ZEND_MM_PAGES)) { |
1123 | | if (UNEXPECTED(ZEND_MM_PAGES - chunk->free_tail < pages_count)) { |
1124 | | goto not_found; |
1125 | | } else { |
1126 | | page_num = chunk->free_tail; |
1127 | | goto found; |
1128 | | } |
1129 | | } else if (0) { |
1130 | | /* First-Fit Search */ |
1131 | | int free_tail = chunk->free_tail; |
1132 | | zend_mm_bitset *bitset = chunk->free_map; |
1133 | | zend_mm_bitset tmp = *(bitset++); |
1134 | | int i = 0; |
1135 | | |
1136 | | while (1) { |
1137 | | /* skip allocated blocks */ |
1138 | | while (tmp == (zend_mm_bitset)-1) { |
1139 | | i += ZEND_MM_BITSET_LEN; |
1140 | | if (i == ZEND_MM_PAGES) { |
1141 | | goto not_found; |
1142 | | } |
1143 | | tmp = *(bitset++); |
1144 | | } |
1145 | | /* find first 0 bit */ |
1146 | | page_num = i + zend_mm_bitset_nts(tmp); |
1147 | | /* reset bits from 0 to "bit" */ |
1148 | | tmp &= tmp + 1; |
1149 | | /* skip free blocks */ |
1150 | | while (tmp == 0) { |
1151 | | i += ZEND_MM_BITSET_LEN; |
1152 | | len = i - page_num; |
1153 | | if (len >= pages_count) { |
1154 | | goto found; |
1155 | | } else if (i >= free_tail) { |
1156 | | goto not_found; |
1157 | | } |
1158 | | tmp = *(bitset++); |
1159 | | } |
1160 | | /* find first 1 bit */ |
1161 | | len = (i + zend_ulong_ntz(tmp)) - page_num; |
1162 | | if (len >= pages_count) { |
1163 | | goto found; |
1164 | | } |
1165 | | /* set bits from 0 to "bit" */ |
1166 | | tmp |= tmp - 1; |
1167 | | } |
1168 | | #endif |
1169 | 0 | } else { |
1170 | | /* Best-Fit Search */ |
1171 | 0 | int best = -1; |
1172 | 0 | uint32_t best_len = ZEND_MM_PAGES; |
1173 | 0 | uint32_t free_tail = chunk->free_tail; |
1174 | 0 | zend_mm_bitset *bitset = chunk->free_map; |
1175 | 0 | zend_mm_bitset tmp = *(bitset++); |
1176 | 0 | uint32_t i = 0; |
1177 | |
|
1178 | 0 | while (1) { |
1179 | | /* skip allocated blocks */ |
1180 | 0 | while (tmp == (zend_mm_bitset)-1) { |
1181 | 0 | i += ZEND_MM_BITSET_LEN; |
1182 | 0 | if (i == ZEND_MM_PAGES) { |
1183 | 0 | if (best > 0) { |
1184 | 0 | page_num = best; |
1185 | 0 | goto found; |
1186 | 0 | } else { |
1187 | 0 | goto not_found; |
1188 | 0 | } |
1189 | 0 | } |
1190 | 0 | tmp = *(bitset++); |
1191 | 0 | } |
1192 | | /* find first 0 bit */ |
1193 | 0 | page_num = i + zend_mm_bitset_nts(tmp); |
1194 | | /* reset bits from 0 to "bit" */ |
1195 | 0 | tmp &= tmp + 1; |
1196 | | /* skip free blocks */ |
1197 | 0 | while (tmp == 0) { |
1198 | 0 | i += ZEND_MM_BITSET_LEN; |
1199 | 0 | if (i >= free_tail || i == ZEND_MM_PAGES) { |
1200 | 0 | len = ZEND_MM_PAGES - page_num; |
1201 | 0 | if (len >= pages_count && len < best_len) { |
1202 | 0 | chunk->free_tail = page_num + pages_count; |
1203 | 0 | goto found; |
1204 | 0 | } else { |
1205 | | /* set accurate value */ |
1206 | 0 | chunk->free_tail = page_num; |
1207 | 0 | if (best > 0) { |
1208 | 0 | page_num = best; |
1209 | 0 | goto found; |
1210 | 0 | } else { |
1211 | 0 | goto not_found; |
1212 | 0 | } |
1213 | 0 | } |
1214 | 0 | } |
1215 | 0 | tmp = *(bitset++); |
1216 | 0 | } |
1217 | | /* find first 1 bit */ |
1218 | 0 | len = i + zend_ulong_ntz(tmp) - page_num; |
1219 | 0 | if (len >= pages_count) { |
1220 | 0 | if (len == pages_count) { |
1221 | 0 | goto found; |
1222 | 0 | } else if (len < best_len) { |
1223 | 0 | best_len = len; |
1224 | 0 | best = page_num; |
1225 | 0 | } |
1226 | 0 | } |
1227 | | /* set bits from 0 to "bit" */ |
1228 | 0 | tmp |= tmp - 1; |
1229 | 0 | } |
1230 | 0 | } |
1231 | | |
1232 | 0 | not_found: |
1233 | 0 | if (chunk->next == heap->main_chunk) { |
1234 | 0 | get_chunk: |
1235 | 0 | if (heap->cached_chunks) { |
1236 | 0 | heap->cached_chunks_count--; |
1237 | 0 | chunk = heap->cached_chunks; |
1238 | | /* The list head lives in the heap, which is as reachable as the chunk headers. */ |
1239 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(chunk, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted"); |
1240 | 0 | heap->cached_chunks = zend_mm_get_next_cached_chunk(heap, chunk); |
1241 | 0 | } else { |
1242 | 0 | bool did_gc = false; |
1243 | 0 | if (UNEXPECTED(zend_mm_check_limit(heap, ZEND_MM_CHUNK_SIZE, /* gc */ true, &did_gc ZEND_FILE_LINE_RELAY_CC) == FAILURE)) { |
1244 | 0 | return NULL; |
1245 | 0 | } else if (did_gc) { |
1246 | 0 | goto get_chunk; |
1247 | 0 | } |
1248 | | |
1249 | 0 | chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
1250 | 0 | if (UNEXPECTED(chunk == NULL)) { |
1251 | | /* insufficient memory */ |
1252 | 0 | if (zend_mm_gc(heap) && |
1253 | 0 | (chunk = (zend_mm_chunk*)zend_mm_chunk_alloc(heap, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE)) != NULL) { |
1254 | | /* pass */ |
1255 | 0 | } else { |
1256 | 0 | zend_out_of_memory(); |
1257 | 0 | return NULL; |
1258 | 0 | } |
1259 | 0 | } |
1260 | 0 | #if ZEND_MM_STAT |
1261 | 0 | do { |
1262 | 0 | size_t size = heap->real_size + ZEND_MM_CHUNK_SIZE; |
1263 | 0 | size_t peak = MAX(heap->real_peak, size); |
1264 | 0 | heap->real_size = size; |
1265 | 0 | heap->real_peak = peak; |
1266 | 0 | } while (0); |
1267 | | #elif ZEND_MM_LIMIT |
1268 | | heap->real_size += ZEND_MM_CHUNK_SIZE; |
1269 | | |
1270 | | #endif |
1271 | 0 | } |
1272 | 0 | heap->chunks_count++; |
1273 | 0 | if (heap->chunks_count > heap->peak_chunks_count) { |
1274 | 0 | heap->peak_chunks_count = heap->chunks_count; |
1275 | 0 | } |
1276 | 0 | zend_mm_chunk_init(heap, chunk); |
1277 | 0 | page_num = ZEND_MM_FIRST_PAGE; |
1278 | 0 | len = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
1279 | 0 | goto found; |
1280 | 0 | } else { |
1281 | 0 | chunk = chunk->next; |
1282 | 0 | steps++; |
1283 | 0 | } |
1284 | 0 | } |
1285 | | |
1286 | 0 | found: |
1287 | 0 | if (steps > 2 && pages_count < 8) { |
1288 | 0 | ZEND_MM_CHECK(chunk->next->prev == chunk, "zend_mm_heap corrupted"); |
1289 | 0 | ZEND_MM_CHECK(chunk->prev->next == chunk, "zend_mm_heap corrupted"); |
1290 | | |
1291 | | /* move chunk into the head of the linked-list */ |
1292 | 0 | chunk->prev->next = chunk->next; |
1293 | 0 | chunk->next->prev = chunk->prev; |
1294 | 0 | chunk->next = heap->main_chunk->next; |
1295 | 0 | chunk->prev = heap->main_chunk; |
1296 | 0 | chunk->prev->next = chunk; |
1297 | 0 | chunk->next->prev = chunk; |
1298 | 0 | } |
1299 | | /* mark run as allocated */ |
1300 | 0 | chunk->free_pages -= pages_count; |
1301 | 0 | zend_mm_bitset_set_range(chunk->free_map, page_num, pages_count); |
1302 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(pages_count); |
1303 | 0 | if (page_num == chunk->free_tail) { |
1304 | 0 | chunk->free_tail = page_num + pages_count; |
1305 | 0 | } |
1306 | 0 | return ZEND_MM_PAGE_ADDR(chunk, page_num); |
1307 | 0 | } |
1308 | | |
1309 | | static zend_always_inline void *zend_mm_alloc_large_ex(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1310 | 0 | { |
1311 | 0 | int pages_count = (int)ZEND_MM_SIZE_TO_NUM(size, ZEND_MM_PAGE_SIZE); |
1312 | 0 | #if ZEND_DEBUG |
1313 | 0 | void *ptr = zend_mm_alloc_pages(heap, pages_count, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1314 | | #else |
1315 | | void *ptr = zend_mm_alloc_pages(heap, pages_count ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1316 | | #endif |
1317 | 0 | if (UNEXPECTED(ptr == NULL)) { |
1318 | 0 | return NULL; |
1319 | 0 | } |
1320 | 0 | #if ZEND_MM_STAT |
1321 | 0 | do { |
1322 | 0 | size_t size = heap->size + pages_count * ZEND_MM_PAGE_SIZE; |
1323 | 0 | size_t peak = MAX(heap->peak, size); |
1324 | 0 | heap->size = size; |
1325 | 0 | heap->peak = peak; |
1326 | 0 | } while (0); |
1327 | 0 | #endif |
1328 | 0 | return ptr; |
1329 | 0 | } |
1330 | | |
1331 | | static zend_never_inline void *zend_mm_alloc_large(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1332 | 0 | { |
1333 | 0 | return zend_mm_alloc_large_ex(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1334 | 0 | } |
1335 | | |
1336 | | static zend_always_inline void zend_mm_delete_chunk(zend_mm_heap *heap, zend_mm_chunk *chunk) |
1337 | 0 | { |
1338 | 0 | ZEND_MM_CHECK(chunk->next->prev == chunk, "zend_mm_heap corrupted"); |
1339 | 0 | ZEND_MM_CHECK(chunk->prev->next == chunk, "zend_mm_heap corrupted"); |
1340 | |
|
1341 | 0 | chunk->next->prev = chunk->prev; |
1342 | 0 | chunk->prev->next = chunk->next; |
1343 | 0 | heap->chunks_count--; |
1344 | 0 | if (heap->chunks_count + heap->cached_chunks_count < heap->avg_chunks_count + 0.1 |
1345 | 0 | || (heap->chunks_count == heap->last_chunks_delete_boundary |
1346 | 0 | && heap->last_chunks_delete_count >= 4)) { |
1347 | | /* delay deletion */ |
1348 | 0 | heap->cached_chunks_count++; |
1349 | 0 | zend_mm_set_next_cached_chunk(heap, chunk, heap->cached_chunks); |
1350 | 0 | heap->cached_chunks = chunk; |
1351 | 0 | } else { |
1352 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1353 | 0 | heap->real_size -= ZEND_MM_CHUNK_SIZE; |
1354 | 0 | #endif |
1355 | 0 | if (!heap->cached_chunks) { |
1356 | 0 | if (heap->chunks_count != heap->last_chunks_delete_boundary) { |
1357 | 0 | heap->last_chunks_delete_boundary = heap->chunks_count; |
1358 | 0 | heap->last_chunks_delete_count = 0; |
1359 | 0 | } else { |
1360 | 0 | heap->last_chunks_delete_count++; |
1361 | 0 | } |
1362 | 0 | } |
1363 | 0 | if (!heap->cached_chunks || chunk->num > heap->cached_chunks->num) { |
1364 | 0 | zend_mm_chunk_free(heap, chunk, ZEND_MM_CHUNK_SIZE); |
1365 | 0 | } else { |
1366 | | //TODO: select the best chunk to delete??? |
1367 | 0 | zend_mm_set_next_cached_chunk(heap, chunk, zend_mm_get_next_cached_chunk(heap, heap->cached_chunks)); |
1368 | 0 | zend_mm_chunk_free(heap, heap->cached_chunks, ZEND_MM_CHUNK_SIZE); |
1369 | 0 | heap->cached_chunks = chunk; |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | } |
1373 | | |
1374 | | static zend_always_inline void zend_mm_free_pages_ex(zend_mm_heap *heap, zend_mm_chunk *chunk, uint32_t page_num, uint32_t pages_count, int free_chunk) |
1375 | 0 | { |
1376 | 0 | chunk->free_pages += pages_count; |
1377 | 0 | zend_mm_bitset_reset_range(chunk->free_map, page_num, pages_count); |
1378 | 0 | chunk->map[page_num] = 0; |
1379 | 0 | if (chunk->free_tail == page_num + pages_count) { |
1380 | | /* this setting may be not accurate */ |
1381 | 0 | chunk->free_tail = page_num; |
1382 | 0 | } |
1383 | 0 | if (free_chunk && chunk != heap->main_chunk && chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE) { |
1384 | 0 | zend_mm_delete_chunk(heap, chunk); |
1385 | 0 | } |
1386 | 0 | } |
1387 | | |
1388 | | static zend_never_inline void zend_mm_free_pages(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count) |
1389 | 0 | { |
1390 | 0 | zend_mm_free_pages_ex(heap, chunk, page_num, pages_count, 1); |
1391 | 0 | } |
1392 | | |
1393 | | static zend_always_inline void zend_mm_free_large(zend_mm_heap *heap, zend_mm_chunk *chunk, int page_num, int pages_count) |
1394 | 0 | { |
1395 | 0 | #if ZEND_MM_STAT |
1396 | 0 | heap->size -= pages_count * ZEND_MM_PAGE_SIZE; |
1397 | 0 | #endif |
1398 | 0 | zend_mm_free_pages(heap, chunk, page_num, pages_count); |
1399 | 0 | } |
1400 | | |
1401 | | /**************/ |
1402 | | /* Small Runs */ |
1403 | | /**************/ |
1404 | | |
1405 | | /* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */ |
1406 | | static zend_always_inline int zend_mm_small_size_to_bit(uint32_t size) |
1407 | 0 | { |
1408 | 0 | #if (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ) |
1409 | 0 | return (__builtin_clz(size) ^ 0x1f) + 1; |
1410 | | #elif defined(_WIN32) |
1411 | | unsigned long index; |
1412 | | |
1413 | | if (!BitScanReverse(&index, (unsigned long)size)) { |
1414 | | /* undefined behavior */ |
1415 | | return 64; |
1416 | | } |
1417 | | |
1418 | | return (((31 - (int)index) ^ 0x1f) + 1); |
1419 | | #else |
1420 | | int n = 16; |
1421 | | if (size <= 0x00ff) {n -= 8; size = size << 8;} |
1422 | | if (size <= 0x0fff) {n -= 4; size = size << 4;} |
1423 | | if (size <= 0x3fff) {n -= 2; size = size << 2;} |
1424 | | if (size <= 0x7fff) {n -= 1;} |
1425 | | return n; |
1426 | | #endif |
1427 | 0 | } |
1428 | | |
1429 | | #ifndef MAX |
1430 | | # define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
1431 | | #endif |
1432 | | |
1433 | | #ifndef MIN |
1434 | | # define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
1435 | | #endif |
1436 | | |
1437 | | static zend_always_inline uint32_t zend_mm_small_size_to_bin(size_t size) |
1438 | 0 | { |
1439 | | #if 0 |
1440 | | int n; |
1441 | | /*0, 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 11, 12*/ |
1442 | | static const uint32_t f1[] = { 3, 3, 3, 3, 3, 3, 3, 4, 5, 6, 7, 8, 9}; |
1443 | | static const uint32_t f2[] = { 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24}; |
1444 | | |
1445 | | if (UNEXPECTED(size <= 2)) return 0; |
1446 | | n = zend_mm_small_size_to_bit(size - 1); |
1447 | | return ((size-1) >> f1[n]) + f2[n]; |
1448 | | #else |
1449 | 0 | uint32_t t1, t2; |
1450 | |
|
1451 | 0 | if (size <= 64) { |
1452 | | /* we need to support size == 0 ... */ |
1453 | 0 | return (size - !!size) >> 3; |
1454 | 0 | } else { |
1455 | 0 | t1 = size - 1; |
1456 | 0 | t2 = zend_mm_small_size_to_bit(t1) - 3; |
1457 | 0 | t1 = t1 >> t2; |
1458 | 0 | t2 = t2 - 3; |
1459 | 0 | t2 = t2 << 2; |
1460 | 0 | return t1 + t2; |
1461 | 0 | } |
1462 | 0 | #endif |
1463 | 0 | } |
1464 | | |
1465 | 0 | #define ZEND_MM_SMALL_SIZE_TO_BIN(size) zend_mm_small_size_to_bin(size) |
1466 | | |
1467 | | #if ZEND_MM_HEAP_PROTECTION |
1468 | | /* We keep track of free slots by organizing them in a linked list, with the |
1469 | | * first word of every free slot being a pointer to the next one. |
1470 | | * |
1471 | | * In order to frustrate corruptions, we check the consistency of these pointers |
1472 | | * before dereference by comparing them with a shadow. |
1473 | | * |
1474 | | * The shadow is a copy of the pointer, stored at the end of the slot. It is |
1475 | | * XOR'ed with a random key and with its own address, and converted to |
1476 | | * big-endian so that smaller corruptions affect the most significant bytes, |
1477 | | * which has a high chance of resulting in an invalid address instead of |
1478 | | * pointing to an adjacent slot. Mixing in the holder address keeps the key from |
1479 | | * being stored verbatim when the encoded pointer is NULL, and prevents a valid |
1480 | | * shadow from being naïvely replayed into another slot. |
1481 | | */ |
1482 | | |
1483 | | #define ZEND_MM_FREE_SLOT_PTR_SHADOW_ADDR(free_slot, bin_num) \ |
1484 | 0 | ((zend_mm_free_slot**)((char*)(free_slot) + bin_data_size[(bin_num)] - sizeof(zend_mm_free_slot*))) |
1485 | | |
1486 | | static zend_always_inline zend_mm_free_slot* zend_mm_encode_free_slot(const zend_mm_heap *heap, const void *holder, const zend_mm_free_slot *next) |
1487 | 0 | { |
1488 | | #ifdef WORDS_BIGENDIAN |
1489 | | return (zend_mm_free_slot*)((uintptr_t)next ^ heap->shadow_key ^ (uintptr_t)holder); |
1490 | | #else |
1491 | 0 | return (zend_mm_free_slot*)(BSWAPPTR((uintptr_t)next) ^ heap->shadow_key ^ (uintptr_t)holder); |
1492 | 0 | #endif |
1493 | 0 | } |
1494 | | |
1495 | | static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot_key(uintptr_t shadow_key, const void *holder, zend_mm_free_slot *shadow) |
1496 | 0 | { |
1497 | | #ifdef WORDS_BIGENDIAN |
1498 | | return (zend_mm_free_slot*)((uintptr_t)shadow ^ shadow_key ^ (uintptr_t)holder); |
1499 | | #else |
1500 | 0 | return (zend_mm_free_slot*)(BSWAPPTR((uintptr_t)shadow ^ shadow_key ^ (uintptr_t)holder)); |
1501 | 0 | #endif |
1502 | 0 | } |
1503 | | |
1504 | | static zend_always_inline zend_mm_free_slot* zend_mm_decode_free_slot(zend_mm_heap *heap, const void *holder, zend_mm_free_slot *shadow) |
1505 | 0 | { |
1506 | 0 | return zend_mm_decode_free_slot_key(heap->shadow_key, holder, shadow); |
1507 | 0 | } |
1508 | | |
1509 | | 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) |
1510 | 0 | { |
1511 | 0 | ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_USEABLE_BIN_SIZE); |
1512 | |
|
1513 | 0 | slot->next_free_slot = next; |
1514 | |
|
1515 | 0 | zend_mm_free_slot **shadow_addr = ZEND_MM_FREE_SLOT_PTR_SHADOW_ADDR(slot, bin_num); |
1516 | 0 | *shadow_addr = zend_mm_encode_free_slot(heap, shadow_addr, next); |
1517 | 0 | } |
1518 | | |
1519 | | static zend_always_inline zend_mm_free_slot *zend_mm_get_next_free_slot(zend_mm_heap *heap, uint32_t bin_num, zend_mm_free_slot* slot) |
1520 | 0 | { |
1521 | 0 | zend_mm_free_slot *next = slot->next_free_slot; |
1522 | 0 | if (EXPECTED(next != NULL)) { |
1523 | 0 | zend_mm_free_slot **shadow_addr = ZEND_MM_FREE_SLOT_PTR_SHADOW_ADDR(slot, bin_num); |
1524 | 0 | if (UNEXPECTED(next != zend_mm_decode_free_slot(heap, shadow_addr, *shadow_addr))) { |
1525 | 0 | zend_mm_panic("zend_mm_heap corrupted"); |
1526 | 0 | } |
1527 | 0 | } |
1528 | 0 | return (zend_mm_free_slot*)next; |
1529 | 0 | } |
1530 | | |
1531 | | #else /* ZEND_MM_HEAP_PROTECTION */ |
1532 | | # define zend_mm_set_next_free_slot(heap, bin_num, slot, next) do { \ |
1533 | | (slot)->next_free_slot = (next); \ |
1534 | | } while (0) |
1535 | | # define zend_mm_get_next_free_slot(heap, bin_num, slot) (slot)->next_free_slot |
1536 | | #endif /* ZEND_MM_HEAP_PROTECTION */ |
1537 | | |
1538 | | static zend_never_inline void *zend_mm_alloc_small_slow(zend_mm_heap *heap, uint32_t bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1539 | 0 | { |
1540 | 0 | zend_mm_chunk *chunk; |
1541 | 0 | int page_num; |
1542 | 0 | zend_mm_bin *bin; |
1543 | 0 | zend_mm_free_slot *p, *end; |
1544 | |
|
1545 | 0 | #if ZEND_DEBUG |
1546 | 0 | bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num], bin_data_size[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1547 | | #else |
1548 | | bin = (zend_mm_bin*)zend_mm_alloc_pages(heap, bin_pages[bin_num] ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1549 | | #endif |
1550 | 0 | if (UNEXPECTED(bin == NULL)) { |
1551 | | /* insufficient memory */ |
1552 | 0 | #if ZEND_MM_STAT |
1553 | | /* heap->size was incremented by the caller. Decrement here to preserve |
1554 | | * the tail call. */ |
1555 | 0 | heap->size -= bin_data_size[bin_num]; |
1556 | 0 | #endif |
1557 | 0 | return NULL; |
1558 | 0 | } |
1559 | | |
1560 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(bin, ZEND_MM_CHUNK_SIZE); |
1561 | 0 | page_num = ZEND_MM_ALIGNED_OFFSET(bin, ZEND_MM_CHUNK_SIZE) / ZEND_MM_PAGE_SIZE; |
1562 | 0 | chunk->map[page_num] = ZEND_MM_SRUN(bin_num); |
1563 | 0 | if (bin_pages[bin_num] > 1) { |
1564 | 0 | uint32_t i = 1; |
1565 | |
|
1566 | 0 | do { |
1567 | 0 | chunk->map[page_num+i] = ZEND_MM_NRUN(bin_num, i); |
1568 | 0 | i++; |
1569 | 0 | } while (i < bin_pages[bin_num]); |
1570 | 0 | } |
1571 | | |
1572 | | /* create a linked list of elements from 1 to last */ |
1573 | 0 | end = (zend_mm_free_slot*)((char*)bin + (bin_data_size[bin_num] * (bin_elements[bin_num] - 1))); |
1574 | 0 | heap->free_slot[bin_num] = p = (zend_mm_free_slot*)((char*)bin + bin_data_size[bin_num]); |
1575 | 0 | do { |
1576 | 0 | zend_mm_set_next_free_slot(heap, bin_num, p, (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num])); |
1577 | 0 | #if ZEND_DEBUG |
1578 | 0 | do { |
1579 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1580 | 0 | dbg->size = 0; |
1581 | 0 | } while (0); |
1582 | 0 | #endif |
1583 | 0 | p = (zend_mm_free_slot*)((char*)p + bin_data_size[bin_num]); |
1584 | 0 | } while (p != end); |
1585 | | |
1586 | | /* terminate list using NULL */ |
1587 | 0 | p->next_free_slot = NULL; |
1588 | 0 | #if ZEND_DEBUG |
1589 | 0 | do { |
1590 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1591 | 0 | dbg->size = 0; |
1592 | 0 | } while (0); |
1593 | 0 | #endif |
1594 | | |
1595 | | /* return first element */ |
1596 | 0 | return bin; |
1597 | 0 | } |
1598 | | |
1599 | | static zend_always_inline void *zend_mm_alloc_small(zend_mm_heap *heap, uint32_t bin_num ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1600 | 0 | { |
1601 | 0 | ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_USEABLE_BIN_SIZE); |
1602 | |
|
1603 | 0 | #if ZEND_MM_STAT |
1604 | 0 | do { |
1605 | 0 | size_t size = heap->size + bin_data_size[bin_num]; |
1606 | 0 | size_t peak = MAX(heap->peak, size); |
1607 | 0 | heap->size = size; |
1608 | 0 | heap->peak = peak; |
1609 | 0 | } while (0); |
1610 | 0 | #endif |
1611 | |
|
1612 | 0 | if (EXPECTED(heap->free_slot[bin_num] != NULL)) { |
1613 | 0 | zend_mm_free_slot *p = heap->free_slot[bin_num]; |
1614 | 0 | heap->free_slot[bin_num] = zend_mm_get_next_free_slot(heap, bin_num, p); |
1615 | 0 | return p; |
1616 | 0 | } else { |
1617 | 0 | return zend_mm_alloc_small_slow(heap, bin_num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1618 | 0 | } |
1619 | 0 | } |
1620 | | |
1621 | | static zend_always_inline void zend_mm_free_small(zend_mm_heap *heap, void *ptr, uint32_t bin_num) |
1622 | 0 | { |
1623 | 0 | ZEND_ASSERT(bin_data_size[bin_num] >= ZEND_MM_MIN_USEABLE_BIN_SIZE); |
1624 | |
|
1625 | 0 | zend_mm_free_slot *p; |
1626 | |
|
1627 | 0 | #if ZEND_MM_STAT |
1628 | 0 | heap->size -= bin_data_size[bin_num]; |
1629 | 0 | #endif |
1630 | |
|
1631 | 0 | #if ZEND_DEBUG |
1632 | 0 | do { |
1633 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1634 | 0 | dbg->size = 0; |
1635 | 0 | } while (0); |
1636 | 0 | #endif |
1637 | |
|
1638 | 0 | p = (zend_mm_free_slot*)ptr; |
1639 | 0 | #if ZEND_MM_HEAP_PROTECTION |
1640 | | /* Catch the most common double-free pattern for free. */ |
1641 | 0 | if (UNEXPECTED(p == heap->free_slot[bin_num])) { |
1642 | 0 | zend_mm_panic("zend_mm_heap corrupted (double free)"); |
1643 | 0 | } |
1644 | 0 | #endif |
1645 | 0 | zend_mm_set_next_free_slot(heap, bin_num, p, heap->free_slot[bin_num]); |
1646 | 0 | heap->free_slot[bin_num] = p; |
1647 | 0 | } |
1648 | | |
1649 | | /********/ |
1650 | | /* Heap */ |
1651 | | /********/ |
1652 | | |
1653 | | #if ZEND_DEBUG |
1654 | | static zend_always_inline zend_mm_debug_info *zend_mm_get_debug_info(zend_mm_heap *heap, void *ptr) |
1655 | 0 | { |
1656 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1657 | 0 | zend_mm_chunk *chunk; |
1658 | 0 | int page_num; |
1659 | 0 | zend_mm_page_info info; |
1660 | |
|
1661 | 0 | ZEND_MM_CHECK(page_offset != 0, "zend_mm_heap corrupted"); |
1662 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1663 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1664 | 0 | info = chunk->map[page_num]; |
1665 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1666 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1667 | 0 | uint32_t bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
1668 | 0 | return (zend_mm_debug_info*)((char*)ptr + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1669 | 0 | } else /* if (info & ZEND_MM_IS_LRUN) */ { |
1670 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(info); |
1671 | |
|
1672 | 0 | return (zend_mm_debug_info*)((char*)ptr + ZEND_MM_PAGE_SIZE * pages_count - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1673 | 0 | } |
1674 | 0 | } |
1675 | | #endif |
1676 | | |
1677 | | 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) |
1678 | 0 | { |
1679 | 0 | void *ptr; |
1680 | 0 | #if ZEND_MM_HEAP_PROTECTION |
1681 | 0 | if (size < ZEND_MM_MIN_USEABLE_BIN_SIZE) { |
1682 | 0 | size = ZEND_MM_MIN_USEABLE_BIN_SIZE; |
1683 | 0 | } |
1684 | 0 | #endif /* ZEND_MM_HEAP_PROTECTION */ |
1685 | 0 | #if ZEND_DEBUG |
1686 | 0 | size_t real_size = size; |
1687 | 0 | zend_mm_debug_info *dbg; |
1688 | | |
1689 | | /* special handling for zero-size allocation */ |
1690 | 0 | size = MAX(size, 1); |
1691 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1692 | 0 | if (UNEXPECTED(size < real_size)) { |
1693 | 0 | zend_mm_delayed_error_or_panic(heap->fallible, "Possible integer overflow in memory allocation (%zu + %zu)", ZEND_MM_ALIGNED_SIZE(real_size), ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
1694 | 0 | return NULL; |
1695 | 0 | } |
1696 | 0 | #endif |
1697 | 0 | if (EXPECTED(size <= ZEND_MM_MAX_SMALL_SIZE)) { |
1698 | 0 | ptr = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1699 | 0 | #if ZEND_DEBUG |
1700 | 0 | if (EXPECTED(ptr != NULL)) { |
1701 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1702 | 0 | dbg->size = real_size; |
1703 | 0 | dbg->filename = __zend_filename; |
1704 | 0 | dbg->orig_filename = __zend_orig_filename; |
1705 | 0 | dbg->lineno = __zend_lineno; |
1706 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1707 | 0 | } |
1708 | 0 | #endif |
1709 | 0 | return ptr; |
1710 | 0 | } else if (EXPECTED(size <= ZEND_MM_MAX_LARGE_SIZE)) { |
1711 | 0 | ptr = zend_mm_alloc_large(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1712 | 0 | #if ZEND_DEBUG |
1713 | 0 | if (EXPECTED(ptr != NULL)) { |
1714 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1715 | 0 | dbg->size = real_size; |
1716 | 0 | dbg->filename = __zend_filename; |
1717 | 0 | dbg->orig_filename = __zend_orig_filename; |
1718 | 0 | dbg->lineno = __zend_lineno; |
1719 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1720 | 0 | } |
1721 | 0 | #endif |
1722 | 0 | return ptr; |
1723 | 0 | } else { |
1724 | 0 | #if ZEND_DEBUG |
1725 | 0 | size = real_size; |
1726 | 0 | #endif |
1727 | 0 | return zend_mm_alloc_huge(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1728 | 0 | } |
1729 | 0 | } |
1730 | | |
1731 | | static zend_always_inline void zend_mm_free_heap(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1732 | 0 | { |
1733 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1734 | |
|
1735 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1736 | 0 | if (ptr != NULL) { |
1737 | 0 | zend_mm_free_huge(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1738 | 0 | } |
1739 | 0 | } else { |
1740 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1741 | 0 | int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1742 | 0 | zend_mm_page_info info = chunk->map[page_num]; |
1743 | |
|
1744 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1745 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1746 | 0 | zend_mm_free_small(heap, ptr, ZEND_MM_SRUN_BIN_NUM(info)); |
1747 | 0 | } else { |
1748 | | /* A freed large run has a zeroed map entry, so this also rejects double frees. */ |
1749 | 0 | ZEND_MM_CHECK(info & ZEND_MM_IS_LRUN, "zend_mm_heap corrupted"); |
1750 | |
|
1751 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(info); |
1752 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
1753 | 0 | zend_mm_free_large(heap, chunk, page_num, pages_count); |
1754 | 0 | } |
1755 | 0 | } |
1756 | 0 | } |
1757 | | |
1758 | | static size_t zend_mm_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1759 | 0 | { |
1760 | 0 | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1761 | |
|
1762 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1763 | 0 | return zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1764 | 0 | } else { |
1765 | 0 | zend_mm_chunk *chunk; |
1766 | | #if 0 && ZEND_DEBUG |
1767 | | zend_mm_debug_info *dbg = zend_mm_get_debug_info(heap, ptr); |
1768 | | return dbg->size; |
1769 | | #else |
1770 | 0 | int page_num; |
1771 | 0 | zend_mm_page_info info; |
1772 | |
|
1773 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1774 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1775 | 0 | info = chunk->map[page_num]; |
1776 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1777 | 0 | if (EXPECTED(info & ZEND_MM_IS_SRUN)) { |
1778 | 0 | return bin_data_size[ZEND_MM_SRUN_BIN_NUM(info)]; |
1779 | 0 | } else { |
1780 | 0 | ZEND_MM_CHECK(info & ZEND_MM_IS_LRUN, "zend_mm_heap corrupted"); |
1781 | 0 | return ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE; |
1782 | 0 | } |
1783 | 0 | #endif |
1784 | 0 | } |
1785 | 0 | } |
1786 | | |
1787 | | static zend_never_inline void *zend_mm_realloc_slow(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1788 | 0 | { |
1789 | 0 | void *ret; |
1790 | |
|
1791 | 0 | #if ZEND_MM_STAT |
1792 | 0 | do { |
1793 | 0 | size_t orig_peak = heap->peak; |
1794 | 0 | #endif |
1795 | 0 | ret = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1796 | 0 | if (EXPECTED(ret != NULL)) { |
1797 | 0 | memcpy(ret, ptr, copy_size); |
1798 | 0 | zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1799 | 0 | } else { |
1800 | | /* original block is left untouched */ |
1801 | 0 | } |
1802 | 0 | #if ZEND_MM_STAT |
1803 | 0 | if (EXPECTED(ret != NULL)) { |
1804 | 0 | heap->peak = MAX(orig_peak, heap->size); |
1805 | 0 | } |
1806 | 0 | } while (0); |
1807 | 0 | #endif |
1808 | 0 | return ret; |
1809 | 0 | } |
1810 | | |
1811 | | static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1812 | 0 | { |
1813 | 0 | size_t old_size; |
1814 | 0 | size_t new_size; |
1815 | 0 | #if ZEND_DEBUG |
1816 | 0 | size_t real_size; |
1817 | 0 | #endif |
1818 | |
|
1819 | 0 | old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1820 | 0 | #if ZEND_DEBUG |
1821 | 0 | real_size = size; |
1822 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1823 | 0 | #endif |
1824 | 0 | if (size > ZEND_MM_MAX_LARGE_SIZE) { |
1825 | 0 | #if ZEND_DEBUG |
1826 | 0 | size = real_size; |
1827 | 0 | #endif |
1828 | | #ifdef ZEND_WIN32 |
1829 | | /* On Windows we don't have ability to extend huge blocks in-place. |
1830 | | * We allocate them with 2MB size granularity, to avoid many |
1831 | | * reallocations when they are extended by small pieces |
1832 | | */ |
1833 | | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE)); |
1834 | | #else |
1835 | 0 | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, REAL_PAGE_SIZE); |
1836 | 0 | #endif |
1837 | 0 | if (new_size == old_size) { |
1838 | 0 | #if ZEND_DEBUG |
1839 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1840 | | #else |
1841 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1842 | | #endif |
1843 | 0 | return ptr; |
1844 | 0 | } else if (new_size < old_size) { |
1845 | | /* unmup tail */ |
1846 | 0 | if (zend_mm_chunk_truncate(heap, ptr, old_size, new_size)) { |
1847 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1848 | 0 | heap->real_size -= old_size - new_size; |
1849 | 0 | #endif |
1850 | 0 | #if ZEND_MM_STAT |
1851 | 0 | heap->size -= old_size - new_size; |
1852 | 0 | #endif |
1853 | 0 | #if ZEND_DEBUG |
1854 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1855 | | #else |
1856 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1857 | | #endif |
1858 | 0 | return ptr; |
1859 | 0 | } |
1860 | 0 | } else /* if (new_size > old_size) */ { |
1861 | 0 | if (UNEXPECTED(zend_mm_check_limit(heap, new_size - old_size, /* gc */ true, NULL ZEND_FILE_LINE_RELAY_CC) == FAILURE)) { |
1862 | 0 | return NULL; |
1863 | 0 | } |
1864 | | |
1865 | | /* try to map tail right after this block */ |
1866 | 0 | if (zend_mm_chunk_extend(heap, ptr, old_size, new_size)) { |
1867 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
1868 | 0 | heap->real_size += new_size - old_size; |
1869 | 0 | #endif |
1870 | 0 | #if ZEND_MM_STAT |
1871 | 0 | heap->real_peak = MAX(heap->real_peak, heap->real_size); |
1872 | 0 | heap->size += new_size - old_size; |
1873 | 0 | heap->peak = MAX(heap->peak, heap->size); |
1874 | 0 | #endif |
1875 | 0 | #if ZEND_DEBUG |
1876 | 0 | zend_mm_change_huge_block_size(heap, ptr, new_size, real_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1877 | | #else |
1878 | | zend_mm_change_huge_block_size(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1879 | | #endif |
1880 | 0 | return ptr; |
1881 | 0 | } |
1882 | 0 | } |
1883 | 0 | } |
1884 | | |
1885 | 0 | return zend_mm_realloc_slow(heap, ptr, size, MIN(old_size, copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1886 | 0 | } |
1887 | | |
1888 | | static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size, bool use_copy_size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
1889 | 0 | { |
1890 | 0 | size_t page_offset; |
1891 | 0 | size_t old_size; |
1892 | 0 | size_t new_size; |
1893 | 0 | void *ret; |
1894 | 0 | #if ZEND_DEBUG |
1895 | 0 | zend_mm_debug_info *dbg; |
1896 | 0 | #endif |
1897 | |
|
1898 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
1899 | 0 | if (UNEXPECTED(page_offset == 0)) { |
1900 | 0 | if (EXPECTED(ptr == NULL)) { |
1901 | 0 | return _zend_mm_alloc(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1902 | 0 | } else { |
1903 | 0 | return zend_mm_realloc_huge(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1904 | 0 | } |
1905 | 0 | } else { |
1906 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
1907 | 0 | int page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
1908 | 0 | zend_mm_page_info info = chunk->map[page_num]; |
1909 | 0 | #if ZEND_MM_HEAP_PROTECTION |
1910 | 0 | if (size < ZEND_MM_MIN_USEABLE_BIN_SIZE) { |
1911 | 0 | size = ZEND_MM_MIN_USEABLE_BIN_SIZE; |
1912 | 0 | } |
1913 | 0 | #endif /* ZEND_MM_HEAP_PROTECTION */ |
1914 | 0 | #if ZEND_DEBUG |
1915 | 0 | size_t real_size = size; |
1916 | |
|
1917 | 0 | size = ZEND_MM_ALIGNED_SIZE(size) + ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info)); |
1918 | 0 | #endif |
1919 | |
|
1920 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
1921 | 0 | if (info & ZEND_MM_IS_SRUN) { |
1922 | 0 | uint32_t old_bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
1923 | |
|
1924 | 0 | do { |
1925 | 0 | old_size = bin_data_size[old_bin_num]; |
1926 | | |
1927 | | /* Check if requested size fits into current bin */ |
1928 | 0 | if (size <= old_size) { |
1929 | | /* Check if truncation is necessary. A size that fits the |
1930 | | * bin below moves there, as efree_size() maps it to that bin. */ |
1931 | 0 | if (old_bin_num > 0 && size <= bin_data_size[old_bin_num - 1]) { |
1932 | | /* truncation */ |
1933 | 0 | ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1934 | 0 | if (UNEXPECTED(ret == NULL)) { |
1935 | 0 | goto failed; |
1936 | 0 | } |
1937 | 0 | copy_size = use_copy_size ? MIN(size, copy_size) : size; |
1938 | 0 | memcpy(ret, ptr, copy_size); |
1939 | 0 | zend_mm_free_small(heap, ptr, old_bin_num); |
1940 | 0 | } else { |
1941 | | /* reallocation in-place */ |
1942 | 0 | ret = ptr; |
1943 | 0 | } |
1944 | 0 | } else if (size <= ZEND_MM_MAX_SMALL_SIZE) { |
1945 | | /* small extension */ |
1946 | |
|
1947 | 0 | #if ZEND_MM_STAT |
1948 | 0 | do { |
1949 | 0 | size_t orig_peak = heap->peak; |
1950 | 0 | #endif |
1951 | 0 | ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
1952 | 0 | if (UNEXPECTED(ret == NULL)) { |
1953 | 0 | goto failed; |
1954 | 0 | } |
1955 | 0 | copy_size = use_copy_size ? MIN(old_size, copy_size) : old_size; |
1956 | 0 | memcpy(ret, ptr, copy_size); |
1957 | 0 | zend_mm_free_small(heap, ptr, old_bin_num); |
1958 | 0 | #if ZEND_MM_STAT |
1959 | 0 | heap->peak = MAX(orig_peak, heap->size); |
1960 | 0 | } while (0); |
1961 | 0 | #endif |
1962 | 0 | } else { |
1963 | | /* slow reallocation */ |
1964 | 0 | break; |
1965 | 0 | } |
1966 | | |
1967 | 0 | #if ZEND_DEBUG |
1968 | 0 | dbg = zend_mm_get_debug_info(heap, ret); |
1969 | 0 | dbg->size = real_size; |
1970 | 0 | dbg->filename = __zend_filename; |
1971 | 0 | dbg->orig_filename = __zend_orig_filename; |
1972 | 0 | dbg->lineno = __zend_lineno; |
1973 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1974 | 0 | #endif |
1975 | 0 | return ret; |
1976 | | |
1977 | 0 | failed: |
1978 | | /* original block is left untouched */ |
1979 | 0 | return NULL; |
1980 | 0 | } while (0); |
1981 | |
|
1982 | 0 | } else { |
1983 | 0 | ZEND_MM_CHECK(info & ZEND_MM_IS_LRUN, "zend_mm_heap corrupted"); |
1984 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
1985 | 0 | old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE; |
1986 | 0 | if (size > ZEND_MM_MAX_SMALL_SIZE && size <= ZEND_MM_MAX_LARGE_SIZE) { |
1987 | 0 | new_size = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE); |
1988 | 0 | if (new_size == old_size) { |
1989 | 0 | #if ZEND_DEBUG |
1990 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
1991 | 0 | dbg->size = real_size; |
1992 | 0 | dbg->filename = __zend_filename; |
1993 | 0 | dbg->orig_filename = __zend_orig_filename; |
1994 | 0 | dbg->lineno = __zend_lineno; |
1995 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
1996 | 0 | #endif |
1997 | 0 | return ptr; |
1998 | 0 | } else if (new_size < old_size) { |
1999 | | /* free tail pages */ |
2000 | 0 | int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE); |
2001 | 0 | int rest_pages_count = (int)((old_size - new_size) / ZEND_MM_PAGE_SIZE); |
2002 | |
|
2003 | 0 | #if ZEND_MM_STAT |
2004 | 0 | heap->size -= rest_pages_count * ZEND_MM_PAGE_SIZE; |
2005 | 0 | #endif |
2006 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count); |
2007 | 0 | chunk->free_pages += rest_pages_count; |
2008 | 0 | zend_mm_bitset_reset_range(chunk->free_map, page_num + new_pages_count, rest_pages_count); |
2009 | 0 | #if ZEND_DEBUG |
2010 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
2011 | 0 | dbg->size = real_size; |
2012 | 0 | dbg->filename = __zend_filename; |
2013 | 0 | dbg->orig_filename = __zend_orig_filename; |
2014 | 0 | dbg->lineno = __zend_lineno; |
2015 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
2016 | 0 | #endif |
2017 | 0 | return ptr; |
2018 | 0 | } else /* if (new_size > old_size) */ { |
2019 | 0 | int new_pages_count = (int)(new_size / ZEND_MM_PAGE_SIZE); |
2020 | 0 | int old_pages_count = (int)(old_size / ZEND_MM_PAGE_SIZE); |
2021 | | |
2022 | | /* try to allocate tail pages after this block */ |
2023 | 0 | if (page_num + new_pages_count <= ZEND_MM_PAGES && |
2024 | 0 | zend_mm_bitset_is_free_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count)) { |
2025 | 0 | #if ZEND_MM_STAT |
2026 | 0 | do { |
2027 | 0 | size_t size = heap->size + (new_size - old_size); |
2028 | 0 | size_t peak = MAX(heap->peak, size); |
2029 | 0 | heap->size = size; |
2030 | 0 | heap->peak = peak; |
2031 | 0 | } while (0); |
2032 | 0 | #endif |
2033 | 0 | chunk->free_pages -= new_pages_count - old_pages_count; |
2034 | 0 | zend_mm_bitset_set_range(chunk->free_map, page_num + old_pages_count, new_pages_count - old_pages_count); |
2035 | 0 | chunk->map[page_num] = ZEND_MM_LRUN(new_pages_count); |
2036 | 0 | #if ZEND_DEBUG |
2037 | 0 | dbg = zend_mm_get_debug_info(heap, ptr); |
2038 | 0 | dbg->size = real_size; |
2039 | 0 | dbg->filename = __zend_filename; |
2040 | 0 | dbg->orig_filename = __zend_orig_filename; |
2041 | 0 | dbg->lineno = __zend_lineno; |
2042 | 0 | dbg->orig_lineno = __zend_orig_lineno; |
2043 | 0 | #endif |
2044 | 0 | return ptr; |
2045 | 0 | } |
2046 | 0 | } |
2047 | 0 | } |
2048 | 0 | } |
2049 | 0 | #if ZEND_DEBUG |
2050 | 0 | size = real_size; |
2051 | 0 | #endif |
2052 | 0 | } |
2053 | | |
2054 | 0 | copy_size = MIN(old_size, copy_size); |
2055 | 0 | return zend_mm_realloc_slow(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2056 | 0 | } |
2057 | | |
2058 | | /*********************/ |
2059 | | /* Huge Runs (again) */ |
2060 | | /*********************/ |
2061 | | |
2062 | | /* Huge block metadata is allocated from the very heap it describes, so a heap |
2063 | | * overflow can reach it. size ends up as a munmap() length, where a corrupted |
2064 | | * value would unmap unrelated mappings, so bound it before use: a live block is |
2065 | | * page aligned and is still accounted for in real_size. */ |
2066 | | static zend_always_inline void zend_mm_check_huge_block_size(const zend_mm_heap *heap, size_t size) |
2067 | 0 | { |
2068 | 0 | ZEND_MM_CHECK(size != 0 && ZEND_MM_ALIGNED_OFFSET(size, REAL_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
2069 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2070 | 0 | ZEND_MM_CHECK(size <= heap->real_size, "zend_mm_heap corrupted"); |
2071 | | #else |
2072 | | (void)heap; |
2073 | | #endif |
2074 | 0 | } |
2075 | | |
2076 | | #if ZEND_DEBUG |
2077 | | static void zend_mm_add_huge_block(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2078 | | #else |
2079 | | static void zend_mm_add_huge_block(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2080 | | #endif |
2081 | 0 | { |
2082 | 0 | bool was_fallible = heap->fallible; |
2083 | 0 | heap->fallible = false; |
2084 | 0 | zend_mm_huge_list *list = (zend_mm_huge_list*)zend_mm_alloc_heap(heap, sizeof(zend_mm_huge_list) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2085 | 0 | list->ptr = ptr; |
2086 | 0 | list->size = size; |
2087 | 0 | list->next = heap->huge_list; |
2088 | 0 | list->prev = NULL; |
2089 | 0 | if (heap->huge_list) { |
2090 | 0 | heap->huge_list->prev = list; |
2091 | 0 | } |
2092 | 0 | #if ZEND_DEBUG |
2093 | 0 | list->dbg.size = dbg_size; |
2094 | 0 | list->dbg.filename = __zend_filename; |
2095 | 0 | list->dbg.orig_filename = __zend_orig_filename; |
2096 | 0 | list->dbg.lineno = __zend_lineno; |
2097 | 0 | list->dbg.orig_lineno = __zend_orig_lineno; |
2098 | 0 | #endif |
2099 | 0 | heap->huge_list = list; |
2100 | 0 | heap->fallible = was_fallible; |
2101 | 0 | } |
2102 | | |
2103 | | static size_t zend_mm_del_huge_block(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2104 | 0 | { |
2105 | 0 | zend_mm_huge_list *list = heap->huge_list; |
2106 | 0 | while (list != NULL) { |
2107 | 0 | if (list->ptr == ptr) { |
2108 | 0 | size_t size; |
2109 | | |
2110 | | /* Unlinking writes through these, so make sure they still point back |
2111 | | * at this block before trusting them. */ |
2112 | 0 | ZEND_MM_CHECK(list->prev ? list->prev->next == list : heap->huge_list == list, "zend_mm_heap corrupted"); |
2113 | 0 | ZEND_MM_CHECK(!list->next || list->next->prev == list, "zend_mm_heap corrupted"); |
2114 | |
|
2115 | 0 | if (list->prev) { |
2116 | 0 | list->prev->next = list->next; |
2117 | 0 | } else { |
2118 | 0 | heap->huge_list = list->next; |
2119 | 0 | } |
2120 | 0 | if (list->next) { |
2121 | 0 | list->next->prev = list->prev; |
2122 | 0 | } |
2123 | 0 | size = list->size; |
2124 | 0 | zend_mm_free_heap(heap, list ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2125 | 0 | return size; |
2126 | 0 | } |
2127 | 0 | list = list->next; |
2128 | 0 | } |
2129 | 0 | ZEND_MM_CHECK(0, "zend_mm_heap corrupted"); |
2130 | 0 | return 0; |
2131 | 0 | } |
2132 | | |
2133 | | static size_t zend_mm_get_huge_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2134 | 0 | { |
2135 | 0 | zend_mm_huge_list *list = heap->huge_list; |
2136 | 0 | while (list != NULL) { |
2137 | 0 | if (list->ptr == ptr) { |
2138 | 0 | zend_mm_check_huge_block_size(heap, list->size); |
2139 | 0 | return list->size; |
2140 | 0 | } |
2141 | 0 | list = list->next; |
2142 | 0 | } |
2143 | 0 | ZEND_MM_CHECK(0, "zend_mm_heap corrupted"); |
2144 | 0 | return 0; |
2145 | 0 | } |
2146 | | |
2147 | | #if ZEND_DEBUG |
2148 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size, size_t dbg_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2149 | | #else |
2150 | | static void zend_mm_change_huge_block_size(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2151 | | #endif |
2152 | 0 | { |
2153 | 0 | zend_mm_huge_list *list = heap->huge_list; |
2154 | 0 | while (list != NULL) { |
2155 | 0 | if (list->ptr == ptr) { |
2156 | 0 | list->size = size; |
2157 | 0 | #if ZEND_DEBUG |
2158 | 0 | list->dbg.size = dbg_size; |
2159 | 0 | list->dbg.filename = __zend_filename; |
2160 | 0 | list->dbg.orig_filename = __zend_orig_filename; |
2161 | 0 | list->dbg.lineno = __zend_lineno; |
2162 | 0 | list->dbg.orig_lineno = __zend_orig_lineno; |
2163 | 0 | #endif |
2164 | 0 | return; |
2165 | 0 | } |
2166 | 0 | list = list->next; |
2167 | 0 | } |
2168 | 0 | } |
2169 | | |
2170 | | static void *zend_mm_alloc_huge(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2171 | 0 | { |
2172 | | #ifdef ZEND_WIN32 |
2173 | | /* On Windows we don't have ability to extend huge blocks in-place. |
2174 | | * We allocate them with 2MB size granularity, to avoid many |
2175 | | * reallocations when they are extended by small pieces |
2176 | | */ |
2177 | | size_t alignment = MAX(REAL_PAGE_SIZE, ZEND_MM_CHUNK_SIZE); |
2178 | | #else |
2179 | 0 | size_t alignment = REAL_PAGE_SIZE; |
2180 | 0 | #endif |
2181 | 0 | size_t new_size = ZEND_MM_ALIGNED_SIZE_EX(size, alignment); |
2182 | 0 | void *ptr; |
2183 | |
|
2184 | 0 | if (UNEXPECTED(new_size < size)) { |
2185 | 0 | zend_mm_delayed_error_or_panic(heap->fallible, "Possible integer overflow in memory allocation (%zu + %zu)", size, alignment); |
2186 | 0 | return NULL; |
2187 | 0 | } |
2188 | | |
2189 | 0 | if (UNEXPECTED(zend_mm_check_limit(heap, new_size, /* gc */ true, NULL ZEND_FILE_LINE_RELAY_CC) == FAILURE)) { |
2190 | 0 | return NULL; |
2191 | 0 | } |
2192 | | |
2193 | 0 | ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE); |
2194 | 0 | if (UNEXPECTED(ptr == NULL)) { |
2195 | | /* insufficient memory */ |
2196 | 0 | if (zend_mm_gc(heap) && |
2197 | 0 | (ptr = zend_mm_chunk_alloc(heap, new_size, ZEND_MM_CHUNK_SIZE)) != NULL) { |
2198 | | /* pass */ |
2199 | 0 | } else { |
2200 | 0 | zend_out_of_memory(); |
2201 | 0 | return NULL; |
2202 | 0 | } |
2203 | 0 | } |
2204 | 0 | #if ZEND_DEBUG |
2205 | 0 | zend_mm_add_huge_block(heap, ptr, new_size, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2206 | | #else |
2207 | | zend_mm_add_huge_block(heap, ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2208 | | #endif |
2209 | 0 | #if ZEND_MM_STAT |
2210 | 0 | do { |
2211 | 0 | size_t size = heap->real_size + new_size; |
2212 | 0 | size_t peak = MAX(heap->real_peak, size); |
2213 | 0 | heap->real_size = size; |
2214 | 0 | heap->real_peak = peak; |
2215 | 0 | } while (0); |
2216 | 0 | do { |
2217 | 0 | size_t size = heap->size + new_size; |
2218 | 0 | size_t peak = MAX(heap->peak, size); |
2219 | 0 | heap->size = size; |
2220 | 0 | heap->peak = peak; |
2221 | 0 | } while (0); |
2222 | | #elif ZEND_MM_LIMIT |
2223 | | heap->real_size += new_size; |
2224 | | #endif |
2225 | 0 | return ptr; |
2226 | 0 | } |
2227 | | |
2228 | | static void zend_mm_free_huge(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2229 | 0 | { |
2230 | 0 | size_t size; |
2231 | |
|
2232 | 0 | ZEND_MM_CHECK(ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE) == 0, "zend_mm_heap corrupted"); |
2233 | 0 | size = zend_mm_del_huge_block(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2234 | 0 | zend_mm_check_huge_block_size(heap, size); |
2235 | 0 | zend_mm_chunk_free(heap, ptr, size); |
2236 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2237 | 0 | heap->real_size -= size; |
2238 | 0 | #endif |
2239 | 0 | #if ZEND_MM_STAT |
2240 | 0 | heap->size -= size; |
2241 | 0 | #endif |
2242 | 0 | } |
2243 | | |
2244 | | /******************/ |
2245 | | /* Initialization */ |
2246 | | /******************/ |
2247 | | |
2248 | | static void zend_mm_refresh_key(zend_mm_heap *heap) |
2249 | 0 | { |
2250 | 0 | zend_random_bytes_insecure(&heap->rand_state, &heap->shadow_key, sizeof(heap->shadow_key)); |
2251 | 0 | } |
2252 | | |
2253 | | static void zend_mm_init_key(zend_mm_heap *heap) |
2254 | 0 | { |
2255 | 0 | memset(&heap->rand_state, 0, sizeof(heap->rand_state)); |
2256 | 0 | zend_mm_refresh_key(heap); |
2257 | 0 | } |
2258 | | |
2259 | | ZEND_API void zend_mm_refresh_key_child(zend_mm_heap *heap) |
2260 | 0 | { |
2261 | 0 | uintptr_t old_key = heap->shadow_key; |
2262 | |
|
2263 | 0 | zend_mm_init_key(heap); |
2264 | | |
2265 | | /* Update shadow pointers with new key */ |
2266 | 0 | for (uint32_t i = 0; i < ZEND_MM_BINS; i++) { |
2267 | 0 | zend_mm_free_slot *slot = heap->free_slot[i]; |
2268 | 0 | if (!slot) { |
2269 | 0 | continue; |
2270 | 0 | } |
2271 | 0 | zend_mm_free_slot *next; |
2272 | 0 | while ((next = slot->next_free_slot)) { |
2273 | 0 | zend_mm_free_slot **shadow_addr = ZEND_MM_FREE_SLOT_PTR_SHADOW_ADDR(slot, i); |
2274 | 0 | if (UNEXPECTED(next != zend_mm_decode_free_slot_key(old_key, shadow_addr, *shadow_addr))) { |
2275 | 0 | zend_mm_panic("zend_mm_heap corrupted"); |
2276 | 0 | } |
2277 | 0 | zend_mm_set_next_free_slot(heap, i, slot, next); |
2278 | 0 | slot = next; |
2279 | 0 | } |
2280 | 0 | } |
2281 | |
|
2282 | 0 | zend_mm_rekey_cached_chunks(heap, old_key); |
2283 | |
|
2284 | 0 | #if ZEND_DEBUG |
2285 | 0 | heap->pid = getpid(); |
2286 | 0 | #endif |
2287 | 0 | } |
2288 | | |
2289 | | static zend_mm_heap *zend_mm_init(void) |
2290 | 0 | { |
2291 | 0 | zend_mm_chunk *chunk = (zend_mm_chunk*)zend_mm_chunk_alloc_int(ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
2292 | 0 | zend_mm_heap *heap; |
2293 | |
|
2294 | 0 | if (UNEXPECTED(chunk == NULL)) { |
2295 | 0 | #if ZEND_MM_ERROR |
2296 | 0 | fprintf(stderr, "Can't initialize heap\n"); |
2297 | 0 | #endif |
2298 | 0 | return NULL; |
2299 | 0 | } |
2300 | 0 | heap = &chunk->heap_slot; |
2301 | 0 | chunk->heap = heap; |
2302 | 0 | chunk->next = chunk; |
2303 | 0 | chunk->prev = chunk; |
2304 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
2305 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
2306 | 0 | chunk->num = 0; |
2307 | 0 | chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1; |
2308 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
2309 | 0 | heap->main_chunk = chunk; |
2310 | 0 | heap->cached_chunks = NULL; |
2311 | 0 | heap->chunks_count = 1; |
2312 | 0 | heap->peak_chunks_count = 1; |
2313 | 0 | heap->cached_chunks_count = 0; |
2314 | 0 | heap->avg_chunks_count = 1.0; |
2315 | 0 | heap->last_chunks_delete_boundary = 0; |
2316 | 0 | heap->last_chunks_delete_count = 0; |
2317 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2318 | 0 | heap->real_size = ZEND_MM_CHUNK_SIZE; |
2319 | 0 | #endif |
2320 | 0 | #if ZEND_MM_STAT |
2321 | 0 | heap->real_peak = ZEND_MM_CHUNK_SIZE; |
2322 | 0 | heap->size = 0; |
2323 | 0 | heap->peak = 0; |
2324 | 0 | #endif |
2325 | 0 | zend_mm_init_key(heap); |
2326 | 0 | #if ZEND_MM_LIMIT |
2327 | 0 | heap->limit = ZEND_MM_LIMIT_MAX; |
2328 | 0 | heap->hard_limit = ZEND_MM_LIMIT_MAX; |
2329 | 0 | heap->hard_limit_percent = -1; |
2330 | 0 | heap->overflow = 0; |
2331 | 0 | #endif |
2332 | 0 | heap->fallible = false; |
2333 | 0 | #if ZEND_MM_CUSTOM |
2334 | 0 | heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE; |
2335 | 0 | #endif |
2336 | 0 | #if ZEND_MM_STORAGE |
2337 | 0 | heap->storage = NULL; |
2338 | 0 | #endif |
2339 | 0 | heap->huge_list = NULL; |
2340 | 0 | #if ZEND_DEBUG |
2341 | 0 | heap->pid = getpid(); |
2342 | 0 | #endif |
2343 | 0 | return heap; |
2344 | 0 | } |
2345 | | |
2346 | | ZEND_API size_t zend_mm_gc(zend_mm_heap *heap) |
2347 | 0 | { |
2348 | 0 | zend_mm_free_slot *p, *q; |
2349 | 0 | zend_mm_chunk *chunk; |
2350 | 0 | size_t page_offset; |
2351 | 0 | int page_num; |
2352 | 0 | zend_mm_page_info info; |
2353 | 0 | uint32_t i, free_counter; |
2354 | 0 | bool has_free_pages; |
2355 | 0 | size_t collected = 0; |
2356 | |
|
2357 | 0 | #if ZEND_MM_CUSTOM |
2358 | 0 | if (heap->use_custom_heap) { |
2359 | 0 | size_t (*gc)(void) = heap->custom_heap._gc; |
2360 | 0 | if (gc) { |
2361 | 0 | return gc(); |
2362 | 0 | } |
2363 | 0 | return 0; |
2364 | 0 | } |
2365 | 0 | #endif |
2366 | | |
2367 | 0 | for (i = 0; i < ZEND_MM_BINS; i++) { |
2368 | 0 | has_free_pages = false; |
2369 | 0 | p = heap->free_slot[i]; |
2370 | 0 | while (p != NULL) { |
2371 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(p, ZEND_MM_CHUNK_SIZE); |
2372 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
2373 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(p, ZEND_MM_CHUNK_SIZE); |
2374 | 0 | ZEND_ASSERT(page_offset != 0); |
2375 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
2376 | 0 | info = chunk->map[page_num]; |
2377 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
2378 | 0 | if (info & ZEND_MM_IS_LRUN) { |
2379 | 0 | page_num -= ZEND_MM_NRUN_OFFSET(info); |
2380 | 0 | info = chunk->map[page_num]; |
2381 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
2382 | 0 | ZEND_ASSERT(!(info & ZEND_MM_IS_LRUN)); |
2383 | 0 | } |
2384 | 0 | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(info) == i); |
2385 | 0 | free_counter = ZEND_MM_SRUN_FREE_COUNTER(info) + 1; |
2386 | | /* A slot can only be on the free list once, so exceeding the number |
2387 | | * of slots in the page means the list is corrupted, most likely a |
2388 | | * cycle, which would loop here forever. */ |
2389 | 0 | ZEND_MM_CHECK(free_counter <= bin_elements[i], "zend_mm_heap corrupted"); |
2390 | 0 | if (free_counter == bin_elements[i]) { |
2391 | 0 | has_free_pages = true; |
2392 | 0 | } |
2393 | 0 | chunk->map[page_num] = ZEND_MM_SRUN_EX(i, free_counter); |
2394 | 0 | p = zend_mm_get_next_free_slot(heap, i, p); |
2395 | 0 | } |
2396 | |
|
2397 | 0 | if (!has_free_pages) { |
2398 | 0 | continue; |
2399 | 0 | } |
2400 | | |
2401 | 0 | q = (zend_mm_free_slot*)&heap->free_slot[i]; |
2402 | 0 | p = q->next_free_slot; |
2403 | 0 | while (p != NULL) { |
2404 | 0 | chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(p, ZEND_MM_CHUNK_SIZE); |
2405 | 0 | ZEND_MM_CHECK(chunk->heap == heap, "zend_mm_heap corrupted"); |
2406 | 0 | page_offset = ZEND_MM_ALIGNED_OFFSET(p, ZEND_MM_CHUNK_SIZE); |
2407 | 0 | ZEND_ASSERT(page_offset != 0); |
2408 | 0 | page_num = (int)(page_offset / ZEND_MM_PAGE_SIZE); |
2409 | 0 | info = chunk->map[page_num]; |
2410 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
2411 | 0 | if (info & ZEND_MM_IS_LRUN) { |
2412 | 0 | page_num -= ZEND_MM_NRUN_OFFSET(info); |
2413 | 0 | info = chunk->map[page_num]; |
2414 | 0 | ZEND_ASSERT(info & ZEND_MM_IS_SRUN); |
2415 | 0 | ZEND_ASSERT(!(info & ZEND_MM_IS_LRUN)); |
2416 | 0 | } |
2417 | 0 | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(info) == i); |
2418 | 0 | if (ZEND_MM_SRUN_FREE_COUNTER(info) == bin_elements[i]) { |
2419 | | /* remove from cache */ |
2420 | 0 | p = zend_mm_get_next_free_slot(heap, i, p); |
2421 | 0 | if (q == (zend_mm_free_slot*)&heap->free_slot[i]) { |
2422 | 0 | q->next_free_slot = p; |
2423 | 0 | } else { |
2424 | 0 | zend_mm_set_next_free_slot(heap, i, q, p); |
2425 | 0 | } |
2426 | 0 | } else { |
2427 | 0 | q = p; |
2428 | 0 | if (q == (zend_mm_free_slot*)&heap->free_slot[i]) { |
2429 | 0 | p = q->next_free_slot; |
2430 | 0 | } else { |
2431 | 0 | p = zend_mm_get_next_free_slot(heap, i, q); |
2432 | 0 | } |
2433 | 0 | } |
2434 | 0 | } |
2435 | 0 | } |
2436 | | |
2437 | 0 | chunk = heap->main_chunk; |
2438 | 0 | do { |
2439 | 0 | i = ZEND_MM_FIRST_PAGE; |
2440 | 0 | while (i < chunk->free_tail) { |
2441 | 0 | if (zend_mm_bitset_is_set(chunk->free_map, i)) { |
2442 | 0 | info = chunk->map[i]; |
2443 | 0 | if (info & ZEND_MM_IS_SRUN) { |
2444 | 0 | uint32_t bin_num = ZEND_MM_SRUN_BIN_NUM(info); |
2445 | 0 | int pages_count = bin_pages[bin_num]; |
2446 | |
|
2447 | 0 | if (ZEND_MM_SRUN_FREE_COUNTER(info) == bin_elements[bin_num]) { |
2448 | | /* all elements are free */ |
2449 | 0 | zend_mm_free_pages_ex(heap, chunk, i, pages_count, 0); |
2450 | 0 | collected += pages_count; |
2451 | 0 | } else { |
2452 | | /* reset counter */ |
2453 | 0 | chunk->map[i] = ZEND_MM_SRUN(bin_num); |
2454 | 0 | } |
2455 | 0 | i += bin_pages[bin_num]; |
2456 | 0 | } else /* if (info & ZEND_MM_IS_LRUN) */ { |
2457 | 0 | i += ZEND_MM_LRUN_PAGES(info); |
2458 | 0 | } |
2459 | 0 | } else { |
2460 | 0 | i++; |
2461 | 0 | } |
2462 | 0 | } |
2463 | 0 | if (chunk->free_pages == ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE && chunk != heap->main_chunk) { |
2464 | 0 | zend_mm_chunk *next_chunk = chunk->next; |
2465 | |
|
2466 | 0 | zend_mm_delete_chunk(heap, chunk); |
2467 | 0 | chunk = next_chunk; |
2468 | 0 | } else { |
2469 | 0 | chunk = chunk->next; |
2470 | 0 | } |
2471 | 0 | } while (chunk != heap->main_chunk); |
2472 | |
|
2473 | 0 | return collected * ZEND_MM_PAGE_SIZE; |
2474 | 0 | } |
2475 | | |
2476 | | #if ZEND_DEBUG |
2477 | | /******************/ |
2478 | | /* Leak detection */ |
2479 | | /******************/ |
2480 | | |
2481 | | static zend_long zend_mm_find_leaks_small(zend_mm_chunk *p, uint32_t i, uint32_t j, zend_leak_info *leak) |
2482 | 0 | { |
2483 | 0 | bool empty = true; |
2484 | 0 | zend_long count = 0; |
2485 | 0 | uint32_t bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2486 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * (j + 1) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2487 | |
|
2488 | 0 | while (j < bin_elements[bin_num]) { |
2489 | 0 | if (dbg->size != 0) { |
2490 | 0 | if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) { |
2491 | 0 | count++; |
2492 | 0 | dbg->size = 0; |
2493 | 0 | dbg->filename = NULL; |
2494 | 0 | dbg->lineno = 0; |
2495 | 0 | } else { |
2496 | 0 | empty = false; |
2497 | 0 | } |
2498 | 0 | } |
2499 | 0 | j++; |
2500 | 0 | dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]); |
2501 | 0 | } |
2502 | 0 | if (empty) { |
2503 | 0 | zend_mm_bitset_reset_range(p->free_map, i, bin_pages[bin_num]); |
2504 | 0 | } |
2505 | 0 | return count; |
2506 | 0 | } |
2507 | | |
2508 | | static zend_long zend_mm_find_leaks(zend_mm_heap *heap, zend_mm_chunk *p, uint32_t i, zend_leak_info *leak) |
2509 | 0 | { |
2510 | 0 | zend_long count = 0; |
2511 | |
|
2512 | 0 | do { |
2513 | 0 | while (i < p->free_tail) { |
2514 | 0 | if (zend_mm_bitset_is_set(p->free_map, i)) { |
2515 | 0 | if (p->map[i] & ZEND_MM_IS_SRUN) { |
2516 | 0 | uint32_t bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2517 | 0 | count += zend_mm_find_leaks_small(p, i, 0, leak); |
2518 | 0 | i += bin_pages[bin_num]; |
2519 | 0 | } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ { |
2520 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]); |
2521 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * (i + pages_count) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2522 | |
|
2523 | 0 | if (dbg->filename == leak->filename && dbg->lineno == leak->lineno) { |
2524 | 0 | count++; |
2525 | 0 | } |
2526 | 0 | zend_mm_bitset_reset_range(p->free_map, i, pages_count); |
2527 | 0 | i += pages_count; |
2528 | 0 | } |
2529 | 0 | } else { |
2530 | 0 | i++; |
2531 | 0 | } |
2532 | 0 | } |
2533 | 0 | p = p->next; |
2534 | 0 | i = ZEND_MM_FIRST_PAGE; |
2535 | 0 | } while (p != heap->main_chunk); |
2536 | 0 | return count; |
2537 | 0 | } |
2538 | | |
2539 | | static zend_long zend_mm_find_leaks_huge(zend_mm_heap *heap, zend_mm_huge_list *list) |
2540 | 0 | { |
2541 | 0 | zend_long count = 0; |
2542 | 0 | zend_mm_huge_list *prev = list; |
2543 | 0 | zend_mm_huge_list *p = list->next; |
2544 | |
|
2545 | 0 | while (p) { |
2546 | 0 | if (p->dbg.filename == list->dbg.filename && p->dbg.lineno == list->dbg.lineno) { |
2547 | 0 | prev->next = p->next; |
2548 | 0 | if (p->next) { |
2549 | 0 | p->next->prev = prev; |
2550 | 0 | } |
2551 | 0 | zend_mm_chunk_free(heap, p->ptr, p->size); |
2552 | 0 | zend_mm_free_heap(heap, p, NULL, 0, NULL, 0); |
2553 | 0 | count++; |
2554 | 0 | } else { |
2555 | 0 | prev = p; |
2556 | 0 | } |
2557 | 0 | p = prev->next; |
2558 | 0 | } |
2559 | |
|
2560 | 0 | return count; |
2561 | 0 | } |
2562 | | |
2563 | | static void zend_mm_check_leaks(zend_mm_heap *heap) |
2564 | 0 | { |
2565 | 0 | zend_mm_huge_list *list; |
2566 | 0 | zend_mm_chunk *p; |
2567 | 0 | zend_leak_info leak; |
2568 | 0 | zend_long repeated = 0; |
2569 | 0 | uint32_t total = 0; |
2570 | 0 | uint32_t i, j; |
2571 | | |
2572 | | /* find leaked huge blocks and free them */ |
2573 | 0 | list = heap->huge_list; |
2574 | 0 | while (list) { |
2575 | 0 | zend_mm_huge_list *q = list; |
2576 | |
|
2577 | 0 | leak.addr = list->ptr; |
2578 | 0 | leak.size = list->dbg.size; |
2579 | 0 | leak.filename = list->dbg.filename; |
2580 | 0 | leak.orig_filename = list->dbg.orig_filename; |
2581 | 0 | leak.lineno = list->dbg.lineno; |
2582 | 0 | leak.orig_lineno = list->dbg.orig_lineno; |
2583 | |
|
2584 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2585 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2586 | 0 | repeated = zend_mm_find_leaks_huge(heap, list); |
2587 | 0 | total += 1 + repeated; |
2588 | 0 | if (repeated) { |
2589 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated); |
2590 | 0 | } |
2591 | |
|
2592 | 0 | heap->huge_list = list = list->next; |
2593 | 0 | if (list) { |
2594 | 0 | list->prev = NULL; |
2595 | 0 | } |
2596 | 0 | zend_mm_chunk_free(heap, q->ptr, q->size); |
2597 | 0 | zend_mm_free_heap(heap, q, NULL, 0, NULL, 0); |
2598 | 0 | } |
2599 | | |
2600 | | /* for each chunk */ |
2601 | 0 | p = heap->main_chunk; |
2602 | 0 | do { |
2603 | 0 | i = ZEND_MM_FIRST_PAGE; |
2604 | 0 | while (i < p->free_tail) { |
2605 | 0 | if (zend_mm_bitset_is_set(p->free_map, i)) { |
2606 | 0 | if (p->map[i] & ZEND_MM_IS_SRUN) { |
2607 | 0 | uint32_t bin_num = ZEND_MM_SRUN_BIN_NUM(p->map[i]); |
2608 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2609 | |
|
2610 | 0 | j = 0; |
2611 | 0 | while (j < bin_elements[bin_num]) { |
2612 | 0 | if (dbg->size != 0) { |
2613 | 0 | leak.addr = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * i + bin_data_size[bin_num] * j); |
2614 | 0 | leak.size = dbg->size; |
2615 | 0 | leak.filename = dbg->filename; |
2616 | 0 | leak.orig_filename = dbg->orig_filename; |
2617 | 0 | leak.lineno = dbg->lineno; |
2618 | 0 | leak.orig_lineno = dbg->orig_lineno; |
2619 | |
|
2620 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2621 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2622 | |
|
2623 | 0 | dbg->size = 0; |
2624 | 0 | dbg->filename = NULL; |
2625 | 0 | dbg->lineno = 0; |
2626 | |
|
2627 | 0 | repeated = zend_mm_find_leaks_small(p, i, j + 1, &leak) + |
2628 | 0 | zend_mm_find_leaks(heap, p, i + bin_pages[bin_num], &leak); |
2629 | 0 | total += 1 + repeated; |
2630 | 0 | if (repeated) { |
2631 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated); |
2632 | 0 | } |
2633 | 0 | } |
2634 | 0 | dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]); |
2635 | 0 | j++; |
2636 | 0 | } |
2637 | 0 | i += bin_pages[bin_num]; |
2638 | 0 | } else /* if (p->map[i] & ZEND_MM_IS_LRUN) */ { |
2639 | 0 | int pages_count = ZEND_MM_LRUN_PAGES(p->map[i]); |
2640 | 0 | zend_mm_debug_info *dbg = (zend_mm_debug_info*)((char*)p + ZEND_MM_PAGE_SIZE * (i + pages_count) - ZEND_MM_ALIGNED_SIZE(sizeof(zend_mm_debug_info))); |
2641 | |
|
2642 | 0 | leak.addr = (void*)((char*)p + ZEND_MM_PAGE_SIZE * i); |
2643 | 0 | leak.size = dbg->size; |
2644 | 0 | leak.filename = dbg->filename; |
2645 | 0 | leak.orig_filename = dbg->orig_filename; |
2646 | 0 | leak.lineno = dbg->lineno; |
2647 | 0 | leak.orig_lineno = dbg->orig_lineno; |
2648 | |
|
2649 | 0 | zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL); |
2650 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, &leak); |
2651 | |
|
2652 | 0 | zend_mm_bitset_reset_range(p->free_map, i, pages_count); |
2653 | |
|
2654 | 0 | repeated = zend_mm_find_leaks(heap, p, i + pages_count, &leak); |
2655 | 0 | total += 1 + repeated; |
2656 | 0 | if (repeated) { |
2657 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated); |
2658 | 0 | } |
2659 | 0 | i += pages_count; |
2660 | 0 | } |
2661 | 0 | } else { |
2662 | 0 | i++; |
2663 | 0 | } |
2664 | 0 | } |
2665 | 0 | p = p->next; |
2666 | 0 | } while (p != heap->main_chunk); |
2667 | 0 | if (total) { |
2668 | 0 | zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &total); |
2669 | 0 | } |
2670 | 0 | } |
2671 | | #endif |
2672 | | |
2673 | | #if ZEND_MM_CUSTOM |
2674 | | static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
2675 | | static void tracked_free_all(zend_mm_heap *heap); |
2676 | | static void *poison_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
2677 | | |
2678 | | static void zend_mm_check_freelists(zend_mm_heap *heap) |
2679 | 0 | { |
2680 | 0 | for (uint32_t bin_num = 0; bin_num < ZEND_MM_BINS; bin_num++) { |
2681 | 0 | zend_mm_free_slot *slot = heap->free_slot[bin_num]; |
2682 | 0 | while (slot) { |
2683 | 0 | slot = zend_mm_get_next_free_slot(heap, bin_num, slot); |
2684 | 0 | } |
2685 | 0 | } |
2686 | 0 | } |
2687 | | #endif |
2688 | | |
2689 | | ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, bool full, bool silent) |
2690 | 306k | { |
2691 | 306k | zend_mm_chunk *p; |
2692 | 306k | zend_mm_huge_list *list; |
2693 | | |
2694 | 306k | #if ZEND_MM_CUSTOM |
2695 | 306k | if (heap->use_custom_heap) { |
2696 | 306k | if (heap->custom_heap._malloc == tracked_malloc) { |
2697 | 162k | if (silent) { |
2698 | 6.73k | tracked_free_all(heap); |
2699 | 6.73k | } |
2700 | 162k | heap->max_allocs = 0; |
2701 | 162k | zend_hash_clean(heap->tracked_allocs); |
2702 | 162k | if (full) { |
2703 | 0 | zend_hash_destroy(heap->tracked_allocs); |
2704 | 0 | free(heap->tracked_allocs); |
2705 | | /* Make sure the heap free below does not use tracked_free(). */ |
2706 | 0 | heap->custom_heap._free = __zend_free; |
2707 | 0 | } |
2708 | 162k | #if ZEND_MM_STAT |
2709 | 162k | heap->size = 0; |
2710 | 162k | heap->real_size = 0; |
2711 | 162k | #endif |
2712 | 162k | } |
2713 | | |
2714 | 306k | void (*shutdown)(bool, bool) = heap->custom_heap._shutdown; |
2715 | | |
2716 | 306k | if (full) { |
2717 | 0 | heap->custom_heap._free(heap ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC); |
2718 | 0 | } |
2719 | | |
2720 | 306k | if (shutdown) { |
2721 | 0 | shutdown(full, silent); |
2722 | 0 | } |
2723 | | |
2724 | 306k | return; |
2725 | 306k | } |
2726 | 0 | #endif |
2727 | | |
2728 | 0 | #if ZEND_DEBUG |
2729 | 0 | if (!silent) { |
2730 | 0 | char *tmp = getenv("ZEND_ALLOC_PRINT_LEAKS"); |
2731 | 0 | if (!tmp || ZEND_ATOL(tmp)) { |
2732 | 0 | zend_mm_check_leaks(heap); |
2733 | 0 | } |
2734 | 0 | } |
2735 | 0 | #endif |
2736 | | |
2737 | | /* free huge blocks */ |
2738 | 0 | list = heap->huge_list; |
2739 | 0 | heap->huge_list = NULL; |
2740 | 0 | while (list) { |
2741 | 0 | zend_mm_huge_list *q = list; |
2742 | 0 | list = list->next; |
2743 | 0 | zend_mm_check_huge_block_size(heap, q->size); |
2744 | 0 | zend_mm_chunk_free(heap, q->ptr, q->size); |
2745 | 0 | } |
2746 | | |
2747 | | /* move all chunks except of the first one into the cache */ |
2748 | 0 | p = heap->main_chunk->next; |
2749 | 0 | while (p != heap->main_chunk) { |
2750 | 0 | zend_mm_chunk *q = p->next; |
2751 | 0 | zend_mm_set_next_cached_chunk(heap, p, heap->cached_chunks); |
2752 | 0 | heap->cached_chunks = p; |
2753 | 0 | p = q; |
2754 | 0 | heap->chunks_count--; |
2755 | 0 | heap->cached_chunks_count++; |
2756 | 0 | } |
2757 | |
|
2758 | 0 | if (full) { |
2759 | | /* free all cached chunks */ |
2760 | 0 | while (heap->cached_chunks) { |
2761 | 0 | p = heap->cached_chunks; |
2762 | 0 | heap->cached_chunks = zend_mm_get_next_cached_chunk(heap, p); |
2763 | 0 | zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE); |
2764 | 0 | } |
2765 | | /* free the first chunk */ |
2766 | 0 | zend_mm_chunk_free(heap, heap->main_chunk, ZEND_MM_CHUNK_SIZE); |
2767 | 0 | } else { |
2768 | | /* free some cached chunks to keep average count */ |
2769 | 0 | heap->avg_chunks_count = (heap->avg_chunks_count + (double)heap->peak_chunks_count) / 2.0; |
2770 | 0 | while ((double)heap->cached_chunks_count + 0.9 > heap->avg_chunks_count && |
2771 | 0 | heap->cached_chunks) { |
2772 | 0 | p = heap->cached_chunks; |
2773 | 0 | heap->cached_chunks = zend_mm_get_next_cached_chunk(heap, p); |
2774 | 0 | zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE); |
2775 | 0 | heap->cached_chunks_count--; |
2776 | 0 | } |
2777 | | /* clear cached chunks */ |
2778 | 0 | p = heap->cached_chunks; |
2779 | 0 | while (p != NULL) { |
2780 | 0 | zend_mm_chunk *q = zend_mm_get_next_cached_chunk(heap, p); |
2781 | 0 | memset(p, 0, sizeof(zend_mm_chunk)); |
2782 | 0 | zend_mm_set_next_cached_chunk(heap, p, q); |
2783 | 0 | p = q; |
2784 | 0 | } |
2785 | | |
2786 | | /* reinitialize the first chunk and heap */ |
2787 | 0 | p = heap->main_chunk; |
2788 | 0 | p->heap = &p->heap_slot; |
2789 | 0 | p->next = p; |
2790 | 0 | p->prev = p; |
2791 | 0 | p->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
2792 | 0 | p->free_tail = ZEND_MM_FIRST_PAGE; |
2793 | 0 | p->num = 0; |
2794 | |
|
2795 | 0 | #if ZEND_MM_STAT |
2796 | 0 | heap->size = heap->peak = 0; |
2797 | 0 | #endif |
2798 | 0 | memset(heap->free_slot, 0, sizeof(heap->free_slot)); |
2799 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
2800 | 0 | heap->real_size = (heap->cached_chunks_count + 1) * ZEND_MM_CHUNK_SIZE; |
2801 | 0 | #endif |
2802 | 0 | #if ZEND_MM_STAT |
2803 | 0 | heap->real_peak = (heap->cached_chunks_count + 1) * ZEND_MM_CHUNK_SIZE; |
2804 | 0 | #endif |
2805 | 0 | heap->chunks_count = 1; |
2806 | 0 | heap->peak_chunks_count = 1; |
2807 | 0 | heap->last_chunks_delete_boundary = 0; |
2808 | 0 | heap->last_chunks_delete_count = 0; |
2809 | |
|
2810 | 0 | memset(p->free_map, 0, sizeof(p->free_map) + sizeof(p->map)); |
2811 | 0 | p->free_map[0] = (1L << ZEND_MM_FIRST_PAGE) - 1; |
2812 | 0 | p->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
2813 | |
|
2814 | 0 | #if ZEND_DEBUG |
2815 | 0 | ZEND_ASSERT(getpid() == heap->pid |
2816 | 0 | && "heap was re-used without calling zend_mm_refresh_key_child() after a fork"); |
2817 | 0 | #endif |
2818 | |
|
2819 | 0 | uintptr_t old_key = heap->shadow_key; |
2820 | |
|
2821 | 0 | zend_mm_refresh_key(heap); |
2822 | | |
2823 | | /* Cached chunks outlive the request, so re-encode their links */ |
2824 | 0 | zend_mm_rekey_cached_chunks(heap, old_key); |
2825 | 0 | } |
2826 | 0 | } |
2827 | | |
2828 | | /**************/ |
2829 | | /* PUBLIC API */ |
2830 | | /**************/ |
2831 | | |
2832 | | ZEND_API void* ZEND_FASTCALL _zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2833 | 0 | { |
2834 | 0 | return zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2835 | 0 | } |
2836 | | |
2837 | | ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2838 | 0 | { |
2839 | 0 | zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2840 | 0 | } |
2841 | | |
2842 | | void* ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2843 | 0 | { |
2844 | 0 | return zend_mm_realloc_heap(heap, ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2845 | 0 | } |
2846 | | |
2847 | | void* ZEND_FASTCALL _zend_mm_realloc2(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2848 | 0 | { |
2849 | 0 | return zend_mm_realloc_heap(heap, ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2850 | 0 | } |
2851 | | |
2852 | | ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2853 | 0 | { |
2854 | 0 | #if ZEND_MM_CUSTOM |
2855 | 0 | if (UNEXPECTED(heap->use_custom_heap)) { |
2856 | 0 | if (heap->custom_heap._malloc == tracked_malloc) { |
2857 | 0 | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
2858 | 0 | zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h); |
2859 | 0 | if (size_zv) { |
2860 | 0 | return Z_LVAL_P(size_zv); |
2861 | 0 | } |
2862 | 0 | } else if (heap->custom_heap._malloc != poison_malloc) { |
2863 | 0 | return 0; |
2864 | 0 | } |
2865 | 0 | } |
2866 | 0 | #endif |
2867 | 0 | return zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2868 | 0 | } |
2869 | | |
2870 | | /**********************/ |
2871 | | /* Allocation Manager */ |
2872 | | /**********************/ |
2873 | | |
2874 | | typedef struct _zend_alloc_globals { |
2875 | | zend_mm_heap *mm_heap; |
2876 | | } zend_alloc_globals; |
2877 | | |
2878 | | #ifdef ZTS |
2879 | | static int alloc_globals_id; |
2880 | | static TSRM_TLS TSRM_TLS_MODEL_ATTR zend_alloc_globals alloc_globals; |
2881 | | static void *alloc_globals_tls_addr(void) { return &alloc_globals; } |
2882 | | #else |
2883 | | static zend_alloc_globals alloc_globals; |
2884 | | #endif |
2885 | 872M | #define AG(v) (alloc_globals.v) |
2886 | | |
2887 | | ZEND_API bool is_zend_mm(void) |
2888 | 0 | { |
2889 | 0 | #if ZEND_MM_CUSTOM |
2890 | 0 | return !AG(mm_heap)->use_custom_heap; |
2891 | | #else |
2892 | | return true; |
2893 | | #endif |
2894 | 0 | } |
2895 | | |
2896 | | ZEND_API bool is_zend_ptr(const void *ptr) |
2897 | 0 | { |
2898 | 0 | #if ZEND_MM_CUSTOM |
2899 | 0 | if (AG(mm_heap)->use_custom_heap) { |
2900 | 0 | if (AG(mm_heap)->custom_heap._malloc == tracked_malloc) { |
2901 | 0 | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
2902 | 0 | zval *size_zv = zend_hash_index_find(AG(mm_heap)->tracked_allocs, h); |
2903 | 0 | if (size_zv) { |
2904 | 0 | return 1; |
2905 | 0 | } |
2906 | 0 | } |
2907 | 0 | return 0; |
2908 | 0 | } |
2909 | 0 | #endif |
2910 | | |
2911 | 0 | if (AG(mm_heap)->main_chunk) { |
2912 | 0 | zend_mm_chunk *chunk = AG(mm_heap)->main_chunk; |
2913 | |
|
2914 | 0 | do { |
2915 | 0 | if (ptr >= (void*)chunk |
2916 | 0 | && ptr < (void*)((char*)chunk + ZEND_MM_CHUNK_SIZE)) { |
2917 | 0 | return 1; |
2918 | 0 | } |
2919 | 0 | chunk = chunk->next; |
2920 | 0 | } while (chunk != AG(mm_heap)->main_chunk); |
2921 | 0 | } |
2922 | | |
2923 | 0 | zend_mm_huge_list *block = AG(mm_heap)->huge_list; |
2924 | 0 | while (block) { |
2925 | 0 | if (ptr >= block->ptr |
2926 | 0 | && ptr < (void*)((char*)block->ptr + block->size)) { |
2927 | 0 | return 1; |
2928 | 0 | } |
2929 | 0 | block = block->next; |
2930 | 0 | } |
2931 | | |
2932 | 0 | return 0; |
2933 | 0 | } |
2934 | | |
2935 | | #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P) |
2936 | | #undef _emalloc |
2937 | | |
2938 | | #if ZEND_MM_CUSTOM |
2939 | | # define ZEND_MM_CUSTOM_ALLOCATOR(size) do { \ |
2940 | | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \ |
2941 | | return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2942 | | } \ |
2943 | | } while (0) |
2944 | | # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) do { \ |
2945 | | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { \ |
2946 | | AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2947 | | return; \ |
2948 | | } \ |
2949 | | } while (0) |
2950 | | #else |
2951 | | # define ZEND_MM_CUSTOM_ALLOCATOR(size) |
2952 | | # define ZEND_MM_CUSTOM_DEALLOCATOR(ptr) |
2953 | | #endif |
2954 | | |
2955 | | # define _ZEND_BIN_ALLOCATOR(_num, _size, _elements, _pages, _min_size, y) \ |
2956 | | ZEND_API void* ZEND_FASTCALL _emalloc_ ## _size(void) { \ |
2957 | | ZEND_MM_CUSTOM_ALLOCATOR(_size); \ |
2958 | | if (_size < _min_size) { \ |
2959 | | return _emalloc_ ## _min_size(); \ |
2960 | | } \ |
2961 | | return zend_mm_alloc_small(AG(mm_heap), _num ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
2962 | | } |
2963 | | |
2964 | | ZEND_MM_BINS_INFO(_ZEND_BIN_ALLOCATOR, ZEND_MM_MIN_USEABLE_BIN_SIZE, y) |
2965 | | |
2966 | | ZEND_API void* ZEND_FASTCALL _emalloc_large(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
2967 | | { |
2968 | | ZEND_MM_CUSTOM_ALLOCATOR(size); |
2969 | | return zend_mm_alloc_large_ex(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
2970 | | } |
2971 | | |
2972 | | ZEND_API void* ZEND_FASTCALL _emalloc_huge(size_t size) |
2973 | | { |
2974 | | ZEND_MM_CUSTOM_ALLOCATOR(size); |
2975 | | return zend_mm_alloc_huge(AG(mm_heap), size); |
2976 | | } |
2977 | | |
2978 | | #if ZEND_DEBUG |
2979 | | # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, _min_size, y) \ |
2980 | | ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \ |
2981 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \ |
2982 | | if (_size < _min_size) { \ |
2983 | | _efree_ ## _min_size(ptr); \ |
2984 | | return; \ |
2985 | | } \ |
2986 | | { \ |
2987 | | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); \ |
2988 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \ |
2989 | | int page_num = page_offset / ZEND_MM_PAGE_SIZE; \ |
2990 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \ |
2991 | | ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_SRUN); \ |
2992 | | ZEND_ASSERT(ZEND_MM_SRUN_BIN_NUM(chunk->map[page_num]) == _num); \ |
2993 | | zend_mm_free_small(AG(mm_heap), ptr, _num); \ |
2994 | | } \ |
2995 | | } |
2996 | | #else |
2997 | | # define _ZEND_BIN_FREE(_num, _size, _elements, _pages, _min_size, y) \ |
2998 | | ZEND_API void ZEND_FASTCALL _efree_ ## _size(void *ptr) { \ |
2999 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); \ |
3000 | | if (_size < _min_size) { \ |
3001 | | _efree_ ## _min_size(ptr); \ |
3002 | | return; \ |
3003 | | } \ |
3004 | | { \ |
3005 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); \ |
3006 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap), "zend_mm_heap corrupted"); \ |
3007 | | zend_mm_free_small(AG(mm_heap), ptr, _num); \ |
3008 | | } \ |
3009 | | } |
3010 | | #endif |
3011 | | |
3012 | | ZEND_MM_BINS_INFO(_ZEND_BIN_FREE, ZEND_MM_MIN_USEABLE_BIN_SIZE, y) |
3013 | | |
3014 | | ZEND_API void ZEND_FASTCALL _efree_large(void *ptr, size_t size) |
3015 | | { |
3016 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); |
3017 | | { |
3018 | | size_t page_offset = ZEND_MM_ALIGNED_OFFSET(ptr, ZEND_MM_CHUNK_SIZE); |
3019 | | zend_mm_chunk *chunk = (zend_mm_chunk*)ZEND_MM_ALIGNED_BASE(ptr, ZEND_MM_CHUNK_SIZE); |
3020 | | int page_num = page_offset / ZEND_MM_PAGE_SIZE; |
3021 | | uint32_t pages_count = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE) / ZEND_MM_PAGE_SIZE; |
3022 | | |
3023 | | ZEND_MM_CHECK(chunk->heap == AG(mm_heap) && ZEND_MM_ALIGNED_OFFSET(page_offset, ZEND_MM_PAGE_SIZE) == 0, "zend_mm_heap corrupted"); |
3024 | | ZEND_ASSERT(chunk->map[page_num] & ZEND_MM_IS_LRUN); |
3025 | | ZEND_ASSERT(ZEND_MM_LRUN_PAGES(chunk->map[page_num]) == pages_count); |
3026 | | zend_mm_free_large(AG(mm_heap), chunk, page_num, pages_count); |
3027 | | } |
3028 | | } |
3029 | | |
3030 | | ZEND_API void ZEND_FASTCALL _efree_huge(void *ptr, size_t size) |
3031 | | { |
3032 | | |
3033 | | ZEND_MM_CUSTOM_DEALLOCATOR(ptr); |
3034 | | zend_mm_free_huge(AG(mm_heap), ptr); |
3035 | | } |
3036 | | #endif |
3037 | | |
3038 | | ZEND_API void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3039 | 298M | { |
3040 | 298M | #if ZEND_MM_CUSTOM |
3041 | 298M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
3042 | 298M | return AG(mm_heap)->custom_heap._malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); \ |
3043 | 298M | } |
3044 | 0 | #endif |
3045 | 0 | return zend_mm_alloc_heap(AG(mm_heap), size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3046 | 298M | } |
3047 | | |
3048 | | ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3049 | 259M | { |
3050 | 259M | #if ZEND_MM_CUSTOM |
3051 | 259M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
3052 | 259M | AG(mm_heap)->custom_heap._free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3053 | 259M | return; |
3054 | 259M | } |
3055 | 0 | #endif |
3056 | 0 | zend_mm_free_heap(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3057 | 0 | } |
3058 | | |
3059 | | ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3060 | 48.0M | { |
3061 | 48.0M | #if ZEND_MM_CUSTOM |
3062 | 48.0M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
3063 | 48.0M | return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3064 | 48.0M | } |
3065 | 0 | #endif |
3066 | 0 | return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3067 | 48.0M | } |
3068 | | |
3069 | | ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3070 | 1.00M | { |
3071 | 1.00M | #if ZEND_MM_CUSTOM |
3072 | 1.00M | if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { |
3073 | 1.00M | return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3074 | 1.00M | } |
3075 | 0 | #endif |
3076 | 0 | return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3077 | 1.00M | } |
3078 | | |
3079 | | ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3080 | 0 | { |
3081 | 0 | return _zend_mm_block_size(AG(mm_heap), ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3082 | 0 | } |
3083 | | |
3084 | 143M | #define ZEND_MM_SAFE_ADDRESS_GUARDED(dest, nmemb, size, offset) do { \ |
3085 | 143M | size_t __nmemb = (nmemb); \ |
3086 | 143M | size_t __size = (size); \ |
3087 | 143M | size_t __offset = (offset); \ |
3088 | 143M | bool overflow; \ |
3089 | 143M | (dest) = zend_safe_address(__nmemb, __size, __offset, &overflow); \ |
3090 | 143M | if (UNEXPECTED(overflow)) { \ |
3091 | 0 | zend_mm_delayed_error_or_panic(AG(mm_heap)->fallible, "Possible integer overflow in memory allocation (%zu * %zu + %zu)", __nmemb, __size, __offset); \ |
3092 | 0 | return NULL; \ |
3093 | 0 | } \ |
3094 | 143M | } while (0) |
3095 | | |
3096 | | ZEND_API void* ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3097 | 6.66M | { |
3098 | 6.66M | size_t actual_size; |
3099 | 6.66M | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, size, offset); |
3100 | 6.66M | return _emalloc(actual_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3101 | 6.66M | } |
3102 | | |
3103 | | ZEND_API void* ZEND_FASTCALL _safe_malloc(size_t nmemb, size_t size, size_t offset) |
3104 | 0 | { |
3105 | 0 | size_t actual_size; |
3106 | 0 | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, size, offset); |
3107 | 0 | return pemalloc(actual_size, 1); |
3108 | 0 | } |
3109 | | |
3110 | | ZEND_API void* ZEND_FASTCALL _safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3111 | 5.88M | { |
3112 | 5.88M | size_t actual_size; |
3113 | 5.88M | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, size, offset); |
3114 | 5.88M | return _erealloc(ptr, actual_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3115 | 5.88M | } |
3116 | | |
3117 | | ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset) |
3118 | 0 | { |
3119 | 0 | size_t actual_size; |
3120 | 0 | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, size, offset); |
3121 | 0 | return perealloc(ptr, actual_size, 1); |
3122 | 0 | } |
3123 | | |
3124 | | ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3125 | 130M | { |
3126 | 130M | void *p; |
3127 | 130M | size_t actual_size; |
3128 | 130M | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, size, 0); |
3129 | | |
3130 | 130M | p = _emalloc(actual_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3131 | 130M | memset(p, 0, actual_size); |
3132 | 130M | return p; |
3133 | 130M | } |
3134 | | |
3135 | | ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3136 | 39.2M | { |
3137 | 39.2M | size_t length; |
3138 | 39.2M | char *p; |
3139 | | |
3140 | 39.2M | length = strlen(s); |
3141 | 39.2M | if (UNEXPECTED(length + 1 == 0)) { |
3142 | 0 | zend_mm_delayed_error_or_panic(AG(mm_heap)->fallible, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
3143 | 0 | return NULL; |
3144 | 0 | } |
3145 | 39.2M | p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3146 | 39.2M | memcpy(p, s, length+1); |
3147 | 39.2M | return p; |
3148 | 39.2M | } |
3149 | | |
3150 | | ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3151 | 4.71M | { |
3152 | 4.71M | char *p; |
3153 | | |
3154 | 4.71M | if (UNEXPECTED(length + 1 == 0)) { |
3155 | 0 | zend_mm_delayed_error_or_panic(AG(mm_heap)->fallible, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
3156 | 0 | return NULL; |
3157 | 0 | } |
3158 | 4.71M | p = (char *) _emalloc(length + 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3159 | 4.71M | memcpy(p, s, length); |
3160 | 4.71M | p[length] = 0; |
3161 | 4.71M | return p; |
3162 | 4.71M | } |
3163 | | |
3164 | | ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) |
3165 | 0 | { |
3166 | 0 | char *p; |
3167 | |
|
3168 | 0 | if (UNEXPECTED(length + 1 == 0)) { |
3169 | 0 | zend_mm_delayed_error_or_panic(AG(mm_heap)->fallible, "Possible integer overflow in memory allocation (1 * %zu + 1)", length); |
3170 | 0 | return NULL; |
3171 | 0 | } |
3172 | 0 | p = (char *) malloc(length + 1); |
3173 | 0 | if (UNEXPECTED(p == NULL)) { |
3174 | 0 | zend_out_of_memory(); |
3175 | 0 | } |
3176 | 0 | if (EXPECTED(length)) { |
3177 | 0 | memcpy(p, s, length); |
3178 | 0 | } |
3179 | 0 | p[length] = 0; |
3180 | 0 | return p; |
3181 | 0 | } |
3182 | | |
3183 | | /* Fallible allocation */ |
3184 | | |
3185 | 22.4M | #define ZEND_MM_FALLIBLE_FORWARD(expr) do { \ |
3186 | 22.4M | bool __was_fallible = AG(mm_heap)->fallible; \ |
3187 | 22.4M | AG(mm_heap)->fallible = true; \ |
3188 | 22.4M | void *__result = (expr); \ |
3189 | 22.4M | AG(mm_heap)->fallible = __was_fallible; \ |
3190 | 22.4M | return __result; \ |
3191 | 22.4M | } while (0) |
3192 | | |
3193 | | ZEND_API void* ZEND_FASTCALL _femalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3194 | 18.3M | { |
3195 | 18.3M | ZEND_MM_FALLIBLE_FORWARD(_emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)); |
3196 | 18.3M | } |
3197 | | |
3198 | | ZEND_API void* ZEND_FASTCALL _fecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3199 | 2.14M | { |
3200 | 2.14M | void *p = _safe_femalloc(nmemb, size, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3201 | | |
3202 | 2.14M | if (!p) { |
3203 | 0 | return NULL; |
3204 | 0 | } |
3205 | | |
3206 | 2.14M | memset(p, 0, nmemb * size); |
3207 | 2.14M | return p; |
3208 | 2.14M | } |
3209 | | |
3210 | | ZEND_API void* ZEND_FASTCALL _ferealloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3211 | 10 | { |
3212 | 10 | ZEND_MM_FALLIBLE_FORWARD(_erealloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)); |
3213 | 10 | } |
3214 | | |
3215 | | ZEND_API void* ZEND_FASTCALL _ferealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3216 | 14.2k | { |
3217 | 14.2k | ZEND_MM_FALLIBLE_FORWARD(_erealloc2(ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)); |
3218 | 14.2k | } |
3219 | | |
3220 | | ZEND_API void* ZEND_FASTCALL _safe_femalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3221 | 3.91M | { |
3222 | 3.91M | ZEND_MM_FALLIBLE_FORWARD(_safe_emalloc(nmemb, size, offset ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)); |
3223 | 3.91M | } |
3224 | | |
3225 | | ZEND_API void* ZEND_FASTCALL _safe_ferealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3226 | 176k | { |
3227 | 176k | ZEND_MM_FALLIBLE_FORWARD(_safe_erealloc(ptr, nmemb, size, offset ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)); |
3228 | 176k | } |
3229 | | |
3230 | | #if ZEND_MM_LIMIT |
3231 | | static size_t scale_by_percent(size_t value, zend_long percent) |
3232 | 313k | { |
3233 | 313k | if (percent <= 0) { |
3234 | 0 | return 0; |
3235 | 0 | } |
3236 | | |
3237 | 313k | size_t p = (size_t)percent; |
3238 | 313k | size_t q = value / 100; |
3239 | 313k | size_t r = value % 100; |
3240 | | |
3241 | 313k | if (q > SIZE_MAX / p) { |
3242 | 0 | return SIZE_MAX; |
3243 | 0 | } |
3244 | | |
3245 | 313k | size_t hi = q * p; |
3246 | 313k | size_t lo = r * (p / 100) + r * (p % 100) / 100; |
3247 | | |
3248 | 313k | if (hi > SIZE_MAX - lo) { |
3249 | 0 | return SIZE_MAX; |
3250 | 0 | } |
3251 | 313k | return hi + lo; |
3252 | 313k | } |
3253 | | |
3254 | | static void zend_mm_update_hard_limit(zend_mm_heap *heap) |
3255 | 313k | { |
3256 | 313k | ZEND_ASSERT(heap->real_size <= heap->limit); |
3257 | | |
3258 | 313k | zend_long percent = heap->hard_limit_percent; |
3259 | | |
3260 | 313k | if (percent == -1) { |
3261 | | /* For memory limit >= 1G, use a fixed 1G buffer */ |
3262 | 313k | if (heap->limit > 1*1024*1024*1024) { |
3263 | 46 | size_t offset = 1*1024*1024*1024; |
3264 | 46 | if (ZEND_MM_LIMIT_MAX - heap->limit < offset) { |
3265 | 19 | heap->hard_limit = ZEND_MM_LIMIT_MAX; |
3266 | 27 | } else { |
3267 | 27 | heap->hard_limit = heap->limit + offset; |
3268 | 27 | } |
3269 | 46 | ZEND_ASSERT(heap->hard_limit >= heap->real_size); |
3270 | 46 | return; |
3271 | 46 | } |
3272 | 313k | percent = 200; |
3273 | 313k | } |
3274 | | |
3275 | 313k | ZEND_ASSERT(percent >= 100); |
3276 | | |
3277 | 313k | size_t hard_limit = scale_by_percent(heap->limit, percent); |
3278 | 313k | if (hard_limit > ZEND_MM_LIMIT_MAX) { |
3279 | 0 | hard_limit = ZEND_MM_LIMIT_MAX; |
3280 | 0 | } |
3281 | | |
3282 | 313k | heap->hard_limit = hard_limit; |
3283 | 313k | ZEND_ASSERT(heap->hard_limit >= heap->real_size); |
3284 | 313k | } |
3285 | | #endif |
3286 | | |
3287 | | ZEND_API zend_result zend_set_memory_limit(size_t memory_limit) |
3288 | 313k | { |
3289 | 313k | #if ZEND_MM_LIMIT |
3290 | 313k | zend_mm_heap *heap = AG(mm_heap); |
3291 | | |
3292 | 313k | if (memory_limit > ZEND_MM_LIMIT_MAX) { |
3293 | 19 | memory_limit = ZEND_MM_LIMIT_MAX; |
3294 | 19 | } |
3295 | | |
3296 | 313k | if (UNEXPECTED(memory_limit < heap->real_size)) { |
3297 | 76 | if (memory_limit >= heap->real_size - heap->cached_chunks_count * ZEND_MM_CHUNK_SIZE) { |
3298 | | /* free some cached chunks to fit into new memory limit */ |
3299 | 0 | do { |
3300 | 0 | zend_mm_chunk *p = heap->cached_chunks; |
3301 | 0 | heap->cached_chunks = zend_mm_get_next_cached_chunk(heap, p); |
3302 | 0 | zend_mm_chunk_free(heap, p, ZEND_MM_CHUNK_SIZE); |
3303 | 0 | heap->cached_chunks_count--; |
3304 | 0 | heap->real_size -= ZEND_MM_CHUNK_SIZE; |
3305 | 0 | } while (memory_limit < heap->real_size); |
3306 | 0 | heap->limit = memory_limit; |
3307 | 0 | zend_mm_update_hard_limit(heap); |
3308 | 0 | return SUCCESS; |
3309 | 0 | } |
3310 | 76 | return FAILURE; |
3311 | 76 | } |
3312 | 313k | heap->limit = memory_limit; |
3313 | 313k | zend_mm_update_hard_limit(heap); |
3314 | 313k | #endif |
3315 | 313k | return SUCCESS; |
3316 | 313k | } |
3317 | | |
3318 | | ZEND_API zend_result zend_set_memory_hard_limit_percent(zend_long percent) |
3319 | 16 | { |
3320 | 16 | #if ZEND_MM_LIMIT |
3321 | 16 | zend_mm_heap *heap = AG(mm_heap); |
3322 | | |
3323 | 16 | if (percent < 100 && percent != -1) { |
3324 | 0 | return FAILURE; |
3325 | 0 | } |
3326 | | |
3327 | 16 | if (heap->real_size > heap->limit) { |
3328 | | /* Do not update memory limits while the soft limit is exceeded, as this |
3329 | | * could break invariant hard_limit >= real_size. */ |
3330 | 0 | return FAILURE; |
3331 | 0 | } |
3332 | 16 | heap->hard_limit_percent = percent; |
3333 | 16 | zend_mm_update_hard_limit(heap); |
3334 | 16 | #endif |
3335 | 16 | return SUCCESS; |
3336 | 16 | } |
3337 | | |
3338 | | ZEND_API bool zend_alloc_in_memory_limit_error_reporting(void) |
3339 | 1.63M | { |
3340 | 1.63M | #if ZEND_MM_LIMIT |
3341 | 1.63M | return AG(mm_heap)->overflow; |
3342 | | #else |
3343 | | return false; |
3344 | | #endif |
3345 | 1.63M | } |
3346 | | |
3347 | | ZEND_API size_t zend_memory_usage(bool real_usage) |
3348 | 42 | { |
3349 | 42 | #if ZEND_MM_STAT |
3350 | 42 | if (real_usage) { |
3351 | 42 | return AG(mm_heap)->real_size; |
3352 | 42 | } else { |
3353 | 0 | size_t usage = AG(mm_heap)->size; |
3354 | 0 | return usage; |
3355 | 0 | } |
3356 | 0 | #endif |
3357 | 0 | return 0; |
3358 | 42 | } |
3359 | | |
3360 | | ZEND_API size_t zend_memory_peak_usage(bool real_usage) |
3361 | 0 | { |
3362 | 0 | #if ZEND_MM_STAT |
3363 | 0 | if (real_usage) { |
3364 | 0 | return AG(mm_heap)->real_peak; |
3365 | 0 | } else { |
3366 | 0 | return AG(mm_heap)->peak; |
3367 | 0 | } |
3368 | 0 | #endif |
3369 | 0 | return 0; |
3370 | 0 | } |
3371 | | |
3372 | | ZEND_API void zend_memory_reset_peak_usage(void) |
3373 | 0 | { |
3374 | 0 | #if ZEND_MM_STAT |
3375 | 0 | AG(mm_heap)->real_peak = AG(mm_heap)->real_size; |
3376 | 0 | AG(mm_heap)->peak = AG(mm_heap)->size; |
3377 | 0 | #endif |
3378 | 0 | } |
3379 | | |
3380 | | ZEND_API void zend_mm_set_max_allocs(uint64_t max_allocs) |
3381 | 0 | { |
3382 | 0 | #if ZEND_MM_CUSTOM |
3383 | 0 | AG(mm_heap)->max_allocs = max_allocs; |
3384 | 0 | zend_mm_max_allocs_reached = false; |
3385 | 0 | #endif |
3386 | 0 | } |
3387 | | |
3388 | | ZEND_API bool zend_mm_max_allocs_hit(void) |
3389 | 6.43k | { |
3390 | 6.43k | return zend_mm_max_allocs_reached; |
3391 | 6.43k | } |
3392 | | |
3393 | | ZEND_API void shutdown_memory_manager(bool silent, bool full_shutdown) |
3394 | 306k | { |
3395 | 306k | zend_mm_shutdown(AG(mm_heap), full_shutdown, silent); |
3396 | 306k | } |
3397 | | |
3398 | | ZEND_API void refresh_memory_manager(void) |
3399 | 0 | { |
3400 | 0 | zend_mm_refresh_key_child(AG(mm_heap)); |
3401 | 0 | } |
3402 | | |
3403 | | ZEND_NORETURN static ZEND_COLD void zend_out_of_memory(void) |
3404 | 0 | { |
3405 | 0 | fprintf(stderr, "Out of memory\n"); |
3406 | 0 | abort(); |
3407 | 0 | } |
3408 | | |
3409 | | #if ZEND_MM_CUSTOM |
3410 | 140M | static zend_always_inline void tracked_add(zend_mm_heap *heap, void *ptr, size_t size) { |
3411 | 140M | zval size_zv; |
3412 | 140M | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
3413 | 140M | ZEND_ASSERT((void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2) == ptr); |
3414 | 140M | ZVAL_LONG(&size_zv, size); |
3415 | 140M | zend_hash_index_add_new(heap->tracked_allocs, h, &size_zv); |
3416 | 140M | } |
3417 | | |
3418 | 93.0M | static zend_always_inline zval *tracked_get_size_zv(zend_mm_heap *heap, void *ptr) { |
3419 | 93.0M | zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2; |
3420 | 93.0M | zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h); |
3421 | 93.0M | ZEND_ASSERT(size_zv && "Trying to free pointer not allocated through ZendMM"); |
3422 | 93.0M | return size_zv; |
3423 | 93.0M | } |
3424 | | |
3425 | 133M | static zend_always_inline zend_result tracked_check_limit(zend_mm_heap *heap, size_t add_size ZEND_FILE_LINE_DC) { |
3426 | 133M | return zend_mm_check_limit(heap, add_size, /* gc */ false, NULL ZEND_FILE_LINE_RELAY_CC); |
3427 | 133M | } |
3428 | | |
3429 | | /* ZEND_MM_MAX_ALLOCS fails the n-th allocation of the request in the same way |
3430 | | * as the memory limit would, so that the OOM handling can be exercised at an |
3431 | | * arbitrary allocation rather than only at the ones large enough to reach the |
3432 | | * limit. */ |
3433 | 0 | static zend_never_inline ZEND_COLD zend_result tracked_max_allocs_error(zend_mm_heap *heap, size_t add_size ZEND_FILE_LINE_DC) { |
3434 | 0 | zend_mm_max_allocs_reached = true; |
3435 | |
|
3436 | 0 | if (!ZEND_HAS_OOM()) { |
3437 | 0 | zend_mm_delayed_error( |
3438 | 0 | #if ZEND_DEBUG |
3439 | 0 | "Allowed memory size of %zu bytes exhausted at %s:%d (tried to allocate %zu bytes)", |
3440 | | #else |
3441 | | "Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)", |
3442 | | #endif |
3443 | 0 | heap->limit |
3444 | 0 | ZEND_FILE_LINE_RELAY_CC, |
3445 | 0 | add_size); |
3446 | 0 | } |
3447 | |
|
3448 | 0 | return heap->fallible ? FAILURE : SUCCESS; |
3449 | 0 | } |
3450 | | |
3451 | 133M | static zend_always_inline zend_result tracked_check_max_allocs(zend_mm_heap *heap, size_t add_size ZEND_FILE_LINE_DC) { |
3452 | 133M | if (UNEXPECTED(heap->max_allocs && !--heap->max_allocs)) { |
3453 | 0 | return tracked_max_allocs_error(heap, add_size ZEND_FILE_LINE_RELAY_CC); |
3454 | 0 | } |
3455 | | |
3456 | 133M | return SUCCESS; |
3457 | 133M | } |
3458 | | |
3459 | | static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3460 | 94.5M | { |
3461 | 94.5M | zend_mm_heap *heap = AG(mm_heap); |
3462 | 94.5M | if (UNEXPECTED(tracked_check_limit(heap, size ZEND_FILE_LINE_CC) == FAILURE)) { |
3463 | 763 | return NULL; |
3464 | 763 | } |
3465 | | |
3466 | 94.5M | if (UNEXPECTED(tracked_check_max_allocs(heap, size ZEND_FILE_LINE_CC) == FAILURE)) { |
3467 | 0 | return NULL; |
3468 | 0 | } |
3469 | | |
3470 | 94.5M | void *ptr = malloc(size); |
3471 | 94.5M | if (!ptr) { |
3472 | 0 | zend_out_of_memory(); |
3473 | 0 | } |
3474 | | |
3475 | 94.5M | tracked_add(heap, ptr, size); |
3476 | 94.5M | #if ZEND_MM_STAT |
3477 | 94.5M | heap->size += size; |
3478 | 94.5M | heap->real_size = heap->size; |
3479 | 94.5M | #endif |
3480 | 94.5M | return ptr; |
3481 | 94.5M | } |
3482 | | |
3483 | 55.2M | static void tracked_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { |
3484 | 55.2M | if (!ptr) { |
3485 | 483k | return; |
3486 | 483k | } |
3487 | | |
3488 | 54.8M | zend_mm_heap *heap = AG(mm_heap); |
3489 | 54.8M | zval *size_zv = tracked_get_size_zv(heap, ptr); |
3490 | 54.8M | #if ZEND_MM_STAT |
3491 | 54.8M | heap->size -= Z_LVAL_P(size_zv); |
3492 | 54.8M | heap->real_size = heap->size; |
3493 | 54.8M | #endif |
3494 | 54.8M | zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) size_zv); |
3495 | 54.8M | free(ptr); |
3496 | 54.8M | } |
3497 | | |
3498 | 45.7M | static void *tracked_realloc(void *ptr, size_t new_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { |
3499 | 45.7M | zend_mm_heap *heap = AG(mm_heap); |
3500 | 45.7M | zval *old_size_zv = NULL; |
3501 | 45.7M | size_t old_size = 0; |
3502 | 45.7M | if (ptr) { |
3503 | 38.2M | old_size_zv = tracked_get_size_zv(heap, ptr); |
3504 | 38.2M | old_size = Z_LVAL_P(old_size_zv); |
3505 | 38.2M | } |
3506 | | |
3507 | 45.7M | if (new_size > old_size) { |
3508 | 38.9M | if (UNEXPECTED(tracked_check_limit(heap, new_size - old_size ZEND_FILE_LINE_CC) == FAILURE)) { |
3509 | 0 | return NULL; |
3510 | 0 | } |
3511 | | |
3512 | 38.9M | if (UNEXPECTED(tracked_check_max_allocs(heap, new_size - old_size ZEND_FILE_LINE_CC) == FAILURE)) { |
3513 | 0 | return NULL; |
3514 | 0 | } |
3515 | 38.9M | } |
3516 | | |
3517 | | /* Delete information about old allocation only after checking the memory limit. */ |
3518 | 45.7M | if (old_size_zv) { |
3519 | 38.2M | zend_hash_del_bucket(heap->tracked_allocs, (Bucket *) old_size_zv); |
3520 | 38.2M | } |
3521 | | |
3522 | 45.7M | ptr = __zend_realloc(ptr, new_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3523 | 45.7M | tracked_add(heap, ptr, new_size); |
3524 | 45.7M | #if ZEND_MM_STAT |
3525 | 45.7M | heap->size += new_size - old_size; |
3526 | 45.7M | heap->real_size = heap->size; |
3527 | 45.7M | #endif |
3528 | 45.7M | return ptr; |
3529 | 45.7M | } |
3530 | | |
3531 | 6.73k | static void tracked_free_all(zend_mm_heap *heap) { |
3532 | 6.73k | HashTable *tracked_allocs = heap->tracked_allocs; |
3533 | 6.73k | zend_ulong h; |
3534 | 103M | ZEND_HASH_FOREACH_NUM_KEY(tracked_allocs, h) { |
3535 | 103M | void *ptr = (void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2); |
3536 | 103M | free(ptr); |
3537 | 103M | } ZEND_HASH_FOREACH_END(); |
3538 | 6.73k | } |
3539 | | |
3540 | | static void* poison_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3541 | 0 | { |
3542 | 0 | zend_mm_heap *heap = AG(mm_heap); |
3543 | |
|
3544 | 0 | if (SIZE_MAX - heap->debug.padding * 2 < size) { |
3545 | 0 | zend_mm_panic("Integer overflow in memory allocation"); |
3546 | 0 | } |
3547 | 0 | size += heap->debug.padding * 2; |
3548 | |
|
3549 | 0 | void *ptr = zend_mm_alloc_heap(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3550 | |
|
3551 | 0 | if (EXPECTED(ptr)) { |
3552 | 0 | if (heap->debug.poison_alloc) { |
3553 | 0 | memset(ptr, heap->debug.poison_alloc_value, size); |
3554 | 0 | } |
3555 | |
|
3556 | 0 | ptr = (char*)ptr + heap->debug.padding; |
3557 | 0 | } |
3558 | |
|
3559 | 0 | return ptr; |
3560 | 0 | } |
3561 | | |
3562 | | static void poison_free(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3563 | 0 | { |
3564 | 0 | zend_mm_heap *heap = AG(mm_heap); |
3565 | |
|
3566 | 0 | if (EXPECTED(ptr)) { |
3567 | | /* zend_mm_shutdown() will try to free the heap when custom handlers |
3568 | | * are installed */ |
3569 | 0 | if (UNEXPECTED(ptr == heap)) { |
3570 | 0 | return; |
3571 | 0 | } |
3572 | | |
3573 | 0 | ptr = (char*)ptr - heap->debug.padding; |
3574 | |
|
3575 | 0 | size_t size = zend_mm_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3576 | |
|
3577 | 0 | if (heap->debug.poison_free) { |
3578 | 0 | memset(ptr, heap->debug.poison_free_value, size); |
3579 | 0 | } |
3580 | 0 | } |
3581 | | |
3582 | 0 | zend_mm_free_heap(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3583 | 0 | } |
3584 | | |
3585 | | static void* poison_realloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
3586 | 0 | { |
3587 | 0 | zend_mm_heap *heap = AG(mm_heap); |
3588 | |
|
3589 | 0 | void *new = poison_malloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3590 | |
|
3591 | 0 | if (ptr) { |
3592 | | /* Determine the size of the old allocation from the unpadded pointer. */ |
3593 | 0 | size_t oldsize = zend_mm_size(heap, (char*)ptr - heap->debug.padding ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3594 | | |
3595 | | /* Remove the padding size to determine the size that is available to the user. */ |
3596 | 0 | oldsize -= (2 * heap->debug.padding); |
3597 | |
|
3598 | 0 | #if ZEND_DEBUG |
3599 | 0 | oldsize -= sizeof(zend_mm_debug_info); |
3600 | 0 | #endif |
3601 | |
|
3602 | 0 | memcpy(new, ptr, MIN(oldsize, size)); |
3603 | 0 | poison_free(ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
3604 | 0 | } |
3605 | |
|
3606 | 0 | return new; |
3607 | 0 | } |
3608 | | |
3609 | | static size_t poison_gc(void) |
3610 | 0 | { |
3611 | 0 | zend_mm_heap *heap = AG(mm_heap); |
3612 | |
|
3613 | 0 | void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3614 | 0 | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3615 | 0 | void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3616 | 0 | size_t (*_gc)(void); |
3617 | 0 | void (*_shutdown)(bool, bool); |
3618 | |
|
3619 | 0 | zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown); |
3620 | 0 | zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL); |
3621 | |
|
3622 | 0 | size_t collected = zend_mm_gc(heap); |
3623 | |
|
3624 | 0 | zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown); |
3625 | |
|
3626 | 0 | return collected; |
3627 | 0 | } |
3628 | | |
3629 | | static void poison_shutdown(bool full, bool silent) |
3630 | 0 | { |
3631 | 0 | zend_mm_heap *heap = AG(mm_heap); |
3632 | |
|
3633 | 0 | void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3634 | 0 | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3635 | 0 | void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); |
3636 | 0 | size_t (*_gc)(void); |
3637 | 0 | void (*_shutdown)(bool, bool); |
3638 | |
|
3639 | 0 | zend_mm_get_custom_handlers_ex(heap, &_malloc, &_free, &_realloc, &_gc, &_shutdown); |
3640 | 0 | zend_mm_set_custom_handlers_ex(heap, NULL, NULL, NULL, NULL, NULL); |
3641 | |
|
3642 | 0 | if (heap->debug.check_freelists_on_shutdown) { |
3643 | 0 | zend_mm_check_freelists(heap); |
3644 | 0 | } |
3645 | |
|
3646 | 0 | zend_mm_shutdown(heap, full, silent); |
3647 | |
|
3648 | 0 | if (!full) { |
3649 | 0 | zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, _gc, _shutdown); |
3650 | 0 | } |
3651 | 0 | } |
3652 | | |
3653 | | static void poison_enable(zend_mm_heap *heap, char *parameters) |
3654 | 0 | { |
3655 | 0 | char *tmp = parameters; |
3656 | 0 | char *end = tmp + strlen(tmp); |
3657 | | |
3658 | | /* Trim heading/trailing whitespaces */ |
3659 | 0 | while (*tmp == ' ' || *tmp == '\t' || *tmp == '\n') { |
3660 | 0 | tmp++; |
3661 | 0 | } |
3662 | 0 | while (end != tmp && (*(end-1) == ' ' || *(end-1) == '\t' || *(end-1) == '\n')) { |
3663 | 0 | end--; |
3664 | 0 | } |
3665 | |
|
3666 | 0 | if (tmp == end) { |
3667 | 0 | return; |
3668 | 0 | } |
3669 | | |
3670 | 0 | while (1) { |
3671 | 0 | char *key = tmp; |
3672 | |
|
3673 | 0 | tmp = memchr(tmp, '=', end - tmp); |
3674 | 0 | if (!tmp) { |
3675 | 0 | size_t key_len = end - key; |
3676 | 0 | fprintf(stderr, "Unexpected EOF after ZEND_MM_DEBUG parameter '%.*s', expected '='\n", |
3677 | 0 | (int)key_len, key); |
3678 | 0 | return; |
3679 | 0 | } |
3680 | | |
3681 | 0 | size_t key_len = tmp - key; |
3682 | 0 | char *value = tmp + 1; |
3683 | |
|
3684 | 0 | if (key_len == strlen("poison_alloc") |
3685 | 0 | && !memcmp(key, "poison_alloc", key_len)) { |
3686 | |
|
3687 | 0 | heap->debug.poison_alloc = true; |
3688 | 0 | heap->debug.poison_alloc_value = (uint8_t) ZEND_STRTOUL(value, &tmp, 0); |
3689 | |
|
3690 | 0 | } else if (key_len == strlen("poison_free") |
3691 | 0 | && !memcmp(key, "poison_free", key_len)) { |
3692 | |
|
3693 | 0 | heap->debug.poison_free = true; |
3694 | 0 | heap->debug.poison_free_value = (uint8_t) ZEND_STRTOUL(value, &tmp, 0); |
3695 | |
|
3696 | 0 | } else if (key_len == strlen("padding") |
3697 | 0 | && !memcmp(key, "padding", key_len)) { |
3698 | |
|
3699 | 0 | uint8_t padding = ZEND_STRTOUL(value, &tmp, 0); |
3700 | 0 | if (ZEND_MM_ALIGNED_SIZE(padding) != padding) { |
3701 | 0 | fprintf(stderr, "ZEND_MM_DEBUG padding must be a multiple of %u, %u given\n", |
3702 | 0 | (unsigned int)ZEND_MM_ALIGNMENT, |
3703 | 0 | (unsigned int)padding); |
3704 | 0 | return; |
3705 | 0 | } |
3706 | 0 | heap->debug.padding = padding; |
3707 | |
|
3708 | 0 | } else if (key_len == strlen("check_freelists_on_shutdown") |
3709 | 0 | && !memcmp(key, "check_freelists_on_shutdown", key_len)) { |
3710 | |
|
3711 | 0 | heap->debug.check_freelists_on_shutdown = (bool) ZEND_STRTOUL(value, &tmp, 0); |
3712 | |
|
3713 | 0 | } else { |
3714 | 0 | fprintf(stderr, "Unknown ZEND_MM_DEBUG parameter: '%.*s'\n", |
3715 | 0 | (int)key_len, key); |
3716 | 0 | return; |
3717 | 0 | } |
3718 | | |
3719 | 0 | if (tmp == end) { |
3720 | 0 | break; |
3721 | 0 | } |
3722 | 0 | if (*tmp != ',') { |
3723 | 0 | fprintf(stderr, "Unexpected '%c' after value of ZEND_MM_DEBUG parameter '%.*s', expected ','\n", |
3724 | 0 | *tmp, (int)key_len, key); |
3725 | 0 | return; |
3726 | 0 | } |
3727 | 0 | tmp++; |
3728 | 0 | } |
3729 | | |
3730 | 0 | zend_mm_set_custom_handlers_ex(heap, poison_malloc, poison_free, |
3731 | 0 | poison_realloc, poison_gc, poison_shutdown); |
3732 | 0 | } |
3733 | | #endif |
3734 | | |
3735 | | static void alloc_globals_ctor(zend_alloc_globals *alloc_globals) |
3736 | 16 | { |
3737 | 16 | char *tmp; |
3738 | | |
3739 | 16 | #if ZEND_MM_CUSTOM |
3740 | 16 | tmp = getenv("USE_ZEND_ALLOC"); |
3741 | 16 | if (tmp && !ZEND_ATOL(tmp)) { |
3742 | 16 | bool tracked = (tmp = getenv("USE_TRACKED_ALLOC")) && ZEND_ATOL(tmp); |
3743 | 16 | zend_mm_heap *mm_heap = alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap)); |
3744 | 16 | memset(mm_heap, 0, sizeof(zend_mm_heap)); |
3745 | 16 | mm_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD; |
3746 | 16 | mm_heap->limit = ZEND_MM_LIMIT_MAX; |
3747 | 16 | mm_heap->hard_limit = ZEND_MM_LIMIT_MAX; |
3748 | 16 | mm_heap->hard_limit_percent = -1; |
3749 | 16 | mm_heap->overflow = 0; |
3750 | 16 | mm_heap->fallible = false; |
3751 | | |
3752 | 16 | if (!tracked) { |
3753 | | /* Use system allocator. */ |
3754 | 8 | mm_heap->custom_heap._malloc = __zend_malloc; |
3755 | 8 | mm_heap->custom_heap._free = __zend_free; |
3756 | 8 | mm_heap->custom_heap._realloc = __zend_realloc; |
3757 | 8 | } else { |
3758 | | /* Use system allocator and track allocations for auto-free. */ |
3759 | 8 | mm_heap->custom_heap._malloc = tracked_malloc; |
3760 | 8 | mm_heap->custom_heap._free = tracked_free; |
3761 | 8 | mm_heap->custom_heap._realloc = tracked_realloc; |
3762 | 8 | mm_heap->tracked_allocs = malloc(sizeof(HashTable)); |
3763 | 8 | zend_hash_init(mm_heap->tracked_allocs, 1024, NULL, NULL, 1); |
3764 | 8 | } |
3765 | 16 | return; |
3766 | 16 | } |
3767 | 0 | #endif |
3768 | | |
3769 | 0 | tmp = getenv("USE_ZEND_ALLOC_HUGE_PAGES"); |
3770 | 0 | if (tmp && ZEND_ATOL(tmp)) { |
3771 | 0 | zend_mm_use_huge_pages = true; |
3772 | 0 | } |
3773 | 0 | alloc_globals->mm_heap = zend_mm_init(); |
3774 | |
|
3775 | 0 | #if ZEND_MM_CUSTOM |
3776 | 0 | ZEND_ASSERT(!alloc_globals->mm_heap->tracked_allocs); |
3777 | 0 | tmp = getenv("ZEND_MM_DEBUG"); |
3778 | 0 | if (tmp) { |
3779 | 0 | poison_enable(alloc_globals->mm_heap, tmp); |
3780 | 0 | } |
3781 | 0 | #endif |
3782 | 0 | } |
3783 | | |
3784 | | #ifdef ZTS |
3785 | | static void alloc_globals_dtor(zend_alloc_globals *alloc_globals) |
3786 | | { |
3787 | | zend_mm_shutdown(alloc_globals->mm_heap, 1, 1); |
3788 | | } |
3789 | | #endif |
3790 | | |
3791 | | ZEND_API void start_memory_manager(void) |
3792 | 16 | { |
3793 | 16 | #ifndef _WIN32 |
3794 | 16 | # if defined(_SC_PAGESIZE) |
3795 | 16 | REAL_PAGE_SIZE = sysconf(_SC_PAGESIZE); |
3796 | | # elif defined(_SC_PAGE_SIZE) |
3797 | | REAL_PAGE_SIZE = sysconf(_SC_PAGE_SIZE); |
3798 | | # endif |
3799 | 16 | #endif |
3800 | | #ifdef ZTS |
3801 | | ts_allocate_tls_id(&alloc_globals_id, alloc_globals_tls_addr, sizeof(zend_alloc_globals), (ts_allocate_ctor) alloc_globals_ctor, (ts_allocate_dtor) alloc_globals_dtor); |
3802 | | #else |
3803 | 16 | alloc_globals_ctor(&alloc_globals); |
3804 | 16 | #endif |
3805 | 16 | } |
3806 | | |
3807 | | ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap) |
3808 | 0 | { |
3809 | 0 | zend_mm_heap *old_heap; |
3810 | |
|
3811 | 0 | old_heap = AG(mm_heap); |
3812 | 0 | AG(mm_heap) = (zend_mm_heap*)new_heap; |
3813 | 0 | return (zend_mm_heap*)old_heap; |
3814 | 0 | } |
3815 | | |
3816 | | ZEND_API zend_mm_heap *zend_mm_get_heap(void) |
3817 | 0 | { |
3818 | 0 | return AG(mm_heap); |
3819 | 0 | } |
3820 | | |
3821 | | ZEND_API bool zend_mm_is_fallible(void) |
3822 | 0 | { |
3823 | 0 | return AG(mm_heap)->fallible; |
3824 | 0 | } |
3825 | | |
3826 | | ZEND_API void zend_mm_set_fallible(bool fallible) |
3827 | 0 | { |
3828 | 0 | AG(mm_heap)->fallible = fallible; |
3829 | 0 | } |
3830 | | |
3831 | | ZEND_API bool zend_mm_is_custom_heap(zend_mm_heap *new_heap) |
3832 | 0 | { |
3833 | 0 | #if ZEND_MM_CUSTOM |
3834 | 0 | return AG(mm_heap)->use_custom_heap; |
3835 | | #else |
3836 | | return 0; |
3837 | | #endif |
3838 | 0 | } |
3839 | | |
3840 | | ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap, |
3841 | | void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3842 | | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3843 | | void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)) |
3844 | 0 | { |
3845 | 0 | #if ZEND_MM_CUSTOM |
3846 | 0 | zend_mm_set_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL); |
3847 | 0 | #endif |
3848 | 0 | } |
3849 | | |
3850 | | ZEND_API void zend_mm_set_custom_handlers_ex(zend_mm_heap *heap, |
3851 | | void* (*_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3852 | | void (*_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3853 | | void* (*_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3854 | | size_t (*_gc)(void), |
3855 | | void (*_shutdown)(bool, bool)) |
3856 | 0 | { |
3857 | 0 | #if ZEND_MM_CUSTOM |
3858 | 0 | zend_mm_heap *_heap = (zend_mm_heap*)heap; |
3859 | |
|
3860 | 0 | if (!_malloc && !_free && !_realloc) { |
3861 | 0 | _heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_NONE; |
3862 | 0 | } else { |
3863 | 0 | _heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD; |
3864 | 0 | _heap->custom_heap._malloc = _malloc; |
3865 | 0 | _heap->custom_heap._free = _free; |
3866 | 0 | _heap->custom_heap._realloc = _realloc; |
3867 | 0 | _heap->custom_heap._gc = _gc; |
3868 | 0 | _heap->custom_heap._shutdown = _shutdown; |
3869 | 0 | } |
3870 | 0 | #endif |
3871 | 0 | } |
3872 | | |
3873 | | ZEND_API void zend_mm_get_custom_handlers(zend_mm_heap *heap, |
3874 | | void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3875 | | void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3876 | | void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)) |
3877 | 0 | { |
3878 | 0 | #if ZEND_MM_CUSTOM |
3879 | 0 | zend_mm_get_custom_handlers_ex(heap, _malloc, _free, _realloc, NULL, NULL); |
3880 | 0 | #endif |
3881 | 0 | } |
3882 | | |
3883 | | ZEND_API void zend_mm_get_custom_handlers_ex(zend_mm_heap *heap, |
3884 | | void* (**_malloc)(size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3885 | | void (**_free)(void* ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3886 | | void* (**_realloc)(void*, size_t ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC), |
3887 | | size_t (**_gc)(void), |
3888 | | void (**_shutdown)(bool, bool)) |
3889 | 0 | { |
3890 | 0 | #if ZEND_MM_CUSTOM |
3891 | 0 | zend_mm_heap *_heap = (zend_mm_heap*)heap; |
3892 | |
|
3893 | 0 | if (heap->use_custom_heap) { |
3894 | 0 | *_malloc = _heap->custom_heap._malloc; |
3895 | 0 | *_free = _heap->custom_heap._free; |
3896 | 0 | *_realloc = _heap->custom_heap._realloc; |
3897 | 0 | if (_gc != NULL) { |
3898 | 0 | *_gc = _heap->custom_heap._gc; |
3899 | 0 | } |
3900 | 0 | if (_shutdown != NULL) { |
3901 | 0 | *_shutdown = _heap->custom_heap._shutdown; |
3902 | 0 | } |
3903 | 0 | } else { |
3904 | 0 | *_malloc = NULL; |
3905 | 0 | *_free = NULL; |
3906 | 0 | *_realloc = NULL; |
3907 | 0 | if (_gc != NULL) { |
3908 | 0 | *_gc = NULL; |
3909 | 0 | } |
3910 | 0 | if (_shutdown != NULL) { |
3911 | 0 | *_shutdown = NULL; |
3912 | 0 | } |
3913 | 0 | } |
3914 | | #else |
3915 | | *_malloc = NULL; |
3916 | | *_free = NULL; |
3917 | | *_realloc = NULL; |
3918 | | *_gc = NULL; |
3919 | | *_shutdown = NULL; |
3920 | | #endif |
3921 | 0 | } |
3922 | | |
3923 | | ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap) |
3924 | 0 | { |
3925 | 0 | #if ZEND_MM_STORAGE |
3926 | 0 | return heap->storage; |
3927 | | #else |
3928 | | return NULL; |
3929 | | #endif |
3930 | 0 | } |
3931 | | |
3932 | | ZEND_API zend_mm_heap *zend_mm_startup(void) |
3933 | 0 | { |
3934 | 0 | return zend_mm_init(); |
3935 | 0 | } |
3936 | | |
3937 | | ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void *data, size_t data_size) |
3938 | 0 | { |
3939 | 0 | #if ZEND_MM_STORAGE |
3940 | 0 | zend_mm_storage *storage; |
3941 | 0 | zend_mm_storage tmp_storage = { |
3942 | 0 | .handlers = *handlers, |
3943 | 0 | .data = data, |
3944 | 0 | }; |
3945 | 0 | zend_mm_chunk *chunk; |
3946 | 0 | zend_mm_heap *heap; |
3947 | |
|
3948 | 0 | chunk = (zend_mm_chunk*)handlers->chunk_alloc(&tmp_storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE); |
3949 | 0 | if (UNEXPECTED(chunk == NULL)) { |
3950 | 0 | #if ZEND_MM_ERROR |
3951 | 0 | fprintf(stderr, "Can't initialize heap\n"); |
3952 | 0 | #endif |
3953 | 0 | return NULL; |
3954 | 0 | } |
3955 | 0 | heap = &chunk->heap_slot; |
3956 | 0 | chunk->heap = heap; |
3957 | 0 | chunk->next = chunk; |
3958 | 0 | chunk->prev = chunk; |
3959 | 0 | chunk->free_pages = ZEND_MM_PAGES - ZEND_MM_FIRST_PAGE; |
3960 | 0 | chunk->free_tail = ZEND_MM_FIRST_PAGE; |
3961 | 0 | chunk->num = 0; |
3962 | 0 | chunk->free_map[0] = (Z_L(1) << ZEND_MM_FIRST_PAGE) - 1; |
3963 | 0 | chunk->map[0] = ZEND_MM_LRUN(ZEND_MM_FIRST_PAGE); |
3964 | 0 | heap->main_chunk = chunk; |
3965 | 0 | heap->cached_chunks = NULL; |
3966 | 0 | heap->chunks_count = 1; |
3967 | 0 | heap->peak_chunks_count = 1; |
3968 | 0 | heap->cached_chunks_count = 0; |
3969 | 0 | heap->avg_chunks_count = 1.0; |
3970 | 0 | heap->last_chunks_delete_boundary = 0; |
3971 | 0 | heap->last_chunks_delete_count = 0; |
3972 | 0 | #if ZEND_MM_STAT || ZEND_MM_LIMIT |
3973 | 0 | heap->real_size = ZEND_MM_CHUNK_SIZE; |
3974 | 0 | #endif |
3975 | 0 | #if ZEND_MM_STAT |
3976 | 0 | heap->real_peak = ZEND_MM_CHUNK_SIZE; |
3977 | 0 | heap->size = 0; |
3978 | 0 | heap->peak = 0; |
3979 | 0 | #endif |
3980 | 0 | zend_mm_init_key(heap); |
3981 | 0 | #if ZEND_MM_LIMIT |
3982 | 0 | heap->limit = ZEND_MM_LIMIT_MAX; |
3983 | 0 | heap->overflow = 0; |
3984 | 0 | heap->fallible = false; |
3985 | 0 | #endif |
3986 | 0 | #if ZEND_MM_CUSTOM |
3987 | 0 | heap->use_custom_heap = 0; |
3988 | 0 | #endif |
3989 | 0 | heap->storage = &tmp_storage; |
3990 | 0 | heap->huge_list = NULL; |
3991 | 0 | memset(heap->free_slot, 0, sizeof(heap->free_slot)); |
3992 | 0 | storage = _zend_mm_alloc(heap, sizeof(zend_mm_storage) + data_size ZEND_FILE_LINE_CC ZEND_FILE_LINE_CC); |
3993 | 0 | if (!storage) { |
3994 | 0 | handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE); |
3995 | 0 | #if ZEND_MM_ERROR |
3996 | 0 | fprintf(stderr, "Can't initialize heap\n"); |
3997 | 0 | #endif |
3998 | 0 | return NULL; |
3999 | 0 | } |
4000 | 0 | memcpy(storage, &tmp_storage, sizeof(zend_mm_storage)); |
4001 | 0 | if (data) { |
4002 | 0 | storage->data = (void*)(((char*)storage + sizeof(zend_mm_storage))); |
4003 | 0 | memcpy(storage->data, data, data_size); |
4004 | 0 | } |
4005 | 0 | heap->storage = storage; |
4006 | 0 | #if ZEND_DEBUG |
4007 | 0 | heap->pid = getpid(); |
4008 | 0 | #endif |
4009 | 0 | return heap; |
4010 | | #else |
4011 | | return NULL; |
4012 | | #endif |
4013 | 0 | } |
4014 | | |
4015 | | ZEND_API void ZEND_FASTCALL zend_handle_delayed_oom(void) |
4016 | 2.15k | { |
4017 | 2.15k | zend_oom_info *info = &EG(delayed_oom); |
4018 | 2.15k | zend_execute_data *execute_data = EG(current_execute_data); |
4019 | | |
4020 | 2.15k | if (info->state != ZEND_OOM_STATE_PENDING) { |
4021 | 1.35k | return; |
4022 | 1.35k | } |
4023 | 800 | info->state = ZEND_OOM_STATE_HANDLING; |
4024 | | |
4025 | 800 | AG(mm_heap)->overflow = 1; |
4026 | | |
4027 | | /* message was allocated persistently, use emalloc() now as error_get_last() |
4028 | | * may expose it to userland. */ |
4029 | 800 | zend_string *message = zend_string_init(ZSTR_VAL(info->message), ZSTR_LEN(info->message), 0); |
4030 | 800 | zend_string_release(info->message); |
4031 | 800 | info->message = message; |
4032 | | |
4033 | 800 | if (execute_data) { |
4034 | | /* An exception may already be in flight, and zend_throw_unwind_exit() |
4035 | | * does not allow that */ |
4036 | 763 | EG(exception) = NULL; |
4037 | | |
4038 | | /* Object allocation should succeed unless it trips the hard limit */ |
4039 | 763 | zend_throw_unwind_exit(); |
4040 | 763 | } |
4041 | | |
4042 | 800 | zend_try { |
4043 | 800 | zend_error_zstr_at(E_ERROR, info->filename, info->lineno, info->message); |
4044 | 800 | } zend_catch { |
4045 | 800 | } zend_end_try(); |
4046 | | |
4047 | 800 | AG(mm_heap)->overflow = 0; |
4048 | | |
4049 | | /* Bailout discards EG(current_execute_data). Would be better to avoid the bailout here. */ |
4050 | 800 | EG(current_execute_data) = execute_data; |
4051 | | |
4052 | | /* This function may be called from the VM interrupt handler: opline is not |
4053 | | * executed yet. Set opline_before_exception to NULL so |
4054 | | * ZEND_HANDLE_EXCEPTION does not use it. */ |
4055 | 800 | EG(opline_before_exception) = NULL; |
4056 | | |
4057 | 800 | zend_mm_oom_info_dtor(info); |
4058 | 800 | info->state = ZEND_OOM_STATE_HANDLED; |
4059 | | |
4060 | | /* No userland frame to unwind: there is nothing to restore afterwards |
4061 | | * and no exception to throw, so catching the bailout here would just |
4062 | | * let the request carry on past a fatal error. Let it propagate and |
4063 | | * abort the request, the way an immediate OOM does. */ |
4064 | 800 | if (!execute_data) { |
4065 | 37 | zend_bailout(); |
4066 | 37 | } |
4067 | 800 | } |
4068 | | |
4069 | | ZEND_API void * __zend_malloc(size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
4070 | 204M | { |
4071 | 204M | void *tmp = malloc(len); |
4072 | 204M | if (EXPECTED(tmp || !len)) { |
4073 | 204M | return tmp; |
4074 | 204M | } |
4075 | 0 | zend_out_of_memory(); |
4076 | 0 | } |
4077 | | |
4078 | | ZEND_API void * __zend_calloc(size_t nmemb, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
4079 | 0 | { |
4080 | 0 | void *tmp; |
4081 | 0 | size_t actual_size; |
4082 | 0 | ZEND_MM_SAFE_ADDRESS_GUARDED(actual_size, nmemb, len, 0); |
4083 | | |
4084 | 0 | tmp = __zend_malloc(actual_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); |
4085 | 0 | memset(tmp, 0, actual_size); |
4086 | 0 | return tmp; |
4087 | 0 | } |
4088 | | |
4089 | | ZEND_API void * __zend_realloc(void *p, size_t len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
4090 | 49.0M | { |
4091 | 49.0M | p = realloc(p, len); |
4092 | 49.0M | if (EXPECTED(p || !len)) { |
4093 | 49.0M | return p; |
4094 | 49.0M | } |
4095 | 0 | zend_out_of_memory(); |
4096 | 0 | } |
4097 | | |
4098 | | ZEND_API void __zend_free(void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) |
4099 | 204M | { |
4100 | 204M | free(p); |
4101 | 204M | return; |
4102 | 204M | } |
4103 | | |
4104 | | ZEND_API char * __zend_strdup(const char *s) |
4105 | 0 | { |
4106 | 0 | char *tmp = strdup(s); |
4107 | 0 | if (EXPECTED(tmp)) { |
4108 | 0 | return tmp; |
4109 | 0 | } |
4110 | 0 | zend_out_of_memory(); |
4111 | 0 | } |