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_object_handlers.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: Andi Gutmans <andi@php.net>                                 |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   |          Dmitry Stogov <dmitry@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend.h"
21
#include "zend_globals.h"
22
#include "zend_lazy_objects.h"
23
#include "zend_variables.h"
24
#include "zend_API.h"
25
#include "zend_objects.h"
26
#include "zend_objects_API.h"
27
#include "zend_object_handlers.h"
28
#include "zend_interfaces.h"
29
#include "zend_exceptions.h"
30
#include "zend_closures.h"
31
#include "zend_compile.h"
32
#include "zend_hash.h"
33
#include "zend_property_hooks.h"
34
#include "zend_observer.h"
35
36
#define DEBUG_OBJECT_HANDLERS 0
37
38
614
#define ZEND_WRONG_PROPERTY_OFFSET   0
39
4.09k
#define ZEND_HOOKED_PROPERTY_OFFSET 1
40
41
/* guard flags */
42
7.56k
#define IN_GET    ZEND_GUARD_PROPERTY_GET
43
3.91k
#define IN_SET    ZEND_GUARD_PROPERTY_SET
44
434
#define IN_UNSET  ZEND_GUARD_PROPERTY_UNSET
45
1.73k
#define IN_ISSET  ZEND_GUARD_PROPERTY_ISSET
46
#define IN_HOOK   ZEND_GUARD_PROPERTY_HOOK
47
48
static zend_arg_info zend_call_trampoline_arginfo[1] = {{0}};
49
static zend_arg_info zend_property_hook_arginfo[1] = {{0}};
50
51
static zend_always_inline bool zend_objects_check_stack_limit(void)
52
747
{
53
747
#ifdef ZEND_CHECK_STACK_LIMIT
54
747
  return zend_call_stack_overflowed(EG(stack_limit));
55
#else
56
  return false;
57
#endif
58
747
}
59
60
/*
61
  __X accessors explanation:
62
63
  if we have __get and property that is not part of the properties array is
64
  requested, we call __get handler. If it fails, we return uninitialized.
65
66
  if we have __set and property that is not part of the properties array is
67
  set, we call __set handler. If it fails, we do not change the array.
68
69
  for both handlers above, when we are inside __get/__set, no further calls for
70
  __get/__set for this property of this object will be made, to prevent endless
71
  recursion and enable accessors to change properties array.
72
73
  if we have __call and method which is not part of the class function table is
74
  called, we cal __call handler.
75
*/
76
77
ZEND_API HashTable *rebuild_object_properties_internal(zend_object *zobj) /* {{{ */
78
731k
{
79
731k
  if (!zobj->properties) {
80
730k
    zend_property_info *prop_info;
81
730k
    zend_class_entry *ce = zobj->ce;
82
730k
    int i;
83
84
730k
    zobj->properties = zend_new_array(ce->default_properties_count);
85
730k
    if (ce->default_properties_count) {
86
145k
      zend_hash_real_init_mixed(zobj->properties);
87
1.10M
      for (i = 0; i < ce->default_properties_count; i++) {
88
961k
        prop_info = ce->properties_info_table[i];
89
90
961k
        if (!prop_info) {
91
177
          continue;
92
177
        }
93
94
961k
        if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
95
2.14k
          HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
96
2.14k
        }
97
98
961k
        _zend_hash_append_ind(zobj->properties, prop_info->name,
99
961k
          OBJ_PROP(zobj, prop_info->offset));
100
961k
      }
101
145k
    }
102
730k
  }
103
104
731k
  return zobj->properties;
105
731k
}
106
/* }}} */
107
108
/* Implements the fast path for array cast */
109
ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /* {{{ */
110
125
{
111
125
  const zend_class_entry *ce = zobj->ce;
112
125
  HashTable *ht;
113
125
  zval* prop;
114
125
  int i;
115
116
125
  ZEND_ASSERT(!(zend_object_is_lazy_proxy(zobj) && zend_lazy_object_initialized(zobj)));
117
125
  ZEND_ASSERT(!zobj->properties);
118
125
  ht = zend_new_array(ce->default_properties_count);
119
125
  if (ce->default_properties_count) {
120
77
    zend_hash_real_init_mixed(ht);
121
182
    for (i = 0; i < ce->default_properties_count; i++) {
122
105
      const zend_property_info *prop_info = ce->properties_info_table[i];
123
124
105
      if (!prop_info) {
125
0
        continue;
126
0
      }
127
128
105
      prop = OBJ_PROP(zobj, prop_info->offset);
129
105
      if (UNEXPECTED(Z_TYPE_P(prop) == IS_UNDEF)) {
130
39
        continue;
131
39
      }
132
133
66
      if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
134
10
        prop = Z_REFVAL_P(prop);
135
10
      }
136
137
66
      Z_TRY_ADDREF_P(prop);
138
66
      _zend_hash_append(ht, prop_info->name, prop);
139
66
    }
140
77
  }
141
125
  return ht;
142
125
}
143
/* }}} */
144
145
ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */
146
1.65M
{
147
1.65M
  return zend_std_get_properties_ex(zobj);
148
1.65M
}
149
/* }}} */
150
151
/* Fetch properties HashTable without triggering lazy initialization */
152
ZEND_API HashTable *zend_get_properties_no_lazy_init(zend_object *zobj)
153
1.01k
{
154
1.01k
  if (zobj->handlers->get_properties == zend_std_get_properties) {
155
1.01k
    if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
156
1.01k
        && zend_lazy_object_initialized(zobj))) {
157
25
      zend_object *instance = zend_lazy_object_get_instance(zobj);
158
25
      return zend_get_properties_no_lazy_init(instance);
159
25
    }
160
161
987
    if (!zobj->properties) {
162
653
      rebuild_object_properties_internal(zobj);
163
653
    }
164
987
    return zobj->properties;
165
1.01k
  }
166
167
0
  ZEND_ASSERT(!zend_object_is_lazy(zobj));
168
169
0
  return zobj->handlers->get_properties(zobj);
170
0
}
171
172
ZEND_API HashTable *zend_std_get_gc(zend_object *zobj, zval **table, int *n) /* {{{ */
173
866k
{
174
866k
  if (zobj->handlers->get_properties != zend_std_get_properties) {
175
0
    *table = NULL;
176
0
    *n = 0;
177
0
    return zobj->handlers->get_properties(zobj);
178
866k
  } else {
179
866k
    if (UNEXPECTED(zend_object_is_lazy(zobj))) {
180
2.09k
      return zend_lazy_object_get_gc(zobj, table, n);
181
864k
    } else if (zobj->properties) {
182
784k
      *table = NULL;
183
784k
      *n = 0;
184
784k
      return zobj->properties;
185
784k
    } else {
186
79.7k
      *table = zobj->properties_table;
187
79.7k
      *n = zobj->ce->default_properties_count;
188
79.7k
      return NULL;
189
79.7k
    }
190
866k
  }
191
866k
}
192
/* }}} */
193
194
ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
195
15.7k
{
196
15.7k
  const zend_class_entry *ce = object->ce;
197
15.7k
  zval retval;
198
15.7k
  HashTable *ht;
199
200
15.7k
  if (!ce->__debugInfo) {
201
15.4k
    if (UNEXPECTED(zend_object_is_lazy(object))) {
202
1.00k
      return zend_lazy_object_debug_info(object, is_temp);
203
1.00k
    }
204
205
14.4k
    *is_temp = 0;
206
14.4k
    return object->handlers->get_properties(object);
207
15.4k
  }
208
209
302
  zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval);
210
302
  if (UNEXPECTED(Z_ISREF(retval))) {
211
6
    zend_unwrap_reference(&retval);
212
6
  }
213
302
  if (Z_TYPE(retval) == IS_ARRAY) {
214
277
    if (!Z_REFCOUNTED(retval)) {
215
29
      *is_temp = 1;
216
29
      return zend_array_dup(Z_ARRVAL(retval));
217
248
    } else if (Z_REFCOUNT(retval) <= 1) {
218
246
      *is_temp = 1;
219
246
      ht = Z_ARR(retval);
220
246
      return ht;
221
246
    } else {
222
2
      *is_temp = 0;
223
2
      zval_ptr_dtor(&retval);
224
2
      return Z_ARRVAL(retval);
225
2
    }
226
277
  } else if (Z_TYPE(retval) == IS_NULL) {
227
11
    zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, return an empty array instead",
228
11
      ZSTR_VAL(ce->name));
229
11
    *is_temp = 1;
230
11
    ht = zend_new_array(0);
231
11
    return ht;
232
11
  }
233
234
14
  zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
235
236
0
  return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
237
302
}
238
/* }}} */
239
240
static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
241
2.28k
{
242
2.28k
  zval member;
243
2.28k
  ZVAL_STR(&member, prop_name);
244
2.28k
  zend_call_known_instance_method_with_1_params(zobj->ce->__get, zobj, retval, &member);
245
2.28k
}
246
/* }}} */
247
248
static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */
249
1.20k
{
250
1.20k
  zval args[2];
251
1.20k
  ZVAL_STR(&args[0], prop_name);
252
1.20k
  ZVAL_COPY_VALUE(&args[1], value);
253
1.20k
  zend_call_known_instance_method(zobj->ce->__set, zobj, NULL, 2, args);
254
1.20k
}
255
/* }}} */
256
257
static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */
258
102
{
259
102
  zval member;
260
102
  ZVAL_STR(&member, prop_name);
261
102
  zend_call_known_instance_method_with_1_params(zobj->ce->__unset, zobj, NULL, &member);
262
102
}
263
/* }}} */
264
265
static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
266
508
{
267
508
  zval member;
268
508
  ZVAL_STR(&member, prop_name);
269
508
  zend_call_known_instance_method_with_1_params(zobj->ce->__isset, zobj, retval, &member);
270
508
}
271
/* }}} */
272
273
274
static zend_always_inline bool is_derived_class(const zend_class_entry *child_class, const zend_class_entry *parent_class) /* {{{ */
275
1.27M
{
276
1.27M
  child_class = child_class->parent;
277
1.90M
  while (child_class) {
278
1.26M
    if (child_class == parent_class) {
279
638k
      return 1;
280
638k
    }
281
628k
    child_class = child_class->parent;
282
628k
  }
283
284
638k
  return 0;
285
1.27M
}
286
/* }}} */
287
288
static zend_never_inline int is_protected_compatible_scope(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */
289
638k
{
290
638k
  return scope &&
291
638k
    (ce == scope || is_derived_class(ce, scope) || is_derived_class(scope, ce));
292
638k
}
293
/* }}} */
294
295
static zend_never_inline zend_property_info *zend_get_parent_private_property(const zend_class_entry *scope, const zend_class_entry *ce, zend_string *member) /* {{{ */
296
577
{
297
577
  zval *zv;
298
577
  zend_property_info *prop_info;
299
300
577
  if (scope != ce && scope && is_derived_class(ce, scope)) {
301
463
    zv = zend_hash_find(&scope->properties_info, member);
302
463
    if (zv != NULL) {
303
463
      prop_info = (zend_property_info*)Z_PTR_P(zv);
304
463
      if ((prop_info->flags & ZEND_ACC_PRIVATE)
305
456
       && prop_info->ce == scope) {
306
437
        return prop_info;
307
437
      }
308
463
    }
309
463
  }
310
140
  return NULL;
311
577
}
312
/* }}} */
313
314
static ZEND_COLD zend_never_inline void zend_bad_property_access(const zend_property_info *property_info, const zend_class_entry *ce, const zend_string *member) /* {{{ */
315
101
{
316
101
  zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
317
101
}
318
/* }}} */
319
320
static ZEND_COLD zend_never_inline void zend_bad_property_name(void) /* {{{ */
321
41
{
322
41
  zend_throw_error(NULL, "Cannot access property starting with \"\\0\"");
323
41
}
324
/* }}} */
325
326
static ZEND_COLD zend_never_inline void zend_forbidden_dynamic_property(
327
63
    const zend_class_entry *ce, const zend_string *member) {
328
63
  zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
329
63
    ZSTR_VAL(ce->name), ZSTR_VAL(member));
330
63
}
331
332
static ZEND_COLD zend_never_inline bool zend_deprecated_dynamic_property(
333
22.8k
    zend_object *obj, const zend_string *member) {
334
22.8k
  GC_ADDREF(obj);
335
22.8k
  zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
336
22.8k
    ZSTR_VAL(obj->ce->name), ZSTR_VAL(member));
337
22.8k
  if (UNEXPECTED(GC_DELREF(obj) == 0)) {
338
0
    const zend_class_entry *ce = obj->ce;
339
0
    zend_objects_store_del(obj);
340
0
    if (!EG(exception)) {
341
      /* We cannot continue execution and have to throw an exception */
342
0
      zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
343
0
        ZSTR_VAL(ce->name), ZSTR_VAL(member));
344
0
    }
345
0
    return 0;
346
0
  }
347
22.8k
  return 1;
348
22.8k
}
349
350
static ZEND_COLD zend_never_inline void zend_readonly_property_unset_error(
351
35
    const zend_class_entry *ce, const zend_string *member) {
352
35
  zend_throw_error(NULL, "Cannot unset readonly property %s::$%s",
353
35
    ZSTR_VAL(ce->name), ZSTR_VAL(member));
354
35
}
355
356
static zend_always_inline const zend_class_entry *get_fake_or_executed_scope(void)
357
32.6M
{
358
32.6M
  if (UNEXPECTED(EG(fake_scope))) {
359
32.6M
    return EG(fake_scope);
360
32.6M
  } else {
361
8.09k
    return zend_get_executed_scope();
362
8.09k
  }
363
32.6M
}
364
365
static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *ce, zend_string *member, int silent, void **cache_slot, const zend_property_info **info_ptr) /* {{{ */
366
30.0M
{
367
30.0M
  zval *zv;
368
30.0M
  zend_property_info *property_info;
369
30.0M
  uint32_t flags;
370
30.0M
  uintptr_t offset;
371
372
30.0M
  if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) {
373
15.8k
    *info_ptr = CACHED_PTR_EX(cache_slot + 2);
374
15.8k
    return (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
375
15.8k
  }
376
377
30.0M
  if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
378
29.9M
   || UNEXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
379
62.5k
    if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
380
70
      if (!silent) {
381
41
        zend_bad_property_name();
382
41
      }
383
70
      return ZEND_WRONG_PROPERTY_OFFSET;
384
70
    }
