Coverage Report

Created: 2026-07-25 06:39

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