Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/Optimizer/escape_analysis.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend OPcache, Escape Analysis                                        |
4
   +----------------------------------------------------------------------+
5
   | Copyright © The PHP Group and Contributors.                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to the Modified BSD License that is      |
8
   | bundled with this package in the file LICENSE, and is available      |
9
   | through the World Wide Web at <https://www.php.net/license/>.        |
10
   |                                                                      |
11
   | SPDX-License-Identifier: BSD-3-Clause                                |
12
   +----------------------------------------------------------------------+
13
   | Authors: Dmitry Stogov <dmitry@php.net>                              |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "Optimizer/zend_optimizer.h"
18
#include "Optimizer/zend_optimizer_internal.h"
19
#include "zend_bitset.h"
20
#include "zend_cfg.h"
21
#include "zend_ssa.h"
22
#include "zend_inference.h"
23
#include "zend_dump.h"
24
25
/*
26
 * T. Kotzmann and H. Mossenbock. Escape analysis  in the context of dynamic
27
 * compilation and deoptimization. In Proceedings of the International
28
 * Conference on Virtual Execution Environments, pages 111-120, Chicago,
29
 * June 2005
30
 */
31
32
static zend_always_inline void union_find_init(int *parent, int *size, int count) /* {{{ */
33
0
{
34
0
  int i;
35
36
0
  for (i = 0; i < count; i++) {
37
0
    parent[i] = i;
38
0
    size[i] = 1;
39
0
  }
40
0
}
41
/* }}} */
42
43
static zend_always_inline int union_find_root(int *parent, int i) /* {{{ */
44
0
{
45
0
  int p = parent[i];
46
47
0
  while (i != p) {
48
0
    p = parent[p];
49
0
    parent[i] = p;
50
0
    i = p;
51
0
    p = parent[i];
52
0
  }
53
0
  return i;
54
0
}
55
/* }}} */
56
57
static zend_always_inline void union_find_unite(int *parent, int *size, int i, int j) /* {{{ */
58
0
{
59
0
  int r1 = union_find_root(parent, i);
60
0
  int r2 = union_find_root(parent, j);
61
62
0
  if (r1 != r2) {
63
0
    if (size[r1] < size[r2]) {
64
0
      parent[r1] = r2;
65
0
      size[r2] += size[r1];
66
0
    } else {
67
0
      parent[r2] = r1;
68
0
      size[r1] += size[r2];
69
0
    }
70
0
  }
71
0
}
72
/* }}} */
73
74
static zend_result zend_build_equi_escape_sets(int *parent, zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
75
0
{
76
0
  zend_ssa_var *ssa_vars = ssa->vars;
77
0
  int ssa_vars_count = ssa->vars_count;
78
0
  zend_ssa_phi *p;
79
0
  int i;
80
0
  int *size;
81
0
  ALLOCA_FLAG(use_heap)
82
83
0
  size = do_alloca(sizeof(int) * ssa_vars_count, use_heap);
84
0
  if (!size) {
85
0
    return FAILURE;
86
0
  }
87
0
  union_find_init(parent, size, ssa_vars_count);
88
89
0
  for (i = 0; i < ssa_vars_count; i++) {
90
0
    if (ssa_vars[i].definition_phi) {
91
0
      p = ssa_vars[i].definition_phi;
92
0
      if (p->pi >= 0) {
93
0
        union_find_unite(parent, size, i, p->sources[0]);
94
0
      } else {
95
0
        for (uint32_t j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
96
0
          union_find_unite(parent, size, i, p->sources[j]);
97
0
        }
98
0
      }
99
0
    } else if (ssa_vars[i].definition >= 0) {
100
0
      int def = ssa_vars[i].definition;
101
0
      zend_ssa_op *op = ssa->ops + def;
102
0
      zend_op *opline =  op_array->opcodes + def;
103
104
0
      if (op->op1_def >= 0) {
105
0
        if (op->op1_use >= 0) {
106
0
          if (opline->opcode != ZEND_ASSIGN) {
107
0
            union_find_unite(parent, size, op->op1_def, op->op1_use);
108
0
          }
109
0
        }
110
0
        if (opline->opcode == ZEND_ASSIGN && op->op2_use >= 0) {
111
0
          union_find_unite(parent, size, op->op1_def, op->op2_use);
112
0
        }
113
0
      }
114
0
      if (op->op2_def >= 0) {
115
0
        if (op->op2_use >= 0) {
116
0
          union_find_unite(parent, size, op->op2_def, op->op2_use);
117
0
        }
118
0
      }
119
0
      if (op->result_def >= 0) {
120
0
        if (op->result_use >= 0) {
121
0
          if (opline->opcode != ZEND_QM_ASSIGN) {
122
0
            union_find_unite(parent, size, op->result_def, op->result_use);
123
0
          }
124
0
        }
125
0
        if (opline->opcode == ZEND_QM_ASSIGN && op->op1_use >= 0) {
126
0
          union_find_unite(parent, size, op->result_def, op->op1_use);
127
0
        }
128
0
        if (opline->opcode == ZEND_ASSIGN && op->op2_use >= 0) {
129
0
          union_find_unite(parent, size, op->result_def, op->op2_use);
130
0
        }
131
0
        if (opline->opcode == ZEND_ASSIGN && op->op1_def >= 0) {
132
0
          union_find_unite(parent, size, op->result_def, op->op1_def);
133
0
        }
134
0
      }
135
0
    }
136
0
  }
137
138
0
  for (i = 0; i < ssa_vars_count; i++) {
139
0
    parent[i] = union_find_root(parent, i);
140
0
  }
141
142
0
  free_alloca(size, use_heap);
143
144
0
  return SUCCESS;
145
0
}
146
/* }}} */
147
148
static bool is_allocation_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
149
0
{
150
0
  zend_ssa_op *ssa_op = ssa->ops + def;
151
0
  zend_op *opline = op_array->opcodes + def;
152
153
0
  if (ssa_op->result_def == var) {
154
0
    switch (opline->opcode) {
155
0
      case ZEND_INIT_ARRAY:
156
0
        return true;
157
0
      case ZEND_NEW: {
158
          /* objects with destructors should escape */
159
0
        zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
160
0
          script, op_array, opline);
161
0
        uint32_t forbidden_flags =
162
          /* These flags will always cause an exception */
163
0
          ZEND_ACC_IMPLICIT_ABSTRACT_CLASS | ZEND_ACC_EXPLICIT_ABSTRACT_CLASS
164
0
          | ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT;
165
0
        if (ce
166
0
         && !ce->parent
167
0
         && !ce->create_object
168
0
         && ce->default_object_handlers->get_constructor == zend_std_get_constructor
169
0
         && ce->default_object_handlers->dtor_obj == zend_objects_destroy_object
170
0
         && !ce->constructor
171
0
         && !ce->destructor
172
0
         && !ce->__get
173
0
         && !ce->__set
174
0
         && !(ce->ce_flags & forbidden_flags)
175
0
         && (ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
176
0
          return true;
177
0
        }
178
0
        break;
179
0
      }
180
0
      case ZEND_QM_ASSIGN:
181
0
        if (opline->op1_type == IS_CONST
182
0
         && Z_TYPE_P(CRT_CONSTANT(opline->op1)) == IS_ARRAY) {
183
0
          return true;
184
0
        }
185
0
        if (opline->op1_type == IS_CV && (OP1_INFO() & MAY_BE_ARRAY)) {
186
0
          return true;
187
0
        }
188
0
        break;
189
0
      case ZEND_ASSIGN:
190
0
        if (opline->op1_type == IS_CV && (OP1_INFO() & MAY_BE_ARRAY)) {
191
0
          return true;
192
0
        }
193
0
        break;
194
0
    }
195
0
  } else if (ssa_op->op1_def == var) {
196
0
    switch (opline->opcode) {
197
0
      case ZEND_ASSIGN:
198
0
        if (opline->op2_type == IS_CONST
199
0
         && Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_ARRAY) {
200
0
          return true;
201
0
        }
202
0
        if (opline->op2_type == IS_CV && (OP2_INFO() & MAY_BE_ARRAY)) {
203
0
          return true;
204
0
        }
205
0
        break;
206
0
      case ZEND_ASSIGN_DIM:
207
0
        if (OP1_INFO() & (MAY_BE_UNDEF | MAY_BE_NULL | MAY_BE_FALSE)) {
208
          /* implicit object/array allocation */
209
0
          return true;
210
0
        }
211
0
        break;
212
0
    }
213
0
  }
214
215
0
  return false;
216
0
}
217
/* }}} */
218
219
static bool is_local_def(zend_op_array *op_array, zend_ssa *ssa, int def, int var, const zend_script *script) /* {{{ */
220
0
{
221
0
  zend_ssa_op *op = ssa->ops + def;
222
0
  zend_op *opline = op_array->opcodes + def;
223
224
0
  if (op->result_def == var) {
225
0
    switch (opline->opcode) {
226
0
      case ZEND_INIT_ARRAY:
227
0
      case ZEND_ADD_ARRAY_ELEMENT:
228
0
      case ZEND_QM_ASSIGN:
229
0
      case ZEND_ASSIGN:
230
0
        return true;
231
0
      case ZEND_NEW: {
232
        /* objects with destructors should escape */
233
0
        zend_class_entry *ce = zend_optimizer_get_class_entry_from_op1(
234
0
          script, op_array, opline);
235
0
        if (ce
236
0
         && !ce->create_object
237
0
         && ce->default_object_handlers->get_constructor == zend_std_get_constructor
238
0
         && ce->default_object_handlers->dtor_obj == zend_objects_destroy_object
239
0
         && !ce->constructor
240
0
         && !ce->destructor
241
0
         && !ce->__get
242
0
         && !ce->__set
243
0
         && !ce->parent) {
244
0
          return true;
245
0
        }
246
0
        break;
247
0
      }
248
0
    }
249
0
  } else if (op->op1_def == var) {
250
0
    switch (opline->opcode) {
251
0
      case ZEND_ASSIGN:
252
0
      case ZEND_ASSIGN_DIM:
253
0
      case ZEND_ASSIGN_OBJ:
254
0
      case ZEND_ASSIGN_OBJ_REF:
255
0
      case ZEND_ASSIGN_DIM_OP:
256
0
      case ZEND_ASSIGN_OBJ_OP:
257
0
      case ZEND_PRE_INC_OBJ:
258
0
      case ZEND_PRE_DEC_OBJ:
259
0
      case ZEND_POST_INC_OBJ:
260
0
      case ZEND_POST_DEC_OBJ:
261
0
        return true;
262
0
    }
263
0
  }
264
265
0
  return false;
266
0
}
267
/* }}} */
268
269
static bool is_escape_use(zend_op_array *op_array, zend_ssa *ssa, int use, int var) /* {{{ */
270
0
{
271
0
  zend_ssa_op *ssa_op = ssa->ops + use;
272
0
  zend_op *opline = op_array->opcodes + use;
273
274
0
  if (ssa_op->op1_use == var) {
275
0
    switch (opline->opcode) {
276
0
      case ZEND_ASSIGN:
277
        /* no_val */
278
0
        break;
279
0
      case ZEND_QM_ASSIGN:
280
0
        if (opline->op1_type == IS_CV) {
281
0
          if (OP1_INFO() & MAY_BE_OBJECT) {
282
            /* object aliasing */
283
0
            return true;
284
0
          }
285
0
        }
286
0
        break;
287
0
      case ZEND_ISSET_ISEMPTY_DIM_OBJ:
288
0
      case ZEND_ISSET_ISEMPTY_PROP_OBJ:
289
0
      case ZEND_FETCH_DIM_R:
290
0
      case ZEND_FETCH_OBJ_R:
291
0
      case ZEND_FETCH_DIM_IS:
292
0
      case ZEND_FETCH_OBJ_IS:
293
0
        break;
294
0
      case ZEND_ASSIGN_OP:
295
0
        return true;
296
0
      case ZEND_ASSIGN_DIM_OP:
297
0
      case ZEND_ASSIGN_OBJ_OP:
298
0
      case ZEND_ASSIGN_STATIC_PROP_OP:
299
0
      case ZEND_ASSIGN_DIM:
300
0
      case ZEND_ASSIGN_OBJ:
301
0
      case ZEND_ASSIGN_OBJ_REF:
302
0
        break;
303
0
      case ZEND_PRE_INC_OBJ:
304
0
      case ZEND_PRE_DEC_OBJ:
305
0
      case ZEND_POST_INC_OBJ:
306
0
      case ZEND_POST_DEC_OBJ:
307
0
        break;
308
0
      case ZEND_INIT_ARRAY:
309
0
      case ZEND_ADD_ARRAY_ELEMENT:
310
0
        if (opline->extended_value & ZEND_ARRAY_ELEMENT_REF) {
311
0
          return true;
312
0
        }
313
0
        if (OP1_INFO() & MAY_BE_OBJECT) {
314
          /* object aliasing */
315
0
          return true;
316
0
        }
317
        /* reference dependencies processed separately */
318
0
        break;
319
0
      case ZEND_OP_DATA:
320
0
        if ((opline-1)->opcode != ZEND_ASSIGN_DIM
321
0
         && (opline-1)->opcode != ZEND_ASSIGN_OBJ) {
322
0
          return true;
323
0
        }
324
0
        if (OP1_INFO() & MAY_BE_OBJECT) {
325
          /* object aliasing */
326
0
          return true;
327
0
        }
328
0
        opline--;
329
0
        ssa_op--;
330
0
        if (opline->op1_type != IS_CV
331
0
         || (OP1_INFO() & MAY_BE_REF)
332
0
         || (ssa_op->op1_def >= 0 && ssa->vars[ssa_op->op1_def].alias)) {
333
          /* assignment into escaping structure */
334
0
          return true;
335
0
        }
336
        /* reference dependencies processed separately */
337
0
        break;
338
0
      default:
339
0
        return true;
340
0
    }
341
0
  }
342
343
0
  if (ssa_op->op2_use == var) {
344
0
    switch (opline->opcode) {
345
0
      case ZEND_ASSIGN:
346
0
        if (opline->op1_type != IS_CV
347
0
         || (OP1_INFO() & MAY_BE_REF)
348
0
         || (ssa_op->op1_def >= 0 && ssa->vars[ssa_op->op1_def].alias)) {
349
          /* assignment into escaping variable */
350
0
          return true;
351
0
        }
352
0
        if (opline->op2_type == IS_CV || opline->result_type != IS_UNUSED) {
353
0
          if (OP2_INFO() & MAY_BE_OBJECT) {
354
            /* object aliasing */
355
0
            return true;
356
0
          }
357
0
        }
358
0
        break;
359
0
      default:
360
0
        return true;
361
0
    }
362
0
  }
363
364
0
  if (ssa_op->result_use == var) {
365
0
    switch (opline->opcode) {
366
0
      case ZEND_ASSIGN:
367
0
      case ZEND_QM_ASSIGN:
368
0
      case ZEND_INIT_ARRAY:
369
0
      case ZEND_ADD_ARRAY_ELEMENT:
370
0
        break;
371
0
      default:
372
0
        return true;
373
0
    }
374
0
  }
375
376
0
  return false;
377
0
}
378
/* }}} */
379
380
zend_result zend_ssa_escape_analysis(const zend_script *script, zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
381
0
{
382
0
  zend_ssa_var *ssa_vars = ssa->vars;
383
0
  int ssa_vars_count = ssa->vars_count;
384
0
  int i, root, use;
385
0
  int *ees;
386
0
  bool has_allocations;
387
0
  int num_non_escaped;
388
0
  ALLOCA_FLAG(use_heap)
389
390
0
  if (!ssa_vars) {
391
0
    return SUCCESS;
392
0
  }
393
394
0
  has_allocations = false;
395
0
  for (i = op_array->last_var; i < ssa_vars_count; i++) {
396
0
    if (ssa_vars[i].definition >= 0
397
0
      && (ssa->var_info[i].type & (MAY_BE_ARRAY|MAY_BE_OBJECT))
398
0
      && is_allocation_def(op_array, ssa, ssa_vars[i].definition, i, script)) {
399
0
      has_allocations = true;
400
0
      break;
401
0
    }
402
0
  }
403
0
  if (!has_allocations) {
404
0
    return SUCCESS;
405
0
  }
406
407
408
  /* 1. Build EES (Equi-Escape Sets) */
409
0
  ees = do_alloca(sizeof(int) * ssa_vars_count, use_heap);
410
0
  if (!ees) {
411
0
    return FAILURE;
412
0
  }
413
414
0
  if (zend_build_equi_escape_sets(ees, op_array, ssa) == FAILURE) {
415
0
    free_alloca(ees, use_heap);
416
0
    return FAILURE;
417
0
  }
418
419
  /* 2. Identify Allocations */
420
0
  num_non_escaped = 0;
421
0
  for (i = op_array->last_var; i < ssa_vars_count; i++) {
422
0
    root = ees[i];
423
0
    if (ssa_vars[root].escape_state > ESCAPE_STATE_NO_ESCAPE) {
424
      /* already escape. skip */
425
0
    } else if (ssa_vars[i].alias && (ssa->var_info[i].type & MAY_BE_REF)) {
426
0
      if (ssa_vars[root].escape_state == ESCAPE_STATE_NO_ESCAPE) {
427
0
        num_non_escaped--;
428
0
      }
429
0
      ssa_vars[root].escape_state = ESCAPE_STATE_GLOBAL_ESCAPE;
430
0
    } else if (ssa_vars[i].definition >= 0
431
0
       && (ssa->var_info[i].type & (MAY_BE_ARRAY|MAY_BE_OBJECT))) {
432
0
      if (!is_local_def(op_array, ssa, ssa_vars[i].definition, i, script)) {
433
0
        if (ssa_vars[root].escape_state == ESCAPE_STATE_NO_ESCAPE) {
434
0
          num_non_escaped--;
435
0
        }
436
0
        ssa_vars[root].escape_state = ESCAPE_STATE_GLOBAL_ESCAPE;
437
0
      } else if (ssa_vars[root].escape_state == ESCAPE_STATE_UNKNOWN
438
0
       && is_allocation_def(op_array, ssa, ssa_vars[i].definition, i, script)) {
439
0
        ssa_vars[root].escape_state = ESCAPE_STATE_NO_ESCAPE;
440
0
        num_non_escaped++;
441
0
      }
442
0
    }
443
0
  }
444
445
  /* 3. Mark escaped EES */
446
0
  if (num_non_escaped) {
447
0
    for (i = 0; i < ssa_vars_count; i++) {
448
0
      if (ssa_vars[i].use_chain >= 0) {
449
0
        root = ees[i];
450
0
        if (ssa_vars[root].escape_state == ESCAPE_STATE_NO_ESCAPE) {
451
0
          FOREACH_USE(ssa_vars + i, use) {
452
0
            if (is_escape_use(op_array, ssa, use, i)) {
453
0
              ssa_vars[root].escape_state = ESCAPE_STATE_GLOBAL_ESCAPE;
454
0
              num_non_escaped--;
455
0
              if (num_non_escaped == 0) {
456
0
                i = ssa_vars_count;
457
0
              }
458
0
              break;
459
0
            }
460
0
          } FOREACH_USE_END();
461
0
        }
462
0
      }
463
0
    }
464
0
  }
465
466
  /* 4. Process referential dependencies */
467
0
  if (num_non_escaped) {
468
0
    bool changed;
469
470
0
    do {
471
0
      changed = false;
472
0
      for (i = 0; i < ssa_vars_count; i++) {
473
0
        if (ssa_vars[i].use_chain >= 0) {
474
0
          root = ees[i];
475
0
          if (ssa_vars[root].escape_state == ESCAPE_STATE_NO_ESCAPE) {
476
0
            FOREACH_USE(ssa_vars + i, use) {
477
0
              zend_ssa_op *op = ssa->ops + use;
478
0
              zend_op *opline = op_array->opcodes + use;
479
0
              int enclosing_root;
480
481
0
              if (opline->opcode == ZEND_OP_DATA &&
482
0
                  ((opline-1)->opcode == ZEND_ASSIGN_DIM ||
483
0
                   (opline-1)->opcode == ZEND_ASSIGN_OBJ ||
484
0
                   (opline-1)->opcode == ZEND_ASSIGN_OBJ_REF) &&
485
0
                  op->op1_use == i &&
486
0
                  (op-1)->op1_use >= 0) {
487
0
                enclosing_root = ees[(op-1)->op1_use];
488
0
              } else if ((opline->opcode == ZEND_INIT_ARRAY ||
489
0
                   opline->opcode == ZEND_ADD_ARRAY_ELEMENT) &&
490
0
                  op->op1_use == i &&
491
0
                  op->result_def >= 0) {
492
0
                enclosing_root = ees[op->result_def];
493
0
              } else {
494
0
                continue;
495
0
              }
496
497
0
              if (ssa_vars[enclosing_root].escape_state == ESCAPE_STATE_UNKNOWN ||
498
0
                  ssa_vars[enclosing_root].escape_state > ssa_vars[root].escape_state) {
499
0
                  if (ssa_vars[enclosing_root].escape_state == ESCAPE_STATE_UNKNOWN) {
500
0
                  ssa_vars[root].escape_state = ESCAPE_STATE_GLOBAL_ESCAPE;
501
0
                  } else {
502
0
                  ssa_vars[root].escape_state = ssa_vars[enclosing_root].escape_state;
503
0
                }
504
0
                if (ssa_vars[root].escape_state == ESCAPE_STATE_GLOBAL_ESCAPE) {
505
0
                  num_non_escaped--;
506
0
                  if (num_non_escaped == 0) {
507
0
                    changed = false;
508
0
                  } else {
509
0
                    changed = true;
510
0
                  }
511
0
                  break;
512
0
                } else {
513
0
                  changed = true;
514
0
                }
515
0
              }
516
0
            } FOREACH_USE_END();
517
0
          }
518
0
        }
519
0
      }
520
0
    } while (changed);
521
0
  }
522
523
  /* 5. Propagate values of escape sets to variables */
524
0
  for (i = 0; i < ssa_vars_count; i++) {
525
0
    root = ees[i];
526
0
    if (i != root) {
527
0
      ssa_vars[i].escape_state = ssa_vars[root].escape_state;
528
0
    }
529
0
  }
530
531
0
  free_alloca(ees, use_heap);
532
533
0
  return SUCCESS;
534
0
}
535
/* }}} */