Coverage Report

Created: 2026-06-02 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/zend_persist.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_vm.h"
26
#include "zend_constants.h"
27
#include "zend_operators.h"
28
#include "zend_interfaces.h"
29
#include "zend_attributes.h"
30
31
#ifdef HAVE_JIT
32
# include "Optimizer/zend_func_info.h"
33
# include "jit/zend_jit.h"
34
#endif
35
36
45.9k
#define zend_set_str_gc_flags(str) do { \
37
45.9k
  GC_SET_REFCOUNT(str, 2); \
38
45.9k
  uint32_t flags = GC_STRING | (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0); \
39
45.9k
  if (file_cache_only \
40
45.9k
   || (ZCG(current_persistent_script) && ZCG(current_persistent_script)->corrupted)) { \
41
0
    GC_TYPE_INFO(str) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT); \
42
0
    flags |= (IS_STR_INTERNED << GC_FLAGS_SHIFT); \
43
45.9k
  } else { \
44
45.9k
    flags |= ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \
45
45.9k
  } \
46
45.9k
  GC_TYPE_INFO(str) = flags; \
47
45.9k
} while (0)
48
49
82.5k
#define zend_accel_store_string(str) do { \
50
82.5k
    zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \
51
82.5k
    if (new_str) { \
52
36.5k
      zend_string_release_ex(str, 0); \
53
36.5k
      str = new_str; \
54
45.9k
    } else { \
55
45.9k
      new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \
56
45.9k
      zend_string_release_ex(str, 0); \
57
45.9k
      str = new_str; \
58
45.9k
      zend_string_hash_val(str); \
59
45.9k
      zend_set_str_gc_flags(str); \
60
45.9k
    } \
61
82.5k
  } while (0)
62
#define zend_accel_memdup_string(str) do { \
63
    zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \
64
    if (new_str) { \
65
      str = new_str; \
66
    } else { \
67
      new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \
68
      str = new_str; \
69
      zend_string_hash_val(str); \
70
      zend_set_str_gc_flags(str); \
71
    } \
72
  } while (0)
73
608k
#define zend_accel_store_interned_string(str) do { \
74
608k
    if (!IS_ACCEL_INTERNED(str)) { \
75
21.5k
      zend_accel_store_string(str); \
76
21.5k
    } \
77
608k
  } while (0)
78
#define zend_accel_memdup_interned_string(str) do { \
79
    if (!IS_ACCEL_INTERNED(str)) { \
80
      zend_accel_memdup_string(str); \
81
    } \
82
  } while (0)
83
84
typedef void (*zend_persist_func_t)(zval*);
85
86
static void zend_persist_zval(zval *z);
87
static void zend_persist_op_array(zval *zv);
88
89
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
90
  {HT_INVALID_IDX, HT_INVALID_IDX};
91
92
static void zend_hash_persist(HashTable *ht)
93
107k
{
94
107k
  uint32_t idx, nIndex;
95
107k
  Bucket *p;
96
97
107k
  HT_FLAGS(ht) |= HASH_FLAG_STATIC_KEYS;
98
107k
  ht->pDestructor = NULL;
99
107k
  ht->nInternalPointer = 0;
100
101
107k
  if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
102
68.9k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
103
68.9k
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
104
68.9k
    } else {
105
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
106
0
    }
107
68.9k
    return;
108
68.9k
  }
109
38.8k
  if (ht->nNumUsed == 0) {
110
2
    efree(HT_GET_DATA_ADDR(ht));
111
2
    ht->nTableMask = HT_MIN_MASK;
112
2
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
113
2
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
114
2
    } else {
115
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
116
0
    }
117
2
    HT_FLAGS(ht) |= HASH_FLAG_UNINITIALIZED;
118
2
    return;
119
2
  }
120
38.8k
  if (HT_IS_PACKED(ht)) {
121
9.19k
    void *data = HT_GET_DATA_ADDR(ht);
122
9.19k
    if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
123
0
      data = zend_shared_memdup(data, HT_PACKED_USED_SIZE(ht));
124
9.19k
    } else {
125
9.19k
      data = zend_shared_memdup_free(data, HT_PACKED_USED_SIZE(ht));
126
9.19k
    }
127
9.19k
    HT_SET_DATA_ADDR(ht, data);
128
29.6k
  } else if (ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) {
129
    /* compact table */
130
4
    void *old_data = HT_GET_DATA_ADDR(ht);
131
4
    Bucket *old_buckets = ht->arData;
132
4
    uint32_t hash_size;
133
134
4
    hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
135
8
    while (hash_size >> 2 > ht->nNumUsed) {
136
4
      hash_size >>= 1;
137
4
    }
138
4
    ht->nTableMask = (uint32_t)(-(int32_t)hash_size);
139
4
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
140
4
    HT_SET_DATA_ADDR(ht, ZCG(mem));
141
4
    ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE((hash_size * sizeof(uint32_t)) + (ht->nNumUsed * sizeof(Bucket))));
142
4
    HT_HASH_RESET(ht);
143
4
    memcpy(ht->arData, old_buckets, ht->nNumUsed * sizeof(Bucket));
144
4
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
145
4
      efree(old_data);
146
4
    }
147
148
    /* rehash */
149
64
    for (idx = 0; idx < ht->nNumUsed; idx++) {
150
60
      p = ht->arData + idx;
151
60
      if (Z_TYPE(p->val) == IS_UNDEF) continue;
152
60
      nIndex = p->h | ht->nTableMask;
153
60
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
154
60
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
155
60
    }
156
29.6k
  } else {
157
29.6k
    void *data = ZCG(mem);
158
29.6k
    void *old_data = HT_GET_DATA_ADDR(ht);
159
160
29.6k
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
161
29.6k
    ZCG(mem) = (void*)((char*)data + ZEND_ALIGNED_SIZE(HT_USED_SIZE(ht)));
162
29.6k
    memcpy(data, old_data, HT_USED_SIZE(ht));
163
29.6k
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
164
29.6k
      efree(old_data);
165
29.6k
    }
166
29.6k
    HT_SET_DATA_ADDR(ht, data);
167
29.6k
  }
168
38.8k
}
169
170
static zend_ast *zend_persist_ast(zend_ast *ast)
171
9.36k
{
172
9.36k
  uint32_t i;
173
9.36k
  zend_ast *node;
174
175
9.36k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
176
5.73k
    zend_ast_zval *copy = zend_shared_memdup(ast, sizeof(zend_ast_zval));
177
5.73k
    zend_persist_zval(&copy->val);
178
5.73k
    node = (zend_ast *) copy;
179
5.73k
  } else if (zend_ast_is_list(ast)) {
180
584
    zend_ast_list *list = zend_ast_get_list(ast);
181
584
    zend_ast_list *copy = zend_shared_memdup(ast,
182
584
      sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
183
1.19k
    for (i = 0; i < list->children; i++) {
184
610
      if (copy->child[i]) {
185
610
        copy->child[i] = zend_persist_ast(copy->child[i]);
186
610
      }
187
610
    }
188
584
    node = (zend_ast *) copy;
189
3.05k
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
190
38
    zend_ast_op_array *copy = zend_shared_memdup(ast, sizeof(zend_ast_op_array));
191
38
    zval z;
192
38
    ZVAL_PTR(&z, copy->op_array);
193
38
    zend_persist_op_array(&z);
194
38
    copy->op_array = Z_PTR(z);
195
38
    node = (zend_ast *) copy;
196
3.01k
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
197
116
    zend_ast_fcc *copy = zend_shared_memdup(ast, sizeof(zend_ast_fcc));
198
116
    copy->args = zend_persist_ast(copy->args);
199
116
    node = (zend_ast *) copy;
200
2.90k
  } else if (zend_ast_is_decl(ast)) {
201
    /* Not implemented. */
202
0
    ZEND_UNREACHABLE();
203
2.90k
  } else {
204
2.90k
    uint32_t children = zend_ast_get_num_children(ast);
205
2.90k
    node = zend_shared_memdup(ast, zend_ast_size(children));
206
9.98k
    for (i = 0; i < children; i++) {
207
7.08k
      if (node->child[i]) {
208
6.39k
        node->child[i] = zend_persist_ast(node->child[i]);
209
6.39k
      }
210
7.08k
    }
211
2.90k
  }
212
213
9.36k
  return node;
214
9.36k
}
215
216
static void zend_persist_zval(zval *z)
217
478k
{
218
478k
  void *new_ptr;
219
220
478k
  switch (Z_TYPE_P(z)) {
221
297k
    case IS_STRING:
222
297k
      zend_accel_store_interned_string(Z_STR_P(z));
223
297k
      Z_TYPE_FLAGS_P(z) = 0;
224
297k
      break;
225
11.0k
    case IS_ARRAY:
226
11.0k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_ARR_P(z));
227
11.0k
      if (new_ptr) {
228
295
        Z_ARR_P(z) = new_ptr;
229
295
        Z_TYPE_FLAGS_P(z) = 0;
230
10.7k
      } else if (!ZCG(current_persistent_script)->corrupted
231
10.7k
       && zend_accel_in_shm(Z_ARR_P(z))) {
232
        /* pass */
233
10.6k
      } else {
234
10.6k
        HashTable *ht;
235
236
10.6k
        if (!Z_REFCOUNTED_P(z)) {
237
1.60k
          ht = zend_shared_memdup_put(Z_ARR_P(z), sizeof(zend_array));
238
9.05k
        } else {
239
9.05k
          GC_REMOVE_FROM_BUFFER(Z_ARR_P(z));
240
9.05k
          ht = zend_shared_memdup_put_free(Z_ARR_P(z), sizeof(zend_array));
241
9.05k
        }
242
10.6k
        Z_ARR_P(z) = ht;
243
10.6k
        zend_hash_persist(ht);
244
10.6k
        if (HT_IS_PACKED(ht)) {
245
7.74k
          zval *zv;
246
247
198k
          ZEND_HASH_PACKED_FOREACH_VAL(ht, zv) {
248
198k
            zend_persist_zval(zv);
249
198k
          } ZEND_HASH_FOREACH_END();
250
7.74k
        } else {
251
2.92k
          Bucket *p;
252
253
18.0k
          ZEND_HASH_MAP_FOREACH_BUCKET(ht, p) {
254
18.0k
            if (p->key) {
255
1.81k
              zend_accel_store_interned_string(p->key);
256
1.81k
            }
257
18.0k
            zend_persist_zval(&p->val);
258
18.0k
          } ZEND_HASH_FOREACH_END();
259
2.92k
        }
260
        /* make immutable array */
261
10.6k
        Z_TYPE_FLAGS_P(z) = 0;
262
10.6k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 2);
263
10.6k
        GC_ADD_FLAGS(Z_COUNTED_P(z), IS_ARRAY_IMMUTABLE);
264
10.6k
      }
