Coverage Report

Created: 2026-07-25 06:39

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
2.25M
{
49
2.25M
  op_array->type = type;
50
2.25M
  op_array->arg_flags[0] = 0;
51
2.25M
  op_array->arg_flags[1] = 0;
52
2.25M
  op_array->arg_flags[2] = 0;
53
54
2.25M
  op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
55
2.25M
  *op_array->refcount = 1;
56
2.25M
  op_array->last = 0;
57
2.25M
  op_array->opcodes = emalloc(initial_ops_size * sizeof(zend_op));
58
59
2.25M
  op_array->last_var = 0;
60
2.25M
  op_array->vars = NULL;
61
62
2.25M
  op_array->T = 0;
63
64
2.25M
  op_array->function_name = NULL;
65
2.25M
  op_array->filename = zend_string_copy(zend_get_compiled_filename());
66
2.25M
  op_array->doc_comment = NULL;
67
2.25M
  op_array->attributes = NULL;
68
69
2.25M
  op_array->arg_info = NULL;
70
2.25M
  op_array->num_args = 0;
71
2.25M
  op_array->required_num_args = 0;
72
73
2.25M
  op_array->scope = NULL;
74
2.25M
  op_array->prototype = NULL;
75
2.25M
  op_array->prop_info = NULL;
76
77
2.25M
  op_array->live_range = NULL;
78
2.25M
  op_array->try_catch_array = NULL;
79
2.25M
  op_array->last_live_range = 0;
80
81
2.25M
  op_array->static_variables = NULL;
82
2.25M
  ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
83
2.25M
  op_array->last_try_catch = 0;
84
85
2.25M
  op_array->fn_flags = 0;
86
2.25M
  op_array->fn_flags2 = 0;
87
88
2.25M
  op_array->last_literal = 0;
89
2.25M
  op_array->literals = NULL;
90
91
2.25M
  op_array->num_dynamic_func_defs = 0;
92
2.25M
  op_array->dynamic_func_defs = NULL;
93
94
2.25M
  ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
95
2.25M
  op_array->cache_size = zend_op_array_extension_handles * sizeof(void*);
96
97
2.25M
  memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
98
99
2.25M
  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
2.25M
}
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
887k
ZEND_API void zend_type_release(zend_type type, bool persistent) {
113
887k
  if (ZEND_TYPE_HAS_LIST(type)) {
114
209k
    zend_type *list_type;
115
631k
    ZEND_TYPE_LIST_FOREACH_MUTABLE(ZEND_TYPE_LIST(type), list_type) {
116
631k
      zend_type_release(*list_type, persistent);
117
631k
    } ZEND_TYPE_LIST_FOREACH_END();
118
209k
    if (!ZEND_TYPE_USES_ARENA(type)) {
119
0
      pefree(ZEND_TYPE_LIST(type), persistent);
120
0
    }
121
678k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
122
521k
    zend_string_release(ZEND_TYPE_NAME(type));
123
521k
  }
124
887k
}
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
138k
{
157
138k
  zend_function *function = Z_PTR_P(zv);
158
159
138k
  if (function->type == ZEND_USER_FUNCTION) {
160
124k
    ZEND_ASSERT(function->common.function_name);
161
124k
    destroy_op_array(&function->op_array);
162
    /* op_arrays are allocated on arena, so we don't have to free them */
163
124k
  } else {
164
14.6k
    ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
165
14.6k
    ZEND_ASSERT(function->common.function_name);
166
14.6k
    zend_string_release_ex(function->common.function_name, 1);
167
168
    /* For methods this will be called explicitly. */
169
14.6k
    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
14.6k
    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
14.6k
    if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
184
512
      pefree(function, 1);
185
512
    }
186
14.6k
  }
187
138k
}
188
189
ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
190
2.96k
{
191
2.96k
  if (ZEND_MAP_PTR(ce->static_members_table) && CE_STATIC_MEMBERS(ce)) {
192
1.52k
    zval *static_members = CE_STATIC_MEMBERS(ce);
193
1.52k
    zval *p = static_members;
194
1.52k
    zval *end = p + ce->default_static_members_count;
195
1.52k
    ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
196
4.26k
    while (p != end) {
197
2.74k
      if (UNEXPECTED(Z_ISREF_P(p))) {
198
312
        zend_property_info *prop_info;
199
802
        ZEND_REF_FOREACH_TYPE_SOURCES(Z_REF_P(p), prop_info) {
200
802
          if (prop_info->ce == ce && p - static_members == prop_info->offset) {
201
217
            ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(p), prop_info);
202
217
            break; /* stop iteration here, the array might be realloc()'ed */
203
217
          }
204
802
        } ZEND_REF_FOREACH_TYPE_SOURCES_END();
205
312
      }
206
2.74k
      i_zval_ptr_dtor(p);
207
2.74k
      p++;
208
2.74k
    }
209
1.52k
    efree(static_members);
210
1.52k
  }
211
2.96k
}
212
213
static void _destroy_zend_class_traits_info(zend_class_entry *ce)
214
65.2k
{
215
65.2k
  uint32_t i;
216
217
132k
  for (i = 0; i < ce->num_traits; i++) {
218
66.7k
    zend_string_release_ex(ce->trait_names[i].name, 0);
219
66.7k
    zend_string_release_ex(ce->trait_names[i].lc_name, 0);
220
66.7k
  }
221
65.2k
  efree(ce->trait_names);
222
223
65.2k
  if (ce->trait_aliases) {
224
64.5k
    i = 0;
225
259k
    while (ce->trait_aliases[i]) {
226
194k
      if (ce->trait_aliases[i]->trait_method.method_name) {
227
194k
        zend_string_release_ex(ce->trait_aliases[i]->trait_method.method_name, 0);
228
194k
      }
229
194k
      if (ce->trait_aliases[i]->trait_method.class_name) {
230
750
        zend_string_release_ex(ce->trait_aliases[i]->trait_method.class_name, 0);
231
750
      }
232
233
194k
      if (ce->trait_aliases[i]->alias) {
234
194k
        zend_string_release_ex(ce->trait_aliases[i]->alias, 0);
235
194k
      }
236
237
194k
      efree(ce->trait_aliases[i]);
238
194k
      i++;
239
194k
    }
240
241
64.5k
    efree(ce->trait_aliases);
242
64.5k
  }
243
244
65.2k
  if (ce->trait_precedences) {
245
89
    uint32_t j;
246
247
89
    i = 0;
248
1.09k
    while (ce->trait_precedences[i]) {
249
1.00k
      zend_string_release_ex(ce->trait_precedences[i]->trait_method.method_name, 0);
250
1.00k
      zend_string_release_ex(ce->trait_precedences[i]->trait_method.class_name, 0);
251
252
2.41k
      for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
253
1.41k
        zend_string_release_ex(ce->trait_precedences[i]->exclude_class_names[j], 0);
254
1.41k
      }
255
1.00k
      efree(ce->trait_precedences[i]);
256
1.00k
      i++;
257
1.00k
    }
258
89
    efree(ce->trait_precedences);
259
89
  }
