Coverage Report

Created: 2025-12-31 07:28

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