Coverage Report

Created: 2025-11-16 06:23

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