260
65.2k
}
261
262
ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce)
263
792
{
264
792
  zend_class_mutable_data *mutable_data = ZEND_MAP_PTR_GET_IMM(ce->mutable_data);
265
266
792
  if (mutable_data) {
267
792
    HashTable *constants_table;
268
792
    zval *p;
269
270
792
    constants_table = mutable_data->constants_table;
271
792
    if (constants_table && constants_table != &ce->constants_table) {
272
514
      zend_class_constant *c;
273
274
3.48k
      ZEND_HASH_MAP_FOREACH_PTR(constants_table, c) {
275
3.48k
        if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
276
1.11k
          zval_ptr_dtor_nogc(&c->value);
277
1.11k
        }
278
3.48k
      } ZEND_HASH_FOREACH_END();
279
514
      zend_hash_destroy(constants_table);
280
514
      mutable_data->constants_table = NULL;
281
514
    }
282
283
792
    p = mutable_data->default_properties_table;
284
792
    if (p && p != ce->default_properties_table) {
285
210
      zval *end = p + ce->default_properties_count;
286
287
601
      while (p < end) {
288
391
        zval_ptr_dtor_nogc(p);
289
391
        p++;
290
391
      }
291
210
      mutable_data->default_properties_table = NULL;
292
210
    }
293
294
792
    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
792
    ZEND_MAP_PTR_SET_IMM(ce->mutable_data, NULL);
300
792
  }
301
792
}
302
303
ZEND_API void destroy_zend_class(zval *zv)
304
449k
{
305
449k
  zend_property_info *prop_info;
306
449k
  zend_class_entry *ce = Z_PTR_P(zv);
307
449k
  zend_function *fn;
308
309
449k
  if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
310
32.9k
    return;
311
32.9k
  }
312
313
  /* We don't increase the refcount for class aliases,
314
   * skip the destruction of aliases entirely. */
315
416k
  if (UNEXPECTED(Z_TYPE_INFO_P(zv) == IS_ALIAS_PTR)) {
316
36
    return;
317
36
  }
318
319
416k
  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
416k
  ZEND_ASSERT(ce->refcount > 0);
342
343
416k
  if (--ce->refcount > 0) {
344
0
    return;
345
0
  }
346
347
416k
  switch (ce->type) {
348
416k
    case ZEND_USER_CLASS:
349
416k
      if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
350
413k
        if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_RESOLVED_PARENT)) {
351
63.7k
          zend_string_release_ex(ce->parent_name, 0);
352
63.7k
        }
353
354
413k
        zend_string_release_ex(ce->name, 0);
355
413k
        zend_string_release_ex(ce->info.user.filename, 0);
356
357
413k
        if (ce->doc_comment) {
358
59
          zend_string_release_ex(ce->doc_comment, 0);
359
59
        }
360
361
413k
        if (ce->attributes) {
362
1.27k
          zend_hash_release(ce->attributes);
363
1.27k
        }
364
365
413k
        if (ce->num_interfaces > 0 && !(ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
366
159k
          uint32_t i;
367
368
452k
          for (i = 0; i < ce->num_interfaces; i++) {
369
292k
            zend_string_release_ex(ce->interface_names[i].name, 0);
370
292k
            zend_string_release_ex(ce->interface_names[i].lc_name, 0);
371
292k
          }
372
159k
          efree(ce->interface_names);
373
159k
        }
374
375
413k
        if (ce->num_traits > 0) {
376
65.2k
          _destroy_zend_class_traits_info(ce);
377
65.2k
        }
378
413k
      }
379
380
416k
      if (ce->default_properties_table) {
381
24.3k
        zval *p = ce->default_properties_table;
382
24.3k
        zval *end = p + ce->default_properties_count;
383
384
53.5k
        while (p != end) {
385
29.2k
          i_zval_ptr_dtor(p);
386
29.2k
          p++;
387
29.2k
        }
388
24.3k
        efree(ce->default_properties_table);
389
24.3k
      }
390
416k
      if (ce->default_static_members_table) {
391
620
        zval *p = ce->default_static_members_table;
392
620
        zval *end = p + ce->default_static_members_count;
393
394
2.32k
        while (p != end) {
395
1.70k
          ZEND_ASSERT(!Z_ISREF_P(p));
396
1.70k
          i_zval_ptr_dtor(p);
397
1.70k
          p++;
398
1.70k
        }
399
620
        efree(ce->default_static_members_table);
400
620
      }
401
895k
      ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop_info) {
402
895k
        if (prop_info->ce == ce) {
403
30.3k
          zend_string_release_ex(prop_info->name, 0);
404
30.3k
          if (prop_info->doc_comment) {
405
105
            zend_string_release_ex(prop_info->doc_comment, 0);
406
105
          }
407
30.3k
          if (prop_info->attributes) {
408
1.77k
            zend_hash_release(prop_info->attributes);
409
1.77k
          }
410
30.3k
          zend_type_release(prop_info->type, /* persistent */ false);
411
30.3k
          if (prop_info->hooks) {
412
6.23k
            for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
413
4.15k
              if (prop_info->hooks[i]) {
414
2.57k
                destroy_op_array(&prop_info->hooks[i]->op_array);
415
2.57k
              }
416
4.15k
            }
417
2.07k
          }
418
30.3k
        }
419
895k
      } ZEND_HASH_FOREACH_END();
420
416k
      zend_hash_destroy(&ce->properties_info);
421
416k
      zend_hash_destroy(&ce->function_table);
422
416k
      if (zend_hash_num_elements(&ce->constants_table)) {
423
13.3k
        zend_class_constant *c;
424
425
91.2k
        ZEND_HASH_MAP_FOREACH_PTR(&ce->constants_table, c) {
426
91.2k
          if (c->ce == ce || (Z_CONSTANT_FLAGS(c->value) & CONST_OWNED)) {
427
27.4k
            zval_ptr_dtor_nogc(&c->value);
428
27.4k
            if (c->doc_comment) {
429
16.3k
              zend_string_release_ex(c->doc_comment, 0);
430
16.3k
            }
431
27.4k
            if (c->attributes) {
432
601
              zend_hash_release(c->attributes);
433
601
            }
434
27.4k
          }
435
91.2k
        } ZEND_HASH_FOREACH_END();
436
13.3k
      }