385
62.5k
dynamic:
386
62.5k
    if (cache_slot) {
387
10.6k
      CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
388
10.6k
      CACHE_PTR_EX(cache_slot + 2, NULL);
389
10.6k
    }
390
62.5k
    return ZEND_DYNAMIC_PROPERTY_OFFSET;
391
62.5k
  }
392
393
29.9M
  property_info = (zend_property_info*)Z_PTR_P(zv);
394
29.9M
  flags = property_info->flags;
395
396
29.9M
  if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
397
29.9M
    const zend_class_entry *scope = get_fake_or_executed_scope();
398
399
29.9M
    if (property_info->ce != scope) {
400
639k
      if (flags & ZEND_ACC_CHANGED) {
401
401
        zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
402
403
        /* If there is a public/protected instance property on ce, don't try to use a
404
         * private static property on scope. If both are static, prefer the static
405
         * property on scope. This will throw a static property notice, rather than
406
         * a visibility error. */
407
401
        if (p && (!(p->flags & ZEND_ACC_STATIC) || (flags & ZEND_ACC_STATIC))) {
408
307
          property_info = p;
409
307
          flags = property_info->flags;
410
307
          goto found;
411
307
        } else if (flags & ZEND_ACC_PUBLIC) {
412
43
          goto found;
413
43
        }
414
401
      }
415
638k
      if (flags & ZEND_ACC_PRIVATE) {
416
360
        if (property_info->ce != ce) {
417
61
          goto dynamic;
418
299
        } else {
419
544
wrong:
420
          /* Information was available, but we were denied access.  Error out. */
421
544
          if (!silent) {
422
83
            zend_bad_property_access(property_info, ce, member);
423
83
          }
424
544
          return ZEND_WRONG_PROPERTY_OFFSET;
425
299
        }
426
638k
      } else {
427
638k
        ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
428
638k
        if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
429
245
          goto wrong;
430
245
        }
431
638k
      }
432
638k
    }
433
29.9M
  }
434
435
29.9M
found:
436
29.9M
  if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
437
107
    if (!silent) {
438
63
      zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
439
63
    }
440
107
    return ZEND_DYNAMIC_PROPERTY_OFFSET;
441
107
  }
442
443
29.9M
  if (property_info->hooks) {
444
4.09k
    *info_ptr = property_info;
445
4.09k
    if (cache_slot) {
446
2.20k
      CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_HOOKED_PROPERTY_OFFSET);
447
2.20k
      CACHE_PTR_EX(cache_slot + 2, property_info);
448
2.20k
    }
449
4.09k
    return ZEND_HOOKED_PROPERTY_OFFSET;
450
4.09k
  }
451
452
29.9M
  offset = property_info->offset;
453
29.9M
  if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
454
878k
    property_info = NULL;
455
29.0M
  } else {
456
29.0M
    *info_ptr = property_info;
457
29.0M
  }
458
29.9M
  if (cache_slot) {
459
17.0k
    CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset);
460
17.0k
    CACHE_PTR_EX(cache_slot + 2, property_info);
461
17.0k
  }
462
29.9M
  return offset;
463
29.9M
}
464
/* }}} */
465
466
static ZEND_COLD void zend_wrong_offset(zend_class_entry *ce, zend_string *member) /* {{{ */
467
35
{
468
35
  const zend_property_info *dummy;
469
470
  /* Trigger the correct error */
471
35
  zend_get_property_offset(ce, member, 0, NULL, &dummy);
472
35
}
473
/* }}} */
474
475
ZEND_API zend_property_info *zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent) /* {{{ */
476
2.76M
{
477
2.76M
  zval *zv;
478
2.76M
  zend_property_info *property_info;
479
2.76M
  uint32_t flags;
480
481
2.76M
  if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
482
2.75M
   || EXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
483
8.76k
    if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
484
1.04k
      if (!silent) {
485
0
        zend_bad_property_name();
486
0
      }
487
1.04k
      return ZEND_WRONG_PROPERTY_INFO;
488
1.04k
    }
489
7.82k
dynamic:
490
7.82k
    return NULL;
491
8.76k
  }
492
493
2.75M
  property_info = (zend_property_info*)Z_PTR_P(zv);
494
2.75M
  flags = property_info->flags;
495
496
2.75M
  if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
497
2.75M
    const zend_class_entry *scope = get_fake_or_executed_scope();
498
2.75M
    if (property_info->ce != scope) {
499
632
      if (flags & ZEND_ACC_CHANGED) {
500
176
        zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
501
502
176
        if (p) {
503
110
          property_info = p;
504
110
          flags = property_info->flags;
505
110
          goto found;
506
110
        } else if (flags & ZEND_ACC_PUBLIC) {
507
10
          goto found;
508
10
        }
509
176
      }
510
512
      if (flags & ZEND_ACC_PRIVATE) {
511
462
        if (property_info->ce != ce) {
512
103
          goto dynamic;
513
359
        } else {
514
382
wrong:
515
          /* Information was available, but we were denied access.  Error out. */
516
382
          if (!silent) {
517
0
            zend_bad_property_access(property_info, ce, member);
518
0
          }
519
382
          return ZEND_WRONG_PROPERTY_INFO;
520
359
        }
521
462
      } else {
522
50
        ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
523
50
        if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
524
23
          goto wrong;
525
23
        }
526
50
      }
527
512
    }
528
2.75M
  }
529
530
2.75M
found:
531
2.75M
  if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
532
2.01k
    if (!silent) {
533
0
      zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
534
0
    }
535
2.01k
  }
536
2.75M
  return property_info;
537
2.75M
}
538
/* }}} */
539
540
ZEND_API zend_result zend_check_property_access(const zend_object *zobj, zend_string *prop_info_name, bool is_dynamic) /* {{{ */
541
2.21k
{
542
2.21k
  zend_property_info *property_info;
543
2.21k
  const char *class_name = NULL;
544
2.21k
  const char *prop_name;
545
2.21k
  zend_string *member;
546
2.21k
  size_t prop_name_len;
547
548
2.21k
  if (ZSTR_VAL(prop_info_name)[0] == 0) {
549
745
    if (is_dynamic) {
550
0
      return SUCCESS;
551
0
    }
552
553
745
    zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len);
554
745
    member = zend_string_init(prop_name, prop_name_len, 0);
555
745
    property_info = zend_get_property_info(zobj->ce, member, 1);
556
745
    zend_string_release_ex(member, 0);
557
745
    if (property_info == NULL || property_info == ZEND_WRONG_PROPERTY_INFO) {
558
429
      return FAILURE;
559
429
    }
560
561
316
    if (class_name[0] != '*') {
562
236
      if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
563
        /* we we're looking for a private prop but found a non private one of the same name */
564
5
        return FAILURE;
565
231
      } else if (strcmp(ZSTR_VAL(prop_info_name)+1, ZSTR_VAL(property_info->name)+1)) {
566
        /* we we're looking for a private prop but found a private one of the same name but another class */
567
52
        return FAILURE;
568
52
      }
569
236
    } else {
570
      /* We were looking for a protected property but found a private one
571
       * belonging to the parent class. */
572
80
      if (property_info->flags & ZEND_ACC_PRIVATE) {
573
5
        return FAILURE;
574
5
      }
575
75
      ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED);
576
75
    }
577
254
    return SUCCESS;
578
1.47k
  } else {
579
1.47k
    property_info = zend_get_property_info(zobj->ce, prop_info_name, 1);
580
1.47k
    if (property_info == NULL) {
581
168
      ZEND_ASSERT(is_dynamic);
582
168
      return SUCCESS;
583
1.30k
    } else if (property_info == ZEND_WRONG_PROPERTY_INFO) {
584
0
      return FAILURE;
585
0
    }
586
1.30k
    return (property_info->flags & ZEND_ACC_PUBLIC) ? SUCCESS : FAILURE;
587
1.47k
  }
588
2.21k
}
589
/* }}} */
590
591
3.25k
ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_property_info *prop_info) {
592
3.25k
  ZEND_ASSERT(prop_info->flags & ZEND_ACC_PPP_SET_MASK);
593
3.25k
  ZEND_ASSERT(!(prop_info->flags & ZEND_ACC_PUBLIC_SET));
594
3.25k
  const zend_class_entry *scope = get_fake_or_executed_scope();
595
3.25k
  if (prop_info->ce == scope) {
596
2.45k
    return true;
597
2.45k
  }
598
794
  return EXPECTED((prop_info->flags & ZEND_ACC_PROTECTED_SET)
599
3.25k
    && is_protected_compatible_scope(prop_info->prototype->ce, scope));
600
3.25k
}
601
602
662
static void zend_property_guard_dtor(zval *el) /* {{{ */ {
603
662
  uint32_t *ptr = (uint32_t*)Z_PTR_P(el);
604
662
  if (EXPECTED(!(((uintptr_t)ptr) & 1))) {
605
430
    efree_size(ptr, sizeof(uint32_t));
606
430
  }
607
662
}
608
/* }}} */
609
610
static zend_always_inline zval *zend_get_guard_value(zend_object *zobj)
611
6.74k
{
612
6.74k
  return zobj->properties_table + zobj->ce->default_properties_count;
613
6.74k
}
614
615
ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */
616
5.94k
{
617
5.94k
  HashTable *guards;
618
5.94k
  zval *zv;
619
5.94k
  uint32_t *ptr;
620
621
622
5.94k
  ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS);
623
5.94k
  zv = zend_get_guard_value(zobj);
624
5.94k
  if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
625
2.90k
    zend_string *str = Z_STR_P(zv);
626
2.90k
    if (EXPECTED(str == member) ||
627
        /* str and member don't necessarily have a pre-calculated hash value here */
628
1.94k
        EXPECTED(zend_string_equal_content(str, member))) {
629
1.94k
      return &Z_GUARD_P(zv);
630
1.94k
    } else if (EXPECTED(Z_GUARD_P(zv) == 0)) {
631
734
      zval_ptr_dtor_str(zv);
632
734
      ZVAL_STR_COPY(zv, member);
633
734
      return &Z_GUARD_P(zv);
634
734
    } else {
635
232
      ALLOC_HASHTABLE(guards);
636
232
      zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0);
637
      /* mark pointer as "special" using low bit */
638
232
      zend_hash_add_new_ptr(guards, str,
639
232
        (void*)(((uintptr_t)&Z_GUARD_P(zv)) | 1));
640
232
      zval_ptr_dtor_str(zv);
641
232
      ZVAL_ARR(zv, guards);
642
232
    }
643
3.03k
  } else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
644
1.64k
    guards = Z_ARRVAL_P(zv);
645
1.64k
    ZEND_ASSERT(guards != NULL);
646
1.64k
    zv = zend_hash_find(guards, member);
647
1.64k
    if (zv != NULL) {
648
1.44k
      return (uint32_t*)(((uintptr_t)Z_PTR_P(zv)) & ~1);
649
1.44k
    }
650
1.64k
  } else {
651
1.39k
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF);
652
1.39k
    ZVAL_STR_COPY(zv, member);
653
1.39k
    Z_GUARD_P(zv) &= ~ZEND_GUARD_PROPERTY_MASK;
654
1.39k
    return &Z_GUARD_P(zv);
655
1.39k
  }
656
  /* we have to allocate uint32_t separately because ht->arData may be reallocated */
657
430
  ptr = (uint32_t*)emalloc(sizeof(uint32_t));
658
430
  *ptr = 0;
659
430
  return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr);
660
5.94k
}
661
/* }}} */
662
663
ZEND_API uint32_t *zend_get_recursion_guard(zend_object *zobj)
664
17.6k
{
665
17.6k
  if (!(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
666
16.8k
    return NULL;
667
16.8k
  }
668
807
  zval *zv = zend_get_guard_value(zobj);
669
807
  return &Z_GUARD_P(zv);
670
17.6k
}
671
672
ZEND_COLD static void zend_typed_property_uninitialized_access(const zend_property_info *prop_info, zend_string *name)
673
254
{
674
254
  zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization",
675
254
    ZSTR_VAL(prop_info->ce->name),
676
254
    ZSTR_VAL(name));
677
254
}
678
679
static ZEND_FUNCTION(zend_parent_hook_get_trampoline);
680
static ZEND_FUNCTION(zend_parent_hook_set_trampoline);
681
682
static bool zend_is_in_hook(const zend_property_info *prop_info)
683
8.73k
{
684
8.73k
  const zend_execute_data *execute_data = EG(current_execute_data);
685
8.73k
  if (!execute_data || !EX(func) || !EX(func)->common.prop_info) {
686
6.57k
    return false;
687
6.57k
  }
688
689
2.16k
  const zend_property_info *parent_info = EX(func)->common.prop_info;
690
2.16k
  ZEND_ASSERT(prop_info->prototype && parent_info->prototype);
691
2.16k
  return prop_info->prototype == parent_info->prototype;
692
2.16k
}
693
694
static bool zend_should_call_hook(const zend_property_info *prop_info, const zend_object *obj)
695
8.22k
{
696
8.22k
  if (!zend_is_in_hook(prop_info)) {
697
7.07k
    return true;
698
7.07k
  }
699
700
  /* execute_data and This are guaranteed to be set if zend_is_in_hook() returns true. */
701
1.15k
  zend_object *parent_obj = Z_OBJ(EG(current_execute_data)->This);
702
1.15k
  if (parent_obj == obj) {
703
1.10k
    return false;
704
1.10k
  }
705
706
53
  if (zend_object_is_lazy_proxy(parent_obj)
707
33
   && zend_lazy_object_initialized(parent_obj)
708
33
   && zend_lazy_object_get_instance(parent_obj) == obj) {
709
33
    return false;
710
33
  }
711
712
20
  return true;
713
53
}
714
715
static ZEND_COLD void zend_throw_no_prop_backing_value_access(const zend_string *class_name, const zend_string *prop_name, bool is_read)
716
0
{
717
0
  zend_throw_error(NULL, "Must not %s virtual property %s::$%s",
718
0
    is_read ? "read from" : "write to",
719
0
    ZSTR_VAL(class_name), ZSTR_VAL(prop_name));
720
0
}
721
722
static bool zend_call_get_hook(
723
  const zend_property_info *prop_info, const zend_string *prop_name,
724
  zend_function *get, zend_object *zobj, zval *rv)
