Coverage Report

Created: 2026-06-02 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
1.65M
{
49
1.65M
  op_array->type = type;
50
1.65M
  op_array->arg_flags[0] = 0;
51
1.65M
  op_array->arg_flags[1] = 0;
52
1.65M
  op_array->arg_flags[2] = 0;
53
54
1.65M
  op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
55
1.65M
  *op_array->refcount = 1;
56
1.65M
  op_array->last = 0;
57
1.65M
  op_array->opcodes = emalloc(initial_ops_size * sizeof(zend_op));
58
59
1.65M
  op_array->last_var = 0;
60
1.65M
  op_array->vars = NULL;
61
62
1.65M
  op_array->T = 0;
63
64
1.65M
  op_array->function_name = NULL;
65
1.65M
  op_array->filename = zend_string_copy(zend_get_compiled_filename());
66
1.65M
  op_array->doc_comment = NULL;
67
1.65M
  op_array->attributes = NULL;
68
69
1.65M
  op_array->arg_info = NULL;
70
1.65M
  op_array->num_args = 0;
71
1.65M
  op_array->required_num_args = 0;
72
73
1.65M
  op_array->scope = NULL;
74
1.65M
  op_array->prototype = NULL;
75
1.65M
  op_array->prop_info = NULL;
76
77
1.65M
  op_array->live_range = NULL;
78
1.65M
  op_array->try_catch_array = NULL;
79
1.65M
  op_array->last_live_range = 0;
80
81
1.65M
  op_array->static_variables = NULL;
82
1.65M
  ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
83
1.65M
  op_array->last_try_catch = 0;
84
85
1.65M
  op_array->fn_flags = 0;
86
1.65M
  op_array->fn_flags2 = 0;
87
88
1.65M
  op_array->last_literal = 0;
89
1.65M
  op_array->literals = NULL;
90
91
1.65M
  op_array->num_dynamic_func_defs = 0;
92
1.65M
  op_array->dynamic_func_defs = NULL;
93
94
1.65M
  ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
95
1.65M
  op_array->cache_size = zend_op_array_extension_handles * sizeof(void*);
96
97
1.65M
  memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
98
99
1.65M
  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
1.65M
}
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
724k
ZEND_API void zend_type_release(zend_type type, bool persistent) {
113
724k
  if (ZEND_TYPE_HAS_LIST(type)) {
114
173k
    zend_type *list_type;
115
524k
    ZEND_TYPE_LIST_FOREACH_MUTABLE(ZEND_TYPE_LIST(type), list_type) {
116
524k
      zend_type_release(*list_type, persistent);
117
524k
    } ZEND_TYPE_LIST_FOREACH_END();
118
173k
    if (!ZEND_TYPE_USES_ARENA(type)) {
119
0
      pefree(ZEND_TYPE_LIST(type), persistent);
120
0
    }
121
550k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
122
405k
    zend_string_release(ZEND_TYPE_NAME(type));
123
405k
  }
124
724k
}
125
126
ZEND_API void zend_free_internal_arg_info(zend_internal_function *function,
127
512
    bool persistent) {
128
512
  if (function->arg_info) {
129
512
    ZEND_ASSERT((persistent || (function->fn_flags & ZEND_ACC_NEVER_CACHE))
130
512
        && "Functions with non-persistent arg_info must be flagged ZEND_ACC_NEVER_CACHE");
131
132
512
    uint32_t i;
133
512
    uint32_t num_args = function->num_args + 1;
134
512
    zend_arg_info *arg_info = function->arg_info - 1;
135
136
512
    if (function->fn_flags & ZEND_ACC_VARIADIC) {
137
0
      num_args++;
138
0
    }
139
2.54k
    for (i = 0 ; i < num_args; i++) {
140
2.03k
      bool is_return_info = i == 0;
141
2.03k
      if (!is_return_info) {
142
1.52k
        zend_string_release_ex(arg_info[i].name, persistent);
143
1.52k
        if (arg_info[i].default_value) {
144
656
          zend_string_release_ex(arg_info[i].default_value,
145
656
              persistent);
146
656
        }
147
1.52k
      }
148
2.03k
      zend_type_release(arg_info[i].type, persistent);
149
2.03k
    }
150
151
512
    pefree(arg_info, persistent);
152
512
  }
153
512
}
154
155
ZEND_API void zend_function_dtor(zval *zv)
156
118k
{
157
118k
  zend_function *function = Z_PTR_P(zv);
158
159
118k
  if (function->type == ZEND_USER_FUNCTION) {
160
103k
    ZEND_ASSERT(function->common.function_name);
161
103k
    destroy_op_array(&function->op_array);
162
    /* op_arrays are allocated on arena, so we don't have to free them */
163
103k
  } else {
164
15.1k
    ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
165
15.1k
    ZEND_ASSERT(function->common.function_name);
166
15.1k
    zend_string_release_ex(function->common.function_name, 1);
167
168
    /* For methods this will be called explicitly. */
169
15.1k
    if (!function->common.scope) {
170
512
      zend_free_internal_arg_info(&function->internal_function, true);
171
172
512
      if (function->common.attributes) {
173
16
        zend_hash_release(function->common.attributes);
174
16
        function->common.attributes = NULL;
175
16
      }
176
512
    }
177
178
15.1k
    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
15.1k
    if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
184
512
      pefree(function, 1);
185
512
    }
186
15.1k
  }
