Coverage Report

Created: 2026-06-02 06:37

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
0
{
71
0
  zend_lazy_object_info *info = (zend_lazy_object_info*) Z_PTR_P(pElement);
72
73
0
  if (info->flags & ZEND_LAZY_OBJECT_INITIALIZED) {
74
0
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_STRATEGY_PROXY);
75
0
    zend_object_release(info->u.instance);
76
0
  } else {
77
0
    zval_ptr_dtor(&info->u.initializer.zv);
78
0
    zend_fcc_dtor(&info->u.initializer.fcc);
79
0
  }
80
81
0
  efree(info);
82
0
}
83
84
void zend_lazy_objects_init(zend_lazy_objects_store *store)
85
38.8k
{
86
38.8k
  zend_hash_init(&store->infos, 8, NULL, zend_lazy_object_info_dtor_func, false);
87
38.8k
}
88
89
void zend_lazy_objects_destroy(zend_lazy_objects_store *store)
90
38.8k
{
91
38.8k
  ZEND_ASSERT(zend_hash_num_elements(&store->infos) == 0 || CG(unclean_shutdown));
92
38.8k
  zend_hash_destroy(&store->infos);
93
38.8k
}
94
95
static void zend_lazy_object_set_info(const zend_object *obj, zend_lazy_object_info *info)
96
0
{
97
0
  ZEND_ASSERT(zend_object_is_lazy(obj));
98
99
0
  zval *zv = zend_hash_index_add_new_ptr(&EG(lazy_objects_store).infos, obj->handle, info);
100
0
  ZEND_ASSERT(zv);
101
0
  (void)zv;
102
0
}
103
104
static zend_lazy_object_info* zend_lazy_object_get_info(const zend_object *obj)
105
0
{
106
0
  ZEND_ASSERT(zend_object_is_lazy(obj));
107
108
0
  zend_lazy_object_info *info = zend_hash_index_find_ptr(&EG(lazy_objects_store).infos, obj->handle);
109
0
  ZEND_ASSERT(info);
110
111
0
  return info;
112
0
}
113
114
static bool zend_lazy_object_has_stale_info(const zend_object *obj)
115
0
{
116
0
  return zend_hash_index_find_ptr(&EG(lazy_objects_store).infos, obj->handle);
117
0
}
118
119
zval* zend_lazy_object_get_initializer_zv(zend_object *obj)
120
0
{
121
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
122
123
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
124
125
0
  ZEND_ASSERT(!(info->flags & ZEND_LAZY_OBJECT_INITIALIZED));
126
127
0
  return &info->u.initializer.zv;
128
0
}
129
130
static zend_fcall_info_cache* zend_lazy_object_get_initializer_fcc(zend_object *obj)
131
0
{
132
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
133
134
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
135
136
0
  ZEND_ASSERT(!(info->flags & ZEND_LAZY_OBJECT_INITIALIZED));
137
138
0
  return &info->u.initializer.fcc;
139
0
}
140
141
zend_object* zend_lazy_object_get_instance(zend_object *obj)
142
0
{
143
0
  ZEND_ASSERT(zend_lazy_object_initialized(obj));
144
145
0
  if (zend_object_is_lazy_proxy(obj)) {
146
0
    zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
147
148
0
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_INITIALIZED);
149
150
0
    return info->u.instance;
151
0
  }
152
153
0
  return obj;
