Coverage Report

Created: 2026-06-13 07:01

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
96.5k
#define zend_set_str_gc_flags(str) do { \
37
96.5k
  GC_SET_REFCOUNT(str, 2); \
38
96.5k
  uint32_t flags = GC_STRING | (ZSTR_IS_VALID_UTF8(str) ? IS_STR_VALID_UTF8 : 0); \
39
96.5k
  if (file_cache_only \
40
96.5k
   || (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
96.5k
  } else { \
44
96.5k
    flags |= ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \
45
96.5k
  } \
46
96.5k
  GC_TYPE_INFO(str) = flags; \
47
96.5k
} while (0)
48
49
171k
#define zend_accel_store_string(str) do { \
50
171k
    zend_string *new_str = zend_shared_alloc_get_xlat_entry(str); \
51
171k
    if (new_str) { \
52
74.5k
      zend_string_release_ex(str, 0); \
53
74.5k
      str = new_str; \
54
96.5k
    } else { \
55
96.5k
      new_str = zend_shared_memdup_put((void*)str, _ZSTR_STRUCT_SIZE(ZSTR_LEN(str))); \
56
96.5k
      zend_string_release_ex(str, 0); \
57
96.5k
      str = new_str; \
58
96.5k
      zend_string_hash_val(str); \
59
96.5k
      zend_set_str_gc_flags(str); \
60
96.5k
    } \
61
171k
  } 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
1.26M
#define zend_accel_store_interned_string(str) do { \
74
1.26M
    if (!IS_ACCEL_INTERNED(str)) { \
75
51.8k
      zend_accel_store_string(str); \
76
51.8k
    } \
77
1.26M
  } 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
207k
{
94
207k
  uint32_t idx, nIndex;
95
207k
  Bucket *p;
96
97
207k
  HT_FLAGS(ht) |= HASH_FLAG_STATIC_KEYS;
98
207k
  ht->pDestructor = NULL;
99
207k
  ht->nInternalPointer = 0;
100
101
207k
  if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
102
130k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
103
130k
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
104
130k
    } else {
105
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
106
0
    }
107
130k
    return;
108
130k
  }
109
76.9k
  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
76.9k
  if (HT_IS_PACKED(ht)) {
121
18.5k
    void *data = HT_GET_DATA_ADDR(ht);
122
18.5k
    if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
123
0
      data = zend_shared_memdup(data, HT_PACKED_USED_SIZE(ht));
124
18.5k
    } else {
125
18.5k
      data = zend_shared_memdup_free(data, HT_PACKED_USED_SIZE(ht));
126
18.5k
    }
127
18.5k
    HT_SET_DATA_ADDR(ht, data);
128
58.3k
  } else if (ht->nNumUsed > HT_MIN_SIZE && ht->nNumUsed < (uint32_t)(-(int32_t)ht->nTableMask) / 4) {
129
    /* compact table */
130
12
    void *old_data = HT_GET_DATA_ADDR(ht);
131
12
    Bucket *old_buckets = ht->arData;
132
12
    uint32_t hash_size;
133
134
12
    hash_size = (uint32_t)(-(int32_t)ht->nTableMask);
135
24
    while (hash_size >> 2 > ht->nNumUsed) {
136
12
      hash_size >>= 1;
137
12
    }
138
12
    ht->nTableMask = (uint32_t)(-(int32_t)hash_size);
139
12
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
140
12
    HT_SET_DATA_ADDR(ht, ZCG(mem));
141
12
    ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE((hash_size * sizeof(uint32_t)) + (ht->nNumUsed * sizeof(Bucket))));
142
12
    HT_HASH_RESET(ht);
143
12
    memcpy(ht->arData, old_buckets, ht->nNumUsed * sizeof(Bucket));
144
12
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
145
12
      efree(old_data);
146
12
    }
147
148
    /* rehash */
149
192
    for (idx = 0; idx < ht->nNumUsed; idx++) {
150
180
      p = ht->arData + idx;
151
180
      if (Z_TYPE(p->val) == IS_UNDEF) continue;
152
180
      nIndex = p->h | ht->nTableMask;
153
180
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
154
180
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
155
180
    }
156
58.3k
  } else {
157
58.3k
    void *data = ZCG(mem);
158
58.3k
    void *old_data = HT_GET_DATA_ADDR(ht);
159
160
58.3k
    ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
161
58.3k
    ZCG(mem) = (void*)((char*)data + ZEND_ALIGNED_SIZE(HT_USED_SIZE(ht)));
162
58.3k
    memcpy(data, old_data, HT_USED_SIZE(ht));
163
58.3k
    if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
164
58.3k
      efree(old_data);
165
58.3k
    }
166
58.3k
    HT_SET_DATA_ADDR(ht, data);
167
58.3k
  }
168
76.9k
}
169
170
static zend_ast *zend_persist_ast(zend_ast *ast)
171
17.8k
{
172
17.8k
  uint32_t i;
173
17.8k
  zend_ast *node;
174
175
17.8k
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
176
10.9k
    zend_ast_zval *copy = zend_shared_memdup(ast, sizeof(zend_ast_zval));
177
10.9k
    zend_persist_zval(&copy->val);
178
10.9k
    node = (zend_ast *) copy;
179
10.9k
  } else if (zend_ast_is_list(ast)) {
180
1.02k
    zend_ast_list *list = zend_ast_get_list(ast);
181
1.02k
    zend_ast_list *copy = zend_shared_memdup(ast,
182
1.02k
      sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * list->children);
183
2.21k
    for (i = 0; i < list->children; i++) {
184
1.19k
      if (copy->child[i]) {
185
1.19k
        copy->child[i] = zend_persist_ast(copy->child[i]);
186
1.19k
      }
187
1.19k
    }
188
1.02k
    node = (zend_ast *) copy;
189
5.84k
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
190
70
    zend_ast_op_array *copy = zend_shared_memdup(ast, sizeof(zend_ast_op_array));
191
70
    zval z;
192
70
    ZVAL_PTR(&z, copy->op_array);
193
70
    zend_persist_op_array(&z);
194
70
    copy->op_array = Z_PTR(z);
195
70
    node = (zend_ast *) copy;
196
5.77k
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
197
188
    zend_ast_fcc *copy = zend_shared_memdup(ast, sizeof(zend_ast_fcc));
198
188
    copy->args = zend_persist_ast(copy->args);
199
188
    node = (zend_ast *) copy;
200
5.59k
  } else if (zend_ast_is_decl(ast)) {
201
    /* Not implemented. */
202
0
    ZEND_UNREACHABLE();
203
5.59k
  } else {
204
5.59k
    uint32_t children = zend_ast_get_num_children(ast);
205
5.59k
    node = zend_shared_memdup(ast, zend_ast_size(children));
206
19.1k
    for (i = 0; i < children; i++) {
207
13.5k
      if (node->child[i]) {
208
12.1k
        node->child[i] = zend_persist_ast(node->child[i]);
209
12.1k
      }
210
13.5k
    }
211
5.59k
  }
212
213
17.8k
  return node;