265
11.0k
      break;
266
11.0k
    case IS_CONSTANT_AST:
267
2.33k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_AST_P(z));
268
2.33k
      if (new_ptr) {
269
18
        Z_AST_P(z) = new_ptr;
270
18
        Z_TYPE_FLAGS_P(z) = 0;
271
2.31k
      } else if (ZCG(current_persistent_script)->corrupted
272
2.31k
       || !zend_accel_in_shm(Z_AST_P(z))) {
273
2.25k
        zend_ast_ref *old_ref = Z_AST_P(z);
274
2.25k
        Z_AST_P(z) = zend_shared_memdup_put(Z_AST_P(z), sizeof(zend_ast_ref));
275
2.25k
        zend_persist_ast(GC_AST(old_ref));
276
2.25k
        Z_TYPE_FLAGS_P(z) = 0;
277
2.25k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 1);
278
2.25k
        GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE);
279
2.25k
        efree(old_ref);
280
2.25k
      }
281
2.33k
      break;
282
120
    case IS_PTR:
283
120
      break;
284
167k
    default:
285
167k
      ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING);
286
167k
      break;
287
478k
  }
288
478k
}
289
290
static HashTable *zend_persist_attributes(HashTable *attributes)
291
1.47k
{
292
1.47k
  uint32_t i;
293
1.47k
  zval *v;
294
295
1.47k
  if (!ZCG(current_persistent_script)->corrupted && zend_accel_in_shm(attributes)) {
296
22
    return attributes;
297
22
  }
298
299
  /* Attributes for trait properties may be shared if preloading is used. */
300
1.45k
  HashTable *xlat = zend_shared_alloc_get_xlat_entry(attributes);
301
1.45k
  if (xlat) {
302
0
    return xlat;
303
0
  }
304
305
1.45k
  zend_hash_persist(attributes);
306
307
7.03k
  ZEND_HASH_PACKED_FOREACH_VAL(attributes, v) {
308
7.03k
    zend_attribute *attr = Z_PTR_P(v);
309
7.03k
    zend_attribute *copy = zend_shared_memdup_put_free(attr, ZEND_ATTRIBUTE_SIZE(attr->argc));
310
311
7.03k
    zend_accel_store_interned_string(copy->name);
312
7.03k
    zend_accel_store_interned_string(copy->lcname);
313
7.03k
    if (copy->validation_error) {
314
46
      zend_accel_store_interned_string(copy->validation_error);
315
46
    }
316
317
7.03k
    for (i = 0; i < copy->argc; i++) {
318
674
      if (copy->args[i].name) {
319
86
        zend_accel_store_interned_string(copy->args[i].name);
320
86
      }
321
674
      zend_persist_zval(&copy->args[i].value);
322
674
    }
323
324
7.03k
    ZVAL_PTR(v, copy);
325
7.03k
  } ZEND_HASH_FOREACH_END();
326
327
1.45k
  HashTable *ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable));
328
1.45k
  GC_SET_REFCOUNT(ptr, 2);
329
1.45k
  GC_TYPE_INFO(ptr) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
330
331
1.45k
  return ptr;
332
1.45k
}
333
334
uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name)
335
15.9k
{
336
15.9k
  uint32_t ret;
337
338
15.9k
  if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF)) ||
339
15.8k
      zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
340
78
    return 0;
341
78
  }
342
343
  /* We use type.name.gc.refcount to keep map_ptr of corresponding type */
344
15.8k
  if (ZSTR_HAS_CE_CACHE(type_name)) {
345
14.9k
    return GC_REFCOUNT(type_name);
346
14.9k
  }
347
348
870
  if ((GC_FLAGS(type_name) & GC_IMMUTABLE)
349
870
   && (GC_FLAGS(type_name) & IS_STR_PERMANENT)) {
350
870
    do {
351
870
      ret = ZEND_MAP_PTR_NEW_OFFSET();
352
870
    } while (ret <= 2);
353
870
    GC_SET_REFCOUNT(type_name, ret);
354
870
    GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
355
870
    return ret;
356
870
  }
357
358
0
  return 0;
359
870
}
360
361
27.8k
static void zend_persist_type(zend_type *type) {
362
27.8k
  if (ZEND_TYPE_HAS_LIST(*type)) {
363
950
    zend_type_list *list = ZEND_TYPE_LIST(*type);
364
950
    if (ZEND_TYPE_USES_ARENA(*type) || zend_accel_in_shm(list)) {
365
950
      list = zend_shared_memdup_put(list, ZEND_TYPE_LIST_SIZE(list->num_types));
366
950
      ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
367
950
    } else {
368
0
      list = zend_shared_memdup_put_free(list, ZEND_TYPE_LIST_SIZE(list->num_types));
369
0
    }
370
950
    ZEND_TYPE_SET_PTR(*type, list);
371
950
  }
372
373
27.8k
  zend_type *single_type;
374
56.6k
  ZEND_TYPE_FOREACH_MUTABLE(*type, single_type) {
375
56.6k
    if (ZEND_TYPE_HAS_LIST(*single_type)) {
376
242
      zend_persist_type(single_type);
377
242
      continue;
378
242
    }
379
28.5k
    if (ZEND_TYPE_HAS_NAME(*single_type)) {
380
3.52k
      zend_string *type_name = ZEND_TYPE_NAME(*single_type);
381
3.52k
      zend_accel_store_interned_string(type_name);
382
3.52k
      ZEND_TYPE_SET_PTR(*single_type, type_name);
383
3.52k
      if (!ZCG(current_persistent_script)->corrupted) {
384
3.52k
        zend_accel_get_class_name_map_ptr(type_name);
385
3.52k
      }
386
3.52k
    }
387
28.5k
  } ZEND_TYPE_FOREACH_END();
388
27.8k
}
389
390
static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_script* main_persistent_script)
391
50.5k
{
392
50.5k
  zend_op *persist_ptr;
393
50.5k
  zval *orig_literals = NULL;
394
395
50.5k
  if (op_array->refcount && --(*op_array->refcount) == 0) {
396
47.4k
    efree(op_array->refcount);
397
47.4k
  }
398
50.5k
  op_array->refcount = NULL;
399
400
50.5k
  if (main_persistent_script) {
401
25.9k
    zend_execute_data *orig_execute_data = EG(current_execute_data);
402
25.9k
    zend_execute_data fake_execute_data;
403
25.9k
    zval *offset;
404
405
25.9k
    memset(&fake_execute_data, 0, sizeof(fake_execute_data));
406
25.9k
    fake_execute_data.func = (zend_function*)op_array;
407
25.9k
    EG(current_execute_data) = &fake_execute_data;
408
25.9k
    if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) {
409
42
      main_persistent_script->compiler_halt_offset = Z_LVAL_P(offset);
410
42
    }
411
25.9k
    EG(current_execute_data) = orig_execute_data;
412
25.9k
  }
