Coverage Report

Created: 2026-10-28 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/zend_file_cache.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: Dmitry Stogov <dmitry@php.net>                              |
14
   +----------------------------------------------------------------------+
15
*/
16
17
#include "zend.h"
18
#include "zend_virtual_cwd.h"
19
#include "zend_compile.h"
20
#include "zend_vm.h"
21
#include "zend_interfaces.h"
22
#include "zend_attributes.h"
23
#include "zend_system_id.h"
24
#include "zend_enum.h"
25
#include "zend_partial.h"
26
27
#include "php.h"
28
#ifdef ZEND_WIN32
29
#include "ext/standard/md5.h"
30
#endif
31
#include "ext/standard/php_filestat.h"
32
33
#include "ZendAccelerator.h"
34
#include "zend_file_cache.h"
35
#include "zend_shared_alloc.h"
36
#include "zend_accelerator_util_funcs.h"
37
#include "zend_accelerator_hash.h"
38
39
#ifdef HAVE_JIT
40
#include "jit/zend_jit.h"
41
#endif
42
43
#include <sys/types.h>
44
#include <sys/stat.h>
45
#include <fcntl.h>
46
47
#ifdef HAVE_UNISTD_H
48
#include <unistd.h>
49
#endif
50
51
#ifdef HAVE_SYS_UIO_H
52
# include <sys/uio.h>
53
#endif
54
55
#ifdef HAVE_SYS_FILE_H
56
# include <sys/file.h>
57
#endif
58
59
#if __has_feature(memory_sanitizer)
60
# include <sanitizer/msan_interface.h>
61
#endif
62
63
#ifndef ZEND_WIN32
64
0
#define zend_file_cache_unlink unlink
65
0
#define zend_file_cache_open open
66
#else
67
#define zend_file_cache_unlink php_win32_ioutil_unlink
68
#define zend_file_cache_open php_win32_ioutil_open
69
#endif
70
71
#ifdef ZEND_WIN32
72
# define LOCK_SH 0
73
# define LOCK_EX 1
74
# define LOCK_UN 2
75
static int zend_file_cache_flock(int fd, int op)
76
{
77
  OVERLAPPED offset = {0, 0, {{0}}, NULL};
78
  if (op == LOCK_EX) {
79
    if (LockFileEx((HANDLE)_get_osfhandle(fd),
80
                   LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset) == TRUE) {
81
      return 0;
82
    }
83
  } else if (op == LOCK_SH) {
84
    if (LockFileEx((HANDLE)_get_osfhandle(fd),
85
                   0, 0, 1, 0, &offset) == TRUE) {
86
      return 0;
87
    }
88
  } else if (op == LOCK_UN) {
89
    if (UnlockFileEx((HANDLE)_get_osfhandle(fd),
90
                     0, 1, 0, &offset) == TRUE) {
91
      return 0;
92
    }
93
  }
94
  return -1;
95
}
96
#elif defined(HAVE_FLOCK)
97
0
# define zend_file_cache_flock flock
98
#else
99
# define LOCK_SH 0
100
# define LOCK_EX 1
101
# define LOCK_UN 2
102
static int zend_file_cache_flock(int fd, int type)
103
{
104
  return 0;
105
}
106
#endif
107
108
#ifndef O_BINARY
109
0
#  define O_BINARY 0
110
#endif
111
112
0
#define SUFFIX ".bin"
113
114
#define IS_SERIALIZED_INTERNED(ptr) \
115
0
  ((size_t)(ptr) & Z_UL(1))
116
117
/* Allowing == on the upper bound accounts for a potential empty allocation at the end of the
118
 * memory region. This can also happen for a return-type-only arg_info, where &arg_info[1] is
119
 * stored, which may point to the end of the region. */
120
#define IS_SERIALIZED(ptr) \
121
0
  ((char*)(ptr) <= (char*)script->size)
122
#define IS_UNSERIALIZED(ptr) \
123
0
  (((char*)(ptr) >= (char*)script->mem && (char*)(ptr) <= (char*)script->mem + script->size) || \
124
0
   IS_ACCEL_INTERNED(ptr))
125
0
#define SERIALIZE_PTR(ptr) do { \
126
0
    if (ptr) { \
127
0
      ZEND_ASSERT(IS_UNSERIALIZED(ptr)); \
128
0
      (ptr) = (void*)((char*)(ptr) - (char*)script->mem); \
129
0
    } \
130
0
  } while (0)
131
0
#define UNSERIALIZE_PTR(ptr) do { \
132
0
    if (ptr) { \
133
0
      ZEND_ASSERT(IS_SERIALIZED(ptr)); \
134
0
      (ptr) = (void*)((char*)buf + (size_t)(ptr)); \
135
0
    } \
136
0
  } while (0)
137
138
0
#define SERIALIZE_STR(ptr) do { \
139
0
    if (ptr) { \
140
0
      if (IS_ACCEL_INTERNED(ptr)) { \
141
0
        (ptr) = zend_file_cache_serialize_interned((zend_string*)(ptr), info); \
142
0
      } else { \
143
0
        ZEND_ASSERT(IS_UNSERIALIZED(ptr)); \
144
0
        (ptr) = (void*)((char*)(ptr) - (char*)script->mem); \
145
0
      } \
146
0
    } \
147
0
  } while (0)
148
0
#define UNSERIALIZE_STR(ptr) do { \
149
0
    if (ptr) { \
150
0
      if (IS_SERIALIZED_INTERNED(ptr)) { \
151
0
        (ptr) = (void*)zend_file_cache_unserialize_interned((zend_string*)(ptr), !script->corrupted); \
152
0
      } else { \
153
0
        ZEND_ASSERT(IS_SERIALIZED(ptr)); \
154
0
        (ptr) = (void*)((char*)buf + (size_t)(ptr)); \
155
0
        /* script->corrupted shows if the script in SHM or not */ \
156
0
        if (EXPECTED(!script->corrupted)) { \
157
0
          GC_ADD_FLAGS(ptr, IS_STR_INTERNED | IS_STR_PERMANENT); \
158
0
        } else { \
159
0
          GC_ADD_FLAGS(ptr, IS_STR_INTERNED); \
160
0
          GC_DEL_FLAGS(ptr, IS_STR_PERMANENT); \
161
0
        } \
162
0
        GC_DEL_FLAGS(ptr, IS_STR_CLASS_NAME_MAP_PTR); \
163
0
      } \
164
0
    } \
165
0
  } while (0)
166
167
0
#define SERIALIZE_ATTRIBUTES(attributes) do { \
168
0
  if ((attributes) && !IS_SERIALIZED(attributes)) { \
169
0
    HashTable *ht; \
170
0
    SERIALIZE_PTR(attributes); \
171
0
    ht = (attributes); \
172
0
    UNSERIALIZE_PTR(ht); \
173
0
    zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_attribute); \
174
0
  } \
175
0
} while (0)
176
177
0
#define UNSERIALIZE_ATTRIBUTES(attributes) do { \
178
0
  if ((attributes) && !IS_UNSERIALIZED(attributes)) { \
179
0
    HashTable *ht; \
180
0
    UNSERIALIZE_PTR(attributes); \
181
0
    ht = (attributes); \
182
0
    zend_file_cache_unserialize_hash(ht, script, buf, zend_file_cache_unserialize_attribute, NULL); \
183
0
  } \
184
0
} while (0)
185
186
0
#define HOOKED_ITERATOR_PLACEHOLDER ((void*)1)
187
188
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
189
  {HT_INVALID_IDX, HT_INVALID_IDX};
190
191
typedef struct _zend_file_cache_metainfo {
192
  char         magic[8];
193
  char         system_id[32];
194
  size_t       mem_size;
195
  size_t       str_size;
196
  size_t       script_offset;
197
  accel_time_t timestamp;
198
  uint32_t     checksum;
199
} zend_file_cache_metainfo;
200
201
static int zend_file_cache_mkdir(char *filename, size_t start)
202
0
{
203
0
  char *s = filename + start;
204
205
0
  while (*s) {
206
0
    if (IS_SLASH(*s)) {
207
0
      char old = *s;
208
0
      *s = '\000';
209
0
#ifndef ZEND_WIN32
210
0
      if (mkdir(filename, S_IRWXU) < 0 && errno != EEXIST) {
211
#else
212
      if (php_win32_ioutil_mkdir(filename, 0700) < 0 && errno != EEXIST) {
213
#endif
214
0
        *s = old;
215
0
        return FAILURE;
216
0
      }
217
0
      *s = old;
218
0
    }
219
0
    s++;
220
0
  }
221
0
  return SUCCESS;
222
0
}
223
224
typedef void (*serialize_callback_t)(zval                     *zv,
225
                                     zend_persistent_script   *script,
226
                                     zend_file_cache_metainfo *info,
227
                                     void                     *buf);
228
229
typedef void (*unserialize_callback_t)(zval                    *zv,
230
                                       zend_persistent_script  *script,
231
                                       void                    *buf);
232
233
static void zend_file_cache_serialize_zval(zval                     *zv,
234
                                           zend_persistent_script   *script,
235
                                           zend_file_cache_metainfo *info,
236
                                           void                     *buf);
237
static void zend_file_cache_unserialize_zval(zval                    *zv,
238
                                             zend_persistent_script  *script,
239
                                             void                    *buf);
240
241
static void zend_file_cache_serialize_func(zval                     *zv,
242
                                           zend_persistent_script   *script,
243
                                           zend_file_cache_metainfo *info,
244
                                           void                     *buf);
245
246
static void zend_file_cache_unserialize_func(zval                    *zv,
247
                                             zend_persistent_script  *script,
248
                                             void                    *buf);
249
250
static void zend_file_cache_serialize_attribute(zval                     *zv,
251
                                                zend_persistent_script   *script,
252
                                                zend_file_cache_metainfo *info,
253
                                                void                     *buf);
254
255
static void zend_file_cache_unserialize_attribute(zval *zv, zend_persistent_script *script, void *buf);
256
257
static void *zend_file_cache_serialize_interned(zend_string              *str,
258
                                                zend_file_cache_metainfo *info)
259
0
{
260
0
  size_t len;
261
0
  void *ret;
262
263
  /* check if the same interned string was already stored */
264
0
  ret = zend_shared_alloc_get_xlat_entry(str);
265
0
  if (ret) {
266
0
    return ret;
267
0
  }
268
269
0
  len = ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(ZSTR_LEN(str)));
270
0
  ret = (void*)(info->str_size | Z_UL(1));
271
0
  zend_shared_alloc_register_xlat_entry(str, ret);
272
273
0
  zend_string *s = (zend_string*)ZCG(mem);
274
0
  if (info->str_size + len > ZSTR_LEN(s)) {
275
0
    size_t new_len = info->str_size + len;
276
0
    s = zend_string_realloc(
277
0
      s,
278
0
      ((_ZSTR_HEADER_SIZE + 1 + new_len + 4095) & ~0xfff) - (_ZSTR_HEADER_SIZE + 1),
279
0
      0);
280
0
    ZCG(mem) = (void*)s;
281
0
  }
282
283
0
  zend_string *new_str = (zend_string *) (ZSTR_VAL(s) + info->str_size);
284
0
  memcpy(new_str, str, len);
285
0
  GC_ADD_FLAGS(new_str, IS_STR_INTERNED);
286
0
  GC_DEL_FLAGS(new_str, IS_STR_PERMANENT|IS_STR_CLASS_NAME_MAP_PTR);
287
0
  info->str_size += len;
288
0
  return ret;
289
0
}
290
291
static void *zend_file_cache_unserialize_interned(zend_string *str, bool in_shm)
292
0
{
293
0
  str = (zend_string*)((char*)ZCG(mem) + ((size_t)(str) & ~Z_UL(1)));
294
0
  if (!in_shm) {
295
0
    return str;
296
0
  }
297
298
0
  zend_string *ret = accel_new_interned_string(str);
299
0
  if (ret == str) {
300
    /* We have to create new SHM allocated string */
301
0
    size_t size = _ZSTR_STRUCT_SIZE(ZSTR_LEN(str));
302
0
    ret = zend_shared_alloc(size);
303
0
    if (!ret) {
304
0
      zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM);
305
0
      LONGJMP(*EG(bailout), FAILURE);
306
0
    }
307
0
    memcpy(ret, str, size);
308
    /* String wasn't interned but we will use it as interned anyway */
309
0
    GC_SET_REFCOUNT(ret, 1);
310
0
    GC_TYPE_INFO(ret) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERSISTENT | IS_STR_PERMANENT) << GC_FLAGS_SHIFT);
