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