187
118k
}
188
189
ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
190
2.59k
{
191
2.59k
  if (ZEND_MAP_PTR(ce->static_members_table) && CE_STATIC_MEMBERS(ce)) {
192
1.30k
    zval *static_members = CE_STATIC_MEMBERS(ce);
193
1.30k
    zval *p = static_members;
194
1.30k
    zval *end = p + ce->default_static_members_count;
195
1.30k
    ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
196
3.69k
    while (p != end) {
197
2.39k
      if (UNEXPECTED(Z_ISREF_P(p))) {
198
244
        zend_property_info *prop_info;
199
640
        ZEND_REF_FOREACH_TYPE_SOURCES(Z_REF_P(p), prop_info) {
200
640
          if (prop_info->ce == ce && p - static_members == prop_info->offset) {
201
183
            ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
202
183
            break; /* stop iteration here, the array might be realloc()'ed */
203
183
          }
204
640
        } ZEND_REF_FOREACH_TYPE_SOURCES_END();
205
244
      }
206
2.39k
      i_zval_ptr_dtor(p);
207
2.39k
      p++;
208
2.39k
    }
209
1.30k
    efree(static_members);
210
1.30k
  }
211
2.59k
}
212
213
static void _destroy_zend_class_traits_info(zend_class_entry *ce)
214
120k
{
215
120k
  uint32_t i;
216
217
242k
  for (i = 0; i < ce->num_traits; i++) {
218
121k
    zend_string_release_ex(ce->trait_names[i].name, 0);
219
121k
    zend_string_release_ex(ce->trait_names[i].lc_name, 0);
220
121k
  }
221
120k
  efree(ce->trait_names);
222
223
120k
  if (ce->trait_aliases) {
224
120k
    i = 0;
225
482k
    while (ce->trait_aliases[i]) {
226
362k
      if (ce->trait_aliases[i]->trait_method.method_name) {
227
362k
        zend_string_release_ex(ce->trait_aliases[i]->trait_method.method_name, 0);
228
362k
      }
229
362k
      if (ce->trait_aliases[i]->trait_method.class_name) {
230
1.01k
        zend_string_release_ex(ce->trait_aliases[i]->trait_method.class_name, 0);
231
1.01k
      }
232
233
362k
      if (ce->trait_aliases[i]->alias) {
234
362k
        zend_string_release_ex(ce->trait_aliases[i]->alias, 0);
235
362k
      }
236
237
362k
      efree(ce->trait_aliases[i]);
238
362k
      i++;
239
362k
    }
240
241
120k
    efree(ce->trait_aliases);
242
120k
  }
243
244
120k
  if (ce->trait_precedences) {
245
77
    uint32_t j;
246
247
77
    i = 0;
248
902
    while (ce->trait_precedences[i]) {
249
825
      zend_string_release_ex(ce->trait_precedences[i]->trait_method.method_name, 0);
250
825
      zend_string_release_ex(ce->trait_precedences[i]->trait_method.class_name, 0);
251
252
2.21k
      for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
253
1.39k
        zend_string_release_ex(ce->trait_precedences[i]->exclude_class_names[j], 0);
254
1.39k
      }
255
825
      efree(ce->trait_precedences[i]);
256
825
      i++;
257
825
    }
258
77
    efree(ce->trait_precedences);
259
77
  }
260
120k
}
261
262
ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce)
263
698
{
264
698
  zend_class_mutable_data *mutable_data = ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
265
266
698
  if (mutable_data) {
267
698
    HashTable *constants_table;
268
698
    zval *p;
269
270
698
    constants_table = mutable_data->constants_table;
271
698
    if (constants_table && constants_table != &ce->constants_table) {
272
429
      zend_class_constant *c;
273
274
2.96k
      ZEND_HASH_MAP_FOREACH_PTR(constants_table, c) {
275
2.96k
        if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
276
970
          zval_ptr_dtor_nogc(&c->value);
277
970
        }
278
2.96k
      } ZEND_HASH_FOREACH_END();
279
429
      zend_hash_destroy(constants_table);
280
429
      mutable_data->constants_table = NULL;
281
429
    }
282
283
698
    p = mutable_data->default_properties_table;
284
698
    if (p && p != ce->default_properties_table) {
285
191
      zval *end = p + ce->default_properties_count;
286
287
499
      while (p < end) {
288
308
        zval_ptr_dtor_nogc(p);
289
308
        p++;
290
308
      }
291
191
      mutable_data->default_properties_table = NULL;
292
191
    }
293
294
698
    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
698
    ZEND_MAP_PTR_SET_IMM(ce->mutable_data, NULL);
300
698
  }
301
698
}
302
303
ZEND_API void destroy_zend_class(zval *zv)
304
333k
{
305
333k
  zend_property_info *prop_info;
306
333k
  zend_class_entry *ce = Z_PTR_P(zv);
307
333k
  zend_function *fn;
308
309
333k
  if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
310
27.8k
    return;
311
27.8k
  }
312
313
  /* We don't increase the refcount for class aliases,
314
   * skip the destruction of aliases entirely. */
315
305k
  if (UNEXPECTED(Z_TYPE_INFO_P(zv) == IS_ALIAS_PTR)) {
316
32
    return;
317
32
  }
318
319
305k
  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
305k
  ZEND_ASSERT(ce->refcount > 0);
342
343
305k
  if (--ce->refcount > 0) {
344
0
    return;
345
0
  }
346
347
305k
  switch (ce->type) {
348
305k
    case ZEND_USER_CLASS:
349
305k
      if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
350
303k
        if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_RESOLVED_PARENT)) {
351
56.2k
          zend_string_release_ex(ce->parent_name, 0);
352
56.2k
        }
353
354
303k
        zend_string_release_ex(ce->name, 0);
355
303k
        zend_string_release_ex(ce->info.user.filename, 0);
356
357
303k
        if (ce->doc_comment) {
358
122
          zend_string_release_ex(ce->doc_comment, 0);
359
122
        }
360
361
303k
        if (ce->attributes) {
362
1.13k
          zend_hash_release(ce->attributes);
363
1.13k
        }
364
365
303k
        if (ce->num_interfaces > 0 && !(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
366
19.3k
          uint32_t i;
367
368
50.5k
          for (i = 0; i < ce->num_interfaces; i++) {
369
31.1k
            zend_string_release_ex(ce->interface_names[i].name, 0);
370
31.1k
            zend_string_release_ex(ce->interface_names[i].lc_name, 0);
371
31.1k
          }
372
19.3k
          efree(ce->interface_names);
373
19.3k
        }
374
375
303k
        if (ce->num_traits > 0) {
376
120k
          _destroy_zend_class_traits_info(ce);
377
120k
        }
378
303k
      }