311
0
  }
312
0
  return ret;
313
0
}
314
315
static void zend_file_cache_serialize_hash(HashTable                *ht,
316
                                           zend_persistent_script   *script,
317
                                           zend_file_cache_metainfo *info,
318
                                           void                     *buf,
319
                                           serialize_callback_t      func)
320
0
{
321
0
  if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
322
0
    ht->arData = NULL;
323
0
    return;
324
0
  }
325
0
  if (IS_SERIALIZED(ht->arData)) {
326
0
    return;
327
0
  }
328
0
  if (HT_IS_PACKED(ht)) {
329
0
    zval *p, *end;
330
331
0
    SERIALIZE_PTR(ht->arPacked);
332
0
    p = ht->arPacked;
333
0
    UNSERIALIZE_PTR(p);
334
0
    end = p + ht->nNumUsed;
335
0
    while (p < end) {
336
0
      if (Z_TYPE_P(p) != IS_UNDEF) {
337
0
        func(p, script, info, buf);
338
0
      }
339
0
      p++;
340
0
    }
341
0
  } else {
342
0
    Bucket *p, *end;
343
344
0
    SERIALIZE_PTR(ht->arData);
345
0
    p = ht->arData;
346
0
    UNSERIALIZE_PTR(p);
347
0
    end = p + ht->nNumUsed;
348
0
    while (p < end) {
349
0
      if (Z_TYPE(p->val) != IS_UNDEF) {
350
0
        SERIALIZE_STR(p->key);
351
0
        func(&p->val, script, info, buf);
352
0
      }
353
0
      p++;
354
0
    }
355
0
  }
356
0
}
357
358
static void zend_file_cache_serialize_ast(zend_ast                 *ast,
359
                                          zend_persistent_script   *script,
360
                                          zend_file_cache_metainfo *info,
361
                                          void                     *buf)
362
0
{
363
0
  uint32_t i;
364
0
  zend_ast *tmp;
365
366
0
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
367
0
    zend_file_cache_serialize_zval(&((zend_ast_zval*)ast)->val, script, info, buf);
368
0
  } else if (zend_ast_is_list(ast)) {
369
0
    zend_ast_list *list = zend_ast_get_list(ast);
370
0
    for (i = 0; i < list->children; i++) {
371
0
      if (list->child[i] && !IS_SERIALIZED(list->child[i])) {
372
0
        SERIALIZE_PTR(list->child[i]);
373
0
        tmp = list->child[i];
374
0
        UNSERIALIZE_PTR(tmp);
375
0
        zend_file_cache_serialize_ast(tmp, script, info, buf);
376
0
      }
377
0
    }
378
0
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
379
0
    zval z;
380
0
    ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array);
381
0
    zend_file_cache_serialize_func(&z, script, info, buf);
382
0
    zend_ast_get_op_array(ast)->op_array = Z_PTR(z);
383
0
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
384
0
    zend_ast_fcc *fcc = (zend_ast_fcc*)ast;
385
0
    ZEND_MAP_PTR_INIT(fcc->fptr, NULL);
386
    /* Will be reset if needed during unserialize */
387
0
    fcc->attr |= ZEND_PARTIAL_CACHEABLE_IN_SHM;
388
0
    SERIALIZE_STR(fcc->filename);
389
0
    SERIALIZE_STR(fcc->name);
390
0
    if (!IS_SERIALIZED(fcc->args)) {
391
0
      SERIALIZE_PTR(fcc->args);
392
0
      tmp = fcc->args;
393
0
      UNSERIALIZE_PTR(tmp);
394
0
      zend_file_cache_serialize_ast(tmp, script, info, buf);
395
0
    }
396
0
  } else if (zend_ast_is_decl(ast)) {
397
    /* Not implemented. */
398
0
    ZEND_UNREACHABLE();
399
0
  } else {
400
0
    uint32_t children = zend_ast_get_num_children(ast);
401
0
    for (i = 0; i < children; i++) {
402
0
      if (ast->child[i] && !IS_SERIALIZED(ast->child[i])) {
403
0
        SERIALIZE_PTR(ast->child[i]);
404
0
        tmp = ast->child[i];
405
0
        UNSERIALIZE_PTR(tmp);
406
0
        zend_file_cache_serialize_ast(tmp, script, info, buf);
407
0
      }
408
0
    }
409
0
  }
410
0
}
411
412
static void zend_file_cache_serialize_zval(zval                     *zv,
413
                                           zend_persistent_script   *script,
414
                                           zend_file_cache_metainfo *info,
415
                                           void                     *buf)
416
0
{
417
0
  switch (Z_TYPE_P(zv)) {
418
0
    case IS_STRING:
419
0
      if (!IS_SERIALIZED(Z_STR_P(zv))) {
420
0
        SERIALIZE_STR(Z_STR_P(zv));
421
0
      }
422
0
      break;
423
0
    case IS_ARRAY:
424
0
      if (!IS_SERIALIZED(Z_ARR_P(zv))) {
425
0
        HashTable *ht;
426
427
0
        SERIALIZE_PTR(Z_ARR_P(zv));
428
0
        ht = Z_ARR_P(zv);
429
0
        UNSERIALIZE_PTR(ht);
430
0
        zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
431
0
      }
432
0
      break;
433
0
    case IS_CONSTANT_AST:
434
0
      if (!IS_SERIALIZED(Z_AST_P(zv))) {
435
0
        zend_ast_ref *ast;
436
437
0
        SERIALIZE_PTR(Z_AST_P(zv));
438
0
        ast = Z_AST_P(zv);
439
0
        UNSERIALIZE_PTR(ast);
440
0
        zend_file_cache_serialize_ast(GC_AST(ast), script, info, buf);
441
0
      }
442
0
      break;
443
0
    case IS_INDIRECT:
444
      /* Used by static properties. */
445
0
      SERIALIZE_PTR(Z_INDIRECT_P(zv));
446
0
      break;
447
0
    case IS_PTR:
448
      /* Used by attributes on constants, will be handled separately */
449
0
      break;
450
0
    default:
451
0
      ZEND_ASSERT(Z_TYPE_P(zv) < IS_STRING);
452
0
      break;
453
0
  }
454
0
}
455
456
static void zend_file_cache_serialize_attribute(zval                     *zv,
457
                                                zend_persistent_script   *script,
458
                                                zend_file_cache_metainfo *info,
459
                                                void                     *buf)
460
0
{
461
0
  zend_attribute *attr = Z_PTR_P(zv);
462
0
  uint32_t i;
463
464
0
  SERIALIZE_PTR(Z_PTR_P(zv));
465
0
  attr = Z_PTR_P(zv);
466
0
  UNSERIALIZE_PTR(attr);
467
468
0
  SERIALIZE_STR(attr->name);
469
0
  SERIALIZE_STR(attr->lcname);
470
0
  SERIALIZE_STR(attr->validation_error);
471
472
0
  for (i = 0; i < attr->argc; i++) {
473
0
    SERIALIZE_STR(attr->args[i].name);
474
0
    zend_file_cache_serialize_zval(&attr->args[i].value, script, info, buf);
475
0
  }
476
0
}
477
478
static void zend_file_cache_serialize_type(
479
    zend_type *type, zend_persistent_script *script, zend_file_cache_metainfo *info, void *buf)
480
0
{
481
0
  if (ZEND_TYPE_HAS_LIST(*type)) {
482
0
    zend_type_list *list = ZEND_TYPE_LIST(*type);
483
0
    SERIALIZE_PTR(list);
484
0
    ZEND_TYPE_SET_PTR(*type, list);
485
0
    UNSERIALIZE_PTR(list);
486
487
0
    zend_type *list_type;
488
0
    ZEND_TYPE_LIST_FOREACH_MUTABLE(list, list_type) {
489
0
      zend_file_cache_serialize_type(list_type, script, info, buf);
490
0
    } ZEND_TYPE_LIST_FOREACH_END();
491
0
  } else if (ZEND_TYPE_HAS_NAME(*type)) {
492
0
    zend_string *type_name = ZEND_TYPE_NAME(*type);
493
0
    SERIALIZE_STR(type_name);
494
0
    ZEND_TYPE_SET_PTR(*type, type_name);
495
0
  }
496
0
}
497
498
static void zend_file_cache_serialize_op_array(zend_op_array            *op_array,
499
                                               zend_persistent_script   *script,
500
                                               zend_file_cache_metainfo *info,
501
                                               void                     *buf)
