xref: /PHP-7.2/ext/opcache/zend_persist_calc.c (revision 7a7ec01a)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend OPcache                                                         |
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: Andi Gutmans <andi@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Stanislav Malyshev <stas@zend.com>                          |
18    |          Dmitry Stogov <dmitry@zend.com>                             |
19    +----------------------------------------------------------------------+
20 */
21 
22 #include "zend.h"
23 #include "ZendAccelerator.h"
24 #include "zend_persist.h"
25 #include "zend_extensions.h"
26 #include "zend_shared_alloc.h"
27 #include "zend_operators.h"
28 
29 #define ADD_DUP_SIZE(m,s)  ZCG(current_persistent_script)->size += zend_shared_memdup_size((void*)m, s)
30 #define ADD_SIZE(m)        ZCG(current_persistent_script)->size += ZEND_ALIGNED_SIZE(m)
31 
32 #define ADD_ARENA_SIZE(m)        ZCG(current_persistent_script)->arena_size += ZEND_ALIGNED_SIZE(m)
33 
34 # define ADD_STRING(str) ADD_DUP_SIZE((str), _ZSTR_STRUCT_SIZE(ZSTR_LEN(str)))
35 
36 # define ADD_INTERNED_STRING(str, do_free) do { \
37 		if (ZCG(current_persistent_script)->corrupted) { \
38 			ADD_STRING(str); \
39 		} else if (!IS_ACCEL_INTERNED(str)) { \
40 			zend_string *tmp = accel_new_interned_string(str); \
41 			if (tmp != (str)) { \
42 				if (do_free) { \
43 					/*zend_string_release(str);*/ \
44 				} \
45 				(str) = tmp; \
46 			} else { \
47 				ADD_STRING(str); \
48 			} \
49 		} \
50 	} while (0)
51 
52 static void zend_persist_zval_calc(zval *z);
53 
zend_hash_persist_calc(HashTable * ht,void (* pPersistElement)(zval * pElement))54 static void zend_hash_persist_calc(HashTable *ht, void (*pPersistElement)(zval *pElement))
55 {
56 	uint32_t idx;
57 	Bucket *p;
58 
59 	if (!(ht->u.flags & HASH_FLAG_INITIALIZED) || ht->nNumUsed == 0) {
60 		return;
61 	}
62 
63 	if (!(ht->u.flags & HASH_FLAG_PACKED) && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 2) {
64 		/* compact table */
65 		uint32_t hash_size;
66 
67 		if (ht->nNumUsed <= HT_MIN_SIZE) {
68 			hash_size = HT_MIN_SIZE;
69 		} else {
70 			hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
71 			while (hash_size >> 1 > ht->nNumUsed) {
72 				hash_size >>= 1;
73 			}
74 		}
75 		ADD_SIZE(hash_size * sizeof(uint32_t) + ht->nNumUsed * sizeof(Bucket));
76 	} else {
77 		ADD_SIZE(HT_USED_SIZE(ht));
78 	}
79 
80 	for (idx = 0; idx < ht->nNumUsed; idx++) {
81 		p = ht->arData + idx;
82 		if (Z_TYPE(p->val) == IS_UNDEF) continue;
83 
84 		/* persist bucket and key */
85 		if (p->key) {
86 			ADD_INTERNED_STRING(p->key, 1);
87 		}
88 
89 		pPersistElement(&p->val);
90 	}
91 }
92 
zend_persist_ast_calc(zend_ast * ast)93 static void zend_persist_ast_calc(zend_ast *ast)
94 {
95 	uint32_t i;
96 
97 	if (ast->kind == ZEND_AST_ZVAL) {
98 		ADD_SIZE(sizeof(zend_ast_zval));
99 		zend_persist_zval_calc(zend_ast_get_zval(ast));
100 	} else if (zend_ast_is_list(ast)) {
101 		zend_ast_list *list = zend_ast_get_list(ast);
102 		ADD_SIZE(sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
103 		for (i = 0; i < list->children; i++) {
104 			if (list->child[i]) {
105 				zend_persist_ast_calc(list->child[i]);
106 			}
107 		}
108 	} else {
109 		uint32_t children = zend_ast_get_num_children(ast);
110 		ADD_SIZE(sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children);
111 		for (i = 0; i < children; i++) {
112 			if (ast->child[i]) {
113 				zend_persist_ast_calc(ast->child[i]);
114 			}
115 		}
116 	}
117 }
118 
zend_persist_zval_calc(zval * z)119 static void zend_persist_zval_calc(zval *z)
120 {
121 	uint32_t size;
122 
123 	switch (Z_TYPE_P(z)) {
124 		case IS_STRING:
125 		case IS_CONSTANT:
126 			ADD_INTERNED_STRING(Z_STR_P(z), 0);
127 			if (ZSTR_IS_INTERNED(Z_STR_P(z))) {
128 				Z_TYPE_FLAGS_P(z) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
129 			}
130 			break;
131 		case IS_ARRAY:
132 			size = zend_shared_memdup_size(Z_ARR_P(z), sizeof(zend_array));
133 			if (size) {
134 				ADD_SIZE(size);
135 				zend_hash_persist_calc(Z_ARRVAL_P(z), zend_persist_zval_calc);
136 			}
137 			break;
138 		case IS_REFERENCE:
139 			size = zend_shared_memdup_size(Z_REF_P(z), sizeof(zend_reference));
140 			if (size) {
141 				ADD_SIZE(size);
142 				zend_persist_zval_calc(Z_REFVAL_P(z));
143 			}
144 			break;
145 		case IS_CONSTANT_AST:
146 			size = zend_shared_memdup_size(Z_AST_P(z), sizeof(zend_ast_ref));
147 			if (size) {
148 				ADD_SIZE(size);
149 				zend_persist_ast_calc(Z_ASTVAL_P(z));
150 			}
151 			break;
152 	}
153 }
154 
zend_persist_op_array_calc_ex(zend_op_array * op_array)155 static void zend_persist_op_array_calc_ex(zend_op_array *op_array)
156 {
157 	if (op_array->type != ZEND_USER_FUNCTION) {
158 		return;
159 	}
160 
161 	if (op_array->static_variables) {
162 		if (!zend_shared_alloc_get_xlat_entry(op_array->static_variables)) {
163 			HashTable *old = op_array->static_variables;
164 
165 			ADD_DUP_SIZE(op_array->static_variables, sizeof(HashTable));
166 			zend_hash_persist_calc(op_array->static_variables, zend_persist_zval_calc);
167 			zend_shared_alloc_register_xlat_entry(old, op_array->static_variables);
168 		}
169 	}
170 
171 	if (zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
172 		/* already stored */
173 		if (op_array->function_name) {
174 			zend_string *new_name = zend_shared_alloc_get_xlat_entry(op_array->function_name);
175 			if (new_name) {
176 				op_array->function_name = new_name;
177 			}
178 		}
179 		return;
180 	}
181 
182 	if (op_array->literals) {
183 		zval *p = op_array->literals;
184 		zval *end = p + op_array->last_literal;
185 		ADD_DUP_SIZE(op_array->literals, sizeof(zval) * op_array->last_literal);
186 		while (p < end) {
187 			zend_persist_zval_calc(p);
188 			p++;
189 		}
190 	}
191 
192 	ADD_DUP_SIZE(op_array->opcodes, sizeof(zend_op) * op_array->last);
193 
194 	if (op_array->function_name) {
195 		zend_string *old_name = op_array->function_name;
196 		zend_string *new_name = zend_shared_alloc_get_xlat_entry(old_name);
197 
198 		if (new_name) {
199 			op_array->function_name = new_name;
200 		} else {
201 			ADD_INTERNED_STRING(op_array->function_name, 0);
202 			zend_shared_alloc_register_xlat_entry(old_name, op_array->function_name);
203 		}
204     }
205 
206 	if (op_array->filename) {
207 		ADD_STRING(op_array->filename);
208 	}
209 
210 	if (op_array->arg_info) {
211 		zend_arg_info *arg_info = op_array->arg_info;
212 		uint32_t num_args = op_array->num_args;
213 		uint32_t i;
214 
215 		num_args = op_array->num_args;
216 		if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
217 			num_args++;
218 		}
219 		if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
220 			arg_info--;
221 			num_args++;
222 		}
223 		ADD_DUP_SIZE(arg_info, sizeof(zend_arg_info) * num_args);
224 		for (i = 0; i < num_args; i++) {
225 			if (arg_info[i].name) {
226 				ADD_INTERNED_STRING(arg_info[i].name, 1);
227 			}
228 			if (ZEND_TYPE_IS_CLASS(arg_info[i].type)) {
229 				zend_string *type_name = ZEND_TYPE_NAME(arg_info[i].type);
230 				zend_bool allow_null = ZEND_TYPE_ALLOW_NULL(arg_info[i].type);
231 
232 				ADD_INTERNED_STRING(type_name, 1);
233 				arg_info[i].type = ZEND_TYPE_ENCODE_CLASS(type_name, allow_null);
234 			}
235 		}
236 	}
237 
238 	if (op_array->live_range) {
239 		ADD_DUP_SIZE(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
240 	}
241 
242 	if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
243 		ADD_STRING(op_array->doc_comment);
244 	}
245 
246 	if (op_array->try_catch_array) {
247 		ADD_DUP_SIZE(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
248 	}
249 
250 	if (op_array->vars) {
251 		int i;
252 
253 		ADD_DUP_SIZE(op_array->vars, sizeof(zend_string*) * op_array->last_var);
254 		for (i = 0; i < op_array->last_var; i++) {
255 			ADD_INTERNED_STRING(op_array->vars[i], 0);
256 		}
257 	}
258 
259 	ADD_SIZE(ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist_calc(op_array)));
260 }
261 
zend_persist_op_array_calc(zval * zv)262 static void zend_persist_op_array_calc(zval *zv)
263 {
264 	zend_op_array *op_array = Z_PTR_P(zv);
265 
266 	if (op_array->type == ZEND_USER_FUNCTION/* &&
267 	    (!op_array->refcount || *(op_array->refcount) > 1)*/) {
268 		zend_op_array *old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
269 		if (old_op_array) {
270 			Z_PTR_P(zv) = old_op_array;
271 		} else {
272 			ADD_ARENA_SIZE(sizeof(zend_op_array));
273 			zend_persist_op_array_calc_ex(Z_PTR_P(zv));
274 			zend_shared_alloc_register_xlat_entry(op_array, Z_PTR_P(zv));
275 		}
276 	} else {
277 		ADD_ARENA_SIZE(sizeof(zend_op_array));
278 		zend_persist_op_array_calc_ex(Z_PTR_P(zv));
279 	}
280 }
281 
zend_persist_property_info_calc(zval * zv)282 static void zend_persist_property_info_calc(zval *zv)
283 {
284 	zend_property_info *prop = Z_PTR_P(zv);
285 
286 	if (!zend_shared_alloc_get_xlat_entry(prop)) {
287 		zend_shared_alloc_register_xlat_entry(prop, prop);
288 		ADD_ARENA_SIZE(sizeof(zend_property_info));
289 		ADD_INTERNED_STRING(prop->name, 0);
290 		if (ZCG(accel_directives).save_comments && prop->doc_comment) {
291 			ADD_STRING(prop->doc_comment);
292 		}
293 	}
294 }
295 
zend_persist_class_constant_calc(zval * zv)296 static void zend_persist_class_constant_calc(zval *zv)
297 {
298 	zend_class_constant *c = Z_PTR_P(zv);
299 
300 	if (!zend_shared_alloc_get_xlat_entry(c)) {
301 		zend_shared_alloc_register_xlat_entry(c, c);
302 		ADD_ARENA_SIZE(sizeof(zend_class_constant));
303 		zend_persist_zval_calc(&c->value);
304 		if (ZCG(accel_directives).save_comments && c->doc_comment) {
305 			ADD_STRING(c->doc_comment);
306 		}
307 	}
308 }
309 
310 
zend_persist_class_entry_calc(zval * zv)311 static void zend_persist_class_entry_calc(zval *zv)
312 {
313 	zend_class_entry *ce = Z_PTR_P(zv);
314 
315 	if (ce->type == ZEND_USER_CLASS) {
316 		ADD_ARENA_SIZE(sizeof(zend_class_entry));
317 		ADD_INTERNED_STRING(ce->name, 0);
318 		zend_hash_persist_calc(&ce->function_table, zend_persist_op_array_calc);
319 		if (ce->default_properties_table) {
320 		    int i;
321 
322 			ADD_SIZE(sizeof(zval) * ce->default_properties_count);
323 			for (i = 0; i < ce->default_properties_count; i++) {
324 				zend_persist_zval_calc(&ce->default_properties_table[i]);
325 			}
326 		}
327 		if (ce->default_static_members_table) {
328 		    int i;
329 
330 			ADD_SIZE(sizeof(zval) * ce->default_static_members_count);
331 			for (i = 0; i < ce->default_static_members_count; i++) {
332 				zend_persist_zval_calc(&ce->default_static_members_table[i]);
333 			}
334 		}
335 		zend_hash_persist_calc(&ce->constants_table, zend_persist_class_constant_calc);
336 
337 		if (ce->info.user.filename) {
338 			ADD_STRING(ce->info.user.filename);
339 		}
340 		if (ZCG(accel_directives).save_comments && ce->info.user.doc_comment) {
341 			ADD_STRING(ce->info.user.doc_comment);
342 		}
343 
344 		zend_hash_persist_calc(&ce->properties_info, zend_persist_property_info_calc);
345 
346 		if (ce->trait_aliases) {
347 			int i = 0;
348 			while (ce->trait_aliases[i]) {
349 				if (ce->trait_aliases[i]->trait_method) {
350 					if (ce->trait_aliases[i]->trait_method->method_name) {
351 						ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->method_name, 0);
352 					}
353 					if (ce->trait_aliases[i]->trait_method->class_name) {
354 						ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method->class_name, 0);
355 					}
356 					ADD_SIZE(sizeof(zend_trait_method_reference));
357 				}
358 
359 				if (ce->trait_aliases[i]->alias) {
360 					ADD_INTERNED_STRING(ce->trait_aliases[i]->alias, 0);
361 				}
362 				ADD_SIZE(sizeof(zend_trait_alias));
363 				i++;
364 			}
365 			ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
366 		}
367 
368 		if (ce->trait_precedences) {
369 			int i = 0;
370 
371 			while (ce->trait_precedences[i]) {
372 				ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->method_name, 0);
373 				ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method->class_name, 0);
374 				ADD_SIZE(sizeof(zend_trait_method_reference));
375 
376 				if (ce->trait_precedences[i]->exclude_from_classes) {
377 					int j = 0;
378 
379 					while (ce->trait_precedences[i]->exclude_from_classes[j].class_name) {
380 						ADD_INTERNED_STRING(ce->trait_precedences[i]->exclude_from_classes[j].class_name, 0);
381 						j++;
382 					}
383 					ADD_SIZE(sizeof(zend_class_entry*) * (j + 1));
384 				}
385 				ADD_SIZE(sizeof(zend_trait_precedence));
386 				i++;
387 			}
388 			ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
389 		}
390 	}
391 }
392 
zend_accel_persist_class_table_calc(HashTable * class_table)393 static void zend_accel_persist_class_table_calc(HashTable *class_table)
394 {
395 	zend_hash_persist_calc(class_table, zend_persist_class_entry_calc);
396 }
397 
zend_accel_script_persist_calc(zend_persistent_script * new_persistent_script,char * key,unsigned int key_length,int for_shm)398 uint32_t zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, char *key, unsigned int key_length, int for_shm)
399 {
400 	new_persistent_script->mem = NULL;
401 	new_persistent_script->size = 0;
402 	new_persistent_script->arena_mem = NULL;
403 	new_persistent_script->arena_size = 0;
404 	new_persistent_script->corrupted = 0;
405 	ZCG(current_persistent_script) = new_persistent_script;
406 
407 	if (!for_shm) {
408 		/* script is not going to be saved in SHM */
409 		new_persistent_script->corrupted = 1;
410 	}
411 
412 	ADD_DUP_SIZE(new_persistent_script, sizeof(zend_persistent_script));
413 	if (key) {
414 		ADD_DUP_SIZE(key, key_length + 1);
415 	}
416 	ADD_STRING(new_persistent_script->script.filename);
417 
418 #ifdef __SSE2__
419 	/* Align size to 64-byte boundary */
420 	new_persistent_script->size = (new_persistent_script->size + 63) & ~63;
421 #endif
422 
423 	zend_accel_persist_class_table_calc(&new_persistent_script->script.class_table);
424 	zend_hash_persist_calc(&new_persistent_script->script.function_table, zend_persist_op_array_calc);
425 	zend_persist_op_array_calc_ex(&new_persistent_script->script.main_op_array);
426 
427 #ifdef __SSE2__
428 	/* Align size to 64-byte boundary */
429 	new_persistent_script->arena_size = (new_persistent_script->arena_size + 63) & ~63;
430 #endif
431 
432 	new_persistent_script->size += new_persistent_script->arena_size;
433 	new_persistent_script->corrupted = 0;
434 
435 	ZCG(current_persistent_script) = NULL;
436 
437 	return new_persistent_script->size;
438 }
439