Coverage Report

Created: 2026-02-14 06:52

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 (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_vm.h"
28
#include "zend_constants.h"
29
#include "zend_operators.h"
30
#include "zend_interfaces.h"
31
#include "zend_attributes.h"
32
33
#ifdef HAVE_JIT
34
# include "Optimizer/zend_func_info.h"
35
# include "jit/zend_jit.h"
36
#endif
37
38
93.8k
#define zend_set_str_gc_flags(str) do { \
39
93.8k
  GC_SET_REFCOUNT(str, 2); \
40
93.8k
  uint32_t flags = GC_STRING | (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0); \
41
93.8k
  if (file_cache_only \
42
93.8k
   || (ZCG(current_persistent_script) && ZCG(current_persistent_script)->corrupted)) { \
43
0
    GC_TYPE_INFO(str) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT); \
44
0
    flags |= (IS_STR_INTERNED << GC_FLAGS_SHIFT); \
45
93.8k
  } else { \
46
93.8k
    flags |= ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \
47
93.8k
  } \
48
93.8k
  GC_TYPE_INFO(str) = flags; \
49
93.8k
} while (0)
50
51
158k
#define zend_accel_store_string(str) do { \
52
158k
    zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \
53
158k
    if (new_str) { \
54
64.7k
      zend_string_release_ex(str, 0); \
55
64.7k
      str = new_str; \
56
93.8k
    } else { \
57
93.8k
      new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \
58
93.8k
      zend_string_release_ex(str, 0); \
59
93.8k
      str = new_str; \
60
93.8k
      zend_string_hash_val(str); \
61
93.8k
      zend_set_str_gc_flags(str); \
62
93.8k
    } \
63
158k
  } while (0)
64
#define zend_accel_memdup_string(str) do { \
65
    zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \
66
    if (new_str) { \
67
      str = new_str; \
68
    } else { \
69
      new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \
70
      str = new_str; \
71
      zend_string_hash_val(str); \
72
      zend_set_str_gc_flags(str); \
73
    } \
74
  } while (0)
75
1.16M
#define zend_accel_store_interned_string(str) do { \
76
1.16M
    if (!IS_ACCEL_INTERNED(str)) { \
77
52.6k
      zend_accel_store_string(str); \
78
52.6k
    } \
79
1.16M
  } while (0)
80
#define zend_accel_memdup_interned_string(str) do { \
81
    if (!IS_ACCEL_INTERNED(str)) { \
82
      zend_accel_memdup_string(str); \
83
    } \
84
  } while (0)
85
86
typedef void (*zend_persist_func_t)(zval*);
87
88
static void zend_persist_zval(zval *z);
89
static void zend_persist_op_array(zval *zv);
90
91
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
92
  {HT_INVALID_IDX, HT_INVALID_IDX};
93
94
static void zend_hash_persist(HashTable *ht)
95
187k
{
96
187k
  uint32_t idx, nIndex;
97
187k
  Bucket *p;
98
99
187k
  HT_FLAGS(ht) |= HASH_FLAG_STATIC_KEYS;
100
187k
  ht->pDestructor = NULL;
101
187k
  ht->nInternalPointer = 0;
102
103
187k
  if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
104
119k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
105
119k
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
106
119k
    } else {
107
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
108
0
    }
109
119k
    return;
110
119k
  }
111
67.9k
  if (ht->nNumUsed == 0) {
112
2
    efree(HT_GET_DATA_ADDR(ht));
113
2
    ht->nTableMask = HT_MIN_MASK;
114
2
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
115
2
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
116
2
    } else {
117
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
118
0
    }
119
2
    HT_FLAGS(ht) |= HASH_FLAG_UNINITIALIZED;
120
2
    return;
121
2
  }
122
67.9k
  if (HT_IS_PACKED(ht)) {
123
17.3k
    void *data = HT_GET_DATA_ADDR(ht);
124
17.3k
    if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
125
0
      data = zend_shared_memdup(data, HT_PACKED_USED_SIZE(ht));
126
17.3k
    } else {
127
17.3k
      data = zend_shared_memdup_free(data, HT_PACKED_USED_SIZE(ht));
128
17.3k
    }
129
17.3k
    HT_SET_DATA_ADDR(ht, data);
130
50.6k
  } else if (ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) {
131
    /* compact table */
132
10
    void *old_data = HT_GET_DATA_ADDR(ht);
133
10
    Bucket *old_buckets = ht->arData;
134
10
    uint32_t hash_size;
135
136
10
    hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
137
24
    while (hash_size >> 2 > ht->nNumUsed) {
138
14
      hash_size >>= 1;
139
14
    }
140
10
    ht->nTableMask = (uint32_t)(-(int32_t)hash_size);
141
10
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
142
10
    HT_SET_DATA_ADDR(ht, ZCG(mem));
143
10
    ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE((hash_size * sizeof(uint32_t)) + (ht->nNumUsed * sizeof(Bucket))));
144
10
    HT_HASH_RESET(ht);
145
10
    memcpy(ht->arData, old_buckets, ht->nNumUsed * sizeof(Bucket));
146
10
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
147
10
      efree(old_data);
148
10
    }
149
150
    /* rehash */
151
192
    for (idx = 0; idx < ht->nNumUsed; idx++) {
152
182
      p = ht->arData + idx;
153
182
      if (Z_TYPE(p->val) == IS_UNDEF) continue;
154
182
      nIndex = p->h | ht->nTableMask;
155
182
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
156
182
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
157
182
    }
158
50.6k
  } else {
159
50.6k
    void *data = ZCG(mem);
160
50.6k
    void *old_data = HT_GET_DATA_ADDR(ht);
161
162
50.6k
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
163
50.6k
    ZCG(mem) = (void*)((char*)data + ZEND_ALIGNED_SIZE(HT_USED_SIZE(ht)));
164
50.6k
    memcpy(data, old_data, HT_USED_SIZE(ht));
165
50.6k
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
166
50.6k
      efree(old_data);
167
50.6k
    }
168
50.6k
    HT_SET_DATA_ADDR(ht, data);
169
50.6k
  }