502
0
{
503
0
  ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
504
0
  ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
505
506
  /* Check whether this op_array has already been serialized. */
507
0
  if (IS_SERIALIZED(op_array->opcodes)) {
508
0
    ZEND_ASSERT(op_array->scope && "Only method op_arrays should be shared");
509
0
    return;
510
0
  }
511
512
0
  if (op_array->scope) {
513
0
    if (UNEXPECTED(zend_shared_alloc_get_xlat_entry(op_array->opcodes))) {
514
0
      op_array->refcount = (uint32_t*)(intptr_t)-1;
515
0
      SERIALIZE_PTR(op_array->static_variables);
516
0
      SERIALIZE_PTR(op_array->literals);
517
0
      SERIALIZE_PTR(op_array->opcodes);
518
0
      SERIALIZE_PTR(op_array->arg_info);
519
0
      SERIALIZE_PTR(op_array->vars);
520
0
      SERIALIZE_STR(op_array->function_name);
521
0
      SERIALIZE_STR(op_array->filename);
522
0
      SERIALIZE_PTR(op_array->live_range);
523
0
      SERIALIZE_PTR(op_array->scope);
524
0
      SERIALIZE_STR(op_array->doc_comment);
525
0
      SERIALIZE_ATTRIBUTES(op_array->attributes);
526
0
      SERIALIZE_PTR(op_array->try_catch_array);
527
0
      SERIALIZE_PTR(op_array->prototype);
528
0
      SERIALIZE_PTR(op_array->prop_info);
529
0
      return;
530
0
    }
531
0
    zend_shared_alloc_register_xlat_entry(op_array->opcodes, op_array->opcodes);
532
0
  }
533
534
0
  if (op_array->static_variables) {
535
0
    HashTable *ht;
536
537
0
    SERIALIZE_PTR(op_array->static_variables);
538
0
    ht = op_array->static_variables;
539
0
    UNSERIALIZE_PTR(ht);
540
0
    zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
541
0
  }
542
543
0
  if (op_array->literals) {
544
0
    zval *p, *end;
545
546
0
    SERIALIZE_PTR(op_array->literals);
547
0
    p = op_array->literals;
548
0
    UNSERIALIZE_PTR(p);
549
0
    end = p + op_array->last_literal;
550
0
    while (p < end) {
551
0
      zend_file_cache_serialize_zval(p, script, info, buf);
552
0
      p++;
553
0
    }
554
0
  }
555
556
0
  {
557
0
    zend_op *opline, *end;
558
559
0
#if !ZEND_USE_ABS_CONST_ADDR
560
0
    zval *literals = op_array->literals;
561
0
    UNSERIALIZE_PTR(literals);
562
0
#endif
563
564
0
    SERIALIZE_PTR(op_array->opcodes);
565
0
    opline = op_array->opcodes;
566
0
    UNSERIALIZE_PTR(opline);
567
0
    end = opline + op_array->last;
568
0
    while (opline < end) {
569
0
      if (opline->opcode == ZEND_OP_DATA
570
0
        && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST
571
0
      ) {
572
0
        zval *literal = RT_CONSTANT(opline, opline->op1);
573
#if ZEND_USE_ABS_CONST_ADDR
574
        SERIALIZE_PTR(literal);
575
        UNSERIALIZE_PTR(literal);
576
#endif
577
0
        SERIALIZE_ATTRIBUTES(Z_PTR_P(literal));
578
0
      }
579
580
#if ZEND_USE_ABS_CONST_ADDR
581
      if (opline->op1_type == IS_CONST) {
582
        SERIALIZE_PTR(opline->op1.zv);
583
      }
584
      if (opline->op2_type == IS_CONST) {
585
        SERIALIZE_PTR(opline->op2.zv);
586
      }
587
#else
588
0
      if (opline->op1_type == IS_CONST) {
589
0
        opline->op1.constant = RT_CONSTANT(opline, opline->op1) - literals;
590
0
      }
591
0
      if (opline->op2_type == IS_CONST) {
592
0
        opline->op2.constant = RT_CONSTANT(opline, opline->op2) - literals;
593
0
      }
594
0
#endif
595
#if ZEND_USE_ABS_JMP_ADDR
596
      switch (opline->opcode) {
597
        case ZEND_JMP:
598
        case ZEND_FAST_CALL:
599
          SERIALIZE_PTR(opline->op1.jmp_addr);
600
          break;
601
        case ZEND_JMPZ:
602
        case ZEND_JMPNZ:
603
        case ZEND_JMPZ_EX:
604
        case ZEND_JMPNZ_EX:
605
        case ZEND_JMP_SET:
606
        case ZEND_COALESCE:
607
        case ZEND_FE_RESET_R:
608
        case ZEND_FE_RESET_RW:
609
        case ZEND_ASSERT_CHECK:
610
        case ZEND_JMP_NULL:
611
        case ZEND_BIND_INIT_STATIC_OR_JMP:
612
        case ZEND_JMP_FRAMELESS:
613
          SERIALIZE_PTR(opline->op2.jmp_addr);
614
          break;
615
        case ZEND_CATCH:
616
          if (!(opline->extended_value & ZEND_LAST_CATCH)) {
617
            SERIALIZE_PTR(opline->op2.jmp_addr);
618
          }
619
          break;
620
        case ZEND_FE_FETCH_R:
621
        case ZEND_FE_FETCH_RW:
622
        case ZEND_SWITCH_LONG:
623
        case ZEND_SWITCH_STRING:
624
        case ZEND_MATCH:
625
          /* relative extended_value don't have to be changed */
626
          break;
627
      }
628
#endif
629
630
0
      if (opline->opcode == ZEND_CALLABLE_CONVERT_PARTIAL) {
631
        /* Will be reset if needed during unserialize */
632
0
        opline->extended_value |= ZEND_PARTIAL_CACHEABLE_IN_SHM;
633
0
      }
634
635
0
      zend_serialize_opcode_handler(opline);
636
0
      opline++;
637
0
    }
638
639
0
    if (op_array->arg_info) {
640
0
      zend_arg_info *p, *end;
641
0
      SERIALIZE_PTR(op_array->arg_info);
642
0
      p = op_array->arg_info;
643
0
      UNSERIALIZE_PTR(p);
644
0
      end = p + op_array->num_args;
645
0
      if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
646
0
        p--;
647
0
      }
648
0
      if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
649
0
        end++;
650
0
      }
651
0
      while (p < end) {
652
0
        if (!IS_SERIALIZED(p->name)) {
653
0
          SERIALIZE_STR(p->name);
654
0
        }
655
0
        zend_file_cache_serialize_type(&p->type, script, info, buf);
656
0
        SERIALIZE_STR(p->doc_comment);
657
0
        p++;
658
0
      }
659
0
    }
660
661
0
    if (op_array->vars) {
662
0
      zend_string **p, **end;
663
664
0
      SERIALIZE_PTR(op_array->vars);
665
0
      p = op_array->vars;
666
0
      UNSERIALIZE_PTR(p);
667
0
      end = p + op_array->last_var;
668
0
      while (p < end) {
669
0
        if (!IS_SERIALIZED(*p)) {
670
0
          SERIALIZE_STR(*p);
671
0
        }
672
0
        p++;
673
0
      }
674
0
    }
675
676
0
    if (op_array->num_dynamic_func_defs) {
677
0
      zend_op_array **defs;
678
0
      SERIALIZE_PTR(op_array->dynamic_func_defs);
679
0
      defs = op_array->dynamic_func_defs;
680
0
      UNSERIALIZE_PTR(defs);
681
0
      for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
682
0
        zend_op_array *def;
683
0
        SERIALIZE_PTR(defs[i]);
684
0
        def = defs[i];
685
0
        UNSERIALIZE_PTR(def);
686
0
        zend_file_cache_serialize_op_array(def, script, info, buf);
687
0
      }
688
0
    }
689
690
0
    SERIALIZE_STR(op_array->function_name);
691
0
    SERIALIZE_STR(op_array->filename);
692
0
    SERIALIZE_PTR(op_array->live_range);
693
0
    SERIALIZE_PTR(op_array->scope);
694
0
    SERIALIZE_STR(op_array->doc_comment);
695
0
    SERIALIZE_ATTRIBUTES(op_array->attributes);
696
0
    SERIALIZE_PTR(op_array->try_catch_array);
697
0
    SERIALIZE_PTR(op_array->prototype);
698
0
    SERIALIZE_PTR(op_array->prop_info);
699
0
  }
700
0
}
701
702
static void zend_file_cache_serialize_func(zval                     *zv,
703
                                           zend_persistent_script   *script,
704
                                           zend_file_cache_metainfo *info,
705
                                           void                     *buf)
706
0
{
707
0
  zend_function *func;
708
0
  SERIALIZE_PTR(Z_PTR_P(zv));
709
0
  func = Z_PTR_P(zv);
710
0
  UNSERIALIZE_PTR(func);
711
0
  ZEND_ASSERT(func->type == ZEND_USER_FUNCTION);
712
0
  zend_file_cache_serialize_op_array(&func->op_array, script, info, buf);
713
0
}
714
715
static void zend_file_cache_serialize_prop_info(zval                     *zv,
716
                                                zend_persistent_script   *script,
717
                                                zend_file_cache_metainfo *info,
718
                                                void                     *buf)
719
0
{
720
0
  if (!IS_SERIALIZED(Z_PTR_P(zv))) {
721
0
    zend_property_info *prop;
722
723
0
    SERIALIZE_PTR(Z_PTR_P(zv));
724
0
    prop = Z_PTR_P(zv);
725
0
    UNSERIALIZE_PTR(prop);
726
727
0
    ZEND_ASSERT(prop->ce != NULL && prop->name != NULL);
728
0
    if (!IS_SERIALIZED(prop->ce)) {
729
0
      SERIALIZE_PTR(prop->ce);
730
0
      SERIALIZE_STR(prop->name);
731
0
      if (prop->doc_comment) {
732
0
        SERIALIZE_STR(prop->doc_comment);
733
0
      }
734
0
      SERIALIZE_ATTRIBUTES(prop->attributes);
735
0
      SERIALIZE_PTR(prop->prototype);
736
0
      if (prop->hooks) {
737
0
        SERIALIZE_PTR(prop->hooks);
738
0
        zend_function **hooks = prop->hooks;
739
0
        UNSERIALIZE_PTR(hooks);
740
0
        for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
741
0
          if (hooks[i]) {
742
0
            SERIALIZE_PTR(hooks[i]);
743
0
            zend_function *hook = hooks[i];
744
0
            UNSERIALIZE_PTR(hook);
745
0
            zend_file_cache_serialize_op_array(&hook->op_array, script, info, buf);
746
0
          }
747
0
        }
748
0
      }
749
0
      zend_file_cache_serialize_type(&prop->type, script, info, buf);
750
0
    }
751
0
  }
752
0
}
753
754
static void zend_file_cache_serialize_class_constant(zval                     *zv,
755
                                                     zend_persistent_script   *script,
756
                                                     zend_file_cache_metainfo *info,
757
                                                     void                     *buf)
758
0
{
759
0
  if (!IS_SERIALIZED(Z_PTR_P(zv))) {
760
0
    zend_class_constant *c;
761
762
0
    SERIALIZE_PTR(Z_PTR_P(zv));
763
0
    c = Z_PTR_P(zv);
764
0
    UNSERIALIZE_PTR(c);
765
766
0
    ZEND_ASSERT(c->ce != NULL);
767
0
    if (!IS_SERIALIZED(c->ce)) {
768
0
      SERIALIZE_PTR(c->ce);
769
770
0
      zend_file_cache_serialize_zval(&c->value, script, info, buf);
771
0
      if (c->doc_comment) {
772
0
        SERIALIZE_STR(c->doc_comment);
773
0
      }
774
775
0
      SERIALIZE_ATTRIBUTES(c->attributes);
776
0
      zend_file_cache_serialize_type(&c->type, script, info, buf);
777
0
    }
778
0
  }
779
0
}
780
781
static void zend_file_cache_serialize_class(zval                     *zv,
782
                                            zend_persistent_script   *script,
783
                                            zend_file_cache_metainfo *info,
784
                                            void                     *buf)
785
0
{
786
0
  zend_class_entry *ce;
787
788
0
  SERIALIZE_PTR(Z_PTR_P(zv));
789
0
  ce = Z_PTR_P(zv);
790
0
  UNSERIALIZE_PTR(ce);
791
792
0
  SERIALIZE_STR(ce->name);
793
0
  if (ce->parent) {
794
0
    if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
795
0
      SERIALIZE_STR(ce->parent_name);
796
0
    } else {
797
0
      SERIALIZE_PTR(ce->parent);
798
0
    }
799
0
  }
800
0
  zend_file_cache_serialize_hash(&ce->function_table, script, info, buf, zend_file_cache_serialize_func);
801
0
  if (ce->default_properties_table) {
802
0
    zval *p, *end;
803
804
0
    SERIALIZE_PTR(ce->default_properties_table);
805
0
    p = ce->default_properties_table;
806
0
    UNSERIALIZE_PTR(p);
807
0
    end = p + ce->default_properties_count;
808
0
    while (p < end) {
809
0
      zend_file_cache_serialize_zval(p, script, info, buf);
810
0
      p++;
811
0
    }
812
0
  }
813
0
  if (ce->default_static_members_table) {
814
0
    zval *p, *end;
815
816
0
    SERIALIZE_PTR(ce->default_static_members_table);
817
0
    p = ce->default_static_members_table;
818
0
    UNSERIALIZE_PTR(p);
819
820
0
    end = p + ce->default_static_members_count;
821
0
    while (p < end) {
822
0
      zend_file_cache_serialize_zval(p, script, info, buf);
823
0
      p++;
824
0
    }
825
0
  }
826
0
  zend_file_cache_serialize_hash(&ce->constants_table, script, info, buf, zend_file_cache_serialize_class_constant);
827
0
  SERIALIZE_STR(ce->info.user.filename);
828
0
  SERIALIZE_STR(ce->doc_comment);
829
0
  SERIALIZE_ATTRIBUTES(ce->attributes);
830
0
  zend_file_cache_serialize_hash(&ce->properties_info, script, info, buf, zend_file_cache_serialize_prop_info);
