1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2017 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    |          Xinchen Hui <laruence@php.net>                              |
17    +----------------------------------------------------------------------+
18 */
19 
20 /* pass 11
21  * - compact literals table
22  */
23 
24 #include "php.h"
25 #include "Optimizer/zend_optimizer.h"
26 #include "Optimizer/zend_optimizer_internal.h"
27 #include "zend_API.h"
28 #include "zend_constants.h"
29 #include "zend_execute.h"
30 #include "zend_vm.h"
31 
32 #define DEBUG_COMPACT_LITERALS 0
33 
34 #define LITERAL_VALUE                        0x0100
35 #define LITERAL_FUNC                         0x0200
36 #define LITERAL_CLASS                        0x0300
37 #define LITERAL_CONST                        0x0400
38 #define LITERAL_CLASS_CONST                  0x0500
39 #define LITERAL_STATIC_METHOD                0x0600
40 #define LITERAL_STATIC_PROPERTY              0x0700
41 #define LITERAL_METHOD                       0x0800
42 #define LITERAL_PROPERTY                     0x0900
43 #define LITERAL_GLOBAL                       0x0A00
44 
45 #define LITERAL_EX_CLASS                     0x4000
46 #define LITERAL_EX_OBJ                       0x2000
47 #define LITERAL_MAY_MERGE                    0x1000
48 #define LITERAL_KIND_MASK                    0x0f00
49 #define LITERAL_NUM_RELATED_MASK             0x000f
50 #define LITERAL_NUM_SLOTS_MASK               0x00f0
51 #define LITERAL_NUM_SLOTS_SHIFT              4
52 
53 #define LITERAL_NUM_RELATED(info) (info & LITERAL_NUM_RELATED_MASK)
54 #define LITERAL_NUM_SLOTS(info)   ((info & LITERAL_NUM_SLOTS_MASK) >> LITERAL_NUM_SLOTS_SHIFT)
55 
56 typedef struct _literal_info {
57 	uint32_t  flags; /* bitmask (see defines above) */
58 	union {
59 		int    num;   /* variable number or class name literal number */
60 	} u;
61 } literal_info;
62 
63 #define LITERAL_FLAGS(kind, slots, related) \
64 	((kind) | ((slots) << LITERAL_NUM_SLOTS_SHIFT) | (related))
65 
66 #define LITERAL_INFO(n, kind, merge, slots, related) do { \
67 		info[n].flags = (((merge) ? LITERAL_MAY_MERGE : 0) | LITERAL_FLAGS(kind, slots, related)); \
68 	} while (0)
69 
70 #define LITERAL_INFO_CLASS(n, kind, merge, slots, related, _num) do { \
71 		info[n].flags = (LITERAL_EX_CLASS | ((merge) ? LITERAL_MAY_MERGE : 0) | LITERAL_FLAGS(kind, slots, related)); \
72 		info[n].u.num = (_num); \
73 	} while (0)
74 
75 #define LITERAL_INFO_OBJ(n, kind, merge, slots, related, _num) do { \
76 		info[n].flags = (LITERAL_EX_OBJ | ((merge) ? LITERAL_MAY_MERGE : 0) | LITERAL_FLAGS(kind, slots, related)); \
77 		info[n].u.num = (_num); \
78 	} while (0)
79 
optimizer_literal_obj_info(literal_info * info,zend_uchar op_type,znode_op op,int constant,uint32_t kind,uint32_t slots,uint32_t related,zend_op_array * op_array)80 static void optimizer_literal_obj_info(literal_info   *info,
81                                        zend_uchar      op_type,
82                                        znode_op        op,
83                                        int             constant,
84                                        uint32_t       kind,
85                                        uint32_t       slots,
86                                        uint32_t       related,
87                                        zend_op_array  *op_array)
88 {
89 	/* For now we merge only $this object properties and methods.
90 	 * In general it's also possible to do it for any CV variable as well,
91 	 * but it would require complex dataflow and/or type analysis.
92 	 */
93 	if (Z_TYPE(op_array->literals[constant]) == IS_STRING &&
94 	    op_type == IS_UNUSED) {
95 		LITERAL_INFO_OBJ(constant, kind, 1, slots, related, op_array->this_var);
96 	} else {
97 		LITERAL_INFO(constant, kind, 0, slots, related);
98 	}
99 }
100 
optimizer_literal_class_info(literal_info * info,zend_uchar op_type,znode_op op,int constant,uint32_t kind,uint32_t slots,uint32_t related,zend_op_array * op_array)101 static void optimizer_literal_class_info(literal_info   *info,
102                                          zend_uchar      op_type,
103                                          znode_op        op,
104                                          int             constant,
105                                          uint32_t       kind,
106                                          uint32_t       slots,
107                                          uint32_t       related,
108                                          zend_op_array  *op_array)
109 {
110 	if (op_type == IS_CONST) {
111 		LITERAL_INFO_CLASS(constant, kind, 1, slots, related, op.constant);
112 	} else {
113 		LITERAL_INFO(constant, kind, 0, slots, related);
114 	}
115 }
116 
zend_optimizer_compact_literals(zend_op_array * op_array,zend_optimizer_ctx * ctx)117 void zend_optimizer_compact_literals(zend_op_array *op_array, zend_optimizer_ctx *ctx)
118 {
119 	zend_op *opline, *end;
120 	int i, j, n, *map, cache_size;
121 	zval zv, *pos;
122 	literal_info *info;
123 	int l_null = -1;
124 	int l_false = -1;
125 	int l_true = -1;
126 	HashTable hash;
127 	zend_string *key = NULL;
128 	void *checkpoint = zend_arena_checkpoint(ctx->arena);
129 
130 	if (op_array->last_literal) {
131 		cache_size = 0;
132 		info = (literal_info*)zend_arena_calloc(&ctx->arena, op_array->last_literal, sizeof(literal_info));
133 
134 	    /* Mark literals of specific types */
135 		opline = op_array->opcodes;
136 		end = opline + op_array->last;
137 		while (opline < end) {
138 			switch (opline->opcode) {
139 				case ZEND_INIT_FCALL:
140 					LITERAL_INFO(opline->op2.constant, LITERAL_FUNC, 1, 1, 1);
141 					break;
142 				case ZEND_INIT_FCALL_BY_NAME:
143 					LITERAL_INFO(opline->op2.constant, LITERAL_FUNC, 1, 1, 2);
144 					break;
145 				case ZEND_INIT_NS_FCALL_BY_NAME:
146 					LITERAL_INFO(opline->op2.constant, LITERAL_FUNC, 1, 1, 3);
147 					break;
148 				case ZEND_INIT_METHOD_CALL:
149 					if (ZEND_OP1_TYPE(opline) == IS_CONST) {
150 						LITERAL_INFO(opline->op1.constant, LITERAL_VALUE, 1, 0, 1);
151 					}
152 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
153 						optimizer_literal_obj_info(
154 							info,
155 							opline->op1_type,
156 							opline->op1,
157 							opline->op2.constant,
158 							LITERAL_METHOD, 2, 2,
159 							op_array);
160 					}
161 					break;
162 				case ZEND_INIT_STATIC_METHOD_CALL:
163 					if (ZEND_OP1_TYPE(opline) == IS_CONST) {
164 						LITERAL_INFO(opline->op1.constant, LITERAL_CLASS, 1, 1, 2);
165 					}
166 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
167 						optimizer_literal_class_info(
168 							info,
169 							opline->op1_type,
170 							opline->op1,
171 							opline->op2.constant,
172 							LITERAL_STATIC_METHOD, (ZEND_OP1_TYPE(opline) == IS_CONST) ? 1 : 2, 2,
173 							op_array);
174 					}
175 					break;
176 				case ZEND_CATCH:
177 					LITERAL_INFO(opline->op1.constant, LITERAL_CLASS, 1, 1, 2);
178 					break;
179 				case ZEND_DEFINED:
180 					LITERAL_INFO(opline->op1.constant, LITERAL_CONST, 1, 1, 2);
181 					break;
182 				case ZEND_FETCH_CONSTANT:
183 					if (ZEND_OP1_TYPE(opline) == IS_UNUSED) {
184 						if ((opline->extended_value & (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) == (IS_CONSTANT_IN_NAMESPACE|IS_CONSTANT_UNQUALIFIED)) {
185 							LITERAL_INFO(opline->op2.constant, LITERAL_CONST, 1, 1, 5);
186 						} else {
187 							LITERAL_INFO(opline->op2.constant, LITERAL_CONST, 1, 1, 3);
188 						}
189 					} else {
190 						if (ZEND_OP1_TYPE(opline) == IS_CONST) {
191 							LITERAL_INFO(opline->op1.constant, LITERAL_CLASS, 1, 1, 2);
192 						}
193 						optimizer_literal_class_info(
194 							info,
195 							opline->op1_type,
196 							opline->op1,
197 							opline->op2.constant,
198 							LITERAL_CLASS_CONST, (ZEND_OP1_TYPE(opline) == IS_CONST) ? 1 : 2, 1,
199 							op_array);
200 					}
201 					break;
202 				case ZEND_FETCH_R:
203 				case ZEND_FETCH_W:
204 				case ZEND_FETCH_RW:
205 				case ZEND_FETCH_IS:
206 				case ZEND_FETCH_UNSET:
207 				case ZEND_FETCH_FUNC_ARG:
208 				case ZEND_UNSET_VAR:
209 				case ZEND_ISSET_ISEMPTY_VAR:
210 					if (ZEND_OP2_TYPE(opline) == IS_UNUSED) {
211 						if (ZEND_OP1_TYPE(opline) == IS_CONST) {
212 							LITERAL_INFO(opline->op1.constant, LITERAL_VALUE, 1, 0, 1);
213 						}
214 					} else {
215 						if (ZEND_OP2_TYPE(opline) == IS_CONST) {
216 							LITERAL_INFO(opline->op2.constant, LITERAL_CLASS, 1, 1, 2);
217 						}
218 						if (ZEND_OP1_TYPE(opline) == IS_CONST) {
219 							optimizer_literal_class_info(
220 								info,
221 								opline->op2_type,
222 								opline->op2,
223 								opline->op1.constant,
224 								LITERAL_STATIC_PROPERTY, 2, 1,
225 								op_array);
226 						}
227 					}
228 					break;
229 				case ZEND_FETCH_CLASS:
230 				case ZEND_ADD_INTERFACE:
231 				case ZEND_ADD_TRAIT:
232 				case ZEND_INSTANCEOF:
233 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
234 						LITERAL_INFO(opline->op2.constant, LITERAL_CLASS, 1, 1, 2);
235 					}
236 					break;
237 				case ZEND_NEW:
238 					if (ZEND_OP1_TYPE(opline) == IS_CONST) {
239 						LITERAL_INFO(opline->op1.constant, LITERAL_CLASS, 1, 1, 2);
240 					}
241 					break;
242 				case ZEND_ASSIGN_OBJ:
243 				case ZEND_FETCH_OBJ_R:
244 				case ZEND_FETCH_OBJ_W:
245 				case ZEND_FETCH_OBJ_RW:
246 				case ZEND_FETCH_OBJ_IS:
247 				case ZEND_FETCH_OBJ_UNSET:
248 				case ZEND_FETCH_OBJ_FUNC_ARG:
249 				case ZEND_UNSET_OBJ:
250 				case ZEND_PRE_INC_OBJ:
251 				case ZEND_PRE_DEC_OBJ:
252 				case ZEND_POST_INC_OBJ:
253 				case ZEND_POST_DEC_OBJ:
254 				case ZEND_ISSET_ISEMPTY_PROP_OBJ:
255 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
256 						optimizer_literal_obj_info(
257 							info,
258 							opline->op1_type,
259 							opline->op1,
260 							opline->op2.constant,
261 							LITERAL_PROPERTY, 2, 1,
262 							op_array);
263 					}
264 					break;
265 				case ZEND_ASSIGN_ADD:
266 				case ZEND_ASSIGN_SUB:
267 				case ZEND_ASSIGN_MUL:
268 				case ZEND_ASSIGN_DIV:
269 				case ZEND_ASSIGN_POW:
270 				case ZEND_ASSIGN_MOD:
271 				case ZEND_ASSIGN_SL:
272 				case ZEND_ASSIGN_SR:
273 				case ZEND_ASSIGN_CONCAT:
274 				case ZEND_ASSIGN_BW_OR:
275 				case ZEND_ASSIGN_BW_AND:
276 				case ZEND_ASSIGN_BW_XOR:
277 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
278 						if (opline->extended_value == ZEND_ASSIGN_OBJ) {
279 							optimizer_literal_obj_info(
280 								info,
281 								opline->op1_type,
282 								opline->op1,
283 								opline->op2.constant,
284 								LITERAL_PROPERTY, 2, 1,
285 								op_array);
286 						} else {
287 							LITERAL_INFO(opline->op2.constant, LITERAL_VALUE, 1, 0, 1);
288 						}
289 					}
290 					break;
291 				case ZEND_BIND_GLOBAL:
292 					LITERAL_INFO(opline->op2.constant, LITERAL_GLOBAL, 0, 1, 1);
293 					break;
294 				case ZEND_RECV_INIT:
295 					LITERAL_INFO(opline->op2.constant, LITERAL_VALUE, 0, 0, 1);
296 					if (Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) != -1) {
297 						Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = cache_size;
298 						cache_size += sizeof(void *);
299 					}
300 					break;
301 				case ZEND_RECV:
302 				case ZEND_RECV_VARIADIC:
303 				case ZEND_VERIFY_RETURN_TYPE:
304 					if (opline->op2.num != -1) {
305 						opline->op2.num = cache_size;
306 						cache_size += sizeof(void *);
307 					}
308 				default:
309 					if (ZEND_OP1_TYPE(opline) == IS_CONST) {
310 						LITERAL_INFO(opline->op1.constant, LITERAL_VALUE, 1, 0, 1);
311 					}
312 					if (ZEND_OP2_TYPE(opline) == IS_CONST) {
313 						LITERAL_INFO(opline->op2.constant, LITERAL_VALUE, 1, 0, 1);
314 					}
315 					break;
316 			}
317 			opline++;
318 		}
319 
320 #if DEBUG_COMPACT_LITERALS
321 		{
322 			int i, use_copy;
323 			fprintf(stderr, "File %s func %s\n", op_array->filename->val,
324 					op_array->function_name ? op_array->function_name->val : "main");
325 			fprintf(stderr, "Literlas table size %d\n", op_array->last_literal);
326 
327 			for (i = 0; i < op_array->last_literal; i++) {
328 				zval zv;
329 				ZVAL_COPY_VALUE(&zv, op_array->literals + i);
330 				use_copy = zend_make_printable_zval(op_array->literals + i, &zv);
331 				fprintf(stderr, "Literal %d, val (%d):%s\n", i, Z_STRLEN(zv), Z_STRVAL(zv));
332 				if (use_copy) {
333 					zval_dtor(&zv);
334 				}
335 			}
336 			fflush(stderr);
337 		}
338 #endif
339 
340 		/* Merge equal constants */
341 		j = 0;
342 		zend_hash_init(&hash, op_array->last_literal, NULL, NULL, 0);
343 		map = (int*)zend_arena_alloc(&ctx->arena, op_array->last_literal * sizeof(int));
344 		memset(map, 0, op_array->last_literal * sizeof(int));
345 		for (i = 0; i < op_array->last_literal; i++) {
346 			if (!info[i].flags) {
347 				/* unsed literal */
348 				zval_dtor(&op_array->literals[i]);
349 				continue;
350 			}
351 			switch (Z_TYPE(op_array->literals[i])) {
352 				case IS_NULL:
353 					/* Only checking MAY_MERGE for IS_NULL here
354 					 * is because only IS_NULL can be default value for class type hinting(RECV_INIT). */
355 					if ((info[i].flags & LITERAL_MAY_MERGE)) {
356 						if (l_null < 0) {
357 							l_null = j;
358 							if (i != j) {
359 								op_array->literals[j] = op_array->literals[i];
360 								info[j] = info[i];
361 							}
362 							j++;
363 						}
364 						map[i] = l_null;
365 					} else {
366 						map[i] = j;
367 						if (i != j) {
368 							op_array->literals[j] = op_array->literals[i];
369 							info[j] = info[i];
370 						}
371 						j++;
372 					}
373 					break;
374 				case IS_FALSE:
375 					if (l_false < 0) {
376 						l_false = j;
377 						if (i != j) {
378 							op_array->literals[j] = op_array->literals[i];
379 							info[j] = info[i];
380 						}
381 						j++;
382 					}
383 					map[i] = l_false;
384 					break;
385 				case IS_TRUE:
386 					if (l_true < 0) {
387 						l_true = j;
388 						if (i != j) {
389 							op_array->literals[j] = op_array->literals[i];
390 							info[j] = info[i];
391 						}
392 						j++;
393 					}
394 					map[i] = l_true;
395 					break;
396 				case IS_LONG:
397 					if ((pos = zend_hash_index_find(&hash, Z_LVAL(op_array->literals[i]))) != NULL) {
398 						map[i] = Z_LVAL_P(pos);
399 					} else {
400 						map[i] = j;
401 						ZVAL_LONG(&zv, j);
402 						zend_hash_index_add_new(&hash, Z_LVAL(op_array->literals[i]), &zv);
403 						if (i != j) {
404 							op_array->literals[j] = op_array->literals[i];
405 							info[j] = info[i];
406 						}
407 						j++;
408 					}
409 					break;
410 				case IS_DOUBLE:
411 					if ((pos = zend_hash_str_find(&hash, (char*)&Z_DVAL(op_array->literals[i]), sizeof(double))) != NULL) {
412 						map[i] = Z_LVAL_P(pos);
413 					} else {
414 						map[i] = j;
415 						ZVAL_LONG(&zv, j);
416 						zend_hash_str_add(&hash, (char*)&Z_DVAL(op_array->literals[i]), sizeof(double), &zv);
417 						if (i != j) {
418 							op_array->literals[j] = op_array->literals[i];
419 							info[j] = info[i];
420 						}
421 						j++;
422 					}
423 					break;
424 				case IS_STRING:
425 				case IS_CONSTANT:
426 					if (info[i].flags & LITERAL_MAY_MERGE) {
427 						if (info[i].flags & LITERAL_EX_OBJ) {
428 							int key_len = MAX_LENGTH_OF_LONG + sizeof("->") - 1 + Z_STRLEN(op_array->literals[i]);
429 							key = zend_string_alloc(key_len, 0);
430 							ZSTR_LEN(key) = snprintf(ZSTR_VAL(key), ZSTR_LEN(key)-1, "%d->%s", info[i].u.num, Z_STRVAL(op_array->literals[i]));
431 						} else if (info[i].flags & LITERAL_EX_CLASS) {
432 							int key_len;
433 							zval *class_name = &op_array->literals[(info[i].u.num < i) ? map[info[i].u.num] : info[i].u.num];
434 							key_len = Z_STRLEN_P(class_name) + sizeof("::") - 1 + Z_STRLEN(op_array->literals[i]);
435 							key = zend_string_alloc(key_len, 0);
436 							memcpy(ZSTR_VAL(key), Z_STRVAL_P(class_name), Z_STRLEN_P(class_name));
437 							memcpy(ZSTR_VAL(key) + Z_STRLEN_P(class_name), "::", sizeof("::") - 1);
438 							memcpy(ZSTR_VAL(key) + Z_STRLEN_P(class_name) + sizeof("::") - 1,
439 								Z_STRVAL(op_array->literals[i]),
440 								Z_STRLEN(op_array->literals[i]) + 1);
441 						} else {
442 							key = zend_string_init(Z_STRVAL(op_array->literals[i]), Z_STRLEN(op_array->literals[i]), 0);
443 						}
444 						ZSTR_H(key) = zend_hash_func(ZSTR_VAL(key), ZSTR_LEN(key));
445 						ZSTR_H(key) += info[i].flags;
446 					}
447 					if ((info[i].flags & LITERAL_MAY_MERGE) &&
448 						(pos = zend_hash_find(&hash, key)) != NULL &&
449 					   	Z_TYPE(op_array->literals[i]) == Z_TYPE(op_array->literals[Z_LVAL_P(pos)]) &&
450 						info[i].flags == info[Z_LVAL_P(pos)].flags) {
451 
452 						zend_string_release(key);
453 						map[i] = Z_LVAL_P(pos);
454 						zval_dtor(&op_array->literals[i]);
455 						n = LITERAL_NUM_RELATED(info[i].flags);
456 						while (n > 1) {
457 							i++;
458 							zval_dtor(&op_array->literals[i]);
459 							n--;
460 						}
461 					} else {
462 						map[i] = j;
463 						if (info[i].flags & LITERAL_MAY_MERGE) {
464 							ZVAL_LONG(&zv, j);
465 							zend_hash_add_new(&hash, key, &zv);
466 							zend_string_release(key);
467 						}
468 						if (i != j) {
469 							op_array->literals[j] = op_array->literals[i];
470 							info[j] = info[i];
471 						}
472 						if (LITERAL_NUM_SLOTS(info[i].flags)) {
473 							Z_CACHE_SLOT(op_array->literals[j]) = cache_size;
474 							cache_size += LITERAL_NUM_SLOTS(info[i].flags) * sizeof(void*);
475 						}
476 						j++;
477 						n = LITERAL_NUM_RELATED(info[i].flags);
478 						while (n > 1) {
479 							i++;
480 							if (i != j) op_array->literals[j] = op_array->literals[i];
481 							j++;
482 							n--;
483 						}
484 					}
485 					break;
486 				default:
487 					/* don't merge other types */
488 					map[i] = j;
489 					if (i != j) {
490 						op_array->literals[j] = op_array->literals[i];
491 						info[j] = info[i];
492 					}
493 					j++;
494 					break;
495 			}
496 		}
497 		zend_hash_destroy(&hash);
498 		op_array->last_literal = j;
499 		op_array->cache_size = cache_size;
500 
501 	    /* Update opcodes to use new literals table */
502 		opline = op_array->opcodes;
503 		end = opline + op_array->last;
504 		while (opline < end) {
505 			if (ZEND_OP1_TYPE(opline) == IS_CONST) {
506 				opline->op1.constant = map[opline->op1.constant];
507 			}
508 			if (ZEND_OP2_TYPE(opline) == IS_CONST) {
509 				opline->op2.constant = map[opline->op2.constant];
510 			}
511 			opline++;
512 		}
513 		zend_arena_release(&ctx->arena, checkpoint);
514 
515 #if DEBUG_COMPACT_LITERALS
516 		{
517 			int i, use_copy;
518 			fprintf(stderr, "Optimized literlas table size %d\n", op_array->last_literal);
519 
520 			for (i = 0; i < op_array->last_literal; i++) {
521 				zval zv;
522 				ZVAL_COPY_VALUE(&zv, op_array->literals + i);
523 				use_copy = zend_make_printable_zval(op_array->literals + i, &zv);
524 				fprintf(stderr, "Literal %d, val (%d):%s\n", i, Z_STRLEN(zv), Z_STRVAL(zv));
525 				if (use_copy) {
526 					zval_dtor(&zv);
527 				}
528 			}
529 			fflush(stderr);
530 		}
531 #endif
532 	}
533 }
534