725
5.30k
{
726
5.30k
  if (!zend_should_call_hook(prop_info, zobj)) {
727
685
    if (UNEXPECTED(prop_info->flags & ZEND_ACC_VIRTUAL)) {
728
0
      zend_throw_no_prop_backing_value_access(zobj->ce->name, prop_name, /* is_read */ true);
729
0
    }
730
685
    return false;
731
685
  }
732
733
4.62k
  GC_ADDREF(zobj);
734
4.62k
  zend_call_known_instance_method_with_0_params(get, zobj, rv);
735
4.62k
  OBJ_RELEASE(zobj);
736
737
4.62k
  return true;
738
5.30k
}
739
740
ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
741
29.0M
{
742
29.0M
  zval *retval;
743
29.0M
  uintptr_t property_offset;
744
29.0M
  const zend_property_info *prop_info = NULL;
745
29.0M
  uint32_t *guard = NULL;
746
747
#if DEBUG_OBJECT_HANDLERS
748
  fprintf(stderr, "Read object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
749
#endif
750
751
  /* make zend_get_property_info silent if we have getter - we may want to use it */
752
29.0M
  property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot, &prop_info);
753
754
29.0M
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
755
28.9M
try_again:
756
28.9M
    retval = OBJ_PROP(zobj, property_offset);
757
758
28.9M
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))
759
2.19k
     && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
760
404
     && ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info))) {
761
404
      if (Z_TYPE_P(retval) == IS_OBJECT) {
762
        /* For objects, W/RW/UNSET fetch modes might not actually modify object.
763
         * Similar as with magic __get() allow them, but return the value as a copy
764
         * to make sure no actual modification is possible. */
765
151
        ZVAL_COPY(rv, retval);
766
151
        retval = rv;
767
151
        goto exit;
768
253
      } else if (Z_TYPE_P(retval) == IS_UNDEF && type == BP_VAR_UNSET) {
769
55
        retval = &EG(uninitialized_zval);
770
55
        goto exit;
771
55
      }
772
198
      if (prop_info->flags & ZEND_ACC_READONLY) {
773
127
        zend_readonly_property_indirect_modification_error(prop_info);
774
127
      } else {
775
71
        zend_asymmetric_visibility_property_modification_error(prop_info, "indirectly modify");
776
71
      }
777
198
      retval = &EG(uninitialized_zval);
778
198
      goto exit;
779
404
    }
780
28.9M
    if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) {
781
28.9M
      goto exit;
782
28.9M
    }
783
764
    if (UNEXPECTED(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT)) {
784
      /* Skip __get() for uninitialized typed properties */
785
598
      goto uninit_error;
786
598
    }
787
14.7k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
788
9.04k
    if (EXPECTED(zobj->properties != NULL)) {
789
1.90k
      if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
790
9
        uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
791
792
9
        if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
793
9
          Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
794
795
9
          if (EXPECTED(p->key == name) ||
796
0
                (EXPECTED(p->h == ZSTR_H(name)) &&
797
0
                 EXPECTED(p->key != NULL) &&
798
9
                 EXPECTED(zend_string_equal_content(p->key, name)))) {
799
9
            retval = &p->val;
800
9
            goto exit;
801
9
          }
802
9
        }
803
0
        CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
804
0
      }
805
1.89k
      retval = zend_hash_find(zobj->properties, name);
806
1.89k
      if (EXPECTED(retval)) {
807
883
        if (cache_slot) {
808
570
          uintptr_t idx = (char*)retval - (char*)zobj->properties->arData;
809
570
          CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
810
570
        }
811
883
        goto exit;
812
883
      }
813
1.89k
    }
814
9.04k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
815
5.47k
    zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET];
816
5.47k
    if (!get) {
817
236
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
818
8
        zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s",
819
8
          ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
820
8
        return &EG(uninitialized_zval);
821
8
      }
822
      /* Cache the fact that this hook has trivial read. This only applies to
823
       * BP_VAR_R and BP_VAR_IS fetches. */
824
228
      ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
825
826
228
      retval = OBJ_PROP(zobj, prop_info->offset);
827
228
      if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
828
        /* As hooked properties can't be unset, the only way to end up with an undef
829
         * value is via an uninitialized property. */
830
27
        ZEND_ASSERT(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT);
831
27
        goto uninit_error;
832
27
      }
833
834
201
      if (UNEXPECTED(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
835
19
        if (UNEXPECTED(Z_TYPE_P(retval) != IS_OBJECT)) {
836
19
          zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed",
837
19
            ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
838
19
          goto exit;
839
19
        }
840
0
        ZVAL_COPY(rv, retval);
841
0
        retval = rv;
842
0
      }
843
182
      goto exit;
844
201
    }
845
846
5.23k
    const zend_class_entry *ce = zobj->ce;
847
848
5.23k
    if (!zend_call_get_hook(prop_info, name, get, zobj, rv)) {
849
680
      if (EG(exception)) {
850
0
        return &EG(uninitialized_zval);
851
0
      }
852
853
      /* Reads from backing store can only occur in hooks, and hence will always remain simple. */
854
680
      const zend_execute_data *execute_data = EG(current_execute_data);
855
680
      if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_FETCH_OBJ_R && EX(opline)->op1_type == IS_UNUSED) {
856
364
        ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
857
364
      }
858
859
680
      property_offset = prop_info->offset;
860
680
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
861
573
        prop_info = NULL;
862
573
      }
863
680
      goto try_again;
864
680
    }
865
866
4.55k
    if (EXPECTED(cache_slot
867
4.55k
     && zend_execute_ex == execute_ex
868
4.55k
     && ce->default_object_handlers->read_property == zend_std_read_property
869
4.55k
     && !ce->create_object
870
4.55k
     && !zend_is_in_hook(prop_info)
871
4.55k
     && !(prop_info->hooks[ZEND_PROPERTY_HOOK_GET]->common.fn_flags & ZEND_ACC_RETURN_REFERENCE))) {
872
458
      ZEND_SET_PROPERTY_HOOK_SIMPLE_GET(cache_slot);
873
458
    }
874
875
4.55k
    if (Z_TYPE_P(rv) != IS_UNDEF) {
876
2.17k
      retval = rv;
877
2.17k
      if (!Z_ISREF_P(rv)
878
1.89k
       && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
879
80
       && UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
880
49
        zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed",
881
49
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
882
49
      }
883
2.38k
    } else {
884
2.38k
      retval = &EG(uninitialized_zval);
885
2.38k
    }
886
887
4.55k
    goto exit;
888
5.23k
  } else if (UNEXPECTED(EG(exception))) {
889
33
    retval = &EG(uninitialized_zval);
890
33
    goto exit;
891
33
  }
892
893
8.54k
  retval = &EG(uninitialized_zval);
894
895
  /* For initialized lazy proxies: if the real instance's magic method
896
   * guard is already set for this property, we are inside a recursive
897
   * call from the real instance's __get/__isset. Forward directly to
898
   * the real instance to avoid double invocation. (GH-21478) */
899
8.54k
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
900
8.54k
      && zend_lazy_object_initialized(zobj))) {
901
81
    zend_object *instance = zend_lazy_object_get_instance(zobj);
902
81
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
903
65
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
904
65
      uint32_t guard_type = ((type == BP_VAR_IS) && zobj->ce->__isset)
905
65
        ? IN_ISSET : IN_GET;
906
65
      if ((*instance_guard) & guard_type) {
907
23
        retval = zend_std_read_property(instance, name, type, cache_slot, rv);
908
23
        if (retval == &EG(uninitialized_zval)) {
909
15
          ZVAL_NULL(rv);
910
15
          retval = rv;
911
15
        }
912
23
        return retval;
913
23
      }
914
65
    }
915
81
  }
916
917
  /* magic isset */
918
8.52k
  if ((type == BP_VAR_IS) && zobj->ce->__isset) {
919
286
    zval tmp_result;
920
286
    guard = zend_get_property_guard(zobj, name);
921
922
286
    if (!((*guard) & IN_ISSET)) {
923
207
      GC_ADDREF(zobj);
924
925
207
      *guard |= IN_ISSET;
926
207
      zend_std_call_issetter(zobj, name, &tmp_result);
927
207
      *guard &= ~IN_ISSET;
928
929
207
      if (!zend_is_true(&tmp_result)) {
930
120
        retval = &EG(uninitialized_zval);
931
120
        OBJ_RELEASE(zobj);
932
120
        zval_ptr_dtor(&tmp_result);
933
120
        goto exit;
934
120
      }
935
936
87
      zval_ptr_dtor(&tmp_result);
937
87
      if (zobj->ce->__get && !((*guard) & IN_GET)) {
938
54
        goto call_getter;
939
54
      }
940
33
      OBJ_RELEASE(zobj);
941
79
    } else if (zobj->ce->__get && !((*guard) & IN_GET)) {
942
27
      goto call_getter_addref;
943
27
    }
944
8.23k
  } else if (zobj->ce->__get) {
945
    /* magic get */
946
2.68k
    guard = zend_get_property_guard(zobj, name);
947
2.68k
    if (!((*guard) & IN_GET)) {
948
      /* have getter - try with it! */
949
2.20k
call_getter_addref:
950
2.20k
      GC_ADDREF(zobj);
951
2.25k
call_getter:
952
2.25k
      *guard |= IN_GET; /* prevent circular getting */
953
2.25k
      zend_std_call_getter(zobj, name, rv);
954
2.25k
      *guard &= ~IN_GET;
955
956
2.25k
      if (Z_TYPE_P(rv) != IS_UNDEF) {
957
2.00k
        retval = rv;
958
2.00k
        if (!Z_ISREF_P(rv) &&
959
1.27k
            (type == BP_VAR_W || type == BP_VAR_RW  || type == BP_VAR_UNSET)) {
960
232
          if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
961
172
            zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
962
172
          }
963
232
        }
964
2.00k
      } else {
965
255
        retval = &EG(uninitialized_zval);
966
255
      }
967
968
2.25k
      if (prop_info) {
969
43
        zend_verify_prop_assignable_by_ref_ex(prop_info, retval, (zobj->ce->__get->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0, ZEND_VERIFY_PROP_ASSIGNABLE_BY_REF_CONTEXT_MAGIC_GET);
970
43
      }
971
972
2.25k
      OBJ_RELEASE(zobj);
973
2.25k
      goto exit;
974
2.20k
    } else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) {
975
      /* Trigger the correct error */
976
15
      zend_wrong_offset(zobj->ce, name);
977
15
      ZEND_ASSERT(EG(exception));
978
15
      retval = &EG(uninitialized_zval);
979
15
      goto exit;
980
15
    }
981
2.68k
  }
982
983
6.75k
uninit_error:
984
6.75k
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
985
577
    if (!prop_info || (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY)) {
986
562
      zend_object *instance = zend_lazy_object_init(zobj);
987
562
      if (!instance) {
988
84
        retval = &EG(uninitialized_zval);
989
84
        goto exit;
990
84
      }
991
992
478
      if (UNEXPECTED(guard && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS))) {
993
        /* Find which guard was used on zobj, so we can set the same
994
         * guard on instance. */
995
36
        uint32_t guard_type = (type == BP_VAR_IS) && zobj->ce->__isset
996
36
          ? IN_ISSET : IN_GET;
997
36
        guard = zend_get_property_guard(instance, name);
998
36
        if (!((*guard) & guard_type)) {
999
26
          (*guard) |= guard_type;
1000
26
          retval = zend_std_read_property(instance, name, type, cache_slot, rv);
1001
26
          (*guard) &= ~guard_type;
1002
26
          return retval;
1003
26
        }
1004
36
      }
1005
1006
452
      return zend_std_read_property(instance, name, type, cache_slot, rv);
1007
478
    }
1008
577
  }
1009
6.19k
  if (type != BP_VAR_IS) {
1010
5.64k
    if (prop_info) {
1011
165
      zend_typed_property_uninitialized_access(prop_info, name);
1012
5.48k
    } else {
1013
5.48k
      zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1014
5.48k
    }
1015
5.64k
  }
1016
6.19k
  retval = &EG(uninitialized_zval);
1017
1018
29.0M
exit:
1019
29.0M
  return retval;
1020
6.19k
}
1021
/* }}} */
1022
1023
1.13M
static zend_always_inline bool property_uses_strict_types(void) {
1024
1.13M
  const zend_execute_data *execute_data = EG(current_execute_data);
1025
1.13M
  return execute_data
1026
962k
    && execute_data->func
1027
962k
    && ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data));
1028
1.13M
}
1029
1030
static zval *forward_write_to_lazy_object(zend_object *zobj,
1031
    zend_string *name, zval *value, void **cache_slot, bool guarded)
1032
235
{
1033
235
  zval *variable_ptr;
1034
1035
  /* backup value as it may change during initialization */
1036
235
  zval backup;
1037
235
  ZVAL_COPY(&backup, value);
1038
1039
235
  zend_object *instance = zend_lazy_object_init(zobj);
1040
235
  if (UNEXPECTED(!instance)) {
1041
57
    zval_ptr_dtor(&backup);
1042
57
    return &EG(error_zval);
1043
57
  }
1044
1045
178
  if (UNEXPECTED(guarded && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS))) {
1046
22
    uint32_t *guard = zend_get_property_guard(instance, name);
1047
22
    if (!((*guard) & IN_SET)) {
1048
17
      (*guard) |= IN_SET;
1049
17
      variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot);
1050
17
      (*guard) &= ~IN_SET;
1051
17
      goto exit;
1052
17
    }
