Coverage Report

Created: 2026-09-14 06:25

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