Coverage Report

Created: 2026-06-02 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_lazy_objects.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Arnaud Le Blanc <arnaud.lb@gmail.com>                       |
15
   +----------------------------------------------------------------------+
16
*/
17
18
/* Lazy objects are standard zend_object whose initialization is deferred until
19
 * one of their properties backing store is accessed for the first time.
20
 *
21
 * This is implemented by using the same fallback mechanism as __get and __set
22
 * magic methods that is triggered when an undefined property is accessed.
23
 *
24
 * Execution of methods or virtual property hooks do not trigger initialization
25
 * until they access properties.
26
 *
27
 * A lazy object can be created via the Reflection API. The user specifies an
28
 * initializer function that is called when initialization is required.
29
 *
30
 * There are two kinds of lazy objects:
31
 *
32
 * - Ghosts: These are initialized in-place by the initializer function
33
 * - Proxy: The initializer returns a new instance. After initialization,
34
 *   interaction with the proxy object are proxied to the instance.
35
 *
36
 * Internal objects are not supported.
37
 */
38
39
#include "zend_API.h"
40
#include "zend_compile.h"
41
#include "zend_execute.h"
42
#include "zend_gc.h"
43
#include "zend_hash.h"
44
#include "zend_object_handlers.h"
45
#include "zend_objects_API.h"
46
#include "zend_operators.h"
47
#include "zend_types.h"
48
#include "zend_variables.h"
49
#include "zend_lazy_objects.h"
50
51
/**
52
 * Information about each lazy object is stored outside of zend_objects, in
53
 * EG(lazy_objects_store). For ghost objects, we can release this after the
54
 * object is initialized.
55
 */
56
typedef struct _zend_lazy_object_info {
57
  union {
58
    struct {
59
      zend_fcall_info_cache fcc;
60
      zval zv; /* ReflectionClass::getLazyInitializer() */
61
    } initializer;
62
    zend_object *instance; /* For initialized lazy proxy objects */
63
  } u;
64
  zend_lazy_object_flags_t flags;
65
  int lazy_properties_count;
66
} zend_lazy_object_info;
67
68
/* zend_hash dtor_func_t for zend_lazy_objects_store.infos */
69
static void zend_lazy_object_info_dtor_func(zval *pElement)
70
1.11k
{
71
1.11k
  zend_lazy_object_info *info = (zend_lazy_object_info*) Z_PTR_P(pElement);
72
73
1.11k
  if (info->flags & ZEND_LAZY_OBJECT_INITIALIZED) {
74
360
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_STRATEGY_PROXY);
75
360
    zend_object_release(info->u.instance);
76
754
  } else {
77
754
    zval_ptr_dtor(&info->u.initializer.zv);
78
754
    zend_fcc_dtor(&info->u.initializer.fcc);
79
754
  }
80
81
1.11k
  efree(info);
82
1.11k
}
83
84
void zend_lazy_objects_init(zend_lazy_objects_store *store)
85
25.7k
{
86
25.7k
  zend_hash_init(&store->infos, 8, NULL, zend_lazy_object_info_dtor_func, false);
87
25.7k
}
88
89
void zend_lazy_objects_destroy(zend_lazy_objects_store *store)
90
25.7k
{
91
25.7k
  ZEND_ASSERT(zend_hash_num_elements(&store->infos) == 0 || CG(unclean_shutdown));
92
25.7k
  zend_hash_destroy(&store->infos);
93
25.7k
}
94
95
static void zend_lazy_object_set_info(const zend_object *obj, zend_lazy_object_info *info)
96
1.11k
{
97
1.11k
  ZEND_ASSERT(zend_object_is_lazy(obj));
98
99
1.11k
  zval *zv = zend_hash_index_add_new_ptr(&EG(lazy_objects_store).infos, obj->handle, info);
100
1.11k
  ZEND_ASSERT(zv);
101
1.11k
  (void)zv;
102
1.11k
}
103
104
static zend_lazy_object_info* zend_lazy_object_get_info(const zend_object *obj)
105
2.71k
{
106
2.71k
  ZEND_ASSERT(zend_object_is_lazy(obj));
107
108
2.71k
  zend_lazy_object_info *info = zend_hash_index_find_ptr(&EG(lazy_objects_store).infos, obj->handle);
109
2.71k
  ZEND_ASSERT(info);
110
111
2.71k
  return info;
112
2.71k
}
113
114
static bool zend_lazy_object_has_stale_info(const zend_object *obj)
115
126
{
116
126
  return zend_hash_index_find_ptr(&EG(lazy_objects_store).infos, obj->handle);
117
126
}
118
119
zval* zend_lazy_object_get_initializer_zv(zend_object *obj)
120
44
{
121
44
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
122
123
44
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
124
125
44
  ZEND_ASSERT(!(info->flags & ZEND_LAZY_OBJECT_INITIALIZED));
126
127
44
  return &info->u.initializer.zv;
128
44
}
129
130
static zend_fcall_info_cache* zend_lazy_object_get_initializer_fcc(zend_object *obj)
131
389
{
132
389
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
133
134
389
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
135
136
389
  ZEND_ASSERT(!(info->flags & ZEND_LAZY_OBJECT_INITIALIZED));
137
138
389
  return &info->u.initializer.fcc;
139
389
}
140
141
zend_object* zend_lazy_object_get_instance(zend_object *obj)
142
494
{
143
494
  ZEND_ASSERT(zend_lazy_object_initialized(obj));
144
145
494
  if (zend_object_is_lazy_proxy(obj)) {
146
388
    zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
147
148
388
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_INITIALIZED);
149
150
388
    return info->u.instance;
151
388
  }
