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/zend_inference.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, e-SSA based Type & Range Inference                      |
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 "zend_compile.h"
18
#include "zend_generators.h"
19
#include "zend_inference.h"
20
#include "zend_func_info.h"
21
#include "zend_call_graph.h"
22
#include "zend_closures.h"
23
#include "zend_worklist.h"
24
#include "zend_optimizer_internal.h"
25
26
/* The used range inference algorithm is described in:
27
 *     V. Campos, R. Rodrigues, I. de Assis Costa and F. Pereira.
28
 *     "Speed and Precision in Range Analysis", SBLP'12.
29
 *
30
 * There are a couple degrees of freedom, we use:
31
 *  * Propagation on SCCs.
32
 *  * e-SSA for live range splitting.
33
 *  * Only intra-procedural inference.
34
 *  * Widening with warmup passes, but without jump sets.
35
 */
36
37
/* Whether to handle symbolic range constraints */
38
#define SYM_RANGE
39
40
/* Whether to handle negative range constraints */
41
/* Negative range inference is buggy, so disabled for now */
42
#undef NEG_RANGE
43
44
/* Number of warmup passes to use prior to widening */
45
0
#define RANGE_WARMUP_PASSES 16
46
47
/* Logging for range inference in general */
48
#if 0
49
#define LOG_SSA_RANGE(...) fprintf(stderr, __VA_ARGS__)
50
#else
51
#define LOG_SSA_RANGE(...)
52
#endif
53
54
/* Logging for negative range constraints */
55
#if 0
56
#define LOG_NEG_RANGE(...) fprintf(stderr, __VA_ARGS__)
57
#else
58
#define LOG_NEG_RANGE(...)
59
#endif
60
61
/* Pop elements in unspecified order from worklist until it is empty */
62
1
#define WHILE_WORKLIST(worklist, len, i) do { \
63
1
  bool _done = 0; \
64
2
  while (!_done) { \
65
1
    _done = 1; \
66
1
    ZEND_BITSET_FOREACH(worklist, len, i) { \
67
0
      zend_bitset_excl(worklist, i); \
68
0
      _done = 0;
69
70
#define WHILE_WORKLIST_END() \
71
0
    } ZEND_BITSET_FOREACH_END(); \
72
1
  } \
73
1
} while (0)
74
75
#define CHECK_SCC_VAR(var2) \
76
  do { \
77
    if (!ssa->vars[var2].no_val) { \
78
      if (ssa->vars[var2].scc < 0) { \
79
        zend_ssa_check_scc_var(op_array, ssa, var2, index, stack); \
80
      } \
81
      if (ssa->vars[var2].scc < ssa->vars[var].scc) { \
82
        ssa->vars[var].scc = ssa->vars[var2].scc; \
83
        is_root = 0; \
84
      } \
85
    } \
86
  } while (0)
87
88
#define CHECK_SCC_ENTRY(var2) \
89
0
  do { \
90
0
    if (ssa->vars[var2].scc != ssa->vars[var].scc) { \
91
0
      ssa->vars[var2].scc_entry = 1; \
92
0
    } \
93
0
  } while (0)
94
95
#define ADD_SCC_VAR(_var) \
96
0
  do { \
97
0
    if (ssa->vars[_var].scc == scc && \
98
0
        !(ssa->var_info[_var].type & MAY_BE_REF)) { \
99
0
      zend_bitset_incl(worklist, _var); \
100
0
    } \
101
0
  } while (0)
102
103
#define ADD_SCC_VAR_1(_var) \
104
0
  do { \
105
0
    if (ssa->vars[_var].scc == scc && \
106
0
        !(ssa->var_info[_var].type & MAY_BE_REF) && \
107
0
        !zend_bitset_in(visited, _var)) { \
108
0
      zend_bitset_incl(worklist, _var); \
109
0
    } \
110
0
  } while (0)
111
112
#define FOR_EACH_DEFINED_VAR(line, MACRO) \
113
0
  do { \
114
0
    if (ssa->ops[line].op1_def >= 0) { \
115
0
      MACRO(ssa->ops[line].op1_def); \
116
0
    } \
117
0
    if (ssa->ops[line].op2_def >= 0) { \
118
0
      MACRO(ssa->ops[line].op2_def); \
119
0
    } \
120
0
    if (ssa->ops[line].result_def >= 0) { \
121
0
      MACRO(ssa->ops[line].result_def); \
122
0
    } \
123
0
    if (op_array->opcodes[line].opcode == ZEND_OP_DATA) { \
124
0
      if (ssa->ops[line-1].op1_def >= 0) { \
125
0
        MACRO(ssa->ops[line-1].op1_def); \
126
0
      } \
127
0
      if (ssa->ops[line-1].op2_def >= 0) { \
128
0
        MACRO(ssa->ops[line-1].op2_def); \
129
0
      } \
130
0
      if (ssa->ops[line-1].result_def >= 0) { \
131
0
        MACRO(ssa->ops[line-1].result_def); \
132
0
      } \
133
0
    } else if ((uint32_t)line+1 < op_array->last && \
134
0
               op_array->opcodes[line+1].opcode == ZEND_OP_DATA) { \
135
0
      if (ssa->ops[line+1].op1_def >= 0) { \
136
0
        MACRO(ssa->ops[line+1].op1_def); \
137
0
      } \
138
0
      if (ssa->ops[line+1].op2_def >= 0) { \
139
0
        MACRO(ssa->ops[line+1].op2_def); \
140
0
      } \
141
0
      if (ssa->ops[line+1].result_def >= 0) { \
142
0
        MACRO(ssa->ops[line+1].result_def); \
143
0
      } \
144
0
    } \
145
0
  } while (0)
146
147
148
#define FOR_EACH_VAR_USAGE(_var, MACRO) \
149
0
  do { \
150
0
    zend_ssa_phi *p = ssa->vars[_var].phi_use_chain; \
151
0
    int use = ssa->vars[_var].use_chain; \
152
0
    while (use >= 0) { \
153
0
      FOR_EACH_DEFINED_VAR(use, MACRO); \
154
0
      use = zend_ssa_next_use(ssa->ops, _var, use); \
155
0
    } \
156
0
    p = ssa->vars[_var].phi_use_chain; \
157
0
    while (p) { \
158
0
      MACRO(p->ssa_var); \
159
0
      p = zend_ssa_next_use_phi(ssa, _var, p); \
160
0
    } \
161
0
  } while (0)
162
163
0
static inline bool add_will_overflow(zend_long a, zend_long b) {
164
0
  return (b > 0 && a > ZEND_LONG_MAX - b)
165
0
    || (b < 0 && a < ZEND_LONG_MIN - b);
166
0
}
167
#if 0
168
static inline bool sub_will_overflow(zend_long a, zend_long b) {
169
  return (b > 0 && a < ZEND_LONG_MIN + b)
170
    || (b < 0 && a > ZEND_LONG_MAX + b);
171
}
172
#endif
173
174
#if 0
175
/* Recursive Pearce's SCC algorithm implementation */
176
static void zend_ssa_check_scc_var(const zend_op_array *op_array, zend_ssa *ssa, int var, int *index, zend_worklist_stack *stack) /* {{{ */
177
{
178
  int is_root = 1;
179
#ifdef SYM_RANGE
180
  zend_ssa_phi *p;
181
#endif
182
183
  ssa->vars[var].scc = *index;
184
  (*index)++;
185
186
  FOR_EACH_VAR_USAGE(var, CHECK_SCC_VAR);
187
188
#ifdef SYM_RANGE
189
  /* Process symbolic control-flow constraints */
190
  p = ssa->vars[var].sym_use_chain;
191
  while (p) {
192
    CHECK_SCC_VAR(p->ssa_var);
193
    p = p->sym_use_chain;
194
  }
195
#endif
196
197
  if (is_root) {
198
    ssa->sccs--;
199
    while (stack->len > 0) {
200
      int var2 = zend_worklist_stack_peek(stack);
201
      if (ssa->vars[var2].scc < ssa->vars[var].scc) {
202
        break;
203
      }
204
      zend_worklist_stack_pop(stack);
205
      ssa->vars[var2].scc = ssa->sccs;
206
      (*index)--;
207
    }
208
    ssa->vars[var].scc = ssa->sccs;
209
    ssa->vars[var].scc_entry = 1;
210
    (*index)--;
211
  } else {
212
    zend_worklist_stack_push(stack, var);
213
  }
214
}
215
/* }}} */
216
217
ZEND_API void zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
218
{
219
  int index = 0;
220
  zend_worklist_stack stack;
221
  int j;
222
  ALLOCA_FLAG(stack_use_heap)
223
224
  ZEND_WORKLIST_STACK_ALLOCA(&stack, ssa->vars_count, stack_use_heap);
225
226
  /* Find SCCs using Pearce's algorithm. */
227
  ssa->sccs = ssa->vars_count;
228
  for (j = 0; j < ssa->vars_count; j++) {
229
    if (!ssa->vars[j].no_val && ssa->vars[j].scc < 0) {
230
      zend_ssa_check_scc_var(op_array, ssa, j, &index, &stack);
231
    }
232
  }
233
234
  if (ssa->sccs) {
235
    /* Shift SCC indexes. */
236
    for (j = 0; j < ssa->vars_count; j++) {
237
      if (ssa->vars[j].scc >= 0) {
238
        ssa->vars[j].scc -= ssa->sccs;
239
      }
240
    }
241
  }
242
  ssa->sccs = ssa->vars_count - ssa->sccs;
243
244
  for (j = 0; j < ssa->vars_count; j++) {
245
    if (ssa->vars[j].scc >= 0) {
246
      int var = j;
247
      FOR_EACH_VAR_USAGE(var, CHECK_SCC_ENTRY);
248
    }
249
  }
250
251
  ZEND_WORKLIST_STACK_FREE_ALLOCA(&stack, stack_use_heap);
252
}
253
/* }}} */
254
255
#else
256
/* Iterative Pearce's SCC algorithm implementation */
257
258
typedef struct _zend_scc_iterator {
259
  int               state;
260
  int               last;
261
  union {
262
    int           use;
263
    zend_ssa_phi *phi;
264
  };
265
} zend_scc_iterator;
266
267
static int zend_scc_next(const zend_op_array *op_array, const zend_ssa *ssa, int var, zend_scc_iterator *iterator) /* {{{ */
268
0
{
269
0
  zend_ssa_phi *phi;
270
0
  int use, var2;
271
272
0
  switch (iterator->state) {
273
0
    case 0:                       goto state_0;
274
0
    case 1:  use = iterator->use; goto state_1;
275
0
    case 2:  use = iterator->use; goto state_2;
276
0
    case 3:  use = iterator->use; goto state_3;
277
0
    case 4:  use = iterator->use; goto state_4;
278
0
    case 5:  use = iterator->use; goto state_5;
279
0
    case 6:  use = iterator->use; goto state_6;
280
0
    case 7:  use = iterator->use; goto state_7;
281
0
    case 8:  use = iterator->use; goto state_8;
282
0
    case 9:  phi = iterator->phi; goto state_9;
283
0
#ifdef SYM_RANGE
284
0
    case 10: phi = iterator->phi; goto state_10;
285
0
#endif
286
0
    case 11:                      goto state_11;
287
0
  }
288
289
0
state_0:
290
0
  use = ssa->vars[var].use_chain;
291
0
  while (use >= 0) {
292
0
    iterator->use = use;
293
0
    var2 = ssa->ops[use].op1_def;
294
0
    if (var2 >= 0 && !ssa->vars[var2].no_val) {
295
0
      iterator->state = 1;
296
0
      return var2;
297
0
    }
298
0
state_1:
299
0
    var2 = ssa->ops[use].op2_def;
300
0
    if (var2 >= 0 && !ssa->vars[var2].no_val) {
301
0
      iterator->state = 2;
302
0
      return var2;
303
0
    }
304
0
state_2:
305
0
    var2 = ssa->ops[use].result_def;
306
0
    if (var2 >= 0 && !ssa->vars[var2].no_val) {
307
0
      iterator->state = 3;
308
0
      return var2;
309
0
    }
310
0
state_3:
311
0
    if (op_array->opcodes[use].opcode == ZEND_OP_DATA) {
312
0
      var2 = ssa->ops[use-1].op1_def;
313
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
314
0
        iterator->state = 4;
315
0
        return var2;
316
0
      }
317
0
state_4:
318
0
      var2 = ssa->ops[use-1].op2_def;
319
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
320
0
        iterator->state = 5;
321
0
        return var2;
322
0
      }
323
0
state_5:
324
0
      var2 = ssa->ops[use-1].result_def;
325
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
326
0
        iterator->state = 8;
327
0
        return var2;
328
0
      }
329
0
    } else if ((uint32_t)use+1 < op_array->last &&
330
0
               op_array->opcodes[use+1].opcode == ZEND_OP_DATA) {
331
0
      var2 = ssa->ops[use+1].op1_def;
332
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
333
0
        iterator->state = 6;
334
0
        return var2;
335
0
      }
336
0
state_6:
337
0
      var2 = ssa->ops[use+1].op2_def;
338
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
339
0
        iterator->state = 7;
340
0
        return var2;
341
0
      }
342
0
state_7:
343
0
      var2 = ssa->ops[use+1].result_def;
344
0
      if (var2 >= 0 && !ssa->vars[var2].no_val) {
345
0
        iterator->state = 8;
346
0
        return var2;
347
0
      }
348
0
    }
349
0
state_8:
350
0
    use = zend_ssa_next_use(ssa->ops, var, use);
351
0
  }
352
353
0
  phi = ssa->vars[var].phi_use_chain;
354
0
  while (phi) {
355
0
    var2 = phi->ssa_var;
356
0
    if (!ssa->vars[var2].no_val) {
357
0
      iterator->state = 9;
358
0
      iterator->phi = phi;
359
0
      return var2;
360
0
    }
361
0
state_9:
362
0
    phi = zend_ssa_next_use_phi(ssa, var, phi);
363
0
  }
364
365
0
#ifdef SYM_RANGE
366
  /* Process symbolic control-flow constraints */
367
0
  phi = ssa->vars[var].sym_use_chain;
368
0
  while (phi) {
369
0
    var2 = phi->ssa_var;
370
0
    if (!ssa->vars[var2].no_val) {
371
0
      iterator->state = 10;
372
0
      iterator->phi = phi;
373
0
      return var2;
374
0
    }
375
0
state_10:
376
0
    phi = phi->sym_use_chain;
377
0
  }
378
0
#endif
379
380
0
  iterator->state = 11;
381
0
state_11:
382
0
  return -1;
383
0
}
384
/* }}} */
385
386
static void zend_ssa_check_scc_var(const zend_op_array *op_array, zend_ssa *ssa, int var, int *index, zend_worklist_stack *stack, zend_worklist_stack *vstack, zend_scc_iterator *iterators) /* {{{ */
387
0
{
388
0
restart:
389
0
  zend_worklist_stack_push(vstack, var);
390
0
  iterators[var].state = 0;
391
0
  iterators[var].last = -1;
392
0
  ssa->vars[var].scc_entry = 1;
393
0
  ssa->vars[var].scc = *index;
394
0
  (*index)++;
395
396
0
  while (vstack->len > 0) {
397
0
    var = zend_worklist_stack_peek(vstack);
398
0
    while (1) {
399
0
      int var2;
400
401
0
      if (iterators[var].last >= 0) {
402
        /* finish edge */
403
0
        var2 = iterators[var].last;
404
0
        if (ssa->vars[var2].scc < ssa->vars[var].scc) {
405
0
          ssa->vars[var].scc = ssa->vars[var2].scc;
406
0
          ssa->vars[var].scc_entry = 0;
407
0
        }
408
0
      }
409
0
      var2 = zend_scc_next(op_array, ssa, var, iterators + var);
410
0
      iterators[var].last = var2;
411
0
      if (var2 < 0) break;
412
      /* begin edge */
413
0
      if (ssa->vars[var2].scc < 0) {
414
0
        var = var2;
415
0
        goto restart;
416
0
      }
417
0
    }
418
419
    /* finish visiting */
420
0
    zend_worklist_stack_pop(vstack);
421
0
    if (ssa->vars[var].scc_entry) {
422
0
      ssa->sccs--;
423
0
      while (stack->len > 0) {
424
0
        int var2 = zend_worklist_stack_peek(stack);
425
0
        if (ssa->vars[var2].scc < ssa->vars[var].scc) {
426
0
          break;
427
0
        }
428
0
        zend_worklist_stack_pop(stack);
429
0
        ssa->vars[var2].scc = ssa->sccs;
430
0
        (*index)--;
431
0
      }
432
0
      ssa->vars[var].scc = ssa->sccs;
433
0
      (*index)--;
434
0
    } else {
435
0
      zend_worklist_stack_push(stack, var);
436
0
    }
437
0
  }
438
0
}
439
/* }}} */
440
441
ZEND_API void zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
442
1
{
443
1
  int index = 0;
444
1
  zend_worklist_stack stack, vstack;
445
1
  zend_scc_iterator *iterators;
446
1
  int j;
447
1
  ALLOCA_FLAG(stack_use_heap)
448
1
  ALLOCA_FLAG(vstack_use_heap)
449
1
  ALLOCA_FLAG(iterators_use_heap)
450
451
1
  iterators = do_alloca(sizeof(zend_scc_iterator) * ssa->vars_count, iterators_use_heap);
452
1
  ZEND_WORKLIST_STACK_ALLOCA(&vstack, ssa->vars_count, vstack_use_heap);
453
1
  ZEND_WORKLIST_STACK_ALLOCA(&stack, ssa->vars_count, stack_use_heap);
454
455
  /* Find SCCs using Pearce's algorithm. */
456
1
  ssa->sccs = ssa->vars_count;
457
1
  for (j = 0; j < ssa->vars_count; j++) {
458
0
    if (!ssa->vars[j].no_val && ssa->vars[j].scc < 0) {
459
0
      zend_ssa_check_scc_var(op_array, ssa, j, &index, &stack, &vstack, iterators);
460
0
    }
461
0
  }
462
463
1
  if (ssa->sccs) {
464
    /* Shift SCC indexes. */
465
0
    for (j = 0; j < ssa->vars_count; j++) {
466
0
      if (ssa->vars[j].scc >= 0) {
467
0
        ssa->vars[j].scc -= ssa->sccs;
468
0
      }
469
0
    }
470
0
  }
471
1
  ssa->sccs = ssa->vars_count - ssa->sccs;
472
473
1
  for (j = 0; j < ssa->vars_count; j++) {
474
0
    if (ssa->vars[j].scc >= 0) {
475
0
      int var = j;
476
0
      FOR_EACH_VAR_USAGE(var, CHECK_SCC_ENTRY);
477
0
    }
478
0
  }
479
480
1
  ZEND_WORKLIST_STACK_FREE_ALLOCA(&stack, stack_use_heap);
481
1
  ZEND_WORKLIST_STACK_FREE_ALLOCA(&vstack, vstack_use_heap);
482
1
  free_alloca(iterators, iterators_use_heap);
483
1
}
484
/* }}} */
485
486
#endif
487
488
ZEND_API void zend_ssa_find_false_dependencies(const zend_op_array *op_array, const zend_ssa *ssa) /* {{{ */
489
1
{
490
1
  zend_ssa_var *ssa_vars = ssa->vars;
491
1
  const zend_ssa_op *ssa_ops = ssa->ops;
492
1
  int ssa_vars_count = ssa->vars_count;
493
1
  zend_bitset worklist;
494
1
  int i, use;
495
1
  const zend_ssa_phi *p;
496
1
  ALLOCA_FLAG(use_heap);
497
498
1
  if (!op_array->function_name || !ssa->vars || !ssa->ops) {
499
1
    return;
500
1
  }
501
502
0
  worklist = do_alloca(sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count), use_heap);
503
0
  memset(worklist, 0, sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count));
504
505
0
  for (i = 0; i < ssa_vars_count; i++) {
506
0
    ssa_vars[i].no_val = 1; /* mark as unused */
507
0
    use = ssa->vars[i].use_chain;
508
0
    while (use >= 0) {
509
0
      if (!zend_ssa_is_no_val_use(&op_array->opcodes[use], &ssa->ops[use], i)) {
510
0
        ssa_vars[i].no_val = 0; /* used directly */
511
0
        zend_bitset_incl(worklist, i);
512
0
        break;
513
0
      }
514
0
      use = zend_ssa_next_use(ssa_ops, i, use);
515
0
    }
516
0
  }
517
518
0
  WHILE_WORKLIST(worklist, zend_bitset_len(ssa_vars_count), i) {
519
0
    if (ssa_vars[i].definition_phi) {
520
      /* mark all possible sources as used */
521
0
      p = ssa_vars[i].definition_phi;
522
0
      if (p->pi >= 0) {
523
0
        if (ssa_vars[p->sources[0]].no_val) {
524
0
          ssa_vars[p->sources[0]].no_val = 0; /* used indirectly */
525
0
          zend_bitset_incl(worklist, p->sources[0]);
526
0
        }
527
0
      } else {
528
0
        for (uint32_t j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
529
0
          ZEND_ASSERT(p->sources[j] >= 0);
530
0
          if (ssa->vars[p->sources[j]].no_val) {
531
0
            ssa_vars[p->sources[j]].no_val = 0; /* used indirectly */
532
0
            zend_bitset_incl(worklist, p->sources[j]);
533
0
          }
534
0
        }
535
0
      }
536
0
    }
537
0
  } WHILE_WORKLIST_END();
538
539
0
  free_alloca(worklist, use_heap);
540
0
}
541
/* }}} */
542
543
/* From "Hacker's Delight" */
544
static zend_ulong minOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
545
0
{
546
0
  zend_ulong m, temp;
547
548
0
  m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
549
0
  while (m != 0) {
550
0
    if (~a & c & m) {
551
0
      temp = (a | m) & -m;
552
0
      if (temp <= b) {
553
0
        a = temp;
554
0
        break;
555
0
      }
556
0
    } else if (a & ~c & m) {
557
0
      temp = (c | m) & -m;
558
0
      if (temp <= d) {
559
0
        c = temp;
560
0
        break;
561
0
      }
562
0
    }
563
0
    m = m >> 1;
564
0
  }
565
0
  return a | c;
566
0
}
567
568
static zend_ulong maxOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
569
0
{
570
0
  zend_ulong m, temp;
571
572
0
  m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
573
0
  while (m != 0) {
574
0
    if (b & d & m) {
575
0
      temp = (b - m) | (m - 1);
576
0
      if (temp >= a) {
577
0
        b = temp;
578
0
        break;
579
0
      }
580
0
      temp = (d - m) | (m - 1);
581
0
      if (temp >= c) {
582
0
        d = temp;
583
0
        break;
584
0
      }
585
0
    }
586
0
    m = m >> 1;
587
0
  }
588
0
  return b | d;
589
0
}
590
591
static zend_ulong minAND(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
592
0
{
593
0
  zend_ulong m, temp;
594
595
0
  m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
596
0
  while (m != 0) {
597
0
    if (~a & ~c & m) {
598
0
      temp = (a | m) & -m;
599
0
      if (temp <= b) {
600
0
        a = temp;
601
0
        break;
602
0
      }
603
0
      temp = (c | m) & -m;
604
0
      if (temp <= d) {
605
0
        c = temp;
606
0
        break;
607
0
      }
608
0
    }
609
0
    m = m >> 1;
610
0
  }
611
0
  return a & c;
612
0
}
613
614
static zend_ulong maxAND(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
615
0
{
616
0
  zend_ulong m, temp;
617
618
0
  m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
619
0
  while (m != 0) {
620
0
    if (b & ~d & m) {
621
0
      temp = (b | ~m) | (m - 1);
622
0
      if (temp >= a) {
623
0
        b = temp;
624
0
        break;
625
0
      }
626
0
    } else if (~b & d & m) {
627
0
      temp = (d | ~m) | (m - 1);
628
0
      if (temp >= c) {
629
0
        d = temp;
630
0
        break;
631
0
      }
632
0
    }
633
0
    m = m >> 1;
634
0
  }
635
0
  return b & d;
636
0
}
637
638
/* Based on "Hacker's Delight" */
639
640
/*
641
0: + + + + 0 0 0 0 => 0 0 + min/max
642
2: + + - + 0 0 1 0 => 1 0 ? min(a,b,c,-1)/max(a,b,0,d)
643
3: + + - - 0 0 1 1 => 1 1 - min/max
644
8: - + + + 1 0 0 0 => 1 0 ? min(a,-1,b,d)/max(0,b,c,d)
645
a: - + - + 1 0 1 0 => 1 0 ? MIN(a,c)/max(0,b,0,d)
646
b: - + - - 1 0 1 1 => 1 1 - c/-1
647
c: - - + + 1 1 0 0 => 1 1 - min/max
648
e: - - - + 1 1 1 0 => 1 1 - a/-1
649
f  - - - - 1 1 1 1 => 1 1 - min/max
650
*/
651
static void zend_ssa_range_or(zend_long a, zend_long b, zend_long c, zend_long d, zend_ssa_range *tmp)
652
0
{
653
0
  int x = ((a < 0) ? 8 : 0) |
654
0
          ((b < 0) ? 4 : 0) |
655
0
          ((c < 0) ? 2 : 0) |
656
0
          ((d < 0) ? 1 : 0);
657
0
  switch (x) {
658
0
    case 0x0:
659
0
    case 0x3:
660
0
    case 0xc:
661
0
    case 0xf:
662
0
      tmp->min = minOR(a, b, c, d);
663
0
      tmp->max = maxOR(a, b, c, d);
664
0
      break;
665
0
    case 0x2:
666
0
      tmp->min = minOR(a, b, c, -1);
667
0
      tmp->max = maxOR(a, b, 0, d);
668
0
      break;
669
0
    case 0x8:
670
0
      tmp->min = minOR(a, -1, c, d);
671
0
      tmp->max = maxOR(0, b, c, d);
672
0
      break;
673
0
    case 0xa:
674
0
      tmp->min = MIN(a, c);
675
0
      tmp->max = maxOR(0, b, 0, d);
676
0
      break;
677
0
    case 0xb:
678
0
      tmp->min = c;
679
0
      tmp->max = -1;
680
0
      break;
681
0
    case 0xe:
682
0
      tmp->min = a;
683
0
      tmp->max = -1;
684
0
      break;
685
0
  }
686
0
}
687
688
/*
689
0: + + + + 0 0 0 0 => 0 0 + min/max
690
2: + + - + 0 0 1 0 => 0 0 + 0/b
691
3: + + - - 0 0 1 1 => 0 0 + min/max
692
8: - + + + 1 0 0 0 => 0 0 + 0/d
693
a: - + - + 1 0 1 0 => 1 0 ? min(a,-1,c,-1)/NAX(b,d)
694
b: - + - - 1 0 1 1 => 1 0 ? min(a,-1,c,d)/max(0,b,c,d)
695
c: - - + + 1 1 0 0 => 1 1 - min/max
696
e: - - - + 1 1 1 0 => 1 0 ? min(a,b,c,-1)/max(a,b,0,d)
697
f  - - - - 1 1 1 1 => 1 1 - min/max
698
*/
699
static void zend_ssa_range_and(zend_long a, zend_long b, zend_long c, zend_long d, zend_ssa_range *tmp)
700
0
{
701
0
  int x = ((a < 0) ? 8 : 0) |
702
0
          ((b < 0) ? 4 : 0) |
703
0
          ((c < 0) ? 2 : 0) |
704
0
          ((d < 0) ? 1 : 0);
705
0
  switch (x) {
706
0
    case 0x0:
707
0
    case 0x3:
708
0
    case 0xc:
709
0
    case 0xf:
710
0
      tmp->min = minAND(a, b, c, d);
711
0
      tmp->max = maxAND(a, b, c, d);
712
0
      break;
713
0
    case 0x2:
714
0
      tmp->min = 0;
715
0
      tmp->max = b;
716
0
      break;
717
0
    case 0x8:
718
0
      tmp->min = 0;
719
0
      tmp->max = d;
720
0
      break;
721
0
    case 0xa:
722
0
      tmp->min = minAND(a, -1, c, -1);
723
0
      tmp->max = MAX(b, d);
724
0
      break;
725
0
    case 0xb:
726
0
      tmp->min = minAND(a, -1, c, d);
727
0
      tmp->max = maxAND(0, b, c, d);
728
0
      break;
729
0
    case 0xe:
730
0
      tmp->min = minAND(a, b, c, -1);
731
0
      tmp->max = maxAND(a, b, 0, d);
732
0
      break;
733
0
  }
734
0
}
735
736
static inline bool zend_abs_range(
737
0
    zend_long min, zend_long max, zend_long *abs_min, zend_long *abs_max) {
738
0
  if (min == ZEND_LONG_MIN) {
739
    /* Cannot take absolute value of LONG_MIN  */
740
0
    return 0;
741
0
  }
742
743
0
  if (min >= 0) {
744
0
    *abs_min = min;
745
0
    *abs_max = max;
746
0
  } else if (max <= 0) {
747
0
    *abs_min = -max;
748
0
    *abs_max = -min;
749
0
  } else {
750
    /* Range crossing zero */
751
0
    *abs_min = 0;
752
0
    *abs_max = MAX(max, -min);
753
0
  }
754
755
0
  return 1;
756
0
}
757
758
0
static inline zend_long safe_shift_left(zend_long n, zend_long s) {
759
0
  return (zend_long) ((zend_ulong) n << (zend_ulong) s);
760
0
}
761
762
0
static inline bool shift_left_overflows(zend_long n, zend_long s) {
763
  /* This considers shifts that shift in the sign bit to be overflowing as well */
764
0
  if (n >= 0) {
765
0
    return s >= SIZEOF_ZEND_LONG * 8 - 1 || safe_shift_left(n, s) < n;
766
0
  } else {
767
0
    return s >= SIZEOF_ZEND_LONG * 8 || safe_shift_left(n, s) > n;
768
0
  }
769
0
}
770
771
/* If b does not divide a exactly, return the two adjacent values between which the real result
772
 * lies. */
773
0
static void float_div(zend_long a, zend_long b, zend_long *r1, zend_long *r2) {
774
0
  *r1 = *r2 = a / b;
775
0
  if (a % b != 0) {
776
0
    if (*r2 < 0) {
777
0
      (*r2)--;
778
0
    } else {
779
0
      (*r2)++;
780
0
    }
781
0
  }
782
0
}
783
784
static bool zend_inference_calc_binary_op_range(
785
    const zend_op_array *op_array, const zend_ssa *ssa,
786
0
    const zend_op *opline, const zend_ssa_op *ssa_op, uint8_t opcode, zend_ssa_range *tmp) {
787
0
  zend_long op1_min, op2_min, op1_max, op2_max, t1, t2, t3, t4;
788
789
0
  switch (opcode) {
790
0
    case ZEND_ADD:
791
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
792
0
        op1_min = OP1_MIN_RANGE();
793
0
        op2_min = OP2_MIN_RANGE();
794
0
        op1_max = OP1_MAX_RANGE();
795
0
        op2_max = OP2_MAX_RANGE();
796
0
        if (OP1_RANGE_UNDERFLOW() ||
797
0
          OP2_RANGE_UNDERFLOW() ||
798
0
          zend_add_will_overflow(op1_min, op2_min)) {
799
0
          tmp->underflow = 1;
800
0
          tmp->min = ZEND_LONG_MIN;
801
0
        } else {
802
0
          tmp->min = op1_min + op2_min;
803
0
        }
804
0
        if (OP1_RANGE_OVERFLOW() ||
805
0
          OP2_RANGE_OVERFLOW() ||
806
0
          zend_add_will_overflow(op1_max, op2_max)) {
807
0
          tmp->overflow = 1;
808
0
          tmp->max = ZEND_LONG_MAX;
809
0
        } else {
810
0
          tmp->max = op1_max + op2_max;
811
0
        }
812
0
        return 1;
813
0
      }
814
0
      break;
815
0
    case ZEND_SUB:
816
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
817
0
        op1_min = OP1_MIN_RANGE();
818
0
        op2_min = OP2_MIN_RANGE();
819
0
        op1_max = OP1_MAX_RANGE();
820
0
        op2_max = OP2_MAX_RANGE();
821
0
        if (OP1_RANGE_UNDERFLOW() ||
822
0
          OP2_RANGE_OVERFLOW() ||
823
0
          zend_sub_will_overflow(op1_min, op2_max)) {
824
0
          tmp->underflow = 1;
825
0
          tmp->min = ZEND_LONG_MIN;
826
0
        } else {
827
0
          tmp->min = op1_min - op2_max;
828
0
        }
829
0
        if (OP1_RANGE_OVERFLOW() ||
830
0
          OP2_RANGE_UNDERFLOW() ||
831
0
          zend_sub_will_overflow(op1_max, op2_min)) {
832
0
          tmp->overflow = 1;
833
0
          tmp->max = ZEND_LONG_MAX;
834
0
        } else {
835
0
          tmp->max = op1_max - op2_min;
836
0
        }
837
0
        return 1;
838
0
      }
839
0
      break;
840
0
    case ZEND_MUL:
841
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
842
0
        double dummy;
843
0
        zend_long t1_overflow, t2_overflow, t3_overflow, t4_overflow;
844
0
        op1_min = OP1_MIN_RANGE();
845
0
        op2_min = OP2_MIN_RANGE();
846
0
        op1_max = OP1_MAX_RANGE();
847
0
        op2_max = OP2_MAX_RANGE();
848
        /* Suppress uninit variable warnings, these will only be used if the overflow
849
         * flags are all false. */
850
0
        t1 = t2 = t3 = t4 = 0;
851
0
        ZEND_SIGNED_MULTIPLY_LONG(op1_min, op2_min, t1, dummy, t1_overflow);
852
0
        ZEND_SIGNED_MULTIPLY_LONG(op1_min, op2_max, t2, dummy, t2_overflow);
853
0
        ZEND_SIGNED_MULTIPLY_LONG(op1_max, op2_min, t3, dummy, t3_overflow);
854
0
        ZEND_SIGNED_MULTIPLY_LONG(op1_max, op2_max, t4, dummy, t4_overflow);
855
0
        (void) dummy;
856
857
        // FIXME: more careful overflow checks?
858
0
        if (OP1_RANGE_UNDERFLOW() || OP2_RANGE_UNDERFLOW() ||
859
0
          OP1_RANGE_OVERFLOW() || OP2_RANGE_OVERFLOW()  ||
860
0
          t1_overflow || t2_overflow || t3_overflow || t4_overflow
861
0
        ) {
862
0
          tmp->underflow = 1;
863
0
          tmp->overflow = 1;
864
0
          tmp->min = ZEND_LONG_MIN;
865
0
          tmp->max = ZEND_LONG_MAX;
866
0
        } else {
867
0
          tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
868
0
          tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
869
0
        }
870
0
        return 1;
871
0
      }
872
0
      break;
873
0
    case ZEND_DIV:
874
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
875
0
        op1_min = OP1_MIN_RANGE();
876
0
        op2_min = OP2_MIN_RANGE();
877
0
        op1_max = OP1_MAX_RANGE();
878
0
        op2_max = OP2_MAX_RANGE();
879
880
        /* If op2 crosses zero, then floating point values close to zero might be
881
         * possible, which will result in arbitrarily large results (overflow). Also
882
         * avoid dividing LONG_MIN by -1, which is UB. */
883
0
        if (OP1_RANGE_UNDERFLOW() || OP2_RANGE_UNDERFLOW() ||
884
0
          OP1_RANGE_OVERFLOW() || OP2_RANGE_OVERFLOW() ||
885
0
          (op2_min <= 0 && op2_max >= 0) ||
886
0
          (op1_min == ZEND_LONG_MIN && op2_max == -1)
887
0
        ) {
888
0
          tmp->underflow = 1;
889
0
          tmp->overflow = 1;
890
0
          tmp->min = ZEND_LONG_MIN;
891
0
          tmp->max = ZEND_LONG_MAX;
892
0
        } else {
893
0
          zend_long t1_, t2_, t3_, t4_;
894
0
          float_div(op1_min, op2_min, &t1, &t1_);
895
0
          float_div(op1_min, op2_max, &t2, &t2_);
896
0
          float_div(op1_max, op2_min, &t3, &t3_);
897
0
          float_div(op1_max, op2_max, &t4, &t4_);
898
899
0
          tmp->min = MIN(MIN(MIN(t1, t2), MIN(t3, t4)), MIN(MIN(t1_, t2_), MIN(t3_, t4_)));
900
0
          tmp->max = MAX(MAX(MAX(t1, t2), MAX(t3, t4)), MAX(MAX(t1_, t2_), MAX(t3_, t4_)));
901
0
        }
902
0
        return 1;
903
0
      }