154
0
}
155
156
zend_lazy_object_flags_t zend_lazy_object_get_flags(const zend_object *obj)
157
0
{
158
0
  return zend_lazy_object_get_info(obj)->flags;
159
0
}
160
161
void zend_lazy_object_del_info(const zend_object *obj)
162
0
{
163
0
  zend_result res = zend_hash_index_del(&EG(lazy_objects_store).infos, obj->handle);
164
0
  ZEND_ASSERT(res == SUCCESS);
165
0
}
166
167
bool zend_lazy_object_decr_lazy_props(const zend_object *obj)
168
0
{
169
0
  ZEND_ASSERT(zend_object_is_lazy(obj));
170
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
171
172
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
173
174
0
  ZEND_ASSERT(info->lazy_properties_count > 0);
175
176
0
  info->lazy_properties_count--;
177
178
0
  return info->lazy_properties_count == 0;
179
0
}
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
0
{
203
0
  if (Z_TYPE_P(pDest) == IS_INDIRECT) {
204
0
    return ZEND_HASH_APPLY_STOP;
205
0
  }
206
207
0
  return ZEND_HASH_APPLY_REMOVE;
208
0
}
209
210
static bool zlo_is_iterating(zend_object *object)
211
0
{
212
0
  if (object->properties && HT_ITERATORS_COUNT(object->properties)) {
213
0
    return true;
214
0
  }
215
0
  if (zend_object_is_lazy_proxy(object)
216
0
      && zend_lazy_object_initialized(object)) {
217
0
    return zlo_is_iterating(zend_lazy_object_get_instance(object));
218
0
  }
219
0
  return false;
220
0
}
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
0
{
228
0
  ZEND_ASSERT(!(flags & ~(ZEND_LAZY_OBJECT_USER_MASK|ZEND_LAZY_OBJECT_STRATEGY_MASK)));
229
0
  ZEND_ASSERT((flags & ZEND_LAZY_OBJECT_STRATEGY_MASK) == ZEND_LAZY_OBJECT_STRATEGY_GHOST
230
0
      || (flags & ZEND_LAZY_OBJECT_STRATEGY_MASK) == ZEND_LAZY_OBJECT_STRATEGY_PROXY);
231
232
0
  ZEND_ASSERT(!obj || (!zend_object_is_lazy(obj) || zend_lazy_object_initialized(obj)));
233
0
  ZEND_ASSERT(!obj || instanceof_function(obj->ce, reflection_ce));
234
235
  /* Internal classes are not supported */
236
0
  if (UNEXPECTED(reflection_ce->type == ZEND_INTERNAL_CLASS && reflection_ce != zend_standard_class_def)) {
237
0
    zend_throw_error(NULL, "Cannot make instance of internal class lazy: %s is internal", ZSTR_VAL(reflection_ce->name));
238
0
    return NULL;
239
0
  }
240
241
0
  for (zend_class_entry *parent = reflection_ce->parent; parent; parent = parent->parent) {
242
0
    if (UNEXPECTED(parent->type == ZEND_INTERNAL_CLASS && parent != zend_standard_class_def)) {
243
0
      zend_throw_error(NULL, "Cannot make instance of internal class lazy: %s inherits internal class %s",
244
0
        ZSTR_VAL(reflection_ce->name), ZSTR_VAL(parent->name));
245
0
      return NULL;
246
0
    }
247
0
  }
248
249
0
  int lazy_properties_count = 0;
250
251
0
  if (!obj) {
252
0
    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
0
    if (UNEXPECTED(!(reflection_ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
262
0
      if (UNEXPECTED(zend_update_class_constants(reflection_ce) != SUCCESS)) {
263
0
        ZEND_ASSERT(EG(exception));
264
0
        return NULL;
265
0
      }
266
0
    }
267
268
0
    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
0
    for (int i = obj->ce->default_properties_count - 1; i >= 0; i--) {
272
0
      zval *p = &obj->properties_table[i];
273
0
      ZVAL_UNDEF(p);
274
0
      Z_PROP_FLAG_P(p) = 0;
275
276
0
      zend_property_info *prop_info = obj->ce->properties_info_table[i];
277
0
      if (prop_info) {
278
0
        zval *p = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
279
0
        Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
280
0
        lazy_properties_count++;
281
0
      }
282
0
    }
283
0
  } else {
284
0
    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
0
    if (zend_object_is_lazy(obj)) {
289
0
      ZEND_ASSERT(zend_object_is_lazy_proxy(obj) && zend_lazy_object_initialized(obj));
290
0
      OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
291
0
      zend_lazy_object_del_info(obj);
292
0
    } else {
293
0
      if (zend_lazy_object_has_stale_info(obj)) {
294
0
        zend_throw_error(NULL, "Can not reset an object while it is being initialized");
295
0
        return NULL;
296
0
      }
297
298
0
      if (!(flags & ZEND_LAZY_OBJECT_SKIP_DESTRUCTOR)
299
0
        && !(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)) {
300
0
        if (obj->handlers->dtor_obj != zend_objects_destroy_object
301
0
            || obj->ce->destructor) {
302
0
          GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
303
0
          GC_ADDREF(obj);
304
0
          obj->handlers->dtor_obj(obj);
305
0
          GC_DELREF(obj);
306
0
          if (EG(exception)) {
307
0
            return NULL;
308
0
          }
309
0
        }
310
0
      }
311
0
    }
312
313
0
    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
0
    if (obj->properties) {
318
0
      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
0
      zend_hash_reverse_apply(obj->properties, zlo_hash_remove_dyn_props_func);
325
0
    }
326
327
    /* unset() declared properties */
328
0
    for (int i = 0; i < reflection_ce->default_properties_count; i++) {
329
0
      zend_property_info *prop_info = obj->ce->properties_info_table[i];
330
0
      if (EXPECTED(prop_info)) {
331
0
        zval *p = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
332
0
        if (Z_TYPE_P(p) != IS_UNDEF) {
333
0
          if ((prop_info->flags & ZEND_ACC_READONLY) && !(Z_PROP_FLAG_P(p) & IS_PROP_REINITABLE)
334
              /* TODO: test final property */
335
0
              && ((obj->ce->ce_flags & ZEND_ACC_FINAL) || (prop_info->flags & ZEND_ACC_FINAL))) {
336
0
            continue;
337
0
          }
338
0
          zend_object_dtor_property(obj, p);
339
0
          ZVAL_UNDEF(p);
340
0
        }
341
0
        Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
342
0
        lazy_properties_count++;
343
0
      }
344
0
    }
345
0
  }
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
0
  if (UNEXPECTED(!lazy_properties_count)) {
351
0
    return obj;
352
0
  }
353
354
0
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED;
355
356
0
  if (flags & ZEND_LAZY_OBJECT_STRATEGY_PROXY) {
357
0
    OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_PROXY;
358
0
  } else {
359
0
    ZEND_ASSERT(flags & ZEND_LAZY_OBJECT_STRATEGY_GHOST);
360
0
  }
361
362
0
  zend_lazy_object_info *info = emalloc(sizeof(*info));
363
0
  zend_fcc_dup(&info->u.initializer.fcc, initializer_fcc);
364
0
  ZVAL_COPY(&info->u.initializer.zv, initializer_zv);
365
0
  info->flags = flags;
366
0
  info->lazy_properties_count = lazy_properties_count;
367
0
  zend_lazy_object_set_info(obj, info);
368
369
0
  return obj;
370
0
}
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
0
{
380
0
  ZEND_ASSERT(zend_object_is_lazy(obj));
381
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
382
383
0
  zend_class_entry *ce = obj->ce;
384
385
0
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED);
386
387
0
  zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
388
0
  zval *properties_table = obj->properties_table;
389
390
0
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
391
392
0
  for (int i = 0; i < ce->default_properties_count; i++) {
393
0
    if (Z_PROP_FLAG_P(&properties_table[i]) & IS_PROP_LAZY) {
394
0
      ZVAL_COPY_PROP(&properties_table[i], &default_properties_table[i]);
395
0
    }
396
0
  }
397
398
0
  zend_lazy_object_del_info(obj);
399
400
0
  return obj;
401
0
}
402
403
/* Revert initializer effects */
404
static void zend_lazy_object_revert_init(zend_object *obj, zval *properties_table_snapshot, HashTable *properties_snapshot)
405
0
{
406
0
  zend_class_entry *ce = obj->ce;
407
408
0
  if (ce->default_properties_count) {
409
0
    ZEND_ASSERT(properties_table_snapshot);
410
0
    zval *properties_table = obj->properties_table;
411
412
0
    for (int i = 0; i < ce->default_properties_count; i++) {
413
0
      zend_property_info *prop_info = ce->properties_info_table[i];
414
0
      if (!prop_info) {
415
0
        continue;
416
0
      }
417
418
0
      zval *p = &properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
419
0
      zend_object_dtor_property(obj, p);
420
0
      ZVAL_COPY_VALUE_PROP(p, &properties_table_snapshot[OBJ_PROP_TO_NUM(prop_info->offset)]);
421
422
0
      if (Z_ISREF_P(p) && ZEND_TYPE_IS_SET(prop_info->type)) {
423
0
        ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(p), prop_info);
424
0
      }
425
0
    }
426
427
0
    efree(properties_table_snapshot);
428
0
  }
429
0
  if (properties_snapshot) {
430
0
    if (obj->properties != properties_snapshot) {
431
0
      ZEND_ASSERT((GC_FLAGS(properties_snapshot) & IS_ARRAY_IMMUTABLE) || GC_REFCOUNT(properties_snapshot) >= 1);
432
0
      zend_release_properties(obj->properties);
433
0
      obj->properties = properties_snapshot;
434
0
    } else {
435
0
      ZEND_ASSERT((GC_FLAGS(properties_snapshot) & IS_ARRAY_IMMUTABLE) || GC_REFCOUNT(properties_snapshot) > 1);
436
0
      zend_release_properties(properties_snapshot);
437
0
    }
438
0
  } else if (obj->properties) {
439
0
    zend_release_properties(obj->properties);
440
0
    obj->properties = NULL;
441
0
  }
442
443
0
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED;
444
0
}
445
446
static bool zend_lazy_object_compatible(const zend_object *real_object, const zend_object *lazy_object)
447
0
{
448
0
  if (EXPECTED(real_object->ce == lazy_object->ce)) {
449
0
    return true;
450
0
  }
451
452
0
  if (!instanceof_function(lazy_object->ce, real_object->ce)) {
453
0
    return false;
454
0
  }
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
0
  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
0
  return lazy_object->ce->destructor == real_object->ce->destructor
464
0
    && lazy_object->ce->clone == real_object->ce->clone;
465
0
}
466
467
/* Initialize a lazy proxy object */
468
static zend_object *zend_lazy_object_init_proxy(zend_object *obj)
469
0
{
470
0
  ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
471
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
472
473
  /* Prevent object from being released during initialization */
474
0
  GC_ADDREF(obj);
475
476
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
477
478
  /* prevent reentrant initialization */
479
0
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY);
480
481
0
  zval *properties_table_snapshot = NULL;
