/src/php-src/ext/opcache/zend_persist.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend OPcache | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © The PHP Group and Contributors. | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to the Modified BSD License that is | |
8 | | | bundled with this package in the file LICENSE, and is available | |
9 | | | through the World Wide Web at <https://www.php.net/license/>. | |
10 | | | | |
11 | | | SPDX-License-Identifier: BSD-3-Clause | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Andi Gutmans <andi@php.net> | |
14 | | | Zeev Suraski <zeev@php.net> | |
15 | | | Stanislav Malyshev <stas@zend.com> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "zend.h" |
21 | | #include "ZendAccelerator.h" |
22 | | #include "zend_persist.h" |
23 | | #include "zend_extensions.h" |
24 | | #include "zend_shared_alloc.h" |
25 | | #include "zend_vm.h" |
26 | | #include "zend_constants.h" |
27 | | #include "zend_operators.h" |
28 | | #include "zend_interfaces.h" |
29 | | #include "zend_attributes.h" |
30 | | |
31 | | #ifdef HAVE_JIT |
32 | | # include "Optimizer/zend_func_info.h" |
33 | | # include "jit/zend_jit.h" |
34 | | #endif |
35 | | |
36 | 50.2k | #define zend_set_str_gc_flags(str) do { \ |
37 | 50.2k | GC_SET_REFCOUNT(str, 2); \ |
38 | 50.2k | uint32_t flags = GC_STRING | (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0); \ |
39 | 50.2k | if (file_cache_only \ |
40 | 50.2k | || (ZCG(current_persistent_script) && ZCG(current_persistent_script)->corrupted)) { \ |
41 | 0 | GC_TYPE_INFO(str) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT); \ |
42 | 0 | flags |= (IS_STR_INTERNED << GC_FLAGS_SHIFT); \ |
43 | 50.2k | } else { \ |
44 | 50.2k | flags |= ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \ |
45 | 50.2k | } \ |
46 | 50.2k | GC_TYPE_INFO(str) = flags; \ |
47 | 50.2k | } while (0) |
48 | | |
49 | 87.0k | #define zend_accel_store_string(str) do { \ |
50 | 87.0k | zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \ |
51 | 87.0k | if (new_str) { \ |
52 | 36.8k | zend_string_release_ex(str, 0); \ |
53 | 36.8k | str = new_str; \ |
54 | 50.2k | } else { \ |
55 | 50.2k | new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \ |
56 | 50.2k | zend_string_release_ex(str, 0); \ |
57 | 50.2k | str = new_str; \ |
58 | 50.2k | zend_string_hash_val(str); \ |
59 | 50.2k | zend_set_str_gc_flags(str); \ |
60 | 50.2k | } \ |
61 | 87.0k | } while (0) |
62 | | #define zend_accel_memdup_string(str) do { \ |
63 | | zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \ |
64 | | if (new_str) { \ |
65 | | str = new_str; \ |
66 | | } else { \ |
67 | | new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \ |
68 | | str = new_str; \ |
69 | | zend_string_hash_val(str); \ |
70 | | zend_set_str_gc_flags(str); \ |
71 | | } \ |
72 | | } while (0) |
73 | 634k | #define zend_accel_store_interned_string(str) do { \ |
74 | 634k | if (!IS_ACCEL_INTERNED(str)) { \ |
75 | 30.6k | zend_accel_store_string(str); \ |
76 | 30.6k | } \ |
77 | 634k | } while (0) |
78 | | #define zend_accel_memdup_interned_string(str) do { \ |
79 | | if (!IS_ACCEL_INTERNED(str)) { \ |
80 | | zend_accel_memdup_string(str); \ |
81 | | } \ |
82 | | } while (0) |
83 | | |
84 | | typedef void (*zend_persist_func_t)(zval*); |
85 | | |
86 | | static void zend_persist_zval(zval *z); |
87 | | static void zend_persist_op_array(zval *zv); |
88 | | |
89 | | static const uint32_t uninitialized_bucket[-HT_MIN_MASK] = |
90 | | {HT_INVALID_IDX, HT_INVALID_IDX}; |
91 | | |
92 | | static void zend_hash_persist(HashTable *ht) |
93 | 96.9k | { |
94 | 96.9k | uint32_t idx, nIndex; |
95 | 96.9k | Bucket *p; |
96 | | |
97 | 96.9k | HT_FLAGS(ht) |= HASH_FLAG_STATIC_KEYS; |
98 | 96.9k | ht->pDestructor = NULL; |
99 | 96.9k | ht->nInternalPointer = 0; |
100 | | |
101 | 96.9k | if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) { |
102 | 60.0k | if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) { |
103 | 60.0k | HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket)); |
104 | 60.0k | } else { |
105 | 0 | HT_SET_DATA_ADDR(ht, &uninitialized_bucket); |
106 | 0 | } |
107 | 60.0k | return; |
108 | 60.0k | } |
109 | 36.8k | if (ht->nNumUsed == 0) { |
110 | 0 | efree(HT_GET_DATA_ADDR(ht)); |
111 | 0 | ht->nTableMask = HT_MIN_MASK; |
112 | 0 | if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) { |
113 | 0 | HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket)); |
114 | 0 | } else { |
115 | 0 | HT_SET_DATA_ADDR(ht, &uninitialized_bucket); |
116 | 0 | } |
117 | 0 | HT_FLAGS(ht) |= HASH_FLAG_UNINITIALIZED; |
118 | 0 | return; |
119 | 0 | } |
120 | 36.8k | if (HT_IS_PACKED(ht)) { |
121 | 9.26k | void *data = HT_GET_DATA_ADDR(ht); |
122 | 9.26k | if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) { |
123 | 0 | data = zend_shared_memdup(data, HT_PACKED_USED_SIZE(ht)); |
124 | 9.26k | } else { |
125 | 9.26k | data = zend_shared_memdup_free(data, HT_PACKED_USED_SIZE(ht)); |
126 | 9.26k | } |
127 | 9.26k | HT_SET_DATA_ADDR(ht, data); |
128 | 27.5k | } else if (ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) { |
129 | | /* compact table */ |
130 | 8 | void *old_data = HT_GET_DATA_ADDR(ht); |
131 | 8 | Bucket *old_buckets = ht->arData; |
132 | 8 | uint32_t hash_size; |
133 | | |
134 | 8 | hash_size = (uint32_t)(-(int32_t)ht->nTableMask); |
135 | 16 | while (hash_size >> 2 > ht->nNumUsed) { |
136 | 8 | hash_size >>= 1; |
137 | 8 | } |
138 | 8 | ht->nTableMask = (uint32_t)(-(int32_t)hash_size); |
139 | 8 | ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */ |
140 | 8 | HT_SET_DATA_ADDR(ht, ZCG(mem)); |
141 | 8 | ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE((hash_size * sizeof(uint32_t)) + (ht->nNumUsed * sizeof(Bucket)))); |
142 | 8 | HT_HASH_RESET(ht); |
143 | 8 | memcpy(ht->arData, old_buckets, ht->nNumUsed * sizeof(Bucket)); |
144 | 8 | if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { |
145 | 8 | efree(old_data); |
146 | 8 | } |
147 | | |
148 | | /* rehash */ |
149 | 128 | for (idx = 0; idx < ht->nNumUsed; idx++) { |
150 | 120 | p = ht->arData + idx; |
151 | 120 | if (Z_TYPE(p->val) == IS_UNDEF) continue; |
152 | 120 | nIndex = p->h | ht->nTableMask; |
153 | 120 | Z_NEXT(p->val) = HT_HASH(ht, nIndex); |
154 | 120 | HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx); |
155 | 120 | } |
156 | 27.5k | } else { |
157 | 27.5k | void *data = ZCG(mem); |
158 | 27.5k | void *old_data = HT_GET_DATA_ADDR(ht); |
159 | | |
160 | 27.5k | ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */ |
161 | 27.5k | ZCG(mem) = (void*)((char*)data + ZEND_ALIGNED_SIZE(HT_USED_SIZE(ht))); |
162 | 27.5k | memcpy(data, old_data, HT_USED_SIZE(ht)); |
163 | 27.5k | if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) { |
164 | 27.5k | efree(old_data); |
165 | 27.5k | } |
166 | 27.5k | HT_SET_DATA_ADDR(ht, data); |
167 | 27.5k | } |
168 | 36.8k | } |
169 | | |
170 | | static zend_ast *zend_persist_ast(zend_ast *ast) |
171 | 8.44k | { |
172 | 8.44k | uint32_t i; |
173 | 8.44k | zend_ast *node; |
174 | | |
175 | 8.44k | if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) { |
176 | 5.16k | zend_ast_zval *copy = zend_shared_memdup(ast, sizeof(zend_ast_zval)); |
177 | 5.16k | zend_persist_zval(©->val); |
178 | 5.16k | node = (zend_ast *) copy; |
179 | 5.16k | } else if (zend_ast_is_list(ast)) { |
180 | 501 | zend_ast_list *list = zend_ast_get_list(ast); |
181 | 501 | zend_ast_list *copy = zend_shared_memdup(ast, |
182 | 501 | sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children); |
183 | 1.13k | for (i = 0; i < list->children; i++) { |
184 | 633 | if (copy->child[i]) { |
185 | 633 | copy->child[i] = zend_persist_ast(copy->child[i]); |
186 | 633 | } |
187 | 633 | } |
188 | 501 | node = (zend_ast *) copy; |
189 | 2.77k | } else if (ast->kind == ZEND_AST_OP_ARRAY) { |
190 | 32 | zend_ast_op_array *copy = zend_shared_memdup(ast, sizeof(zend_ast_op_array)); |
191 | 32 | zval z; |
192 | 32 | ZVAL_PTR(&z, copy->op_array); |
193 | 32 | zend_persist_op_array(&z); |
194 | 32 | copy->op_array = Z_PTR(z); |
195 | 32 | node = (zend_ast *) copy; |
196 | 2.74k | } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
197 | 76 | zend_ast_fcc *copy = zend_shared_memdup(ast, sizeof(zend_ast_fcc)); |
198 | 76 | copy->args = zend_persist_ast(copy->args); |
199 | 76 | node = (zend_ast *) copy; |
200 | 2.67k | } else if (zend_ast_is_decl(ast)) { |
201 | | /* Not implemented. */ |
202 | 0 | ZEND_UNREACHABLE(); |
203 | 2.67k | } else { |
204 | 2.67k | uint32_t children = zend_ast_get_num_children(ast); |
205 | 2.67k | node = zend_shared_memdup(ast, zend_ast_size(children)); |
206 | 8.97k | for (i = 0; i < children; i++) { |
207 | 6.30k | if (node->child[i]) { |
208 | 5.63k | node->child[i] = zend_persist_ast(node->child[i]); |
209 | 5.63k | } |
210 | 6.30k | } |
211 | 2.67k | } |
212 | | |
213 | 8.44k | return node; |
214 | 8.44k | } |
215 | | |
216 | | static void zend_persist_zval(zval *z) |
217 | 534k | { |
218 | 534k | void *new_ptr; |
219 | | |
220 | 534k | switch (Z_TYPE_P(z)) { |
221 | 315k | case IS_STRING: |
222 | 315k | zend_accel_store_interned_string(Z_STR_P(z)); |
223 | 315k | Z_TYPE_FLAGS_P(z) = 0; |
224 | 315k | break; |
225 | 11.1k | case IS_ARRAY: |
226 | 11.1k | new_ptr = zend_shared_alloc_get_xlat_entry(Z_ARR_P(z)); |
227 | 11.1k | if (new_ptr) { |
228 | 218 | Z_ARR_P(z) = new_ptr; |
229 | 218 | Z_TYPE_FLAGS_P(z) = 0; |
230 | 10.9k | } else if (!ZCG(current_persistent_script)->corrupted |
231 | 10.9k | && zend_accel_in_shm(Z_ARR_P(z))) { |
232 | | /* pass */ |
233 | 10.9k | } else { |
234 | 10.9k | HashTable *ht; |
235 | | |
236 | 10.9k | if (!Z_REFCOUNTED_P(z)) { |
237 | 1.77k | ht = zend_shared_memdup_put(Z_ARR_P(z), sizeof(zend_array)); |
238 | 9.15k | } else { |
239 | 9.15k | GC_REMOVE_FROM_BUFFER(Z_ARR_P(z)); |
240 | 9.15k | ht = zend_shared_memdup_put_free(Z_ARR_P(z), sizeof(zend_array)); |
241 | 9.15k | } |
242 | 10.9k | Z_ARR_P(z) = ht; |
243 | 10.9k | zend_hash_persist(ht); |
244 | 10.9k | if (HT_IS_PACKED(ht)) { |
245 | 8.03k | zval *zv; |
246 | | |
247 | 276k | ZEND_HASH_PACKED_FOREACH_VAL(ht, zv) { |
248 | 276k | zend_persist_zval(zv); |
249 | 276k | } ZEND_HASH_FOREACH_END(); |
250 | 8.03k | } else { |
251 | 2.89k | Bucket *p; |
252 | | |
253 | 10.4k | ZEND_HASH_MAP_FOREACH_BUCKET(ht, p) { |
254 | 10.4k | if (p->key) { |
255 | 1.70k | zend_accel_store_interned_string(p->key); |
256 | 1.70k | } |
257 | 10.4k | zend_persist_zval(&p->val); |
258 | 10.4k | } ZEND_HASH_FOREACH_END(); |
259 | 2.89k | } |
260 | | /* make immutable array */ |
261 | 10.9k | Z_TYPE_FLAGS_P(z) = 0; |
262 | 10.9k | GC_SET_REFCOUNT(Z_COUNTED_P(z), 2); |
263 | 10.9k | GC_ADD_FLAGS(Z_COUNTED_P(z), IS_ARRAY_IMMUTABLE); |
264 | 10.9k | } |
265 | 11.1k | break; |
266 | 11.1k | case IS_CONSTANT_AST: |
267 | 2.15k | new_ptr = zend_shared_alloc_get_xlat_entry(Z_AST_P(z)); |
268 | 2.15k | if (new_ptr) { |
269 | 18 | Z_AST_P(z) = new_ptr; |
270 | 18 | Z_TYPE_FLAGS_P(z) = 0; |
271 | 2.14k | } else if (ZCG(current_persistent_script)->corrupted |
272 | 2.14k | || !zend_accel_in_shm(Z_AST_P(z))) { |
273 | 2.09k | zend_ast_ref *old_ref = Z_AST_P(z); |
274 | 2.09k | Z_AST_P(z) = zend_shared_memdup_put(Z_AST_P(z), sizeof(zend_ast_ref)); |
275 | 2.09k | zend_persist_ast(GC_AST(old_ref)); |
276 | 2.09k | Z_TYPE_FLAGS_P(z) = 0; |
277 | 2.09k | GC_SET_REFCOUNT(Z_COUNTED_P(z), 1); |
278 | 2.09k | GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE); |
279 | 2.09k | efree(old_ref); |
280 | 2.09k | } |
281 | 2.15k | break; |
282 | 72 | case IS_PTR: |
283 | 72 | break; |
284 | 205k | default: |
285 | 205k | ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING); |
286 | 205k | break; |
287 | 534k | } |
288 | 534k | } |
289 | | |
290 | | static HashTable *zend_persist_attributes(HashTable *attributes) |
291 | 1.24k | { |
292 | 1.24k | uint32_t i; |
293 | 1.24k | zval *v; |
294 | | |
295 | 1.24k | if (!ZCG(current_persistent_script)->corrupted && zend_accel_in_shm(attributes)) { |
296 | 12 | return attributes; |
297 | 12 | } |
298 | | |
299 | | /* Attributes for trait properties may be shared if preloading is used. */ |
300 | 1.22k | HashTable *xlat = zend_shared_alloc_get_xlat_entry(attributes); |
301 | 1.22k | if (xlat) { |
302 | 0 | return xlat; |
303 | 0 | } |
304 | | |
305 | 1.22k | zend_hash_persist(attributes); |
306 | | |
307 | 5.60k | ZEND_HASH_PACKED_FOREACH_VAL(attributes, v) { |
308 | 5.60k | zend_attribute *attr = Z_PTR_P(v); |
309 | 5.60k | zend_attribute *copy = zend_shared_memdup_put_free(attr, ZEND_ATTRIBUTE_SIZE(attr->argc)); |
310 | | |
311 | 5.60k | zend_accel_store_interned_string(copy->name); |
312 | 5.60k | zend_accel_store_interned_string(copy->lcname); |
313 | 5.60k | if (copy->validation_error) { |
314 | 44 | zend_accel_store_interned_string(copy->validation_error); |
315 | 44 | } |
316 | | |
317 | 5.60k | for (i = 0; i < copy->argc; i++) { |
318 | 582 | if (copy->args[i].name) { |
319 | 82 | zend_accel_store_interned_string(copy->args[i].name); |
320 | 82 | } |
321 | 582 | zend_persist_zval(©->args[i].value); |
322 | 582 | } |
323 | | |
324 | 5.60k | ZVAL_PTR(v, copy); |
325 | 5.60k | } ZEND_HASH_FOREACH_END(); |
326 | | |
327 | 1.22k | HashTable *ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable)); |
328 | 1.22k | GC_SET_REFCOUNT(ptr, 2); |
329 | 1.22k | GC_TYPE_INFO(ptr) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT); |
330 | | |
331 | 1.22k | return ptr; |
332 | 1.22k | } |
333 | | |
334 | | uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name) |
335 | 13.7k | { |
336 | 13.7k | uint32_t ret; |
337 | | |
338 | 13.7k | if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF)) || |
339 | 13.7k | zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
340 | 44 | return 0; |
341 | 44 | } |
342 | | |
343 | | /* We use type.name.gc.refcount to keep map_ptr of corresponding type */ |
344 | 13.7k | if (ZSTR_HAS_CE_CACHE(type_name)) { |
345 | 12.7k | return GC_REFCOUNT(type_name); |
346 | 12.7k | } |
347 | | |
348 | 1.00k | if ((GC_FLAGS(type_name) & GC_IMMUTABLE) |
349 | 1.00k | && (GC_FLAGS(type_name) & IS_STR_PERMANENT)) { |
350 | 1.00k | do { |
351 | 1.00k | ret = ZEND_MAP_PTR_NEW_OFFSET(); |
352 | 1.00k | } while (ret <= 2); |
353 | 1.00k | GC_SET_REFCOUNT(type_name, ret); |
354 | 1.00k | GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR); |
355 | 1.00k | return ret; |
356 | 1.00k | } |
357 | | |
358 | 0 | return 0; |
359 | 1.00k | } |
360 | | |
361 | 27.1k | static void zend_persist_type(zend_type *type) { |
362 | 27.1k | if (ZEND_TYPE_HAS_LIST(*type)) { |
363 | 818 | zend_type_list *list = ZEND_TYPE_LIST(*type); |
364 | 818 | if (ZEND_TYPE_USES_ARENA(*type) || zend_accel_in_shm(list)) { |
365 | 818 | list = zend_shared_memdup_put(list, ZEND_TYPE_LIST_SIZE(list->num_types)); |
366 | 818 | ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT; |
367 | 818 | } else { |
368 | 0 | list = zend_shared_memdup_put_free(list, ZEND_TYPE_LIST_SIZE(list->num_types)); |
369 | 0 | } |
370 | 818 | ZEND_TYPE_SET_PTR(*type, list); |
371 | 818 | } |
372 | | |
373 | 27.1k | zend_type *single_type; |
374 | 55.0k | ZEND_TYPE_FOREACH_MUTABLE(*type, single_type) { |
375 | 55.0k | if (ZEND_TYPE_HAS_LIST(*single_type)) { |
376 | 266 | zend_persist_type(single_type); |
377 | 266 | continue; |
378 | 266 | } |
379 | 27.6k | if (ZEND_TYPE_HAS_NAME(*single_type)) { |
380 | 3.52k | zend_string *type_name = ZEND_TYPE_NAME(*single_type); |
381 | 3.52k | zend_accel_store_interned_string(type_name); |
382 | 3.52k | ZEND_TYPE_SET_PTR(*single_type, type_name); |
383 | 3.52k | if (!ZCG(current_persistent_script)->corrupted) { |
384 | 3.52k | zend_accel_get_class_name_map_ptr(type_name); |
385 | 3.52k | } |
386 | 3.52k | } |
387 | 27.6k | } ZEND_TYPE_FOREACH_END(); |
388 | 27.1k | } |
389 | | |
390 | | static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_script* main_persistent_script) |
391 | 47.9k | { |
392 | 47.9k | zend_op *persist_ptr; |
393 | 47.9k | zval *orig_literals = NULL; |
394 | | |
395 | 47.9k | if (op_array->refcount && --(*op_array->refcount) == 0) { |
396 | 44.8k | efree(op_array->refcount); |
397 | 44.8k | } |
398 | 47.9k | op_array->refcount = NULL; |
399 | | |
400 | 47.9k | if (main_persistent_script) { |
401 | 23.5k | zend_execute_data *orig_execute_data = EG(current_execute_data); |
402 | 23.5k | zend_execute_data fake_execute_data; |
403 | 23.5k | zval *offset; |
404 | | |
405 | 23.5k | memset(&fake_execute_data, 0, sizeof(fake_execute_data)); |
406 | 23.5k | fake_execute_data.func = (zend_function*)op_array; |
407 | 23.5k | EG(current_execute_data) = &fake_execute_data; |
408 | 23.5k | if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) { |
409 | 8 | main_persistent_script->compiler_halt_offset = Z_LVAL_P(offset); |
410 | 8 | } |
411 | 23.5k | EG(current_execute_data) = orig_execute_data; |
412 | 23.5k | } |
413 | | |
414 | 47.9k | if (op_array->function_name) { |
415 | 24.3k | zend_string *old_name = op_array->function_name; |
416 | 24.3k | zend_accel_store_interned_string(op_array->function_name); |
417 | | /* Remember old function name, so it can be released multiple times if shared. */ |
418 | 24.3k | if (op_array->function_name != old_name |
419 | 598 | && !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) { |
420 | 598 | zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name); |
421 | 598 | } |
422 | 24.3k | } |
423 | | |
424 | 47.9k | if (op_array->scope) { |
425 | 13.7k | zend_class_entry *scope = zend_shared_alloc_get_xlat_entry(op_array->scope); |
426 | | |
427 | 13.7k | if (scope) { |
428 | 13.7k | op_array->scope = scope; |
429 | 13.7k | } |
430 | | |
431 | 13.7k | if (op_array->prototype) { |
432 | 1.76k | zend_function *ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype); |
433 | | |
434 | 1.76k | if (ptr) { |
435 | 437 | op_array->prototype = ptr; |
436 | 437 | } |
437 | 1.76k | } |
438 | | |
439 | 13.7k | persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->opcodes); |
440 | 13.7k | if (persist_ptr) { |
441 | 0 | op_array->opcodes = persist_ptr; |
442 | 0 | if (op_array->static_variables) { |
443 | 0 | op_array->static_variables = zend_shared_alloc_get_xlat_entry(op_array->static_variables); |
444 | 0 | ZEND_ASSERT(op_array->static_variables != NULL); |
445 | 0 | } |
446 | 0 | if (op_array->literals) { |
447 | 0 | op_array->literals = zend_shared_alloc_get_xlat_entry(op_array->literals); |
448 | 0 | ZEND_ASSERT(op_array->literals != NULL); |
449 | 0 | } |
450 | 0 | if (op_array->filename) { |
451 | 0 | op_array->filename = zend_shared_alloc_get_xlat_entry(op_array->filename); |
452 | 0 | ZEND_ASSERT(op_array->filename != NULL); |
453 | 0 | } |
454 | 0 | if (op_array->arg_info) { |
455 | 0 | zend_arg_info *arg_info = op_array->arg_info; |
456 | 0 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
457 | 0 | arg_info--; |
458 | 0 | } |
459 | 0 | arg_info = zend_shared_alloc_get_xlat_entry(arg_info); |
460 | 0 | ZEND_ASSERT(arg_info != NULL); |
461 | 0 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
462 | 0 | arg_info++; |
463 | 0 | } |
464 | 0 | op_array->arg_info = arg_info; |
465 | 0 | } |
466 | 0 | if (op_array->live_range) { |
467 | 0 | op_array->live_range = zend_shared_alloc_get_xlat_entry(op_array->live_range); |
468 | 0 | ZEND_ASSERT(op_array->live_range != NULL); |
469 | 0 | } |
470 | 0 | if (op_array->doc_comment) { |
471 | 0 | if (ZCG(accel_directives).save_comments) { |
472 | 0 | op_array->doc_comment = zend_shared_alloc_get_xlat_entry(op_array->doc_comment); |
473 | 0 | ZEND_ASSERT(op_array->doc_comment != NULL); |
474 | 0 | } else { |
475 | 0 | op_array->doc_comment = NULL; |
476 | 0 | } |
477 | 0 | } |
478 | 0 | if (op_array->attributes) { |
479 | 0 | op_array->attributes = zend_shared_alloc_get_xlat_entry(op_array->attributes); |
480 | 0 | ZEND_ASSERT(op_array->attributes != NULL); |
481 | 0 | } |
482 | |
|
483 | 0 | if (op_array->try_catch_array) { |
484 | 0 | op_array->try_catch_array = zend_shared_alloc_get_xlat_entry(op_array->try_catch_array); |
485 | 0 | ZEND_ASSERT(op_array->try_catch_array != NULL); |
486 | 0 | } |
487 | 0 | if (op_array->vars) { |
488 | 0 | op_array->vars = zend_shared_alloc_get_xlat_entry(op_array->vars); |
489 | 0 | ZEND_ASSERT(op_array->vars != NULL); |
490 | 0 | } |
491 | 0 | if (op_array->dynamic_func_defs) { |
492 | 0 | op_array->dynamic_func_defs = zend_shared_alloc_get_xlat_entry(op_array->dynamic_func_defs); |
493 | 0 | ZEND_ASSERT(op_array->dynamic_func_defs != NULL); |
494 | 0 | } |
495 | 0 | ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem)))); |
496 | 0 | return; |
497 | 0 | } |
498 | 34.2k | } else { |
499 | | /* "prototype" may be undefined if "scope" isn't set */ |
500 | 34.2k | op_array->prototype = NULL; |
501 | 34.2k | } |
502 | | |
503 | 47.9k | if (op_array->scope |
504 | 13.7k | && !(op_array->fn_flags & ZEND_ACC_CLOSURE) |
505 | 13.7k | && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) { |
506 | 2.26k | return; |
507 | 2.26k | } |
508 | | |
509 | 45.6k | if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) { |
510 | 1.45k | Bucket *p; |
511 | | |
512 | 1.45k | zend_hash_persist(op_array->static_variables); |
513 | 15.2k | ZEND_HASH_MAP_FOREACH_BUCKET(op_array->static_variables, p) { |
514 | 15.2k | ZEND_ASSERT(p->key != NULL); |
515 | 15.2k | zend_accel_store_interned_string(p->key); |
516 | 6.14k | zend_persist_zval(&p->val); |
517 | 6.14k | } ZEND_HASH_FOREACH_END(); |
518 | 1.45k | op_array->static_variables = zend_shared_memdup_put_free(op_array->static_variables, sizeof(HashTable)); |
519 | | /* make immutable array */ |
520 | 1.45k | GC_SET_REFCOUNT(op_array->static_variables, 2); |
521 | 1.45k | GC_TYPE_INFO(op_array->static_variables) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT); |
522 | 1.45k | } |
523 | | |
524 | 45.6k | if (op_array->literals) { |
525 | 44.7k | zval *p, *end; |
526 | | |
527 | 44.7k | orig_literals = op_array->literals; |
528 | | #if ZEND_USE_ABS_CONST_ADDR |
529 | | p = zend_shared_memdup_put_free(op_array->literals, sizeof(zval) * op_array->last_literal); |
530 | | #else |
531 | 44.7k | p = zend_shared_memdup_put(op_array->literals, sizeof(zval) * op_array->last_literal); |
532 | 44.7k | #endif |
533 | 44.7k | end = p + op_array->last_literal; |
534 | 44.7k | op_array->literals = p; |
535 | 424k | while (p < end) { |
536 | 379k | zend_persist_zval(p); |
537 | 379k | p++; |
538 | 379k | } |
539 | 44.7k | } |
540 | | |
541 | 45.6k | { |
542 | 45.6k | zend_op *new_opcodes = zend_shared_memdup_put(op_array->opcodes, sizeof(zend_op) * op_array->last); |
543 | 45.6k | zend_op *opline = new_opcodes; |
544 | 45.6k | zend_op *end = new_opcodes + op_array->last; |
545 | | |
546 | 1.17M | for (; opline < end ; opline++) { |
547 | | #if ZEND_USE_ABS_CONST_ADDR |
548 | | if (opline->op1_type == IS_CONST) { |
549 | | opline->op1.zv = (zval*)((char*)opline->op1.zv + ((char*)op_array->literals - (char*)orig_literals)); |
550 | | if (opline->opcode == ZEND_SEND_VAL |
551 | | || opline->opcode == ZEND_SEND_VAL_EX |
552 | | || opline->opcode == ZEND_QM_ASSIGN) { |
553 | | /* Update handlers to eliminate REFCOUNTED check */ |
554 | | zend_vm_set_opcode_handler_ex(opline, 1 << Z_TYPE_P(opline->op1.zv), 0, 0); |
555 | | } |
556 | | } |
557 | | if (opline->op2_type == IS_CONST) { |
558 | | opline->op2.zv = (zval*)((char*)opline->op2.zv + ((char*)op_array->literals - (char*)orig_literals)); |
559 | | } |
560 | | #else |
561 | 1.13M | if (opline->op1_type == IS_CONST) { |
562 | 198k | opline->op1.constant = |
563 | 198k | (char*)(op_array->literals + |
564 | 198k | ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) + |
565 | 198k | (int32_t)opline->op1.constant) - orig_literals)) - |
566 | 198k | (char*)opline; |
567 | 198k | if (opline->opcode == ZEND_SEND_VAL |
568 | 173k | || opline->opcode == ZEND_SEND_VAL_EX |
569 | 168k | || opline->opcode == ZEND_QM_ASSIGN) { |
570 | 33.8k | zend_vm_set_opcode_handler_ex(opline, 0, 0, 0); |
571 | 33.8k | } |
572 | 198k | } |
573 | 1.13M | if (opline->op2_type == IS_CONST) { |
574 | 286k | opline->op2.constant = |
575 | 286k | (char*)(op_array->literals + |
576 | 286k | ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) + |
577 | 286k | (int32_t)opline->op2.constant) - orig_literals)) - |
578 | 286k | (char*)opline; |
579 | 286k | } |
580 | 1.13M | #endif |
581 | | #if ZEND_USE_ABS_JMP_ADDR |
582 | | if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) { |
583 | | /* fix jumps to point to new array */ |
584 | | switch (opline->opcode) { |
585 | | case ZEND_JMP: |
586 | | case ZEND_FAST_CALL: |
587 | | opline->op1.jmp_addr = &new_opcodes[opline->op1.jmp_addr - op_array->opcodes]; |
588 | | break; |
589 | | case ZEND_JMPZ: |
590 | | case ZEND_JMPNZ: |
591 | | case ZEND_JMPZ_EX: |
592 | | case ZEND_JMPNZ_EX: |
593 | | case ZEND_JMP_SET: |
594 | | case ZEND_COALESCE: |
595 | | case ZEND_FE_RESET_R: |
596 | | case ZEND_FE_RESET_RW: |
597 | | case ZEND_ASSERT_CHECK: |
598 | | case ZEND_JMP_NULL: |
599 | | case ZEND_BIND_INIT_STATIC_OR_JMP: |
600 | | case ZEND_JMP_FRAMELESS: |
601 | | opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes]; |
602 | | break; |
603 | | case ZEND_CATCH: |
604 | | if (!(opline->extended_value & ZEND_LAST_CATCH)) { |
605 | | opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes]; |
606 | | } |
607 | | break; |
608 | | case ZEND_FE_FETCH_R: |
609 | | case ZEND_FE_FETCH_RW: |
610 | | case ZEND_SWITCH_LONG: |
611 | | case ZEND_SWITCH_STRING: |
612 | | case ZEND_MATCH: |
613 | | /* relative extended_value don't have to be changed */ |
614 | | break; |
615 | | } |
616 | | } |
617 | | #endif |
618 | 1.13M | if (opline->opcode == ZEND_OP_DATA && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) { |
619 | 72 | zval *literal = RT_CONSTANT(opline, opline->op1); |
620 | 72 | HashTable *attributes = Z_PTR_P(literal); |
621 | 72 | attributes = zend_persist_attributes(attributes); |
622 | 72 | ZVAL_PTR(literal, attributes); |
623 | 72 | } |
624 | 1.13M | } |
625 | | |
626 | 45.6k | efree(op_array->opcodes); |
627 | 45.6k | op_array->opcodes = new_opcodes; |
628 | 45.6k | } |
629 | | |
630 | 45.6k | if (op_array->filename) { |
631 | 45.6k | zend_accel_store_string(op_array->filename); |
632 | 45.6k | } |
633 | | |
634 | 45.6k | if (op_array->arg_info) { |
635 | 11.4k | zend_arg_info *arg_info = op_array->arg_info; |
636 | 11.4k | uint32_t num_args = op_array->num_args; |
637 | 11.4k | uint32_t i; |
638 | | |
639 | 11.4k | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
640 | 3.53k | arg_info--; |
641 | 3.53k | num_args++; |
642 | 3.53k | } |
643 | 11.4k | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
644 | 194 | num_args++; |
645 | 194 | } |
646 | 11.4k | arg_info = zend_shared_memdup_put_free(arg_info, sizeof(zend_arg_info) * num_args); |
647 | 28.5k | for (i = 0; i < num_args; i++) { |
648 | 17.0k | if (arg_info[i].name) { |
649 | 13.5k | zend_accel_store_interned_string(arg_info[i].name); |
650 | 13.5k | } |
651 | 17.0k | zend_persist_type(&arg_info[i].type); |
652 | 17.0k | if (arg_info[i].doc_comment) { |
653 | 0 | zend_accel_store_interned_string(arg_info[i].doc_comment); |
654 | 0 | } |
655 | 17.0k | } |
656 | 11.4k | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
657 | 3.53k | arg_info++; |
658 | 3.53k | } |
659 | 11.4k | op_array->arg_info = arg_info; |
660 | 11.4k | } |
661 | | |
662 | 45.6k | if (op_array->live_range) { |
663 | 23.5k | op_array->live_range = zend_shared_memdup_put_free(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range); |
664 | 23.5k | } |
665 | | |
666 | 45.6k | if (op_array->doc_comment) { |
667 | 10 | if (ZCG(accel_directives).save_comments) { |
668 | 10 | zend_accel_store_interned_string(op_array->doc_comment); |
669 | 10 | } else { |
670 | 0 | zend_string_release_ex(op_array->doc_comment, 0); |
671 | 0 | op_array->doc_comment = NULL; |
672 | 0 | } |
673 | 10 | } |
674 | | |
675 | 45.6k | if (op_array->attributes) { |
676 | 449 | op_array->attributes = zend_persist_attributes(op_array->attributes); |
677 | 449 | } |
678 | | |
679 | 45.6k | if (op_array->try_catch_array) { |
680 | 9.62k | op_array->try_catch_array = zend_shared_memdup_put_free(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch); |
681 | 9.62k | } |
682 | | |
683 | 45.6k | if (op_array->vars) { |
684 | 32.1k | int i; |
685 | 32.1k | op_array->vars = zend_shared_memdup_put_free(op_array->vars, sizeof(zend_string*) * op_array->last_var); |
686 | 205k | for (i = 0; i < op_array->last_var; i++) { |
687 | 173k | zend_accel_store_interned_string(op_array->vars[i]); |
688 | 173k | } |
689 | 32.1k | } |
690 | | |
691 | 45.6k | if (op_array->num_dynamic_func_defs) { |
692 | 2.48k | op_array->dynamic_func_defs = zend_shared_memdup_put_free( |
693 | 2.48k | op_array->dynamic_func_defs, sizeof(zend_function *) * op_array->num_dynamic_func_defs); |
694 | 6.41k | for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) { |
695 | 3.92k | zval tmp; |
696 | 3.92k | ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]); |
697 | 3.92k | zend_persist_op_array(&tmp); |
698 | 3.92k | op_array->dynamic_func_defs[i] = Z_PTR(tmp); |
699 | 3.92k | } |
700 | 2.48k | } |
701 | | |
702 | 45.6k | ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem)))); |
703 | 45.6k | } |
704 | | |
705 | | static void zend_persist_op_array(zval *zv) |
706 | 10.6k | { |
707 | 10.6k | zend_op_array *op_array = Z_PTR_P(zv); |
708 | 10.6k | zend_op_array *old_op_array; |
709 | 10.6k | ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION); |
710 | | |
711 | 10.6k | old_op_array = zend_shared_alloc_get_xlat_entry(op_array); |
712 | 10.6k | if (!old_op_array) { |
713 | 10.6k | op_array = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_op_array)); |
714 | 10.6k | zend_persist_op_array_ex(op_array, NULL); |
715 | 10.6k | if (!ZCG(current_persistent_script)->corrupted) { |
716 | 10.6k | op_array->fn_flags |= ZEND_ACC_IMMUTABLE; |
717 | 10.6k | ZEND_MAP_PTR_NEW(op_array->run_time_cache); |
718 | 10.6k | if (op_array->static_variables) { |
719 | 1.36k | ZEND_MAP_PTR_NEW(op_array->static_variables_ptr); |
720 | 1.36k | } |
721 | 10.6k | } |
722 | 10.6k | #ifdef HAVE_JIT |
723 | 10.6k | if (JIT_G(on) |
724 | 0 | && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS |
725 | 0 | && (!ZCG(current_persistent_script) |
726 | 0 | || !ZCG(current_persistent_script)->corrupted)) { |
727 | 0 | zend_jit_op_array(op_array, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL); |
728 | 0 | } |
729 | 10.6k | #endif |
730 | 10.6k | } else { |
731 | | /* This can happen during preloading, if a dynamic function definition is declared. */ |
732 | 0 | Z_PTR_P(zv) = old_op_array; |
733 | 0 | } |
734 | 10.6k | } |
735 | | |
736 | | static zend_op_array *zend_persist_class_method(zend_op_array *op_array, const zend_class_entry *ce) |
737 | 16.9k | { |
738 | 16.9k | zend_op_array *old_op_array; |
739 | | |
740 | 16.9k | if (op_array->type != ZEND_USER_FUNCTION) { |
741 | 2.14k | ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION); |
742 | 2.14k | if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) { |
743 | 2.14k | old_op_array = zend_shared_alloc_get_xlat_entry(op_array); |
744 | 2.14k | if (old_op_array) { |
745 | 0 | return old_op_array; |
746 | 2.14k | } else { |
747 | 2.14k | op_array = zend_shared_memdup_put(op_array, sizeof(zend_internal_function)); |
748 | 2.14k | if (op_array->scope) { |
749 | 2.14k | void *persist_ptr; |
750 | | |
751 | 2.14k | if ((persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->scope))) { |
752 | 0 | op_array->scope = (zend_class_entry*)persist_ptr; |
753 | 0 | } |
754 | 2.14k | if (op_array->prototype) { |
755 | 912 | if ((persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype))) { |
756 | 0 | op_array->prototype = (zend_function*)persist_ptr; |
757 | 0 | } |
758 | 912 | } |
759 | 2.14k | } |
760 | | // Real dynamically created internal functions like enum methods must have their own run_time_cache pointer. They're always on the same scope as their defining class. |
761 | | // However, copies - as caused by inheritance of internal methods - must retain the original run_time_cache pointer, shared with the source function. |
762 | 2.14k | if (!op_array->scope || (op_array->scope == ce && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE))) { |
763 | 0 | if (op_array->fn_flags & ZEND_ACC_PRELOADED) { |
764 | 0 | ZEND_MAP_PTR_NEW_STATIC(op_array->run_time_cache); |
765 | 0 | } else { |
766 | 0 | ZEND_MAP_PTR_NEW(op_array->run_time_cache); |
767 | 0 | } |
768 | 0 | } |
769 | 2.14k | } |
770 | 2.14k | } |
771 | 2.14k | return op_array; |
772 | 2.14k | } |
773 | | |
774 | 14.8k | if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE) |
775 | 196 | && !ZCG(current_persistent_script)->corrupted |
776 | 196 | && zend_accel_in_shm(op_array)) { |
777 | 192 | zend_shared_alloc_register_xlat_entry(op_array, op_array); |
778 | 192 | return op_array; |
779 | 192 | } |
780 | | |
781 | 14.6k | old_op_array = zend_shared_alloc_get_xlat_entry(op_array); |
782 | 14.6k | if (old_op_array) { |
783 | 943 | if (op_array->refcount && --(*op_array->refcount) == 0) { |
784 | 823 | efree(op_array->refcount); |
785 | 823 | } |
786 | | |
787 | | /* If op_array is shared, the function name refcount is still incremented for each use, |
788 | | * so we need to release it here. We remembered the original function name in xlat. */ |
789 | 943 | zend_string *old_function_name = |
790 | 943 | zend_shared_alloc_get_xlat_entry(&old_op_array->function_name); |
791 | 943 | if (old_function_name) { |
792 | 0 | zend_string_release_ex(old_function_name, 0); |
793 | 0 | } |
794 | 943 | return old_op_array; |
795 | 943 | } |
796 | | |
797 | 13.7k | op_array = zend_shared_memdup_put(op_array, sizeof(zend_op_array)); |
798 | 13.7k | zend_persist_op_array_ex(op_array, NULL); |
799 | 13.7k | if (ce->ce_flags & ZEND_ACC_IMMUTABLE) { |
800 | 13.7k | op_array->fn_flags |= ZEND_ACC_IMMUTABLE; |
801 | 13.7k | if (ce->ce_flags & ZEND_ACC_LINKED) { |
802 | 11.7k | ZEND_MAP_PTR_NEW(op_array->run_time_cache); |
803 | 11.7k | if (op_array->static_variables) { |
804 | 105 | ZEND_MAP_PTR_NEW(op_array->static_variables_ptr); |
805 | 105 | } |
806 | 11.7k | } else { |
807 | 1.99k | ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL); |
808 | 1.99k | ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL); |
809 | 1.99k | } |
810 | 13.7k | } |
811 | 13.7k | return op_array; |
812 | 14.6k | } |
813 | | |
814 | | static zend_property_info *zend_persist_property_info(zend_property_info *prop) |
815 | 7.78k | { |
816 | 7.78k | zend_class_entry *ce; |
817 | 7.78k | prop = zend_shared_memdup_put(prop, sizeof(zend_property_info)); |
818 | 7.78k | ce = zend_shared_alloc_get_xlat_entry(prop->ce); |
819 | 7.78k | if (ce) { |
820 | 7.78k | prop->ce = ce; |
821 | 7.78k | } |
822 | 7.78k | zend_accel_store_interned_string(prop->name); |
823 | 7.78k | if (prop->doc_comment) { |
824 | 140 | if (ZCG(accel_directives).save_comments) { |
825 | 140 | zend_accel_store_interned_string(prop->doc_comment); |
826 | 140 | } else { |
827 | 0 | if (!zend_shared_alloc_get_xlat_entry(prop->doc_comment)) { |
828 | 0 | zend_shared_alloc_register_xlat_entry(prop->doc_comment, prop->doc_comment); |
829 | 0 | } |
830 | 0 | zend_string_release_ex(prop->doc_comment, 0); |
831 | 0 | prop->doc_comment = NULL; |
832 | 0 | } |
833 | 140 | } |
834 | 7.78k | if (prop->attributes) { |
835 | 90 | prop->attributes = zend_persist_attributes(prop->attributes); |
836 | 90 | } |
837 | 7.78k | if (prop->prototype) { |
838 | 7.78k | const zend_property_info *new_prototype = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(prop->prototype); |
839 | 7.78k | if (new_prototype) { |
840 | 7.74k | prop->prototype = new_prototype; |
841 | 7.74k | } |
842 | 7.78k | } |
843 | 7.78k | if (prop->hooks) { |
844 | 748 | prop->hooks = zend_shared_memdup_put(prop->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
845 | 2.24k | for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) { |
846 | 1.49k | if (prop->hooks[i]) { |
847 | 964 | zend_op_array *hook = zend_persist_class_method(&prop->hooks[i]->op_array, ce); |
848 | 964 | #ifdef HAVE_JIT |
849 | 964 | if (JIT_G(on) |
850 | 0 | && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS |
851 | 0 | && (!ZCG(current_persistent_script) |
852 | 0 | || !ZCG(current_persistent_script)->corrupted)) { |
853 | 0 | if (hook->scope == ce && !(hook->fn_flags & ZEND_ACC_TRAIT_CLONE)) { |
854 | 0 | zend_jit_op_array(hook, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL); |
855 | 0 | } |
856 | 0 | } |
857 | 964 | #endif |
858 | 964 | const zend_property_info *new_prop_info = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(hook->prop_info); |
859 | 964 | if (new_prop_info) { |
860 | 940 | hook->prop_info = new_prop_info; |
861 | 940 | } |
862 | 964 | prop->hooks[i] = (zend_function *) hook; |
863 | 964 | } |
864 | 1.49k | } |
865 | 748 | } |
866 | 7.78k | zend_persist_type(&prop->type); |
867 | 7.78k | return prop; |
868 | 7.78k | } |
869 | | |
870 | | static void zend_persist_class_constant(zval *zv) |
871 | 2.50k | { |
872 | 2.50k | const zend_class_constant *orig_c = Z_PTR_P(zv); |
873 | 2.50k | zend_class_constant *c = zend_shared_alloc_get_xlat_entry(orig_c); |
874 | 2.50k | zend_class_entry *ce; |
875 | | |
876 | 2.50k | if (c) { |
877 | 94 | Z_PTR_P(zv) = c; |
878 | 94 | return; |
879 | 2.41k | } else if (((orig_c->ce->ce_flags & ZEND_ACC_IMMUTABLE) && !(Z_CONSTANT_FLAGS(orig_c->value) & CONST_OWNED)) |
880 | 2.33k | || orig_c->ce->type == ZEND_INTERNAL_CLASS) { |
881 | | /* Class constant comes from a different file in shm or internal class, keep existing pointer. */ |
882 | 410 | return; |
883 | 2.00k | } else if (!ZCG(current_persistent_script)->corrupted |
884 | 2.00k | && zend_accel_in_shm(Z_PTR_P(zv))) { |
885 | 0 | return; |
886 | 0 | } |
887 | 2.00k | c = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_class_constant)); |
888 | 2.00k | zend_persist_zval(&c->value); |
889 | 2.00k | ce = zend_shared_alloc_get_xlat_entry(c->ce); |
890 | 2.00k | if (ce) { |
891 | 1.99k | c->ce = ce; |
892 | 1.99k | } |
893 | 2.00k | if (c->doc_comment) { |
894 | 10 | if (ZCG(accel_directives).save_comments) { |
895 | 10 | zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment); |
896 | 10 | if (doc_comment) { |
897 | 0 | c->doc_comment = doc_comment; |
898 | 10 | } else { |
899 | 10 | zend_accel_store_interned_string(c->doc_comment); |
900 | 10 | } |
901 | 10 | } else { |
902 | 0 | zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment); |
903 | 0 | if (!doc_comment) { |
904 | 0 | zend_shared_alloc_register_xlat_entry(c->doc_comment, c->doc_comment); |
905 | 0 | zend_string_release_ex(c->doc_comment, 0); |
906 | 0 | } |
907 | 0 | c->doc_comment = NULL; |
908 | 0 | } |
909 | 10 | } |
910 | 2.00k | if (c->attributes) { |
911 | 74 | c->attributes = zend_persist_attributes(c->attributes); |
912 | 74 | } |
913 | 2.00k | zend_persist_type(&c->type); |
914 | 2.00k | } |
915 | | |
916 | | zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce) |
917 | 12.0k | { |
918 | 12.0k | Bucket *p; |
919 | 12.0k | zend_class_entry *ce = orig_ce; |
920 | | |
921 | 12.0k | if (ce->type == ZEND_USER_CLASS) { |
922 | | /* The same zend_class_entry may be reused by class_alias */ |
923 | 12.0k | zend_class_entry *new_ce = zend_shared_alloc_get_xlat_entry(ce); |
924 | 12.0k | if (new_ce) { |
925 | 0 | return new_ce; |
926 | 0 | } |
927 | 12.0k | ce = zend_shared_memdup_put(ce, sizeof(zend_class_entry)); |
928 | 12.0k | if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) { |
929 | 12.0k | ce->ce_flags |= ZEND_ACC_IMMUTABLE; |
930 | 12.0k | if ((ce->ce_flags & ZEND_ACC_LINKED) |
931 | 9.90k | && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) { |
932 | 292 | ZEND_MAP_PTR_NEW(ce->mutable_data); |
933 | 11.7k | } else { |
934 | 11.7k | ZEND_MAP_PTR_INIT(ce->mutable_data, NULL); |
935 | 11.7k | } |
936 | 12.0k | } else { |
937 | 0 | ce->ce_flags |= ZEND_ACC_FILE_CACHED; |
938 | 0 | } |
939 | 12.0k | ce->inheritance_cache = NULL; |
940 | | |
941 | 12.0k | if (!(ce->ce_flags & ZEND_ACC_CACHED)) { |
942 | 10.6k | if (ZSTR_HAS_CE_CACHE(ce->name)) { |
943 | 9.55k | ZSTR_SET_CE_CACHE_EX(ce->name, NULL, 0); |
944 | 9.55k | } |
945 | 10.6k | zend_accel_store_interned_string(ce->name); |
946 | 10.6k | if (!(ce->ce_flags & ZEND_ACC_ANON_CLASS) |
947 | 10.2k | && !ZCG(current_persistent_script)->corrupted) { |
948 | 10.2k | zend_accel_get_class_name_map_ptr(ce->name); |
949 | 10.2k | } |
950 | 10.6k | if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) { |
951 | 516 | zend_accel_store_interned_string(ce->parent_name); |
952 | 516 | } |
953 | 10.6k | } |
954 | | |
955 | 12.0k | zend_hash_persist(&ce->function_table); |
956 | 56.1k | ZEND_HASH_MAP_FOREACH_BUCKET(&ce->function_table, p) { |
957 | 56.1k | ZEND_ASSERT(p->key != NULL); |
958 | 56.1k | zend_accel_store_interned_string(p->key); |
959 | 16.0k | Z_PTR(p->val) = zend_persist_class_method(Z_PTR(p->val), ce); |
960 | 16.0k | } ZEND_HASH_FOREACH_END(); |
961 | 12.0k | HT_FLAGS(&ce->function_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS); |
962 | 12.0k | if (ce->default_properties_table) { |
963 | 4.41k | int i; |
964 | | |
965 | 4.41k | ce->default_properties_table = zend_shared_memdup_free(ce->default_properties_table, sizeof(zval) * ce->default_properties_count); |
966 | 11.8k | for (i = 0; i < ce->default_properties_count; i++) { |
967 | 7.48k | zend_persist_zval(&ce->default_properties_table[i]); |
968 | 7.48k | } |
969 | 4.41k | } |
970 | 12.0k | if (ce->default_static_members_table) { |
971 | 787 | ce->default_static_members_table = zend_shared_memdup_free(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count); |
972 | | |
973 | | /* Persist only static properties in this class. |
974 | | * Static properties from parent classes will be handled in class_copy_ctor and are marked with IS_INDIRECT */ |
975 | 2.23k | for (uint32_t i = 0; i < ce->default_static_members_count; i++) { |
976 | 1.45k | if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) { |
977 | 1.27k | zend_persist_zval(&ce->default_static_members_table[i]); |
978 | 1.27k | } |
979 | 1.45k | } |
980 | 787 | if (ce->ce_flags & ZEND_ACC_IMMUTABLE) { |
981 | 787 | if (ce->ce_flags & ZEND_ACC_LINKED) { |
982 | 765 | ZEND_MAP_PTR_NEW(ce->static_members_table); |
983 | 765 | } else { |
984 | 22 | ZEND_MAP_PTR_INIT(ce->static_members_table, NULL); |
985 | 22 | } |
986 | 787 | } |
987 | 787 | } |
988 | | |
989 | 12.0k | zend_hash_persist(&ce->constants_table); |
990 | 29.1k | ZEND_HASH_MAP_FOREACH_BUCKET(&ce->constants_table, p) { |
991 | 29.1k | ZEND_ASSERT(p->key != NULL); |
992 | 29.1k | zend_accel_store_interned_string(p->key); |
993 | 2.50k | zend_persist_class_constant(&p->val); |
994 | 2.50k | } ZEND_HASH_FOREACH_END(); |
995 | 12.0k | HT_FLAGS(&ce->constants_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS); |
996 | | |
997 | 12.0k | zend_hash_persist(&ce->properties_info); |
998 | 42.4k | ZEND_HASH_MAP_FOREACH_BUCKET(&ce->properties_info, p) { |
999 | 42.4k | zend_property_info *prop = Z_PTR(p->val); |
1000 | 42.4k | ZEND_ASSERT(p->key != NULL); |
1001 | 42.4k | zend_accel_store_interned_string(p->key); |
1002 | 9.18k | if (prop->ce == orig_ce) { |
1003 | 7.78k | Z_PTR(p->val) = zend_persist_property_info(prop); |
1004 | 7.78k | } else { |
1005 | 1.40k | prop = zend_shared_alloc_get_xlat_entry(prop); |
1006 | 1.40k | if (prop) { |
1007 | 602 | Z_PTR(p->val) = prop; |
1008 | 798 | } else { |
1009 | | /* This can happen if preloading is used and we inherit a property from an |
1010 | | * internal class. In that case we should keep pointing to the internal |
1011 | | * property, without any adjustments. */ |
1012 | 798 | } |
1013 | 1.40k | } |
1014 | 9.18k | } ZEND_HASH_FOREACH_END(); |
1015 | 12.0k | HT_FLAGS(&ce->properties_info) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS); |
1016 | | |
1017 | 12.0k | if (ce->properties_info_table) { |
1018 | 3.81k | int i; |
1019 | | |
1020 | 3.81k | size_t size = sizeof(zend_property_info *) * ce->default_properties_count; |
1021 | 3.81k | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED); |
1022 | 3.81k | ce->properties_info_table = zend_shared_memdup( |
1023 | 3.81k | ce->properties_info_table, size); |
1024 | | |
1025 | 10.4k | for (i = 0; i < ce->default_properties_count; i++) { |
1026 | 6.66k | if (ce->properties_info_table[i]) { |
1027 | 6.47k | zend_property_info *prop_info = zend_shared_alloc_get_xlat_entry( |
1028 | 6.47k | ce->properties_info_table[i]); |
1029 | 6.47k | if (prop_info) { |
1030 | 5.80k | ce->properties_info_table[i] = prop_info; |
1031 | 5.80k | } |
1032 | 6.47k | } |
1033 | 6.66k | } |
1034 | 3.81k | } |
1035 | | |
1036 | 12.0k | if (ce->iterator_funcs_ptr) { |
1037 | 134 | ce->iterator_funcs_ptr = zend_shared_memdup(ce->iterator_funcs_ptr, sizeof(zend_class_iterator_funcs)); |
1038 | 134 | } |
1039 | 12.0k | if (ce->arrayaccess_funcs_ptr) { |
1040 | 172 | ce->arrayaccess_funcs_ptr = zend_shared_memdup(ce->arrayaccess_funcs_ptr, sizeof(zend_class_arrayaccess_funcs)); |
1041 | 172 | } |
1042 | | |
1043 | 12.0k | if (ce->ce_flags & ZEND_ACC_CACHED) { |
1044 | 1.41k | return ce; |
1045 | 1.41k | } |
1046 | | |
1047 | 10.6k | ce->ce_flags |= ZEND_ACC_CACHED; |
1048 | | |
1049 | 10.6k | if (ce->info.user.filename) { |
1050 | 10.6k | zend_accel_store_string(ce->info.user.filename); |
1051 | 10.6k | } |
1052 | | |
1053 | 10.6k | if (ce->doc_comment) { |
1054 | 14 | if (ZCG(accel_directives).save_comments) { |
1055 | 14 | zend_accel_store_interned_string(ce->doc_comment); |
1056 | 14 | } else { |
1057 | 0 | if (!zend_shared_alloc_get_xlat_entry(ce->doc_comment)) { |
1058 | 0 | zend_shared_alloc_register_xlat_entry(ce->doc_comment, ce->doc_comment); |
1059 | 0 | zend_string_release_ex(ce->doc_comment, 0); |
1060 | 0 | } |
1061 | 0 | ce->doc_comment = NULL; |
1062 | 0 | } |
1063 | 14 | } |
1064 | | |
1065 | 10.6k | if (ce->attributes) { |
1066 | 556 | ce->attributes = zend_persist_attributes(ce->attributes); |
1067 | 556 | } |
1068 | | |
1069 | 10.6k | if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_LINKED)) { |
1070 | 1.29k | uint32_t i = 0; |
1071 | | |
1072 | 2.89k | for (i = 0; i < ce->num_interfaces; i++) { |
1073 | 1.60k | zend_accel_store_interned_string(ce->interface_names[i].name); |
1074 | 1.60k | zend_accel_store_interned_string(ce->interface_names[i].lc_name); |
1075 | 1.60k | } |
1076 | 1.29k | ce->interface_names = zend_shared_memdup_free(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
1077 | 1.29k | } |
1078 | | |
1079 | 10.6k | if (ce->num_traits) { |
1080 | 592 | uint32_t i = 0; |
1081 | | |
1082 | 1.34k | for (i = 0; i < ce->num_traits; i++) { |
1083 | 748 | zend_accel_store_interned_string(ce->trait_names[i].name); |
1084 | 748 | zend_accel_store_interned_string(ce->trait_names[i].lc_name); |
1085 | 748 | } |
1086 | 592 | ce->trait_names = zend_shared_memdup_free(ce->trait_names, sizeof(zend_class_name) * ce->num_traits); |
1087 | | |
1088 | 592 | i = 0; |
1089 | 592 | if (ce->trait_aliases) { |
1090 | 268 | while (ce->trait_aliases[i]) { |
1091 | 176 | if (ce->trait_aliases[i]->trait_method.method_name) { |
1092 | 176 | zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.method_name); |
1093 | 176 | } |
1094 | 176 | if (ce->trait_aliases[i]->trait_method.class_name) { |
1095 | 70 | zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.class_name); |
1096 | 70 | } |
1097 | | |
1098 | 176 | if (ce->trait_aliases[i]->alias) { |
1099 | 116 | zend_accel_store_interned_string(ce->trait_aliases[i]->alias); |
1100 | 116 | } |
1101 | | |
1102 | 176 | ce->trait_aliases[i] = zend_shared_memdup_free(ce->trait_aliases[i], sizeof(zend_trait_alias)); |
1103 | 176 | i++; |
1104 | 176 | } |
1105 | | |
1106 | 92 | ce->trait_aliases = zend_shared_memdup_free(ce->trait_aliases, sizeof(zend_trait_alias*) * (i + 1)); |
1107 | 92 | } |
1108 | | |
1109 | 592 | if (ce->trait_precedences) { |
1110 | 36 | uint32_t j; |
1111 | | |
1112 | 36 | i = 0; |
1113 | 82 | while (ce->trait_precedences[i]) { |
1114 | 46 | zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.method_name); |
1115 | 46 | zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.class_name); |
1116 | | |
1117 | 94 | for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) { |
1118 | 48 | zend_accel_store_interned_string(ce->trait_precedences[i]->exclude_class_names[j]); |
1119 | 48 | } |
1120 | | |
1121 | 46 | ce->trait_precedences[i] = zend_shared_memdup_free(ce->trait_precedences[i], sizeof(zend_trait_precedence) + (ce->trait_precedences[i]->num_excludes - 1) * sizeof(zend_string*)); |
1122 | 46 | i++; |
1123 | 46 | } |
1124 | 36 | ce->trait_precedences = zend_shared_memdup_free( |
1125 | 36 | ce->trait_precedences, sizeof(zend_trait_precedence*) * (i + 1)); |
1126 | 36 | } |
1127 | 592 | } |
1128 | | |
1129 | 10.6k | ZEND_ASSERT(ce->backed_enum_table == NULL); |
1130 | 10.6k | } |
1131 | | |
1132 | 10.6k | return ce; |
1133 | 12.0k | } |
1134 | | |
1135 | | void zend_update_parent_ce(zend_class_entry *ce) |
1136 | 12.0k | { |
1137 | 12.0k | if (ce->ce_flags & ZEND_ACC_LINKED) { |
1138 | 9.90k | if (ce->parent) { |
1139 | 1.63k | int i, end; |
1140 | 1.63k | zend_class_entry *parent = ce->parent; |
1141 | | |
1142 | 1.63k | if (parent->type == ZEND_USER_CLASS) { |
1143 | 1.43k | zend_class_entry *p = zend_shared_alloc_get_xlat_entry(parent); |
1144 | | |
1145 | 1.43k | if (p) { |
1146 | 1.14k | ce->parent = parent = p; |
1147 | 1.14k | } |
1148 | 1.43k | } |
1149 | | |
1150 | | /* Create indirections to static properties from parent classes */ |
1151 | 1.63k | i = parent->default_static_members_count - 1; |
1152 | 1.74k | while (parent && parent->default_static_members_table) { |
1153 | 108 | end = parent->parent ? parent->parent->default_static_members_count : 0; |
1154 | 280 | for (; i >= end; i--) { |
1155 | 172 | zval *p = &ce->default_static_members_table[i]; |
1156 | | /* The static property may have been overridden by a trait |
1157 | | * during inheritance. In that case, the property default |
1158 | | * value is replaced by zend_declare_typed_property() at the |
1159 | | * property index of the parent property. Make sure we only |
1160 | | * point to the parent property value if the child value was |
1161 | | * already indirect. */ |
1162 | 172 | if (Z_TYPE_P(p) == IS_INDIRECT) { |
1163 | 172 | ZVAL_INDIRECT(p, &parent->default_static_members_table[i]); |
1164 | 172 | } |
1165 | 172 | } |
1166 | | |
1167 | 108 | parent = parent->parent; |
1168 | 108 | } |
1169 | 1.63k | } |
1170 | | |
1171 | 9.90k | if (ce->num_interfaces) { |
1172 | 991 | uint32_t i = 0; |
1173 | | |
1174 | 991 | ce->interfaces = zend_shared_memdup_free(ce->interfaces, sizeof(zend_class_entry*) * ce->num_interfaces); |
1175 | 2.43k | for (i = 0; i < ce->num_interfaces; i++) { |
1176 | 1.44k | if (ce->interfaces[i]->type == ZEND_USER_CLASS) { |
1177 | 526 | zend_class_entry *tmp = zend_shared_alloc_get_xlat_entry(ce->interfaces[i]); |
1178 | 526 | if (tmp != NULL) { |
1179 | 0 | ce->interfaces[i] = tmp; |
1180 | 0 | } |
1181 | 526 | } |
1182 | 1.44k | } |
1183 | 991 | } |
1184 | | |
1185 | 9.90k | if (ce->iterator_funcs_ptr) { |
1186 | 134 | memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs)); |
1187 | 134 | if (zend_class_implements_interface(ce, zend_ce_aggregate)) { |
1188 | 88 | ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1); |
1189 | 88 | } |
1190 | 134 | if (zend_class_implements_interface(ce, zend_ce_iterator)) { |
1191 | 46 | ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1); |
1192 | 46 | ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1); |
1193 | 46 | ce->iterator_funcs_ptr->zf_key = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_KEY)); |
1194 | 46 | ce->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&ce->function_table, "current", sizeof("current") - 1); |
1195 | 46 | ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1); |
1196 | 46 | } |
1197 | 134 | } |
1198 | | |
1199 | 9.90k | if (ce->arrayaccess_funcs_ptr) { |
1200 | 172 | ZEND_ASSERT(zend_class_implements_interface(ce, zend_ce_arrayaccess)); |
1201 | 172 | ce->arrayaccess_funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(&ce->function_table, "offsetget", sizeof("offsetget") - 1); |
1202 | 172 | ce->arrayaccess_funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(&ce->function_table, "offsetexists", sizeof("offsetexists") - 1); |
1203 | 172 | ce->arrayaccess_funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(&ce->function_table, "offsetset", sizeof("offsetset") - 1); |
1204 | 172 | ce->arrayaccess_funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(&ce->function_table, "offsetunset", sizeof("offsetunset") - 1); |
1205 | 172 | } |
1206 | 9.90k | } |
1207 | | |
1208 | | /* update methods */ |
1209 | 12.0k | if (ce->constructor) { |
1210 | 1.92k | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->constructor); |
1211 | 1.92k | if (tmp != NULL) { |
1212 | 1.84k | ce->constructor = tmp; |
1213 | 1.84k | } |
1214 | 1.92k | } |
1215 | 12.0k | if (ce->destructor) { |
1216 | 825 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->destructor); |
1217 | 825 | if (tmp != NULL) { |
1218 | 825 | ce->destructor = tmp; |
1219 | 825 | } |
1220 | 825 | } |
1221 | 12.0k | if (ce->clone) { |
1222 | 178 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->clone); |
1223 | 178 | if (tmp != NULL) { |
1224 | 112 | ce->clone = tmp; |
1225 | 112 | } |
1226 | 178 | } |
1227 | 12.0k | if (ce->__get) { |
1228 | 472 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__get); |
1229 | 472 | if (tmp != NULL) { |
1230 | 472 | ce->__get = tmp; |
1231 | 472 | } |
1232 | 472 | } |
1233 | 12.0k | if (ce->__set) { |
1234 | 336 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__set); |
1235 | 336 | if (tmp != NULL) { |
1236 | 336 | ce->__set = tmp; |
1237 | 336 | } |
1238 | 336 | } |
1239 | 12.0k | if (ce->__call) { |
1240 | 322 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__call); |
1241 | 322 | if (tmp != NULL) { |
1242 | 322 | ce->__call = tmp; |
1243 | 322 | } |
1244 | 322 | } |
1245 | 12.0k | if (ce->__serialize) { |
1246 | 90 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__serialize); |
1247 | 90 | if (tmp != NULL) { |
1248 | 24 | ce->__serialize = tmp; |
1249 | 24 | } |
1250 | 90 | } |
1251 | 12.0k | if (ce->__unserialize) { |
1252 | 86 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unserialize); |
1253 | 86 | if (tmp != NULL) { |
1254 | 20 | ce->__unserialize = tmp; |
1255 | 20 | } |
1256 | 86 | } |
1257 | 12.0k | if (ce->__isset) { |
1258 | 166 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__isset); |
1259 | 166 | if (tmp != NULL) { |
1260 | 166 | ce->__isset = tmp; |
1261 | 166 | } |
1262 | 166 | } |
1263 | 12.0k | if (ce->__unset) { |
1264 | 70 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unset); |
1265 | 70 | if (tmp != NULL) { |
1266 | 70 | ce->__unset = tmp; |
1267 | 70 | } |
1268 | 70 | } |
1269 | 12.0k | if (ce->__tostring) { |
1270 | 510 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__tostring); |
1271 | 510 | if (tmp != NULL) { |
1272 | 450 | ce->__tostring = tmp; |
1273 | 450 | } |
1274 | 510 | } |
1275 | 12.0k | if (ce->__callstatic) { |
1276 | 194 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__callstatic); |
1277 | 194 | if (tmp != NULL) { |
1278 | 194 | ce->__callstatic = tmp; |
1279 | 194 | } |
1280 | 194 | } |
1281 | 12.0k | if (ce->__debugInfo) { |
1282 | 76 | zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__debugInfo); |
1283 | 76 | if (tmp != NULL) { |
1284 | 48 | ce->__debugInfo = tmp; |
1285 | 48 | } |
1286 | 76 | } |
1287 | 12.0k | } |
1288 | | |
1289 | | #ifdef HAVE_JIT |
1290 | | static void zend_accel_persist_jit_op_array(zend_op_array *op_array, const zend_class_entry *ce) |
1291 | 0 | { |
1292 | 0 | if (op_array->type == ZEND_USER_FUNCTION) { |
1293 | 0 | if (op_array->scope == ce |
1294 | 0 | && !(op_array->fn_flags & ZEND_ACC_ABSTRACT) |
1295 | 0 | && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) { |
1296 | 0 | zend_jit_op_array(op_array, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL); |
1297 | 0 | for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) { |
1298 | 0 | zend_jit_op_array(op_array->dynamic_func_defs[i], ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL); |
1299 | 0 | } |
1300 | 0 | } |
1301 | 0 | } |
1302 | 0 | } |
1303 | | |
1304 | | static void zend_accel_persist_link_func_info(zend_op_array *op_array, const zend_class_entry *ce) |
1305 | 0 | { |
1306 | 0 | if (op_array->type == ZEND_USER_FUNCTION |
1307 | 0 | && !(op_array->fn_flags & ZEND_ACC_ABSTRACT)) { |
1308 | 0 | if ((op_array->scope != ce |
1309 | 0 | || (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) |
1310 | 0 | && (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC |
1311 | 0 | || JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST |
1312 | 0 | || JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS |
1313 | 0 | || JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE)) { |
1314 | 0 | void *jit_extension = zend_shared_alloc_get_xlat_entry(op_array->opcodes); |
1315 | |
|
1316 | 0 | if (jit_extension) { |
1317 | 0 | ZEND_SET_FUNC_INFO(op_array, jit_extension); |
1318 | 0 | } |
1319 | 0 | } |
1320 | 0 | } |
1321 | 0 | } |
1322 | | #endif |
1323 | | |
1324 | | static void zend_accel_persist_class_table(HashTable *class_table) |
1325 | 23.5k | { |
1326 | 23.5k | Bucket *p; |
1327 | 23.5k | zend_class_entry *ce; |
1328 | 23.5k | #ifdef HAVE_JIT |
1329 | 23.5k | bool orig_jit_on = JIT_G(on); |
1330 | | |
1331 | 23.5k | JIT_G(on) = 0; |
1332 | 23.5k | #endif |
1333 | 23.5k | zend_hash_persist(class_table); |
1334 | 68.4k | ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) { |
1335 | 68.4k | ZEND_ASSERT(p->key != NULL); |
1336 | 68.4k | zend_accel_store_interned_string(p->key); |
1337 | 10.6k | Z_CE(p->val) = zend_persist_class_entry(Z_CE(p->val)); |
1338 | 10.6k | } ZEND_HASH_FOREACH_END(); |
1339 | 68.4k | ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) { |
1340 | 68.4k | if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) { |
1341 | 10.6k | ce = Z_PTR(p->val); |
1342 | 10.6k | zend_update_parent_ce(ce); |
1343 | 10.6k | } |
1344 | 68.4k | } ZEND_HASH_FOREACH_END(); |
1345 | 23.5k | #ifdef HAVE_JIT |
1346 | 23.5k | JIT_G(on) = orig_jit_on; |
1347 | 23.5k | if (JIT_G(on) && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS && |
1348 | 0 | !ZCG(current_persistent_script)->corrupted) { |
1349 | 0 | zend_op_array *op_array; |
1350 | 0 | zend_property_info *prop; |
1351 | |
|
1352 | 0 | ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) { |
1353 | 0 | if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) { |
1354 | 0 | ce = Z_PTR(p->val); |
1355 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) { |
1356 | 0 | zend_accel_persist_jit_op_array(op_array, ce); |
1357 | 0 | } ZEND_HASH_FOREACH_END(); |
1358 | |
|
1359 | 0 | if (ce->num_hooked_props > 0) { |
1360 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) { |
1361 | 0 | if (prop->hooks) { |
1362 | 0 | for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) { |
1363 | 0 | if (prop->hooks[i]) { |
1364 | 0 | op_array = &prop->hooks[i]->op_array; |
1365 | 0 | zend_accel_persist_jit_op_array(op_array, ce); |
1366 | 0 | } |
1367 | 0 | } |
1368 | 0 | } |
1369 | 0 | } ZEND_HASH_FOREACH_END(); |
1370 | 0 | } |
1371 | 0 | } |
1372 | 0 | } ZEND_HASH_FOREACH_END(); |
1373 | 0 | ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) { |
1374 | 0 | if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) { |
1375 | 0 | ce = Z_PTR(p->val); |
1376 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) { |
1377 | 0 | zend_accel_persist_link_func_info(op_array, ce); |
1378 | 0 | } ZEND_HASH_FOREACH_END(); |
1379 | |
|
1380 | 0 | if (ce->num_hooked_props > 0) { |
1381 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) { |
1382 | 0 | if (prop->hooks) { |
1383 | 0 | for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) { |
1384 | 0 | if (prop->hooks[i]) { |
1385 | 0 | op_array = &prop->hooks[i]->op_array; |
1386 | 0 | zend_accel_persist_link_func_info(op_array, ce); |
1387 | 0 | } |
1388 | 0 | } |
1389 | 0 | } |
1390 | 0 | } ZEND_HASH_FOREACH_END(); |
1391 | 0 | } |
1392 | 0 | } |
1393 | 0 | } ZEND_HASH_FOREACH_END(); |
1394 | 0 | } |
1395 | 23.5k | #endif |
1396 | 23.5k | } |
1397 | | |
1398 | 25.0k | zend_error_info **zend_persist_warnings(uint32_t num_warnings, zend_error_info **warnings) { |
1399 | 25.0k | if (warnings) { |
1400 | 26 | warnings = zend_shared_memdup(warnings, num_warnings * sizeof(zend_error_info *)); |
1401 | 54 | for (uint32_t i = 0; i < num_warnings; i++) { |
1402 | 28 | zend_accel_store_string(warnings[i]->filename); |
1403 | 28 | zend_accel_store_string(warnings[i]->message); |
1404 | 28 | warnings[i] = zend_shared_memdup(warnings[i], sizeof(zend_error_info)); |
1405 | 28 | } |
1406 | 26 | } |
1407 | 25.0k | return warnings; |
1408 | 25.0k | } |
1409 | | |
1410 | | static zend_early_binding *zend_persist_early_bindings( |
1411 | 23.5k | uint32_t num_early_bindings, zend_early_binding *early_bindings) { |
1412 | 23.5k | if (early_bindings) { |
1413 | 191 | early_bindings = zend_shared_memdup_free( |
1414 | 191 | early_bindings, num_early_bindings * sizeof(zend_early_binding)); |
1415 | 450 | for (uint32_t i = 0; i < num_early_bindings; i++) { |
1416 | 259 | zend_accel_store_interned_string(early_bindings[i].lcname); |
1417 | 259 | zend_accel_store_interned_string(early_bindings[i].rtd_key); |
1418 | 259 | zend_accel_store_interned_string(early_bindings[i].lc_parent_name); |
1419 | 259 | } |
1420 | 191 | } |
1421 | 23.5k | return early_bindings; |
1422 | 23.5k | } |
1423 | | |
1424 | | zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, bool for_shm) |
1425 | 23.5k | { |
1426 | 23.5k | Bucket *p; |
1427 | | |
1428 | 23.5k | script->mem = ZCG(mem); |
1429 | | |
1430 | 23.5k | ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */ |
1431 | | |
1432 | 23.5k | script = zend_shared_memdup_free(script, sizeof(zend_persistent_script)); |
1433 | 23.5k | script->corrupted = false; |
1434 | 23.5k | ZCG(current_persistent_script) = script; |
1435 | | |
1436 | 23.5k | if (!for_shm) { |
1437 | | /* script is not going to be saved in SHM */ |
1438 | 0 | script->corrupted = true; |
1439 | 0 | } |
1440 | | |
1441 | 23.5k | zend_accel_store_interned_string(script->script.filename); |
1442 | | |
1443 | 23.5k | #if defined(__AVX__) || defined(__SSE2__) |
1444 | | /* Align to 64-byte boundary */ |
1445 | 23.5k | ZCG(mem) = (void*)(((uintptr_t)ZCG(mem) + 63L) & ~63L); |
1446 | | #else |
1447 | | ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */ |
1448 | | #endif |
1449 | | |
1450 | 23.5k | #ifdef HAVE_JIT |
1451 | 23.5k | if (JIT_G(on) && for_shm) { |
1452 | 0 | zend_jit_unprotect(); |
1453 | 0 | } |
1454 | 23.5k | #endif |
1455 | | |
1456 | 23.5k | zend_map_ptr_extend(ZCSG(map_ptr_last)); |
1457 | | |
1458 | 23.5k | zend_accel_persist_class_table(&script->script.class_table); |
1459 | 23.5k | zend_hash_persist(&script->script.function_table); |
1460 | 60.5k | ZEND_HASH_MAP_FOREACH_BUCKET(&script->script.function_table, p) { |
1461 | 60.5k | ZEND_ASSERT(p->key != NULL); |
1462 | 60.5k | zend_accel_store_interned_string(p->key); |
1463 | 6.68k | zend_persist_op_array(&p->val); |
1464 | 6.68k | } ZEND_HASH_FOREACH_END(); |
1465 | 23.5k | zend_persist_op_array_ex(&script->script.main_op_array, script); |
1466 | 23.5k | if (!script->corrupted) { |
1467 | 23.5k | ZEND_MAP_PTR_INIT(script->script.main_op_array.run_time_cache, NULL); |
1468 | 23.5k | if (script->script.main_op_array.static_variables) { |
1469 | 16 | ZEND_MAP_PTR_NEW(script->script.main_op_array.static_variables_ptr); |
1470 | 16 | } |
1471 | 23.5k | #ifdef HAVE_JIT |
1472 | 23.5k | if (JIT_G(on) && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS) { |
1473 | 0 | zend_jit_op_array(&script->script.main_op_array, &script->script); |
1474 | 0 | } |
1475 | 23.5k | #endif |
1476 | 23.5k | } |
1477 | 23.5k | script->warnings = zend_persist_warnings(script->num_warnings, script->warnings); |
1478 | 23.5k | script->early_bindings = zend_persist_early_bindings( |
1479 | 23.5k | script->num_early_bindings, script->early_bindings); |
1480 | | |
1481 | 23.5k | if (for_shm) { |
1482 | 23.5k | ZCSG(map_ptr_last) = CG(map_ptr_last); |
1483 | 23.5k | ZCSG(map_ptr_static_last) = zend_map_ptr_static_last; |
1484 | 23.5k | } |
1485 | | |
1486 | 23.5k | #ifdef HAVE_JIT |
1487 | 23.5k | if (JIT_G(on) && for_shm) { |
1488 | 0 | if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_SCRIPT) { |
1489 | 0 | zend_jit_script(&script->script); |
1490 | 0 | } |
1491 | 0 | zend_jit_protect(); |
1492 | 0 | } |
1493 | 23.5k | #endif |
1494 | | |
1495 | 23.5k | script->corrupted = false; |
1496 | 23.5k | ZCG(current_persistent_script) = NULL; |
1497 | | |
1498 | 23.5k | return script; |
1499 | 23.5k | } |