904
0
      break;
905
0
    case ZEND_MOD:
906
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
907
0
        if (OP1_RANGE_UNDERFLOW() ||
908
0
          OP2_RANGE_UNDERFLOW() ||
909
0
          OP1_RANGE_OVERFLOW()  ||
910
0
          OP2_RANGE_OVERFLOW()) {
911
0
          tmp->min = ZEND_LONG_MIN;
912
0
          tmp->max = ZEND_LONG_MAX;
913
0
        } else {
914
0
          zend_long op2_abs_min, op2_abs_max;
915
916
0
          op1_min = OP1_MIN_RANGE();
917
0
          op2_min = OP2_MIN_RANGE();
918
0
          op1_max = OP1_MAX_RANGE();
919
0
          op2_max = OP2_MAX_RANGE();
920
0
          if (!zend_abs_range(op2_min, op2_max, &op2_abs_min, &op2_abs_max)) {
921
0
            break;
922
0
          }
923
924
0
          if (op2_abs_max == 0) {
925
            /* Always modulus by zero, nothing we can do */
926
0
            break;
927
0
          }
928
0
          if (op2_abs_min == 0) {
929
            /* Ignore the modulus by zero case, which will throw */
930
0
            op2_abs_min++;
931
0
          }
932
933
0
          if (op1_min >= 0) {
934
0
            tmp->min = op1_max < op2_abs_min ? op1_min : 0;
935
0
            tmp->max = MIN(op1_max, op2_abs_max - 1);
936
0
          } else if (op1_max <= 0) {
937
0
            tmp->min = MAX(op1_min, -op2_abs_max + 1);
938
0
            tmp->max = op1_min > -op2_abs_min ? op1_max : 0;
939
0
          } else {
940
0
            tmp->min = MAX(op1_min, -op2_abs_max + 1);
941
0
            tmp->max = MIN(op1_max, op2_abs_max - 1);
942
0
          }
943
0
        }
944
0
        return 1;
945
0
      }
946
0
      break;
947
0
    case ZEND_SL:
948
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
949
0
        if (OP1_RANGE_UNDERFLOW() ||
950
0
          OP2_RANGE_UNDERFLOW() ||
951
0
          OP1_RANGE_OVERFLOW() ||
952
0
          OP2_RANGE_OVERFLOW()) {
953
0
          tmp->min = ZEND_LONG_MIN;
954
0
          tmp->max = ZEND_LONG_MAX;
955
0
        } else {
956
0
          op1_min = OP1_MIN_RANGE();
957
0
          op2_min = OP2_MIN_RANGE();
958
0
          op1_max = OP1_MAX_RANGE();
959
0
          op2_max = OP2_MAX_RANGE();
960
961
          /* Shifts by negative numbers will throw, ignore them */
962
0
          if (op2_min < 0) {
963
0
            op2_min = 0;
964
0
          }
965
0
          if (op2_max < 0) {
966
0
            op2_max = 0;
967
0
          }
968
969
0
          if (shift_left_overflows(op1_min, op2_max)
970
0
              || shift_left_overflows(op1_max, op2_max)) {
971
0
            tmp->min = ZEND_LONG_MIN;
972
0
            tmp->max = ZEND_LONG_MAX;
973
0
          } else {
974
0
            t1 = safe_shift_left(op1_min, op2_min);
975
0
            t2 = safe_shift_left(op1_min, op2_max);
976
0
            t3 = safe_shift_left(op1_max, op2_min);
977
0
            t4 = safe_shift_left(op1_max, op2_max);
978
0
            tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
979
0
            tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
980
0
          }
981
0
        }
982
0
        return 1;
983
0
      }
984
0
      break;
985
0
    case ZEND_SR:
986
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
987
0
        if (OP1_RANGE_UNDERFLOW() ||
988
0
          OP2_RANGE_UNDERFLOW() ||
989
0
          OP1_RANGE_OVERFLOW() ||
990
0
          OP2_RANGE_OVERFLOW()) {
991
0
          tmp->min = ZEND_LONG_MIN;
992
0
          tmp->max = ZEND_LONG_MAX;
993
0
        } else {
994
0
          op1_min = OP1_MIN_RANGE();
995
0
          op2_min = OP2_MIN_RANGE();
996
0
          op1_max = OP1_MAX_RANGE();
997
0
          op2_max = OP2_MAX_RANGE();
998
999
          /* Shifts by negative numbers will throw, ignore them */
1000
0
          if (op2_min < 0) {
1001
0
            op2_min = 0;
1002
0
          }
1003
0
          if (op2_max < 0) {
1004
0
            op2_max = 0;
1005
0
          }
1006
1007
          /* Shifts by more than the integer size will be 0 or -1 */
1008
0
          if (op2_min >= SIZEOF_ZEND_LONG * 8) {
1009
0
            op2_min = SIZEOF_ZEND_LONG * 8 - 1;
1010
0
          }
1011
0
          if (op2_max >= SIZEOF_ZEND_LONG * 8) {
1012
0
            op2_max = SIZEOF_ZEND_LONG * 8 - 1;
1013
0
          }
1014
1015
0
          t1 = op1_min >> op2_min;
1016
0
          t2 = op1_min >> op2_max;
1017
0
          t3 = op1_max >> op2_min;
1018
0
          t4 = op1_max >> op2_max;
1019
0
          tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
1020
0
          tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
1021
0
        }
1022
0
        return 1;
1023
0
      }
1024
0
      break;
1025
0
    case ZEND_BW_OR:
1026
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1027
0
        if (OP1_RANGE_UNDERFLOW() ||
1028
0
          OP2_RANGE_UNDERFLOW() ||
1029
0
          OP1_RANGE_OVERFLOW() ||
1030
0
          OP2_RANGE_OVERFLOW()) {
1031
0
          tmp->min = ZEND_LONG_MIN;
1032
0
          tmp->max = ZEND_LONG_MAX;
1033
0
        } else {
1034
0
          op1_min = OP1_MIN_RANGE();
1035
0
          op2_min = OP2_MIN_RANGE();
1036
0
          op1_max = OP1_MAX_RANGE();
1037
0
          op2_max = OP2_MAX_RANGE();
1038
0
          zend_ssa_range_or(op1_min, op1_max, op2_min, op2_max, tmp);
1039
0
        }
1040
0
        return 1;
1041
0
      }
1042
0
      break;
1043
0
    case ZEND_BW_AND:
1044
0
      if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1045
0
        if (OP1_RANGE_UNDERFLOW() ||
1046
0
          OP2_RANGE_UNDERFLOW() ||
1047
0
          OP1_RANGE_OVERFLOW() ||
1048
0
          OP2_RANGE_OVERFLOW()) {
1049
0
          tmp->min = ZEND_LONG_MIN;
1050
0
          tmp->max = ZEND_LONG_MAX;
1051
0
        } else {
1052
0
          op1_min = OP1_MIN_RANGE();
1053
0
          op2_min = OP2_MIN_RANGE();
1054
0
          op1_max = OP1_MAX_RANGE();
1055
0
          op2_max = OP2_MAX_RANGE();
1056
0
          zend_ssa_range_and(op1_min, op1_max, op2_min, op2_max, tmp);
1057
0
        }
1058
0
        return 1;
1059
0
      }
1060
0
      break;
1061
0
    case ZEND_BW_XOR:
1062
      // TODO
1063
0
      break;
1064
0
    default: ZEND_UNREACHABLE();
1065
0
  }
1066
0
  return 0;
1067
0
}
1068
1069
static bool zend_inference_calc_range(const zend_op_array *op_array, const zend_ssa *ssa, int var, int widening, bool narrowing, zend_ssa_range *tmp)
1070
0
{
1071
0
  uint32_t line;
1072
0
  const zend_op *opline;
1073
0
  const zend_ssa_op *ssa_op;
1074
1075
0
  if (ssa->vars[var].definition_phi) {
1076
0
    const zend_ssa_phi *p = ssa->vars[var].definition_phi;
1077
1078
0
    tmp->underflow = 0;
1079
0
    tmp->min = ZEND_LONG_MAX;
1080
0
    tmp->max = ZEND_LONG_MIN;
1081
0
    tmp->overflow = 0;
1082
0
    if (p->pi >= 0 && p->has_range_constraint) {
1083
0
      const zend_ssa_range_constraint *constraint = &p->constraint.range;
1084
0
      if (constraint->negative) {
1085
0
        int src1 = p->sources[0];
1086
1087
0
        if (ssa->var_info[src1].has_range) {
1088
0
          *tmp = ssa->var_info[src1].range;
1089
0
          if (constraint->range.min == constraint->range.max
1090
0
           && !constraint->range.underflow
1091
0
           && !constraint->range.overflow
1092
0
           && p->constraint.range.min_ssa_var < 0
1093
0
           && p->constraint.range.max_ssa_var < 0
1094
0
           && ssa->vars[src1].definition >= 0) {
1095
            /* Check for constrained induction variable */
1096
0
            line = ssa->vars[src1].definition;
1097
0
            opline = op_array->opcodes + line;
1098
0
            switch (opline->opcode) {
1099
0
              case ZEND_PRE_DEC:
1100
0
              case ZEND_POST_DEC:
1101
0
                if (!tmp->underflow) {
1102
0
                  const zend_ssa_phi *p = ssa->vars[ssa->ops[line].op1_use].definition_phi;
1103
1104
0
                  if (p && p->pi < 0
1105
0
                   && ssa->cfg.blocks[p->block].predecessors_count == 2
1106
0
                   && p->sources[1] == var
1107
0
                   && ssa->var_info[p->sources[0]].has_range
1108
0
                   && ssa->var_info[p->sources[0]].range.min > constraint->range.max) {
1109
0
                    tmp->min = constraint->range.max + 1;
1110
0
                  }
1111
0
                }
1112
0
                break;
1113
0
              case ZEND_PRE_INC:
1114
0
              case ZEND_POST_INC:
1115
0
                if (!tmp->overflow) {
1116
0
                  const zend_ssa_phi *p = ssa->vars[ssa->ops[line].op1_use].definition_phi;
1117
1118
0
                  if (p && p->pi < 0
1119
0
                   && ssa->cfg.blocks[p->block].predecessors_count == 2
1120
0
                   && p->sources[1] == var
1121
0
                   && ssa->var_info[p->sources[0]].has_range
1122
0
                   && ssa->var_info[p->sources[0]].range.max < constraint->range.min) {
1123
0
                    tmp->max = constraint->range.min - 1;
1124
0
                  }
1125
0
                }
1126
0
                break;
1127
0
            }
1128
0
          }
1129
0
        } else if (narrowing) {
1130
0
          tmp->underflow = 1;
1131
0
          tmp->min = ZEND_LONG_MIN;
1132
0
          tmp->max = ZEND_LONG_MAX;
1133
0
          tmp->overflow = 1;
1134
0
        }
1135
1136
#ifdef NEG_RANGE
1137
        if (constraint->min_ssa_var < 0 &&
1138
            constraint->max_ssa_var < 0 &&
1139
            ssa->var_info[p->ssa_var].has_range) {
1140
          LOG_NEG_RANGE("%s() #%d [%ld..%ld] -> [%ld..%ld]?\n",
1141
            ZSTR_VAL(op_array->function_name),
1142
            p->ssa_var,
1143
            ssa->var_info[p->ssa_var].range.min,
1144
            ssa->var_info[p->ssa_var].range.max,
1145
            tmp->min,
1146
            tmp->max);
1147
          if (constraint->negative == NEG_USE_LT &&
1148
              tmp->max >= constraint->range.min) {
1149
            tmp->overflow = 0;
1150
            tmp->max = constraint->range.min - 1;
1151
            LOG_NEG_RANGE("  => [%ld..%ld]\n", tmp->min, tmp->max);
1152
          } else if (constraint->negative == NEG_USE_GT &&
1153
                     tmp->min <= constraint->range.max) {
1154
            tmp->underflow = 0;
1155
            tmp->min = constraint->range.max + 1;
1156
            LOG_NEG_RANGE("  => [%ld..%ld]\n", tmp->min, tmp->max);
1157
          }
1158
        }
1159
#endif
1160
0
      } else if (ssa->var_info[p->sources[0]].has_range) {
1161
        /* intersection */
1162
0
        *tmp = ssa->var_info[p->sources[0]].range;
1163
0
        if (constraint->min_ssa_var < 0) {
1164
0
          tmp->underflow = constraint->range.underflow && tmp->underflow;
1165
0
          tmp->min = MAX(constraint->range.min, tmp->min);
1166
0
#ifdef SYM_RANGE
1167
0
        } else if (narrowing && ssa->var_info[constraint->min_ssa_var].has_range) {
1168
0
          tmp->underflow = ssa->var_info[constraint->min_ssa_var].range.underflow && tmp->underflow;
1169
0
          if (!add_will_overflow(ssa->var_info[constraint->min_ssa_var].range.min, constraint->range.min)) {
1170
0
            tmp->min = MAX(ssa->var_info[constraint->min_ssa_var].range.min + constraint->range.min, tmp->min);
1171
0
          }
1172
0
#endif
1173
0
        }
1174
0
        if (constraint->max_ssa_var < 0) {
1175
0
          tmp->max = MIN(constraint->range.max, tmp->max);
1176
0
          tmp->overflow = constraint->range.overflow && tmp->overflow;
1177
0
#ifdef SYM_RANGE
1178
0
        } else if (narrowing && ssa->var_info[constraint->max_ssa_var].has_range) {
1179
0
          if (!add_will_overflow(ssa->var_info[constraint->max_ssa_var].range.max, constraint->range.max)) {
1180
0
            tmp->max = MIN(ssa->var_info[constraint->max_ssa_var].range.max + constraint->range.max, tmp->max);
1181
0
          }
1182
0
          tmp->overflow = ssa->var_info[constraint->max_ssa_var].range.overflow && tmp->overflow;
1183
0
#endif
1184
0
        }
1185
0
      } else if (narrowing) {
1186
0
        if (constraint->min_ssa_var < 0) {
1187
0
          tmp->underflow = constraint->range.underflow;
1188
0
          tmp->min = constraint->range.min;
1189
0
#ifdef SYM_RANGE
1190
0
        } else if (narrowing && ssa->var_info[constraint->min_ssa_var].has_range) {
1191
0
          if (add_will_overflow(ssa->var_info[constraint->min_ssa_var].range.min, constraint->range.min)) {
1192
0
            tmp->underflow = 1;
1193
0
            tmp->min = ZEND_LONG_MIN;
1194
0
          } else {
1195
0
            tmp->underflow = ssa->var_info[constraint->min_ssa_var].range.underflow;
1196
0
            tmp->min = ssa->var_info[constraint->min_ssa_var].range.min + constraint->range.min;
1197
0
          }
1198
0
#endif
1199
0
        } else {
1200
0
          tmp->underflow = 1;
1201
0
          tmp->min = ZEND_LONG_MIN;
1202
0
        }
1203
0
        if (constraint->max_ssa_var < 0) {
1204
0
          tmp->max = constraint->range.max;
1205
0
          tmp->overflow = constraint->range.overflow;
1206
0
#ifdef SYM_RANGE
1207
0
        } else if (narrowing && ssa->var_info[constraint->max_ssa_var].has_range) {
1208
0
          if (add_will_overflow(ssa->var_info[constraint->max_ssa_var].range.max, constraint->range.max)) {
1209
0
            tmp->overflow = 1;
1210
0
            tmp->max = ZEND_LONG_MAX;
1211
0
          } else {
1212
0
            tmp->max = ssa->var_info[constraint->max_ssa_var].range.max + constraint->range.max;
1213
0
            tmp->overflow = ssa->var_info[constraint->max_ssa_var].range.overflow;
1214
0
          }
1215
0
#endif
1216
0
        } else {
1217
0
          tmp->max = ZEND_LONG_MAX;
1218
0
          tmp->overflow = 1;
1219
0
        }
1220
0
      }
1221
0
    } else {
1222
0
      for (uint32_t i = 0; i < ssa->cfg.blocks[p->block].predecessors_count; i++) {
1223
0
        ZEND_ASSERT(p->sources[i] >= 0);
1224
0
        if (ssa->var_info[p->sources[i]].has_range) {
1225
          /* union */
1226
0
          tmp->underflow |= ssa->var_info[p->sources[i]].range.underflow;
1227
0
          tmp->min = MIN(tmp->min, ssa->var_info[p->sources[i]].range.min);
1228
0
          tmp->max = MAX(tmp->max, ssa->var_info[p->sources[i]].range.max);
1229
0
          tmp->overflow |= ssa->var_info[p->sources[i]].range.overflow;
1230
0
        } else if (narrowing) {
1231
0
          tmp->underflow = 1;
1232
0
          tmp->min = ZEND_LONG_MIN;
1233
0
          tmp->max = ZEND_LONG_MAX;
1234
0
          tmp->overflow = 1;
1235
0
        }
1236
0
      }
1237
0
    }
1238
0
    return (tmp->min <= tmp->max);
1239
0
  } else if (ssa->vars[var].definition < 0) {
1240
0
    return 0;
1241
0
  }
1242
0
  line = ssa->vars[var].definition;
1243
0
  opline = op_array->opcodes + line;
1244
0
  ssa_op = &ssa->ops[line];
1245
1246
0
  return zend_inference_propagate_range(op_array, ssa, opline, ssa_op, var, tmp);
1247
0
}
1248
1249
ZEND_API bool zend_inference_propagate_range(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op* ssa_op, int var, zend_ssa_range *tmp)
1250
0
{
1251
0
  tmp->underflow = 0;
1252
0
  tmp->overflow = 0;
1253
0
  switch (opline->opcode) {
1254
0
    case ZEND_ADD:
1255
0
    case ZEND_SUB:
1256
0
    case ZEND_MUL:
1257
0
    case ZEND_DIV:
1258
0
    case ZEND_MOD:
1259
0
    case ZEND_SL:
1260
0
    case ZEND_SR:
1261
0
    case ZEND_BW_OR:
1262
0
    case ZEND_BW_AND:
1263
0
    case ZEND_BW_XOR:
1264
0
      if (ssa_op->result_def == var) {
1265
0
        return zend_inference_calc_binary_op_range(
1266
0
          op_array, ssa, opline, ssa_op, opline->opcode, tmp);
1267
0
      }
1268
0
      break;
1269
1270
0
    case ZEND_BW_NOT:
1271
0
      if (ssa_op->result_def == var) {
1272
0
        if (OP1_HAS_RANGE()) {
1273
0
          if (OP1_RANGE_UNDERFLOW() ||
1274
0
              OP1_RANGE_OVERFLOW()) {
1275
0
            tmp->min = ZEND_LONG_MIN;
1276
0
            tmp->max = ZEND_LONG_MAX;
1277
0
          } else {
1278
0
            zend_long op1_min = OP1_MIN_RANGE();
1279
0
            zend_long op1_max = OP1_MAX_RANGE();
1280
0
            tmp->min = ~op1_max;
1281
0
            tmp->max = ~op1_min;
1282
0
          }
1283
0
          return 1;
1284
0
        }
1285
0
      }
1286
0
      break;
1287
0
    case ZEND_CAST:
1288
0
      if (ssa_op->op1_def == var) {
1289
0
        if (ssa_op->op1_def >= 0) {
1290
0
          if (OP1_HAS_RANGE()) {
1291
0
            tmp->underflow = OP1_RANGE_UNDERFLOW();
1292
0
            tmp->min = OP1_MIN_RANGE();
1293
0
            tmp->max = OP1_MAX_RANGE();
1294
0
            tmp->overflow  = OP1_RANGE_OVERFLOW();
1295
0
            return 1;
1296
0
          }
1297
0
        }
1298
0
      } else if (ssa_op->result_def == var) {
1299
0
        if (opline->extended_value == IS_LONG) {
1300
0
          if (OP1_HAS_RANGE()) {
1301
0
            tmp->min = OP1_MIN_RANGE();
1302
0
            tmp->max = OP1_MAX_RANGE();
1303
0
            return 1;
1304
0
          } else {
1305
0
            tmp->min = ZEND_LONG_MIN;
1306
0
            tmp->max = ZEND_LONG_MAX;
1307
0
            return 1;
1308
0
          }
1309
0
        }
1310
0
      }
1311
0
      break;
1312
0
    case ZEND_QM_ASSIGN:
1313
0
    case ZEND_JMP_SET:
1314
0
    case ZEND_COALESCE:
1315
0
    case ZEND_COPY_TMP:
1316
0
      if (ssa_op->op1_def == var) {
1317
0
        if (ssa_op->op1_def >= 0) {
1318
0
          if (OP1_HAS_RANGE()) {
1319
0
            tmp->underflow = OP1_RANGE_UNDERFLOW();
1320
0
            tmp->min = OP1_MIN_RANGE();
1321
0
            tmp->max = OP1_MAX_RANGE();
1322
0
            tmp->overflow  = OP1_RANGE_OVERFLOW();
1323
0
            return 1;
1324
0
          }
1325
0
        }
1326
0
      }
1327
0
      if (ssa_op->result_def == var) {
1328
0
        if (OP1_HAS_RANGE()) {
1329
0
          tmp->min = OP1_MIN_RANGE();
1330
0
          tmp->max = OP1_MAX_RANGE();
1331
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1332
0
          tmp->overflow  = OP1_RANGE_OVERFLOW();
1333
0
          return 1;
1334
0
        }
1335
0
      }
1336
0
      break;
1337
0
    case ZEND_SEND_VAR:
1338
0
      if (ssa_op->op1_def == var) {
1339
0
        if (ssa_op->op1_def >= 0) {
1340
0
          if (OP1_HAS_RANGE()) {
1341
0
            tmp->underflow = OP1_RANGE_UNDERFLOW();
1342
0
            tmp->min = OP1_MIN_RANGE();
1343
0
            tmp->max = OP1_MAX_RANGE();
1344
0
            tmp->overflow  = OP1_RANGE_OVERFLOW();
1345
0
            return 1;
1346
0
          }
1347
0
        }
1348
0
      }
1349
0
      break;
1350
0
    case ZEND_PRE_INC:
1351
0
      if (ssa_op->op1_def == var || ssa_op->result_def == var) {
1352
0
        if (OP1_HAS_RANGE()) {
1353
0
          tmp->min = OP1_MIN_RANGE();
1354
0
          tmp->max = OP1_MAX_RANGE();
1355
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1356
0
          tmp->overflow = OP1_RANGE_OVERFLOW();
1357
0
          if (tmp->max < ZEND_LONG_MAX) {
1358
0
            tmp->max++;
1359
0
          } else {
1360
0
            tmp->overflow = 1;
1361
0
          }
1362
0
          if (tmp->min < ZEND_LONG_MAX && !tmp->underflow) {
1363
0
            tmp->min++;
1364
0
          }
1365
0
          return 1;
1366
0
        }
1367
0
      }
1368
0
      break;
1369
0
    case ZEND_PRE_DEC:
1370
0
      if (ssa_op->op1_def == var || ssa_op->result_def == var) {
1371
0
        if (OP1_HAS_RANGE()) {
1372
0
          tmp->min = OP1_MIN_RANGE();
1373
0
          tmp->max = OP1_MAX_RANGE();
1374
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1375
0
          tmp->overflow = OP1_RANGE_OVERFLOW();
1376
0
          if (tmp->min > ZEND_LONG_MIN) {
1377
0
            tmp->min--;
1378
0
          } else {
1379
0
            tmp->underflow = 1;
1380
0
          }
1381
0
          if (tmp->max > ZEND_LONG_MIN && !tmp->overflow) {
1382
0
            tmp->max--;
1383
0
          }
1384
0
          return 1;
1385
0
        }
1386
0
      }
1387
0
      break;
1388
0
    case ZEND_POST_INC:
1389
0
      if (ssa_op->op1_def == var || ssa_op->result_def == var) {
1390
0
        if (OP1_HAS_RANGE()) {
1391
0
          tmp->min = OP1_MIN_RANGE();
1392
0
          tmp->max = OP1_MAX_RANGE();
1393
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1394
0
          tmp->overflow = OP1_RANGE_OVERFLOW();
1395
0
          if (ssa_op->result_def == var) {
1396
0
            return 1;
1397
0
          }
1398
0
          if (tmp->max < ZEND_LONG_MAX) {
1399
0
            tmp->max++;
1400
0
          } else {
1401
0
            tmp->overflow = 1;
1402
0
          }
1403
0
          if (tmp->min < ZEND_LONG_MAX && !tmp->underflow) {
1404
0
            tmp->min++;
1405
0
          }
1406
0
          return 1;
1407
0
        }
1408
0
      }
1409
0
      break;
1410
0
    case ZEND_POST_DEC:
1411
0
      if (ssa_op->op1_def == var || ssa_op->result_def == var) {
1412
0
        if (OP1_HAS_RANGE()) {
1413
0
          tmp->min = OP1_MIN_RANGE();
1414
0
          tmp->max = OP1_MAX_RANGE();
1415
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1416
0
          tmp->overflow = OP1_RANGE_OVERFLOW();
1417
0
          if (ssa_op->result_def == var) {
1418
0
            return 1;
1419
0
          }
1420
0
          if (tmp->min > ZEND_LONG_MIN) {
1421
0
            tmp->min--;
1422
0
          } else {
1423
0
            tmp->underflow = 1;
1424
0
          }
1425
0
          if (tmp->max > ZEND_LONG_MIN && !tmp->overflow) {
1426
0
            tmp->max--;
1427
0
          }
1428
0
          return 1;
1429
0
        }
1430
0
      }
1431
0
      break;
1432
0
    case ZEND_UNSET_DIM:
1433
0
    case ZEND_UNSET_OBJ:
1434
0
      if (ssa_op->op1_def == var) {
1435
        /* If op1 is scalar, UNSET_DIM and UNSET_OBJ have no effect, so we can keep
1436
         * the previous ranges. */
1437
0
        if (OP1_HAS_RANGE()) {
1438
0
          tmp->min = OP1_MIN_RANGE();
1439
0
          tmp->max = OP1_MAX_RANGE();
1440
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1441
0
          tmp->overflow  = OP1_RANGE_OVERFLOW();
1442
0
          return 1;
1443
0
        }
1444
0
      }
1445
0
      break;
1446
0
    case ZEND_ASSIGN:
1447
0
      if (ssa_op->op1_def == var || ssa_op->op2_def == var || ssa_op->result_def == var) {
1448
0
        if (OP2_HAS_RANGE()) {
1449
0
          tmp->min = OP2_MIN_RANGE();
1450
0
          tmp->max = OP2_MAX_RANGE();
1451
0
          tmp->underflow = OP2_RANGE_UNDERFLOW();
1452
0
          tmp->overflow  = OP2_RANGE_OVERFLOW();
1453
0
          return 1;
1454
0
        }
1455
0
      }
1456
0
      break;
1457
0
    case ZEND_ASSIGN_DIM:
1458
0
    case ZEND_ASSIGN_OBJ:
1459
0
    case ZEND_ASSIGN_STATIC_PROP:
1460
0
    case ZEND_ASSIGN_DIM_OP:
1461
0
    case ZEND_ASSIGN_OBJ_OP:
1462
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
1463
0
      if ((ssa_op+1)->op1_def == var) {
1464
0
        opline++;
1465
0
        ssa_op++;
1466
0
        if (OP1_HAS_RANGE()) {
1467
0
          tmp->min = OP1_MIN_RANGE();
1468
0
          tmp->max = OP1_MAX_RANGE();
1469
0
          tmp->underflow = OP1_RANGE_UNDERFLOW();
1470
0
          tmp->overflow  = OP1_RANGE_OVERFLOW();
1471
0
        }
1472
0
        return 1;
1473
0
      }
1474
0
      break;
1475
0
    case ZEND_ASSIGN_OP:
1476
0
      if (opline->extended_value != ZEND_CONCAT
1477
0
       && opline->extended_value != ZEND_POW) {
1478
0
        if (ssa_op->op1_def == var || ssa_op->result_def == var) {
1479
0
          return zend_inference_calc_binary_op_range(
1480
0
            op_array, ssa, opline, ssa_op,
1481
0
            opline->extended_value, tmp);
1482
0
        }
1483
0
      }
1484
0
      break;
1485
0
    case ZEND_OP_DATA:
1486
0
      if (ssa_op->op1_def == var) {
1487
0
        if ((opline-1)->opcode == ZEND_ASSIGN_DIM ||
1488
0
            (opline-1)->opcode == ZEND_ASSIGN_OBJ ||
1489
0
            (opline-1)->opcode == ZEND_ASSIGN_STATIC_PROP ||
1490
0
            (opline-1)->opcode == ZEND_ASSIGN_DIM_OP ||
1491
0
            (opline-1)->opcode == ZEND_ASSIGN_OBJ_OP ||
1492
0
            (opline-1)->opcode == ZEND_ASSIGN_STATIC_PROP_OP) {
1493
0
          if (OP1_HAS_RANGE()) {
1494
0
            tmp->min = OP1_MIN_RANGE();
1495
0
            tmp->max = OP1_MAX_RANGE();
1496
0
            tmp->underflow = OP1_RANGE_UNDERFLOW();
1497
0
            tmp->overflow  = OP1_RANGE_OVERFLOW();
1498
0
            return 1;
1499
0
          }
1500
0
        }
1501
0
        break;
1502
0
      }
1503
0
      break;
1504
0
    case ZEND_RECV:
1505
0
    case ZEND_RECV_INIT:
1506
0
      if (ssa_op->result_def == var) {
1507
0
        if (op_array->arg_info &&
1508
0
            opline->op1.num <= op_array->num_args) {
1509
0
          zend_type type = op_array->arg_info[opline->op1.num-1].type;
1510
0
          uint32_t mask = ZEND_TYPE_PURE_MASK_WITHOUT_NULL(type);
1511
0
          if (mask == MAY_BE_LONG) {
1512
0
            tmp->underflow = 0;
1513
0
            tmp->min = ZEND_LONG_MIN;
1514
0
            tmp->max = ZEND_LONG_MAX;
1515
0
            tmp->overflow = 0;
1516
0
            return 1;
1517
0
          }
1518
0
        }
1519
0
      }
1520
0
      break;
1521
0
    case ZEND_STRLEN:
1522
0
      if (ssa_op->result_def == var) {
1523
#if SIZEOF_ZEND_LONG == 4
1524
        /* The length of a string is a non-negative integer. However, on 32-bit
1525
         * platforms overflows into negative lengths may occur, so it's better
1526
         * to not assume any particular range. */
1527
        tmp->min = ZEND_LONG_MIN;
1528
#else
1529
0
        tmp->min = 0;
1530
0
#endif
1531
0
        tmp->max = ZEND_LONG_MAX;
1532
0
        return 1;
1533
0
      }
1534
0
      break;
1535
0
    case ZEND_FUNC_NUM_ARGS:
1536
0
      tmp->min = 0;
1537
0
      tmp->max = ZEND_LONG_MAX;
1538
0
      return 1;
1539
0
    case ZEND_COUNT:
1540
      /* count() on Countable objects may return negative numbers */
1541
0
      tmp->min = ZEND_LONG_MIN;
1542
0
      tmp->max = ZEND_LONG_MAX;
1543
0
      return 1;
1544
0
    case ZEND_DO_FCALL:
1545
0
    case ZEND_DO_ICALL:
1546
0
    case ZEND_DO_UCALL:
1547
0
    case ZEND_DO_FCALL_BY_NAME:
1548
0
      if (ssa_op->result_def == var) {
1549
0
        const zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
1550
0
        const zend_call_info *call_info;
1551
0
        if (!func_info || !func_info->call_map) {
1552
0
          break;
1553
0
        }
1554
1555
0
        call_info = func_info->call_map[opline - op_array->opcodes];
1556
0
        if (!call_info || call_info->is_prototype) {
1557
0
          break;
1558
0
        }
1559
0
        if (call_info->callee_func->type == ZEND_USER_FUNCTION) {
1560
0
          func_info = ZEND_FUNC_INFO(&call_info->callee_func->op_array);
1561
0
          if (func_info && func_info->return_info.has_range) {
1562
0
            *tmp = func_info->return_info.range;
1563
0
            return 1;
1564
0
          }
1565
0
        }
1566
//TODO: we can't use type inference for internal functions at this point ???
1567
#if 0
1568
          uint32_t type;
1569
1570
          type = zend_get_func_info(call_info, ssa);
1571
          if (!(type & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG)))) {
1572
            tmp->underflow = 0;
1573
            tmp->min = 0;
1574
            tmp->max = 0;
1575
            tmp->overflow = 0;
1576
            if (type & MAY_BE_LONG) {
1577
              tmp->min = ZEND_LONG_MIN;
1578
              tmp->max = ZEND_LONG_MAX;
1579
            } else if (type & MAY_BE_TRUE) {
1580
              if (!(type & (MAY_BE_NULL|MAY_BE_FALSE))) {
1581
                tmp->min = 1;
1582
              }
1583
              tmp->max = 1;
1584
            }
1585
            return 1;
1586
          }
1587
#endif
1588
0
      }