152
153
106
  return obj;
154
494
}
155
156
zend_lazy_object_flags_t zend_lazy_object_get_flags(const zend_object *obj)
157
42
{
158
42
  return zend_lazy_object_get_info(obj)->flags;
159
42
}
160
161
void zend_lazy_object_del_info(const zend_object *obj)
162
1.11k
{
163
1.11k
  zend_result res = zend_hash_index_del(&EG(lazy_objects_store).infos, obj->handle);
164
1.11k
  ZEND_ASSERT(res == SUCCESS);
165
1.11k
}
166
167
bool zend_lazy_object_decr_lazy_props(const zend_object *obj)
168
323
{
169
323
  ZEND_ASSERT(zend_object_is_lazy(obj));
170
323
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
171
172
323
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
173
174
323
  ZEND_ASSERT(info->lazy_properties_count > 0);
175
176
323
  info->lazy_properties_count--;
177
178
323
  return info->lazy_properties_count == 0;
179
323
}
180
181
/**
182
 * Making objects lazy
183
 */
184
185
ZEND_API bool zend_class_can_be_lazy(const zend_class_entry *ce)
186
0
{
187
  /* Internal classes are not supported */
188
0
  if (UNEXPECTED(ce->type == ZEND_INTERNAL_CLASS && ce != zend_standard_class_def)) {
189
0
    return false;
190
0
  }
191
192
0
  for (zend_class_entry *parent = ce->parent; parent; parent = parent->parent) {
193
0
    if (UNEXPECTED(parent->type == ZEND_INTERNAL_CLASS && parent != zend_standard_class_def)) {
194
0
      return false;
195
0
    }
196
0
  }
197
198
0
  return true;
199
0
}
200
201
static int zlo_hash_remove_dyn_props_func(zval *pDest)
202
54
{
203
54
  if (Z_TYPE_P(pDest) == IS_INDIRECT) {
204
24
    return ZEND_HASH_APPLY_STOP;
205
24
  }
206
207
30
  return ZEND_HASH_APPLY_REMOVE;
208
54
}
209
210
static bool zlo_is_iterating(zend_object *object)
211
138
{
212
138
  if (object->properties && HT_ITERATORS_COUNT(object->properties)) {
213
0
    return true;
214
0
  }
215
138
  if (zend_object_is_lazy_proxy(object)
216
6
      && zend_lazy_object_initialized(object)) {
217
6
    return zlo_is_iterating(zend_lazy_object_get_instance(object));
218
6
  }
219
132
  return false;
220
138
}
221
222
/* Make object 'obj' lazy. If 'obj' is NULL, create a lazy instance of
223
 * class 'reflection_ce' */
224
ZEND_API zend_object *zend_object_make_lazy(zend_object *obj,
225
    zend_class_entry *reflection_ce, zval *initializer_zv,
226
    zend_fcall_info_cache *initializer_fcc, zend_lazy_object_flags_t flags)
