Coverage Report

Created: 2025-12-31 07:28

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