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/zend_cfg.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, CFG - Control Flow Graph                                |
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 "zend_compile.h"
20
#include "zend_cfg.h"
21
#include "zend_func_info.h"
22
#include "zend_worklist.h"
23
#include "zend_optimizer.h"
24
#include "zend_optimizer_internal.h"
25
#include "zend_sort.h"
26
27
static void zend_mark_reachable(zend_op *opcodes, zend_cfg *cfg, zend_basic_block *b) /* {{{ */
28
0
{
29
0
  zend_basic_block *blocks = cfg->blocks;
30
31
0
  zend_worklist work;
32
0
  ALLOCA_FLAG(list_use_heap)
33
0
  ZEND_WORKLIST_ALLOCA(&work, cfg->blocks_count, list_use_heap);
34
35
0
  zend_worklist_push(&work, b - cfg->blocks);
36
37
0
  while (zend_worklist_len(&work)) {
38
0
    int i;
39
0
    b = cfg->blocks + zend_worklist_pop(&work);
40
41
0
    b->flags |= ZEND_BB_REACHABLE;
42
0
    if (b->successors_count == 0) {
43
0
      b->flags |= ZEND_BB_EXIT;
44
0
      continue;
45
0
    }
46
47
0
    for (i = 0; i < b->successors_count; i++) {
48
0
      zend_basic_block *succ = blocks + b->successors[i];
49
50
0
      if (b->len != 0) {
51
0
        uint8_t opcode = opcodes[b->start + b->len - 1].opcode;
52
0
        if (opcode == ZEND_MATCH) {
53
0
          succ->flags |= ZEND_BB_TARGET;
54
0
        } else if (opcode == ZEND_SWITCH_LONG || opcode == ZEND_SWITCH_STRING) {
55
0
          if (i == b->successors_count - 1) {
56
0
            succ->flags |= ZEND_BB_FOLLOW | ZEND_BB_TARGET;
57
0
          } else {
58
0
            succ->flags |= ZEND_BB_TARGET;
59
0
          }
60
0
        } else if (b->successors_count == 1) {
61
0
          if (opcode == ZEND_JMP) {
62
0
            succ->flags |= ZEND_BB_TARGET;
63
0
          } else {
64
0
            succ->flags |= ZEND_BB_FOLLOW;
65
66
0
            if ((cfg->flags & ZEND_CFG_STACKLESS)) {
67
0
              if (opcode == ZEND_INCLUDE_OR_EVAL ||
68
0
                opcode == ZEND_GENERATOR_CREATE ||
69
0
                opcode == ZEND_YIELD ||
70
0
                opcode == ZEND_YIELD_FROM ||
71
0
                opcode == ZEND_DO_FCALL ||
72
0
                opcode == ZEND_DO_UCALL ||
73
0
                opcode == ZEND_DO_FCALL_BY_NAME) {
74
0
                succ->flags |= ZEND_BB_ENTRY;
75
0
              }
76
0
            }
77
0
            if ((cfg->flags & ZEND_CFG_RECV_ENTRY)) {
78
0
              if (opcode == ZEND_RECV ||
79
0
                opcode == ZEND_RECV_INIT) {
80
0
                succ->flags |= ZEND_BB_RECV_ENTRY;
81
0
              }
82
0
            }
83
0
          }
84
0
        } else {
85
0
          ZEND_ASSERT(b->successors_count == 2);
86
0
          if (i == 0) {
87
0
            succ->flags |= ZEND_BB_TARGET;
88
0
          } else {
89
0
            succ->flags |= ZEND_BB_FOLLOW;
90
0
          }
91
0
        }
92
0
      } else {
93
0
        succ->flags |= ZEND_BB_FOLLOW;
94
0
      }
95
96
      /* Check reachability of successor */
97
0
      if (!(succ->flags & ZEND_BB_REACHABLE)) {
98
0
        zend_worklist_push(&work, succ - cfg->blocks);
99
0
      }
100
0
    }
101
0
  }
102
103
0
  ZEND_WORKLIST_FREE_ALLOCA(&work, list_use_heap);
104
0
}
105
/* }}} */
106
107
static void zend_mark_reachable_blocks(const zend_op_array *op_array, zend_cfg *cfg, int start) /* {{{ */
108
0
{
109
0
  zend_basic_block *blocks = cfg->blocks;
110
111
0
  blocks[start].flags = ZEND_BB_START;
112
0
  zend_mark_reachable(op_array->opcodes, cfg, blocks + start);
113
114
0
  if (op_array->last_try_catch) {
115
0
    zend_basic_block *b;
116
0
    int j, changed;
117
0
    uint32_t *block_map = cfg->map;
118
119
0
    do {
120
0
      changed = 0;
121
122
      /* Add exception paths */
123
0
      for (j = 0; j < op_array->last_try_catch; j++) {
124
125
        /* check for jumps into the middle of try block */
126
0
        b = blocks + block_map[op_array->try_catch_array[j].try_op];
127
0
        if (!(b->flags & ZEND_BB_REACHABLE)) {
128
0
          zend_basic_block *end;
129
130
0
          if (op_array->try_catch_array[j].catch_op) {
131
0
            end = blocks + block_map[op_array->try_catch_array[j].catch_op];
132
0
            while (b != end) {
133
0
              if (b->flags & ZEND_BB_REACHABLE) {
134
0
                op_array->try_catch_array[j].try_op = b->start;
135
0
                break;
136
0
              }
137
0
              b++;
138
0
            }
139
0
          }
140
0
          b = blocks + block_map[op_array->try_catch_array[j].try_op];
141
0
          if (!(b->flags & ZEND_BB_REACHABLE)) {
142
0
            if (op_array->try_catch_array[j].finally_op) {
143
0
              end = blocks + block_map[op_array->try_catch_array[j].finally_op];
144
0
              while (b != end) {
145
0
                if (b->flags & ZEND_BB_REACHABLE) {
146
                  /* In case we get here, there is no live try block but there is a live finally block.
147
                   * If we do have catch_op set, we need to set it to the first catch block to satisfy
148
                   * the constraint try_op <= catch_op <= finally_op */
149
0
                  op_array->try_catch_array[j].try_op =
150
0
                    op_array->try_catch_array[j].catch_op ? op_array->try_catch_array[j].catch_op : b->start;
151
0
                  changed = 1;
152
0
                  zend_mark_reachable(op_array->opcodes, cfg, blocks + block_map[op_array->try_catch_array[j].try_op]);
153
0
                  break;
154
0
                }
155
0
                b++;
156
0
              }
157
0
            }
158
0
          }
159
0
        }
160
161
0
        b = blocks + block_map[op_array->try_catch_array[j].try_op];
162
0
        if (b->flags & ZEND_BB_REACHABLE) {
163
0
          b->flags |= ZEND_BB_TRY;
164
0
          if (op_array->try_catch_array[j].catch_op) {
165
0
            b = blocks + block_map[op_array->try_catch_array[j].catch_op];
166
0
            b->flags |= ZEND_BB_CATCH;
167
0
            if (!(b->flags & ZEND_BB_REACHABLE)) {
168
0
              changed = 1;
169
0
              zend_mark_reachable(op_array->opcodes, cfg, b);
170
0
            }
171
0
          }
172
0
          if (op_array->try_catch_array[j].finally_op) {
173
0
            b = blocks + block_map[op_array->try_catch_array[j].finally_op];
174
0
            b->flags |= ZEND_BB_FINALLY;
175
0
            if (!(b->flags & ZEND_BB_REACHABLE)) {
176
0
              changed = 1;
177
0
              zend_mark_reachable(op_array->opcodes, cfg, b);
178
0
            }
179
0
          }
180
0
          if (op_array->try_catch_array[j].finally_end) {
181
0
            b = blocks + block_map[op_array->try_catch_array[j].finally_end];
182
0
            b->flags |= ZEND_BB_FINALLY_END;
183
0
            if (!(b->flags & ZEND_BB_REACHABLE)) {
184
0
              changed = 1;
185
0
              zend_mark_reachable(op_array->opcodes, cfg, b);
186
0
            }
187
0
          }
188
0
        } else {
189
0
          if (op_array->try_catch_array[j].catch_op) {
190
0
            ZEND_ASSERT(!(blocks[block_map[op_array->try_catch_array[j].catch_op]].flags & ZEND_BB_REACHABLE));
191
0
          }
192
0
          if (op_array->try_catch_array[j].finally_op) {
193
0
            ZEND_ASSERT(!(blocks[block_map[op_array->try_catch_array[j].finally_op]].flags & ZEND_BB_REACHABLE));
194
0
          }
195
0
          if (op_array->try_catch_array[j].finally_end) {
196
0
            ZEND_ASSERT(!(blocks[block_map[op_array->try_catch_array[j].finally_end]].flags & ZEND_BB_REACHABLE));
197
0
          }
198
0
        }
199
0
      }
200
0
    } while (changed);
201
0
  }
202
203
0
  if (cfg->flags & ZEND_FUNC_FREE_LOOP_VAR) {
204
0
    zend_basic_block *b;
205
0
    int j;
206
0
    uint32_t *block_map = cfg->map;
207
208
    /* Mark blocks that are unreachable, but free a loop var created in a reachable block. */
209
0
    for (b = blocks; b < blocks + cfg->blocks_count; b++) {
210
0
      if (b->flags & ZEND_BB_REACHABLE) {
211
0
        continue;
212
0
      }
213
214
0
      for (j = b->start; j < b->start + b->len; j++) {
215
0
        zend_op *opline = &op_array->opcodes[j];
216
0
        if (zend_optimizer_is_loop_var_free(opline)) {
217
0
          zend_op *def_opline = zend_optimizer_get_loop_var_def(op_array, opline);
218
0
          if (def_opline) {
219
0
            uint32_t def_block = block_map[def_opline - op_array->opcodes];
220
0
            if (blocks[def_block].flags & ZEND_BB_REACHABLE) {
221
0
              b->flags |= ZEND_BB_UNREACHABLE_FREE;
222
0
              break;
223
0
            }
224
0
          }
225
0
        }
226
0
      }
227
0
    }
228
0
  }
229
0
}
230
/* }}} */
231
232
void zend_cfg_remark_reachable_blocks(const zend_op_array *op_array, zend_cfg *cfg) /* {{{ */
233
0
{
234
0
  zend_basic_block *blocks = cfg->blocks;
235
0
  int i;
236
0
  int start = 0;
237
238
0
  for (i = 0; i < cfg->blocks_count; i++) {
239
0
    if (blocks[i].flags & ZEND_BB_REACHABLE) {
240
0
      start = i;
241
0
      i++;
242
0
      break;
243
0
    }
244
0
  }
245
246
  /* clear all flags */
247
0
  for (i = 0; i < cfg->blocks_count; i++) {
248
0
    blocks[i].flags = 0;
249
0
  }
250
251
0
  zend_mark_reachable_blocks(op_array, cfg, start);
252
0
}
253
/* }}} */
254
255
0
static void initialize_block(zend_basic_block *block) {
256
0
  block->flags = 0;
257
0
  block->successors = block->successors_storage;
258
0
  block->successors_count = 0;
259
0
  block->predecessors_count = 0;
260
0
  block->predecessor_offset = -1;
261
0
  block->idom = -1;
262
0
  block->loop_header = -1;
263
0
  block->level = -1;
264
0
  block->children = -1;
265
0
  block->next_child = -1;
266
0
}
267
268
0
#define BB_START(i) do { \
269
0
    if (!block_map[i]) { blocks_count++;} \
