Coverage Report

Created: 2026-01-18 06:48

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