413
414
50.5k
  if (op_array->function_name) {
415
24.6k
    zend_string *old_name = op_array->function_name;
416
24.6k
    zend_accel_store_interned_string(op_array->function_name);
417
    /* Remember old function name, so it can be released multiple times if shared. */
418
24.6k
    if (op_array->function_name != old_name
419
280
        && !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) {
420
280
      zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name);
421
280
    }
422
24.6k
  }
423
424
50.5k
  if (op_array->scope) {
425
13.8k
    zend_class_entry *scope = zend_shared_alloc_get_xlat_entry(op_array->scope);
426
427
13.8k
    if (scope) {
428
13.8k
      op_array->scope = scope;
429
13.8k
    }
430
431
13.8k
    if (op_array->prototype) {
432
1.85k
      zend_function *ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype);
433
434
1.85k
      if (ptr) {
435
430
        op_array->prototype = ptr;
436
430
      }
437
1.85k
    }
438
439
13.8k
    persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->opcodes);
440
13.8k
    if (persist_ptr) {
441
0
      op_array->opcodes = persist_ptr;
442
0
      if (op_array->static_variables) {
443
0
        op_array->static_variables = zend_shared_alloc_get_xlat_entry(op_array->static_variables);
444
0
        ZEND_ASSERT(op_array->static_variables != NULL);
445
0
      }
446
0
      if (op_array->literals) {
447
0
        op_array->literals = zend_shared_alloc_get_xlat_entry(op_array->literals);
448
0
        ZEND_ASSERT(op_array->literals != NULL);
449
0
      }
450
0
      if (op_array->filename) {
451
0
        op_array->filename = zend_shared_alloc_get_xlat_entry(op_array->filename);
452
0
        ZEND_ASSERT(op_array->filename != NULL);
453
0
      }
454
0
      if (op_array->arg_info) {
455
0
        zend_arg_info *arg_info = op_array->arg_info;
456
0
        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
457
0
          arg_info--;
458
0
        }
459
0
        arg_info = zend_shared_alloc_get_xlat_entry(arg_info);
460
0
        ZEND_ASSERT(arg_info != NULL);
461
0
        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
462
0
          arg_info++;
463
0
        }
464
0
        op_array->arg_info = arg_info;
465
0
      }
466
0
      if (op_array->live_range) {
467
0
        op_array->live_range = zend_shared_alloc_get_xlat_entry(op_array->live_range);
468
0
        ZEND_ASSERT(op_array->live_range != NULL);
469
0
      }
470
0
      if (op_array->doc_comment) {
471
0
        if (ZCG(accel_directives).save_comments) {
472
0
          op_array->doc_comment = zend_shared_alloc_get_xlat_entry(op_array->doc_comment);
473
0
          ZEND_ASSERT(op_array->doc_comment != NULL);
474
0
        } else {
475
0
          op_array->doc_comment = NULL;
476
0
        }
477
0
      }
478
0
      if (op_array->attributes) {
479
0
        op_array->attributes = zend_shared_alloc_get_xlat_entry(op_array->attributes);
480
0
        ZEND_ASSERT(op_array->attributes != NULL);
481
0
      }
482
483
0
      if (op_array->try_catch_array) {
484
0
        op_array->try_catch_array = zend_shared_alloc_get_xlat_entry(op_array->try_catch_array);
485
0
        ZEND_ASSERT(op_array->try_catch_array != NULL);
486
0
      }
487
0
      if (op_array->vars) {
488
0
        op_array->vars = zend_shared_alloc_get_xlat_entry(op_array->vars);
489
0
        ZEND_ASSERT(op_array->vars != NULL);
490
0
      }
491
0
      if (op_array->dynamic_func_defs) {
492
0
        op_array->dynamic_func_defs = zend_shared_alloc_get_xlat_entry(op_array->dynamic_func_defs);
493
0
        ZEND_ASSERT(op_array->dynamic_func_defs != NULL);
494
0
      }
495
0
      ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem))));
496
0
      return;
497
0
    }
498
36.7k
  } else {
499
    /* "prototype" may be undefined if "scope" isn't set */
500
36.7k
    op_array->prototype = NULL;
501
36.7k
  }
502
503
50.5k
  if (op_array->scope
504
13.8k
   && !(op_array->fn_flags & ZEND_ACC_CLOSURE)
505
13.8k
   && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) {
506
2.39k
    return;
507
2.39k
  }
508
509
48.1k
  if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) {
510
947
    Bucket *p;
511
512
947
    zend_hash_persist(op_array->static_variables);
513
6.32k
    ZEND_HASH_MAP_FOREACH_BUCKET(op_array->static_variables, p) {
514
6.32k
      ZEND_ASSERT(p->key != NULL);
515
6.32k
      zend_accel_store_interned_string(p->key);
516
2.21k
      zend_persist_zval(&p->val);
517
2.21k
    } ZEND_HASH_FOREACH_END();
518
947
    op_array->static_variables = zend_shared_memdup_put_free(op_array->static_variables, sizeof(HashTable));
519
    /* make immutable array */
520
947
    GC_SET_REFCOUNT(op_array->static_variables, 2);
521
947
    GC_TYPE_INFO(op_array->static_variables) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
522
947
  }
523
524
48.1k
  if (op_array->literals) {
525
47.2k
    zval *p, *end;
526
527
47.2k
    orig_literals = op_array->literals;
528
#if ZEND_USE_ABS_CONST_ADDR
529
    p = zend_shared_memdup_put_free(op_array->literals, sizeof(zval) * op_array->last_literal);
530
#else
531
47.2k
    p = zend_shared_memdup_put(op_array->literals, sizeof(zval) * op_array->last_literal);
532
47.2k
#endif
533
47.2k
    end = p + op_array->last_literal;
534
47.2k
    op_array->literals = p;
535
409k
    while (p < end) {
536
361k
      zend_persist_zval(p);
537
361k
      p++;
538
361k
    }
539
47.2k
  }
540
541
48.1k
  {
542
48.1k
    zend_op *new_opcodes = zend_shared_memdup_put(op_array->opcodes, sizeof(zend_op) * op_array->last);
543
48.1k
    zend_op *opline = new_opcodes;
544
48.1k
    zend_op *end = new_opcodes + op_array->last;
545
546
1.13M
    for (; opline < end ; opline++) {
547
#if ZEND_USE_ABS_CONST_ADDR
548
      if (opline->op1_type == IS_CONST) {
549
        opline->op1.zv = (zval*)((char*)opline->op1.zv + ((char*)op_array->literals - (char*)orig_literals));
550
        if (opline->opcode == ZEND_SEND_VAL
551
         || opline->opcode == ZEND_SEND_VAL_EX
552
         || opline->opcode == ZEND_QM_ASSIGN) {
553
          /* Update handlers to eliminate REFCOUNTED check */
554
          zend_vm_set_opcode_handler_ex(opline, 1 << Z_TYPE_P(opline->op1.zv), 0, 0);
555
        }
556
      }
557
      if (opline->op2_type == IS_CONST) {
558
        opline->op2.zv = (zval*)((char*)opline->op2.zv + ((char*)op_array->literals - (char*)orig_literals));
559
      }
560
#else
561
1.08M
      if (opline->op1_type == IS_CONST) {
562
193k
        opline->op1.constant =
563
193k
          (char*)(op_array->literals +
564
193k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
565
193k
            (int32_t)opline->op1.constant) - orig_literals)) -
566
193k
          (char*)opline;
567
193k
        if (opline->opcode == ZEND_SEND_VAL
568
168k
         || opline->opcode == ZEND_SEND_VAL_EX
569
164k
         || opline->opcode == ZEND_QM_ASSIGN) {
570
32.7k
          zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
571
32.7k
        }
572
193k
      }
