Coverage Report

Created: 2026-06-13 07:01

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