482
483
  /* Snapshot dynamic properties */
484
0
  HashTable *properties_snapshot = obj->properties;
485
0
  if (properties_snapshot) {
486
0
    GC_TRY_ADDREF(properties_snapshot);
487
0
  }
488
489
  /* Snapshot declared properties */
490
0
  if (obj->ce->default_properties_count) {
491
0
    zval *properties_table = obj->properties_table;
492
0
    properties_table_snapshot = emalloc(sizeof(*properties_table_snapshot) * obj->ce->default_properties_count);
493
494
0
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
495
0
      ZVAL_COPY_PROP(&properties_table_snapshot[i], &properties_table[i]);
496
0
    }
497
0
  }
498
499
  /* Call factory */
500
0
  zval retval;
501
0
  int argc = 1;
502
0
  zval zobj;
503
0
  HashTable *named_params = NULL;
504
0
  zend_fcall_info_cache *initializer = &info->u.initializer.fcc;
505
0
  zend_object *instance = NULL;
506
507
0
  ZVAL_OBJ(&zobj, obj);
508
509
0
  zend_call_known_fcc(initializer, &retval, argc, &zobj, named_params);
510
511
0
  if (UNEXPECTED(EG(exception))) {
512
0
    goto fail;
513
0
  }
514
515
0
  if (UNEXPECTED(Z_TYPE(retval) != IS_OBJECT)) {
516
0
    zend_type_error("Lazy proxy factory must return an instance of a class compatible with %s, %s returned",
517
0
        ZSTR_VAL(obj->ce->name),
518
0
        zend_zval_value_name(&retval));
519
0
    zval_ptr_dtor(&retval);
520
0
    goto fail;
521
0
  }
