Coverage Report

Created: 2026-01-18 06:47

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