437
416k
      zend_hash_destroy(&ce->constants_table);
438
416k
      if (ce->num_interfaces > 0 && (ce->ce_flags & ZEND_ACC_RESOLVED_INTERFACES)) {
439
2.21k
        efree(ce->interfaces);
440
2.21k
      }
441
416k
      if (ce->backed_enum_table) {
442
0
        zend_hash_release(ce->backed_enum_table);
443
0
      }
444
416k
      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 && !(fn->common.fn_flags & ZEND_ACC_TRAIT_CLONE)) {
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
      if (ce->num_traits > 0) {
539
0
        for (uint32_t i = 0; i < ce->num_traits; i++) {
540
0
          zend_string_release(ce->trait_names[i].name);
541
0
          zend_string_release(ce->trait_names[i].lc_name);
542
0
        }
543
0
        free(ce->trait_names);
544
0
      }
545
0
      free(ce);
546
0
      break;
547
416k
  }
548
416k
}
549
550
void zend_class_add_ref(zval *zv)
551
0
{
552
0
  zend_class_entry *ce = Z_PTR_P(zv);
553
554
0
  if (Z_TYPE_P(zv) != IS_ALIAS_PTR && !(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
555
0
    ce->refcount++;
556
0
  }
557
0
}
558
559
ZEND_API void zend_destroy_static_vars(zend_op_array *op_array)
560
161k
{
561
161k
  if (ZEND_MAP_PTR(op_array->static_variables_ptr)) {
562
3.58k
    HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr);
563
3.58k
    if (ht) {
564
3.56k
      zend_array_destroy(ht);
565
3.56k
      ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
566
3.56k
    }
567
3.58k
  }
568
161k
}
569
570
ZEND_API void destroy_op_array(zend_op_array *op_array)
571
594k
{
572
594k
  uint32_t i;
573
574
594k
  if ((op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE)
575
150k
   && ZEND_MAP_PTR(op_array->run_time_cache)) {
576
119k
    efree(ZEND_MAP_PTR(op_array->run_time_cache));
577
119k
  }
578
579
594k
  if (op_array->function_name) {
580
447k
    zend_string_release_ex(op_array->function_name, 0);
581
447k
  }
582
583
594k
  if (!op_array->refcount || --(*op_array->refcount) > 0) {
584
162k
    return;
585
162k
  }
586
587
432k
  efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
588
589
432k
  if (op_array->vars) {
590
383k
    i = op_array->last_var;
591
907k
    while (i > 0) {
592
523k
      i--;
593
523k
      zend_string_release_ex(op_array->vars[i], 0);
594
523k
    }
595
383k
    efree(op_array->vars);
596
383k
  }
597
598
  /* ZEND_ACC_PTR_OPS and ZEND_ACC_OVERRIDE use the same value */
599
432k
  if ((op_array->fn_flags & ZEND_ACC_PTR_OPS) && !op_array->function_name) {
600
38
    zend_op *op = op_array->opcodes;
601
38
    zend_op *end = op + op_array->last;
602
3.23k
    while (op < end) {
603
3.19k
      if (op->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) {
604
560
        HashTable *attributes = Z_PTR_P(RT_CONSTANT(op+1, (op+1)->op1));
605
560
        zend_hash_release(attributes);
606
560
      }
607
3.19k
      op++;
608
3.19k
    }
609
38
  }
610
432k
  if (op_array->literals) {
611
432k
    zval *literal = op_array->literals;
612
432k
    zval *end = literal + op_array->last_literal;
613
9.73M
    while (literal < end) {
614
9.30M
      zval_ptr_dtor_nogc(literal);
615
9.30M
      literal++;
616
9.30M
    }
617
432k
    if (ZEND_USE_ABS_CONST_ADDR
618
432k
     || !(op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO)) {
619
0
      efree(op_array->literals);
620
0
    }
621
432k
  }
622
432k
  efree(op_array->opcodes);
623
624
432k
  zend_string_release_ex(op_array->filename, 0);
625
432k
  if (op_array->doc_comment) {
626
473
    zend_string_release_ex(op_array->doc_comment, 0);
627
473
  }
628
432k
  if (op_array->attributes) {
629
185k
    zend_hash_release(op_array->attributes);
630
185k
  }
631
432k
  if (op_array->live_range) {
632
217k
    efree(op_array->live_range);
633
217k
  }
634
432k
  if (op_array->try_catch_array) {
635
1.88k
    efree(op_array->try_catch_array);
636
1.88k
  }
637
432k
  if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR) {
638
0
    if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
639
0
      zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
640
0
    }
641
0
  }
642
432k
  if (op_array->arg_info) {
643
309k
    uint32_t num_args = op_array->num_args;
644
309k
    zend_arg_info *arg_info = op_array->arg_info;
645
646
309k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
647
111k
      arg_info--;
648
111k
      num_args++;
649
111k
    }
650
309k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
651
586
      num_args++;
652
586
    }
653
741k
    for (i = 0 ; i < num_args; i++) {
654
432k
      if (arg_info[i].name) {
655
320k
        zend_string_release_ex(arg_info[i].name, 0);
656
320k
      }
657
432k
      if (arg_info[i].doc_comment) {
658
103
        zend_string_release_ex(arg_info[i].doc_comment, 0);
659
103
      }
660
432k
      zend_type_release(arg_info[i].type, /* persistent */ false);
661
432k
    }
662
309k
    efree(arg_info);
663
309k
  }
664
432k
  if (op_array->static_variables) {
665
15.5k
    zend_array_destroy(op_array->static_variables);
666
15.5k
  }
667
432k
  if (op_array->num_dynamic_func_defs) {
668
444k
    for (i = 0; i < op_array->num_dynamic_func_defs; i++) {
669
281k
      destroy_op_array(op_array->dynamic_func_defs[i]);
670
281k
    }
671
162k
    efree(op_array->dynamic_func_defs);
672
162k
  }