1589
0
      break;
1590
    // FIXME: support for more opcodes
1591
0
    default:
1592
0
      break;
1593
0
  }
1594
0
  return 0;
1595
0
}
1596
1597
static void zend_inference_init_range(const zend_op_array *op_array, const zend_ssa *ssa, int var, bool underflow, zend_long min, zend_long max, bool overflow)
1598
0
{
1599
0
  if (underflow) {
1600
0
    min = ZEND_LONG_MIN;
1601
0
  }
1602
0
  if (overflow) {
1603
0
    max = ZEND_LONG_MAX;
1604
0
  }
1605
0
  ssa->var_info[var].has_range = 1;
1606
0
  ssa->var_info[var].range.underflow = underflow;
1607
0
  ssa->var_info[var].range.min = min;
1608
0
  ssa->var_info[var].range.max = max;
1609
0
  ssa->var_info[var].range.overflow = overflow;
1610
0
  LOG_SSA_RANGE("  change range (init      SCC %2d) %2d [%s%ld..%ld%s]\n", ssa->vars[var].scc, var, (underflow?"-- ":""), min, max, (overflow?" ++":""));
1611
0
}
1612
1613
static bool zend_inference_widening_meet(zend_ssa_var_info *var_info, zend_ssa_range *r)
1614
0
{
1615
0
  if (!var_info->has_range) {
1616
0
    var_info->has_range = 1;
1617
0
  } else {
1618
0
    if (r->underflow ||
1619
0
        var_info->range.underflow ||
1620
0
        r->min < var_info->range.min) {
1621
0
      r->underflow = 1;
1622
0
      r->min = ZEND_LONG_MIN;
1623
0
    } else {
1624
0
      r->min = var_info->range.min;
1625
0
    }
1626
0
    if (r->overflow ||
1627
0
        var_info->range.overflow ||
1628
0
        r->max > var_info->range.max) {
1629
0
      r->overflow = 1;
1630
0
      r->max = ZEND_LONG_MAX;
1631
0
    } else {
1632
0
      r->max = var_info->range.max;
1633
0
    }
1634
0
    if (var_info->range.min == r->min &&
1635
0
        var_info->range.max == r->max &&
1636
0
        var_info->range.underflow == r->underflow &&
1637
0
        var_info->range.overflow == r->overflow) {
1638
0
      return 0;
1639
0
    }
1640
0
  }
1641
0
  var_info->range = *r;
1642
0
  return 1;
1643
0
}
1644
1645
static bool zend_ssa_range_widening(const zend_op_array *op_array, const zend_ssa *ssa, int var, int scc)
1646
0
{
1647
0
  zend_ssa_range tmp;
1648
1649
0
  if (zend_inference_calc_range(op_array, ssa, var, 1, 0, &tmp)) {
1650
0
    if (zend_inference_widening_meet(&ssa->var_info[var], &tmp)) {
1651
0
      LOG_SSA_RANGE("  change range (widening  SCC %2d) %2d [%s%ld..%ld%s]\n", scc, var, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1652
0
      return 1;
1653
0
    }
1654
0
  }
1655
0
  return 0;
1656
0
}
1657
1658
static bool zend_inference_narrowing_meet(zend_ssa_var_info *var_info, zend_ssa_range *r)
1659
0
{
1660
0
  if (!var_info->has_range) {
1661
0
    var_info->has_range = 1;
1662
0
  } else {
1663
0
    if (!r->underflow &&
1664
0
        !var_info->range.underflow &&
1665
0
        var_info->range.min < r->min) {
1666
0
      r->min = var_info->range.min;
1667
0
    }
1668
0
    if (!r->overflow &&
1669
0
        !var_info->range.overflow &&
1670
0
        var_info->range.max > r->max) {
1671
0
      r->max = var_info->range.max;
1672
0
    }
1673
0
    if (r->underflow) {
1674
0
      r->min = ZEND_LONG_MIN;
1675
0
    }
1676
0
    if (r->overflow) {
1677
0
      r->max = ZEND_LONG_MAX;
1678
0
    }
1679
0
    if (var_info->range.min == r->min &&
1680
0
        var_info->range.max == r->max &&
1681
0
        var_info->range.underflow == r->underflow &&
1682
0
        var_info->range.overflow == r->overflow) {
1683
0
      return 0;
1684
0
    }
1685
0
  }
1686
0
  var_info->range = *r;
1687
0
  return 1;
1688
0
}
1689
1690
static bool zend_ssa_range_narrowing(const zend_op_array *op_array, const zend_ssa *ssa, int var, int scc)
1691
0
{
1692
0
  zend_ssa_range tmp;
1693
1694
0
  if (zend_inference_calc_range(op_array, ssa, var, 0, 1, &tmp)) {
1695
0
    if (zend_inference_narrowing_meet(&ssa->var_info[var], &tmp)) {
1696
0
      LOG_SSA_RANGE("  change range (narrowing SCC %2d) %2d [%s%ld..%ld%s]\n", scc, var, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1697
0
      return 1;
1698
0
    }
1699
0
  }
1700
0
  return 0;
1701
0
}
1702
1703
#ifdef NEG_RANGE
1704
# define CHECK_INNER_CYCLE(var2) \
1705
  do { \
1706
    if (ssa->vars[var2].scc == ssa->vars[var].scc && \
1707
        !ssa->vars[var2].scc_entry && \
1708
        !zend_bitset_in(visited, var2) && \
1709
      zend_check_inner_cycles(op_array, ssa, worklist, visited, var2)) { \
1710
      return 1; \
1711
    } \
1712
  } while (0)
1713
1714
static bool zend_check_inner_cycles(const zend_op_array *op_array, zend_ssa *ssa, zend_bitset worklist, zend_bitset visited, int var)
1715
{
1716
  if (zend_bitset_in(worklist, var)) {
1717
    return 1;
1718
  }
1719
  zend_bitset_incl(worklist, var);
1720
  FOR_EACH_VAR_USAGE(var, CHECK_INNER_CYCLE);
1721
  zend_bitset_incl(visited, var);
1722
  return 0;
1723
}
1724
#endif
1725
1726
static void zend_infer_ranges_warmup(const zend_op_array *op_array, zend_ssa *ssa, const int *scc_var, const int *next_scc_var, int scc)
1727
0
{
1728
0
  int worklist_len = zend_bitset_len(ssa->vars_count);
1729
0
  int j, n;
1730
0
  zend_ssa_range tmp;
1731
0
  ALLOCA_FLAG(use_heap)
1732
0
  zend_bitset worklist = do_alloca(sizeof(zend_ulong) * worklist_len * 2, use_heap);
1733
0
  zend_bitset visited = worklist + worklist_len;
1734
#ifdef NEG_RANGE
1735
  bool has_inner_cycles = false;
1736
1737
  memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1738
  memset(visited, 0, sizeof(zend_ulong) * worklist_len);
1739
  j = scc_var[scc];
1740
  while (j >= 0) {
1741
    if (!zend_bitset_in(visited, j) &&
1742
        zend_check_inner_cycles(op_array, ssa, worklist, visited, j)) {
1743
      has_inner_cycles = true;
1744
      break;
1745
    }
1746
    j = next_scc_var[j];
1747
  }
1748
#endif
1749
1750
0
  memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1751
1752
0
  for (n = 0; n < RANGE_WARMUP_PASSES; n++) {
1753
0
    j= scc_var[scc];
1754
0
    while (j >= 0) {
1755
0
      if (ssa->vars[j].scc_entry
1756
0
       && !(ssa->var_info[j].type & MAY_BE_REF)) {
1757
0
        zend_bitset_incl(worklist, j);
1758
0
      }
1759
0
      j = next_scc_var[j];
1760
0
    }
1761
1762
0
    memset(visited, 0, sizeof(zend_ulong) * worklist_len);
1763
1764
0
    WHILE_WORKLIST(worklist, worklist_len, j) {
1765
0
      if (zend_inference_calc_range(op_array, ssa, j, 0, 0, &tmp)) {
1766
#ifdef NEG_RANGE
1767
        if (!has_inner_cycles &&
1768
            ssa->var_info[j].has_range &&
1769
            ssa->vars[j].definition_phi &&
1770
            ssa->vars[j].definition_phi->pi >= 0 &&
1771
          ssa->vars[j].definition_phi->has_range_constraint &&
1772
            ssa->vars[j].definition_phi->constraint.range.negative &&
1773
            ssa->vars[j].definition_phi->constraint.range.min_ssa_var < 0 &&
1774
            ssa->vars[j].definition_phi->constraint.range.max_ssa_var < 0) {
1775
          zend_ssa_range_constraint *constraint =
1776
            &ssa->vars[j].definition_phi->constraint.range;
1777
          if (tmp.min == ssa->var_info[j].range.min &&
1778
              tmp.max == ssa->var_info[j].range.max) {
1779
            if (constraint->negative == NEG_INIT) {
1780
              LOG_NEG_RANGE("#%d INVARIANT\n", j);
1781
              constraint->negative = NEG_INVARIANT;
1782
            }
1783
          } else if (tmp.min == ssa->var_info[j].range.min &&
1784
                     tmp.max == ssa->var_info[j].range.max + 1 &&
1785
                     tmp.max < constraint->range.min) {
1786
            if (constraint->negative == NEG_INIT ||
1787
                constraint->negative == NEG_INVARIANT) {
1788
              LOG_NEG_RANGE("#%d LT\n", j);
1789
              constraint->negative = NEG_USE_LT;
1790
//???NEG
1791
            } else if (constraint->negative == NEG_USE_GT) {
1792
              LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1793
              constraint->negative = NEG_UNKNOWN;
1794
            }
1795
          } else if (tmp.max == ssa->var_info[j].range.max &&
1796
                       tmp.min == ssa->var_info[j].range.min - 1 &&
1797
                     tmp.min > constraint->range.max) {
1798
            if (constraint->negative == NEG_INIT ||
1799
                constraint->negative == NEG_INVARIANT) {
1800
              LOG_NEG_RANGE("#%d GT\n", j);
1801
              constraint->negative = NEG_USE_GT;
1802
//???NEG
1803
            } else if (constraint->negative == NEG_USE_LT) {
1804
              LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1805
              constraint->negative = NEG_UNKNOWN;
1806
            }
1807
          } else {
1808
            LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1809
            constraint->negative = NEG_UNKNOWN;
1810
          }
1811
        }
1812
#endif
1813
0
        if (zend_inference_narrowing_meet(&ssa->var_info[j], &tmp)) {
1814
0
          LOG_SSA_RANGE("  change range (warmup %2d SCC %2d) %2d [%s%ld..%ld%s]\n", n, scc, j, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1815
0
          zend_bitset_incl(visited, j);
1816
0
          FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR_1);
1817
0
        }
1818
0
      }
1819
0
    } WHILE_WORKLIST_END();
1820
0
  }
1821
0
  free_alloca(worklist, use_heap);
1822
0
}
1823
1824
static void zend_infer_ranges(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
1825
1
{
1826
1
  int worklist_len = zend_bitset_len(ssa->vars_count);
1827
1
  zend_bitset worklist;
1828
1
  int *next_scc_var;
1829
1
  int *scc_var;
1830
1
  zend_ssa_phi *p;
1831
1
  zend_ssa_range tmp;
1832
1
  int scc, j;
1833
1
  ALLOCA_FLAG(use_heap);
1834
1835
1
  worklist = do_alloca(
1836
1
    ZEND_MM_ALIGNED_SIZE(sizeof(zend_ulong) * worklist_len) +
1837
1
    ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->vars_count) +
1838
1
    sizeof(int) * ssa->sccs, use_heap);
1839
1
  next_scc_var = (int*)((char*)worklist + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ulong) * worklist_len));
1840
1
  scc_var = (int*)((char*)next_scc_var + ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->vars_count));
1841
1842
1
  LOG_SSA_RANGE("Range Inference\n");
1843
1844
  /* Create linked lists of SSA variables for each SCC */
1845
1
  memset(scc_var, -1, sizeof(int) * ssa->sccs);
1846
1
  for (j = 0; j < ssa->vars_count; j++) {
1847
0
    if (ssa->vars[j].scc >= 0) {
1848
0
      next_scc_var[j] = scc_var[ssa->vars[j].scc];
1849
0
      scc_var[ssa->vars[j].scc] = j;
1850
0
    }
1851
0
  }
1852
1853
1
  for (scc = 0; scc < ssa->sccs; scc++) {
1854
0
    j = scc_var[scc];
1855
0
    if (next_scc_var[j] < 0) {
1856
      /* SCC with a single element */
1857
0
      if (ssa->var_info[j].type & MAY_BE_REF) {
1858
        /* pass */
1859
0
      } else if (zend_inference_calc_range(op_array, ssa, j, 0, 1, &tmp)) {
1860
0
        zend_inference_init_range(op_array, ssa, j, tmp.underflow, tmp.min, tmp.max, tmp.overflow);
1861
0
      } else {
1862
0
        zend_inference_init_range(op_array, ssa, j, true, ZEND_LONG_MIN, ZEND_LONG_MAX, true);
1863
0
      }
1864
0
    } else {
1865
      /* Find SCC entry points */
1866
0
      memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1867
0
      do {
1868
0
        if (ssa->vars[j].scc_entry
1869
0
         && !(ssa->var_info[j].type & MAY_BE_REF)) {
1870
0
          zend_bitset_incl(worklist, j);
1871
0
        }
1872
0
        j = next_scc_var[j];
1873
0
      } while (j >= 0);
1874
1875
0
#if RANGE_WARMUP_PASSES > 0
1876
0
      zend_infer_ranges_warmup(op_array, ssa, scc_var, next_scc_var, scc);
1877
0
      j = scc_var[scc];
1878
0
      do {
1879
0
        if (!(ssa->var_info[j].type & MAY_BE_REF)) {
1880
0
          zend_bitset_incl(worklist, j);
1881
0
        }
1882
0
        j = next_scc_var[j];
1883
0
      } while (j >= 0);
1884
0
#endif
1885
1886
      /* widening */
1887
0
      WHILE_WORKLIST(worklist, worklist_len, j) {
1888
0
        if (zend_ssa_range_widening(op_array, ssa, j, scc)) {
1889
0
          FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1890
0
        }
1891
0
      } WHILE_WORKLIST_END();
1892
1893
      /* initialize missing ranges */
1894
0
      for (j = scc_var[scc]; j >= 0; j = next_scc_var[j]) {
1895
0
        if (!ssa->var_info[j].has_range
1896
0
         && !(ssa->var_info[j].type & MAY_BE_REF)) {
1897
0
          zend_inference_init_range(op_array, ssa, j, true, ZEND_LONG_MIN, ZEND_LONG_MAX,
1898
0
                  true);
1899
0
          FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1900
0
        }
1901
0
      }
1902
1903
      /* widening (second round) */
1904
0
      WHILE_WORKLIST(worklist, worklist_len, j) {
1905
0
        if (zend_ssa_range_widening(op_array, ssa, j, scc)) {
1906
0
          FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1907
0
        }
1908
0
      } WHILE_WORKLIST_END();
1909
1910
      /* Add all SCC entry variables into worklist for narrowing */
1911
0
      for (j = scc_var[scc]; j >= 0; j = next_scc_var[j]) {
1912
0
        if (ssa->vars[j].definition_phi
1913
0
         && ssa->vars[j].definition_phi->pi < 0
1914
0
         && !(ssa->var_info[j].type & MAY_BE_REF)) {
1915
          /* narrowing Phi functions first */
1916
0
          zend_ssa_range_narrowing(op_array, ssa, j, scc);
1917
0
        }
1918
0
        zend_bitset_incl(worklist, j);
1919
0
      }
1920
1921
      /* narrowing */
1922
0
      WHILE_WORKLIST(worklist, worklist_len, j) {
1923
0
        if (zend_ssa_range_narrowing(op_array, ssa, j, scc)) {
1924
0
          FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1925
0
#ifdef SYM_RANGE
1926
          /* Process symbolic control-flow constraints */
1927
0
          p = ssa->vars[j].sym_use_chain;
1928
0
          while (p) {
1929
0
            ADD_SCC_VAR(p->ssa_var);
1930
0
            p = p->sym_use_chain;
1931
0
          }
1932
0
#endif
1933
0
        }
1934
0
      } WHILE_WORKLIST_END();
1935
0
    }
1936
0
  }
1937
1938
1
  free_alloca(worklist, use_heap);
1939
1
}
1940
/* }}} */
1941
1942
0
static uint32_t get_ssa_alias_types(zend_ssa_alias_kind alias) {
1943
0
  if (alias == HTTP_RESPONSE_HEADER_ALIAS) {
1944
0
    return MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING | MAY_BE_RC1 | MAY_BE_RCN;
1945
0
  } else {
1946
0
    return MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1947
0
  }
1948
0
}
1949
1950
#define UPDATE_SSA_TYPE(_type, _var)                  \
1951
0
  do {                               \
1952
0
    uint32_t __type = (_type) & ~MAY_BE_GUARD;           \
1953
0
    int __var = (_var);                       \
1954
0
    if (__type & MAY_BE_REF) {                   \
1955
0
      __type |= MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF; \
1956
0
    }                               \
1957
0
    if (__var >= 0) {                       \
1958
0
      zend_ssa_var *__ssa_var = &ssa_vars[__var];         \
1959
0
      if (__ssa_var->var < op_array->num_args) {         \
1960
0
        if (__type & MAY_BE_RC1) {                              \
1961
0
          /* TODO: may be captured by exception backtreace */ \
1962
0
          __type |= MAY_BE_RCN;                               \
1963
0
        }                                                       \
1964
0
      }                                                           \
1965
0
      if (__ssa_var->var < op_array->last_var) {         \
1966
0
        if (__type & (MAY_BE_REF|MAY_BE_RCN)) {         \
1967
0
          __type |= MAY_BE_RC1 | MAY_BE_RCN;         \
1968
0
        }                           \
1969
0
        if ((__type & MAY_BE_RC1) && (__type & MAY_BE_STRING)) {\
1970
0
          /* TODO: support for array keys and ($str . "")*/   \
1971
0
          __type |= MAY_BE_RCN;                               \
1972
0
        }                                                       \
1973
0
        if ((__type & MAY_BE_RC1) && (__type & MAY_BE_OBJECT)) {\
1974
0
          /* TODO: object may be captured by magic handlers */\
1975
0
          __type |= MAY_BE_RCN;                               \
1976
0
        }                                                       \
1977
0
        if (__ssa_var->alias) {                 \
1978
0
          __type |= get_ssa_alias_types(__ssa_var->alias);  \
1979
0
        }                            \
1980
0
      }                             \
1981
0
      if (ssa_var_info[__var].type != __type) {           \
1982
0
        ZEND_ASSERT(ssa_opcodes != NULL ||           \
1983
0
          __ssa_var->var >= op_array->last_var ||       \
1984
0
          (ssa_var_info[__var].type & MAY_BE_REF)       \
1985
0
            == (__type & MAY_BE_REF));            \
1986
0
        if (ssa_var_info[__var].type & ~__type) {       \
1987
0
          emit_type_narrowing_warning(op_array, ssa, __var);  \
1988
0
          return FAILURE;                   \
1989
0
        }                            \
1990
0
        ssa_var_info[__var].type = __type;            \
1991
0
        if (update_worklist) {                   \
1992
0
          add_usages(op_array, ssa, worklist, __var);     \
1993
0
        }                            \
1994
0
      }                             \
1995
0
      /*zend_bitset_excl(worklist, var);*/            \
1996
0
    }                               \
1997
0
  } while (0)
1998
1999
#define UPDATE_SSA_OBJ_TYPE(_ce, _is_instanceof, var)           \
2000
0
  do {                                                                \
2001
0
    if (var >= 0) {                         \
2002
0
      zend_class_entry *__ce = (_ce);               \
2003
0
      bool __is_instanceof = (_is_instanceof);          \
2004
0
      if (__ce && (__ce->ce_flags & ZEND_ACC_FINAL)) {     \
2005
0
        __is_instanceof = false;                \
2006
0
      }                              \
2007
0
      if (ssa_var_info[var].ce != __ce ||             \
2008
0
          ssa_var_info[var].is_instanceof != __is_instanceof) { \
2009
0
        ssa_var_info[var].ce = __ce;              \
2010
0
        ssa_var_info[var].is_instanceof = __is_instanceof;    \
2011
0
        if (update_worklist) {                   \
2012
0
          add_usages(op_array, ssa, worklist, var);     \
2013
0
        }                            \
2014
0
      }                             \
2015
0
      /*zend_bitset_excl(worklist, var);*/            \
2016
0
    }                               \
2017
0
  } while (0)
2018
2019
0
#define COPY_SSA_OBJ_TYPE(from_var, to_var) do { \
2020
0
  if ((from_var) >= 0 && (ssa_var_info[(from_var)].type & MAY_BE_OBJECT) \
2021
0
      && ssa_var_info[(from_var)].ce && !(ssa_var_info[(to_var)].type & MAY_BE_REF)) { \
2022
0
    UPDATE_SSA_OBJ_TYPE(ssa_var_info[(from_var)].ce, \
2023
0
      ssa_var_info[(from_var)].is_instanceof, (to_var)); \
2024
0
  } else { \
2025
0
    UPDATE_SSA_OBJ_TYPE(NULL, 0, (to_var)); \
2026
0
  } \
2027
0
} while (0)
2028
2029
static void add_usages(const zend_op_array *op_array, const zend_ssa *ssa, zend_bitset worklist, int var)
2030
0
{
2031
0
  if (ssa->vars[var].phi_use_chain) {
2032
0
    const zend_ssa_phi *p = ssa->vars[var].phi_use_chain;
2033
0
    do {
2034
0
      zend_bitset_incl(worklist, p->ssa_var);
2035
0
      p = zend_ssa_next_use_phi(ssa, var, p);
2036
0
    } while (p);
2037
0
  }
2038
0
  if (ssa->vars[var].use_chain >= 0) {
2039
0
    int use = ssa->vars[var].use_chain;
2040
0
    const zend_ssa_op *op;
2041
2042
0
    do {
2043
0
      op = ssa->ops + use;
2044
0
      if (op->result_def >= 0) {
2045
0
        zend_bitset_incl(worklist, op->result_def);
2046
0
      }
2047
0
      if (op->op1_def >= 0) {
2048
0
        zend_bitset_incl(worklist, op->op1_def);
2049
0
      }
2050
0
      if (op->op2_def >= 0) {
2051
0
        zend_bitset_incl(worklist, op->op2_def);
2052
0
      }
2053
0
      if (op_array->opcodes[use].opcode == ZEND_OP_DATA) {
2054
0
        op--;
2055
0
        if (op->result_def >= 0) {
2056
0
          zend_bitset_incl(worklist, op->result_def);
2057
0
        }
2058
0
        if (op->op1_def >= 0) {
2059
0
          zend_bitset_incl(worklist, op->op1_def);
2060
0
        }
2061
0
        if (op->op2_def >= 0) {
2062
0
          zend_bitset_incl(worklist, op->op2_def);
2063
0
        }
2064
0
      } else if (use + 1 < op_array->last
2065
0
       && op_array->opcodes[use + 1].opcode == ZEND_OP_DATA) {
2066
0
        op++;
2067
0
        if (op->result_def >= 0) {
2068
0
          zend_bitset_incl(worklist, op->result_def);
2069
0
        }
2070
0
        if (op->op1_def >= 0) {
2071
0
          zend_bitset_incl(worklist, op->op1_def);
2072
0
        }
2073
0
        if (op->op2_def >= 0) {
2074
0
          zend_bitset_incl(worklist, op->op2_def);
2075
0
        }
2076
0
      }
2077
0
      use = zend_ssa_next_use(ssa->ops, var, use);
2078
0
    } while (use >= 0);
2079
0
  }
2080
0
}
2081
2082
static void emit_type_narrowing_warning(const zend_op_array *op_array, const zend_ssa *ssa, int var)
2083
0
{
2084
0
  int def_op_num = ssa->vars[var].definition;
2085
0
  const zend_op *def_opline = def_op_num >= 0 ? &op_array->opcodes[def_op_num] : NULL;
2086
0
  const char *def_op_name = def_opline ? zend_get_opcode_name(def_opline->opcode) : "PHI";
2087
0
  uint32_t lineno = def_opline ? def_opline->lineno : 0;
2088
0
  zend_error_at(
2089
0
    E_WARNING, op_array->filename, lineno,
2090
0
    "Narrowing occurred during type inference of %s. Please file a bug report on https://github.com/php/php-src/issues", def_op_name);
2091
0
#if ZEND_DEBUG
2092
0
  ZEND_ASSERT(0 && "Narrowing during type inference");
2093
0
#endif
2094
0
}
2095
2096
ZEND_API uint32_t ZEND_FASTCALL zend_array_type_info(const zval *zv)
2097
0
{
2098
0
  HashTable *ht = Z_ARRVAL_P(zv);
2099
0
  uint32_t tmp = MAY_BE_ARRAY;
2100
0
  zend_string *str;
2101
0
  zval *val;
2102
2103
0
  if (Z_REFCOUNTED_P(zv)) {
2104
0
    tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2105
0
  } else {
2106
0
    tmp |= MAY_BE_RCN;
2107
0
  }
2108
2109
0
  if (zend_hash_num_elements(ht) == 0) {
2110
0
    tmp |=  MAY_BE_ARRAY_EMPTY;
2111
0
  } else if (HT_IS_PACKED(ht)) {
2112
0
    tmp |= MAY_BE_ARRAY_PACKED;
2113
0
    ZEND_HASH_PACKED_FOREACH_VAL(ht, val) {
2114
0
      tmp |= 1 << (Z_TYPE_P(val) + MAY_BE_ARRAY_SHIFT);
2115
0
    } ZEND_HASH_FOREACH_END();
2116
0
  } else {
2117
0
    ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht, str, val) {
2118
0
      if (str) {
2119
0
        tmp |= MAY_BE_ARRAY_STRING_HASH;
2120
0
      } else {
2121
0
        tmp |= MAY_BE_ARRAY_NUMERIC_HASH;
2122
0
      }
2123
0
      tmp |= 1 << (Z_TYPE_P(val) + MAY_BE_ARRAY_SHIFT);
2124
0
    } ZEND_HASH_FOREACH_END();
2125
0
  }
