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/sccp.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, SCCP - Sparse Conditional Constant Propagation          |
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: Nikita Popov <nikic@php.net>                                |
14
   |          Dmitry Stogov <dmitry@php.net>                              |
15
   +----------------------------------------------------------------------+
16
*/
17
18
#include "zend_API.h"
19
#include "zend_exceptions.h"
20
#include "zend_ini.h"
21
#include "zend_type_info.h"
22
#include "Optimizer/zend_optimizer_internal.h"
23
#include "Optimizer/zend_call_graph.h"
24
#include "Optimizer/zend_inference.h"
25
#include "Optimizer/scdf.h"
26
#include "Optimizer/zend_dump.h"
27
28
/* This implements sparse conditional constant propagation (SCCP) based on the SCDF framework. The
29
 * used value lattice is defined as follows:
30
 *
31
 * BOT < {constant values} < TOP
32
 *
33
 * TOP indicates an underdefined value, i.e. that we do not yet know the value of variable.
34
 * BOT indicates an overdefined value, i.e. that we know the variable to be non-constant.
35
 *
36
 * All variables are optimistically initialized to TOP, apart from the implicit variables defined
37
 * at the start of the first block. Note that variables that MAY_BE_REF are *not* initialized to
38
 * BOT. We rely on the fact that any operation resulting in a reference will produce a BOT anyway.
39
 * This is better because such operations might never be reached due to the conditional nature of
40
 * the algorithm.
41
 *
42
 * The meet operation for phi functions is defined as follows:
43
 * BOT + any = BOT
44
 * TOP + any = any
45
 * C_i + C_i = C_i (i.e. two equal constants)
46
 * C_i + C_j = BOT (i.e. two different constants)
47
 *
48
 * When evaluating instructions TOP and BOT are handled as follows:
49
 * a) If any operand is BOT, the result is BOT. The main exception to this is op1 of ASSIGN, which
50
 *    is ignored. However, if the op1 MAY_BE_REF we do have to propagate the BOT.
51
 * b) Otherwise, if the instruction can never be evaluated (either in general, or with the
52
 *    specific modifiers) the result is BOT.
53
 * c) Otherwise, if any operand is TOP, the result is TOP.
54
 * d) Otherwise (at this point all operands are known and constant), if we can compute the result
55
 *    for these specific constants (without throwing notices or similar) then that is the result.
56
 * e) Otherwise the result is BOT.
57
 *
58
 * It is sometimes possible to determine a result even if one argument is TOP / BOT, e.g. for things
59
 * like BOT*0. Right now we don't bother with this.
60
 *
61
 * Feasible successors for conditional branches are determined as follows:
62
 * a) If we don't support the branch type or branch on BOT, all successors are feasible.
63
 * b) Otherwise, if we branch on TOP none of the successors are feasible.
64
 * c) Otherwise (we branch on a constant), the feasible successors are marked based on the constant
65
 *    (usually only one successor will be feasible).
66
 *
67
 * The original SCCP algorithm is extended with ability to propagate constant array
68
 * elements and object properties. The extension is based on a variation of Array
69
 * SSA form and its application to Spare Constant Propagation, described at
70
 * "Array SSA Form" by Vivek Sarkar, Kathleen Knobe and Stephen Fink in chapter
71
 * 16 of the SSA book.
72
 */
73
74
#define SCP_DEBUG 0
75
76
typedef struct _sccp_ctx {
77
  scdf_ctx scdf;
78
  zend_call_info **call_map;
79
  zval *values;
80
  zval top;
81
  zval bot;
82
} sccp_ctx;
83
84
1
#define TOP ((uint8_t)-1)
85
2
#define BOT ((uint8_t)-2)
86
0
#define PARTIAL_ARRAY ((uint8_t)-3)
87
0
#define PARTIAL_OBJECT ((uint8_t)-4)
88
0
#define IS_TOP(zv) (Z_TYPE_P(zv) == TOP)
89
1
#define IS_BOT(zv) (Z_TYPE_P(zv) == BOT)
90
0
#define IS_PARTIAL_ARRAY(zv) (Z_TYPE_P(zv) == PARTIAL_ARRAY)
91
0
#define IS_PARTIAL_OBJECT(zv) (Z_TYPE_P(zv) == PARTIAL_OBJECT)
92
93
0
#define MAKE_PARTIAL_ARRAY(zv) (Z_TYPE_INFO_P(zv) = PARTIAL_ARRAY | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
94
0
#define MAKE_PARTIAL_OBJECT(zv) (Z_TYPE_INFO_P(zv) = PARTIAL_OBJECT | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
95
96
1
#define MAKE_TOP(zv) (Z_TYPE_INFO_P(zv) = TOP)
97
1
#define MAKE_BOT(zv) (Z_TYPE_INFO_P(zv) = BOT)
98
99
0
static void scp_dump_value(zval *zv) {
100
0
  if (IS_TOP(zv)) {
101
0
    fprintf(stderr, " top");
102
0
  } else if (IS_BOT(zv)) {
103
0
    fprintf(stderr, " bot");
104
0
  } else if (Z_TYPE_P(zv) == IS_ARRAY || IS_PARTIAL_ARRAY(zv)) {
105
0
    fprintf(stderr, " %s[", IS_PARTIAL_ARRAY(zv) ? "partial " : "");
106
0
    zend_dump_ht(Z_ARRVAL_P(zv));
107
0
    fprintf(stderr, "]");
108
0
  } else if (IS_PARTIAL_OBJECT(zv)) {
109
0
    fprintf(stderr, " {");
110
0
    zend_dump_ht(Z_ARRVAL_P(zv));
111
0
    fprintf(stderr, "}");
112
0
  } else {
113
0
    zend_dump_const(zv);
114
0
  }
115
0
}
116
117
static void empty_partial_array(zval *zv)
118
0
{
119
0
  MAKE_PARTIAL_ARRAY(zv);
120
0
  Z_ARR_P(zv) = zend_new_array(8);
121
0
}
122
123
static void dup_partial_array(zval *dst, const zval *src)
124
0
{
125
0
  MAKE_PARTIAL_ARRAY(dst);
126
0
  Z_ARR_P(dst) = zend_array_dup(Z_ARR_P(src));
127
0
}
128
129
static void empty_partial_object(zval *zv)
130
0
{
131
0
  MAKE_PARTIAL_OBJECT(zv);
132
0
  Z_ARR_P(zv) = zend_new_array(8);
133
0
}
134
135
static void dup_partial_object(zval *dst, const zval *src)
136
0
{
137
0
  MAKE_PARTIAL_OBJECT(dst);
138
0
  Z_ARR_P(dst) = zend_array_dup(Z_ARR_P(src));
139
0
}
140
141
0
static inline bool value_known(zval *zv) {
142
0
  return !IS_TOP(zv) && !IS_BOT(zv);
143
0
}
144
145
/* Sets new value for variable and ensures that it is lower or equal
146
 * the previous one in the constant propagation lattice. */
147
0
static void set_value(scdf_ctx *scdf, sccp_ctx *ctx, int var, const zval *new) {
148
0
  zval *value = &ctx->values[var];
149
0
  if (IS_BOT(value) || IS_TOP(new)) {
150
0
    return;
151
0
  }
152
153
#if SCP_DEBUG
154
  fprintf(stderr, "Lowering #%d.", var);
155
  zend_dump_var(scdf->op_array, IS_CV, scdf->ssa->vars[var].var);
156
  fprintf(stderr, " from");
157
  scp_dump_value(value);
158
  fprintf(stderr, " to");
159
  scp_dump_value(new);
160
  fprintf(stderr, "\n");
161
#endif
162
163
0
  if (IS_TOP(value) || IS_BOT(new)) {
164
0
    zval_ptr_dtor_nogc(value);
165
0
    ZVAL_COPY(value, new);
166
0
    scdf_add_to_worklist(scdf, var);
167
0
    return;
168
0
  }
169
170
  /* Always replace PARTIAL_(ARRAY|OBJECT), as new maybe changed by join_partial_(arrays|object) */
171
0
  if (IS_PARTIAL_ARRAY(new) || IS_PARTIAL_OBJECT(new)) {
172
0
    if (Z_TYPE_P(value) != Z_TYPE_P(new)
173
0
      || zend_hash_num_elements(Z_ARR_P(new)) != zend_hash_num_elements(Z_ARR_P(value))) {
174
0
      zval_ptr_dtor_nogc(value);
175
0
      ZVAL_COPY(value, new);
176
0
      scdf_add_to_worklist(scdf, var);
177
0
    }
178
0
    return;
179
0
  }
180
181
0
#if ZEND_DEBUG
182
0
  ZEND_ASSERT(zend_is_identical(value, new) ||
183
0
    (Z_TYPE_P(value) == IS_DOUBLE && Z_TYPE_P(new) == IS_DOUBLE && isnan(Z_DVAL_P(value)) && isnan(Z_DVAL_P(new))));
184
0
#endif
185
0
}
186
187
1
static zval *get_op1_value(sccp_ctx *ctx, zend_op *opline, const zend_ssa_op *ssa_op) {
188
1
  if (opline->op1_type == IS_CONST) {
189
1
    return CT_CONSTANT_EX(ctx->scdf.op_array, opline->op1.constant);
190
1
  } else if (ssa_op->op1_use != -1) {
191
0
    return &ctx->values[ssa_op->op1_use];
192
0
  } else {
193
0
    return NULL;
194
0
  }
195
1
}
196
197
1
static zval *get_op2_value(sccp_ctx *ctx, const zend_op *opline, const zend_ssa_op *ssa_op) {
198
1
  if (opline->op2_type == IS_CONST) {
199
0
    return CT_CONSTANT_EX(ctx->scdf.op_array, opline->op2.constant);
200
1
  } else if (ssa_op->op2_use != -1) {
201
0
    return &ctx->values[ssa_op->op2_use];
202
1
  } else {
203
1
    return NULL;
204
1
  }
205
1
}
206
207
static bool can_replace_op1(
208
0
    const zend_op_array *op_array, const zend_op *opline, const zend_ssa_op *ssa_op) {
209
0
  switch (opline->opcode) {
210
0
    case ZEND_PRE_INC:
211
0
    case ZEND_PRE_DEC:
212
0
    case ZEND_PRE_INC_OBJ:
213
0
    case ZEND_PRE_DEC_OBJ:
214
0
    case ZEND_POST_INC:
215
0
    case ZEND_POST_DEC:
216
0
    case ZEND_POST_INC_OBJ:
217
0
    case ZEND_POST_DEC_OBJ:
218
0
    case ZEND_ASSIGN:
219
0
    case ZEND_ASSIGN_REF:
220
0
    case ZEND_ASSIGN_DIM:
221
0
    case ZEND_ASSIGN_OBJ:
222
0
    case ZEND_ASSIGN_OBJ_REF:
223
0
    case ZEND_ASSIGN_OP:
224
0
    case ZEND_ASSIGN_DIM_OP:
225
0
    case ZEND_ASSIGN_OBJ_OP:
226
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
227
0
    case ZEND_FETCH_DIM_W:
228
0
    case ZEND_FETCH_DIM_RW:
229
0
    case ZEND_FETCH_DIM_UNSET:
230
0
    case ZEND_FETCH_DIM_FUNC_ARG:
231
0
    case ZEND_FETCH_OBJ_W:
232
0
    case ZEND_FETCH_OBJ_RW:
233
0
    case ZEND_FETCH_OBJ_UNSET:
234
0
    case ZEND_FETCH_OBJ_FUNC_ARG:
235
0
    case ZEND_FETCH_LIST_W:
236
0
    case ZEND_UNSET_DIM:
237
0
    case ZEND_UNSET_OBJ:
238
0
    case ZEND_SEND_REF:
239
0
    case ZEND_SEND_VAR_EX:
240
0
    case ZEND_SEND_FUNC_ARG:
241
0
    case ZEND_SEND_UNPACK:
242
0
    case ZEND_SEND_ARRAY:
243
0
    case ZEND_SEND_USER:
244
0
    case ZEND_FE_RESET_RW:
245
0
      return false;
246
    /* Do not accept CONST */
247
0
    case ZEND_ROPE_ADD:
248
0
    case ZEND_ROPE_END:
249
0
    case ZEND_BIND_STATIC:
250
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
251
0
    case ZEND_BIND_GLOBAL:
252
0
    case ZEND_MAKE_REF:
253
0
    case ZEND_UNSET_CV:
254
0
    case ZEND_ISSET_ISEMPTY_CV:
255
0
      return false;
256
0
    case ZEND_INIT_ARRAY:
257
0
    case ZEND_ADD_ARRAY_ELEMENT:
258
0
      return !(opline->extended_value & ZEND_ARRAY_ELEMENT_REF);
259
0
    case ZEND_YIELD:
260
0
      return !(op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE);
261
0
    case ZEND_VERIFY_RETURN_TYPE:
262
      // TODO: This would require a non-local change ???
263
0
      return false;
264
0
    case ZEND_OP_DATA:
265
0
      return (opline - 1)->opcode != ZEND_ASSIGN_OBJ_REF &&
266
0
        (opline - 1)->opcode != ZEND_ASSIGN_STATIC_PROP_REF;
267
0
    default:
268
0
      if (ssa_op->op1_def != -1) {
269
0
        ZEND_UNREACHABLE();
270
0
        return false;
271
0
      }
272
0
  }
273
274
0
  return true;
275
0
}
276
277
static bool can_replace_op2(
278
0
    const zend_op_array *op_array, zend_op *opline, zend_ssa_op *ssa_op) {
279
0
  switch (opline->opcode) {
280
    /* Do not accept CONST */
281
0
    case ZEND_DECLARE_CLASS_DELAYED:
282
0
    case ZEND_BIND_LEXICAL:
283
0
    case ZEND_FE_FETCH_R:
284
0
    case ZEND_FE_FETCH_RW:
285
0
      return false;
286
0
  }
287
0
  return true;
288
0
}
289
290
static bool try_replace_op1(
291
0
    sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op, int var, zval *value) {
292
0
  if (ssa_op->op1_use == var && can_replace_op1(ctx->scdf.op_array, opline, ssa_op)) {
293
0
    zval zv;
294
0
    ZVAL_COPY(&zv, value);
295
0
    if (zend_optimizer_update_op1_const(ctx->scdf.op_array, opline, &zv)) {
296
0
      return true;
297
0
    }
298
0
    zval_ptr_dtor_nogc(&zv);
299
0
  }
300
0
  return false;
301
0
}
302
303
static bool try_replace_op2(
304
0
    sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op, int var, zval *value) {
305
0
  if (ssa_op->op2_use == var && can_replace_op2(ctx->scdf.op_array, opline, ssa_op)) {
306
0
    zval zv;
307
0
    ZVAL_COPY(&zv, value);
308
0
    if (zend_optimizer_update_op2_const(ctx->scdf.op_array, opline, &zv)) {
309
0
      return true;
310
0
    }
311
0
    zval_ptr_dtor_nogc(&zv);
312
0
  }
313
0
  return false;
314
0
}
315
316
0
static inline zend_result ct_eval_binary_op(zval *result, uint8_t binop, zval *op1, zval *op2) {
317
  /* TODO: We could implement support for evaluation of + on partial arrays. */
318
0
  if (IS_PARTIAL_ARRAY(op1) || IS_PARTIAL_ARRAY(op2)) {
319
0
    return FAILURE;
320
0
  }
321
322
0
  return zend_optimizer_eval_binary_op(result, binop, op1, op2);
323
0
}
324
325
0
static inline zend_result ct_eval_bool_cast(zval *result, zval *op) {
326
0
  if (IS_PARTIAL_ARRAY(op)) {
327
0
    if (zend_hash_num_elements(Z_ARRVAL_P(op)) == 0) {
328
      /* An empty partial array may be non-empty at runtime, we don't know whether the
329
       * result will be true or false. */
330
0
      return FAILURE;
331
0
    }
332
333
0
    ZVAL_TRUE(result);
334
0
    return SUCCESS;
335
0
  }
336
  /* NAN warns when casting */
337
0
  if (Z_TYPE_P(op) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op))) {
338
0
    return FAILURE;
339
0
  }
