Coverage Report

Created: 2026-06-02 06:40

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