2126
0
  return tmp;
2127
0
}
2128
2129
2130
ZEND_API uint32_t zend_array_element_type(uint32_t t1, uint8_t op_type, bool write, bool insert)
2131
0
{
2132
0
  uint32_t tmp = 0;
2133
2134
0
  if (t1 & MAY_BE_OBJECT) {
2135
0
      if (!write) {
2136
      /* can't be REF  because of ZVAL_COPY_DEREF() usage */
2137
0
      tmp |= MAY_BE_ANY | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2138
0
    } else {
2139
0
      tmp |= MAY_BE_ANY | MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2140
0
      }
2141
0
    if (write) {
2142
0
      tmp |= MAY_BE_INDIRECT;
2143
0
    }
2144
0
  }
2145
0
  if (t1 & MAY_BE_ARRAY) {
2146
0
    if (insert) {
2147
0
      tmp |= MAY_BE_NULL;
2148
0
    } else {
2149
0
      tmp |= MAY_BE_NULL | ((t1 & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
2150
0
      if (tmp & MAY_BE_ARRAY) {
2151
0
        tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2152
0
      }
2153
0
      if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2154
0
        if (!write) {
2155
          /* can't be REF  because of ZVAL_COPY_DEREF() usage */
2156
0
          tmp |= MAY_BE_RCN;
2157
0
          if ((op_type & (IS_VAR|IS_TMP_VAR)) && (t1 & MAY_BE_RC1)) {
2158
0
            tmp |= MAY_BE_RC1;
2159
0
          }
2160
0
        } else if (t1 & MAY_BE_ARRAY_OF_REF) {
2161
0
          tmp |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN;
2162
0
        } else {
2163
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2164
0
        }
2165
0
      }
2166
0
    }
2167
0
    if (write) {
2168
0
      tmp |= MAY_BE_INDIRECT;
2169
0
    }
2170
0
  }
2171
0
  if (t1 & MAY_BE_STRING) {
2172
0
    tmp |= MAY_BE_STRING | MAY_BE_RC1;
2173
0
    if (write) {
2174
0
      tmp |= MAY_BE_NULL;
2175
0
    }
2176
0
  }
2177
0
  if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2178
0
    tmp |= MAY_BE_NULL;
2179
0
    if (write) {
2180
0
      tmp |= MAY_BE_INDIRECT;
2181
0
    }
2182
0
  }
2183
0
  if (t1 & (MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE)) {
2184
0
    if (!write) {
2185
0
      tmp |= MAY_BE_NULL;
2186
0
    }
2187
0
  }
2188
0
  return tmp;
2189
0
}
2190
2191
static uint32_t assign_dim_array_result_type(
2192
0
    uint32_t arr_type, uint32_t dim_type, uint32_t value_type, uint8_t dim_op_type) {
2193
0
  uint32_t tmp = 0;
2194
  /* Only add key type if we have a value type. We want to maintain the invariant that a
2195
   * key type exists iff a value type exists even in dead code that may use empty types. */
2196
0
  if (value_type & (MAY_BE_ANY|MAY_BE_UNDEF)) {
2197
0
    if (value_type & MAY_BE_UNDEF) {
2198
0
      value_type |= MAY_BE_NULL;
2199
0
    }
2200
0
    if (dim_op_type == IS_UNUSED) {
2201
0
      if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2202
0
        tmp |= MAY_BE_ARRAY_PACKED;
2203
0
      }
2204
0
      tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
2205
0
    } else {
2206
0
      if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
2207
0
        if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2208
0
          tmp |= MAY_BE_ARRAY_PACKED;
2209
0
        }
2210
0
        tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
2211
0
      }
2212
0
      if (dim_type & MAY_BE_STRING) {
2213
0
        tmp |= MAY_BE_ARRAY_KEY_STRING;
2214
0
        if (dim_op_type != IS_CONST) {
2215
          // FIXME: numeric string
2216
0
          if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2217
0
            tmp |= MAY_BE_ARRAY_PACKED;
2218
0
          }
2219
0
          tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
2220
0
        }
2221
0
      }
2222
0
      if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
2223
0
        tmp |= MAY_BE_ARRAY_KEY_STRING;
2224
0
      }
2225
0
    }
2226
0
  }
2227
  /* Only add value type if we have a key type. It might be that the key type is illegal
2228
   * for arrays. */
2229
0
  if (tmp & MAY_BE_ARRAY_KEY_ANY) {
2230
0
    tmp |= (value_type & MAY_BE_ANY) << MAY_BE_ARRAY_SHIFT;
2231
0
  }
2232
0
  tmp &= ~MAY_BE_ARRAY_EMPTY;
2233
0
  return tmp;
2234
0
}
2235
2236
static uint32_t assign_dim_result_type(
2237
0
    uint32_t arr_type, uint32_t dim_type, uint32_t value_type, uint8_t dim_op_type) {
2238
0
  uint32_t tmp = arr_type & ~(MAY_BE_RC1|MAY_BE_RCN);
2239
2240
0
  if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2241
0
    tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
2242
0
    tmp |= MAY_BE_ARRAY|MAY_BE_RC1;
2243
0
  }
2244
0
  if (tmp & (MAY_BE_ARRAY|MAY_BE_STRING)) {
2245
0
    tmp |= MAY_BE_RC1;
2246
0
  }
2247
0
  if (tmp & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2248
0
    tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2249
0
  }
2250
0
  if (tmp & MAY_BE_ARRAY) {
2251
0
    tmp |= assign_dim_array_result_type(arr_type, dim_type, value_type, dim_op_type);
2252
0
  }
2253
0
  return tmp;
2254
0
}
2255
2256
/* For binary ops that have compound assignment operators */
2257
static uint32_t binary_op_result_type(
2258
    const zend_ssa *ssa, uint8_t opcode, uint32_t t1, uint32_t t2, int result_var,
2259
0
    zend_long optimization_level) {
2260
0
  uint32_t tmp = 0;
2261
0
  uint32_t t1_type = (t1 & MAY_BE_ANY) | (t1 & MAY_BE_UNDEF ? MAY_BE_NULL : 0);
2262
0
  uint32_t t2_type = (t2 & MAY_BE_ANY) | (t2 & MAY_BE_UNDEF ? MAY_BE_NULL : 0);
2263
2264
0
  if (!(ZEND_OPTIMIZER_IGNORE_OVERLOADING & optimization_level)) {
2265
    /* Handle potentially overloaded operators.
2266
     * This could be made more precise by checking the class type, if known. */
2267
0
    if ((t1_type & MAY_BE_OBJECT) || (t2_type & MAY_BE_OBJECT)) {
2268
      /* This is somewhat GMP specific. */
2269
0
      tmp |= MAY_BE_OBJECT | MAY_BE_FALSE | MAY_BE_RC1;
2270
0
    }
2271
0
  }
2272
2273
0
  switch (opcode) {
2274
0
    case ZEND_ADD:
2275
0
      if (t1_type == MAY_BE_LONG && t2_type == MAY_BE_LONG) {
2276
0
        if (result_var < 0 ||
2277
0
          !ssa->var_info[result_var].has_range ||
2278
0
            ssa->var_info[result_var].range.underflow ||
2279
0
            ssa->var_info[result_var].range.overflow) {
2280
          /* may overflow */
2281
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2282
0
        } else {
2283
0
          tmp |= MAY_BE_LONG;
2284
0
        }
2285
0
      } else if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2286
0
        tmp |= MAY_BE_DOUBLE;
2287
0
      } else if (t1_type == MAY_BE_ARRAY && t2_type == MAY_BE_ARRAY) {
2288
0
        tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
2289
0
        tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2290
0
        tmp |= t2 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2291
0
      } else {
2292
0
        tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2293
0
        if ((t1_type & MAY_BE_ARRAY) && (t2_type & MAY_BE_ARRAY)) {
2294
0
          tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
2295
0
          tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2296
0
          tmp |= t2 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2297
0
        }
2298
0
      }
2299
0
      break;
2300
0
    case ZEND_SUB:
2301
0
    case ZEND_MUL:
2302
0
      if (t1_type == MAY_BE_LONG && t2_type == MAY_BE_LONG) {
2303
0
        if (result_var < 0 ||
2304
0
          !ssa->var_info[result_var].has_range ||
2305
0
            ssa->var_info[result_var].range.underflow ||
2306
0
            ssa->var_info[result_var].range.overflow) {
2307
          /* may overflow */
2308
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2309
0
        } else {
2310
0
          tmp |= MAY_BE_LONG;
2311
0
        }
2312
0
      } else if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2313
0
        tmp |= MAY_BE_DOUBLE;
2314
0
      } else {
2315
0
        tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2316
0
      }
2317
0
      break;
2318
0
    case ZEND_DIV:
2319
0
    case ZEND_POW:
2320
0
      if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2321
0
        tmp |= MAY_BE_DOUBLE;
2322
0
      } else {
2323
0
        tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2324
0
      }
2325
      /* Division by zero results in Inf/-Inf/Nan (double), so it doesn't need any special
2326
       * handling */
2327
0
      break;
2328
0
    case ZEND_MOD:
2329
0
      tmp |= MAY_BE_LONG;
2330
      /* Division by zero results in an exception, so it doesn't need any special handling */
2331
0
      break;
2332
0
    case ZEND_BW_OR:
2333
0
    case ZEND_BW_AND:
2334
0
    case ZEND_BW_XOR:
2335
0
      if ((t1_type & MAY_BE_STRING) && (t2_type & MAY_BE_STRING)) {
2336
0
        tmp |= MAY_BE_STRING | MAY_BE_RC1 | MAY_BE_RCN;
2337
0
      }
2338
0
      if ((t1_type & ~MAY_BE_STRING) || (t2_type & ~MAY_BE_STRING)) {
2339
0
        tmp |= MAY_BE_LONG;
2340
0
      }
2341
0
      break;
2342
0
    case ZEND_SL:
2343
0
    case ZEND_SR:
2344
0
      tmp |= MAY_BE_LONG;
2345
0
      break;
2346
0
    case ZEND_CONCAT:
2347
0
    case ZEND_FAST_CONCAT:
2348
      /* TODO: +MAY_BE_OBJECT ??? */
2349
0
      tmp = MAY_BE_STRING | MAY_BE_RC1 | MAY_BE_RCN;
2350
0
      break;
2351
0
    default: ZEND_UNREACHABLE();
2352
0
  }
2353
0
  return tmp;
2354
0
}
2355
2356
0
static uint32_t zend_convert_type_declaration_mask(uint32_t type_mask) {
2357
0
  uint32_t result_mask = type_mask & MAY_BE_ANY;
2358
0
  if (type_mask & MAY_BE_VOID) {
2359
0
    result_mask |= MAY_BE_NULL;
2360
0
  }
2361
0
  if (type_mask & MAY_BE_CALLABLE) {
2362
0
    result_mask |= MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2363
0
  }
2364
0
  if (type_mask & MAY_BE_STATIC) {
2365
0
    result_mask |= MAY_BE_OBJECT;
2366
0
  }
2367
0
  if (type_mask & MAY_BE_ARRAY) {
2368
0
    result_mask |= MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2369
0
  }
2370
0
  return result_mask;
2371
0
}
2372
2373
static uint32_t zend_convert_type(const zend_script *script, zend_type type, zend_class_entry **pce)
2374
0
{
2375
0
  if (pce) {
2376
0
    *pce = NULL;
2377
0
  }
2378
2379
0
  if (!ZEND_TYPE_IS_SET(type)) {
2380
0
    return MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_RC1|MAY_BE_RCN;
2381
0
  }
2382
2383
0
  uint32_t tmp = zend_convert_type_declaration_mask(ZEND_TYPE_PURE_MASK(type));
2384
0
  if (ZEND_TYPE_IS_COMPLEX(type)) {
2385
0
    tmp |= MAY_BE_OBJECT;
2386
0
    if (pce) {
2387
      /* As we only have space to store one CE,
2388
       * we use a plain object type for class unions. */
2389
0
      if (ZEND_TYPE_HAS_NAME(type)) {
2390
0
        zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(type));
2391
        // TODO: Pass through op_array.
2392
0
        *pce = zend_optimizer_get_class_entry(script, NULL, lcname);
2393
0
        zend_string_release_ex(lcname, 0);
2394
0
      }
2395
0
    }
2396
0
  }
2397
0
  if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2398
0
    tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2399
0
  }
2400
0
  return tmp;
2401
0
}
2402
2403
ZEND_API uint32_t zend_fetch_arg_info_type(const zend_script *script, const zend_arg_info *arg_info, zend_class_entry **pce)
2404
0
{
2405
0
  return zend_convert_type(script, arg_info->type, pce);
2406
0
}
2407
2408
0
static const zend_property_info *lookup_prop_info(const zend_class_entry *ce, zend_string *name, zend_class_entry *scope) {
2409
0
  const zend_property_info *prop_info;
2410
2411
  /* If the class is linked, reuse the precise runtime logic. */
2412
0
  if ((ce->ce_flags & ZEND_ACC_LINKED)
2413
0
   && (!scope || (scope->ce_flags & ZEND_ACC_LINKED))) {
2414
0
    const zend_class_entry *prev_scope = EG(fake_scope);
2415
0
    EG(fake_scope) = scope;
2416
0
    prop_info = zend_get_property_info(ce, name, 1);
2417
0
    EG(fake_scope) = prev_scope;
2418
0
    if (prop_info && prop_info != ZEND_WRONG_PROPERTY_INFO) {
2419
0
      return prop_info;
2420
0
    }
2421
0
    return NULL;
2422
0
  }
2423
2424
  /* Otherwise, handle only some safe cases */
2425
0
  prop_info = zend_hash_find_ptr(&ce->properties_info, name);
2426
0
  if (prop_info &&
2427
0
    ((prop_info->ce == scope) ||
2428
0
     (!scope && (prop_info->flags & ZEND_ACC_PUBLIC)))
2429
0
  ) {
2430
0
    return prop_info;
2431
0
  }
2432
0
  return NULL;
2433
0
}
2434
2435
static const zend_property_info *zend_fetch_prop_info(const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline, const zend_ssa_op *ssa_op)
2436
0
{
2437
0
  const zend_property_info *prop_info = NULL;
2438
0
  if (opline->op2_type == IS_CONST) {
2439
0
    const zend_class_entry *ce = NULL;
2440
2441
0
    if (opline->op1_type == IS_UNUSED && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
2442
0
      ce = op_array->scope;
2443
0
    } else if (ssa_op->op1_use >= 0) {
2444
0
      ce = ssa->var_info[ssa_op->op1_use].ce;
2445
0
    }
2446
0
    if (ce) {
2447
0
      prop_info = lookup_prop_info(ce,
2448
0
        Z_STR_P(CRT_CONSTANT(opline->op2)),
2449
0
        op_array->scope);
2450
0
      if (prop_info && (prop_info->flags & ZEND_ACC_STATIC)) {
2451
0
        prop_info = NULL;
2452
0
      }
2453
0
    }
2454
0
  }
2455
0
  return prop_info;
2456
0
}
2457
2458
static const zend_property_info *zend_fetch_static_prop_info(const zend_script *script, const zend_op_array *op_array, const zend_ssa *ssa, const zend_op *opline)
2459
0
{
2460
0
  const zend_property_info *prop_info = NULL;
2461
0
  if (opline->op1_type == IS_CONST) {
2462
0
    const zend_class_entry *ce = NULL;
2463
0
    if (opline->op2_type == IS_UNUSED) {
2464
0
      uint32_t fetch_type = opline->op2.num & ZEND_FETCH_CLASS_MASK;
2465
0
      switch (fetch_type) {
2466
0
        case ZEND_FETCH_CLASS_SELF:
2467
0
        case ZEND_FETCH_CLASS_STATIC:
2468
          /* We enforce that static property types cannot change during inheritance, so
2469
           * handling static the same way as self here is legal. */
2470
0
          ce = op_array->scope;
2471
0
          break;
2472
0
        case ZEND_FETCH_CLASS_PARENT:
2473
0
          if (op_array->scope && (op_array->scope->ce_flags & ZEND_ACC_LINKED)) {
2474
0
            ce = op_array->scope->parent;
2475
0
          }
2476
0
          break;
2477
0
      }
2478
0
    } else if (opline->op2_type == IS_CONST) {
2479
0
      const zval *zv = CRT_CONSTANT(opline->op2);
2480
0
      ce = zend_optimizer_get_class_entry(script, op_array, Z_STR_P(zv + 1));
2481
0
    }
2482
2483
0
    if (ce) {
2484
0
      const zval *zv = CRT_CONSTANT(opline->op1);
2485
0
      prop_info = lookup_prop_info(ce, Z_STR_P(zv), op_array->scope);
2486
0
      if (prop_info && !(prop_info->flags & ZEND_ACC_STATIC)) {
2487
0
        prop_info = NULL;
2488
0
      }
2489
0
    }
2490
0
  }
2491
0
  return prop_info;
2492
0
}
2493
2494
static uint32_t zend_fetch_prop_type(const zend_script *script, const zend_property_info *prop_info, zend_class_entry **pce)
2495
0
{
2496
0
  if (!prop_info) {
2497
0
    if (pce) {
2498
0
      *pce = NULL;
2499
0
    }
2500
0
    return MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_RC1 | MAY_BE_RCN;
2501
0
  }
2502
2503
0
  return zend_convert_type(script, prop_info->type, pce);
2504
0
}
2505
2506
static bool result_may_be_separated(const zend_ssa *ssa, const zend_ssa_op *ssa_op)
2507
0
{
2508
0
  int tmp_var = ssa_op->result_def;
2509
2510
0
  if (ssa->vars[tmp_var].use_chain >= 0
2511
0
   && !ssa->vars[tmp_var].phi_use_chain) {
2512
0
    const zend_ssa_op *use_op = &ssa->ops[ssa->vars[tmp_var].use_chain];
2513
2514
    /* TODO: analyze instructions between ssa_op and use_op */
2515
0
    if (use_op == ssa_op + 1) {
2516
0
      if ((use_op->op1_use == tmp_var && use_op->op1_use_chain < 0)
2517
0
       || (use_op->op2_use == tmp_var && use_op->op2_use_chain < 0)) {
2518
0
        return 0;
2519
0
      }
2520
0
    }
2521
0
  }
2522
0
  return 1;
2523
0
}
2524
2525
static zend_always_inline zend_result _zend_update_type_info(
2526
      const zend_op_array *op_array,
2527
      zend_ssa            *ssa,
2528
      const zend_script   *script,
2529
      zend_bitset          worklist,
2530
      const zend_op       *opline,
2531
      zend_ssa_op         *ssa_op,
2532
      const zend_op      **ssa_opcodes,
2533
      zend_long            optimization_level,
2534
      bool            update_worklist)
2535
0
{
2536
0
  uint32_t t1, t2;
2537
0
  uint32_t tmp, orig;
2538
0
  zend_ssa_var *ssa_vars = ssa->vars;
2539
0
  zend_ssa_var_info *ssa_var_info = ssa->var_info;
2540
0
  zend_class_entry *ce;
2541
0
  int j;
2542
2543
0
  if (opline->opcode == ZEND_OP_DATA) {
2544
0
    opline--;
2545
0
    ssa_op--;
2546
0
  }
2547
2548
0
  t1 = OP1_INFO();
2549
0
  t2 = OP2_INFO();
2550
2551
  /* If one of the operands cannot have any type, this means the operand derives from
2552
   * unreachable code. Propagate the empty result early, so that that the following
2553
   * code may assume that operands have at least one type. */
2554
0
  if (!(t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS))
2555
0
   || !(t2 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS))
2556
0
   || (ssa_op->result_use >= 0 && !(RES_USE_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS)))
2557
0
   || ((opline->opcode == ZEND_ASSIGN_DIM_OP
2558
0
     || opline->opcode == ZEND_ASSIGN_OBJ_OP
2559
0
     || opline->opcode == ZEND_ASSIGN_STATIC_PROP_OP
2560
0
     || opline->opcode == ZEND_ASSIGN_DIM
2561
0
     || opline->opcode == ZEND_ASSIGN_OBJ)
2562
0
      && !(OP1_DATA_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS)) /*&& 0*/)) {
2563
0
    tmp = 0;
2564
0
    if (ssa_op->result_def >= 0 && !(ssa_var_info[ssa_op->result_def].type & MAY_BE_REF)) {
2565
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2566
0
    }
2567
0
    if (ssa_op->op1_def >= 0 && !(ssa_var_info[ssa_op->op1_def].type & MAY_BE_REF)) {
2568
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2569
0
    }
2570
0
    if (ssa_op->op2_def >= 0 && !(ssa_var_info[ssa_op->op2_def].type & MAY_BE_REF)) {
2571
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
2572
0
    }
2573
0
    if (opline->opcode == ZEND_ASSIGN_DIM_OP
2574
0
     || opline->opcode == ZEND_ASSIGN_OBJ_OP
2575
0
     || opline->opcode == ZEND_ASSIGN_STATIC_PROP_OP
2576
0
     || opline->opcode == ZEND_ASSIGN_DIM
2577
0
     || opline->opcode == ZEND_ASSIGN_OBJ) {
2578
0
      if ((ssa_op+1)->op1_def >= 0 && !(ssa_var_info[(ssa_op+1)->op1_def].type & MAY_BE_REF)) {
2579
0
        UPDATE_SSA_TYPE(tmp, (ssa_op+1)->op1_def);
2580
0
      }
2581
0
    }
2582
0
    return SUCCESS;
2583
0
  }
2584
2585
0
  switch (opline->opcode) {
2586
0
    case ZEND_ADD:
2587
0
    case ZEND_SUB:
2588
0
    case ZEND_MUL:
2589
0
    case ZEND_DIV:
2590
0
    case ZEND_POW:
2591
0
    case ZEND_MOD:
2592
0
    case ZEND_BW_OR:
2593
0
    case ZEND_BW_AND:
2594
0
    case ZEND_BW_XOR:
2595
0
    case ZEND_SL:
2596
0
    case ZEND_SR:
2597
0
    case ZEND_CONCAT:
2598
0
      tmp = binary_op_result_type(ssa, opline->opcode, t1, t2, ssa_op->result_def, optimization_level);
2599
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2600
0
      break;
2601
0
    case ZEND_BW_NOT:
2602
0
      tmp = 0;
2603
0
      if (t1 & MAY_BE_STRING) {
2604
0
        tmp |= MAY_BE_STRING | MAY_BE_RC1 | MAY_BE_RCN;
2605
0
      }
2606
0
      if (t1 & (MAY_BE_ANY-MAY_BE_STRING)) {
2607
0
        tmp |= MAY_BE_LONG;
2608
0
      }
2609
0
      if (!(ZEND_OPTIMIZER_IGNORE_OVERLOADING & optimization_level)) {
2610
0
        if (t1 & MAY_BE_OBJECT) {
2611
          /* Potentially overloaded operator. */
2612
0
          tmp |= MAY_BE_OBJECT | MAY_BE_RC1;
2613
0
        }
2614
0
      }
2615
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2616
0
      break;
2617
0
    case ZEND_BEGIN_SILENCE:
2618
0
      UPDATE_SSA_TYPE(MAY_BE_LONG, ssa_op->result_def);
2619
0
      break;
2620
0
    case ZEND_BOOL_NOT:
2621
0
    case ZEND_BOOL_XOR:
2622
0
    case ZEND_IS_IDENTICAL:
2623
0
    case ZEND_IS_NOT_IDENTICAL:
2624
0
    case ZEND_IS_EQUAL:
2625
0
    case ZEND_IS_NOT_EQUAL:
2626
0
    case ZEND_IS_SMALLER:
2627
0
    case ZEND_IS_SMALLER_OR_EQUAL:
2628
0
    case ZEND_INSTANCEOF:
2629
0
    case ZEND_JMPZ_EX:
2630
0
    case ZEND_JMPNZ_EX:
2631
0
    case ZEND_CASE:
2632
0
    case ZEND_CASE_STRICT:
2633
0
    case ZEND_BOOL:
2634
0
    case ZEND_ISSET_ISEMPTY_CV:
2635
0
    case ZEND_ISSET_ISEMPTY_VAR:
2636
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2637
0
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2638
0
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2639
0
    case ZEND_ASSERT_CHECK:
2640
0
    case ZEND_IN_ARRAY:
2641
0
    case ZEND_ARRAY_KEY_EXISTS:
2642
0
      UPDATE_SSA_TYPE(MAY_BE_FALSE|MAY_BE_TRUE, ssa_op->result_def);
2643
0
      break;
2644
0
    case ZEND_CAST:
2645
0
      if (ssa_op->op1_def >= 0) {
2646
0
        tmp = t1;
2647
0
        if ((t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) &&
2648
0
            (opline->extended_value == IS_ARRAY ||
2649
0
             opline->extended_value == IS_OBJECT)) {
2650
0
          tmp |= MAY_BE_RCN;
2651
0
        } else if ((t1 & MAY_BE_STRING) &&
2652
0
            opline->extended_value == IS_STRING) {
2653
0
          tmp |= MAY_BE_RCN;
2654
0
        }
2655
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2656
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
2657
0
      }
2658
0
      tmp = 1 << opline->extended_value;
2659
0
      if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2660
0
        if ((tmp & MAY_BE_ANY) == (t1 & MAY_BE_ANY)) {
2661
0
          tmp |= (t1 & MAY_BE_RC1) | MAY_BE_RCN;
2662
0
        } else if ((opline->extended_value == IS_ARRAY ||
2663
0
              opline->extended_value == IS_OBJECT) &&
2664
0
               (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT))) {
2665
0
            tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2666
0
        } else if (opline->extended_value == IS_STRING &&
2667
0
               (t1 & (MAY_BE_STRING|MAY_BE_OBJECT))) {
2668
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2669
0
        } else {
2670
0
          tmp |= MAY_BE_RC1;
2671
0
          if (opline->extended_value == IS_ARRAY
2672
0
           && (t1 & (MAY_BE_UNDEF|MAY_BE_NULL))) {
2673
0
            tmp |= MAY_BE_RCN;
2674
0
          }
2675
0
        }
2676
0
      }
2677
0
      if (opline->extended_value == IS_ARRAY) {
2678
0
        if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL)) {
2679
0
          tmp |= MAY_BE_ARRAY_EMPTY;
2680
0
        }
2681
0
        if (t1 & MAY_BE_ARRAY) {
2682
0
          tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF);
2683
0
        }
2684
0
        if (t1 & MAY_BE_OBJECT) {
2685
0
          tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2686
0
        } else if (t1 & (MAY_BE_ANY - MAY_BE_NULL)) {
2687
0
          tmp |= ((t1 & (MAY_BE_ANY - MAY_BE_NULL)) << MAY_BE_ARRAY_SHIFT) | ((t1 & MAY_BE_NULL) ? MAY_BE_ARRAY_KEY_LONG : MAY_BE_ARRAY_PACKED);
2688
0
        }
2689
0
      }
2690
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2691
0
      break;
2692
0
    case ZEND_QM_ASSIGN:
2693
0
    case ZEND_JMP_SET:
2694
0
    case ZEND_COALESCE:
2695
0
    case ZEND_COPY_TMP:
2696
0
      if (ssa_op->op1_def >= 0) {
2697
0
        tmp = t1;
2698
0
        if (t1 & (MAY_BE_RC1|MAY_BE_REF)) {
2699
0
          tmp |= MAY_BE_RCN;
2700
0
        }
2701
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2702
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
2703
0
      }
2704
0
      tmp = t1 & ~MAY_BE_UNDEF;
2705
0
      if (opline->opcode != ZEND_COPY_TMP || opline->op1_type != IS_VAR) {
2706
0
        tmp &= ~MAY_BE_REF;
2707
0
      }
2708
0
      if (t1 & MAY_BE_UNDEF) {
2709
0
        tmp |= MAY_BE_NULL;
2710
0
      }
2711
0
      if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2712
0
        tmp |= (t1 & (MAY_BE_RC1|MAY_BE_RCN));
2713
0
        if (opline->opcode == ZEND_COPY_TMP || opline->op1_type == IS_CV) {
2714
0
          tmp |= MAY_BE_RCN;
2715
0
        }
2716
0
      }
2717
0
      if (opline->opcode == ZEND_COALESCE || opline->opcode == ZEND_JMP_SET) {
2718
        /* COALESCE and JMP_SET result can't be null */
2719
0
        tmp &= ~MAY_BE_NULL;
2720
0
        if (opline->opcode == ZEND_JMP_SET) {
2721
          /* JMP_SET result can't be false either */
2722
0
          tmp &= ~MAY_BE_FALSE;
2723
0
        }
2724
0
      }
2725
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2726
0
      COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->result_def);
2727
0
      break;
2728
0
    case ZEND_JMP_NULL:
2729
0
    {
2730
0
      uint32_t short_circuiting_type = opline->extended_value & ZEND_SHORT_CIRCUITING_CHAIN_MASK;
2731
0
      if (short_circuiting_type == ZEND_SHORT_CIRCUITING_CHAIN_EXPR) {
2732
0
        tmp = MAY_BE_NULL;
2733
0
      } else if (short_circuiting_type == ZEND_SHORT_CIRCUITING_CHAIN_ISSET) {
2734
0
        tmp = MAY_BE_FALSE;
2735
0
      } else {
2736
0
        ZEND_ASSERT(short_circuiting_type == ZEND_SHORT_CIRCUITING_CHAIN_EMPTY);
2737
0
        tmp = MAY_BE_TRUE;
2738
0
      }
2739
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2740
0
      break;
2741
0
    }
2742
0
    case ZEND_ASSIGN_OP:
2743
0
    case ZEND_ASSIGN_DIM_OP:
2744
0
    case ZEND_ASSIGN_OBJ_OP:
2745
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
2746
0
    {
2747
0
      const zend_property_info *prop_info = NULL;
2748
0
      orig = 0;
2749
0
      tmp = 0;
2750
0
      if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
2751
0
        prop_info = zend_fetch_prop_info(op_array, ssa, opline, ssa_op);
2752
0
        orig = t1;
2753
0
        t1 = zend_fetch_prop_type(script, prop_info, NULL);
2754
0
        t2 = OP1_DATA_INFO();
2755
0
      } else if (opline->opcode == ZEND_ASSIGN_DIM_OP) {
2756
0
        if (t1 & MAY_BE_ARRAY_OF_REF) {
2757
0
              tmp |= MAY_BE_REF;
2758
0
        }
2759
0
        orig = t1;
2760
0
        t1 = zend_array_element_type(t1, opline->op1_type, 1, 0);
2761
0
        t2 = OP1_DATA_INFO();
2762
0
      } else if (opline->opcode == ZEND_ASSIGN_STATIC_PROP_OP) {
2763
0
        prop_info = zend_fetch_static_prop_info(script, op_array, ssa, opline);
2764
0
        t1 = zend_fetch_prop_type(script, prop_info, NULL);
2765
0
        t2 = OP1_DATA_INFO();
2766
0
      } else {
2767
0
        if (t1 & MAY_BE_REF) {
2768
0
              tmp |= MAY_BE_REF;
2769
0
        }
2770
0
      }
2771
2772
0
      tmp |= binary_op_result_type(
2773
0
        ssa, opline->extended_value, t1, t2,
2774
0
        opline->opcode == ZEND_ASSIGN_OP ? ssa_op->op1_def : -1, optimization_level);
2775
0
      if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY)) {
2776
0
        tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2777
0
      }
2778
0
      if (tmp & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2779
0
        tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2780
0
      }
2781
2782
0
      if (opline->opcode == ZEND_ASSIGN_DIM_OP) {
2783
0
        if (opline->op1_type == IS_CV) {
2784
0
          orig = assign_dim_result_type(orig, OP2_INFO(), tmp, opline->op2_type);
2785
0
          UPDATE_SSA_TYPE(orig, ssa_op->op1_def);
2786
0
          COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
2787
0
        }
2788
0
      } else if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
2789
0
        if (opline->op1_type == IS_CV) {
2790
0
          orig = (orig & (MAY_BE_REF|MAY_BE_OBJECT))|MAY_BE_RC1|MAY_BE_RCN;
2791
0
          UPDATE_SSA_TYPE(orig, ssa_op->op1_def);
2792
0
          COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
2793
0
        }
2794
0
      } else if (opline->opcode == ZEND_ASSIGN_STATIC_PROP_OP) {
2795
        /* Nothing to do */
2796
0
      } else {
2797
0
        if (opline->opcode == ZEND_ASSIGN_OP && ssa_op->result_def >= 0 && (tmp & MAY_BE_RC1)) {
2798
0
          tmp |= MAY_BE_RCN;
2799
0
        }
2800
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2801
0
      }
2802
0
      if (ssa_op->result_def >= 0) {
2803
0
        ce = NULL;
2804
0
        if (opline->opcode == ZEND_ASSIGN_DIM_OP) {
2805
0
          if (opline->op2_type == IS_UNUSED) {
2806
            /* When appending to an array and the LONG_MAX key is already used
2807
             * null will be returned. */
2808
0
            tmp |= MAY_BE_NULL;
2809
0
          }
2810
0
          if (t2 & (MAY_BE_ARRAY | MAY_BE_OBJECT)) {
2811
            /* Arrays and objects cannot be used as keys. */
2812
0
            tmp |= MAY_BE_NULL;
2813
0
          }
2814
0
          if (t1 & (MAY_BE_ANY - (MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_STRING | MAY_BE_ARRAY))) {
2815
            /* null and false are implicitly converted to array, anything else
2816
             * results in a null return value. */
2817
0
            tmp |= MAY_BE_NULL;
2818
0
          }
2819
0
          if (tmp & MAY_BE_REF) {
2820
            /* Typed reference may cause auto conversion */
2821
0
            tmp |= MAY_BE_ANY;
2822
0
          }
2823
0
        } else if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
2824
          /* The return value must also satisfy the property type */
2825
0
          if (prop_info) {
2826
0
            t1 = zend_fetch_prop_type(script, prop_info, &ce);
2827
0
            if ((t1 & (MAY_BE_LONG|MAY_BE_DOUBLE)) == MAY_BE_LONG
2828
0
             && (tmp & (MAY_BE_LONG|MAY_BE_DOUBLE)) == MAY_BE_DOUBLE) {
2829
              /* DOUBLE may be auto-converted to LONG */
2830
0
              tmp |= MAY_BE_LONG;
2831
0
              tmp &= ~MAY_BE_DOUBLE;
2832
0
            } else if ((t1 & (MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING)) == MAY_BE_STRING
2833
0
             && (tmp & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
2834
              /* LONG/DOUBLE may be auto-converted to STRING */
2835
0
              tmp |= MAY_BE_STRING;
2836
0
              tmp &= ~(MAY_BE_LONG|MAY_BE_DOUBLE);
2837
0
            }
2838
0
            tmp &= t1;
2839
0
          } else {
2840
0
            tmp |= MAY_BE_LONG | MAY_BE_STRING;
2841
0
          }
2842
0
        } else if (opline->opcode == ZEND_ASSIGN_STATIC_PROP_OP) {
2843
          /* The return value must also satisfy the property type */
2844
0
          if (prop_info) {
2845
0
            t1 = zend_fetch_prop_type(script, prop_info, &ce);
2846
0
            if ((t1 & (MAY_BE_LONG|MAY_BE_DOUBLE)) == MAY_BE_LONG
2847
0
             && (tmp & (MAY_BE_LONG|MAY_BE_DOUBLE)) == MAY_BE_DOUBLE) {
2848
              /* DOUBLE may be auto-converted to LONG */
2849
0
              tmp |= MAY_BE_LONG;
2850
0
              tmp &= ~MAY_BE_DOUBLE;
2851
0
            } else if ((t1 & (MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING)) == MAY_BE_STRING
2852
0
             && (tmp & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
2853
              /* LONG/DOUBLE may be auto-converted to STRING */
2854
0
              tmp |= MAY_BE_STRING;
2855
0
              tmp &= ~(MAY_BE_LONG|MAY_BE_DOUBLE);
2856
0
            }
2857
0
            tmp &= t1;
2858
0
          } else {
2859
0
            tmp |= MAY_BE_LONG | MAY_BE_STRING;
2860
0
          }
2861
0
        } else {
2862
0
          if (tmp & MAY_BE_REF) {
2863
            /* Typed reference may cause auto conversion */
2864
0
            tmp |= MAY_BE_ANY;
2865
0
          }
2866
0
        }
2867
0
        tmp &= ~MAY_BE_REF;
2868
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2869
0
        if (ce) {
2870
0
          UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
2871
0
        }
2872
0
      }
2873
0
      break;
2874
0
    }
2875
0
    case ZEND_PRE_INC:
2876
0
    case ZEND_PRE_DEC:
2877
0
      tmp = 0;
2878
0
      if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2879
0
        tmp |= MAY_BE_RC1;
2880
0
        if (ssa_op->result_def >= 0) {
2881
0
          tmp |= MAY_BE_RCN;
2882
0
        }
2883
0
      }
2884
0
      if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
2885
0
        if (!ssa_var_info[ssa_op->op1_use].has_range ||
2886
0
            (opline->opcode == ZEND_PRE_DEC &&
2887
0
             (ssa_var_info[ssa_op->op1_use].range.underflow ||
2888
0
              ssa_var_info[ssa_op->op1_use].range.min == ZEND_LONG_MIN)) ||
2889
0
             (opline->opcode == ZEND_PRE_INC &&
2890
0
              (ssa_var_info[ssa_op->op1_use].range.overflow ||
2891
0
               ssa_var_info[ssa_op->op1_use].range.max == ZEND_LONG_MAX))) {
2892
          /* may overflow */
2893
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2894
0
        } else {
2895
0
          tmp |= MAY_BE_LONG;
2896
0
        }
2897
0
      } else {
2898
0
        if (t1 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
2899
0
          if (opline->opcode == ZEND_PRE_INC) {
2900
0
            tmp |= MAY_BE_LONG;
2901
0
          } else {
2902
0
            tmp |= MAY_BE_NULL;
2903
0
          }
2904
0
        }
2905
0
        if (t1 & MAY_BE_LONG) {
2906
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2907
0
        }
2908
0
        if (t1 & MAY_BE_DOUBLE) {
2909
0
          tmp |= MAY_BE_DOUBLE;
2910
0
        }
2911
0
        if (t1 & MAY_BE_STRING) {
2912
0
          tmp |= MAY_BE_STRING | MAY_BE_LONG | MAY_BE_DOUBLE;
2913
0
        }
2914
0
        tmp |= t1 & (MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_OBJECT);
2915
0
      }
2916
0
      if (ssa_op->result_def >= 0) {
2917
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2918
0
      }
2919
0
      if (ssa_op->op1_def >= 0) {
2920
0
        if (t1 & MAY_BE_REF) {
2921
0
          tmp |= MAY_BE_REF;
2922
0
        }
2923
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2924
0
      }
2925
0
      break;
2926
0
    case ZEND_POST_INC:
2927
0
    case ZEND_POST_DEC:
2928
0
      if (ssa_op->result_def >= 0) {
2929
0
        tmp = 0;
2930
0
        if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2931
0
          tmp |= MAY_BE_RC1|MAY_BE_RCN;
2932
0
        }
2933
0
        tmp |= t1 & ~(MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_RCN);
2934
0
        if (t1 & MAY_BE_UNDEF) {
2935
0
          tmp |= MAY_BE_NULL;
2936
0
        }
2937
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
2938
0
      }
2939
0
      tmp = 0;
2940
0
      if (t1 & MAY_BE_REF) {
2941
0
        tmp |= MAY_BE_REF;
2942
0
      }
2943
0
      if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2944
0
        tmp |= MAY_BE_RC1;
2945
0
      }
2946
0
      if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
2947
0
        if (!ssa_var_info[ssa_op->op1_use].has_range ||
2948
0
             (opline->opcode == ZEND_POST_DEC &&
2949
0
              (ssa_var_info[ssa_op->op1_use].range.underflow ||
2950
0
               ssa_var_info[ssa_op->op1_use].range.min == ZEND_LONG_MIN)) ||
2951
0
              (opline->opcode == ZEND_POST_INC &&
2952
0
               (ssa_var_info[ssa_op->op1_use].range.overflow ||
2953
0
                ssa_var_info[ssa_op->op1_use].range.max == ZEND_LONG_MAX))) {
2954
          /* may overflow */
2955
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2956
0
        } else {
2957
0
          tmp |= MAY_BE_LONG;
2958
0
        }
2959
0
      } else {
2960
0
        if (t1 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
2961
0
          if (opline->opcode == ZEND_POST_INC) {
2962
0
            tmp |= MAY_BE_LONG;
2963
0
          } else {
2964
0
            tmp |= MAY_BE_NULL;
2965
0
          }
2966
0
        }
2967
0
        if (t1 & MAY_BE_LONG) {
2968
0
          tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2969
0
        }
2970
0
        if (t1 & MAY_BE_DOUBLE) {
2971
0
          tmp |= MAY_BE_DOUBLE;
2972
0
        }
2973
0
        if (t1 & MAY_BE_STRING) {
2974
0
          tmp |= MAY_BE_STRING | MAY_BE_LONG | MAY_BE_DOUBLE;
2975
0
        }
2976
0
        tmp |= t1 & (MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_RESOURCE | MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_KEY_ANY);
2977
0
      }
2978
0
      if (ssa_op->op1_def >= 0) {
2979
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2980
0
      }
2981
0
      break;
2982
0
    case ZEND_ASSIGN_DIM:
2983
0
      if (opline->op1_type == IS_CV) {
2984
0
        tmp = assign_dim_result_type(t1, t2, OP1_DATA_INFO(), opline->op2_type);
2985
0
        tmp |= ssa->var_info[ssa_op->op1_def].type & (MAY_BE_ARRAY_PACKED|MAY_BE_ARRAY_NUMERIC_HASH|MAY_BE_ARRAY_STRING_HASH);
2986
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
2987
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
2988
0
      }
2989
0
      if (ssa_op->result_def >= 0) {
2990
0
        tmp = 0;
2991
0
        if (t1 & MAY_BE_STRING) {
2992
0
          tmp |= MAY_BE_STRING | MAY_BE_NULL;
2993
0
        }
2994
0
        if (t1 & MAY_BE_OBJECT) {
2995
0
          tmp |= (MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF);
2996
0
        }
2997
0
        if (t1 & (MAY_BE_ARRAY|MAY_BE_FALSE|MAY_BE_NULL|MAY_BE_UNDEF)) {
2998
0
          tmp |= (OP1_DATA_INFO() & (MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF));
2999
3000
0
          if (OP1_DATA_INFO() & MAY_BE_UNDEF) {
3001
0
            tmp |= MAY_BE_NULL;
3002
0
          }
3003
0
          if (t1 & MAY_BE_ARRAY_OF_REF) {
3004
            /* A scalar type conversion may occur when assigning to a typed reference. */
3005
0
            tmp |= MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING;
3006
0
          }
3007
0
        }
3008
0
        if (t1 & (MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE)) {
3009
0
          tmp |= MAY_BE_NULL;
3010
0
        }
3011
0
        tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3012
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3013
0
      }
3014
0
      if ((ssa_op+1)->op1_def >= 0) {
3015
0
        opline++;
3016
0
        ssa_op++;
3017
0
        tmp = OP1_INFO();
3018
0
        if (tmp & (MAY_BE_ANY | MAY_BE_REF)) {
3019
0
          if (tmp & MAY_BE_RC1) {
3020
0
            tmp |= MAY_BE_RCN;
3021
0
          }
3022
0
        }
3023
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3024
0
      }
3025
0
      break;
3026
0
    case ZEND_ASSIGN_OBJ:
3027
0
      if (opline->op1_type == IS_CV) {
3028
0
        const zend_class_entry *ce = ssa_var_info[ssa_op->op1_use].ce;
3029
0
        bool add_rc = (t1 & (MAY_BE_OBJECT|MAY_BE_REF)) && (!ce
3030
0
          || ce->__set
3031
          /* Non-default write_property may be set within create_object. */
3032
0
          || ce->create_object
3033
0
          || ce->default_object_handlers->write_property != zend_std_write_property
3034
0
          || ssa_var_info[ssa_op->op1_use].is_instanceof);
3035
0
        tmp = (t1 & (MAY_BE_REF|MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN))|(add_rc ? (MAY_BE_RC1|MAY_BE_RCN) : 0);
3036
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3037
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3038
0
      }
3039
0
      if (ssa_op->result_def >= 0) {
3040
        // TODO: If there is no __set we might do better
3041
0
        tmp = zend_fetch_prop_type(script,
3042
0
          zend_fetch_prop_info(op_array, ssa, opline, ssa_op), &ce);
3043
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3044
0
        if (ce) {
3045
0
          UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
3046
0
        }
3047
0
      }
3048
0
      if ((ssa_op+1)->op1_def >= 0) {
3049
0
        opline++;
3050
0
        ssa_op++;
3051
0
        tmp = OP1_INFO();
3052
0
        if (tmp & MAY_BE_RC1) {
3053
0
          tmp |= MAY_BE_RCN;
3054
0
        }
3055
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3056
0
      }
3057
0
      break;
3058
0
    case ZEND_ASSIGN_STATIC_PROP:
3059
0
      if (ssa_op->result_def >= 0) {
3060
0
        tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_RC1 | MAY_BE_RCN;
3061
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3062
0
      }
3063
0
      if ((ssa_op+1)->op1_def >= 0) {
3064
0
        opline++;
3065
0
        ssa_op++;
3066
0
        tmp = OP1_INFO();
3067
0
        if (tmp & MAY_BE_RC1) {
3068
0
          tmp |= MAY_BE_RCN;
3069
0
        }
3070
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3071
0
      }
3072
0
      break;
3073
0
    case ZEND_PRE_INC_OBJ:
3074
0
    case ZEND_PRE_DEC_OBJ:
3075
0
    case ZEND_POST_INC_OBJ:
3076
0
    case ZEND_POST_DEC_OBJ:
3077
0
      if (opline->op1_type == IS_CV) {
3078
0
        tmp = (t1 & (MAY_BE_REF|MAY_BE_OBJECT))|MAY_BE_RC1|MAY_BE_RCN;
3079
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3080
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3081
0
      }
3082
0
      if (ssa_op->result_def >= 0) {
3083
        // TODO: ???
3084
0
        tmp = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3085
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3086
0
      }
3087
0
      break;
3088
0
    case ZEND_ASSIGN:
3089
0
      if (ssa_op->op2_def >= 0) {
3090
0
        tmp = t2;
3091
0
        if (tmp & MAY_BE_RC1) {
3092
0
          tmp |= MAY_BE_RCN;
3093
0
        }
3094
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
3095
0
      }
3096
0
      tmp = t2 & ~(MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN);
3097
0
      if (t2 & MAY_BE_UNDEF) {
3098
0
        tmp |= MAY_BE_NULL;
3099
0
      }
3100
0
      if (t1 & MAY_BE_REF) {
3101
0
        tmp |= MAY_BE_REF;
3102
0
      }
3103
0
      if (t2 & MAY_BE_REF) {
3104
0
        tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3105
0
      } else if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
3106
0
        tmp |= t2 & (MAY_BE_RC1|MAY_BE_RCN);
3107
0
      } else if (t2 & (MAY_BE_RC1|MAY_BE_RCN)) {
3108
0
        tmp |= MAY_BE_RCN;
3109
0
      }
3110
0
      if (RETURN_VALUE_USED(opline) && (tmp & MAY_BE_RC1)) {
3111
0
        tmp |= MAY_BE_RCN;
3112
0
      }
3113
0
      if (ssa_op->op1_def >= 0) {
3114
0
        if (ssa_var_info[ssa_op->op1_def].use_as_double) {
3115
0
          tmp &= ~MAY_BE_LONG;
3116
0
          tmp |= MAY_BE_DOUBLE;
3117
0
        }
3118
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3119
0
        COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->op1_def);
3120
0
      }
3121
0
      if (ssa_op->result_def >= 0) {
3122
0
        if (tmp & MAY_BE_REF) {
3123
          /* A scalar type conversion may occur when assigning to a typed reference. */
3124
0
          tmp &= ~MAY_BE_REF;
3125
0
          tmp |= MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN;
3126
0
        }
3127
0
        if ((tmp & (MAY_BE_RC1|MAY_BE_RCN)) == MAY_BE_RCN) {
3128
          /* refcount may be indirectly decremented. Make an exception if the result is used in the next instruction */
3129
0
          if (!ssa_opcodes) {
3130
0
            if (ssa->vars[ssa_op->result_def].use_chain < 0
3131
0
             || opline + 1 != op_array->opcodes + ssa->vars[ssa_op->result_def].use_chain) {
3132
0
              tmp |= MAY_BE_RC1;
3133
0
              }
3134
0
          } else {
3135
0
            if (ssa->vars[ssa_op->result_def].use_chain < 0
3136
0
             || opline + 1 != ssa_opcodes[ssa->vars[ssa_op->result_def].use_chain]) {
3137
0
              tmp |= MAY_BE_RC1;
3138
0
              }
3139
0
          }
3140
0
        }
3141
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3142
0
        COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->result_def);
3143
0
      }
3144
0
      break;
3145
0
    case ZEND_ASSIGN_REF:
3146
// TODO: ???
3147
0
      if (opline->op2_type == IS_CV) {
3148
0
        tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3149
0
        if (t2 & MAY_BE_UNDEF) {
3150
0
          tmp |= MAY_BE_NULL;
3151
0
        }
3152
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
3153
0
      }
3154
0
      if (opline->op2_type == IS_VAR && opline->extended_value == ZEND_RETURNS_FUNCTION) {
3155
0
        tmp = (MAY_BE_REF | MAY_BE_RCN | MAY_BE_RC1 | t2) & ~MAY_BE_UNDEF;
3156
0
      } else {
3157
0
        tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3158
0
      }
3159
0
      if (t2 & MAY_BE_UNDEF) {
3160
0
        tmp |= MAY_BE_NULL;
3161
0
      }
3162
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3163
0
      if (ssa_op->result_def >= 0) {
3164
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3165
0
      }
3166
0
      break;
3167
0
    case ZEND_ASSIGN_OBJ_REF:
3168
0
      if (opline->op1_type == IS_CV && ssa_op->op1_def >= 0) {
3169
0
        tmp = t1;
3170
0
        if (tmp & MAY_BE_OBJECT) {
3171
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3172
0
        }
3173
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3174
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3175
0
      }
3176
3177
0
      t2 = OP1_DATA_INFO();
3178
0
      if ((opline+1)->op1_type == IS_VAR && (opline->extended_value & ZEND_RETURNS_FUNCTION)) {
3179
0
        tmp = (MAY_BE_REF | MAY_BE_RCN | MAY_BE_RC1 | t2) & ~MAY_BE_UNDEF;
3180
0
      } else {
3181
0
        tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3182
0
      }
3183
0
      if (t2 & MAY_BE_UNDEF) {
3184
0
        tmp |= MAY_BE_NULL;
3185
0
      }
3186
0
      if (ssa_op->result_def >= 0) {
3187
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3188
0
      }
3189
0
      if ((opline+1)->op1_type == IS_CV) {
3190
0
        opline++;
3191
0
        ssa_op++;
3192
0
        tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3193
0
        if (t2 & MAY_BE_UNDEF) {
3194
0
          tmp |= MAY_BE_NULL;
3195
0
        }
3196
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3197
0
      }
3198
0
      break;
3199
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
3200
0
      if (ssa_op->result_def >= 0) {
3201
0
        UPDATE_SSA_TYPE(MAY_BE_REF, ssa_op->result_def);
3202
0
      }
3203
0
      if ((opline+1)->op1_type == IS_CV) {
3204
0
        opline++;
3205
0
        ssa_op++;
3206
0
        UPDATE_SSA_TYPE(MAY_BE_REF, ssa_op->op1_def);
3207
0
      }
3208
0
      break;
3209
0
    case ZEND_BIND_GLOBAL:
3210
0
      tmp = MAY_BE_REF | MAY_BE_ANY
3211
0
        | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3212
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3213
0
      break;
3214
0
    case ZEND_BIND_STATIC:
3215
0
      tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
3216
0
        | ((opline->extended_value & ZEND_BIND_REF) ? MAY_BE_REF : (MAY_BE_RC1 | MAY_BE_RCN));
3217
0
      if (opline->extended_value & ZEND_BIND_IMPLICIT) {
3218
0
        tmp |= MAY_BE_UNDEF;
3219
0
      }
3220
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3221
0
      break;
3222
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
3223
0
      tmp = MAY_BE_UNDEF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_REF;
3224
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3225
0
      break;
3226
0
    case ZEND_SEND_VAR:
3227
0
      if (ssa_op->op1_def >= 0) {
3228
0
        tmp = t1;
3229
0
        if (t1 & (MAY_BE_RC1|MAY_BE_REF)) {
3230
0
          tmp |= MAY_BE_RCN;
3231
0
        }
3232
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3233
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3234
0
      }
3235
0
      break;
3236
0
    case ZEND_BIND_LEXICAL:
3237
0
      if (ssa_op->op2_def >= 0) {
3238
0
        if (opline->extended_value & ZEND_BIND_REF) {
3239
0
          tmp = t2 | MAY_BE_REF;
3240
0
        } else {
3241
0
          tmp = t2 & ~(MAY_BE_RC1|MAY_BE_RCN);
3242
0
          if (t2 & (MAY_BE_RC1|MAY_BE_RCN)) {
3243
0
            tmp |= MAY_BE_RCN;
3244
0
          }
3245
0
        }
3246
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
3247
0
        COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->op2_def);
3248
0
      }
3249
0
      break;
3250
0
    case ZEND_YIELD:
3251
0
      if (ssa_op->op1_def >= 0) {
3252
0
        if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
3253
0
          tmp = t1 | MAY_BE_REF;
3254
0
        } else {
3255
0
          tmp = t1 & ~(MAY_BE_RC1|MAY_BE_RCN);
3256
0
          if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
3257
0
            tmp |= MAY_BE_RCN;
3258
0
          }
3259
0
        }
3260
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3261
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3262
0
      }
3263
0
      if (ssa_op->result_def >= 0) {
3264
0
        tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
3265
0
          | MAY_BE_RC1 | MAY_BE_RCN;
3266
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3267
0
      }
3268
0
      break;
3269
0
    case ZEND_SEND_VAR_EX:
3270
0
    case ZEND_SEND_FUNC_ARG:
3271
0
      if (ssa_op->op1_def >= 0) {
3272
0
        tmp = (t1 & MAY_BE_UNDEF)|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
3273
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3274
0
      }
3275
0
      break;
3276
0
    case ZEND_SEND_REF:
3277
0
      if (ssa_op->op1_def >= 0) {
3278
0
        tmp = MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
3279
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3280
0
      }
3281
0
      break;
3282
0
    case ZEND_SEND_UNPACK:
3283
0
      if (ssa_op->op1_def >= 0) {
3284
0
        tmp = t1;
3285
0
        if (t1 & MAY_BE_ARRAY) {
3286
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3287
0
          if (t1 & MAY_BE_ARRAY_OF_ANY) {
3288
            /* SEND_UNPACK may acquire references into the array */
3289
0
            tmp |= MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3290
0
          }
3291
0
        }
3292
0
        if (t1 & MAY_BE_OBJECT) {
3293
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3294
0
        }
3295
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3296
0
      }
3297
0
      break;
3298
0
    case ZEND_FAST_CONCAT:
3299
0
    case ZEND_ROPE_INIT:
3300
0
    case ZEND_ROPE_ADD:
3301
0
    case ZEND_ROPE_END:
3302
0
      UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN, ssa_op->result_def);
3303
0
      break;
3304
0
    case ZEND_RECV:
3305
0
    case ZEND_RECV_INIT:
3306
0
    case ZEND_RECV_VARIADIC:
3307
0
    {
3308
      /* Typehinting */
3309
0
      zend_arg_info *arg_info = &op_array->arg_info[opline->op1.num-1];
3310
3311
0
      ce = NULL;
3312
0
      tmp = zend_fetch_arg_info_type(script, arg_info, &ce);
3313
0
      if (ZEND_ARG_SEND_MODE(arg_info)) {
3314
0
        tmp |= MAY_BE_REF;
3315
0
        ce = NULL;
3316
0
      }
3317
3318
0
      if (opline->opcode == ZEND_RECV_VARIADIC) {
3319
0
        uint32_t elem_type = tmp & MAY_BE_REF
3320
0
          ? MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF
3321
0
          : (tmp & MAY_BE_ANY) << MAY_BE_ARRAY_SHIFT;
3322
0
        tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|elem_type;
3323
0
        ce = NULL;
3324
0
      }
3325
3326
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3327
0
      if (ce) {
3328
0
        UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
3329
0
      } else {
3330
0
        UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3331
0
      }
3332
0
      break;
3333
0
    }
3334
0
    case ZEND_DECLARE_ANON_CLASS:
3335
0
      UPDATE_SSA_TYPE(MAY_BE_CLASS, ssa_op->result_def);
3336
0
      if (script && (ce = zend_hash_find_ptr(&script->class_table, Z_STR_P(CRT_CONSTANT(opline->op1)))) != NULL) {
3337
0
        UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_op->result_def);
3338
0
      }
3339
0
      break;
3340
0
    case ZEND_FETCH_CLASS:
3341
0
      UPDATE_SSA_TYPE(MAY_BE_CLASS, ssa_op->result_def);
3342
0
      if (opline->op2_type == IS_UNUSED) {
3343
0
        switch (opline->op1.num & ZEND_FETCH_CLASS_MASK) {
3344
0
          case ZEND_FETCH_CLASS_SELF:
3345
0
            if (op_array->scope) {
3346
0
              UPDATE_SSA_OBJ_TYPE(op_array->scope, 0, ssa_op->result_def);
3347
0
            } else {
3348
0
              UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3349
0
            }
3350
0
            break;
3351
0
          case ZEND_FETCH_CLASS_PARENT:
3352
0
            if (op_array->scope && op_array->scope->parent && (op_array->scope->ce_flags & ZEND_ACC_LINKED)) {
3353
0
              UPDATE_SSA_OBJ_TYPE(op_array->scope->parent, 0, ssa_op->result_def);
3354
0
            } else {
3355
0
              UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3356
0
            }
3357
0
            break;
3358
0
          case ZEND_FETCH_CLASS_STATIC:
3359
0
            if (op_array->scope && (op_array->scope->ce_flags & ZEND_ACC_FINAL)) {
3360
0
              UPDATE_SSA_OBJ_TYPE(op_array->scope, 0, ssa_op->result_def);
3361
0
            } else {
3362
0
              UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3363
0
            }
3364
0
            break;
3365
0
          default:
3366
0
            UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3367
0
            break;
3368
0
        }
3369
0
      } else if (opline->op2_type == IS_CONST) {
3370
0
        zval *zv = CRT_CONSTANT(opline->op2);
3371
0
        if (Z_TYPE_P(zv) == IS_STRING) {
3372
0
          ce = zend_optimizer_get_class_entry(script, op_array, Z_STR_P(zv+1));
3373
0
          UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_op->result_def);
3374
0
        } else {
3375
0
          UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3376
0
        }
3377
0
      } else {
3378
0
        COPY_SSA_OBJ_TYPE(ssa_op->op2_use, ssa_op->result_def);
3379
0
      }
3380
0
      break;
3381
0
    case ZEND_NEW:
3382
0
      tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT;
3383
0
      ce = zend_optimizer_get_class_entry_from_op1(script, op_array, opline);
3384
0
      if (ce) {
3385
0
        UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_op->result_def);
3386
0
      } else if ((t1 & MAY_BE_CLASS) && ssa_op->op1_use >= 0 && ssa_var_info[ssa_op->op1_use].ce) {
3387
0
        UPDATE_SSA_OBJ_TYPE(ssa_var_info[ssa_op->op1_use].ce, ssa_var_info[ssa_op->op1_use].is_instanceof, ssa_op->result_def);
3388
0
        if (!ssa_var_info[ssa_op->result_def].is_instanceof) {
3389
0
          ce = ssa_var_info[ssa_op->op1_use].ce;
3390
0
        }
3391
0
      } else {
3392
0
        UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3393
0
      }
3394
      /* New objects without constructors cannot escape. */
3395
0
      if (ce
3396
0
       && !ce->constructor
3397
0
       && !ce->create_object
3398
0
       && ce->default_object_handlers->get_constructor == zend_std_get_constructor) {
3399
0
        tmp &= ~MAY_BE_RCN;
3400
0
      }
3401
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3402
0
      break;
3403
0
    case ZEND_CLONE:
3404
0
      UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT, ssa_op->result_def);
3405
0
      COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->result_def);
3406
0
      break;
3407
0
    case ZEND_INIT_ARRAY:
3408
0
    case ZEND_ADD_ARRAY_ELEMENT:
3409
0
      if (ssa_op->op1_def >= 0) {
3410
0
        if (opline->extended_value & ZEND_ARRAY_ELEMENT_REF) {
3411
0
          tmp = (MAY_BE_REF | t1) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3412
0
          if (t1 & MAY_BE_UNDEF) {
3413
0
            tmp |= MAY_BE_NULL;
3414
0
          }
3415
0
        } else if ((t1 & (MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN)) == MAY_BE_REF) {
3416
0
          tmp = (MAY_BE_REF | t1) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3417
0
          if (t1 & MAY_BE_UNDEF) {
3418
0
            tmp |= MAY_BE_NULL;
3419
0
          }
3420
0
        } else if (t1 & MAY_BE_REF) {
3421
0
          tmp = (MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | t1);
3422
0
        } else {
3423
0
          tmp = t1;
3424
0
          if (t1 & MAY_BE_RC1) {
3425
0
            tmp |= MAY_BE_RCN;
3426
0
          }
3427
0
        }
3428
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3429
0
      }