340
341
0
  ZVAL_BOOL(result, zend_is_true(op));
342
0
  return SUCCESS;
343
0
}
344
345
0
static inline zend_result zval_to_string_offset(zend_long *result, zval *op) {
346
0
  switch (Z_TYPE_P(op)) {
347
0
    case IS_LONG:
348
0
      *result = Z_LVAL_P(op);
349
0
      return SUCCESS;
350
0
    case IS_STRING:
351
0
      if (IS_LONG == is_numeric_string(
352
0
          Z_STRVAL_P(op), Z_STRLEN_P(op), result, NULL, 0)) {
353
0
        return SUCCESS;
354
0
      }
355
0
      return FAILURE;
356
0
    default:
357
0
      return FAILURE;
358
0
  }
359
0
}
360
361
0
static inline zend_result fetch_array_elem(zval **result, zval *op1, zval *op2) {
362
0
  switch (Z_TYPE_P(op2)) {
363
0
    case IS_NULL:
364
0
      return FAILURE;
365
0
    case IS_FALSE:
366
0
      *result = zend_hash_index_find(Z_ARR_P(op1), 0);
367
0
      return SUCCESS;
368
0
    case IS_TRUE:
369
0
      *result = zend_hash_index_find(Z_ARR_P(op1), 1);
370
0
      return SUCCESS;
371
0
    case IS_LONG:
372
0
      *result = zend_hash_index_find(Z_ARR_P(op1), Z_LVAL_P(op2));
373
0
      return SUCCESS;
374
0
    case IS_DOUBLE: {
375
0
      zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(op2));
376
0
      if (!zend_is_long_compatible(Z_DVAL_P(op2), lval)) {
377
0
        return FAILURE;
378
0
      }
379
0
      *result = zend_hash_index_find(Z_ARR_P(op1), lval);
380
0
      return SUCCESS;
381
0
    }
382
0
    case IS_STRING:
383
0
      *result = zend_symtable_find(Z_ARR_P(op1), Z_STR_P(op2));
384
0
      return SUCCESS;
385
0
    default:
386
0
      return FAILURE;
387
0
  }
388
0
}
389
390
0
static inline zend_result ct_eval_fetch_dim(zval *result, zval *op1, zval *op2, int support_strings) {
391
0
  if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
392
0
    zval *value;
393
0
    if (fetch_array_elem(&value, op1, op2) == SUCCESS && value && !IS_BOT(value)) {
394
0
      ZVAL_COPY(result, value);
395
0
      return SUCCESS;
396
0
    }
397
0
  } else if (support_strings && Z_TYPE_P(op1) == IS_STRING) {
398
0
    zend_long index;
399
0
    if (zval_to_string_offset(&index, op2) == FAILURE) {
400
0
      return FAILURE;
401
0
    }
402
0
    if (index >= 0 && index < Z_STRLEN_P(op1)) {
403
0
      ZVAL_CHAR(result, Z_STRVAL_P(op1)[index]);
404
0
      return SUCCESS;
405
0
    }
406
0
  }
407
0
  return FAILURE;
408
0
}
409
410
/* op1 may be NULL here to indicate an unset value */
411
0
static inline zend_result ct_eval_isset_isempty(zval *result, uint32_t extended_value, zval *op1) {
412
0
  zval zv;
413
0
  if (!(extended_value & ZEND_ISEMPTY)) {
414
0
    ZVAL_BOOL(result, op1 && Z_TYPE_P(op1) != IS_NULL);
415
0
    return SUCCESS;
416
0
  } else if (!op1) {
417
0
    ZVAL_TRUE(result);
418
0
    return SUCCESS;
419
0
  } else if (ct_eval_bool_cast(&zv, op1) == SUCCESS) {
420
0
    ZVAL_BOOL(result, Z_TYPE(zv) == IS_FALSE);
421
0
    return SUCCESS;
422
0
  } else {
423
0
    return FAILURE;
424
0
  }
425
0
}
426
427
0
static inline zend_result ct_eval_isset_dim(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
428
0
  if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
429
0
    zval *value;
430
0
    if (fetch_array_elem(&value, op1, op2) == FAILURE) {
431
0
      return FAILURE;
432
0
    }
433
0
    if (IS_PARTIAL_ARRAY(op1) && (!value || IS_BOT(value))) {
434
0
      return FAILURE;
435
0
    }
436
0
    return ct_eval_isset_isempty(result, extended_value, value);
437
0
  } else if (Z_TYPE_P(op1) == IS_STRING) {
438
    // TODO
439
0
    return FAILURE;
440
0
  } else {
441
0
    ZVAL_BOOL(result, (extended_value & ZEND_ISEMPTY));
442
0
    return SUCCESS;
443
0
  }
444
0
}
445
446
0
static inline zend_result ct_eval_del_array_elem(zval *result, const zval *key) {
447
0
  ZEND_ASSERT(IS_PARTIAL_ARRAY(result));
448
449
0
  switch (Z_TYPE_P(key)) {
450
0
    case IS_NULL:
451
0
      zend_hash_del(Z_ARR_P(result), ZSTR_EMPTY_ALLOC());
452
0
      break;
453
0
    case IS_FALSE:
454
0
      zend_hash_index_del(Z_ARR_P(result), 0);
455
0
      break;
456
0
    case IS_TRUE:
457
0
      zend_hash_index_del(Z_ARR_P(result), 1);
458
0
      break;
459
0
    case IS_LONG:
460
0
      zend_hash_index_del(Z_ARR_P(result), Z_LVAL_P(key));
461
0
      break;
462
0
    case IS_DOUBLE: {
463
0
      zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
464
0
      if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
465
0
        return FAILURE;
466
0
      }
467
0
      zend_hash_index_del(Z_ARR_P(result), lval);
468
0
      break;
469
0
    }
470
0
    case IS_STRING:
471
0
      zend_symtable_del(Z_ARR_P(result), Z_STR_P(key));
472
0
      break;
473
0
    default:
474
0
      return FAILURE;
475
0
  }
476
477
0
  return SUCCESS;
478
0
}
479
480
0
static inline zend_result ct_eval_add_array_elem(zval *result, zval *value, const zval *key) {
481
0
  if (!key) {
482
0
    SEPARATE_ARRAY(result);
483
0
    if ((value = zend_hash_next_index_insert(Z_ARR_P(result), value))) {
484
0
      Z_TRY_ADDREF_P(value);
485
0
      return SUCCESS;
486
0
    }
487
0
    return FAILURE;
488
0
  }
489
490
0
  switch (Z_TYPE_P(key)) {
491
0
    case IS_NULL:
492
0
      SEPARATE_ARRAY(result);
493
0
      value = zend_hash_update(Z_ARR_P(result), ZSTR_EMPTY_ALLOC(), value);
494
0
      break;
495
0
    case IS_FALSE:
496
0
      SEPARATE_ARRAY(result);
497
0
      value = zend_hash_index_update(Z_ARR_P(result), 0, value);
498
0
      break;
499
0
    case IS_TRUE:
500
0
      SEPARATE_ARRAY(result);
501
0
      value = zend_hash_index_update(Z_ARR_P(result), 1, value);
502
0
      break;
503
0
    case IS_LONG:
504
0
      SEPARATE_ARRAY(result);
505
0
      value = zend_hash_index_update(Z_ARR_P(result), Z_LVAL_P(key), value);
506
0
      break;
507
0
    case IS_DOUBLE: {
508
0
      zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
509
0
      if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
510
0
        return FAILURE;
511
0
      }
512
0
      SEPARATE_ARRAY(result);
513
0
      value = zend_hash_index_update(
514
0
        Z_ARR_P(result), lval, value);
515
0
      break;
516
0
    }
517
0
    case IS_STRING:
518
0
      SEPARATE_ARRAY(result);
519
0
      value = zend_symtable_update(Z_ARR_P(result), Z_STR_P(key), value);
520
0
      break;
521
0
    default:
522
0
      return FAILURE;
523
0
  }
524
525
0
  Z_TRY_ADDREF_P(value);
526
0
  return SUCCESS;
527
0
}
528
529
0
static inline zend_result ct_eval_add_array_unpack(zval *result, zval *array) {
530
0
  zend_string *key;
531
0
  zval *value;
532
0
  if (Z_TYPE_P(array) != IS_ARRAY) {
533
0
    return FAILURE;
534
0
  }
535
536
0
  SEPARATE_ARRAY(result);
537
0
  ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(array), key, value) {
538
0
    if (key) {
539
0
      value = zend_hash_update(Z_ARR_P(result), key, value);
540
0
    } else {
541
0
      value = zend_hash_next_index_insert(Z_ARR_P(result), value);
542
0
    }
543
0
    if (!value) {
544
0
      return FAILURE;
545
0
    }
546
0
    Z_TRY_ADDREF_P(value);
547
0
  } ZEND_HASH_FOREACH_END();
548
0
  return SUCCESS;
549
0
}
550
551
0
static inline zend_result ct_eval_assign_dim(zval *result, zval *value, const zval *key) {
552
0
  switch (Z_TYPE_P(result)) {
553
0
    case IS_NULL:
554
0
    case IS_FALSE:
555
0
      array_init(result);
556
0
      ZEND_FALLTHROUGH;
557
0
    case IS_ARRAY:
558
0
    case PARTIAL_ARRAY:
559
0
      return ct_eval_add_array_elem(result, value, key);
560
0
    case IS_STRING:
561
      // TODO Before enabling this case, make sure ARRAY_DIM result op is correct
562
#if 0
563
      zend_long index;
564
      zend_string *new_str, *value_str;
565
      if (!key || Z_TYPE_P(value) == IS_ARRAY
566
          || zval_to_string_offset(&index, key) == FAILURE || index < 0) {
567
        return FAILURE;
568
      }
569
570
      if (index >= Z_STRLEN_P(result)) {
571
        new_str = zend_string_alloc(index + 1, 0);
572
        memcpy(ZSTR_VAL(new_str), Z_STRVAL_P(result), Z_STRLEN_P(result));
573
        memset(ZSTR_VAL(new_str) + Z_STRLEN_P(result), ' ', index - Z_STRLEN_P(result));
574
        ZSTR_VAL(new_str)[index + 1] = 0;
575
      } else {
576
        new_str = zend_string_init(Z_STRVAL_P(result), Z_STRLEN_P(result), 0);
577
      }
578
579
      value_str = zval_get_string(value);
580
      ZVAL_STR(result, new_str);
581
      Z_STRVAL_P(result)[index] = ZSTR_VAL(value_str)[0];
582
      zend_string_release_ex(value_str, 0);
583
#endif
584
0
      return FAILURE;
585
0
    default:
586
0
      return FAILURE;
587
0
  }
588
0
}
589
590
0
static inline zend_result fetch_obj_prop(zval **result, zval *op1, zval *op2) {
591
0
  switch (Z_TYPE_P(op2)) {
592
0
    case IS_STRING:
593
0
      *result = zend_symtable_find(Z_ARR_P(op1), Z_STR_P(op2));
594
0
      return SUCCESS;
595
0
    default:
596
0
      return FAILURE;
597
0
  }
598
0
}
599
600
0
static inline zend_result ct_eval_fetch_obj(zval *result, zval *op1, zval *op2) {
601
0
  if (IS_PARTIAL_OBJECT(op1)) {
602
0
    zval *value;
603
0
    if (fetch_obj_prop(&value, op1, op2) == SUCCESS && value && !IS_BOT(value)) {
604
0
      ZVAL_COPY(result, value);
605
0
      return SUCCESS;
606
0
    }
607
0
  }
608
0
  return FAILURE;
609
0
}
610
611
0
static inline zend_result ct_eval_isset_obj(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
612
0
  if (IS_PARTIAL_OBJECT(op1)) {
613
0
    zval *value;
614
0
    if (fetch_obj_prop(&value, op1, op2) == FAILURE) {
615
0
      return FAILURE;
616
0
    }
617
0
    if (!value || IS_BOT(value)) {
618
0
      return FAILURE;
619
0
    }
620
0
    return ct_eval_isset_isempty(result, extended_value, value);
621
0
  } else {
622
0
    ZVAL_BOOL(result, (extended_value & ZEND_ISEMPTY));
623
0
    return SUCCESS;
624
0
  }
625
0
}
626
627
0
static inline zend_result ct_eval_del_obj_prop(zval *result, const zval *key) {
628
0
  ZEND_ASSERT(IS_PARTIAL_OBJECT(result));
629
630
0
  switch (Z_TYPE_P(key)) {
631
0
    case IS_STRING:
632
0
      zend_symtable_del(Z_ARR_P(result), Z_STR_P(key));
633
0
      break;
634
0
    default:
635
0
      return FAILURE;
636
0
  }
637
638
0
  return SUCCESS;
639
0
}
640
641
0
static inline zend_result ct_eval_add_obj_prop(zval *result, zval *value, const zval *key) {
642
0
  switch (Z_TYPE_P(key)) {
643
0
    case IS_STRING:
644
0
      value = zend_symtable_update(Z_ARR_P(result), Z_STR_P(key), value);
645
0
      break;
646
0
    default:
647
0
      return FAILURE;
648
0
  }
649
650
0
  Z_TRY_ADDREF_P(value);
651
0
  return SUCCESS;
652
0
}
653
654
0
static inline zend_result ct_eval_assign_obj(zval *result, zval *value, const zval *key) {
655
0
  switch (Z_TYPE_P(result)) {
656
0
    case IS_NULL:
657
0
    case IS_FALSE:
658
0
      empty_partial_object(result);
659
0
      ZEND_FALLTHROUGH;
660
0
    case PARTIAL_OBJECT:
661
0
      return ct_eval_add_obj_prop(result, value, key);
662
0
    default:
663
0
      return FAILURE;
664
0
  }
665
0
}
666
667
0
static inline zend_result ct_eval_incdec(zval *result, uint8_t opcode, zval *op1) {
668
  /* As of PHP 8.3 with the warning/deprecation notices any type other than int/double/null will emit a diagnostic
669
  if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
670
    return FAILURE;
671
  }
672
  */
673
0
  if (Z_TYPE_P(op1) != IS_LONG && Z_TYPE_P(op1) != IS_DOUBLE && Z_TYPE_P(op1) != IS_NULL) {
674
0
    return FAILURE;
675
0
  }
676
677
0
  ZVAL_COPY(result, op1);
678
0
  if (opcode == ZEND_PRE_INC
679
0
      || opcode == ZEND_POST_INC
680
0
      || opcode == ZEND_PRE_INC_OBJ
681
0
      || opcode == ZEND_POST_INC_OBJ) {
682
0
    increment_function(result);
683
0
  } else {
684
    /* Decrement on null emits a deprecation notice */
685
0
    if (Z_TYPE_P(op1) == IS_NULL) {
686
0
      zval_ptr_dtor(result);
687
0
      return FAILURE;
688
0
    }
689
0
    decrement_function(result);
690
0
  }
691
0
  return SUCCESS;
692
0
}
693
694
0
static inline void ct_eval_type_check(zval *result, uint32_t type_mask, zval *op1) {
695
0
  uint32_t type = Z_TYPE_P(op1);
696
0
  if (type == PARTIAL_ARRAY) {
697
0
    type = IS_ARRAY;
698
0
  } else if (type == PARTIAL_OBJECT) {
699
0
    type = IS_OBJECT;
700
0
  }
701
0
  ZVAL_BOOL(result, (type_mask >> type) & 1);
702
0
}
703
704
0
static inline zend_result ct_eval_in_array(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
705
0
  HashTable *ht;
706
0
  bool res;
707
708
0
  if (Z_TYPE_P(op2) != IS_ARRAY) {
709
0
    return FAILURE;
710
0
  }
711
0
  ht = Z_ARRVAL_P(op2);
712
0
  if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
713
0
    res = zend_hash_exists(ht, Z_STR_P(op1));
714
0
  } else if (extended_value) {
715
0
    if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
716
0
      res = zend_hash_index_exists(ht, Z_LVAL_P(op1));
717
0
    } else {
718
0
      res = false;
719
0
    }