831
832
0
  if (ce->properties_info_table) {
833
0
    uint32_t i;
834
0
    zend_property_info **table;
835
836
0
    SERIALIZE_PTR(ce->properties_info_table);
837
0
    table = ce->properties_info_table;
838
0
    UNSERIALIZE_PTR(table);
839
840
0
    for (i = 0; i < ce->default_properties_count; i++) {
841
0
      SERIALIZE_PTR(table[i]);
842
0
    }
843
0
  }
844
845
0
  if (ce->num_interfaces) {
846
0
    uint32_t i;
847
0
    zend_class_name *interface_names;
848
849
0
    ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_LINKED));
850
851
0
    SERIALIZE_PTR(ce->interface_names);
852
0
    interface_names = ce->interface_names;
853
0
    UNSERIALIZE_PTR(interface_names);
854
855
0
    for (i = 0; i < ce->num_interfaces; i++) {
856
0
      SERIALIZE_STR(interface_names[i].name);
857
0
      SERIALIZE_STR(interface_names[i].lc_name);
858
0
    }
859
0
  }
860
861
0
  if (ce->num_traits) {
862
0
    uint32_t i;
863
0
    zend_class_name *trait_names;
864
865
0
    SERIALIZE_PTR(ce->trait_names);
866
0
    trait_names = ce->trait_names;
867
0
    UNSERIALIZE_PTR(trait_names);
868
869
0
    for (i = 0; i < ce->num_traits; i++) {
870
0
      SERIALIZE_STR(trait_names[i].name);
871
0
      SERIALIZE_STR(trait_names[i].lc_name);
872
0
    }
873
874
0
    if (ce->trait_aliases) {
875
0
      zend_trait_alias **p, *q;
876
877
0
      SERIALIZE_PTR(ce->trait_aliases);
878
0
      p = ce->trait_aliases;
879
0
      UNSERIALIZE_PTR(p);
880
881
0
      while (*p) {
882
0
        SERIALIZE_PTR(*p);
883
0
        q = *p;
884
0
        UNSERIALIZE_PTR(q);
885
886
0
        if (q->trait_method.method_name) {
887
0
          SERIALIZE_STR(q->trait_method.method_name);
888
0
        }
889
0
        if (q->trait_method.class_name) {
890
0
          SERIALIZE_STR(q->trait_method.class_name);
891
0
        }
892
893
0
        if (q->alias) {
894
0
          SERIALIZE_STR(q->alias);
895
0
        }
896
0
        p++;
897
0
      }
898
0
    }
899
900
0
    if (ce->trait_precedences) {
901
0
      zend_trait_precedence **p, *q;
902
0
      uint32_t j;
903
904
0
      SERIALIZE_PTR(ce->trait_precedences);
905
0
      p = ce->trait_precedences;
906
0
      UNSERIALIZE_PTR(p);
907
908
0
      while (*p) {
909
0
        SERIALIZE_PTR(*p);
910
0
        q = *p;
911
0
        UNSERIALIZE_PTR(q);
912
913
0
        if (q->trait_method.method_name) {
914
0
          SERIALIZE_STR(q->trait_method.method_name);
915
0
        }
916
0
        if (q->trait_method.class_name) {
917
0
          SERIALIZE_STR(q->trait_method.class_name);
918
0
        }
919
920
0
        for (j = 0; j < q->num_excludes; j++) {
921
0
          SERIALIZE_STR(q->exclude_class_names[j]);
922
0
        }
923
0
        p++;
924
0
      }
925
0
    }
926
0
  }
927
928
0
  SERIALIZE_PTR(ce->constructor);
929
0
  SERIALIZE_PTR(ce->destructor);
930
0
  SERIALIZE_PTR(ce->clone);
931
0
  SERIALIZE_PTR(ce->__get);
932
0
  SERIALIZE_PTR(ce->__set);
933
0
  SERIALIZE_PTR(ce->__call);
934
0
  SERIALIZE_PTR(ce->__serialize);
935
0
  SERIALIZE_PTR(ce->__unserialize);
936
0
  SERIALIZE_PTR(ce->__isset);
937
0
  SERIALIZE_PTR(ce->__unset);
938
0
  SERIALIZE_PTR(ce->__tostring);
939
0
  SERIALIZE_PTR(ce->__callstatic);
940
0
  SERIALIZE_PTR(ce->__debugInfo);
941
942
0
  if (ce->iterator_funcs_ptr) {
943
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_new_iterator);
944
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_rewind);
945
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_valid);
946
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_key);
947
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_current);
948
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr->zf_next);
949
0
    SERIALIZE_PTR(ce->iterator_funcs_ptr);
950
0
  }
951
952
0
  if (ce->arrayaccess_funcs_ptr) {
953
0
    SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetget);
954
0
    SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetexists);
955
0
    SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetset);
956
0
    SERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetunset);
957
0
    SERIALIZE_PTR(ce->arrayaccess_funcs_ptr);
958
0
  }
959
960
0
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
961
0
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
962
963
0
  ce->inheritance_cache = NULL;
964
965
0
  if (ce->get_iterator) {
966
0
    ZEND_ASSERT(ce->get_iterator == zend_hooked_object_get_iterator);
967
0
    ce->get_iterator = HOOKED_ITERATOR_PLACEHOLDER;
968
0
  }
969
0
}
970
971
static void zend_file_cache_serialize_warnings(
972
    zend_persistent_script *script, zend_file_cache_metainfo *info, void *buf)
973
0
{
974
0
  if (script->warnings) {
975
0
    zend_error_info **warnings;
976
0
    SERIALIZE_PTR(script->warnings);
977
0
    warnings = script->warnings;
978
0
    UNSERIALIZE_PTR(warnings);
979
980
0
    for (uint32_t i = 0; i < script->num_warnings; i++) {
981
0
      zend_error_info *warning;
982
0
      SERIALIZE_PTR(warnings[i]);
983
0
      warning = warnings[i];
984
0
      UNSERIALIZE_PTR(warning);
985
0
      SERIALIZE_STR(warning->filename);
986
0
      SERIALIZE_STR(warning->message);
987
0
    }
988
0
  }
989
0
}
990
991
static void zend_file_cache_serialize_early_bindings(
992
    zend_persistent_script *script, zend_file_cache_metainfo *info, void *buf)
993
0
{
994
0
  if (script->early_bindings) {
995
0
    SERIALIZE_PTR(script->early_bindings);
996
0
    zend_early_binding *early_bindings = script->early_bindings;
997
0
    UNSERIALIZE_PTR(early_bindings);
998
0
    for (uint32_t i = 0; i < script->num_early_bindings; i++) {
999
0
      SERIALIZE_STR(early_bindings[i].lcname);
1000
0
      SERIALIZE_STR(early_bindings[i].rtd_key);
1001
0
      SERIALIZE_STR(early_bindings[i].lc_parent_name);
1002
0
    }
1003
0
  }
1004
0
}
1005
1006
static void zend_file_cache_serialize(zend_persistent_script   *script,
1007
                                      zend_file_cache_metainfo *info,
1008
                                      void                     *buf)
1009
0
{
1010
0
  zend_persistent_script *new_script;
1011
1012
0
  memcpy(info->magic, "OPCACHE", 8);
1013
0
  memcpy(info->system_id, zend_system_id, 32);
1014
0
  info->mem_size = script->size;
1015
0
  info->str_size = 0;
1016
0
  info->script_offset = (char*)script - (char*)script->mem;
1017
0
  info->timestamp = script->timestamp;
1018
1019
0
  memcpy(buf, script->mem, script->size);
1020
1021
0
  new_script = (zend_persistent_script*)((char*)buf + info->script_offset);
1022
0
  SERIALIZE_STR(new_script->script.filename);
1023
1024
0
  zend_file_cache_serialize_hash(&new_script->script.class_table, script, info, buf, zend_file_cache_serialize_class);
1025
0
  zend_file_cache_serialize_hash(&new_script->script.function_table, script, info, buf, zend_file_cache_serialize_func);
1026
0
  zend_file_cache_serialize_op_array(&new_script->script.main_op_array, script, info, buf);
1027
0
  zend_file_cache_serialize_warnings(new_script, info, buf);
1028
0
  zend_file_cache_serialize_early_bindings(new_script, info, buf);
1029
1030
0
  new_script->mem = NULL;
1031
0
}
1032
1033
static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
1034
0
{
1035
0
  size_t len;
1036
0
  char *filename;
1037
1038
0
#ifndef ZEND_WIN32
1039
0
  len = strlen(ZCG(accel_directives).file_cache);
1040
0
  filename = emalloc(len + 33 + ZSTR_LEN(script_path) + sizeof(SUFFIX));
1041
0
  memcpy(filename, ZCG(accel_directives).file_cache, len);
1042
0
  filename[len] = '/';
1043
0
  memcpy(filename + len + 1, zend_system_id, 32);
1044
0
  memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path));
1045
0
  memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX));
1046
#else
1047
  len = strlen(ZCG(accel_directives).file_cache);
1048
1049
  filename = emalloc(len + 33 + 33 + ZSTR_LEN(script_path) + sizeof(SUFFIX));
1050
1051
  memcpy(filename, ZCG(accel_directives).file_cache, len);
1052
  filename[len] = '\\';
1053
  memcpy(filename + 1 + len, accel_uname_id, 32);
1054
  len += 1 + 32;
1055
  filename[len] = '\\';
1056
1057
  memcpy(filename + len + 1, zend_system_id, 32);
1058
1059
  if (ZSTR_LEN(script_path) >= 7 && ':' == ZSTR_VAL(script_path)[4] && '/' == ZSTR_VAL(script_path)[5]  && '/' == ZSTR_VAL(script_path)[6]) {
1060
    /* phar:// or file:// */
1061
    *(filename + len + 33) = '\\';
1062
    memcpy(filename + len + 34, ZSTR_VAL(script_path), 4);
1063
    if (ZSTR_LEN(script_path) - 7 >= 2 && ':' == ZSTR_VAL(script_path)[8]) {
1064
      *(filename + len + 38) = '\\';
1065
      *(filename + len + 39) = ZSTR_VAL(script_path)[7];
1066
      memcpy(filename + len + 40, ZSTR_VAL(script_path) + 9, ZSTR_LEN(script_path) - 9);
1067
      memcpy(filename + len + 40 + ZSTR_LEN(script_path) - 9, SUFFIX, sizeof(SUFFIX));
1068
    } else {
1069
      memcpy(filename + len + 38, ZSTR_VAL(script_path) + 7, ZSTR_LEN(script_path) - 7);
1070
      memcpy(filename + len + 38 + ZSTR_LEN(script_path) - 7, SUFFIX, sizeof(SUFFIX));
1071
    }
1072
  } else if (ZSTR_LEN(script_path) >= 2 && ':' == ZSTR_VAL(script_path)[1]) {
1073
    /* local fs */
1074
    *(filename + len + 33) = '\\';
1075
    *(filename + len + 34) = ZSTR_VAL(script_path)[0];
1076
    memcpy(filename + len + 35, ZSTR_VAL(script_path) + 2, ZSTR_LEN(script_path) - 2);
1077
    memcpy(filename + len + 35 + ZSTR_LEN(script_path) - 2, SUFFIX, sizeof(SUFFIX));
1078
  } else {
1079
    /* network path */
1080
    memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path));
1081
    memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX));
1082
  }
1083
#endif
1084
1085
0
  return filename;
1086
0
}
1087
1088
/**
1089
 * Helper function for zend_file_cache_script_store().
1090
 *
1091
 * @return true on success, false on error and errno is set to indicate the cause of the error
1092
 */