214
17.8k
}
215
216
static void zend_persist_zval(zval *z)
217
1.02M
{
218
1.02M
  void *new_ptr;
219
220
1.02M
  switch (Z_TYPE_P(z)) {
221
621k
    case IS_STRING:
222
621k
      zend_accel_store_interned_string(Z_STR_P(z));
223
621k
      Z_TYPE_FLAGS_P(z) = 0;
224
621k
      break;
225
22.6k
    case IS_ARRAY:
226
22.6k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_ARR_P(z));
227
22.6k
      if (new_ptr) {
228
532
        Z_ARR_P(z) = new_ptr;
229
532
        Z_TYPE_FLAGS_P(z) = 0;
230
22.1k
      } else if (!ZCG(current_persistent_script)->corrupted
231
22.1k
       && zend_accel_in_shm(Z_ARR_P(z))) {
232
        /* pass */
233
22.0k
      } else {
234
22.0k
        HashTable *ht;
235
236
22.0k
        if (!Z_REFCOUNTED_P(z)) {
237
3.48k
          ht = zend_shared_memdup_put(Z_ARR_P(z), sizeof(zend_array));
238
18.5k
        } else {
239
18.5k
          GC_REMOVE_FROM_BUFFER(Z_ARR_P(z));
240
18.5k
          ht = zend_shared_memdup_put_free(Z_ARR_P(z), sizeof(zend_array));
241
18.5k
        }
242
22.0k
        Z_ARR_P(z) = ht;
243
22.0k
        zend_hash_persist(ht);
244
22.0k
        if (HT_IS_PACKED(ht)) {
245
15.9k
          zval *zv;
246
247
476k
          ZEND_HASH_PACKED_FOREACH_VAL(ht, zv) {
248
476k
            zend_persist_zval(zv);
249
476k
          } ZEND_HASH_FOREACH_END();
250
15.9k
        } else {
251
6.08k
          Bucket *p;
252
253
29.5k
          ZEND_HASH_MAP_FOREACH_BUCKET(ht, p) {
254
29.5k
            if (p->key) {
255
3.83k
              zend_accel_store_interned_string(p->key);
256
3.83k
            }
257
29.5k
            zend_persist_zval(&p->val);
258
29.5k
          } ZEND_HASH_FOREACH_END();
259
6.08k
        }
260
        /* make immutable array */
261
22.0k
        Z_TYPE_FLAGS_P(z) = 0;
262
22.0k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 2);
263
22.0k
        GC_ADD_FLAGS(Z_COUNTED_P(z), IS_ARRAY_IMMUTABLE);
264
22.0k
      }
265
22.6k
      break;
266
22.6k
    case IS_CONSTANT_AST:
267
4.51k
      new_ptr = zend_shared_alloc_get_xlat_entry(Z_AST_P(z));
268
4.51k
      if (new_ptr) {
269
38
        Z_AST_P(z) = new_ptr;
270
38
        Z_TYPE_FLAGS_P(z) = 0;
271
4.47k
      } else if (ZCG(current_persistent_script)->corrupted
272
4.47k
       || !zend_accel_in_shm(Z_AST_P(z))) {
273
4.36k
        zend_ast_ref *old_ref = Z_AST_P(z);
274
4.36k
        Z_AST_P(z) = zend_shared_memdup_put(Z_AST_P(z), sizeof(zend_ast_ref));
275
4.36k
        zend_persist_ast(GC_AST(old_ref));
276
4.36k
        Z_TYPE_FLAGS_P(z) = 0;
277
4.36k
        GC_SET_REFCOUNT(Z_COUNTED_P(z), 1);
278
4.36k
        GC_ADD_FLAGS(Z_COUNTED_P(z), GC_IMMUTABLE);
279
4.36k
        efree(old_ref);
280
4.36k
      }
281
4.51k
      break;
282
198
    case IS_PTR:
283
198
      break;
284
378k
    default:
285
378k
      ZEND_ASSERT(Z_TYPE_P(z) < IS_STRING);
286
378k
      break;
287
1.02M
  }
288
1.02M
}
289
290
static HashTable *zend_persist_attributes(HashTable *attributes)
291
2.69k
{
292
2.69k
  uint32_t i;
293
2.69k
  zval *v;
294
295
2.69k
  if (!ZCG(current_persistent_script)->corrupted && zend_accel_in_shm(attributes)) {
296
32
    return attributes;
297
32
  }
298
299
  /* Attributes for trait properties may be shared if preloading is used. */
300
2.66k
  HashTable *xlat = zend_shared_alloc_get_xlat_entry(attributes);
301
2.66k
  if (xlat) {
302
0
    return xlat;
303
0
  }
304
305
2.66k
  zend_hash_persist(attributes);
306
307
12.5k
  ZEND_HASH_PACKED_FOREACH_VAL(attributes, v) {
308
12.5k
    zend_attribute *attr = Z_PTR_P(v);
309
12.5k
    zend_attribute *copy = zend_shared_memdup_put_free(attr, ZEND_ATTRIBUTE_SIZE(attr->argc));
310
311
12.5k
    zend_accel_store_interned_string(copy->name);
312
12.5k
    zend_accel_store_interned_string(copy->lcname);
313
12.5k
    if (copy->validation_error) {
314
84
      zend_accel_store_interned_string(copy->validation_error);
315
84
    }
316
317
12.5k
    for (i = 0; i < copy->argc; i++) {
318
1.27k
      if (copy->args[i].name) {
319
166
        zend_accel_store_interned_string(copy->args[i].name);
320
166
      }
321
1.27k
      zend_persist_zval(&copy->args[i].value);
322
1.27k
    }
323
324
12.5k
    ZVAL_PTR(v, copy);
325
12.5k
  } ZEND_HASH_FOREACH_END();
326
327
2.66k
  HashTable *ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable));
328
2.66k
  GC_SET_REFCOUNT(ptr, 2);
329
2.66k
  GC_TYPE_INFO(ptr) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
330
331
2.66k
  return ptr;
332
2.66k
}
333
334
uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name)
335
30.2k
{
336
30.2k
  uint32_t ret;
337
338
30.2k
  if (zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_SELF)) ||
339
30.1k
      zend_string_equals_ci(type_name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
340
124
    return 0;
341
124
  }
342
343
  /* We use type.name.gc.refcount to keep map_ptr of corresponding type */
344
30.1k
  if (ZSTR_HAS_CE_CACHE(type_name)) {
345
28.2k
    return GC_REFCOUNT(type_name);
346
28.2k
  }
347
348
1.87k
  if ((GC_FLAGS(type_name) & GC_IMMUTABLE)
349
1.87k
   && (GC_FLAGS(type_name) & IS_STR_PERMANENT)) {
350
1.87k
    do {
351
1.87k
      ret = ZEND_MAP_PTR_NEW_OFFSET();
352
1.87k
    } while (ret <= 2);
353
1.87k
    GC_SET_REFCOUNT(type_name, ret);
354
1.87k
    GC_ADD_FLAGS(type_name, IS_STR_CLASS_NAME_MAP_PTR);
355
1.87k
    return ret;
356
1.87k
  }
357
358
0
  return 0;