170
67.9k
}
171
172
static zend_ast *zend_persist_ast(zend_ast *ast)
173
15.5k
{
174
15.5k
  uint32_t i;
175
15.5k
  zend_ast *node;
176
177
15.5k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
178
9.48k
    zend_ast_zval *copy = zend_shared_memdup(ast, sizeof(zend_ast_zval));
179
9.48k
    zend_persist_zval(&copy->val);
180
9.48k
    node = (zend_ast *) copy;
181
9.48k
  } else if (zend_ast_is_list(ast)) {
182
949
    zend_ast_list *list = zend_ast_get_list(ast);
183
949
    zend_ast_list *copy = zend_shared_memdup(ast,
184
949
      sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
185
1.98k
    for (i = 0; i < list->children; i++) {
186
1.03k
      if (copy->child[i]) {
187
1.03k
        copy->child[i] = zend_persist_ast(copy->child[i]);
188
1.03k
      }
189
1.03k
    }
190
949
    node = (zend_ast *) copy;
191
5.07k
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
192
58
    zend_ast_op_array *copy = zend_shared_memdup(ast, sizeof(zend_ast_op_array));
193
58
    zval z;
194
58
    ZVAL_PTR(&z, copy->op_array);
195
58
    zend_persist_op_array(&z);
196
58
    copy->op_array = Z_PTR(z);
197
58
    node = (zend_ast *) copy;
198
5.02k
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
199
196
    zend_ast_fcc *copy = zend_shared_memdup(ast, sizeof(zend_ast_fcc));
200
196
    copy->args = zend_persist_ast(copy->args);
201
196
    node = (zend_ast *) copy;
202
4.82k
  } else if (zend_ast_is_decl(ast)) {
203
    /* Not implemented. */
204
0
    ZEND_UNREACHABLE();
205
4.82k
  } else {
206
4.82k
    uint32_t children = zend_ast_get_num_children(ast);
207
4.82k
    node = zend_shared_memdup(ast, zend_ast_size(children));
208
16.5k
    for (i = 0; i < children; i++) {
209
11.7k
      if (node->child[i]) {
210
10.4k
        node->child[i] = zend_persist_ast(node->child[i]);
211
10.4k
      }
212
11.7k
    }
213
4.82k
  }
214
215
15.5k
  return node;
216
15.5k
}
217
218
static void zend_persist_zval(zval *z)
219
973k
{
220
973k
  void *new_ptr;
221
222
973k
  switch (Z_TYPE_P(z)) {
223
579k
    case IS_STRING:
224
579k
      zend_accel_store_interned_string(Z_STR_P(z));
225
579k
      Z_TYPE_FLAGS_P(z) = 0;
226
579k
      break;
227
20.3k
    case IS_ARRAY:
228
20.3k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_ARR_P(z));
229
20.3k
      if (new_ptr) {
230
418
        Z_ARR_P(z) = new_ptr;
231
418
        Z_TYPE_FLAGS_P(z) = 0;
232
19.9k
      } else if (!ZCG(current_persistent_script)->corrupted
233
19.9k
       && zend_accel_in_shm(Z_ARR_P(z))) {
234
        /* pass */
235
19.8k
      } else {
236
19.8k
        HashTable *ht;
237
238
19.8k
        if (!Z_REFCOUNTED_P(z)) {
239
2.70k
          ht = zend_shared_memdup_put(Z_ARR_P(z), sizeof(zend_array));
240
17.1k
        } else {
241
17.1k
          GC_REMOVE_FROM_BUFFER(Z_ARR_P(z));
242
17.1k
          ht = zend_shared_memdup_put_free(Z_ARR_P(z), sizeof(zend_array));
243
17.1k
        }
244
19.8k
        Z_ARR_P(z) = ht;
245
19.8k
        zend_hash_persist(ht);
246
19.8k
        if (HT_IS_PACKED(ht)) {
247
15.1k
          zval *zv;
248
249
502k
          ZEND_HASH_PACKED_FOREACH_VAL(ht, zv) {
250
502k
            zend_persist_zval(zv);
251
502k
          } ZEND_HASH_FOREACH_END();
252
15.1k
        } else {
253
4.70k
          Bucket *p;
254
255
26.2k
          ZEND_HASH_MAP_FOREACH_BUCKET(ht, p) {
256
26.2k
            if (p->key) {
257
2.82k
              zend_accel_store_interned_string(p->key);
258
2.82k
            }
259
26.2k
            zend_persist_zval(&p->val);
260
26.2k
          } ZEND_HASH_FOREACH_END();
261
4.70k
        }
262
        /* make immutable array */
263
19.8k
        Z_TYPE_FLAGS_P(z) = 0;
264
19.8k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 2);
265
19.8k
        GC_ADD_FLAGS(Z_COUNTED_P(z), IS_ARRAY_IMMUTABLE);
266
19.8k
      }
267
20.3k
      break;
268
20.3k
    case IS_CONSTANT_AST:
269
3.92k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_AST_P(z));
270
3.92k
      if (new_ptr) {
271
30
        Z_AST_P(z) = new_ptr;
272
30
        Z_TYPE_FLAGS_P(z) = 0;
273
3.89k
      } else if (ZCG(current_persistent_script)->corrupted
274
3.89k
       || !zend_accel_in_shm(Z_AST_P(z))) {
275
3.79k
        zend_ast_ref *old_ref = Z_AST_P(z);
276
3.79k
        Z_AST_P(z) = zend_shared_memdup_put(Z_AST_P(z), sizeof(zend_ast_ref));
277
3.79k
        zend_persist_ast(GC_AST(old_ref));
278
3.79k
        Z_TYPE_FLAGS_P(z) = 0;
279
3.79k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 1);
280
3.79k
        GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE);
281
3.79k
        efree(old_ref);
282
3.79k
      }
283
3.92k
      break;
284
152
    case IS_PTR:
285
152
      break;
286
369k
    default:
287
369k
      ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING);
288
369k
      break;
289
973k
  }
290
973k
}
291
292
static HashTable *zend_persist_attributes(HashTable *attributes)
293
2.24k
{
294
2.24k
  uint32_t i;
295
2.24k
  zval *v;
296
297
2.24k
  if (!ZCG(current_persistent_script)->corrupted && zend_accel_in_shm(attributes)) {
298
40
    return attributes;
299
40
  }
300
301
  /* Attributes for trait properties may be shared if preloading is used. */
302
2.20k
  HashTable *xlat = zend_shared_alloc_get_xlat_entry(attributes);
303
2.20k
  if (xlat) {
304
0
    return xlat;
305
0
  }
306
307
2.20k
  zend_hash_persist(attributes);
308
309
10.0k
  ZEND_HASH_PACKED_FOREACH_VAL(attributes, v) {
310
10.0k
    zend_attribute *attr = Z_PTR_P(v);
311
10.0k
    zend_attribute *copy = zend_shared_memdup_put_free(attr, ZEND_ATTRIBUTE_SIZE(attr->argc));
312
313
10.0k
    zend_accel_store_interned_string(copy->name);
314
10.0k
    zend_accel_store_interned_string(copy->lcname);
315
10.0k
    if (copy->validation_error) {
316
74
      zend_accel_store_interned_string(copy->validation_error);
317
74
    }
318
319
10.0k
    for (i = 0; i < copy->argc; i++) {
320
1.05k
      if (copy->args[i].name) {
321
114
        zend_accel_store_interned_string(copy->args[i].name);
322
114
      }
323
1.05k
      zend_persist_zval(&copy->args[i].value);
324
1.05k
    }
325
326
10.0k
    ZVAL_PTR(v, copy);
327
10.0k
  } ZEND_HASH_FOREACH_END();
328
329
2.20k
  HashTable *ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable));
330
2.20k
  GC_SET_REFCOUNT(ptr, 2);
331
2.20k
  GC_TYPE_INFO(ptr) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
332
333
2.20k
  return ptr;
334
2.20k
}
335
336
uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name)
337
27.6k
{
338
27.6k
  uint32_t ret;
339
340
27.6k
  if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF)) ||
341
27.5k
      zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
342
112
    return 0;
343
112
  }
344
345
  /* We use type.name.gc.refcount to keep map_ptr of corresponding type */
346
27.5k
  if (ZSTR_HAS_CE_CACHE(type_name)) {
347
25.7k
    return GC_REFCOUNT(type_name);
348
25.7k
  }
349
350
1.77k
  if ((GC_FLAGS(type_name) & GC_IMMUTABLE)
351
1.77k
   && (GC_FLAGS(type_name) & IS_STR_PERMANENT)) {
352
1.77k
    do {
353
1.77k
      ret = ZEND_MAP_PTR_NEW_OFFSET();
354
1.77k
    } while (ret <= 2);
355
1.77k
    GC_SET_REFCOUNT(type_name, ret);
356
1.77k
    GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
357
1.77k
    return ret;
358
1.77k
  }
359
360
0
  return 0;