522
523
0
  if (UNEXPECTED(Z_TYPE(retval) != IS_OBJECT || !zend_lazy_object_compatible(Z_OBJ(retval), obj))) {
524
0
    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
0
        zend_zval_value_name(&retval),
526
0
        ZSTR_VAL(obj->ce->name));
527
0
    zval_ptr_dtor(&retval);
528
0
    goto fail;
529
0
  }
530
531
0
  if (UNEXPECTED(Z_OBJ(retval) == obj || zend_object_is_lazy(Z_OBJ(retval)))) {
532
0
    zend_throw_error(NULL, "Lazy proxy factory must return a non-lazy object");
533
0
    zval_ptr_dtor(&retval);
534
0
    goto fail;
535
0
  }
536
537
0
  zend_fcc_dtor(&info->u.initializer.fcc);
538
0
  zval_ptr_dtor(&info->u.initializer.zv);
539
0
  info->u.instance = Z_OBJ(retval);
540
0
  info->flags |= ZEND_LAZY_OBJECT_INITIALIZED;
541
0
  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
0
  zend_object_dtor_dynamic_properties(obj);
546
0
  obj->properties = NULL;
547
548
0
  for (int i = 0; i < Z_OBJ(retval)->ce->default_properties_count; i++) {
549
0
    zend_property_info *prop_info = Z_OBJ(retval)->ce->properties_info_table[i];
550
0
    if (EXPECTED(prop_info)) {
551
0
      zval *prop = &obj->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
552
0
      zend_object_dtor_property(obj, prop);
553
0
      ZVAL_UNDEF(prop);
554
0
      Z_PROP_FLAG_P(prop) = IS_PROP_UNINIT | IS_PROP_LAZY;
555
0
    }
556
0
  }