227
1.15k
{
228
1.15k
  ZEND_ASSERT(!(flags & ~(ZEND_LAZY_OBJECT_USER_MASK|ZEND_LAZY_OBJECT_STRATEGY_MASK)));
229
1.15k
  ZEND_ASSERT((flags & ZEND_LAZY_OBJECT_STRATEGY_MASK) == ZEND_LAZY_OBJECT_STRATEGY_GHOST
230
1.15k
      || (flags & ZEND_LAZY_OBJECT_STRATEGY_MASK) == ZEND_LAZY_OBJECT_STRATEGY_PROXY);
231
232
1.15k
  ZEND_ASSERT(!obj || (!zend_object_is_lazy(obj) || zend_lazy_object_initialized(obj)));
233
1.15k
  ZEND_ASSERT(!obj || instanceof_function(obj->ce, reflection_ce));
234
235
  /* Internal classes are not supported */
236
1.15k
  if (UNEXPECTED(reflection_ce->type == ZEND_INTERNAL_CLASS && reflection_ce != zend_standard_class_def)) {
237
4
    zend_throw_error(NULL, "Cannot make instance of internal class lazy: %s is internal", ZSTR_VAL(reflection_ce->name));
238
4
    return NULL;
239
4
  }
240
241
1.27k
  for (zend_class_entry *parent = reflection_ce->parent; parent; parent = parent->parent) {
242
120
    if (UNEXPECTED(parent->type == ZEND_INTERNAL_CLASS && parent != zend_standard_class_def)) {
243
4
      zend_throw_error(NULL, "Cannot make instance of internal class lazy: %s inherits internal class %s",
244
4
        ZSTR_VAL(reflection_ce->name), ZSTR_VAL(parent->name));
245
4
      return NULL;
246
4
    }
247
120
  }
248
249
1.15k
  int lazy_properties_count = 0;
250
251
1.15k
  if (!obj) {
252
1.01k
    if (UNEXPECTED(reflection_ce->ce_flags & ZEND_ACC_UNINSTANTIABLE)) {
253
0
      zval zobj;
254
      /* Call object_init_ex() for the generated exception */
255
0
      zend_result result = object_init_ex(&zobj, reflection_ce);
256
0
      ZEND_ASSERT(result == FAILURE && EG(exception));
257
0
      (void)result;
258
0
      return NULL;
259
0
    }
260
261
1.01k
    if (UNEXPECTED(!(reflection_ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
262
2
      if (UNEXPECTED(zend_update_class_constants(reflection_ce) != SUCCESS)) {
263
2
        ZEND_ASSERT(EG(exception));
264
2
        return NULL;
265
2
      }
266
2
    }
267
268
1.01k
    obj = zend_objects_new(reflection_ce);
269
270
    /* Iterate in reverse to avoid overriding Z_PROP_FLAG_P() of child props with added hooks (GH-17870). */
271
2.50k
    for (int i = obj->ce->default_properties_count - 1; i >= 0; i--) {
272
1.48k
      zval *p = &obj->properties_table[i];
273
1.48k
      ZVAL_UNDEF(p);
274
1.48k
      Z_PROP_FLAG_P(p) = 0;
275
276
1.48k
      zend_property_info *prop_info = obj->ce->properties_info_table[i];
277
1.48k
      if (prop_info) {
278
1.47k
        zval *p = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
279
1.47k
        Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
280
1.47k
        lazy_properties_count++;
281
1.47k
      }
282
1.48k
    }
283
1.01k
  } else {
284
132
    if (zlo_is_iterating(obj)) {
285
0
      zend_throw_error(NULL, "Can not reset an object during property iteration");
286
0
      return NULL;
287
0
    }
288
132
    if (zend_object_is_lazy(obj)) {
289
6
      ZEND_ASSERT(zend_object_is_lazy_proxy(obj) && zend_lazy_object_initialized(obj));
290
6
      OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
291
6
      zend_lazy_object_del_info(obj);
292
126
    } else {
293
126
      if (zend_lazy_object_has_stale_info(obj)) {
294
8
        zend_throw_error(NULL, "Can not reset an object while it is being initialized");
295
8
        return NULL;
296
8
      }
297
298
118
      if (!(flags & ZEND_LAZY_OBJECT_SKIP_DESTRUCTOR)
299
108
        && !(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
300
108
        if (obj->handlers->dtor_obj != zend_objects_destroy_object
301
108
            || obj->ce->destructor) {
302
12
          GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
303
12
          GC_ADDREF(obj);
304
12
          obj->handlers->dtor_obj(obj);
305
12
          GC_DELREF(obj);
306
12
          if (EG(exception)) {
307
6
            return NULL;
308
6
          }
309
12
        }
310
108
      }
311
118
    }
312
313
118
    GC_DEL_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
314
315
    /* unset() dynamic properties. Do not NULL out obj->properties, as this
316
     * would be unexpected. */
317
118
    if (obj->properties) {
318
32
      if (UNEXPECTED(GC_REFCOUNT(obj->properties) > 1)) {
319
0
        if (EXPECTED(!(GC_FLAGS(obj->properties) & IS_ARRAY_IMMUTABLE))) {
320
0
          GC_DELREF(obj->properties);
321
0
        }
322
0
        obj->properties = zend_array_dup(obj->properties);
323
0
      }
324
32
      zend_hash_reverse_apply(obj->properties, zlo_hash_remove_dyn_props_func);
325
32
    }
326
327
    /* unset() declared properties */
328
260
    for (int i = 0; i < reflection_ce->default_properties_count; i++) {
329
142
      zend_property_info *prop_info = obj->ce->properties_info_table[i];
330
142
      if (EXPECTED(prop_info)) {
331
142
        zval *p = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
332
142
        if (Z_TYPE_P(p) != IS_UNDEF) {
333
94
          if ((prop_info->flags & ZEND_ACC_READONLY) && !(Z_PROP_FLAG_P(p) & IS_PROP_REINITABLE)
334
              /* TODO: test final property */
335
14
              && ((obj->ce->ce_flags & ZEND_ACC_FINAL) || (prop_info->flags & ZEND_ACC_FINAL))) {
336
2
            continue;
337
2
          }
338
92
          zend_object_dtor_property(obj, p);
339
92
          ZVAL_UNDEF(p);
340
92
        }
341
140
        Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
342
140
        lazy_properties_count++;
343
140
      }
344
142
    }
345
118
  }
346
347
  /* Objects become non-lazy if all properties are made non-lazy before
348
   * initialization is triggered. If the object has no properties to begin
349
   * with, this happens immediately. */
350
1.13k
  if (UNEXPECTED(!lazy_properties_count)) {
351
34
    return obj;
352
34
  }
353
354
1.10k
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED;
355
356
1.10k
  if (flags & ZEND_LAZY_OBJECT_STRATEGY_PROXY) {
357
508
    OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_PROXY;
358
592
  } else {
359
592
    ZEND_ASSERT(flags & ZEND_LAZY_OBJECT_STRATEGY_GHOST);
360
592
  }
361
362
1.10k
  zend_lazy_object_info *info = emalloc(sizeof(*info));
363
1.10k
  zend_fcc_dup(&info->u.initializer.fcc, initializer_fcc);
364
1.10k
  ZVAL_COPY(&info->u.initializer.zv, initializer_zv);
365
1.10k
  info->flags = flags;
366
1.10k
  info->lazy_properties_count = lazy_properties_count;
367
1.10k
  zend_lazy_object_set_info(obj, info);
368
369
1.10k
  return obj;
370
1.10k
}
371
372
/**
373
 * Initialization of lazy objects
374
 */
375
376
/* Mark object as initialized. Lazy properties are initialized to their default
377
 * value and the initializer is not called. */
378
ZEND_API zend_object *zend_lazy_object_mark_as_initialized(zend_object *obj)
379
6
{
380
6
  ZEND_ASSERT(zend_object_is_lazy(obj));
381
6
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
382
383
6
  zend_class_entry *ce = obj->ce;
384
385
6
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED);
386
387
6
  zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
388
6
  zval *properties_table = obj->properties_table;
389
390
6
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
391
392
12
  for (int i = 0; i < ce->default_properties_count; i++) {
393
6
    if (Z_PROP_FLAG_P(&properties_table[i]) & IS_PROP_LAZY) {
394
6
      ZVAL_COPY_PROP(&properties_table[i], &default_properties_table[i]);
395
6
    }
396
6
  }
397
398
6
  zend_lazy_object_del_info(obj);
399
400
6
  return obj;
401
6
}
402
403
/* Revert initializer effects */
404
static void zend_lazy_object_revert_init(zend_object *obj, zval *properties_table_snapshot, HashTable *properties_snapshot)
405
150
{
406
150
  zend_class_entry *ce = obj->ce;
407
408
150
  if (ce->default_properties_count) {
409
150
    ZEND_ASSERT(properties_table_snapshot);
410
150
    zval *properties_table = obj->properties_table;
411
412
404
    for (int i = 0; i < ce->default_properties_count; i++) {
413
254
      zend_property_info *prop_info = ce->properties_info_table[i];
414
254
      if (!prop_info) {
415
4
        continue;
416
4
      }
417
418
250
      zval *p = &properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
419
250
      zend_object_dtor_property(obj, p);
420
250
      ZVAL_COPY_VALUE_PROP(p, &properties_table_snapshot[OBJ_PROP_TO_NUM(prop_info->offset)]);
421
422
250
      if (Z_ISREF_P(p) && ZEND_TYPE_IS_SET(prop_info->type)) {
423
12
        ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(p), prop_info);
424
12
      }
425
250
    }
426
427
150
    efree(properties_table_snapshot);
428
150
  }
429
150
  if (properties_snapshot) {
430
54
    if (obj->properties != properties_snapshot) {
431
4
      ZEND_ASSERT((GC_FLAGS(properties_snapshot) & IS_ARRAY_IMMUTABLE) || GC_REFCOUNT(properties_snapshot) >= 1);
432
4
      zend_release_properties(obj->properties);
433
4
      obj->properties = properties_snapshot;
434
50
    } else {
435
50
      ZEND_ASSERT((GC_FLAGS(properties_snapshot) & IS_ARRAY_IMMUTABLE) || GC_REFCOUNT(properties_snapshot) > 1);
436
50
      zend_release_properties(properties_snapshot);
437
50
    }
438
96
  } else if (obj->properties) {
439
4
    zend_release_properties(obj->properties);
440
4
    obj->properties = NULL;
441
4
  }
442
443
150
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED;
444
150
}
445
446
static bool zend_lazy_object_compatible(const zend_object *real_object, const zend_object *lazy_object)
447
360
{
448
360
  if (EXPECTED(real_object->ce == lazy_object->ce)) {
449
296
    return true;
450
296
  }
451
452
64
  if (!instanceof_function(lazy_object->ce, real_object->ce)) {
453
8
    return false;
454
8
  }
455
456
  /* zend_hash_num_elements(ce.properties_info) reports the actual number of
457
   * properties. ce.default_properties_count is off by the number of property
458
   * overrides. */
459
56
  if (zend_hash_num_elements(&lazy_object->ce->properties_info) != zend_hash_num_elements(&real_object->ce->properties_info)) {
460
0
    return false;
461
0
  }
462
463
56
  return lazy_object->ce->destructor == real_object->ce->destructor
464
52
    && lazy_object->ce->clone == real_object->ce->clone;
465
56
}
466
467
/* Initialize a lazy proxy object */
468
static zend_object *zend_lazy_object_init_proxy(zend_object *obj)
469
428
{
470
428
  ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
471
428
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
472
473
  /* Prevent object from being released during initialization */
474
428
  GC_ADDREF(obj);
475
476
428
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
477
478
  /* prevent reentrant initialization */
479
428
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
480
481
428
  zval *properties_table_snapshot = NULL;
482
483
  /* Snapshot dynamic properties */
484
428
  HashTable *properties_snapshot = obj->properties;
485
428
  if (properties_snapshot) {
486
98
    GC_TRY_ADDREF(properties_snapshot);
487
98
  }
488
489
  /* Snapshot declared properties */
490
428
  if (obj->ce->default_properties_count) {
491
428
    zval *properties_table = obj->properties_table;
492
428
    properties_table_snapshot = emalloc(sizeof(*properties_table_snapshot) * obj->ce->default_properties_count);
493
494
1.05k
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
495
624
      ZVAL_COPY_PROP(&properties_table_snapshot[i], &properties_table[i]);
496
624
    }
497
428
  }
498
499
  /* Call factory */
500
428
  zval retval;
501
428
  int argc = 1;
502
428
  zval zobj;
503
428
  HashTable *named_params = NULL;
504
428
  zend_fcall_info_cache *initializer = &info->u.initializer.fcc;
505
428
  zend_object *instance = NULL;
506
507
428
  ZVAL_OBJ(&zobj, obj);
508
509
428
  zend_call_known_fcc(initializer, &retval, argc, &zobj, named_params);
510
511
428
  if (UNEXPECTED(EG(exception))) {
512
66
    goto fail;
513
66
  }
514
515
362
  if (UNEXPECTED(Z_TYPE(retval) != IS_OBJECT)) {
516
2
    zend_type_error("Lazy proxy factory must return an instance of a class compatible with %s, %s returned",
517
2
        ZSTR_VAL(obj->ce->name),
518
2
        zend_zval_value_name(&retval));
519
2
    zval_ptr_dtor(&retval);
520
2
    goto fail;
521
2
  }
522
523
360
  if (UNEXPECTED(Z_TYPE(retval) != IS_OBJECT || !zend_lazy_object_compatible(Z_OBJ(retval), obj))) {
524
12
    zend_type_error("The real instance class %s is not compatible with the proxy class %s. The proxy must be a instance of the same class as the real instance, or a sub-class with no additional properties, and no overrides of the __destructor or __clone methods.",
525
12
        zend_zval_value_name(&retval),
526
12
        ZSTR_VAL(obj->ce->name));
527
12
    zval_ptr_dtor(&retval);
528
12
    goto fail;
529
12
  }
530
531
348
  if (UNEXPECTED(Z_OBJ(retval) == obj || zend_object_is_lazy(Z_OBJ(retval)))) {
532
2
    zend_throw_error(NULL, "Lazy proxy factory must return a non-lazy object");
533
2
    zval_ptr_dtor(&retval);
534
2
    goto fail;
535
2
  }
536
537
346
  zend_fcc_dtor(&info->u.initializer.fcc);
538
346
  zval_ptr_dtor(&info->u.initializer.zv);
539
346
  info->u.instance = Z_OBJ(retval);
540
346
  info->flags |= ZEND_LAZY_OBJECT_INITIALIZED;
541
346
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_PROXY;
542
543
  /* unset() properties of the proxy. This ensures that all accesses are be
544
   * delegated to the backing instance from now on. */
545
346
  zend_object_dtor_dynamic_properties(obj);
546
346
  obj->properties = NULL;
547
548
830
  for (int i = 0; i < Z_OBJ(retval)->ce->default_properties_count; i++) {
549
484
    zend_property_info *prop_info = Z_OBJ(retval)->ce->properties_info_table[i];
550
484
    if (EXPECTED(prop_info)) {
551
484
      zval *prop = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
552
484
      zend_object_dtor_property(obj, prop);
553
484
      ZVAL_UNDEF(prop);
554
484
      Z_PROP_FLAG_P(prop) = IS_PROP_UNINIT | IS_PROP_LAZY;
555
484
    }
556
484
  }
557
558
346
  if (properties_table_snapshot) {
559
832
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
560
486
      zval *p = &properties_table_snapshot[i];
561
      /* Use zval_ptr_dtor directly here (not zend_object_dtor_property),
562
       * as any reference type_source will have already been deleted in
563
       * case the prop is not bound to this value anymore. */
564
486
      i_zval_ptr_dtor(p);
565
486
    }
566
346
    efree(properties_table_snapshot);
567
346
  }