1053
22
  }
1054
1055
161
  variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot);
1056
1057
178
exit:
1058
178
  zval_ptr_dtor(&backup);
1059
1060
178
  if (variable_ptr == &backup) {
1061
0
    variable_ptr = value;
1062
0
  }
1063
1064
178
  return variable_ptr;
1065
161
}
1066
1067
ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zval *value, void **cache_slot) /* {{{ */
1068
1.00M
{
1069
1.00M
  zval *variable_ptr, tmp;
1070
1.00M
  uintptr_t property_offset;
1071
1.00M
  const zend_property_info *prop_info = NULL;
1072
1.00M
  uint32_t *guard = NULL;
1073
1.00M
  ZEND_ASSERT(!Z_ISREF_P(value));
1074
1075
1.00M
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__set != NULL), cache_slot, &prop_info);
1076
1077
1.00M
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1078
946k
try_again:
1079
946k
    variable_ptr = OBJ_PROP(zobj, property_offset);
1080
1081
946k
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1082
3.09k
      bool error;
1083
3.09k
      if (Z_TYPE_P(variable_ptr) != IS_UNDEF || (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) || !zobj->ce->__set) {
1084
3.07k
        error = true;
1085
3.07k
      } else {
1086
20
        guard = zend_get_property_guard(zobj, name);
1087
20
        error = (*guard) & IN_SET;
1088
20
      }
1089
3.09k
      if (error) {
1090
3.07k
        if ((prop_info->flags & ZEND_ACC_READONLY)
1091
2.33k
         && Z_TYPE_P(variable_ptr) != IS_UNDEF
1092
272
         && !(Z_PROP_FLAG_P(variable_ptr) & IS_PROP_REINITABLE)) {
1093
219
          zend_readonly_property_modification_error(prop_info);
1094
219
          variable_ptr = &EG(error_zval);
1095
219
          goto exit;
1096
219
        }
1097
2.85k
        if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) {
1098
172
          zend_asymmetric_visibility_property_modification_error(prop_info, "modify");
1099
172
          variable_ptr = &EG(error_zval);
1100
172
          goto exit;
1101
172
        }
1102
2.85k
      }
1103
3.09k
    }
1104
1105
945k
    if (Z_TYPE_P(variable_ptr) != IS_UNDEF) {
1106
938k
      Z_TRY_ADDREF_P(value);
1107
1108
938k
      if (prop_info) {
1109
178k
typed_property:
1110
178k
        ZVAL_COPY_VALUE(&tmp, value);
1111
        // Increase refcount to prevent object from being released in __toString()
1112
178k
        GC_ADDREF(zobj);
1113
178k
        bool type_matched = zend_verify_property_type(prop_info, &tmp, property_uses_strict_types());
1114
178k
        if (UNEXPECTED(GC_DELREF(zobj) == 0)) {
1115
12
          zend_object_released_while_assigning_to_property_error(prop_info);
1116
12
          zend_objects_store_del(zobj);
1117
12
          zval_ptr_dtor(&tmp);
1118
12
          variable_ptr = &EG(error_zval);
1119
12
          goto exit;
1120
12
        }
1121
178k
        if (UNEXPECTED(!type_matched)) {
1122
358
          zval_ptr_dtor(&tmp);
1123
358
          variable_ptr = &EG(error_zval);
1124
358
          goto exit;
1125
358
        }
1126
177k
        Z_PROP_FLAG_P(variable_ptr) &= ~(IS_PROP_UNINIT|IS_PROP_REINITABLE);
1127
177k
        value = &tmp;
1128
177k
      }
1129
1130
955k
found:;
1131
955k
      zend_refcounted *garbage = NULL;
1132
1133
955k
      variable_ptr = zend_assign_to_variable_ex(
1134
955k
        variable_ptr, value, IS_TMP_VAR, property_uses_strict_types(), &garbage);
1135
1136
955k
      if (garbage) {
1137
5.51k
        if (GC_DELREF(garbage) == 0) {
1138
3.39k
          zend_execute_data *execute_data = EG(current_execute_data);
1139
          // Assign to result variable before calling the destructor as it may release the object
1140
3.39k
          if (execute_data
1141
976
           && EX(func)
1142
976
           && ZEND_USER_CODE(EX(func)->common.type)
1143
245
           && EX(opline)
1144
245
           && EX(opline)->opcode == ZEND_ASSIGN_OBJ
1145
245
           && EX(opline)->result_type) {
1146
71
            ZVAL_COPY_DEREF(EX_VAR(EX(opline)->result.var), variable_ptr);
1147
71
            variable_ptr = NULL;
1148
71
          }
1149
3.39k
          rc_dtor_func(garbage);
1150
3.39k
        } else {
1151
2.11k
          gc_check_possible_root_no_ref(garbage);
1152
2.11k
        }
1153
5.51k
      }
1154
955k
      goto exit;
1155
938k
    }
1156
7.23k
    if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) {
1157
7.07k
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1158
741
        if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_LAZY) {
1159
196
          goto lazy_init;
1160
196
        }
1161
741
      }
1162
      /* Writes to uninitialized typed properties bypass __set(). */
1163
6.87k
      goto write_std_property;
1164
7.07k
    }
1165
57.3k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
1166
54.5k
    if (EXPECTED(zobj->properties != NULL)) {
1167
22.8k
      if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1168
45
        if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1169
45
          GC_DELREF(zobj->properties);
1170
45
        }
1171
45
        zobj->properties = zend_array_dup(zobj->properties);
1172
45
      }
1173
22.8k
      if ((variable_ptr = zend_hash_find(zobj->properties, name)) != NULL) {
1174
10.5k
        Z_TRY_ADDREF_P(value);
1175
10.5k
        goto found;
1176
10.5k
      }
1177
22.8k
    }
1178
54.5k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1179
2.63k
    zend_function *set = prop_info->hooks[ZEND_PROPERTY_HOOK_SET];
1180
1181
2.63k
    if (!set) {
1182
55
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
1183
11
        zend_throw_error(NULL, "Cannot write to get-only virtual property %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1184
11
        variable_ptr = &EG(error_zval);
1185
11
        goto exit;
1186
11
      }
1187
44
      ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot);
1188
44
      property_offset = prop_info->offset;
1189
44
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1190
29
        prop_info = NULL;
1191
29
      }
1192
44
      goto try_again;
1193
55
    }
1194
1195
2.58k
    if (!zend_should_call_hook(prop_info, zobj)) {
1196
347
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
1197
0
        zend_throw_no_prop_backing_value_access(zobj->ce->name, name, /* is_read */ false);
1198
0
        variable_ptr = &EG(error_zval);
1199
0
        goto exit;
1200
0
      }
1201
1202
      /* Writes to backing store can only occur in hooks, and hence will always remain simple. */
1203
347
      zend_execute_data *execute_data = EG(current_execute_data);
1204
347
      if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_ASSIGN_OBJ && EX(opline)->op1_type == IS_UNUSED) {
1205
324
        ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot);
1206
324
      }
1207
1208
347
      property_offset = prop_info->offset;
1209
347
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1210
178
        prop_info = NULL;
1211
178
      }
1212
347
      goto try_again;
1213
347
    }
1214
1215
2.23k
    if (UNEXPECTED(prop_info->flags & ZEND_ACC_PPP_SET_MASK
1216
2.23k
     && !zend_asymmetric_property_has_set_access(prop_info))) {
1217
5
      zend_asymmetric_visibility_property_modification_error(prop_info, "modify");
1218
5
      variable_ptr = &EG(error_zval);
1219
5
      goto exit;
1220
5
    }
1221
1222
2.23k
    GC_ADDREF(zobj);
1223
2.23k
    zend_call_known_instance_method_with_1_params(set, zobj, NULL, value);
1224
2.23k
    OBJ_RELEASE(zobj);
1225
1226
2.23k
    variable_ptr = value;
1227
2.23k
    goto exit;
1228
2.23k
  } else if (UNEXPECTED(EG(exception))) {
1229
43
    variable_ptr = &EG(error_zval);
1230
43
    goto exit;
1231
43
  }
1232
1233
  /* For initialized lazy proxies: if the real instance's __set guard
1234
   * is already set, we are inside a recursive call from the real
1235
   * instance's __set. Forward directly to avoid double invocation. */
1236
44.2k
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
1237
44.2k
      && zend_lazy_object_initialized(zobj))) {
1238
38
    zend_object *instance = zend_lazy_object_get_instance(zobj);
1239
38
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
1240
38
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
1241
38
      if ((*instance_guard) & IN_SET) {
1242
19
        return zend_std_write_property(instance, name, value, cache_slot);
1243
19
      }
1244
38
    }
1245
38
  }
1246
1247
  /* magic set */
1248
44.2k
  if (zobj->ce->__set) {
1249
1.38k
    if (!guard) {
1250
1.37k
      guard = zend_get_property_guard(zobj, name);
1251
1.37k
    }
1252
1253
1.38k
    if (!((*guard) & IN_SET)) {
1254
1.20k
      GC_ADDREF(zobj);
1255
1.20k
      (*guard) |= IN_SET; /* prevent circular setting */
1256
1.20k
      zend_std_call_setter(zobj, name, value);
1257
1.20k
      (*guard) &= ~IN_SET;
1258
1.20k
      OBJ_RELEASE(zobj);
1259
1.20k
      variable_ptr = value;
1260
1.20k
    } else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) {
1261
172
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1262
22
        return forward_write_to_lazy_object(zobj, name, value,
1263
22
            cache_slot, /* guarded */ true);
1264
22
      }
1265
1266
150
      goto write_std_property;
1267
172
    } else {
1268
      /* Trigger the correct error */
1269
11
      zend_wrong_offset(zobj->ce, name);
1270
11
      ZEND_ASSERT(EG(exception));
1271
11
      variable_ptr = &EG(error_zval);
1272
11
      goto exit;
1273
11
    }
1274
42.8k
  } else {
1275
42.8k
    ZEND_ASSERT(!IS_WRONG_PROPERTY_OFFSET(property_offset));
1276
42.8k
    if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1277
17
      goto lazy_init;
1278
17
    }
1279
49.8k
write_std_property:
1280
49.8k
    if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1281
6.98k
      variable_ptr = OBJ_PROP(zobj, property_offset);
1282
1283
6.98k
      Z_TRY_ADDREF_P(value);
1284
6.98k
      if (prop_info) {
1285
6.60k
        goto typed_property;
1286
6.60k
      }
1287
1288
381
      ZVAL_COPY_VALUE(variable_ptr, value);
1289
42.9k
    } else {
1290
42.9k
      if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1291
48
        zend_forbidden_dynamic_property(zobj->ce, name);
1292
48
        variable_ptr = &EG(error_zval);
1293
48
        goto exit;
1294
48
      }
1295
42.8k
      if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) {
1296
22.1k
        if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) {
1297
0
          variable_ptr = &EG(error_zval);
1298
0
          goto exit;
1299
0
        }
1300
22.1k
      }
1301
1302
42.8k
      Z_TRY_ADDREF_P(value);
1303
42.8k
      variable_ptr = zend_hash_add_new(zend_std_get_properties(zobj), name, value);
1304
42.8k
    }
1305
49.8k
  }
1306
1307
1.00M
exit:
1308
1.00M
  return variable_ptr;
1309
1310
213
lazy_init:
1311
213
  return forward_write_to_lazy_object(zobj, name, value, cache_slot,
1312
213
      /* guarded */ false);
1313
44.2k
}
1314
/* }}} */
1315
1316
static ZEND_COLD zend_never_inline void zend_bad_array_access(const zend_class_entry *ce) /* {{{ */
1317
42
{
1318
42
  zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
1319
42
}
1320
/* }}} */
1321
1322
ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
1323
1.15k
{
1324
1.15k
  const zend_class_entry *ce = object->ce;
1325
1.15k
  zval tmp_offset;
1326
1327
  /* arrayaccess_funcs_ptr is set if (and only if) the class implements zend_ce_arrayaccess */
1328
1.15k
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1329
1.15k
  if (EXPECTED(funcs)) {
1330
1.12k
    if (offset == NULL) {
1331
      /* [] construct */
1332
12
      ZVAL_NULL(&tmp_offset);
1333
1.11k
    } else {
1334
1.11k
      ZVAL_COPY_DEREF(&tmp_offset, offset);
1335
1.11k
    }
1336
1337
1.12k
    GC_ADDREF(object);
1338
1.12k
    if (type == BP_VAR_IS) {
1339
161
      zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, rv, &tmp_offset);
1340
161
      if (UNEXPECTED(Z_ISUNDEF_P(rv))) {
1341
2
        OBJ_RELEASE(object);
1342
2
        zval_ptr_dtor(&tmp_offset);
1343
2
        return NULL;
1344
2
      }
1345
159
      if (!i_zend_is_true(rv)) {
1346
77
        OBJ_RELEASE(object);
1347
77
        zval_ptr_dtor(&tmp_offset);
1348
77
        zval_ptr_dtor(rv);
1349
77
        return &EG(uninitialized_zval);
1350
77
      }
1351
82
      zval_ptr_dtor(rv);
1352
82
    }
1353
1354
1.04k
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, rv, &tmp_offset);
1355
1356
1.04k
    OBJ_RELEASE(object);
1357
1.04k
    zval_ptr_dtor(&tmp_offset);
1358
1359
1.04k
    if (UNEXPECTED(Z_TYPE_P(rv) == IS_UNDEF)) {
1360
13
      if (UNEXPECTED(!EG(exception))) {
1361
0
        zend_throw_error(NULL, "Undefined offset for object of type %s used as array", ZSTR_VAL(ce->name));
1362
0
      }
1363
13
      return NULL;
1364
13
    }
1365
1.03k
    return rv;
1366
1.04k
  } else {
1367
32
      zend_bad_array_access(ce);
1368
32
    return NULL;
1369
32
  }
