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