1093
static bool zend_file_cache_script_write(int fd, const zend_persistent_script *script, const zend_file_cache_metainfo *info, const void *buf, const zend_string *s)
1094
0
{
1095
0
  ssize_t written;
1096
0
  const ssize_t total_size = (ssize_t)(sizeof(*info) + script->size + info->str_size);
1097
1098
0
#ifdef HAVE_SYS_UIO_H
1099
0
  const struct iovec vec[] = {
1100
0
    { .iov_base = (void *)info, .iov_len = sizeof(*info) },
1101
0
    { .iov_base = (void *)buf, .iov_len = script->size },
1102
0
    { .iov_base = (void *)ZSTR_VAL(s), .iov_len = info->str_size },
1103
0
  };
1104
1105
0
  written = writev(fd, vec, sizeof(vec) / sizeof(vec[0]));
1106
0
  if (EXPECTED(written == total_size)) {
1107
0
    return true;
1108
0
  }
1109
1110
0
  errno = written == -1 ? errno : EAGAIN;
1111
0
  return false;
1112
#else
1113
  if (UNEXPECTED(ZEND_LONG_MAX < (zend_long)total_size)) {
1114
# ifdef EFBIG
1115
    errno = EFBIG;
1116
# else
1117
    errno = ERANGE;
1118
# endif
1119
    return false;
1120
  }
1121
1122
  written = write(fd, info, sizeof(*info));
1123
  if (UNEXPECTED(written != sizeof(*info))) {
1124
    errno = written == -1 ? errno : EAGAIN;
1125
    return false;
1126
  }
1127
1128
  written = write(fd, buf, script->size);
1129
  if (UNEXPECTED(written != script->size)) {
1130
    errno = written == -1 ? errno : EAGAIN;
1131
    return false;
1132
  }
1133
1134
  written = write(fd, ZSTR_VAL(s), info->str_size);
1135
  if (UNEXPECTED(written != info->str_size)) {
1136
    errno = written == -1 ? errno : EAGAIN;
1137
    return false;
1138
  }
1139
1140
  return true;
1141
#endif
1142
0
}
1143
1144
int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
1145
0
{
1146
0
  int fd;
1147
0
  char *filename;
1148
0
  zend_file_cache_metainfo info;
1149
0
  void *mem, *buf;
1150
1151
0
#ifdef HAVE_JIT
1152
  /* FIXME: dump jited codes out to file cache? */
1153
0
  if (JIT_G(on)) {
1154
0
    return FAILURE;
1155
0
  }
1156
0
#endif
1157
1158
0
  if (ZCG(accel_directives).file_cache_read_only) {
1159
0
    return FAILURE;
1160
0
  }
1161
1162
0
  filename = zend_file_cache_get_bin_file_path(script->script.filename);
1163
1164
0
  if (zend_file_cache_mkdir(filename, strlen(ZCG(accel_directives).file_cache)) != SUCCESS) {
1165
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot create directory for file '%s', %s\n", filename, strerror(errno));
1166
0
    efree(filename);
1167
0
    return FAILURE;
1168
0
  }
1169
1170
0
  fd = zend_file_cache_open(filename, O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
1171
0
  if (fd < 0) {
1172
0
    if (errno != EEXIST) {
1173
0
      zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot create file '%s', %s\n", filename, strerror(errno));
1174
0
    }
1175
0
    efree(filename);
1176
0
    return FAILURE;
1177
0
  }
1178
1179
0
  if (zend_file_cache_flock(fd, LOCK_EX) != 0) {
1180
0
    close(fd);
1181
0
    efree(filename);
1182
0
    return FAILURE;
1183
0
  }
1184
1185
0
#if defined(__AVX__) || defined(__SSE2__)
1186
  /* Align to 64-byte boundary */
1187
0
  mem = emalloc(script->size + 64);
1188
0
  buf = (void*)(((uintptr_t)mem + 63L) & ~63L);
1189
#else
1190
  mem = buf = emalloc(script->size);
1191
#endif
1192
1193
0
  ZCG(mem) = zend_string_alloc(4096 - (_ZSTR_HEADER_SIZE + 1), 0);
1194
1195
0
  zend_shared_alloc_init_xlat_table();
1196
0
  if (!in_shm) {
1197
0
    script->corrupted = true; /* used to check if script restored to SHM or process memory */
1198
0
  }
1199
0
  zend_file_cache_serialize(script, &info, buf);
1200
0
  if (!in_shm) {
1201
0
    script->corrupted = false;
1202
0
  }
1203
0
  zend_shared_alloc_destroy_xlat_table();
1204
1205
0
  zend_string *const s = (zend_string*)ZCG(mem);
1206
1207
#if __has_feature(memory_sanitizer)
1208
  /* The buffer may contain uninitialized regions. However, the uninitialized parts will not be
1209
   * used when reading the cache. We should probably still try to get things fully initialized
1210
   * for reproducibility, but for now ignore this issue. */
1211
  __msan_unpoison(&info, sizeof(info));
1212
  __msan_unpoison(buf, script->size);
1213
#endif
1214
1215
0
  info.checksum = zend_adler32(ADLER32_INIT, buf, script->size);
1216
0
  info.checksum = zend_adler32(info.checksum, (unsigned char*)ZSTR_VAL(s), info.str_size);
1217
1218
0
  if (!zend_file_cache_script_write(fd, script, &info, buf, s)) {
1219
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot write to file '%s': %s\n", filename, strerror(errno));
1220
0
    zend_string_release_ex(s, 0);
1221
0
    close(fd);
1222
0
    efree(mem);
1223
0
    zend_file_cache_unlink(filename);
1224
0
    efree(filename);
1225
0
    return FAILURE;
1226
0
  }
1227
1228
0
  zend_string_release_ex(s, 0);
1229
0
  efree(mem);
1230
0
  if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
1231
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s': %s\n", filename, strerror(errno));
1232
0
  }
1233
0
  close(fd);
1234
0
  efree(filename);
1235
1236
0
  return SUCCESS;
1237
0
}
1238
1239
static void zend_file_cache_unserialize_hash(HashTable               *ht,
1240
                                             zend_persistent_script  *script,
1241
                                             void                    *buf,
1242
                                             unserialize_callback_t   func,
1243
                                             dtor_func_t              dtor)
1244
0
{
1245
0
  ht->pDestructor = dtor;
1246
0
  if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
1247
0
    if (EXPECTED(!file_cache_only)) {
1248
0
      HT_SET_DATA_ADDR(ht, &ZCSG(uninitialized_bucket));
1249
0
    } else {
1250
0
      HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
1251
0
    }
1252
0
    return;
1253
0
  }
1254
0
  if (IS_UNSERIALIZED(ht->arData)) {
1255
0
    return;
1256
0
  }
1257
0
  UNSERIALIZE_PTR(ht->arData);
1258
0
  if (HT_IS_PACKED(ht)) {
1259
0
    zval *p, *end;
1260
1261
0
    p = ht->arPacked;
1262
0
    end = p + ht->nNumUsed;
1263
0
    while (p < end) {
1264
0
      if (Z_TYPE_P(p) != IS_UNDEF) {
1265
0
        func(p, script, buf);
1266
0
      }
1267
0
      p++;
1268
0
    }
1269
0
  } else {
1270
0
    Bucket *p, *end;
1271
1272
0
    p = ht->arData;
1273
0
    end = p + ht->nNumUsed;
1274
0
    while (p < end) {
1275
0
      if (Z_TYPE(p->val) != IS_UNDEF) {
1276
0
        UNSERIALIZE_STR(p->key);
1277
0
        func(&p->val, script, buf);
1278
0
      }
1279
0
      p++;
1280
0
    }
1281
0
  }
1282
0
}
1283
1284
static void zend_file_cache_unserialize_ast(zend_ast                *ast,
1285
                                            zend_persistent_script  *script,
1286
                                            void                    *buf)
1287
0
{
1288
0
  uint32_t i;
1289
1290
0
  if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
1291
0
    zend_file_cache_unserialize_zval(&((zend_ast_zval*)ast)->val, script, buf);
1292
0
  } else if (zend_ast_is_list(ast)) {
1293
0
    zend_ast_list *list = zend_ast_get_list(ast);
1294
0
    for (i = 0; i < list->children; i++) {
1295
0
      if (list->child[i] && !IS_UNSERIALIZED(list->child[i])) {
1296
0
        UNSERIALIZE_PTR(list->child[i]);
1297
0
        zend_file_cache_unserialize_ast(list->child[i], script, buf);
1298
0
      }
1299
0
    }
1300
0
  } else if (ast->kind == ZEND_AST_OP_ARRAY) {
1301
0
    zval z;
1302
0
    ZVAL_PTR(&z, zend_ast_get_op_array(ast)->op_array);
1303
0
    zend_file_cache_unserialize_func(&z, script, buf);
1304
0
    zend_ast_get_op_array(ast)->op_array = Z_PTR(z);
1305
0
  } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) {
1306
0
    zend_ast_fcc *fcc = (zend_ast_fcc*)ast;
1307
0
    if (script->corrupted) {
1308
0
      fcc->attr &= ~ZEND_PARTIAL_CACHEABLE_IN_SHM;
1309
0
    } else {
1310
0
      ZEND_MAP_PTR_NEW(fcc->fptr);
1311
0
    }
1312
0
    UNSERIALIZE_STR(fcc->filename);
1313
0
    UNSERIALIZE_STR(fcc->name);
1314
0
    if (!IS_UNSERIALIZED(fcc->args)) {
1315
0
      UNSERIALIZE_PTR(fcc->args);
1316
0
      zend_file_cache_unserialize_ast(fcc->args, script, buf);
1317
0
    }
1318
0
  } else if (zend_ast_is_decl(ast)) {
1319
    /* Not implemented. */
1320
0
    ZEND_UNREACHABLE();
1321
0
  } else {
1322
0
    uint32_t children = zend_ast_get_num_children(ast);
1323
0
    for (i = 0; i < children; i++) {
1324
0
      if (ast->child[i] && !IS_UNSERIALIZED(ast->child[i])) {
1325
0
        UNSERIALIZE_PTR(ast->child[i]);
1326
0
        zend_file_cache_unserialize_ast(ast->child[i], script, buf);
1327
0
      }
1328
0
    }
1329
0
  }
1330
0
}
1331
1332
static void zend_file_cache_unserialize_zval(zval                    *zv,
1333
                                             zend_persistent_script  *script,
1334
                                             void                    *buf)