573
1.08M
      if (opline->op2_type == IS_CONST) {
574
284k
        opline->op2.constant =
575
284k
          (char*)(op_array->literals +
576
284k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
577
284k
            (int32_t)opline->op2.constant) - orig_literals)) -
578
284k
          (char*)opline;
579
284k
      }
580
1.08M
#endif
581
#if ZEND_USE_ABS_JMP_ADDR
582
      if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
583
        /* fix jumps to point to new array */
584
        switch (opline->opcode) {
585
          case ZEND_JMP:
586
          case ZEND_FAST_CALL:
587
            opline->op1.jmp_addr = &new_opcodes[opline->op1.jmp_addr - op_array->opcodes];
588
            break;
589
          case ZEND_JMPZ:
590
          case ZEND_JMPNZ:
591
          case ZEND_JMPZ_EX:
592
          case ZEND_JMPNZ_EX:
593
          case ZEND_JMP_SET:
594
          case ZEND_COALESCE:
595
          case ZEND_FE_RESET_R:
596
          case ZEND_FE_RESET_RW:
597
          case ZEND_ASSERT_CHECK:
598
          case ZEND_JMP_NULL:
599
          case ZEND_BIND_INIT_STATIC_OR_JMP:
600
          case ZEND_JMP_FRAMELESS:
601
            opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes];
602
            break;
603
          case ZEND_CATCH:
604
            if (!(opline->extended_value & ZEND_LAST_CATCH)) {
605
              opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes];
606
            }
607
            break;
608
          case ZEND_FE_FETCH_R:
609
          case ZEND_FE_FETCH_RW:
610
          case ZEND_SWITCH_LONG:
611
          case ZEND_SWITCH_STRING:
612
          case ZEND_MATCH:
613
            /* relative extended_value don't have to be changed */
614
            break;
615
        }
616
      }
617
#endif
618
1.08M
      if (opline->opcode == ZEND_OP_DATA && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) {
619
120
        zval *literal = RT_CONSTANT(opline, opline->op1);
620
120
        HashTable *attributes = Z_PTR_P(literal);
621
120
        attributes = zend_persist_attributes(attributes);
622
120
        ZVAL_PTR(literal, attributes);
623
120
      }
624
1.08M
    }
625
626
48.1k
    efree(op_array->opcodes);
627
48.1k
    op_array->opcodes = new_opcodes;
628
48.1k
  }
629
630
48.1k
  if (op_array->filename) {
631
48.1k
    zend_accel_store_string(op_array->filename);
632
48.1k
  }
633
634
48.1k
  if (op_array->arg_info) {
635
11.6k
    zend_arg_info *arg_info = op_array->arg_info;
636
11.6k
    uint32_t num_args = op_array->num_args;
637
11.6k
    uint32_t i;
638
639
11.6k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
640
4.00k
      arg_info--;
641
4.00k
      num_args++;
642
4.00k
    }
643
11.6k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
644
212
      num_args++;
645
212
    }
646
11.6k
    arg_info = zend_shared_memdup_put_free(arg_info, sizeof(zend_arg_info) * num_args);
647
28.9k
    for (i = 0; i < num_args; i++) {
648
17.3k
      if (arg_info[i].name) {
649
13.3k
        zend_accel_store_interned_string(arg_info[i].name);
650
13.3k
      }
651
17.3k
      zend_persist_type(&arg_info[i].type);
652
17.3k
      if (arg_info[i].doc_comment) {
653
0
        zend_accel_store_interned_string(arg_info[i].doc_comment);
654
0
      }
655
17.3k
    }
656
11.6k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
657
4.00k
      arg_info++;
658
4.00k
    }
659
11.6k
    op_array->arg_info = arg_info;
660
11.6k
  }
661
662
48.1k
  if (op_array->live_range) {
663
22.4k
    op_array->live_range = zend_shared_memdup_put_free(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
664
22.4k
  }
665
666
48.1k
  if (op_array->doc_comment) {
667
32
    if (ZCG(accel_directives).save_comments) {
668
32
      zend_accel_store_interned_string(op_array->doc_comment);
669
32
    } else {
670
0
      zend_string_release_ex(op_array->doc_comment, 0);
671
0
      op_array->doc_comment = NULL;
672
0
    }
673
32
  }
674
675
48.1k
  if (op_array->attributes) {
676
519
    op_array->attributes = zend_persist_attributes(op_array->attributes);
677
519
  }
678
679
48.1k
  if (op_array->try_catch_array) {
680
9.06k
    op_array->try_catch_array = zend_shared_memdup_put_free(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch);
681
9.06k
  }
682
683
48.1k
  if (op_array->vars) {
684
31.4k
    int i;
685
31.4k
    op_array->vars = zend_shared_memdup_put_free(op_array->vars, sizeof(zend_string*) * op_array->last_var);
686
187k
    for (i = 0; i < op_array->last_var; i++) {
687
155k
      zend_accel_store_interned_string(op_array->vars[i]);
688
155k
    }
689
31.4k
  }
690
691
48.1k
  if (op_array->num_dynamic_func_defs) {
692
2.51k
    op_array->dynamic_func_defs = zend_shared_memdup_put_free(
693
2.51k
      op_array->dynamic_func_defs, sizeof(zend_function *) * op_array->num_dynamic_func_defs);
694
6.19k
    for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
695
3.68k
      zval tmp;
696
3.68k
      ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]);
697
3.68k
      zend_persist_op_array(&tmp);
698
3.68k
      op_array->dynamic_func_defs[i] = Z_PTR(tmp);
699
3.68k
    }
700
2.51k
  }
701
702
48.1k
  ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem))));
703
48.1k
}
704
705
static void zend_persist_op_array(zval *zv)
706
10.7k
{
707
10.7k
  zend_op_array *op_array = Z_PTR_P(zv);
708
10.7k
  zend_op_array *old_op_array;
709
10.7k
  ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
710
711
10.7k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
712
10.7k
  if (!old_op_array) {
713
10.7k
    op_array = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_op_array));
714
10.7k
    zend_persist_op_array_ex(op_array, NULL);
715
10.7k
    if (!ZCG(current_persistent_script)->corrupted) {
716
10.7k
      op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
717
10.7k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
718
10.7k
      if (op_array->static_variables) {
719
839
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
720
839
      }
721
10.7k
    }
722
10.7k
#ifdef HAVE_JIT
723
10.7k
    if (JIT_G(on)
724
0
     && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS
725
0
     && (!ZCG(current_persistent_script)
726
0
      || !ZCG(current_persistent_script)->corrupted)) {
727
0
      zend_jit_op_array(op_array, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL);
728
0
    }
729
10.7k
#endif
730
10.7k
  } else {
731
    /* This can happen during preloading, if a dynamic function definition is declared. */
732
0
    Z_PTR_P(zv) = old_op_array;
733
0
  }
734
10.7k
}
735
736
static zend_op_array *zend_persist_class_method(zend_op_array *op_array, const zend_class_entry *ce)
737
17.1k
{
738
17.1k
  zend_op_array *old_op_array;
739
740
17.1k
  if (op_array->type != ZEND_USER_FUNCTION) {
741
2.07k
    ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION);
742
2.07k
    if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) {
743
2.07k
      old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
744
2.07k
      if (old_op_array) {
745
0
        return old_op_array;
746
2.07k
      } else {
747
2.07k
        op_array = zend_shared_memdup_put(op_array, sizeof(zend_internal_function));
748
2.07k
        if (op_array->scope) {
749
2.07k
          void *persist_ptr;
750
751
2.07k
          if ((persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->scope))) {
752
0
            op_array->scope = (zend_class_entry*)persist_ptr;
753
0
          }
754
2.07k
          if (op_array->prototype) {
755
916
            if ((persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype))) {
756
0
              op_array->prototype = (zend_function*)persist_ptr;
757
0
            }
758
916
          }
759
2.07k
        }
760
        // Real dynamically created internal functions like enum methods must have their own run_time_cache pointer. They're always on the same scope as their defining class.
761
        // However, copies - as caused by inheritance of internal methods - must retain the original run_time_cache pointer, shared with the source function.
762
2.07k
        if (!op_array->scope || (op_array->scope == ce && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE))) {
763
0
          if (op_array->fn_flags & ZEND_ACC_PRELOADED) {
764
0
            ZEND_MAP_PTR_NEW_STATIC(op_array->run_time_cache);
765
0
          } else {
766
0
            ZEND_MAP_PTR_NEW(op_array->run_time_cache);
767
0
          }
768
0
        }