361
1.77k
}
362
363
48.1k
static void zend_persist_type(zend_type *type) {
364
48.1k
  if (ZEND_TYPE_HAS_LIST(*type)) {
365
1.94k
    zend_type_list *list = ZEND_TYPE_LIST(*type);
366
1.94k
    if (ZEND_TYPE_USES_ARENA(*type) || zend_accel_in_shm(list)) {
367
1.94k
      list = zend_shared_memdup_put(list, ZEND_TYPE_LIST_SIZE(list->num_types));
368
1.94k
      ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
369
1.94k
    } else {
370
0
      list = zend_shared_memdup_put_free(list, ZEND_TYPE_LIST_SIZE(list->num_types));
371
0
    }
372
1.94k
    ZEND_TYPE_SET_PTR(*type, list);
373
1.94k
  }
374
375
48.1k
  zend_type *single_type;
376
98.1k
  ZEND_TYPE_FOREACH_MUTABLE(*type, single_type) {
377
98.1k
    if (ZEND_TYPE_HAS_LIST(*single_type)) {
378
560
      zend_persist_type(single_type);
379
560
      continue;
380
560
    }
381
49.4k
    if (ZEND_TYPE_HAS_NAME(*single_type)) {
382
6.85k
      zend_string *type_name = ZEND_TYPE_NAME(*single_type);
383
6.85k
      zend_accel_store_interned_string(type_name);
384
6.85k
      ZEND_TYPE_SET_PTR(*single_type, type_name);
385
6.85k
      if (!ZCG(current_persistent_script)->corrupted) {
386
6.85k
        zend_accel_get_class_name_map_ptr(type_name);
387
6.85k
      }
388
6.85k
    }
389
49.4k
  } ZEND_TYPE_FOREACH_END();
390
48.1k
}
391
392
static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_script* main_persistent_script)
393
88.7k
{
394
88.7k
  zend_op *persist_ptr;
395
88.7k
  zval *orig_literals = NULL;
396
397
88.7k
  if (op_array->refcount && --(*op_array->refcount) == 0) {
398
83.0k
    efree(op_array->refcount);
399
83.0k
  }
400
88.7k
  op_array->refcount = NULL;
401
402
88.7k
  if (main_persistent_script) {
403
45.5k
    zend_execute_data *orig_execute_data = EG(current_execute_data);
404
45.5k
    zend_execute_data fake_execute_data;
405
45.5k
    zval *offset;
406
407
45.5k
    memset(&fake_execute_data, 0, sizeof(fake_execute_data));
408
45.5k
    fake_execute_data.func = (zend_function*)op_array;
409
45.5k
    EG(current_execute_data) = &fake_execute_data;
410
45.5k
    if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) {
411
34
      main_persistent_script->compiler_halt_offset = Z_LVAL_P(offset);
412
34
    }
413
45.5k
    EG(current_execute_data) = orig_execute_data;
414
45.5k
  }
415
416
88.7k
  if (op_array->function_name) {
417
43.2k
    zend_string *old_name = op_array->function_name;
418
43.2k
    zend_accel_store_interned_string(op_array->function_name);
419
    /* Remember old function name, so it can be released multiple times if shared. */
420
43.2k
    if (op_array->function_name != old_name
421
638
        && !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) {
422
638
      zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name);
423
638
    }
424
43.2k
  }
425
426
88.7k
  if (op_array->scope) {
427
25.0k
    zend_class_entry *scope = zend_shared_alloc_get_xlat_entry(op_array->scope);
428
429
25.0k
    if (scope) {
430
25.0k
      op_array->scope = scope;
431
25.0k
    }
432
433
25.0k
    if (op_array->prototype) {
434
3.30k
      zend_function *ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype);
435
436
3.30k
      if (ptr) {
437
809
        op_array->prototype = ptr;
438
809
      }
439
3.30k
    }
440
441
25.0k
    persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->opcodes);
442
25.0k
    if (persist_ptr) {
443
0
      op_array->opcodes = persist_ptr;
444
0
      if (op_array->static_variables) {
445
0
        op_array->static_variables = zend_shared_alloc_get_xlat_entry(op_array->static_variables);
446
0
        ZEND_ASSERT(op_array->static_variables != NULL);
447
0
      }
448
0
      if (op_array->literals) {
449
0
        op_array->literals = zend_shared_alloc_get_xlat_entry(op_array->literals);
450
0
        ZEND_ASSERT(op_array->literals != NULL);
451
0
      }
452
0
      if (op_array->filename) {
453
0
        op_array->filename = zend_shared_alloc_get_xlat_entry(op_array->filename);
454
0
        ZEND_ASSERT(op_array->filename != NULL);
455
0
      }
456
0
      if (op_array->arg_info) {
457
0
        zend_arg_info *arg_info = op_array->arg_info;
458
0
        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
459
0
          arg_info--;
460
0
        }
461
0
        arg_info = zend_shared_alloc_get_xlat_entry(arg_info);
462
0
        ZEND_ASSERT(arg_info != NULL);
463
0
        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
464
0
          arg_info++;
465
0
        }
466
0
        op_array->arg_info = arg_info;
467
0
      }
468
0
      if (op_array->live_range) {
469
0
        op_array->live_range = zend_shared_alloc_get_xlat_entry(op_array->live_range);
470
0
        ZEND_ASSERT(op_array->live_range != NULL);
471
0
      }
472
0
      if (op_array->doc_comment) {
473
0
        if (ZCG(accel_directives).save_comments) {
474
0
          op_array->doc_comment = zend_shared_alloc_get_xlat_entry(op_array->doc_comment);
475
0
          ZEND_ASSERT(op_array->doc_comment != NULL);
476
0
        } else {
477
0
          op_array->doc_comment = NULL;
478
0
        }
479
0
      }
480
0
      if (op_array->attributes) {
481
0
        op_array->attributes = zend_shared_alloc_get_xlat_entry(op_array->attributes);
482
0
        ZEND_ASSERT(op_array->attributes != NULL);
483
0
      }
484
485
0
      if (op_array->try_catch_array) {
486
0
        op_array->try_catch_array = zend_shared_alloc_get_xlat_entry(op_array->try_catch_array);
487
0
        ZEND_ASSERT(op_array->try_catch_array != NULL);
488
0
      }
489
0
      if (op_array->vars) {
490
0
        op_array->vars = zend_shared_alloc_get_xlat_entry(op_array->vars);
491
0
        ZEND_ASSERT(op_array->vars != NULL);
492
0
      }
493
0
      if (op_array->dynamic_func_defs) {
494
0
        op_array->dynamic_func_defs = zend_shared_alloc_get_xlat_entry(op_array->dynamic_func_defs);
495
0
        ZEND_ASSERT(op_array->dynamic_func_defs != NULL);
496
0
      }
497
0
      ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem))));
498
0
      return;
499
0
    }
500
63.6k
  } else {
501
    /* "prototype" may be undefined if "scope" isn't set */
502
63.6k
    op_array->prototype = NULL;
503
63.6k
  }
504
505
88.7k
  if (op_array->scope
506
25.0k
   && !(op_array->fn_flags & ZEND_ACC_CLOSURE)
507
25.0k
   && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) {
508
4.33k
    return;
509
4.33k
  }
510
511
84.4k
  if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) {
512
1.84k
    Bucket *p;
513
514
1.84k
    zend_hash_persist(op_array->static_variables);
515
16.7k
    ZEND_HASH_MAP_FOREACH_BUCKET(op_array->static_variables, p) {
516
16.7k
      ZEND_ASSERT(p->key != NULL);
517
16.7k
      zend_accel_store_interned_string(p->key);
518
6.50k
      zend_persist_zval(&p->val);
519
6.50k
    } ZEND_HASH_FOREACH_END();
520
1.84k
    op_array->static_variables = zend_shared_memdup_put_free(op_array->static_variables, sizeof(HashTable));
521
    /* make immutable array */
522
1.84k
    GC_SET_REFCOUNT(op_array->static_variables, 2);
523
1.84k
    GC_TYPE_INFO(op_array->static_variables) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
524
1.84k
  }