379
380
305k
      if (ce->default_properties_table) {
381
27.7k
        zval *p = ce->default_properties_table;
382
27.7k
        zval *end = p + ce->default_properties_count;
383
384
60.1k
        while (p != end) {
385
32.4k
          i_zval_ptr_dtor(p);
386
32.4k
          p++;
387
32.4k
        }
388
27.7k
        efree(ce->default_properties_table);
389
27.7k
      }
390
305k
      if (ce->default_static_members_table) {
391
621
        zval *p = ce->default_static_members_table;
392
621
        zval *end = p + ce->default_static_members_count;
393
394
2.01k
        while (p != end) {
395
1.39k
          ZEND_ASSERT(!Z_ISREF_P(p));
396
1.39k
          i_zval_ptr_dtor(p);
397
1.39k
          p++;
398
1.39k
        }
399
621
        efree(ce->default_static_members_table);
400
621
      }
401
680k
      ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop_info) {
402
680k
        if (prop_info->ce == ce) {
403
33.1k
          zend_string_release_ex(prop_info->name, 0);
404
33.1k
          if (prop_info->doc_comment) {
405
164
            zend_string_release_ex(prop_info->doc_comment, 0);
406
164
          }
407
33.1k
          if (prop_info->attributes) {
408
1.58k
            zend_hash_release(prop_info->attributes);
409
1.58k
          }
410
33.1k
          zend_type_release(prop_info->type, /* persistent */ false);
411
33.1k
          if (prop_info->hooks) {
412
5.73k
            for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
413
3.82k
              if (prop_info->hooks[i]) {
414
2.29k
                destroy_op_array(&prop_info->hooks[i]->op_array);
415
2.29k
              }
416
3.82k
            }
417
1.91k
          }
418
33.1k
        }
419
680k
      } ZEND_HASH_FOREACH_END();
420
305k
      zend_hash_destroy(&ce->properties_info);
421
305k
      zend_hash_destroy(&ce->function_table);
422
305k
      if (zend_hash_num_elements(&ce->constants_table)) {
423
14.5k
        zend_class_constant *c;
424
425
102k
        ZEND_HASH_MAP_FOREACH_PTR(&ce->constants_table, c) {
426
102k
          if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
427
31.8k
            zval_ptr_dtor_nogc(&c->value);
428
31.8k
            if (c->doc_comment) {
429
22.3k
              zend_string_release_ex(c->doc_comment, 0);
430
22.3k
            }
431
31.8k
            if (c->attributes) {
432
445
              zend_hash_release(c->attributes);
433
445
            }
434
31.8k
          }
435
102k
        } ZEND_HASH_FOREACH_END();
436
14.5k
      }
437
305k
      zend_hash_destroy(&ce->constants_table);
438
305k
      if (ce->num_interfaces > 0 && (ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
439
1.97k
        efree(ce->interfaces);
440
1.97k
      }
441
305k
      if (ce->backed_enum_table) {
442
0
        zend_hash_release(ce->backed_enum_table);
443
0
      }
444
305k
      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
305k
  }
541
305k
}
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
154k
{
554
154k
  if (ZEND_MAP_PTR(op_array->static_variables_ptr)) {
555
2.57k
    HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr);
556
2.57k
    if (ht) {
557
2.56k
      zend_array_destroy(ht);
558
2.56k
      ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
559
2.56k
    }
560
2.57k
  }
561
154k
}
562
563
ZEND_API void destroy_op_array(zend_op_array *op_array)
564
493k
{
565
493k
  uint32_t i;
566
567
493k
  if ((op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE)
568
145k
   && ZEND_MAP_PTR(op_array->run_time_cache)) {
569
83.8k
    efree(ZEND_MAP_PTR(op_array->run_time_cache));
570
83.8k
  }
571
572
493k
  if (op_array->function_name) {
573
350k
    zend_string_release_ex(op_array->function_name, 0);
574
350k
  }
575
576
493k
  if (!op_array->refcount || --(*op_array->refcount) > 0) {
577
150k
    return;
578
150k
  }
579
580
342k
  efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
581
582
342k
  if (op_array->vars) {
583
295k
    i = op_array->last_var;
584
726k
    while (i > 0) {
585
431k
      i--;
586
431k
      zend_string_release_ex(op_array->vars[i], 0);
587
431k
    }
588
295k
    efree(op_array->vars);
589
295k
  }
590
591
  /* ZEND_ACC_PTR_OPS and ZEND_ACC_OVERRIDE use the same value */
592
342k
  if ((op_array->fn_flags & ZEND_ACC_PTR_OPS) && !op_array->function_name) {
593
34
    zend_op *op = op_array->opcodes;
594
34
    zend_op *end = op + op_array->last;
595
3.64k
    while (op < end) {
596
3.60k
      if (op->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) {
597
672
        HashTable *attributes = Z_PTR_P(RT_CONSTANT(op+1, (op+1)->op1));
598
672
        zend_hash_release(attributes);
599
672
      }
600
3.60k
      op++;
601
3.60k
    }
602
34
  }
603
342k
  if (op_array->literals) {
604
342k
    zval *literal = op_array->literals;
605
342k
    zval *end = literal + op_array->last_literal;
606
7.64M
    while (literal < end) {
607
7.29M
      zval_ptr_dtor_nogc(literal);
608
7.29M
      literal++;
609
7.29M
    }
610
342k
    if (ZEND_USE_ABS_CONST_ADDR
611
342k
     || !(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) {
612
0
      efree(op_array->literals);
613
0
    }
614
342k
  }
615
342k
  efree(op_array->opcodes);
616
617
342k
  zend_string_release_ex(op_array->filename, 0);
618
342k
  if (op_array->doc_comment) {
619
279
    zend_string_release_ex(op_array->doc_comment, 0);
620
279
  }
621
342k
  if (op_array->attributes) {
622
134k
    zend_hash_release(op_array->attributes);
623
134k
  }
624
342k
  if (op_array->live_range) {
625
153k
    efree(op_array->live_range);
626
153k
  }
627
342k
  if (op_array->try_catch_array) {
628
1.41k
    efree(op_array->try_catch_array);
629
1.41k
  }
630
342k
  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
342k
  if (op_array->arg_info) {
636
235k
    uint32_t num_args = op_array->num_args;
637
235k
    zend_arg_info *arg_info = op_array->arg_info;
638
639
235k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
640
89.8k
      arg_info--;
641
89.8k
      num_args++;
642
89.8k
    }
643
235k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
644
450
      num_args++;
645
450
    }
646
571k
    for (i = 0 ; i < num_args; i++) {
647
336k
      if (arg_info[i].name) {
648
246k
        zend_string_release_ex(arg_info[i].name, 0);
649
246k
      }
650
336k
      if (arg_info[i].doc_comment) {
651
66
        zend_string_release_ex(arg_info[i].doc_comment, 0);
652
66
      }
653
336k
      zend_type_release(arg_info[i].type, /* persistent */ false);
654
336k
    }
655
235k
    efree(arg_info);
656
235k
  }