769
2.07k
      }
770
2.07k
    }
771
2.07k
    return op_array;
772
2.07k
  }
773
774
15.0k
  if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
775
264
   && !ZCG(current_persistent_script)->corrupted
776
264
   && zend_accel_in_shm(op_array)) {
777
260
    zend_shared_alloc_register_xlat_entry(op_array, op_array);
778
260
    return op_array;
779
260
  }
780
781
14.7k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
782
14.7k
  if (old_op_array) {
783
907
    if (op_array->refcount && --(*op_array->refcount) == 0) {
784
765
      efree(op_array->refcount);
785
765
    }
786
787
    /* If op_array is shared, the function name refcount is still incremented for each use,
788
     * so we need to release it here. We remembered the original function name in xlat. */
789
907
    zend_string *old_function_name =
790
907
      zend_shared_alloc_get_xlat_entry(&old_op_array->function_name);
791
907
    if (old_function_name) {
792
0
      zend_string_release_ex(old_function_name, 0);
793
0
    }
794
907
    return old_op_array;
795
907
  }
796
797
13.8k
  op_array = zend_shared_memdup_put(op_array, sizeof(zend_op_array));
798
13.8k
  zend_persist_op_array_ex(op_array, NULL);
799
13.8k
  if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
800
13.8k
    op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
801
13.8k
    if (ce->ce_flags & ZEND_ACC_LINKED) {
802
11.6k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
803
11.6k
      if (op_array->static_variables) {
804
106
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
805
106
      }
806
11.6k
    } else {
807
2.26k
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
808
2.26k
      ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
809
2.26k
    }
810
13.8k
  }
811
13.8k
  return op_array;
812
14.7k
}
813
814
static zend_property_info *zend_persist_property_info(zend_property_info *prop)
815
8.03k
{
816
8.03k
  zend_class_entry *ce;
817
8.03k
  prop = zend_shared_memdup_put(prop, sizeof(zend_property_info));
818
8.03k
  ce = zend_shared_alloc_get_xlat_entry(prop->ce);
819
8.03k
  if (ce) {
820
8.03k
    prop->ce = ce;
821
8.03k
  }
822
8.03k
  zend_accel_store_interned_string(prop->name);
823
8.03k
  if (prop->doc_comment) {
824
144
    if (ZCG(accel_directives).save_comments) {
825
144
      zend_accel_store_interned_string(prop->doc_comment);
826
144
    } else {
827
0
      if (!zend_shared_alloc_get_xlat_entry(prop->doc_comment)) {
828
0
        zend_shared_alloc_register_xlat_entry(prop->doc_comment, prop->doc_comment);
829
0
      }
830
0
      zend_string_release_ex(prop->doc_comment, 0);
831
0
      prop->doc_comment = NULL;
832
0
    }
833
144
  }
834
8.03k
  if (prop->attributes) {
835
102
    prop->attributes = zend_persist_attributes(prop->attributes);
836
102
  }
837
8.03k
  if (prop->prototype) {
838
8.03k
    const zend_property_info *new_prototype = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(prop->prototype);
839
8.03k
    if (new_prototype) {
840
7.98k
      prop->prototype = new_prototype;
841
7.98k
    }
842
8.03k
  }
843
8.03k
  if (prop->hooks) {
844
883
    prop->hooks = zend_shared_memdup_put(prop->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
845
2.64k
    for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
846
1.76k
      if (prop->hooks[i]) {
847
1.17k
        zend_op_array *hook = zend_persist_class_method(&prop->hooks[i]->op_array, ce);
848
1.17k
#ifdef HAVE_JIT
849
1.17k
        if (JIT_G(on)
850
0
         && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS
851
0
         && (!ZCG(current_persistent_script)
852
0
          || !ZCG(current_persistent_script)->corrupted)) {
853
0
          if (hook->scope == ce && !(hook->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
854
0
            zend_jit_op_array(hook, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL);
855
0
          }
856
0
        }
857
1.17k
#endif
858
1.17k
        const zend_property_info *new_prop_info = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(hook->prop_info);
859
1.17k
        if (new_prop_info) {
860
1.15k
          hook->prop_info = new_prop_info;
861
1.15k
        }
862
1.17k
        prop->hooks[i] = (zend_function *) hook;
863
1.17k
      }
864
1.76k
    }
865
883
  }
866
8.03k
  zend_persist_type(&prop->type);
867
8.03k
  return prop;
868
8.03k
}
869
870
static void zend_persist_class_constant(zval *zv)
871
2.92k
{
872
2.92k
  const zend_class_constant *orig_c = Z_PTR_P(zv);
873
2.92k
  zend_class_constant *c = zend_shared_alloc_get_xlat_entry(orig_c);
874
2.92k
  zend_class_entry *ce;
875
876
2.92k
  if (c) {
877
80
    Z_PTR_P(zv) = c;
878
80
    return;
879
2.84k
  } else if (((orig_c->ce->ce_flags & ZEND_ACC_IMMUTABLE) && !(Z_CONSTANT_FLAGS(orig_c->value) & CONST_OWNED))
880
2.78k
   || orig_c->ce->type == ZEND_INTERNAL_CLASS) {
881
    /* Class constant comes from a different file in shm or internal class, keep existing pointer. */
882
556
    return;
883
2.28k
  } else if (!ZCG(current_persistent_script)->corrupted
884
2.28k
   && zend_accel_in_shm(Z_PTR_P(zv))) {
885
0
    return;
886
0
  }
887
2.28k
  c = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_class_constant));
888
2.28k
  zend_persist_zval(&c->value);
889
2.28k
  ce = zend_shared_alloc_get_xlat_entry(c->ce);
890
2.28k
  if (ce) {
891
2.28k
    c->ce = ce;
892
2.28k
  }
893
2.28k
  if (c->doc_comment) {
894
18
    if (ZCG(accel_directives).save_comments) {
895
18
      zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment);
896
18
      if (doc_comment) {
897
0
        c->doc_comment = doc_comment;
898
18
      } else {
899
18
        zend_accel_store_interned_string(c->doc_comment);
900
18
      }
901
18
    } else {
902
0
      zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment);
903
0
      if (!doc_comment) {
904
0
        zend_shared_alloc_register_xlat_entry(c->doc_comment, c->doc_comment);
905
0
        zend_string_release_ex(c->doc_comment, 0);
906
0
      }
907
0
      c->doc_comment = NULL;
908
0
    }
909
18
  }
910
2.28k
  if (c->attributes) {
911
94
    c->attributes = zend_persist_attributes(c->attributes);
912
94
  }
913
2.28k
  zend_persist_type(&c->type);
914
2.28k
}
915
916
zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce)
917
14.3k
{
918
14.3k
  Bucket *p;
919
14.3k
  zend_class_entry *ce = orig_ce;
920
921
14.3k
  if (ce->type == ZEND_USER_CLASS) {
922
    /* The same zend_class_entry may be reused by class_alias */
923
14.3k
    zend_class_entry *new_ce = zend_shared_alloc_get_xlat_entry(ce);
924
14.3k
    if (new_ce) {
925
0
      return new_ce;
926
0
    }
927
14.3k
    ce = zend_shared_memdup_put(ce, sizeof(zend_class_entry));
928
14.3k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
929
14.3k
      ce->ce_flags |= ZEND_ACC_IMMUTABLE;
930
14.3k
      if ((ce->ce_flags & ZEND_ACC_LINKED)
931
11.5k
       && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
932
362
        ZEND_MAP_PTR_NEW(ce->mutable_data);
933
13.9k
      } else {
934
13.9k
        ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
935
13.9k
      }
936
14.3k
    } else {
937
0
      ce->ce_flags |= ZEND_ACC_FILE_CACHED;
938
0
    }
939
14.3k
    ce->inheritance_cache = NULL;
940
941
14.3k
    if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
942
12.7k
      if (ZSTR_HAS_CE_CACHE(ce->name)) {
943
11.6k
        ZSTR_SET_CE_CACHE_EX(ce->name, NULL, 0);
944
11.6k
      }
945
12.7k
      zend_accel_store_interned_string(ce->name);
946
12.7k
      if (!(ce->ce_flags & ZEND_ACC_ANON_CLASS)
947
12.4k
       && !ZCG(current_persistent_script)->corrupted) {
948
12.4k
        zend_accel_get_class_name_map_ptr(ce->name);
949
12.4k
      }
950
12.7k
      if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) {
951
661
        zend_accel_store_interned_string(ce->parent_name);
952
661
      }
