Coverage Report

Created: 2026-06-02 06:36

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