3430
0
      if (ssa_op->result_def >= 0) {
3431
0
        uint32_t arr_type;
3432
0
        if (opline->opcode == ZEND_INIT_ARRAY) {
3433
0
          arr_type = 0;
3434
0
        } else {
3435
0
          arr_type = RES_USE_INFO();
3436
0
        }
3437
0
        tmp = MAY_BE_RC1|MAY_BE_ARRAY|arr_type;
3438
0
        if (opline->opcode == ZEND_INIT_ARRAY && opline->op1_type == IS_UNUSED) {
3439
0
          tmp |= MAY_BE_ARRAY_EMPTY;
3440
0
        }
3441
0
        if (opline->op1_type != IS_UNUSED
3442
0
         && (opline->op2_type == IS_UNUSED
3443
0
          || (t2 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE|MAY_BE_STRING)))) {
3444
0
          tmp |= assign_dim_array_result_type(arr_type, t2, t1, opline->op2_type);
3445
0
          if (opline->extended_value & ZEND_ARRAY_ELEMENT_REF) {
3446
0
            tmp |= MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
3447
0
          }
3448
0
        }
3449
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3450
0
      }
3451
0
      break;
3452
0
    case ZEND_ADD_ARRAY_UNPACK:
3453
0
      tmp = ssa_var_info[ssa_op->result_use].type;
3454
0
      ZEND_ASSERT(tmp & MAY_BE_ARRAY);
3455
0
      tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
3456
0
      if (t1 & MAY_BE_OBJECT) {
3457
0
        tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY;
3458
0
      }
3459
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3460
0
      break;
3461
0
    case ZEND_UNSET_CV:
3462
0
      tmp = MAY_BE_UNDEF;
3463
0
      if (!op_array->function_name) {
3464
        /* In global scope, we know nothing */
3465
0
        tmp |= MAY_BE_REF;
3466
0
      }
3467
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3468
0
      break;
3469
0
    case ZEND_UNSET_DIM:
3470
0
    case ZEND_UNSET_OBJ:
3471
0
      if (ssa_op->op1_def >= 0) {
3472
0
        UPDATE_SSA_TYPE(t1, ssa_op->op1_def);
3473
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3474
0
      }
3475
0
      break;
3476
0
    case ZEND_FE_RESET_R:
3477
0
    case ZEND_FE_RESET_RW:
3478
0
      if (ssa_op->op1_def >= 0) {
3479
0
        tmp = t1;
3480
0
        if (opline->opcode == ZEND_FE_RESET_RW) {
3481
0
          tmp |= MAY_BE_REF;
3482
0
        } else if (t1 & MAY_BE_RC1) {
3483
0
          tmp |= MAY_BE_RCN;
3484
0
        }
3485
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3486
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3487
0
      }
3488
0
      if (opline->opcode == ZEND_FE_RESET_RW) {
3489
//???
3490
0
        tmp = MAY_BE_REF | (t1 & (MAY_BE_ARRAY | MAY_BE_OBJECT));
3491
0
      } else {
3492
0
        tmp = MAY_BE_RC1 | MAY_BE_RCN | (t1 & (MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF));
3493
0
      }
3494
      /* The result is set to UNDEF for invalid foreach inputs. */
3495
0
      if ((t1 & (MAY_BE_ANY | MAY_BE_UNDEF)) & ~(MAY_BE_ARRAY | MAY_BE_OBJECT)) {
3496
0
        tmp |= MAY_BE_UNDEF;
3497
0
      }
3498
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3499
0
      COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->result_def);
3500
0
      break;
3501
0
    case ZEND_FE_FETCH_R:
3502
0
    case ZEND_FE_FETCH_RW:
3503
0
      tmp = 0;
3504
0
      if (opline->op2_type == IS_CV) {
3505
0
        tmp = t2 & MAY_BE_REF;
3506
0
      }
3507
0
      if (t1 & MAY_BE_OBJECT) {
3508
0
        if (opline->opcode == ZEND_FE_FETCH_RW) {
3509
0
          tmp |= MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3510
0
        } else {
3511
0
          tmp |= MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3512
0
          if (opline->op2_type != IS_CV) {
3513
0
            tmp |= MAY_BE_REF;
3514
0
          }
3515
0
        }
3516
0
      }
3517
0
      if (t1 & MAY_BE_ARRAY) {
3518
0
        if (opline->opcode == ZEND_FE_FETCH_RW) {
3519
0
          tmp |= MAY_BE_REF | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3520
0
        } else {
3521
0
          tmp |= ((t1 & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
3522
0
          if (tmp & MAY_BE_ARRAY) {
3523
0
            tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3524
0
          }
3525
0
          if (t1 & MAY_BE_ARRAY_OF_REF) {
3526
0
            tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3527
0
            if (opline->op2_type != IS_CV) {
3528
0
              tmp |= MAY_BE_REF;
3529
0
            }
3530
0
          } else if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3531
0
            tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3532
0
          }
3533
0
        }
3534
0
      }
3535
0
      UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
3536
0
      if (ssa_op->result_def >= 0) {
3537
0
        tmp = (ssa_op->result_use >= 0) ? RES_USE_INFO() : 0;
3538
0
        if (t1 & MAY_BE_OBJECT) {
3539
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3540
0
        }
3541
0
        if (t1 & MAY_BE_ARRAY) {
3542
0
          if (t1 & MAY_BE_ARRAY_KEY_LONG) {
3543
0
            tmp |= MAY_BE_LONG;
3544
0
          }
3545
0
          if (t1 & MAY_BE_ARRAY_KEY_STRING) {
3546
0
            tmp |= MAY_BE_STRING | MAY_BE_RCN;
3547
0
          }
3548
0
        }
3549
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3550
0
      }
3551
0
      break;
3552
0
    case ZEND_FETCH_DIM_R:
3553
0
    case ZEND_FETCH_DIM_IS:
3554
0
    case ZEND_FETCH_DIM_RW:
3555
0
    case ZEND_FETCH_DIM_W:
3556
0
    case ZEND_FETCH_DIM_UNSET:
3557
0
    case ZEND_FETCH_DIM_FUNC_ARG:
3558
0
    case ZEND_FETCH_LIST_R:
3559
0
    case ZEND_FETCH_LIST_W:
3560
0
      if (ssa_op->op1_def >= 0) {
3561
0
        uint32_t key_type = 0;
3562
0
        tmp = t1 & ~(MAY_BE_RC1|MAY_BE_RCN);
3563
0
        if (opline->opcode == ZEND_FETCH_DIM_W ||
3564
0
            opline->opcode == ZEND_FETCH_DIM_RW ||
3565
0
            opline->opcode == ZEND_FETCH_DIM_FUNC_ARG ||
3566
0
            opline->opcode == ZEND_FETCH_LIST_W) {
3567
0
          if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3568
0
            if (opline->opcode != ZEND_FETCH_DIM_FUNC_ARG) {
3569
0
              tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
3570
0
            }
3571
0
            tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
3572
0
          }
3573
0
          if (t1 & (MAY_BE_STRING|MAY_BE_ARRAY)) {
3574
0
            tmp |= MAY_BE_RC1;
3575
0
            if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG) {
3576
0
              tmp |= t1 & MAY_BE_RCN;
3577
0
            }
3578
0
          }
3579
0
          if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3580
0
            tmp |= t1 & (MAY_BE_RC1|MAY_BE_RCN);
3581
0
          }
3582
0
          if (opline->op2_type == IS_UNUSED) {
3583
0
            if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3584
0
              key_type |= MAY_BE_ARRAY_PACKED;
3585
0
            }
3586
0
            if (t1 & MAY_BE_ARRAY) {
3587
0
              key_type |= MAY_BE_HASH_ONLY(t1) ?
3588
0
                MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
3589
0
            }
3590
0
          } else {
3591
0
            if (t2 & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
3592
0
              if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3593
0
                key_type |= MAY_BE_ARRAY_PACKED;
3594
0
              }
3595
0
              if (t1 & MAY_BE_ARRAY) {
3596
0
                key_type |= MAY_BE_HASH_ONLY(t1) ?
3597
0
                  MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
3598
0
                }
3599
0
            }
3600
0
            if (t2 & MAY_BE_STRING) {
3601
0
              key_type |= MAY_BE_ARRAY_KEY_STRING;
3602
0
              if (opline->op2_type != IS_CONST) {
3603
                // FIXME: numeric string
3604
0
                if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3605
0
                  key_type |= MAY_BE_ARRAY_PACKED;
3606
0
                }
3607
0
                if (t1 & MAY_BE_ARRAY) {
3608
0
                  key_type |= MAY_BE_HASH_ONLY(t1) ?
3609
0
                    MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
3610
0
                  }
3611
0
              }
3612
0
            }
3613
0
            if (t2 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
3614
0
              key_type |= MAY_BE_ARRAY_KEY_STRING;
3615
0
            }
3616
0
          }
3617
0
        } else if (opline->opcode == ZEND_FETCH_DIM_UNSET) {
3618
0
          if (t1 & MAY_BE_ARRAY) {
3619
0
            tmp |= MAY_BE_RC1;
3620
0
          }
3621
0
          if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3622
0
            tmp |= t1 & (MAY_BE_RC1|MAY_BE_RCN);
3623
0
          }
3624
0
        }
3625
0
        if ((key_type & (MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING))
3626
0
            && (opline->opcode == ZEND_FETCH_DIM_RW
3627
0
            || opline->opcode == ZEND_FETCH_DIM_W
3628
0
            || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3629
0
            || opline->opcode == ZEND_FETCH_LIST_W)) {
3630
0
          j = ssa_vars[ssa_op->result_def].use_chain;
3631
0
          if (j < 0) {
3632
            /* no uses */
3633
0
            tmp |= key_type | MAY_BE_ARRAY | MAY_BE_ARRAY_OF_NULL;
3634
0
          }
3635
0
          while (j >= 0) {
3636
0
            uint8_t opcode;
3637
3638
0
            if (!ssa_opcodes) {
3639
0
              if (j != (opline - op_array->opcodes) + 1) {
3640
                /* Use must be in next opline */
3641
0
                tmp |= key_type | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3642
0
                break;
3643
0
              }
3644
0
              opcode = op_array->opcodes[j].opcode;
3645
0
            } else {
3646
0
              if (ssa_opcodes[j] != opline + 1) {
3647
                /* Use must be in next opline */
3648
0
                tmp |= key_type | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3649
0
                break;
3650
0
              }
3651
0
              opcode = ssa_opcodes[j]->opcode;
3652
0
            }
3653
0
            switch (opcode) {
3654
0
              case ZEND_FETCH_DIM_W:
3655
0
              case ZEND_FETCH_DIM_RW:
3656
0
              case ZEND_FETCH_DIM_FUNC_ARG:
3657
0
              case ZEND_FETCH_LIST_W:
3658
0
              case ZEND_ASSIGN_DIM:
3659
0
              case ZEND_ASSIGN_DIM_OP:
3660
0
                tmp |= key_type | MAY_BE_ARRAY | MAY_BE_ARRAY_OF_ARRAY;
3661
0
                break;
3662
0
              case ZEND_SEND_VAR_EX:
3663
0
              case ZEND_SEND_FUNC_ARG:
3664
0
              case ZEND_SEND_VAR_NO_REF:
3665
0
              case ZEND_SEND_VAR_NO_REF_EX:
3666
0
              case ZEND_SEND_REF:
3667
0
              case ZEND_ASSIGN_REF:
3668
0
              case ZEND_YIELD:
3669
0
              case ZEND_INIT_ARRAY:
3670
0
              case ZEND_ADD_ARRAY_ELEMENT:
3671
0
              case ZEND_RETURN_BY_REF:
3672
0
              case ZEND_VERIFY_RETURN_TYPE:
3673
0
              case ZEND_MAKE_REF:
3674
0
              case ZEND_FE_RESET_RW:
3675
0
                tmp |= key_type | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3676
0
                break;
3677
0
              case ZEND_PRE_INC:
3678
0
              case ZEND_PRE_DEC:
3679
0
              case ZEND_POST_INC:
3680
0
              case ZEND_POST_DEC:
3681
0
                if (tmp & MAY_BE_ARRAY_OF_LONG) {
3682
                  /* may overflow */
3683
0
                  tmp |= key_type | MAY_BE_ARRAY_OF_DOUBLE;
3684
0
                } else if (!(tmp & (MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_DOUBLE))) {
3685
0
                  tmp |= key_type | MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE;
3686
0
                }
3687
0
                break;
3688
0
              case ZEND_FETCH_OBJ_W:
3689
0
              case ZEND_FETCH_OBJ_RW:
3690
0
              case ZEND_FETCH_OBJ_FUNC_ARG:
3691
0
              case ZEND_ASSIGN_OBJ:
3692
0
              case ZEND_ASSIGN_OBJ_OP:
3693
0
              case ZEND_ASSIGN_OBJ_REF:
3694
0
              case ZEND_PRE_INC_OBJ:
3695
0
              case ZEND_PRE_DEC_OBJ:
3696
0
              case ZEND_POST_INC_OBJ:
3697
0
              case ZEND_POST_DEC_OBJ:
3698
                /* These will result in an error exception, unless the element
3699
                 * is already an object. */
3700
0
                break;
3701
0
              case ZEND_SEND_VAR:
3702
0
              case ZEND_FETCH_DIM_R:
3703
                /* This can occur if a DIM_FETCH_FUNC_ARG with UNUSED op2 is left
3704
                 * behind, because it can't be converted to DIM_FETCH_R. */
3705
0
                break;
3706
0
              case ZEND_FREE:
3707
                /* This may happen if the using opcode is DCEd.  */
3708
0
                break;
3709
0
              default: ZEND_UNREACHABLE();
3710
0
            }
3711
0
            j = zend_ssa_next_use(ssa->ops, ssa_op->result_def, j);
3712
0
            if (j >= 0) {
3713
0
              tmp |= key_type | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3714
0
              break;
3715
0
            }
3716
0
          }
3717
0
          if (opline->opcode != ZEND_FETCH_DIM_FUNC_ARG) {
3718
0
            tmp &= ~MAY_BE_ARRAY_EMPTY;
3719
0
          }
3720
0
        }
3721
0
        if (!(tmp & MAY_BE_ARRAY)
3722
0
         || (tmp & MAY_BE_ARRAY_KEY_ANY)
3723
0
         || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3724
0
         || opline->opcode == ZEND_FETCH_DIM_R
3725
0
         || opline->opcode == ZEND_FETCH_DIM_IS
3726
0
         || opline->opcode == ZEND_FETCH_DIM_UNSET
3727
0
         || opline->opcode == ZEND_FETCH_LIST_R) {
3728
0
          UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3729
0
        } else {
3730
          /* invalid key type */
3731
0
          return SUCCESS;
3732
0
        }
3733
0
        COPY_SSA_OBJ_TYPE(ssa_op->op1_use, ssa_op->op1_def);
3734
0
      }
3735
      /* FETCH_LIST on a string behaves like FETCH_R on null */
3736
0
      tmp = zend_array_element_type(
3737
0
        opline->opcode != ZEND_FETCH_LIST_R ? t1 : ((t1 & ~MAY_BE_STRING) | MAY_BE_NULL),
3738
0
        opline->op1_type,
3739
0
        opline->opcode != ZEND_FETCH_DIM_R && opline->opcode != ZEND_FETCH_DIM_IS
3740
0
          && opline->opcode != ZEND_FETCH_LIST_R,
3741
0
        opline->op2_type == IS_UNUSED);
3742
0
      if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG && (t1 & (MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE))) {
3743
0
        tmp |= MAY_BE_NULL;
3744
0
      }
3745
0
      if (opline->opcode == ZEND_FETCH_DIM_IS && (t1 & MAY_BE_STRING)) {
3746
0
        tmp |= MAY_BE_NULL;
3747
0
      }
3748
0
      if ((tmp & (MAY_BE_RC1|MAY_BE_RCN)) == MAY_BE_RCN && opline->result_type == IS_TMP_VAR) {
3749
        /* refcount may be indirectly decremented. Make an exception if the result is used in the next instruction */
3750
0
        if (!ssa_opcodes) {
3751
0
          if (ssa->vars[ssa_op->result_def].use_chain < 0
3752
0
           || opline + 1 != op_array->opcodes + ssa->vars[ssa_op->result_def].use_chain) {
3753
0
            tmp |= MAY_BE_RC1;
3754
0
            }
3755
0
        } else {
3756
0
          if (ssa->vars[ssa_op->result_def].use_chain < 0
3757
0
           || opline + 1 != ssa_opcodes[ssa->vars[ssa_op->result_def].use_chain]) {
3758
0
            tmp |= MAY_BE_RC1;
3759
0
            }
3760
0
        }
3761
0
      }
3762
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3763
0
      break;
3764
0
    case ZEND_FETCH_THIS:
3765
0
      if (!(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
3766
0
        UPDATE_SSA_OBJ_TYPE(op_array->scope, 1, ssa_op->result_def);
3767
0
      }
3768
0
      UPDATE_SSA_TYPE(MAY_BE_RCN|MAY_BE_OBJECT, ssa_op->result_def);
3769
0
      break;
3770
0
    case ZEND_FETCH_OBJ_R:
3771
0
    case ZEND_FETCH_OBJ_IS:
3772
0
    case ZEND_FETCH_OBJ_RW:
3773
0
    case ZEND_FETCH_OBJ_W:
3774
0
    case ZEND_FETCH_OBJ_UNSET:
3775
0
    case ZEND_FETCH_OBJ_FUNC_ARG:
3776
0
      if (ssa_op->result_def >= 0) {
3777
0
        uint32_t tmp = 0;
3778
0
        ce = NULL;
3779
0
        if (opline->op1_type != IS_UNUSED
3780
0
            && (t1 & (MAY_BE_ANY | MAY_BE_UNDEF) & ~MAY_BE_OBJECT)) {
3781
0
          tmp |= MAY_BE_NULL;
3782
0
        }
3783
0
        if (opline->op1_type == IS_UNUSED || (t1 & MAY_BE_OBJECT)) {
3784
0
          const zend_property_info *prop_info = zend_fetch_prop_info(op_array, ssa, opline, ssa_op);
3785
0
          tmp |= zend_fetch_prop_type(script, prop_info, &ce);
3786
0
          if (opline->opcode != ZEND_FETCH_OBJ_R && opline->opcode != ZEND_FETCH_OBJ_IS) {
3787
0
            tmp |= MAY_BE_REF | MAY_BE_INDIRECT;
3788
0
            if ((opline->extended_value & ZEND_FETCH_OBJ_FLAGS) == ZEND_FETCH_DIM_WRITE) {
3789
0
              tmp |= MAY_BE_UNDEF;
3790
0
            }
3791
0
            ce = NULL;
3792
0
          } else if (!(opline->op1_type & (IS_VAR|IS_TMP_VAR)) || !(t1 & MAY_BE_RC1)) {
3793
0
            const zend_class_entry *ce = NULL;
3794
3795
0
            if (opline->op1_type == IS_UNUSED) {
3796
0
              ce = op_array->scope;
3797
0
            } else if (ssa_op->op1_use >= 0 && !ssa->var_info[ssa_op->op1_use].is_instanceof) {
3798
0
              ce = ssa->var_info[ssa_op->op1_use].ce;
3799
0
            }
3800
            /* Unset properties will resort back to __get/__set */
3801
0
            if (ce
3802
0
             && !ce->create_object
3803
0
             && ce->default_object_handlers->read_property == zend_std_read_property
3804
0
             && !ce->__get
3805
0
             && !result_may_be_separated(ssa, ssa_op)) {
3806
0
              tmp &= ~MAY_BE_RC1;
3807
0
            }
3808
0
            if (opline->opcode == ZEND_FETCH_OBJ_IS) {
3809
              /* IS check may return null for uninitialized typed property. */
3810
0
              tmp |= MAY_BE_NULL;
3811
0
            }
3812
0
          }
3813
0
        }
3814
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3815
0
        if (ce) {
3816
0
          UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
3817
0
        }
3818
0
      }
3819
0
      break;
3820
0
    case ZEND_FETCH_STATIC_PROP_R:
3821
0
    case ZEND_FETCH_STATIC_PROP_IS:
3822
0
    case ZEND_FETCH_STATIC_PROP_RW:
3823
0
    case ZEND_FETCH_STATIC_PROP_W:
3824
0
    case ZEND_FETCH_STATIC_PROP_UNSET:
3825
0
    case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
3826
0
      tmp = zend_fetch_prop_type(script,
3827
0
        zend_fetch_static_prop_info(script, op_array, ssa, opline), &ce);
3828
0
      if (opline->opcode != ZEND_FETCH_STATIC_PROP_R
3829
0
          && opline->opcode != ZEND_FETCH_STATIC_PROP_IS) {
3830
0
        tmp |= MAY_BE_REF | MAY_BE_INDIRECT;
3831
0
        if ((opline->extended_value & ZEND_FETCH_OBJ_FLAGS) == ZEND_FETCH_DIM_WRITE) {
3832
0
          tmp |= MAY_BE_UNDEF;
3833
0
        }
3834
0
        ce = NULL;
3835
0
      } else {
3836
0
        if (!result_may_be_separated(ssa, ssa_op)) {
3837
0
          tmp &= ~MAY_BE_RC1;
3838
0
        }
3839
0
        if (opline->opcode == ZEND_FETCH_STATIC_PROP_IS) {
3840
0
          tmp |= MAY_BE_NULL;
3841
0
        }
3842
0
      }
3843
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3844
0
      if (ce) {
3845
0
        UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
3846
0
      }
3847
0
      break;
3848
0
    case ZEND_FRAMELESS_ICALL_1:
3849
0
    case ZEND_FRAMELESS_ICALL_2:
3850
0
    case ZEND_FRAMELESS_ICALL_3:
3851
0
      if (ssa_op->op1_def >= 0) {
3852
0
        ZEND_ASSERT(ssa_op->op1_use >= 0);
3853
0
        tmp = ssa->var_info[ssa_op->op1_use].type;
3854
0
        if (tmp & MAY_BE_RC1) {
3855
0
          tmp |= MAY_BE_RCN;
3856
0
        }
3857
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3858
0
      }
3859
0
      if (ssa_op->op2_def >= 0) {
3860
0
        ZEND_ASSERT(ssa_op->op2_use >= 0);
3861
0
        tmp = ssa->var_info[ssa_op->op2_use].type;
3862
0
        if (tmp & MAY_BE_RC1) {
3863
0
          tmp |= MAY_BE_RCN;
3864
0
        }
3865
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op2_def);
3866
0
      }
3867
0
      if (opline->opcode == ZEND_FRAMELESS_ICALL_3) {
3868
0
        zend_ssa_op *next_ssa_op = ssa_op + 1;
3869
0
        if (next_ssa_op->op1_def >= 0) {
3870
0
          ZEND_ASSERT(next_ssa_op->op1_use >= 0);
3871
0
          tmp = ssa->var_info[next_ssa_op->op1_use].type;
3872
0
          if (tmp & MAY_BE_RC1) {
3873
0
            tmp |= MAY_BE_RCN;
3874
0
          }
3875
0
          UPDATE_SSA_TYPE(tmp, next_ssa_op->op1_def);
3876
0
        }
3877
0
      }
3878
0
      ZEND_FALLTHROUGH;
3879
0
    case ZEND_FRAMELESS_ICALL_0:
3880
0
    case ZEND_DO_FCALL:
3881
0
    case ZEND_DO_ICALL:
3882
0
    case ZEND_DO_UCALL:
3883
0
    case ZEND_DO_FCALL_BY_NAME:
3884
0
      if (ssa_op->result_def >= 0) {
3885
0
        zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
3886
0
        zend_call_info *call_info;
3887
3888
0
        if (!func_info || !func_info->call_map) {
3889
0
          goto unknown_opcode;
3890
0
        }
3891
0
        call_info = func_info->call_map[opline - op_array->opcodes];
3892
0
        if (!call_info) {
3893
0
          goto unknown_opcode;
3894
0
        }
3895
3896
0
        zend_class_entry *ce;
3897
0
        bool ce_is_instanceof;
3898
0
        tmp = zend_get_func_info(call_info, ssa, &ce, &ce_is_instanceof);
3899
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3900
0
        if (ce) {
3901
0
          UPDATE_SSA_OBJ_TYPE(ce, ce_is_instanceof, ssa_op->result_def);
3902
0
        }
3903
0
      }
3904
0
      break;
3905
0
    case ZEND_CALLABLE_CONVERT:
3906
0
      UPDATE_SSA_TYPE(MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN, ssa_op->result_def);
3907
0
      UPDATE_SSA_OBJ_TYPE(zend_ce_closure, /* is_instanceof */ false, ssa_op->result_def);
3908
0
      break;
3909
0
    case ZEND_FETCH_CONSTANT:
3910
0
      UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY, ssa_op->result_def);
3911
0
      break;
3912
0
    case ZEND_FETCH_CLASS_CONSTANT: {
3913
0
      bool is_prototype;
3914
0
      const zend_class_constant *cc = zend_fetch_class_const_info(script, op_array, opline, &is_prototype);
3915
0
      if (!cc || !ZEND_TYPE_IS_SET(cc->type)) {
3916
0
        UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY, ssa_op->result_def);
3917
0
        break;
3918
0
      }
3919
0
      UPDATE_SSA_TYPE(zend_convert_type(script, cc->type, &ce), ssa_op->result_def);
3920
0
      if (ce) {
3921
0
        UPDATE_SSA_OBJ_TYPE(ce, /* is_instanceof */ true, ssa_op->result_def);
3922
0
      }
3923
0
      break;
3924
0
    }
3925
0
    case ZEND_STRLEN:
3926
0
    case ZEND_COUNT:
3927
0
    case ZEND_FUNC_NUM_ARGS:
3928
0
      UPDATE_SSA_TYPE(MAY_BE_LONG, ssa_op->result_def);
3929
0
      break;
3930
0
    case ZEND_FUNC_GET_ARGS:
3931
0
      UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ARRAY|MAY_BE_ARRAY_EMPTY|MAY_BE_ARRAY_PACKED|MAY_BE_ARRAY_OF_ANY, ssa_op->result_def);
3932
0
      break;
3933
0
    case ZEND_GET_CLASS:
3934
0
    case ZEND_GET_CALLED_CLASS:
3935
0
      UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RCN, ssa_op->result_def);
3936
0
      break;
3937
0
    case ZEND_GET_TYPE:
3938
0
      UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN, ssa_op->result_def);
3939
0
      break;
3940
0
    case ZEND_TYPE_CHECK: {
3941
0
      uint32_t expected_type_mask = opline->extended_value;
3942
0
      if (t1 & MAY_BE_UNDEF) {
3943
0
        t1 |= MAY_BE_NULL;
3944
0
      }
3945
0
      tmp = 0;
3946
0
      if (t1 & expected_type_mask) {
3947
0
        tmp |= MAY_BE_TRUE;
3948
0
        if ((t1 & expected_type_mask) & MAY_BE_RESOURCE) {
3949
0
          tmp |= MAY_BE_FALSE;
3950
0
        }
3951
0
      }
3952
0
      if (t1 & (MAY_BE_ANY - expected_type_mask)) {
3953
0
        tmp |= MAY_BE_FALSE;
3954
0
      }
3955
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3956
0
      break;
3957
0
    }
3958
0
    case ZEND_DEFINED:
3959
0
      UPDATE_SSA_TYPE(MAY_BE_FALSE|MAY_BE_TRUE, ssa_op->result_def);
3960
0
      break;
3961
0
    case ZEND_VERIFY_RETURN_TYPE:
3962
0
      if (t1 & MAY_BE_REF) {
3963
0
        tmp = t1;
3964
0
        ce = NULL;
3965
0
      } else {
3966
0
        zend_arg_info *ret_info = op_array->arg_info - 1;
3967
0
        tmp = zend_fetch_arg_info_type(script, ret_info, &ce);
3968
0
        if ((tmp & MAY_BE_NULL) && opline->op1_type == IS_CV) {
3969
0
          tmp |= MAY_BE_UNDEF;
3970
0
        }
3971
0
        tmp |= (t1 & MAY_BE_INDIRECT);
3972
3973
        // TODO: We could model more precisely how illegal types are converted.
3974
0
        uint32_t extra_types = t1 & ~tmp;
3975
0
        if (!extra_types) {
3976
0
          tmp &= t1;
3977
0
        }
3978
0
      }
3979
0
      if (opline->op1_type & (IS_TMP_VAR|IS_VAR|IS_CV)) {
3980
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
3981
0
        if (ce) {
3982
0
          UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->op1_def);
3983
0
        } else {
3984
0
          UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->op1_def);
3985
0
        }
3986
0
      } else {
3987
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3988
0
        if (ce) {
3989
0
          UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_op->result_def);
3990
0
        } else {
3991
0
          UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_op->result_def);
3992
0
        }
3993
0
      }
3994
0
      break;
3995
0
    case ZEND_MAKE_REF:
3996
0
      tmp = MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
3997
0
      UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
3998
0
      if (ssa_op->op1_def >= 0) {
3999
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
4000
0
      }
4001
0
      break;
4002
0
    case ZEND_CATCH:
4003
      /* Forbidden opcodes */
4004
0
      ZEND_UNREACHABLE();
4005
0
      break;
4006
0
    case ZEND_FETCH_CLASS_NAME:
4007
0
      UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RCN, ssa_op->result_def);
4008
0
      break;
4009
0
    case ZEND_ISSET_ISEMPTY_THIS:
4010
0
      UPDATE_SSA_TYPE(MAY_BE_BOOL, ssa_op->result_def);
4011
0
      break;
4012
0
    case ZEND_DECLARE_LAMBDA_FUNCTION:
4013
0
      UPDATE_SSA_TYPE(MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN, ssa_op->result_def);
4014
0
      UPDATE_SSA_OBJ_TYPE(zend_ce_closure, /* is_instanceof */ false, ssa_op->result_def);
4015
0
      break;
4016
0
    case ZEND_PRE_DEC_STATIC_PROP:
4017
0
    case ZEND_PRE_INC_STATIC_PROP:
4018
0
    case ZEND_POST_DEC_STATIC_PROP:
4019
0
    case ZEND_POST_INC_STATIC_PROP: {
4020
0
      if (ssa_op->result_def >= 0) {
4021
0
        const zend_property_info *prop_info = zend_fetch_static_prop_info(script, op_array, ssa, opline);
4022
0
        zend_class_entry *prop_ce;
4023
0
        tmp = zend_fetch_prop_type(script, prop_info, &prop_ce);
4024
        /* Internal objects may result in essentially anything. */
4025
0
        if (tmp & MAY_BE_OBJECT) {
4026
0
          goto unknown_opcode;
4027
0
        }
4028
0
        tmp &= MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_BOOL|MAY_BE_NULL;
4029
0
        if (tmp & MAY_BE_STRING) {
4030
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
4031
0
        }
4032
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
4033
0
      }
4034
0
      break;
4035
0
    }
4036
0
    case ZEND_SPACESHIP:
4037
0
      UPDATE_SSA_TYPE(MAY_BE_LONG, ssa_op->result_def);
4038
0
      break;
4039
0
    case ZEND_FETCH_GLOBALS:
4040
0
      UPDATE_SSA_TYPE(MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF|MAY_BE_RC1|MAY_BE_RCN, ssa_op->result_def);