1370
1.15k
}
1371
/* }}} */
1372
1373
ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
1374
344
{
1375
344
  const zend_class_entry *ce = object->ce;
1376
344
  zval tmp_offset;
1377
1378
344
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1379
344
  if (EXPECTED(funcs)) {
1380
339
    if (!offset) {
1381
28
      ZVAL_NULL(&tmp_offset);
1382
311
    } else {
1383
311
      ZVAL_COPY_DEREF(&tmp_offset, offset);
1384
311
    }
1385
339
    GC_ADDREF(object);
1386
339
    zend_call_known_instance_method_with_2_params(funcs->zf_offsetset, object, NULL, &tmp_offset, value);
1387
339
    OBJ_RELEASE(object);
1388
339
    zval_ptr_dtor(&tmp_offset);
1389
339
  } else {
1390
5
      zend_bad_array_access(ce);
1391
5
  }
1392
344
}
1393
/* }}} */
1394
1395
// todo: make zend_std_has_dimension return bool as well
1396
ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
1397
211
{
1398
211
  const zend_class_entry *ce = object->ce;
1399
211
  zval retval, tmp_offset;
1400
211
  bool result;
1401
1402
211
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1403
211
  if (EXPECTED(funcs)) {
1404
211
    ZVAL_COPY_DEREF(&tmp_offset, offset);
1405
211
    GC_ADDREF(object);
1406
211
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, &retval, &tmp_offset);
1407
211
    result = i_zend_is_true(&retval);
1408
211
    zval_ptr_dtor(&retval);
1409
211
    if (check_empty && result && EXPECTED(!EG(exception))) {
1410
21
      zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, &retval, &tmp_offset);
1411
21
      result = i_zend_is_true(&retval);
1412
21
      zval_ptr_dtor(&retval);
1413
21
    }
1414
211
    OBJ_RELEASE(object);
1415
211
    zval_ptr_dtor(&tmp_offset);
1416
211
  } else {
1417
0
      zend_bad_array_access(ce);
1418
0
    return 0;
1419
0
  }
1420
1421
211
  return result;
1422
211
}
1423
/* }}} */
1424
1425
ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *name, int type, void **cache_slot) /* {{{ */
1426
9.29k
{
1427
9.29k
  zval *retval = NULL;
1428
9.29k
  uintptr_t property_offset;
1429
9.29k
  const zend_property_info *prop_info = NULL;
1430
1431
9.29k
  ZEND_ASSERT(type != BP_VAR_R && type != BP_VAR_IS);
1432
1433
#if DEBUG_OBJECT_HANDLERS
1434
  fprintf(stderr, "Ptr object #%d property: %s\n", zobj->handle, ZSTR_VAL(name));
1435
#endif
1436
1437
9.29k
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__get != NULL), cache_slot, &prop_info);
1438
1439
9.29k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1440
5.53k
try_again:
1441
5.53k
    retval = OBJ_PROP(zobj, property_offset);
1442
5.53k
    if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
1443
790
      if (EXPECTED(!zobj->ce->__get) ||
1444
30
          UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET) ||
1445
767
          UNEXPECTED(prop_info && (Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT))) {
1446
767
        if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY))) {
1447
218
          bool guarded = zobj->ce->__get
1448
0
            && (*zend_get_property_guard(zobj, name) & IN_GET);
1449
218
          zend_object *instance = zend_lazy_object_init(zobj);
1450
218
          if (!instance) {
1451
13
            return &EG(error_zval);
1452
13
          }
1453
1454
205
          if (guarded && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
1455
0
            uint32_t *guard = zend_get_property_guard(instance, name);
1456
0
            if (!(*guard & IN_GET)) {
1457
0
              (*guard) |= IN_GET;
1458
0
              retval = zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1459
0
              (*guard) &= ~IN_GET;
1460
0
              return retval;
1461
0
            }
1462
0
          }
1463
1464
205
          return zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1465
205
        }
1466
549
        if (UNEXPECTED(type == BP_VAR_RW)) {
1467
105
          if (prop_info) {
1468
89
            zend_typed_property_uninitialized_access(prop_info, name);
1469
89
            retval = &EG(error_zval);
1470
89
          } else {
1471
16
            zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1472
            /* An error handler may set the property */
1473
16
             if (EXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
1474
16
              ZVAL_NULL(retval);
1475
16
             }
1476
16
          }
1477
444
        } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1478
157
          if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) {
1479
137
            retval = NULL;
1480
137
          }
1481
287
        } else if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
1482
15
          ZVAL_NULL(retval);
1483
15
        }
1484
549
      } else {
1485
        /* we do have getter - fail and let it try again with usual get/set */
1486
23
        retval = NULL;
1487
23
      }
1488
4.74k
    } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1489
492
      if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) {
1490
454
        retval = NULL;
1491
454
      }
1492
492
    }
1493
5.53k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
1494
3.34k
    if (EXPECTED(zobj->properties)) {
1495
1.58k
      if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1496
30
        if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1497
30
          GC_DELREF(zobj->properties);
1498
30
        }
1499
30
        zobj->properties = zend_array_dup(zobj->properties);
1500
30
      }
1501
1.58k
        if (EXPECTED((retval = zend_hash_find(zobj->properties, name)) != NULL)) {
1502
1.10k
        return retval;
1503
1.10k
        }
1504
1.58k
    }
1505
2.23k
    if (EXPECTED(!zobj->ce->__get) ||
1506
1.55k
        UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) {
1507
1.55k
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1508
95
        bool guarded = (zobj->ce->__get != NULL);
1509
95
        zend_object *instance = zend_lazy_object_init(zobj);
1510
95
        if (!instance) {
1511
12
          return &EG(error_zval);
1512
12
        }
1513
1514
83
        if (guarded && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
1515
22
          uint32_t *guard = zend_get_property_guard(instance, name);
1516
22
          if (!(*guard & IN_GET)) {
1517
22
            (*guard) |= IN_GET;
1518
22
            retval = zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1519
22
            (*guard) &= ~IN_GET;
1520
22
            return retval;
1521
22
          }
1522
22
        }
1523
1524
61
        return zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1525
83
      }
1526
1.46k
      if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1527
15
        zend_forbidden_dynamic_property(zobj->ce, name);
1528
15
        return &EG(error_zval);
1529
15
      }
1530
1.44k
      if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) {
1531
756
        if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) {
1532
0
          return &EG(error_zval);
1533
0
        }
1534
756
      }
1535
1.44k
      if (UNEXPECTED(!zobj->properties)) {
1536
1.04k
        rebuild_object_properties_internal(zobj);
1537
1.04k
      }
1538
1.44k
      if (UNEXPECTED(type == BP_VAR_RW)) {
1539
262
        zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1540
262
      }
1541
1.44k
      retval = zend_hash_add(zobj->properties, name, &EG(uninitialized_zval));
1542
1.44k
    }
1543
2.23k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1544
428
    if (!(prop_info->flags & ZEND_ACC_VIRTUAL) && !zend_should_call_hook(prop_info, zobj)) {
1545
104
      property_offset = prop_info->offset;
1546
104
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1547
97
        prop_info = NULL;
1548
97
      }
1549
104
      goto try_again;
1550
104
    }
1551
428
  } else if (zobj->ce->__get == NULL) {
1552
10
    retval = &EG(error_zval);
1553
10
  }
1554
1555
7.85k
  return retval;
1556
9.29k
}
1557
/* }}} */
1558
1559
ZEND_API void zend_std_unset_property(zend_object *zobj, zend_string *name, void **cache_slot) /* {{{ */
1560
1.26k
{
1561
1.26k
  uintptr_t property_offset;
1562
1.26k
  const zend_property_info *prop_info = NULL;
1563
1.26k
  uint32_t *guard = NULL;
1564
1565
1.26k
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__unset != NULL), cache_slot, &prop_info);
1566
1567
1.26k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1568
774
    zval *slot = OBJ_PROP(zobj, property_offset);
1569
1570
774
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1571
229
      bool error;
1572
229
      if (Z_TYPE_P(slot) != IS_UNDEF || Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT || !zobj->ce->__unset) {
1573
204
        error = true;
1574
204
      } else {
1575
25
        guard = zend_get_property_guard(zobj, name);
1576
25
        error = (*guard) & IN_UNSET;
1577
25
      }
1578
229
      if (error) {
1579
209
        if ((prop_info->flags & ZEND_ACC_READONLY)
1580
79
         && Z_TYPE_P(slot) != IS_UNDEF
1581
44
         && !(Z_PROP_FLAG_P(slot) & IS_PROP_REINITABLE)) {
1582
35
          zend_readonly_property_unset_error(prop_info->ce, name);
1583
35
          return;
1584
35
        }
1585
174
        if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) {
1586
79
          zend_asymmetric_visibility_property_modification_error(prop_info, "unset");
1587
79
          return;
1588
79
        }
1589
174
      }
1590
229
    }
1591
1592
660
    if (Z_TYPE_P(slot) != IS_UNDEF) {
1593
517
      if (UNEXPECTED(Z_ISREF_P(slot)) &&
1594
83
          (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(slot)))) {
1595
83
        if (prop_info) {
1596
77
          ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(slot), prop_info);
1597
77
        }
1598
83
      }
1599
517
      zval tmp;
1600
517
      ZVAL_COPY_VALUE(&tmp, slot);
1601
517
      ZVAL_UNDEF(slot);
1602
517
      zval_ptr_dtor(&tmp);
1603
517
      if (zobj->properties) {
1604
190
        HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
1605
190
      }
1606
517
      return;
1607
517
    }
1608
143
    if (UNEXPECTED(Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT)) {
1609
118
      if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(slot) & IS_PROP_LAZY))) {
1610
26
        zobj = zend_lazy_object_init(zobj);
1611
26
        if (!zobj) {
1612
2
          return;
1613
2
        }
1614
24
        zend_std_unset_property(zobj, name, cache_slot);
1615
24
        return;
1616
26
      }
1617
1618
      /* Reset the IS_PROP_UNINIT flag, if it exists and bypass __unset(). */
1619
92
      Z_PROP_FLAG_P(slot) = 0;
1620
92
      return;
1621
118
    }
1622
493
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))
1623
436
   && EXPECTED(zobj->properties != NULL)) {
1624
288
    if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1625
0
      if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1626
0
        GC_DELREF(zobj->properties);
1627
0
      }
1628
0
      zobj->properties = zend_array_dup(zobj->properties);
1629
0
    }
1630
288
    if (EXPECTED(zend_hash_del(zobj->properties, name) != FAILURE)) {
1631
185
      return;
1632
185
    }
1633
288
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1634
34
    zend_throw_error(NULL, "Cannot unset hooked property %s::$%s",
1635
34
      ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1636
34
    return;
1637
171
  } else if (UNEXPECTED(EG(exception))) {
1638
3
    return;
1639
3
  }
1640
1641
  /* For initialized lazy proxies: if the real instance's __unset guard
1642
   * is already set, we are inside a recursive call from the real
1643
   * instance's __unset. Forward directly to avoid double invocation. */
1644
296
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
1645
296
      && zend_lazy_object_initialized(zobj))) {
1646
12
    zend_object *instance = zend_lazy_object_get_instance(zobj);
1647
12
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
1648
12
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
1649
12
      if ((*instance_guard) & IN_UNSET) {
1650
5
        zend_std_unset_property(instance, name, cache_slot);
1651
5
        return;
1652
5
      }
1653
12
    }
1654
12
  }
1655
1656
  /* magic unset */
1657
291
  if (zobj->ce->__unset) {
1658
158
    if (!guard) {
1659
138
      guard = zend_get_property_guard(zobj, name);
1660
138
    }
1661
158
    if (!((*guard) & IN_UNSET)) {
1662
      /* have unsetter - try with it! */
1663
102
      GC_ADDREF(zobj);
1664
102
      (*guard) |= IN_UNSET; /* prevent circular unsetting */
1665
102
      zend_std_call_unsetter(zobj, name);
1666
102
      (*guard) &= ~IN_UNSET;
1667
102
      OBJ_RELEASE(zobj);
1668
102
      return;
1669
102
    } else if (UNEXPECTED(IS_WRONG_PROPERTY_OFFSET(property_offset))) {
1670
      /* Trigger the correct error */
1671
9
      zend_wrong_offset(zobj->ce, name);
1672
9
      ZEND_ASSERT(EG(exception));
1673
9
      return;
1674
47
    } else {
1675
      /* Nothing to do: The property already does not exist. */
1676
47
    }
1677
158
  }
1678
1679
180
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1680
46
    zobj = zend_lazy_object_init(zobj);
1681
46
    if (!zobj) {
1682
2
      return;
1683
2
    }
1684
1685
44
    if (UNEXPECTED(guard && zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
1686
15
      guard = zend_get_property_guard(zobj, name);
1687
15
      if (!((*guard) & IN_UNSET)) {
1688
10
        (*guard) |= IN_UNSET;
1689
10
        zend_std_unset_property(zobj, name, cache_slot);
1690
10
        (*guard) &= ~IN_UNSET;
1691
10
        return;
1692
10
      }
1693
15
    }
1694
1695
34
    zend_std_unset_property(zobj, name, cache_slot);
1696
34
    return;
1697
44
  }
1698
180
}
1699
/* }}} */
1700
1701
ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset) /* {{{ */
1702
186
{
1703
186
  const zend_class_entry *ce = object->ce;
1704
186
  zval tmp_offset;
1705
1706
186
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1707
186
  if (EXPECTED(funcs)) {
1708
181
    ZVAL_COPY_DEREF(&tmp_offset, offset);
1709
181
    GC_ADDREF(object);
1710
181
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetunset, object, NULL, &tmp_offset);
1711
181
    OBJ_RELEASE(object);
1712
181
    zval_ptr_dtor(&tmp_offset);
1713
181
  } else {
1714
5
      zend_bad_array_access(ce);
1715
5
  }