359
1.87k
}
360
361
56.4k
static void zend_persist_type(zend_type *type) {
362
56.4k
  if (ZEND_TYPE_HAS_LIST(*type)) {
363
1.80k
    zend_type_list *list = ZEND_TYPE_LIST(*type);
364
1.80k
    if (ZEND_TYPE_USES_ARENA(*type) || zend_accel_in_shm(list)) {
365
1.80k
      list = zend_shared_memdup_put(list, ZEND_TYPE_LIST_SIZE(list->num_types));
366
1.80k
      ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
367
1.80k
    } else {
368
0
      list = zend_shared_memdup_put_free(list, ZEND_TYPE_LIST_SIZE(list->num_types));
369
0
    }
370
1.80k
    ZEND_TYPE_SET_PTR(*type, list);
371
1.80k
  }
372
373
56.4k
  zend_type *single_type;
374
114k
  ZEND_TYPE_FOREACH_MUTABLE(*type, single_type) {
375
114k
    if (ZEND_TYPE_HAS_LIST(*single_type)) {
376
514
      zend_persist_type(single_type);
377
514
      continue;
378
514
    }
379
57.6k
    if (ZEND_TYPE_HAS_NAME(*single_type)) {
380
7.22k
      zend_string *type_name = ZEND_TYPE_NAME(*single_type);
381
7.22k
      zend_accel_store_interned_string(type_name);
382
7.22k
      ZEND_TYPE_SET_PTR(*single_type, type_name);
383
7.22k
      if (!ZCG(current_persistent_script)->corrupted) {
384
7.22k
        zend_accel_get_class_name_map_ptr(type_name);
385
7.22k
      }
386
7.22k
    }
387
57.6k
  } ZEND_TYPE_FOREACH_END();
388
56.4k
}
389
390
static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_script* main_persistent_script)
391
100k
{
392
100k
  zend_op *persist_ptr;
393
100k
  zval *orig_literals = NULL;
394
395
100k
  if (op_array->refcount && --(*op_array->refcount) == 0) {
396
93.8k
    efree(op_array->refcount);
397
93.8k
  }
398
100k
  op_array->refcount = NULL;
399
400
100k
  if (main_persistent_script) {
401
50.2k
    zend_execute_data *orig_execute_data = EG(current_execute_data);
402
50.2k
    zend_execute_data fake_execute_data;
403
50.2k
    zval *offset;
404
405
50.2k
    memset(&fake_execute_data, 0, sizeof(fake_execute_data));
406
50.2k
    fake_execute_data.func = (zend_function*)op_array;
407
50.2k
    EG(current_execute_data) = &fake_execute_data;
408
50.2k
    if ((offset = zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__") - 1)) != NULL) {
409
54
      main_persistent_script->compiler_halt_offset = Z_LVAL_P(offset);
410
54
    }
411
50.2k
    EG(current_execute_data) = orig_execute_data;
412
50.2k
  }
413
414
100k
  if (op_array->function_name) {
415
49.9k
    zend_string *old_name = op_array->function_name;
416
49.9k
    zend_accel_store_interned_string(op_array->function_name);
417
    /* Remember old function name, so it can be released multiple times if shared. */
418
49.9k
    if (op_array->function_name != old_name
419
898
        && !zend_shared_alloc_get_xlat_entry(&op_array->function_name)) {
420
898
      zend_shared_alloc_register_xlat_entry(&op_array->function_name, old_name);
421
898
    }
422
49.9k
  }
423
424
100k
  if (op_array->scope) {
425
27.9k
    zend_class_entry *scope = zend_shared_alloc_get_xlat_entry(op_array->scope);
426
427
27.9k
    if (scope) {
428
27.9k
      op_array->scope = scope;
429
27.9k
    }
430
431
27.9k
    if (op_array->prototype) {
432
3.66k
      zend_function *ptr = zend_shared_alloc_get_xlat_entry(op_array->prototype);
433
434
3.66k
      if (ptr) {
435
869
        op_array->prototype = ptr;
436
869
      }
437
3.66k
    }
438
439
27.9k
    persist_ptr = zend_shared_alloc_get_xlat_entry(op_array->opcodes);
440
27.9k
    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
72.2k
  } else {
499
    /* "prototype" may be undefined if "scope" isn't set */
500
72.2k
    op_array->prototype = NULL;
501
72.2k
  }
502
503
100k
  if (op_array->scope
504
27.9k
   && !(op_array->fn_flags & ZEND_ACC_CLOSURE)
505
27.9k
   && (op_array->scope->ce_flags & ZEND_ACC_CACHED)) {
506
4.74k
    return;
507
4.74k
  }
508
509
95.4k
  if (op_array->static_variables && !zend_accel_in_shm(op_array->static_variables)) {
510
2.47k
    Bucket *p;
511
512
2.47k
    zend_hash_persist(op_array->static_variables);
513
22.3k
    ZEND_HASH_MAP_FOREACH_BUCKET(op_array->static_variables, p) {
514
22.3k
      ZEND_ASSERT(p->key != NULL);
515
22.3k
      zend_accel_store_interned_string(p->key);
516
8.70k
      zend_persist_zval(&p->val);
517
8.70k
    } ZEND_HASH_FOREACH_END();
518
2.47k
    op_array->static_variables = zend_shared_memdup_put_free(op_array->static_variables, sizeof(HashTable));
519
    /* make immutable array */
520
2.47k
    GC_SET_REFCOUNT(op_array->static_variables, 2);
521
2.47k
    GC_TYPE_INFO(op_array->static_variables) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT);
522
2.47k
  }
523
524
95.4k
  if (op_array->literals) {
525
93.5k
    zval *p, *end;
526
527
93.5k
    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
93.5k
    p = zend_shared_memdup_put(op_array->literals, sizeof(zval) * op_array->last_literal);
532
93.5k
#endif
533
93.5k
    end = p + op_array->last_literal;
534
93.5k
    op_array->literals = p;
535
846k
    while (p < end) {
536
752k
      zend_persist_zval(p);
537
752k
      p++;
538
752k
    }
539
93.5k
  }
540
541
95.4k
  {
542
95.4k
    zend_op *new_opcodes = zend_shared_memdup_put(op_array->opcodes, sizeof(zend_op) * op_array->last);
543
95.4k
    zend_op *opline = new_opcodes;
544
95.4k
    zend_op *end = new_opcodes + op_array->last;
545
546
2.35M
    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
2.26M
      if (opline->op1_type == IS_CONST) {
562
400k
        opline->op1.constant =
563
400k
          (char*)(op_array->literals +
564
400k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
565
400k
            (int32_t)opline->op1.constant) - orig_literals)) -
566
400k
          (char*)opline;
567
400k
        if (opline->opcode == ZEND_SEND_VAL
568
349k
         || opline->opcode == ZEND_SEND_VAL_EX
569
339k
         || opline->opcode == ZEND_QM_ASSIGN) {
570
69.5k
          zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
571
69.5k
        }
572
400k
      }