557
558
0
  if (properties_table_snapshot) {
559
0
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
560
0
      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
0
      i_zval_ptr_dtor(p);
565
0
    }
566
0
    efree(properties_table_snapshot);
567
0
  }
568
569
0
  if (properties_snapshot) {
570
0
    zend_release_properties(properties_snapshot);
571
0
  }
572
573
0
  instance = Z_OBJ(retval);
574
575
0
exit:
576
0
  if (UNEXPECTED(GC_DELREF(obj) == 0)) {
577
0
    zend_throw_error(NULL, "Lazy object was released during initialization");
578
0
    zend_objects_store_del(obj);
579
0
    instance = NULL;
580
0
  } else {
581
0
    gc_check_possible_root((zend_refcounted*) obj);
582
0
  }
583
584
0
  return instance;
585
586
0
fail:
587
0
  OBJ_EXTRA_FLAGS(obj) |= IS_OBJ_LAZY_UNINITIALIZED|IS_OBJ_LAZY_PROXY;
588
0
  zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
589
0
  goto exit;
590
0
}
591
592
/* Initialize a lazy object. */
593
ZEND_API zend_object *zend_lazy_object_init(zend_object *obj)
594
0
{
595
0
  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
0
  if (zend_lazy_object_initialized(obj)) {
604
0
    ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
605
0
    zend_lazy_object_info *info = zend_lazy_object_get_info(obj);
606
0
    ZEND_ASSERT(info->flags & ZEND_LAZY_OBJECT_INITIALIZED);
607
0
    if (zend_object_is_lazy(info->u.instance)) {
608
0
      return zend_lazy_object_init(info->u.instance);
609
0
    }
610
0
    return info->u.instance;
611
0
  }
612
613
0
  zend_class_entry *ce = obj->ce;
614
615
0
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED);
616
617
0
  if (zend_object_is_lazy_proxy(obj)) {
618
0
    return zend_lazy_object_init_proxy(obj);
619
0
  }
620
621
  /* Prevent object from being released during initialization */
622
0
  GC_ADDREF(obj);
623
624
0
  zend_fcall_info_cache *initializer = zend_lazy_object_get_initializer_fcc(obj);
625
626
  /* Prevent reentrant initialization */
627
0
  OBJ_EXTRA_FLAGS(obj) &= ~IS_OBJ_LAZY_UNINITIALIZED;
628
629
  /* Snapshot dynamic properties */
630
0
  HashTable *properties_snapshot = obj->properties;
631
0
  if (properties_snapshot) {
632
0
    GC_TRY_ADDREF(properties_snapshot);
633
0
  }
634
635
0
  zval *properties_table_snapshot = NULL;
636
637
  /* Snapshot declared properties and initialize lazy properties to their
638
   * default value */
639
0
  if (ce->default_properties_count) {
640
0
    zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
641
0
    zval *properties_table = obj->properties_table;
642
0
    properties_table_snapshot = emalloc(sizeof(*properties_table_snapshot) * ce->default_properties_count);
643
644
0
    for (int i = 0; i < ce->default_properties_count; i++) {
645
0
      ZVAL_COPY_PROP(&properties_table_snapshot[i], &properties_table[i]);
646
0
      if (Z_PROP_FLAG_P(&properties_table[i]) & IS_PROP_LAZY) {
647
0
        ZVAL_COPY_PROP(&properties_table[i], &default_properties_table[i]);
648
0
      }
649
0
    }
650
0
  }
651
652
  /* Call initializer */
653
0
  zval retval;
654
0
  int argc = 1;
655
0
  zval zobj;
