Coverage Report

Created: 2026-01-18 06:47

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