573
2.26M
      if (opline->op2_type == IS_CONST) {
574
583k
        opline->op2.constant =
575
583k
          (char*)(op_array->literals +
576
583k
            ((zval*)((char*)(op_array->opcodes + (opline - new_opcodes)) +
577
583k
            (int32_t)opline->op2.constant) - orig_literals)) -
578
583k
          (char*)opline;
579
583k
      }
580
2.26M
#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
2.26M
      if (opline->opcode == ZEND_OP_DATA && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST) {
619
198
        zval *literal = RT_CONSTANT(opline, opline->op1);
620
198
        HashTable *attributes = Z_PTR_P(literal);
621
198
        attributes = zend_persist_attributes(attributes);
622
198
        ZVAL_PTR(literal, attributes);
623
198
      }
624
2.26M
    }
625
626
95.4k
    efree(op_array->opcodes);
627
95.4k
    op_array->opcodes = new_opcodes;
628
95.4k
  }
629
630
95.4k
  if (op_array->filename) {
631
95.4k
    zend_accel_store_string(op_array->filename);
632
95.4k
  }
633
634
95.4k
  if (op_array->arg_info) {
635
23.5k
    zend_arg_info *arg_info = op_array->arg_info;
636
23.5k
    uint32_t num_args = op_array->num_args;
637
23.5k
    uint32_t i;
638
639
23.5k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
640
7.69k
      arg_info--;
641
7.69k
      num_args++;
642
7.69k
    }
643
23.5k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
644
394
      num_args++;
645
394
    }
646
23.5k
    arg_info = zend_shared_memdup_put_free(arg_info, sizeof(zend_arg_info) * num_args);
647
58.9k
    for (i = 0; i < num_args; i++) {
648
35.4k
      if (arg_info[i].name) {
649
27.7k
        zend_accel_store_interned_string(arg_info[i].name);
650
27.7k
      }
651
35.4k
      zend_persist_type(&arg_info[i].type);
652
35.4k
      if (arg_info[i].doc_comment) {
653
0
        zend_accel_store_interned_string(arg_info[i].doc_comment);
654
0
      }
655
35.4k
    }
656
23.5k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
657
7.69k
      arg_info++;
658
7.69k
    }
659
23.5k
    op_array->arg_info = arg_info;
660
23.5k
  }
661
662
95.4k
  if (op_array->live_range) {
663
46.7k
    op_array->live_range = zend_shared_memdup_put_free(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
664
46.7k
  }
665
666
95.4k
  if (op_array->doc_comment) {
667
44
    if (ZCG(accel_directives).save_comments) {
668
44
      zend_accel_store_interned_string(op_array->doc_comment);
669
44
    } else {
670
0
      zend_string_release_ex(op_array->doc_comment, 0);
671
0
      op_array->doc_comment = NULL;
672
0
    }
673
44
  }
674
675
95.4k
  if (op_array->attributes) {
676
934
    op_array->attributes = zend_persist_attributes(op_array->attributes);
677
934
  }
678
679
95.4k
  if (op_array->try_catch_array) {
680
18.9k
    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
18.9k
  }
682
683
95.4k
  if (op_array->vars) {
684
64.6k
    int i;
685
64.6k
    op_array->vars = zend_shared_memdup_put_free(op_array->vars, sizeof(zend_string*) * op_array->last_var);
686
396k
    for (i = 0; i < op_array->last_var; i++) {
687
332k
      zend_accel_store_interned_string(op_array->vars[i]);
688
332k
    }
689
64.6k
  }
690
691
95.4k
  if (op_array->num_dynamic_func_defs) {
692
5.10k
    op_array->dynamic_func_defs = zend_shared_memdup_put_free(
693
5.10k
      op_array->dynamic_func_defs, sizeof(zend_function *) * op_array->num_dynamic_func_defs);
694
12.9k
    for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
695
7.83k
      zval tmp;
696
7.83k
      ZVAL_PTR(&tmp, op_array->dynamic_func_defs[i]);
697
7.83k
      zend_persist_op_array(&tmp);
698
7.83k
      op_array->dynamic_func_defs[i] = Z_PTR(tmp);
699
7.83k
    }
700
5.10k
  }
701
702
95.4k
  ZCG(mem) = (void*)((char*)ZCG(mem) + ZEND_ALIGNED_SIZE(zend_extensions_op_array_persist(op_array, ZCG(mem))));
703
95.4k
}
704
705
static void zend_persist_op_array(zval *zv)
706
21.9k
{
707
21.9k
  zend_op_array *op_array = Z_PTR_P(zv);
708
21.9k
  zend_op_array *old_op_array;
709
21.9k
  ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
710
711
21.9k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
712
21.9k
  if (!old_op_array) {
713
21.9k
    op_array = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_op_array));
714
21.9k
    zend_persist_op_array_ex(op_array, NULL);
715
21.9k
    if (!ZCG(current_persistent_script)->corrupted) {
716
21.9k
      op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
717
21.9k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
718
21.9k
      if (op_array->static_variables) {
719
2.26k
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
720
2.26k
      }
721
21.9k
    }
722
21.9k
#ifdef HAVE_JIT
723
21.9k
    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
21.9k
#endif
730
21.9k
  } 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
21.9k
}
735
736
static zend_op_array *zend_persist_class_method(zend_op_array *op_array, const zend_class_entry *ce)
737
34.5k
{
738
34.5k
  zend_op_array *old_op_array;
739
740
34.5k
  if (op_array->type != ZEND_USER_FUNCTION) {
741
4.24k
    ZEND_ASSERT(op_array->type == ZEND_INTERNAL_FUNCTION);
742
4.24k
    if (op_array->fn_flags & ZEND_ACC_ARENA_ALLOCATED) {
743
4.24k
      old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
744
4.24k
      if (old_op_array) {
745
0
        return old_op_array;
746
4.24k
      } else {
747
4.24k
        op_array = zend_shared_memdup_put(op_array, sizeof(zend_internal_function));
748
4.24k
        if (op_array->scope) {
749
4.24k
          void *persist_ptr;
750
751
4.24k
          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
4.24k
          if (op_array->prototype) {
755
1.80k
            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.80k
          }
759
4.24k
        }
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
4.24k
        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
4.24k
      }
770
4.24k
    }
771
4.24k
    return op_array;
772
4.24k
  }
773
774
30.3k
  if ((op_array->fn_flags & ZEND_ACC_IMMUTABLE)
775
500
   && !ZCG(current_persistent_script)->corrupted
776
500
   && zend_accel_in_shm(op_array)) {
777
492
    zend_shared_alloc_register_xlat_entry(op_array, op_array);
778
492
    return op_array;
779
492
  }
780
781
29.8k
  old_op_array = zend_shared_alloc_get_xlat_entry(op_array);
782
29.8k
  if (old_op_array) {
783
1.88k
    if (op_array->refcount && --(*op_array->refcount) == 0) {
784
1.61k
      efree(op_array->refcount);
785
1.61k
    }
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.88k
    zend_string *old_function_name =
790
1.88k
      zend_shared_alloc_get_xlat_entry(&old_op_array->function_name);
791
1.88k
    if (old_function_name) {
792
0
      zend_string_release_ex(old_function_name, 0);
793
0
    }
794
1.88k
    return old_op_array;
795
1.88k
  }
796
797
27.9k
  op_array = zend_shared_memdup_put(op_array, sizeof(zend_op_array));
798
27.9k
  zend_persist_op_array_ex(op_array, NULL);
799
27.9k
  if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
800
27.9k
    op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
801
27.9k
    if (ce->ce_flags & ZEND_ACC_LINKED) {
802
23.6k
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
803
23.6k
      if (op_array->static_variables) {
804
207
        ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
805
207
      }
806
23.6k
    } else {
807
4.33k
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
808
4.33k
      ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
809
4.33k
    }
810
27.9k
  }