568
569
346
  if (properties_snapshot) {
570
72
    zend_release_properties(properties_snapshot);
571
72
  }
572
573
346
  instance = Z_OBJ(retval);
574
575
428
exit:
576
428
  if (UNEXPECTED(GC_DELREF(obj) == 0)) {
577
4
    zend_throw_error(NULL, "Lazy object was released during initialization");
578
4
    zend_objects_store_del(obj);
579
4
    instance = NULL;
580
424
  } else {
581
424
    gc_check_possible_root((zend_refcounted*) obj);
582
424
  }
583
584
428
  return instance;
585
586
82
fail:
587
82
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY;
588
82
  zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
589
82
  goto exit;
590
346
}
591
592
/* Initialize a lazy object. */
593
ZEND_API zend_object *zend_lazy_object_init(zend_object *obj)
594
1.05k
{
595
1.05k
  ZEND_ASSERT(zend_object_is_lazy(obj));
596
597
  /* If obj is an initialized lazy proxy, return the real instance. This
598
   * supports the following pattern:
599
   * if (zend_lazy_object_must_init(obj)) {
600
   *     instance = zend_lazy_object_init(obj);
601
   * }
602
   */
603
1.05k
  if (zend_lazy_object_initialized(obj)) {
604
240
    ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
605
240
    zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
606
240
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_INITIALIZED);
607
240
    if (zend_object_is_lazy(info->u.instance)) {
608
34
      return zend_lazy_object_init(info->u.instance);
609
34
    }
610
206
    return info->u.instance;
611
240
  }
