Coverage Report

Created: 2026-02-14 06:52

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