1335
0
{
1336
0
  switch (Z_TYPE_P(zv)) {
1337
0
    case IS_STRING:
1338
      /* We can't use !IS_UNSERIALIZED here, because that does not recognize unserialized
1339
       * interned strings in non-shm mode. */
1340
0
      if (IS_SERIALIZED(Z_STR_P(zv)) || IS_SERIALIZED_INTERNED(Z_STR_P(zv))) {
1341
0
        UNSERIALIZE_STR(Z_STR_P(zv));
1342
0
      }
1343
0
      break;
1344
0
    case IS_ARRAY:
1345
0
      if (!IS_UNSERIALIZED(Z_ARR_P(zv))) {
1346
0
        HashTable *ht;
1347
1348
0
        UNSERIALIZE_PTR(Z_ARR_P(zv));
1349
0
        ht = Z_ARR_P(zv);
1350
0
        zend_file_cache_unserialize_hash(ht,
1351
0
            script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1352
0
      }
1353
0
      break;
1354
0
    case IS_CONSTANT_AST:
1355
0
      if (!IS_UNSERIALIZED(Z_AST_P(zv))) {
1356
0
        UNSERIALIZE_PTR(Z_AST_P(zv));
1357
0
        zend_file_cache_unserialize_ast(Z_ASTVAL_P(zv), script, buf);
1358
0
      }
1359
0
      break;
1360
0
    case IS_INDIRECT:
1361
      /* Used by static properties. */
1362
0
      UNSERIALIZE_PTR(Z_INDIRECT_P(zv));
1363
0
      break;
1364
0
    case IS_PTR:
1365
      /* Used by attributes on constants, will be handled separately */
1366
0
      break;
1367
0
    default:
1368
0
      ZEND_ASSERT(Z_TYPE_P(zv) < IS_STRING);
1369
0
      break;
1370
0
  }
1371
0
}
1372
1373
static void zend_file_cache_unserialize_attribute(zval *zv, zend_persistent_script *script, void *buf)
1374
0
{
1375
0
  zend_attribute *attr;
1376
0
  uint32_t i;
1377
1378
0
  UNSERIALIZE_PTR(Z_PTR_P(zv));
1379
0
  attr = Z_PTR_P(zv);
1380
1381
0
  UNSERIALIZE_STR(attr->name);
1382
0
  UNSERIALIZE_STR(attr->lcname);
1383
0
  UNSERIALIZE_STR(attr->validation_error);
1384
1385
0
  for (i = 0; i < attr->argc; i++) {
1386
0
    UNSERIALIZE_STR(attr->args[i].name);
1387
0
    zend_file_cache_unserialize_zval(&attr->args[i].value, script, buf);
1388
0
  }
1389
0
}
1390
1391
static void zend_file_cache_unserialize_type(
1392
    zend_type *type, zend_class_entry *scope, zend_persistent_script *script, void *buf)
1393
0
{
1394
0
  if (ZEND_TYPE_HAS_LIST(*type)) {
1395
0
    zend_type_list *list = ZEND_TYPE_LIST(*type);
1396
0
    UNSERIALIZE_PTR(list);
1397
0
    ZEND_TYPE_SET_PTR(*type, list);
1398
1399
0
    zend_type *list_type;
1400
0
    ZEND_TYPE_LIST_FOREACH_MUTABLE(list, list_type) {
1401
0
      zend_file_cache_unserialize_type(list_type, scope, script, buf);
1402
0
    } ZEND_TYPE_LIST_FOREACH_END();
1403
0
  } else if (ZEND_TYPE_HAS_NAME(*type)) {
1404
0
    zend_string *type_name = ZEND_TYPE_NAME(*type);
1405
0
    UNSERIALIZE_STR(type_name);
1406
0
    ZEND_TYPE_SET_PTR(*type, type_name);
1407
0
    if (!script->corrupted) {
1408
0
      zend_accel_get_class_name_map_ptr(type_name);
1409
0
    } else {
1410
0
      zend_alloc_ce_cache(type_name);
1411
0
    }
1412
0
  }
1413
0
}
1414
1415
static void zend_file_cache_unserialize_op_array(zend_op_array           *op_array,
1416
                                                 zend_persistent_script  *script,
1417
                                                 void                    *buf)
1418
0
{
1419
0
  if (!script->corrupted) {
1420
0
    if (op_array != &script->script.main_op_array) {
1421
0
      op_array->fn_flags |= ZEND_ACC_IMMUTABLE;
1422
0
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
1423
0
    } else {
1424
0
      ZEND_ASSERT(!(op_array->fn_flags & ZEND_ACC_IMMUTABLE));
1425
0
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1426
0
    }
1427
0
    if (op_array->static_variables) {
1428
0
      ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
1429
0
    }
1430
0
  } else {
1431
0
    op_array->fn_flags &= ~ZEND_ACC_IMMUTABLE;
1432
0
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1433
0
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1434
0
  }
1435
1436
  /* Check whether this op_array has already been unserialized. */
1437
0
  if (IS_UNSERIALIZED(op_array->opcodes)) {
1438
0
    ZEND_ASSERT(op_array->scope && "Only method op_arrays should be shared");
1439
0
    return;
1440
0
  }
1441
1442
0
  if (op_array->refcount) {
1443
0
    op_array->refcount = NULL;
1444
0
    UNSERIALIZE_PTR(op_array->static_variables);
1445
0
    UNSERIALIZE_PTR(op_array->literals);
1446
0
    UNSERIALIZE_PTR(op_array->opcodes);
1447
0
    UNSERIALIZE_PTR(op_array->arg_info);
1448
0
    UNSERIALIZE_PTR(op_array->vars);
1449
0
    UNSERIALIZE_STR(op_array->function_name);
1450
0
    UNSERIALIZE_STR(op_array->filename);
1451
0
    UNSERIALIZE_PTR(op_array->live_range);
1452
0
    UNSERIALIZE_PTR(op_array->scope);
1453
0
    UNSERIALIZE_STR(op_array->doc_comment);
1454
0
    UNSERIALIZE_ATTRIBUTES(op_array->attributes);
1455
0
    UNSERIALIZE_PTR(op_array->try_catch_array);
1456
0
    UNSERIALIZE_PTR(op_array->prototype);
1457
0
    UNSERIALIZE_PTR(op_array->prop_info);
1458
0
    return;
1459
0
  }
1460
1461
0
  if (op_array->static_variables) {
1462
0
    HashTable *ht;
1463
1464
0
    UNSERIALIZE_PTR(op_array->static_variables);
1465
0
    ht = op_array->static_variables;
1466
0
    zend_file_cache_unserialize_hash(ht,
1467
0
        script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1468
0
  }
1469
1470
0
  if (op_array->literals) {
1471
0
    zval *p, *end;
1472
1473
0
    UNSERIALIZE_PTR(op_array->literals);
1474
0
    p = op_array->literals;
1475
0
    end = p + op_array->last_literal;
1476
0
    while (p < end) {
1477
0
      zend_file_cache_unserialize_zval(p, script, buf);
1478
0
      p++;
1479
0
    }
1480
0
  }
1481
1482
0
  {
1483
0
    zend_op *opline, *end;
1484
1485
0
    UNSERIALIZE_PTR(op_array->opcodes);
1486
0
    opline = op_array->opcodes;
1487
0
    end = opline + op_array->last;
1488
0
    while (opline < end) {
1489
#if ZEND_USE_ABS_CONST_ADDR
1490
      if (opline->op1_type == IS_CONST) {
1491
        UNSERIALIZE_PTR(opline->op1.zv);
1492
      }
1493
      if (opline->op2_type == IS_CONST) {
1494
        UNSERIALIZE_PTR(opline->op2.zv);
1495
      }
1496
#else
1497
0
      if (opline->op1_type == IS_CONST) {
1498
0
        ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op1);
1499
0
      }
1500
0
      if (opline->op2_type == IS_CONST) {
1501
0
        ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, opline->op2);
1502
0
      }
1503
0
#endif
1504
#if ZEND_USE_ABS_JMP_ADDR
1505
      switch (opline->opcode) {
1506
        case ZEND_JMP:
1507
        case ZEND_FAST_CALL:
1508
          UNSERIALIZE_PTR(opline->op1.jmp_addr);
1509
          break;
1510
        case ZEND_JMPZ:
1511
        case ZEND_JMPNZ:
1512
        case ZEND_JMPZ_EX:
1513
        case ZEND_JMPNZ_EX:
1514
        case ZEND_JMP_SET:
1515
        case ZEND_COALESCE:
1516
        case ZEND_FE_RESET_R:
1517
        case ZEND_FE_RESET_RW:
1518
        case ZEND_ASSERT_CHECK:
1519
        case ZEND_JMP_NULL:
1520
        case ZEND_BIND_INIT_STATIC_OR_JMP:
1521
        case ZEND_JMP_FRAMELESS:
1522
          UNSERIALIZE_PTR(opline->op2.jmp_addr);
1523
          break;
1524
        case ZEND_CATCH:
1525
          if (!(opline->extended_value & ZEND_LAST_CATCH)) {
1526
            UNSERIALIZE_PTR(opline->op2.jmp_addr);
1527
          }
1528
          break;
1529
        case ZEND_FE_FETCH_R:
1530
        case ZEND_FE_FETCH_RW:
1531
        case ZEND_SWITCH_LONG:
1532
        case ZEND_SWITCH_STRING:
1533
          /* relative extended_value don't have to be changed */
1534
          break;
1535
      }
1536
#endif
1537
1538
0
      if (opline->opcode == ZEND_OP_DATA
1539
0
        && (opline-1)->opcode == ZEND_DECLARE_ATTRIBUTED_CONST
1540
0
      ) {
1541
0
        zval *literal = RT_CONSTANT(opline, opline->op1);
1542
0
        UNSERIALIZE_ATTRIBUTES(Z_PTR_P(literal));
1543
0
      }
1544
1545
0
      if (opline->opcode == ZEND_CALLABLE_CONVERT_PARTIAL) {
1546
0
        if (script->corrupted) {
1547
0
          opline->extended_value &= ~ZEND_PARTIAL_CACHEABLE_IN_SHM;
1548
0
        }
1549
0
      }
1550
1551
0
      zend_deserialize_opcode_handler(opline);
1552
0
      opline++;
1553
0
    }
1554
1555
0
    UNSERIALIZE_PTR(op_array->scope);
1556
1557
0
    if (op_array->arg_info) {
1558
0
      zend_arg_info *p, *end;
1559
0
      UNSERIALIZE_PTR(op_array->arg_info);
1560
0
      p = op_array->arg_info;
1561
0
      end = p + op_array->num_args;
1562
0
      if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1563
0
        p--;
1564
0
      }
1565
0
      if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
1566
0
        end++;
1567
0
      }
1568
0
      while (p < end) {
1569
0
        if (!IS_UNSERIALIZED(p->name)) {
1570
0
          UNSERIALIZE_STR(p->name);
1571
0
        }
1572
0
        zend_file_cache_unserialize_type(&p->type, (op_array->fn_flags & ZEND_ACC_CLOSURE) ? NULL : op_array->scope, script, buf);
1573
0
        UNSERIALIZE_STR(p->doc_comment);
1574
0
        p++;
1575
0
      }
1576
0
    }
1577
1578
0
    if (op_array->vars) {
1579
0
      zend_string **p, **end;
1580
1581
0
      UNSERIALIZE_PTR(op_array->vars);
1582
0
      p = op_array->vars;
1583
0
      end = p + op_array->last_var;
1584
0
      while (p < end) {
1585
0
        if (!IS_UNSERIALIZED(*p)) {
1586
0
          UNSERIALIZE_STR(*p);
1587
0
        }
1588
0
        p++;
1589
0
      }
1590
0
    }
1591
1592
0
    if (op_array->num_dynamic_func_defs) {
1593
0
      UNSERIALIZE_PTR(op_array->dynamic_func_defs);
1594
0
      for (uint32_t i = 0; i < op_array->num_dynamic_func_defs; i++) {
1595
0
        UNSERIALIZE_PTR(op_array->dynamic_func_defs[i]);
1596
0
        zend_file_cache_unserialize_op_array(op_array->dynamic_func_defs[i], script, buf);
1597
0
      }
1598
0
    }
1599
1600
0
    UNSERIALIZE_STR(op_array->function_name);
1601
0
    UNSERIALIZE_STR(op_array->filename);
1602
0
    UNSERIALIZE_PTR(op_array->live_range);
1603
0
    UNSERIALIZE_STR(op_array->doc_comment);
1604
0
    UNSERIALIZE_ATTRIBUTES(op_array->attributes);
1605
0
    UNSERIALIZE_PTR(op_array->try_catch_array);
1606
0
    UNSERIALIZE_PTR(op_array->prototype);
1607
0
    UNSERIALIZE_PTR(op_array->prop_info);
1608
0
  }
1609
0
}
1610
1611
static void zend_file_cache_unserialize_func(zval                    *zv,
1612
                                             zend_persistent_script  *script,
1613
                                             void                    *buf)
1614
0
{
1615
0
  zend_function *func;
1616
0
  UNSERIALIZE_PTR(Z_PTR_P(zv));
1617
0
  func = Z_PTR_P(zv);
1618
0
  ZEND_ASSERT(func->type == ZEND_USER_FUNCTION);
1619
0
  zend_file_cache_unserialize_op_array(&func->op_array, script, buf);
1620
0
}
1621
1622
static void zend_file_cache_unserialize_prop_info(zval                    *zv,
1623
                                                  zend_persistent_script  *script,
1624
                                                  void                    *buf)