1716
186
}
1717
/* }}} */
1718
1719
static zend_never_inline zend_function *zend_get_parent_private_method(const zend_class_entry *scope, const zend_class_entry *ce, zend_string *function_name) /* {{{ */
1720
63
{
1721
63
  zval *func;
1722
63
  zend_function *fbc;
1723
1724
63
  if (scope != ce && scope && is_derived_class(ce, scope)) {
1725
48
    func = zend_hash_find(&scope->function_table, function_name);
1726
48
    if (func != NULL) {
1727
48
      fbc = Z_FUNC_P(func);
1728
48
      if ((fbc->common.fn_flags & ZEND_ACC_PRIVATE)
1729
48
       && fbc->common.scope == scope) {
1730
38
        return fbc;
1731
38
      }
1732
48
    }
1733
48
  }
1734
25
  return NULL;
1735
63
}
1736
/* }}} */
1737
1738
/* Ensures that we're allowed to call a protected method.
1739
 */
1740
ZEND_API bool zend_check_protected(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */
1741
705
{
1742
705
  const zend_class_entry *fbc_scope = ce;
1743
1744
  /* Is the context that's calling the function, the same as one of
1745
   * the function's parents?
1746
   */
1747
1.37k
  while (fbc_scope) {
1748
800
    if (fbc_scope==scope) {
1749
135
      return 1;
1750
135
    }
1751
665
    fbc_scope = fbc_scope->parent;
1752
665
  }
1753
1754
  /* Is the function's scope the same as our current object context,
1755
   * or any of the parents of our context?
1756
   */
1757
977
  while (scope) {
1758
703
    if (scope==ce) {
1759
296
      return 1;
1760
296
    }
1761
407
    scope = scope->parent;
1762
407
  }
1763
274
  return 0;
1764
570
}
1765
/* }}} */
1766
1767
ZEND_API ZEND_ATTRIBUTE_NONNULL zend_function *zend_get_call_trampoline_func(
1768
  const zend_function *fbc, zend_string *method_name) /* {{{ */
1769
5.13k
{
1770
5.13k
  size_t mname_len;
1771
5.13k
  zend_op_array *func;
1772
  /* We use non-NULL value to avoid useless run_time_cache allocation.
1773
   * The low bit must be zero, to not be interpreted as a MAP_PTR offset.
1774
   */
1775
5.13k
  static const void *dummy = (void*)(intptr_t)2;
1776
1777
5.13k
  if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
1778
3.80k
    func = &EG(trampoline).op_array;
1779
3.80k
  } else {
1780
1.33k
    func = ecalloc(1, sizeof(zend_op_array));
1781
1.33k
  }
1782
1783
5.13k
  func->type = ZEND_USER_FUNCTION;
1784
5.13k
  func->arg_flags[0] = 0;
1785
5.13k
  func->arg_flags[1] = 0;
1786
5.13k
  func->arg_flags[2] = 0;
1787
5.13k
  func->fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE
1788
5.13k
    | ZEND_ACC_PUBLIC
1789
5.13k
    | ZEND_ACC_VARIADIC
1790
5.13k
    | (fbc->common.fn_flags & (ZEND_ACC_RETURN_REFERENCE|ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD|ZEND_ACC_STATIC));
1791
5.13k
  func->fn_flags2 = 0;
1792
  /* Attributes outlive the trampoline because they are created by the compiler. */
1793
5.13k
  func->attributes = fbc->common.attributes;
1794
5.13k
  func->opcodes = &EG(call_trampoline_op);
1795
5.13k
  ZEND_MAP_PTR_INIT(func->run_time_cache, (void**)dummy);
1796
5.13k
  func->scope = fbc->common.scope;
1797
  /* reserve space for arguments, local and temporary variables */
1798
  /* EG(trampoline) is reused from other places, like FFI (e.g. zend_ffi_cdata_get_closure()) where
1799
   * it is used as an internal function. It may set fields that don't belong to common, thus
1800
   * modifying zend_op_array specific data, most significantly last_var. We need to reset this
1801
   * value so that it doesn't contain garbage when the engine allocates space for the next stack
1802
   * frame. This didn't cause any issues until now due to "lucky" structure layout. */
1803
5.13k
  func->last_var = 0;
1804
5.13k
  uint32_t min_T = 2 + ZEND_OBSERVER_ENABLED;
1805
5.13k
  func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, min_T) : min_T;
1806
5.13k
  func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC();
1807
5.13k
  func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0;
1808
5.13k
  func->line_end = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_end : 0;
1809
1810
  //??? keep compatibility for "\0" characters
1811
  //??? see: Zend/tests/bug46238.phpt
1812
5.13k
  if (UNEXPECTED((mname_len = strlen(ZSTR_VAL(method_name))) != ZSTR_LEN(method_name))) {
1813
41
    func->function_name = zend_string_init(ZSTR_VAL(method_name), mname_len, 0);
1814
5.09k
  } else {
1815
5.09k
    func->function_name = zend_string_copy(method_name);
1816
5.09k
  }
1817
1818
5.13k
  func->prototype = NULL;
1819
5.13k
  func->prop_info = NULL;
1820
5.13k
  func->num_args = 0;
1821
5.13k
  func->required_num_args = 0;
1822
5.13k
  func->arg_info = zend_call_trampoline_arginfo;
1823
1824
5.13k
  return (zend_function*)func;
1825
5.13k
}
1826
/* }}} */
1827
1828
static ZEND_FUNCTION(zend_parent_hook_get_trampoline)
1829
285
{
1830
285
  zend_object *obj = Z_PTR_P(ZEND_THIS);
1831
285
  zend_string *prop_name = EX(func)->internal_function.reserved[0];
1832
1833
285
  if (UNEXPECTED(ZEND_NUM_ARGS() != 0)) {
1834
13
    zend_wrong_parameters_none_error();
1835
13
    goto clean;
1836
13
  }
1837
1838
272
  zval rv;
1839
272
  const zval *retval = obj->handlers->read_property(obj, prop_name, BP_VAR_R, NULL, &rv);
1840
272
  if (retval == &rv) {
1841
0
    RETVAL_COPY_VALUE(retval);
1842
272
  } else {
1843
272
    RETVAL_COPY(retval);
1844
272
  }
1845
1846
285
clean:
1847
285
  zend_string_release(EX(func)->common.function_name);
1848
285
  zend_free_trampoline(EX(func));
1849
285
  EX(func) = NULL;
1850
285
}
1851
1852
static ZEND_FUNCTION(zend_parent_hook_set_trampoline)
1853
36
{
1854
36
  zend_object *obj = Z_PTR_P(ZEND_THIS);
1855
36
  zend_string *prop_name = EX(func)->internal_function.reserved[0];
1856
1857
36
  zval *value;
1858
1859
95
  ZEND_PARSE_PARAMETERS_START(1, 1)
1860
95
    Z_PARAM_ZVAL(value)
1861
92
  ZEND_PARSE_PARAMETERS_END_EX(goto clean);
1862
1863
23
  RETVAL_COPY(obj->handlers->write_property(obj, prop_name, value, NULL));
1864
1865
36
clean:
1866
36
  zend_string_release(EX(func)->common.function_name);
1867
36
  zend_free_trampoline(EX(func));
1868
36
  EX(func) = NULL;
1869
36
}
1870
1871
ZEND_API zend_function *zend_get_property_hook_trampoline(
1872
  const zend_property_info *prop_info,
1873
  zend_property_hook_kind kind, zend_string *prop_name)
1874
326
{
1875
326
  zend_function *func;
1876
326
  if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
1877
326
    func = &EG(trampoline);
1878
326
  } else {
1879
0
    func = (zend_function *)(uintptr_t)ecalloc(1, sizeof(zend_internal_function));
1880
0
  }
1881
326
  func->type = ZEND_INTERNAL_FUNCTION;
1882
  /* This trampoline does not use the call_trampoline_op, so it won't reuse the call frame,
1883
   * which means we don't even need to reserve a temporary for observers. */
1884
326
  func->common.T = 0;
1885
326
  func->common.arg_flags[0] = 0;
1886
326
  func->common.arg_flags[1] = 0;
1887
326
  func->common.arg_flags[2] = 0;
1888
326
  func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
1889
326
  func->common.fn_flags2 = 0;
1890
326
  func->common.function_name = zend_string_concat3(
1891
326
    "$", 1, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name),
1892
326
    kind == ZEND_PROPERTY_HOOK_GET ? "::get" : "::set", 5);
1893
  /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
1894
326
  uint32_t args = kind == ZEND_PROPERTY_HOOK_GET ? 0 : 1;
1895
326
  func->common.num_args = args;
1896
326
  func->common.required_num_args = args;
1897
326
  func->common.scope = prop_info->ce;
1898
326
  func->common.prototype = NULL;
1899
326
  func->common.prop_info = prop_info;
1900
326
  func->common.arg_info = zend_property_hook_arginfo;
1901
326
  func->internal_function.handler = kind == ZEND_PROPERTY_HOOK_GET
1902
326
    ? ZEND_FN(zend_parent_hook_get_trampoline)
1903
326
    : ZEND_FN(zend_parent_hook_set_trampoline);
1904
326
  func->internal_function.module = NULL;
1905
1906
326
  func->internal_function.reserved[0] = prop_name;
1907
326
  func->internal_function.reserved[1] = NULL;
1908
1909
326
  return func;
1910
326
}
1911
1912
ZEND_API ZEND_COLD zend_never_inline void zend_bad_method_call(const zend_function *fbc, const zend_string *method_name, const zend_class_entry *scope) /* {{{ */
1913
62
{
1914
62
  zend_throw_error(NULL, "Call to %s method %s::%s() from %s%s",
1915
62
    zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name),
1916
62
    scope ? "scope " : "global scope",
1917
62
    scope ? ZSTR_VAL(scope->name) : ""
1918
62
  );
1919
62
}
1920
/* }}} */
1921
1922
ZEND_API ZEND_COLD zend_never_inline void zend_abstract_method_call(const zend_function *fbc) /* {{{ */
1923
31
{
1924
31
  zend_throw_error(NULL, "Cannot call abstract method %s::%s()",
1925
31
    ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
1926
31
}
1927
/* }}} */
1928
1929
ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
1930
89.2k
{
1931
89.2k
  zend_object *zobj = *obj_ptr;
1932
89.2k
  zval *func;
1933
89.2k
  zend_function *fbc;
1934
89.2k
  zend_string *lc_method_name;
1935
89.2k
  ALLOCA_FLAG(use_heap);
1936
1937
89.2k
  if (EXPECTED(key != NULL)) {
1938
85.1k
    lc_method_name = Z_STR_P(key);
1939
#ifdef ZEND_ALLOCA_MAX_SIZE
1940
    use_heap = 0;
1941
#endif
1942
85.1k
  } else {
1943
4.17k
    ZSTR_ALLOCA_ALLOC(lc_method_name, ZSTR_LEN(method_name), use_heap);
1944
4.17k
    zend_str_tolower_copy(ZSTR_VAL(lc_method_name), ZSTR_VAL(method_name), ZSTR_LEN(method_name));
1945
4.17k
  }
1946
1947
89.2k
  if (UNEXPECTED((func = zend_hash_find(&zobj->ce->function_table, lc_method_name)) == NULL)) {
1948
4.43k
    if (UNEXPECTED(!key)) {
1949
3.43k
      ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1950
3.43k
    }
1951
4.43k
    if (zobj->ce->__call) {
1952
3.73k
      return zend_get_call_trampoline_func(zobj->ce->__call, method_name);
1953
3.73k
    } else {
1954
696
      return NULL;
1955
696
    }
1956
4.43k
  }
1957
1958
84.8k
  fbc = Z_FUNC_P(func);
1959
1960
  /* Check access level */
1961
84.8k
  if (fbc->op_array.fn_flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
1962
497
    const zend_class_entry *scope = zend_get_executed_scope();
1963
1964
497
    if (fbc->common.scope != scope) {
1965
208
      if (fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
1966
63
        zend_function *updated_fbc = zend_get_parent_private_method(scope, zobj->ce, lc_method_name);
1967
1968
63
        if (EXPECTED(updated_fbc != NULL)) {
1969
38
          fbc = updated_fbc;
1970
38
          goto exit;
1971
38
        } else if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1972
20
          goto exit;
1973
20
        }
1974
63
      }
1975
150
      if (UNEXPECTED(fbc->op_array.fn_flags & ZEND_ACC_PRIVATE)
1976
100
       || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
1977
82
        if (zobj->ce->__call) {
1978
52
          fbc = zend_get_call_trampoline_func(zobj->ce->__call, method_name);
1979
52
        } else {
1980
30
          zend_bad_method_call(fbc, method_name, scope);
1981
30
          fbc = NULL;
1982
30
        }
1983
82
      }
1984
150
    }
1985
497
  }
1986
1987
84.8k
exit:
1988
84.8k
  if (fbc && UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1989
0
    zend_abstract_method_call(fbc);
1990
0
    fbc = NULL;
1991
0
  }
1992
84.8k
  if (UNEXPECTED(!key)) {
1993
743
    ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1994
743
  }
1995
84.8k
  return fbc;
1996
84.8k
}
1997
/* }}} */
1998
1999
static zend_always_inline zend_function *get_static_method_fallback(
2000
    const zend_class_entry *ce, zend_string *function_name)
