1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine, e-SSA based Type & Range Inference |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Dmitry Stogov <dmitry@zend.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include "php.h"
20 #include "zend_compile.h"
21 #include "zend_generators.h"
22 #include "zend_inference.h"
23 #include "zend_func_info.h"
24 #include "zend_call_graph.h"
25 #include "zend_worklist.h"
26
27 /* The used range inference algorithm is described in:
28 * V. Campos, R. Rodrigues, I. de Assis Costa and F. Pereira.
29 * "Speed and Precision in Range Analysis", SBLP'12.
30 *
31 * There are a couple degrees of freedom, we use:
32 * * Propagation on SCCs.
33 * * e-SSA for live range splitting.
34 * * Only intra-procedural inference.
35 * * Widening with warmup passes, but without jump sets.
36 */
37
38 /* Whether to handle symbolic range constraints */
39 #define SYM_RANGE
40
41 /* Whether to handle negative range constraints */
42 /* Negative range inference is buggy, so disabled for now */
43 #undef NEG_RANGE
44
45 /* Number of warmup passes to use prior to widening */
46 #define RANGE_WARMUP_PASSES 16
47
48 /* Logging for range inference in general */
49 #if 0
50 #define LOG_SSA_RANGE(...) fprintf(stderr, __VA_ARGS__)
51 #else
52 #define LOG_SSA_RANGE(...)
53 #endif
54
55 /* Logging for negative range constraints */
56 #if 0
57 #define LOG_NEG_RANGE(...) fprintf(stderr, __VA_ARGS__)
58 #else
59 #define LOG_NEG_RANGE(...)
60 #endif
61
62 /* Pop elements in unspecified order from worklist until it is empty */
63 #define WHILE_WORKLIST(worklist, len, i) do { \
64 zend_bool _done = 0; \
65 while (!_done) { \
66 _done = 1; \
67 ZEND_BITSET_FOREACH(worklist, len, i) { \
68 zend_bitset_excl(worklist, i); \
69 _done = 0;
70
71 #define WHILE_WORKLIST_END() \
72 } ZEND_BITSET_FOREACH_END(); \
73 } \
74 } while (0)
75
76 #define CHECK_SCC_VAR(var2) \
77 do { \
78 if (!ssa->vars[var2].no_val) { \
79 if (dfs[var2] < 0) { \
80 zend_ssa_check_scc_var(op_array, ssa, var2, index, dfs, root, stack); \
81 } \
82 if (ssa->vars[var2].scc < 0 && dfs[root[var]] >= dfs[root[var2]]) { \
83 root[var] = root[var2]; \
84 } \
85 } \
86 } while (0)
87
88 #define CHECK_SCC_ENTRY(var2) \
89 do { \
90 if (ssa->vars[var2].scc != ssa->vars[var].scc) { \
91 ssa->vars[var2].scc_entry = 1; \
92 } \
93 } while (0)
94
95 #define ADD_SCC_VAR(_var) \
96 do { \
97 if (ssa->vars[_var].scc == scc) { \
98 zend_bitset_incl(worklist, _var); \
99 } \
100 } while (0)
101
102 #define ADD_SCC_VAR_1(_var) \
103 do { \
104 if (ssa->vars[_var].scc == scc && \
105 !zend_bitset_in(visited, _var)) { \
106 zend_bitset_incl(worklist, _var); \
107 } \
108 } while (0)
109
110 #define FOR_EACH_DEFINED_VAR(line, MACRO) \
111 do { \
112 if (ssa->ops[line].op1_def >= 0) { \
113 MACRO(ssa->ops[line].op1_def); \
114 } \
115 if (ssa->ops[line].op2_def >= 0) { \
116 MACRO(ssa->ops[line].op2_def); \
117 } \
118 if (ssa->ops[line].result_def >= 0) { \
119 MACRO(ssa->ops[line].result_def); \
120 } \
121 if (op_array->opcodes[line].opcode == ZEND_OP_DATA) { \
122 if (ssa->ops[line-1].op1_def >= 0) { \
123 MACRO(ssa->ops[line-1].op1_def); \
124 } \
125 if (ssa->ops[line-1].op2_def >= 0) { \
126 MACRO(ssa->ops[line-1].op2_def); \
127 } \
128 if (ssa->ops[line-1].result_def >= 0) { \
129 MACRO(ssa->ops[line-1].result_def); \
130 } \
131 } else if ((uint32_t)line+1 < op_array->last && \
132 op_array->opcodes[line+1].opcode == ZEND_OP_DATA) { \
133 if (ssa->ops[line+1].op1_def >= 0) { \
134 MACRO(ssa->ops[line+1].op1_def); \
135 } \
136 if (ssa->ops[line+1].op2_def >= 0) { \
137 MACRO(ssa->ops[line+1].op2_def); \
138 } \
139 if (ssa->ops[line+1].result_def >= 0) { \
140 MACRO(ssa->ops[line+1].result_def); \
141 } \
142 } \
143 } while (0)
144
145
146 #define FOR_EACH_VAR_USAGE(_var, MACRO) \
147 do { \
148 zend_ssa_phi *p = ssa->vars[_var].phi_use_chain; \
149 int use = ssa->vars[_var].use_chain; \
150 while (use >= 0) { \
151 FOR_EACH_DEFINED_VAR(use, MACRO); \
152 use = zend_ssa_next_use(ssa->ops, _var, use); \
153 } \
154 p = ssa->vars[_var].phi_use_chain; \
155 while (p) { \
156 MACRO(p->ssa_var); \
157 p = zend_ssa_next_use_phi(ssa, _var, p); \
158 } \
159 } while (0)
160
add_will_overflow(zend_long a,zend_long b)161 static inline zend_bool add_will_overflow(zend_long a, zend_long b) {
162 return (b > 0 && a > ZEND_LONG_MAX - b)
163 || (b < 0 && a < ZEND_LONG_MIN - b);
164 }
165 #if 0
166 static inline zend_bool sub_will_overflow(zend_long a, zend_long b) {
167 return (b > 0 && a < ZEND_LONG_MIN + b)
168 || (b < 0 && a > ZEND_LONG_MAX + b);
169 }
170 #endif
171
zend_ssa_check_scc_var(const zend_op_array * op_array,zend_ssa * ssa,int var,int * index,int * dfs,int * root,zend_worklist_stack * stack)172 static void zend_ssa_check_scc_var(const zend_op_array *op_array, zend_ssa *ssa, int var, int *index, int *dfs, int *root, zend_worklist_stack *stack) /* {{{ */
173 {
174 #ifdef SYM_RANGE
175 zend_ssa_phi *p;
176 #endif
177
178 dfs[var] = *index;
179 (*index)++;
180 root[var] = var;
181
182 FOR_EACH_VAR_USAGE(var, CHECK_SCC_VAR);
183
184 #ifdef SYM_RANGE
185 /* Process symbolic control-flow constraints */
186 p = ssa->vars[var].sym_use_chain;
187 while (p) {
188 CHECK_SCC_VAR(p->ssa_var);
189 p = p->sym_use_chain;
190 }
191 #endif
192
193 if (root[var] == var) {
194 ssa->vars[var].scc = ssa->sccs;
195 while (stack->len > 0) {
196 int var2 = zend_worklist_stack_peek(stack);
197 if (dfs[var2] <= dfs[var]) {
198 break;
199 }
200 zend_worklist_stack_pop(stack);
201 ssa->vars[var2].scc = ssa->sccs;
202 }
203 ssa->sccs++;
204 } else {
205 zend_worklist_stack_push(stack, var);
206 }
207 }
208 /* }}} */
209
zend_ssa_find_sccs(const zend_op_array * op_array,zend_ssa * ssa)210 int zend_ssa_find_sccs(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
211 {
212 int index = 0, *dfs, *root;
213 zend_worklist_stack stack;
214 int j;
215 ALLOCA_FLAG(dfs_use_heap)
216 ALLOCA_FLAG(root_use_heap)
217 ALLOCA_FLAG(stack_use_heap)
218
219 dfs = do_alloca(sizeof(int) * ssa->vars_count, dfs_use_heap);
220 memset(dfs, -1, sizeof(int) * ssa->vars_count);
221 root = do_alloca(sizeof(int) * ssa->vars_count, root_use_heap);
222 ZEND_WORKLIST_STACK_ALLOCA(&stack, ssa->vars_count, stack_use_heap);
223
224 /* Find SCCs using Tarjan's algorithm. */
225 for (j = 0; j < ssa->vars_count; j++) {
226 if (!ssa->vars[j].no_val && dfs[j] < 0) {
227 zend_ssa_check_scc_var(op_array, ssa, j, &index, dfs, root, &stack);
228 }
229 }
230
231 /* Revert SCC order. This results in a topological order. */
232 for (j = 0; j < ssa->vars_count; j++) {
233 if (ssa->vars[j].scc >= 0) {
234 ssa->vars[j].scc = ssa->sccs - (ssa->vars[j].scc + 1);
235 }
236 }
237
238 for (j = 0; j < ssa->vars_count; j++) {
239 if (ssa->vars[j].scc >= 0) {
240 int var = j;
241 if (root[j] == j) {
242 ssa->vars[j].scc_entry = 1;
243 }
244 FOR_EACH_VAR_USAGE(var, CHECK_SCC_ENTRY);
245 }
246 }
247
248 ZEND_WORKLIST_STACK_FREE_ALLOCA(&stack, stack_use_heap);
249 free_alloca(root, root_use_heap);
250 free_alloca(dfs, dfs_use_heap);
251
252 return SUCCESS;
253 }
254 /* }}} */
255
zend_ssa_find_false_dependencies(const zend_op_array * op_array,zend_ssa * ssa)256 int zend_ssa_find_false_dependencies(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
257 {
258 zend_ssa_var *ssa_vars = ssa->vars;
259 zend_ssa_op *ssa_ops = ssa->ops;
260 int ssa_vars_count = ssa->vars_count;
261 zend_bitset worklist;
262 int i, j, use;
263 zend_ssa_phi *p;
264 ALLOCA_FLAG(use_heap);
265
266 if (!op_array->function_name || !ssa->vars || !ssa->ops) {
267 return SUCCESS;
268 }
269
270 worklist = do_alloca(sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count), use_heap);
271 memset(worklist, 0, sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count));
272
273 for (i = 0; i < ssa_vars_count; i++) {
274 ssa_vars[i].no_val = 1; /* mark as unused */
275 use = ssa->vars[i].use_chain;
276 while (use >= 0) {
277 if (!zend_ssa_is_no_val_use(&op_array->opcodes[use], &ssa->ops[use], i)) {
278 ssa_vars[i].no_val = 0; /* used directly */
279 zend_bitset_incl(worklist, i);
280 break;
281 }
282 use = zend_ssa_next_use(ssa_ops, i, use);
283 }
284 }
285
286 WHILE_WORKLIST(worklist, zend_bitset_len(ssa_vars_count), i) {
287 if (ssa_vars[i].definition_phi) {
288 /* mark all possible sources as used */
289 p = ssa_vars[i].definition_phi;
290 if (p->pi >= 0) {
291 if (ssa_vars[p->sources[0]].no_val) {
292 ssa_vars[p->sources[0]].no_val = 0; /* used indirectly */
293 zend_bitset_incl(worklist, p->sources[0]);
294 }
295 } else {
296 for (j = 0; j < ssa->cfg.blocks[p->block].predecessors_count; j++) {
297 ZEND_ASSERT(p->sources[j] >= 0);
298 if (ssa->vars[p->sources[j]].no_val) {
299 ssa_vars[p->sources[j]].no_val = 0; /* used indirectly */
300 zend_bitset_incl(worklist, p->sources[j]);
301 }
302 }
303 }
304 }
305 } WHILE_WORKLIST_END();
306
307 free_alloca(worklist, use_heap);
308
309 return SUCCESS;
310 }
311 /* }}} */
312
313 /* From "Hacker's Delight" */
minOR(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)314 zend_ulong minOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
315 {
316 zend_ulong m, temp;
317
318 m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
319 while (m != 0) {
320 if (~a & c & m) {
321 temp = (a | m) & -m;
322 if (temp <= b) {
323 a = temp;
324 break;
325 }
326 } else if (a & ~c & m) {
327 temp = (c | m) & -m;
328 if (temp <= d) {
329 c = temp;
330 break;
331 }
332 }
333 m = m >> 1;
334 }
335 return a | c;
336 }
337
maxOR(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)338 zend_ulong maxOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
339 {
340 zend_ulong m, temp;
341
342 m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
343 while (m != 0) {
344 if (b & d & m) {
345 temp = (b - m) | (m - 1);
346 if (temp >= a) {
347 b = temp;
348 break;
349 }
350 temp = (d - m) | (m - 1);
351 if (temp >= c) {
352 d = temp;
353 break;
354 }
355 }
356 m = m >> 1;
357 }
358 return b | d;
359 }
360
minAND(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)361 zend_ulong minAND(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
362 {
363 zend_ulong m, temp;
364
365 m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
366 while (m != 0) {
367 if (~a & ~c & m) {
368 temp = (a | m) & -m;
369 if (temp <= b) {
370 a = temp;
371 break;
372 }
373 temp = (c | m) & -m;
374 if (temp <= d) {
375 c = temp;
376 break;
377 }
378 }
379 m = m >> 1;
380 }
381 return a & c;
382 }
383
maxAND(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)384 zend_ulong maxAND(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
385 {
386 zend_ulong m, temp;
387
388 m = Z_UL(1) << (sizeof(zend_ulong) * 8 - 1);
389 while (m != 0) {
390 if (b & ~d & m) {
391 temp = (b | ~m) | (m - 1);
392 if (temp >= a) {
393 b = temp;
394 break;
395 }
396 } else if (~b & d & m) {
397 temp = (d | ~m) | (m - 1);
398 if (temp >= c) {
399 d = temp;
400 break;
401 }
402 }
403 m = m >> 1;
404 }
405 return b & d;
406 }
407
minXOR(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)408 zend_ulong minXOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
409 {
410 return minAND(a, b, ~d, ~c) | minAND(~b, ~a, c, d);
411 }
412
maxXOR(zend_ulong a,zend_ulong b,zend_ulong c,zend_ulong d)413 zend_ulong maxXOR(zend_ulong a, zend_ulong b, zend_ulong c, zend_ulong d)
414 {
415 return maxOR(0, maxAND(a, b, ~d, ~c), 0, maxAND(~b, ~a, c, d));
416 }
417
418 /* Based on "Hacker's Delight" */
419
420 /*
421 0: + + + + 0 0 0 0 => 0 0 + min/max
422 2: + + - + 0 0 1 0 => 1 0 ? min(a,b,c,-1)/max(a,b,0,d)
423 3: + + - - 0 0 1 1 => 1 1 - min/max
424 8: - + + + 1 0 0 0 => 1 0 ? min(a,-1,b,d)/max(0,b,c,d)
425 a: - + - + 1 0 1 0 => 1 0 ? MIN(a,c)/max(0,b,0,d)
426 b: - + - - 1 0 1 1 => 1 1 - c/-1
427 c: - - + + 1 1 0 0 => 1 1 - min/max
428 e: - - - + 1 1 1 0 => 1 1 - a/-1
429 f - - - - 1 1 1 1 => 1 1 - min/max
430 */
zend_ssa_range_or(zend_long a,zend_long b,zend_long c,zend_long d,zend_ssa_range * tmp)431 static void zend_ssa_range_or(zend_long a, zend_long b, zend_long c, zend_long d, zend_ssa_range *tmp)
432 {
433 int x = ((a < 0) ? 8 : 0) |
434 ((b < 0) ? 4 : 0) |
435 ((c < 0) ? 2 : 0) |
436 ((d < 0) ? 2 : 0);
437 switch (x) {
438 case 0x0:
439 case 0x3:
440 case 0xc:
441 case 0xf:
442 tmp->min = minOR(a, b, c, d);
443 tmp->max = maxOR(a, b, c, d);
444 break;
445 case 0x2:
446 tmp->min = minOR(a, b, c, -1);
447 tmp->max = maxOR(a, b, 0, d);
448 break;
449 case 0x8:
450 tmp->min = minOR(a, -1, c, d);
451 tmp->max = maxOR(0, b, c, d);
452 break;
453 case 0xa:
454 tmp->min = MIN(a, c);
455 tmp->max = maxOR(0, b, 0, d);
456 break;
457 case 0xb:
458 tmp->min = c;
459 tmp->max = -1;
460 break;
461 case 0xe:
462 tmp->min = a;
463 tmp->max = -1;
464 break;
465 }
466 }
467
468 /*
469 0: + + + + 0 0 0 0 => 0 0 + min/max
470 2: + + - + 0 0 1 0 => 0 0 + 0/b
471 3: + + - - 0 0 1 1 => 0 0 + min/max
472 8: - + + + 1 0 0 0 => 0 0 + 0/d
473 a: - + - + 1 0 1 0 => 1 0 ? min(a,-1,c,-1)/NAX(b,d)
474 b: - + - - 1 0 1 1 => 1 0 ? min(a,-1,c,d)/max(0,b,c,d)
475 c: - - + + 1 1 0 0 => 1 1 - min/max
476 e: - - - + 1 1 1 0 => 1 0 ? min(a,b,c,-1)/max(a,b,0,d)
477 f - - - - 1 1 1 1 => 1 1 - min/max
478 */
zend_ssa_range_and(zend_long a,zend_long b,zend_long c,zend_long d,zend_ssa_range * tmp)479 static void zend_ssa_range_and(zend_long a, zend_long b, zend_long c, zend_long d, zend_ssa_range *tmp)
480 {
481 int x = ((a < 0) ? 8 : 0) |
482 ((b < 0) ? 4 : 0) |
483 ((c < 0) ? 2 : 0) |
484 ((d < 0) ? 2 : 0);
485 switch (x) {
486 case 0x0:
487 case 0x3:
488 case 0xc:
489 case 0xf:
490 tmp->min = minAND(a, b, c, d);
491 tmp->max = maxAND(a, b, c, d);
492 break;
493 case 0x2:
494 tmp->min = 0;
495 tmp->max = b;
496 break;
497 case 0x8:
498 tmp->min = 0;
499 tmp->max = d;
500 break;
501 case 0xa:
502 tmp->min = minAND(a, -1, c, -1);
503 tmp->max = MAX(b, d);
504 break;
505 case 0xb:
506 tmp->min = minAND(a, -1, c, d);
507 tmp->max = maxAND(0, b, c, d);
508 break;
509 case 0xe:
510 tmp->min = minAND(a, b, c, -1);
511 tmp->max = maxAND(a, b, 0, d);
512 break;
513 }
514 }
515
zend_abs_range(zend_long min,zend_long max,zend_long * abs_min,zend_long * abs_max)516 static inline zend_bool zend_abs_range(
517 zend_long min, zend_long max, zend_long *abs_min, zend_long *abs_max) {
518 if (min == ZEND_LONG_MIN) {
519 /* Cannot take absolute value of LONG_MIN */
520 return 0;
521 }
522
523 if (min >= 0) {
524 *abs_min = min;
525 *abs_max = max;
526 } else if (max <= 0) {
527 *abs_min = -max;
528 *abs_max = -min;
529 } else {
530 /* Range crossing zero */
531 *abs_min = 0;
532 *abs_max = MAX(max, -min);
533 }
534
535 return 1;
536 }
537
shift_left_overflows(zend_long n,zend_long s)538 static inline zend_bool shift_left_overflows(zend_long n, zend_long s) {
539 /* This considers shifts that shift in the sign bit to be overflowing as well */
540 if (n >= 0) {
541 return s >= SIZEOF_ZEND_LONG * 8 - 1 || (n << s) < n;
542 } else {
543 return s >= SIZEOF_ZEND_LONG * 8 - 1 || (n << s) > n;
544 }
545 }
546
547 /* Get the normal op corresponding to a compound assignment op */
get_compound_assign_op(zend_uchar opcode)548 static inline zend_uchar get_compound_assign_op(zend_uchar opcode) {
549 switch (opcode) {
550 case ZEND_ASSIGN_ADD: return ZEND_ADD;
551 case ZEND_ASSIGN_SUB: return ZEND_SUB;
552 case ZEND_ASSIGN_MUL: return ZEND_MUL;
553 case ZEND_ASSIGN_DIV: return ZEND_DIV;
554 case ZEND_ASSIGN_MOD: return ZEND_MOD;
555 case ZEND_ASSIGN_SL: return ZEND_SL;
556 case ZEND_ASSIGN_SR: return ZEND_SR;
557 case ZEND_ASSIGN_CONCAT: return ZEND_CONCAT;
558 case ZEND_ASSIGN_BW_OR: return ZEND_BW_OR;
559 case ZEND_ASSIGN_BW_AND: return ZEND_BW_AND;
560 case ZEND_ASSIGN_BW_XOR: return ZEND_BW_XOR;
561 case ZEND_ASSIGN_POW: return ZEND_POW;
562 EMPTY_SWITCH_DEFAULT_CASE()
563 }
564 }
565
zend_inference_calc_binary_op_range(const zend_op_array * op_array,zend_ssa * ssa,zend_op * opline,zend_ssa_op * ssa_op,zend_uchar opcode,zend_ssa_range * tmp)566 static int zend_inference_calc_binary_op_range(
567 const zend_op_array *op_array, zend_ssa *ssa,
568 zend_op *opline, zend_ssa_op *ssa_op, zend_uchar opcode, zend_ssa_range *tmp) {
569 zend_long op1_min, op2_min, op1_max, op2_max, t1, t2, t3, t4;
570
571 switch (opcode) {
572 case ZEND_ADD:
573 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
574 op1_min = OP1_MIN_RANGE();
575 op2_min = OP2_MIN_RANGE();
576 op1_max = OP1_MAX_RANGE();
577 op2_max = OP2_MAX_RANGE();
578 tmp->min = op1_min + op2_min;
579 tmp->max = op1_max + op2_max;
580 if (OP1_RANGE_UNDERFLOW() ||
581 OP2_RANGE_UNDERFLOW() ||
582 (op1_min < 0 && op2_min < 0 && tmp->min >= 0)) {
583 tmp->underflow = 1;
584 tmp->min = ZEND_LONG_MIN;
585 }
586 if (OP1_RANGE_OVERFLOW() ||
587 OP2_RANGE_OVERFLOW() ||
588 (op1_max > 0 && op2_max > 0 && tmp->max <= 0)) {
589 tmp->overflow = 1;
590 tmp->max = ZEND_LONG_MAX;
591 }
592 return 1;
593 }
594 break;
595 case ZEND_SUB:
596 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
597 op1_min = OP1_MIN_RANGE();
598 op2_min = OP2_MIN_RANGE();
599 op1_max = OP1_MAX_RANGE();
600 op2_max = OP2_MAX_RANGE();
601 tmp->min = op1_min - op2_max;
602 tmp->max = op1_max - op2_min;
603 if (OP1_RANGE_UNDERFLOW() ||
604 OP2_RANGE_OVERFLOW() ||
605 (op1_min < 0 && op2_max > 0 && tmp->min >= 0)) {
606 tmp->underflow = 1;
607 tmp->min = ZEND_LONG_MIN;
608 }
609 if (OP1_RANGE_OVERFLOW() ||
610 OP2_RANGE_UNDERFLOW() ||
611 (op1_max > 0 && op2_min < 0 && tmp->max <= 0)) {
612 tmp->overflow = 1;
613 tmp->max = ZEND_LONG_MAX;
614 }
615 return 1;
616 }
617 break;
618 case ZEND_MUL:
619 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
620 op1_min = OP1_MIN_RANGE();
621 op2_min = OP2_MIN_RANGE();
622 op1_max = OP1_MAX_RANGE();
623 op2_max = OP2_MAX_RANGE();
624 t1 = op1_min * op2_min;
625 t2 = op1_min * op2_max;
626 t3 = op1_max * op2_min;
627 t4 = op1_max * op2_max;
628 // FIXME: more careful overflow checks?
629 if (OP1_RANGE_UNDERFLOW() ||
630 OP2_RANGE_UNDERFLOW() ||
631 OP1_RANGE_OVERFLOW() ||
632 OP2_RANGE_OVERFLOW() ||
633 (double)t1 != (double)op1_min * (double)op2_min ||
634 (double)t2 != (double)op1_min * (double)op2_max ||
635 (double)t3 != (double)op1_max * (double)op2_min ||
636 (double)t4 != (double)op1_max * (double)op2_max) {
637 tmp->underflow = 1;
638 tmp->overflow = 1;
639 tmp->min = ZEND_LONG_MIN;
640 tmp->max = ZEND_LONG_MAX;
641 } else {
642 tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
643 tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
644 }
645 return 1;
646 }
647 break;
648 case ZEND_DIV:
649 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
650 op1_min = OP1_MIN_RANGE();
651 op2_min = OP2_MIN_RANGE();
652 op1_max = OP1_MAX_RANGE();
653 op2_max = OP2_MAX_RANGE();
654 if (op2_min <= 0 && op2_max >= 0) {
655 break;
656 }
657 if (op1_min == ZEND_LONG_MIN && op2_max == -1) {
658 /* Avoid ill-defined division, which may trigger SIGFPE. */
659 break;
660 }
661 t1 = op1_min / op2_min;
662 t2 = op1_min / op2_max;
663 t3 = op1_max / op2_min;
664 t4 = op1_max / op2_max;
665 // FIXME: more careful overflow checks?
666 if (OP1_RANGE_UNDERFLOW() ||
667 OP2_RANGE_UNDERFLOW() ||
668 OP1_RANGE_OVERFLOW() ||
669 OP2_RANGE_OVERFLOW() ||
670 t1 != (zend_long)((double)op1_min / (double)op2_min) ||
671 t2 != (zend_long)((double)op1_min / (double)op2_max) ||
672 t3 != (zend_long)((double)op1_max / (double)op2_min) ||
673 t4 != (zend_long)((double)op1_max / (double)op2_max)) {
674 tmp->underflow = 1;
675 tmp->overflow = 1;
676 tmp->min = ZEND_LONG_MIN;
677 tmp->max = ZEND_LONG_MAX;
678 } else {
679 tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
680 tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
681 }
682 return 1;
683 }
684 break;
685 case ZEND_MOD:
686 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
687 if (OP1_RANGE_UNDERFLOW() ||
688 OP2_RANGE_UNDERFLOW() ||
689 OP1_RANGE_OVERFLOW() ||
690 OP2_RANGE_OVERFLOW()) {
691 tmp->min = ZEND_LONG_MIN;
692 tmp->max = ZEND_LONG_MAX;
693 } else {
694 zend_long op2_abs_min, op2_abs_max;
695
696 op1_min = OP1_MIN_RANGE();
697 op2_min = OP2_MIN_RANGE();
698 op1_max = OP1_MAX_RANGE();
699 op2_max = OP2_MAX_RANGE();
700 if (!zend_abs_range(op2_min, op2_max, &op2_abs_min, &op2_abs_max)) {
701 break;
702 }
703
704 if (op2_abs_max == 0) {
705 /* Always modulus by zero, nothing we can do */
706 break;
707 }
708 if (op2_abs_min == 0) {
709 /* Ignore the modulus by zero case, which will throw */
710 op2_abs_min++;
711 }
712
713 if (op1_min >= 0) {
714 tmp->min = op1_max < op2_abs_min ? op1_min : 0;
715 tmp->max = MIN(op1_max, op2_abs_max - 1);
716 } else if (op1_max <= 0) {
717 tmp->min = MAX(op1_min, -op2_abs_max + 1);
718 tmp->max = op1_min > -op2_abs_min ? op1_max : 0;
719 } else {
720 tmp->min = MAX(op1_min, -op2_abs_max + 1);
721 tmp->max = MIN(op1_max, op2_abs_max - 1);
722 }
723 }
724 return 1;
725 }
726 break;
727 case ZEND_SL:
728 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
729 if (OP1_RANGE_UNDERFLOW() ||
730 OP2_RANGE_UNDERFLOW() ||
731 OP1_RANGE_OVERFLOW() ||
732 OP2_RANGE_OVERFLOW()) {
733 tmp->min = ZEND_LONG_MIN;
734 tmp->max = ZEND_LONG_MAX;
735 } else {
736 op1_min = OP1_MIN_RANGE();
737 op2_min = OP2_MIN_RANGE();
738 op1_max = OP1_MAX_RANGE();
739 op2_max = OP2_MAX_RANGE();
740
741 /* Shifts by negative numbers will throw, ignore them */
742 if (op2_min < 0) {
743 op2_min = 0;
744 }
745 if (op2_max < 0) {
746 op2_max = 0;
747 }
748
749 if (shift_left_overflows(op1_min, op2_max)
750 || shift_left_overflows(op1_max, op2_max)) {
751 tmp->min = ZEND_LONG_MIN;
752 tmp->max = ZEND_LONG_MAX;
753 } else {
754 t1 = op1_min << op2_min;
755 t2 = op1_min << op2_max;
756 t3 = op1_max << op2_min;
757 t4 = op1_max << op2_max;
758 tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
759 tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
760 }
761 }
762 return 1;
763 }
764 break;
765 case ZEND_SR:
766 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
767 if (OP1_RANGE_UNDERFLOW() ||
768 OP2_RANGE_UNDERFLOW() ||
769 OP1_RANGE_OVERFLOW() ||
770 OP2_RANGE_OVERFLOW()) {
771 tmp->min = ZEND_LONG_MIN;
772 tmp->max = ZEND_LONG_MAX;
773 } else {
774 op1_min = OP1_MIN_RANGE();
775 op2_min = OP2_MIN_RANGE();
776 op1_max = OP1_MAX_RANGE();
777 op2_max = OP2_MAX_RANGE();
778
779 /* Shifts by negative numbers will throw, ignore them */
780 if (op2_min < 0) {
781 op2_min = 0;
782 }
783 if (op2_max < 0) {
784 op2_max = 0;
785 }
786
787 /* Shifts by more than the integer size will be 0 or -1 */
788 if (op2_min >= SIZEOF_ZEND_LONG * 8) {
789 op2_min = SIZEOF_ZEND_LONG * 8 - 1;
790 }
791 if (op2_max >= SIZEOF_ZEND_LONG * 8) {
792 op2_max = SIZEOF_ZEND_LONG * 8 - 1;
793 }
794
795 t1 = op1_min >> op2_min;
796 t2 = op1_min >> op2_max;
797 t3 = op1_max >> op2_min;
798 t4 = op1_max >> op2_max;
799 tmp->min = MIN(MIN(t1, t2), MIN(t3, t4));
800 tmp->max = MAX(MAX(t1, t2), MAX(t3, t4));
801 }
802 return 1;
803 }
804 break;
805 case ZEND_BW_OR:
806 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
807 if (OP1_RANGE_UNDERFLOW() ||
808 OP2_RANGE_UNDERFLOW() ||
809 OP1_RANGE_OVERFLOW() ||
810 OP2_RANGE_OVERFLOW()) {
811 tmp->min = ZEND_LONG_MIN;
812 tmp->max = ZEND_LONG_MAX;
813 } else {
814 op1_min = OP1_MIN_RANGE();
815 op2_min = OP2_MIN_RANGE();
816 op1_max = OP1_MAX_RANGE();
817 op2_max = OP2_MAX_RANGE();
818 zend_ssa_range_or(op1_min, op1_max, op2_min, op2_max, tmp);
819 }
820 return 1;
821 }
822 break;
823 case ZEND_BW_AND:
824 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
825 if (OP1_RANGE_UNDERFLOW() ||
826 OP2_RANGE_UNDERFLOW() ||
827 OP1_RANGE_OVERFLOW() ||
828 OP2_RANGE_OVERFLOW()) {
829 tmp->min = ZEND_LONG_MIN;
830 tmp->max = ZEND_LONG_MAX;
831 } else {
832 op1_min = OP1_MIN_RANGE();
833 op2_min = OP2_MIN_RANGE();
834 op1_max = OP1_MAX_RANGE();
835 op2_max = OP2_MAX_RANGE();
836 zend_ssa_range_and(op1_min, op1_max, op2_min, op2_max, tmp);
837 }
838 return 1;
839 }
840 break;
841 case ZEND_BW_XOR:
842 // TODO
843 break;
844 EMPTY_SWITCH_DEFAULT_CASE()
845 }
846 return 0;
847 }
848
zend_inference_calc_range(const zend_op_array * op_array,zend_ssa * ssa,int var,int widening,int narrowing,zend_ssa_range * tmp)849 int zend_inference_calc_range(const zend_op_array *op_array, zend_ssa *ssa, int var, int widening, int narrowing, zend_ssa_range *tmp)
850 {
851 uint32_t line;
852 zend_op *opline;
853 zend_long op1_min, op2_min, op1_max, op2_max;
854
855 if (ssa->vars[var].definition_phi) {
856 zend_ssa_phi *p = ssa->vars[var].definition_phi;
857 int i;
858
859 tmp->underflow = 0;
860 tmp->min = ZEND_LONG_MAX;
861 tmp->max = ZEND_LONG_MIN;
862 tmp->overflow = 0;
863 if (p->pi >= 0 && p->has_range_constraint) {
864 zend_ssa_range_constraint *constraint = &p->constraint.range;
865 if (constraint->negative) {
866 if (ssa->var_info[p->sources[0]].has_range) {
867 *tmp = ssa->var_info[p->sources[0]].range;
868 } else if (narrowing) {
869 tmp->underflow = 1;
870 tmp->min = ZEND_LONG_MIN;
871 tmp->max = ZEND_LONG_MAX;
872 tmp->overflow = 1;
873 }
874
875 #ifdef NEG_RANGE
876 if (constraint->min_ssa_var < 0 &&
877 constraint->max_ssa_var < 0 &&
878 ssa->var_info[p->ssa_var].has_range) {
879 LOG_NEG_RANGE("%s() #%d [%ld..%ld] -> [%ld..%ld]?\n",
880 ZSTR_VAL(op_array->function_name),
881 p->ssa_var,
882 ssa->var_info[p->ssa_var].range.min,
883 ssa->var_info[p->ssa_var].range.max,
884 tmp->min,
885 tmp->max);
886 if (constraint->negative == NEG_USE_LT &&
887 tmp->max >= constraint->range.min) {
888 tmp->overflow = 0;
889 tmp->max = constraint->range.min - 1;
890 LOG_NEG_RANGE(" => [%ld..%ld]\n", tmp->min, tmp->max);
891 } else if (constraint->negative == NEG_USE_GT &&
892 tmp->min <= constraint->range.max) {
893 tmp->underflow = 0;
894 tmp->min = constraint->range.max + 1;
895 LOG_NEG_RANGE(" => [%ld..%ld]\n", tmp->min, tmp->max);
896 }
897 }
898 #endif
899 } else if (ssa->var_info[p->sources[0]].has_range) {
900 /* intersection */
901 *tmp = ssa->var_info[p->sources[0]].range;
902 if (constraint->min_ssa_var < 0) {
903 tmp->underflow = constraint->range.underflow && tmp->underflow;
904 tmp->min = MAX(constraint->range.min, tmp->min);
905 #ifdef SYM_RANGE
906 } else if (narrowing && ssa->var_info[constraint->min_ssa_var].has_range) {
907 tmp->underflow = ssa->var_info[constraint->min_ssa_var].range.underflow && tmp->underflow;
908 if (!add_will_overflow(ssa->var_info[constraint->min_ssa_var].range.min, constraint->range.min)) {
909 tmp->min = MAX(ssa->var_info[constraint->min_ssa_var].range.min + constraint->range.min, tmp->min);
910 }
911 #endif
912 }
913 if (constraint->max_ssa_var < 0) {
914 tmp->max = MIN(constraint->range.max, tmp->max);
915 tmp->overflow = constraint->range.overflow && tmp->overflow;
916 #ifdef SYM_RANGE
917 } else if (narrowing && ssa->var_info[constraint->max_ssa_var].has_range) {
918 if (!add_will_overflow(ssa->var_info[constraint->max_ssa_var].range.max, constraint->range.max)) {
919 tmp->max = MIN(ssa->var_info[constraint->max_ssa_var].range.max + constraint->range.max, tmp->max);
920 }
921 tmp->overflow = ssa->var_info[constraint->max_ssa_var].range.overflow && tmp->overflow;
922 #endif
923 }
924 } else if (narrowing) {
925 if (constraint->min_ssa_var < 0) {
926 tmp->underflow = constraint->range.underflow;
927 tmp->min = constraint->range.min;
928 #ifdef SYM_RANGE
929 } else if (narrowing && ssa->var_info[constraint->min_ssa_var].has_range) {
930 if (add_will_overflow(ssa->var_info[constraint->min_ssa_var].range.min, constraint->range.min)) {
931 tmp->underflow = 1;
932 tmp->min = ZEND_LONG_MIN;
933 } else {
934 tmp->underflow = ssa->var_info[constraint->min_ssa_var].range.underflow;
935 tmp->min = ssa->var_info[constraint->min_ssa_var].range.min + constraint->range.min;
936 }
937 #endif
938 } else {
939 tmp->underflow = 1;
940 tmp->min = ZEND_LONG_MIN;
941 }
942 if (constraint->max_ssa_var < 0) {
943 tmp->max = constraint->range.max;
944 tmp->overflow = constraint->range.overflow;
945 #ifdef SYM_RANGE
946 } else if (narrowing && ssa->var_info[constraint->max_ssa_var].has_range) {
947 if (add_will_overflow(ssa->var_info[constraint->max_ssa_var].range.max, constraint->range.max)) {
948 tmp->overflow = 1;
949 tmp->max = ZEND_LONG_MAX;
950 } else {
951 tmp->max = ssa->var_info[constraint->max_ssa_var].range.max + constraint->range.max;
952 tmp->overflow = ssa->var_info[constraint->max_ssa_var].range.overflow;
953 }
954 #endif
955 } else {
956 tmp->max = ZEND_LONG_MAX;
957 tmp->overflow = 1;
958 }
959 }
960 } else {
961 for (i = 0; i < ssa->cfg.blocks[p->block].predecessors_count; i++) {
962 ZEND_ASSERT(p->sources[i] >= 0);
963 if (ssa->var_info[p->sources[i]].has_range) {
964 /* union */
965 tmp->underflow |= ssa->var_info[p->sources[i]].range.underflow;
966 tmp->min = MIN(tmp->min, ssa->var_info[p->sources[i]].range.min);
967 tmp->max = MAX(tmp->max, ssa->var_info[p->sources[i]].range.max);
968 tmp->overflow |= ssa->var_info[p->sources[i]].range.overflow;
969 } else if (narrowing) {
970 tmp->underflow = 1;
971 tmp->min = ZEND_LONG_MIN;
972 tmp->max = ZEND_LONG_MAX;
973 tmp->overflow = 1;
974 }
975 }
976 }
977 return (tmp->min <= tmp->max);
978 } else if (ssa->vars[var].definition < 0) {
979 if (var < op_array->last_var &&
980 op_array->function_name) {
981
982 tmp->min = 0;
983 tmp->max = 0;
984 tmp->underflow = 0;
985 tmp->overflow = 0;
986 return 1;
987 }
988 return 0;
989 }
990 line = ssa->vars[var].definition;
991 opline = op_array->opcodes + line;
992
993 tmp->underflow = 0;
994 tmp->overflow = 0;
995 switch (opline->opcode) {
996 case ZEND_ADD:
997 case ZEND_SUB:
998 case ZEND_MUL:
999 case ZEND_DIV:
1000 case ZEND_MOD:
1001 case ZEND_SL:
1002 case ZEND_SR:
1003 case ZEND_BW_OR:
1004 case ZEND_BW_AND:
1005 case ZEND_BW_XOR:
1006 if (ssa->ops[line].result_def == var) {
1007 return zend_inference_calc_binary_op_range(
1008 op_array, ssa, opline, &ssa->ops[line], opline->opcode, tmp);
1009 }
1010 break;
1011
1012 case ZEND_BW_NOT:
1013 if (ssa->ops[line].result_def == var) {
1014 if (OP1_HAS_RANGE()) {
1015 if (OP1_RANGE_UNDERFLOW() ||
1016 OP1_RANGE_OVERFLOW()) {
1017 tmp->min = ZEND_LONG_MIN;
1018 tmp->max = ZEND_LONG_MAX;
1019 } else {
1020 op1_min = OP1_MIN_RANGE();
1021 op1_max = OP1_MAX_RANGE();
1022 tmp->min = ~op1_max;
1023 tmp->max = ~op1_min;
1024 }
1025 return 1;
1026 }
1027 }
1028 break;
1029 case ZEND_CAST:
1030 if (ssa->ops[line].op1_def == var) {
1031 if (ssa->ops[line].op1_def >= 0) {
1032 if (OP1_HAS_RANGE()) {
1033 tmp->underflow = OP1_RANGE_UNDERFLOW();
1034 tmp->min = OP1_MIN_RANGE();
1035 tmp->max = OP1_MAX_RANGE();
1036 tmp->overflow = OP1_RANGE_OVERFLOW();
1037 return 1;
1038 }
1039 }
1040 } else if (ssa->ops[line].result_def == var) {
1041 if (opline->extended_value == IS_NULL) {
1042 tmp->min = 0;
1043 tmp->max = 0;
1044 return 1;
1045 } else if (opline->extended_value == _IS_BOOL) {
1046 if (OP1_HAS_RANGE()) {
1047 op1_min = OP1_MIN_RANGE();
1048 op1_max = OP1_MAX_RANGE();
1049 tmp->min = (op1_min > 0 || op1_max < 0);
1050 tmp->max = (op1_min != 0 || op1_max != 0);
1051 return 1;
1052 } else {
1053 tmp->min = 0;
1054 tmp->max = 1;
1055 return 1;
1056 }
1057 } else if (opline->extended_value == IS_LONG) {
1058 if (OP1_HAS_RANGE()) {
1059 tmp->min = OP1_MIN_RANGE();
1060 tmp->max = OP1_MAX_RANGE();
1061 return 1;
1062 } else {
1063 tmp->min = ZEND_LONG_MIN;
1064 tmp->max = ZEND_LONG_MAX;
1065 return 1;
1066 }
1067 }
1068 }
1069 break;
1070 case ZEND_BOOL:
1071 case ZEND_JMPZ_EX:
1072 case ZEND_JMPNZ_EX:
1073 if (ssa->ops[line].result_def == var) {
1074 if (OP1_HAS_RANGE()) {
1075 op1_min = OP1_MIN_RANGE();
1076 op1_max = OP1_MAX_RANGE();
1077 tmp->min = (op1_min > 0 || op1_max < 0);
1078 tmp->max = (op1_min != 0 || op1_max != 0);
1079 return 1;
1080 } else {
1081 tmp->min = 0;
1082 tmp->max = 1;
1083 return 1;
1084 }
1085 }
1086 break;
1087 case ZEND_BOOL_NOT:
1088 if (ssa->ops[line].result_def == var) {
1089 if (OP1_HAS_RANGE()) {
1090 op1_min = OP1_MIN_RANGE();
1091 op1_max = OP1_MAX_RANGE();
1092 tmp->min = (op1_min == 0 && op1_max == 0);
1093 tmp->max = (op1_min <= 0 && op1_max >= 0);
1094 return 1;
1095 } else {
1096 tmp->min = 0;
1097 tmp->max = 1;
1098 return 1;
1099 }
1100 }
1101 break;
1102 case ZEND_BOOL_XOR:
1103 if (ssa->ops[line].result_def == var) {
1104 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1105 op1_min = OP1_MIN_RANGE();
1106 op2_min = OP2_MIN_RANGE();
1107 op1_max = OP1_MAX_RANGE();
1108 op2_max = OP2_MAX_RANGE();
1109 op1_min = (op1_min > 0 || op1_max < 0);
1110 op1_max = (op1_min != 0 || op1_max != 0);
1111 op2_min = (op2_min > 0 || op2_max < 0);
1112 op2_max = (op2_min != 0 || op2_max != 0);
1113 tmp->min = 0;
1114 tmp->max = 1;
1115 if (op1_min == op1_max && op2_min == op2_max) {
1116 if (op1_min == op2_min) {
1117 tmp->max = 0;
1118 } else {
1119 tmp->min = 1;
1120 }
1121 }
1122 return 1;
1123 } else {
1124 tmp->min = 0;
1125 tmp->max = 1;
1126 return 1;
1127 }
1128 }
1129 break;
1130 case ZEND_IS_IDENTICAL:
1131 case ZEND_IS_EQUAL:
1132 if (ssa->ops[line].result_def == var) {
1133 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1134 op1_min = OP1_MIN_RANGE();
1135 op2_min = OP2_MIN_RANGE();
1136 op1_max = OP1_MAX_RANGE();
1137 op2_max = OP2_MAX_RANGE();
1138
1139 tmp->min = (op1_min == op1_max &&
1140 op2_min == op2_max &&
1141 op1_min == op2_max);
1142 tmp->max = (op1_min <= op2_max && op1_max >= op2_min);
1143 return 1;
1144 } else {
1145 tmp->min = 0;
1146 tmp->max = 1;
1147 return 1;
1148 }
1149 }
1150 break;
1151 case ZEND_IS_NOT_IDENTICAL:
1152 case ZEND_IS_NOT_EQUAL:
1153 if (ssa->ops[line].result_def == var) {
1154 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1155 op1_min = OP1_MIN_RANGE();
1156 op2_min = OP2_MIN_RANGE();
1157 op1_max = OP1_MAX_RANGE();
1158 op2_max = OP2_MAX_RANGE();
1159
1160 tmp->min = (op1_min > op2_max || op1_max < op2_min);
1161 tmp->max = (op1_min != op1_max ||
1162 op2_min != op2_max ||
1163 op1_min != op2_max);
1164 return 1;
1165 } else {
1166 tmp->min = 0;
1167 tmp->max = 1;
1168 return 1;
1169 }
1170 }
1171 break;
1172 case ZEND_IS_SMALLER:
1173 if (ssa->ops[line].result_def == var) {
1174 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1175 op1_min = OP1_MIN_RANGE();
1176 op2_min = OP2_MIN_RANGE();
1177 op1_max = OP1_MAX_RANGE();
1178 op2_max = OP2_MAX_RANGE();
1179
1180 tmp->min = op1_max < op2_min;
1181 tmp->max = op1_min < op2_max;
1182 return 1;
1183 } else {
1184 tmp->min = 0;
1185 tmp->max = 1;
1186 return 1;
1187 }
1188 }
1189 break;
1190 case ZEND_IS_SMALLER_OR_EQUAL:
1191 if (ssa->ops[line].result_def == var) {
1192 if (OP1_HAS_RANGE() && OP2_HAS_RANGE()) {
1193 op1_min = OP1_MIN_RANGE();
1194 op2_min = OP2_MIN_RANGE();
1195 op1_max = OP1_MAX_RANGE();
1196 op2_max = OP2_MAX_RANGE();
1197
1198 tmp->min = op1_max <= op2_min;
1199 tmp->max = op1_min <= op2_max;
1200 return 1;
1201 } else {
1202 tmp->min = 0;
1203 tmp->max = 1;
1204 return 1;
1205 }
1206 }
1207 break;
1208 case ZEND_QM_ASSIGN:
1209 case ZEND_JMP_SET:
1210 case ZEND_COALESCE:
1211 if (ssa->ops[line].op1_def == var) {
1212 if (ssa->ops[line].op1_def >= 0) {
1213 if (OP1_HAS_RANGE()) {
1214 tmp->underflow = OP1_RANGE_UNDERFLOW();
1215 tmp->min = OP1_MIN_RANGE();
1216 tmp->max = OP1_MAX_RANGE();
1217 tmp->overflow = OP1_RANGE_OVERFLOW();
1218 return 1;
1219 }
1220 }
1221 }
1222 if (ssa->ops[line].result_def == var) {
1223 if (OP1_HAS_RANGE()) {
1224 tmp->min = OP1_MIN_RANGE();
1225 tmp->max = OP1_MAX_RANGE();
1226 tmp->underflow = OP1_RANGE_UNDERFLOW();
1227 tmp->overflow = OP1_RANGE_OVERFLOW();
1228 return 1;
1229 }
1230 }
1231 break;
1232 case ZEND_ASSERT_CHECK:
1233 if (ssa->ops[line].result_def == var) {
1234 tmp->min = 0;
1235 tmp->max = 1;
1236 return 1;
1237 }
1238 break;
1239 case ZEND_SEND_VAR:
1240 if (ssa->ops[line].op1_def == var) {
1241 if (ssa->ops[line].op1_def >= 0) {
1242 if (OP1_HAS_RANGE()) {
1243 tmp->underflow = OP1_RANGE_UNDERFLOW();
1244 tmp->min = OP1_MIN_RANGE();
1245 tmp->max = OP1_MAX_RANGE();
1246 tmp->overflow = OP1_RANGE_OVERFLOW();
1247 return 1;
1248 }
1249 }
1250 }
1251 break;
1252 case ZEND_PRE_INC:
1253 if (ssa->ops[line].op1_def == var || ssa->ops[line].result_def == var) {
1254 if (OP1_HAS_RANGE()) {
1255 tmp->min = OP1_MIN_RANGE();
1256 tmp->max = OP1_MAX_RANGE();
1257 tmp->underflow = OP1_RANGE_UNDERFLOW();
1258 tmp->overflow = OP1_RANGE_OVERFLOW();
1259 if (tmp->max < ZEND_LONG_MAX) {
1260 tmp->max++;
1261 } else {
1262 tmp->overflow = 1;
1263 }
1264 if (tmp->min < ZEND_LONG_MAX && !tmp->underflow) {
1265 tmp->min++;
1266 }
1267 return 1;
1268 }
1269 }
1270 break;
1271 case ZEND_PRE_DEC:
1272 if (ssa->ops[line].op1_def == var || ssa->ops[line].result_def == var) {
1273 if (OP1_HAS_RANGE()) {
1274 tmp->min = OP1_MIN_RANGE();
1275 tmp->max = OP1_MAX_RANGE();
1276 tmp->underflow = OP1_RANGE_UNDERFLOW();
1277 tmp->overflow = OP1_RANGE_OVERFLOW();
1278 if (tmp->min > ZEND_LONG_MIN) {
1279 tmp->min--;
1280 } else {
1281 tmp->underflow = 1;
1282 }
1283 if (tmp->max > ZEND_LONG_MIN && !tmp->overflow) {
1284 tmp->max--;
1285 }
1286 return 1;
1287 }
1288 }
1289 break;
1290 case ZEND_POST_INC:
1291 if (ssa->ops[line].op1_def == var || ssa->ops[line].result_def == var) {
1292 if (OP1_HAS_RANGE()) {
1293 tmp->min = OP1_MIN_RANGE();
1294 tmp->max = OP1_MAX_RANGE();
1295 tmp->underflow = OP1_RANGE_UNDERFLOW();
1296 tmp->overflow = OP1_RANGE_OVERFLOW();
1297 if (ssa->ops[line].result_def == var) {
1298 return 1;
1299 }
1300 if (tmp->max < ZEND_LONG_MAX) {
1301 tmp->max++;
1302 } else {
1303 tmp->overflow = 1;
1304 }
1305 if (tmp->min < ZEND_LONG_MAX && !tmp->underflow) {
1306 tmp->min++;
1307 }
1308 return 1;
1309 }
1310 }
1311 break;
1312 case ZEND_POST_DEC:
1313 if (ssa->ops[line].op1_def == var || ssa->ops[line].result_def == var) {
1314 if (OP1_HAS_RANGE()) {
1315 tmp->min = OP1_MIN_RANGE();
1316 tmp->max = OP1_MAX_RANGE();
1317 tmp->underflow = OP1_RANGE_UNDERFLOW();
1318 tmp->overflow = OP1_RANGE_OVERFLOW();
1319 if (ssa->ops[line].result_def == var) {
1320 return 1;
1321 }
1322 if (tmp->min > ZEND_LONG_MIN) {
1323 tmp->min--;
1324 } else {
1325 tmp->underflow = 1;
1326 }
1327 if (tmp->max > ZEND_LONG_MIN && !tmp->overflow) {
1328 tmp->max--;
1329 }
1330 return 1;
1331 }
1332 }
1333 break;
1334 case ZEND_UNSET_DIM:
1335 case ZEND_UNSET_OBJ:
1336 if (ssa->ops[line].op1_def == var) {
1337 /* If op1 is scalar, UNSET_DIM and UNSET_OBJ have no effect, so we can keep
1338 * the previous ranges. */
1339 if (OP1_HAS_RANGE()) {
1340 tmp->min = OP1_MIN_RANGE();
1341 tmp->max = OP1_MAX_RANGE();
1342 tmp->underflow = OP1_RANGE_UNDERFLOW();
1343 tmp->overflow = OP1_RANGE_OVERFLOW();
1344 return 1;
1345 }
1346 }
1347 break;
1348 case ZEND_ASSIGN:
1349 if (ssa->ops[line].op1_def == var || ssa->ops[line].op2_def == var || ssa->ops[line].result_def == var) {
1350 if (OP2_HAS_RANGE()) {
1351 tmp->min = OP2_MIN_RANGE();
1352 tmp->max = OP2_MAX_RANGE();
1353 tmp->underflow = OP2_RANGE_UNDERFLOW();
1354 tmp->overflow = OP2_RANGE_OVERFLOW();
1355 return 1;
1356 }
1357 }
1358 break;
1359 case ZEND_ASSIGN_DIM:
1360 case ZEND_ASSIGN_OBJ:
1361 if (ssa->ops[line+1].op1_def == var) {
1362 if ((opline+1)->opcode == ZEND_OP_DATA) {
1363 opline++;
1364 tmp->min = OP1_MIN_RANGE();
1365 tmp->max = OP1_MAX_RANGE();
1366 tmp->underflow = OP1_RANGE_UNDERFLOW();
1367 tmp->overflow = OP1_RANGE_OVERFLOW();
1368 return 1;
1369 }
1370 }
1371 break;
1372 case ZEND_ASSIGN_ADD:
1373 case ZEND_ASSIGN_SUB:
1374 case ZEND_ASSIGN_MUL:
1375 case ZEND_ASSIGN_DIV:
1376 case ZEND_ASSIGN_MOD:
1377 case ZEND_ASSIGN_SL:
1378 case ZEND_ASSIGN_SR:
1379 case ZEND_ASSIGN_BW_OR:
1380 case ZEND_ASSIGN_BW_AND:
1381 case ZEND_ASSIGN_BW_XOR:
1382 if (opline->extended_value == 0) {
1383 if (ssa->ops[line].op1_def == var || ssa->ops[line].result_def == var) {
1384 return zend_inference_calc_binary_op_range(
1385 op_array, ssa, opline, &ssa->ops[line],
1386 get_compound_assign_op(opline->opcode), tmp);
1387 }
1388 } else if ((opline+1)->opcode == ZEND_OP_DATA) {
1389 if (ssa->ops[line+1].op1_def == var) {
1390 opline++;
1391 if (OP1_HAS_RANGE()) {
1392 tmp->min = OP1_MIN_RANGE();
1393 tmp->max = OP1_MAX_RANGE();
1394 tmp->underflow = OP1_RANGE_UNDERFLOW();
1395 tmp->overflow = OP1_RANGE_OVERFLOW();
1396 return 1;
1397 }
1398 }
1399 }
1400 break;
1401 // case ZEND_ASSIGN_CONCAT:
1402 case ZEND_OP_DATA:
1403 if ((opline-1)->opcode == ZEND_ASSIGN_DIM ||
1404 (opline-1)->opcode == ZEND_ASSIGN_OBJ ||
1405 (opline-1)->opcode == ZEND_ASSIGN_ADD ||
1406 (opline-1)->opcode == ZEND_ASSIGN_SUB ||
1407 (opline-1)->opcode == ZEND_ASSIGN_MUL) {
1408 if (ssa->ops[line].op1_def == var) {
1409 if (OP1_HAS_RANGE()) {
1410 tmp->min = OP1_MIN_RANGE();
1411 tmp->max = OP1_MAX_RANGE();
1412 tmp->underflow = OP1_RANGE_UNDERFLOW();
1413 tmp->overflow = OP1_RANGE_OVERFLOW();
1414 return 1;
1415 }
1416 }
1417 break;
1418 }
1419 break;
1420 case ZEND_RECV:
1421 case ZEND_RECV_INIT:
1422 if (ssa->ops[line].result_def == var) {
1423 zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
1424
1425 if (func_info &&
1426 (int)opline->op1.num-1 < func_info->num_args &&
1427 func_info->arg_info[opline->op1.num-1].info.has_range) {
1428 *tmp = func_info->arg_info[opline->op1.num-1].info.range;
1429 return 1;
1430 } else if (op_array->arg_info &&
1431 opline->op1.num <= op_array->num_args) {
1432 if (ZEND_TYPE_CODE(op_array->arg_info[opline->op1.num-1].type) == IS_LONG) {
1433 tmp->underflow = 0;
1434 tmp->min = ZEND_LONG_MIN;
1435 tmp->max = ZEND_LONG_MAX;
1436 tmp->overflow = 0;
1437 return 1;
1438 } else if (ZEND_TYPE_CODE(op_array->arg_info[opline->op1.num-1].type) == _IS_BOOL) {
1439 tmp->underflow = 0;
1440 tmp->min = 0;
1441 tmp->max = 1;
1442 tmp->overflow = 0;
1443 return 1;
1444 }
1445 }
1446 }
1447 break;
1448 case ZEND_STRLEN:
1449 if (ssa->ops[line].result_def == var) {
1450 #if SIZEOF_ZEND_LONG == 4
1451 /* The length of a string is a non-negative integer. However, on 32-bit
1452 * platforms overflows into negative lengths may occur, so it's better
1453 * to not assume any particular range. */
1454 tmp->min = ZEND_LONG_MIN;
1455 #else
1456 tmp->min = 0;
1457 #endif
1458 tmp->max = ZEND_LONG_MAX;
1459 return 1;
1460 }
1461 break;
1462 case ZEND_FUNC_NUM_ARGS:
1463 tmp->min = 0;
1464 tmp->max = ZEND_LONG_MAX;
1465 return 1;
1466 case ZEND_COUNT:
1467 /* count() on Countable objects may return negative numbers */
1468 tmp->min = ZEND_LONG_MIN;
1469 tmp->max = ZEND_LONG_MAX;
1470 return 1;
1471 case ZEND_DO_FCALL:
1472 case ZEND_DO_ICALL:
1473 case ZEND_DO_UCALL:
1474 case ZEND_DO_FCALL_BY_NAME:
1475 if (ssa->ops[line].result_def == var) {
1476 zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
1477 zend_call_info *call_info;
1478 if (!func_info || !func_info->call_map) {
1479 break;
1480 }
1481
1482 call_info = func_info->call_map[opline - op_array->opcodes];
1483 if (!call_info) {
1484 break;
1485 }
1486 if (call_info->callee_func->type == ZEND_USER_FUNCTION) {
1487 func_info = ZEND_FUNC_INFO(&call_info->callee_func->op_array);
1488 if (func_info && func_info->return_info.has_range) {
1489 *tmp = func_info->return_info.range;
1490 return 1;
1491 }
1492 }
1493 //TODO: we can't use type inference for internal functions at this point ???
1494 #if 0
1495 uint32_t type;
1496
1497 type = zend_get_func_info(call_info, ssa);
1498 if (!(type & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG)))) {
1499 tmp->underflow = 0;
1500 tmp->min = 0;
1501 tmp->max = 0;
1502 tmp->overflow = 0;
1503 if (type & MAY_BE_LONG) {
1504 tmp->min = ZEND_LONG_MIN;
1505 tmp->max = ZEND_LONG_MAX;
1506 } else if (type & MAY_BE_TRUE) {
1507 if (!(type & (MAY_BE_NULL|MAY_BE_FALSE))) {
1508 tmp->min = 1;
1509 }
1510 tmp->max = 1;
1511 }
1512 return 1;
1513 }
1514 #endif
1515 }
1516 break;
1517 // FIXME: support for more opcodes
1518 default:
1519 break;
1520 }
1521 return 0;
1522 }
1523
zend_inference_init_range(const zend_op_array * op_array,zend_ssa * ssa,int var,zend_bool underflow,zend_long min,zend_long max,zend_bool overflow)1524 void zend_inference_init_range(const zend_op_array *op_array, zend_ssa *ssa, int var, zend_bool underflow, zend_long min, zend_long max, zend_bool overflow)
1525 {
1526 if (underflow) {
1527 min = ZEND_LONG_MIN;
1528 }
1529 if (overflow) {
1530 max = ZEND_LONG_MAX;
1531 }
1532 ssa->var_info[var].has_range = 1;
1533 ssa->var_info[var].range.underflow = underflow;
1534 ssa->var_info[var].range.min = min;
1535 ssa->var_info[var].range.max = max;
1536 ssa->var_info[var].range.overflow = overflow;
1537 LOG_SSA_RANGE(" change range (init SCC %2d) %2d [%s%ld..%ld%s]\n", ssa->vars[var].scc, var, (underflow?"-- ":""), min, max, (overflow?" ++":""));
1538 }
1539
zend_inference_widening_meet(zend_ssa_var_info * var_info,zend_ssa_range * r)1540 int zend_inference_widening_meet(zend_ssa_var_info *var_info, zend_ssa_range *r)
1541 {
1542 if (!var_info->has_range) {
1543 var_info->has_range = 1;
1544 } else {
1545 if (r->underflow ||
1546 var_info->range.underflow ||
1547 r->min < var_info->range.min) {
1548 r->underflow = 1;
1549 r->min = ZEND_LONG_MIN;
1550 }
1551 if (r->overflow ||
1552 var_info->range.overflow ||
1553 r->max > var_info->range.max) {
1554 r->overflow = 1;
1555 r->max = ZEND_LONG_MAX;
1556 }
1557 if (var_info->range.min == r->min &&
1558 var_info->range.max == r->max &&
1559 var_info->range.underflow == r->underflow &&
1560 var_info->range.overflow == r->overflow) {
1561 return 0;
1562 }
1563 }
1564 var_info->range = *r;
1565 return 1;
1566 }
1567
zend_ssa_range_widening(const zend_op_array * op_array,zend_ssa * ssa,int var,int scc)1568 static int zend_ssa_range_widening(const zend_op_array *op_array, zend_ssa *ssa, int var, int scc)
1569 {
1570 zend_ssa_range tmp;
1571
1572 if (zend_inference_calc_range(op_array, ssa, var, 1, 0, &tmp)) {
1573 if (zend_inference_widening_meet(&ssa->var_info[var], &tmp)) {
1574 LOG_SSA_RANGE(" change range (widening SCC %2d) %2d [%s%ld..%ld%s]\n", scc, var, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1575 return 1;
1576 }
1577 }
1578 return 0;
1579 }
1580
zend_inference_narrowing_meet(zend_ssa_var_info * var_info,zend_ssa_range * r)1581 int zend_inference_narrowing_meet(zend_ssa_var_info *var_info, zend_ssa_range *r)
1582 {
1583 if (!var_info->has_range) {
1584 var_info->has_range = 1;
1585 } else {
1586 if (!r->underflow &&
1587 !var_info->range.underflow &&
1588 var_info->range.min < r->min) {
1589 r->min = var_info->range.min;
1590 }
1591 if (!r->overflow &&
1592 !var_info->range.overflow &&
1593 var_info->range.max > r->max) {
1594 r->max = var_info->range.max;
1595 }
1596 if (r->underflow) {
1597 r->min = ZEND_LONG_MIN;
1598 }
1599 if (r->overflow) {
1600 r->max = ZEND_LONG_MAX;
1601 }
1602 if (var_info->range.min == r->min &&
1603 var_info->range.max == r->max &&
1604 var_info->range.underflow == r->underflow &&
1605 var_info->range.overflow == r->overflow) {
1606 return 0;
1607 }
1608 }
1609 var_info->range = *r;
1610 return 1;
1611 }
1612
zend_ssa_range_narrowing(const zend_op_array * op_array,zend_ssa * ssa,int var,int scc)1613 static int zend_ssa_range_narrowing(const zend_op_array *op_array, zend_ssa *ssa, int var, int scc)
1614 {
1615 zend_ssa_range tmp;
1616
1617 if (zend_inference_calc_range(op_array, ssa, var, 0, 1, &tmp)) {
1618 if (zend_inference_narrowing_meet(&ssa->var_info[var], &tmp)) {
1619 LOG_SSA_RANGE(" change range (narrowing SCC %2d) %2d [%s%ld..%ld%s]\n", scc, var, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1620 return 1;
1621 }
1622 }
1623 return 0;
1624 }
1625
1626 #ifdef NEG_RANGE
1627 # define CHECK_INNER_CYCLE(var2) \
1628 do { \
1629 if (ssa->vars[var2].scc == ssa->vars[var].scc && \
1630 !ssa->vars[var2].scc_entry && \
1631 !zend_bitset_in(visited, var2) && \
1632 zend_check_inner_cycles(op_array, ssa, worklist, visited, var2)) { \
1633 return 1; \
1634 } \
1635 } while (0)
1636
zend_check_inner_cycles(const zend_op_array * op_array,zend_ssa * ssa,zend_bitset worklist,zend_bitset visited,int var)1637 static int zend_check_inner_cycles(const zend_op_array *op_array, zend_ssa *ssa, zend_bitset worklist, zend_bitset visited, int var)
1638 {
1639 if (zend_bitset_in(worklist, var)) {
1640 return 1;
1641 }
1642 zend_bitset_incl(worklist, var);
1643 FOR_EACH_VAR_USAGE(var, CHECK_INNER_CYCLE);
1644 zend_bitset_incl(visited, var);
1645 return 0;
1646 }
1647 #endif
1648
zend_infer_ranges_warmup(const zend_op_array * op_array,zend_ssa * ssa,int * scc_var,int * next_scc_var,int scc)1649 static void zend_infer_ranges_warmup(const zend_op_array *op_array, zend_ssa *ssa, int *scc_var, int *next_scc_var, int scc)
1650 {
1651 int worklist_len = zend_bitset_len(ssa->vars_count);
1652 int j, n;
1653 zend_ssa_range tmp;
1654 ALLOCA_FLAG(use_heap)
1655 zend_bitset worklist = do_alloca(sizeof(zend_ulong) * worklist_len * 2, use_heap);
1656 zend_bitset visited = worklist + worklist_len;
1657 #ifdef NEG_RANGE
1658 int has_inner_cycles = 0;
1659
1660 memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1661 memset(visited, 0, sizeof(zend_ulong) * worklist_len);
1662 j = scc_var[scc];
1663 while (j >= 0) {
1664 if (!zend_bitset_in(visited, j) &&
1665 zend_check_inner_cycles(op_array, ssa, worklist, visited, j)) {
1666 has_inner_cycles = 1;
1667 break;
1668 }
1669 j = next_scc_var[j];
1670 }
1671 #endif
1672
1673 memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1674
1675 for (n = 0; n < RANGE_WARMUP_PASSES; n++) {
1676 j= scc_var[scc];
1677 while (j >= 0) {
1678 if (ssa->vars[j].scc_entry) {
1679 zend_bitset_incl(worklist, j);
1680 }
1681 j = next_scc_var[j];
1682 }
1683
1684 memset(visited, 0, sizeof(zend_ulong) * worklist_len);
1685
1686 WHILE_WORKLIST(worklist, worklist_len, j) {
1687 if (zend_inference_calc_range(op_array, ssa, j, 0, 0, &tmp)) {
1688 #ifdef NEG_RANGE
1689 if (!has_inner_cycles &&
1690 ssa->var_info[j].has_range &&
1691 ssa->vars[j].definition_phi &&
1692 ssa->vars[j].definition_phi->pi >= 0 &&
1693 ssa->vars[j].definition_phi->has_range_constraint &&
1694 ssa->vars[j].definition_phi->constraint.range.negative &&
1695 ssa->vars[j].definition_phi->constraint.range.min_ssa_var < 0 &&
1696 ssa->vars[j].definition_phi->constraint.range.max_ssa_var < 0) {
1697 zend_ssa_range_constraint *constraint =
1698 &ssa->vars[j].definition_phi->constraint.range;
1699 if (tmp.min == ssa->var_info[j].range.min &&
1700 tmp.max == ssa->var_info[j].range.max) {
1701 if (constraint->negative == NEG_INIT) {
1702 LOG_NEG_RANGE("#%d INVARIANT\n", j);
1703 constraint->negative = NEG_INVARIANT;
1704 }
1705 } else if (tmp.min == ssa->var_info[j].range.min &&
1706 tmp.max == ssa->var_info[j].range.max + 1 &&
1707 tmp.max < constraint->range.min) {
1708 if (constraint->negative == NEG_INIT ||
1709 constraint->negative == NEG_INVARIANT) {
1710 LOG_NEG_RANGE("#%d LT\n", j);
1711 constraint->negative = NEG_USE_LT;
1712 //???NEG
1713 } else if (constraint->negative == NEG_USE_GT) {
1714 LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1715 constraint->negative = NEG_UNKNOWN;
1716 }
1717 } else if (tmp.max == ssa->var_info[j].range.max &&
1718 tmp.min == ssa->var_info[j].range.min - 1 &&
1719 tmp.min > constraint->range.max) {
1720 if (constraint->negative == NEG_INIT ||
1721 constraint->negative == NEG_INVARIANT) {
1722 LOG_NEG_RANGE("#%d GT\n", j);
1723 constraint->negative = NEG_USE_GT;
1724 //???NEG
1725 } else if (constraint->negative == NEG_USE_LT) {
1726 LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1727 constraint->negative = NEG_UNKNOWN;
1728 }
1729 } else {
1730 LOG_NEG_RANGE("#%d UNKNOWN\n", j);
1731 constraint->negative = NEG_UNKNOWN;
1732 }
1733 }
1734 #endif
1735 if (zend_inference_narrowing_meet(&ssa->var_info[j], &tmp)) {
1736 LOG_SSA_RANGE(" change range (warmup %2d SCC %2d) %2d [%s%ld..%ld%s]\n", n, scc, j, (tmp.underflow?"-- ":""), tmp.min, tmp.max, (tmp.overflow?" ++":""));
1737 zend_bitset_incl(visited, j);
1738 FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR_1);
1739 }
1740 }
1741 } WHILE_WORKLIST_END();
1742 }
1743 free_alloca(worklist, use_heap);
1744 }
1745
zend_infer_ranges(const zend_op_array * op_array,zend_ssa * ssa)1746 static int zend_infer_ranges(const zend_op_array *op_array, zend_ssa *ssa) /* {{{ */
1747 {
1748 int worklist_len = zend_bitset_len(ssa->vars_count);
1749 zend_bitset worklist;
1750 int *next_scc_var;
1751 int *scc_var;
1752 zend_ssa_phi *p;
1753 zend_ssa_range tmp;
1754 int scc, j;
1755 ALLOCA_FLAG(use_heap);
1756
1757 worklist = do_alloca(
1758 ZEND_MM_ALIGNED_SIZE(sizeof(zend_ulong) * worklist_len) +
1759 ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->vars_count) +
1760 sizeof(int) * ssa->sccs, use_heap);
1761 next_scc_var = (int*)((char*)worklist + ZEND_MM_ALIGNED_SIZE(sizeof(zend_ulong) * worklist_len));
1762 scc_var = (int*)((char*)next_scc_var + ZEND_MM_ALIGNED_SIZE(sizeof(int) * ssa->vars_count));
1763
1764 LOG_SSA_RANGE("Range Inference\n");
1765
1766 /* Create linked lists of SSA variables for each SCC */
1767 memset(scc_var, -1, sizeof(int) * ssa->sccs);
1768 for (j = 0; j < ssa->vars_count; j++) {
1769 if (ssa->vars[j].scc >= 0) {
1770 next_scc_var[j] = scc_var[ssa->vars[j].scc];
1771 scc_var[ssa->vars[j].scc] = j;
1772 }
1773 }
1774
1775 for (scc = 0; scc < ssa->sccs; scc++) {
1776 j = scc_var[scc];
1777 if (next_scc_var[j] < 0) {
1778 /* SCC with a single element */
1779 if (zend_inference_calc_range(op_array, ssa, j, 0, 1, &tmp)) {
1780 zend_inference_init_range(op_array, ssa, j, tmp.underflow, tmp.min, tmp.max, tmp.overflow);
1781 } else {
1782 zend_inference_init_range(op_array, ssa, j, 1, ZEND_LONG_MIN, ZEND_LONG_MAX, 1);
1783 }
1784 } else {
1785 /* Find SCC entry points */
1786 memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
1787 do {
1788 if (ssa->vars[j].scc_entry) {
1789 zend_bitset_incl(worklist, j);
1790 }
1791 j = next_scc_var[j];
1792 } while (j >= 0);
1793
1794 #if RANGE_WARMUP_PASSES > 0
1795 zend_infer_ranges_warmup(op_array, ssa, scc_var, next_scc_var, scc);
1796 j = scc_var[scc];
1797 do {
1798 zend_bitset_incl(worklist, j);
1799 j = next_scc_var[j];
1800 } while (j >= 0);
1801 #endif
1802
1803 /* widening */
1804 WHILE_WORKLIST(worklist, worklist_len, j) {
1805 if (zend_ssa_range_widening(op_array, ssa, j, scc)) {
1806 FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1807 }
1808 } WHILE_WORKLIST_END();
1809
1810 /* Add all SCC entry variables into worklist for narrowing */
1811 for (j = scc_var[scc]; j >= 0; j = next_scc_var[j]) {
1812 if (!ssa->var_info[j].has_range) {
1813 zend_inference_init_range(op_array, ssa, j, 1, ZEND_LONG_MIN, ZEND_LONG_MAX, 1);
1814 } else if (ssa->vars[j].definition_phi &&
1815 ssa->vars[j].definition_phi->pi < 0) {
1816 /* narrowing Phi functions first */
1817 zend_ssa_range_narrowing(op_array, ssa, j, scc);
1818 }
1819 zend_bitset_incl(worklist, j);
1820 }
1821
1822 /* narrowing */
1823 WHILE_WORKLIST(worklist, worklist_len, j) {
1824 if (zend_ssa_range_narrowing(op_array, ssa, j, scc)) {
1825 FOR_EACH_VAR_USAGE(j, ADD_SCC_VAR);
1826 #ifdef SYM_RANGE
1827 /* Process symbolic control-flow constraints */
1828 p = ssa->vars[j].sym_use_chain;
1829 while (p) {
1830 ADD_SCC_VAR(p->ssa_var);
1831 p = p->sym_use_chain;
1832 }
1833 #endif
1834 }
1835 } WHILE_WORKLIST_END();
1836 }
1837 }
1838
1839 free_alloca(worklist, use_heap);
1840
1841 return SUCCESS;
1842 }
1843 /* }}} */
1844
get_ssa_alias_types(zend_ssa_alias_kind alias)1845 static uint32_t get_ssa_alias_types(zend_ssa_alias_kind alias) {
1846 if (alias == PHP_ERRORMSG_ALIAS) {
1847 return MAY_BE_STRING | MAY_BE_RC1 | MAY_BE_RCN;
1848 } else if (alias == HTTP_RESPONSE_HEADER_ALIAS) {
1849 return MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_STRING | MAY_BE_RC1 | MAY_BE_RCN;
1850 } else {
1851 return MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
1852 }
1853 }
1854
1855 #define UPDATE_SSA_TYPE(_type, _var) \
1856 do { \
1857 uint32_t __type = (_type); \
1858 int __var = (_var); \
1859 if (__type & MAY_BE_REF) { \
1860 __type |= MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF; \
1861 } \
1862 if (__var >= 0) { \
1863 zend_ssa_var *__ssa_var = &ssa_vars[__var]; \
1864 if (__ssa_var->var < op_array->last_var) { \
1865 if (__type & (MAY_BE_REF|MAY_BE_RCN)) { \
1866 __type |= MAY_BE_RC1 | MAY_BE_RCN; \
1867 } \
1868 if ((__type & MAY_BE_RC1) && (__type & MAY_BE_STRING)) {\
1869 /* TODO: support for array keys and ($str . "")*/ \
1870 __type |= MAY_BE_RCN; \
1871 } \
1872 if (__ssa_var->alias) { \
1873 __type |= get_ssa_alias_types(__ssa_var->alias); \
1874 } \
1875 } \
1876 if (ssa_var_info[__var].type != __type) { \
1877 if (ssa_var_info[__var].type & ~__type) { \
1878 handle_type_narrowing(op_array, ssa, worklist, \
1879 __var, ssa_var_info[__var].type, __type); \
1880 return FAILURE; \
1881 } \
1882 ssa_var_info[__var].type = __type; \
1883 add_usages(op_array, ssa, worklist, __var); \
1884 } \
1885 /*zend_bitset_excl(worklist, var);*/ \
1886 } \
1887 } while (0)
1888
1889 #define UPDATE_SSA_OBJ_TYPE(_ce, _is_instanceof, var) \
1890 do { \
1891 if (var >= 0) { \
1892 if (ssa_var_info[var].ce != (_ce) || \
1893 ssa_var_info[var].is_instanceof != (_is_instanceof)) { \
1894 ssa_var_info[var].ce = (_ce); \
1895 ssa_var_info[var].is_instanceof = (_is_instanceof); \
1896 add_usages(op_array, ssa, worklist, var); \
1897 } \
1898 /*zend_bitset_excl(worklist, var);*/ \
1899 } \
1900 } while (0)
1901
1902 #define COPY_SSA_OBJ_TYPE(from_var, to_var) do { \
1903 if ((from_var) >= 0 && (ssa_var_info[(from_var)].type & MAY_BE_OBJECT) \
1904 && ssa_var_info[(from_var)].ce) { \
1905 UPDATE_SSA_OBJ_TYPE(ssa_var_info[(from_var)].ce, \
1906 ssa_var_info[(from_var)].is_instanceof, (to_var)); \
1907 } else { \
1908 UPDATE_SSA_OBJ_TYPE(NULL, 0, (to_var)); \
1909 } \
1910 } while (0)
1911
add_usages(const zend_op_array * op_array,zend_ssa * ssa,zend_bitset worklist,int var)1912 static void add_usages(const zend_op_array *op_array, zend_ssa *ssa, zend_bitset worklist, int var)
1913 {
1914 if (ssa->vars[var].phi_use_chain) {
1915 zend_ssa_phi *p = ssa->vars[var].phi_use_chain;
1916 do {
1917 zend_bitset_incl(worklist, p->ssa_var);
1918 p = zend_ssa_next_use_phi(ssa, var, p);
1919 } while (p);
1920 }
1921 if (ssa->vars[var].use_chain >= 0) {
1922 int use = ssa->vars[var].use_chain;
1923 zend_ssa_op *op;
1924
1925 do {
1926 op = ssa->ops + use;
1927 if (op->result_def >= 0) {
1928 zend_bitset_incl(worklist, op->result_def);
1929 }
1930 if (op->op1_def >= 0) {
1931 zend_bitset_incl(worklist, op->op1_def);
1932 }
1933 if (op->op2_def >= 0) {
1934 zend_bitset_incl(worklist, op->op2_def);
1935 }
1936 if (op_array->opcodes[use].opcode == ZEND_OP_DATA) {
1937 op--;
1938 if (op->result_def >= 0) {
1939 zend_bitset_incl(worklist, op->result_def);
1940 }
1941 if (op->op1_def >= 0) {
1942 zend_bitset_incl(worklist, op->op1_def);
1943 }
1944 if (op->op2_def >= 0) {
1945 zend_bitset_incl(worklist, op->op2_def);
1946 }
1947 }
1948 use = zend_ssa_next_use(ssa->ops, var, use);
1949 } while (use >= 0);
1950 }
1951 }
1952
reset_dependent_vars(const zend_op_array * op_array,zend_ssa * ssa,zend_bitset worklist,int var)1953 static void reset_dependent_vars(const zend_op_array *op_array, zend_ssa *ssa, zend_bitset worklist, int var)
1954 {
1955 zend_ssa_op *ssa_ops = ssa->ops;
1956 zend_ssa_var *ssa_vars = ssa->vars;
1957 zend_ssa_var_info *ssa_var_info = ssa->var_info;
1958 zend_ssa_phi *p;
1959 int use;
1960
1961 p = ssa_vars[var].phi_use_chain;
1962 while (p) {
1963 if (ssa_var_info[p->ssa_var].type) {
1964 ssa_var_info[p->ssa_var].type = 0;
1965 zend_bitset_incl(worklist, p->ssa_var);
1966 reset_dependent_vars(op_array, ssa, worklist, p->ssa_var);
1967 }
1968 p = zend_ssa_next_use_phi(ssa, var, p);
1969 }
1970 use = ssa_vars[var].use_chain;
1971 while (use >= 0) {
1972 if (ssa_ops[use].op1_def >= 0 && ssa_var_info[ssa_ops[use].op1_def].type) {
1973 ssa_var_info[ssa_ops[use].op1_def].type = 0;
1974 zend_bitset_incl(worklist, ssa_ops[use].op1_def);
1975 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use].op1_def);
1976 }
1977 if (ssa_ops[use].op2_def >= 0 && ssa_var_info[ssa_ops[use].op2_def].type) {
1978 ssa_var_info[ssa_ops[use].op2_def].type = 0;
1979 zend_bitset_incl(worklist, ssa_ops[use].op2_def);
1980 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use].op2_def);
1981 }
1982 if (ssa_ops[use].result_def >= 0 && ssa_var_info[ssa_ops[use].result_def].type) {
1983 ssa_var_info[ssa_ops[use].result_def].type = 0;
1984 zend_bitset_incl(worklist, ssa_ops[use].result_def);
1985 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use].result_def);
1986 }
1987 if (op_array->opcodes[use+1].opcode == ZEND_OP_DATA) {
1988 if (ssa_ops[use+1].op1_def >= 0 && ssa_var_info[ssa_ops[use+1].op1_def].type) {
1989 ssa_var_info[ssa_ops[use+1].op1_def].type = 0;
1990 zend_bitset_incl(worklist, ssa_ops[use+1].op1_def);
1991 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use+1].op1_def);
1992 }
1993 if (ssa_ops[use+1].op2_def >= 0 && ssa_var_info[ssa_ops[use+1].op2_def].type) {
1994 ssa_var_info[ssa_ops[use+1].op2_def].type = 0;
1995 zend_bitset_incl(worklist, ssa_ops[use+1].op2_def);
1996 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use+1].op2_def);
1997 }
1998 if (ssa_ops[use+1].result_def >= 0 && ssa_var_info[ssa_ops[use+1].result_def].type) {
1999 ssa_var_info[ssa_ops[use+1].result_def].type = 0;
2000 zend_bitset_incl(worklist, ssa_ops[use+1].result_def);
2001 reset_dependent_vars(op_array, ssa, worklist, ssa_ops[use+1].result_def);
2002 }
2003 }
2004 use = zend_ssa_next_use(ssa_ops, var, use);
2005 }
2006 #ifdef SYM_RANGE
2007 /* Process symbolic control-flow constraints */
2008 p = ssa->vars[var].sym_use_chain;
2009 while (p) {
2010 ssa_var_info[p->ssa_var].type = 0;
2011 zend_bitset_incl(worklist, p->ssa_var);
2012 reset_dependent_vars(op_array, ssa, worklist, p->ssa_var);
2013 p = p->sym_use_chain;
2014 }
2015 #endif
2016 }
2017
handle_type_narrowing(const zend_op_array * op_array,zend_ssa * ssa,zend_bitset worklist,int var,uint32_t old_type,uint32_t new_type)2018 static void handle_type_narrowing(const zend_op_array *op_array, zend_ssa *ssa, zend_bitset worklist, int var, uint32_t old_type, uint32_t new_type)
2019 {
2020 if (1) {
2021 /* Right now, this is always a bug */
2022 int def_op_num = ssa->vars[var].definition;
2023 const zend_op *def_opline = def_op_num >= 0 ? &op_array->opcodes[def_op_num] : NULL;
2024 const char *def_op_name = def_opline ? zend_get_opcode_name(def_opline->opcode) : "PHI";
2025 zend_error(E_WARNING, "Narrowing occurred during type inference of %s. Please file a bug report on bugs.php.net", def_op_name);
2026 } else {
2027 /* if new_type set resets some bits from old_type set
2028 * We have completely recalculate types of some dependent SSA variables
2029 * (this may occurs mainly because of incremental inter-precudure
2030 * type inference)
2031 */
2032 reset_dependent_vars(op_array, ssa, worklist, var);
2033 }
2034 }
2035
zend_array_element_type(uint32_t t1,int write,int insert)2036 uint32_t zend_array_element_type(uint32_t t1, int write, int insert)
2037 {
2038 uint32_t tmp = 0;
2039
2040 if (t1 & MAY_BE_OBJECT) {
2041 tmp |= MAY_BE_ANY | MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2042 }
2043 if (t1 & MAY_BE_ARRAY) {
2044 if (insert) {
2045 tmp |= MAY_BE_NULL;
2046 } else {
2047 tmp |= MAY_BE_NULL | ((t1 & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
2048 if (tmp & MAY_BE_ARRAY) {
2049 tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2050 }
2051 if (t1 & MAY_BE_ARRAY_OF_REF) {
2052 tmp |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN;
2053 } else if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2054 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2055 }
2056 }
2057 }
2058 if (t1 & MAY_BE_STRING) {
2059 tmp |= MAY_BE_STRING | MAY_BE_RC1;
2060 if (write) {
2061 tmp |= MAY_BE_NULL;
2062 }
2063 }
2064 if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2065 tmp |= MAY_BE_NULL;
2066 if (t1 & MAY_BE_ERROR) {
2067 if (write) {
2068 tmp |= MAY_BE_ERROR;
2069 }
2070 }
2071 }
2072 if (t1 & (MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE)) {
2073 tmp |= MAY_BE_NULL;
2074 if (write) {
2075 tmp |= MAY_BE_ERROR;
2076 }
2077 }
2078 return tmp;
2079 }
2080
assign_dim_result_type(uint32_t arr_type,uint32_t dim_type,uint32_t value_type,zend_uchar dim_op_type)2081 static uint32_t assign_dim_result_type(
2082 uint32_t arr_type, uint32_t dim_type, uint32_t value_type, zend_uchar dim_op_type) {
2083 uint32_t tmp = arr_type & ~(MAY_BE_RC1|MAY_BE_RCN);
2084
2085 if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2086 tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
2087 tmp |= MAY_BE_ARRAY|MAY_BE_RC1;
2088 }
2089 if (tmp & (MAY_BE_ARRAY|MAY_BE_STRING)) {
2090 tmp |= MAY_BE_RC1;
2091 }
2092 if (tmp & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2093 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2094 }
2095 if (tmp & MAY_BE_ARRAY) {
2096 if (value_type & MAY_BE_UNDEF) {
2097 tmp |= MAY_BE_ARRAY_OF_NULL;
2098 }
2099 if (dim_op_type == IS_UNUSED) {
2100 tmp |= MAY_BE_ARRAY_KEY_LONG;
2101 } else {
2102 if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
2103 tmp |= MAY_BE_ARRAY_KEY_LONG;
2104 }
2105 if (dim_type & MAY_BE_STRING) {
2106 tmp |= MAY_BE_ARRAY_KEY_STRING;
2107 if (dim_op_type != IS_CONST) {
2108 // FIXME: numeric string
2109 tmp |= MAY_BE_ARRAY_KEY_LONG;
2110 }
2111 }
2112 if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
2113 tmp |= MAY_BE_ARRAY_KEY_STRING;
2114 }
2115 }
2116 /* Only add value type if we have a key type. It might be that the key type is illegal
2117 * for arrays. */
2118 if (tmp & MAY_BE_ARRAY_KEY_ANY) {
2119 tmp |= (value_type & MAY_BE_ANY) << MAY_BE_ARRAY_SHIFT;
2120 }
2121 }
2122 return tmp;
2123 }
2124
2125 /* For binary ops that have compound assignment operators */
binary_op_result_type(zend_ssa * ssa,zend_uchar opcode,uint32_t t1,uint32_t t2,uint32_t result_var)2126 static uint32_t binary_op_result_type(
2127 zend_ssa *ssa, zend_uchar opcode, uint32_t t1, uint32_t t2, uint32_t result_var) {
2128 uint32_t tmp = 0;
2129 uint32_t t1_type = (t1 & MAY_BE_ANY) | (t1 & MAY_BE_UNDEF ? MAY_BE_NULL : 0);
2130 uint32_t t2_type = (t2 & MAY_BE_ANY) | (t2 & MAY_BE_UNDEF ? MAY_BE_NULL : 0);
2131
2132 /* Handle potentially overloaded operators.
2133 * This could be made more precise by checking the class type, if known. */
2134 if ((t1_type & MAY_BE_OBJECT) || (t2_type & MAY_BE_OBJECT)) {
2135 /* This is somewhat GMP specific. */
2136 tmp |= MAY_BE_OBJECT | MAY_BE_FALSE | MAY_BE_RC1;
2137 }
2138
2139 switch (opcode) {
2140 case ZEND_ADD:
2141 if (t1_type == MAY_BE_LONG && t2_type == MAY_BE_LONG) {
2142 if (!ssa->var_info[result_var].has_range ||
2143 ssa->var_info[result_var].range.underflow ||
2144 ssa->var_info[result_var].range.overflow) {
2145 /* may overflow */
2146 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2147 } else {
2148 tmp |= MAY_BE_LONG;
2149 }
2150 } else if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2151 tmp |= MAY_BE_DOUBLE;
2152 } else if (t1_type == MAY_BE_ARRAY && t2_type == MAY_BE_ARRAY) {
2153 tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
2154 tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2155 tmp |= t2 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2156 } else {
2157 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2158 if ((t1_type & MAY_BE_ARRAY) && (t2_type & MAY_BE_ARRAY)) {
2159 tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
2160 tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2161 tmp |= t2 & (MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF);
2162 }
2163 }
2164 break;
2165 case ZEND_SUB:
2166 case ZEND_MUL:
2167 if (t1_type == MAY_BE_LONG && t2_type == MAY_BE_LONG) {
2168 if (!ssa->var_info[result_var].has_range ||
2169 ssa->var_info[result_var].range.underflow ||
2170 ssa->var_info[result_var].range.overflow) {
2171 /* may overflow */
2172 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2173 } else {
2174 tmp |= MAY_BE_LONG;
2175 }
2176 } else if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2177 tmp |= MAY_BE_DOUBLE;
2178 } else {
2179 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2180 }
2181 break;
2182 case ZEND_DIV:
2183 case ZEND_POW:
2184 if (t1_type == MAY_BE_DOUBLE || t2_type == MAY_BE_DOUBLE) {
2185 tmp |= MAY_BE_DOUBLE;
2186 } else {
2187 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2188 }
2189 /* Division by zero results in Inf/-Inf/Nan (double), so it doesn't need any special
2190 * handling */
2191 break;
2192 case ZEND_MOD:
2193 tmp |= MAY_BE_LONG;
2194 /* Division by zero results in an exception, so it doesn't need any special handling */
2195 break;
2196 case ZEND_BW_OR:
2197 case ZEND_BW_AND:
2198 case ZEND_BW_XOR:
2199 if ((t1_type & MAY_BE_STRING) && (t2_type & MAY_BE_STRING)) {
2200 tmp |= MAY_BE_STRING | MAY_BE_RC1;
2201 }
2202 if ((t1_type & ~MAY_BE_STRING) || (t2_type & ~MAY_BE_STRING)) {
2203 tmp |= MAY_BE_LONG;
2204 }
2205 break;
2206 case ZEND_SL:
2207 case ZEND_SR:
2208 tmp |= MAY_BE_LONG;
2209 break;
2210 case ZEND_CONCAT:
2211 case ZEND_FAST_CONCAT:
2212 /* TODO: +MAY_BE_OBJECT ??? */
2213 tmp = MAY_BE_STRING | MAY_BE_RC1 | MAY_BE_RCN;
2214 break;
2215 EMPTY_SWITCH_DEFAULT_CASE()
2216 }
2217 return tmp;
2218 }
2219
get_class_entry(const zend_script * script,zend_string * lcname)2220 static inline zend_class_entry *get_class_entry(const zend_script *script, zend_string *lcname) {
2221 zend_class_entry *ce = script ? zend_hash_find_ptr(&script->class_table, lcname) : NULL;
2222 if (ce) {
2223 return ce;
2224 }
2225
2226 ce = zend_hash_find_ptr(CG(class_table), lcname);
2227 if (ce && ce->type == ZEND_INTERNAL_CLASS) {
2228 return ce;
2229 }
2230
2231 return NULL;
2232 }
2233
zend_fetch_arg_info(const zend_script * script,zend_arg_info * arg_info,zend_class_entry ** pce)2234 static uint32_t zend_fetch_arg_info(const zend_script *script, zend_arg_info *arg_info, zend_class_entry **pce)
2235 {
2236 uint32_t tmp = 0;
2237
2238 *pce = NULL;
2239 if (ZEND_TYPE_IS_CLASS(arg_info->type)) {
2240 // class type hinting...
2241 zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(arg_info->type));
2242 tmp |= MAY_BE_OBJECT;
2243 *pce = get_class_entry(script, lcname);
2244 zend_string_release(lcname);
2245 } else if (ZEND_TYPE_IS_CODE(arg_info->type)) {
2246 zend_uchar type_hint = ZEND_TYPE_CODE(arg_info->type);
2247
2248 if (type_hint == IS_VOID) {
2249 tmp |= MAY_BE_NULL;
2250 } else if (type_hint == IS_CALLABLE) {
2251 tmp |= MAY_BE_STRING|MAY_BE_OBJECT|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2252 } else if (type_hint == IS_ITERABLE) {
2253 tmp |= MAY_BE_OBJECT|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2254 } else if (type_hint == IS_ARRAY) {
2255 tmp |= MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2256 } else if (type_hint == _IS_BOOL) {
2257 tmp |= MAY_BE_TRUE|MAY_BE_FALSE;
2258 } else {
2259 ZEND_ASSERT(type_hint < IS_REFERENCE);
2260 tmp |= 1 << type_hint;
2261 }
2262 } else {
2263 tmp |= MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2264 }
2265 if (ZEND_TYPE_ALLOW_NULL(arg_info->type)) {
2266 tmp |= MAY_BE_NULL;
2267 }
2268 return tmp;
2269 }
2270
zend_update_type_info(const zend_op_array * op_array,zend_ssa * ssa,const zend_script * script,zend_bitset worklist,int i)2271 static int zend_update_type_info(const zend_op_array *op_array,
2272 zend_ssa *ssa,
2273 const zend_script *script,
2274 zend_bitset worklist,
2275 int i)
2276 {
2277 uint32_t t1, t2;
2278 uint32_t tmp, orig;
2279 zend_op *opline = op_array->opcodes + i;
2280 zend_ssa_op *ssa_ops = ssa->ops;
2281 zend_ssa_var *ssa_vars = ssa->vars;
2282 zend_ssa_var_info *ssa_var_info = ssa->var_info;
2283 zend_class_entry *ce;
2284 int j;
2285
2286 if (opline->opcode == ZEND_OP_DATA) {
2287 opline--;
2288 i--;
2289 }
2290
2291 t1 = OP1_INFO();
2292 t2 = OP2_INFO();
2293
2294 /* If one of the operands cannot have any type, this means the operand derives from
2295 * unreachable code. Propagate the empty result early, so that that the following
2296 * code may assume that operands have at least one type. */
2297 if (!(t1 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS|MAY_BE_ERROR))
2298 || !(t2 & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_CLASS|MAY_BE_ERROR))) {
2299 tmp = 0;
2300 if (ssa_ops[i].result_def >= 0) {
2301 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2302 }
2303 if (ssa_ops[i].op1_def >= 0) {
2304 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2305 }
2306 if (ssa_ops[i].op2_def >= 0) {
2307 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op2_def);
2308 }
2309 return 1;
2310 }
2311
2312 switch (opline->opcode) {
2313 case ZEND_ADD:
2314 case ZEND_SUB:
2315 case ZEND_MUL:
2316 case ZEND_DIV:
2317 case ZEND_POW:
2318 case ZEND_MOD:
2319 case ZEND_BW_OR:
2320 case ZEND_BW_AND:
2321 case ZEND_BW_XOR:
2322 case ZEND_SL:
2323 case ZEND_SR:
2324 case ZEND_CONCAT:
2325 tmp = binary_op_result_type(ssa, opline->opcode, t1, t2, ssa_ops[i].result_def);
2326 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2327 break;
2328 case ZEND_BW_NOT:
2329 tmp = 0;
2330 if (t1 & MAY_BE_STRING) {
2331 tmp |= MAY_BE_STRING | MAY_BE_RC1;
2332 }
2333 if (t1 & (MAY_BE_ANY-MAY_BE_STRING)) {
2334 tmp |= MAY_BE_LONG;
2335 }
2336 if (t1 & MAY_BE_OBJECT) {
2337 /* Potentially overloaded operator. */
2338 tmp |= MAY_BE_OBJECT | MAY_BE_RC1;
2339 }
2340 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2341 break;
2342 case ZEND_BEGIN_SILENCE:
2343 UPDATE_SSA_TYPE(MAY_BE_LONG, ssa_ops[i].result_def);
2344 break;
2345 case ZEND_BOOL_NOT:
2346 case ZEND_BOOL_XOR:
2347 case ZEND_IS_IDENTICAL:
2348 case ZEND_IS_NOT_IDENTICAL:
2349 case ZEND_IS_EQUAL:
2350 case ZEND_IS_NOT_EQUAL:
2351 case ZEND_IS_SMALLER:
2352 case ZEND_IS_SMALLER_OR_EQUAL:
2353 case ZEND_INSTANCEOF:
2354 case ZEND_JMPZ_EX:
2355 case ZEND_JMPNZ_EX:
2356 case ZEND_CASE:
2357 case ZEND_BOOL:
2358 case ZEND_ISSET_ISEMPTY_CV:
2359 case ZEND_ISSET_ISEMPTY_VAR:
2360 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2361 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2362 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2363 case ZEND_ASSERT_CHECK:
2364 case ZEND_IN_ARRAY:
2365 UPDATE_SSA_TYPE(MAY_BE_FALSE|MAY_BE_TRUE, ssa_ops[i].result_def);
2366 break;
2367 case ZEND_CAST:
2368 if (ssa_ops[i].op1_def >= 0) {
2369 tmp = t1;
2370 if ((t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) &&
2371 (opline->op1_type == IS_CV) &&
2372 (opline->extended_value == IS_ARRAY ||
2373 opline->extended_value == IS_OBJECT)) {
2374 tmp |= MAY_BE_RCN;
2375 } else if ((t1 & MAY_BE_STRING) &&
2376 (opline->op1_type == IS_CV) &&
2377 opline->extended_value == IS_STRING) {
2378 tmp |= MAY_BE_RCN;
2379 }
2380 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2381 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2382 }
2383 tmp = 0;
2384 if (opline->extended_value == _IS_BOOL) {
2385 tmp |= MAY_BE_TRUE|MAY_BE_FALSE;
2386 } else {
2387 tmp |= 1 << opline->extended_value;
2388 if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2389 if ((tmp & MAY_BE_ANY) == (t1 & MAY_BE_ANY)) {
2390 tmp |= (t1 & MAY_BE_RC1) | MAY_BE_RCN;
2391 } else if ((opline->extended_value == IS_ARRAY ||
2392 opline->extended_value == IS_OBJECT) &&
2393 (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT))) {
2394 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2395 } else if (opline->extended_value == IS_STRING &&
2396 (t1 & (MAY_BE_STRING|MAY_BE_OBJECT))) {
2397 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2398 } else {
2399 tmp |= MAY_BE_RC1;
2400 }
2401 }
2402 }
2403 if (opline->extended_value == IS_ARRAY) {
2404 if (t1 & MAY_BE_ARRAY) {
2405 tmp |= t1 & (MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF);
2406 }
2407 if (t1 & MAY_BE_OBJECT) {
2408 tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2409 } else {
2410 tmp |= ((t1 & MAY_BE_ANY) << MAY_BE_ARRAY_SHIFT) | MAY_BE_ARRAY_KEY_LONG;
2411 }
2412 }
2413 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2414 break;
2415 case ZEND_QM_ASSIGN:
2416 case ZEND_JMP_SET:
2417 case ZEND_COALESCE:
2418 if (ssa_ops[i].op1_def >= 0) {
2419 tmp = t1;
2420 if ((t1 & (MAY_BE_RC1|MAY_BE_REF)) && (opline->op1_type == IS_CV)) {
2421 tmp |= MAY_BE_RCN;
2422 }
2423 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2424 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2425 }
2426 tmp = t1 & ~(MAY_BE_UNDEF|MAY_BE_REF);
2427 if (t1 & MAY_BE_UNDEF) {
2428 tmp |= MAY_BE_NULL;
2429 }
2430 if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2431 tmp |= (t1 & (MAY_BE_RC1|MAY_BE_RCN));
2432 if (opline->op1_type == IS_CV) {
2433 tmp |= MAY_BE_RCN;
2434 }
2435 }
2436 if (opline->opcode != ZEND_QM_ASSIGN) {
2437 /* COALESCE and JMP_SET result can't be null */
2438 tmp &= ~MAY_BE_NULL;
2439 if (opline->opcode == ZEND_JMP_SET) {
2440 /* JMP_SET result can't be false either */
2441 tmp &= ~MAY_BE_FALSE;
2442 }
2443 }
2444 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2445 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].result_def);
2446 break;
2447 case ZEND_ASSIGN_ADD:
2448 case ZEND_ASSIGN_SUB:
2449 case ZEND_ASSIGN_MUL:
2450 case ZEND_ASSIGN_DIV:
2451 case ZEND_ASSIGN_POW:
2452 case ZEND_ASSIGN_MOD:
2453 case ZEND_ASSIGN_SL:
2454 case ZEND_ASSIGN_SR:
2455 case ZEND_ASSIGN_BW_OR:
2456 case ZEND_ASSIGN_BW_AND:
2457 case ZEND_ASSIGN_BW_XOR:
2458 case ZEND_ASSIGN_CONCAT:
2459 orig = 0;
2460 tmp = 0;
2461 if (opline->extended_value == ZEND_ASSIGN_OBJ) {
2462 tmp |= MAY_BE_REF;
2463 orig = t1;
2464 t1 = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2465 t2 = OP1_DATA_INFO();
2466 } else if (opline->extended_value == ZEND_ASSIGN_DIM) {
2467 if (t1 & MAY_BE_ARRAY_OF_REF) {
2468 tmp |= MAY_BE_REF;
2469 }
2470 orig = t1;
2471 t1 = zend_array_element_type(t1, 1, 0);
2472 t2 = OP1_DATA_INFO();
2473 } else {
2474 if (t1 & MAY_BE_REF) {
2475 tmp |= MAY_BE_REF;
2476 }
2477 }
2478
2479 tmp |= binary_op_result_type(
2480 ssa, get_compound_assign_op(opline->opcode), t1, t2, ssa_ops[i].op1_def);
2481 if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY)) {
2482 tmp |= MAY_BE_RC1;
2483 }
2484 if (tmp & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2485 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2486 }
2487
2488 if (opline->extended_value == ZEND_ASSIGN_DIM) {
2489 if (opline->op1_type == IS_CV) {
2490 orig = assign_dim_result_type(orig, OP2_INFO(), tmp, opline->op2_type);
2491 UPDATE_SSA_TYPE(orig, ssa_ops[i].op1_def);
2492 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2493 }
2494 } else if (opline->extended_value == ZEND_ASSIGN_OBJ) {
2495 if (opline->op1_type == IS_CV) {
2496 if (!(orig & MAY_BE_REF)) {
2497 if (orig & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2498 orig &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
2499 orig |= MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN;
2500 }
2501 if (orig & MAY_BE_OBJECT) {
2502 orig |= (MAY_BE_RC1|MAY_BE_RCN);
2503 }
2504 }
2505 UPDATE_SSA_TYPE(orig, ssa_ops[i].op1_def);
2506 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2507 }
2508 } else {
2509 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2510 }
2511 if (ssa_ops[i].result_def >= 0) {
2512 if (opline->extended_value == ZEND_ASSIGN_DIM) {
2513 if (opline->op2_type == IS_UNUSED) {
2514 /* When appending to an array and the LONG_MAX key is already used
2515 * null will be returned. */
2516 tmp |= MAY_BE_NULL;
2517 }
2518 if (t2 & (MAY_BE_ARRAY | MAY_BE_OBJECT)) {
2519 /* Arrays and objects cannot be used as keys. */
2520 tmp |= MAY_BE_NULL;
2521 }
2522 if (t1 & (MAY_BE_ANY - (MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_STRING | MAY_BE_ARRAY))) {
2523 /* null and false are implicitly converted to array, anything else
2524 * results in a null return value. */
2525 tmp |= MAY_BE_NULL;
2526 }
2527 } else if (opline->extended_value == ZEND_ASSIGN_OBJ) {
2528 if (orig & (MAY_BE_ANY - (MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_OBJECT))) {
2529 /* null and false (and empty string) are implicitly converted to object,
2530 * anything else results in a null return value. */
2531 tmp |= MAY_BE_NULL;
2532 }
2533 }
2534 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2535 }
2536 break;
2537 case ZEND_PRE_INC:
2538 case ZEND_PRE_DEC:
2539 tmp = 0;
2540 if (t1 & MAY_BE_REF) {
2541 tmp |= MAY_BE_REF;
2542 }
2543 if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2544 tmp |= MAY_BE_RC1;
2545 if (ssa_ops[i].result_def >= 0) {
2546 tmp |= MAY_BE_RCN;
2547 }
2548 }
2549 if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
2550 if (!ssa_var_info[ssa_ops[i].op1_use].has_range ||
2551 (opline->opcode == ZEND_PRE_DEC &&
2552 (ssa_var_info[ssa_ops[i].op1_use].range.underflow ||
2553 ssa_var_info[ssa_ops[i].op1_use].range.min == ZEND_LONG_MIN)) ||
2554 (opline->opcode == ZEND_PRE_INC &&
2555 (ssa_var_info[ssa_ops[i].op1_use].range.overflow ||
2556 ssa_var_info[ssa_ops[i].op1_use].range.max == ZEND_LONG_MAX))) {
2557 /* may overflow */
2558 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2559 } else {
2560 tmp |= MAY_BE_LONG;
2561 }
2562 } else {
2563 if (t1 & MAY_BE_ERROR) {
2564 tmp |= MAY_BE_NULL;
2565 }
2566 if (t1 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
2567 if (opline->opcode == ZEND_PRE_INC) {
2568 tmp |= MAY_BE_LONG;
2569 } else {
2570 tmp |= MAY_BE_NULL;
2571 }
2572 }
2573 if (t1 & MAY_BE_LONG) {
2574 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2575 }
2576 if (t1 & MAY_BE_DOUBLE) {
2577 tmp |= MAY_BE_DOUBLE;
2578 }
2579 if (t1 & MAY_BE_STRING) {
2580 tmp |= MAY_BE_STRING | MAY_BE_LONG | MAY_BE_DOUBLE;
2581 }
2582 tmp |= t1 & (MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_RESOURCE | MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_KEY_ANY);
2583 }
2584 if (ssa_ops[i].op1_def >= 0) {
2585 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2586 }
2587 if (ssa_ops[i].result_def >= 0) {
2588 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2589 }
2590 break;
2591 case ZEND_POST_INC:
2592 case ZEND_POST_DEC:
2593 if (ssa_ops[i].result_def >= 0) {
2594 tmp = 0;
2595 if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2596 tmp |= MAY_BE_RC1|MAY_BE_RCN;
2597 }
2598 tmp |= t1 & ~(MAY_BE_UNDEF|MAY_BE_ERROR|MAY_BE_REF|MAY_BE_RCN);
2599 if (t1 & MAY_BE_UNDEF) {
2600 tmp |= MAY_BE_NULL;
2601 }
2602 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2603 }
2604 tmp = 0;
2605 if (t1 & MAY_BE_REF) {
2606 tmp |= MAY_BE_REF;
2607 }
2608 if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2609 tmp |= MAY_BE_RC1;
2610 }
2611 if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_LONG) {
2612 if (!ssa_var_info[ssa_ops[i].op1_use].has_range ||
2613 (opline->opcode == ZEND_PRE_DEC &&
2614 (ssa_var_info[ssa_ops[i].op1_use].range.underflow ||
2615 ssa_var_info[ssa_ops[i].op1_use].range.min == ZEND_LONG_MIN)) ||
2616 (opline->opcode == ZEND_PRE_INC &&
2617 (ssa_var_info[ssa_ops[i].op1_use].range.overflow ||
2618 ssa_var_info[ssa_ops[i].op1_use].range.max == ZEND_LONG_MAX))) {
2619 /* may overflow */
2620 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2621 } else {
2622 tmp |= MAY_BE_LONG;
2623 }
2624 } else {
2625 if (t1 & MAY_BE_ERROR) {
2626 tmp |= MAY_BE_NULL;
2627 }
2628 if (t1 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
2629 if (opline->opcode == ZEND_POST_INC) {
2630 tmp |= MAY_BE_LONG;
2631 } else {
2632 tmp |= MAY_BE_NULL;
2633 }
2634 }
2635 if (t1 & MAY_BE_LONG) {
2636 tmp |= MAY_BE_LONG | MAY_BE_DOUBLE;
2637 }
2638 if (t1 & MAY_BE_DOUBLE) {
2639 tmp |= MAY_BE_DOUBLE;
2640 }
2641 if (t1 & MAY_BE_STRING) {
2642 tmp |= MAY_BE_STRING | MAY_BE_LONG | MAY_BE_DOUBLE;
2643 }
2644 tmp |= t1 & (MAY_BE_FALSE | MAY_BE_TRUE | MAY_BE_RESOURCE | MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF | MAY_BE_ARRAY_KEY_ANY);
2645 }
2646 if (ssa_ops[i].op1_def >= 0) {
2647 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2648 }
2649 break;
2650 case ZEND_ASSIGN_DIM:
2651 if (opline->op1_type == IS_CV) {
2652 tmp = assign_dim_result_type(t1, t2, OP1_DATA_INFO(), opline->op2_type);
2653 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2654 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2655 }
2656 if (ssa_ops[i].result_def >= 0) {
2657 tmp = 0;
2658 if (t1 & MAY_BE_STRING) {
2659 tmp |= MAY_BE_STRING;
2660 }
2661 if (t1 & ((MAY_BE_ANY|MAY_BE_UNDEF) - MAY_BE_STRING)) {
2662 tmp |= (OP1_DATA_INFO() & (MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF));
2663
2664 if (opline->op2_type == IS_UNUSED) {
2665 /* When appending to an array and the LONG_MAX key is already used
2666 * null will be returned. */
2667 tmp |= MAY_BE_NULL;
2668 }
2669 if (t2 & (MAY_BE_ARRAY | MAY_BE_OBJECT)) {
2670 /* Arrays and objects cannot be used as keys. */
2671 tmp |= MAY_BE_NULL;
2672 }
2673 if (t1 & (MAY_BE_ANY - (MAY_BE_NULL | MAY_BE_FALSE | MAY_BE_STRING | MAY_BE_ARRAY))) {
2674 /* undef, null and false are implicitly converted to array, anything else
2675 * results in a null return value. */
2676 tmp |= MAY_BE_NULL;
2677 }
2678 }
2679 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2680 if (t1 & MAY_BE_OBJECT) {
2681 tmp |= MAY_BE_REF;
2682 }
2683 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2684 }
2685 if ((opline+1)->op1_type == IS_CV && ssa_ops[i+1].op1_def >= 0) {
2686 opline++;
2687 i++;
2688 tmp = OP1_INFO();
2689 if (tmp & (MAY_BE_ANY | MAY_BE_REF)) {
2690 if (tmp & MAY_BE_RC1) {
2691 tmp |= MAY_BE_RCN;
2692 }
2693 }
2694 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2695 }
2696 break;
2697 case ZEND_ASSIGN_OBJ:
2698 if (opline->op1_type == IS_CV) {
2699 tmp = t1;
2700 if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
2701 tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
2702 tmp |= MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN;
2703 }
2704 if (tmp & MAY_BE_OBJECT) {
2705 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2706 }
2707 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2708 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2709 }
2710 if (ssa_ops[i].result_def >= 0) {
2711 // TODO: ???
2712 tmp = MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2713 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2714 }
2715 if ((opline+1)->op1_type == IS_CV) {
2716 opline++;
2717 i++;
2718 tmp = OP1_INFO();
2719 if (tmp & (MAY_BE_ANY | MAY_BE_REF)) {
2720 if (tmp & MAY_BE_RC1) {
2721 tmp |= MAY_BE_RCN;
2722 }
2723 }
2724 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2725 }
2726 break;
2727 case ZEND_ASSIGN:
2728 if (opline->op2_type == IS_CV && ssa_ops[i].op2_def >= 0) {
2729 tmp = t2;
2730 if (tmp & (MAY_BE_ANY | MAY_BE_REF)) {
2731 if (tmp & MAY_BE_RC1) {
2732 tmp |= MAY_BE_RCN;
2733 }
2734 }
2735 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op2_def);
2736 }
2737 tmp = t2 & ~(MAY_BE_UNDEF|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN);
2738 if (t2 & MAY_BE_UNDEF) {
2739 tmp |= MAY_BE_NULL;
2740 }
2741 if (t1 & MAY_BE_REF) {
2742 tmp |= MAY_BE_REF;
2743 }
2744 if (t2 & MAY_BE_REF) {
2745 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2746 } else if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
2747 tmp |= t2 & (MAY_BE_RC1|MAY_BE_RCN);
2748 } else if (t2 & (MAY_BE_RC1|MAY_BE_RCN)) {
2749 tmp |= MAY_BE_RCN;
2750 }
2751 if (RETURN_VALUE_USED(opline) && (tmp & MAY_BE_RC1)) {
2752 tmp |= MAY_BE_RCN;
2753 }
2754 if (ssa_ops[i].op1_def >= 0) {
2755 if (ssa_var_info[ssa_ops[i].op1_def].use_as_double) {
2756 tmp &= ~MAY_BE_LONG;
2757 tmp |= MAY_BE_DOUBLE;
2758 }
2759 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2760 COPY_SSA_OBJ_TYPE(ssa_ops[i].op2_use, ssa_ops[i].op1_def);
2761 }
2762 if (ssa_ops[i].result_def >= 0) {
2763 UPDATE_SSA_TYPE(tmp & ~MAY_BE_REF, ssa_ops[i].result_def);
2764 COPY_SSA_OBJ_TYPE(ssa_ops[i].op2_use, ssa_ops[i].result_def);
2765 }
2766 break;
2767 case ZEND_ASSIGN_REF:
2768 // TODO: ???
2769 if (opline->op2_type == IS_CV) {
2770 tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
2771 if (t2 & MAY_BE_UNDEF) {
2772 tmp |= MAY_BE_NULL;
2773 }
2774 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op2_def);
2775 }
2776 if (opline->op2_type == IS_VAR && opline->extended_value == ZEND_RETURNS_FUNCTION) {
2777 tmp = (MAY_BE_REF | MAY_BE_RCN | MAY_BE_RC1 | t2) & ~MAY_BE_UNDEF;
2778 } else {
2779 tmp = (MAY_BE_REF | t2) & ~(MAY_BE_UNDEF|MAY_BE_ERROR|MAY_BE_RC1|MAY_BE_RCN);
2780 }
2781 if (t2 & MAY_BE_UNDEF) {
2782 tmp |= MAY_BE_NULL;
2783 }
2784 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2785 if (ssa_ops[i].result_def >= 0) {
2786 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2787 }
2788 break;
2789 case ZEND_BIND_GLOBAL:
2790 tmp = MAY_BE_REF | MAY_BE_ANY
2791 | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2792 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2793 break;
2794 case ZEND_BIND_STATIC:
2795 tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
2796 | (opline->extended_value ? MAY_BE_REF : (MAY_BE_RC1 | MAY_BE_RCN));
2797 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2798 break;
2799 case ZEND_SEND_VAR:
2800 if (ssa_ops[i].op1_def >= 0) {
2801 tmp = t1;
2802 if ((t1 & (MAY_BE_RC1|MAY_BE_REF)) && (opline->op1_type == IS_CV)) {
2803 tmp |= MAY_BE_RCN;
2804 }
2805 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2806 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2807 }
2808 break;
2809 case ZEND_BIND_LEXICAL:
2810 if (ssa_ops[i].op2_def >= 0) {
2811 if (opline->extended_value) {
2812 tmp = t2 | MAY_BE_REF;
2813 } else {
2814 tmp = t2 & ~(MAY_BE_RC1|MAY_BE_RCN);
2815 if (t2 & (MAY_BE_RC1|MAY_BE_RCN)) {
2816 tmp |= MAY_BE_RCN;
2817 }
2818 }
2819 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op2_def);
2820 COPY_SSA_OBJ_TYPE(ssa_ops[i].op2_use, ssa_ops[i].op2_def);
2821 }
2822 break;
2823 case ZEND_YIELD:
2824 if (ssa_ops[i].op1_def >= 0) {
2825 if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
2826 tmp = t1 | MAY_BE_REF;
2827 } else {
2828 tmp = t1 & ~(MAY_BE_RC1|MAY_BE_RCN);
2829 if (t1 & (MAY_BE_RC1|MAY_BE_RCN)) {
2830 tmp |= MAY_BE_RCN;
2831 }
2832 }
2833 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2834 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
2835 }
2836 if (ssa_ops[i].result_def >= 0) {
2837 tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF
2838 | MAY_BE_RC1 | MAY_BE_RCN;
2839 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2840 }
2841 break;
2842 case ZEND_SEND_VAR_EX:
2843 if (ssa_ops[i].op1_def >= 0) {
2844 tmp = (t1 & MAY_BE_UNDEF)|MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2845 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2846 }
2847 break;
2848 case ZEND_SEND_REF:
2849 if (ssa_ops[i].op1_def >= 0) {
2850 tmp = MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2851 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2852 }
2853 break;
2854 case ZEND_SEND_UNPACK:
2855 if (ssa_ops[i].op1_def >= 0) {
2856 tmp = t1;
2857 if (t1 & MAY_BE_ARRAY) {
2858 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2859 if (t1 & MAY_BE_ARRAY_OF_ANY) {
2860 /* SEND_UNPACK may acquire references into the array */
2861 tmp |= MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
2862 }
2863 }
2864 if (t1 & MAY_BE_OBJECT) {
2865 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
2866 }
2867 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
2868 }
2869 break;
2870 case ZEND_FAST_CONCAT:
2871 case ZEND_ROPE_INIT:
2872 case ZEND_ROPE_ADD:
2873 case ZEND_ROPE_END:
2874 UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN, ssa_ops[i].result_def);
2875 break;
2876 case ZEND_RECV:
2877 case ZEND_RECV_INIT:
2878 {
2879 /* Typehinting */
2880 zend_func_info *func_info;
2881 zend_arg_info *arg_info = NULL;
2882 if (op_array->arg_info && opline->op1.num <= op_array->num_args) {
2883 arg_info = &op_array->arg_info[opline->op1.num-1];
2884 }
2885
2886 ce = NULL;
2887 if (arg_info) {
2888 tmp = zend_fetch_arg_info(script, arg_info, &ce);
2889 if (opline->opcode == ZEND_RECV_INIT &&
2890 Z_CONSTANT_P(CRT_CONSTANT_EX(op_array, opline->op2, ssa->rt_constants))) {
2891 /* The constant may resolve to NULL */
2892 tmp |= MAY_BE_NULL;
2893 }
2894 if (arg_info->pass_by_reference) {
2895 tmp |= MAY_BE_REF;
2896 } else if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
2897 tmp |= MAY_BE_RC1|MAY_BE_RCN;
2898 }
2899 } else {
2900 tmp = MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN|MAY_BE_ANY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
2901 }
2902 func_info = ZEND_FUNC_INFO(op_array);
2903 if (func_info && (int)opline->op1.num-1 < func_info->num_args) {
2904 tmp = (tmp & (MAY_BE_RC1|MAY_BE_RCN|MAY_BE_REF)) |
2905 (tmp & func_info->arg_info[opline->op1.num-1].info.type);
2906 }
2907 #if 0
2908 /* We won't recieve unused arguments */
2909 if (ssa_vars[ssa_ops[i].result_def].use_chain < 0 &&
2910 ssa_vars[ssa_ops[i].result_def].phi_use_chain == NULL &&
2911 op_array->arg_info &&
2912 opline->op1.num <= op_array->num_args &&
2913 op_array->arg_info[opline->op1.num-1].class_name == NULL &&
2914 !op_array->arg_info[opline->op1.num-1].type_hint) {
2915 tmp = MAY_BE_UNDEF|MAY_BE_RCN;
2916 }
2917 #endif
2918 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2919 if (func_info &&
2920 (int)opline->op1.num-1 < func_info->num_args &&
2921 func_info->arg_info[opline->op1.num-1].info.ce) {
2922 UPDATE_SSA_OBJ_TYPE(
2923 func_info->arg_info[opline->op1.num-1].info.ce,
2924 func_info->arg_info[opline->op1.num-1].info.is_instanceof,
2925 ssa_ops[i].result_def);
2926 } else if (ce) {
2927 UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_ops[i].result_def);
2928 } else {
2929 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2930 }
2931 break;
2932 }
2933 case ZEND_DECLARE_CLASS:
2934 case ZEND_DECLARE_INHERITED_CLASS:
2935 case ZEND_DECLARE_ANON_CLASS:
2936 case ZEND_DECLARE_ANON_INHERITED_CLASS:
2937 UPDATE_SSA_TYPE(MAY_BE_CLASS, ssa_ops[i].result_def);
2938 if (script && (ce = zend_hash_find_ptr(&script->class_table, Z_STR_P(CRT_CONSTANT_EX(op_array, opline->op1, ssa->rt_constants)))) != NULL) {
2939 UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_ops[i].result_def);
2940 }
2941 break;
2942 case ZEND_FETCH_CLASS:
2943 UPDATE_SSA_TYPE(MAY_BE_CLASS, ssa_ops[i].result_def);
2944 if (opline->op2_type == IS_UNUSED) {
2945 switch (opline->extended_value & ZEND_FETCH_CLASS_MASK) {
2946 case ZEND_FETCH_CLASS_SELF:
2947 if (op_array->scope) {
2948 UPDATE_SSA_OBJ_TYPE(op_array->scope, 0, ssa_ops[i].result_def);
2949 } else {
2950 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2951 }
2952 break;
2953 case ZEND_FETCH_CLASS_PARENT:
2954 if (op_array->scope && op_array->scope->parent) {
2955 UPDATE_SSA_OBJ_TYPE(op_array->scope->parent, 0, ssa_ops[i].result_def);
2956 } else {
2957 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2958 }
2959 break;
2960 case ZEND_FETCH_CLASS_STATIC:
2961 default:
2962 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2963 break;
2964 }
2965 } else if (opline->op2_type == IS_CONST) {
2966 zval *zv = CRT_CONSTANT_EX(op_array, opline->op2, ssa->rt_constants);
2967 if (Z_TYPE_P(zv) == IS_STRING) {
2968 ce = get_class_entry(script, Z_STR_P(zv+1));
2969 UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_ops[i].result_def);
2970 } else {
2971 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2972 }
2973 } else {
2974 COPY_SSA_OBJ_TYPE(ssa_ops[i].op2_use, ssa_ops[i].result_def);
2975 }
2976 break;
2977 case ZEND_NEW:
2978 tmp = MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT;
2979 if (opline->op1_type == IS_CONST &&
2980 (ce = get_class_entry(script, Z_STR_P(CRT_CONSTANT_EX(op_array, opline->op1, ssa->rt_constants)+1))) != NULL) {
2981 UPDATE_SSA_OBJ_TYPE(ce, 0, ssa_ops[i].result_def);
2982 } else if ((t1 & MAY_BE_CLASS) && ssa_ops[i].op1_use >= 0 && ssa_var_info[ssa_ops[i].op1_use].ce) {
2983 UPDATE_SSA_OBJ_TYPE(ssa_var_info[ssa_ops[i].op1_use].ce, ssa_var_info[ssa_ops[i].op1_use].is_instanceof, ssa_ops[i].result_def);
2984 } else {
2985 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
2986 }
2987 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
2988 break;
2989 case ZEND_CLONE:
2990 UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT, ssa_ops[i].result_def);
2991 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].result_def);
2992 break;
2993 case ZEND_INIT_ARRAY:
2994 case ZEND_ADD_ARRAY_ELEMENT:
2995 if (opline->op1_type == IS_CV && ssa_ops[i].op1_def >= 0) {
2996 if (opline->extended_value & ZEND_ARRAY_ELEMENT_REF) {
2997 tmp = (MAY_BE_REF | t1) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
2998 if (t1 & MAY_BE_UNDEF) {
2999 tmp |= MAY_BE_NULL;
3000 }
3001 } else if ((t1 & (MAY_BE_REF|MAY_BE_RC1|MAY_BE_RCN)) == MAY_BE_REF) {
3002 tmp = (MAY_BE_REF | t1) & ~(MAY_BE_UNDEF|MAY_BE_RC1|MAY_BE_RCN);
3003 if (t1 & MAY_BE_UNDEF) {
3004 tmp |= MAY_BE_NULL;
3005 }
3006 } else if (t1 & MAY_BE_REF) {
3007 tmp = (MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | t1);
3008 } else {
3009 tmp = t1;
3010 if (t1 & MAY_BE_RC1) {
3011 tmp |= MAY_BE_RCN;
3012 }
3013 }
3014 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3015 }
3016 if (ssa_ops[i].result_def >= 0) {
3017 tmp = MAY_BE_RC1|MAY_BE_ARRAY;
3018 if (opline->op1_type != IS_UNUSED) {
3019 tmp |= (t1 & MAY_BE_ANY) << MAY_BE_ARRAY_SHIFT;
3020 if (t1 & MAY_BE_UNDEF) {
3021 tmp |= MAY_BE_ARRAY_OF_NULL;
3022 }
3023 if (opline->extended_value & ZEND_ARRAY_ELEMENT_REF) {
3024 tmp |= MAY_BE_ARRAY_OF_ANY|MAY_BE_ARRAY_OF_REF;
3025 }
3026 }
3027 if (ssa_ops[i].result_use >= 0) {
3028 tmp |= ssa_var_info[ssa_ops[i].result_use].type;
3029 }
3030 if (opline->op2_type == IS_UNUSED) {
3031 tmp |= MAY_BE_ARRAY_KEY_LONG;
3032 } else {
3033 if (t2 & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_DOUBLE)) {
3034 tmp |= MAY_BE_ARRAY_KEY_LONG;
3035 }
3036 if (t2 & (MAY_BE_STRING)) {
3037 tmp |= MAY_BE_ARRAY_KEY_STRING;
3038 if (opline->op2_type != IS_CONST) {
3039 // FIXME: numeric string
3040 tmp |= MAY_BE_ARRAY_KEY_LONG;
3041 }
3042 }
3043 if (t2 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
3044 tmp |= MAY_BE_ARRAY_KEY_STRING;
3045 }
3046 }
3047 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3048 }
3049 break;
3050 case ZEND_UNSET_CV:
3051 tmp = MAY_BE_UNDEF;
3052 if (!op_array->function_name) {
3053 /* In global scope, we know nothing */
3054 tmp |= MAY_BE_REF;
3055 }
3056 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3057 break;
3058 case ZEND_UNSET_DIM:
3059 case ZEND_UNSET_OBJ:
3060 if (ssa_ops[i].op1_def >= 0) {
3061 UPDATE_SSA_TYPE(t1, ssa_ops[i].op1_def);
3062 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
3063 }
3064 break;
3065 case ZEND_FE_RESET_R:
3066 case ZEND_FE_RESET_RW:
3067 if (ssa_ops[i].op1_def >= 0) {
3068 tmp = t1;
3069 if (opline->opcode == ZEND_FE_RESET_RW) {
3070 tmp |= MAY_BE_REF;
3071 } else {
3072 if ((t1 & MAY_BE_RC1) && opline->op1_type != IS_TMP_VAR) {
3073 tmp |= MAY_BE_RCN;
3074 }
3075 }
3076 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3077 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
3078 }
3079 if (opline->opcode == ZEND_FE_RESET_RW) {
3080 //???
3081 tmp = MAY_BE_REF | (t1 & (MAY_BE_ARRAY | MAY_BE_OBJECT));
3082 } else {
3083 tmp = MAY_BE_RC1 | MAY_BE_RCN | (t1 & (MAY_BE_ARRAY | MAY_BE_OBJECT | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF));
3084 }
3085 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3086 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].result_def);
3087 break;
3088 case ZEND_FE_FETCH_R:
3089 case ZEND_FE_FETCH_RW:
3090 tmp = t2;
3091 if (t1 & MAY_BE_OBJECT) {
3092 if (opline->opcode == ZEND_FE_FETCH_RW) {
3093 tmp |= MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3094 } else {
3095 tmp |= MAY_BE_REF | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3096 }
3097 }
3098 if (t1 & MAY_BE_ARRAY) {
3099 if (opline->opcode == ZEND_FE_FETCH_RW) {
3100 tmp |= MAY_BE_REF | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3101 } else {
3102 tmp |= ((t1 & MAY_BE_ARRAY_OF_ANY) >> MAY_BE_ARRAY_SHIFT);
3103 if (tmp & MAY_BE_ARRAY) {
3104 tmp |= MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3105 }
3106 if (t1 & MAY_BE_ARRAY_OF_REF) {
3107 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3108 } else if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3109 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3110 }
3111 }
3112 }
3113 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op2_def);
3114 if (ssa_ops[i].result_def >= 0) {
3115 tmp = (ssa_ops[i].result_use >= 0) ? RES_USE_INFO() : 0;
3116 if (t1 & MAY_BE_OBJECT) {
3117 tmp |= MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3118 }
3119 if (t1 & MAY_BE_ARRAY) {
3120 if (t1 & MAY_BE_ARRAY_KEY_LONG) {
3121 tmp |= MAY_BE_LONG;
3122 }
3123 if (t1 & MAY_BE_ARRAY_KEY_STRING) {
3124 tmp |= MAY_BE_STRING | MAY_BE_RCN;
3125 }
3126 }
3127 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3128 }
3129 break;
3130 case ZEND_FETCH_DIM_R:
3131 case ZEND_FETCH_DIM_IS:
3132 case ZEND_FETCH_DIM_RW:
3133 case ZEND_FETCH_DIM_W:
3134 case ZEND_FETCH_DIM_UNSET:
3135 case ZEND_FETCH_DIM_FUNC_ARG:
3136 case ZEND_FETCH_LIST:
3137 if (ssa_ops[i].op1_def >= 0) {
3138 tmp = t1 & ~(MAY_BE_RC1|MAY_BE_RCN);
3139 if (opline->opcode == ZEND_FETCH_DIM_W ||
3140 opline->opcode == ZEND_FETCH_DIM_RW ||
3141 opline->opcode == ZEND_FETCH_DIM_FUNC_ARG) {
3142 if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3143 if (opline->opcode != ZEND_FETCH_DIM_FUNC_ARG) {
3144 tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
3145 }
3146 tmp |= MAY_BE_ARRAY | MAY_BE_RC1;
3147 }
3148 if (t1 & (MAY_BE_STRING|MAY_BE_ARRAY)) {
3149 tmp |= MAY_BE_RC1;
3150 if (opline->opcode == ZEND_FETCH_DIM_FUNC_ARG) {
3151 tmp |= t1 & MAY_BE_RCN;
3152 }
3153 }
3154 if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3155 tmp |= t1 & (MAY_BE_RC1|MAY_BE_RCN);
3156 }
3157 if (opline->op2_type == IS_UNUSED) {
3158 tmp |= MAY_BE_ARRAY_KEY_LONG;
3159 } else {
3160 if (t2 & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
3161 tmp |= MAY_BE_ARRAY_KEY_LONG;
3162 }
3163 if (t2 & MAY_BE_STRING) {
3164 tmp |= MAY_BE_ARRAY_KEY_STRING;
3165 if (opline->op2_type != IS_CONST) {
3166 // FIXME: numeric string
3167 tmp |= MAY_BE_ARRAY_KEY_LONG;
3168 }
3169 }
3170 if (t2 & (MAY_BE_UNDEF | MAY_BE_NULL)) {
3171 tmp |= MAY_BE_ARRAY_KEY_STRING;
3172 }
3173 }
3174 } else if (opline->opcode == ZEND_FETCH_DIM_UNSET) {
3175 if (t1 & MAY_BE_ARRAY) {
3176 tmp |= MAY_BE_RC1;
3177 }
3178 if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3179 tmp |= t1 & (MAY_BE_RC1|MAY_BE_RCN);
3180 }
3181 }
3182 j = ssa_vars[ssa_ops[i].result_def].use_chain;
3183 while (j >= 0) {
3184 switch (op_array->opcodes[j].opcode) {
3185 case ZEND_FETCH_DIM_W:
3186 case ZEND_FETCH_DIM_RW:
3187 case ZEND_FETCH_DIM_FUNC_ARG:
3188 case ZEND_ASSIGN_DIM:
3189 tmp |= MAY_BE_ARRAY | MAY_BE_ARRAY_OF_ARRAY;
3190 break;
3191 case ZEND_ASSIGN_ADD:
3192 case ZEND_ASSIGN_SUB:
3193 case ZEND_ASSIGN_MUL:
3194 case ZEND_ASSIGN_DIV:
3195 case ZEND_ASSIGN_MOD:
3196 case ZEND_ASSIGN_SL:
3197 case ZEND_ASSIGN_SR:
3198 case ZEND_ASSIGN_CONCAT:
3199 case ZEND_ASSIGN_BW_OR:
3200 case ZEND_ASSIGN_BW_AND:
3201 case ZEND_ASSIGN_BW_XOR:
3202 case ZEND_ASSIGN_POW:
3203 if (op_array->opcodes[j].extended_value == ZEND_ASSIGN_DIM) {
3204 tmp |= MAY_BE_ARRAY | MAY_BE_ARRAY_OF_ARRAY;
3205 } else if (op_array->opcodes[j].extended_value == ZEND_ASSIGN_OBJ) {
3206 tmp |= MAY_BE_ARRAY_OF_OBJECT;
3207 }
3208 break;
3209 case ZEND_FETCH_OBJ_W:
3210 case ZEND_FETCH_OBJ_RW:
3211 case ZEND_FETCH_OBJ_FUNC_ARG:
3212 case ZEND_ASSIGN_OBJ:
3213 case ZEND_PRE_INC_OBJ:
3214 case ZEND_PRE_DEC_OBJ:
3215 case ZEND_POST_INC_OBJ:
3216 case ZEND_POST_DEC_OBJ:
3217 tmp |= MAY_BE_ARRAY_OF_OBJECT;
3218 break;
3219 case ZEND_SEND_VAR_EX:
3220 case ZEND_SEND_VAR_NO_REF:
3221 case ZEND_SEND_VAR_NO_REF_EX:
3222 case ZEND_SEND_REF:
3223 case ZEND_ASSIGN_REF:
3224 case ZEND_YIELD:
3225 case ZEND_INIT_ARRAY:
3226 case ZEND_ADD_ARRAY_ELEMENT:
3227 case ZEND_RETURN_BY_REF:
3228 case ZEND_VERIFY_RETURN_TYPE:
3229 case ZEND_MAKE_REF:
3230 case ZEND_FE_RESET_RW:
3231 tmp |= MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3232 break;
3233 case ZEND_PRE_INC:
3234 case ZEND_PRE_DEC:
3235 case ZEND_POST_INC:
3236 case ZEND_POST_DEC:
3237 if (tmp & MAY_BE_ARRAY_OF_LONG) {
3238 /* may overflow */
3239 tmp |= MAY_BE_ARRAY_OF_DOUBLE;
3240 } else if (!(tmp & (MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_DOUBLE))) {
3241 tmp |= MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE;
3242 }
3243 break;
3244 case ZEND_UNSET_DIM:
3245 case ZEND_UNSET_OBJ:
3246 case ZEND_FETCH_DIM_UNSET:
3247 case ZEND_FETCH_OBJ_UNSET:
3248 break;
3249 default :
3250 break;
3251 }
3252 j = zend_ssa_next_use(ssa_ops, ssa_ops[i].result_def, j);
3253 }
3254 if ((tmp & MAY_BE_ARRAY) && (tmp & MAY_BE_ARRAY_KEY_ANY)) {
3255 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3256 } else {
3257 /* invalid key type */
3258 tmp = (tmp & (MAY_BE_RC1|MAY_BE_RCN)) | (t1 & ~(MAY_BE_RC1|MAY_BE_RCN));
3259 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3260 }
3261 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
3262 }
3263 /* FETCH_LIST on a string behaves like FETCH_R on null */
3264 tmp = zend_array_element_type(
3265 opline->opcode != ZEND_FETCH_LIST ? t1 : ((t1 & ~MAY_BE_STRING) | MAY_BE_NULL),
3266 opline->opcode != ZEND_FETCH_DIM_R && opline->opcode != ZEND_FETCH_DIM_IS
3267 && opline->opcode != ZEND_FETCH_LIST,
3268 opline->op2_type == IS_UNUSED);
3269 if (opline->opcode == ZEND_FETCH_DIM_W ||
3270 opline->opcode == ZEND_FETCH_DIM_RW ||
3271 opline->opcode == ZEND_FETCH_DIM_FUNC_ARG) {
3272 if (t1 & (MAY_BE_ERROR|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_RESOURCE|MAY_BE_OBJECT)) {
3273 tmp |= MAY_BE_ERROR;
3274 } else if (opline->op2_type == IS_UNUSED) {
3275 tmp |= MAY_BE_ERROR;
3276 } else if (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) {
3277 tmp |= MAY_BE_ERROR;
3278 }
3279 } else if (opline->opcode == ZEND_FETCH_DIM_IS && (t1 & MAY_BE_STRING)) {
3280 tmp |= MAY_BE_NULL;
3281 }
3282 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3283 break;
3284 case ZEND_FETCH_THIS:
3285 UPDATE_SSA_OBJ_TYPE(op_array->scope, 1, ssa_ops[i].result_def);
3286 UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_OBJECT, ssa_ops[i].result_def);
3287 break;
3288 case ZEND_FETCH_OBJ_R:
3289 case ZEND_FETCH_OBJ_IS:
3290 case ZEND_FETCH_OBJ_RW:
3291 case ZEND_FETCH_OBJ_W:
3292 case ZEND_FETCH_OBJ_UNSET:
3293 case ZEND_FETCH_OBJ_FUNC_ARG:
3294 if (ssa_ops[i].op1_def >= 0) {
3295 tmp = t1;
3296 if (opline->opcode == ZEND_FETCH_OBJ_W ||
3297 opline->opcode == ZEND_FETCH_OBJ_RW ||
3298 opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG) {
3299 if (opline->opcode != ZEND_FETCH_DIM_FUNC_ARG) {
3300 if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
3301 tmp &= ~(MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE);
3302 tmp |= MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN;
3303 }
3304 }
3305 }
3306 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3307 COPY_SSA_OBJ_TYPE(ssa_ops[i].op1_use, ssa_ops[i].op1_def);
3308 }
3309 if (ssa_ops[i].result_def >= 0) {
3310 tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3311 if (opline->opcode != ZEND_FETCH_OBJ_R && opline->opcode != ZEND_FETCH_OBJ_IS) {
3312 tmp |= MAY_BE_ERROR;
3313 }
3314 if (opline->result_type == IS_TMP_VAR) {
3315 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3316 } else {
3317 tmp |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN;
3318 }
3319 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3320 }
3321 break;
3322 case ZEND_DO_FCALL:
3323 case ZEND_DO_ICALL:
3324 case ZEND_DO_UCALL:
3325 case ZEND_DO_FCALL_BY_NAME:
3326 if (ssa_ops[i].result_def >= 0) {
3327 zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
3328 zend_call_info *call_info;
3329
3330 if (!func_info || !func_info->call_map) {
3331 goto unknown_opcode;
3332 }
3333 call_info = func_info->call_map[opline - op_array->opcodes];
3334 if (!call_info) {
3335 goto unknown_opcode;
3336 }
3337 tmp = zend_get_func_info(call_info, ssa) & ~FUNC_MAY_WARN;
3338 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3339 if (call_info->callee_func->type == ZEND_USER_FUNCTION) {
3340 func_info = ZEND_FUNC_INFO(&call_info->callee_func->op_array);
3341 if (func_info) {
3342 UPDATE_SSA_OBJ_TYPE(
3343 func_info->return_info.ce,
3344 func_info->return_info.is_instanceof,
3345 ssa_ops[i].result_def);
3346 }
3347 }
3348 }
3349 break;
3350 case ZEND_FETCH_CONSTANT:
3351 case ZEND_FETCH_CLASS_CONSTANT:
3352 UPDATE_SSA_TYPE(MAY_BE_RC1|MAY_BE_RCN|MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING|MAY_BE_RESOURCE|MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_ANY|MAY_BE_ARRAY_OF_ANY, ssa_ops[i].result_def);
3353 break;
3354 case ZEND_STRLEN:
3355 tmp = MAY_BE_LONG;
3356 if (t1 & (MAY_BE_ANY - (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING))) {
3357 tmp |= MAY_BE_NULL;
3358 }
3359 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3360 break;
3361 case ZEND_COUNT:
3362 case ZEND_FUNC_NUM_ARGS:
3363 UPDATE_SSA_TYPE(MAY_BE_LONG, ssa_ops[i].result_def);
3364 break;
3365 case ZEND_FUNC_GET_ARGS:
3366 UPDATE_SSA_TYPE(MAY_BE_RC1| MAY_BE_ARRAY | MAY_BE_ARRAY_KEY_LONG | MAY_BE_ARRAY_OF_ANY, ssa_ops[i].result_def);
3367 break;
3368 case ZEND_GET_CLASS:
3369 case ZEND_GET_CALLED_CLASS:
3370 UPDATE_SSA_TYPE(MAY_BE_FALSE|MAY_BE_STRING|MAY_BE_RCN, ssa_ops[i].result_def);
3371 break;
3372 case ZEND_GET_TYPE:
3373 UPDATE_SSA_TYPE(MAY_BE_STRING|MAY_BE_RC1|MAY_BE_RCN, ssa_ops[i].result_def);
3374 break;
3375 case ZEND_TYPE_CHECK:
3376 case ZEND_DEFINED:
3377 UPDATE_SSA_TYPE(MAY_BE_FALSE|MAY_BE_TRUE, ssa_ops[i].result_def);
3378 break;
3379 case ZEND_VERIFY_RETURN_TYPE:
3380 if (t1 & MAY_BE_REF) {
3381 tmp = t1;
3382 ce = NULL;
3383 } else {
3384 zend_arg_info *ret_info = op_array->arg_info - 1;
3385
3386 tmp = zend_fetch_arg_info(script, ret_info, &ce);
3387 if (tmp & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3388 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3389 }
3390 }
3391 if (opline->op1_type & (IS_TMP_VAR|IS_VAR|IS_CV)) {
3392 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3393 if (ce) {
3394 UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_ops[i].op1_def);
3395 } else {
3396 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].op1_def);
3397 }
3398 } else {
3399 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3400 if (ce) {
3401 UPDATE_SSA_OBJ_TYPE(ce, 1, ssa_ops[i].result_def);
3402 } else {
3403 UPDATE_SSA_OBJ_TYPE(NULL, 0, ssa_ops[i].result_def);
3404 }
3405 }
3406 break;
3407 case ZEND_CATCH:
3408 case ZEND_INCLUDE_OR_EVAL:
3409 /* Forbidden opcodes */
3410 ZEND_ASSERT(0);
3411 break;
3412 default:
3413 unknown_opcode:
3414 if (ssa_ops[i].op1_def >= 0) {
3415 tmp = MAY_BE_ANY | MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3416 UPDATE_SSA_TYPE(tmp, ssa_ops[i].op1_def);
3417 }
3418 if (ssa_ops[i].result_def >= 0) {
3419 tmp = MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
3420 if (opline->result_type == IS_TMP_VAR) {
3421 tmp |= MAY_BE_RC1 | MAY_BE_RCN;
3422 } else {
3423 tmp |= MAY_BE_REF | MAY_BE_RC1 | MAY_BE_RCN;
3424 }
3425 UPDATE_SSA_TYPE(tmp, ssa_ops[i].result_def);
3426 }
3427 break;
3428 }
3429
3430 return SUCCESS;
3431 }
3432
get_class_entry_rank(zend_class_entry * ce)3433 static uint32_t get_class_entry_rank(zend_class_entry *ce) {
3434 uint32_t rank = 0;
3435 while (ce->parent) {
3436 rank++;
3437 ce = ce->parent;
3438 }
3439 return rank;
3440 }
3441
3442 /* Compute least common ancestor on class inheritance tree only */
join_class_entries(zend_class_entry * ce1,zend_class_entry * ce2,int * is_instanceof)3443 static zend_class_entry *join_class_entries(
3444 zend_class_entry *ce1, zend_class_entry *ce2, int *is_instanceof) {
3445 uint32_t rank1, rank2;
3446 if (ce1 == ce2) {
3447 return ce1;
3448 }
3449 if (!ce1 || !ce2) {
3450 return NULL;
3451 }
3452
3453 rank1 = get_class_entry_rank(ce1);
3454 rank2 = get_class_entry_rank(ce2);
3455
3456 while (rank1 != rank2) {
3457 if (rank1 > rank2) {
3458 ce1 = ce1->parent;
3459 rank1--;
3460 } else {
3461 ce2 = ce2->parent;
3462 rank2--;
3463 }
3464 }
3465
3466 while (ce1 != ce2) {
3467 ce1 = ce1->parent;
3468 ce2 = ce2->parent;
3469 }
3470
3471 if (ce1) {
3472 *is_instanceof = 1;
3473 }
3474 return ce1;
3475 }
3476
zend_infer_types_ex(const zend_op_array * op_array,const zend_script * script,zend_ssa * ssa,zend_bitset worklist)3477 int zend_infer_types_ex(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa, zend_bitset worklist)
3478 {
3479 zend_basic_block *blocks = ssa->cfg.blocks;
3480 zend_ssa_var *ssa_vars = ssa->vars;
3481 zend_ssa_var_info *ssa_var_info = ssa->var_info;
3482 int ssa_vars_count = ssa->vars_count;
3483 int i, j;
3484 uint32_t tmp, worklist_len = zend_bitset_len(ssa_vars_count);
3485
3486 while (!zend_bitset_empty(worklist, worklist_len)) {
3487 j = zend_bitset_first(worklist, worklist_len);
3488 zend_bitset_excl(worklist, j);
3489 if (ssa_vars[j].definition_phi) {
3490 zend_ssa_phi *p = ssa_vars[j].definition_phi;
3491 if (p->pi >= 0) {
3492 zend_class_entry *ce = ssa_var_info[p->sources[0]].ce;
3493 int is_instanceof = ssa_var_info[p->sources[0]].is_instanceof;
3494 tmp = get_ssa_var_info(ssa, p->sources[0]);
3495
3496 if (!p->has_range_constraint) {
3497 zend_ssa_type_constraint *constraint = &p->constraint.type;
3498 tmp &= constraint->type_mask;
3499 if ((tmp & MAY_BE_OBJECT) && constraint->ce && ce != constraint->ce) {
3500 if (!ce) {
3501 ce = constraint->ce;
3502 is_instanceof = 1;
3503 } else if (is_instanceof && instanceof_function(constraint->ce, ce)) {
3504 ce = constraint->ce;
3505 } else {
3506 /* Ignore the constraint (either ce instanceof constraint->ce or
3507 * they are unrelated, as far as we can statically determine) */
3508 }
3509 }
3510 }
3511
3512 UPDATE_SSA_TYPE(tmp, j);
3513 UPDATE_SSA_OBJ_TYPE(ce, is_instanceof, j);
3514 } else {
3515 int first = 1;
3516 int is_instanceof = 0;
3517 zend_class_entry *ce = NULL;
3518
3519 tmp = 0;
3520 for (i = 0; i < blocks[p->block].predecessors_count; i++) {
3521 tmp |= get_ssa_var_info(ssa, p->sources[i]);
3522 }
3523 UPDATE_SSA_TYPE(tmp, j);
3524 for (i = 0; i < blocks[p->block].predecessors_count; i++) {
3525 zend_ssa_var_info *info;
3526
3527 ZEND_ASSERT(p->sources[i] >= 0);
3528 info = &ssa_var_info[p->sources[i]];
3529 if (info->type & MAY_BE_OBJECT) {
3530 if (first) {
3531 ce = info->ce;
3532 is_instanceof = info->is_instanceof;
3533 first = 0;
3534 } else {
3535 is_instanceof |= info->is_instanceof;
3536 ce = join_class_entries(ce, info->ce, &is_instanceof);
3537 }
3538 }
3539 }
3540 UPDATE_SSA_OBJ_TYPE(ce, ce ? is_instanceof : 0, j);
3541 }
3542 } else if (ssa_vars[j].definition >= 0) {
3543 i = ssa_vars[j].definition;
3544 if (zend_update_type_info(op_array, ssa, script, worklist, i) == FAILURE) {
3545 return FAILURE;
3546 }
3547 }
3548 }
3549 return SUCCESS;
3550 }
3551
is_narrowable_instr(zend_op * opline)3552 static zend_bool is_narrowable_instr(zend_op *opline) {
3553 return opline->opcode == ZEND_ADD || opline->opcode == ZEND_SUB
3554 || opline->opcode == ZEND_MUL || opline->opcode == ZEND_DIV;
3555 }
3556
is_effective_op1_double_cast(zend_op * opline,zval * op2)3557 static zend_bool is_effective_op1_double_cast(zend_op *opline, zval *op2) {
3558 return (opline->opcode == ZEND_ADD && Z_LVAL_P(op2) == 0)
3559 || (opline->opcode == ZEND_SUB && Z_LVAL_P(op2) == 0)
3560 || (opline->opcode == ZEND_MUL && Z_LVAL_P(op2) == 1)
3561 || (opline->opcode == ZEND_DIV && Z_LVAL_P(op2) == 1);
3562 }
is_effective_op2_double_cast(zend_op * opline,zval * op1)3563 static zend_bool is_effective_op2_double_cast(zend_op *opline, zval *op1) {
3564 /* In PHP it holds that (double)(0-$int) is bitwise identical to 0.0-(double)$int,
3565 * so allowing SUB here is fine. */
3566 return (opline->opcode == ZEND_ADD && Z_LVAL_P(op1) == 0)
3567 || (opline->opcode == ZEND_SUB && Z_LVAL_P(op1) == 0)
3568 || (opline->opcode == ZEND_MUL && Z_LVAL_P(op1) == 1);
3569 }
3570
3571 /* This function recursively checks whether it's possible to convert an integer variable
3572 * initialization to a double initialization. The basic idea is that if the value is used
3573 * only in add/sub/mul/div ("narrowable" instructions) with a double result value, then it
3574 * will be cast to double at that point anyway, so we may as well do it earlier already.
3575 *
3576 * The tricky case are chains of operations, where it's not necessarily a given that converting
3577 * an integer to double before the chain of operations is the same as converting it after the
3578 * chain. What this function does is detect two cases where it is safe:
3579 * * If the operations only involve constants, then we can simply verify that performing the
3580 * calculation on integers and doubles yields the same value.
3581 * * Even if one operand is not known, we may be able to determine that the operations with the
3582 * integer replaced by a double only acts as an effective double cast on the unknown operand.
3583 * E.g. 0+$i and 0.0+$i only differ by that cast. If then the consuming instruction of this
3584 * result will perform a double cast anyway, the conversion is safe.
3585 *
3586 * The checks happens recursively, while keeping track of which variables are already visisted to
3587 * avoid infinite loops. An iterative, worklist driven approach would be possible, but the state
3588 * management more cumbersome to implement, so we don't bother for now.
3589 */
can_convert_to_double(const zend_op_array * op_array,zend_ssa * ssa,int var_num,zval * value,zend_bitset visited)3590 static zend_bool can_convert_to_double(
3591 const zend_op_array *op_array, zend_ssa *ssa, int var_num,
3592 zval *value, zend_bitset visited) {
3593 zend_ssa_var *var = &ssa->vars[var_num];
3594 zend_ssa_phi *phi;
3595 int use;
3596 uint32_t type;
3597
3598 if (zend_bitset_in(visited, var_num)) {
3599 return 1;
3600 }
3601 zend_bitset_incl(visited, var_num);
3602
3603 for (use = var->use_chain; use >= 0; use = zend_ssa_next_use(ssa->ops, var_num, use)) {
3604 zend_op *opline = &op_array->opcodes[use];
3605 zend_ssa_op *ssa_op = &ssa->ops[use];
3606
3607 if (zend_ssa_is_no_val_use(opline, ssa_op, var_num)) {
3608 continue;
3609 }
3610
3611 if (!is_narrowable_instr(opline)) {
3612 return 0;
3613 }
3614
3615 /* Instruction always returns double, the conversion is certainly fine */
3616 type = ssa->var_info[ssa_op->result_def].type;
3617 if ((type & MAY_BE_ANY) == MAY_BE_DOUBLE) {
3618 continue;
3619 }
3620
3621 /* UNDEF signals that the previous result is an effective double cast, this is only allowed
3622 * if this instruction would have done the cast anyway (previous check). */
3623 if (Z_ISUNDEF_P(value)) {
3624 return 0;
3625 }
3626
3627 /* Check that narrowing can actually be useful */
3628 if ((type & MAY_BE_ANY) & ~(MAY_BE_LONG|MAY_BE_DOUBLE)) {
3629 return 0;
3630 }
3631
3632 {
3633 /* For calculation on original values */
3634 zval orig_op1, orig_op2, orig_result;
3635 /* For calculation with var_num cast to double */
3636 zval dval_op1, dval_op2, dval_result;
3637
3638 ZVAL_UNDEF(&orig_op1);
3639 ZVAL_UNDEF(&dval_op1);
3640 if (ssa_op->op1_use == var_num) {
3641 ZVAL_COPY_VALUE(&orig_op1, value);
3642 ZVAL_DOUBLE(&dval_op1, (double) Z_LVAL_P(value));
3643 } else if (opline->op1_type == IS_CONST) {
3644 zval *zv = CRT_CONSTANT_EX(op_array, opline->op1, ssa->rt_constants);
3645 if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_DOUBLE) {
3646 ZVAL_COPY_VALUE(&orig_op1, zv);
3647 ZVAL_COPY_VALUE(&dval_op1, zv);
3648 }
3649 }
3650
3651 ZVAL_UNDEF(&orig_op2);
3652 ZVAL_UNDEF(&dval_op2);
3653 if (ssa_op->op2_use == var_num) {
3654 ZVAL_COPY_VALUE(&orig_op2, value);
3655 ZVAL_DOUBLE(&dval_op2, (double) Z_LVAL_P(value));
3656 } else if (opline->op2_type == IS_CONST) {
3657 zval *zv = CRT_CONSTANT_EX(op_array, opline->op2, ssa->rt_constants);
3658 if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_DOUBLE) {
3659 ZVAL_COPY_VALUE(&orig_op2, zv);
3660 ZVAL_COPY_VALUE(&dval_op2, zv);
3661 }
3662 }
3663
3664 ZEND_ASSERT(!Z_ISUNDEF(orig_op1) || !Z_ISUNDEF(orig_op2));
3665 if (Z_ISUNDEF(orig_op1)) {
3666 if (opline->opcode == ZEND_MUL && Z_LVAL(orig_op2) == 0) {
3667 ZVAL_LONG(&orig_result, 0);
3668 } else if (is_effective_op1_double_cast(opline, &orig_op2)) {
3669 ZVAL_UNDEF(&orig_result);
3670 } else {
3671 return 0;
3672 }
3673 } else if (Z_ISUNDEF(orig_op2)) {
3674 if (opline->opcode == ZEND_MUL && Z_LVAL(orig_op1) == 0) {
3675 ZVAL_LONG(&orig_result, 0);
3676 } else if (is_effective_op2_double_cast(opline, &orig_op1)) {
3677 ZVAL_UNDEF(&orig_result);
3678 } else {
3679 return 0;
3680 }
3681 } else {
3682 /* Avoid division by zero */
3683 if (opline->opcode == ZEND_DIV && zval_get_double(&orig_op2) == 0.0) {
3684 return 0;
3685 }
3686
3687 get_binary_op(opline->opcode)(&orig_result, &orig_op1, &orig_op2);
3688 get_binary_op(opline->opcode)(&dval_result, &dval_op1, &dval_op2);
3689 ZEND_ASSERT(Z_TYPE(dval_result) == IS_DOUBLE);
3690 if (zval_get_double(&orig_result) != Z_DVAL(dval_result)) {
3691 return 0;
3692 }
3693 }
3694
3695 if (!can_convert_to_double(op_array, ssa, ssa_op->result_def, &orig_result, visited)) {
3696 return 0;
3697 }
3698 }
3699 }
3700
3701 for (phi = var->phi_use_chain; phi; phi = zend_ssa_next_use_phi(ssa, var_num, phi)) {
3702 /* Check that narrowing can actually be useful */
3703 type = ssa->var_info[phi->ssa_var].type;
3704 if ((type & MAY_BE_ANY) & ~(MAY_BE_LONG|MAY_BE_DOUBLE)) {
3705 return 0;
3706 }
3707
3708 if (!can_convert_to_double(op_array, ssa, phi->ssa_var, value, visited)) {
3709 return 0;
3710 }
3711 }
3712
3713 return 1;
3714 }
3715
zend_type_narrowing(const zend_op_array * op_array,const zend_script * script,zend_ssa * ssa)3716 static int zend_type_narrowing(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa)
3717 {
3718 uint32_t bitset_len = zend_bitset_len(ssa->vars_count);
3719 zend_bitset visited, worklist;
3720 int i, v;
3721 zend_op *opline;
3722 zend_bool narrowed = 0;
3723 ALLOCA_FLAG(use_heap)
3724
3725 visited = ZEND_BITSET_ALLOCA(2 * bitset_len, use_heap);
3726 worklist = visited + bitset_len;
3727
3728 zend_bitset_clear(worklist, bitset_len);
3729
3730 for (v = op_array->last_var; v < ssa->vars_count; v++) {
3731 if ((ssa->var_info[v].type & (MAY_BE_REF | MAY_BE_ANY | MAY_BE_UNDEF)) != MAY_BE_LONG) continue;
3732 if (ssa->vars[v].definition < 0) continue;
3733 if (ssa->vars[v].no_val) continue;
3734 opline = op_array->opcodes + ssa->vars[v].definition;
3735 /* Go through assignments of literal integers and check if they can be converted to
3736 * doubles instead, in the hope that we'll narrow long|double to double. */
3737 if (opline->opcode == ZEND_ASSIGN && opline->result_type == IS_UNUSED &&
3738 opline->op1_type == IS_CV && opline->op2_type == IS_CONST) {
3739 zval *value = CRT_CONSTANT_EX(op_array, opline->op2, ssa->rt_constants);
3740
3741 zend_bitset_clear(visited, bitset_len);
3742 if (can_convert_to_double(op_array, ssa, v, value, visited)) {
3743 narrowed = 1;
3744 ssa->var_info[v].use_as_double = 1;
3745 /* The "visited" vars are exactly those which may change their type due to
3746 * narrowing. Reset their types and add them to the type inference worklist */
3747 ZEND_BITSET_FOREACH(visited, bitset_len, i) {
3748 ssa->var_info[i].type &= ~MAY_BE_ANY;
3749 } ZEND_BITSET_FOREACH_END();
3750 zend_bitset_union(worklist, visited, bitset_len);
3751 }
3752 }
3753 }
3754
3755 if (!narrowed) {
3756 free_alloca(visited, use_heap);
3757 return SUCCESS;
3758 }
3759
3760 if (zend_infer_types_ex(op_array, script, ssa, worklist) != SUCCESS) {
3761 free_alloca(visited, use_heap);
3762 return FAILURE;
3763 }
3764
3765 free_alloca(visited, use_heap);
3766 return SUCCESS;
3767 }
3768
is_recursive_tail_call(const zend_op_array * op_array,zend_op * opline)3769 static int is_recursive_tail_call(const zend_op_array *op_array,
3770 zend_op *opline)
3771 {
3772 zend_func_info *info = ZEND_FUNC_INFO(op_array);
3773
3774 if (info->ssa.ops && info->ssa.vars && info->call_map &&
3775 info->ssa.ops[opline - op_array->opcodes].op1_use >= 0 &&
3776 info->ssa.vars[info->ssa.ops[opline - op_array->opcodes].op1_use].definition >= 0) {
3777
3778 zend_op *op = op_array->opcodes + info->ssa.vars[info->ssa.ops[opline - op_array->opcodes].op1_use].definition;
3779
3780 if (op->opcode == ZEND_DO_UCALL) {
3781 zend_call_info *call_info = info->call_map[op - op_array->opcodes];
3782 if (call_info && op_array == &call_info->callee_func->op_array) {
3783 return 1;
3784 }
3785 }
3786 }
3787 return 0;
3788 }
3789
zend_init_func_return_info(const zend_op_array * op_array,const zend_script * script,zend_ssa_var_info * ret)3790 void zend_init_func_return_info(const zend_op_array *op_array,
3791 const zend_script *script,
3792 zend_ssa_var_info *ret)
3793 {
3794 if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
3795 zend_arg_info *ret_info = op_array->arg_info - 1;
3796 zend_ssa_range tmp_range = {0, 0, 0, 0};
3797
3798 ret->type = zend_fetch_arg_info(script, ret_info, &ret->ce);
3799 if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
3800 ret->type |= MAY_BE_REF;
3801 } else if (ret->type & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE)) {
3802 ret->type |= MAY_BE_RC1|MAY_BE_RCN;
3803 }
3804 ret->is_instanceof = (ret->ce) ? 1 : 0;
3805 ret->range = tmp_range;
3806 ret->has_range = 0;
3807 }
3808 }
3809
zend_func_return_info(const zend_op_array * op_array,const zend_script * script,int recursive,int widening,zend_ssa_var_info * ret)3810 void zend_func_return_info(const zend_op_array *op_array,
3811 const zend_script *script,
3812 int recursive,
3813 int widening,
3814 zend_ssa_var_info *ret)
3815 {
3816 zend_func_info *info = ZEND_FUNC_INFO(op_array);
3817 zend_ssa *ssa = &info->ssa;
3818 int blocks_count = info->ssa.cfg.blocks_count;
3819 zend_basic_block *blocks = info->ssa.cfg.blocks;
3820 int j;
3821 uint32_t t1;
3822 uint32_t tmp = 0;
3823 zend_class_entry *tmp_ce = NULL;
3824 int tmp_is_instanceof = -1;
3825 zend_class_entry *arg_ce;
3826 int arg_is_instanceof;
3827 zend_ssa_range tmp_range = {0, 0, 0, 0};
3828 int tmp_has_range = -1;
3829
3830 if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
3831 ret->type = MAY_BE_OBJECT | MAY_BE_RC1 | MAY_BE_RCN;
3832 ret->ce = zend_ce_generator;
3833 ret->is_instanceof = 0;
3834 ret->range = tmp_range;
3835 ret->has_range = 0;
3836 return;
3837 }
3838
3839 for (j = 0; j < blocks_count; j++) {
3840 if ((blocks[j].flags & ZEND_BB_REACHABLE) && blocks[j].len != 0) {
3841 zend_op *opline = op_array->opcodes + blocks[j].start + blocks[j].len - 1;
3842
3843 if (opline->opcode == ZEND_RETURN || opline->opcode == ZEND_RETURN_BY_REF) {
3844 if (!recursive &&
3845 info->ssa.ops &&
3846 info->ssa.var_info &&
3847 info->ssa.ops[opline - op_array->opcodes].op1_use >= 0 &&
3848 info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].recursive) {
3849 continue;
3850 }
3851 if (is_recursive_tail_call(op_array, opline)) {
3852 continue;
3853 }
3854 t1 = OP1_INFO();
3855 if (t1 & MAY_BE_UNDEF) {
3856 t1 |= MAY_BE_NULL;
3857 }
3858 if (opline->opcode == ZEND_RETURN) {
3859 if (t1 & MAY_BE_RC1) {
3860 t1 |= MAY_BE_RCN;
3861 }
3862 t1 &= ~(MAY_BE_UNDEF | MAY_BE_REF);
3863 } else {
3864 t1 |= MAY_BE_REF;
3865 t1 &= ~(MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN);
3866 }
3867 tmp |= t1;
3868
3869 if (info->ssa.ops &&
3870 info->ssa.var_info &&
3871 info->ssa.ops[opline - op_array->opcodes].op1_use >= 0 &&
3872 info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].ce) {
3873 arg_ce = info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].ce;
3874 arg_is_instanceof = info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].is_instanceof;
3875 } else {
3876 arg_ce = NULL;
3877 arg_is_instanceof = 0;
3878 }
3879
3880 if (tmp_is_instanceof < 0) {
3881 tmp_ce = arg_ce;
3882 tmp_is_instanceof = arg_is_instanceof;
3883 } else if (arg_ce && arg_ce == tmp_ce) {
3884 if (tmp_is_instanceof != arg_is_instanceof) {
3885 tmp_is_instanceof = 1;
3886 }
3887 } else {
3888 tmp_ce = NULL;
3889 tmp_is_instanceof = 0;
3890 }
3891
3892 if (opline->op1_type == IS_CONST) {
3893 zval *zv = CRT_CONSTANT_EX(op_array, opline->op1, info->ssa.rt_constants);
3894
3895 if (Z_TYPE_P(zv) == IS_NULL) {
3896 if (tmp_has_range < 0) {
3897 tmp_has_range = 1;
3898 tmp_range.underflow = 0;
3899 tmp_range.min = 0;
3900 tmp_range.max = 0;
3901 tmp_range.overflow = 0;
3902 } else if (tmp_has_range) {
3903 if (!tmp_range.underflow) {
3904 tmp_range.min = MIN(tmp_range.min, 0);
3905 }
3906 if (!tmp_range.overflow) {
3907 tmp_range.max = MAX(tmp_range.max, 0);
3908 }
3909 }
3910 } else if (Z_TYPE_P(zv) == IS_FALSE) {
3911 if (tmp_has_range < 0) {
3912 tmp_has_range = 1;
3913 tmp_range.underflow = 0;
3914 tmp_range.min = 0;
3915 tmp_range.max = 0;
3916 tmp_range.overflow = 0;
3917 } else if (tmp_has_range) {
3918 if (!tmp_range.underflow) {
3919 tmp_range.min = MIN(tmp_range.min, 0);
3920 }
3921 if (!tmp_range.overflow) {
3922 tmp_range.max = MAX(tmp_range.max, 0);
3923 }
3924 }
3925 } else if (Z_TYPE_P(zv) == IS_TRUE) {
3926 if (tmp_has_range < 0) {
3927 tmp_has_range = 1;
3928 tmp_range.underflow = 0;
3929 tmp_range.min = 1;
3930 tmp_range.max = 1;
3931 tmp_range.overflow = 0;
3932 } else if (tmp_has_range) {
3933 if (!tmp_range.underflow) {
3934 tmp_range.min = MIN(tmp_range.min, 1);
3935 }
3936 if (!tmp_range.overflow) {
3937 tmp_range.max = MAX(tmp_range.max, 1);
3938 }
3939 }
3940 } else if (Z_TYPE_P(zv) == IS_LONG) {
3941 if (tmp_has_range < 0) {
3942 tmp_has_range = 1;
3943 tmp_range.underflow = 0;
3944 tmp_range.min = Z_LVAL_P(zv);
3945 tmp_range.max = Z_LVAL_P(zv);
3946 tmp_range.overflow = 0;
3947 } else if (tmp_has_range) {
3948 if (!tmp_range.underflow) {
3949 tmp_range.min = MIN(tmp_range.min, Z_LVAL_P(zv));
3950 }
3951 if (!tmp_range.overflow) {
3952 tmp_range.max = MAX(tmp_range.max, Z_LVAL_P(zv));
3953 }
3954 }
3955 } else {
3956 tmp_has_range = 0;
3957 }
3958 } else if (info->ssa.ops &&
3959 info->ssa.var_info &&
3960 info->ssa.ops[opline - op_array->opcodes].op1_use >= 0) {
3961 if (info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].has_range) {
3962 if (tmp_has_range < 0) {
3963 tmp_has_range = 1;
3964 tmp_range = info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].range;
3965 } else if (tmp_has_range) {
3966 /* union */
3967 if (info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].range.underflow) {
3968 tmp_range.underflow = 1;
3969 tmp_range.min = ZEND_LONG_MIN;
3970 } else {
3971 tmp_range.min = MIN(tmp_range.min, info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].range.min);
3972 }
3973 if (info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].range.overflow) {
3974 tmp_range.overflow = 1;
3975 tmp_range.max = ZEND_LONG_MAX;
3976 } else {
3977 tmp_range.max = MAX(tmp_range.max, info->ssa.var_info[info->ssa.ops[opline - op_array->opcodes].op1_use].range.max);
3978 }
3979 }
3980 } else if (!widening) {
3981 tmp_has_range = 1;
3982 tmp_range.underflow = 1;
3983 tmp_range.min = ZEND_LONG_MIN;
3984 tmp_range.max = ZEND_LONG_MAX;
3985 tmp_range.overflow = 1;
3986 }
3987 } else {
3988 tmp_has_range = 0;
3989 }
3990 }
3991 }
3992 }
3993
3994 if (!(op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
3995 if (tmp_is_instanceof < 0) {
3996 tmp_is_instanceof = 0;
3997 tmp_ce = NULL;
3998 }
3999 if (tmp_has_range < 0) {
4000 tmp_has_range = 0;
4001 }
4002 ret->type = tmp;
4003 ret->ce = tmp_ce;
4004 ret->is_instanceof = tmp_is_instanceof;
4005 }
4006 ret->range = tmp_range;
4007 ret->has_range = tmp_has_range;
4008 }
4009
zend_infer_types(const zend_op_array * op_array,const zend_script * script,zend_ssa * ssa)4010 static int zend_infer_types(const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa)
4011 {
4012 zend_ssa_var_info *ssa_var_info = ssa->var_info;
4013 int ssa_vars_count = ssa->vars_count;
4014 int j;
4015 zend_bitset worklist;
4016 ALLOCA_FLAG(use_heap);
4017
4018 worklist = do_alloca(sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count), use_heap);
4019 memset(worklist, 0, sizeof(zend_ulong) * zend_bitset_len(ssa_vars_count));
4020
4021 /* Type Inference */
4022 for (j = op_array->last_var; j < ssa_vars_count; j++) {
4023 zend_bitset_incl(worklist, j);
4024 ssa_var_info[j].type = 0;
4025 }
4026
4027 if (zend_infer_types_ex(op_array, script, ssa, worklist) != SUCCESS) {
4028 free_alloca(worklist, use_heap);
4029 return FAILURE;
4030 }
4031
4032 /* Narrowing integer initialization to doubles */
4033 zend_type_narrowing(op_array, script, ssa);
4034
4035 if (ZEND_FUNC_INFO(op_array)) {
4036 zend_func_return_info(op_array, script, 1, 0, &ZEND_FUNC_INFO(op_array)->return_info);
4037 }
4038
4039 free_alloca(worklist, use_heap);
4040 return SUCCESS;
4041 }
4042
zend_ssa_inference(zend_arena ** arena,const zend_op_array * op_array,const zend_script * script,zend_ssa * ssa)4043 int zend_ssa_inference(zend_arena **arena, const zend_op_array *op_array, const zend_script *script, zend_ssa *ssa) /* {{{ */
4044 {
4045 zend_ssa_var_info *ssa_var_info;
4046 int i;
4047
4048 if (!ssa->var_info) {
4049 ssa->var_info = zend_arena_calloc(arena, ssa->vars_count, sizeof(zend_ssa_var_info));
4050 }
4051 ssa_var_info = ssa->var_info;
4052
4053 if (!op_array->function_name) {
4054 for (i = 0; i < op_array->last_var; i++) {
4055 ssa_var_info[i].type = MAY_BE_UNDEF | MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
4056 ssa_var_info[i].has_range = 0;
4057 }
4058 } else {
4059 for (i = 0; i < op_array->last_var; i++) {
4060 ssa_var_info[i].type = MAY_BE_UNDEF;
4061 ssa_var_info[i].has_range = 0;
4062 if (ssa->vars[i].alias) {
4063 ssa_var_info[i].type |= get_ssa_alias_types(ssa->vars[i].alias);
4064 }
4065 }
4066 }
4067 for (i = op_array->last_var; i < ssa->vars_count; i++) {
4068 ssa_var_info[i].type = 0;
4069 ssa_var_info[i].has_range = 0;
4070 }
4071
4072 if (zend_infer_ranges(op_array, ssa) != SUCCESS) {
4073 return FAILURE;
4074 }
4075
4076 if (zend_infer_types(op_array, script, ssa) != SUCCESS) {
4077 return FAILURE;
4078 }
4079
4080 return SUCCESS;
4081 }
4082 /* }}} */
4083
zend_inference_check_recursive_dependencies(zend_op_array * op_array)4084 void zend_inference_check_recursive_dependencies(zend_op_array *op_array)
4085 {
4086 zend_func_info *info = ZEND_FUNC_INFO(op_array);
4087 zend_call_info *call_info;
4088 zend_bitset worklist;
4089 int worklist_len, i;
4090 ALLOCA_FLAG(use_heap);
4091
4092 if (!info->ssa.var_info || !(info->flags & ZEND_FUNC_RECURSIVE)) {
4093 return;
4094 }
4095 worklist_len = zend_bitset_len(info->ssa.vars_count);
4096 worklist = do_alloca(sizeof(zend_ulong) * worklist_len, use_heap);
4097 memset(worklist, 0, sizeof(zend_ulong) * worklist_len);
4098 call_info = info->callee_info;
4099 while (call_info) {
4100 if (call_info->recursive &&
4101 info->ssa.ops[call_info->caller_call_opline - op_array->opcodes].result_def >= 0) {
4102 zend_bitset_incl(worklist, info->ssa.ops[call_info->caller_call_opline - op_array->opcodes].result_def);
4103 }
4104 call_info = call_info->next_callee;
4105 }
4106 WHILE_WORKLIST(worklist, worklist_len, i) {
4107 if (!info->ssa.var_info[i].recursive) {
4108 info->ssa.var_info[i].recursive = 1;
4109 add_usages(op_array, &info->ssa, worklist, i);
4110 }
4111 } WHILE_WORKLIST_END();
4112 free_alloca(worklist, use_heap);
4113 }
4114
zend_may_throw(const zend_op * opline,zend_op_array * op_array,zend_ssa * ssa)4115 int zend_may_throw(const zend_op *opline, zend_op_array *op_array, zend_ssa *ssa)
4116 {
4117 uint32_t t1 = OP1_INFO();
4118 uint32_t t2 = OP2_INFO();
4119
4120 if (opline->op1_type == IS_CV) {
4121 if (t1 & MAY_BE_UNDEF) {
4122 switch (opline->opcode) {
4123 case ZEND_UNSET_VAR:
4124 case ZEND_ISSET_ISEMPTY_VAR:
4125 return 1;
4126 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
4127 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
4128 case ZEND_ASSIGN:
4129 case ZEND_ASSIGN_DIM:
4130 case ZEND_ASSIGN_REF:
4131 case ZEND_BIND_GLOBAL:
4132 case ZEND_FETCH_DIM_IS:
4133 case ZEND_FETCH_OBJ_IS:
4134 case ZEND_SEND_REF:
4135 case ZEND_UNSET_CV:
4136 case ZEND_ISSET_ISEMPTY_CV:
4137 break;
4138 default:
4139 /* undefined variable warning */
4140 return 1;
4141 }
4142 }
4143 } else if (opline->op1_type & (IS_TMP_VAR|IS_VAR)) {
4144 if (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)) {
4145 switch (opline->opcode) {
4146 case ZEND_CASE:
4147 case ZEND_FE_FETCH_R:
4148 case ZEND_FE_FETCH_RW:
4149 case ZEND_FETCH_LIST:
4150 case ZEND_QM_ASSIGN:
4151 case ZEND_SEND_VAL:
4152 case ZEND_SEND_VAL_EX:
4153 case ZEND_SEND_VAR:
4154 case ZEND_SEND_VAR_EX:
4155 case ZEND_SEND_VAR_NO_REF:
4156 case ZEND_SEND_VAR_NO_REF_EX:
4157 case ZEND_SEND_REF:
4158 case ZEND_SEPARATE:
4159 case ZEND_END_SILENCE:
4160 break;
4161 default:
4162 /* destructor may be called */
4163 return 1;
4164 }
4165 }
4166 }
4167
4168 if (opline->op2_type == IS_CV) {
4169 if (t2 & MAY_BE_UNDEF) {
4170 switch (opline->opcode) {
4171 case ZEND_ASSIGN_REF:
4172 break;
4173 default:
4174 /* undefined variable warning */
4175 return 1;
4176 }
4177 }
4178 } else if (opline->op2_type & (IS_TMP_VAR|IS_VAR)) {
4179 if (t2 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY)) {
4180 switch (opline->opcode) {
4181 case ZEND_ASSIGN:
4182 break;
4183 default:
4184 /* destructor may be called */
4185 return 1;
4186 }
4187 }
4188 }
4189
4190 switch (opline->opcode) {
4191 case ZEND_NOP:
4192 case ZEND_IS_IDENTICAL:
4193 case ZEND_IS_NOT_IDENTICAL:
4194 case ZEND_QM_ASSIGN:
4195 case ZEND_JMP:
4196 case ZEND_CHECK_VAR:
4197 case ZEND_MAKE_REF:
4198 case ZEND_SEND_VAR:
4199 case ZEND_BEGIN_SILENCE:
4200 case ZEND_END_SILENCE:
4201 case ZEND_SEND_VAL:
4202 case ZEND_SEND_REF:
4203 case ZEND_SEND_VAR_EX:
4204 case ZEND_FREE:
4205 case ZEND_SEPARATE:
4206 case ZEND_TYPE_CHECK:
4207 case ZEND_DEFINED:
4208 case ZEND_ISSET_ISEMPTY_THIS:
4209 case ZEND_COALESCE:
4210 case ZEND_SWITCH_LONG:
4211 case ZEND_SWITCH_STRING:
4212 case ZEND_ISSET_ISEMPTY_VAR:
4213 case ZEND_ISSET_ISEMPTY_CV:
4214 return 0;
4215 case ZEND_INIT_FCALL:
4216 /* can't throw, because call is resolved at compile time */
4217 return 0;
4218 case ZEND_BIND_GLOBAL:
4219 if ((opline+1)->opcode == ZEND_BIND_GLOBAL) {
4220 return zend_may_throw(opline + 1, op_array, ssa);
4221 }
4222 return 0;
4223 case ZEND_ADD:
4224 if ((t1 & MAY_BE_ANY) == MAY_BE_ARRAY
4225 && (t2 & MAY_BE_ANY) == MAY_BE_ARRAY) {
4226 return 0;
4227 }
4228 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4229 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4230 case ZEND_DIV:
4231 case ZEND_MOD:
4232 if (!OP2_HAS_RANGE() ||
4233 (OP2_MIN_RANGE() <= 0 && OP2_MAX_RANGE() >= 0)) {
4234 /* Division by zero */
4235 return 1;
4236 }
4237 /* break missing intentionally */
4238 case ZEND_SUB:
4239 case ZEND_MUL:
4240 case ZEND_POW:
4241 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4242 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4243 case ZEND_SL:
4244 case ZEND_SR:
4245 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4246 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4247 !OP2_HAS_RANGE() ||
4248 OP2_MIN_RANGE() < 0;
4249 case ZEND_CONCAT:
4250 case ZEND_FAST_CONCAT:
4251 return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4252 (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
4253 case ZEND_BW_OR:
4254 case ZEND_BW_AND:
4255 case ZEND_BW_XOR:
4256 if ((t1 & MAY_BE_ANY) == MAY_BE_STRING
4257 && (t2 & MAY_BE_ANY) == MAY_BE_STRING) {
4258 return 0;
4259 }
4260 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4261 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4262 case ZEND_BW_NOT:
4263 return (t1 & (MAY_BE_NULL|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
4264 case ZEND_BOOL_NOT:
4265 case ZEND_PRE_INC:
4266 case ZEND_POST_INC:
4267 case ZEND_PRE_DEC:
4268 case ZEND_POST_DEC:
4269 case ZEND_JMPZ:
4270 case ZEND_JMPNZ:
4271 case ZEND_JMPZNZ:
4272 case ZEND_JMPZ_EX:
4273 case ZEND_JMPNZ_EX:
4274 case ZEND_BOOL:
4275 case ZEND_JMP_SET:
4276 return (t1 & MAY_BE_OBJECT);
4277 case ZEND_BOOL_XOR:
4278 return (t1 & MAY_BE_OBJECT) || (t2 & MAY_BE_OBJECT);
4279 case ZEND_IS_EQUAL:
4280 case ZEND_IS_NOT_EQUAL:
4281 case ZEND_IS_SMALLER:
4282 case ZEND_IS_SMALLER_OR_EQUAL:
4283 case ZEND_CASE:
4284 case ZEND_SPACESHIP:
4285 if ((t1 & MAY_BE_ANY) == MAY_BE_NULL
4286 || (t2 & MAY_BE_ANY) == MAY_BE_NULL) {
4287 return 0;
4288 }
4289 return (t1 & (MAY_BE_OBJECT|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT)) || (t2 & (MAY_BE_OBJECT|MAY_BE_ARRAY_OF_ARRAY|MAY_BE_ARRAY_OF_OBJECT));
4290 case ZEND_ASSIGN_ADD:
4291 if (opline->extended_value != 0) {
4292 return 1;
4293 }
4294 if ((t1 & MAY_BE_ANY) == MAY_BE_ARRAY
4295 && (t2 & MAY_BE_ANY) == MAY_BE_ARRAY) {
4296 return 0;
4297 }
4298 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4299 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4300 case ZEND_ASSIGN_DIV:
4301 case ZEND_ASSIGN_MOD:
4302 if (opline->extended_value != 0) {
4303 return 1;
4304 }
4305 if (!OP2_HAS_RANGE() ||
4306 (OP2_MIN_RANGE() <= 0 && OP2_MAX_RANGE() >= 0)) {
4307 /* Division by zero */
4308 return 1;
4309 }
4310 /* break missing intentionally */
4311 case ZEND_ASSIGN_SUB:
4312 case ZEND_ASSIGN_MUL:
4313 case ZEND_ASSIGN_POW:
4314 if (opline->extended_value != 0) {
4315 return 1;
4316 }
4317 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4318 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4319 case ZEND_ASSIGN_SL:
4320 case ZEND_ASSIGN_SR:
4321 if (opline->extended_value != 0) {
4322 return 1;
4323 }
4324 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4325 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4326 !OP2_HAS_RANGE() ||
4327 OP2_MIN_RANGE() < 0;
4328 case ZEND_ASSIGN_CONCAT:
4329 if (opline->extended_value != 0) {
4330 return 1;
4331 }
4332 return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4333 (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
4334 case ZEND_ASSIGN_BW_OR:
4335 case ZEND_ASSIGN_BW_AND:
4336 case ZEND_ASSIGN_BW_XOR:
4337 if (opline->extended_value != 0) {
4338 return 1;
4339 }
4340 if ((t1 & MAY_BE_ANY) == MAY_BE_STRING
4341 && (t2 & MAY_BE_ANY) == MAY_BE_STRING) {
4342 return 0;
4343 }
4344 return (t1 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT)) ||
4345 (t2 & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT));
4346 case ZEND_ASSIGN:
4347 case ZEND_UNSET_VAR:
4348 return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_ARRAY_OF_OBJECT|MAY_BE_ARRAY_OF_RESOURCE|MAY_BE_ARRAY_OF_ARRAY));
4349 case ZEND_ASSIGN_DIM:
4350 return (t1 & (MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_TRUE|MAY_BE_STRING|MAY_BE_LONG|MAY_BE_DOUBLE)) || opline->op2_type == IS_UNUSED ||
4351 (t2 & (MAY_BE_UNDEF|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
4352 case ZEND_ROPE_INIT:
4353 case ZEND_ROPE_ADD:
4354 case ZEND_ROPE_END:
4355 return t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT);
4356 case ZEND_INIT_ARRAY:
4357 case ZEND_ADD_ARRAY_ELEMENT:
4358 return (opline->op2_type != IS_UNUSED) && (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
4359 case ZEND_STRLEN:
4360 return (t1 & MAY_BE_ANY) != MAY_BE_STRING;
4361 case ZEND_COUNT:
4362 return (t1 & MAY_BE_ANY) != MAY_BE_ARRAY;
4363 case ZEND_RECV_INIT:
4364 if (Z_CONSTANT_P(CRT_CONSTANT_EX(op_array, opline->op2, ssa->rt_constants))) {
4365 return 1;
4366 }
4367 if (op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) {
4368 uint32_t arg_num = opline->op1.num;
4369 zend_arg_info *cur_arg_info;
4370
4371 if (EXPECTED(arg_num <= op_array->num_args)) {
4372 cur_arg_info = &op_array->arg_info[arg_num-1];
4373 } else if (UNEXPECTED(op_array->fn_flags & ZEND_ACC_VARIADIC)) {
4374 cur_arg_info = &op_array->arg_info[op_array->num_args];
4375 } else {
4376 return 0;
4377 }
4378 return ZEND_TYPE_IS_SET(cur_arg_info->type);
4379 } else {
4380 return 0;
4381 }
4382 case ZEND_FETCH_IS:
4383 return (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
4384 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
4385 return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
4386 case ZEND_FETCH_DIM_IS:
4387 return (t1 & MAY_BE_OBJECT) || (t2 & (MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE));
4388 case ZEND_CAST:
4389 switch (opline->extended_value) {
4390 case IS_NULL:
4391 return 0;
4392 case _IS_BOOL:
4393 return (t1 & MAY_BE_OBJECT);
4394 case IS_LONG:
4395 case IS_DOUBLE:
4396 return (t1 & MAY_BE_OBJECT);
4397 case IS_STRING:
4398 return (t1 & (MAY_BE_ARRAY|MAY_BE_OBJECT));
4399 case IS_ARRAY:
4400 return (t1 & MAY_BE_OBJECT);
4401 case IS_OBJECT:
4402 return (t1 & MAY_BE_ARRAY);
4403 default:
4404 return 1;
4405 }
4406 default:
4407 return 1;
4408 }
4409 }
4410
4411 /*
4412 * Local variables:
4413 * tab-width: 4
4414 * c-basic-offset: 4
4415 * indent-tabs-mode: t
4416 * End:
4417 */
4418