525
526
84.4k
  if (op_array->literals) {
527
82.8k
    zval *p, *end;
528
529
82.8k
    orig_literals = op_array->literals;
530
#if ZEND_USE_ABS_CONST_ADDR
531
    p = zend_shared_memdup_put_free(op_array->literals, sizeof(zval) * op_array->last_literal);
532
#else
533
82.8k
    p = zend_shared_memdup_put(op_array->literals, sizeof(zval) * op_array->last_literal);
534
82.8k
#endif
535
82.8k
    end = p + op_array->last_literal;
536
82.8k
    op_array->literals = p;
537
776k
    while (p < end) {
538
693k
      zend_persist_zval(p);
539
693k
      p++;
540
693k
    }
541
82.8k
  }
542
543
84.4k
  {
544
84.4k
    zend_op *new_opcodes = zend_shared_memdup_put(op_array->opcodes, sizeof(zend_op) * op_array->last);
545
84.4k
    zend_op *opline = new_opcodes;
546
84.4k
    zend_op *end = new_opcodes + op_array->last;
547
84.4k
    int offset = 0;
548
549
2.14M
    for (; opline < end ; opline++, offset++) {
550
#if ZEND_USE_ABS_CONST_ADDR
551
      if (opline->op1_type == IS_CONST) {
552
        opline->op1.zv = (zval*)((char*)opline->op1.zv + ((char*)op_array->literals - (char*)orig_literals));
553
        if (opline->opcode == ZEND_SEND_VAL
554
         || opline->opcode == ZEND_SEND_VAL_EX
555
         || opline->opcode == ZEND_QM_ASSIGN) {
556
          /* Update handlers to eliminate REFCOUNTED check */
557
          zend_vm_set_opcode_handler_ex(opline, 1 << Z_TYPE_P(opline->op1.zv), 0, 0);
558
        }
559
      }
560
      if (opline->op2_type == IS_CONST) {
561
        opline->op2.zv = (zval*)((char*)opline->op2.zv + ((char*)op_array->literals - (char*)orig_literals));
562
      }
563
#else
564
2.05M
      if (opline->op1_type == IS_CONST) {
565
366k
        opline->op1.constant =
566
366k
          (char*)(op_array->literals +
567
366k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
568
366k
            (int32_t)opline->op1.constant) - orig_literals)) -
569
366k
          (char*)opline;
570
366k
        if (opline->opcode == ZEND_SEND_VAL
571
323k
         || opline->opcode == ZEND_SEND_VAL_EX
572
315k
         || opline->opcode == ZEND_QM_ASSIGN) {
573
57.3k
          zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
574
57.3k
        }
575
366k
      }
576
2.05M
      if (opline->op2_type == IS_CONST) {
577
523k
        opline->op2.constant =
578
523k
          (char*)(op_array->literals +
579
523k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
580
523k
            (int32_t)opline->op2.constant) - orig_literals)) -
581
523k
          (char*)opline;
582
523k
      }
583
2.05M
#endif
584
#if ZEND_USE_ABS_JMP_ADDR
585
      if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
586
        /* fix jumps to point to new array */
587
        switch (opline->opcode) {
588
          case ZEND_JMP:
589
          case ZEND_FAST_CALL:
590
            opline->op1.jmp_addr = &new_opcodes[opline->op1.jmp_addr - op_array->opcodes];
591
            break;
592
          case ZEND_JMPZ:
593
          case ZEND_JMPNZ:
594
          case ZEND_JMPZ_EX:
595
          case ZEND_JMPNZ_EX:
596
          case ZEND_JMP_SET:
597
          case ZEND_COALESCE:
598
          case ZEND_FE_RESET_R:
599
          case ZEND_FE_RESET_RW:
600
          case ZEND_ASSERT_CHECK:
601
          case ZEND_JMP_NULL:
602
          case ZEND_BIND_INIT_STATIC_OR_JMP:
603
          case ZEND_JMP_FRAMELESS:
604
            opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes];
605
            break;
606
          case ZEND_CATCH:
607
            if (!(opline->extended_value & ZEND_LAST_CATCH)) {
608
              opline->op2.jmp_addr = &new_opcodes[opline->op2.jmp_addr - op_array->opcodes];
609
            }
610
            break;
611
          case ZEND_FE_FETCH_R:
612
          case ZEND_FE_FETCH_RW:
613
          case ZEND_SWITCH_LONG:
614
          case ZEND_SWITCH_STRING:
615
          case ZEND_MATCH:
616
            /* relative extended_value don't have to be changed */
617
            break;
618
        }
619
      }
620
#endif
621
2.05M
      if (opline->opcode == ZEND_OP_DATA && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) {
622
152
        zval *literal = RT_CONSTANT(opline, opline->op1);
623
152
        HashTable *attributes = Z_PTR_P(literal);
624
152
        attributes = zend_persist_attributes(attributes);
625
152
        ZVAL_PTR(literal, attributes);
626
152
      }
627
2.05M
    }
628
629
84.4k
    efree(op_array->opcodes);
630
84.4k
    op_array->opcodes = new_opcodes;
631
84.4k
  }
632
633
84.4k
  if (op_array->filename) {
634
84.4k
    zend_accel_store_string(op_array->filename);
635
84.4k
  }
636
637
84.4k
  if (op_array->arg_info) {
638
20.3k
    zend_arg_info *arg_info = op_array->arg_info;
639
20.3k
    uint32_t num_args = op_array->num_args;
640
20.3k
    uint32_t i;
641
642
20.3k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
643
6.90k
      arg_info--;
644
6.90k
      num_args++;
645
6.90k
    }
646
20.3k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
647
348
      num_args++;
648
348
    }
649
20.3k
    arg_info = zend_shared_memdup_put_free(arg_info, sizeof(zend_arg_info) * num_args);
650
50.2k
    for (i = 0; i < num_args; i++) {
651
29.8k
      if (arg_info[i].name) {
652
22.9k
        zend_accel_store_interned_string(arg_info[i].name);
653
22.9k
      }
654
29.8k
      zend_persist_type(&arg_info[i].type);
655
29.8k
    }
656
20.3k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
657
6.90k
      arg_info++;
658
6.90k
    }
659
20.3k
    op_array->arg_info = arg_info;
660
20.3k
  }
661
662
84.4k
  if (op_array->live_range) {
663
42.3k
    op_array->live_range = zend_shared_memdup_put_free(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
664
42.3k
  }
665
666
84.4k
  if (op_array->doc_comment) {
667
50
    if (ZCG(accel_directives).save_comments) {
668
50
      zend_accel_store_interned_string(op_array->doc_comment);
669
50
    } else {
670
0
      zend_string_release_ex(op_array->doc_comment, 0);
671
0
      op_array->doc_comment = NULL;
672
0
    }
673
50
  }
674
675
84.4k
  if (op_array->attributes) {
676
857
    op_array->attributes = zend_persist_attributes(op_array->attributes);
677
857
  }
678
679
84.4k
  if (op_array->try_catch_array) {
680
19.6k
    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
19.6k
  }
682
683
84.4k
  if (op_array->vars) {
684
57.3k
    int i;
685
57.3k
    op_array->vars = zend_shared_memdup_put_free(op_array->vars, sizeof(zend_string*) * op_array->last_var);
686
378k
    for (i = 0; i < op_array->last_var; i++) {
687
321k
      zend_accel_store_interned_string(op_array->vars[i]);
688
321k
    }
689
57.3k
  }
690
691
84.4k
  if (op_array->num_dynamic_func_defs) {
692
4.33k
    op_array->dynamic_func_defs = zend_shared_memdup_put_free(
693
4.33k
      op_array->dynamic_func_defs, sizeof(zend_function *) * op_array->num_dynamic_func_defs);
694
10.8k
    for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
695
6.53k
      zval tmp;
696
6.53k
      ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]);
