Coverage Report

Created: 2025-07-23 06:33

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