4041
0
      break;
4042
0
    default:
4043
#ifdef ZEND_DEBUG_TYPE_INFERENCE
4044
      if (ssa_op->result_def >= 0) {
4045
        switch (opline->opcode) {
4046
          case ZEND_FETCH_R:
4047
          case ZEND_FETCH_W:
4048
          case ZEND_FETCH_RW:
4049
          case ZEND_FETCH_IS:
4050
          case ZEND_FETCH_UNSET:
4051
          case ZEND_YIELD_FROM:
4052
          /* Currently unimplemented due to some assumptions in JIT. See:
4053
           * https://github.com/php/php-src/pull/13304#issuecomment-1926668141 */
4054
          case ZEND_SEPARATE:
4055
            break;
4056
          default:
4057
            fprintf(stderr, "Missing result type inference for opcode %s, line %d\n", zend_get_opcode_name(opline->opcode), opline->lineno);
4058
            break;
4059
        }
4060
      }
4061
      if (ssa_op->op1_def >= 0) {
4062
        fprintf(stderr, "Missing op1 type inference for opcode %s, line %d\n", zend_get_opcode_name(opline->opcode), opline->lineno);
4063
      }
4064
      if (ssa_op->op2_def >= 0) {
4065
        fprintf(stderr, "Missing op2 type inference for opcode %s, line %d\n", zend_get_opcode_name(opline->opcode), opline->lineno);
4066
      }
4067
#endif
4068
0
      if (ssa_op->op1_def >= 0) {
4069
0
        tmp = MAY_BE_ANY | MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
4070
0
        UPDATE_SSA_TYPE(tmp, ssa_op->op1_def);
4071
0
      }
4072
0
unknown_opcode:
4073
0
      if (ssa_op->result_def >= 0) {
4074
0
        tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
4075
0
        if (opline->result_type == IS_TMP_VAR) {
4076
0
          if (opline->opcode == ZEND_FETCH_R || opline->opcode == ZEND_FETCH_IS) {
4077
            /* Variable reference counter may be decremented before use */
4078
            /* See: ext/opcache/tests/jit/fetch_r_001.phpt */
4079
0
            tmp |= MAY_BE_RC1 | MAY_BE_RCN;
4080
0
          } else {
4081
0
            tmp |= MAY_BE_RC1 | MAY_BE_RCN;
4082
0
          }
4083
0
        } else if (opline->result_type == IS_CV) {
4084
0
          tmp |= MAY_BE_RC1 | MAY_BE_RCN;
4085
0
        } else {
4086
0
          tmp |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN;
4087
0
          switch (opline->opcode) {
4088
0
            case ZEND_FETCH_W:
4089
0
            case ZEND_FETCH_RW:
4090
0
            case ZEND_FETCH_FUNC_ARG:
4091
0
            case ZEND_FETCH_UNSET:
4092
0
            case ZEND_FETCH_DIM_W:
4093
0
            case ZEND_FETCH_DIM_RW:
4094
0
            case ZEND_FETCH_DIM_FUNC_ARG:
4095
0
            case ZEND_FETCH_DIM_UNSET:
4096
0
            case ZEND_FETCH_OBJ_W:
4097
0
            case ZEND_FETCH_OBJ_RW:
4098
0
            case ZEND_FETCH_OBJ_FUNC_ARG:
4099
0
            case ZEND_FETCH_OBJ_UNSET:
4100
0
            case ZEND_FETCH_STATIC_PROP_W:
4101
0
            case ZEND_FETCH_STATIC_PROP_RW:
4102
0
            case ZEND_FETCH_STATIC_PROP_FUNC_ARG:
4103
0
            case ZEND_FETCH_STATIC_PROP_UNSET:
4104
0
              tmp |= MAY_BE_INDIRECT;
4105
0
              break;
4106
0
          }
4107
0
        }
4108
0
        UPDATE_SSA_TYPE(tmp, ssa_op->result_def);
4109
0
      }
4110
0
      break;
4111
0
  }
4112
4113
0
  return SUCCESS;
4114
0
}
4115
4116
ZEND_API zend_result zend_update_type_info(
4117
      const zend_op_array *op_array,
4118
      zend_ssa            *ssa,
4119
      const zend_script   *script,
4120
      const zend_op       *opline,
4121
      zend_ssa_op         *ssa_op,
4122
      const zend_op      **ssa_opcodes,
4123
      zend_long            optimization_level)
4124
0
{
4125
0
  return _zend_update_type_info(op_array, ssa, script, NULL, opline, ssa_op, ssa_opcodes, optimization_level,
4126
0
              false);
4127
0
}
4128
4129
0
static uint32_t get_class_entry_rank(const zend_class_entry *ce) {
4130
0
  uint32_t rank = 0;
4131
0
  if (ce->ce_flags & ZEND_ACC_LINKED) {
4132
0
    while (ce->parent) {
4133
0
      rank++;
4134
0
      ce = ce->parent;
4135
0
    }
4136
0
  }
4137
0
  return rank;
4138
0
}
4139
4140
/* Compute least common ancestor on class inheritance tree only */
4141
static zend_class_entry *join_class_entries(
4142
0
    zend_class_entry *ce1, zend_class_entry *ce2, bool *is_instanceof) {
4143
0
  uint32_t rank1, rank2;
4144
0
  if (ce1 == ce2) {
4145
0
    return ce1;
4146
0
  }
4147
0
  if (!ce1 || !ce2) {
4148
0
    return NULL;
4149
0
  }
4150
4151
0
  rank1 = get_class_entry_rank(ce1);
4152
0
  rank2 = get_class_entry_rank(ce2);
4153
4154
0
  while (rank1 != rank2) {
4155
0
    if (rank1 > rank2) {
4156
0
      ce1 = !(ce1->ce_flags & ZEND_ACC_LINKED) ? NULL : ce1->parent;
4157
0
      rank1--;
4158
0
    } else {
4159
0
      ce2 = !(ce2->ce_flags & ZEND_ACC_LINKED) ? NULL : ce2->parent;
4160
0
      rank2--;
4161
0
    }
4162
0
  }
4163
4164
0
  while (ce1 != ce2) {
4165
0
    ce1 = !(ce1->ce_flags & ZEND_ACC_LINKED) ? NULL : ce1->parent;
4166
0
    ce2 = !(ce2->ce_flags & ZEND_ACC_LINKED) ? NULL : ce2->parent;
4167
0
  }
4168
4169
0
  if (ce1) {
4170
0
    *is_instanceof = true;
4171
0
  }
4172
0
  return ce1;
4173
0
}
4174
4175
0
static bool safe_instanceof(const zend_class_entry *ce1, const zend_class_entry *ce2) {
4176
0
  if (ce1 == ce2) {
4177
0
    return 1;
4178
0
  }
4179
0
  if (!(ce1->ce_flags & ZEND_ACC_LINKED)) {
4180
    /* This case could be generalized, similarly to unlinked_instanceof */
4181
0
    return 0;
4182
0
  }
4183
0
  return instanceof_function(ce1, ce2);
4184
0
}
4185
4186
static zend_result zend_infer_types_ex(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_bitset worklist, zend_long optimization_level)
4187
1
{
4188
1
  const zend_basic_block *blocks = ssa->cfg.blocks;
4189
1
  zend_ssa_var *ssa_vars = ssa->vars;
4190
1
  zend_ssa_var_info *ssa_var_info = ssa->var_info;
4191
1
  int ssa_vars_count = ssa->vars_count;
4192
1
  int j;
4193
1
  uint32_t tmp, worklist_len = zend_bitset_len(ssa_vars_count);
4194
1
  bool update_worklist = 1;
4195
1
  const zend_op **ssa_opcodes = NULL;
4196
4197
1
  while (!zend_bitset_empty(worklist, worklist_len)) {
4198
0
    j = zend_bitset_first(worklist, worklist_len);
4199
0
    zend_bitset_excl(worklist, j);
4200
0
    if (ssa_vars[j].definition_phi) {
4201
0
      zend_ssa_phi *p = ssa_vars[j].definition_phi;
4202
0
      if (p->pi >= 0) {
4203
0
        zend_class_entry *ce = ssa_var_info[p->sources[0]].ce;
4204
0
        bool is_instanceof = ssa_var_info[p->sources[0]].is_instanceof;
4205
0
        tmp = get_ssa_var_info(ssa, p->sources[0]);
4206
4207
0
        if (!p->has_range_constraint) {
4208
0
          const zend_ssa_type_constraint *constraint = &p->constraint.type;
4209
0
          tmp &= constraint->type_mask;
4210
0
          if (!(tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
4211
0
            tmp &= ~(MAY_BE_RC1|MAY_BE_RCN);
4212
0
          }
4213
0
          if ((tmp & MAY_BE_OBJECT) && constraint->ce && ce != constraint->ce) {
4214
0
            if (!ce) {
4215
0
              ce = constraint->ce;
4216
0
              is_instanceof = true;
4217
0
            } else if (is_instanceof && safe_instanceof(constraint->ce, ce)) {
4218
0
              ce = constraint->ce;
4219
0
            } else {
4220
              /* Ignore the constraint (either ce instanceof constraint->ce or
4221
               * they are unrelated, as far as we can statically determine) */
4222
0
            }
4223
0
          }
4224
0
        }
4225
4226
0
        UPDATE_SSA_TYPE(tmp, j);
4227
0
        if (tmp & MAY_BE_REF) {
4228
0
          UPDATE_SSA_OBJ_TYPE(NULL, 0, j);
4229
0
        } else {
4230
0
          UPDATE_SSA_OBJ_TYPE(ce, is_instanceof, j);
4231
0
        }
4232
0
      } else {
4233
0
        bool first = true;
4234
0
        bool is_instanceof = false;
4235
0
        zend_class_entry *ce = NULL;
4236
0
        uint32_t i;
4237
4238
0
        tmp = 0;
4239
0
        for (i = 0; i < blocks[p->block].predecessors_count; i++) {
4240
0
          tmp |= get_ssa_var_info(ssa, p->sources[i]);
4241
0
        }
4242
0
        UPDATE_SSA_TYPE(tmp, j);
4243
0
        for (i = 0; i < blocks[p->block].predecessors_count; i++) {
4244
0
          zend_ssa_var_info *info;
4245
4246
0
          ZEND_ASSERT(p->sources[i] >= 0);
4247
0
          info = &ssa_var_info[p->sources[i]];
4248
0
          if (info->type & MAY_BE_OBJECT) {
4249
0
            if (first) {
4250
0
              ce = info->ce;
4251
0
              is_instanceof = info->is_instanceof;
4252
0
              first = false;
4253
0
            } else {
4254
0
              is_instanceof |= info->is_instanceof;
4255
0
              ce = join_class_entries(ce, info->ce, &is_instanceof);
4256
0
            }
4257
0
          }
4258
0
        }
4259
0
        UPDATE_SSA_OBJ_TYPE(ce, ce ? is_instanceof : 0, j);
4260
0
      }
4261
0
    } else if (ssa_vars[j].definition >= 0) {
4262
0
      int i = ssa_vars[j].definition;
4263
0
      if (_zend_update_type_info(op_array, ssa, script, worklist, op_array->opcodes + i, ssa->ops + i, NULL, optimization_level, true) == FAILURE) {
4264
0
        return FAILURE;
4265
0
      }
4266
0
    }
4267
0
  }
4268
1
  return SUCCESS;
4269
1
}
4270
4271
0
static bool is_narrowable_instr(const zend_op *opline)  {
4272
0
  return opline->opcode == ZEND_ADD || opline->opcode == ZEND_SUB
4273
0
    || opline->opcode == ZEND_MUL || opline->opcode == ZEND_DIV;
4274
0
}
4275
4276
0
static bool is_effective_op1_double_cast(const zend_op *opline, const zval *op2) {
4277
0
  return (opline->opcode == ZEND_ADD && Z_LVAL_P(op2) == 0)
4278
0
    || (opline->opcode == ZEND_SUB && Z_LVAL_P(op2) == 0)
4279
0
    || (opline->opcode == ZEND_MUL && Z_LVAL_P(op2) == 1)
4280
0
    || (opline->opcode == ZEND_DIV && Z_LVAL_P(op2) == 1);
4281
0
}
4282
0
static bool is_effective_op2_double_cast(const zend_op *opline, const zval *op1) {
4283
  /* In PHP it holds that (double)(0-$int) is bitwise identical to 0.0-(double)$int,
4284
   * so allowing SUB here is fine. */
4285
0
  return (opline->opcode == ZEND_ADD && Z_LVAL_P(op1) == 0)
4286
0
    || (opline->opcode == ZEND_SUB && Z_LVAL_P(op1) == 0)
4287
0
    || (opline->opcode == ZEND_MUL && Z_LVAL_P(op1) == 1);
4288
0
}
4289
4290
/* This function recursively checks whether it's possible to convert an integer variable
4291
 * initialization to a double initialization. The basic idea is that if the value is used
4292
 * only in add/sub/mul/div ("narrowable" instructions) with a double result value, then it
4293
 * will be cast to double at that point anyway, so we may as well do it earlier already.
4294
 *
4295
 * The tricky case are chains of operations, where it's not necessarily a given that converting
4296
 * an integer to double before the chain of operations is the same as converting it after the
4297
 * chain. What this function does is detect two cases where it is safe:
4298
 *  * If the operations only involve constants, then we can simply verify that performing the
4299
 *    calculation on integers and doubles yields the same value.
4300
 *  * Even if one operand is not known, we may be able to determine that the operations with the
4301
 *    integer replaced by a double only acts as an effective double cast on the unknown operand.
4302
 *    E.g. 0+$i and 0.0+$i only differ by that cast. If then the consuming instruction of this
4303
 *    result will perform a double cast anyway, the conversion is safe.
4304
 *
4305
 * The checks happens recursively, while keeping track of which variables are already visited to
4306
 * avoid infinite loops. An iterative, worklist driven approach would be possible, but the state
4307
 * management more cumbersome to implement, so we don't bother for now.
4308
 */
4309
static bool can_convert_to_double(
4310
    const zend_op_array *op_array, zend_ssa *ssa, int var_num,
4311
0
    zval *value, zend_bitset visited) {
4312
0
  zend_ssa_var *var = &ssa->vars[var_num];
4313
0
  zend_ssa_phi *phi;
4314
0
  int use;
4315
0
  uint32_t type;
4316
4317
0
  if (zend_bitset_in(visited, var_num)) {
4318
0
    return 1;
4319
0
  }
4320
0
  zend_bitset_incl(visited, var_num);
4321
4322
0
  for (use = var->use_chain; use >= 0; use = zend_ssa_next_use(ssa->ops, var_num, use)) {
4323
0
    zend_op *opline = &op_array->opcodes[use];
4324
0
    zend_ssa_op *ssa_op = &ssa->ops[use];
4325
4326
0
    if (zend_ssa_is_no_val_use(opline, ssa_op, var_num)) {
4327
0
      continue;
4328
0
    }
4329
4330
0
    if (!is_narrowable_instr(opline)) {
4331
0
      return 0;
4332
0
    }
4333
4334
    /* Instruction always returns double, the conversion is certainly fine */
4335
0
    type = ssa->var_info[ssa_op->result_def].type;
4336
0
    if ((type & MAY_BE_ANY) == MAY_BE_DOUBLE) {
4337
0
      continue;
4338
0
    }
4339
4340
    /* UNDEF signals that the previous result is an effective double cast, this is only allowed
4341
     * if this instruction would have done the cast anyway (previous check). */
4342
0
    if (Z_ISUNDEF_P(value)) {
4343
0
      return 0;
4344
0
    }
4345
4346
    /* Check that narrowing can actually be useful */
4347
0
    if ((type & MAY_BE_ANY) & ~(MAY_BE_LONG|MAY_BE_DOUBLE)) {
4348
0
      return 0;
4349
0
    }
4350
4351
0
    {
4352
      /* For calculation on original values */
4353
0
      zval orig_op1, orig_op2, orig_result;
4354
      /* For calculation with var_num cast to double */
4355
0
      zval dval_op1, dval_op2, dval_result;
4356
4357
0
      ZVAL_UNDEF(&orig_op1);
4358
0
      ZVAL_UNDEF(&dval_op1);
4359
0
      if (ssa_op->op1_use == var_num) {
4360
0
        ZVAL_COPY_VALUE(&orig_op1, value);
4361
0
        ZVAL_DOUBLE(&dval_op1, (double) Z_LVAL_P(value));
4362
0
      } else if (opline->op1_type == IS_CONST) {
4363
0
        zval *zv = CRT_CONSTANT(opline->op1);
4364
0
        if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_DOUBLE) {
4365
0
          ZVAL_COPY_VALUE(&orig_op1, zv);
4366
0
          ZVAL_COPY_VALUE(&dval_op1, zv);
4367
0
        }
4368
0
      }
4369
4370
0
      ZVAL_UNDEF(&orig_op2);
4371
0
      ZVAL_UNDEF(&dval_op2);
4372
0
      if (ssa_op->op2_use == var_num) {
4373
0
        ZVAL_COPY_VALUE(&orig_op2, value);
4374
0
        ZVAL_DOUBLE(&dval_op2, (double) Z_LVAL_P(value));
4375
0
      } else if (opline->op2_type == IS_CONST) {
4376
0
        zval *zv = CRT_CONSTANT(opline->op2);
4377
0
        if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_DOUBLE) {
4378
0
          ZVAL_COPY_VALUE(&orig_op2, zv);
4379
0
          ZVAL_COPY_VALUE(&dval_op2, zv);
4380
0
        }
4381
0
      }
4382
4383
0
      ZEND_ASSERT(!Z_ISUNDEF(orig_op1) || !Z_ISUNDEF(orig_op2));
4384
0
      if (Z_ISUNDEF(orig_op1)) {
4385
0
        if (opline->opcode == ZEND_MUL && Z_LVAL(orig_op2) == 0) {
4386
0
          ZVAL_LONG(&orig_result, 0);
4387
0
        } else if (is_effective_op1_double_cast(opline, &orig_op2)) {
4388
0
          ZVAL_UNDEF(&orig_result);
4389
0
        } else {
4390
0
          return 0;
4391
0
        }
4392
0
      } else if (Z_ISUNDEF(orig_op2)) {
4393
0
        if (opline->opcode == ZEND_MUL && Z_LVAL(orig_op1) == 0) {
4394
0
          ZVAL_LONG(&orig_result, 0);
4395
0
        } else if (is_effective_op2_double_cast(opline, &orig_op1)) {
4396
0
          ZVAL_UNDEF(&orig_result);
4397
0
        } else {
4398
0
          return 0;
4399
0
        }
4400
0
      } else {
4401
0
        uint8_t opcode = opline->opcode;
4402
4403
0
        if (opcode == ZEND_ASSIGN_OP) {
4404
0
          opcode = opline->extended_value;
4405
0
        }
4406
4407
        /* Avoid division by zero */
4408
0
        if (opcode == ZEND_DIV && zval_get_double(&orig_op2) == 0.0) {
4409
0
          return 0;
4410
0
        }
4411
4412
0
        get_binary_op(opcode)(&orig_result, &orig_op1, &orig_op2);
4413
0
        get_binary_op(opcode)(&dval_result, &dval_op1, &dval_op2);
4414
0
        ZEND_ASSERT(Z_TYPE(dval_result) == IS_DOUBLE);
4415
0
        if (zval_get_double(&orig_result) != Z_DVAL(dval_result)) {
4416
0
          return 0;
4417
0
        }
4418
0
      }
4419
4420
0
      if (!can_convert_to_double(op_array, ssa, ssa_op->result_def, &orig_result, visited)) {
4421
0
        return 0;
4422
0
      }
4423
0
    }
4424
0
  }
4425
4426
0
  for (phi = var->phi_use_chain; phi; phi = zend_ssa_next_use_phi(ssa, var_num, phi)) {
4427
    /* Check that narrowing can actually be useful */
4428
0
    type = ssa->var_info[phi->ssa_var].type;
4429
0
    if ((type & MAY_BE_ANY) & ~(MAY_BE_LONG|MAY_BE_DOUBLE)) {
4430
0
      return 0;
4431
0
    }
4432
4433
0
    if (!can_convert_to_double(op_array, ssa, phi->ssa_var, value, visited)) {
4434
0
      return 0;
4435
0
    }
4436
0
  }
4437
4438
0
  return 1;
4439
0
}
4440
4441
static zend_result zend_type_narrowing(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level)
4442
1
{
4443
1
  uint32_t bitset_len = zend_bitset_len(ssa->vars_count);
4444
1
  zend_bitset visited, worklist;
4445
1
  int i, v;
4446
1
  zend_op *opline;
4447
1
  bool narrowed = 0;
4448
1
  ALLOCA_FLAG(use_heap)
4449
4450
1
  visited = ZEND_BITSET_ALLOCA(2 * bitset_len, use_heap);
4451
1
  worklist = visited + bitset_len;
4452
4453
1
  zend_bitset_clear(worklist, bitset_len);
4454
4455
1
  for (v = op_array->last_var; v < ssa->vars_count; v++) {
4456
0
    if ((ssa->var_info[v].type & (MAY_BE_REF | MAY_BE_ANY | MAY_BE_UNDEF)) != MAY_BE_LONG) continue;
4457
0
    if (ssa->vars[v].definition < 0) continue;
4458
0
    if (ssa->vars[v].no_val) continue;
4459
0
    opline = op_array->opcodes + ssa->vars[v].definition;
4460
    /* Go through assignments of literal integers and check if they can be converted to
4461
     * doubles instead, in the hope that we'll narrow long|double to double. */
4462
0
    if (opline->opcode == ZEND_ASSIGN && opline->result_type == IS_UNUSED &&
4463
0
        opline->op1_type == IS_CV && opline->op2_type == IS_CONST) {
4464
0
      zval *value = CRT_CONSTANT(opline->op2);
4465
4466
0
      zend_bitset_clear(visited, bitset_len);
4467
0
      if (can_convert_to_double(op_array, ssa, v, value, visited)) {
4468
0
        narrowed = 1;
4469
0
        ssa->var_info[v].use_as_double = 1;
4470
        /* The "visited" vars are exactly those which may change their type due to
4471
         * narrowing. Reset their types and add them to the type inference worklist */
4472
0
        ZEND_BITSET_FOREACH(visited, bitset_len, i) {
4473
0
          ssa->var_info[i].type &= ~MAY_BE_ANY;
4474
0
        } ZEND_BITSET_FOREACH_END();
4475
0
        zend_bitset_union(worklist, visited, bitset_len);
4476
0
      }
4477
0
    }
4478
0
  }
4479
4480
1
  if (!narrowed) {
4481
1
    free_alloca(visited, use_heap);
4482
1
    return SUCCESS;
4483
1
  }
4484
4485
0
  if (zend_infer_types_ex(op_array, script, ssa, worklist, optimization_level) == FAILURE) {
4486
0
    free_alloca(visited, use_heap);
4487
0
    return FAILURE;
4488
0
  }
4489
4490
0
  free_alloca(visited, use_heap);
4491
0
  return SUCCESS;
4492
0
}
4493
4494
static bool is_recursive_tail_call(const zend_op_array *op_array, const zend_op *opline)
4495
1
{
4496
1
  const zend_func_info *info = ZEND_FUNC_INFO(op_array);
4497
4498
1
  if (info->ssa.ops && info->ssa.vars && info->call_map &&
4499
0
      info->ssa.ops[opline - op_array->opcodes].op1_use >= 0 &&
4500
0
      info->ssa.vars[info->ssa.ops[opline - op_array->opcodes].op1_use].definition >= 0) {
4501
4502
0
    const zend_op *op = op_array->opcodes + info->ssa.vars[info->ssa.ops[opline - op_array->opcodes].op1_use].definition;
4503
4504
0
    if (op->opcode == ZEND_DO_UCALL) {
4505
0
      const zend_call_info *call_info = info->call_map[op - op_array->opcodes];
4506
0
      if (call_info && op_array == &call_info->callee_func->op_array) {
4507
0
        return 1;
4508
0
      }
4509
0
    }
4510
0
  }
4511
1
  return 0;
4512
1
}
4513
4514
uint32_t zend_get_return_info_from_signature_only(
4515
    const zend_function *func, const zend_script *script,
4516
0
    zend_class_entry **ce, bool *ce_is_instanceof, bool use_tentative_return_info) {
4517
0
  uint32_t type;
4518
0
  if (func->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE &&
4519
0
    (use_tentative_return_info || !ZEND_ARG_TYPE_IS_TENTATIVE(func->common.arg_info - 1))
4520
0
  ) {
4521
0
    const zend_arg_info *ret_info = func->common.arg_info - 1;
4522
0
    type = zend_fetch_arg_info_type(script, ret_info, ce);
4523
0
    *ce_is_instanceof = ce != NULL;
4524
0
  } else {
4525
0
    type = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
4526
0
      | MAY_BE_RC1 | MAY_BE_RCN;
4527
0
    *ce = NULL;
4528
0
    *ce_is_instanceof = false;
4529
0
  }
4530
4531
  /* For generators RETURN_REFERENCE refers to the yielded values. */
4532
0
  if ((func->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)
4533
0
      && !(func->common.fn_flags & ZEND_ACC_GENERATOR)) {
4534
0
    type |= MAY_BE_REF;
4535
0
    *ce = NULL;
4536
0
    *ce_is_instanceof = false;
4537
0
  }
4538
0
  return type;
4539
0
}
4540
4541
ZEND_API void zend_init_func_return_info(
4542
  const zend_op_array *op_array, const zend_script *script, zend_ssa_var_info *ret)
4543
0
{
4544
0
  ZEND_ASSERT((op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE));
4545
4546
0
  zend_ssa_range tmp_range = {0, 0, 0, 0};
4547
0
  bool is_instanceof = false;
4548
0
  ret->type = zend_get_return_info_from_signature_only(
4549
0
    (zend_function *) op_array, script, &ret->ce, &is_instanceof, /* use_tentative_return_info */ true);
4550
0
  ret->is_instanceof = is_instanceof;
4551
0
  ret->range = tmp_range;
4552
0
  ret->has_range = 0;
4553
0
}
4554
4555
static void zend_func_return_info(const zend_op_array   *op_array,
4556
                                  const zend_script     *script,
4557
                                  bool                   recursive,
4558
                                  bool                   widening,
4559
                                  zend_ssa_var_info     *ret)
4560
1
{
4561
1
  const zend_func_info *info = ZEND_FUNC_INFO(op_array);
4562
1
  const zend_ssa *ssa = &info->ssa;
4563
1
  uint32_t blocks_count = info->ssa.cfg.blocks_count;
4564
1
  const zend_basic_block *blocks = info->ssa.cfg.blocks;
4565
1
  uint32_t t1;
4566
1
  uint32_t tmp = 0;
4567
1
  zend_class_entry *tmp_ce = NULL;
4568
1
  int tmp_is_instanceof = -1;
4569
1
  zend_class_entry *arg_ce;
4570
1
  bool arg_is_instanceof;
4571
1
  zend_ssa_range tmp_range = {0, 0, 0, 0};
4572
1
  int tmp_has_range = -1;
4573
4574
1
  if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
4575
0
    ret->type = MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN;
4576
0
    ret->ce = zend_ce_generator;
4577
0
    ret->is_instanceof = 0;
4578
0
    ret->range = tmp_range;
4579
0
    ret->has_range = 0;
4580
0
    return;
4581
0
  }
4582
4583
1
  if (!ret->type) {
4584
    /* We will intersect the type later. */
4585
1
    ret->type = MAY_BE_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_KEY_ANY
4586
1
      | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF;
4587
1
  }
4588
4589
2
  for (uint32_t j = 0; j < blocks_count; j++) {
4590
1
    if ((blocks[j].flags & ZEND_BB_REACHABLE) && blocks[j].len != 0) {
4591
1
      zend_op *opline = op_array->opcodes + blocks[j].start + blocks[j].len - 1;
4592
4593
1
      if (opline->opcode == ZEND_RETURN || opline->opcode == ZEND_RETURN_BY_REF) {
4594
1
        const zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[opline - op_array->opcodes] : NULL;
4595
1
        if (!recursive && ssa_op && info->ssa.var_info &&
4596
0
            ssa_op->op1_use >= 0 &&
4597
0
            info->ssa.var_info[ssa_op->op1_use].recursive) {
4598
0
          continue;
4599
0
        }
4600
1
        if (is_recursive_tail_call(op_array, opline)) {
4601
0
          continue;
4602
0
        }
4603
1
        t1 = OP1_INFO();
4604
1
        if (t1 & MAY_BE_UNDEF) {
4605
0
          t1 |= MAY_BE_NULL;
4606
0
        }
4607
1
        if (opline->opcode == ZEND_RETURN) {
4608
1
          if (t1 & MAY_BE_RC1) {
4609
0
            t1 |= MAY_BE_RCN;
4610
0
          }
4611
1
          t1 &= ~(MAY_BE_UNDEF | MAY_BE_REF);
4612
1
        } else {
4613
0
          t1 |= MAY_BE_REF;
4614
0
          t1 &= ~(MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN);
4615
0
        }
4616
1
        tmp |= t1;
4617
4618
1
        if (ssa_op && info->ssa.var_info &&
4619
1
            ssa_op->op1_use >= 0 && !(t1 & MAY_BE_REF) &&
4620
0
            info->ssa.var_info[ssa_op->op1_use].ce) {
4621
0
          arg_ce = info->ssa.var_info[ssa_op->op1_use].ce;
4622
0
          arg_is_instanceof = info->ssa.var_info[ssa_op->op1_use].is_instanceof;
4623
1
        } else {
4624
1
          arg_ce = NULL;
4625
1
          arg_is_instanceof = false;
4626
1
        }
4627
4628
1
        if (tmp_is_instanceof < 0) {
4629
1
          tmp_ce = arg_ce;
4630
1
          tmp_is_instanceof = arg_is_instanceof;
4631
1
        } else if (arg_ce && arg_ce == tmp_ce) {
4632
0
          if (tmp_is_instanceof != arg_is_instanceof) {
4633
0
            tmp_is_instanceof = 1;
4634
0
          }
4635
0
        } else {
4636
0
          tmp_ce = NULL;
4637
0
          tmp_is_instanceof = 0;
4638
0
        }
4639
4640
1
        if (opline->op1_type == IS_CONST) {
4641
1
          const zval *zv = CRT_CONSTANT(opline->op1);
4642
4643
1
          if (Z_TYPE_P(zv) == IS_LONG) {
4644
1
            if (tmp_has_range < 0) {
4645
1
              tmp_has_range = 1;
4646
1
              tmp_range.underflow = 0;
4647
1
              tmp_range.min = Z_LVAL_P(zv);
4648
1
              tmp_range.max = Z_LVAL_P(zv);
4649
1
              tmp_range.overflow = 0;
4650
1
            } else if (tmp_has_range) {
4651
0
              if (!tmp_range.underflow) {
4652
0
                tmp_range.min = MIN(tmp_range.min, Z_LVAL_P(zv));
4653
0
              }
4654
0
              if (!tmp_range.overflow) {
4655
0
                tmp_range.max = MAX(tmp_range.max, Z_LVAL_P(zv));
4656
0
              }
4657
0
            }
4658
1
          } else {
4659
0
            tmp_has_range = 0;
4660
0
          }
4661
1
        } else if (ssa_op && info->ssa.var_info && ssa_op->op1_use >= 0) {
4662
0
          if (info->ssa.var_info[ssa_op->op1_use].has_range) {
4663
0
            if (tmp_has_range < 0) {
4664
0
              tmp_has_range = 1;
4665
0
              tmp_range = info->ssa.var_info[ssa_op->op1_use].range;
4666
0
            } else if (tmp_has_range) {
4667
              /* union */
4668
0
              if (info->ssa.var_info[ssa_op->op1_use].range.underflow) {
4669
0
                tmp_range.underflow = 1;
4670
0
                tmp_range.min = ZEND_LONG_MIN;
4671
0
              } else {
4672
0
                tmp_range.min = MIN(tmp_range.min, info->ssa.var_info[ssa_op->op1_use].range.min);
4673
0
              }
4674
0
              if (info->ssa.var_info[ssa_op->op1_use].range.overflow) {
4675
0
                tmp_range.overflow = 1;
4676
0
                tmp_range.max = ZEND_LONG_MAX;
4677
0
              } else {
4678
0
                tmp_range.max = MAX(tmp_range.max, info->ssa.var_info[ssa_op->op1_use].range.max);
4679
0
              }
4680
0
            }
4681
0
          } else if (!widening) {
4682
0
            tmp_has_range = 1;
4683
0
            tmp_range.underflow = 1;
4684
0
            tmp_range.min = ZEND_LONG_MIN;
4685
0
            tmp_range.max = ZEND_LONG_MAX;
4686
0
            tmp_range.overflow = 1;
4687
0
          }
4688
0
        } else {
4689
0
          tmp_has_range = 0;
4690
0
        }
4691
1
      }
4692
1
    }
4693
1
  }
