Coverage Report

Created: 2026-10-28 06:20

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