270
0
    block_map[i]++; \
271
0
  } while (0)
272
273
ZEND_API void zend_build_cfg(zend_arena **arena, const zend_op_array *op_array, uint32_t build_flags, zend_cfg *cfg) /* {{{ */
274
0
{
275
0
  uint32_t flags = 0;
276
0
  uint32_t i;
277
0
  uint32_t *block_map;
278
0
  zend_function *fn;
279
0
  int blocks_count = 0;
280
0
  zend_basic_block *blocks;
281
0
  zval *zv;
282
0
  bool extra_entry_block = false;
283
284
0
  cfg->flags = build_flags & (ZEND_CFG_STACKLESS|ZEND_CFG_RECV_ENTRY);
285
286
0
  cfg->map = block_map = zend_arena_calloc(arena, op_array->last, sizeof(uint32_t));
287
288
  /* Build CFG, Step 1: Find basic blocks starts, calculate number of blocks */
289
0
  BB_START(0);
290
0
  for (i = 0; i < op_array->last; i++) {
291
0
    zend_op *opline = op_array->opcodes + i;
292
0
    switch (opline->opcode) {
293
0
      case ZEND_RECV:
294
0
      case ZEND_RECV_INIT:
295
0
        if (build_flags & ZEND_CFG_RECV_ENTRY) {
296
0
          BB_START(i + 1);
297
0
        }
298
0
        break;
299
0
      case ZEND_RETURN:
300
0
      case ZEND_RETURN_BY_REF:
301
0
      case ZEND_GENERATOR_RETURN:
302
0
      case ZEND_VERIFY_NEVER_TYPE:
303
0
        if (i + 1 < op_array->last) {
304
0
          BB_START(i + 1);
305
0
        }
306
0
        break;
307
0
      case ZEND_MATCH_ERROR:
308
0
      case ZEND_THROW:
309
        /* Don't treat THROW as terminator if it's used in expression context,
310
         * as we may lose live ranges when eliminating unreachable code. */
311
0
        if (opline->extended_value != ZEND_THROW_IS_EXPR && i + 1 < op_array->last) {
312
0
          BB_START(i + 1);
313
0
        }
314
0
        break;
315
0
      case ZEND_INCLUDE_OR_EVAL:
316
0
        flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
317
0
        ZEND_FALLTHROUGH;
318
0
      case ZEND_GENERATOR_CREATE:
319
0
      case ZEND_YIELD:
320
0
      case ZEND_YIELD_FROM:
321
0
        if (build_flags & ZEND_CFG_STACKLESS) {
322
0
          BB_START(i + 1);
323
0
        }
324
0
        break;
325
0
      case ZEND_DO_FCALL:
326
0
      case ZEND_DO_UCALL:
327
0
      case ZEND_DO_FCALL_BY_NAME:
328
0
        flags |= ZEND_FUNC_HAS_CALLS;
329
0
        if (build_flags & ZEND_CFG_STACKLESS) {
330
0
          BB_START(i + 1);
331
0
        }
332
0
        break;
333
0
      case ZEND_DO_ICALL:
334
0
        flags |= ZEND_FUNC_HAS_CALLS;
335
0
        break;
336
0
      case ZEND_INIT_FCALL:
337
0
      case ZEND_INIT_NS_FCALL_BY_NAME:
338
0
        zv = CRT_CONSTANT(opline->op2);
339
0
        if (opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
340
          /* The third literal is the lowercased unqualified name */
341
0
          zv += 2;
342
0
        }
343
0
        if ((fn = zend_hash_find_ptr(EG(function_table), Z_STR_P(zv))) != NULL) {
344
0
          if (fn->type == ZEND_INTERNAL_FUNCTION) {
345
0
            flags |= zend_optimizer_classify_function(
346
0
              Z_STR_P(zv), opline->extended_value);
347
0
          }
348
0
        }
349
0
        break;
350
0
      case ZEND_FAST_CALL:
351
0
        BB_START(OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes);
352
0
        BB_START(i + 1);
353
0
        break;
354
0
      case ZEND_FAST_RET:
355
0
        if (i + 1 < op_array->last) {
356
0
          BB_START(i + 1);
357
0
        }
358
0
        break;
359
0
      case ZEND_JMP:
360
0
        BB_START(OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes);
361
0
        if (i + 1 < op_array->last) {
362
0
          BB_START(i + 1);
363
0
        }
364
0
        break;
365
0
      case ZEND_JMPZ:
366
0
      case ZEND_JMPNZ:
367
0
      case ZEND_JMPZ_EX:
368
0
      case ZEND_JMPNZ_EX:
369
0
      case ZEND_JMP_SET:
370
0
      case ZEND_COALESCE:
371
0
      case ZEND_ASSERT_CHECK:
372
0
      case ZEND_JMP_NULL:
373
0
      case ZEND_BIND_INIT_STATIC_OR_JMP:
374
0
      case ZEND_JMP_FRAMELESS:
375
0
        BB_START(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes);
376
0
        BB_START(i + 1);
377
0
        break;
378
0
      case ZEND_CATCH:
379
0
        if (!(opline->extended_value & ZEND_LAST_CATCH)) {
380
0
          BB_START(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes);
381
0
        }
382
0
        BB_START(i + 1);
383
0
        break;
384
0
      case ZEND_FE_FETCH_R:
385
0
      case ZEND_FE_FETCH_RW:
386
0
        BB_START(ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
387
0
        BB_START(i + 1);
388
0
        break;
389
0
      case ZEND_FE_RESET_R:
390
0
      case ZEND_FE_RESET_RW:
391
0
        BB_START(OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes);
392
0
        BB_START(i + 1);
393
0
        break;
394
0
      case ZEND_SWITCH_LONG:
395
0
      case ZEND_SWITCH_STRING:
396
0
      case ZEND_MATCH:
397
0
      {
398
0
        HashTable *jumptable = Z_ARRVAL_P(CRT_CONSTANT(opline->op2));
399
0
        zval *zv;
400
0
        ZEND_HASH_FOREACH_VAL(jumptable, zv) {
401
0
          BB_START(ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv)));
402
0
        } ZEND_HASH_FOREACH_END();
403
0
        BB_START(ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value));
404
0
        BB_START(i + 1);
405
0
        break;
406
0
      }
407
0
      case ZEND_FETCH_R:
408
0
      case ZEND_FETCH_W:
409
0
      case ZEND_FETCH_RW:
410
0
      case ZEND_FETCH_FUNC_ARG:
411
0
      case ZEND_FETCH_IS:
412
0
      case ZEND_FETCH_UNSET:
413
0
      case ZEND_UNSET_VAR:
414
0
      case ZEND_ISSET_ISEMPTY_VAR:
415
0
        if (opline->extended_value & ZEND_FETCH_LOCAL) {
416
0
          flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
417
0
        } else if ((opline->extended_value & (ZEND_FETCH_GLOBAL | ZEND_FETCH_GLOBAL_LOCK)) &&
418
0
                   !op_array->function_name) {
419
0
          flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
420
0
        }
421
0
        break;
422
0
      case ZEND_FUNC_GET_ARGS:
423
0
        flags |= ZEND_FUNC_VARARG;
424
0
        break;
425
0
      case ZEND_EXT_STMT:
426
0
        flags |= ZEND_FUNC_HAS_EXTENDED_STMT;
427
0
        break;
428
0
      case ZEND_EXT_FCALL_BEGIN:
429
0
      case ZEND_EXT_FCALL_END:
430
0
        flags |= ZEND_FUNC_HAS_EXTENDED_FCALL;
431
0
        break;
432
0
      case ZEND_FREE:
433
0
      case ZEND_FE_FREE:
434
0
        if (zend_optimizer_is_loop_var_free(opline)
435
0
         && ((opline-1)->opcode != ZEND_MATCH_ERROR
436
0
          || (opline-1)->extended_value != ZEND_THROW_IS_EXPR)) {
437
0
          BB_START(i);
438
0
          flags |= ZEND_FUNC_FREE_LOOP_VAR;
439
0
        }
440
0
        break;
441
0
    }