953
12.7k
    }
954
955
14.3k
    zend_hash_persist(&ce->function_table);
956
60.5k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->function_table, p) {
957
60.5k
      ZEND_ASSERT(p->key != NULL);
958
60.5k
      zend_accel_store_interned_string(p->key);
959
15.9k
      Z_PTR(p->val) = zend_persist_class_method(Z_PTR(p->val), ce);
960
15.9k
    } ZEND_HASH_FOREACH_END();
961
14.3k
    HT_FLAGS(&ce->function_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
962
14.3k
    if (ce->default_properties_table) {
963
4.69k
        int i;
964
965
4.69k
      ce->default_properties_table = zend_shared_memdup_free(ce->default_properties_table, sizeof(zval) * ce->default_properties_count);
966
12.1k
      for (i = 0; i < ce->default_properties_count; i++) {
967
7.43k
        zend_persist_zval(&ce->default_properties_table[i]);
968
7.43k
      }
969
4.69k
    }
970
14.3k
    if (ce->default_static_members_table) {
971
832
      ce->default_static_members_table = zend_shared_memdup_free(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count);
972
973
      /* Persist only static properties in this class.
974
       * Static properties from parent classes will be handled in class_copy_ctor and are marked with IS_INDIRECT */
975
2.35k
      for (uint32_t i = 0; i < ce->default_static_members_count; i++) {
976
1.52k
        if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) {
977
1.33k
          zend_persist_zval(&ce->default_static_members_table[i]);
978
1.33k
        }
979
1.52k
      }
980
832
      if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
981
832
        if (ce->ce_flags & ZEND_ACC_LINKED) {
982
806
          ZEND_MAP_PTR_NEW(ce->static_members_table);
983
806
        } else {
984
26
          ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
985
26
        }
986
832
      }
987
832
    }
988
989
14.3k
    zend_hash_persist(&ce->constants_table);
990
34.4k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->constants_table, p) {
991
34.4k
      ZEND_ASSERT(p->key != NULL);
992
34.4k
      zend_accel_store_interned_string(p->key);
993
2.92k
      zend_persist_class_constant(&p->val);
994
2.92k
    } ZEND_HASH_FOREACH_END();
995
14.3k
    HT_FLAGS(&ce->constants_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
996
997
14.3k
    zend_hash_persist(&ce->properties_info);
998
47.1k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->properties_info, p) {
999
47.1k
      zend_property_info *prop = Z_PTR(p->val);
1000
47.1k
      ZEND_ASSERT(p->key != NULL);
1001
47.1k
      zend_accel_store_interned_string(p->key);
1002
9.25k
      if (prop->ce == orig_ce) {
1003
8.03k
        Z_PTR(p->val) = zend_persist_property_info(prop);
1004
8.03k
      } else {
1005
1.22k
        prop = zend_shared_alloc_get_xlat_entry(prop);
1006
1.22k
        if (prop) {
1007
600
          Z_PTR(p->val) = prop;
1008
624
        } else {
1009
          /* This can happen if preloading is used and we inherit a property from an
1010
           * internal class. In that case we should keep pointing to the internal
1011
           * property, without any adjustments. */
1012
624
        }
1013
1.22k
      }
1014
9.25k
    } ZEND_HASH_FOREACH_END();
1015
14.3k
    HT_FLAGS(&ce->properties_info) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
1016
1017
14.3k
    if (ce->properties_info_table) {
1018
3.89k
      int i;
1019
1020
3.89k
      size_t size = sizeof(zend_property_info *) * ce->default_properties_count;
1021
3.89k
      ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
1022
3.89k
      ce->properties_info_table = zend_shared_memdup(
1023
3.89k
        ce->properties_info_table, size);
1024
1025
10.2k
      for (i = 0; i < ce->default_properties_count; i++) {
1026
6.38k
        if (ce->properties_info_table[i]) {
1027
6.19k
          zend_property_info *prop_info = zend_shared_alloc_get_xlat_entry(
1028
6.19k
            ce->properties_info_table[i]);
1029
6.19k
          if (prop_info) {
1030
5.61k
            ce->properties_info_table[i] = prop_info;
1031
5.61k
          }
1032
6.19k
        }
1033
6.38k
      }
1034
3.89k
    }
1035
1036
14.3k
    if (ce->iterator_funcs_ptr) {
1037
135
      ce->iterator_funcs_ptr = zend_shared_memdup(ce->iterator_funcs_ptr, sizeof(zend_class_iterator_funcs));
1038
135
    }
1039
14.3k
    if (ce->arrayaccess_funcs_ptr) {
1040
154
      ce->arrayaccess_funcs_ptr = zend_shared_memdup(ce->arrayaccess_funcs_ptr, sizeof(zend_class_arrayaccess_funcs));
1041
154
    }
1042
1043
14.3k
    if (ce->ce_flags & ZEND_ACC_CACHED) {
1044
1.56k
      return ce;
1045
1.56k
    }
1046
1047
12.7k
    ce->ce_flags |= ZEND_ACC_CACHED;
1048
1049
12.7k
    if (ce->info.user.filename) {
1050
12.7k
      zend_accel_store_string(ce->info.user.filename);
1051
12.7k
    }
1052
1053
12.7k
    if (ce->doc_comment) {
1054
16
      if (ZCG(accel_directives).save_comments) {
1055
16
        zend_accel_store_interned_string(ce->doc_comment);
1056
16
      } else {
1057
0
        if (!zend_shared_alloc_get_xlat_entry(ce->doc_comment)) {
1058
0
          zend_shared_alloc_register_xlat_entry(ce->doc_comment, ce->doc_comment);
1059
0
          zend_string_release_ex(ce->doc_comment, 0);
1060
0
        }
1061
0
        ce->doc_comment = NULL;
1062
0
      }
1063
16
    }
1064
1065
12.7k
    if (ce->attributes) {
1066
639
      ce->attributes = zend_persist_attributes(ce->attributes);
1067
639
    }
1068
1069
12.7k
    if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_LINKED)) {
1070
1.60k
      uint32_t i = 0;
1071
1072
3.56k
      for (i = 0; i < ce->num_interfaces; i++) {
1073
1.96k
        zend_accel_store_interned_string(ce->interface_names[i].name);
1074
1.96k
        zend_accel_store_interned_string(ce->interface_names[i].lc_name);
1075
1.96k
      }
1076
1.60k
      ce->interface_names = zend_shared_memdup_free(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
1077
1.60k
    }
1078
1079
12.7k
    if (ce->num_traits) {
1080
742
      uint32_t i = 0;
1081
1082
1.67k
      for (i = 0; i < ce->num_traits; i++) {
1083
928
        zend_accel_store_interned_string(ce->trait_names[i].name);
1084
928
        zend_accel_store_interned_string(ce->trait_names[i].lc_name);
1085
928
      }
1086
742
      ce->trait_names = zend_shared_memdup_free(ce->trait_names, sizeof(zend_class_name) * ce->num_traits);
1087
1088
742
      i = 0;
1089
742
      if (ce->trait_aliases) {
1090
308
        while (ce->trait_aliases[i]) {
1091
202
          if (ce->trait_aliases[i]->trait_method.method_name) {
1092
202
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.method_name);
1093
202
          }
1094
202
          if (ce->trait_aliases[i]->trait_method.class_name) {
1095
88
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.class_name);
1096
88
          }
1097
1098
202
          if (ce->trait_aliases[i]->alias) {
1099
142
            zend_accel_store_interned_string(ce->trait_aliases[i]->alias);
1100
142
          }
1101
1102
202
          ce->trait_aliases[i] = zend_shared_memdup_free(ce->trait_aliases[i], sizeof(zend_trait_alias));
1103
202
          i++;
1104
202
        }
1105
1106
106
        ce->trait_aliases = zend_shared_memdup_free(ce->trait_aliases, sizeof(zend_trait_alias*) * (i + 1));
1107
106
      }
1108
1109
742
      if (ce->trait_precedences) {
1110
46
        uint32_t j;
1111
1112
46
        i = 0;
1113
104
        while (ce->trait_precedences[i]) {
1114
58
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.method_name);
1115
58
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.class_name);
1116
1117
120
          for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
1118
62
            zend_accel_store_interned_string(ce->trait_precedences[i]->exclude_class_names[j]);
1119
62
          }
1120
1121
58
          ce->trait_precedences[i] = zend_shared_memdup_free(ce->trait_precedences[i], sizeof(zend_trait_precedence) + (ce->trait_precedences[i]->num_excludes - 1) * sizeof(zend_string*));