697
6.53k
      zend_persist_op_array(&tmp);
698
6.53k
      op_array->dynamic_func_defs[i] = Z_PTR(tmp);
699
6.53k
    }
700
4.33k
  }
701
702
84.4k
  ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem))));
703
84.4k
}
704
705
static void zend_persist_op_array(zval *zv)
706
18.1k
{
707
18.1k
  zend_op_array *op_array = Z_PTR_P(zv);
708
18.1k
  zend_op_array *old_op_array;
709
18.1k
  ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
710
711
18.1k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
712
18.1k
  if (!old_op_array) {
713
18.1k
    op_array = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_op_array));
714
18.1k
    zend_persist_op_array_ex(op_array, NULL);
715
18.1k
    if (!ZCG(current_persistent_script)->corrupted) {
716
18.1k
      op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
717
18.1k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
718
18.1k
      if (op_array->static_variables) {
719
1.67k
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
720
1.67k
      }
721
18.1k
    }
722
18.1k
#ifdef HAVE_JIT
723
18.1k
    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
18.1k
#endif
730
18.1k
  } 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
18.1k
}
735
736
static zend_op_array *zend_persist_class_method(zend_op_array *op_array, const zend_class_entry *ce)
737
30.7k
{
738
30.7k
  zend_op_array *old_op_array;
739
740
30.7k
  if (op_array->type != ZEND_USER_FUNCTION) {
741
3.57k
    ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION);
742
3.57k
    if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) {
743
3.57k
      old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
744
3.57k
      if (old_op_array) {
745
0
        return old_op_array;
746
3.57k
      } else {
747
3.57k
        op_array = zend_shared_memdup_put(op_array, sizeof(zend_internal_function));
748
3.57k
        if (op_array->scope) {
749
3.57k
          void *persist_ptr;
750
751
3.57k
          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
3.57k
          if (op_array->prototype) {
755
1.61k
            if ((persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype))) {
756
0
              op_array->prototype = (zend_function*)persist_ptr;
757
0
            }
758
1.61k
          }
759
3.57k
        }
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
3.57k
        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
3.57k
      }
770
3.57k
    }
771
3.57k
    return op_array;
772
3.57k
  }
773
774
27.1k
  if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
775
460
   && !ZCG(current_persistent_script)->corrupted
776
460
   && zend_accel_in_shm(op_array)) {
777
452
    zend_shared_alloc_register_xlat_entry(op_array, op_array);
778
452
    return op_array;
779
452
  }
780
781
26.6k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
782
26.6k
  if (old_op_array) {
783
1.61k
    if (op_array->refcount && --(*op_array->refcount) == 0) {
784
1.37k
      efree(op_array->refcount);
785
1.37k
    }
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
1.61k
    zend_string *old_function_name =
790
1.61k
      zend_shared_alloc_get_xlat_entry(&old_op_array->function_name);
791
1.61k
    if (old_function_name) {
792
0
      zend_string_release_ex(old_function_name, 0);
793
0
    }
794
1.61k
    return old_op_array;
795
1.61k
  }
796
797
25.0k
  op_array = zend_shared_memdup_put(op_array, sizeof(zend_op_array));
798
25.0k
  zend_persist_op_array_ex(op_array, NULL);
799
25.0k
  if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
800
25.0k
    op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
801
25.0k
    if (ce->ce_flags & ZEND_ACC_LINKED) {
802
21.1k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
803
21.1k
      if (op_array->static_variables) {
804
191
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
805
191
      }
806
21.1k
    } else {
807
3.97k
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
808
3.97k
      ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
809
3.97k
    }
810
25.0k
  }
811
25.0k
  return op_array;
812
26.6k
}
813
814
static zend_property_info *zend_persist_property_info(zend_property_info *prop)
815
13.8k
{
816
13.8k
  zend_class_entry *ce;
817
13.8k
  prop = zend_shared_memdup_put(prop, sizeof(zend_property_info));
818
13.8k
  ce = zend_shared_alloc_get_xlat_entry(prop->ce);
819
13.8k
  if (ce) {
820
13.8k
    prop->ce = ce;
821
13.8k
  }
822
13.8k
  zend_accel_store_interned_string(prop->name);
823
13.8k
  if (prop->doc_comment) {
824
199
    if (ZCG(accel_directives).save_comments) {
825
199
      zend_accel_store_interned_string(prop->doc_comment);
826
199
    } 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
199
  }
834
13.8k
  if (prop->attributes) {
835
172
    prop->attributes = zend_persist_attributes(prop->attributes);
836
172
  }
837
13.8k
  if (prop->prototype) {
838
13.8k
    const zend_property_info *new_prototype = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(prop->prototype);
839
13.8k
    if (new_prototype) {
840
13.7k
      prop->prototype = new_prototype;
841
13.7k
    }
842
13.8k
  }
843
13.8k
  if (prop->hooks) {
844
1.47k
    prop->hooks = zend_shared_memdup_put(prop->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
845
4.41k
    for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
846
2.94k
      if (prop->hooks[i]) {
847
1.94k
        zend_op_array *hook = zend_persist_class_method(&prop->hooks[i]->op_array, ce);
848
1.94k
#ifdef HAVE_JIT
849
1.94k
        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.94k
#endif
858
1.94k
        const zend_property_info *new_prop_info = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(hook->prop_info);
859
1.94k
        if (new_prop_info) {
860
1.89k
          hook->prop_info = new_prop_info;
861
1.89k
        }
862
1.94k
        prop->hooks[i] = (zend_function *) hook;
863
1.94k
      }
864
2.94k
    }
865
1.47k
  }
866
13.8k
  zend_persist_type(&prop->type);
867
13.8k
  return prop;
868
13.8k
}
869
870
static void zend_persist_class_constant(zval *zv)
871
5.03k
{
872
5.03k
  const zend_class_constant *orig_c = Z_PTR_P(zv);
873
5.03k
  zend_class_constant *c = zend_shared_alloc_get_xlat_entry(orig_c);
874
5.03k
  zend_class_entry *ce;
875
876
5.03k
  if (c) {
877
138
    Z_PTR_P(zv) = c;
878
138
    return;
879
4.89k
  } else if (((orig_c->ce->ce_flags & ZEND_ACC_IMMUTABLE) && !(Z_CONSTANT_FLAGS(orig_c->value) & CONST_OWNED))
880
4.76k
   || orig_c->ce->type == ZEND_INTERNAL_CLASS) {
881
    /* Class constant comes from a different file in shm or internal class, keep existing pointer. */
882
1.01k
    return;
883
3.87k
  } else if (!ZCG(current_persistent_script)->corrupted
884
3.87k
   && zend_accel_in_shm(Z_PTR_P(zv))) {
885
0
    return;
886
0
  }
887
3.87k
  c = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_class_constant));
888
3.87k
  zend_persist_zval(&c->value);
889
3.87k
  ce = zend_shared_alloc_get_xlat_entry(c->ce);