811
27.9k
  return op_array;
812
29.8k
}
813
814
static zend_property_info *zend_persist_property_info(zend_property_info *prop)
815
16.1k
{
816
16.1k
  zend_class_entry *ce;
817
16.1k
  prop = zend_shared_memdup_put(prop, sizeof(zend_property_info));
818
16.1k
  ce = zend_shared_alloc_get_xlat_entry(prop->ce);
819
16.1k
  if (ce) {
820
16.1k
    prop->ce = ce;
821
16.1k
  }
822
16.1k
  zend_accel_store_interned_string(prop->name);
823
16.1k
  if (prop->doc_comment) {
824
284
    if (ZCG(accel_directives).save_comments) {
825
284
      zend_accel_store_interned_string(prop->doc_comment);
826
284
    } 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
284
  }
834
16.1k
  if (prop->attributes) {
835
186
    prop->attributes = zend_persist_attributes(prop->attributes);
836
186
  }
837
16.1k
  if (prop->prototype) {
838
16.1k
    const zend_property_info *new_prototype = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(prop->prototype);
839
16.1k
    if (new_prototype) {
840
16.0k
      prop->prototype = new_prototype;
841
16.0k
    }
842
16.1k
  }
843
16.1k
  if (prop->hooks) {
844
1.67k
    prop->hooks = zend_shared_memdup_put(prop->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
845
5.02k
    for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
846
3.35k
      if (prop->hooks[i]) {
847
2.18k
        zend_op_array *hook = zend_persist_class_method(&prop->hooks[i]->op_array, ce);
848
2.18k
#ifdef HAVE_JIT
849
2.18k
        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
2.18k
#endif
858
2.18k
        const zend_property_info *new_prop_info = (const zend_property_info *) zend_shared_alloc_get_xlat_entry(hook->prop_info);
859
2.18k
        if (new_prop_info) {
860
2.13k
          hook->prop_info = new_prop_info;
861
2.13k
        }
862
2.18k
        prop->hooks[i] = (zend_function *) hook;
863
2.18k
      }
864
3.35k
    }
865
1.67k
  }
866
16.1k
  zend_persist_type(&prop->type);
867
16.1k
  return prop;
868
16.1k
}
869
870
static void zend_persist_class_constant(zval *zv)
871
5.58k
{
872
5.58k
  const zend_class_constant *orig_c = Z_PTR_P(zv);
873
5.58k
  zend_class_constant *c = zend_shared_alloc_get_xlat_entry(orig_c);
874
5.58k
  zend_class_entry *ce;
875
876
5.58k
  if (c) {
877
182
    Z_PTR_P(zv) = c;
878
182
    return;
879
5.40k
  } else if (((orig_c->ce->ce_flags & ZEND_ACC_IMMUTABLE) && !(Z_CONSTANT_FLAGS(orig_c->value) & CONST_OWNED))
880
5.27k
   || 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.00k
    return;
883
4.40k
  } else if (!ZCG(current_persistent_script)->corrupted
884
4.40k
   && zend_accel_in_shm(Z_PTR_P(zv))) {
885
0
    return;
886
0
  }
887
4.40k
  c = Z_PTR_P(zv) = zend_shared_memdup_put(Z_PTR_P(zv), sizeof(zend_class_constant));
888
4.40k
  zend_persist_zval(&c->value);
889
4.40k
  ce = zend_shared_alloc_get_xlat_entry(c->ce);
890
4.40k
  if (ce) {
891
4.38k
    c->ce = ce;
892
4.38k
  }
893
4.40k
  if (c->doc_comment) {
894
28
    if (ZCG(accel_directives).save_comments) {
895
28
      zend_string *doc_comment = zend_shared_alloc_get_xlat_entry(c->doc_comment);
896
28
      if (doc_comment) {
897
0
        c->doc_comment = doc_comment;
898
28
      } else {
899
28
        zend_accel_store_interned_string(c->doc_comment);
900
28
      }
901
28
    } 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
28
  }
910
4.40k
  if (c->attributes) {
911
174
    c->attributes = zend_persist_attributes(c->attributes);
912
174
  }
913
4.40k
  zend_persist_type(&c->type);
914
4.40k
}
915
916
zend_class_entry *zend_persist_class_entry(zend_class_entry *orig_ce)
917
26.7k
{
918
26.7k
  Bucket *p;
919
26.7k
  zend_class_entry *ce = orig_ce;
920
921
26.7k
  if (ce->type == ZEND_USER_CLASS) {
922
    /* The same zend_class_entry may be reused by class_alias */
923
26.7k
    zend_class_entry *new_ce = zend_shared_alloc_get_xlat_entry(ce);
924
26.7k
    if (new_ce) {
925
0
      return new_ce;
926
0
    }
927
26.7k
    ce = zend_shared_memdup_put(ce, sizeof(zend_class_entry));
928
26.7k
    if (EXPECTED(!ZCG(current_persistent_script)->corrupted)) {
929
26.7k
      ce->ce_flags |= ZEND_ACC_IMMUTABLE;
930
26.7k
      if ((ce->ce_flags & ZEND_ACC_LINKED)
931
21.7k
       && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
932
648
        ZEND_MAP_PTR_NEW(ce->mutable_data);
933
26.0k
      } else {
934
26.0k
        ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
935
26.0k
      }
936
26.7k
    } else {
937
0
      ce->ce_flags |= ZEND_ACC_FILE_CACHED;
938
0
    }
939
26.7k
    ce->inheritance_cache = NULL;
940
941
26.7k
    if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
942
23.6k
      if (ZSTR_HAS_CE_CACHE(ce->name)) {
943
21.5k
        ZSTR_SET_CE_CACHE_EX(ce->name, NULL, 0);
944
21.5k
      }
945
23.6k
      zend_accel_store_interned_string(ce->name);
946
23.6k
      if (!(ce->ce_flags & ZEND_ACC_ANON_CLASS)
947
23.0k
       && !ZCG(current_persistent_script)->corrupted) {
948
23.0k
        zend_accel_get_class_name_map_ptr(ce->name);
949
23.0k
      }
950
23.6k
      if (ce->parent_name && !(ce->ce_flags & ZEND_ACC_LINKED)) {
951
1.19k
        zend_accel_store_interned_string(ce->parent_name);
952
1.19k
      }
953
23.6k
    }
954
955
26.7k
    zend_hash_persist(&ce->function_table);
956
118k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->function_table, p) {
957
118k
      ZEND_ASSERT(p->key != NULL);
958
118k
      zend_accel_store_interned_string(p->key);
959
32.4k
      Z_PTR(p->val) = zend_persist_class_method(Z_PTR(p->val), ce);
960
32.4k
    } ZEND_HASH_FOREACH_END();