612
613
817
  zend_class_entry *ce = obj->ce;
614
615
817
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED);
616
617
817
  if (zend_object_is_lazy_proxy(obj)) {
618
428
    return zend_lazy_object_init_proxy(obj);
619
428
  }
620
621
  /* Prevent object from being released during initialization */
622
389
  GC_ADDREF(obj);
623
624
389
  zend_fcall_info_cache *initializer = zend_lazy_object_get_initializer_fcc(obj);
625
626
  /* Prevent reentrant initialization */
627
389
  OBJ_EXTRA_FLAGS(obj) &= ~IS_OBJ_LAZY_UNINITIALIZED;
628
629
  /* Snapshot dynamic properties */
630
389
  HashTable *properties_snapshot = obj->properties;
631
389
  if (properties_snapshot) {
632
110
    GC_TRY_ADDREF(properties_snapshot);
633
110
  }
634
635
389
  zval *properties_table_snapshot = NULL;
636
637
  /* Snapshot declared properties and initialize lazy properties to their
638
   * default value */
639
389
  if (ce->default_properties_count) {
640
389
    zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
641
389
    zval *properties_table = obj->properties_table;
642
389
    properties_table_snapshot = emalloc(sizeof(*properties_table_snapshot) * ce->default_properties_count);
643
644
991
    for (int i = 0; i < ce->default_properties_count; i++) {
645
602
      ZVAL_COPY_PROP(&properties_table_snapshot[i], &properties_table[i]);
646
602
      if (Z_PROP_FLAG_P(&properties_table[i]) & IS_PROP_LAZY) {
647
538
        ZVAL_COPY_PROP(&properties_table[i], &default_properties_table[i]);
648
538
      }
649
602
    }
650
389
  }