657
342k
  if (op_array->static_variables) {
658
15.0k
    zend_array_destroy(op_array->static_variables);
659
15.0k
  }
660
342k
  if (op_array->num_dynamic_func_defs) {
661
331k
    for (i = 0; i < op_array->num_dynamic_func_defs; i++) {
662
213k
      destroy_op_array(op_array->dynamic_func_defs[i]);
663
213k
    }
664
117k
    efree(op_array->dynamic_func_defs);
665
117k
  }
666
342k
}
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
2.11k
{
700
9.64k
  for (uint32_t i = 0; i < op_array->last_try_catch; i++) {
701
7.56k
    if ((op_num < op_array->try_catch_array[i].finally_op ||
702
3.23k
          op_num >= op_array->try_catch_array[i].finally_end)
703
7.29k
        && (dst_num >= op_array->try_catch_array[i].finally_op &&
704
2.70k
           dst_num <= op_array->try_catch_array[i].finally_end)) {
705
14
      CG(in_compilation) = 1;
706
14
      CG(active_op_array) = op_array;
707
14
      CG(zend_lineno) = op_array->opcodes[op_num].lineno;
708
14
      zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
709
7.54k
    } else if ((op_num >= op_array->try_catch_array[i].finally_op
710
3.23k
          && op_num <= op_array->try_catch_array[i].finally_end)
711
266
        && (dst_num > op_array->try_catch_array[i].finally_end
712
252
          || dst_num < op_array->try_catch_array[i].finally_op)) {
713
20
      CG(in_compilation) = 1;
714
20
      CG(active_op_array) = op_array;
715
20
      CG(zend_lineno) = op_array->opcodes[op_num].lineno;
716
20
      zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
717
20
    }
718
7.56k
  }
719
2.11k
}
720
721
2.08k
static uint32_t zend_get_brk_cont_target(const zend_op *opline) {
722
2.08k
  int nest_levels = opline->op2.num;
723
2.08k
  int array_offset = opline->op1.num;
724
2.08k
  zend_brk_cont_element *jmp_to;
725
2.36k
  do {
726
2.36k
    jmp_to = &CG(context).brk_cont_array[array_offset];
727
2.36k
    if (nest_levels > 1) {
728
276
      array_offset = jmp_to->parent;
729
276
    }
730
2.36k
  } while (--nest_levels > 0);
731
732
2.08k
  return opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont;
733
2.08k
}
734
735
static void emit_live_range_raw(
736
20.2M
    zend_op_array *op_array, uint32_t var_num, uint32_t kind, uint32_t start, uint32_t end) {
737
20.2M
  zend_live_range *range;
738
739
20.2M
  op_array->last_live_range++;
740
20.2M
  op_array->live_range = erealloc(op_array->live_range,
741
20.2M
    sizeof(zend_live_range) * op_array->last_live_range);
742
743
20.2M
  ZEND_ASSERT(start < end);
744
20.2M
  range = &op_array->live_range[op_array->last_live_range - 1];
745
20.2M
  range->var = EX_NUM_TO_VAR(op_array->last_var + var_num);
746
20.2M
  range->var |= kind;
747
20.2M
  range->start = start;
748
20.2M
  range->end = end;
749
20.2M
}
750
751
static void emit_live_range(
752
    zend_op_array *op_array, uint32_t var_num, uint32_t start, uint32_t end,
753
20.1M
    zend_needs_live_range_cb needs_live_range) {
754
20.1M
  zend_op *def_opline = &op_array->opcodes[start], *orig_def_opline = def_opline;
755
20.1M
  zend_op *use_opline = &op_array->opcodes[end];
756
20.1M
  uint32_t kind;
757
758
20.1M
  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
4
    case ZEND_JMPZ_EX:
767
6
    case ZEND_JMPNZ_EX:
768
562
    case ZEND_BOOL:
769
4.64k
    case ZEND_BOOL_NOT:
770
    /* Classes don't have to be destroyed. */
771
5.84k
    case ZEND_FETCH_CLASS:
772
5.84k
    case ZEND_DECLARE_ANON_CLASS:
773
    /* FAST_CALLs don't have to be destroyed. */
774
8.96k
    case ZEND_FAST_CALL:
775
8.96k
      return;
776
13.7M
    case ZEND_BEGIN_SILENCE:
777
13.7M
      kind = ZEND_LIVE_SILENCE;
778
13.7M
      start++;
779
13.7M
      break;
780
82.0k
    case ZEND_ROPE_INIT:
781
82.0k
      kind = ZEND_LIVE_ROPE;
782
      /* ROPE live ranges include the generating opcode. */
783
82.0k
      def_opline--;
784
82.0k
      break;
785
27.8k
    case ZEND_FE_RESET_R:
786
30.1k
    case ZEND_FE_RESET_RW:
787
30.1k
      kind = ZEND_LIVE_LOOP;
788
30.1k
      start++;
789
30.1k
      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
120k
    case ZEND_NEW:
796
120k
    {
797
120k
      int level = 0;
798
120k
      uint32_t orig_start = start;
799
800
360k
      while (def_opline + 1 < use_opline) {
801
356k
        def_opline++;
802
356k
        start++;
803
356k
        switch (def_opline->opcode) {
804
777
          case ZEND_INIT_FCALL:
805
7.85k
          case ZEND_INIT_FCALL_BY_NAME:
806
15.7k
          case ZEND_INIT_NS_FCALL_BY_NAME:
807
16.5k
          case ZEND_INIT_DYNAMIC_CALL:
808
19.6k
          case ZEND_INIT_USER_CALL:
809
20.1k
          case ZEND_INIT_METHOD_CALL:
810
20.3k
          case ZEND_INIT_STATIC_METHOD_CALL:
811
20.3k
          case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
812
33.4k
          case ZEND_NEW:
813
33.4k
            level++;
814
33.4k
            break;
815
138k
          case ZEND_DO_FCALL:
816
143k
          case ZEND_DO_FCALL_BY_NAME:
817
143k
          case ZEND_DO_ICALL:
818
143k
          case ZEND_DO_UCALL:
819
143k
            if (level == 0) {
820
116k
              goto done;
821
116k
            }
822
27.3k
            level--;
823
27.3k
            break;
824
356k
        }
825
356k
      }
826
120k
done:
827
120k
      emit_live_range_raw(op_array, var_num, ZEND_LIVE_NEW, orig_start + 1, start + 1);
828
120k
      if (start + 1 == end) {
829
        /* Trivial live-range, no need to store it. */
830
115k
        return;
831
115k
      }
832
120k
    }
833
5.51k
    ZEND_FALLTHROUGH;
834
5.85M
    default:
835
5.85M
      start++;
836
5.85M
      kind = ZEND_LIVE_TMPVAR;
837
838
      /* Check hook to determine whether a live range is necessary,
839
       * e.g. based on type info. */
840
5.85M
      if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
841
3.95k
        return;
842
3.95k
      }
