Coverage Report

Created: 2025-12-14 06:09

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