2001
1.51k
{
2002
1.51k
  zend_object *object;
2003
1.51k
  if (ce->__call &&
2004
896
    (object = zend_get_this_object(EG(current_execute_data))) != NULL &&
2005
606
    instanceof_function(object->ce, ce)) {
2006
    /* Call the top-level defined __call().
2007
     * see: tests/classes/__call_004.phpt  */
2008
2009
598
    ZEND_ASSERT(object->ce->__call);
2010
598
    return zend_get_call_trampoline_func(object->ce->__call, function_name);
2011
914
  } else if (ce->__callstatic) {
2012
733
    return zend_get_call_trampoline_func(ce->__callstatic, function_name);
2013
733
  } else {
2014
181
    return NULL;
2015
181
  }
2016
1.51k
}
2017
2018
ZEND_API zend_function *zend_std_get_static_method(const zend_class_entry *ce, zend_string *function_name, const zval *key) /* {{{ */
2019
5.88k
{
2020
5.88k
  zend_string *lc_function_name;
2021
5.88k
  if (EXPECTED(key != NULL)) {
2022
5.24k
    lc_function_name = Z_STR_P(key);
2023
5.24k
  } else {
2024
636
    lc_function_name = zend_string_tolower(function_name);
2025
636
  }
2026
2027
5.88k
  zend_function *fbc;
2028
5.88k
  zval *func = zend_hash_find(&ce->function_table, lc_function_name);
2029
5.88k
  if (EXPECTED(func)) {
2030
4.41k
    fbc = Z_FUNC_P(func);
2031
4.41k
    if (!(fbc->common.fn_flags & ZEND_ACC_PUBLIC)) {
2032
151
      const zend_class_entry *scope = zend_get_executed_scope();
2033
151
      ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_PUBLIC));
2034
151
      if (!zend_check_method_accessible(fbc, scope)) {
2035
42
        zend_function *fallback_fbc = get_static_method_fallback(ce, function_name);
2036
42
        if (!fallback_fbc) {
2037
22
          zend_bad_method_call(fbc, function_name, scope);
2038
22
        }
2039
42
        fbc = fallback_fbc;
2040
42
      }
2041
151
    }
2042
4.41k
  } else {
2043
1.47k
    fbc = get_static_method_fallback(ce, function_name);
2044
1.47k
  }
2045
2046
5.88k
  if (UNEXPECTED(!key)) {
2047
636
    zend_string_release_ex(lc_function_name, 0);
2048
636
  }
2049
2050
5.88k
  if (EXPECTED(fbc)) {
2051
5.70k
    if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
2052
25
      zend_abstract_method_call(fbc);
2053
25
      goto fail;
2054
5.67k
    } else if (UNEXPECTED(fbc->common.scope->ce_flags & ZEND_ACC_TRAIT)) {
2055
20
      zend_error(E_DEPRECATED,
2056
20
        "Calling static trait method %s::%s is deprecated, "
2057
20
        "it should only be called on a class using the trait",
2058
20
        ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
2059
20
      if (EG(exception)) {
2060
0
        goto fail;
2061
0
      }
2062
20
    }
2063
5.70k
  }
2064
2065
5.85k
  return fbc;
2066
2067
25
 fail:
2068
25
  if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
2069
4
    zend_string_release_ex(fbc->common.function_name, 0);
2070
4
    zend_free_trampoline(fbc);
2071
4
  }
2072
2073
25
  return NULL;
2074
5.88k
}
2075
/* }}} */
2076
2077
ZEND_API void zend_class_init_statics(zend_class_entry *class_type) /* {{{ */
2078
1.38k
{
2079
1.38k
  zval *p;
2080
2081
1.38k
  if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
2082
1.30k
    if (class_type->parent) {
2083
107
      zend_class_init_statics(class_type->parent);
2084
107
    }
2085
2086
1.30k
    ZEND_MAP_PTR_SET(class_type->static_members_table, emalloc(sizeof(zval) * class_type->default_static_members_count));
2087
3.69k
    for (uint32_t i = 0; i < class_type->default_static_members_count; i++) {
2088
2.39k
      p = &class_type->default_static_members_table[i];
2089
2.39k
      if (Z_TYPE_P(p) == IS_INDIRECT) {
2090
112
        zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
2091
112
        ZVAL_DEINDIRECT(q);
2092
112
        ZVAL_INDIRECT(&CE_STATIC_MEMBERS(class_type)[i], q);
2093
2.28k
      } else {
2094
2.28k
        ZVAL_COPY_OR_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
2095
2.28k
      }
2096
2.39k
    }
2097
1.30k
  }
2098
1.38k
} /* }}} */
2099
2100
ZEND_API zval *zend_std_get_static_property_with_info(zend_class_entry *ce, zend_string *property_name, int type, zend_property_info **property_info_ptr) /* {{{ */
2101
3.50k
{
2102
3.50k
  zval *ret;
2103
3.50k
  zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property_name);
2104
3.50k
  *property_info_ptr = property_info;
2105
2106
3.50k
  if (UNEXPECTED(property_info == NULL)) {
2107
243
    goto undeclared_property;
2108
243
  }
2109
2110
3.26k
  if (!(property_info->flags & ZEND_ACC_PUBLIC)) {
2111
935
    const zend_class_entry *scope = get_fake_or_executed_scope();
2112
935
    if (property_info->ce != scope) {
2113
159
      if (UNEXPECTED(property_info->flags & ZEND_ACC_PRIVATE)
2114
111
       || UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
2115
111
        if (type != BP_VAR_IS) {
2116
18
          zend_bad_property_access(property_info, ce, property_name);
2117
18
        }
2118
111
        return NULL;
2119
111
      }
2120
159
    }
2121
935
  }
2122
2123
3.15k
  if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
2124
277
undeclared_property:
2125
277
    if (type != BP_VAR_IS) {
2126
197
      zend_throw_error(NULL, "Access to undeclared static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
2127
197
    }
2128
277
    return NULL;
2129
34
  }
2130
2131
3.11k
  if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
2132
343
    if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
2133
21
      return NULL;
2134
21
    }
2135
343
  }
2136
2137
  /* Ensure static properties are initialized. */
2138
3.09k
  if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL)) {
2139
1.14k
    zend_class_init_statics(ce);
2140
1.14k
  }
2141
2142
3.09k
  ret = CE_STATIC_MEMBERS(ce) + property_info->offset;
2143
3.09k
  ZVAL_DEINDIRECT(ret);
2144
2145
3.09k
  if (UNEXPECTED((type == BP_VAR_R || type == BP_VAR_RW)
2146
3.09k
        && Z_TYPE_P(ret) == IS_UNDEF && ZEND_TYPE_IS_SET(property_info->type))) {
2147
15
    zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
2148
15
      ZSTR_VAL(property_info->ce->name), ZSTR_VAL(property_name));
2149
15
    return NULL;
2150
15
  }
2151
2152
3.08k
  if (UNEXPECTED(ce->ce_flags & ZEND_ACC_TRAIT)) {
2153
70
    zend_error(E_DEPRECATED,
2154
70
      "Accessing static trait property %s::$%s is deprecated, "
2155
70
      "it should only be accessed on a class using the trait",
2156
70
      ZSTR_VAL(property_info->ce->name), ZSTR_VAL(property_name));
2157
70
  }
2158
2159
3.08k
  return ret;
2160
3.09k
}
2161
/* }}} */
2162
2163
ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type) /* {{{ */
2164
10
{
2165
10
  zend_property_info *prop_info;
2166
10
  return zend_std_get_static_property_with_info(ce, property_name, type, &prop_info);
2167
10
}
2168
2169
ZEND_API ZEND_COLD bool zend_std_unset_static_property(const zend_class_entry *ce, const zend_string *property_name) /* {{{ */
2170
8
{
2171
8
  zend_throw_error(NULL, "Attempt to unset static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
2172
8
  return 0;
2173
8
}
2174
/* }}} */
2175
2176
static ZEND_COLD zend_never_inline void zend_bad_constructor_call(const zend_function *constructor, const zend_class_entry *scope) /* {{{ */
2177
42
{
2178
42
  if (scope) {
2179
10
    zend_throw_error(NULL, "Call to %s %s::__construct() from scope %s",
2180
10
      zend_visibility_string(constructor->common.fn_flags),
2181
10
      ZSTR_VAL(constructor->common.scope->name),
2182
10
      ZSTR_VAL(scope->name)
2183
10
    );
2184
32
  } else {
2185
32
    zend_throw_error(NULL, "Call to %s %s::__construct() from global scope",
2186
32
      zend_visibility_string(constructor->common.fn_flags),
2187
32
      ZSTR_VAL(constructor->common.scope->name)
2188
32
    );
2189
32
  }
2190
42
}
2191
/* }}} */
2192
2193
ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
2194
476k
{
2195
476k
  zend_function *constructor = zobj->ce->constructor;
2196
2197
476k
  if (constructor) {
2198
364k
    if (UNEXPECTED(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC))) {
2199
85
      const zend_class_entry *scope = get_fake_or_executed_scope();
2200
85
      ZEND_ASSERT(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC));
2201
85
      if (!zend_check_method_accessible(constructor, scope)) {
2202
42
        zend_bad_constructor_call(constructor, scope);
2203
42
        zend_object_store_ctor_failed(zobj);
2204
42
        constructor = NULL;
2205
42
      }
2206
85
    }
2207
364k
  }
2208
2209
476k
  return constructor;
2210
476k
}
2211
/* }}} */
2212
2213
ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
2214
747
{
2215
747
  zend_object *zobj1, *zobj2;
2216
2217
747
  if (zend_objects_check_stack_limit()) {
2218
0
    zend_throw_error(NULL, "Maximum call stack size reached during object comparison");
2219
0
    return ZEND_UNCOMPARABLE;
2220
0
  }
2221
2222
747
  if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
2223
    /* Object and non-object */
2224
584
    zval *object;
2225
584
    zval *value;
2226
584
    zval casted;
2227
584
    bool object_lhs;
2228
584
    if (Z_TYPE_P(o1) == IS_OBJECT) {
2229
421
      object = o1;
2230
421
      value = o2;
2231
421
      object_lhs = true;
2232
421
    } else {
2233
163
      object = o2;
2234
163
      value = o1;
2235
163
      object_lhs = false;
2236
163
    }
2237
584
    ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT);
2238
584
    uint8_t target_type = Z_TYPE_P(value);
2239
    /* Should be handled in zend_compare(). */
2240
584
    ZEND_ASSERT(target_type != IS_FALSE && target_type != IS_TRUE);
2241
584
    if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) {
2242
      // TODO: Less crazy.
2243
477
      if (target_type == IS_LONG || target_type == IS_DOUBLE) {
2244
280
        zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
2245
280
               ZSTR_VAL(Z_OBJCE_P(object)->name), zend_get_type_by_const(target_type));
2246
280
        if (target_type == IS_LONG) {
2247
246
          ZVAL_LONG(&casted, 1);
2248
246
        } else {
2249
34
          ZVAL_DOUBLE(&casted, 1.0);
2250
34
        }
2251
280
      } else {
2252
197
        return object_lhs ? 1 : -1;
2253
197
      }
2254
477
    }
2255
387
    int ret = object_lhs ? zend_compare(&casted, value) : zend_compare(value, &casted);
2256
387
    zval_ptr_dtor(&casted);
2257
387
    return ret;
2258
584
  }
2259
2260
163
  zobj1 = Z_OBJ_P(o1);
2261
163
  zobj2 = Z_OBJ_P(o2);
2262
2263
163
  if (zobj1 == zobj2) {
2264
0
    return 0; /* the same object */
2265
0
  }
2266
163
  if (zobj1->ce != zobj2->ce) {
2267
18
    return ZEND_UNCOMPARABLE; /* different classes */
2268
18
  }
2269
145
  if (!zobj1->properties && !zobj2->properties
2270
118
      && !zend_object_is_lazy(zobj1) && !zend_object_is_lazy(zobj2)) {
2271
94
    zend_property_info *info;
2272
94
    int i;
2273
2274
94
    if (!zobj1->ce->default_properties_count) {
2275
48
      return 0;
2276
48
    }
2277
2278
    /* It's enough to protect only one of the objects.
2279
     * The second one may be referenced from the first and this may cause
2280
     * false recursion detection.
2281
     */
2282
    /* use bitwise OR to make only one conditional jump */
2283
46
    if (UNEXPECTED(Z_IS_RECURSIVE_P(o1))) {
2284
5
      zend_throw_error(NULL, "Nesting level too deep - recursive dependency?");
2285
5
      return ZEND_UNCOMPARABLE;
2286
5
    }
2287
41
    Z_PROTECT_RECURSION_P(o1);
2288
2289
41
    GC_ADDREF(zobj1);
2290
41
    GC_ADDREF(zobj2);
2291
41
    int ret;
2292
2293
69
    for (i = 0; i < zobj1->ce->default_properties_count; i++) {
2294
61
      zval *p1, *p2;
2295
2296
61
      info = zobj1->ce->properties_info_table[i];
2297
2298
61
      if (!info) {
2299
10
        continue;
2300
10
      }
2301
2302
51
      p1 = OBJ_PROP(zobj1, info->offset);
2303
51
      p2 = OBJ_PROP(zobj2, info->offset);
2304
2305
51
      if (Z_TYPE_P(p1) != IS_UNDEF) {
2306
51
        if (Z_TYPE_P(p2) != IS_UNDEF) {
2307
51
          ret = zend_compare(p1, p2);
2308
51
          if (ret != 0) {
2309
33
            Z_UNPROTECT_RECURSION_P(o1);
2310
33
            goto done;
2311
33
          }
2312
51
        } else {
2313
0
          Z_UNPROTECT_RECURSION_P(o1);
2314
0
          ret = 1;
2315
0
          goto done;
2316
0
        }
2317
51
      } else {
2318
0
        if (Z_TYPE_P(p2) != IS_UNDEF) {
2319
0
          Z_UNPROTECT_RECURSION_P(o1);
2320
0
          ret = 1;
2321
0
          goto done;
2322
0
        }
2323
0
      }
2324
51
    }
2325
2326
8
    Z_UNPROTECT_RECURSION_P(o1);
2327
8
    ret = 0;
2328
2329
41
done:
2330
41
    OBJ_RELEASE(zobj1);
2331
41
    OBJ_RELEASE(zobj2);
2332
2333
41
    return ret;
2334
51
  } else {
2335
51
    GC_ADDREF(zobj1);
2336
51
    GC_ADDREF(zobj2);
2337
2338
51
    int ret = zend_compare_symbol_tables(
2339
51
        zend_std_get_properties_ex(zobj1),
2340
51
        zend_std_get_properties_ex(zobj2));
2341
2342
51
    OBJ_RELEASE(zobj1);
2343
51
    OBJ_RELEASE(zobj2);
2344
2345
51
    return ret;
2346
51
  }