673
432k
}
674
675
static void zend_update_extended_stmts(zend_op_array *op_array)
676
0
{
677
0
  zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
678
679
0
  while (opline<end) {
680
0
    if (opline->opcode == ZEND_EXT_STMT) {
681
0
      if (opline+1<end) {
682
0
        if ((opline+1)->opcode == ZEND_EXT_STMT) {
683
0
          opline->opcode = ZEND_NOP;
684
0
          opline++;
685
0
          continue;
686
0
        }
687
0
        if (opline+1<end) {
688
0
          opline->lineno = (opline+1)->lineno;
689
0
        }
690
0
      } else {
691
0
        opline->opcode = ZEND_NOP;
692
0
      }
693
0
    }
694
0
    opline++;
695
0
  }
696
0
}
697
698
static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
699
0
{
700
0
  if (extension->op_array_handler) {
701
0
    extension->op_array_handler(op_array);
702
0
  }
703
0
}
704
705
static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
706
1.41k
{
707
6.77k
  for (uint32_t i = 0; i < op_array->last_try_catch; i++) {
708
5.40k
    if (!op_array->try_catch_array[i].finally_op) {
709
316
      continue;
710
316
    }
711
712
5.08k
    if ((op_num < op_array->try_catch_array[i].finally_op ||
713
2.40k
          op_num >= op_array->try_catch_array[i].finally_end)
714
4.84k
        && (dst_num >= op_array->try_catch_array[i].finally_op &&
715
1.98k
           dst_num <= op_array->try_catch_array[i].finally_end)) {
716
15
      CG(in_compilation) = 1;
717
15
      CG(active_op_array) = op_array;
718
15
      CG(zend_lineno) = op_array->opcodes[op_num].lineno;
719
15
      zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
720
5.07k
    } else if ((op_num >= op_array->try_catch_array[i].finally_op
721
2.39k
          && op_num <= op_array->try_catch_array[i].finally_end)
722
242
        && (dst_num > op_array->try_catch_array[i].finally_end
723
228
          || dst_num < op_array->try_catch_array[i].finally_op)) {
724
20
      CG(in_compilation) = 1;
725
20
      CG(active_op_array) = op_array;
726
20
      CG(zend_lineno) = op_array->opcodes[op_num].lineno;
727
20
      zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
728
20
    }
729
5.08k
  }
730
1.41k
}
731
732
2.29k
static uint32_t zend_get_brk_cont_target(const zend_op *opline) {
733
2.29k
  int nest_levels = opline->op2.num;
734
2.29k
  int array_offset = opline->op1.num;
735
2.29k
  zend_brk_cont_element *jmp_to;
736
2.63k
  do {
737
2.63k
    jmp_to = &CG(context).brk_cont_array[array_offset];
738
2.63k
    if (nest_levels > 1) {
739
339
      array_offset = jmp_to->parent;
740
339
    }
741
2.63k
  } while (--nest_levels > 0);
742
743
2.29k
  return opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont;
744
2.29k
}
745
746
static void emit_live_range_raw(
747
24.1M
    zend_op_array *op_array, uint32_t var_num, uint32_t kind, uint32_t start, uint32_t end) {
748
24.1M
  zend_live_range *range;
749
750
24.1M
  op_array->last_live_range++;
751
24.1M
  op_array->live_range = erealloc(op_array->live_range,
752
24.1M
    sizeof(zend_live_range) * op_array->last_live_range);
753
754
24.1M
  ZEND_ASSERT(start < end);
755
24.1M
  range = &op_array->live_range[op_array->last_live_range - 1];
756
24.1M
  range->var = EX_NUM_TO_VAR(op_array->last_var + var_num);
757
24.1M
  range->var |= kind;
758
24.1M
  range->start = start;
759
24.1M
  range->end = end;
760
24.1M
}
761
762
static void emit_live_range(
763
    zend_op_array *op_array, uint32_t var_num, uint32_t start, uint32_t end,
764
23.9M
    zend_needs_live_range_cb needs_live_range) {
765
23.9M
  zend_op *def_opline = &op_array->opcodes[start], *orig_def_opline = def_opline;
766
23.9M
  zend_op *use_opline = &op_array->opcodes[end];
767
23.9M
  uint32_t kind;
768
769
23.9M
  switch (def_opline->opcode) {
770
    /* These should never be the first def. */
771
0
    case ZEND_ADD_ARRAY_ELEMENT:
772
0
    case ZEND_ADD_ARRAY_UNPACK:
773
0
    case ZEND_ROPE_ADD:
774
0
      ZEND_UNREACHABLE();
775
0
      return;
776
    /* Result is boolean, it doesn't have to be destroyed. */
777
8
    case ZEND_JMPZ_EX:
778
10
    case ZEND_JMPNZ_EX:
779
495
    case ZEND_BOOL:
780
5.98k
    case ZEND_BOOL_NOT:
781
    /* Classes don't have to be destroyed. */
782
6.80k
    case ZEND_FETCH_CLASS:
783
6.80k
    case ZEND_DECLARE_ANON_CLASS:
784
    /* FAST_CALLs don't have to be destroyed. */
785
10.0k
    case ZEND_FAST_CALL:
786
10.0k
      return;
787
16.3M
    case ZEND_BEGIN_SILENCE:
788
16.3M
      kind = ZEND_LIVE_SILENCE;
789
16.3M
      start++;
790
16.3M
      break;
791
118k
    case ZEND_ROPE_INIT:
792
118k
      kind = ZEND_LIVE_ROPE;
793
      /* ROPE live ranges include the generating opcode. */
794
118k
      def_opline--;
795
118k
      break;
796
32.1k
    case ZEND_FE_RESET_R:
797
35.0k
    case ZEND_FE_RESET_RW:
798
35.0k
      kind = ZEND_LIVE_LOOP;
799
35.0k
      start++;
800
35.0k
      break;
801
    /* Objects created via ZEND_NEW are only fully initialized
802
     * after the DO_FCALL (constructor call).
803
     * We are creating two live-ranges: ZEND_LINE_NEW for uninitialized
804
     * part, and ZEND_LIVE_TMPVAR for initialized.
805
     */
806
289k
    case ZEND_NEW:
807
289k
    {
808
289k
      int level = 0;
809
289k
      uint32_t orig_start = start;
810
811
560k
      while (def_opline + 1 < use_opline) {
812
556k
        def_opline++;
813
556k
        start++;
814
556k
        switch (def_opline->opcode) {
815
1.04k
          case ZEND_INIT_FCALL:
816
11.0k
          case ZEND_INIT_FCALL_BY_NAME:
817
17.8k
          case ZEND_INIT_NS_FCALL_BY_NAME:
818
18.9k
          case ZEND_INIT_DYNAMIC_CALL:
819
25.3k
          case ZEND_INIT_USER_CALL:
820
25.5k
          case ZEND_INIT_METHOD_CALL:
821
25.7k
          case ZEND_INIT_STATIC_METHOD_CALL:
822
25.7k
          case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
823
36.1k
          case ZEND_NEW:
824
36.1k
            level++;
825
36.1k
            break;
826
307k
          case ZEND_DO_FCALL:
827
316k
          case ZEND_DO_FCALL_BY_NAME:
828
316k
          case ZEND_DO_ICALL:
829
317k
          case ZEND_DO_UCALL:
830
317k
            if (level == 0) {
831
286k
              goto done;
832
286k
            }
833
31.1k
            level--;
834
31.1k
            break;
835
556k
        }
836
556k
      }
837
289k
done:
838
289k
      emit_live_range_raw(op_array, var_num, ZEND_LIVE_NEW, orig_start + 1, start + 1);
839
289k
      if (start + 1 == end) {
840
        /* Trivial live-range, no need to store it. */
841
285k
        return;
842
285k
      }
843
289k
    }
844
3.75k
    ZEND_FALLTHROUGH;
845
6.80M
    default:
846
6.80M
      start++;
847
6.80M
      kind = ZEND_LIVE_TMPVAR;
848
849
      /* Check hook to determine whether a live range is necessary,
850
       * e.g. based on type info. */
851
6.80M
      if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
852
5.67k
        return;
853
5.67k
      }
854
6.79M
      break;
855
6.79M
    case ZEND_COPY_TMP:
856
340k
    {
857
      /* COPY_TMP has a split live-range: One from the definition until the use in
858
       * "null" branch, and another from the start of the "non-null" branch to the
859
       * FREE opcode. */
860
340k
      uint32_t rt_var_num = EX_NUM_TO_VAR(op_array->last_var + var_num);
861
340k
      if (needs_live_range && !needs_live_range(op_array, orig_def_opline)) {
862
168
        return;
863
168
      }
864
865
340k
      kind = ZEND_LIVE_TMPVAR;
866
340k
      if (use_opline->opcode != ZEND_FREE) {
867
        /* This can happen if one branch of the coalesce has been optimized away.
868
         * In this case we should emit a normal live-range instead. */
869
3.71k
        start++;
870
3.71k
        break;
871
3.71k
      }
872
873
336k
      zend_op *block_start_op = use_opline;
874
7.80M
      while ((block_start_op-1)->opcode == ZEND_FREE) {
875
7.46M
        block_start_op--;
876
7.46M
      }
877
878
336k
      start = block_start_op - op_array->opcodes;
879
336k
      if (start != end) {
880
206k
        emit_live_range_raw(op_array, var_num, kind, start, end);
881
206k
      }
882
883
16.1M
      do {
884
16.1M
        use_opline--;
885
886
        /* The use might have been optimized away, in which case we will hit the def
887
         * instead. */
888
16.1M
        if (use_opline->opcode == ZEND_COPY_TMP && use_opline->result.var == rt_var_num) {
889
0
          start = def_opline + 1 - op_array->opcodes;
890
0
          emit_live_range_raw(op_array, var_num, kind, start, end);
891
0
          return;
892
0
        }
893
16.1M
      } while (!(
894
16.1M
        ((use_opline->op1_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op1.var == rt_var_num) ||
895
16.0M
        ((use_opline->op2_type & (IS_TMP_VAR|IS_VAR)) && use_opline->op2.var == rt_var_num)
896
16.1M
      ));
897
898
336k
      start = def_opline + 1 - op_array->opcodes;
899
336k
      end = use_opline - op_array->opcodes;
900
336k
      emit_live_range_raw(op_array, var_num, kind, start, end);
901
336k
      return;
902
336k
    }
903
23.9M
  }
904
905
23.2M
  emit_live_range_raw(op_array, var_num, kind, start, end);
906
23.2M
}
907
908
47.5M
static bool is_fake_def(zend_op *opline) {
909
  /* These opcodes only modify the result, not create it. */
910
47.5M
  return opline->opcode == ZEND_ROPE_ADD
911
46.7M
    || opline->opcode == ZEND_ADD_ARRAY_ELEMENT
912
46.5M
    || opline->opcode == ZEND_ADD_ARRAY_UNPACK;
913
47.5M
}
914
915
38.2M
static bool keeps_op1_alive(zend_op *opline) {
916
  /* These opcodes don't consume their OP1 operand,
917
   * it is later freed by something else. */
918
38.2M
  if (opline->opcode == ZEND_CASE
919
38.2M
   || opline->opcode == ZEND_CASE_STRICT
920
38.2M
   || opline->opcode == ZEND_SWITCH_LONG
921
38.2M
   || opline->opcode == ZEND_SWITCH_STRING
922
38.2M
   || opline->opcode == ZEND_MATCH
923
38.2M
   || opline->opcode == ZEND_MATCH_ERROR
924
38.2M
   || opline->opcode == ZEND_FETCH_LIST_R
925
38.2M
   || opline->opcode == ZEND_FETCH_LIST_W
926
38.2M
   || opline->opcode == ZEND_COPY_TMP
927
38.2M
   || opline->opcode == ZEND_EXT_STMT) {
928
212
    return true;
929
212
  }
930
38.2M
  ZEND_ASSERT(opline->opcode != ZEND_FE_FETCH_R
931
38.2M
    && opline->opcode != ZEND_FE_FETCH_RW
932
38.2M
    && opline->opcode != ZEND_VERIFY_RETURN_TYPE
933
38.2M
    && opline->opcode != ZEND_BIND_LEXICAL
934
38.2M
    && opline->opcode != ZEND_ROPE_ADD);
935
38.2M
  return false;
936
38.2M
}
937
938
/* Live ranges must be sorted by increasing start opline */
939
12.3M
static int cmp_live_range(const zend_live_range *a, const zend_live_range *b) {
940
12.3M
  return a->start - b->start;
941
12.3M
}
942
15.1M
static void swap_live_range(zend_live_range *a, zend_live_range *b) {
943
15.1M
  uint32_t tmp;
944
15.1M
  tmp = a->var;
945
15.1M
  a->var = b->var;
946
15.1M
  b->var = tmp;
947
15.1M
  tmp = a->start;
948
15.1M
  a->start = b->start;
949
15.1M
  b->start = tmp;
950
15.1M
  tmp = a->end;
951
15.1M
  a->end = b->end;
952
15.1M
  b->end = tmp;
953
15.1M
}
954
955
static void zend_calc_live_ranges(
956
2.30M
    zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) {
957
2.30M
  uint32_t opnum = op_array->last;
958
2.30M
  zend_op *opline = &op_array->opcodes[opnum];
959
2.30M
  ALLOCA_FLAG(use_heap)
960
2.30M
  uint32_t var_offset = op_array->last_var;
961
2.30M
  uint32_t *last_use = do_alloca(sizeof(uint32_t) * op_array->T, use_heap);
962
2.30M
  memset(last_use, -1, sizeof(uint32_t) * op_array->T);
963
964
2.30M
  ZEND_ASSERT(!op_array->live_range);
965
93.5M
  while (opnum > 0) {
966
91.2M
    opnum--;
967
91.2M
    opline--;
968
969
    /* SEPARATE always redeclares its op1. For the purposes of live-ranges,
970
     * its declaration is irrelevant. Don't terminate the current live-range
971
     * to avoid breaking special handling of COPY_TMP. */
972
91.2M
    if (opline->opcode == ZEND_SEPARATE) {
973
125k
      ZEND_ASSERT(opline->op1.var == opline->result.var);
974
125k
      continue;
975
125k
    }
976
977
91.1M
    if ((opline->result_type & (IS_TMP_VAR|IS_VAR)) && !is_fake_def(opline)) {
978
46.5M
      uint32_t var_num = EX_VAR_TO_NUM(opline->result.var) - var_offset;
979
      /* Defs without uses can occur for two reasons: Either because the result is
980
       * genuinely unused (e.g. omitted FREE opcode for an unused boolean result), or
981
       * because there are multiple defining opcodes (e.g. JMPZ_EX and QM_ASSIGN), in
982
       * which case the last one starts the live range. As such, we can simply ignore
983
       * missing uses here. */
984
46.5M
      if (EXPECTED(last_use[var_num] != (uint32_t) -1)) {
985
        /* Skip trivial live-range */
986
43.6M
        if (opnum + 1 != last_use[var_num]) {
987
23.9M
          uint32_t num;
988
989
23.9M
#if 1
990
          /* OP_DATA uses only op1 operand */
991
23.9M
          ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
992
23.9M
          num = opnum;
993
#else
994
          /* OP_DATA is really part of the previous opcode. */
995
          num = opnum - (opline->opcode == ZEND_OP_DATA);
996
#endif
997
23.9M
          emit_live_range(op_array, var_num, num, last_use[var_num], needs_live_range);
998
23.9M
        }
999
43.6M
        last_use[var_num] = (uint32_t) -1;
1000
43.6M
      }
1001
46.5M
    }
1002
1003
91.1M
    if ((opline->op1_type & (IS_TMP_VAR|IS_VAR))) {
1004
39.7M
      uint32_t var_num = EX_VAR_TO_NUM(opline->op1.var) - var_offset;
1005
39.7M
      if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
1006
38.2M
        if (EXPECTED(!keeps_op1_alive(opline))) {
1007
          /* OP_DATA is really part of the previous opcode. */
1008
38.2M
          last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
1009
38.2M
        }
1010
38.2M
      } else if ((opline->opcode == ZEND_FREE || opline->opcode == ZEND_FE_FREE) && opline->extended_value & ZEND_FREE_ON_RETURN) {
1011
1.90k
        int jump_offset = 1;
1012
3.73k
        while (((opline + jump_offset)->opcode == ZEND_FREE || (opline + jump_offset)->opcode == ZEND_FE_FREE)
1013
1.83k
          && (opline + jump_offset)->extended_value & ZEND_FREE_ON_RETURN) {
1014
1.83k
          ++jump_offset;
1015
1.83k
        }
1016
        // loop var frees directly precede the jump (or return) operand, except that ZEND_VERIFY_RETURN_TYPE may happen first.
1017
1.90k
        if ((opline + jump_offset)->opcode == ZEND_VERIFY_RETURN_TYPE) {
1018
0
          ++jump_offset;
1019
0
        }
1020
        /* FREE with ZEND_FREE_ON_RETURN immediately followed by RETURN frees
1021
         * the loop variable on early return. We need to split the live range
1022
         * so GC doesn't access the freed variable after this FREE. */
1023
1.90k
        uint32_t opnum_last_use = last_use[var_num];
1024
1.90k
        zend_op *opline_last_use = op_array->opcodes + opnum_last_use;
1025
1.90k
        ZEND_ASSERT(opline_last_use->opcode == opline->opcode); // any ZEND_FREE_ON_RETURN must be followed by a FREE without
1026
1.90k
        if (opnum + jump_offset + 1 != opnum_last_use) {
1027
1.54k
          emit_live_range_raw(op_array, var_num, opline->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
1028
1.54k
              opnum + jump_offset + 1, opnum_last_use);
1029
1.54k
        }
1030
1031
        /* Update last_use so next range includes this FREE */
1032
1.90k
        last_use[var_num] = opnum;
1033
1034
        /* Store opline offset to loop end */
1035
1.90k
        opline->op2.opline_num = opnum_last_use - opnum;
1036
1.90k
        if (opline_last_use->extended_value & ZEND_FREE_ON_RETURN) {
1037
1.17k
          opline->op2.opline_num += opline_last_use->op2.opline_num;
1038
1.17k
        }
1039
1.90k
      }
1040
39.7M
    }
1041
91.1M
    if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
1042
5.61M
      uint32_t var_num = EX_VAR_TO_NUM(opline->op2.var) - var_offset;
1043
5.61M
      if (UNEXPECTED(opline->opcode == ZEND_FE_FETCH_R
1044
5.61M
          || opline->opcode == ZEND_FE_FETCH_RW)) {
1045
        /* OP2 of FE_FETCH is actually a def, not a use. */
1046
1.24k
        if (last_use[var_num] != (uint32_t) -1) {
1047
1.24k
          if (opnum + 1 != last_use[var_num]) {
1048
894
            emit_live_range(
1049
894
              op_array, var_num, opnum, last_use[var_num], needs_live_range);
1050
894
          }
1051
1.24k
          last_use[var_num] = (uint32_t) -1;
1052
1.24k
        }
1053
5.61M
      } else if (EXPECTED(last_use[var_num] == (uint32_t) -1)) {
1054
5.40M
#if 1
1055
        /* OP_DATA uses only op1 operand */
1056
5.40M
        ZEND_ASSERT(opline->opcode != ZEND_OP_DATA);
1057
5.40M
        last_use[var_num] = opnum;
1058
#else
1059
        /* OP_DATA is really part of the previous opcode. */
1060
        last_use[var_num] = opnum - (opline->opcode == ZEND_OP_DATA);
1061
#endif
1062
5.40M
      }
1063
5.61M
    }