720
0
  } else if (Z_TYPE_P(op1) <= IS_FALSE) {
721
0
    res = zend_hash_exists(ht, ZSTR_EMPTY_ALLOC());
722
0
  } else {
723
0
    zend_string *key;
724
0
    zval key_tmp;
725
726
0
    res = false;
727
0
    ZEND_HASH_MAP_FOREACH_STR_KEY(ht, key) {
728
0
      ZVAL_STR(&key_tmp, key);
729
0
      if (zend_compare(op1, &key_tmp) == 0) {
730
0
        res = true;
731
0
        break;
732
0
      }
733
0
    } ZEND_HASH_FOREACH_END();
734
0
  }
735
0
  ZVAL_BOOL(result, res);
736
0
  return SUCCESS;
737
0
}
738
739
0
static inline zend_result ct_eval_array_key_exists(zval *result, zval *op1, zval *op2) {
740
0
  zval *value;
741
742
0
  if (Z_TYPE_P(op2) != IS_ARRAY && !IS_PARTIAL_ARRAY(op2)) {
743
0
    return FAILURE;
744
0
  }
745
0
  if (Z_TYPE_P(op1) != IS_STRING && Z_TYPE_P(op1) != IS_LONG && Z_TYPE_P(op1) != IS_NULL) {
746
0
    return FAILURE;
747
0
  }
748
0
  if (fetch_array_elem(&value, op2, op1) == FAILURE) {
749
0
    return FAILURE;
750
0
  }
751
0
  if (IS_PARTIAL_ARRAY(op2) && (!value || IS_BOT(value))) {
752
0
    return FAILURE;
753
0
  }
754
755
0
  ZVAL_BOOL(result, value != NULL);
756
0
  return SUCCESS;
757
0
}
758
759
0
static bool can_ct_eval_func_call(zend_function *func, zend_string *name, uint32_t num_args, zval **args) {
760
  /* Precondition: func->type == ZEND_INTERNAL_FUNCTION, this is a global function */
761
  /* Functions setting ZEND_ACC_COMPILE_TIME_EVAL (@compile-time-eval) must always produce the same result for the same arguments,
762
   * and have no dependence on global state (such as locales). It is okay if they throw
763
   * or warn on invalid arguments, as we detect this and will discard the evaluation result. */
764
0
  if (func->common.fn_flags & ZEND_ACC_COMPILE_TIME_EVAL) {
765
    /* This has @compile-time-eval in stub info and uses a macro such as ZEND_SUPPORTS_COMPILE_TIME_EVAL_FE */
766
0
    return true;
767
0
  }
768
0
#ifndef ZEND_WIN32
769
  /* On Windows this function may be code page dependent. */
770
0
  if (zend_string_equals_literal(name, "dirname")) {
771
0
    return true;
772
0
  }
773
0
#endif
774
775
0
  if (num_args == 2) {
776
0
    if (zend_string_equals_literal(name, "str_repeat")) {
777
      /* Avoid creating overly large strings at compile-time. */
778
0
      bool overflow;
779
0
      return Z_TYPE_P(args[0]) == IS_STRING
780
0
        && Z_TYPE_P(args[1]) == IS_LONG
781
0
        && zend_safe_address(Z_STRLEN_P(args[0]), Z_LVAL_P(args[1]), 0, &overflow) < 64 * 1024
782
0
        && !overflow;
783
0
    }
784
0
    return false;
785
0
  }
786
787
0
  return false;
788
0
}
789
790
/* The functions chosen here are simple to implement and either likely to affect a branch,
791
 * or just happened to be commonly used with constant operands in WP (need to test other
792
 * applications as well, of course). */
793
static inline zend_result ct_eval_func_call_ex(
794
0
    zend_op_array *op_array, zval *result, zend_function *func, uint32_t num_args, zval **args) {
795
0
  uint32_t i;
796
0
  zend_string *name = func->common.function_name;
797
0
  if (num_args == 1 && Z_TYPE_P(args[0]) == IS_STRING &&
798
0
      zend_optimizer_eval_special_func_call(result, name, Z_STR_P(args[0])) == SUCCESS) {
799
0
    return SUCCESS;
800
0
  }
801
802
0
  if (!can_ct_eval_func_call(func, name, num_args, args)) {
803
0
    return FAILURE;
804
0
  }
805
806
0
  zend_execute_data *prev_execute_data = EG(current_execute_data);
807
0
  zend_execute_data *execute_data, dummy_frame;
808
0
  zend_op dummy_opline;
809
810
  /* Add a dummy frame to get the correct strict_types behavior. */
811
0
  memset(&dummy_frame, 0, sizeof(zend_execute_data));
812
0
  memset(&dummy_opline, 0, sizeof(zend_op));
813
0
  dummy_frame.func = (zend_function *) op_array;
814
0
  dummy_frame.opline = &dummy_opline;
815
0
  dummy_opline.opcode = ZEND_DO_FCALL;
816
817
0
  execute_data = safe_emalloc(num_args, sizeof(zval), ZEND_CALL_FRAME_SLOT * sizeof(zval));
818
0
  memset(execute_data, 0, sizeof(zend_execute_data));
819
0
  execute_data->prev_execute_data = &dummy_frame;
820
0
  EG(current_execute_data) = execute_data;
821
822
  /* Enable suppression and counting of warnings. */
823
0
  ZEND_ASSERT(EG(capture_warnings_during_sccp) == 0);
824
0
  EG(capture_warnings_during_sccp) = 1;
825
826
0
  EX(func) = func;
827
0
  EX_NUM_ARGS() = num_args;
828
0
  for (i = 0; i < num_args; i++) {
829
0
    ZVAL_COPY(EX_VAR_NUM(i), args[i]);
830
0
  }
831
0
  ZVAL_NULL(result);
832
0
  func->internal_function.handler(execute_data, result);
833
0
  for (i = 0; i < num_args; i++) {
834
0
    zval_ptr_dtor_nogc(EX_VAR_NUM(i));
835
0
  }
836
837
0
  zend_result retval = SUCCESS;
838
0
  if (EG(exception)) {
839
0
    zval_ptr_dtor(result);
840
0
    zend_clear_exception();
841
0
    retval = FAILURE;
842
0
  } else if (EG(capture_warnings_during_sccp) > 1) {
843
0
    zval_ptr_dtor(result);
844
0
    retval = FAILURE;
845
0
  }
846
0
  EG(capture_warnings_during_sccp) = 0;
847
848
0
  efree(execute_data);
849
0
  EG(current_execute_data) = prev_execute_data;
850
0
  return retval;
851
0
}
852
853
static inline zend_result ct_eval_func_call(
854
0
    zend_op_array *op_array, zval *result, zend_string *name, uint32_t num_args, zval **args) {
855
0
  zend_function *func = zend_hash_find_ptr(CG(function_table), name);
856
0
  if (!func || func->type != ZEND_INTERNAL_FUNCTION) {
857
0
    return FAILURE;
858
0
  }
859
0
  return ct_eval_func_call_ex(op_array, result, func, num_args, args);
860
0
}
861
862
3
#define SET_RESULT(op, zv) do { \
863
3
  if (ssa_op->op##_def >= 0) { \
864
0
    set_value(scdf, ctx, ssa_op->op##_def, zv); \
865
0
  } \
866
3
} while (0)
867
3
#define SET_RESULT_BOT(op) SET_RESULT(op, &ctx->bot)
868
#define SET_RESULT_TOP(op) SET_RESULT(op, &ctx->top)
869
870
0
#define SKIP_IF_TOP(op) if (IS_TOP(op)) return;
871
872
1
static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_op) {
873
1
  sccp_ctx *ctx = (sccp_ctx *) scdf;
874
1
  zval *op1, *op2, zv; /* zv is a temporary to hold result values */
875
876
1
  op1 = get_op1_value(ctx, opline, ssa_op);
877
1
  op2 = get_op2_value(ctx, opline, ssa_op);
878
879
1
  switch (opline->opcode) {
880
0
    case ZEND_ASSIGN:
881
      /* The value of op1 is irrelevant here, because we are overwriting it
882
       * -- unless it can be a reference, in which case we propagate a BOT.
883
       * The result is also BOT in this case, because it might be a typed reference. */
884
0
      if (IS_BOT(op1) && (ctx->scdf.ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF)) {
885
0
        SET_RESULT_BOT(op1);
886
0
        SET_RESULT_BOT(result);
887
0
      } else {
888
0
        SET_RESULT(op1, op2);
889
0
        SET_RESULT(result, op2);
890
0
      }
891
0
      return;
892
0
    case ZEND_ASSIGN_DIM:
893
0
    {
894
0
      zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
895
896
      /* If $a in $a[$b]=$c is UNDEF, treat it like NULL. There is no warning. */
897
0
      if ((ctx->scdf.ssa->var_info[ssa_op->op1_use].type & MAY_BE_ANY) == 0) {
898
0
        op1 = &EG(uninitialized_zval);
899
0
      }
900
901
0
      if (IS_BOT(op1)) {
902
0
        SET_RESULT_BOT(result);
903
0
        SET_RESULT_BOT(op1);
904
0
        return;
905
0
      }
906
907
0
      SKIP_IF_TOP(op1);
908
0
      SKIP_IF_TOP(data);
909
0
      if (op2) {
910
0
        SKIP_IF_TOP(op2);
911
0
      }
912
913
0
      if (op2 && IS_BOT(op2)) {
914
        /* Update of unknown index */
915
0
        SET_RESULT_BOT(result);
916
0
        if (ssa_op->op1_def >= 0) {
917
0
          empty_partial_array(&zv);
918
0
          SET_RESULT(op1, &zv);
919
0
          zval_ptr_dtor_nogc(&zv);
920
0
        } else {
921
0
          SET_RESULT_BOT(op1);
922
0
        }
923
0
        return;
924
0
      }
925
926
0
      if (IS_BOT(data)) {
927
928
0
        SET_RESULT_BOT(result);
929
0
        if ((IS_PARTIAL_ARRAY(op1)
930
0
            || Z_TYPE_P(op1) == IS_NULL
931
0
            || Z_TYPE_P(op1) == IS_FALSE
932
0
            || Z_TYPE_P(op1) == IS_ARRAY)
933
0
          && ssa_op->op1_def >= 0) {
934
935
0
          if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) {
936
0
            empty_partial_array(&zv);
937
0
          } else {
938
0
            dup_partial_array(&zv, op1);
939
0
          }
940
941
0
          if (!op2) {
942
            /* We can't add NEXT element into partial array (skip it) */
943
0
            SET_RESULT(op1, &zv);
944
0
          } else if (ct_eval_del_array_elem(&zv, op2) == SUCCESS) {
945
0
            SET_RESULT(op1, &zv);
946
0
          } else {
947
0
            SET_RESULT_BOT(op1);
948
0
          }
949
950
0
          zval_ptr_dtor_nogc(&zv);
951
0
        } else {
952
0
          SET_RESULT_BOT(op1);
953
0
        }
954
955
0
      } else {
956
957
0
        if (IS_PARTIAL_ARRAY(op1)) {
958
0
          dup_partial_array(&zv, op1);
959
0
        } else {
960
0
          ZVAL_COPY(&zv, op1);
961
0
        }
962
963
0
        if (!op2 && IS_PARTIAL_ARRAY(&zv)) {
964
          /* We can't add NEXT element into partial array (skip it) */
965
0
          SET_RESULT(result, data);
966
0
          SET_RESULT(op1, &zv);
967
0
        } else if (ct_eval_assign_dim(&zv, data, op2) == SUCCESS) {
968
          /* Mark array containing partial array as partial */
969
0
          if (IS_PARTIAL_ARRAY(data) || IS_PARTIAL_OBJECT(data)) {
970
0
            MAKE_PARTIAL_ARRAY(&zv);
971
0
          }
972
0
          SET_RESULT(result, data);
973
0
          SET_RESULT(op1, &zv);
974
0
        } else {
975
0
          SET_RESULT_BOT(result);
976
0
          SET_RESULT_BOT(op1);
977
0
        }
978
979
0
        zval_ptr_dtor_nogc(&zv);
980
0
      }
981
0
      return;
982
0
    }
983
984
0
    case ZEND_ASSIGN_OBJ:
985
0
      if (ssa_op->op1_def >= 0
986
0
          && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
987
0
        zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
988
0
        zend_ssa_var_info *var_info = &ctx->scdf.ssa->var_info[ssa_op->op1_use];
989
990
        /* Don't try to propagate assignments to (potentially) typed properties. We would
991
         * need to deal with errors and type conversions first. */
992
        // TODO: Distinguish dynamic and declared property assignments here?
993
0
        if (!var_info->ce || (var_info->ce->ce_flags & ZEND_ACC_HAS_TYPE_HINTS) ||
994
0
            !(var_info->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
995
0
          SET_RESULT_BOT(result);
996
0
          SET_RESULT_BOT(op1);
997
0
          return;
998
0
        }
999
1000
0
        if (IS_BOT(op1)) {
1001
0
          SET_RESULT_BOT(result);
1002
0
          SET_RESULT_BOT(op1);
1003
0
          return;
1004
0
        }
1005
1006
0
        SKIP_IF_TOP(op1);
1007
0
        SKIP_IF_TOP(data);
1008
0
        SKIP_IF_TOP(op2);
1009
1010
0
        if (IS_BOT(op2)) {
1011
          /* Update of unknown property */
1012
0
          SET_RESULT_BOT(result);
1013
0
          empty_partial_object(&zv);
1014
0
          SET_RESULT(op1, &zv);
1015
0
          zval_ptr_dtor_nogc(&zv);
1016
0
          return;
1017
0
        }
1018
1019
0
        if (IS_BOT(data)) {
1020
0
          SET_RESULT_BOT(result);
1021
0
          if (IS_PARTIAL_OBJECT(op1)
1022
0
              || Z_TYPE_P(op1) == IS_NULL
1023
0
              || Z_TYPE_P(op1) == IS_FALSE) {
1024
1025
0
            if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) {
1026
0
              empty_partial_object(&zv);
1027
0
            } else {
1028
0
              dup_partial_object(&zv, op1);
1029
0
            }
1030
1031
0
            if (ct_eval_del_obj_prop(&zv, op2) == SUCCESS) {
1032
0
              SET_RESULT(op1, &zv);
1033
0
            } else {
1034
0
              SET_RESULT_BOT(op1);
1035
0
            }
1036
0
            zval_ptr_dtor_nogc(&zv);
1037
0
          } else {
1038
0
            SET_RESULT_BOT(op1);
1039
0
          }
1040
1041
0
        } else {
1042
1043
0
          if (IS_PARTIAL_OBJECT(op1)) {
1044
0
            dup_partial_object(&zv, op1);
1045
0
          } else {
1046
0
            ZVAL_COPY(&zv, op1);
1047
0
          }
1048
1049
0
          if (ct_eval_assign_obj(&zv, data, op2) == SUCCESS) {
1050
0
            SET_RESULT(result, data);
1051
0
            SET_RESULT(op1, &zv);
1052
0
          } else {
1053
0
            SET_RESULT_BOT(result);
1054
0
            SET_RESULT_BOT(op1);
1055
0
          }
1056
1057
0
          zval_ptr_dtor_nogc(&zv);
1058
0
        }
1059
0
      } else {
1060
0
        SET_RESULT_BOT(result);
1061
0
        SET_RESULT_BOT(op1);
1062
0
      }
1063
0
      return;
1064
1065
0
    case ZEND_SEND_VAL:
1066
0
    case ZEND_SEND_VAR:
1067
0
    {
1068
      /* If the value of a SEND for an ICALL changes, we need to reconsider the
1069
       * ICALL result value. Otherwise we can ignore the opcode. */
1070
0
      zend_call_info *call;
1071
0
      if (!ctx->call_map) {
1072
0
        return;
1073
0
      }
1074
1075
0
      call = ctx->call_map[opline - ctx->scdf.op_array->opcodes];
1076
0
      if (IS_TOP(op1) || !call || !call->caller_call_opline
1077
0
          || call->caller_call_opline->opcode != ZEND_DO_ICALL) {
1078
0
        return;
1079
0
      }
1080
1081
0
      opline = call->caller_call_opline;
1082
0
      ssa_op = &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes];
1083
0
      break;
1084
0
    }