656
0
  HashTable *named_params = NULL;
657
0
  zend_object *instance = NULL;
658
659
0
  ZVAL_OBJ(&zobj, obj);
660
661
0
  zend_call_known_fcc(initializer, &retval, argc, &zobj, named_params);
662
663
0
  if (EG(exception)) {
664
0
    zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
665
0
    goto exit;
666
0
  }
667
668
0
  if (Z_TYPE(retval) != IS_NULL) {
669
0
    zend_lazy_object_revert_init(obj, properties_table_snapshot, properties_snapshot);
670
0
    zval_ptr_dtor(&retval);
671
0
    zend_type_error("Lazy object initializer must return NULL or no value");
672
0
    goto exit;
673
0
  }
674
675
0
  if (properties_table_snapshot) {
676
0
    for (int i = 0; i < obj->ce->default_properties_count; i++) {
677
0
      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
0
      i_zval_ptr_dtor(p);
682
0
    }
683
0
    efree(properties_table_snapshot);
684
0
  }
685
686
0
  if (properties_snapshot) {
687
0
    zend_release_properties(properties_snapshot);
688
0
  }
689
690
  /* Must be very last in this function, for the
691
   * zend_lazy_object_has_stale_info() check */
692
0
  zend_lazy_object_del_info(obj);
693
694
0
  instance = obj;
695
696
0
exit:
697
0
  if (UNEXPECTED(GC_DELREF(obj) == 0)) {
698
0
    zend_throw_error(NULL, "Lazy object was released during initialization");
699
0
    zend_objects_store_del(obj);
700
0
    instance = NULL;
701
0
  } else {
702
0
    gc_check_possible_root((zend_refcounted*) obj);
703
0
  }
704
705
0
  return instance;