1064
91.1M
  }
1065
1066
2.30M
  if (op_array->last_live_range > 1) {
1067
1.06M
    zend_live_range *r1 = op_array->live_range;
1068
1.06M
    zend_live_range *r2 = r1 + op_array->last_live_range - 1;
1069
1070
    /* In most cases we need just revert the array */
1071
12.7M
    while (r1 < r2) {
1072
11.6M
      swap_live_range(r1, r2);
1073
11.6M
      r1++;
1074
11.6M
      r2--;
1075
11.6M
    }
1076
1077
1.06M
    r1 = op_array->live_range;
1078
1.06M
    r2 = r1 + op_array->last_live_range - 1;
1079
22.1M
    while (r1 < r2) {
1080
21.1M
      if (r1->start > (r1+1)->start) {
1081
49.4k
        zend_sort(r1, r2 - r1 + 1, sizeof(zend_live_range),
1082
49.4k
          (compare_func_t) cmp_live_range, (swap_func_t) swap_live_range);
1083
49.4k
        break;
1084
49.4k
      }
1085
21.1M
      r1++;
1086
21.1M
    }
1087
1.06M
  }
1088
1089
2.30M
  free_alloca(last_use, use_heap);
1090
2.30M
}
1091
1092
ZEND_API void zend_recalc_live_ranges(
1093
60.7k
    zend_op_array *op_array, zend_needs_live_range_cb needs_live_range) {
1094
  /* We assume that we never create live-ranges where there were none before. */
1095
60.7k
  ZEND_ASSERT(op_array->live_range);
1096
60.7k
  efree(op_array->live_range);
1097
60.7k
  op_array->live_range = NULL;
1098
60.7k
  op_array->last_live_range = 0;
1099
60.7k
  zend_calc_live_ranges(op_array, needs_live_range);
1100
60.7k
}
1101
1102
ZEND_API void pass_two(zend_op_array *op_array)
1103
2.24M
{
1104
2.24M
  zend_op *opline, *end;
1105
1106
2.24M
  if (!ZEND_USER_CODE(op_array->type)) {
1107
0
    return;
1108
0
  }
1109
2.24M
  if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) {
1110
0
    zend_update_extended_stmts(op_array);
1111
0
  }