890
3.87k
  if (ce) {
891
3.86k
    c->ce = ce;
892
3.86k
  }
893
3.87k
  if (c->doc_comment) {
894
20
    if (ZCG(accel_directives).save_comments) {
895
20
      zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment);
896
20
      if (doc_comment) {
897
0
        c->doc_comment = doc_comment;
898
20
      } else {
899
20
        zend_accel_store_interned_string(c->doc_comment);
900
20
      }
901
20
    } 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
20
  }
910
3.87k
  if (c->attributes) {
911
160
    c->attributes = zend_persist_attributes(c->attributes);
912
160
  }
913
3.87k
  zend_persist_type(&c->type);
914
3.87k
}
915
916
zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce)
917
24.2k
{
918
24.2k
  Bucket *p;
919
24.2k
  zend_class_entry *ce = orig_ce;
920
921
24.2k
  if (ce->type == ZEND_USER_CLASS) {
922
    /* The same zend_class_entry may be reused by class_alias */
923
24.2k
    zend_class_entry *new_ce = zend_shared_alloc_get_xlat_entry(ce);
924
24.2k
    if (new_ce) {
925
0
      return new_ce;
926
0
    }
927
24.2k
    ce = zend_shared_memdup_put(ce, sizeof(zend_class_entry));
928
24.2k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
929
24.2k
      ce->ce_flags |= ZEND_ACC_IMMUTABLE;
930
24.2k
      if ((ce->ce_flags & ZEND_ACC_LINKED)
931
19.6k
       && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
932
564
        ZEND_MAP_PTR_NEW(ce->mutable_data);
933
23.6k
      } else {
934
23.6k
        ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
935
23.6k
      }
936
24.2k
    } else {
937
0
      ce->ce_flags |= ZEND_ACC_FILE_CACHED;
938
0
    }
939
24.2k
    ce->inheritance_cache = NULL;
940
941
24.2k
    if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
942
21.3k
      if (ZSTR_HAS_CE_CACHE(ce->name)) {
943
19.3k
        ZSTR_SET_CE_CACHE_EX(ce->name, NULL, 0);
944
19.3k
      }
945
21.3k
      zend_accel_store_interned_string(ce->name);
946
21.3k
      if (!(ce->ce_flags & ZEND_ACC_ANON_CLASS)
947
20.7k
       && !ZCG(current_persistent_script)->corrupted) {
948
20.7k
        zend_accel_get_class_name_map_ptr(ce->name);
949
20.7k
      }
950
21.3k
      if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) {
951
1.12k
        zend_accel_store_interned_string(ce->parent_name);
952
1.12k
      }
953
21.3k
    }
954
955
24.2k
    zend_hash_persist(&ce->function_table);
956
105k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->function_table, p) {
957
105k
      ZEND_ASSERT(p->key != NULL);
958
105k
      zend_accel_store_interned_string(p->key);
959
28.7k
      Z_PTR(p->val) = zend_persist_class_method(Z_PTR(p->val), ce);
960
28.7k
    } ZEND_HASH_FOREACH_END();
961
24.2k
    HT_FLAGS(&ce->function_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
962
24.2k
    if (ce->default_properties_table) {
963
8.10k
        int i;
964
965
8.10k
      ce->default_properties_table = zend_shared_memdup_free(ce->default_properties_table, sizeof(zval) * ce->default_properties_count);
966
21.2k
      for (i = 0; i < ce->default_properties_count; i++) {
967
13.1k
        zend_persist_zval(&ce->default_properties_table[i]);
968
13.1k
      }
969
8.10k
    }
970
24.2k
    if (ce->default_static_members_table) {
971
1.38k
      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
3.93k
      for (uint32_t i = 0; i < ce->default_static_members_count; i++) {
976
2.54k
        if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) {
977
2.20k
          zend_persist_zval(&ce->default_static_members_table[i]);
978
2.20k
        }
979
2.54k
      }
980
1.38k
      if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
981
1.38k
        if (ce->ce_flags & ZEND_ACC_LINKED) {
982
1.34k
          ZEND_MAP_PTR_NEW(ce->static_members_table);
983
1.34k
        } else {
984
42
          ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
985
42
        }
986
1.38k
      }
987
1.38k
    }
988
989
24.2k
    zend_hash_persist(&ce->constants_table);
990
58.4k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->constants_table, p) {
991
58.4k
      ZEND_ASSERT(p->key != NULL);
992
58.4k
      zend_accel_store_interned_string(p->key);
993
5.03k
      zend_persist_class_constant(&p->val);
994
5.03k
    } ZEND_HASH_FOREACH_END();
995
24.2k
    HT_FLAGS(&ce->constants_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
996
997
24.2k
    zend_hash_persist(&ce->properties_info);
998
80.5k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->properties_info, p) {
999
80.5k
      zend_property_info *prop = Z_PTR(p->val);
1000
80.5k
      ZEND_ASSERT(p->key != NULL);
1001
80.5k
      zend_accel_store_interned_string(p->key);
1002
16.0k
      if (prop->ce == orig_ce) {
1003
13.8k
        Z_PTR(p->val) = zend_persist_property_info(prop);
1004
13.8k
      } else {
1005
2.20k
        prop = zend_shared_alloc_get_xlat_entry(prop);
1006
2.20k
        if (prop) {
1007
1.07k
          Z_PTR(p->val) = prop;
1008
1.12k
        } 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
1.12k
        }
1013
2.20k
      }
1014
16.0k
    } ZEND_HASH_FOREACH_END();
1015
24.2k
    HT_FLAGS(&ce->properties_info) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
1016
1017
24.2k
    if (ce->properties_info_table) {
1018
6.82k
      int i;
1019
1020
6.82k
      size_t size = sizeof(zend_property_info *) * ce->default_properties_count;
1021
6.82k
      ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
1022
6.82k
      ce->properties_info_table = zend_shared_memdup(
1023
6.82k
        ce->properties_info_table, size);
1024
1025
18.2k
      for (i = 0; i < ce->default_properties_count; i++) {
1026
11.4k
        if (ce->properties_info_table[i]) {
1027
11.0k
          zend_property_info *prop_info = zend_shared_alloc_get_xlat_entry(
1028
11.0k
            ce->properties_info_table[i]);
1029
11.0k
          if (prop_info) {
1030
10.0k
            ce->properties_info_table[i] = prop_info;
1031
10.0k
          }
1032
11.0k
        }
1033
11.4k
      }
1034
6.82k
    }
1035
1036
24.2k
    if (ce->iterator_funcs_ptr) {
1037
230
      ce->iterator_funcs_ptr = zend_shared_memdup(ce->iterator_funcs_ptr, sizeof(zend_class_iterator_funcs));
1038
230
    }
1039
24.2k
    if (ce->arrayaccess_funcs_ptr) {
1040
278
      ce->arrayaccess_funcs_ptr = zend_shared_memdup(ce->arrayaccess_funcs_ptr, sizeof(zend_class_arrayaccess_funcs));
1041
278
    }
1042
1043
24.2k
    if (ce->ce_flags & ZEND_ACC_CACHED) {
1044
2.83k
      return ce;
1045
2.83k
    }
1046
1047
21.3k
    ce->ce_flags |= ZEND_ACC_CACHED;
1048
1049
21.3k
    if (ce->info.user.filename) {
1050
21.3k
      zend_accel_store_string(ce->info.user.filename);
1051
21.3k
    }
1052
1053
21.3k
    if (ce->doc_comment) {
1054
20
      if (ZCG(accel_directives).save_comments) {
1055
20
        zend_accel_store_interned_string(ce->doc_comment);
1056
20
      } 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
20
    }