1085
0
    case ZEND_INIT_ARRAY:
1086
0
    case ZEND_ADD_ARRAY_ELEMENT:
1087
0
    {
1088
0
      zval *result = NULL;
1089
1090
0
      if (opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
1091
0
        result = &ctx->values[ssa_op->result_use];
1092
0
        if (IS_BOT(result)) {
1093
0
          SET_RESULT_BOT(result);
1094
0
          SET_RESULT_BOT(op1);
1095
0
          return;
1096
0
        }
1097
0
        SKIP_IF_TOP(result);
1098
0
      }
1099
1100
0
      if (op1) {
1101
0
        SKIP_IF_TOP(op1);
1102
0
      }
1103
1104
0
      if (op2) {
1105
0
        SKIP_IF_TOP(op2);
1106
0
        if (Z_TYPE_P(op2) == IS_NULL) {
1107
          /* Emits deprecation at run-time. */
1108
0
          SET_RESULT_BOT(result);
1109
0
          return;
1110
0
        }
1111
0
      }
1112
1113
      /* We want to avoid keeping around intermediate arrays for each SSA variable in the
1114
       * ADD_ARRAY_ELEMENT chain. We do this by only keeping the array on the last opcode
1115
       * and use a NULL value everywhere else. */
1116
0
      if (result && Z_TYPE_P(result) == IS_NULL) {
1117
0
        SET_RESULT_BOT(result);
1118
0
        return;
1119
0
      }
1120
1121
0
      if (op2 && IS_BOT(op2)) {
1122
        /* Update of unknown index */
1123
0
        SET_RESULT_BOT(op1);
1124
0
        if (ssa_op->result_def >= 0) {
1125
0
          empty_partial_array(&zv);
1126
0
          SET_RESULT(result, &zv);
1127
0
          zval_ptr_dtor_nogc(&zv);
1128
0
        } else {
1129
0
          SET_RESULT_BOT(result);
1130
0
        }
1131
0
        return;
1132
0
      }
1133
1134
0
      if ((op1 && IS_BOT(op1))
1135
0
          || (opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
1136
1137
0
        SET_RESULT_BOT(op1);
1138
0
        if (ssa_op->result_def >= 0) {
1139
0
          if (!result) {
1140
0
            empty_partial_array(&zv);
1141
0
          } else {
1142
0
            MAKE_PARTIAL_ARRAY(result);
1143
0
            ZVAL_COPY_VALUE(&zv, result);
1144
0
            ZVAL_NULL(result);
1145
0
          }
1146
0
          if (!op2) {
1147
            /* We can't add NEXT element into partial array (skip it) */
1148
0
            SET_RESULT(result, &zv);
1149
0
          } else if (ct_eval_del_array_elem(&zv, op2) == SUCCESS) {
1150
0
            SET_RESULT(result, &zv);
1151
0
          } else {
1152
0
            SET_RESULT_BOT(result);
1153
0
          }
1154
0
          zval_ptr_dtor_nogc(&zv);
1155
0
        } else {
1156
          /* If any operand is BOT, mark the result as BOT right away.
1157
           * Exceptions to this rule are handled above. */
1158
0
          SET_RESULT_BOT(result);
1159
0
        }
1160
1161
0
      } else {
1162
0
        if (result) {
1163
0
          ZVAL_COPY_VALUE(&zv, result);
1164
0
          ZVAL_NULL(result);
1165
0
        } else {
1166
0
          array_init(&zv);
1167
0
        }
1168
1169
0
        if (op1) {
1170
0
          if (!op2 && IS_PARTIAL_ARRAY(&zv)) {
1171
            /* We can't add NEXT element into partial array (skip it) */
1172
0
            SET_RESULT(result, &zv);
1173
0
          } else if (ct_eval_add_array_elem(&zv, op1, op2) == SUCCESS) {
1174
0
            if (IS_PARTIAL_ARRAY(op1) || IS_PARTIAL_OBJECT(op1)) {
1175
0
              MAKE_PARTIAL_ARRAY(&zv);
1176
0
            }
1177
0
            SET_RESULT(result, &zv);
1178
0
          } else {
1179
0
            SET_RESULT_BOT(result);
1180
0
          }
1181
0
        } else {
1182
0
          SET_RESULT(result, &zv);
1183
0
        }
1184
1185
0
        zval_ptr_dtor_nogc(&zv);
1186
0
      }
1187
0
      return;
1188
0
    }
1189
0
    case ZEND_ADD_ARRAY_UNPACK: {
1190
0
      zval *result = &ctx->values[ssa_op->result_use];
1191
0
      if (IS_BOT(result) || IS_BOT(op1)) {
1192
0
        SET_RESULT_BOT(result);
1193
0
        return;
1194
0
      }
1195
0
      SKIP_IF_TOP(result);
1196
0
      SKIP_IF_TOP(op1);
1197
1198
      /* See comment for ADD_ARRAY_ELEMENT. */
1199
0
      if (Z_TYPE_P(result) == IS_NULL) {
1200
0
        SET_RESULT_BOT(result);
1201
0
        return;
1202
0
      }
1203
0
      ZVAL_COPY_VALUE(&zv, result);
1204
0
      ZVAL_NULL(result);
1205
1206
0
      if (ct_eval_add_array_unpack(&zv, op1) == SUCCESS) {
1207
0
        SET_RESULT(result, &zv);
1208
0
      } else {
1209
0
        SET_RESULT_BOT(result);
1210
0
      }
1211
0
      zval_ptr_dtor_nogc(&zv);
1212
0
      return;
1213
0
    }
1214
0
    case ZEND_NEW:
1215
0
      if (ssa_op->result_def >= 0
1216
0
          && ctx->scdf.ssa->vars[ssa_op->result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
1217
0
        empty_partial_object(&zv);
1218
0
        SET_RESULT(result, &zv);
1219
0
        zval_ptr_dtor_nogc(&zv);
1220
0
      } else {
1221
0
        SET_RESULT_BOT(result);
1222
0
      }
1223
0
      return;
1224
0
    case ZEND_ASSIGN_STATIC_PROP_REF:
1225
0
    case ZEND_ASSIGN_OBJ_REF:
1226
      /* Handled here because we also need to BOT the OP_DATA operand, while the generic
1227
       * code below will not do so. */
1228
0
      SET_RESULT_BOT(result);
1229
0
      SET_RESULT_BOT(op1);
1230
0
      SET_RESULT_BOT(op2);
1231
0
      opline++;
1232
0
      ssa_op++;
1233
0
      SET_RESULT_BOT(op1);
1234
0
      break;
1235
1
  }
1236
1237
1
  if ((op1 && IS_BOT(op1)) || (op2 && IS_BOT(op2))) {
1238
    /* If any operand is BOT, mark the result as BOT right away.
1239
     * Exceptions to this rule are handled above. */
1240
0
    SET_RESULT_BOT(result);
1241
0
    SET_RESULT_BOT(op1);
1242
0
    SET_RESULT_BOT(op2);
1243
0
    return;
1244
0
  }
1245
1246
1
  switch (opline->opcode) {
1247
0
    case ZEND_ADD:
1248
0
    case ZEND_SUB:
1249
0
    case ZEND_MUL:
1250
0
    case ZEND_DIV:
1251
0
    case ZEND_MOD:
1252
0
    case ZEND_POW:
1253
0
    case ZEND_SL:
1254
0
    case ZEND_SR:
1255
0
    case ZEND_CONCAT:
1256
0
    case ZEND_FAST_CONCAT:
1257
0
    case ZEND_IS_EQUAL:
1258
0
    case ZEND_IS_NOT_EQUAL:
1259
0
    case ZEND_IS_SMALLER:
1260
0
    case ZEND_IS_SMALLER_OR_EQUAL:
1261
0
    case ZEND_IS_IDENTICAL:
1262
0
    case ZEND_IS_NOT_IDENTICAL:
1263
0
    case ZEND_BW_OR:
1264
0
    case ZEND_BW_AND:
1265
0
    case ZEND_BW_XOR:
1266
0
    case ZEND_BOOL_XOR:
1267
0
    case ZEND_CASE:
1268
0
    case ZEND_CASE_STRICT:
1269
0
      SKIP_IF_TOP(op1);
1270
0
      SKIP_IF_TOP(op2);
1271
1272
0
      if (ct_eval_binary_op(&zv, opline->opcode, op1, op2) == SUCCESS) {
1273
0
        SET_RESULT(result, &zv);
1274
0
        zval_ptr_dtor_nogc(&zv);
1275
0
        break;
1276
0
      }
1277
0
      SET_RESULT_BOT(result);
1278
0
      break;
1279
0
    case ZEND_ASSIGN_OP:
1280
0
    case ZEND_ASSIGN_DIM_OP:
1281
0
    case ZEND_ASSIGN_OBJ_OP:
1282
0
    case ZEND_ASSIGN_STATIC_PROP_OP:
1283
0
      if (op1) {
1284
0
        SKIP_IF_TOP(op1);
1285
0
      }
1286
0
      if (op2) {
1287
0
        SKIP_IF_TOP(op2);
1288
0
      }
1289
0
      if (opline->opcode == ZEND_ASSIGN_OP) {
1290
0
        if (ct_eval_binary_op(&zv, opline->extended_value, op1, op2) == SUCCESS) {
1291
0
          SET_RESULT(op1, &zv);
1292
0
          SET_RESULT(result, &zv);
1293
0
          zval_ptr_dtor_nogc(&zv);
1294
0
          break;
1295
0
        }
1296
0
      } else if (opline->opcode == ZEND_ASSIGN_DIM_OP) {
1297
0
        if ((IS_PARTIAL_ARRAY(op1) || Z_TYPE_P(op1) == IS_ARRAY)
1298
0
            && ssa_op->op1_def >= 0 && op2) {
1299
0
          zval tmp;
1300
0
          zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
1301
1302
0
          SKIP_IF_TOP(data);
1303
1304
0
          if (ct_eval_fetch_dim(&tmp, op1, op2, 0) == SUCCESS) {
1305
0
            if (IS_BOT(data)) {
1306
0
              dup_partial_array(&zv, op1);
1307
0
              ct_eval_del_array_elem(&zv, op2);
1308
0
              SET_RESULT_BOT(result);
1309
0
              SET_RESULT(op1, &zv);
1310
0
              zval_ptr_dtor_nogc(&tmp);
1311
0
              zval_ptr_dtor_nogc(&zv);
1312
0
              break;
1313
0
            }
1314
1315
0
            if (ct_eval_binary_op(&tmp, opline->extended_value, &tmp, data) == FAILURE) {
1316
0
              SET_RESULT_BOT(result);
1317
0
              SET_RESULT_BOT(op1);
1318
0
              zval_ptr_dtor_nogc(&tmp);
1319
0
              break;
1320
0
            }
1321
1322
0
            if (IS_PARTIAL_ARRAY(op1)) {
1323
0
              dup_partial_array(&zv, op1);
1324
0
            } else {
1325
0
              ZVAL_COPY(&zv, op1);
1326
0
            }
1327
1328
0
            if (ct_eval_assign_dim(&zv, &tmp, op2) == SUCCESS) {
1329
0
              SET_RESULT(result, &tmp);
1330
0
              SET_RESULT(op1, &zv);
1331
0
              zval_ptr_dtor_nogc(&tmp);
1332
0
              zval_ptr_dtor_nogc(&zv);
1333
0
              break;
1334
0
            }
1335
1336
0
            zval_ptr_dtor_nogc(&tmp);
1337
0
            zval_ptr_dtor_nogc(&zv);
1338
0
          }
1339
0
        }
1340
0
      } else if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
1341
0
        if (op1 && IS_PARTIAL_OBJECT(op1)
1342
0
            && ssa_op->op1_def >= 0
1343
0
            && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
1344
0
          zval tmp;
1345
0
          zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
1346
1347
0
          SKIP_IF_TOP(data);
1348
1349
0
          if (ct_eval_fetch_obj(&tmp, op1, op2) == SUCCESS) {
1350
0
            if (IS_BOT(data)) {
1351
0
              dup_partial_object(&zv, op1);
1352
0
              ct_eval_del_obj_prop(&zv, op2);
1353
0
              SET_RESULT_BOT(result);
1354
0
              SET_RESULT(op1, &zv);
1355
0
              zval_ptr_dtor_nogc(&tmp);
1356
0
              zval_ptr_dtor_nogc(&zv);
1357
0
              break;
1358
0
            }
1359
1360
0
            if (ct_eval_binary_op(&tmp, opline->extended_value, &tmp, data) == FAILURE) {
1361
0
              SET_RESULT_BOT(result);
1362
0
              SET_RESULT_BOT(op1);
1363
0
              zval_ptr_dtor_nogc(&tmp);
1364
0
              break;
1365
0
            }
1366
1367
0
            dup_partial_object(&zv, op1);
1368
1369
0
            if (ct_eval_assign_obj(&zv, &tmp, op2) == SUCCESS) {
1370
0
              SET_RESULT(result, &tmp);
1371
0
              SET_RESULT(op1, &zv);
1372
0
              zval_ptr_dtor_nogc(&tmp);
1373
0
              zval_ptr_dtor_nogc(&zv);
1374
0
              break;
1375
0
            }
1376
1377
0
            zval_ptr_dtor_nogc(&tmp);
1378
0
            zval_ptr_dtor_nogc(&zv);
1379
0
          }
1380
0
        }
1381
0
      }
1382
0
      SET_RESULT_BOT(result);
1383
0
      SET_RESULT_BOT(op1);
1384
0
      break;
1385
0
    case ZEND_PRE_INC_OBJ:
1386
0
    case ZEND_PRE_DEC_OBJ:
1387
0
    case ZEND_POST_INC_OBJ:
1388
0
    case ZEND_POST_DEC_OBJ:
1389
0
      if (op1) {
1390
0
        SKIP_IF_TOP(op1);
1391
0
        SKIP_IF_TOP(op2);
1392
0
        if (IS_PARTIAL_OBJECT(op1)
1393
0
            && ssa_op->op1_def >= 0
1394
0
            && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
1395
0
          zval tmp1, tmp2;
1396
1397
0
          if (ct_eval_fetch_obj(&tmp1, op1, op2) == SUCCESS) {
1398
0
            if (ct_eval_incdec(&tmp2, opline->opcode, &tmp1) == SUCCESS) {
1399
0
              dup_partial_object(&zv, op1);
1400
0
              ct_eval_assign_obj(&zv, &tmp2, op2);
1401
0
              if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ) {
1402
0
                SET_RESULT(result, &tmp2);
1403
0
              } else {
1404
0
                SET_RESULT(result, &tmp1);
1405
0
              }
1406
0
              zval_ptr_dtor_nogc(&tmp1);
1407
0
              zval_ptr_dtor_nogc(&tmp2);
1408
0
              SET_RESULT(op1, &zv);
1409
0
              zval_ptr_dtor_nogc(&zv);
1410
0
              break;
1411
0
            }
1412
0
            zval_ptr_dtor_nogc(&tmp1);
1413
0
          }
1414
0
        }
1415
0
      }
