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