2347
145
}
2348
/* }}} */
2349
2350
ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
2351
600
{
2352
600
  return ZEND_UNCOMPARABLE;
2353
600
}
2354
2355
// todo: make zend_std_has_property return bool as well
2356
ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
2357
1.04k
{
2358
1.04k
  bool result;
2359
1.04k
  zval *value = NULL;
2360
1.04k
  uintptr_t property_offset;
2361
1.04k
  const zend_property_info *prop_info = NULL;
2362
2363
1.04k
  property_offset = zend_get_property_offset(zobj->ce, name, 1, cache_slot, &prop_info);
2364
2365
1.04k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
2366
257
try_again:
2367
257
    value = OBJ_PROP(zobj, property_offset);
2368
257
    if (Z_TYPE_P(value) != IS_UNDEF) {
2369
136
      goto found;
2370
136
    }
2371
121
    if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) {
2372
      /* Skip __isset() for uninitialized typed properties */
2373
73
      goto lazy_init;
2374
73
    }
2375
798
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
2376
683
    if (EXPECTED(zobj->properties != NULL)) {
2377
254
      if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
2378
9
        uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
2379
2380
9
        if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
2381
9
          Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
2382
2383
9
          if (EXPECTED(p->key == name) ||
2384
3
                (EXPECTED(p->h == ZSTR_H(name)) &&
2385
3
                 EXPECTED(p->key != NULL) &&
2386
6
                 EXPECTED(zend_string_equal_content(p->key, name)))) {
2387
6
            value = &p->val;
2388
6
            goto found;
2389
6
          }
2390
9
        }
2391
3
        CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
2392
3
      }
2393
248
      value = zend_hash_find(zobj->properties, name);
2394
248
      if (value) {
2395
195
        if (cache_slot) {
2396
43
          uintptr_t idx = (char*)value - (char*)zobj->properties->arData;
2397
43
          CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
2398
43
        }
2399
337
found:
2400
337
        if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
2401
15
          result = zend_is_true(value);
2402
322
        } else if (has_set_exists < ZEND_PROPERTY_NOT_EMPTY) {
2403
274
          ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
2404
274
          ZVAL_DEREF(value);
2405
274
          result = (Z_TYPE_P(value) != IS_NULL);
2406
274
        } else {
2407
48
          ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
2408
48
          result = true;
2409
48
        }
2410
337
        goto exit;
2411
337
      }
2412
248
    }
2413
683
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
2414
81
    zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET];
2415
2416
81
    if (has_set_exists == ZEND_PROPERTY_EXISTS) {
2417
0
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
2418
0
        return true;
2419
0
      }
2420
0
      property_offset = prop_info->offset;
2421
0
      goto try_again;
2422
0
    }
2423
2424
81
    if (!get) {
2425
11
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
2426
5
        zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s",
2427
5
          ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
2428
5
        return 0;
2429
6
      } else {
2430
6
        property_offset = prop_info->offset;
2431
6
        goto try_again;
2432
6
      }
2433
11
    }
2434
2435
70
    zval rv;
2436
70
    if (!zend_call_get_hook(prop_info, name, get, zobj, &rv)) {
2437
5
      if (EG(exception)) {
2438
0
        return 0;
2439
0
      }
2440
5
      property_offset = prop_info->offset;
2441
5
      goto try_again;
2442
5
    }
2443
2444
65
    if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
2445
15
      result = zend_is_true(&rv);
2446
50
    } else {
2447
50
      ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
2448
50
      result = Z_TYPE(rv) != IS_NULL
2449
35
        && (Z_TYPE(rv) != IS_REFERENCE || Z_TYPE_P(Z_REFVAL(rv)) != IS_NULL);
2450
50
    }
2451
65
    zval_ptr_dtor(&rv);
2452
65
    return result;
2453
65
  } else if (UNEXPECTED(EG(exception))) {
2454
0
    result = false;
2455
0
    goto exit;
2456
0
  }
2457
2458
  /* For initialized lazy proxies: if the real instance's __isset guard
2459
   * is already set, we are inside a recursive call from the real
2460
   * instance's __isset. Forward directly to avoid double invocation. */
2461
564
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
2462
564
      && zend_lazy_object_initialized(zobj))) {
2463
8
    zend_object *instance = zend_lazy_object_get_instance(zobj);
2464
8
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
2465
8
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
2466
8
      if ((*instance_guard) & IN_ISSET) {
2467
8
        return zend_std_has_property(instance, name, has_set_exists, cache_slot);
2468
8
      }
2469
8
    }
2470
8
  }
2471
2472
556
  if (!zobj->ce->__isset) {
2473
169
    goto lazy_init;
2474
169
  }
2475
2476
387
  result = false;
2477
387
  if (has_set_exists != ZEND_PROPERTY_EXISTS) {
2478
387
    uint32_t *guard = zend_get_property_guard(zobj, name);
2479
2480
387
    if (!((*guard) & IN_ISSET)) {
2481
301
      zval rv;
2482
2483
      /* have issetter - try with it! */
2484
301
      GC_ADDREF(zobj);
2485
301
      (*guard) |= IN_ISSET; /* prevent circular getting */
2486
301
      zend_std_call_issetter(zobj, name, &rv);
2487
301
      result = zend_is_true(&rv);
2488
301
      zval_ptr_dtor(&rv);
2489
301
      if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY && result) {
2490
30
        if (EXPECTED(!EG(exception)) && zobj->ce->__get && !((*guard) & IN_GET)) {
2491
25
          (*guard) |= IN_GET;
2492
25
          zend_std_call_getter(zobj, name, &rv);
2493
25
          (*guard) &= ~IN_GET;
2494
25
          result = i_zend_is_true(&rv);
2495
25
          zval_ptr_dtor(&rv);
2496
25
        } else {
2497
5
          result = false;
2498
5
        }
2499
30
      }
2500
301
      (*guard) &= ~IN_ISSET;
2501
301
      OBJ_RELEASE(zobj);
2502
301
    } else {
2503
86
      goto lazy_init;
2504
86
    }
2505
387
  }
2506
2507
949
exit:
2508
949
  return result;
2509
2510
328
lazy_init:
2511
328
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
2512
17
    if (!value || (Z_PROP_FLAG_P(value) & IS_PROP_LAZY)) {
2513
17
      zobj = zend_lazy_object_init(zobj);
2514
17
      if (!zobj) {
2515
0
        result = false;
2516
0
        goto exit;
2517
0
      }
2518
2519
17
      if (UNEXPECTED(zobj->ce->__isset)) {
2520
7
        uint32_t *guard = zend_get_property_guard(zobj, name);
2521
7
        if (!((*guard) & IN_ISSET)) {
2522
7
          (*guard) |= IN_ISSET;
2523
7
          result = zend_std_has_property(zobj, name, has_set_exists, cache_slot);
2524
7
          (*guard) &= ~IN_ISSET;
2525
7
          return result;
2526
7
        }
2527
7
      }
2528
2529
10
      return zend_std_has_property(zobj, name, has_set_exists, cache_slot);
2530
17
    }
2531
17
  }
2532
2533
311
  result = false;
2534
311
  goto exit;
2535
328
}
2536
/* }}} */
2537
2538
ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj) /* {{{ */
2539
18.0k
{
2540
18.0k
  return zend_string_copy(zobj->ce->name);
2541
18.0k
}
2542
/* }}} */
2543
2544
ZEND_API zend_result zend_std_cast_object_tostring(zend_object *readobj, zval *writeobj, int type) /* {{{ */
2545
9.84k
{
2546
9.84k
  switch (type) {
2547
8.35k
    case IS_STRING: {
2548
8.35k
      const zend_class_entry *ce = readobj->ce;
2549
8.35k
      if (ce->__tostring) {
2550
7.32k
        zval retval;
2551
7.32k
        GC_ADDREF(readobj);
2552
7.32k
        zend_call_known_instance_method_with_0_params(ce->__tostring, readobj, &retval);
2553
7.32k
        zend_object_release(readobj);
2554
7.32k
        if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
2555
4.79k
is_string:
2556
4.79k
          ZVAL_COPY_VALUE(writeobj, &retval);
2557
4.79k
          return SUCCESS;
2558
4.79k
        } else if (Z_ISREF(retval)) {
2559
6
          zend_unwrap_reference(&retval);
2560
6
          goto is_string;
2561
6
        }
2562
2.53k
        zval_ptr_dtor(&retval);
2563
2.53k
        if (!EG(exception)) {
2564
0
          zend_throw_error(NULL, "Method %s::__toString() must return a string value", ZSTR_VAL(ce->name));
2565
0
        }
2566
2.53k
      }
2567
3.55k
      return FAILURE;
2568
8.35k
    }
2569
397
    case _IS_BOOL:
2570
397
      ZVAL_TRUE(writeobj);
2571
397
      return SUCCESS;
2572
1.09k
    default:
2573
1.09k
      return FAILURE;
2574
9.84k
  }
2575
9.84k
}
2576
/* }}} */
2577
2578
ZEND_API zend_result zend_std_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */
2579
259
{
2580
259
  zend_class_entry *ce = obj->ce;
2581
259
  const zval *func = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE));
2582
2583
259
  if (func == NULL) {
2584
24
    return FAILURE;
2585
24
  }
2586
235
  *fptr_ptr = Z_FUNC_P(func);
2587
235
  *ce_ptr = ce;
2588
235
  *obj_ptr = obj;
2589
2590
235
  return SUCCESS;
2591
259
}
2592
/* }}} */
2593
2594
17.7k
ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose) {
2595
17.7k
  HashTable *ht;
2596
17.7k
  switch (purpose) {
2597
16.7k
    case ZEND_PROP_PURPOSE_DEBUG:
2598
16.7k
      if (obj->handlers->get_debug_info) {
2599
16.7k
        int is_temp;
2600
16.7k
        ht = obj->handlers->get_debug_info(obj, &is_temp);
2601
16.7k
        if (ht && !is_temp) {
2602
15.0k
          GC_TRY_ADDREF(ht);
2603
15.0k
        }
2604
16.7k
        return ht;
2605
16.7k
      }
2606
0
      ZEND_FALLTHROUGH;
2607
237
    case ZEND_PROP_PURPOSE_JSON:
2608
501
    case ZEND_PROP_PURPOSE_GET_OBJECT_VARS:
2609
659
    case ZEND_PROP_PURPOSE_VAR_EXPORT:
2610
659
      if (obj->ce->num_hooked_props) {
2611
397
        return zend_hooked_object_build_properties(obj);
2612
397
      }
2613
262
      ht = obj->handlers->get_properties(obj);
2614
262
      if (ht) {
2615
262
        GC_TRY_ADDREF(ht);
2616
262
      }
2617
262
      return ht;
2618
160
    case ZEND_PROP_PURPOSE_ARRAY_CAST:
2619
160
      ht = zend_get_properties_no_lazy_init(obj);
2620
160
      if (ht) {
2621
160
        GC_TRY_ADDREF(ht);
2622
160
      }
2623
160
      return ht;
2624
173
    case ZEND_PROP_PURPOSE_SERIALIZE: {
2625
173
      if (zend_object_is_lazy(obj)
2626
117
          && !zend_lazy_object_initialize_on_serialize(obj)) {
2627
31
        ht = zend_get_properties_no_lazy_init(obj);
2628
142
      } else {
2629
142
        ht = obj->handlers->get_properties(obj);
2630
142
      }
2631
173
      if (ht) {
2632
173
        GC_TRY_ADDREF(ht);
2633
173
      }
2634
173
      return ht;
2635
659
    }
2636
0
    default:
2637
0
      ZEND_UNREACHABLE();
2638
0
      return NULL;
2639
17.7k
  }
2640
17.7k
}
2641
2642
18.2k
ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose) {
2643
18.2k
  zend_object *zobj = Z_OBJ_P(obj);
2644
2645
18.2k
  if (zobj->handlers->get_properties_for) {
2646
533
    return zobj->handlers->get_properties_for(zobj, purpose);
2647
533
  }
2648
2649
17.7k
  return zend_std_get_properties_for(zobj, purpose);
2650
18.2k
}
2651
2652
ZEND_API const zend_object_handlers std_object_handlers = {
2653
  0,                    /* offset */
2654
2655
  zend_object_std_dtor,         /* free_obj */
2656
  zend_objects_destroy_object,      /* dtor_obj */
2657
  zend_objects_clone_obj,         /* clone_obj */
2658
  zend_objects_clone_obj_with,      /* clone_obj_with */
2659
2660
  zend_std_read_property,         /* read_property */
2661
  zend_std_write_property,        /* write_property */
2662
  zend_std_read_dimension,        /* read_dimension */
2663
  zend_std_write_dimension,       /* write_dimension */
2664
  zend_std_get_property_ptr_ptr,      /* get_property_ptr_ptr */
2665
  zend_std_has_property,          /* has_property */
2666
  zend_std_unset_property,        /* unset_property */
2667
  zend_std_has_dimension,         /* has_dimension */
2668
  zend_std_unset_dimension,       /* unset_dimension */
2669
  zend_std_get_properties,        /* get_properties */
2670
  zend_std_get_method,          /* get_method */
2671
  zend_std_get_constructor,       /* get_constructor */
2672
  zend_std_get_class_name,        /* get_class_name */
2673
  zend_std_cast_object_tostring,      /* cast_object */
2674
  NULL,                 /* count_elements */
2675
  zend_std_get_debug_info,        /* get_debug_info */
2676
  zend_std_get_closure,         /* get_closure */
2677
  zend_std_get_gc,            /* get_gc */
2678
  NULL,                 /* do_operation */
2679
  zend_std_compare_objects,       /* compare */
2680
  NULL,                 /* get_properties_for */
2681
};
2682
2683
16
void zend_object_handlers_startup(void) {
2684
16
  zend_call_trampoline_arginfo[0].name = ZSTR_KNOWN(ZEND_STR_ARGUMENTS);
2685
  zend_call_trampoline_arginfo[0].type = (zend_type)ZEND_TYPE_INIT_CODE(IS_MIXED, false, _ZEND_ARG_INFO_FLAGS(false, 1, 0));
2686
16
  zend_property_hook_arginfo[0].name = ZSTR_KNOWN(ZEND_STR_VALUE);
2687
16
}