961
26.7k
    HT_FLAGS(&ce->function_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
962
26.7k
    if (ce->default_properties_table) {
963
9.23k
        int i;
964
965
9.23k
      ce->default_properties_table = zend_shared_memdup_free(ce->default_properties_table, sizeof(zval) * ce->default_properties_count);
966
24.4k
      for (i = 0; i < ce->default_properties_count; i++) {
967
15.1k
        zend_persist_zval(&ce->default_properties_table[i]);
968
15.1k
      }
969
9.23k
    }
970
26.7k
    if (ce->default_static_members_table) {
971
1.63k
      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
4.58k
      for (uint32_t i = 0; i < ce->default_static_members_count; i++) {
976
2.94k
        if (Z_TYPE(ce->default_static_members_table[i]) != IS_INDIRECT) {
977
2.58k
          zend_persist_zval(&ce->default_static_members_table[i]);
978
2.58k
        }
979
2.94k
      }
980
1.63k
      if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
981
1.63k
        if (ce->ce_flags & ZEND_ACC_LINKED) {
982
1.58k
          ZEND_MAP_PTR_NEW(ce->static_members_table);
983
1.58k
        } else {
984
54
          ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
985
54
        }
986
1.63k
      }
987
1.63k
    }
988
989
26.7k
    zend_hash_persist(&ce->constants_table);
990
64.6k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->constants_table, p) {
991
64.6k
      ZEND_ASSERT(p->key != NULL);
992
64.6k
      zend_accel_store_interned_string(p->key);
993
5.58k
      zend_persist_class_constant(&p->val);
994
5.58k
    } ZEND_HASH_FOREACH_END();
995
26.7k
    HT_FLAGS(&ce->constants_table) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
996
997
26.7k
    zend_hash_persist(&ce->properties_info);
998
91.0k
    ZEND_HASH_MAP_FOREACH_BUCKET(&ce->properties_info, p) {
999
91.0k
      zend_property_info *prop = Z_PTR(p->val);
1000
91.0k
      ZEND_ASSERT(p->key != NULL);
1001
91.0k
      zend_accel_store_interned_string(p->key);
1002
18.7k
      if (prop->ce == orig_ce) {
1003
16.1k
        Z_PTR(p->val) = zend_persist_property_info(prop);
1004
16.1k
      } else {
1005
2.67k
        prop = zend_shared_alloc_get_xlat_entry(prop);
1006
2.67k
        if (prop) {
1007
1.24k
          Z_PTR(p->val) = prop;
1008
1.43k
        } 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.43k
        }
1013
2.67k
      }
1014
18.7k
    } ZEND_HASH_FOREACH_END();
1015
26.7k
    HT_FLAGS(&ce->properties_info) &= (HASH_FLAG_UNINITIALIZED | HASH_FLAG_STATIC_KEYS);
1016
1017
26.7k
    if (ce->properties_info_table) {
1018
7.81k
      int i;
1019
1020
7.81k
      size_t size = sizeof(zend_property_info *) * ce->default_properties_count;
1021
7.81k
      ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
1022
7.81k
      ce->properties_info_table = zend_shared_memdup(
1023
7.81k
        ce->properties_info_table, size);
1024
1025
21.0k
      for (i = 0; i < ce->default_properties_count; i++) {
1026
13.2k
        if (ce->properties_info_table[i]) {
1027
12.8k
          zend_property_info *prop_info = zend_shared_alloc_get_xlat_entry(
1028
12.8k
            ce->properties_info_table[i]);
1029
12.8k
          if (prop_info) {
1030
11.6k
            ce->properties_info_table[i] = prop_info;
1031
11.6k
          }
1032
12.8k
        }
1033
13.2k
      }
1034
7.81k
    }
1035
1036
26.7k
    if (ce->iterator_funcs_ptr) {
1037
277
      ce->iterator_funcs_ptr = zend_shared_memdup(ce->iterator_funcs_ptr, sizeof(zend_class_iterator_funcs));
1038
277
    }
1039
26.7k
    if (ce->arrayaccess_funcs_ptr) {
1040
333
      ce->arrayaccess_funcs_ptr = zend_shared_memdup(ce->arrayaccess_funcs_ptr, sizeof(zend_class_arrayaccess_funcs));
1041
333
    }
1042
1043
26.7k
    if (ce->ce_flags & ZEND_ACC_CACHED) {
1044
3.04k
      return ce;
1045
3.04k
    }
1046
1047
23.6k
    ce->ce_flags |= ZEND_ACC_CACHED;
1048
1049
23.6k
    if (ce->info.user.filename) {
1050
23.6k
      zend_accel_store_string(ce->info.user.filename);
1051
23.6k
    }
1052
1053
23.6k
    if (ce->doc_comment) {
1054
30
      if (ZCG(accel_directives).save_comments) {
1055
30
        zend_accel_store_interned_string(ce->doc_comment);
1056
30
      } 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
30
    }
1064
1065
23.6k
    if (ce->attributes) {
1066
1.20k
      ce->attributes = zend_persist_attributes(ce->attributes);
1067
1.20k
    }
1068
1069
23.6k
    if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_LINKED)) {
1070
2.95k
      uint32_t i = 0;
1071
1072
6.60k
      for (i = 0; i < ce->num_interfaces; i++) {
1073
3.64k
        zend_accel_store_interned_string(ce->interface_names[i].name);
1074
3.64k
        zend_accel_store_interned_string(ce->interface_names[i].lc_name);
1075
3.64k
      }
1076
2.95k
      ce->interface_names = zend_shared_memdup_free(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
1077
2.95k
    }
1078
1079
23.6k
    if (ce->num_traits) {
1080
1.38k
      uint32_t i = 0;
1081
1082
3.14k
      for (i = 0; i < ce->num_traits; i++) {
1083
1.76k
        zend_accel_store_interned_string(ce->trait_names[i].name);
1084
1.76k
        zend_accel_store_interned_string(ce->trait_names[i].lc_name);
1085
1.76k
      }
1086
1.38k
      ce->trait_names = zend_shared_memdup_free(ce->trait_names, sizeof(zend_class_name) * ce->num_traits);
1087
1088
1.38k
      i = 0;
1089
1.38k
      if (ce->trait_aliases) {
1090
608
        while (ce->trait_aliases[i]) {
1091
394
          if (ce->trait_aliases[i]->trait_method.method_name) {
1092
394
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.method_name);
1093
394
          }
1094
394
          if (ce->trait_aliases[i]->trait_method.class_name) {
1095
168
            zend_accel_store_interned_string(ce->trait_aliases[i]->trait_method.class_name);
1096
168
          }
1097
1098
394
          if (ce->trait_aliases[i]->alias) {
1099
268
            zend_accel_store_interned_string(ce->trait_aliases[i]->alias);
1100
268
          }
1101
1102
394
          ce->trait_aliases[i] = zend_shared_memdup_free(ce->trait_aliases[i], sizeof(zend_trait_alias));
1103
394
          i++;
1104
394
        }
1105
1106
214
        ce->trait_aliases = zend_shared_memdup_free(ce->trait_aliases, sizeof(zend_trait_alias*) * (i + 1));
1107
214
      }
1108
1109
1.38k
      if (ce->trait_precedences) {
1110
84
        uint32_t j;
1111
1112
84
        i = 0;
1113
192
        while (ce->trait_precedences[i]) {
1114
108
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.method_name);
1115
108
          zend_accel_store_interned_string(ce->trait_precedences[i]->trait_method.class_name);
1116
1117
222
          for (j = 0; j < ce->trait_precedences[i]->num_excludes; j++) {
1118
114
            zend_accel_store_interned_string(ce->trait_precedences[i]->exclude_class_names[j]);
1119
114
          }
1120
1121
108
          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
108
          i++;
1123
108
        }
1124
84
        ce->trait_precedences = zend_shared_memdup_free(
1125
84
          ce->trait_precedences, sizeof(zend_trait_precedence*) * (i + 1));
1126
84
      }
