1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | https://www.php.net/license/3_01.txt                                 |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Andi Gutmans <andi@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "Optimizer/zend_optimizer.h"
23 #include "Optimizer/zend_optimizer_internal.h"
24 #include "zend_API.h"
25 #include "zend_constants.h"
26 #include "zend_execute.h"
27 #include "zend_vm.h"
28 #include "zend_bitset.h"
29 
30 #define INVALID_VAR ((uint32_t)-1)
31 #define GET_AVAILABLE_T()					\
32 	for (i = 0; i < T; i++) {				\
33 		if (!zend_bitset_in(taken_T, i)) {	\
34 			break;							\
35 		}									\
36 	}										\
37 	zend_bitset_incl(taken_T, i);			\
38 	if (i > max) {							\
39 		max = i;							\
40 	}
41 
zend_optimize_temporary_variables(zend_op_array * op_array,zend_optimizer_ctx * ctx)42 void zend_optimize_temporary_variables(zend_op_array *op_array, zend_optimizer_ctx *ctx)
43 {
44 	uint32_t T = op_array->T;
45 	int offset = op_array->last_var;
46 	uint32_t bitset_len;
47 	zend_bitset taken_T;	/* T index in use */
48 	zend_op **start_of_T;	/* opline where T is first used */
49 	int *map_T;				/* Map's the T to its new index */
50 	zend_op *opline, *end;
51 	int currT;
52 	int i;
53 	int max = -1;
54 	void *checkpoint = zend_arena_checkpoint(ctx->arena);
55 
56 	bitset_len = zend_bitset_len(T);
57 	taken_T = (zend_bitset) zend_arena_alloc(&ctx->arena, bitset_len * ZEND_BITSET_ELM_SIZE);
58 	start_of_T = (zend_op **) zend_arena_alloc(&ctx->arena, T * sizeof(zend_op *));
59 	map_T = (int *) zend_arena_alloc(&ctx->arena, T * sizeof(int));
60 	memset(map_T, 0xff, T * sizeof(int));
61 
62 	end = op_array->opcodes;
63 	opline = &op_array->opcodes[op_array->last - 1];
64 
65 	/* Find T definition points */
66 	while (opline >= end) {
67 		if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
68 			start_of_T[VAR_NUM(opline->result.var) - offset] = opline;
69 		}
70 		opline--;
71 	}
72 
73 	zend_bitset_clear(taken_T, bitset_len);
74 
75 	end = op_array->opcodes;
76 	opline = &op_array->opcodes[op_array->last - 1];
77 
78 	while (opline >= end) {
79 		if ((opline->op1_type & (IS_VAR | IS_TMP_VAR))) {
80 			currT = VAR_NUM(opline->op1.var) - offset;
81 			if (opline->opcode == ZEND_ROPE_END) {
82 				int num = (((opline->extended_value + 1) * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
83 				int var;
84 
85 				var = max;
86 				while (var >= 0 && !zend_bitset_in(taken_T, var)) {
87 					var--;
88 				}
89 				max = MAX(max, var + num);
90 				var = var + 1;
91 				map_T[currT] = var;
92 				zend_bitset_incl(taken_T, var);
93 				opline->op1.var = NUM_VAR(var + offset);
94 				while (num > 1) {
95 					num--;
96 					zend_bitset_incl(taken_T, var + num);
97 				}
98 			} else {
99 				if (map_T[currT] == INVALID_VAR) {
100 					int use_new_var = 0;
101 
102 					/* Code in "finally" blocks may modify temporary variables.
103 					 * We allocate new temporaries for values that need to
104 					 * relive FAST_CALLs.
105 					 */
106 					if ((op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) &&
107 					    (opline->opcode == ZEND_RETURN ||
108 					     opline->opcode == ZEND_GENERATOR_RETURN ||
109 					     opline->opcode == ZEND_RETURN_BY_REF ||
110 					     opline->opcode == ZEND_FREE ||
111 					     opline->opcode == ZEND_FE_FREE)) {
112 						zend_op *curr = opline;
113 
114 						while (--curr >= end) {
115 							if (curr->opcode == ZEND_FAST_CALL) {
116 								use_new_var = 1;
117 								break;
118 							} else if (curr->opcode != ZEND_FREE &&
119 							           curr->opcode != ZEND_FE_FREE &&
120 							           curr->opcode != ZEND_VERIFY_RETURN_TYPE &&
121 							           curr->opcode != ZEND_DISCARD_EXCEPTION) {
122 								break;
123 							}
124 						}
125 					}
126 					if (use_new_var) {
127 						i = ++max;
128 						zend_bitset_incl(taken_T, i);
129 					} else {
130 						GET_AVAILABLE_T();
131 					}
132 					map_T[currT] = i;
133 				}
134 				opline->op1.var = NUM_VAR(map_T[currT] + offset);
135 			}
136 		}
137 
138 		if ((opline->op2_type & (IS_VAR | IS_TMP_VAR))) {
139 			currT = VAR_NUM(opline->op2.var) - offset;
140 			if (map_T[currT] == INVALID_VAR) {
141 				GET_AVAILABLE_T();
142 				map_T[currT] = i;
143 			}
144 			opline->op2.var = NUM_VAR(map_T[currT] + offset);
145 		}
146 
147 		if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
148 			currT = VAR_NUM(opline->result.var) - offset;
149 			if (map_T[currT] == INVALID_VAR) {
150 				/* As a result of DCE, an opcode may have an unused result. */
151 				GET_AVAILABLE_T();
152 				map_T[currT] = i;
153 			}
154 			opline->result.var = NUM_VAR(map_T[currT] + offset);
155 			if (start_of_T[currT] == opline) {
156 				/* ZEND_FAST_CALL can not share temporary var with others
157 				 * since the fast_var could also be set by ZEND_HANDLE_EXCEPTION
158 				 * which could be ahead of it */
159 				if (opline->opcode != ZEND_FAST_CALL) {
160 					zend_bitset_excl(taken_T, map_T[currT]);
161 				}
162 				if (opline->opcode == ZEND_ROPE_INIT) {
163 					uint32_t num = ((opline->extended_value * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
164 					while (num > 1) {
165 						num--;
166 						zend_bitset_excl(taken_T, map_T[currT]+num);
167 					}
168 				}
169 			}
170 		}
171 
172 		opline--;
173 	}
174 
175 	zend_arena_release(&ctx->arena, checkpoint);
176 	op_array->T = max + 1;
177 }
178