xref: /php-src/ext/opcache/zend_persist_calc.c (revision 780a8280)
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 "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 #include "zend_attributes.h"
29 #include "zend_constants.h"
30 
31 #define ADD_DUP_SIZE(m,s)  ZCG(current_persistent_script)->size += zend_shared_memdup_size((void*)m, s)
32 #define ADD_SIZE(m)        ZCG(current_persistent_script)->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 { \
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 				(str) = tmp; \
43 			} else { \
44 				ADD_STRING(str); \
45 			} \
46 		} \
47 	} while (0)
48 
49 static void zend_persist_zval_calc(zval *z);
50 static void zend_persist_op_array_calc(zval *zv);
51 
zend_hash_persist_calc(HashTable * ht)52 static void zend_hash_persist_calc(HashTable *ht)
53 {
54 	if ((HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) || ht->nNumUsed == 0) {
55 		return;
56 	}
57 
58 	if (HT_IS_PACKED(ht)) {
59 		ADD_SIZE(HT_PACKED_USED_SIZE(ht));
60 	} else if (ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) {
61 		/* compact table */
62 		uint32_t hash_size;
63 
64 		hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
65 		while (hash_size >> 2 > ht->nNumUsed) {
66 			hash_size >>= 1;
67 		}
68 		ADD_SIZE(hash_size * sizeof(uint32_t) + ht->nNumUsed * sizeof(Bucket));
69 	} else {
70 		ADD_SIZE(HT_USED_SIZE(ht));
71 	}
72 }
73 
zend_persist_ast_calc(zend_ast * ast)74 static void zend_persist_ast_calc(zend_ast *ast)
75 {
76 	uint32_t i;
77 
78 	if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
79 		ADD_SIZE(sizeof(zend_ast_zval));
80 		zend_persist_zval_calc(&((zend_ast_zval*)(ast))->val);
81 	} else if (zend_ast_is_list(ast)) {
82 		zend_ast_list *list = zend_ast_get_list(ast);
83 		ADD_SIZE(sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
84 		for (i = 0; i < list->children; i++) {
85 			if (list->child[i]) {
86 				zend_persist_ast_calc(list->child[i]);
87 			}
88 		}
89 	} else {
90 		uint32_t children = zend_ast_get_num_children(ast);
91 		ADD_SIZE(zend_ast_size(children));
92 		for (i = 0; i < children; i++) {
93 			if (ast->child[i]) {
94 				zend_persist_ast_calc(ast->child[i]);
95 			}
96 		}
97 	}
98 }
99 
zend_persist_zval_calc(zval * z)100 static void zend_persist_zval_calc(zval *z)
101 {
102 	uint32_t size;
103 
104 	switch (Z_TYPE_P(z)) {
105 		case IS_STRING:
106 			ADD_INTERNED_STRING(Z_STR_P(z));
107 			if (ZSTR_IS_INTERNED(Z_STR_P(z))) {
108 				Z_TYPE_FLAGS_P(z) = 0;
109 			}
110 			break;
111 		case IS_ARRAY:
112 			if (!ZCG(current_persistent_script)->corrupted
113 			 && zend_accel_in_shm(Z_ARR_P(z))) {
114 				return;
115 			}
116 			size = zend_shared_memdup_size(Z_ARR_P(z), sizeof(zend_array));
117 			if (size) {
118 				HashTable *ht = Z_ARRVAL_P(z);
119 
120 				ADD_SIZE(size);
121 				zend_hash_persist_calc(ht);
122 				if (HT_IS_PACKED(ht)) {
123 					zval *zv;
124 
125 					ZEND_HASH_PACKED_FOREACH_VAL(Z_ARRVAL_P(z), zv) {
126 						zend_persist_zval_calc(zv);
127 					} ZEND_HASH_FOREACH_END();
128 				} else {
129 					Bucket *p;
130 
131 					ZEND_HASH_MAP_FOREACH_BUCKET(Z_ARRVAL_P(z), p) {
132 						if (p->key) {
133 							ADD_INTERNED_STRING(p->key);
134 						}
135 						zend_persist_zval_calc(&p->val);
136 					} ZEND_HASH_FOREACH_END();
137 				}
138 			}
139 			break;
140 		case IS_CONSTANT_AST:
141 			if (ZCG(current_persistent_script)->corrupted
142 			 || !zend_accel_in_shm(Z_AST_P(z))) {
143 				size = zend_shared_memdup_size(Z_AST_P(z), sizeof(zend_ast_ref));
144 				if (size) {
145 					ADD_SIZE(size);
146 					zend_persist_ast_calc(Z_ASTVAL_P(z));
147 				}
148 			}
149 			break;
150 		default:
151 			ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING);
152 			break;
153 	}
154 }
155 
zend_persist_attributes_calc(HashTable * attributes)156 static void zend_persist_attributes_calc(HashTable *attributes)
157 {
158 	if (!zend_shared_alloc_get_xlat_entry(attributes)
159 	 && (ZCG(current_persistent_script)->corrupted
160 	  || !zend_accel_in_shm(attributes))) {
161 		zend_attribute *attr;
162 		uint32_t i;
163 
164 		zend_shared_alloc_register_xlat_entry(attributes, attributes);
165 		ADD_SIZE(sizeof(HashTable));
166 		zend_hash_persist_calc(attributes);
167 
168 		ZEND_HASH_PACKED_FOREACH_PTR(attributes, attr) {
169 			ADD_SIZE(ZEND_ATTRIBUTE_SIZE(attr->argc));
170 			ADD_INTERNED_STRING(attr->name);
171 			ADD_INTERNED_STRING(attr->lcname);
172 
173 			for (i = 0; i < attr->argc; i++) {
174 				if (attr->args[i].name) {
175 					ADD_INTERNED_STRING(attr->args[i].name);
176 				}
177 				zend_persist_zval_calc(&attr->args[i].value);
178 			}
179 		} ZEND_HASH_FOREACH_END();
180 	}
181 }
182 
zend_persist_type_calc(zend_type * type)183 static void zend_persist_type_calc(zend_type *type)
184 {
185 	if (ZEND_TYPE_HAS_LIST(*type)) {
186 		ADD_SIZE(ZEND_TYPE_LIST_SIZE(ZEND_TYPE_LIST(*type)->num_types));
187 	}
188 
189 	zend_type *single_type;
190 	ZEND_TYPE_FOREACH(*type, single_type) {
191 		if (ZEND_TYPE_HAS_LIST(*single_type)) {
192 			zend_persist_type_calc(single_type);
193 			continue;
194 		}
195 		if (ZEND_TYPE_HAS_NAME(*single_type)) {
196 			zend_string *type_name = ZEND_TYPE_NAME(*single_type);
197 			ADD_INTERNED_STRING(type_name);
198 			ZEND_TYPE_SET_PTR(*single_type, type_name);
199 		}
200 	} ZEND_TYPE_FOREACH_END();
201 }
202 
zend_persist_op_array_calc_ex(zend_op_array * op_array)203 static void zend_persist_op_array_calc_ex(zend_op_array *op_array)
204 {
205 	if (op_array->function_name) {
206 		zend_string *old_name = op_array->function_name;
207 		ADD_INTERNED_STRING(op_array->function_name);
208 		/* Remember old function name, so it can be released multiple times if shared. */
209 		if (op_array->function_name != old_name
210 				&& !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) {
211 			zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name);
212 		}
213 	}
214 
215 	if (op_array->scope) {
216 		if (zend_shared_alloc_get_xlat_entry(op_array->opcodes)) {
217 			/* already stored */
218 			ADD_SIZE(ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist_calc(op_array)));
219 			return;
220 		}
221 	}
222 
223 	if (op_array->scope
224 	 && !(op_array->fn_flags & ZEND_ACC_CLOSURE)
225 	 && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) {
226 		return;
227 	}
228 
229 	if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) {
230 		if (!zend_shared_alloc_get_xlat_entry(op_array->static_variables)) {
231 			Bucket *p;
232 
233 			zend_shared_alloc_register_xlat_entry(op_array->static_variables, op_array->static_variables);
234 			ADD_SIZE(sizeof(HashTable));
235 			zend_hash_persist_calc(op_array->static_variables);
236 			ZEND_HASH_MAP_FOREACH_BUCKET(op_array->static_variables, p) {
237 				ZEND_ASSERT(p->key != NULL);
238 				ADD_INTERNED_STRING(p->key);
239 				zend_persist_zval_calc(&p->val);
240 			} ZEND_HASH_FOREACH_END();
241 		}
242 	}
243 
244 	if (op_array->literals) {
245 		zval *p = op_array->literals;
246 		zval *end = p + op_array->last_literal;
247 		ADD_SIZE(sizeof(zval) * op_array->last_literal);
248 		while (p < end) {
249 			zend_persist_zval_calc(p);
250 			p++;
251 		}
252 	}
253 
254 	zend_shared_alloc_register_xlat_entry(op_array->opcodes, op_array->opcodes);
255 	ADD_SIZE(sizeof(zend_op) * op_array->last);
256 
257 	if (op_array->filename) {
258 		ADD_STRING(op_array->filename);
259 	}
260 
261 	if (op_array->arg_info) {
262 		zend_arg_info *arg_info = op_array->arg_info;
263 		uint32_t num_args = op_array->num_args;
264 		uint32_t i;
265 
266 		if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
267 			num_args++;
268 		}
269 		if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
270 			arg_info--;
271 			num_args++;
272 		}
273 		ADD_SIZE(sizeof(zend_arg_info) * num_args);
274 		for (i = 0; i < num_args; i++) {
275 			if (arg_info[i].name) {
276 				ADD_INTERNED_STRING(arg_info[i].name);
277 			}
278 			zend_persist_type_calc(&arg_info[i].type);
279 		}
280 	}
281 
282 	if (op_array->live_range) {
283 		ADD_SIZE(sizeof(zend_live_range) * op_array->last_live_range);
284 	}
285 
286 	if (ZCG(accel_directives).save_comments && op_array->doc_comment) {
287 		ADD_STRING(op_array->doc_comment);
288 	}
289 
290 	if (op_array->attributes) {
291 		zend_persist_attributes_calc(op_array->attributes);
292 	}
293 
294 	if (op_array->try_catch_array) {
295 		ADD_SIZE(sizeof(zend_try_catch_element) * op_array->last_try_catch);
296 	}
297 
298 	if (op_array->vars) {
299 		int i;
300 
301 		ADD_SIZE(sizeof(zend_string*) * op_array->last_var);
302 		for (i = 0; i < op_array->last_var; i++) {
303 			ADD_INTERNED_STRING(op_array->vars[i]);
304 		}
305 	}
306 
307 	if (op_array->num_dynamic_func_defs) {
308 		ADD_SIZE(sizeof(void *) * op_array->num_dynamic_func_defs);
309 		for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
310 			zval tmp;
311 			ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]);
312 			zend_persist_op_array_calc(&tmp);
313 		}
314 	}
315 
316 	ADD_SIZE(ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist_calc(op_array)));
317 }
318 
zend_persist_op_array_calc(zval * zv)319 static void zend_persist_op_array_calc(zval *zv)
320 {
321 	zend_op_array *op_array = Z_PTR_P(zv);
322 	ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
323 	if (!zend_shared_alloc_get_xlat_entry(op_array)) {
324 		zend_shared_alloc_register_xlat_entry(op_array, op_array);
325 		ADD_SIZE(sizeof(zend_op_array));
326 		zend_persist_op_array_calc_ex(op_array);
327 	} else {
328 		/* This can happen during preloading, if a dynamic function definition is declared. */
329 	}
330 }
331 
zend_persist_class_method_calc(zend_op_array * op_array)332 static void zend_persist_class_method_calc(zend_op_array *op_array)
333 {
334 	zend_op_array *old_op_array;
335 
336 	if (op_array->type != ZEND_USER_FUNCTION) {
337 		ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION);
338 		if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) {
339 			old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
340 			if (!old_op_array) {
341 				ADD_SIZE(sizeof(zend_internal_function));
342 				zend_shared_alloc_register_xlat_entry(op_array, op_array);
343 			}
344 		}
345 		return;
346 	}
347 
348 	if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
349 	 && !ZCG(current_persistent_script)->corrupted
350 	 && zend_accel_in_shm(op_array)) {
351 		zend_shared_alloc_register_xlat_entry(op_array, op_array);
352 		return;
353 	}
354 
355 	old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
356 	if (!old_op_array) {
357 		ADD_SIZE(sizeof(zend_op_array));
358 		zend_persist_op_array_calc_ex(op_array);
359 		zend_shared_alloc_register_xlat_entry(op_array, op_array);
360 	} else {
361 		/* If op_array is shared, the function name refcount is still incremented for each use,
362 		 * so we need to release it here. We remembered the original function name in xlat. */
363 		zend_string *old_function_name =
364 			zend_shared_alloc_get_xlat_entry(&old_op_array->function_name);
365 		if (old_function_name) {
366 			zend_string_release_ex(old_function_name, 0);
367 		}
368 	}
369 }
370 
zend_persist_property_info_calc(zend_property_info * prop)371 static void zend_persist_property_info_calc(zend_property_info *prop)
372 {
373 	ADD_SIZE(sizeof(zend_property_info));
374 	ADD_INTERNED_STRING(prop->name);
375 	zend_persist_type_calc(&prop->type);
376 	if (ZCG(accel_directives).save_comments && prop->doc_comment) {
377 		ADD_STRING(prop->doc_comment);
378 	}
379 	if (prop->attributes) {
380 		zend_persist_attributes_calc(prop->attributes);
381 	}
382 	if (prop->hooks) {
383 		ADD_SIZE(ZEND_PROPERTY_HOOK_STRUCT_SIZE);
384 		for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
385 			if (prop->hooks[i]) {
386 				zend_persist_class_method_calc(&prop->hooks[i]->op_array);
387 			}
388 		}
389 	}
390 }
391 
zend_persist_class_constant_calc(zval * zv)392 static void zend_persist_class_constant_calc(zval *zv)
393 {
394 	zend_class_constant *c = Z_PTR_P(zv);
395 
396 	if (!zend_shared_alloc_get_xlat_entry(c)) {
397 		if (((c->ce->ce_flags & ZEND_ACC_IMMUTABLE) && !(Z_CONSTANT_FLAGS(c->value) & CONST_OWNED))
398 		 || c->ce->type == ZEND_INTERNAL_CLASS) {
399 			/* Class constant comes from a different file in shm or internal class, keep existing pointer. */
400 			return;
401 		}
402 		if (!ZCG(current_persistent_script)->corrupted
403 		 && zend_accel_in_shm(Z_PTR_P(zv))) {
404 			return;
405 		}
406 		zend_shared_alloc_register_xlat_entry(c, c);
407 		ADD_SIZE(sizeof(zend_class_constant));
408 		zend_persist_zval_calc(&c->value);
409 		if (ZCG(accel_directives).save_comments && c->doc_comment) {
410 			ADD_STRING(c->doc_comment);
411 		}
412 		if (c->attributes) {
413 			zend_persist_attributes_calc(c->attributes);
414 		}
415 		zend_persist_type_calc(&c->type);
416 	}
417 }
418 
zend_persist_class_entry_calc(zend_class_entry * ce)419 void zend_persist_class_entry_calc(zend_class_entry *ce)
420 {
421 	Bucket *p;
422 
423 	if (ce->type == ZEND_USER_CLASS) {
424 		/* The same zend_class_entry may be reused by class_alias */
425 		if (zend_shared_alloc_get_xlat_entry(ce)) {
426 			return;
427 		}
428 		zend_shared_alloc_register_xlat_entry(ce, ce);
429 
430 		ADD_SIZE(sizeof(zend_class_entry));
431 
432 		if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
433 			ADD_INTERNED_STRING(ce->name);
434 			if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) {
435 				ADD_INTERNED_STRING(ce->parent_name);
436 			}
437 		}
438 
439 		zend_hash_persist_calc(&ce->function_table);
440 		ZEND_HASH_MAP_FOREACH_BUCKET(&ce->function_table, p) {
441 			ZEND_ASSERT(p->key != NULL);
442 			ADD_INTERNED_STRING(p->key);
443 			zend_persist_class_method_calc(Z_PTR(p->val));
444 		} ZEND_HASH_FOREACH_END();
445 		if (ce->default_properties_table) {
446 		    int i;
447 
448 			ADD_SIZE(sizeof(zval) * ce->default_properties_count);
449 			for (i = 0; i < ce->default_properties_count; i++) {
450 				zend_persist_zval_calc(&ce->default_properties_table[i]);
451 			}
452 		}
453 		if (ce->default_static_members_table) {
454 		    int i;
455 
456 			ADD_SIZE(sizeof(zval) * ce->default_static_members_count);
457 			for (i = 0; i < ce->default_static_members_count; i++) {
458 				if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) {
459 					zend_persist_zval_calc(&ce->default_static_members_table[i]);
460 				}
461 			}
462 		}
463 		zend_hash_persist_calc(&ce->constants_table);
464 		ZEND_HASH_MAP_FOREACH_BUCKET(&ce->constants_table, p) {
465 			ZEND_ASSERT(p->key != NULL);
466 			ADD_INTERNED_STRING(p->key);
467 			zend_persist_class_constant_calc(&p->val);
468 		} ZEND_HASH_FOREACH_END();
469 
470 		zend_hash_persist_calc(&ce->properties_info);
471 		ZEND_HASH_MAP_FOREACH_BUCKET(&ce->properties_info, p) {
472 			zend_property_info *prop = Z_PTR(p->val);
473 			ZEND_ASSERT(p->key != NULL);
474 			ADD_INTERNED_STRING(p->key);
475 			if (prop->ce == ce) {
476 				zend_persist_property_info_calc(prop);
477 			}
478 		} ZEND_HASH_FOREACH_END();
479 
480 		if (ce->properties_info_table) {
481 			ADD_SIZE(sizeof(zend_property_info *) * ce->default_properties_count);
482 		}
483 
484 		if (ce->num_interfaces && (ce->ce_flags & ZEND_ACC_LINKED)) {
485 			ADD_SIZE(sizeof(zend_class_entry*) * ce->num_interfaces);
486 		}
487 
488 		if (ce->iterator_funcs_ptr) {
489 			ADD_SIZE(sizeof(zend_class_iterator_funcs));
490 		}
491 		if (ce->arrayaccess_funcs_ptr) {
492 			ADD_SIZE(sizeof(zend_class_arrayaccess_funcs));
493 		}
494 
495 		if (ce->ce_flags & ZEND_ACC_CACHED) {
496 			return;
497 		}
498 
499 		if (ce->info.user.filename) {
500 			ADD_STRING(ce->info.user.filename);
501 		}
502 
503 		if (ZCG(accel_directives).save_comments && ce->doc_comment) {
504 			ADD_STRING(ce->doc_comment);
505 		}
506 
507 		if (ce->attributes) {
508 			zend_persist_attributes_calc(ce->attributes);
509 		}
510 
511 		if (ce->num_interfaces) {
512 			uint32_t i;
513 
514 			if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
515 				for (i = 0; i < ce->num_interfaces; i++) {
516 					ADD_INTERNED_STRING(ce->interface_names[i].name);
517 					ADD_INTERNED_STRING(ce->interface_names[i].lc_name);
518 				}
519 				ADD_SIZE(sizeof(zend_class_name) * ce->num_interfaces);
520 			}
521 		}
522 
523 		if (ce->num_traits) {
524 			uint32_t i;
525 
526 			for (i = 0; i < ce->num_traits; i++) {
527 				ADD_INTERNED_STRING(ce->trait_names[i].name);
528 				ADD_INTERNED_STRING(ce->trait_names[i].lc_name);
529 			}
530 			ADD_SIZE(sizeof(zend_class_name) * ce->num_traits);
531 
532 			if (ce->trait_aliases) {
533 				i = 0;
534 				while (ce->trait_aliases[i]) {
535 					if (ce->trait_aliases[i]->trait_method.method_name) {
536 						ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method.method_name);
537 					}
538 					if (ce->trait_aliases[i]->trait_method.class_name) {
539 						ADD_INTERNED_STRING(ce->trait_aliases[i]->trait_method.class_name);
540 					}
541 
542 					if (ce->trait_aliases[i]->alias) {
543 						ADD_INTERNED_STRING(ce->trait_aliases[i]->alias);
544 					}
545 					ADD_SIZE(sizeof(zend_trait_alias));
546 					i++;
547 				}
548 				ADD_SIZE(sizeof(zend_trait_alias*) * (i + 1));
549 			}
550 
551 			if (ce->trait_precedences) {
552 				int j;
553 
554 				i = 0;
555 				while (ce->trait_precedences[i]) {
556 					ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method.method_name);
557 					ADD_INTERNED_STRING(ce->trait_precedences[i]->trait_method.class_name);
558 
559 					for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
560 						ADD_INTERNED_STRING(ce->trait_precedences[i]->exclude_class_names[j]);
561 					}
562 					ADD_SIZE(sizeof(zend_trait_precedence) + (ce->trait_precedences[i]->num_excludes - 1) * sizeof(zend_string*));
563 					i++;
564 				}
565 				ADD_SIZE(sizeof(zend_trait_precedence*) * (i + 1));
566 			}
567 		}
568 	}
569 }
570 
zend_accel_persist_class_table_calc(HashTable * class_table)571 static void zend_accel_persist_class_table_calc(HashTable *class_table)
572 {
573 	Bucket *p;
574 
575 	zend_hash_persist_calc(class_table);
576 	ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
577 		ZEND_ASSERT(p->key != NULL);
578 		ADD_INTERNED_STRING(p->key);
579 		zend_persist_class_entry_calc(Z_CE(p->val));
580 	} ZEND_HASH_FOREACH_END();
581 }
582 
zend_persist_warnings_calc(uint32_t num_warnings,zend_error_info ** warnings)583 void zend_persist_warnings_calc(uint32_t num_warnings, zend_error_info **warnings) {
584 	ADD_SIZE(num_warnings * sizeof(zend_error_info *));
585 	for (uint32_t i = 0; i < num_warnings; i++) {
586 		ADD_SIZE(sizeof(zend_error_info));
587 		ADD_STRING(warnings[i]->filename);
588 		ADD_STRING(warnings[i]->message);
589 	}
590 }
591 
zend_persist_early_bindings_calc(uint32_t num_early_bindings,zend_early_binding * early_bindings)592 static void zend_persist_early_bindings_calc(
593 	uint32_t num_early_bindings, zend_early_binding *early_bindings)
594 {
595 	ADD_SIZE(sizeof(zend_early_binding) * num_early_bindings);
596 	for (uint32_t i = 0; i < num_early_bindings; i++) {
597 		zend_early_binding *early_binding = &early_bindings[i];
598 		ADD_INTERNED_STRING(early_binding->lcname);
599 		ADD_INTERNED_STRING(early_binding->rtd_key);
600 		ADD_INTERNED_STRING(early_binding->lc_parent_name);
601 	}
602 }
603 
zend_accel_script_persist_calc(zend_persistent_script * new_persistent_script,int for_shm)604 uint32_t zend_accel_script_persist_calc(zend_persistent_script *new_persistent_script, int for_shm)
605 {
606 	Bucket *p;
607 
608 	new_persistent_script->mem = NULL;
609 	new_persistent_script->size = 0;
610 	new_persistent_script->corrupted = false;
611 	ZCG(current_persistent_script) = new_persistent_script;
612 
613 	if (!for_shm) {
614 		/* script is not going to be saved in SHM */
615 		new_persistent_script->corrupted = true;
616 	}
617 
618 	ADD_SIZE(sizeof(zend_persistent_script));
619 	ADD_INTERNED_STRING(new_persistent_script->script.filename);
620 
621 #if defined(__AVX__) || defined(__SSE2__)
622 	/* Align size to 64-byte boundary */
623 	new_persistent_script->size = (new_persistent_script->size + 63) & ~63;
624 #endif
625 
626 	if (new_persistent_script->script.class_table.nNumUsed != new_persistent_script->script.class_table.nNumOfElements) {
627 		zend_hash_rehash(&new_persistent_script->script.class_table);
628 	}
629 	zend_accel_persist_class_table_calc(&new_persistent_script->script.class_table);
630 	if (new_persistent_script->script.function_table.nNumUsed != new_persistent_script->script.function_table.nNumOfElements) {
631 		zend_hash_rehash(&new_persistent_script->script.function_table);
632 	}
633 	zend_hash_persist_calc(&new_persistent_script->script.function_table);
634 	ZEND_HASH_MAP_FOREACH_BUCKET(&new_persistent_script->script.function_table, p) {
635 		ZEND_ASSERT(p->key != NULL);
636 		ADD_INTERNED_STRING(p->key);
637 		zend_persist_op_array_calc(&p->val);
638 	} ZEND_HASH_FOREACH_END();
639 	zend_persist_op_array_calc_ex(&new_persistent_script->script.main_op_array);
640 	zend_persist_warnings_calc(
641 		new_persistent_script->num_warnings, new_persistent_script->warnings);
642 	zend_persist_early_bindings_calc(
643 		new_persistent_script->num_early_bindings, new_persistent_script->early_bindings);
644 
645 	new_persistent_script->corrupted = false;
646 
647 	ZCG(current_persistent_script) = NULL;
648 
649 	return new_persistent_script->size;
650 }
651