Coverage Report

Created: 2025-11-16 06:23

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