Coverage Report

Created: 2025-09-27 06:26

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