1112
2.24M
  if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
1113
2.24M
    if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER) {
1114
0
      zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
1115
0
    }
1116
2.24M
  }
1117
1118
2.24M
  if (CG(context).vars_size != op_array->last_var) {
1119
1.76M
    op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
1120
1.76M
    CG(context).vars_size = op_array->last_var;
1121
1.76M
  }
1122
1123
#if ZEND_USE_ABS_CONST_ADDR
1124
  if (CG(context).opcodes_size != op_array->last) {
1125
    op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
1126
    CG(context).opcodes_size = op_array->last;
1127
  }
1128
  if (CG(context).literals_size != op_array->last_literal) {
1129
    op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
1130
    CG(context).literals_size = op_array->last_literal;
1131
  }
1132
#else
1133
2.24M
  op_array->opcodes = (zend_op *) erealloc(op_array->opcodes,
1134
2.24M
    ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16) +
1135
2.24M
    sizeof(zval) * op_array->last_literal);
1136
2.24M
  if (op_array->literals) {
1137
2.24M
    memcpy(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16),
1138
2.24M
      op_array->literals, sizeof(zval) * op_array->last_literal);
1139
2.24M
    efree(op_array->literals);
1140
2.24M
    op_array->literals = (zval*)(((char*)op_array->opcodes) + ZEND_MM_ALIGNED_SIZE_EX(sizeof(zend_op) * op_array->last, 16));