1416
0
      SET_RESULT_BOT(op1);
1417
0
      SET_RESULT_BOT(result);
1418
0
      break;
1419
0
    case ZEND_PRE_INC:
1420
0
    case ZEND_PRE_DEC:
1421
0
      SKIP_IF_TOP(op1);
1422
0
      if (ct_eval_incdec(&zv, opline->opcode, op1) == SUCCESS) {
1423
0
        SET_RESULT(op1, &zv);
1424
0
        SET_RESULT(result, &zv);
1425
0
        zval_ptr_dtor_nogc(&zv);
1426
0
        break;
1427
0
      }
1428
0
      SET_RESULT_BOT(op1);
1429
0
      SET_RESULT_BOT(result);
1430
0
      break;
1431
0
    case ZEND_POST_INC:
1432
0
    case ZEND_POST_DEC:
1433
0
      SKIP_IF_TOP(op1);
1434
0
      SET_RESULT(result, op1);
1435
0
      if (ct_eval_incdec(&zv, opline->opcode, op1) == SUCCESS) {
1436
0
        SET_RESULT(op1, &zv);
1437
0
        zval_ptr_dtor_nogc(&zv);
1438
0
        break;
1439
0
      }
1440
0
      SET_RESULT_BOT(op1);
1441
0
      break;
1442
0
    case ZEND_BW_NOT:
1443
0
    case ZEND_BOOL_NOT:
1444
0
      SKIP_IF_TOP(op1);
1445
0
      if (IS_PARTIAL_ARRAY(op1)) {
1446
0
        SET_RESULT_BOT(result);
1447
0
        break;
1448
0
      }
1449
0
      if (zend_optimizer_eval_unary_op(&zv, opline->opcode, op1) == SUCCESS) {
1450
0
        SET_RESULT(result, &zv);
1451
0
        zval_ptr_dtor_nogc(&zv);
1452
0
        break;
1453
0
      }
1454
0
      SET_RESULT_BOT(result);
1455
0
      break;
1456
0
    case ZEND_CAST:
1457
0
      SKIP_IF_TOP(op1);
1458
0
      if (IS_PARTIAL_ARRAY(op1)) {
1459
0
        SET_RESULT_BOT(result);
1460
0
        break;
1461
0
      }
1462
0
      if (zend_optimizer_eval_cast(&zv, opline->extended_value, op1) == SUCCESS) {
1463
0
        SET_RESULT(result, &zv);
1464
0
        zval_ptr_dtor_nogc(&zv);
1465
0
        break;
1466
0
      }
1467
0
      SET_RESULT_BOT(result);
1468
0
      break;
1469
0
    case ZEND_BOOL:
1470
0
    case ZEND_JMPZ_EX:
1471
0
    case ZEND_JMPNZ_EX:
1472
0
      SKIP_IF_TOP(op1);
1473
0
      if (ct_eval_bool_cast(&zv, op1) == SUCCESS) {
1474
0
        SET_RESULT(result, &zv);
1475
0
        zval_ptr_dtor_nogc(&zv);
1476
0
        break;
1477
0
      }
1478
0
      SET_RESULT_BOT(result);
1479
0
      break;
1480
0
    case ZEND_STRLEN:
1481
0
      SKIP_IF_TOP(op1);
1482
0
      if (zend_optimizer_eval_strlen(&zv, op1) == SUCCESS) {
1483
0
        SET_RESULT(result, &zv);
1484
0
        zval_ptr_dtor_nogc(&zv);
1485
0
        break;
1486
0
      }
1487
0
      SET_RESULT_BOT(result);
1488
0
      break;
1489
0
    case ZEND_YIELD_FROM:
1490
      // tmp = yield from [] -> tmp = null
1491
0
      SKIP_IF_TOP(op1);
1492
0
      if (Z_TYPE_P(op1) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(op1)) == 0) {
1493
0
        ZVAL_NULL(&zv);
1494
0
        SET_RESULT(result, &zv);
1495
0
        break;
1496
0
      }
1497
0
      SET_RESULT_BOT(result);
1498
0
      break;
1499
0
    case ZEND_COUNT:
1500
0
      SKIP_IF_TOP(op1);
1501
0
      if (Z_TYPE_P(op1) == IS_ARRAY) {
1502
0
        ZVAL_LONG(&zv, zend_hash_num_elements(Z_ARRVAL_P(op1)));
1503
0
        SET_RESULT(result, &zv);
1504
0
        zval_ptr_dtor_nogc(&zv);
1505
0
        break;
1506
0
      }
1507
0
      SET_RESULT_BOT(result);
1508
0
      break;
1509
0
    case ZEND_IN_ARRAY:
1510
0
      SKIP_IF_TOP(op1);
1511
0
      SKIP_IF_TOP(op2);
1512
0
      if (ct_eval_in_array(&zv, opline->extended_value, op1, op2) == SUCCESS) {
1513
0
        SET_RESULT(result, &zv);
1514
0
        zval_ptr_dtor_nogc(&zv);
1515
0
        break;
1516
0
      }
1517
0
      SET_RESULT_BOT(result);
1518
0
      break;
1519
0
    case ZEND_ARRAY_KEY_EXISTS:
1520
0
      SKIP_IF_TOP(op1);
1521
0
      SKIP_IF_TOP(op2);
1522
0
      if (ct_eval_array_key_exists(&zv, op1, op2) == SUCCESS) {
1523
0
        SET_RESULT(result, &zv);
1524
0
        zval_ptr_dtor_nogc(&zv);
1525
0
        break;
1526
0
      }
1527
0
      SET_RESULT_BOT(result);
1528
0
      break;
1529
0
    case ZEND_FETCH_DIM_R:
1530
0
    case ZEND_FETCH_DIM_IS:
1531
0
    case ZEND_FETCH_LIST_R:
1532
0
      SKIP_IF_TOP(op1);
1533
0
      SKIP_IF_TOP(op2);
1534
1535
0
      if (ct_eval_fetch_dim(&zv, op1, op2, (opline->opcode != ZEND_FETCH_LIST_R)) == SUCCESS) {
1536
0
        SET_RESULT(result, &zv);
1537
0
        zval_ptr_dtor_nogc(&zv);
1538
0
        break;
1539
0
      }
1540
0
      SET_RESULT_BOT(result);
1541
0
      break;
1542
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
1543
0
      SKIP_IF_TOP(op1);
1544
0
      SKIP_IF_TOP(op2);
1545
1546
0
      if (ct_eval_isset_dim(&zv, opline->extended_value, op1, op2) == SUCCESS) {
1547
0
        SET_RESULT(result, &zv);
1548
0
        zval_ptr_dtor_nogc(&zv);
1549
0
        break;
1550
0
      }
1551
0
      SET_RESULT_BOT(result);
1552
0
      break;
1553
0
    case ZEND_FETCH_OBJ_R:
1554
0
    case ZEND_FETCH_OBJ_IS:
1555
0
      if (op1) {
1556
0
        SKIP_IF_TOP(op1);
1557
0
        SKIP_IF_TOP(op2);
1558
1559
0
        if (ct_eval_fetch_obj(&zv, op1, op2) == SUCCESS) {
1560
0
          SET_RESULT(result, &zv);
1561
0
          zval_ptr_dtor_nogc(&zv);
1562
0
          break;
1563
0
        }
1564
0
      }
1565
0
      SET_RESULT_BOT(result);
1566
0
      break;
1567
0
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
1568
0
      if (op1) {
1569
0
        SKIP_IF_TOP(op1);
1570
0
        SKIP_IF_TOP(op2);
1571
1572
0
        if (ct_eval_isset_obj(&zv, opline->extended_value, op1, op2) == SUCCESS) {
1573
0
          SET_RESULT(result, &zv);
1574
0
          zval_ptr_dtor_nogc(&zv);
1575
0
          break;
1576
0
        }
1577
0
      }
1578
0
      SET_RESULT_BOT(result);
1579
0
      break;
1580
0
    case ZEND_QM_ASSIGN:
1581
0
    case ZEND_JMP_SET:
1582
0
    case ZEND_COALESCE:
1583
0
    case ZEND_COPY_TMP:
1584
0
      SET_RESULT(result, op1);
1585
0
      break;
1586
0
    case ZEND_JMP_NULL:
1587
0
      switch (opline->extended_value & ZEND_SHORT_CIRCUITING_CHAIN_MASK) {
1588
0
        case ZEND_SHORT_CIRCUITING_CHAIN_EXPR:
1589
0
          ZVAL_NULL(&zv);
1590
0
          break;
1591
0
        case ZEND_SHORT_CIRCUITING_CHAIN_ISSET:
1592
0
          ZVAL_FALSE(&zv);
1593
0
          break;
1594
0
        case ZEND_SHORT_CIRCUITING_CHAIN_EMPTY:
1595
0
          ZVAL_TRUE(&zv);
1596
0
          break;
1597
0
        default: ZEND_UNREACHABLE();
1598
0
      }
1599
0
      SET_RESULT(result, &zv);
1600
0
      break;
1601
0
    case ZEND_FETCH_CLASS:
1602
0
      SET_RESULT(result, op2);
1603
0
      break;
1604
0
    case ZEND_ISSET_ISEMPTY_CV:
1605
0
      SKIP_IF_TOP(op1);
1606
0
      if (ct_eval_isset_isempty(&zv, opline->extended_value, op1) == SUCCESS) {
1607
0
        SET_RESULT(result, &zv);
1608
0
        zval_ptr_dtor_nogc(&zv);
1609
0
        break;
1610
0
      }
1611
0
      SET_RESULT_BOT(result);
