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