xref: /PHP-8.0/Zend/zend_arena.h (revision fa8d9b11)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef _ZEND_ARENA_H_
20 #define _ZEND_ARENA_H_
21 
22 #include "zend.h"
23 
24 #ifndef ZEND_TRACK_ARENA_ALLOC
25 
26 typedef struct _zend_arena zend_arena;
27 
28 struct _zend_arena {
29 	char		*ptr;
30 	char		*end;
31 	zend_arena  *prev;
32 };
33 
zend_arena_create(size_t size)34 static zend_always_inline zend_arena* zend_arena_create(size_t size)
35 {
36 	zend_arena *arena = (zend_arena*)emalloc(size);
37 
38 	arena->ptr = (char*) arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
39 	arena->end = (char*) arena + size;
40 	arena->prev = NULL;
41 	return arena;
42 }
43 
zend_arena_destroy(zend_arena * arena)44 static zend_always_inline void zend_arena_destroy(zend_arena *arena)
45 {
46 	do {
47 		zend_arena *prev = arena->prev;
48 		efree(arena);
49 		arena = prev;
50 	} while (arena);
51 }
52 
zend_arena_alloc(zend_arena ** arena_ptr,size_t size)53 static zend_always_inline void* zend_arena_alloc(zend_arena **arena_ptr, size_t size)
54 {
55 	zend_arena *arena = *arena_ptr;
56 	char *ptr = arena->ptr;
57 
58 	size = ZEND_MM_ALIGNED_SIZE(size);
59 
60 	if (EXPECTED(size <= (size_t)(arena->end - ptr))) {
61 		arena->ptr = ptr + size;
62 	} else {
63 		size_t arena_size =
64 			UNEXPECTED((size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) > (size_t)(arena->end - (char*) arena)) ?
65 				(size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) :
66 				(size_t)(arena->end - (char*) arena);
67 		zend_arena *new_arena = (zend_arena*)emalloc(arena_size);
68 
69 		ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
70 		new_arena->ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena)) + size;
71 		new_arena->end = (char*) new_arena + arena_size;
72 		new_arena->prev = arena;
73 		*arena_ptr = new_arena;
74 	}
75 
76 	return (void*) ptr;
77 }
78 
zend_arena_calloc(zend_arena ** arena_ptr,size_t count,size_t unit_size)79 static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
80 {
81 	bool overflow;
82 	size_t size;
83 	void *ret;
84 
85 	size = zend_safe_address(unit_size, count, 0, &overflow);
86 	if (UNEXPECTED(overflow)) {
87 		zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
88 	}
89 	ret = zend_arena_alloc(arena_ptr, size);
90 	memset(ret, 0, size);
91 	return ret;
92 }
93 
zend_arena_checkpoint(zend_arena * arena)94 static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
95 {
96 	return arena->ptr;
97 }
98 
zend_arena_release(zend_arena ** arena_ptr,void * checkpoint)99 static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
100 {
101 	zend_arena *arena = *arena_ptr;
102 
103 	while (UNEXPECTED((char*)checkpoint > arena->end) ||
104 	       UNEXPECTED((char*)checkpoint <= (char*)arena)) {
105 		zend_arena *prev = arena->prev;
106 		efree(arena);
107 		*arena_ptr = arena = prev;
108 	}
109 	ZEND_ASSERT((char*)checkpoint > (char*)arena && (char*)checkpoint <= arena->end);
110 	arena->ptr = (char*)checkpoint;
111 }
112 
zend_arena_contains(zend_arena * arena,void * ptr)113 static zend_always_inline zend_bool zend_arena_contains(zend_arena *arena, void *ptr)
114 {
115 	while (arena) {
116 		if ((char*)ptr > (char*)arena && (char*)ptr <= arena->ptr) {
117 			return 1;
118 		}
119 		arena = arena->prev;
120 	}
121 	return 0;
122 }
123 
124 #else
125 
126 /* Use normal allocations and keep track of them for mass-freeing.
127  * This is intended for use with asan/valgrind. */
128 
129 typedef struct _zend_arena zend_arena;
130 
131 struct _zend_arena {
132 	void **ptr;
133 	void **end;
134 	struct _zend_arena *prev;
135 	void *ptrs[0];
136 };
137 
138 #define ZEND_TRACKED_ARENA_SIZE 1000
139 
zend_arena_create(size_t _size)140 static zend_always_inline zend_arena *zend_arena_create(size_t _size)
141 {
142 	zend_arena *arena = (zend_arena*) emalloc(
143 		sizeof(zend_arena) + sizeof(void *) * ZEND_TRACKED_ARENA_SIZE);
144 	arena->ptr = &arena->ptrs[0];
145 	arena->end = &arena->ptrs[ZEND_TRACKED_ARENA_SIZE];
146 	arena->prev = NULL;
147 	return arena;
148 }
149 
zend_arena_destroy(zend_arena * arena)150 static zend_always_inline void zend_arena_destroy(zend_arena *arena)
151 {
152 	do {
153 		zend_arena *prev = arena->prev;
154 		void **ptr;
155 		for (ptr = arena->ptrs; ptr < arena->ptr; ptr++) {
156 			efree(*ptr);
157 		}
158 		efree(arena);
159 		arena = prev;
160 	} while (arena);
161 }
162 
zend_arena_alloc(zend_arena ** arena_ptr,size_t size)163 static zend_always_inline void *zend_arena_alloc(zend_arena **arena_ptr, size_t size)
164 {
165 	zend_arena *arena = *arena_ptr;
166 	if (arena->ptr == arena->end) {
167 		*arena_ptr = zend_arena_create(0);
168 		(*arena_ptr)->prev = arena;
169 		arena = *arena_ptr;
170 	}
171 
172 	return *arena->ptr++ = emalloc(size);
173 }
174 
zend_arena_calloc(zend_arena ** arena_ptr,size_t count,size_t unit_size)175 static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
176 {
177 	bool overflow;
178 	size_t size;
179 	void *ret;
180 
181 	size = zend_safe_address(unit_size, count, 0, &overflow);
182 	if (UNEXPECTED(overflow)) {
183 		zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
184 	}
185 	ret = zend_arena_alloc(arena_ptr, size);
186 	memset(ret, 0, size);
187 	return ret;
188 }
189 
zend_arena_checkpoint(zend_arena * arena)190 static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
191 {
192 	return arena->ptr;
193 }
194 
zend_arena_release(zend_arena ** arena_ptr,void * checkpoint)195 static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
196 {
197 	while (1) {
198 		zend_arena *arena = *arena_ptr;
199 		zend_arena *prev = arena->prev;
200 		while (1) {
201 			if (arena->ptr == (void **) checkpoint) {
202 				return;
203 			}
204 			if (arena->ptr == arena->ptrs) {
205 				break;
206 			}
207 			arena->ptr--;
208 			efree(*arena->ptr);
209 		}
210 		efree(arena);
211 		*arena_ptr = prev;
212 		ZEND_ASSERT(*arena_ptr);
213 	}
214 }
215 
zend_arena_contains(zend_arena * arena,void * ptr)216 static zend_always_inline zend_bool zend_arena_contains(zend_arena *arena, void *ptr)
217 {
218 	/* TODO: Dummy */
219 	return 1;
220 }
221 
222 #endif
223 
224 #endif /* _ZEND_ARENA_H_ */
225