1625
0
{
1626
0
  if (!IS_UNSERIALIZED(Z_PTR_P(zv))) {
1627
0
    zend_property_info *prop;
1628
1629
0
    UNSERIALIZE_PTR(Z_PTR_P(zv));
1630
0
    prop = Z_PTR_P(zv);
1631
1632
0
    ZEND_ASSERT(prop->ce != NULL && prop->name != NULL);
1633
0
    if (!IS_UNSERIALIZED(prop->ce)) {
1634
0
      UNSERIALIZE_PTR(prop->ce);
1635
0
      UNSERIALIZE_STR(prop->name);
1636
0
      if (prop->doc_comment) {
1637
0
        UNSERIALIZE_STR(prop->doc_comment);
1638
0
      }
1639
0
      UNSERIALIZE_ATTRIBUTES(prop->attributes);
1640
0
      UNSERIALIZE_PTR(prop->prototype);
1641
0
      if (prop->hooks) {
1642
0
        UNSERIALIZE_PTR(prop->hooks);
1643
0
        for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
1644
0
          if (prop->hooks[i]) {
1645
0
            UNSERIALIZE_PTR(prop->hooks[i]);
1646
0
            zend_file_cache_unserialize_op_array(&prop->hooks[i]->op_array, script, buf);
1647
0
          }
1648
0
        }
1649
0
      }
1650
0
      zend_file_cache_unserialize_type(&prop->type, prop->ce, script, buf);
1651
0
    }
1652
0
  }
1653
0
}
1654
1655
static void zend_file_cache_unserialize_class_constant(zval                    *zv,
1656
                                                       zend_persistent_script  *script,
1657
                                                       void                    *buf)
1658
0
{
1659
0
  if (!IS_UNSERIALIZED(Z_PTR_P(zv))) {
1660
0
    zend_class_constant *c;
1661
1662
0
    UNSERIALIZE_PTR(Z_PTR_P(zv));
1663
0
    c = Z_PTR_P(zv);
1664
1665
0
    ZEND_ASSERT(c->ce != NULL);
1666
0
    if (!IS_UNSERIALIZED(c->ce)) {
1667
0
      UNSERIALIZE_PTR(c->ce);
1668
1669
0
      zend_file_cache_unserialize_zval(&c->value, script, buf);
1670
1671
0
      if (c->doc_comment) {
1672
0
        UNSERIALIZE_STR(c->doc_comment);
1673
0
      }
1674
0
      UNSERIALIZE_ATTRIBUTES(c->attributes);
1675
0
      zend_file_cache_unserialize_type(&c->type, c->ce, script, buf);
1676
0
    }
1677
0
  }
1678
0
}
1679
1680
static void zend_file_cache_unserialize_class(zval                    *zv,
1681
                                              zend_persistent_script  *script,
1682
                                              void                    *buf)
1683
0
{
1684
0
  zend_class_entry *ce;
1685
1686
0
  UNSERIALIZE_PTR(Z_PTR_P(zv));
1687
0
  ce = Z_PTR_P(zv);
1688
1689
0
  UNSERIALIZE_STR(ce->name);
1690
0
  if (!(ce->ce_flags & ZEND_ACC_ANON_CLASS)) {
1691
0
    if (!script->corrupted) {
1692
0
      zend_accel_get_class_name_map_ptr(ce->name);
1693
0
    } else {
1694
0
      zend_alloc_ce_cache(ce->name);
1695
0
    }
1696
0
  }
1697
0
  if (ce->parent) {
1698
0
    if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
1699
0
      UNSERIALIZE_STR(ce->parent_name);
1700
0
    } else {
1701
0
      UNSERIALIZE_PTR(ce->parent);
1702
0
    }
1703
0
  }
1704
0
  zend_file_cache_unserialize_hash(&ce->function_table,
1705
0
      script, buf, zend_file_cache_unserialize_func, ZEND_FUNCTION_DTOR);
1706
0
  if (ce->default_properties_table) {
1707
0
    zval *p, *end;
1708
1709
0
    UNSERIALIZE_PTR(ce->default_properties_table);
1710
0
    p = ce->default_properties_table;
1711
0
    end = p + ce->default_properties_count;
1712
0
    while (p < end) {
1713
0
      zend_file_cache_unserialize_zval(p, script, buf);
1714
0
      p++;
1715
0
    }
1716
0
  }
1717
0
  if (ce->default_static_members_table) {
1718
0
    zval *p, *end;
1719
0
    UNSERIALIZE_PTR(ce->default_static_members_table);
1720
0
    p = ce->default_static_members_table;
1721
0
    end = p + ce->default_static_members_count;
1722
0
    while (p < end) {
1723
0
      zend_file_cache_unserialize_zval(p, script, buf);
1724
0
      p++;
1725
0
    }
1726
0
  }
1727
0
  zend_file_cache_unserialize_hash(&ce->constants_table,
1728
0
      script, buf, zend_file_cache_unserialize_class_constant, NULL);
1729
0
  UNSERIALIZE_STR(ce->info.user.filename);
1730
0
  UNSERIALIZE_STR(ce->doc_comment);
1731
0
  UNSERIALIZE_ATTRIBUTES(ce->attributes);
1732
0
  zend_file_cache_unserialize_hash(&ce->properties_info,
1733
0
      script, buf, zend_file_cache_unserialize_prop_info, NULL);
1734
1735
0
  if (ce->properties_info_table) {
1736
0
    uint32_t i;
1737
0
    UNSERIALIZE_PTR(ce->properties_info_table);
1738
1739
0
    for (i = 0; i < ce->default_properties_count; i++) {
1740
0
      UNSERIALIZE_PTR(ce->properties_info_table[i]);
1741
0
    }
1742
0
  }
1743
1744
0
  if (ce->num_interfaces) {
1745
0
    uint32_t i;
1746
1747
0
    ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_LINKED));
1748
0
    UNSERIALIZE_PTR(ce->interface_names);
1749
1750
0
    for (i = 0; i < ce->num_interfaces; i++) {
1751
0
      UNSERIALIZE_STR(ce->interface_names[i].name);
1752
0
      UNSERIALIZE_STR(ce->interface_names[i].lc_name);
1753
0
    }
1754
0
  }
1755
1756
0
  if (ce->num_traits) {
1757
0
    uint32_t i;
1758
1759
0
    UNSERIALIZE_PTR(ce->trait_names);
1760
1761
0
    for (i = 0; i < ce->num_traits; i++) {
1762
0
      UNSERIALIZE_STR(ce->trait_names[i].name);
1763
0
      UNSERIALIZE_STR(ce->trait_names[i].lc_name);
1764
0
    }
1765
1766
0
    if (ce->trait_aliases) {
1767
0
      zend_trait_alias **p, *q;
1768
1769
0
      UNSERIALIZE_PTR(ce->trait_aliases);
1770
0
      p = ce->trait_aliases;
1771
1772
0
      while (*p) {
1773
0
        UNSERIALIZE_PTR(*p);
1774
0
        q = *p;
1775
1776
0
        if (q->trait_method.method_name) {
1777
0
          UNSERIALIZE_STR(q->trait_method.method_name);
1778
0
        }
1779
0
        if (q->trait_method.class_name) {
1780
0
          UNSERIALIZE_STR(q->trait_method.class_name);
1781
0
        }
1782
1783
0
        if (q->alias) {
1784
0
          UNSERIALIZE_STR(q->alias);
1785
0
        }
1786
0
        p++;
1787
0
      }
1788
0
    }
1789
1790
0
    if (ce->trait_precedences) {
1791
0
      zend_trait_precedence **p, *q;
1792
0
      uint32_t j;
1793
1794
0
      UNSERIALIZE_PTR(ce->trait_precedences);
1795
0
      p = ce->trait_precedences;
1796
1797
0
      while (*p) {
1798
0
        UNSERIALIZE_PTR(*p);
1799
0
        q = *p;
1800
1801
0
        if (q->trait_method.method_name) {
1802
0
          UNSERIALIZE_STR(q->trait_method.method_name);
1803
0
        }
1804
0
        if (q->trait_method.class_name) {
1805
0
          UNSERIALIZE_STR(q->trait_method.class_name);
1806
0
        }
1807
1808
0
        for (j = 0; j < q->num_excludes; j++) {
1809
0
          UNSERIALIZE_STR(q->exclude_class_names[j]);
1810
0
        }
1811
0
        p++;
1812
0
      }
1813
0
    }
1814
0
  }
1815
1816
0
  UNSERIALIZE_PTR(ce->constructor);
1817
0
  UNSERIALIZE_PTR(ce->destructor);
1818
0
  UNSERIALIZE_PTR(ce->clone);
1819
0
  UNSERIALIZE_PTR(ce->__get);
1820
0
  UNSERIALIZE_PTR(ce->__set);
1821
0
  UNSERIALIZE_PTR(ce->__call);
1822
0
  UNSERIALIZE_PTR(ce->__serialize);
1823
0
  UNSERIALIZE_PTR(ce->__unserialize);
1824
0
  UNSERIALIZE_PTR(ce->__isset);
1825
0
  UNSERIALIZE_PTR(ce->__unset);
1826
0
  UNSERIALIZE_PTR(ce->__tostring);
1827
0
  UNSERIALIZE_PTR(ce->__callstatic);
1828
0
  UNSERIALIZE_PTR(ce->__debugInfo);
1829
1830
0
  if (ce->iterator_funcs_ptr) {
1831
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr);
1832
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_new_iterator);
1833
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_rewind);
1834
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_valid);
1835
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_key);
1836
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_current);
1837
0
    UNSERIALIZE_PTR(ce->iterator_funcs_ptr->zf_next);
1838
0
  }
1839
0
  if (ce->arrayaccess_funcs_ptr) {
1840
0
    UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr);
1841
0
    UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetget);
1842
0
    UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetexists);
1843
0
    UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetset);
1844
0
    UNSERIALIZE_PTR(ce->arrayaccess_funcs_ptr->zf_offsetunset);
1845
0
  }
1846
1847
0
  if (!(script->corrupted)) {
1848
0
    ce->ce_flags |= ZEND_ACC_IMMUTABLE;
1849
0
    ce->ce_flags &= ~ZEND_ACC_FILE_CACHED;
1850
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
1851
0
    if (ce->default_static_members_count) {
1852
0
      ZEND_MAP_PTR_NEW(ce->static_members_table);
1853
0
    }
1854
0
  } else {
1855
0
    ce->ce_flags &= ~ZEND_ACC_IMMUTABLE;
1856
0
    ce->ce_flags |= ZEND_ACC_FILE_CACHED;
1857
0
    ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
1858
0
    ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
1859
0
  }
1860
1861
0
  if (ce->get_iterator) {
1862
0
    ZEND_ASSERT(ce->get_iterator == HOOKED_ITERATOR_PLACEHOLDER);
1863
0
    ce->get_iterator = zend_hooked_object_get_iterator;
1864
0
  }
1865
1866
  // Memory addresses of object handlers are not stable. They can change due to ASLR or order of linking dynamic. To
1867
  // avoid pointing to invalid memory we relink default_object_handlers here.
1868
0
  ce->default_object_handlers = ce->ce_flags & ZEND_ACC_ENUM ? &zend_enum_object_handlers : &std_object_handlers;
