Coverage Report

Created: 2026-06-02 06:40

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