442
0
  }
443
444
  /* If the entry block has predecessors, we may need to split it */
445
0
  if ((build_flags & ZEND_CFG_NO_ENTRY_PREDECESSORS)
446
0
      && op_array->last > 0 && block_map[0] > 1) {
447
0
    extra_entry_block = true;
448
0
  }
449
450
0
  if (op_array->last_try_catch) {
451
0
    for (uint32_t j = 0; j < op_array->last_try_catch; j++) {
452
0
      BB_START(op_array->try_catch_array[j].try_op);
453
0
      if (op_array->try_catch_array[j].catch_op) {
454
0
        BB_START(op_array->try_catch_array[j].catch_op);
455
0
      }
456
0
      if (op_array->try_catch_array[j].finally_op) {
457
0
        BB_START(op_array->try_catch_array[j].finally_op);
458
0
      }
459
0
      if (op_array->try_catch_array[j].finally_end) {
460
0
        BB_START(op_array->try_catch_array[j].finally_end);
461
0
      }
462
0
    }
463
0
  }
464
465
0
  blocks_count += extra_entry_block;
466
0
  cfg->blocks_count = blocks_count;
467
468
  /* Build CFG, Step 2: Build Array of Basic Blocks */
469
0
  cfg->blocks = blocks = zend_arena_calloc(arena, sizeof(zend_basic_block), blocks_count);