1869
0
}
1870
1871
static void zend_file_cache_unserialize_warnings(zend_persistent_script *script, void *buf)
1872
0
{
1873
0
  if (script->warnings) {
1874
0
    UNSERIALIZE_PTR(script->warnings);
1875
0
    for (uint32_t i = 0; i < script->num_warnings; i++) {
1876
0
      UNSERIALIZE_PTR(script->warnings[i]);
1877
0
      UNSERIALIZE_STR(script->warnings[i]->filename);
1878
0
      UNSERIALIZE_STR(script->warnings[i]->message);
1879
0
    }
1880
0
  }
1881
0
}
1882
1883
static void zend_file_cache_unserialize_early_bindings(zend_persistent_script *script, void *buf)
1884
0
{
1885
0
  if (script->early_bindings) {
1886
0
    UNSERIALIZE_PTR(script->early_bindings);
1887
0
    for (uint32_t i = 0; i < script->num_early_bindings; i++) {
1888
0
      UNSERIALIZE_STR(script->early_bindings[i].lcname);
1889
0
      UNSERIALIZE_STR(script->early_bindings[i].rtd_key);
1890
0
      UNSERIALIZE_STR(script->early_bindings[i].lc_parent_name);
1891
0
    }
1892
0
  }
1893
0
}
1894
1895
static void zend_file_cache_unserialize(zend_persistent_script  *script,
1896
                                        void                    *buf)
1897
0
{
1898
0
  script->mem = buf;
1899
1900
0
  UNSERIALIZE_STR(script->script.filename);
1901
1902
0
  zend_file_cache_unserialize_hash(&script->script.class_table,
1903
0
      script, buf, zend_file_cache_unserialize_class, ZEND_CLASS_DTOR);
1904
0
  zend_file_cache_unserialize_hash(&script->script.function_table,
1905
0
      script, buf, zend_file_cache_unserialize_func, ZEND_FUNCTION_DTOR);
1906
0
  zend_file_cache_unserialize_op_array(&script->script.main_op_array, script, buf);
1907
0
  zend_file_cache_unserialize_warnings(script, buf);
1908
0
  zend_file_cache_unserialize_early_bindings(script, buf);
1909
0
}
1910
1911
static zend_persistent_script file_cache_validate_success_script;
1912
1913
zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handle)
1914
0
{
1915
0
  return zend_file_cache_script_load_ex(file_handle, false);
1916
0
}
1917
1918
zend_persistent_script *zend_file_cache_script_load_ex(zend_file_handle *file_handle, bool validate_only)
1919
0
{
1920
0
  zend_string *full_path = file_handle->opened_path;
1921
0
  int fd;
1922
0
  char *filename;
1923
0
  zend_persistent_script *script;
1924
0
  zend_file_cache_metainfo info;
1925
0
  zend_accel_hash_entry *bucket;
1926
0
  void *mem, *checkpoint, *buf;
1927
0
  bool cache_it = true;
1928
0
  unsigned int actual_checksum;
1929
0
  bool ok;
1930
0
  size_t orig_map_ptr_last = 0;
1931
1932
0
  if (!full_path) {
1933
0
    return NULL;
1934
0
  }
1935
0
  filename = zend_file_cache_get_bin_file_path(full_path);
1936
1937
0
  fd = zend_file_cache_open(filename, O_RDONLY | O_BINARY);
1938
0
  if (fd < 0) {
1939
0
    efree(filename);
1940
0
    return NULL;
1941
0
  }
1942
1943
0
  if (zend_file_cache_flock(fd, LOCK_SH) != 0) {
1944
0
    close(fd);
1945
0
    efree(filename);
1946
0
    return NULL;
1947
0
  }
1948
1949
0
  if (read(fd, &info, sizeof(info)) != sizeof(info)) {
1950
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (info)\n", filename);
1951
0
    zend_file_cache_flock(fd, LOCK_UN);
1952
0
    close(fd);
1953
0
    if (!ZCG(accel_directives).file_cache_read_only) {
1954
0
      zend_file_cache_unlink(filename);
1955
0
    }
1956
0
    efree(filename);
1957
0
    return NULL;
1958
0
  }
1959
1960
  /* verify header */
1961
0
  if (memcmp(info.magic, "OPCACHE", 8) != 0) {
1962
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong header)\n", filename);
1963
0
    zend_file_cache_flock(fd, LOCK_UN);
1964
0
    close(fd);
1965
0
    if (!ZCG(accel_directives).file_cache_read_only) {
1966
0
      zend_file_cache_unlink(filename);
1967
0
    }
1968
0
    efree(filename);
1969
0
    return NULL;
1970
0
  }
1971
0
  if (memcmp(info.system_id, zend_system_id, 32) != 0) {
1972
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong \"system_id\")\n", filename);
1973
0
    zend_file_cache_flock(fd, LOCK_UN);
1974
0
    close(fd);
1975
0
    if (!ZCG(accel_directives).file_cache_read_only) {
1976
0
      zend_file_cache_unlink(filename);
1977
0
    }
1978
0
    efree(filename);
1979
0
    return NULL;
1980
0
  }
1981
1982
  /* verify timestamp */
1983
0
  if (ZCG(accel_directives).validate_timestamps &&
1984
0
      zend_get_file_handle_timestamp(file_handle, NULL) != info.timestamp) {
1985
0
    if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
1986
0
      zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
1987
0
    }
1988
0
    close(fd);
1989
0
    if (!ZCG(accel_directives).file_cache_read_only) {
1990
0
      zend_file_cache_unlink(filename);
1991
0
    }
1992
0
    efree(filename);
1993
0
    return NULL;
1994
0
  }
1995
1996
  /* return here if validating */
1997
0
  if (validate_only) {
1998
0
    if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
1999
0
      zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
2000
0
    }
2001
0
    close(fd);
2002
0
    efree(filename);
2003
0
    return &file_cache_validate_success_script;
2004
0
  }
2005
2006
0
  checkpoint = zend_arena_checkpoint(CG(arena));
2007
0
#if defined(__AVX__) || defined(__SSE2__)
2008
  /* Align to 64-byte boundary */
2009
0
  mem = zend_arena_alloc(&CG(arena), info.mem_size + info.str_size + 64);
2010
0
  mem = (void*)(((uintptr_t)mem + 63L) & ~63L);
2011
#else
2012
  mem = zend_arena_alloc(&CG(arena), info.mem_size + info.str_size);
2013
#endif
2014
2015
0
  if (read(fd, mem, info.mem_size + info.str_size) != (ssize_t)(info.mem_size + info.str_size)) {
2016
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (mem)\n", filename);
2017
0
    zend_file_cache_flock(fd, LOCK_UN);
2018
0
    close(fd);
2019
0
    if (!ZCG(accel_directives).file_cache_read_only) {
2020
0
      zend_file_cache_unlink(filename);
2021
0
    }
2022
0
    zend_arena_release(&CG(arena), checkpoint);
2023
0
    efree(filename);
2024
0
    return NULL;
2025
0
  }
2026
0
  if (zend_file_cache_flock(fd, LOCK_UN) != 0) {
2027
0
    zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
2028
0
  }
2029
0
  close(fd);
2030
2031
  /* verify checksum */
2032
0
  if (ZCG(accel_directives).file_cache_consistency_checks &&
2033
0
      (actual_checksum = zend_adler32(ADLER32_INIT, mem, info.mem_size + info.str_size)) != info.checksum) {
2034
0
    zend_accel_error(ACCEL_LOG_WARNING, "corrupted file '%s' excepted checksum: 0x%08x actual checksum: 0x%08x\n", filename, info.checksum, actual_checksum);
2035
0
    if (!ZCG(accel_directives).file_cache_read_only) {
2036
0
      zend_file_cache_unlink(filename);
2037
0
    }
2038
0
    zend_arena_release(&CG(arena), checkpoint);
2039
0
    efree(filename);
2040
0
    return NULL;
2041
0
  }
2042
2043
0
  if (!file_cache_only &&
2044
0
      !ZCSG(restart_in_progress) &&
2045
0
      !ZCSG(restart_pending) &&
2046
0
    !ZSMMG(memory_exhausted) &&
2047
0
      accelerator_shm_read_lock() == SUCCESS) {
2048
    /* exclusive lock */
2049
0
    zend_shared_alloc_lock();
2050
2051
    /* Check if we still need to put the file into the cache (may be it was
2052
     * already stored by another process. This final check is done under
2053
     * exclusive lock) */
2054
0
    bucket = zend_accel_hash_find_entry(&ZCSG(hash), full_path);
2055
0
    if (bucket) {
2056
0
      script = (zend_persistent_script *)bucket->data;
2057
0
      if (!script->corrupted) {
2058
0
        zend_shared_alloc_unlock();
2059
0
        zend_arena_release(&CG(arena), checkpoint);
2060
0
        efree(filename);
2061
0
        return script;
2062
0
      }
2063
0
    }
2064
2065
0
    if (zend_accel_hash_is_full(&ZCSG(hash))) {
2066
0
      zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
2067
0
      ZSMMG(memory_exhausted) = 1;
2068
0
      zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
2069
0
      zend_shared_alloc_unlock();
2070
0
      goto use_process_mem;
2071
0
    }
2072
2073
0
    buf = zend_shared_alloc_aligned(info.mem_size);
2074
2075
0
    if (!buf) {
2076
0
      zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM);
2077
0
      zend_shared_alloc_unlock();
2078
0
      goto use_process_mem;
2079
0
    }
2080
0
    memcpy(buf, mem, info.mem_size);
2081
0
    zend_map_ptr_extend(ZCSG(map_ptr_last));
2082
0
    orig_map_ptr_last = CG(map_ptr_last);
2083
0
    zend_accel_pending_ce_cache_begin();
2084
0
  } else {
2085
0
use_process_mem:
2086
0
    buf = mem;
2087
0
    cache_it = false;
2088
0
  }
2089
2090
0
  ZCG(mem) = ((char*)mem + info.mem_size);
2091
0
  script = (zend_persistent_script*)((char*)buf + info.script_offset);
2092
0
  script->corrupted = !cache_it; /* used to check if script restored to SHM or process memory */
2093
2094
0
  ok = true;
2095
0
  zend_try {
2096
0
    zend_file_cache_unserialize(script, buf);
2097
0
  } zend_catch {
2098
0
    ok = false;
2099
0
  } zend_end_try();
2100
0
  if (!ok) {
2101
0
    if (cache_it) {
2102
      /* The slots allocated by the failed unserialization are referenced
2103
       * only by the abandoned buffer, so they can be reclaimed. */
2104
0
      zend_accel_pending_ce_cache_discard();
2105
0
      CG(map_ptr_last) = orig_map_ptr_last;
2106
0
      zend_shared_alloc_unlock();
2107
0
      goto use_process_mem;
2108
0
    } else {
2109
0
      zend_arena_release(&CG(arena), checkpoint);
2110
0
      efree(filename);
2111
0
      return NULL;
2112
0
    }
2113
0
  }
2114
2115
0
  script->corrupted = false;
2116
2117
0
  if (cache_it) {
2118
0
    ZCSG(map_ptr_last) = CG(map_ptr_last);
2119
0
    zend_accel_pending_ce_cache_commit();
2120
0
    script->dynamic_members.last_used = ZCG(request_time);
2121
2122
0
    zend_accel_hash_update(&ZCSG(hash), script->script.filename, 0, script);
2123
2124
0
    zend_shared_alloc_unlock();
2125
0
    zend_accel_error(ACCEL_LOG_INFO, "File cached script loaded into memory '%s'", ZSTR_VAL(script->script.filename));
2126
2127
0
    zend_arena_release(&CG(arena), checkpoint);
2128
0
  }
2129
0
  efree(filename);
2130
2131
0
  return script;
2132
0
}
2133
2134
void zend_file_cache_invalidate(zend_string *full_path)
2135
0
{
2136
0
  if (ZCG(accel_directives).file_cache_read_only) {
2137
0
    return;
2138
0
  }
2139
2140
0
  char *filename;
2141
2142
0
  filename = zend_file_cache_get_bin_file_path(full_path);
2143
2144
0
  zend_file_cache_unlink(filename);
2145
  efree(filename);
2146
0
}