1064
1065
21.3k
    if (ce->attributes) {
1066
906
      ce->attributes = zend_persist_attributes(ce->attributes);
1067
906
    }
1068
1069
21.3k
    if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_LINKED)) {
1070
2.73k
      uint32_t i = 0;
1071
1072
6.10k
      for (i = 0; i < ce->num_interfaces; i++) {
1073
3.36k
        zend_accel_store_interned_string(ce->interface_names[i].name);
1074
3.36k
        zend_accel_store_interned_string(ce->interface_names[i].lc_name);
1075
3.36k
      }
1076
2.73k
      ce->interface_names = zend_shared_memdup_free(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
1077
2.73k
    }
1078
1079
21.3k
    if (ce->num_traits) {
1080
1.25k
      uint32_t i = 0;
1081
1082
2.81k
      for (i = 0; i < ce->num_traits; i++) {
1083
1.56k
        zend_accel_store_interned_string(ce->trait_names[i].name);
1084
1.56k
        zend_accel_store_interned_string(ce->trait_names[i].lc_name);
1085
1.56k
      }
1086
1.25k
      ce->trait_names = zend_shared_memdup_free(ce->trait_names, sizeof(zend_class_name) * ce->num_traits);
1087
1088
1.25k
      i = 0;
1089
1.25k
      if (ce->trait_aliases) {
1090
560
        while (ce->trait_aliases[i]) {
1091
360
          if (ce->trait_aliases[i]->trait_method.method_name) {
1092
360
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.method_name);
1093
360
          }
1094
360
          if (ce->trait_aliases[i]->trait_method.class_name) {
1095
148
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.class_name);
1096
148
          }
1097
1098
360
          if (ce->trait_aliases[i]->alias) {
1099
262
            zend_accel_store_interned_string(ce->trait_aliases[i]->alias);
1100
262
          }
1101
1102
360
          ce->trait_aliases[i] = zend_shared_memdup_free(ce->trait_aliases[i], sizeof(zend_trait_alias));
1103
360
          i++;
1104
360
        }
1105
1106
200
        ce->trait_aliases = zend_shared_memdup_free(ce->trait_aliases, sizeof(zend_trait_alias*) * (i + 1));
1107
200
      }
1108
1109
1.25k
      if (ce->trait_precedences) {
1110
68
        uint32_t j;
1111
1112
68
        i = 0;
1113
156
        while (ce->trait_precedences[i]) {
1114
88
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.method_name);
1115
88
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.class_name);
1116
1117
180
          for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
1118
92
            zend_accel_store_interned_string(ce->trait_precedences[i]->exclude_class_names[j]);
1119
92
          }
1120
1121
88
          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
88
          i++;
1123
88
        }
1124
68
        ce->trait_precedences = zend_shared_memdup_free(
1125
68
          ce->trait_precedences, sizeof(zend_trait_precedence*) * (i + 1));
1126
68
      }
1127
1.25k
    }
1128
1129
21.3k
    ZEND_ASSERT(ce->backed_enum_table == NULL);
1130
21.3k
  }
1131
1132
21.3k
  return ce;
1133
24.2k
}
1134
1135
void zend_update_parent_ce(zend_class_entry *ce)
1136
24.2k
{
1137
24.2k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1138
19.6k
    if (ce->parent) {
1139
3.02k
      int i, end;
1140
3.02k
      zend_class_entry *parent = ce->parent;
1141
1142
3.02k
      if (parent->type == ZEND_USER_CLASS) {
1143
2.71k
        zend_class_entry *p = zend_shared_alloc_get_xlat_entry(parent);
1144
1145
2.71k
        if (p) {
1146
2.11k
          ce->parent = parent = p;
1147
2.11k
        }
1148
2.71k
      }
1149
1150
      /* Create indirections to static properties from parent classes */
1151
3.02k
      i = parent->default_static_members_count - 1;
1152
3.27k
      while (parent && parent->default_static_members_table) {
1153
246
        end = parent->parent ? parent->parent->default_static_members_count : 0;
1154
588
        for (; i >= end; i--) {
1155
342
          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
342
          if (Z_TYPE_P(p) == IS_INDIRECT) {
1163
342
            ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
1164
342
          }
1165
342
        }
1166
1167
246
        parent = parent->parent;
1168
246
      }
1169
3.02k
    }
1170
1171
19.6k
    if (ce->num_interfaces) {
1172
1.90k
      uint32_t i = 0;
1173
1174
1.90k
      ce->interfaces = zend_shared_memdup_free(ce->interfaces, sizeof(zend_class_entry*) * ce->num_interfaces);
1175
4.59k
      for (i = 0; i < ce->num_interfaces; i++) {
1176
2.68k
        if (ce->interfaces[i]->type == ZEND_USER_CLASS) {
1177
1.07k
          zend_class_entry *tmp = zend_shared_alloc_get_xlat_entry(ce->interfaces[i]);
1178
1.07k
          if (tmp != NULL) {
1179
0
            ce->interfaces[i] = tmp;
1180
0
          }
1181
1.07k
        }
1182
2.68k
      }
1183
1.90k
    }
1184
1185
19.6k
    if (ce->iterator_funcs_ptr) {
1186
230
      memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
1187
230
      if (zend_class_implements_interface(ce, zend_ce_aggregate)) {
1188
152
        ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1);
1189
152
      }
1190
230
      if (zend_class_implements_interface(ce, zend_ce_iterator)) {
1191
78
        ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1);
1192
78
        ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1);
1193
78
        ce->iterator_funcs_ptr->zf_key = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_KEY));
1194
78
        ce->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&ce->function_table, "current", sizeof("current") - 1);
1195
78
        ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1);
1196
78
      }
1197
230
    }
1198
1199
19.6k
    if (ce->arrayaccess_funcs_ptr) {
1200
278
      ZEND_ASSERT(zend_class_implements_interface(ce, zend_ce_arrayaccess));
1201
278
      ce->arrayaccess_funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(&ce->function_table, "offsetget", sizeof("offsetget") - 1);
1202
278
      ce->arrayaccess_funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(&ce->function_table, "offsetexists", sizeof("offsetexists") - 1);
1203
278
      ce->arrayaccess_funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(&ce->function_table, "offsetset", sizeof("offsetset") - 1);
1204
278
      ce->arrayaccess_funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(&ce->function_table, "offsetunset", sizeof("offsetunset") - 1);
1205
278
    }
1206
19.6k
  }
1207
1208
  /* update methods */
1209
24.2k
  if (ce->constructor) {
1210
3.41k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->constructor);
1211
3.41k
    if (tmp != NULL) {
1212
3.25k
      ce->constructor = tmp;
1213
3.25k
    }
1214
3.41k
  }
1215
24.2k
  if (ce->destructor) {
1216
1.55k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->destructor);
1217
1.55k
    if (tmp != NULL) {
1218
1.55k
      ce->destructor = tmp;
1219
1.55k
    }
1220
1.55k
  }
1221
24.2k
  if (ce->clone) {
1222
320
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->clone);
1223
320
    if (tmp != NULL) {
1224
212
      ce->clone = tmp;
1225
212
    }
1226
320
  }
1227
24.2k
  if (ce->__get) {
1228
737
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__get);
1229
737
    if (tmp != NULL) {
1230
737
      ce->__get = tmp;
1231
737
    }
1232
737
  }
1233
24.2k
  if (ce->__set) {
1234
492
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__set);
1235
492
    if (tmp != NULL) {
1236
492
      ce->__set = tmp;
1237
492
    }