470
471
0
  blocks_count = -1;
472
473
0
  if (extra_entry_block) {
474
0
    initialize_block(&blocks[0]);
475
0
    blocks[0].start = 0;
476
0
    blocks[0].len = 0;
477
0
    blocks_count++;
478
0
  }
479
480
0
  for (i = 0; i < op_array->last; i++) {
481
0
    if (block_map[i]) {
482
0
      if (blocks_count >= 0) {
483
0
        blocks[blocks_count].len = i - blocks[blocks_count].start;
484
0
      }
485
0
      blocks_count++;
486
0
      initialize_block(&blocks[blocks_count]);
487
0
      blocks[blocks_count].start = i;
488
0
    }
489
0
    block_map[i] = blocks_count;
490
0
  }
491
492
0
  blocks[blocks_count].len = i - blocks[blocks_count].start;
493
0
  blocks_count++;
494
495
  /* Build CFG, Step 3: Calculate successors */
496
0
  for (int j = 0; j < blocks_count; j++) {
497
0
    zend_basic_block *block = &blocks[j];
498
0
    zend_op *opline;
499
0
    if (block->len == 0) {
500
0
      block->successors_count = 1;
501
0
      block->successors[0] = j + 1;
502
0
      continue;
503
0
    }
504
505
0
    opline = op_array->opcodes + block->start + block->len - 1;
506
0
    switch (opline->opcode) {
507
0
      case ZEND_FAST_RET:
508
0
      case ZEND_RETURN:
509
0
      case ZEND_RETURN_BY_REF:
510
0
      case ZEND_GENERATOR_RETURN:
511
0
      case ZEND_THROW:
512
0
      case ZEND_MATCH_ERROR:
513
0
      case ZEND_VERIFY_NEVER_TYPE:
514
0
        break;
515
0
      case ZEND_JMP:
516
0
        block->successors_count = 1;
517
0
        block->successors[0] = block_map[OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes];
518
0
        break;
519
0
      case ZEND_JMPZ:
520
0
      case ZEND_JMPNZ:
521
0
      case ZEND_JMPZ_EX:
522
0
      case ZEND_JMPNZ_EX:
523
0
      case ZEND_JMP_SET:
524
0
      case ZEND_COALESCE:
525
0
      case ZEND_ASSERT_CHECK:
526
0
      case ZEND_JMP_NULL:
527
0
      case ZEND_BIND_INIT_STATIC_OR_JMP:
528
0
      case ZEND_JMP_FRAMELESS:
529
0
        block->successors_count = 2;
530
0
        block->successors[0] = block_map[OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes];
531
0
        block->successors[1] = j + 1;
532
0
        break;
533
0
      case ZEND_CATCH:
534
0
        if (!(opline->extended_value & ZEND_LAST_CATCH)) {
535
0
          block->successors_count = 2;
536
0
          block->successors[0] = block_map[OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes];
537
0
          block->successors[1] = j + 1;
538
0
        } else {
539
0
          block->successors_count = 1;
540
0
          block->successors[0] = j + 1;
541
0
        }
542
0
        break;
543
0
      case ZEND_FE_FETCH_R:
544
0
      case ZEND_FE_FETCH_RW:
545
0
        block->successors_count = 2;
546
0
        block->successors[0] = block_map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
547
0
        block->successors[1] = j + 1;
548
0
        break;
549
0
      case ZEND_FE_RESET_R:
550
0
      case ZEND_FE_RESET_RW:
551
0
        block->successors_count = 2;
552
0
        block->successors[0] = block_map[OP_JMP_ADDR(opline, opline->op2) - op_array->opcodes];
553
0
        block->successors[1] = j + 1;
554
0
        break;
555
0
      case ZEND_FAST_CALL:
556
0
        block->successors_count = 2;
557
0
        block->successors[0] = block_map[OP_JMP_ADDR(opline, opline->op1) - op_array->opcodes];
558
0
        block->successors[1] = j + 1;
559
0
        break;
560
0
      case ZEND_SWITCH_LONG:
561
0
      case ZEND_SWITCH_STRING:
562
0
      case ZEND_MATCH:
563
0
      {
564
0
        HashTable *jumptable = Z_ARRVAL_P(CRT_CONSTANT(opline->op2));
565
0
        zval *zv;
566
0
        uint32_t s = 0;
567
568
0
        block->successors_count = (opline->opcode == ZEND_MATCH ? 1 : 2) + zend_hash_num_elements(jumptable);
569
0
        block->successors = zend_arena_calloc(arena, block->successors_count, sizeof(int));
570
571
0
        ZEND_HASH_FOREACH_VAL(jumptable, zv) {
572
0
          block->successors[s++] = block_map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(zv))];
573
0
        } ZEND_HASH_FOREACH_END();
574
575
0
        block->successors[s++] = block_map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
576
0
        if (opline->opcode != ZEND_MATCH) {
577
0
          block->successors[s++] = j + 1;
578
0
        }
579
0
        break;
580
0
      }