651
652
  /* Call initializer */
653
389
  zval retval;
654
389
  int argc = 1;
655
389
  zval zobj;
656
389
  HashTable *named_params = NULL;
657
389
  zend_object *instance = NULL;
658
659
389
  ZVAL_OBJ(&zobj, obj);
660
661
389
  zend_call_known_fcc(initializer, &retval, argc, &zobj, named_params);
662
663
389
  if (EG(exception)) {
664
64
    zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
665
64
    goto exit;
666
64
  }
667
668
325
  if (Z_TYPE(retval) != IS_NULL) {
669
4
    zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
670
4
    zval_ptr_dtor(&retval);
671
4
    zend_type_error("Lazy object initializer must return NULL or no value");
672
4
    goto exit;
673
4
  }
674
675
321
  if (properties_table_snapshot) {
676
807
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
677
486
      zval *p = &properties_table_snapshot[i];
678
      /* Use zval_ptr_dtor directly here (not zend_object_dtor_property),
679
       * as any reference type_source will have already been deleted in
680
       * case the prop is not bound to this value anymore. */
681
486
      i_zval_ptr_dtor(p);
682
486
    }
683
321
    efree(properties_table_snapshot);
684
321
  }
685
686
321
  if (properties_snapshot) {
687
82
    zend_release_properties(properties_snapshot);
688
82
  }