1612
0
      break;
1613
0
    case ZEND_TYPE_CHECK:
1614
0
      SKIP_IF_TOP(op1);
1615
0
      ct_eval_type_check(&zv, opline->extended_value, op1);
1616
0
      SET_RESULT(result, &zv);
1617
0
      zval_ptr_dtor_nogc(&zv);
1618
0
      break;
1619
0
    case ZEND_INSTANCEOF:
1620
0
      SKIP_IF_TOP(op1);
1621
0
      ZVAL_FALSE(&zv);
1622
0
      SET_RESULT(result, &zv);
1623
0
      break;
1624
0
    case ZEND_ROPE_INIT:
1625
0
      SKIP_IF_TOP(op2);
1626
0
      if (IS_PARTIAL_ARRAY(op2)) {
1627
0
        SET_RESULT_BOT(result);
1628
0
        break;
1629
0
      }
1630
0
      if (zend_optimizer_eval_cast(&zv, IS_STRING, op2) == SUCCESS) {
1631
0
        SET_RESULT(result, &zv);
1632
0
        zval_ptr_dtor_nogc(&zv);
1633
0
        break;
1634
0
      }
1635
0
      SET_RESULT_BOT(result);
1636
0
      break;
1637
0
    case ZEND_ROPE_ADD:
1638
0
    case ZEND_ROPE_END:
1639
      // TODO The way this is currently implemented will result in quadratic runtime
1640
      // This is not necessary, the way the algorithm works it's okay to reuse the same
1641
      // string for all SSA vars with some extra checks
1642
0
      SKIP_IF_TOP(op1);
1643
0
      SKIP_IF_TOP(op2);
1644
0
      if (ct_eval_binary_op(&zv, ZEND_CONCAT, op1, op2) == SUCCESS) {
1645
0
        SET_RESULT(result, &zv);
1646
0
        zval_ptr_dtor_nogc(&zv);
1647
0
        break;
1648
0
      }
1649
0
      SET_RESULT_BOT(result);
1650
0
      break;
1651
0
    case ZEND_DO_ICALL:
1652
0
    {
1653
0
      zend_call_info *call;
1654
0
      zval *name, *args[3] = {NULL};
1655
1656
0
      if (!ctx->call_map) {
1657
0
        SET_RESULT_BOT(result);
1658
0
        break;
1659
0
      }
1660
1661
0
      call = ctx->call_map[opline - ctx->scdf.op_array->opcodes];
1662
0
      name = CT_CONSTANT_EX(ctx->scdf.op_array, call->caller_init_opline->op2.constant);
1663
1664
      /* We already know it can't be evaluated, don't bother checking again */
1665
0
      if (ssa_op->result_def < 0 || IS_BOT(&ctx->values[ssa_op->result_def])) {
1666
0
        break;
1667
0
      }
1668
1669
      /* We're only interested in functions with up to three arguments right now.
1670
       * Note that named arguments with the argument in declaration order will still work. */
1671
0
      if (call->num_args > 3 || call->send_unpack || call->is_prototype || call->named_args) {
1672
0
        SET_RESULT_BOT(result);
1673
0
        break;
1674
0
      }
1675
1676
0
      for (uint32_t i = 0; i < call->num_args; i++) {
1677
0
        zend_op *opline = call->arg_info[i].opline;
1678
0
        if (opline->opcode != ZEND_SEND_VAL && opline->opcode != ZEND_SEND_VAR) {
1679
0
          SET_RESULT_BOT(result);
1680
0
          return;
1681
0
        }
1682
1683
0
        args[i] = get_op1_value(ctx, opline,
1684
0
          &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes]);
1685
0
        if (args[i]) {
1686
0
          if (IS_BOT(args[i]) || IS_PARTIAL_ARRAY(args[i])) {
1687
0
            SET_RESULT_BOT(result);
1688
0
            return;
1689
0
          } else if (IS_TOP(args[i])) {
1690
0
            return;
1691
0
          }
1692
0
        }
1693
0
      }
1694
1695
      /* We didn't get a BOT argument, so value stays the same */
1696
0
      if (!IS_TOP(&ctx->values[ssa_op->result_def])) {
1697
0
        break;
1698
0
      }
1699
1700
0
      if (ct_eval_func_call(scdf->op_array, &zv, Z_STR_P(name), call->num_args, args) == SUCCESS) {
1701
0
        SET_RESULT(result, &zv);
1702
0
        zval_ptr_dtor_nogc(&zv);
1703
0
        break;
1704
0
      }
1705
1706
#if 0
1707
      /* sort out | uniq -c | sort -n */
1708
      fprintf(stderr, "%s\n", Z_STRVAL_P(name));
1709
      /*if (args[1]) {
1710
        php_printf("%s %Z %Z\n", Z_STRVAL_P(name), args[0], args[1]);
1711
      } else {
1712
        php_printf("%s %Z\n", Z_STRVAL_P(name), args[0]);
1713
      }*/
1714
#endif
1715
1716
0
      SET_RESULT_BOT(result);
1717
0
      break;
1718
0
    }
1719
0
    case ZEND_FRAMELESS_ICALL_0:
1720
0
    case ZEND_FRAMELESS_ICALL_1:
1721
0
    case ZEND_FRAMELESS_ICALL_2:
1722
0
    case ZEND_FRAMELESS_ICALL_3: {
1723
      /* We already know it can't be evaluated, don't bother checking again */
1724
0
      if (ssa_op->result_def < 0 || IS_BOT(&ctx->values[ssa_op->result_def])) {
1725
0
        break;
1726
0
      }
1727
1728
0
      zval *args[3] = {NULL};
1729
0
      zend_function *func = ZEND_FLF_FUNC(opline);
1730
0
      uint32_t num_args = ZEND_FLF_NUM_ARGS(opline->opcode);
1731
1732
0
      switch (num_args) {
1733
0
        case 3: {
1734
0
          zend_op *op_data = opline + 1;
1735
0
          args[2] = get_op1_value(ctx, op_data, &ctx->scdf.ssa->ops[op_data - ctx->scdf.op_array->opcodes]);
1736
0
          ZEND_FALLTHROUGH;
1737
0
        }
1738
0
        case 2:
1739
0
          args[1] = get_op2_value(ctx, opline, &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes]);
1740
0
          ZEND_FALLTHROUGH;
1741
0
        case 1:
1742
0
          args[0] = get_op1_value(ctx, opline, &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes]);
1743
0
          break;
1744
0
      }
1745
0
      for (uint32_t i = 0; i < num_args; i++) {
1746
0
        if (!args[i]) {
1747
0
          SET_RESULT_BOT(result);
1748
0
          return;
1749
0
        } else if (IS_BOT(args[i]) || IS_PARTIAL_ARRAY(args[i])) {
1750
0
          SET_RESULT_BOT(result);
1751
0
          return;
1752
0
        } else if (IS_TOP(args[i])) {
1753
0
          return;
1754
0
        }
1755
0
      }
1756
0
      if (ct_eval_func_call_ex(scdf->op_array, &zv, func, num_args, args) == SUCCESS) {
1757
0
        SET_RESULT(result, &zv);
1758
0
        zval_ptr_dtor_nogc(&zv);
1759
0
        break;
1760
0
      }
1761
0
      SET_RESULT_BOT(result);
1762
0
      break;
1763
0
    }
1764
1
    default:
1765
1
    {
1766
      /* If we have no explicit implementation return BOT */
1767
1
      SET_RESULT_BOT(result);
1768
1
      SET_RESULT_BOT(op1);
1769
1
      SET_RESULT_BOT(op2);
1770
1
      break;
1771
0
    }
1772
1
  }
1773
1
}
1774
1775
0
static zval *value_from_type_and_range(sccp_ctx *ctx, int var_num, zval *tmp) {
1776
0
  zend_ssa *ssa = ctx->scdf.ssa;
1777
0
  zend_ssa_var_info *info = &ssa->var_info[var_num];
1778
1779
0
  if (info->type & MAY_BE_UNDEF) {
1780
0
    return NULL;
1781
0
  }
1782
1783
0
  if (!(info->type & MAY_BE_ANY)) {
1784
    /* This code must be unreachable. We could replace operands with NULL, but this doesn't
1785
     * really make things better. It would be better to later remove this code entirely. */
1786
0
    return NULL;
1787
0
  }
1788
1789
0
  if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_NULL))) {
1790
0
    if (ssa->vars[var_num].definition >= 0
1791
0
     && ctx->scdf.op_array->opcodes[ssa->vars[var_num].definition].opcode == ZEND_VERIFY_RETURN_TYPE) {
1792
0
      return NULL;
1793
0
    }
1794
0
    ZVAL_NULL(tmp);
1795
0
    return tmp;
1796
0
  }
1797
0
  if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_FALSE))) {
1798
0
    if (ssa->vars[var_num].definition >= 0
1799
0
     && ctx->scdf.op_array->opcodes[ssa->vars[var_num].definition].opcode == ZEND_VERIFY_RETURN_TYPE) {
1800
0
      return NULL;
1801
0
    }
1802
0
    ZVAL_FALSE(tmp);
1803
0
    return tmp;
1804
0
  }
1805
0
  if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_TRUE))) {
1806
0
    if (ssa->vars[var_num].definition >= 0
1807
0
     && ctx->scdf.op_array->opcodes[ssa->vars[var_num].definition].opcode == ZEND_VERIFY_RETURN_TYPE) {
1808
0
      return NULL;
1809
0
    }
1810
0
    ZVAL_TRUE(tmp);
1811
0
    return tmp;
1812
0
  }
1813
1814
0
  if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))
1815
0
      && info->has_range
1816
0
      && !info->range.overflow && !info->range.underflow
1817
0
      && info->range.min == info->range.max) {
1818
0
    ZVAL_LONG(tmp, info->range.min);
1819
0
    return tmp;
1820
0
  }
1821
1822
0
  return NULL;
1823
0
}
1824
1825
1826
/* Returns whether there is a successor */
1827
static void sccp_mark_feasible_successors(
1828
    scdf_ctx *scdf,
1829
    int block_num, zend_basic_block *block,
1830
0
    zend_op *opline, zend_ssa_op *ssa_op) {
1831
0
  sccp_ctx *ctx = (sccp_ctx *) scdf;
1832
0
  zval *op1, zv;
1833
0
  uint32_t s;
1834
1835
  /* We can't determine the branch target at compile-time for these */
1836
0
  switch (opline->opcode) {
1837
0
    case ZEND_ASSERT_CHECK:
1838
0
    case ZEND_CATCH:
1839
0
    case ZEND_FE_FETCH_R:
1840
0
    case ZEND_FE_FETCH_RW:
1841
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
1842
0
      scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
1843
0
      scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
1844
0
      return;
1845
0
  }
1846
1847
0
  op1 = get_op1_value(ctx, opline, ssa_op);
1848
0
  if (IS_BOT(op1)) {
1849
0
    ZEND_ASSERT(ssa_op->op1_use >= 0);
1850
0
    op1 = value_from_type_and_range(ctx, ssa_op->op1_use, &zv);
1851
0
  }
1852
1853
  /* Branch target can be either one */
1854
0
  if (!op1 || IS_BOT(op1)) {
1855
0
    for (s = 0; s < block->successors_count; s++) {
1856
0
      scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
1857
0
    }
1858
0
    return;
1859
0
  }
1860
1861
  /* Branch target not yet known */
1862
0
  if (IS_TOP(op1)) {
1863
0
    return;
1864
0
  }
1865
1866
0
  switch (opline->opcode) {
1867
0
    case ZEND_JMPZ:
1868
0
    case ZEND_JMPZ_EX:
1869
0
    {
1870
0
      if (ct_eval_bool_cast(&zv, op1) == FAILURE) {
1871
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
1872
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
1873
0
        return;
1874
0
      }
1875
0
      s = Z_TYPE(zv) == IS_TRUE;
1876
0
      break;
1877
0
    }
1878
0
    case ZEND_JMPNZ:
1879
0
    case ZEND_JMPNZ_EX:
1880
0
    case ZEND_JMP_SET:
1881
0
    {
1882
0
      if (ct_eval_bool_cast(&zv, op1) == FAILURE) {
1883
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
1884
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
1885
0
        return;
1886
0
      }
1887
0
      s = Z_TYPE(zv) == IS_FALSE;
1888
0
      break;
1889
0
    }
1890
0
    case ZEND_COALESCE:
1891
0
      s = (Z_TYPE_P(op1) == IS_NULL);
1892
0
      break;
1893
0
    case ZEND_JMP_NULL:
1894
0
      s = (Z_TYPE_P(op1) != IS_NULL);
1895
0
      break;
1896
0
    case ZEND_FE_RESET_R:
1897
0
    case ZEND_FE_RESET_RW:
1898
      /* A non-empty partial array is definitely non-empty, but an
1899
       * empty partial array may be non-empty at runtime. */
1900
0
      if (Z_TYPE_P(op1) != IS_ARRAY ||
1901
0
          (IS_PARTIAL_ARRAY(op1) && zend_hash_num_elements(Z_ARR_P(op1)) == 0)) {
1902
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
1903
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
1904
0
        return;
1905
0
      }
1906
0
      s = zend_hash_num_elements(Z_ARR_P(op1)) != 0;
1907
0
      break;
1908
0
    case ZEND_SWITCH_LONG:
1909
0
    case ZEND_SWITCH_STRING:
1910
0
    case ZEND_MATCH:
1911
0
    {
1912
0
      bool strict_comparison = opline->opcode == ZEND_MATCH;
1913
0
      uint8_t type = Z_TYPE_P(op1);
1914
0
      bool correct_type =
1915
0
        (opline->opcode == ZEND_SWITCH_LONG && type == IS_LONG)
1916
0
        || (opline->opcode == ZEND_SWITCH_STRING && type == IS_STRING)
1917
0
        || (opline->opcode == ZEND_MATCH && (type == IS_LONG || type == IS_STRING));
1918
1919
0
      if (correct_type) {
1920
0
        zend_op_array *op_array = scdf->op_array;
1921
0
        zend_ssa *ssa = scdf->ssa;
1922
0
        HashTable *jmptable = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant));
1923
0
        zval *jmp_zv = type == IS_LONG
1924
0
          ? zend_hash_index_find(jmptable, Z_LVAL_P(op1))
1925
0
          : zend_hash_find(jmptable, Z_STR_P(op1));
1926
0
        int target;
1927
1928
0
        if (jmp_zv) {
1929
0
          target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(jmp_zv))];
1930
0
        } else {
1931
0
          target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
1932
0
        }
1933
0
        scdf_mark_edge_feasible(scdf, block_num, target);