1238
492
  }
1239
24.2k
  if (ce->__call) {
1240
547
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__call);
1241
547
    if (tmp != NULL) {
1242
547
      ce->__call = tmp;
1243
547
    }
1244
547
  }
1245
24.2k
  if (ce->__serialize) {
1246
154
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__serialize);
1247
154
    if (tmp != NULL) {
1248
48
      ce->__serialize = tmp;
1249
48
    }
1250
154
  }
1251
24.2k
  if (ce->__unserialize) {
1252
146
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unserialize);
1253
146
    if (tmp != NULL) {
1254
40
      ce->__unserialize = tmp;
1255
40
    }
1256
146
  }
1257
24.2k
  if (ce->__isset) {
1258
260
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__isset);
1259
260
    if (tmp != NULL) {
1260
260
      ce->__isset = tmp;
1261
260
    }
1262
260
  }
1263
24.2k
  if (ce->__unset) {
1264
122
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unset);
1265
122
    if (tmp != NULL) {
1266
122
      ce->__unset = tmp;
1267
122
    }
1268
122
  }
1269
24.2k
  if (ce->__tostring) {
1270
966
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__tostring);
1271
966
    if (tmp != NULL) {
1272
866
      ce->__tostring = tmp;
1273
866
    }
1274
966
  }
1275
24.2k
  if (ce->__callstatic) {
1276
343
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__callstatic);
1277
343
    if (tmp != NULL) {
1278
343
      ce->__callstatic = tmp;
1279
343
    }
1280
343
  }
1281
24.2k
  if (ce->__debugInfo) {
1282
118
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__debugInfo);
1283
118
    if (tmp != NULL) {
1284
86
      ce->__debugInfo = tmp;
1285
86
    }
1286
118
  }
1287
24.2k
}
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
45.5k
{
1326
45.5k
  Bucket *p;
1327
45.5k
  zend_class_entry *ce;
1328
45.5k
#ifdef HAVE_JIT
1329
45.5k
  bool orig_jit_on = JIT_G(on);
1330
1331
45.5k
  JIT_G(on) = 0;
1332
45.5k
#endif
1333
45.5k
  zend_hash_persist(class_table);
1334
133k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1335
133k
    ZEND_ASSERT(p->key != NULL);
1336
133k
    zend_accel_store_interned_string(p->key);
1337
21.3k
    Z_CE(p->val) = zend_persist_class_entry(Z_CE(p->val));
1338
21.3k
  } ZEND_HASH_FOREACH_END();
1339
133k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1340
133k
    if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) {
1341
21.3k
      ce = Z_PTR(p->val);
1342
21.3k
      zend_update_parent_ce(ce);
1343
21.3k
    }
1344
133k
  } ZEND_HASH_FOREACH_END();
1345
45.5k
#ifdef HAVE_JIT
1346
45.5k
  JIT_G(on) = orig_jit_on;
1347
45.5k
  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
45.5k
#endif
1396
45.5k
}
1397
1398
48.3k
zend_error_info **zend_persist_warnings(uint32_t num_warnings, zend_error_info **warnings) {
1399
48.3k
  if (warnings) {
1400
38
    warnings = zend_shared_memdup(warnings, num_warnings * sizeof(zend_error_info *));
1401
78
    for (uint32_t i = 0; i < num_warnings; i++) {
1402
40
      zend_accel_store_string(warnings[i]->filename);
1403
40
      zend_accel_store_string(warnings[i]->message);
1404
40
      warnings[i] = zend_shared_memdup(warnings[i], sizeof(zend_error_info));
1405
40
    }
1406
38
  }
1407
48.3k
  return warnings;
1408
48.3k
}
1409
1410
static zend_early_binding *zend_persist_early_bindings(
1411
45.5k
    uint32_t num_early_bindings, zend_early_binding *early_bindings) {
1412
45.5k
  if (early_bindings) {
1413
429
    early_bindings = zend_shared_memdup_free(
1414
429
      early_bindings, num_early_bindings * sizeof(zend_early_binding));
1415
1.70k
    for (uint32_t i = 0; i < num_early_bindings; i++) {
1416
1.27k
      zend_accel_store_interned_string(early_bindings[i].lcname);
1417
1.27k
      zend_accel_store_interned_string(early_bindings[i].rtd_key);
1418
1.27k
      zend_accel_store_interned_string(early_bindings[i].lc_parent_name);
1419
1.27k
    }
1420
429
  }
1421
45.5k
  return early_bindings;
1422
45.5k
}
1423
1424
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, bool for_shm)
1425
45.5k
{
1426
45.5k
  Bucket *p;
1427
1428
45.5k
  script->mem = ZCG(mem);
1429
1430
45.5k
  ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
1431
1432
45.5k
  script = zend_shared_memdup_free(script, sizeof(zend_persistent_script));
1433
45.5k
  script->corrupted = false;
1434
45.5k
  ZCG(current_persistent_script) = script;
1435
1436
45.5k
  if (!for_shm) {
1437
    /* script is not going to be saved in SHM */
1438
0
    script->corrupted = true;
1439
0
  }
1440
1441
45.5k
  zend_accel_store_interned_string(script->script.filename);
1442
1443
45.5k
#if defined(__AVX__) || defined(__SSE2__)
1444
  /* Align to 64-byte boundary */
1445
45.5k
  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
45.5k
#ifdef HAVE_JIT
1451
45.5k
  if (JIT_G(on) && for_shm) {
1452
0
    zend_jit_unprotect();
1453
0
  }
1454
45.5k
#endif
1455
1456
45.5k
  zend_map_ptr_extend(ZCSG(map_ptr_last));
1457
1458
45.5k
  zend_accel_persist_class_table(&script->script.class_table);
1459
45.5k
  zend_hash_persist(&script->script.function_table);
1460
114k
  ZEND_HASH_MAP_FOREACH_BUCKET(&script->script.function_table, p) {
1461
114k
    ZEND_ASSERT(p->key != NULL);
1462
114k
    zend_accel_store_interned_string(p->key);
1463
11.5k
    zend_persist_op_array(&p->val);
1464
11.5k
  } ZEND_HASH_FOREACH_END();
1465
45.5k
  zend_persist_op_array_ex(&script->script.main_op_array, script);
1466
45.5k
  if (!script->corrupted) {
1467
45.5k
    ZEND_MAP_PTR_INIT(script->script.main_op_array.run_time_cache, NULL);
1468
45.5k
    if (script->script.main_op_array.static_variables) {
1469
36
      ZEND_MAP_PTR_NEW(script->script.main_op_array.static_variables_ptr);
1470
36
    }
1471
45.5k
#ifdef HAVE_JIT
1472
45.5k
    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
45.5k
#endif
1476
45.5k
  }
1477
45.5k
  script->warnings = zend_persist_warnings(script->num_warnings, script->warnings);
1478
45.5k
  script->early_bindings = zend_persist_early_bindings(
1479
45.5k
    script->num_early_bindings, script->early_bindings);
1480
1481
45.5k
  if (for_shm) {
1482
45.5k
    ZCSG(map_ptr_last) = CG(map_ptr_last);
1483
45.5k
    ZCSG(map_ptr_static_last) = zend_map_ptr_static_last;
1484
45.5k
  }
1485
1486
45.5k
#ifdef HAVE_JIT
1487
45.5k
  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
45.5k
#endif
1494
1495
45.5k
  script->corrupted = false;
1496
45.5k
  ZCG(current_persistent_script) = NULL;
1497
1498
45.5k
  return script;
1499
45.5k
}