581
0
      default:
582
0
        block->successors_count = 1;
583
0
        block->successors[0] = j + 1;
584
0
        break;
585
0
    }
586
0
  }
587
588
  /* Build CFG, Step 4, Mark Reachable Basic Blocks */
589
0
  cfg->flags |= flags;
590
0
  zend_mark_reachable_blocks(op_array, cfg, 0);
591
0
}
592
/* }}} */
593
594
ZEND_API void zend_cfg_build_predecessors(zend_arena **arena, zend_cfg *cfg) /* {{{ */
595
0
{
596
0
  int j, s, edges;
597
0
  zend_basic_block *b;
598
0
  zend_basic_block *blocks = cfg->blocks;
599
0
  zend_basic_block *end = blocks + cfg->blocks_count;
600
0
  int *predecessors;
601
602
0
  edges = 0;
603
0
  for (b = blocks; b < end; b++) {
604
0
    b->predecessors_count = 0;
605
0
  }
606
0
  for (b = blocks; b < end; b++) {
607
0
    if (!(b->flags & ZEND_BB_REACHABLE)) {
608
0
      b->successors_count = 0;
609
0
      b->predecessors_count = 0;
610
0
    } else {
611
0
      for (s = 0; s < b->successors_count; s++) {
612
0
        edges++;
613
0
        blocks[b->successors[s]].predecessors_count++;
614
0
      }
615
0
    }
616
0
  }
617
618
0
  cfg->edges_count = edges;
619
0
  cfg->predecessors = predecessors = (int*)zend_arena_calloc(arena, sizeof(int), edges);
620
621
0
  edges = 0;
622
0
  for (b = blocks; b < end; b++) {
623
0
    if (b->flags & ZEND_BB_REACHABLE) {
624
0
      b->predecessor_offset = edges;
625
0
      edges += b->predecessors_count;
626
0
      b->predecessors_count = 0;
627
0
    }
628
0
  }
629
630
0
  for (j = 0; j < cfg->blocks_count; j++) {
631
0
    if (blocks[j].flags & ZEND_BB_REACHABLE) {
632
      /* SWITCH_STRING/LONG may have few identical successors */
633
0
      for (s = 0; s < blocks[j].successors_count; s++) {
634
0
        int duplicate = 0;
635
0
        int p;
636
637
0
        for (p = 0; p < s; p++) {
638
0
          if (blocks[j].successors[p] == blocks[j].successors[s]) {
639
0
            duplicate = 1;
640
0
            break;
641
0
          }
642
0
        }
643
0
        if (!duplicate) {
644
0
          zend_basic_block *b = blocks + blocks[j].successors[s];
645
646
0
          predecessors[b->predecessor_offset + b->predecessors_count] = j;
647
0
          b->predecessors_count++;
648
0
        }
649
0
      }
650
0
    }
651
0
  }
652
0
}
653
/* }}} */
654
655
/* Computes a postorder numbering of the CFG */
656
static void compute_postnum_recursive(
657
    int *postnum, int *cur, const zend_cfg *cfg, int block_num) /* {{{ */