843
5.84M
      break;
844
5.84M
    case ZEND_COPY_TMP:
845
303k
    {
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
303k
      uint32_t rt_var_num = EX_NUM_TO_VAR(op_array->last_var + var_num);
850
303k
      if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
851
99
        return;
852
99
      }
853
854
303k
      kind = ZEND_LIVE_TMPVAR;
855
303k
      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
2.82k
        start++;
859
2.82k
        break;
860
2.82k
      }
861
862
300k
      zend_op *block_start_op = use_opline;
863
8.28M
      while ((block_start_op-1)->opcode == ZEND_FREE) {
864
7.98M
        block_start_op--;
865
7.98M
      }
866
867
300k
      start = block_start_op - op_array->opcodes;
868
300k
      if (start != end) {
869
174k
        emit_live_range_raw(op_array, var_num, kind, start, end);
870
174k
      }
871
872
17.0M
      do {
873
17.0M
        use_opline--;
874
875
        /* The use might have been optimized away, in which case we will hit the def
876
         * instead. */
877
17.0M
        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
17.0M
      } while (!(
883
17.0M
        ((use_opline->op1_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op1.var == rt_var_num) ||
884
16.9M
        ((use_opline->op2_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op2.var == rt_var_num)
885
17.0M
      ));
886
887
300k
      start = def_opline + 1 - op_array->opcodes;
888
300k
      end = use_opline - op_array->opcodes;
889
300k
      emit_live_range_raw(op_array, var_num, kind, start, end);
890
300k
      return;
891
300k
    }
892
20.1M
  }
893
894
19.6M
  emit_live_range_raw(op_array, var_num, kind, start, end);
895
19.6M
}
896
897
39.0M
static bool is_fake_def(zend_op *opline) {
898
  /* These opcodes only modify the result, not create it. */
899
39.0M
  return opline->opcode == ZEND_ROPE_ADD
900
38.3M
    || opline->opcode == ZEND_ADD_ARRAY_ELEMENT
901
38.2M
    || opline->opcode == ZEND_ADD_ARRAY_UNPACK;
902
39.0M
}
903
904
31.3M
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
31.3M
  if (opline->opcode == ZEND_CASE
908
31.3M
   || opline->opcode == ZEND_CASE_STRICT
909
31.3M
   || opline->opcode == ZEND_SWITCH_LONG
910
31.3M
   || opline->opcode == ZEND_SWITCH_STRING
911
31.3M
   || opline->opcode == ZEND_MATCH
912
31.3M
   || opline->opcode == ZEND_MATCH_ERROR
913
31.3M
   || opline->opcode == ZEND_FETCH_LIST_R
914
31.3M
   || opline->opcode == ZEND_FETCH_LIST_W
915
31.3M
   || opline->opcode == ZEND_COPY_TMP
916
31.3M
   || opline->opcode == ZEND_EXT_STMT) {
917
91
    return true;
918
91
  }
919
31.3M
  ZEND_ASSERT(opline->opcode != ZEND_FE_FETCH_R
920
31.3M
    && opline->opcode != ZEND_FE_FETCH_RW
921
31.3M
    && opline->opcode != ZEND_VERIFY_RETURN_TYPE
922
31.3M
    && opline->opcode != ZEND_BIND_LEXICAL
923
31.3M
    && opline->opcode != ZEND_ROPE_ADD);
924
31.3M
  return false;