706
0
}
707
708
/* Mark an object as non-lazy (after all properties were initialized) */
709
void zend_lazy_object_realize(zend_object *obj)
710
0
{
711
0
  ZEND_ASSERT(zend_object_is_lazy(obj));
712
0
  ZEND_ASSERT(!zend_lazy_object_initialized(obj));
713
714
0
#if ZEND_DEBUG
715
0
  for (int i = 0; i < obj->ce->default_properties_count; i++) {
716
0
    ZEND_ASSERT(!(Z_PROP_FLAG_P(&obj->properties_table[i]) & IS_PROP_LAZY));
717
0
  }
718
0
#endif
719
720
0
  OBJ_EXTRA_FLAGS(obj) &= ~(IS_OBJ_LAZY_UNINITIALIZED | IS_OBJ_LAZY_PROXY);
721
0
  zend_lazy_object_del_info(obj);
722
0
}
723
724
ZEND_API HashTable *zend_lazy_object_get_properties(zend_object *object)
725
0
{
726
0
  ZEND_ASSERT(zend_object_is_lazy(object));
727
728
0
  zend_object *tmp = zend_lazy_object_init(object);
729
0
  if (UNEXPECTED(!tmp)) {
730
0
    if (object->properties) {
731
0
      return object->properties;
732
0
    }
733
0
    return object->properties = zend_new_array(0);
734
0
  }
735
736
0
  object = tmp;
737
0
  ZEND_ASSERT(!zend_lazy_object_must_init(object));
738
739
0
  return zend_std_get_properties_ex(object);
740
0
}
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
0
{
746
0
  ZEND_ASSERT(zend_object_is_lazy(old_obj));
747
748
0
  if (UNEXPECTED(!zend_lazy_object_initialized(old_obj) && !zend_lazy_object_init(old_obj))) {
749
0
    ZEND_ASSERT(EG(exception));
750
    /* Clone handler must always return an object. It is discarded later due
751
     * to the exception. */
752
0
    zval zv;
753
0
    object_init_ex(&zv, old_obj->ce);
754
0
    GC_ADD_FLAGS(Z_OBJ(zv), IS_OBJ_DESTRUCTOR_CALLED);
755
0
    return Z_OBJ(zv);
756
0
  }
757
758
0
  if (!zend_object_is_lazy_proxy(old_obj)) {
759
0
    return zend_objects_clone_obj(old_obj);
760
0
  }
761
762
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(old_obj);
763
0
  zend_class_entry *ce = old_obj->ce;
764
0
  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
0
  for (int i = ce->default_properties_count - 1; i >= 0; i--) {
768
0
    zval *p = &new_proxy->properties_table[i];
769
0
    ZVAL_UNDEF(p);
770
0
    Z_PROP_FLAG_P(p) = 0;
771
772
0
    zend_property_info *prop_info = ce->properties_info_table[i];
773
0
    if (prop_info) {
774
0
      zval *p = &new_proxy->properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
775
0
      Z_PROP_FLAG_P(p) = IS_PROP_UNINIT | IS_PROP_LAZY;
776
0
    }
777
0
  }
778
779
0
  zend_lazy_object_info *new_info = emalloc(sizeof(*info));
780
0
  *new_info = *info;
781
0
  new_info->u.instance = zend_objects_clone_obj(info->u.instance);
782
783
0
  OBJ_EXTRA_FLAGS(new_proxy) = OBJ_EXTRA_FLAGS(old_obj);
784
0
  zend_lazy_object_set_info(new_proxy, new_info);
785
786
0
  return new_proxy;
787
0
}
788
789
HashTable *zend_lazy_object_debug_info(zend_object *object, int *is_temp)
790
0
{
791
0
  ZEND_ASSERT(zend_object_is_lazy(object));
792
793
0
  if (zend_object_is_lazy_proxy(object)) {
794
0
    if (zend_lazy_object_initialized(object)) {
795
0
      HashTable *properties = zend_new_array(0);
796
0
      zval instance;
797
0
      ZVAL_OBJ(&instance, zend_lazy_object_get_instance(object));
798
0
      Z_ADDREF(instance);
799
0
      zend_hash_str_add(properties, "instance", strlen("instance"), &instance);
800
0
      *is_temp = 1;
801
0
      return properties;
802
0
    }
803
0
  }
804
805
0
  *is_temp = 0;
806
0
  return zend_get_properties_no_lazy_init(object);
807
0
}
808
809
HashTable *zend_lazy_object_get_gc(zend_object *zobj, zval **table, int *n)
810
0
{
811
0
  ZEND_ASSERT(zend_object_is_lazy(zobj));
812
813
0
  zend_lazy_object_info *info = zend_lazy_object_get_info(zobj);
814
0
  zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
815
816
0
  if (zend_lazy_object_initialized(zobj)) {
817
0
    ZEND_ASSERT(zend_object_is_lazy_proxy(zobj));
818
0
    zend_get_gc_buffer_add_obj(gc_buffer, info->u.instance);
819
0
    zend_get_gc_buffer_use(gc_buffer, table, n);
820
    /* Initialized proxy object can not have properties */
821
0
    return NULL;
822
0
  }
823
824
0
  zend_fcall_info_cache *fcc = &info->u.initializer.fcc;
825
0
  if (fcc->object) {
826
0
    zend_get_gc_buffer_add_obj(gc_buffer, fcc->object);
827
0
  }
828
0
  if (fcc->closure) {
829
0
    zend_get_gc_buffer_add_obj(gc_buffer, fcc->closure);
830
0
  }
831
0
  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
0
  zval *prop = zobj->properties_table;
836
0
  zval *end = prop + zobj->ce->default_properties_count;
837
0
  for ( ; prop < end; prop++) {
838
0
    zend_get_gc_buffer_add_zval(gc_buffer, prop);
839
0
  }
840
841
0
  zend_get_gc_buffer_use(gc_buffer, table, n);
842
0
  return NULL;
843
0
}
844
845
zend_property_info *zend_lazy_object_get_property_info_for_slot(zend_object *obj, zval *slot)
846
0
{
847
0
  ZEND_ASSERT(zend_object_is_lazy_proxy(obj));
848
849
0
  zend_property_info **table = obj->ce->properties_info_table;
850
0
  intptr_t prop_num = slot - obj->properties_table;
851
0
  if (prop_num >= 0 && prop_num < obj->ce->default_properties_count) {
852
0
    if (table[prop_num]) {
853
0
      return table[prop_num];
854
0
    } else {
855
0
      return zend_get_property_info_for_slot_slow(obj, slot);
856
0
    }
857
0
  }
858
859
0
  if (!zend_lazy_object_initialized(obj)) {
860
0
    return NULL;
861
0
  }
862
863
0
  obj = zend_lazy_object_get_instance(obj);
864
0
  return zend_get_property_info_for_slot(obj, slot);
865
0
}