658
0
{
659
0
  int s;
660
0
  zend_basic_block *block = &cfg->blocks[block_num];
661
0
  if (postnum[block_num] != -1) {
662
0
    return;
663
0
  }
664
665
0
  postnum[block_num] = -2; /* Marker for "currently visiting" */
666
0
  for (s = 0; s < block->successors_count; s++) {
667
0
    compute_postnum_recursive(postnum, cur, cfg, block->successors[s]);
668
0
  }
669
0
  postnum[block_num] = (*cur)++;
670
0
}
671
/* }}} */
672
673
/* Computes dominator tree using algorithm from "A Simple, Fast Dominance Algorithm" by
674
 * Cooper, Harvey and Kennedy. */
675
ZEND_API void zend_cfg_compute_dominators_tree(const zend_op_array *op_array, zend_cfg *cfg) /* {{{ */
676
0
{
677
0
  zend_basic_block *blocks = cfg->blocks;
678
0
  int blocks_count = cfg->blocks_count;
679
0
  int j, k, changed;
680
681
0
  if (cfg->blocks_count == 1) {
682
0
    blocks[0].level = 0;
683
0
    return;
684
0
  }
685
686
0
  ALLOCA_FLAG(use_heap)
687
0
  int *postnum = do_alloca(sizeof(int) * cfg->blocks_count, use_heap);
688
0
  memset(postnum, -1, sizeof(int) * cfg->blocks_count);
689
0
  j = 0;
690
0
  compute_postnum_recursive(postnum, &j, cfg, 0);
691
692
  /* FIXME: move declarations */
693
0
  blocks[0].idom = 0;
694
0
  do {
695
0
    changed = 0;
696
    /* Iterating in RPO here would converge faster */
697
0
    for (j = 1; j < blocks_count; j++) {
698
0
      int idom = -1;
699
700
0
      if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
701
0
        continue;
702
0
      }
703
0
      for (k = 0; k < blocks[j].predecessors_count; k++) {
704
0
        int pred = cfg->predecessors[blocks[j].predecessor_offset + k];
705
706
0
        if (blocks[pred].idom >= 0) {
707
0
          if (idom < 0) {
708
0
            idom = pred;
709
0
          } else {
710
0
            while (idom != pred) {
711
0
              while (postnum[pred] < postnum[idom]) pred = blocks[pred].idom;
712
0
              while (postnum[idom] < postnum[pred]) idom = blocks[idom].idom;
713
0
            }
714
0
          }
715
0
        }
716
0
      }
717
718
0
      if (idom >= 0 && blocks[j].idom != idom) {
719
0
        blocks[j].idom = idom;
720
0
        changed = 1;
721
0
      }
722
0
    }
723
0
  } while (changed);