925
31.3M
}
926
927
/* Live ranges must be sorted by increasing start opline */
928
15.0M
static int cmp_live_range(const zend_live_range *a, const zend_live_range *b) {
929
15.0M
  return a->start - b->start;
930
15.0M
}
931
13.8M
static void swap_live_range(zend_live_range *a, zend_live_range *b) {
932
13.8M
  uint32_t tmp;
933
13.8M
  tmp = a->var;
934
13.8M
  a->var = b->var;
935
13.8M
  b->var = tmp;
936
13.8M
  tmp = a->start;
937
13.8M
  a->start = b->start;
938
13.8M
  b->start = tmp;
939
13.8M
  tmp = a->end;
940
13.8M
  a->end = b->end;
941
13.8M
  b->end = tmp;
942
13.8M
}
943
944
static void zend_calc_live_ranges(
945
1.69M
    zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) {
946
1.69M
  uint32_t opnum = op_array->last;
947
1.69M
  zend_op *opline = &op_array->opcodes[opnum];
948
1.69M
  ALLOCA_FLAG(use_heap)
949
1.69M
  uint32_t var_offset = op_array->last_var;
950
1.69M
  uint32_t *last_use = do_alloca(sizeof(uint32_t) * op_array->T, use_heap);
951
1.69M
  memset(last_use, -1, sizeof(uint32_t) * op_array->T);
952
953
1.69M
  ZEND_ASSERT(!op_array->live_range);
954
75.9M
  while (opnum > 0) {
955
74.2M
    opnum--;
956
74.2M
    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
74.2M
    if (opline->opcode == ZEND_SEPARATE) {
962
121k
      ZEND_ASSERT(opline->op1.var == opline->result.var);
963
121k
      continue;
964
121k
    }
965
966
74.1M
    if ((opline->result_type & (IS_TMP_VAR|IS_VAR)) && !is_fake_def(opline)) {
967
38.2M
      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
38.2M
      if (EXPECTED(last_use[var_num] != (uint32_t) -1)) {
974
        /* Skip trivial live-range */
975
35.7M
        if (opnum + 1 != last_use[var_num]) {
976
20.1M
          uint32_t num;
977
978
20.1M
#if 1
979
          /* OP_DATA uses only op1 operand */
980
20.1M
          ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
981
20.1M
          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
20.1M
          emit_live_range(op_array, var_num, num, last_use[var_num], needs_live_range);
987
20.1M
        }
988
35.7M
        last_use[var_num] = (uint32_t) -1;
989
35.7M
      }
990
38.2M
    }
991
992
74.1M
    if ((opline->op1_type & (IS_TMP_VAR|IS_VAR))) {
993
32.5M
      uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var) - var_offset;
994
32.5M
      if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
995
31.3M
        if (EXPECTED(!keeps_op1_alive(opline))) {
996
          /* OP_DATA is really part of the previous opcode. */
997
31.3M
          last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
998
31.3M
        }
999
31.3M
      } else if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) && opline->extended_value & ZEND_FREE_ON_RETURN) {
1000
1.53k
        int jump_offset = 1;
1001
3.01k
        while (((opline + jump_offset)->opcode == ZEND_FREE || (opline + jump_offset)->opcode == ZEND_FE_FREE)
1002
1.47k
          && (opline + jump_offset)->extended_value & ZEND_FREE_ON_RETURN) {
1003
1.47k
          ++jump_offset;
1004
1.47k
        }
1005
        // loop var frees directly precede the jump (or return) operand, except that ZEND_VERIFY_RETURN_TYPE may happen first.
1006
1.53k
        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
1.53k
        uint32_t opnum_last_use = last_use[var_num];
1013
1.53k
        zend_op *opline_last_use = op_array->opcodes + opnum_last_use;
1014
1.53k
        ZEND_ASSERT(opline_last_use->opcode == opline->opcode); // any ZEND_FREE_ON_RETURN must be followed by a FREE without
1015
1.53k
        if (opnum + jump_offset + 1 != opnum_last_use) {
1016
1.25k
          emit_live_range_raw(op_array, var_num, opline->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
1017
1.25k
              opnum + jump_offset + 1, opnum_last_use);
1018
1.25k
        }
1019
1020
        /* Update last_use so next range includes this FREE */
1021
1.53k
        last_use[var_num] = opnum;
1022
1023
        /* Store opline offset to loop end */
1024
1.53k
        opline->op2.opline_num = opnum_last_use - opnum;
1025
1.53k
        if (opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
1026
915
          opline->op2.opline_num += opline_last_use->op2.opline_num;
1027
915
        }
1028
1.53k
      }
1029
32.5M
    }
1030
74.1M
    if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
1031
4.58M
      uint32_t var_num = EX_VAR_TO_NUM(opline->op2.var) - var_offset;
1032
4.58M
      if (UNEXPECTED(opline->opcode == ZEND_FE_FETCH_R
1033
4.58M
          || opline->opcode == ZEND_FE_FETCH_RW)) {
1034
        /* OP2 of FE_FETCH is actually a def, not a use. */
1035
986
        if (last_use[var_num] != (uint32_t) -1) {
1036
986
          if (opnum + 1 != last_use[var_num]) {
1037
703
            emit_live_range(
1038
703
              op_array, var_num, opnum, last_use[var_num], needs_live_range);
1039
703
          }
1040
986
          last_use[var_num] = (uint32_t) -1;
1041
986
        }
1042
4.58M
      } else if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
1043
4.40M
#if 1
1044
        /* OP_DATA uses only op1 operand */
1045
4.40M
        ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
1046
4.40M
        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
4.40M
      }
1052
4.58M
    }
1053
74.1M
  }
1054
1055
1.69M
  if (op_array->last_live_range > 1) {
1056
875k
    zend_live_range *r1 = op_array->live_range;
1057
875k
    zend_live_range *r2 = r1 + op_array->last_live_range - 1;
1058
1059
    /* In most cases we need just revert the array */
1060
10.6M
    while (r1 < r2) {
1061
9.76M
      swap_live_range(r1, r2);
1062
9.76M
      r1++;
1063
9.76M
      r2--;
1064
9.76M
    }
1065
1066
875k
    r1 = op_array->live_range;
1067
875k
    r2 = r1 + op_array->last_live_range - 1;
1068
18.0M
    while (r1 < r2) {
1069
17.2M
      if (r1->start > (r1+1)->start) {
1070
24.1k
        zend_sort(r1, r2 - r1 + 1, sizeof(zend_live_range),
1071
24.1k
          (compare_func_t) cmp_live_range, (swap_func_t) swap_live_range);
1072
24.1k
        break;
1073
24.1k
      }
1074
17.1M
      r1++;
1075
17.1M
    }
1076
875k
  }
