Coverage Report

Created: 2026-04-01 06:49

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