724
0
  blocks[0].idom = -1;
725
726
0
  for (j = 1; j < blocks_count; j++) {
727
0
    if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
728
0
      continue;
729
0
    }
730
0
    if (blocks[j].idom >= 0) {
731
      /* Sort by block number to traverse children in pre-order */
732
0
      if (blocks[blocks[j].idom].children < 0 ||
733
0
          j < blocks[blocks[j].idom].children) {
734
0
        blocks[j].next_child = blocks[blocks[j].idom].children;
735
0
        blocks[blocks[j].idom].children = j;
736
0
      } else {
737
0
        int k = blocks[blocks[j].idom].children;
738
0
        while (blocks[k].next_child >=0 && j > blocks[k].next_child) {
739
0
          k = blocks[k].next_child;
740
0
        }
741
0
        blocks[j].next_child = blocks[k].next_child;
742
0
        blocks[k].next_child = j;
743
0
      }
744
0
    }
745
0
  }
746
747
0
  for (j = 0; j < blocks_count; j++) {
748
0
    int idom = blocks[j].idom, level = 0;
749
0
    if ((blocks[j].flags & ZEND_BB_REACHABLE) == 0) {
750
0
      continue;
751
0
    }
752
0
    while (idom >= 0) {
753
0
      level++;
754
0
      if (blocks[idom].level >= 0) {
755
0
        level += blocks[idom].level;
756
0
        break;
757
0
      } else {
758
0
        idom = blocks[idom].idom;
759
0
      }
760
0
    }
761
0
    blocks[j].level = level;
762
0
  }
763
764
0
  free_alloca(postnum, use_heap);