1934
0
        return;
1935
0
      } else if (strict_comparison) {
1936
0
        zend_op_array *op_array = scdf->op_array;
1937
0
        zend_ssa *ssa = scdf->ssa;
1938
0
        int target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
1939
0
        scdf_mark_edge_feasible(scdf, block_num, target);
1940
0
        return;
1941
0
      }
1942
0
      s = block->successors_count - 1;
1943
0
      break;
1944
0
    }
1945
0
    default:
1946
0
      for (s = 0; s < block->successors_count; s++) {
1947
0
        scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
1948
0
      }
1949
0
      return;
1950
0
  }
1951
0
  scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
1952
0
}
1953
1954
static void join_hash_tables(HashTable *ret, HashTable *ht1, HashTable *ht2)
1955
0
{
1956
0
  zend_ulong index;
1957
0
  zend_string *key;
1958
0
  zval *val1, *val2;
1959
1960
0
  ZEND_HASH_FOREACH_KEY_VAL(ht1, index, key, val1) {
1961
0
    if (key) {
1962
0
      val2 = zend_hash_find(ht2, key);
1963
0
    } else {
1964
0
      val2 = zend_hash_index_find(ht2, index);
1965
0
    }
1966
0
    if (val2 && zend_is_identical(val1, val2)) {
1967
0
      if (key) {
1968
0
        val1 = zend_hash_add_new(ret, key, val1);
1969
0
      } else {
1970
0
        val1 = zend_hash_index_add_new(ret, index, val1);
1971
0
      }
1972
0
      Z_TRY_ADDREF_P(val1);
1973
0
    }
1974
0
  } ZEND_HASH_FOREACH_END();
1975
0
}
1976
1977
static zend_result join_partial_arrays(zval *a, zval *b)
1978
0
{
1979
0
  zval ret;
1980
1981
0
  if ((Z_TYPE_P(a) != IS_ARRAY && !IS_PARTIAL_ARRAY(a))
1982
0
      || (Z_TYPE_P(b) != IS_ARRAY && !IS_PARTIAL_ARRAY(b))) {
1983
0
    return FAILURE;
1984
0
  }
1985
1986
0
  empty_partial_array(&ret);
1987
0
  join_hash_tables(Z_ARRVAL(ret), Z_ARRVAL_P(a), Z_ARRVAL_P(b));
1988
0
  zval_ptr_dtor_nogc(a);
1989
0
  ZVAL_COPY_VALUE(a, &ret);
1990
1991
0
  return SUCCESS;
1992
0
}
1993
1994
static zend_result join_partial_objects(zval *a, zval *b)
1995
0
{
1996
0
  zval ret;
1997
1998
0
  if (!IS_PARTIAL_OBJECT(a) || !IS_PARTIAL_OBJECT(b)) {
1999
0
    return FAILURE;
2000
0
  }
2001
2002
0
  empty_partial_object(&ret);
2003
0
  join_hash_tables(Z_ARRVAL(ret), Z_ARRVAL_P(a), Z_ARRVAL_P(b));
2004
0
  zval_ptr_dtor_nogc(a);
2005
0
  ZVAL_COPY_VALUE(a, &ret);
2006
2007
0
  return SUCCESS;
2008
0
}
2009
2010
0
static void join_phi_values(zval *a, zval *b, bool escape) {
2011
0
  if (IS_BOT(a) || IS_TOP(b)) {
2012
0
    return;
2013
0
  }
2014
0
  if (IS_TOP(a)) {
2015
0
    zval_ptr_dtor_nogc(a);
2016
0
    ZVAL_COPY(a, b);
2017
0
    return;
2018
0
  }
2019
0
  if (IS_BOT(b)) {
2020
0
    zval_ptr_dtor_nogc(a);
2021
0
    MAKE_BOT(a);
2022
0
    return;
2023
0
  }
2024
0
  if (IS_PARTIAL_ARRAY(a) || IS_PARTIAL_ARRAY(b)) {
2025
0
    if (join_partial_arrays(a, b) == FAILURE) {
2026
0
      zval_ptr_dtor_nogc(a);
2027
0
      MAKE_BOT(a);
2028
0
    }
2029
0
  } else if (IS_PARTIAL_OBJECT(a) || IS_PARTIAL_OBJECT(b)) {
2030
0
    if (escape || join_partial_objects(a, b) == FAILURE) {
2031
0
      zval_ptr_dtor_nogc(a);
2032
0
      MAKE_BOT(a);
2033
0
    }
2034
0
  } else if (!zend_is_identical(a, b)) {
2035
0
    if (join_partial_arrays(a, b) == FAILURE) {
2036
0
      zval_ptr_dtor_nogc(a);
2037
0
      MAKE_BOT(a);
2038
0
    }
2039
0
  }
2040
0
}
2041
2042
0
static void sccp_visit_phi(scdf_ctx *scdf, const zend_ssa_phi *phi) {
2043
0
  sccp_ctx *ctx = (sccp_ctx *) scdf;
2044
0
  zend_ssa *ssa = scdf->ssa;
2045
0
  ZEND_ASSERT(phi->ssa_var >= 0);
2046
0
  if (!IS_BOT(&ctx->values[phi->ssa_var])) {
2047
0
    zend_basic_block *block = &ssa->cfg.blocks[phi->block];
2048
0
    int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
2049
2050
0
    zval result;
2051
0
    MAKE_TOP(&result);
2052
#if SCP_DEBUG
2053
    fprintf(stderr, "Handling phi(");
2054
#endif
2055
0
    if (phi->pi >= 0) {
2056
0
      ZEND_ASSERT(phi->sources[0] >= 0);
2057
0
      if (scdf_is_edge_feasible(scdf, phi->pi, phi->block)) {
2058
0
        join_phi_values(&result, &ctx->values[phi->sources[0]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
2059
0
      }
2060
0
    } else {
2061
0
      for (uint32_t i = 0; i < block->predecessors_count; i++) {
2062
0
        ZEND_ASSERT(phi->sources[i] >= 0);
2063
0
        if (scdf_is_edge_feasible(scdf, predecessors[i], phi->block)) {
2064
#if SCP_DEBUG
2065
          scp_dump_value(&ctx->values[phi->sources[i]]);
2066
          fprintf(stderr, ",");
2067
#endif
2068
0
          join_phi_values(&result, &ctx->values[phi->sources[i]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
2069
0
        } else {
2070
#if SCP_DEBUG
2071
          fprintf(stderr, " --,");
2072
#endif
2073
0
        }
2074
0
      }
2075
0
    }
2076
#if SCP_DEBUG
2077
    fprintf(stderr, ")\n");
2078
#endif
2079
2080
0
    set_value(scdf, ctx, phi->ssa_var, &result);
2081
0
    zval_ptr_dtor_nogc(&result);
2082
0
  }
2083
0
}
2084
2085
/* Call instruction -> remove opcodes that are part of the call */
2086
static int remove_call(sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op)
2087
0
{
2088
0
  zend_ssa *ssa = ctx->scdf.ssa;
2089
0
  zend_op_array *op_array = ctx->scdf.op_array;
2090
0
  zend_call_info *call;
2091
2092
0
  ZEND_ASSERT(ctx->call_map);
2093
0
  call = ctx->call_map[opline - op_array->opcodes];
2094
0
  ZEND_ASSERT(call);
2095
0
  ZEND_ASSERT(call->caller_call_opline == opline);
2096
0
  zend_ssa_remove_instr(ssa, opline, ssa_op);
2097
0
  zend_ssa_remove_instr(ssa, call->caller_init_opline,
2098
0
    &ssa->ops[call->caller_init_opline - op_array->opcodes]);
2099
2100
0
  for (uint32_t i = 0; i < call->num_args; i++) {
2101
0
    zend_ssa_remove_instr(ssa, call->arg_info[i].opline,
2102
0
      &ssa->ops[call->arg_info[i].opline - op_array->opcodes]);
2103
0
  }
2104
2105
  // TODO: remove call_info completely???
2106
0
  call->callee_func = NULL;
2107
2108
0
  return call->num_args + 2;
2109
0
}
2110
2111
/* This is a basic DCE pass we run after SCCP. It only works on those instructions those result
2112
 * value(s) were determined by SCCP. It removes dead computational instructions and converts
2113
 * CV-affecting instructions into CONST ASSIGNs. This basic DCE is performed for multiple reasons:
2114
 * a) During operand replacement we eliminate FREEs. The corresponding computational instructions
2115
 *    must be removed to avoid leaks. This way SCCP can run independently of the full DCE pass.
2116
 * b) The main DCE pass relies on type analysis to determine whether instructions have side-effects
2117
 *    and can't be DCEd. This means that it will not be able collect all instructions rendered dead
2118
 *    by SCCP, because they may have potentially side-effecting types, but the actual values are
2119
 *    not. As such doing DCE here will allow us to eliminate more dead code in combination.
2120
 * c) The ordinary DCE pass cannot collect dead calls. However SCCP can result in dead calls, which
2121
 *    we need to collect.
2122
 * d) The ordinary DCE pass cannot collect construction of dead non-escaping arrays and objects.
2123
 */
2124
static uint32_t try_remove_definition(sccp_ctx *ctx, int var_num, zend_ssa_var *var, zval *value)
2125
0
{
2126
0
  zend_ssa *ssa = ctx->scdf.ssa;
2127
0
  zend_op_array *op_array = ctx->scdf.op_array;
2128
0
  uint32_t removed_ops = 0;
2129
2130
0
  if (var->definition >= 0) {
2131
0
    zend_op *opline = &op_array->opcodes[var->definition];
2132
0
    zend_ssa_op *ssa_op = &ssa->ops[var->definition];
2133
2134
0
    if (ssa_op->result_def == var_num) {
2135
0
      if (opline->opcode == ZEND_ASSIGN) {
2136
        /* We can't drop the ASSIGN, but we can remove the result. */
2137
0
        if (var->use_chain < 0 && var->phi_use_chain == NULL) {
2138
0
          opline->result_type = IS_UNUSED;
2139
0
          zend_ssa_remove_result_def(ssa, ssa_op);
2140
0
        }
2141
0
        return 0;
2142
0
      }
2143
0
      if (ssa_op->op1_def >= 0 || ssa_op->op2_def >= 0) {
2144
0
        if (var->use_chain < 0 && var->phi_use_chain == NULL) {
2145
0
          switch (opline->opcode) {
2146
0
            case ZEND_ASSIGN:
2147
0
            case ZEND_ASSIGN_REF:
2148
0
            case ZEND_ASSIGN_DIM:
2149
0
            case ZEND_ASSIGN_OBJ:
2150
0
            case ZEND_ASSIGN_OBJ_REF:
2151
0
            case ZEND_ASSIGN_STATIC_PROP:
2152
0
            case ZEND_ASSIGN_STATIC_PROP_REF:
2153
0
            case ZEND_ASSIGN_OP:
2154
0
            case ZEND_ASSIGN_DIM_OP:
2155
0
            case ZEND_ASSIGN_OBJ_OP:
2156
0
            case ZEND_ASSIGN_STATIC_PROP_OP:
2157
0
            case ZEND_PRE_INC:
2158
0
            case ZEND_PRE_DEC:
2159
0
            case ZEND_PRE_INC_OBJ:
2160
0
            case ZEND_PRE_DEC_OBJ:
2161
0
            case ZEND_DO_ICALL:
2162
0
            case ZEND_DO_UCALL:
2163
0
            case ZEND_DO_FCALL_BY_NAME:
2164
0
            case ZEND_DO_FCALL:
2165
0
            case ZEND_INCLUDE_OR_EVAL:
2166
0
            case ZEND_YIELD:
2167
0
            case ZEND_YIELD_FROM:
2168
0
            case ZEND_ASSERT_CHECK:
2169
0
              opline->result_type = IS_UNUSED;
2170
0
              zend_ssa_remove_result_def(ssa, ssa_op);
2171
0
              break;
2172
0
            default:
2173
0
              break;
2174
0
          }
2175
0
        }
2176
        /* we cannot remove instruction that defines other variables */
2177
0
        return 0;
2178
0
      } else if (opline->opcode == ZEND_JMPZ_EX
2179
0
          || opline->opcode == ZEND_JMPNZ_EX
2180
0
          || opline->opcode == ZEND_JMP_SET
2181
0
          || opline->opcode == ZEND_COALESCE
2182
0
          || opline->opcode == ZEND_JMP_NULL
2183
0
          || opline->opcode == ZEND_FE_RESET_R
2184
0
          || opline->opcode == ZEND_FE_RESET_RW
2185
0
          || opline->opcode == ZEND_FE_FETCH_R
2186
0
          || opline->opcode == ZEND_FE_FETCH_RW
2187
0
          || opline->opcode == ZEND_NEW) {
2188
        /* we cannot simple remove jump instructions */
2189
0
        return 0;
2190
0
      } else if (var->use_chain >= 0
2191
0
          || var->phi_use_chain != NULL) {
2192
0
        if (value
2193
0
            && (opline->result_type & (IS_VAR|IS_TMP_VAR))
2194
0
            && opline->opcode != ZEND_QM_ASSIGN
2195
0
            && opline->opcode != ZEND_FETCH_CLASS
2196
0
            && opline->opcode != ZEND_ROPE_INIT
2197
0
            && opline->opcode != ZEND_ROPE_ADD
2198
0
            && opline->opcode != ZEND_INIT_ARRAY
2199
0
            && opline->opcode != ZEND_ADD_ARRAY_ELEMENT
2200
0
            && opline->opcode != ZEND_ADD_ARRAY_UNPACK) {
2201
          /* Replace with QM_ASSIGN */
2202
0
          uint8_t old_type = opline->result_type;
2203
0
          uint32_t old_var = opline->result.var;
2204
2205
0
          ssa_op->result_def = -1;
2206
0
          if (opline->opcode == ZEND_DO_ICALL) {
2207
0
            removed_ops = remove_call(ctx, opline, ssa_op) - 1;
2208
0
          } else {
2209
0
            bool has_op_data = opline->opcode == ZEND_FRAMELESS_ICALL_3;
2210
0
            zend_ssa_remove_instr(ssa, opline, ssa_op);
2211
0
            removed_ops++;
2212
0
            if (has_op_data) {
2213
0
              zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
2214
0
              removed_ops++;
2215
0
            }
2216
0
          }
2217
0
          ssa_op->result_def = var_num;
2218
0
          opline->opcode = ZEND_QM_ASSIGN;
2219
0
          opline->result_type = old_type;
2220
0
          opline->result.var = old_var;
2221
0
          Z_TRY_ADDREF_P(value);
2222
0
          zend_optimizer_update_op1_const(ctx->scdf.op_array, opline, value);
2223
0
        }
2224
0
        return 0;
2225
0
      } else if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
2226
0
          && (!value_known(&ctx->values[ssa_op->op2_use])
2227
0
            || IS_PARTIAL_ARRAY(&ctx->values[ssa_op->op2_use])
2228
0
            || IS_PARTIAL_OBJECT(&ctx->values[ssa_op->op2_use]))) {
2229
0
        return 0;
2230
0
      } else if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
2231
0
          && (!value_known(&ctx->values[ssa_op->op1_use])
2232
0
            || IS_PARTIAL_ARRAY(&ctx->values[ssa_op->op1_use])
2233
0
            || IS_PARTIAL_OBJECT(&ctx->values[ssa_op->op1_use]))) {
2234
0
        if (opline->opcode == ZEND_TYPE_CHECK
2235
0
         || opline->opcode == ZEND_BOOL) {
2236
0
          zend_ssa_remove_result_def(ssa, ssa_op);
2237
          /* For TYPE_CHECK we may compute the result value without knowing the
2238
           * operand, based on type inference information. Make sure the operand is
2239
           * freed and leave further cleanup to DCE. */
2240
0
          opline->opcode = ZEND_FREE;
2241
0
          opline->result_type = IS_UNUSED;
2242
0
          removed_ops++;
2243
0
        } else {
2244
0
          return 0;
2245
0
        }
2246
0
      } else {
2247
0
        zend_ssa_remove_result_def(ssa, ssa_op);
2248
0
        if (opline->opcode == ZEND_DO_ICALL) {
2249
0
          removed_ops = remove_call(ctx, opline, ssa_op);
2250
0
        } else {
2251
0
          bool has_op_data = opline->opcode == ZEND_FRAMELESS_ICALL_3;
2252
0
          zend_ssa_remove_instr(ssa, opline, ssa_op);
2253
0
          removed_ops++;
2254
0
          if (has_op_data) {
2255
0
            zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
2256
0
            removed_ops++;
2257
0
          }
2258
0
        }
2259
0
      }
2260
0
    } else if (ssa_op->op1_def == var_num) {
2261
0
      if (opline->opcode == ZEND_ASSIGN) {
2262
        /* Leave assigns to DCE (due to dtor effects) */
2263
0
        return 0;
2264
0
      }
2265
2266
      /* Compound assign or incdec -> convert to direct ASSIGN */
2267
2268
0
      if (!value) {
2269
        /* In some cases zend_may_throw() may be avoided */
2270
0
        switch (opline->opcode) {
2271
0
          case ZEND_ASSIGN_DIM:
2272
0
          case ZEND_ASSIGN_OBJ:
2273
0
          case ZEND_ASSIGN_OP:
2274
0
          case ZEND_ASSIGN_DIM_OP:
2275
0
          case ZEND_ASSIGN_OBJ_OP:
2276
0
          case ZEND_ASSIGN_STATIC_PROP_OP:
2277
0
            if ((ssa_op->op2_use >= 0 && !value_known(&ctx->values[ssa_op->op2_use]))
2278
0
                || ((ssa_op+1)->op1_use >= 0 &&!value_known(&ctx->values[(ssa_op+1)->op1_use]))) {
2279
0
              return 0;
2280
0
            }
2281
0
            break;
2282
0
          case ZEND_PRE_INC_OBJ:
2283
0
          case ZEND_PRE_DEC_OBJ:
2284
0
          case ZEND_POST_INC_OBJ:
2285
0
          case ZEND_POST_DEC_OBJ:
2286
0
            if (ssa_op->op2_use >= 0 && !value_known(&ctx->values[ssa_op->op2_use])) {
2287
0
              return 0;
2288
0
            }
2289
0
            break;
2290
0
          case ZEND_INIT_ARRAY:
2291
0
          case ZEND_ADD_ARRAY_ELEMENT:
2292
0
            if (opline->op2_type == IS_UNUSED) {
2293
0
              return 0;
2294
0
            }
2295
            /* break missing intentionally */
2296
0
          default:
2297
0
            if (zend_may_throw(opline, ssa_op, op_array, ssa)) {
2298
0
              return 0;
2299
0
            }
2300
0
            break;
2301
0
        }
2302
0
      }
2303
2304
      /* Mark result unused, if possible */
2305
0
      if (ssa_op->result_def >= 0) {
2306
0
        if (ssa->vars[ssa_op->result_def].use_chain < 0
2307
0
            && ssa->vars[ssa_op->result_def].phi_use_chain == NULL) {
2308
0
          zend_ssa_remove_result_def(ssa, ssa_op);
2309
0
          opline->result_type = IS_UNUSED;
2310
0
        } else if (opline->opcode != ZEND_PRE_INC &&
2311
0
            opline->opcode != ZEND_PRE_DEC) {
2312
          /* op1_def and result_def are different */
2313
0
          return removed_ops;
2314
0
        }
2315
0
      }
2316
2317
      /* Destroy previous op2 */
2318
0
      if (opline->op2_type == IS_CONST) {
2319
0
        literal_dtor(&ZEND_OP2_LITERAL(opline));
2320
0
      } else if (ssa_op->op2_use >= 0) {
2321
0
        if (ssa_op->op2_use != ssa_op->op1_use) {
2322
0
          zend_ssa_unlink_use_chain(ssa, var->definition, ssa_op->op2_use);
2323
0
        }
2324
0
        ssa_op->op2_use = -1;
2325
0
        ssa_op->op2_use_chain = -1;
2326
0
      }
2327
2328
      /* Remove OP_DATA opcode */
2329
0
      switch (opline->opcode) {
2330
0
        case ZEND_ASSIGN_DIM:
2331
0
        case ZEND_ASSIGN_OBJ:
2332
0
          removed_ops++;
2333
0
          zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
2334
0
          break;
2335
0
        case ZEND_ASSIGN_DIM_OP:
2336
0
        case ZEND_ASSIGN_OBJ_OP:
2337
0
        case ZEND_ASSIGN_STATIC_PROP_OP:
2338
0
          removed_ops++;
2339
0
          zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
2340
0
          break;
2341
0
        default:
2342
0
          break;
2343
0
      }
2344
2345
0
      if (value) {
2346
        /* Convert to ASSIGN */
2347
0
        opline->opcode = ZEND_ASSIGN;
2348
0
        opline->op2_type = IS_CONST;
2349
0
        opline->op2.constant = zend_optimizer_add_literal(op_array, value);
2350
0
        Z_TRY_ADDREF_P(value);
2351
0
      } else {
2352
        /* Remove dead array or object construction */
2353
0
        removed_ops++;
2354
0
        if (var->use_chain >= 0 || var->phi_use_chain != NULL) {
2355
0
          zend_ssa_rename_var_uses(ssa, ssa_op->op1_def, ssa_op->op1_use, 1);
2356
0
        }
2357
0
        zend_ssa_remove_op1_def(ssa, ssa_op);
2358
0
        zend_ssa_remove_instr(ssa, opline, ssa_op);
2359
0
      }
2360
0
    }
2361
0
  } else if (var->definition_phi
2362
0
      && var->use_chain < 0
2363
0
      && var->phi_use_chain == NULL) {
2364
0
    zend_ssa_remove_phi(ssa, var->definition_phi);
2365
0
  }
2366
0
  return removed_ops;
2367
0
}
2368
2369
/* This will try to replace uses of SSA variables we have determined to be constant. Not all uses
2370
 * can be replaced, because some instructions don't accept constant operands or only accept them
2371
 * if they have a certain type. */
2372
1
static uint32_t replace_constant_operands(sccp_ctx *ctx) {
2373
1
  zend_ssa *ssa = ctx->scdf.ssa;
2374
1
  zend_op_array *op_array = ctx->scdf.op_array;
2375
1
  int i;
2376
1
  zval tmp;
2377
1
  uint32_t removed_ops = 0;
2378
2379
  /* We iterate the variables backwards, so we can eliminate sequences like INIT_ROPE
2380
   * and INIT_ARRAY. */
2381
1
  for (i = ssa->vars_count - 1; i >= op_array->last_var; i--) {
2382
0
    zend_ssa_var *var = &ssa->vars[i];
2383
0
    zval *value;
2384
0
    int use;
2385
2386
0
    if (IS_PARTIAL_ARRAY(&ctx->values[i])
2387
0
        || IS_PARTIAL_OBJECT(&ctx->values[i])) {
2388
0
      if (!Z_DELREF(ctx->values[i])) {
2389
0
        zend_array_destroy(Z_ARR(ctx->values[i]));
2390
0
      }
2391
0
      MAKE_BOT(&ctx->values[i]);
2392
0
      if ((var->use_chain < 0 && var->phi_use_chain == NULL) || var->no_val) {
2393
0
        removed_ops += try_remove_definition(ctx, i, var, NULL);
2394
0
      }
2395
0
      continue;
2396
0
    } else if (value_known(&ctx->values[i])) {
2397
0
      value = &ctx->values[i];
2398
0
    } else {
2399
0
      value = value_from_type_and_range(ctx, i, &tmp);
2400
0
      if (!value) {
2401
0
        continue;
2402
0
      }
2403
0
    }
2404
2405
0
    FOREACH_USE(var, use) {
2406
0
      zend_op *opline = &op_array->opcodes[use];
2407
0
      zend_ssa_op *ssa_op = &ssa->ops[use];
2408
0
      if (try_replace_op1(ctx, opline, ssa_op, i, value)) {
2409
0
        if (opline->opcode == ZEND_NOP) {
2410
0
          removed_ops++;
2411
0
        }
2412
0
        ZEND_ASSERT(ssa_op->op1_def == -1);
2413
0
        if (ssa_op->op1_use != ssa_op->op2_use) {
2414
0
          zend_ssa_unlink_use_chain(ssa, use, ssa_op->op1_use);
2415
0
        } else {
2416
0
          ssa_op->op2_use_chain = ssa_op->op1_use_chain;
2417
0
        }
2418
0
        ssa_op->op1_use = -1;
2419
0
        ssa_op->op1_use_chain = -1;
2420
0
      }
2421
0
      if (try_replace_op2(ctx, opline, ssa_op, i, value)) {
2422
0
        ZEND_ASSERT(ssa_op->op2_def == -1);
2423
0
        if (ssa_op->op2_use != ssa_op->op1_use) {
2424
0
          zend_ssa_unlink_use_chain(ssa, use, ssa_op->op2_use);
2425
0
        }
2426
0
        ssa_op->op2_use = -1;
2427
0
        ssa_op->op2_use_chain = -1;
2428
0
      }
2429
0
    } FOREACH_USE_END();
2430
2431
0
    if (value_known(&ctx->values[i])) {
2432
0
      removed_ops += try_remove_definition(ctx, i, var, value);
2433
0
    }
2434
0
  }
2435
2436
1
  return removed_ops;
2437
1
}
2438
2439
static void sccp_context_init(zend_optimizer_ctx *ctx, sccp_ctx *sccp,
2440
1
    zend_ssa *ssa, zend_op_array *op_array, zend_call_info **call_map) {
2441
1
  int i;
2442
1
  sccp->call_map = call_map;
2443
1
  sccp->values = zend_arena_alloc(&ctx->arena, sizeof(zval) * ssa->vars_count);
2444
2445
1
  MAKE_TOP(&sccp->top);
2446
1
  MAKE_BOT(&sccp->bot);
2447
2448
1
  i = 0;
2449
1
  for (; i < op_array->last_var; ++i) {
2450
    /* These are all undefined variables, which we have to mark BOT.
2451
     * Otherwise the undefined variable warning might not be preserved. */
2452
0
    MAKE_BOT(&sccp->values[i]);
2453
0
  }
2454
1
  for (; i < ssa->vars_count; ++i) {
2455
0
    if (ssa->vars[i].alias) {
2456
0
      MAKE_BOT(&sccp->values[i]);
2457
0
    } else {
2458
0
      MAKE_TOP(&sccp->values[i]);
2459
0
    }
2460
0
  }
2461
1
}
2462
2463
1
static void sccp_context_free(sccp_ctx *sccp) {
2464
1
  int i;
2465
1
  for (i = sccp->scdf.op_array->last_var; i < sccp->scdf.ssa->vars_count; ++i) {
2466
0
    zval_ptr_dtor_nogc(&sccp->values[i]);
2467
0
  }
2468
1
}
2469
2470
uint32_t sccp_optimize_op_array(zend_optimizer_ctx *ctx, zend_op_array *op_array, zend_ssa *ssa, zend_call_info **call_map)
2471
1
{
2472
1
  sccp_ctx sccp;
2473
1
  uint32_t removed_ops = 0;
2474
1
  void *checkpoint = zend_arena_checkpoint(ctx->arena);
2475
2476
1
  sccp_context_init(ctx, &sccp, ssa, op_array, call_map);
2477
2478
1
  sccp.scdf.handlers.visit_instr = sccp_visit_instr;
2479
1
  sccp.scdf.handlers.visit_phi = sccp_visit_phi;
2480
1
  sccp.scdf.handlers.mark_feasible_successors = sccp_mark_feasible_successors;
2481
2482
1
  scdf_init(ctx, &sccp.scdf, op_array, ssa);
2483
1
  scdf_solve(&sccp.scdf, "SCCP");
2484
2485
1
  if (ctx->debug_level & ZEND_DUMP_SCCP) {
2486
0
    int i, first = 1;
2487
2488
0
    for (i = op_array->last_var; i < ssa->vars_count; i++) {
2489
0
      zval *zv = &sccp.values[i];
2490
2491
0
      if (IS_TOP(zv) || IS_BOT(zv)) {
2492
0
        continue;
2493
0
      }
2494
0
      if (first) {
2495
0
        first = 0;
2496
0
        fprintf(stderr, "\nSCCP Values for \"");
2497
0
        zend_dump_op_array_name(op_array);
2498
0
        fprintf(stderr, "\":\n");
2499
0
      }
2500
0
      fprintf(stderr, "    #%d.", i);
2501
0
      zend_dump_var(op_array, IS_CV, ssa->vars[i].var);
2502
0
      fprintf(stderr, " =");
2503
0
      scp_dump_value(zv);
2504
0
      fprintf(stderr, "\n");
2505
0
    }
2506
0
  }
2507
2508
1
  removed_ops += scdf_remove_unreachable_blocks(&sccp.scdf);
2509
1
  removed_ops += replace_constant_operands(&sccp);
2510
2511
1
  sccp_context_free(&sccp);
2512
1
  zend_arena_release(&ctx->arena, checkpoint);
2513
2514
1
  return removed_ops;
2515
1
}