Coverage Report

Created: 2025-12-14 06:05

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