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