1077
1078
1.69M
  free_alloca(last_use, use_heap);
1079
1.69M
}
1080
1081
ZEND_API void zend_recalc_live_ranges(
1082
47.7k
    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
47.7k
  ZEND_ASSERT(op_array->live_range);
1085
47.7k
  efree(op_array->live_range);
1086
47.7k
  op_array->live_range = NULL;
1087
47.7k
  op_array->last_live_range = 0;
1088
47.7k
  zend_calc_live_ranges(op_array, needs_live_range);
1089
47.7k
}
1090
1091
ZEND_API void pass_two(zend_op_array *op_array)
1092
1.65M
{
1093
1.65M
  zend_op *opline, *end;
1094
1095
1.65M
  if (!ZEND_USER_CODE(op_array->type)) {
1096
0
    return;
1097
0
  }
1098
1.65M
  if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) {
1099
0
    zend_update_extended_stmts(op_array);
1100
0
  }
1101
1.65M
  if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
1102
1.64M
    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
1.64M
  }
1106
1107
1.65M
  if (CG(context).vars_size != op_array->last_var) {
1108
1.46M
    op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
1109
1.46M
    CG(context).vars_size = op_array->last_var;
1110
1.46M
  }
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
1.65M
  op_array->opcodes = (zend_op *) erealloc(op_array->opcodes,
1123
1.65M
    ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
1124
1.65M
    sizeof(zval) * op_array->last_literal);
1125
1.65M
  if (op_array->literals) {
1126
1.65M
    memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16),
1127
1.65M
      op_array->literals, sizeof(zval) * op_array->last_literal);
1128
1.65M
    efree(op_array->literals);
1129
1.65M
    op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
1130
1.65M
  }
1131
1.65M
  CG(context).opcodes_size = op_array->last;
1132
1.65M
  CG(context).literals_size = op_array->last_literal;
1133
1.65M
#endif
1134
1135
1.65M
    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
1.65M
  op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
1140
1141
1.65M
  opline = op_array->opcodes;
1142
1.65M
  end = opline + op_array->last;
1143
74.1M
  while (opline < end) {
1144
72.4M
    switch (opline->opcode) {
1145
4.84k
      case ZEND_RECV_INIT:
1146
4.84k
        {
1147
4.84k
          zval *val = CT_CONSTANT(opline->op2);
1148
4.84k
          if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1149
1.95k
            uint32_t slot = ZEND_MM_ALIGNED_SIZE_EX(op_array->cache_size, 8);
1150
1.95k
            Z_CACHE_SLOT_P(val) = slot;
1151
1.95k
            op_array->cache_size += sizeof(zval);
1152
1.95k
          }
1153
4.84k
        }
1154
4.84k
        break;
1155
5.78k
      case ZEND_FAST_CALL:
1156
5.78k
        opline->op1.opline_num = op_array->try_catch_array[opline->op1.num].finally_op;
1157
5.78k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1158
5.78k
        break;
1159
910
      case ZEND_BRK:
1160
2.08k
      case ZEND_CONT:
1161
2.08k
        {
1162
2.08k
          uint32_t jmp_target = zend_get_brk_cont_target(opline);
1163
1164
2.08k
          if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1165
431
            zend_check_finally_breakout(op_array, opline - op_array->opcodes, jmp_target);
1166
431
          }
1167
2.08k
          opline->opcode = ZEND_JMP;
1168
2.08k
          opline->op1.opline_num = jmp_target;
1169
2.08k
          opline->op2.num = 0;
1170
2.08k
          ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1171
2.08k
        }
1172
2.08k
        break;
1173
2.11k
      case ZEND_GOTO:
1174
2.11k
        zend_resolve_goto_label(op_array, opline);
1175
2.11k
        if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1176
1.68k
          zend_check_finally_breakout(op_array, opline - op_array->opcodes, opline->op1.opline_num);
1177
1.68k
        }
1178
2.11k
        ZEND_FALLTHROUGH;
1179
886k
      case ZEND_JMP:
1180
886k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1181
886k
        break;
1182
170k
      case ZEND_JMPZ:
1183
233k
      case ZEND_JMPNZ:
1184
238k
      case ZEND_JMPZ_EX:
1185
243k
      case ZEND_JMPNZ_EX:
1186
246k
      case ZEND_JMP_SET:
1187
2.00M
      case ZEND_COALESCE:
1188
2.02M
      case ZEND_FE_RESET_R:
1189
2.02M
      case ZEND_FE_RESET_RW:
1190
2.08M
      case ZEND_JMP_NULL:
1191
2.08M
      case ZEND_BIND_INIT_STATIC_OR_JMP:
1192
2.68M
      case ZEND_JMP_FRAMELESS:
1193
2.68M
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1194
2.68M
        break;
1195
38.8k
      case ZEND_ASSERT_CHECK:
1196
38.8k
      {
1197
        /* If result of assert is unused, result of check is unused as well */
1198
38.8k
        zend_op *call = &op_array->opcodes[opline->op2.opline_num - 1];
1199
38.8k
        if (call->opcode == ZEND_EXT_FCALL_END) {
1200
0
          call--;
1201
0
        }
1202
38.8k
        if (call->result_type == IS_UNUSED) {
1203
30.4k
          opline->result_type = IS_UNUSED;
1204
30.4k
        }
1205
38.8k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1206
38.8k
        break;
1207
2.08M
      }
