/src/php-src/Zend/zend_opcode.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Andi Gutmans <andi@php.net> | |
15 | | | Zeev Suraski <zeev@php.net> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include <stdio.h> |
21 | | |
22 | | #include "zend.h" |
23 | | #include "zend_alloc.h" |
24 | | #include "zend_compile.h" |
25 | | #include "zend_extensions.h" |
26 | | #include "zend_API.h" |
27 | | #include "zend_sort.h" |
28 | | #include "zend_constants.h" |
29 | | #include "zend_observer.h" |
30 | | |
31 | | #include "zend_vm.h" |
32 | | |
33 | | static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array) |
34 | 0 | { |
35 | 0 | if (extension->op_array_ctor) { |
36 | 0 | extension->op_array_ctor(op_array); |
37 | 0 | } |
38 | 0 | } |
39 | | |
40 | | static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array) |
41 | 0 | { |
42 | 0 | if (extension->op_array_dtor) { |
43 | 0 | extension->op_array_dtor(op_array); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | void init_op_array(zend_op_array *op_array, zend_function_type type, int initial_ops_size) |
48 | 6.59k | { |
49 | 6.59k | op_array->type = type; |
50 | 6.59k | op_array->arg_flags[0] = 0; |
51 | 6.59k | op_array->arg_flags[1] = 0; |
52 | 6.59k | op_array->arg_flags[2] = 0; |
53 | | |
54 | 6.59k | op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t)); |
55 | 6.59k | *op_array->refcount = 1; |
56 | 6.59k | op_array->last = 0; |
57 | 6.59k | op_array->opcodes = emalloc(initial_ops_size * sizeof(zend_op)); |
58 | | |
59 | 6.59k | op_array->last_var = 0; |
60 | 6.59k | op_array->vars = NULL; |
61 | | |
62 | 6.59k | op_array->T = 0; |
63 | | |
64 | 6.59k | op_array->function_name = NULL; |
65 | 6.59k | op_array->filename = zend_string_copy(zend_get_compiled_filename()); |
66 | 6.59k | op_array->doc_comment = NULL; |
67 | 6.59k | op_array->attributes = NULL; |
68 | | |
69 | 6.59k | op_array->arg_info = NULL; |
70 | 6.59k | op_array->num_args = 0; |
71 | 6.59k | op_array->required_num_args = 0; |
72 | | |
73 | 6.59k | op_array->scope = NULL; |
74 | 6.59k | op_array->prototype = NULL; |
75 | 6.59k | op_array->prop_info = NULL; |
76 | | |
77 | 6.59k | op_array->live_range = NULL; |
78 | 6.59k | op_array->try_catch_array = NULL; |
79 | 6.59k | op_array->last_live_range = 0; |
80 | | |
81 | 6.59k | op_array->static_variables = NULL; |
82 | 6.59k | ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL); |
83 | 6.59k | op_array->last_try_catch = 0; |
84 | | |
85 | 6.59k | op_array->fn_flags = 0; |
86 | 6.59k | op_array->fn_flags2 = 0; |
87 | | |
88 | 6.59k | op_array->last_literal = 0; |
89 | 6.59k | op_array->literals = NULL; |
90 | | |
91 | 6.59k | op_array->num_dynamic_func_defs = 0; |
92 | 6.59k | op_array->dynamic_func_defs = NULL; |
93 | | |
94 | 6.59k | ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL); |
95 | 6.59k | op_array->cache_size = zend_op_array_extension_handles * sizeof(void*); |
96 | | |
97 | 6.59k | memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*)); |
98 | | |
99 | 6.59k | if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_CTOR) { |
100 | 0 | zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array); |
101 | 0 | } |
102 | 6.59k | } |
103 | | |
104 | | ZEND_API void destroy_zend_function(zend_function *function) |
105 | 0 | { |
106 | 0 | zval tmp; |
107 | |
|
108 | 0 | ZVAL_PTR(&tmp, function); |
109 | 0 | zend_function_dtor(&tmp); |
110 | 0 | } |
111 | | |
112 | 2.05k | ZEND_API void zend_type_release(zend_type type, bool persistent) { |
113 | 2.05k | if (ZEND_TYPE_HAS_LIST(type)) { |
114 | 65 | zend_type *list_type; |
115 | 192 | ZEND_TYPE_LIST_FOREACH_MUTABLE(ZEND_TYPE_LIST(type), list_type) { |
116 | 192 | zend_type_release(*list_type, persistent); |
117 | 192 | } ZEND_TYPE_LIST_FOREACH_END(); |
118 | 65 | if (!ZEND_TYPE_USES_ARENA(type)) { |
119 | 0 | pefree(ZEND_TYPE_LIST(type), persistent); |
120 | 0 | } |
121 | 1.99k | } else if (ZEND_TYPE_HAS_NAME(type)) { |
122 | 315 | zend_string_release(ZEND_TYPE_NAME(type)); |
123 | 315 | } |
124 | 2.05k | } |
125 | | |
126 | | ZEND_API void zend_free_internal_arg_info(zend_internal_function *function, |
127 | 64 | bool persistent) { |
128 | 64 | if (function->arg_info) { |
129 | 64 | ZEND_ASSERT((persistent || (function->fn_flags & ZEND_ACC_NEVER_CACHE)) |
130 | 64 | && "Functions with non-persistent arg_info must be flagged ZEND_ACC_NEVER_CACHE"); |
131 | | |
132 | 64 | uint32_t i; |
133 | 64 | uint32_t num_args = function->num_args + 1; |
134 | 64 | zend_arg_info *arg_info = function->arg_info - 1; |
135 | | |
136 | 64 | if (function->fn_flags & ZEND_ACC_VARIADIC) { |
137 | 0 | num_args++; |
138 | 0 | } |
139 | 318 | for (i = 0 ; i < num_args; i++) { |
140 | 254 | bool is_return_info = i == 0; |
141 | 254 | if (!is_return_info) { |
142 | 190 | zend_string_release_ex(arg_info[i].name, persistent); |
143 | 190 | if (arg_info[i].default_value) { |
144 | 82 | zend_string_release_ex(arg_info[i].default_value, |
145 | 82 | persistent); |
146 | 82 | } |
147 | 190 | } |
148 | 254 | zend_type_release(arg_info[i].type, persistent); |
149 | 254 | } |
150 | | |
151 | 64 | pefree(arg_info, persistent); |
152 | 64 | } |
153 | 64 | } |
154 | | |
155 | | ZEND_API void zend_function_dtor(zval *zv) |
156 | 1.34k | { |
157 | 1.34k | zend_function *function = Z_PTR_P(zv); |
158 | | |
159 | 1.34k | if (function->type == ZEND_USER_FUNCTION) { |
160 | 657 | ZEND_ASSERT(function->common.function_name); |
161 | 657 | destroy_op_array(&function->op_array); |
162 | | /* op_arrays are allocated on arena, so we don't have to free them */ |
163 | 685 | } else { |
164 | 685 | ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION); |
165 | 685 | ZEND_ASSERT(function->common.function_name); |
166 | 685 | zend_string_release_ex(function->common.function_name, 1); |
167 | | |
168 | | /* For methods this will be called explicitly. */ |
169 | 685 | if (!function->common.scope) { |
170 | 64 | zend_free_internal_arg_info(&function->internal_function, true); |
171 | | |
172 | 64 | if (function->common.attributes) { |
173 | 2 | zend_hash_release(function->common.attributes); |
174 | 2 | function->common.attributes = NULL; |
175 | 2 | } |
176 | 64 | } |
177 | | |
178 | 685 | if (function->common.doc_comment) { |
179 | 0 | zend_string_release_ex(function->common.doc_comment, 1); |
180 | 0 | function->common.doc_comment = NULL; |
181 | 0 | } |
182 | | |
183 | 685 | if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) { |
184 | 64 | pefree(function, 1); |
185 | 64 | } |
186 | 685 | } |
187 | 1.34k | } |
188 | | |
189 | | ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce) |
190 | 74 | { |
191 | 74 | if (ZEND_MAP_PTR(ce->static_members_table) && CE_STATIC_MEMBERS(ce)) { |
192 | 50 | zval *static_members = CE_STATIC_MEMBERS(ce); |
193 | 50 | zval *p = static_members; |
194 | 50 | zval *end = p + ce->default_static_members_count; |
195 | 50 | ZEND_MAP_PTR_SET(ce->static_members_table, NULL); |
196 | 101 | while (p != end) { |
197 | 51 | if (UNEXPECTED(Z_ISREF_P(p))) { |
198 | 10 | zend_property_info *prop_info; |
199 | 24 | ZEND_REF_FOREACH_TYPE_SOURCES(Z_REF_P(p), prop_info) { |
200 | 24 | if (prop_info->ce == ce && p - static_members == prop_info->offset) { |
201 | 7 | ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info); |
202 | 7 | break; /* stop iteration here, the array might be realloc()'ed */ |
203 | 7 | } |
204 | 24 | } ZEND_REF_FOREACH_TYPE_SOURCES_END(); |
205 | 10 | } |
206 | 51 | i_zval_ptr_dtor(p); |
207 | 51 | p++; |
208 | 51 | } |
209 | 50 | efree(static_members); |
210 | 50 | } |
211 | 74 | } |
212 | | |
213 | | static void _destroy_zend_class_traits_info(zend_class_entry *ce) |
214 | 97 | { |
215 | 97 | uint32_t i; |
216 | | |
217 | 208 | for (i = 0; i < ce->num_traits; i++) { |
218 | 111 | zend_string_release_ex(ce->trait_names[i].name, 0); |
219 | 111 | zend_string_release_ex(ce->trait_names[i].lc_name, 0); |
220 | 111 | } |
221 | 97 | efree(ce->trait_names); |
222 | | |
223 | 97 | if (ce->trait_aliases) { |
224 | 15 | i = 0; |
225 | 32 | while (ce->trait_aliases[i]) { |
226 | 17 | if (ce->trait_aliases[i]->trait_method.method_name) { |
227 | 17 | zend_string_release_ex(ce->trait_aliases[i]->trait_method.method_name, 0); |
228 | 17 | } |
229 | 17 | if (ce->trait_aliases[i]->trait_method.class_name) { |
230 | 4 | zend_string_release_ex(ce->trait_aliases[i]->trait_method.class_name, 0); |
231 | 4 | } |
232 | | |
233 | 17 | if (ce->trait_aliases[i]->alias) { |
234 | 12 | zend_string_release_ex(ce->trait_aliases[i]->alias, 0); |
235 | 12 | } |
236 | | |
237 | 17 | efree(ce->trait_aliases[i]); |
238 | 17 | i++; |
239 | 17 | } |
240 | | |
241 | 15 | efree(ce->trait_aliases); |
242 | 15 | } |
243 | | |
244 | 97 | if (ce->trait_precedences) { |
245 | 2 | uint32_t j; |
246 | | |
247 | 2 | i = 0; |
248 | 5 | while (ce->trait_precedences[i]) { |
249 | 3 | zend_string_release_ex(ce->trait_precedences[i]->trait_method.method_name, 0); |
250 | 3 | zend_string_release_ex(ce->trait_precedences[i]->trait_method.class_name, 0); |
251 | | |
252 | 6 | for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) { |
253 | 3 | zend_string_release_ex(ce->trait_precedences[i]->exclude_class_names[j], 0); |
254 | 3 | } |
255 | 3 | efree(ce->trait_precedences[i]); |
256 | 3 | i++; |
257 | 3 | } |
258 | 2 | efree(ce->trait_precedences); |
259 | 2 | } |
260 | 97 | } |
261 | | |
262 | | ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce) |
263 | 1 | { |
264 | 1 | zend_class_mutable_data *mutable_data = ZEND_MAP_PTR_GET_IMM(ce->mutable_data); |
265 | | |
266 | 1 | if (mutable_data) { |
267 | 1 | HashTable *constants_table; |
268 | 1 | zval *p; |
269 | | |
270 | 1 | constants_table = mutable_data->constants_table; |
271 | 1 | if (constants_table && constants_table != &ce->constants_table) { |
272 | 1 | zend_class_constant *c; |
273 | | |
274 | 10 | ZEND_HASH_MAP_FOREACH_PTR(constants_table, c) { |
275 | 10 | if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) { |
276 | 4 | zval_ptr_dtor_nogc(&c->value); |
277 | 4 | } |
278 | 10 | } ZEND_HASH_FOREACH_END(); |
279 | 1 | zend_hash_destroy(constants_table); |
280 | 1 | mutable_data->constants_table = NULL; |
281 | 1 | } |
282 | | |
283 | 1 | p = mutable_data->default_properties_table; |
284 | 1 | if (p && p != ce->default_properties_table) { |
285 | 0 | zval *end = p + ce->default_properties_count; |
286 | |
|
287 | 0 | while (p < end) { |
288 | 0 | zval_ptr_dtor_nogc(p); |
289 | 0 | p++; |
290 | 0 | } |
291 | 0 | mutable_data->default_properties_table = NULL; |
292 | 0 | } |
293 | | |
294 | 1 | if (mutable_data->backed_enum_table) { |
295 | 0 | zend_hash_release(mutable_data->backed_enum_table); |
296 | 0 | mutable_data->backed_enum_table = NULL; |
297 | 0 | } |
298 | | |
299 | 1 | ZEND_MAP_PTR_SET_IMM(ce->mutable_data, NULL); |
300 | 1 | } |
301 | 1 | } |
302 | | |
303 | | ZEND_API void destroy_zend_class(zval *zv) |
304 | 1.58k | { |
305 | 1.58k | zend_property_info *prop_info; |
306 | 1.58k | zend_class_entry *ce = Z_PTR_P(zv); |
307 | 1.58k | zend_function *fn; |
308 | | |
309 | 1.58k | if (ce->ce_flags & ZEND_ACC_IMMUTABLE) { |
310 | 0 | return; |
311 | 0 | } |
312 | | |
313 | | /* We don't increase the refcount for class aliases, |
314 | | * skip the destruction of aliases entirely. */ |
315 | 1.58k | if (UNEXPECTED(Z_TYPE_INFO_P(zv) == IS_ALIAS_PTR)) { |
316 | 21 | return; |
317 | 21 | } |
318 | | |
319 | 1.55k | if (ce->ce_flags & ZEND_ACC_FILE_CACHED) { |
320 | 0 | zend_class_constant *c; |
321 | 0 | zval *p, *end; |
322 | |
|
323 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->constants_table, c) { |
324 | 0 | if (c->ce == ce) { |
325 | 0 | zval_ptr_dtor_nogc(&c->value); |
326 | 0 | } |
327 | 0 | } ZEND_HASH_FOREACH_END(); |
328 | |
|
329 | 0 | if (ce->default_properties_table) { |
330 | 0 | p = ce->default_properties_table; |
331 | 0 | end = p + ce->default_properties_count; |
332 | |
|
333 | 0 | while (p < end) { |
334 | 0 | zval_ptr_dtor_nogc(p); |
335 | 0 | p++; |
336 | 0 | } |
337 | 0 | } |
338 | 0 | return; |
339 | 0 | } |
340 | | |
341 | 1.55k | ZEND_ASSERT(ce->refcount > 0); |
342 | | |
343 | 1.55k | if (--ce->refcount > 0) { |
344 | 0 | return; |
345 | 0 | } |
346 | | |
347 | 1.55k | switch (ce->type) { |
348 | 1.55k | case ZEND_USER_CLASS: |
349 | 1.55k | if (!(ce->ce_flags & ZEND_ACC_CACHED)) { |
350 | 1.55k | if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_RESOLVED_PARENT)) { |
351 | 21 | zend_string_release_ex(ce->parent_name, 0); |
352 | 21 | } |
353 | | |
354 | 1.55k | zend_string_release_ex(ce->name, 0); |
355 | 1.55k | zend_string_release_ex(ce->info.user.filename, 0); |
356 | | |
357 | 1.55k | if (ce->doc_comment) { |
358 | 0 | zend_string_release_ex(ce->doc_comment, 0); |
359 | 0 | } |
360 | | |
361 | 1.55k | if (ce->attributes) { |
362 | 33 | zend_hash_release(ce->attributes); |
363 | 33 | } |
364 | | |
365 | 1.55k | if (ce->num_interfaces > 0 && !(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) { |
366 | 22 | uint32_t i; |
367 | | |
368 | 53 | for (i = 0; i < ce->num_interfaces; i++) { |
369 | 31 | zend_string_release_ex(ce->interface_names[i].name, 0); |
370 | 31 | zend_string_release_ex(ce->interface_names[i].lc_name, 0); |
371 | 31 | } |
372 | 22 | efree(ce->interface_names); |
373 | 22 | } |
374 | | |
375 | 1.55k | if (ce->num_traits > 0) { |
376 | 97 | _destroy_zend_class_traits_info(ce); |
377 | 97 | } |
378 | 1.55k | } |
379 | | |
380 | 1.55k | if (ce->default_properties_table) { |
381 | 458 | zval *p = ce->default_properties_table; |
382 | 458 | zval *end = p + ce->default_properties_count; |
383 | | |
384 | 1.11k | while (p != end) { |
385 | 656 | i_zval_ptr_dtor(p); |
386 | 656 | p++; |
387 | 656 | } |
388 | 458 | efree(ce->default_properties_table); |
389 | 458 | } |
390 | 1.55k | if (ce->default_static_members_table) { |
391 | 74 | zval *p = ce->default_static_members_table; |
392 | 74 | zval *end = p + ce->default_static_members_count; |
393 | | |
394 | 154 | while (p != end) { |
395 | 80 | ZEND_ASSERT(!Z_ISREF_P(p)); |
396 | 80 | i_zval_ptr_dtor(p); |
397 | 80 | p++; |
398 | 80 | } |
399 | 74 | efree(ce->default_static_members_table); |
400 | 74 | } |
401 | 4.59k | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop_info) { |
402 | 4.59k | if (prop_info->ce == ce) { |
403 | 636 | zend_string_release_ex(prop_info->name, 0); |
404 | 636 | if (prop_info->doc_comment) { |
405 | 10 | zend_string_release_ex(prop_info->doc_comment, 0); |
406 | 10 | } |
407 | 636 | if (prop_info->attributes) { |
408 | 11 | zend_hash_release(prop_info->attributes); |
409 | 11 | } |
410 | 636 | zend_type_release(prop_info->type, /* persistent */ false); |
411 | 636 | if (prop_info->hooks) { |
412 | 315 | for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) { |
413 | 210 | if (prop_info->hooks[i]) { |
414 | 133 | destroy_op_array(&prop_info->hooks[i]->op_array); |
415 | 133 | } |
416 | 210 | } |
417 | 105 | } |
418 | 636 | } |
419 | 4.59k | } ZEND_HASH_FOREACH_END(); |
420 | 1.55k | zend_hash_destroy(&ce->properties_info); |
421 | 1.55k | zend_hash_destroy(&ce->function_table); |
422 | 1.55k | if (zend_hash_num_elements(&ce->constants_table)) { |
423 | 233 | zend_class_constant *c; |
424 | | |
425 | 1.25k | ZEND_HASH_MAP_FOREACH_PTR(&ce->constants_table, c) { |
426 | 1.25k | if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) { |
427 | 258 | zval_ptr_dtor_nogc(&c->value); |
428 | 258 | if (c->doc_comment) { |
429 | 1 | zend_string_release_ex(c->doc_comment, 0); |
430 | 1 | } |
431 | 258 | if (c->attributes) { |
432 | 7 | zend_hash_release(c->attributes); |
433 | 7 | } |
434 | 258 | } |
435 | 1.25k | } ZEND_HASH_FOREACH_END(); |
436 | 233 | } |
437 | 1.55k | zend_hash_destroy(&ce->constants_table); |
438 | 1.55k | if (ce->num_interfaces > 0 && (ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) { |
439 | 217 | efree(ce->interfaces); |
440 | 217 | } |
441 | 1.55k | if (ce->backed_enum_table) { |
442 | 0 | zend_hash_release(ce->backed_enum_table); |
443 | 0 | } |
444 | 1.55k | break; |
445 | 0 | case ZEND_INTERNAL_CLASS: |
446 | 0 | if (ce->doc_comment) { |
447 | 0 | zend_string_release_ex(ce->doc_comment, 1); |
448 | 0 | } |
449 | |
|
450 | 0 | if (ce->backed_enum_table) { |
451 | 0 | zend_hash_release(ce->backed_enum_table); |
452 | 0 | } |
453 | 0 | if (ce->default_properties_table) { |
454 | 0 | zval *p = ce->default_properties_table; |
455 | 0 | zval *end = p + ce->default_properties_count; |
456 | |
|
457 | 0 | while (p != end) { |
458 | 0 | zval_internal_ptr_dtor(p); |
459 | 0 | p++; |
460 | 0 | } |
461 | 0 | free(ce->default_properties_table); |
462 | 0 | } |
463 | 0 | if (ce->default_static_members_table) { |
464 | 0 | zval *p = ce->default_static_members_table; |
465 | 0 | zval *end = p + ce->default_static_members_count; |
466 | |
|
467 | 0 | while (p != end) { |
468 | 0 | zval_internal_ptr_dtor(p); |
469 | 0 | p++; |
470 | 0 | } |
471 | 0 | free(ce->default_static_members_table); |
472 | 0 | } |
473 | |
|
474 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop_info) { |
475 | 0 | if (prop_info->ce == ce) { |
476 | 0 | zend_string_release(prop_info->name); |
477 | 0 | zend_type_release(prop_info->type, /* persistent */ true); |
478 | 0 | if (prop_info->attributes) { |
479 | 0 | zend_hash_release(prop_info->attributes); |
480 | 0 | } |
481 | 0 | free(prop_info); |
482 | 0 | } |
483 | 0 | } ZEND_HASH_FOREACH_END(); |
484 | 0 | zend_hash_destroy(&ce->properties_info); |
485 | 0 | zend_string_release_ex(ce->name, 1); |
486 | |
|
487 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) { |
488 | 0 | if (fn->common.scope == ce) { |
489 | 0 | zend_free_internal_arg_info(&fn->internal_function, true); |
490 | |
|
491 | 0 | if (fn->common.attributes) { |
492 | 0 | zend_hash_release(fn->common.attributes); |
493 | 0 | fn->common.attributes = NULL; |
494 | 0 | } |
495 | 0 | } |
496 | 0 | } ZEND_HASH_FOREACH_END(); |
497 | |
|
498 | 0 | zend_hash_destroy(&ce->function_table); |
499 | 0 | if (zend_hash_num_elements(&ce->constants_table)) { |
500 | 0 | zend_class_constant *c; |
501 | |
|
502 | 0 | ZEND_HASH_MAP_FOREACH_PTR(&ce->constants_table, c) { |
503 | 0 | if (c->ce == ce) { |
504 | 0 | if (Z_TYPE(c->value) == IS_CONSTANT_AST) { |
505 | | /* We marked this as IMMUTABLE, but do need to free it when the |
506 | | * class is destroyed. */ |
507 | 0 | ZEND_ASSERT(Z_ASTVAL(c->value)->kind == ZEND_AST_CONST_ENUM_INIT); |
508 | 0 | free(Z_AST(c->value)); |
509 | 0 | } else { |
510 | 0 | zval_internal_ptr_dtor(&c->value); |
511 | 0 | } |
512 | 0 | if (c->doc_comment) { |
513 | 0 | zend_string_release_ex(c->doc_comment, 1); |
514 | 0 | } |
515 | 0 | if (c->attributes) { |
516 | 0 | zend_hash_release(c->attributes); |
517 | 0 | } |
518 | 0 | } |
519 | 0 | free(c); |
520 | 0 | } ZEND_HASH_FOREACH_END(); |
521 | 0 | zend_hash_destroy(&ce->constants_table); |
522 | 0 | } |
523 | 0 | if (ce->iterator_funcs_ptr) { |
524 | 0 | free(ce->iterator_funcs_ptr); |
525 | 0 | } |
526 | 0 | if (ce->arrayaccess_funcs_ptr) { |
527 | 0 | free(ce->arrayaccess_funcs_ptr); |
528 | 0 | } |
529 | 0 | if (ce->num_interfaces > 0) { |
530 | 0 | free(ce->interfaces); |
531 | 0 | } |
532 | 0 | if (ce->properties_info_table) { |
533 | 0 | free(ce->properties_info_table); |
534 | 0 | } |
535 | 0 | if (ce->attributes) { |
536 | 0 | zend_hash_release(ce->attributes); |
537 | 0 | } |
538 | 0 | free(ce); |
539 | 0 | break; |
540 | 1.55k | } |
541 | 1.55k | } |
542 | | |
543 | | void zend_class_add_ref(zval *zv) |
544 | 0 | { |
545 | 0 | zend_class_entry *ce = Z_PTR_P(zv); |
546 | |
|
547 | 0 | if (Z_TYPE_P(zv) != IS_ALIAS_PTR && !(ce->ce_flags & ZEND_ACC_IMMUTABLE)) { |
548 | 0 | ce->refcount++; |
549 | 0 | } |
550 | 0 | } |
551 | | |
552 | | ZEND_API void zend_destroy_static_vars(zend_op_array *op_array) |
553 | 31.1k | { |
554 | 31.1k | if (ZEND_MAP_PTR(op_array->static_variables_ptr)) { |
555 | 40 | HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr); |
556 | 40 | if (ht) { |
557 | 40 | zend_array_destroy(ht); |
558 | 40 | ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL); |
559 | 40 | } |
560 | 40 | } |
561 | 31.1k | } |
562 | | |
563 | | ZEND_API void destroy_op_array(zend_op_array *op_array) |
564 | 32.6k | { |
565 | 32.6k | uint32_t i; |
566 | | |
567 | 32.6k | if ((op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE) |
568 | 30.6k | && ZEND_MAP_PTR(op_array->run_time_cache)) { |
569 | 30.6k | efree(ZEND_MAP_PTR(op_array->run_time_cache)); |
570 | 30.6k | } |
571 | | |
572 | 32.6k | if (op_array->function_name) { |
573 | 2.04k | zend_string_release_ex(op_array->function_name, 0); |
574 | 2.04k | } |
575 | | |
576 | 32.6k | if (!op_array->refcount || --(*op_array->refcount) > 0) { |
577 | 27.1k | return; |
578 | 27.1k | } |
579 | | |
580 | 5.47k | efree_size(op_array->refcount, sizeof(*(op_array->refcount))); |
581 | | |
582 | 5.47k | if (op_array->vars) { |
583 | 2.93k | i = op_array->last_var; |
584 | 7.64k | while (i > 0) { |
585 | 4.71k | i--; |
586 | 4.71k | zend_string_release_ex(op_array->vars[i], 0); |
587 | 4.71k | } |
588 | 2.93k | efree(op_array->vars); |
589 | 2.93k | } |
590 | | |
591 | | /* ZEND_ACC_PTR_OPS and ZEND_ACC_OVERRIDE use the same value */ |
592 | 5.47k | if ((op_array->fn_flags & ZEND_ACC_PTR_OPS) && !op_array->function_name) { |
593 | 9 | zend_op *op = op_array->opcodes; |
594 | 9 | zend_op *end = op + op_array->last; |
595 | 103 | while (op < end) { |
596 | 94 | if (op->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) { |
597 | 12 | HashTable *attributes = Z_PTR_P(RT_CONSTANT(op+1, (op+1)->op1)); |
598 | 12 | zend_hash_release(attributes); |
599 | 12 | } |
600 | 94 | op++; |
601 | 94 | } |
602 | 9 | } |
603 | 5.47k | if (op_array->literals) { |
604 | 5.47k | zval *literal = op_array->literals; |
605 | 5.47k | zval *end = literal + op_array->last_literal; |
606 | 33.5k | while (literal < end) { |
607 | 28.1k | zval_ptr_dtor_nogc(literal); |
608 | 28.1k | literal++; |
609 | 28.1k | } |
610 | 5.47k | if (ZEND_USE_ABS_CONST_ADDR |
611 | 5.47k | || !(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) { |
612 | 0 | efree(op_array->literals); |
613 | 0 | } |
614 | 5.47k | } |
615 | 5.47k | efree(op_array->opcodes); |
616 | | |
617 | 5.47k | zend_string_release_ex(op_array->filename, 0); |
618 | 5.47k | if (op_array->doc_comment) { |
619 | 0 | zend_string_release_ex(op_array->doc_comment, 0); |
620 | 0 | } |
621 | 5.47k | if (op_array->attributes) { |
622 | 41 | zend_hash_release(op_array->attributes); |
623 | 41 | } |
624 | 5.47k | if (op_array->live_range) { |
625 | 2.25k | efree(op_array->live_range); |
626 | 2.25k | } |
627 | 5.47k | if (op_array->try_catch_array) { |
628 | 197 | efree(op_array->try_catch_array); |
629 | 197 | } |
630 | 5.47k | if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR) { |
631 | 0 | if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) { |
632 | 0 | zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array); |
633 | 0 | } |
634 | 0 | } |
635 | 5.47k | if (op_array->arg_info) { |
636 | 733 | uint32_t num_args = op_array->num_args; |
637 | 733 | zend_arg_info *arg_info = op_array->arg_info; |
638 | | |
639 | 733 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
640 | 284 | arg_info--; |
641 | 284 | num_args++; |
642 | 284 | } |
643 | 733 | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
644 | 36 | num_args++; |
645 | 36 | } |
646 | 1.74k | for (i = 0 ; i < num_args; i++) { |
647 | 1.00k | if (arg_info[i].name) { |
648 | 723 | zend_string_release_ex(arg_info[i].name, 0); |
649 | 723 | } |
650 | 1.00k | if (arg_info[i].doc_comment) { |
651 | 0 | zend_string_release_ex(arg_info[i].doc_comment, 0); |
652 | 0 | } |
653 | 1.00k | zend_type_release(arg_info[i].type, /* persistent */ false); |
654 | 1.00k | } |
655 | 733 | efree(arg_info); |
656 | 733 | } |
657 | 5.47k | if (op_array->static_variables) { |
658 | 65 | zend_array_destroy(op_array->static_variables); |
659 | 65 | } |
660 | 5.47k | if (op_array->num_dynamic_func_defs) { |
661 | 505 | for (i = 0; i < op_array->num_dynamic_func_defs; i++) { |
662 | 263 | destroy_op_array(op_array->dynamic_func_defs[i]); |
663 | 263 | } |
664 | 242 | efree(op_array->dynamic_func_defs); |
665 | 242 | } |
666 | 5.47k | } |
667 | | |
668 | | static void zend_update_extended_stmts(zend_op_array *op_array) |
669 | 0 | { |
670 | 0 | zend_op *opline = op_array->opcodes, *end=opline+op_array->last; |
671 | |
|
672 | 0 | while (opline<end) { |
673 | 0 | if (opline->opcode == ZEND_EXT_STMT) { |
674 | 0 | if (opline+1<end) { |
675 | 0 | if ((opline+1)->opcode == ZEND_EXT_STMT) { |
676 | 0 | opline->opcode = ZEND_NOP; |
677 | 0 | opline++; |
678 | 0 | continue; |
679 | 0 | } |
680 | 0 | if (opline+1<end) { |
681 | 0 | opline->lineno = (opline+1)->lineno; |
682 | 0 | } |
683 | 0 | } else { |
684 | 0 | opline->opcode = ZEND_NOP; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | opline++; |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | | static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array) |
692 | 0 | { |
693 | 0 | if (extension->op_array_handler) { |
694 | 0 | extension->op_array_handler(op_array); |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num) |
699 | 7 | { |
700 | 10 | for (uint32_t i = 0; i < op_array->last_try_catch; i++) { |
701 | 7 | if ((op_num < op_array->try_catch_array[i].finally_op || |
702 | 3 | op_num >= op_array->try_catch_array[i].finally_end) |
703 | 5 | && (dst_num >= op_array->try_catch_array[i].finally_op && |
704 | 3 | dst_num <= op_array->try_catch_array[i].finally_end)) { |
705 | 3 | CG(in_compilation) = 1; |
706 | 3 | CG(active_op_array) = op_array; |
707 | 3 | CG(zend_lineno) = op_array->opcodes[op_num].lineno; |
708 | 3 | zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed"); |
709 | 4 | } else if ((op_num >= op_array->try_catch_array[i].finally_op |
710 | 2 | && op_num <= op_array->try_catch_array[i].finally_end) |
711 | 2 | && (dst_num > op_array->try_catch_array[i].finally_end |
712 | 1 | || dst_num < op_array->try_catch_array[i].finally_op)) { |
713 | 1 | CG(in_compilation) = 1; |
714 | 1 | CG(active_op_array) = op_array; |
715 | 1 | CG(zend_lineno) = op_array->opcodes[op_num].lineno; |
716 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed"); |
717 | 1 | } |
718 | 7 | } |
719 | 7 | } |
720 | | |
721 | 21 | static uint32_t zend_get_brk_cont_target(const zend_op *opline) { |
722 | 21 | int nest_levels = opline->op2.num; |
723 | 21 | int array_offset = opline->op1.num; |
724 | 21 | zend_brk_cont_element *jmp_to; |
725 | 23 | do { |
726 | 23 | jmp_to = &CG(context).brk_cont_array[array_offset]; |
727 | 23 | if (nest_levels > 1) { |
728 | 2 | array_offset = jmp_to->parent; |
729 | 2 | } |
730 | 23 | } while (--nest_levels > 0); |
731 | | |
732 | 21 | return opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont; |
733 | 21 | } |
734 | | |
735 | | static void emit_live_range_raw( |
736 | 3.35k | zend_op_array *op_array, uint32_t var_num, uint32_t kind, uint32_t start, uint32_t end) { |
737 | 3.35k | zend_live_range *range; |
738 | | |
739 | 3.35k | op_array->last_live_range++; |
740 | 3.35k | op_array->live_range = erealloc(op_array->live_range, |
741 | 3.35k | sizeof(zend_live_range) * op_array->last_live_range); |
742 | | |
743 | 3.35k | ZEND_ASSERT(start < end); |
744 | 3.35k | range = &op_array->live_range[op_array->last_live_range - 1]; |
745 | 3.35k | range->var = EX_NUM_TO_VAR(op_array->last_var + var_num); |
746 | 3.35k | range->var |= kind; |
747 | 3.35k | range->start = start; |
748 | 3.35k | range->end = end; |
749 | 3.35k | } |
750 | | |
751 | | static void emit_live_range( |
752 | | zend_op_array *op_array, uint32_t var_num, uint32_t start, uint32_t end, |
753 | 3.38k | zend_needs_live_range_cb needs_live_range) { |
754 | 3.38k | zend_op *def_opline = &op_array->opcodes[start], *orig_def_opline = def_opline; |
755 | 3.38k | zend_op *use_opline = &op_array->opcodes[end]; |
756 | 3.38k | uint32_t kind; |
757 | | |
758 | 3.38k | switch (def_opline->opcode) { |
759 | | /* These should never be the first def. */ |
760 | 0 | case ZEND_ADD_ARRAY_ELEMENT: |
761 | 0 | case ZEND_ADD_ARRAY_UNPACK: |
762 | 0 | case ZEND_ROPE_ADD: |
763 | 0 | ZEND_UNREACHABLE(); |
764 | 0 | return; |
765 | | /* Result is boolean, it doesn't have to be destroyed. */ |
766 | 0 | case ZEND_JMPZ_EX: |
767 | 0 | case ZEND_JMPNZ_EX: |
768 | 8 | case ZEND_BOOL: |
769 | 19 | case ZEND_BOOL_NOT: |
770 | | /* Classes don't have to be destroyed. */ |
771 | 21 | case ZEND_FETCH_CLASS: |
772 | 21 | case ZEND_DECLARE_ANON_CLASS: |
773 | | /* FAST_CALLs don't have to be destroyed. */ |
774 | 54 | case ZEND_FAST_CALL: |
775 | 54 | return; |
776 | 132 | case ZEND_BEGIN_SILENCE: |
777 | 132 | kind = ZEND_LIVE_SILENCE; |
778 | 132 | start++; |
779 | 132 | break; |
780 | 344 | case ZEND_ROPE_INIT: |
781 | 344 | kind = ZEND_LIVE_ROPE; |
782 | | /* ROPE live ranges include the generating opcode. */ |
783 | 344 | def_opline--; |
784 | 344 | break; |
785 | 115 | case ZEND_FE_RESET_R: |
786 | 187 | case ZEND_FE_RESET_RW: |
787 | 187 | kind = ZEND_LIVE_LOOP; |
788 | 187 | start++; |
789 | 187 | break; |
790 | | /* Objects created via ZEND_NEW are only fully initialized |
791 | | * after the DO_FCALL (constructor call). |
792 | | * We are creating two live-ranges: ZEND_LINE_NEW for uninitialized |
793 | | * part, and ZEND_LIVE_TMPVAR for initialized. |
794 | | */ |
795 | 1.65k | case ZEND_NEW: |
796 | 1.65k | { |
797 | 1.65k | int level = 0; |
798 | 1.65k | uint32_t orig_start = start; |
799 | | |
800 | 2.79k | while (def_opline + 1 < use_opline) { |
801 | 2.78k | def_opline++; |
802 | 2.78k | start++; |
803 | 2.78k | switch (def_opline->opcode) { |
804 | 6 | case ZEND_INIT_FCALL: |
805 | 7 | case ZEND_INIT_FCALL_BY_NAME: |
806 | 7 | case ZEND_INIT_NS_FCALL_BY_NAME: |
807 | 10 | case ZEND_INIT_DYNAMIC_CALL: |
808 | 10 | case ZEND_INIT_USER_CALL: |
809 | 15 | case ZEND_INIT_METHOD_CALL: |
810 | 17 | case ZEND_INIT_STATIC_METHOD_CALL: |
811 | 17 | case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL: |
812 | 29 | case ZEND_NEW: |
813 | 29 | level++; |
814 | 29 | break; |
815 | 1.67k | case ZEND_DO_FCALL: |
816 | 1.67k | case ZEND_DO_FCALL_BY_NAME: |
817 | 1.67k | case ZEND_DO_ICALL: |
818 | 1.67k | case ZEND_DO_UCALL: |
819 | 1.67k | if (level == 0) { |
820 | 1.64k | goto done; |
821 | 1.64k | } |
822 | 29 | level--; |
823 | 29 | break; |
824 | 2.78k | } |
825 | 2.78k | } |
826 | 1.65k | done: |
827 | 1.65k | emit_live_range_raw(op_array, var_num, ZEND_LIVE_NEW, orig_start + 1, start + 1); |
828 | 1.65k | if (start + 1 == end) { |
829 | | /* Trivial live-range, no need to store it. */ |
830 | 1.63k | return; |
831 | 1.63k | } |
832 | 1.65k | } |
833 | 21 | ZEND_FALLTHROUGH; |
834 | 1.01k | default: |
835 | 1.01k | start++; |
836 | 1.01k | kind = ZEND_LIVE_TMPVAR; |
837 | | |
838 | | /* Check hook to determine whether a live range is necessary, |
839 | | * e.g. based on type info. */ |
840 | 1.01k | if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) { |
841 | 0 | return; |
842 | 0 | } |
843 | 1.01k | break; |
844 | 1.01k | case ZEND_COPY_TMP: |
845 | 18 | { |
846 | | /* COPY_TMP has a split live-range: One from the definition until the use in |
847 | | * "null" branch, and another from the start of the "non-null" branch to the |
848 | | * FREE opcode. */ |
849 | 18 | uint32_t rt_var_num = EX_NUM_TO_VAR(op_array->last_var + var_num); |
850 | 18 | if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) { |
851 | 0 | return; |
852 | 0 | } |
853 | | |
854 | 18 | kind = ZEND_LIVE_TMPVAR; |
855 | 18 | if (use_opline->opcode != ZEND_FREE) { |
856 | | /* This can happen if one branch of the coalesce has been optimized away. |
857 | | * In this case we should emit a normal live-range instead. */ |
858 | 3 | start++; |
859 | 3 | break; |
860 | 3 | } |
861 | | |
862 | 15 | zend_op *block_start_op = use_opline; |
863 | 19 | while ((block_start_op-1)->opcode == ZEND_FREE) { |
864 | 4 | block_start_op--; |
865 | 4 | } |
866 | | |
867 | 15 | start = block_start_op - op_array->opcodes; |
868 | 15 | if (start != end) { |
869 | 4 | emit_live_range_raw(op_array, var_num, kind, start, end); |
870 | 4 | } |
871 | | |
872 | 64 | do { |
873 | 64 | use_opline--; |
874 | | |
875 | | /* The use might have been optimized away, in which case we will hit the def |
876 | | * instead. */ |
877 | 64 | if (use_opline->opcode == ZEND_COPY_TMP && use_opline->result.var == rt_var_num) { |
878 | 0 | start = def_opline + 1 - op_array->opcodes; |
879 | 0 | emit_live_range_raw(op_array, var_num, kind, start, end); |
880 | 0 | return; |
881 | 0 | } |
882 | 64 | } while (!( |
883 | 64 | ((use_opline->op1_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op1.var == rt_var_num) || |
884 | 58 | ((use_opline->op2_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op2.var == rt_var_num) |
885 | 64 | )); |
886 | | |
887 | 15 | start = def_opline + 1 - op_array->opcodes; |
888 | 15 | end = use_opline - op_array->opcodes; |
889 | 15 | emit_live_range_raw(op_array, var_num, kind, start, end); |
890 | 15 | return; |
891 | 15 | } |
892 | 3.38k | } |
893 | | |
894 | 1.67k | emit_live_range_raw(op_array, var_num, kind, start, end); |
895 | 1.67k | } |
896 | | |
897 | 12.7k | static bool is_fake_def(zend_op *opline) { |
898 | | /* These opcodes only modify the result, not create it. */ |
899 | 12.7k | return opline->opcode == ZEND_ROPE_ADD |
900 | 11.9k | || opline->opcode == ZEND_ADD_ARRAY_ELEMENT |
901 | 11.8k | || opline->opcode == ZEND_ADD_ARRAY_UNPACK; |
902 | 12.7k | } |
903 | | |
904 | 8.91k | static bool keeps_op1_alive(zend_op *opline) { |
905 | | /* These opcodes don't consume their OP1 operand, |
906 | | * it is later freed by something else. */ |
907 | 8.91k | if (opline->opcode == ZEND_CASE |
908 | 8.91k | || opline->opcode == ZEND_CASE_STRICT |
909 | 8.91k | || opline->opcode == ZEND_SWITCH_LONG |
910 | 8.91k | || opline->opcode == ZEND_SWITCH_STRING |
911 | 8.91k | || opline->opcode == ZEND_MATCH |
912 | 8.91k | || opline->opcode == ZEND_MATCH_ERROR |
913 | 8.91k | || opline->opcode == ZEND_FETCH_LIST_R |
914 | 8.91k | || opline->opcode == ZEND_FETCH_LIST_W |
915 | 8.91k | || opline->opcode == ZEND_COPY_TMP |
916 | 8.91k | || opline->opcode == ZEND_EXT_STMT) { |
917 | 0 | return true; |
918 | 0 | } |
919 | 8.91k | ZEND_ASSERT(opline->opcode != ZEND_FE_FETCH_R |
920 | 8.91k | && opline->opcode != ZEND_FE_FETCH_RW |
921 | 8.91k | && opline->opcode != ZEND_VERIFY_RETURN_TYPE |
922 | 8.91k | && opline->opcode != ZEND_BIND_LEXICAL |
923 | 8.91k | && opline->opcode != ZEND_ROPE_ADD); |
924 | 8.91k | return false; |
925 | 8.91k | } |
926 | | |
927 | | /* Live ranges must be sorted by increasing start opline */ |
928 | 79 | static int cmp_live_range(const zend_live_range *a, const zend_live_range *b) { |
929 | 79 | return a->start - b->start; |
930 | 79 | } |
931 | 722 | static void swap_live_range(zend_live_range *a, zend_live_range *b) { |
932 | 722 | uint32_t tmp; |
933 | 722 | tmp = a->var; |
934 | 722 | a->var = b->var; |
935 | 722 | b->var = tmp; |
936 | 722 | tmp = a->start; |
937 | 722 | a->start = b->start; |
938 | 722 | b->start = tmp; |
939 | 722 | tmp = a->end; |
940 | 722 | a->end = b->end; |
941 | 722 | b->end = tmp; |
942 | 722 | } |
943 | | |
944 | | static void zend_calc_live_ranges( |
945 | 5.89k | zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) { |
946 | 5.89k | uint32_t opnum = op_array->last; |
947 | 5.89k | zend_op *opline = &op_array->opcodes[opnum]; |
948 | 5.89k | ALLOCA_FLAG(use_heap) |
949 | 5.89k | uint32_t var_offset = op_array->last_var; |
950 | 5.89k | uint32_t *last_use = do_alloca(sizeof(uint32_t) * op_array->T, use_heap); |
951 | 5.89k | memset(last_use, -1, sizeof(uint32_t) * op_array->T); |
952 | | |
953 | 5.89k | ZEND_ASSERT(!op_array->live_range); |
954 | 53.7k | while (opnum > 0) { |
955 | 47.8k | opnum--; |
956 | 47.8k | opline--; |
957 | | |
958 | | /* SEPARATE always redeclares its op1. For the purposes of live-ranges, |
959 | | * its declaration is irrelevant. Don't terminate the current live-range |
960 | | * to avoid breaking special handling of COPY_TMP. */ |
961 | 47.8k | if (opline->opcode == ZEND_SEPARATE) { |
962 | 25 | ZEND_ASSERT(opline->op1.var == opline->result.var); |
963 | 25 | continue; |
964 | 25 | } |
965 | | |
966 | 47.7k | if ((opline->result_type & (IS_TMP_VAR|IS_VAR)) && !is_fake_def(opline)) { |
967 | 11.8k | uint32_t var_num = EX_VAR_TO_NUM(opline->result.var) - var_offset; |
968 | | /* Defs without uses can occur for two reasons: Either because the result is |
969 | | * genuinely unused (e.g. omitted FREE opcode for an unused boolean result), or |
970 | | * because there are multiple defining opcodes (e.g. JMPZ_EX and QM_ASSIGN), in |
971 | | * which case the last one starts the live range. As such, we can simply ignore |
972 | | * missing uses here. */ |
973 | 11.8k | if (EXPECTED(last_use[var_num] != (uint32_t) -1)) { |
974 | | /* Skip trivial live-range */ |
975 | 11.5k | if (opnum + 1 != last_use[var_num]) { |
976 | 3.37k | uint32_t num; |
977 | | |
978 | 3.37k | #if 1 |
979 | | /* OP_DATA uses only op1 operand */ |
980 | 3.37k | ZEND_ASSERT(opline->opcode != ZEND_OP_DATA); |
981 | 3.37k | num = opnum; |
982 | | #else |
983 | | /* OP_DATA is really part of the previous opcode. */ |
984 | | num = opnum - (opline->opcode == ZEND_OP_DATA); |
985 | | #endif |
986 | 3.37k | emit_live_range(op_array, var_num, num, last_use[var_num], needs_live_range); |
987 | 3.37k | } |
988 | 11.5k | last_use[var_num] = (uint32_t) -1; |
989 | 11.5k | } |
990 | 11.8k | } |
991 | | |
992 | 47.7k | if ((opline->op1_type & (IS_TMP_VAR|IS_VAR))) { |
993 | 10.1k | uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var) - var_offset; |
994 | 10.1k | if (EXPECTED(last_use[var_num] == (uint32_t) -1)) { |
995 | 8.91k | if (EXPECTED(!keeps_op1_alive(opline))) { |
996 | | /* OP_DATA is really part of the previous opcode. */ |
997 | 8.91k | last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA); |
998 | 8.91k | } |
999 | 8.91k | } else if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) && opline->extended_value & ZEND_FREE_ON_RETURN) { |
1000 | 7 | int jump_offset = 1; |
1001 | 7 | while (((opline + jump_offset)->opcode == ZEND_FREE || (opline + jump_offset)->opcode == ZEND_FE_FREE) |
1002 | 0 | && (opline + jump_offset)->extended_value & ZEND_FREE_ON_RETURN) { |
1003 | 0 | ++jump_offset; |
1004 | 0 | } |
1005 | | // loop var frees directly precede the jump (or return) operand, except that ZEND_VERIFY_RETURN_TYPE may happen first. |
1006 | 7 | if ((opline + jump_offset)->opcode == ZEND_VERIFY_RETURN_TYPE) { |
1007 | 0 | ++jump_offset; |
1008 | 0 | } |
1009 | | /* FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees |
1010 | | * the loop variable on early return. We need to split the live range |
1011 | | * so GC doesn't access the freed variable after this FREE. */ |
1012 | 7 | uint32_t opnum_last_use = last_use[var_num]; |
1013 | 7 | zend_op *opline_last_use = op_array->opcodes + opnum_last_use; |
1014 | 7 | ZEND_ASSERT(opline_last_use->opcode == opline->opcode); // any ZEND_FREE_ON_RETURN must be followed by a FREE without |
1015 | 7 | if (opnum + jump_offset + 1 != opnum_last_use) { |
1016 | 6 | emit_live_range_raw(op_array, var_num, opline->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR, |
1017 | 6 | opnum + jump_offset + 1, opnum_last_use); |
1018 | 6 | } |
1019 | | |
1020 | | /* Update last_use so next range includes this FREE */ |
1021 | 7 | last_use[var_num] = opnum; |
1022 | | |
1023 | | /* Store opline offset to loop end */ |
1024 | 7 | opline->op2.opline_num = opnum_last_use - opnum; |
1025 | 7 | if (opline_last_use->extended_value & ZEND_FREE_ON_RETURN) { |
1026 | 1 | opline->op2.opline_num += opline_last_use->op2.opline_num; |
1027 | 1 | } |
1028 | 7 | } |
1029 | 10.1k | } |
1030 | 47.7k | if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) { |
1031 | 2.63k | uint32_t var_num = EX_VAR_TO_NUM(opline->op2.var) - var_offset; |
1032 | 2.63k | if (UNEXPECTED(opline->opcode == ZEND_FE_FETCH_R |
1033 | 2.63k | || opline->opcode == ZEND_FE_FETCH_RW)) { |
1034 | | /* OP2 of FE_FETCH is actually a def, not a use. */ |
1035 | 11 | if (last_use[var_num] != (uint32_t) -1) { |
1036 | 11 | if (opnum + 1 != last_use[var_num]) { |
1037 | 9 | emit_live_range( |
1038 | 9 | op_array, var_num, opnum, last_use[var_num], needs_live_range); |
1039 | 9 | } |
1040 | 11 | last_use[var_num] = (uint32_t) -1; |
1041 | 11 | } |
1042 | 2.62k | } else if (EXPECTED(last_use[var_num] == (uint32_t) -1)) { |
1043 | 2.60k | #if 1 |
1044 | | /* OP_DATA uses only op1 operand */ |
1045 | 2.60k | ZEND_ASSERT(opline->opcode != ZEND_OP_DATA); |
1046 | 2.60k | last_use[var_num] = opnum; |
1047 | | #else |
1048 | | /* OP_DATA is really part of the previous opcode. */ |
1049 | | last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA); |
1050 | | #endif |
1051 | 2.60k | } |
1052 | 2.63k | } |
1053 | 47.7k | } |
1054 | | |
1055 | 5.89k | if (op_array->last_live_range > 1) { |
1056 | 523 | zend_live_range *r1 = op_array->live_range; |
1057 | 523 | zend_live_range *r2 = r1 + op_array->last_live_range - 1; |
1058 | | |
1059 | | /* In most cases we need just revert the array */ |
1060 | 1.20k | while (r1 < r2) { |
1061 | 682 | swap_live_range(r1, r2); |
1062 | 682 | r1++; |
1063 | 682 | r2--; |
1064 | 682 | } |
1065 | | |
1066 | 523 | r1 = op_array->live_range; |
1067 | 523 | r2 = r1 + op_array->last_live_range - 1; |
1068 | 1.45k | while (r1 < r2) { |
1069 | 951 | if (r1->start > (r1+1)->start) { |
1070 | 23 | zend_sort(r1, r2 - r1 + 1, sizeof(zend_live_range), |
1071 | 23 | (compare_func_t) cmp_live_range, (swap_func_t) swap_live_range); |
1072 | 23 | break; |
1073 | 23 | } |
1074 | 928 | r1++; |
1075 | 928 | } |
1076 | 523 | } |
1077 | | |
1078 | 5.89k | free_alloca(last_use, use_heap); |
1079 | 5.89k | } |
1080 | | |
1081 | | ZEND_API void zend_recalc_live_ranges( |
1082 | 0 | zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) { |
1083 | | /* We assume that we never create live-ranges where there were none before. */ |
1084 | 0 | ZEND_ASSERT(op_array->live_range); |
1085 | 0 | efree(op_array->live_range); |
1086 | 0 | op_array->live_range = NULL; |
1087 | 0 | op_array->last_live_range = 0; |
1088 | 0 | zend_calc_live_ranges(op_array, needs_live_range); |
1089 | 0 | } |
1090 | | |
1091 | | ZEND_API void pass_two(zend_op_array *op_array) |
1092 | 5.90k | { |
1093 | 5.90k | zend_op *opline, *end; |
1094 | | |
1095 | 5.90k | if (!ZEND_USER_CODE(op_array->type)) { |
1096 | 0 | return; |
1097 | 0 | } |
1098 | 5.90k | if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) { |
1099 | 0 | zend_update_extended_stmts(op_array); |
1100 | 0 | } |
1101 | 5.90k | if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) { |
1102 | 5.90k | if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER) { |
1103 | 0 | zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array); |
1104 | 0 | } |
1105 | 5.90k | } |
1106 | | |
1107 | 5.90k | if (CG(context).vars_size != op_array->last_var) { |
1108 | 3.16k | op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var); |
1109 | 3.16k | CG(context).vars_size = op_array->last_var; |
1110 | 3.16k | } |
1111 | | |
1112 | | #if ZEND_USE_ABS_CONST_ADDR |
1113 | | if (CG(context).opcodes_size != op_array->last) { |
1114 | | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last); |
1115 | | CG(context).opcodes_size = op_array->last; |
1116 | | } |
1117 | | if (CG(context).literals_size != op_array->last_literal) { |
1118 | | op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal); |
1119 | | CG(context).literals_size = op_array->last_literal; |
1120 | | } |
1121 | | #else |
1122 | 5.90k | op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, |
1123 | 5.90k | ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) + |
1124 | 5.90k | sizeof(zval) * op_array->last_literal); |
1125 | 5.90k | if (op_array->literals) { |
1126 | 5.90k | memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16), |
1127 | 5.90k | op_array->literals, sizeof(zval) * op_array->last_literal); |
1128 | 5.90k | efree(op_array->literals); |
1129 | 5.90k | op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16)); |
1130 | 5.90k | } |
1131 | 5.90k | CG(context).opcodes_size = op_array->last; |
1132 | 5.90k | CG(context).literals_size = op_array->last_literal; |
1133 | 5.90k | #endif |
1134 | | |
1135 | 5.90k | op_array->T += ZEND_OBSERVER_ENABLED; // reserve last temporary for observers if enabled |
1136 | | |
1137 | | /* Needs to be set directly after the opcode/literal reallocation, to ensure destruction |
1138 | | * happens correctly if any of the following fixups generate a fatal error. */ |
1139 | 5.90k | op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO; |
1140 | | |
1141 | 5.90k | opline = op_array->opcodes; |
1142 | 5.90k | end = opline + op_array->last; |
1143 | 53.7k | while (opline < end) { |
1144 | 47.8k | switch (opline->opcode) { |
1145 | 100 | case ZEND_RECV_INIT: |
1146 | 100 | { |
1147 | 100 | zval *val = CT_CONSTANT(opline->op2); |
1148 | 100 | if (Z_TYPE_P(val) == IS_CONSTANT_AST) { |
1149 | 36 | uint32_t slot = ZEND_MM_ALIGNED_SIZE_EX(op_array->cache_size, 8); |
1150 | 36 | Z_CACHE_SLOT_P(val) = slot; |
1151 | 36 | op_array->cache_size += sizeof(zval); |
1152 | 36 | } |
1153 | 100 | } |
1154 | 100 | break; |
1155 | 51 | case ZEND_FAST_CALL: |
1156 | 51 | opline->op1.opline_num = op_array->try_catch_array[opline->op1.num].finally_op; |
1157 | 51 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1); |
1158 | 51 | break; |
1159 | 20 | case ZEND_BRK: |
1160 | 21 | case ZEND_CONT: |
1161 | 21 | { |
1162 | 21 | uint32_t jmp_target = zend_get_brk_cont_target(opline); |
1163 | | |
1164 | 21 | if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) { |
1165 | 0 | zend_check_finally_breakout(op_array, opline - op_array->opcodes, jmp_target); |
1166 | 0 | } |
1167 | 21 | opline->opcode = ZEND_JMP; |
1168 | 21 | opline->op1.opline_num = jmp_target; |
1169 | 21 | opline->op2.num = 0; |
1170 | 21 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1); |
1171 | 21 | } |
1172 | 21 | break; |
1173 | 40 | case ZEND_GOTO: |
1174 | 40 | zend_resolve_goto_label(op_array, opline); |
1175 | 40 | if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) { |
1176 | 7 | zend_check_finally_breakout(op_array, opline - op_array->opcodes, opline->op1.opline_num); |
1177 | 7 | } |
1178 | 40 | ZEND_FALLTHROUGH; |
1179 | 830 | case ZEND_JMP: |
1180 | 830 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1); |
1181 | 830 | break; |
1182 | 104 | case ZEND_JMPZ: |
1183 | 408 | case ZEND_JMPNZ: |
1184 | 444 | case ZEND_JMPZ_EX: |
1185 | 451 | case ZEND_JMPNZ_EX: |
1186 | 468 | case ZEND_JMP_SET: |
1187 | 505 | case ZEND_COALESCE: |
1188 | 620 | case ZEND_FE_RESET_R: |
1189 | 692 | case ZEND_FE_RESET_RW: |
1190 | 758 | case ZEND_JMP_NULL: |
1191 | 770 | case ZEND_BIND_INIT_STATIC_OR_JMP: |
1192 | 770 | case ZEND_JMP_FRAMELESS: |
1193 | 770 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2); |
1194 | 770 | break; |
1195 | 88 | case ZEND_ASSERT_CHECK: |
1196 | 88 | { |
1197 | | /* If result of assert is unused, result of check is unused as well */ |
1198 | 88 | zend_op *call = &op_array->opcodes[opline->op2.opline_num - 1]; |
1199 | 88 | if (call->opcode == ZEND_EXT_FCALL_END) { |
1200 | 0 | call--; |
1201 | 0 | } |
1202 | 88 | if (call->result_type == IS_UNUSED) { |
1203 | 41 | opline->result_type = IS_UNUSED; |
1204 | 41 | } |
1205 | 88 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2); |
1206 | 88 | break; |
1207 | 770 | } |
1208 | 115 | case ZEND_FE_FETCH_R: |
1209 | 187 | case ZEND_FE_FETCH_RW: |
1210 | | /* absolute index to relative offset */ |
1211 | 187 | opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value); |
1212 | 187 | break; |
1213 | 186 | case ZEND_CATCH: |
1214 | 186 | if (!(opline->extended_value & ZEND_LAST_CATCH)) { |
1215 | 2 | ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2); |
1216 | 2 | } |
1217 | 186 | break; |
1218 | 6.10k | case ZEND_RETURN: |
1219 | 6.22k | case ZEND_RETURN_BY_REF: |
1220 | 6.22k | if (op_array->fn_flags & ZEND_ACC_GENERATOR) { |
1221 | 105 | opline->opcode = ZEND_GENERATOR_RETURN; |
1222 | 105 | } |
1223 | 6.22k | break; |
1224 | 0 | case ZEND_SWITCH_LONG: |
1225 | 0 | case ZEND_SWITCH_STRING: |
1226 | 10 | case ZEND_MATCH: |
1227 | 10 | { |
1228 | | /* absolute indexes to relative offsets */ |
1229 | 10 | HashTable *jumptable = Z_ARRVAL_P(CT_CONSTANT(opline->op2)); |
1230 | 10 | zval *zv; |
1231 | 79 | ZEND_HASH_FOREACH_VAL(jumptable, zv) { |
1232 | 79 | Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, Z_LVAL_P(zv)); |
1233 | 79 | } ZEND_HASH_FOREACH_END(); |
1234 | | |
1235 | 10 | opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value); |
1236 | 10 | break; |
1237 | 0 | } |
1238 | 47.8k | } |
1239 | 47.8k | if (opline->op1_type == IS_CONST) { |
1240 | 14.8k | ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1); |
1241 | 32.9k | } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) { |
1242 | 10.0k | opline->op1.var = EX_NUM_TO_VAR(op_array->last_var + opline->op1.var); |
1243 | 10.0k | } |
1244 | 47.8k | if (opline->op2_type == IS_CONST) { |
1245 | 10.6k | ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2); |
1246 | 37.1k | } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) { |
1247 | 2.70k | opline->op2.var = EX_NUM_TO_VAR(op_array->last_var + opline->op2.var); |
1248 | 2.70k | } |
1249 | 47.8k | if (opline->result_type & (IS_VAR|IS_TMP_VAR)) { |
1250 | 12.7k | opline->result.var = EX_NUM_TO_VAR(op_array->last_var + opline->result.var); |
1251 | 12.7k | } |
1252 | 47.8k | ZEND_VM_SET_OPCODE_HANDLER(opline); |
1253 | 47.8k | opline++; |
1254 | 47.8k | } |
1255 | | |
1256 | 5.89k | zend_calc_live_ranges(op_array, NULL); |
1257 | | |
1258 | 5.89k | return; |
1259 | 5.90k | } |
1260 | | |
1261 | | ZEND_API unary_op_type get_unary_op(int opcode) |
1262 | 137 | { |
1263 | 137 | switch (opcode) { |
1264 | 71 | case ZEND_BW_NOT: |
1265 | 71 | return (unary_op_type) bitwise_not_function; |
1266 | 66 | case ZEND_BOOL_NOT: |
1267 | 66 | return (unary_op_type) boolean_not_function; |
1268 | 0 | default: |
1269 | 0 | return (unary_op_type) NULL; |
1270 | 137 | } |
1271 | 137 | } |
1272 | | |
1273 | | ZEND_API binary_op_type get_binary_op(int opcode) |
1274 | 1.11k | { |
1275 | 1.11k | switch (opcode) { |
1276 | 40 | case ZEND_ADD: |
1277 | 40 | return (binary_op_type) add_function; |
1278 | 124 | case ZEND_SUB: |
1279 | 124 | return (binary_op_type) sub_function; |
1280 | 389 | case ZEND_MUL: |
1281 | 389 | return (binary_op_type) mul_function; |
1282 | 75 | case ZEND_POW: |
1283 | 75 | return (binary_op_type) pow_function; |
1284 | 115 | case ZEND_DIV: |
1285 | 115 | return (binary_op_type) div_function; |
1286 | 21 | case ZEND_MOD: |
1287 | 21 | return (binary_op_type) mod_function; |
1288 | 25 | case ZEND_SL: |
1289 | 25 | return (binary_op_type) shift_left_function; |
1290 | 5 | case ZEND_SR: |
1291 | 5 | return (binary_op_type) shift_right_function; |
1292 | 0 | case ZEND_FAST_CONCAT: |
1293 | 61 | case ZEND_CONCAT: |
1294 | 61 | return (binary_op_type) concat_function; |
1295 | 8 | case ZEND_IS_IDENTICAL: |
1296 | 8 | case ZEND_CASE_STRICT: |
1297 | 8 | return (binary_op_type) is_identical_function; |
1298 | 0 | case ZEND_IS_NOT_IDENTICAL: |
1299 | 0 | return (binary_op_type) is_not_identical_function; |
1300 | 25 | case ZEND_IS_EQUAL: |
1301 | 25 | case ZEND_CASE: |
1302 | 25 | return (binary_op_type) is_equal_function; |
1303 | 4 | case ZEND_IS_NOT_EQUAL: |
1304 | 4 | return (binary_op_type) is_not_equal_function; |
1305 | 62 | case ZEND_IS_SMALLER: |
1306 | 62 | return (binary_op_type) is_smaller_function; |
1307 | 2 | case ZEND_IS_SMALLER_OR_EQUAL: |
1308 | 2 | return (binary_op_type) is_smaller_or_equal_function; |
1309 | 4 | case ZEND_SPACESHIP: |
1310 | 4 | return (binary_op_type) compare_function; |
1311 | 15 | case ZEND_BW_OR: |
1312 | 15 | return (binary_op_type) bitwise_or_function; |
1313 | 33 | case ZEND_BW_AND: |
1314 | 33 | return (binary_op_type) bitwise_and_function; |
1315 | 107 | case ZEND_BW_XOR: |
1316 | 107 | return (binary_op_type) bitwise_xor_function; |
1317 | 1 | case ZEND_BOOL_XOR: |
1318 | 1 | return (binary_op_type) boolean_xor_function; |
1319 | 0 | default: |
1320 | 0 | ZEND_UNREACHABLE(); |
1321 | 0 | return (binary_op_type) NULL; |
1322 | 1.11k | } |
1323 | 1.11k | } |