Coverage Report

Created: 2026-06-02 06:39

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