689
690
  /* Must be very last in this function, for the
691
   * zend_lazy_object_has_stale_info() check */
692
321
  zend_lazy_object_del_info(obj);
693
694
321
  instance = obj;
695
696
389
exit:
697
389
  if (UNEXPECTED(GC_DELREF(obj) == 0)) {
698
6
    zend_throw_error(NULL, "Lazy object was released during initialization");
699
6
    zend_objects_store_del(obj);
700
6
    instance = NULL;
701
383
  } else {
702
383
    gc_check_possible_root((zend_refcounted*) obj);
703
383
  }
704
705
389
  return instance;
706
321
}
707
708
/* Mark an object as non-lazy (after all properties were initialized) */
709
void zend_lazy_object_realize(zend_object *obj)
710
143
{
711
143
  ZEND_ASSERT(zend_object_is_lazy(obj));
712
143
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
713
714
143
#if ZEND_DEBUG
715
334
  for (int i = 0; i < obj->ce->default_properties_count; i++) {
716
191
    ZEND_ASSERT(!(Z_PROP_FLAG_P(&obj->properties_table[i]) & IS_PROP_LAZY));
717
191
  }
718
143
#endif
719
720
143
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED | IS_OBJ_LAZY_PROXY);
721
143
  zend_lazy_object_del_info(obj);
722
143
}
723
724
ZEND_API HashTable *zend_lazy_object_get_properties(zend_object *object)
725
126
{
726
126
  ZEND_ASSERT(zend_object_is_lazy(object));
727
728
126
  zend_object *tmp = zend_lazy_object_init(object);
729
126
  if (UNEXPECTED(!tmp)) {
730
20
    if (object->properties) {
731
2
      return object->properties;
732
2
    }
733
18
    return object->properties = zend_new_array(0);
734
20
  }
735
736
106
  object = tmp;
737
106
  ZEND_ASSERT(!zend_lazy_object_must_init(object));
738
739
106
  return zend_std_get_properties_ex(object);
740
106
}
741
742
/* Initialize object and clone it. For proxies, we clone both the proxy and its
743
 * real instance, and we don't call __clone() on the proxy. */