1208
15.0k
      case ZEND_FE_FETCH_R:
1209
16.4k
      case ZEND_FE_FETCH_RW:
1210
        /* absolute index to relative offset */
1211
16.4k
        opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
1212
16.4k
        break;
1213
37.9k
      case ZEND_CATCH:
1214
37.9k
        if (!(opline->extended_value & ZEND_LAST_CATCH)) {
1215
12.2k
          ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1216
12.2k
        }
1217
37.9k
        break;
1218
1.66M
      case ZEND_RETURN:
1219
1.78M
      case ZEND_RETURN_BY_REF:
1220
1.78M
        if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
1221
242k
          opline->opcode = ZEND_GENERATOR_RETURN;
1222
242k
        }
1223
1.78M
        break;
1224
13
      case ZEND_SWITCH_LONG:
1225
1.66k
      case ZEND_SWITCH_STRING:
1226
2.56k
      case ZEND_MATCH:
1227
2.56k
      {
1228
        /* absolute indexes to relative offsets */
1229
2.56k
        HashTable *jumptable = Z_ARRVAL_P(CT_CONSTANT(opline->op2));
1230
2.56k
        zval *zv;
1231
17.8k
        ZEND_HASH_FOREACH_VAL(jumptable, zv) {
1232
17.8k
          Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, Z_LVAL_P(zv));
1233
17.8k
        } ZEND_HASH_FOREACH_END();
1234
1235
2.56k
        opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
1236
2.56k
        break;
1237
1.66k
      }
1238
72.4M
    }
1239
72.4M
    if (opline->op1_type == IS_CONST) {
1240
5.52M
      ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
1241
66.9M
    } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
1242
32.0M
      opline->op1.var = EX_NUM_TO_VAR(op_array->last_var + opline->op1.var);
1243
32.0M
    }
1244
72.4M
    if (opline->op2_type == IS_CONST) {
1245
13.8M
      ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
1246
58.6M
    } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
1247
4.34M
      opline->op2.var = EX_NUM_TO_VAR(op_array->last_var + opline->op2.var);
1248
4.34M
    }
1249
72.4M
    if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
1250
38.1M
      opline->result.var = EX_NUM_TO_VAR(op_array->last_var + opline->result.var);
1251
38.1M
    }
1252
72.4M
    ZEND_VM_SET_OPCODE_HANDLER(opline);
1253
72.4M
    opline++;
1254
72.4M
  }
1255
1256
1.65M
  zend_calc_live_ranges(op_array, NULL);
1257
1258
1.65M
  return;
1259
1.65M
}
1260
1261
ZEND_API unary_op_type get_unary_op(int opcode)
1262
209k
{
1263
209k
  switch (opcode) {
1264
4.36k
    case ZEND_BW_NOT:
1265
4.36k
      return (unary_op_type) bitwise_not_function;
1266
203k
    case ZEND_BOOL_NOT:
1267
203k
      return (unary_op_type) boolean_not_function;
1268
1.61k
    default:
1269
1.61k
      return (unary_op_type) NULL;
1270
209k
  }
1271
209k
}
1272
1273
ZEND_API binary_op_type get_binary_op(int opcode)
1274
466k
{
1275
466k
  switch (opcode) {
1276
17.0k
    case ZEND_ADD:
1277
17.0k
      return (binary_op_type) add_function;
1278
14.0k
    case ZEND_SUB:
1279
14.0k
      return (binary_op_type) sub_function;
1280
59.1k
    case ZEND_MUL:
1281
59.1k
      return (binary_op_type) mul_function;
1282
7.67k
    case ZEND_POW:
1283
7.67k
      return (binary_op_type) pow_function;
1284
8.49k
    case ZEND_DIV:
1285
8.49k
      return (binary_op_type) div_function;
1286
3.17k
    case ZEND_MOD:
1287
3.17k
      return (binary_op_type) mod_function;
1288
2.23k
    case ZEND_SL:
1289
2.23k
      return (binary_op_type) shift_left_function;
1290
9.54k
    case ZEND_SR:
1291
9.54k
      return (binary_op_type) shift_right_function;
1292
139
    case ZEND_FAST_CONCAT:
1293
8.45k
    case ZEND_CONCAT:
1294
8.45k
      return (binary_op_type) concat_function;
1295
28.0k
    case ZEND_IS_IDENTICAL:
1296
28.0k
    case ZEND_CASE_STRICT:
1297
28.0k
      return (binary_op_type) is_identical_function;
1298
54.9k
    case ZEND_IS_NOT_IDENTICAL:
1299
54.9k
      return (binary_op_type) is_not_identical_function;
1300
8.28k
    case ZEND_IS_EQUAL:
1301
8.28k
    case ZEND_CASE:
1302
8.28k
      return (binary_op_type) is_equal_function;
1303
2.41k
    case ZEND_IS_NOT_EQUAL:
1304
2.41k
      return (binary_op_type) is_not_equal_function;
1305
146k
    case ZEND_IS_SMALLER:
1306
146k
      return (binary_op_type) is_smaller_function;
1307
67.0k
    case ZEND_IS_SMALLER_OR_EQUAL:
1308
67.0k
      return (binary_op_type) is_smaller_or_equal_function;
1309
402
    case ZEND_SPACESHIP:
1310
402
      return (binary_op_type) compare_function;
1311
14.2k
    case ZEND_BW_OR:
1312
14.2k
      return (binary_op_type) bitwise_or_function;
1313
8.08k
    case ZEND_BW_AND:
1314
8.08k
      return (binary_op_type) bitwise_and_function;
1315
3.66k
    case ZEND_BW_XOR:
1316
3.66k
      return (binary_op_type) bitwise_xor_function;
1317
3.27k
    case ZEND_BOOL_XOR:
1318
3.27k
      return (binary_op_type) boolean_xor_function;
1319
0
    default:
1320
0
      ZEND_UNREACHABLE();
1321
0
      return (binary_op_type) NULL;
1322
466k
  }
1323
466k
}