1127
1.38k
    }
1128
1129
23.6k
    ZEND_ASSERT(ce->backed_enum_table == NULL);
1130
23.6k
  }
1131
1132
23.6k
  return ce;
1133
26.7k
}
1134
1135
void zend_update_parent_ce(zend_class_entry *ce)
1136
26.7k
{
1137
26.7k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1138
21.7k
    if (ce->parent) {
1139
3.34k
      int i, end;
1140
3.34k
      zend_class_entry *parent = ce->parent;
1141
1142
3.34k
      if (parent->type == ZEND_USER_CLASS) {
1143
2.96k
        zend_class_entry *p = zend_shared_alloc_get_xlat_entry(parent);
1144
1145
2.96k
        if (p) {
1146
2.34k
          ce->parent = parent = p;
1147
2.34k
        }
1148
2.96k
      }
1149
1150
      /* Create indirections to static properties from parent classes */
1151
3.34k
      i = parent->default_static_members_count - 1;
1152
3.60k
      while (parent && parent->default_static_members_table) {
1153
252
        end = parent->parent ? parent->parent->default_static_members_count : 0;
1154
616
        for (; i >= end; i--) {
1155
364
          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
364
          if (Z_TYPE_P(p) == IS_INDIRECT) {
1163
364
            ZVAL_INDIRECT(p, &parent->default_static_members_table[i]);
1164
364
          }
1165
364
        }
1166
1167
252
        parent = parent->parent;
1168
252
      }
1169
3.34k
    }
1170
1171
21.7k
    if (ce->num_interfaces) {
1172
2.07k
      uint32_t i = 0;
1173
1174
2.07k
      ce->interfaces = zend_shared_memdup_free(ce->interfaces, sizeof(zend_class_entry*) * ce->num_interfaces);
1175
5.03k
      for (i = 0; i < ce->num_interfaces; i++) {
1176
2.95k
        if (ce->interfaces[i]->type == ZEND_USER_CLASS) {
1177
1.06k
          zend_class_entry *tmp = zend_shared_alloc_get_xlat_entry(ce->interfaces[i]);
1178
1.06k
          if (tmp != NULL) {
1179
0
            ce->interfaces[i] = tmp;
1180
0
          }
1181
1.06k
        }
1182
2.95k
      }
1183
2.07k
    }
1184
1185
21.7k
    if (ce->iterator_funcs_ptr) {
1186
277
      memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
1187
277
      if (zend_class_implements_interface(ce, zend_ce_aggregate)) {
1188
186
        ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1);
1189
186
      }
1190
277
      if (zend_class_implements_interface(ce, zend_ce_iterator)) {
1191
91
        ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1);
1192
91
        ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1);
1193
91
        ce->iterator_funcs_ptr->zf_key = zend_hash_find_ptr(&ce->function_table, ZSTR_KNOWN(ZEND_STR_KEY));
1194
91
        ce->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&ce->function_table, "current", sizeof("current") - 1);
1195
91
        ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1);
1196
91
      }
1197
277
    }
1198
1199
21.7k
    if (ce->arrayaccess_funcs_ptr) {
1200
333
      ZEND_ASSERT(zend_class_implements_interface(ce, zend_ce_arrayaccess));
1201
333
      ce->arrayaccess_funcs_ptr->zf_offsetget = zend_hash_str_find_ptr(&ce->function_table, "offsetget", sizeof("offsetget") - 1);
1202
333
      ce->arrayaccess_funcs_ptr->zf_offsetexists = zend_hash_str_find_ptr(&ce->function_table, "offsetexists", sizeof("offsetexists") - 1);
1203
333
      ce->arrayaccess_funcs_ptr->zf_offsetset = zend_hash_str_find_ptr(&ce->function_table, "offsetset", sizeof("offsetset") - 1);
1204
333
      ce->arrayaccess_funcs_ptr->zf_offsetunset = zend_hash_str_find_ptr(&ce->function_table, "offsetunset", sizeof("offsetunset") - 1);
1205
333
    }
1206
21.7k
  }
1207
1208
  /* update methods */
1209
26.7k
  if (ce->constructor) {
1210
3.79k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->constructor);
1211
3.79k
    if (tmp != NULL) {
1212
3.61k
      ce->constructor = tmp;
1213
3.61k
    }
1214
3.79k
  }
1215
26.7k
  if (ce->destructor) {
1216
1.65k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->destructor);
1217
1.65k
    if (tmp != NULL) {
1218
1.65k
      ce->destructor = tmp;
1219
1.65k
    }
1220
1.65k
  }
1221
26.7k
  if (ce->clone) {
1222
346
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->clone);
1223
346
    if (tmp != NULL) {
1224
222
      ce->clone = tmp;
1225
222
    }
1226
346
  }
1227
26.7k
  if (ce->__get) {
1228
919
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__get);
1229
919
    if (tmp != NULL) {
1230
919
      ce->__get = tmp;
1231
919
    }
1232
919
  }
1233
26.7k
  if (ce->__set) {
1234
674
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__set);
1235
674
    if (tmp != NULL) {
1236
674
      ce->__set = tmp;
1237
674
    }
1238
674
  }
1239
26.7k
  if (ce->__call) {
1240
630
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__call);
1241
630
    if (tmp != NULL) {
1242
630
      ce->__call = tmp;
1243
630
    }
1244
630
  }
1245
26.7k
  if (ce->__serialize) {
1246
198
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__serialize);
1247
198
    if (tmp != NULL) {
1248
64
      ce->__serialize = tmp;
1249
64
    }
1250
198
  }
1251
26.7k
  if (ce->__unserialize) {
1252
182
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unserialize);
1253
182
    if (tmp != NULL) {
1254
48
      ce->__unserialize = tmp;
1255
48
    }
1256
182
  }
1257
26.7k
  if (ce->__isset) {
1258
340
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__isset);
1259
340
    if (tmp != NULL) {
1260
340
      ce->__isset = tmp;
1261
340
    }
1262
340
  }