1122
58
          i++;
1123
58
        }
1124
46
        ce->trait_precedences = zend_shared_memdup_free(
1125
46
          ce->trait_precedences, sizeof(zend_trait_precedence*) * (i + 1));
1126
46
      }
1127
742
    }
1128
1129
12.7k
    ZEND_ASSERT(ce->backed_enum_table == NULL);
1130
12.7k
  }
1131
1132
12.7k
  return ce;
1133
14.3k
}
1134
1135
void zend_update_parent_ce(zend_class_entry *ce)
1136
14.3k
{
1137
14.3k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1138
11.5k
    if (ce->parent) {
1139
1.65k
      int i, end;
1140
1.65k
      zend_class_entry *parent = ce->parent;
1141
1142
1.65k
      if (parent->type == ZEND_USER_CLASS) {
1143
1.48k
        zend_class_entry *p = zend_shared_alloc_get_xlat_entry(parent);
1144
1145
1.48k
        if (p) {
1146
1.16k
          ce->parent = parent = p;
1147
1.16k
        }
1148
1.48k
      }
1149
1150
      /* Create indirections to static properties from parent classes */
1151
1.65k
      i = parent->default_static_members_count - 1;
1152
1.79k
      while (parent && parent->default_static_members_table) {
1153
133
        end = parent->parent ? parent->parent->default_static_members_count : 0;
1154
316
        for (; i >= end; i--) {
1155
183
          zval *p = &ce->default_static_members_table[i];
1156
          /* The static property may have been overridden by a trait
1157
           * during inheritance. In that case, the property default
1158
           * value is replaced by zend_declare_typed_property() at the
1159
           * property index of the parent property. Make sure we only
1160
           * point to the parent property value if the child value was
1161
           * already indirect. */
1162
183
          if (Z_TYPE_P(p) == IS_INDIRECT) {
1163
183
            ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
1164
183
          }
1165
183
        }
1166
1167
133
        parent = parent->parent;
1168
133
      }
1169
1.65k
    }
1170
1171
11.5k
    if (ce->num_interfaces) {
1172
1.06k
      uint32_t i = 0;
1173
1174
1.06k
      ce->interfaces = zend_shared_memdup_free(ce->interfaces, sizeof(zend_class_entry*) * ce->num_interfaces);
1175
2.53k
      for (i = 0; i < ce->num_interfaces; i++) {
1176
1.47k
        if (ce->interfaces[i]->type == ZEND_USER_CLASS) {
1177
526
          zend_class_entry *tmp = zend_shared_alloc_get_xlat_entry(ce->interfaces[i]);
1178
526
          if (tmp != NULL) {
1179
0
            ce->interfaces[i] = tmp;
1180
0
          }
1181
526
        }
1182
1.47k
      }
1183
1.06k
    }
1184
1185
11.5k
    if (ce->iterator_funcs_ptr) {
1186
135
      memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
1187
135
      if (zend_class_implements_interface(ce, zend_ce_aggregate)) {
1188
90
        ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1);
1189
90
      }
1190
135
      if (zend_class_implements_interface(ce, zend_ce_iterator)) {
1191
45
        ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1);
1192
45
        ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1);
1193
45
        ce->iterator_funcs_ptr->zf_key = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_KEY));
1194
45
        ce->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&ce->function_table, "current", sizeof("current") - 1);
1195
45
        ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1);
1196
45
      }
1197
135
    }
1198
1199
11.5k
    if (ce->arrayaccess_funcs_ptr) {
1200
154
      ZEND_ASSERT(zend_class_implements_interface(ce, zend_ce_arrayaccess));
1201
154
      ce->arrayaccess_funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(&ce->function_table, "offsetget", sizeof("offsetget") - 1);
1202
154
      ce->arrayaccess_funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(&ce->function_table, "offsetexists", sizeof("offsetexists") - 1);
1203
154
      ce->arrayaccess_funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(&ce->function_table, "offsetset", sizeof("offsetset") - 1);
1204
154
      ce->arrayaccess_funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(&ce->function_table, "offsetunset", sizeof("offsetunset") - 1);
1205
154
    }
1206
11.5k
  }
1207
1208
  /* update methods */
1209
14.3k
  if (ce->constructor) {
1210
1.81k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->constructor);
1211
1.81k
    if (tmp != NULL) {
1212
1.72k
      ce->constructor = tmp;
1213
1.72k
    }
1214
1.81k
  }
1215
14.3k
  if (ce->destructor) {
1216
835
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->destructor);
1217
835
    if (tmp != NULL) {
1218
835
      ce->destructor = tmp;
1219
835
    }
1220
835
  }
1221
14.3k
  if (ce->clone) {
1222
186
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->clone);
1223
186
    if (tmp != NULL) {
1224
124
      ce->clone = tmp;
1225
124
    }
1226
186
  }
1227
14.3k
  if (ce->__get) {
1228
485
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__get);
1229
485
    if (tmp != NULL) {
1230
485
      ce->__get = tmp;
1231
485
    }
1232
485
  }
1233
14.3k
  if (ce->__set) {
1234
344
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__set);
1235
344
    if (tmp != NULL) {
1236
344
      ce->__set = tmp;
1237
344
    }
1238
344
  }
1239
14.3k
  if (ce->__call) {
1240
298
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__call);
1241
298
    if (tmp != NULL) {
1242
298
      ce->__call = tmp;
1243
298
    }
1244
298
  }
1245
14.3k
  if (ce->__serialize) {
1246
96
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__serialize);
1247
96
    if (tmp != NULL) {
1248
36
      ce->__serialize = tmp;
1249
36
    }
1250
96
  }
1251
14.3k
  if (ce->__unserialize) {
1252
88
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unserialize);
1253
88
    if (tmp != NULL) {
1254
28
      ce->__unserialize = tmp;
1255
28
    }
1256
88
  }
1257
14.3k
  if (ce->__isset) {
1258
172
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__isset);
1259
172
    if (tmp != NULL) {
1260
172
      ce->__isset = tmp;
1261
172
    }
1262
172
  }
1263
14.3k
  if (ce->__unset) {
1264
78
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unset);
1265
78
    if (tmp != NULL) {
1266
78
      ce->__unset = tmp;
1267
78
    }
1268
78
  }
1269
14.3k
  if (ce->__tostring) {
1270
580
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__tostring);
1271
580
    if (tmp != NULL) {
1272
522
      ce->__tostring = tmp;
1273
522
    }
1274
580
  }
1275
14.3k
  if (ce->__callstatic) {
1276
176
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__callstatic);
1277
176
    if (tmp != NULL) {
1278
176
      ce->__callstatic = tmp;
1279
176
    }
1280
176
  }
1281
14.3k
  if (ce->__debugInfo) {
1282
80
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__debugInfo);
1283
80
    if (tmp != NULL) {
1284
62
      ce->__debugInfo = tmp;
1285
62
    }
1286
80
  }
1287
14.3k
}
1288
1289
#ifdef HAVE_JIT
1290
static void zend_accel_persist_jit_op_array(zend_op_array *op_array, const zend_class_entry *ce)
1291
0
{
1292
0
  if (op_array->type == ZEND_USER_FUNCTION) {
1293
0
    if (op_array->scope == ce
1294
0
     && !(op_array->fn_flags & ZEND_ACC_ABSTRACT)
1295
0
     && !(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
1296
0
      zend_jit_op_array(op_array, ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL);
1297
0
      for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
1298
0
        zend_jit_op_array(op_array->dynamic_func_defs[i], ZCG(current_persistent_script) ? &ZCG(current_persistent_script)->script : NULL);
1299
0
      }
1300
0
    }
1301
0
  }
1302
0
}
1303
1304
static void zend_accel_persist_link_func_info(zend_op_array *op_array, const zend_class_entry *ce)
1305
0
{
1306
0
  if (op_array->type == ZEND_USER_FUNCTION
1307
0
   && !(op_array->fn_flags & ZEND_ACC_ABSTRACT)) {
1308
0
    if ((op_array->scope != ce
1309
0
     || (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE))
1310
0
      && (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC
1311
0
      || JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST
1312
0
      || JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS
1313
0
      || JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE)) {
1314
0
      void *jit_extension = zend_shared_alloc_get_xlat_entry(op_array->opcodes);
1315
1316
0
      if (jit_extension) {
1317
0
        ZEND_SET_FUNC_INFO(op_array, jit_extension);
1318
0
      }
1319
0
    }
1320
0
  }
1321
0
}
1322
#endif
1323
1324
static void zend_accel_persist_class_table(HashTable *class_table)
1325
25.9k
{
1326
25.9k
  Bucket *p;
1327
25.9k
  zend_class_entry *ce;
1328
25.9k
#ifdef HAVE_JIT
1329
25.9k
  bool orig_jit_on = JIT_G(on);
1330
1331
25.9k
  JIT_G(on) = 0;
1332
25.9k
#endif
1333
25.9k
  zend_hash_persist(class_table);
1334
77.3k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1335
77.3k
    ZEND_ASSERT(p->key != NULL);
1336
77.3k
    zend_accel_store_interned_string(p->key);
1337
12.7k
    Z_CE(p->val) = zend_persist_class_entry(Z_CE(p->val));
1338
12.7k
  } ZEND_HASH_FOREACH_END();
