Coverage Report

Created: 2025-12-14 06:06

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