1263
26.7k
  if (ce->__unset) {
1264
150
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__unset);
1265
150
    if (tmp != NULL) {
1266
150
      ce->__unset = tmp;
1267
150
    }
1268
150
  }
1269
26.7k
  if (ce->__tostring) {
1270
1.09k
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__tostring);
1271
1.09k
    if (tmp != NULL) {
1272
976
      ce->__tostring = tmp;
1273
976
    }
1274
1.09k
  }
1275
26.7k
  if (ce->__callstatic) {
1276
370
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__callstatic);
1277
370
    if (tmp != NULL) {
1278
370
      ce->__callstatic = tmp;
1279
370
    }
1280
370
  }
1281
26.7k
  if (ce->__debugInfo) {
1282
156
    zend_function *tmp = zend_shared_alloc_get_xlat_entry(ce->__debugInfo);
1283
156
    if (tmp != NULL) {
1284
110
      ce->__debugInfo = tmp;
1285
110
    }
1286
156
  }
1287
26.7k
}
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
50.2k
{
1326
50.2k
  Bucket *p;
1327
50.2k
  zend_class_entry *ce;
1328
50.2k
#ifdef HAVE_JIT
1329
50.2k
  bool orig_jit_on = JIT_G(on);
1330
1331
50.2k
  JIT_G(on) = 0;
1332
50.2k
#endif
1333
50.2k
  zend_hash_persist(class_table);
1334
147k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1335
147k
    ZEND_ASSERT(p->key != NULL);
1336
147k
    zend_accel_store_interned_string(p->key);
1337
23.6k
    Z_CE(p->val) = zend_persist_class_entry(Z_CE(p->val));
1338
23.6k
  } ZEND_HASH_FOREACH_END();
1339
147k
  ZEND_HASH_MAP_FOREACH_BUCKET(class_table, p) {
1340
147k
    if (EXPECTED(Z_TYPE(p->val) != IS_ALIAS_PTR)) {
1341
23.6k
      ce = Z_PTR(p->val);
1342
23.6k
      zend_update_parent_ce(ce);
1343
23.6k
    }
1344
147k
  } ZEND_HASH_FOREACH_END();
1345
50.2k
#ifdef HAVE_JIT
1346
50.2k
  JIT_G(on) = orig_jit_on;
1347
50.2k
  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
50.2k
#endif
1396
50.2k
}
1397
1398
53.2k
zend_error_info **zend_persist_warnings(uint32_t num_warnings, zend_error_info **warnings) {
1399
53.2k
  if (warnings) {
1400
52
    warnings = zend_shared_memdup(warnings, num_warnings * sizeof(zend_error_info *));
1401
106
    for (uint32_t i = 0; i < num_warnings; i++) {
1402
54
      zend_accel_store_string(warnings[i]->filename);
1403
54
      zend_accel_store_string(warnings[i]->message);
1404
54
      warnings[i] = zend_shared_memdup(warnings[i], sizeof(zend_error_info));
1405
54
    }
1406
52
  }
1407
53.2k
  return warnings;
1408
53.2k
}
1409
1410
static zend_early_binding *zend_persist_early_bindings(
1411
50.2k
    uint32_t num_early_bindings, zend_early_binding *early_bindings) {
1412
50.2k
  if (early_bindings) {
1413
477
    early_bindings = zend_shared_memdup_free(
1414
477
      early_bindings, num_early_bindings * sizeof(zend_early_binding));
1415
1.79k
    for (uint32_t i = 0; i < num_early_bindings; i++) {
1416
1.31k
      zend_accel_store_interned_string(early_bindings[i].lcname);
1417
1.31k
      zend_accel_store_interned_string(early_bindings[i].rtd_key);
1418
1.31k
      zend_accel_store_interned_string(early_bindings[i].lc_parent_name);
1419
1.31k
    }
1420
477
  }
1421
50.2k
  return early_bindings;
1422
50.2k
}
1423
1424
zend_persistent_script *zend_accel_script_persist(zend_persistent_script *script, bool for_shm)
1425
50.2k
{
1426
50.2k
  Bucket *p;
1427
1428
50.2k
  script->mem = ZCG(mem);
1429
1430
50.2k
  ZEND_ASSERT(((uintptr_t)ZCG(mem) & 0x7) == 0); /* should be 8 byte aligned */
1431
1432
50.2k
  script = zend_shared_memdup_free(script, sizeof(zend_persistent_script));
1433
50.2k
  script->corrupted = false;
1434
50.2k
  ZCG(current_persistent_script) = script;
1435
1436
50.2k
  if (!for_shm) {
1437
    /* script is not going to be saved in SHM */
1438
0
    script->corrupted = true;
1439
0
  }
1440
1441
50.2k
  zend_accel_store_interned_string(script->script.filename);
1442
1443
50.2k
#if defined(__AVX__) || defined(__SSE2__)
1444
  /* Align to 64-byte boundary */
1445
50.2k
  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
50.2k
#ifdef HAVE_JIT
1451
50.2k
  if (JIT_G(on) && for_shm) {
1452
0
    zend_jit_unprotect();
1453
0
  }
1454
50.2k
#endif
1455
1456
50.2k
  zend_map_ptr_extend(ZCSG(map_ptr_last));
1457
1458
50.2k
  zend_accel_persist_class_table(&script->script.class_table);
1459
50.2k
  zend_hash_persist(&script->script.function_table);
1460
128k
  ZEND_HASH_MAP_FOREACH_BUCKET(&script->script.function_table, p) {
1461
128k
    ZEND_ASSERT(p->key != NULL);
1462
128k
    zend_accel_store_interned_string(p->key);
1463
14.0k
    zend_persist_op_array(&p->val);
1464
14.0k
  } ZEND_HASH_FOREACH_END();
1465
50.2k
  zend_persist_op_array_ex(&script->script.main_op_array, script);
1466
50.2k
  if (!script->corrupted) {
1467
50.2k
    ZEND_MAP_PTR_INIT(script->script.main_op_array.run_time_cache, NULL);
1468
50.2k
    if (script->script.main_op_array.static_variables) {
1469
50
      ZEND_MAP_PTR_NEW(script->script.main_op_array.static_variables_ptr);
1470
50
    }
1471
50.2k
#ifdef HAVE_JIT
1472
50.2k
    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
50.2k
#endif
1476
50.2k
  }
1477
50.2k
  script->warnings = zend_persist_warnings(script->num_warnings, script->warnings);
1478
50.2k
  script->early_bindings = zend_persist_early_bindings(
1479
50.2k
    script->num_early_bindings, script->early_bindings);
1480
1481
50.2k
  if (for_shm) {
1482
50.2k
    ZCSG(map_ptr_last) = CG(map_ptr_last);
1483
50.2k
    ZCSG(map_ptr_static_last) = zend_map_ptr_static_last;
1484
50.2k
  }
1485
1486
50.2k
#ifdef HAVE_JIT
1487
50.2k
  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
50.2k
#endif
1494
1495
50.2k
  script->corrupted = false;
1496
50.2k
  ZCG(current_persistent_script) = NULL;
1497
1498
50.2k
  return script;
1499
50.2k
}