1339
77.3k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1340
77.3k
    if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) {
1341
12.7k
      ce = Z_PTR(p->val);
1342
12.7k
      zend_update_parent_ce(ce);
1343
12.7k
    }
1344
77.3k
  } ZEND_HASH_FOREACH_END();
1345
25.9k
#ifdef HAVE_JIT
1346
25.9k
  JIT_G(on) = orig_jit_on;
1347
25.9k
  if (JIT_G(on) && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS &&
1348
0
      !ZCG(current_persistent_script)->corrupted) {
1349
0
      zend_op_array *op_array;
1350
0
    zend_property_info *prop;
1351
1352
0
      ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1353
0
      if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) {
1354
0
        ce = Z_PTR(p->val);
1355
0
        ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
1356
0
          zend_accel_persist_jit_op_array(op_array, ce);
1357
0
        } ZEND_HASH_FOREACH_END();
1358
1359
0
        if (ce->num_hooked_props > 0) {
1360
0
          ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) {
1361
0
            if (prop->hooks) {
1362
0
              for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
1363
0
                if (prop->hooks[i]) {
1364
0
                  op_array = &prop->hooks[i]->op_array;
1365
0
                  zend_accel_persist_jit_op_array(op_array, ce);
1366
0
                }
1367
0
              }
1368
0
            }
1369
0
          } ZEND_HASH_FOREACH_END();
1370
0
        }
1371
0
      }
1372
0
    } ZEND_HASH_FOREACH_END();
1373
0
      ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1374
0
      if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) {
1375
0
        ce = Z_PTR(p->val);
1376
0
        ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
1377
0
          zend_accel_persist_link_func_info(op_array, ce);
1378
0
        } ZEND_HASH_FOREACH_END();
1379
1380
0
        if (ce->num_hooked_props > 0) {
1381
0
          ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) {
1382
0
            if (prop->hooks) {
1383
0
              for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
1384
0
                if (prop->hooks[i]) {
1385
0
                  op_array = &prop->hooks[i]->op_array;
1386
0
                  zend_accel_persist_link_func_info(op_array, ce);
1387
0
                }
1388
0
              }
1389
0
            }
1390
0
          } ZEND_HASH_FOREACH_END();
1391
0
        }
1392
0
      }
1393
0
    } ZEND_HASH_FOREACH_END();
1394
0
  }
1395
25.9k
#endif
1396
25.9k
}
1397
1398
27.4k
zend_error_info **zend_persist_warnings(uint32_t num_warnings, zend_error_info **warnings) {
1399
27.4k
  if (warnings) {
1400
26
    warnings = zend_shared_memdup(warnings, num_warnings * sizeof(zend_error_info *));
1401
52
    for (uint32_t i = 0; i < num_warnings; i++) {
1402
26
      zend_accel_store_string(warnings[i]->filename);
1403
26
      zend_accel_store_string(warnings[i]->message);
1404
26
      warnings[i] = zend_shared_memdup(warnings[i], sizeof(zend_error_info));
1405
26
    }
1406
26
  }
1407
27.4k
  return warnings;
1408
27.4k
}
1409
1410
static zend_early_binding *zend_persist_early_bindings(
1411
25.9k
    uint32_t num_early_bindings, zend_early_binding *early_bindings) {
1412
25.9k
  if (early_bindings) {
1413
268
    early_bindings = zend_shared_memdup_free(
1414
268
      early_bindings, num_early_bindings * sizeof(zend_early_binding));
1415
1.31k
    for (uint32_t i = 0; i < num_early_bindings; i++) {
1416
1.04k
      zend_accel_store_interned_string(early_bindings[i].lcname);
1417
1.04k
      zend_accel_store_interned_string(early_bindings[i].rtd_key);
1418
1.04k
      zend_accel_store_interned_string(early_bindings[i].lc_parent_name);
1419
1.04k
    }
1420
268
  }
1421
25.9k
  return early_bindings;
1422
25.9k
}
1423
1424
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, bool for_shm)
1425
25.9k
{
1426
25.9k
  Bucket *p;
1427
1428
25.9k
  script->mem = ZCG(mem);
1429
1430
25.9k
  ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
1431
1432
25.9k
  script = zend_shared_memdup_free(script, sizeof(zend_persistent_script));
1433
25.9k
  script->corrupted = false;
1434
25.9k
  ZCG(current_persistent_script) = script;
1435
1436
25.9k
  if (!for_shm) {
1437
    /* script is not going to be saved in SHM */
1438
0
    script->corrupted = true;
1439
0
  }
1440
1441
25.9k
  zend_accel_store_interned_string(script->script.filename);
1442
1443
25.9k
#if defined(__AVX__) || defined(__SSE2__)
1444
  /* Align to 64-byte boundary */
1445
25.9k
  ZCG(mem) = (void*)(((uintptr_t)ZCG(mem) + 63L) & ~63L);
1446
#else
1447
  ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
1448
#endif
1449
1450
25.9k
#ifdef HAVE_JIT
1451
25.9k
  if (JIT_G(on) && for_shm) {
1452
0
    zend_jit_unprotect();
1453
0
  }
1454
25.9k
#endif
1455
1456
25.9k
  zend_map_ptr_extend(ZCSG(map_ptr_last));
1457
1458
25.9k
  zend_accel_persist_class_table(&script->script.class_table);
1459
25.9k
  zend_hash_persist(&script->script.function_table);
1460
65.9k
  ZEND_HASH_MAP_FOREACH_BUCKET(&script->script.function_table, p) {
1461
65.9k
    ZEND_ASSERT(p->key != NULL);
1462
65.9k
    zend_accel_store_interned_string(p->key);
1463
7.07k
    zend_persist_op_array(&p->val);
1464
7.07k
  } ZEND_HASH_FOREACH_END();
1465
25.9k
  zend_persist_op_array_ex(&script->script.main_op_array, script);
1466
25.9k
  if (!script->corrupted) {
1467
25.9k
    ZEND_MAP_PTR_INIT(script->script.main_op_array.run_time_cache, NULL);
1468
25.9k
    if (script->script.main_op_array.static_variables) {
1469
28
      ZEND_MAP_PTR_NEW(script->script.main_op_array.static_variables_ptr);
1470
28
    }
1471
25.9k
#ifdef HAVE_JIT
1472
25.9k
    if (JIT_G(on) && JIT_G(opt_level) <= ZEND_JIT_LEVEL_OPT_FUNCS) {
1473
0
      zend_jit_op_array(&script->script.main_op_array, &script->script);
1474
0
    }
1475
25.9k
#endif
1476
25.9k
  }
1477
25.9k
  script->warnings = zend_persist_warnings(script->num_warnings, script->warnings);
1478
25.9k
  script->early_bindings = zend_persist_early_bindings(
1479
25.9k
    script->num_early_bindings, script->early_bindings);
1480
1481
25.9k
  if (for_shm) {
1482
25.9k
    ZCSG(map_ptr_last) = CG(map_ptr_last);
1483
25.9k
    ZCSG(map_ptr_static_last) = zend_map_ptr_static_last;
1484
25.9k
  }
1485
1486
25.9k
#ifdef HAVE_JIT
1487
25.9k
  if (JIT_G(on) && for_shm) {
1488
0
    if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_SCRIPT) {
1489
0
      zend_jit_script(&script->script);
1490
0
    }
1491
0
    zend_jit_protect();
1492
0
  }
1493
25.9k
#endif
1494
1495
25.9k
  script->corrupted = false;
1496
25.9k
  ZCG(current_persistent_script) = NULL;
1497
1498
25.9k
  return script;
1499
25.9k
}