4694
4695
1
  if (!(op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
4696
1
    if (tmp_is_instanceof < 0) {
4697
0
      tmp_is_instanceof = 0;
4698
0
      tmp_ce = NULL;
4699
0
    }
4700
1
    if (tmp_has_range < 0) {
4701
0
      tmp_has_range = 0;
4702
0
    }
4703
1
    ret->ce = tmp_ce;
4704
1
    ret->is_instanceof = tmp_is_instanceof;
4705
1
  }
4706
1
  ret->type &= tmp;
4707
1
  ret->range = tmp_range;
4708
1
  ret->has_range = tmp_has_range;
4709
1
}
4710
4711
static zend_result zend_infer_types(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level)
4712
1
{
4713
1
  int ssa_vars_count = ssa->vars_count;
4714
1
  int j;
4715
1
  zend_bitset worklist;
4716
1
  ALLOCA_FLAG(use_heap);
4717
4718
1
  worklist = do_alloca(sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count), use_heap);
4719
1
  memset(worklist, 0, sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count));
4720
4721
  /* Type Inference */
4722
1
  for (j = op_array->last_var; j < ssa_vars_count; j++) {
4723
0
    zend_bitset_incl(worklist, j);
4724
0
  }
4725
4726
1
  if (zend_infer_types_ex(op_array, script, ssa, worklist, optimization_level) == FAILURE) {
4727
0
    free_alloca(worklist,  use_heap);
4728
0
    return FAILURE;
4729
0
  }
4730
4731
1
  if (optimization_level & ZEND_OPTIMIZER_NARROW_TO_DOUBLE) {
4732
    /* Narrowing integer initialization to doubles */
4733
1
    zend_type_narrowing(op_array, script, ssa, optimization_level);
4734
1
  }
4735
4736
1
  if (ZEND_FUNC_INFO(op_array)) {
4737
1
    zend_func_return_info(op_array, script, 1, 0, &ZEND_FUNC_INFO(op_array)->return_info);
4738
1
  }
4739
4740
1
  free_alloca(worklist,  use_heap);
4741
1
  return SUCCESS;
4742
1
}
4743
4744
static void zend_mark_cv_references(const zend_op_array *op_array, const zend_script *script, const zend_ssa *ssa)
4745
1
{
4746
1
  int var, def;
4747
1
  const zend_op *opline;
4748
1
  zend_arg_info *arg_info;
4749
1
  uint32_t worklist_len = zend_bitset_len(ssa->vars_count);
4750
1
  zend_bitset worklist;
4751
1
  ALLOCA_FLAG(use_heap);
4752
4753
1
  worklist = do_alloca(sizeof(zend_ulong) * worklist_len, use_heap);
4754
1
  memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
4755
4756
  /* Collect SSA variables which definitions creates PHP reference */
4757
1
  for (var = 0; var < ssa->vars_count; var++) {
4758
0
    def = ssa->vars[var].definition;
4759
0
    if (def >= 0 && ssa->vars[var].var < op_array->last_var) {
4760
0
      opline = op_array->opcodes + def;
4761
0
      if (ssa->ops[def].result_def == var) {
4762
0
        switch (opline->opcode) {
4763
0
          case ZEND_RECV:
4764
0
          case ZEND_RECV_INIT:
4765
0
            arg_info = &op_array->arg_info[opline->op1.num-1];
4766
0
            if (!ZEND_ARG_SEND_MODE(arg_info)) {
4767
0
              continue;
4768
0
            }
4769
0
            break;
4770
0
          default:
4771
0
            continue;
4772
0
        }
4773
0
      } else if (ssa->ops[def].op1_def == var) {
4774
0
        switch (opline->opcode) {
4775
0
          case ZEND_ASSIGN_REF:
4776
0
          case ZEND_MAKE_REF:
4777
0
          case ZEND_FE_RESET_RW:
4778
0
          case ZEND_BIND_GLOBAL:
4779
0
          case ZEND_SEND_REF:
4780
0
          case ZEND_SEND_VAR_EX:
4781
0
          case ZEND_SEND_FUNC_ARG:
4782
0
          case ZEND_BIND_INIT_STATIC_OR_JMP:
4783
0
            break;
4784
0
          case ZEND_INIT_ARRAY:
4785
0
          case ZEND_ADD_ARRAY_ELEMENT:
4786
0
            if (!(opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
4787
0
              continue;
4788
0
            }
4789
0
            break;
4790
0
          case ZEND_BIND_STATIC:
4791
0
            if (!(opline->extended_value & ZEND_BIND_REF)) {
4792
0
              continue;
4793
0
            }
4794
0
            break;
4795
0
          case ZEND_YIELD:
4796
0
            if (!(op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
4797
0
              continue;
4798
0
            }
4799
0
            break;
4800
0
          case ZEND_OP_DATA:
4801
0
            switch ((opline-1)->opcode) {
4802
0
              case ZEND_ASSIGN_OBJ_REF:
4803
0
              case ZEND_ASSIGN_STATIC_PROP_REF:
4804
0
                break;
4805
0
              default:
4806
0
                continue;
4807
0
            }
4808
0
            break;
4809
0
          default:
4810
0
            continue;
4811
0
        }
4812
0
      } else if (ssa->ops[def].op2_def == var) {
4813
0
        switch (opline->opcode) {
4814
0
          case ZEND_ASSIGN_REF:
4815
0
          case ZEND_FE_FETCH_RW:
4816
0
            break;
4817
0
          case ZEND_BIND_LEXICAL:
4818
0
            if (!(opline->extended_value & ZEND_BIND_REF)) {
4819
0
              continue;
4820
0
            }
4821
0
            break;
4822
0
          default:
4823
0
            continue;
4824
0
        }
4825
0
      } else {
4826
0
        ZEND_UNREACHABLE();
4827
0
      }
4828
0
      zend_bitset_incl(worklist, var);
4829
0
    } else if (ssa->var_info[var].type & MAY_BE_REF) {
4830
0
      zend_bitset_incl(worklist, var);
4831
0
    } else if (ssa->vars[var].alias == SYMTABLE_ALIAS) {
4832
0
      zend_bitset_incl(worklist, var);
4833
0
    }
4834
0
  }
4835
4836
  /* Set and propagate MAY_BE_REF */
4837
3
  WHILE_WORKLIST(worklist, worklist_len, var) {
4838
4839
3
    ssa->var_info[var].type |= MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
4840
4841
3
    if (ssa->vars[var].phi_use_chain) {
4842
0
      zend_ssa_phi *p = ssa->vars[var].phi_use_chain;
4843
0
      do {
4844
0
        if (!(ssa->var_info[p->ssa_var].type & MAY_BE_REF)) {
4845
0
          zend_bitset_incl(worklist, p->ssa_var);
4846
0
        }
4847
0
        p = zend_ssa_next_use_phi(ssa, var, p);
4848
0
      } while (p);
4849
0
    }
4850
4851
3
    if (ssa->vars[var].use_chain >= 0) {
4852
0
      int use = ssa->vars[var].use_chain;
4853
0
      FOREACH_USE(&ssa->vars[var], use) {
4854
0
        zend_ssa_op *op = ssa->ops + use;
4855
0
        if (op->op1_use == var && op->op1_def >= 0) {
4856
0
          if (!(ssa->var_info[op->op1_def].type & MAY_BE_REF)) {
4857
            /* Unset breaks references (outside global scope). */
4858
0
            if (op_array->opcodes[use].opcode == ZEND_UNSET_CV
4859
0
                && op_array->function_name) {
4860
0
              continue;
4861
0
            }
4862
0
            zend_bitset_incl(worklist, op->op1_def);
4863
0
          }
4864
0
        }
4865
0
        if (op->op2_use == var && op->op2_def >= 0) {
4866
0
          if (!(ssa->var_info[op->op2_def].type & MAY_BE_REF)) {
4867
0
            zend_bitset_incl(worklist, op->op2_def);
4868
0
          }
4869
0
        }
4870
0
        if (op->result_use == var && op->result_def >= 0) {
4871
0
          if (!(ssa->var_info[op->result_def].type & MAY_BE_REF)) {
4872
0
            zend_bitset_incl(worklist, op->result_def);
4873
0
          }
4874
0
        }
4875
0
      } FOREACH_USE_END();
4876
0
    }
4877
3
  } WHILE_WORKLIST_END();
4878
4879
1
  free_alloca(worklist,  use_heap);
4880
1
}
4881
4882
ZEND_API zend_result zend_ssa_inference(zend_arena **arena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_long optimization_level) /* {{{ */
4883
1
{
4884
1
  zend_ssa_var_info *ssa_var_info;
4885
1
  int i;
4886
4887
1
  if (!ssa->var_info) {
4888
1
    ssa->var_info = zend_arena_calloc(arena, ssa->vars_count, sizeof(zend_ssa_var_info));
4889
1
  }
4890
1
  ssa_var_info = ssa->var_info;
4891
4892
1
  if (!op_array->function_name) {
4893
1
    for (i = 0; i < op_array->last_var; i++) {
4894
0
      ssa_var_info[i].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY  | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
4895
0
      ssa_var_info[i].has_range = 0;
4896
0
    }
4897
1
  } else {
4898
0
    for (i = 0; i < op_array->last_var; i++) {
4899
0
      ssa_var_info[i].type = MAY_BE_UNDEF;
4900
0
      ssa_var_info[i].has_range = 0;
4901
0
      if (ssa->vars[i].alias) {
4902
0
        ssa_var_info[i].type |= get_ssa_alias_types(ssa->vars[i].alias);
4903
0
      }
4904
0
    }
4905
0
  }
4906
1
  for (i = op_array->last_var; i < ssa->vars_count; i++) {
4907
0
    ssa_var_info[i].type = 0;
4908
0
    ssa_var_info[i].has_range = 0;
4909
0
  }
4910
4911
1
  zend_mark_cv_references(op_array, script, ssa);
4912
4913
1
  zend_infer_ranges(op_array, ssa);
4914
4915
1
  if (zend_infer_types(op_array, script, ssa, optimization_level) == FAILURE) {
4916
0
    return FAILURE;
4917
0
  }
4918
4919
1
  return SUCCESS;
4920
1
}
4921
/* }}} */
4922
4923
ZEND_API bool zend_may_throw_ex(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa, uint32_t t1, uint32_t t2)
4924
0
{
4925
0
  if (opline->op1_type == IS_CV) {
4926
0
    if (t1 & MAY_BE_UNDEF) {
4927
0
      switch (opline->opcode) {
4928
0
        case ZEND_UNSET_VAR:
4929
0
        case ZEND_ISSET_ISEMPTY_VAR:
4930
0
          return 1;
4931
0
        case ZEND_ISSET_ISEMPTY_DIM_OBJ:
4932
0
        case ZEND_ISSET_ISEMPTY_PROP_OBJ:
4933
0
        case ZEND_ASSIGN:
4934
0
        case ZEND_ASSIGN_DIM:
4935
0
        case ZEND_ASSIGN_REF:
4936
0
        case ZEND_BIND_GLOBAL:
4937
0
        case ZEND_BIND_STATIC:
4938
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
4939
0
        case ZEND_FETCH_DIM_IS:
4940
0
        case ZEND_FETCH_OBJ_IS:
4941
0
        case ZEND_SEND_REF:
4942
0
        case ZEND_UNSET_CV:
4943
0
        case ZEND_ISSET_ISEMPTY_CV:
4944
0
        case ZEND_MAKE_REF:
4945
0
        case ZEND_FETCH_DIM_W:
4946
0
          break;
4947
0
        default:
4948
          /* undefined variable warning */
4949
0
          return 1;
4950
0
      }
4951
0
    }
4952
0
  } else if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
4953
0
    if ((t1 & MAY_BE_RC1)
4954
0
     && (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
4955
0
      switch (opline->opcode) {
4956
0
        case ZEND_CASE:
4957
0
        case ZEND_CASE_STRICT:
4958
0
        case ZEND_FE_FETCH_R:
4959
0
        case ZEND_FE_FETCH_RW:
4960
0
        case ZEND_FETCH_LIST_R:
4961
0
        case ZEND_QM_ASSIGN:
4962
0
        case ZEND_SEND_VAL:
4963
0
        case ZEND_SEND_VAL_EX:
4964
0
        case ZEND_SEND_VAR:
4965
0
        case ZEND_SEND_VAR_EX:
4966
0
        case ZEND_SEND_FUNC_ARG:
4967
0
        case ZEND_SEND_VAR_NO_REF:
4968
0
        case ZEND_SEND_VAR_NO_REF_EX:
4969
0
        case ZEND_SEND_REF:
4970
0
        case ZEND_SEPARATE:
4971
0
        case ZEND_END_SILENCE:
4972
0
        case ZEND_MAKE_REF:
4973
0
          break;
4974
0
        default:
4975
          /* destructor may be called */
4976
0
          return 1;
4977
0
      }
4978
0
    }
4979
0
  }
4980
4981
0
  if (opline->op2_type == IS_CV) {
4982
0
    if (t2 & MAY_BE_UNDEF) {
4983
0
      switch (opline->opcode) {
4984
0
        case ZEND_ASSIGN_REF:
4985
0
        case ZEND_FE_FETCH_R:
4986
0
        case ZEND_FE_FETCH_RW:
4987
0
          break;
4988
0
        default:
4989
          /* undefined variable warning */
4990
0
          return 1;
4991
0
      }
4992
0
    }
4993
0
  } else if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
4994
0
    if ((t2 & MAY_BE_RC1)
4995
0
     && (t2 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
4996
0
      switch (opline->opcode) {
4997
0
        case ZEND_ASSIGN:
4998
0
        case ZEND_FE_FETCH_R:
4999
0
        case ZEND_FE_FETCH_RW:
5000
0
          break;
5001
0
        default:
5002
          /* destructor may be called */
5003
0
          return 1;
5004
0
      }
5005
0
    }
5006
0
  }
5007
5008
0
  switch (opline->opcode) {
5009
0
    case ZEND_NOP:
5010
0
    case ZEND_QM_ASSIGN:
5011
0
    case ZEND_JMP:
5012
0
    case ZEND_CHECK_VAR:
5013
0
    case ZEND_MAKE_REF:
5014
0
    case ZEND_BEGIN_SILENCE:
5015
0
    case ZEND_END_SILENCE:
5016
0
    case ZEND_FREE:
5017
0
    case ZEND_FE_FREE:
5018
0
    case ZEND_SEPARATE:
5019
0
    case ZEND_TYPE_CHECK:
5020
0
    case ZEND_DEFINED:
5021
0
    case ZEND_ISSET_ISEMPTY_THIS:
5022
0
    case ZEND_COALESCE:
5023
0
    case ZEND_SWITCH_LONG:
5024
0
    case ZEND_SWITCH_STRING:
5025
0
    case ZEND_MATCH:
5026
0
    case ZEND_ISSET_ISEMPTY_VAR:
5027
0
    case ZEND_ISSET_ISEMPTY_CV:
5028
0
    case ZEND_FUNC_NUM_ARGS:
5029
0
    case ZEND_FUNC_GET_ARGS:
5030
0
    case ZEND_COPY_TMP:
5031
0
    case ZEND_JMP_NULL:
5032
0
    case ZEND_JMP_FRAMELESS:
5033
0
      return 0;
5034
0
    case ZEND_IS_IDENTICAL:
5035
0
    case ZEND_IS_NOT_IDENTICAL:
5036
0
    case ZEND_CASE_STRICT:
5037
      /* Array to array comparison may lead to recursion. */
5038
0
      return (t1 & t2) & MAY_BE_ARRAY_OF_ARRAY;
5039
0
    case ZEND_SEND_VAR:
5040
0
    case ZEND_SEND_VAL:
5041
0
    case ZEND_SEND_REF:
5042
0
    case ZEND_SEND_VAR_EX:
5043
0
    case ZEND_SEND_FUNC_ARG:
5044
0
    case ZEND_CHECK_FUNC_ARG:
5045
      /* May throw for named params. */
5046
0
      return opline->op2_type == IS_CONST;
5047
0
    case ZEND_INIT_FCALL:
5048
      /* can't throw, because call is resolved at compile time */
5049
0
      return 0;
5050
0
    case ZEND_BIND_GLOBAL:
5051
0
      if ((opline+1)->opcode == ZEND_BIND_GLOBAL) {
5052
0
        return zend_may_throw(opline + 1, ssa_op ? ssa_op + 1 : NULL, op_array, ssa);
5053
0
      }
5054
0
      return 0;
5055
0
    case ZEND_ADD:
5056
0
      if ((t1 & MAY_BE_ANY) == MAY_BE_ARRAY
5057
0
       && (t2 & MAY_BE_ANY) == MAY_BE_ARRAY) {
5058
0
        return 0;
5059
0
      }
5060
0
      return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5061
0
        (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5062
0
    case ZEND_DIV:
5063
0
      if (!OP2_HAS_RANGE() ||
5064
0
        (OP2_MIN_RANGE() <= 0 && OP2_MAX_RANGE() >= 0)) {
5065
        /* Division by zero */
5066
0
        return 1;
5067
0
      }
5068
0
      ZEND_FALLTHROUGH;
5069
0
    case ZEND_SUB:
5070
0
    case ZEND_MUL:
5071
0
    case ZEND_POW:
5072
0
      return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5073
0
        (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5074
    /* Ops may throw if not an integer */
5075
0
    case ZEND_MOD:
5076
0
      if (!OP2_HAS_RANGE() ||
5077
0
        (OP2_MIN_RANGE() <= 0 && OP2_MAX_RANGE() >= 0)) {
5078
        /* Division by zero */
5079
0
        return 1;
5080
0
      }
5081
0
      ZEND_FALLTHROUGH;
5082
0
    case ZEND_SL:
5083
0
    case ZEND_SR:
5084
0
      return (t1 & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5085
0
        (t2 & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5086
0
        !OP2_HAS_RANGE() ||
5087
0
        OP2_MIN_RANGE() < 0;
5088
0
    case ZEND_CONCAT:
5089
0
    case ZEND_FAST_CONCAT:
5090
0
      return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
5091
0
        (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
5092
0
    case ZEND_BW_OR:
5093
0
    case ZEND_BW_AND:
5094
0
    case ZEND_BW_XOR:
5095
0
      if ((t1 & MAY_BE_ANY) == MAY_BE_STRING
5096
0
       && (t2 & MAY_BE_ANY) == MAY_BE_STRING) {
5097
0
        return 0;
5098
0
      }
5099
0
      return (t1 & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5100
0
        (t2 & (MAY_BE_STRING|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5101
0
    case ZEND_BW_NOT:
5102
0
      return (t1 & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5103
0
    case ZEND_PRE_INC:
5104
0
    case ZEND_POST_INC:
5105
0
      return (t1 & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5106
    /* null emits a warning as it has no effect compared to ++ which converts the value to 1 */
5107
0
    case ZEND_PRE_DEC:
5108
0
    case ZEND_POST_DEC:
5109
0
      return (t1 & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5110
0
    case ZEND_JMPZ:
5111
0
    case ZEND_JMPNZ:
5112
0
    case ZEND_JMPZ_EX:
5113
0
    case ZEND_JMPNZ_EX:
5114
0
    case ZEND_JMP_SET:
5115
0
      return (t1 & MAY_BE_OBJECT);
5116
0
    case ZEND_BOOL:
5117
0
    case ZEND_BOOL_NOT:
5118
      /* NAN Cast to bool will warn, but if we have a range it is fine */
5119
0
      return (t1 & MAY_BE_OBJECT) || ((t1 & MAY_BE_DOUBLE) && !OP1_HAS_RANGE());
5120
0
    case ZEND_BOOL_XOR:
5121
0
      return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_OBJECT);
5122
0
    case ZEND_IS_EQUAL:
5123
0
    case ZEND_IS_NOT_EQUAL:
5124
0
    case ZEND_IS_SMALLER:
5125
0
    case ZEND_IS_SMALLER_OR_EQUAL:
5126
0
    case ZEND_CASE:
5127
0
    case ZEND_SPACESHIP:
5128
0
      if ((t1 & MAY_BE_ANY) == MAY_BE_NULL
5129
0
       || (t2 & MAY_BE_ANY) == MAY_BE_NULL) {
5130
0
        return 0;
5131
0
      }
5132
0
      return (t1 & (MAY_BE_OBJECT|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT)) || (t2 & (MAY_BE_OBJECT|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT));
5133
0
    case ZEND_ASSIGN_OP:
5134
0
      if (opline->extended_value == ZEND_ADD) {
5135
0
        if ((t1 & MAY_BE_ANY) == MAY_BE_ARRAY
5136
0
         && (t2 & MAY_BE_ANY) == MAY_BE_ARRAY) {
5137
0
          return 0;
5138
0
        }
5139
0
        return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5140
0
          (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5141
0
      } else if (opline->extended_value == ZEND_DIV ||
5142
0
        opline->extended_value == ZEND_MOD) {
5143
0
        if (!OP2_HAS_RANGE() ||
5144
0
          (OP2_MIN_RANGE() <= 0 && OP2_MAX_RANGE() >= 0)) {
5145
          /* Division by zero */
5146
0
          return 1;
5147
0
        }
5148
0
        return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5149
0
          (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5150
0
      } else if (opline->extended_value == ZEND_SUB ||
5151
0
        opline->extended_value == ZEND_MUL ||
5152
0
        opline->extended_value == ZEND_POW) {
5153
0
        return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5154
0
          (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5155
0
      } else if (opline->extended_value == ZEND_SL ||
5156
0
        opline->extended_value == ZEND_SR) {
5157
0
        return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5158
0
          (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5159
0
          !OP2_HAS_RANGE() ||
5160
0
          OP2_MIN_RANGE() < 0;
5161
0
      } else if (opline->extended_value == ZEND_CONCAT) {
5162
0
        return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
5163
0
          (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
5164
0
      } else if (opline->extended_value == ZEND_BW_OR ||
5165
0
        opline->extended_value == ZEND_BW_AND ||
5166
0
        opline->extended_value == ZEND_BW_XOR) {
5167
0
        if ((t1 & MAY_BE_ANY) == MAY_BE_STRING
5168
0
         && (t2 & MAY_BE_ANY) == MAY_BE_STRING) {
5169
0
          return 0;
5170
0
        }
5171
0
        return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) ||
5172
0
          (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5173
0
      }
5174
0
      return 1;
5175
0
    case ZEND_ASSIGN:
5176
0
      if (t1 & MAY_BE_REF) {
5177
0
        return 1;
5178
0
      }
5179
0
      ZEND_FALLTHROUGH;
5180
0
    case ZEND_UNSET_VAR:
5181
0
      return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY));
5182
0
    case ZEND_BIND_STATIC:
5183
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
5184
0
      if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)) {
5185
        /* Destructor may throw. */
5186
0
        return 1;
5187
0
      }
5188
0
      return 0;
5189
0
    case ZEND_ASSIGN_DIM:
5190
0
      if ((opline+1)->op1_type == IS_CV) {
5191
0
        if (OP1_DATA_INFO() & MAY_BE_UNDEF) {
5192
0
          return 1;
5193
0
        }
5194
0
      }
5195
0
      if (t1 & (MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_REF)) {
5196
0
        return 1;
5197
0
      }
5198
0
      return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_STRING|MAY_BE_LONG|MAY_BE_DOUBLE)) || opline->op2_type == IS_UNUSED ||
5199
0
        (t2 & (MAY_BE_UNDEF|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5200
0
    case ZEND_ASSIGN_OBJ:
5201
0
      if (t1 & (MAY_BE_ANY-MAY_BE_OBJECT)) {
5202
0
        return 1;
5203
0
      }
5204
0
      if ((opline+1)->op1_type == IS_CV) {
5205
0
        if (OP1_DATA_INFO() & MAY_BE_UNDEF) {
5206
0
          return 1;
5207
0
        }
5208
0
      }
5209
0
      if (ssa_op->op1_use) {
5210
0
        const zend_ssa_var_info *var_info = ssa->var_info + ssa_op->op1_use;
5211
0
        const zend_class_entry *ce = var_info->ce;
5212
5213
0
        if (var_info->is_instanceof
5214
0
         || !ce
5215
0
         || ce->create_object
5216
0
         || ce->default_object_handlers->write_property != zend_std_write_property
5217
0
         || ce->default_object_handlers->get_property_ptr_ptr != zend_std_get_property_ptr_ptr
5218
0
         || ce->__get
5219
0
         || ce->__set
5220
0
         || ce->parent) {
5221
0
          return 1;
5222
0
        }
5223
5224
0
        if (opline->op2_type != IS_CONST) {
5225
0
          return 1;
5226
0
        }
5227
5228
0
        zend_string *prop_name = Z_STR_P(CRT_CONSTANT(opline->op2));
5229
0
        if (ZSTR_LEN(prop_name) > 0 && ZSTR_VAL(prop_name)[0] == '\0') {
5230
0
          return 1;
5231
0
        }
5232
5233
0
        zend_property_info *prop_info =
5234
0
          zend_hash_find_ptr(&ce->properties_info, prop_name);
5235
0
        if (prop_info) {
5236
0
          if (ZEND_TYPE_IS_SET(prop_info->type)) {
5237
0
            return 1;
5238
0
          }
5239
0
          return !(prop_info->flags & ZEND_ACC_PUBLIC)
5240
0
            && prop_info->ce != op_array->scope;
5241
0
        } else {
5242
0
          return !(ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES);
5243
0
        }
5244
0
      }
5245
0
      return 1;
5246
0
    case ZEND_ROPE_INIT:
5247
0
    case ZEND_ROPE_ADD:
5248
0
    case ZEND_ROPE_END:
5249
0
      return t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT);
5250
0
    case ZEND_INIT_ARRAY:
5251
0
      return (opline->op2_type != IS_UNUSED) && (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5252
0
    case ZEND_ADD_ARRAY_ELEMENT:
5253
0
      return (opline->op2_type == IS_UNUSED) || (t2 & (MAY_BE_NULL|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5254
0
    case ZEND_STRLEN:
5255
0
      return (t1 & MAY_BE_ANY) != MAY_BE_STRING;
5256
0
    case ZEND_COUNT:
5257
0
      return (t1 & MAY_BE_ANY) != MAY_BE_ARRAY;
5258
0
    case ZEND_RECV_INIT:
5259
0
      if (Z_TYPE_P(CRT_CONSTANT(opline->op2)) == IS_CONSTANT_AST) {
5260
0
        return 1;
5261
0
      }
5262
0
      if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
5263
0
        uint32_t arg_num = opline->op1.num;
5264
0
        const zend_arg_info *cur_arg_info;
5265
5266
0
        if (EXPECTED(arg_num <= op_array->num_args)) {
5267
0
          cur_arg_info = &op_array->arg_info[arg_num-1];
5268
0
        } else if (UNEXPECTED(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
5269
0
          cur_arg_info = &op_array->arg_info[op_array->num_args];
5270
0
        } else {
5271
0
          return 0;
5272
0
        }
5273
0
        return ZEND_TYPE_IS_SET(cur_arg_info->type);
5274
0
      } else {
5275
0
        return 0;
5276
0
      }
5277
0
    case ZEND_FETCH_IS:
5278
0
      return (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
5279
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
5280
0
      return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT));
5281
0
    case ZEND_FETCH_DIM_IS:
5282
0
      return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_DOUBLE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
5283
0
    case ZEND_CAST:
5284
0
      switch (opline->extended_value) {
5285
0
        case IS_LONG:
5286
0
          return (t1 & (MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_OBJECT));
5287
0
        case IS_DOUBLE:
5288
0
          return (t1 & MAY_BE_OBJECT);
5289
0
        case IS_STRING:
5290
0
          return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
5291
0
        case IS_ARRAY:
5292
0
          return (t1 & MAY_BE_OBJECT);
5293
0
        case IS_OBJECT:
5294
0
          return 0;
5295
0
        default: ZEND_UNREACHABLE();
5296
0
      }
5297
      /* GCC is getting confused here for the Wimplicit-fallthrough warning with
5298
       * default: ZEND_UNREACHABLE(); macro */
5299
0
      return 0;
5300
0
    case ZEND_ARRAY_KEY_EXISTS:
5301
0
      if ((t2 & MAY_BE_ANY) != MAY_BE_ARRAY) {
5302
0
        return 1;
5303
0
      }
5304
0
      if ((t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE))) {
5305
0
        return 1;
5306
0
      }
5307
0
      return 0;
5308
0
    case ZEND_FE_RESET_R:
5309
0
    case ZEND_FE_RESET_RW:
5310
0
      if ((t1 & (MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
5311
0
        return 1;
5312
0
      }
5313
0
      return 0;
5314
0
    case ZEND_FE_FETCH_R:
5315
0
      if ((t1 & (MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
5316
0
        return 1;
5317
0
      }
5318
0
      if (opline->op2_type == IS_CV
5319
0
       && (t2 & MAY_BE_RC1)
5320
0
       && (t2 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY))) {
5321
0
        return 1;
5322
0
      }
5323
0
      return 0;
5324
0
    case ZEND_FETCH_DIM_W:
5325
0
    case ZEND_FETCH_LIST_W:
5326
0
      if (t1 & (MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
5327
0
        return 1;
5328
0
      }
5329
0
      if (t2 & (MAY_BE_RESOURCE|MAY_BE_ARRAY|MAY_BE_OBJECT)) {
5330
0
        return 1;
5331
0
      }
5332
0
      if (opline->op2_type == IS_UNUSED) {
5333
0
        return 1;
5334
0
      }
5335
0
      return 0;
5336
0
    case ZEND_YIELD_FROM: {
5337
0
      uint32_t t1 = OP1_INFO();
5338
0
      if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY && MAY_BE_EMPTY_ONLY(t1)) {
5339
0
        return false;
5340
0
      }
5341
0
      return true;
5342
0
    }
5343
0
    default:
5344
0
      return 1;
5345
0
  }
5346
0
}
5347
5348
ZEND_API bool zend_may_throw(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, const zend_ssa *ssa)
5349
0
{
5350
0
  return zend_may_throw_ex(opline, ssa_op, op_array, ssa, OP1_INFO(), OP2_INFO());
5351
0
}