1141
2.24M
  }
1142
2.24M
  CG(context).opcodes_size = op_array->last;
1143
2.24M
  CG(context).literals_size = op_array->last_literal;
1144
2.24M
#endif
1145
1146
2.24M
    op_array->T += ZEND_OBSERVER_ENABLED; // reserve last temporary for observers if enabled
1147
1148
  /* Needs to be set directly after the opcode/literal reallocation, to ensure destruction
1149
   * happens correctly if any of the following fixups generate a fatal error. */
1150
2.24M
  op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
1151
1152
2.24M
  opline = op_array->opcodes;
1153
2.24M
  end = opline + op_array->last;
1154
91.2M
  while (opline < end) {
1155
88.9M
    switch (opline->opcode) {
1156
6.81k
      case ZEND_RECV_INIT:
1157
6.81k
        {
1158
6.81k
          zval *val = CT_CONSTANT(opline->op2);
1159
6.81k
          if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
1160
2.55k
            uint32_t slot = ZEND_MM_ALIGNED_SIZE_EX(op_array->cache_size, 8);
1161
2.55k
            Z_CACHE_SLOT_P(val) = slot;
1162
2.55k
            op_array->cache_size += sizeof(zval);
1163
2.55k
          }
1164
6.81k
        }
1165
6.81k
        break;
1166
4.79k
      case ZEND_FAST_CALL:
1167
4.79k
        opline->op1.opline_num = op_array->try_catch_array[opline->op1.num].finally_op;
1168
4.79k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1169
4.79k
        break;
1170
1.09k
      case ZEND_BRK:
1171
2.29k
      case ZEND_CONT:
1172
2.29k
        {
1173
2.29k
          uint32_t jmp_target = zend_get_brk_cont_target(opline);
1174
1175
2.29k
          if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1176
397
            zend_check_finally_breakout(op_array, opline - op_array->opcodes, jmp_target);
1177
397
          }
1178
2.29k
          opline->opcode = ZEND_JMP;
1179
2.29k
          opline->op1.opline_num = jmp_target;
1180
2.29k
          opline->op2.num = 0;
1181
2.29k
          ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1182
2.29k
        }
1183
2.29k
        break;
1184
1.52k
      case ZEND_GOTO:
1185
1.52k
        zend_resolve_goto_label(op_array, opline);
1186
1.52k
        if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
1187
1.01k
          zend_check_finally_breakout(op_array, opline - op_array->opcodes, opline->op1.opline_num);
1188
1.01k
        }
1189
1.52k
        ZEND_FALLTHROUGH;
1190
968k
      case ZEND_JMP:
1191
968k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
1192
968k
        break;
1193
74.3k
      case ZEND_JMPZ:
1194
107k
      case ZEND_JMPNZ:
1195
112k
      case ZEND_JMPZ_EX:
1196
118k
      case ZEND_JMPNZ_EX:
1197
123k
      case ZEND_JMP_SET:
1198
2.11M
      case ZEND_COALESCE:
1199
2.13M
      case ZEND_FE_RESET_R:
1200
2.13M
      case ZEND_FE_RESET_RW:
1201
2.22M
      case ZEND_JMP_NULL:
1202
2.22M
      case ZEND_BIND_INIT_STATIC_OR_JMP:
1203
2.94M
      case ZEND_JMP_FRAMELESS:
1204
2.94M
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1205
2.94M
        break;