765
0
}
766
/* }}} */
767
768
static bool dominates(zend_basic_block *blocks, int a, int b) /* {{{ */
769
0
{
770
0
  while (blocks[b].level > blocks[a].level) {
771
0
    b = blocks[b].idom;
772
0
  }
773
0
  return a == b;
774
0
}
775
/* }}} */
776
777
ZEND_API void zend_cfg_identify_loops(const zend_op_array *op_array, zend_cfg *cfg) /* {{{ */
778
0
{
779
0
  int i, j, k, n;
780
0
  int time;
781
0
  zend_basic_block *blocks = cfg->blocks;
782
0
  int *entry_times, *exit_times;
783
0
  zend_worklist work;
784
0
  int flag = ZEND_FUNC_NO_LOOPS;
785
0
  int *sorted_blocks;
786
0
  ALLOCA_FLAG(list_use_heap)
787
0
  ALLOCA_FLAG(tree_use_heap)
788
789
0
  if (cfg->blocks_count == 1) {
790
0
    cfg->flags |= flag;
791
0
    return;
792
0
  }
793
794
0
  ZEND_WORKLIST_ALLOCA(&work, cfg->blocks_count, list_use_heap);
795
796
  /* We don't materialize the DJ spanning tree explicitly, as we are only interested in ancestor
797
   * queries. These are implemented by checking entry/exit times of the DFS search. */
798
0
  entry_times = do_alloca(3 * sizeof(int) * cfg->blocks_count, tree_use_heap);
799
0
  exit_times = entry_times + cfg->blocks_count;
800
0
  sorted_blocks = exit_times + cfg->blocks_count;
801
0
  memset(entry_times, -1, 2 * sizeof(int) * cfg->blocks_count);
802
803
0
  zend_worklist_push(&work, 0);
804
0
  time = 0;
805
0
  while (zend_worklist_len(&work)) {
806
0
  next:
807
0
    i = zend_worklist_peek(&work);
808
0
    if (entry_times[i] == -1) {
809
0
      entry_times[i] = time++;
810
0
    }
811
    /* Visit blocks immediately dominated by i. */
812
0
    for (j = blocks[i].children; j >= 0; j = blocks[j].next_child) {
813
0
      if (zend_worklist_push(&work, j)) {
814
0
        goto next;
815
0
      }
816
0
    }
817
    /* Visit join edges.  */
818
0
    for (j = 0; j < blocks[i].successors_count; j++) {
819
0
      int succ = blocks[i].successors[j];
820
0
      if (blocks[succ].idom == i) {
821
0
        continue;
822
0
      } else if (zend_worklist_push(&work, succ)) {
823
0
        goto next;
824
0
      }
825
0
    }
826
0
    exit_times[i] = time++;
827
0
    zend_worklist_pop(&work);
828
0
  }
829
830
  /* Sort blocks by level, which is the opposite order in which we want to process them */
831
0
  sorted_blocks[0] = 0;
832
0
  j = 0;
833
0
  n = 1;
834
0
  while (j != n) {
835
0
    i = j;
836
0
    j = n;
837
0
    for (; i < j; i++) {
838
0
      int child;
839
0
      for (child = blocks[sorted_blocks[i]].children; child >= 0; child = blocks[child].next_child) {
840
0
        sorted_blocks[n++] = child;
841
0
      }
842
0
    }
843
0
  }
844
845
  /* Identify loops. See Sreedhar et al, "Identifying Loops Using DJ Graphs". */
846
0
  while (n > 0) {
847
0
    i = sorted_blocks[--n];
848
849
0
    if (blocks[i].predecessors_count < 2) {
850
        /* loop header has at least two input edges */
851
0
      continue;
852
0
    }
853
854
0
    for (j = 0; j < blocks[i].predecessors_count; j++) {
855
0
      int pred = cfg->predecessors[blocks[i].predecessor_offset + j];
856
857
      /* A join edge is one for which the predecessor does not
858
         immediately dominate the successor. */
859
0
      if (blocks[i].idom == pred) {
860
0
        continue;
861
0
      }
862
863
      /* In a loop back-edge (back-join edge), the successor dominates
864
         the predecessor.  */
865
0
      if (dominates(blocks, i, pred)) {
866
0
        blocks[i].flags |= ZEND_BB_LOOP_HEADER;
867
0
        flag &= ~ZEND_FUNC_NO_LOOPS;
868
0
        if (!zend_worklist_len(&work)) {
869
0
          zend_bitset_clear(work.visited, zend_bitset_len(cfg->blocks_count));
870
0
        }
871
0
        zend_worklist_push(&work, pred);
872
0
      } else {
873
        /* Otherwise it's a cross-join edge.  See if it's a branch
874
           to an ancestor on the DJ spanning tree.  */
875
0
        if (entry_times[pred] > entry_times[i] && exit_times[pred] < exit_times[i]) {
876
0
          blocks[i].flags |= ZEND_BB_IRREDUCIBLE_LOOP;
877
0
          flag |= ZEND_FUNC_IRREDUCIBLE;
878
0
          flag &= ~ZEND_FUNC_NO_LOOPS;
879
0
        }
880
0
      }
881
0
    }
882
0
    while (zend_worklist_len(&work)) {
883
0
      j = zend_worklist_pop(&work);
884
0
      while (blocks[j].loop_header >= 0) {
885
0
        j = blocks[j].loop_header;
886
0
      }
887
0
      if (j != i) {
888
0
        if (blocks[j].idom < 0 && j != 0) {
889
          /* Ignore blocks that are unreachable or only abnormally reachable. */
890
0
          continue;
891
0
        }
892
0
        blocks[j].loop_header = i;
893
0
        for (k = 0; k < blocks[j].predecessors_count; k++) {
894
0
          zend_worklist_push(&work, cfg->predecessors[blocks[j].predecessor_offset + k]);
895
0
        }
896
0
      }
897
0
    }
898
0
  }
899
900
0
  free_alloca(entry_times, tree_use_heap);
901
0
  ZEND_WORKLIST_FREE_ALLOCA(&work, list_use_heap);
902
903
0
  cfg->flags |= flag;
904
0
}
905
/* }}} */