744
zend_object *zend_lazy_object_clone(zend_object *old_obj)
745
32
{
746
32
  ZEND_ASSERT(zend_object_is_lazy(old_obj));
747
748
32
  if (UNEXPECTED(!zend_lazy_object_initialized(old_obj) && !zend_lazy_object_init(old_obj))) {
749
4
    ZEND_ASSERT(EG(exception));
750
    /* Clone handler must always return an object. It is discarded later due
751
     * to the exception. */
752
4
    zval zv;
753
4
    object_init_ex(&zv, old_obj->ce);
754
4
    GC_ADD_FLAGS(Z_OBJ(zv), IS_OBJ_DESTRUCTOR_CALLED);
755
4
    return Z_OBJ(zv);
756
4
  }
757
758
28
  if (!zend_object_is_lazy_proxy(old_obj)) {
759
12
    return zend_objects_clone_obj(old_obj);
760
12
  }
761
762
16
  zend_lazy_object_info *info = zend_lazy_object_get_info(old_obj);
763
16
  zend_class_entry *ce = old_obj->ce;
764
16
  zend_object *new_proxy = zend_objects_new(ce);
765
766
  /* Iterate in reverse to avoid overriding Z_PROP_FLAG_P() of child props with added hooks (GH-17870). */
767
36
  for (int i = ce->default_properties_count - 1; i >= 0; i--) {
768
20
    zval *p = &new_proxy->properties_table[i];
769
20
    ZVAL_UNDEF(p);
770
20
    Z_PROP_FLAG_P(p) = 0;
771
772
20
    zend_property_info *prop_info = ce->properties_info_table[i];
773
20
    if (prop_info) {
774
20
      zval *p = &new_proxy->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
775
20
      Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
776
20
    }
777
20
  }
778
779
16
  zend_lazy_object_info *new_info = emalloc(sizeof(*info));
780
16
  *new_info = *info;
781
16
  new_info->u.instance = zend_objects_clone_obj(info->u.instance);
782
783
16
  OBJ_EXTRA_FLAGS(new_proxy) = OBJ_EXTRA_FLAGS(old_obj);
784
16
  zend_lazy_object_set_info(new_proxy, new_info);
785
786
16
  return new_proxy;
787
28
}
788
789
HashTable *zend_lazy_object_debug_info(zend_object *object, int *is_temp)
790
406
{
791
406
  ZEND_ASSERT(zend_object_is_lazy(object));
792
793
406
  if (zend_object_is_lazy_proxy(object)) {
794
248
    if (zend_lazy_object_initialized(object)) {
795
124
      HashTable *properties = zend_new_array(0);
796
124
      zval instance;
797
124
      ZVAL_OBJ(&instance, zend_lazy_object_get_instance(object));
798
124
      Z_ADDREF(instance);
799
124
      zend_hash_str_add(properties, "instance", strlen("instance"), &instance);
800
124
      *is_temp = 1;
801
124
      return properties;
802
124
    }
803
248
  }
804
805
282
  *is_temp = 0;
806
282
  return zend_get_properties_no_lazy_init(object);
807
406
}
808
809
HashTable *zend_lazy_object_get_gc(zend_object *zobj, zval **table, int *n)
810
848
{
811
848
  ZEND_ASSERT(zend_object_is_lazy(zobj));
812
813
848
  zend_lazy_object_info *info = zend_lazy_object_get_info(zobj);
814
848
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
815
816
848
  if (zend_lazy_object_initialized(zobj)) {
817
520
    ZEND_ASSERT(zend_object_is_lazy_proxy(zobj));
818
520
    zend_get_gc_buffer_add_obj(gc_buffer, info->u.instance);
819
520
    zend_get_gc_buffer_use(gc_buffer, table, n);
820
    /* Initialized proxy object can not have properties */
821
520
    return NULL;
822
520
  }
823
824
328
  zend_fcall_info_cache *fcc = &info->u.initializer.fcc;
825
328
  if (fcc->object) {
826
14
    zend_get_gc_buffer_add_obj(gc_buffer, fcc->object);
827
14
  }
828
328
  if (fcc->closure) {
829
328
    zend_get_gc_buffer_add_obj(gc_buffer, fcc->closure);
830
328
  }
831
328
  zend_get_gc_buffer_add_zval(gc_buffer, &info->u.initializer.zv);
832
833
  /* Uninitialized lazy objects can not have dynamic properties, so we can
834
   * ignore zobj->properties. */
835
328
  zval *prop = zobj->properties_table;
836
328
  zval *end = prop + zobj->ce->default_properties_count;
837
876
  for ( ; prop < end; prop++) {
838
548
    zend_get_gc_buffer_add_zval(gc_buffer, prop);
839
548
  }
840
841
328
  zend_get_gc_buffer_use(gc_buffer, table, n);
842
328
  return NULL;
843
848
}
844
845
zend_property_info *zend_lazy_object_get_property_info_for_slot(zend_object *obj, zval *slot)
846
208
{
847
208
  ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
848
849
208
  zend_property_info **table = obj->ce->properties_info_table;
850
208
  intptr_t prop_num = slot - obj->properties_table;
851
208
  if (prop_num >= 0 && prop_num < obj->ce->default_properties_count) {
852
196
    if (table[prop_num]) {
853
196
      return table[prop_num];
854
196
    } else {
855
0
      return zend_get_property_info_for_slot_slow(obj, slot);
856
0
    }
857
196
  }
858
859
12
  if (!zend_lazy_object_initialized(obj)) {
860
0
    return NULL;
861
0
  }
862
863
12
  obj = zend_lazy_object_get_instance(obj);
864
12
  return zend_get_property_info_for_slot(obj, slot);
865
12
}