1206
40.7k
      case ZEND_ASSERT_CHECK:
1207
40.7k
      {
1208
        /* If result of assert is unused, result of check is unused as well */
1209
40.7k
        zend_op *call = &op_array->opcodes[opline->op2.opline_num - 1];
1210
40.7k
        if (call->opcode == ZEND_EXT_FCALL_END) {
1211
0
          call--;
1212
0
        }
1213
40.7k
        if (call->result_type == IS_UNUSED) {
1214
30.4k
          opline->result_type = IS_UNUSED;
1215
30.4k
        }
1216
40.7k
        ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1217
40.7k
        break;
1218
2.22M
      }
1219
17.1k
      case ZEND_FE_FETCH_R:
1220
18.8k
      case ZEND_FE_FETCH_RW:
1221
        /* absolute index to relative offset */
1222
18.8k
        opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
1223
18.8k
        break;
1224
42.2k
      case ZEND_CATCH:
1225
42.2k
        if (!(opline->extended_value & ZEND_LAST_CATCH)) {
1226
9.52k
          ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
1227
9.52k
        }
1228
42.2k
        break;
1229
2.16M
      case ZEND_RETURN:
1230
2.66M
      case ZEND_RETURN_BY_REF:
1231
2.66M
        if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
1232
648k
          opline->opcode = ZEND_GENERATOR_RETURN;
1233
648k
        }
1234
2.66M
        break;
1235
38
      case ZEND_SWITCH_LONG:
1236
1.23k
      case ZEND_SWITCH_STRING:
1237
2.26k
      case ZEND_MATCH:
1238
2.26k
      {
1239
        /* absolute indexes to relative offsets */
1240
2.26k
        HashTable *jumptable = Z_ARRVAL_P(CT_CONSTANT(opline->op2));
1241
2.26k
        zval *zv;
1242
15.7k
        ZEND_HASH_FOREACH_VAL(jumptable, zv) {
1243
15.7k
          Z_LVAL_P(zv) = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, Z_LVAL_P(zv));
1244
15.7k
        } ZEND_HASH_FOREACH_END();
1245
1246
2.26k
        opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
1247
2.26k
        break;
1248
1.23k
      }
1249
88.9M
    }
1250
88.9M
    if (opline->op1_type == IS_CONST) {
1251
7.18M
      ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
1252
81.7M
    } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
1253
39.0M
      opline->op1.var = EX_NUM_TO_VAR(op_array->last_var + opline->op1.var);
1254
39.0M
    }
1255
88.9M
    if (opline->op2_type == IS_CONST) {
1256
16.8M
      ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
1257
72.1M
    } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
1258
5.30M
      opline->op2.var = EX_NUM_TO_VAR(op_array->last_var + opline->op2.var);
1259
5.30M
    }
1260
88.9M
    if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
1261
46.4M
      opline->result.var = EX_NUM_TO_VAR(op_array->last_var + opline->result.var);
1262
46.4M
    }
1263
88.9M
    ZEND_VM_SET_OPCODE_HANDLER(opline);
1264
88.9M
    opline++;
1265
88.9M
  }
1266
1267
2.24M
  zend_calc_live_ranges(op_array, NULL);
1268
1269
2.24M
  return;
1270
2.24M
}
1271
1272
ZEND_API unary_op_type get_unary_op(int opcode)
1273
276k
{
1274
276k
  switch (opcode) {
1275
6.76k
    case ZEND_BW_NOT:
1276
6.76k
      return (unary_op_type) bitwise_not_function;
1277
268k
    case ZEND_BOOL_NOT:
1278
268k
      return (unary_op_type) boolean_not_function;
1279
1.91k
    default:
1280
1.91k
      return (unary_op_type) NULL;
1281
276k
  }
1282
276k
}
1283
1284
ZEND_API binary_op_type get_binary_op(int opcode)
1285
802k
{
1286
802k
  switch (opcode) {
1287
108k
    case ZEND_ADD:
1288
108k
      return (binary_op_type) add_function;
1289
13.6k
    case ZEND_SUB:
1290
13.6k
      return (binary_op_type) sub_function;
1291
43.7k
    case ZEND_MUL:
1292
43.7k
      return (binary_op_type) mul_function;
1293
4.13k
    case ZEND_POW:
1294
4.13k
      return (binary_op_type) pow_function;
1295
9.63k
    case ZEND_DIV:
1296
9.63k
      return (binary_op_type) div_function;
1297
3.30k
    case ZEND_MOD:
1298
3.30k
      return (binary_op_type) mod_function;
1299
1.42k
    case ZEND_SL:
1300
1.42k
      return (binary_op_type) shift_left_function;
1301
4.82k
    case ZEND_SR:
1302
4.82k
      return (binary_op_type) shift_right_function;
1303
163
    case ZEND_FAST_CONCAT:
1304
9.63k
    case ZEND_CONCAT:
1305
9.63k
      return (binary_op_type) concat_function;
1306
55.6k
    case ZEND_IS_IDENTICAL:
1307
55.7k
    case ZEND_CASE_STRICT:
1308
55.7k
      return (binary_op_type) is_identical_function;
1309
96.9k
    case ZEND_IS_NOT_IDENTICAL:
1310
96.9k
      return (binary_op_type) is_not_identical_function;
1311
6.89k
    case ZEND_IS_EQUAL:
1312
6.90k
    case ZEND_CASE:
1313
6.90k
      return (binary_op_type) is_equal_function;
1314
2.27k
    case ZEND_IS_NOT_EQUAL:
1315
2.27k
      return (binary_op_type) is_not_equal_function;
1316
328k
    case ZEND_IS_SMALLER:
1317
328k
      return (binary_op_type) is_smaller_function;
1318
82.7k
    case ZEND_IS_SMALLER_OR_EQUAL:
1319
82.7k
      return (binary_op_type) is_smaller_or_equal_function;
1320
119
    case ZEND_SPACESHIP:
1321
119
      return (binary_op_type) compare_function;
1322
16.1k
    case ZEND_BW_OR:
1323
16.1k
      return (binary_op_type) bitwise_or_function;
1324
7.25k
    case ZEND_BW_AND:
1325
7.25k
      return (binary_op_type) bitwise_and_function;
1326
4.29k
    case ZEND_BW_XOR:
1327
4.29k
      return (binary_op_type) bitwise_xor_function;
1328
3.15k
    case ZEND_BOOL_XOR:
1329
3.15k
      return (binary_op_type) boolean_xor_function;
1330
0
    default:
1331
0
      ZEND_UNREACHABLE();
1332
0
      return (binary_op_type) NULL;
1333
802k
  }
1334
802k
}