Coverage Report

Created: 2026-06-13 07:01

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
648
#define ZEND_WRONG_PROPERTY_OFFSET   0
39
4.27k
#define ZEND_HOOKED_PROPERTY_OFFSET 1
40
41
/* guard flags */
42
8.18k
#define IN_GET    ZEND_GUARD_PROPERTY_GET
43
4.06k
#define IN_SET    ZEND_GUARD_PROPERTY_SET
44
451
#define IN_UNSET  ZEND_GUARD_PROPERTY_UNSET
45
1.83k
#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
763
{
53
763
#ifdef ZEND_CHECK_STACK_LIMIT
54
763
  return zend_call_stack_overflowed(EG(stack_limit));
55
#else
56
  return false;
57
#endif
58
763
}
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
294k
{
79
294k
  if (!zobj->properties) {
80
293k
    zend_property_info *prop_info;
81
293k
    zend_class_entry *ce = zobj->ce;
82
293k
    int i;
83
84
293k
    zobj->properties = zend_new_array(ce->default_properties_count);
85
293k
    if (ce->default_properties_count) {
86
103k
      zend_hash_real_init_mixed(zobj->properties);
87
769k
      for (i = 0; i < ce->default_properties_count; i++) {
88
666k
        prop_info = ce->properties_info_table[i];
89
90
666k
        if (!prop_info) {
91
179
          continue;
92
179
        }
93
94
666k
        if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
95
2.34k
          HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
96
2.34k
        }
97
98
666k
        _zend_hash_append_ind(zobj->properties, prop_info->name,
99
666k
          OBJ_PROP(zobj, prop_info->offset));
100
666k
      }
101
103k
    }
102
293k
  }
103
104
294k
  return zobj->properties;
105
294k
}
106
/* }}} */
107
108
/* Implements the fast path for array cast */
109
ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /* {{{ */
110
133
{
111
133
  const zend_class_entry *ce = zobj->ce;
112
133
  HashTable *ht;
113
133
  zval* prop;
114
133
  int i;
115
116
133
  ZEND_ASSERT(!(zend_object_is_lazy_proxy(zobj) && zend_lazy_object_initialized(zobj)));
117
133
  ZEND_ASSERT(!zobj->properties);
118
133
  ht = zend_new_array(ce->default_properties_count);
119
133
  if (ce->default_properties_count) {
120
84
    zend_hash_real_init_mixed(ht);
121
202
    for (i = 0; i < ce->default_properties_count; i++) {
122
118
      const zend_property_info *prop_info = ce->properties_info_table[i];
123
124
118
      if (!prop_info) {
125
0
        continue;
126
0
      }
127
128
118
      prop = OBJ_PROP(zobj, prop_info->offset);
129
118
      if (UNEXPECTED(Z_TYPE_P(prop) == IS_UNDEF)) {
130
45
        continue;
131
45
      }
132
133
73
      if (Z_ISREF_P(prop) && Z_REFCOUNT_P(prop) == 1) {
134
10
        prop = Z_REFVAL_P(prop);
135
10
      }
136
137
73
      Z_TRY_ADDREF_P(prop);
138
73
      _zend_hash_append(ht, prop_info->name, prop);
139
73
    }
140
84
  }
141
133
  return ht;
142
133
}
143
/* }}} */
144
145
ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */
146
539k
{
147
539k
  return zend_std_get_properties_ex(zobj);
148
539k
}
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.03k
{
154
1.03k
  if (zobj->handlers->get_properties == zend_std_get_properties) {
155
1.03k
    if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
156
1.03k
        && zend_lazy_object_initialized(zobj))) {
157
28
      zend_object *instance = zend_lazy_object_get_instance(zobj);
158
28
      return zend_get_properties_no_lazy_init(instance);
159
28
    }
160
161
1.00k
    if (!zobj->properties) {
162
665
      rebuild_object_properties_internal(zobj);
163
665
    }
164
1.00k
    return zobj->properties;
165
1.03k
  }
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
506k
{
174
506k
  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
506k
  } else {
179
506k
    if (UNEXPECTED(zend_object_is_lazy(zobj))) {
180
2.17k
      return zend_lazy_object_get_gc(zobj, table, n);
181
504k
    } else if (zobj->properties) {
182
449k
      *table = NULL;
183
449k
      *n = 0;
184
449k
      return zobj->properties;
185
449k
    } else {
186
55.2k
      *table = zobj->properties_table;
187
55.2k
      *n = zobj->ce->default_properties_count;
188
55.2k
      return NULL;
189
55.2k
    }
190
506k
  }
191
506k
}
192
/* }}} */
193
194
ZEND_API HashTable *zend_std_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
195
16.3k
{
196
16.3k
  const zend_class_entry *ce = object->ce;
197
16.3k
  zval retval;
198
16.3k
  HashTable *ht;
199
200
16.3k
  if (!ce->__debugInfo) {
201
16.0k
    if (UNEXPECTED(zend_object_is_lazy(object))) {
202
1.01k
      return zend_lazy_object_debug_info(object, is_temp);
203
1.01k
    }
204
205
15.0k
    *is_temp = 0;
206
15.0k
    return object->handlers->get_properties(object);
207
16.0k
  }
208
209
322
  zend_call_known_instance_method_with_0_params(ce->__debugInfo, object, &retval);
210
322
  if (UNEXPECTED(Z_ISREF(retval))) {
211
6
    zend_unwrap_reference(&retval);
212
6
  }
213
322
  if (Z_TYPE(retval) == IS_ARRAY) {
214
296
    if (!Z_REFCOUNTED(retval)) {
215
29
      *is_temp = 1;
216
29
      return zend_array_dup(Z_ARRVAL(retval));
217
267
    } else if (Z_REFCOUNT(retval) <= 1) {
218
264
      *is_temp = 1;
219
264
      ht = Z_ARR(retval);
220
264
      return ht;
221
264
    } else {
222
3
      *is_temp = 0;
223
3
      zval_ptr_dtor(&retval);
224
3
      return Z_ARRVAL(retval);
225
3
    }
226
296
  } else if (Z_TYPE(retval) == IS_NULL) {
227
12
    zend_error(E_DEPRECATED, "Returning null from %s::__debugInfo() is deprecated, return an empty array instead",
228
12
      ZSTR_VAL(ce->name));
229
12
    *is_temp = 1;
230
12
    ht = zend_new_array(0);
231
12
    return ht;
232
12
  }
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
322
}
238
/* }}} */
239
240
static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
241
2.47k
{
242
2.47k
  zval member;
243
2.47k
  ZVAL_STR(&member, prop_name);
244
2.47k
  zend_call_known_instance_method_with_1_params(zobj->ce->__get, zobj, retval, &member);
245
2.47k
}
246
/* }}} */
247
248
static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */
249
1.24k
{
250
1.24k
  zval args[2];
251
1.24k
  ZVAL_STR(&args[0], prop_name);
252
1.24k
  ZVAL_COPY_VALUE(&args[1], value);
253
1.24k
  zend_call_known_instance_method(zobj->ce->__set, zobj, NULL, 2, args);
254
1.24k
}
255
/* }}} */
256
257
static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */
258
105
{
259
105
  zval member;
260
105
  ZVAL_STR(&member, prop_name);
261
105
  zend_call_known_instance_method_with_1_params(zobj->ce->__unset, zobj, NULL, &member);
262
105
}
263
/* }}} */
264
265
static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
266
535
{
267
535
  zval member;
268
535
  ZVAL_STR(&member, prop_name);
269
535
  zend_call_known_instance_method_with_1_params(zobj->ce->__isset, zobj, retval, &member);
270
535
}
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.24M
{
276
1.24M
  child_class = child_class->parent;
277
1.85M
  while (child_class) {
278
1.23M
    if (child_class == parent_class) {
279
622k
      return 1;
280
622k
    }
281
611k
    child_class = child_class->parent;
282
611k
  }
283
284
621k
  return 0;
285
1.24M
}
286
/* }}} */
287
288
static zend_never_inline int is_protected_compatible_scope(const zend_class_entry *ce, const zend_class_entry *scope) /* {{{ */
289
622k
{
290
622k
  return scope &&
291
622k
    (ce == scope || is_derived_class(ce, scope) || is_derived_class(scope, ce));
292
622k
}
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
598
{
297
598
  zval *zv;
298
598
  zend_property_info *prop_info;
299
300
598
  if (scope != ce && scope && is_derived_class(ce, scope)) {
301
477
    zv = zend_hash_find(&scope->properties_info, member);
302
477
    if (zv != NULL) {
303
477
      prop_info = (zend_property_info*)Z_PTR_P(zv);
304
477
      if ((prop_info->flags & ZEND_ACC_PRIVATE)
305
470
       && prop_info->ce == scope) {
306
451
        return prop_info;
307
451
      }
308
477
    }
309
477
  }
310
147
  return NULL;
311
598
}
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
111
{
316
111
  zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
317
111
}
318
/* }}} */
319
320
static ZEND_COLD zend_never_inline void zend_bad_property_name(void) /* {{{ */
321
47
{
322
47
  zend_throw_error(NULL, "Cannot access property starting with \"\\0\"");
323
47
}
324
/* }}} */
325
326
static ZEND_COLD zend_never_inline void zend_forbidden_dynamic_property(
327
69
    const zend_class_entry *ce, const zend_string *member) {
328
69
  zend_throw_error(NULL, "Cannot create dynamic property %s::$%s",
329
69
    ZSTR_VAL(ce->name), ZSTR_VAL(member));
330
69
}
331
332
static ZEND_COLD zend_never_inline bool zend_deprecated_dynamic_property(
333
14.1k
    zend_object *obj, const zend_string *member) {
334
14.1k
  GC_ADDREF(obj);
335
14.1k
  zend_error(E_DEPRECATED, "Creation of dynamic property %s::$%s is deprecated",
336
14.1k
    ZSTR_VAL(obj->ce->name), ZSTR_VAL(member));
337
14.1k
  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
14.1k
  return 1;
348
14.1k
}
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
30.8M
{
358
30.8M
  if (UNEXPECTED(EG(fake_scope))) {
359
30.8M
    return EG(fake_scope);
360
30.8M
  } else {
361
8.41k
    return zend_get_executed_scope();
362
8.41k
  }
363
30.8M
}
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
28.4M
{
367
28.4M
  zval *zv;
368
28.4M
  zend_property_info *property_info;
369
28.4M
  uint32_t flags;
370
28.4M
  uintptr_t offset;
371
372
28.4M
  if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) {
373
17.0k
    *info_ptr = CACHED_PTR_EX(cache_slot + 2);
374
17.0k
    return (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
375
17.0k
  }
376
377
28.4M
  if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
378
28.4M
   || UNEXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
379
54.4k
    if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
380
77
      if (!silent) {
381
47
        zend_bad_property_name();
382
47
      }
383
77
      return ZEND_WRONG_PROPERTY_OFFSET;
384
77
    }
385
54.4k
dynamic:
386
54.4k
    if (cache_slot) {
387
11.2k
      CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
388
11.2k
      CACHE_PTR_EX(cache_slot + 2, NULL);
389
11.2k
    }
390
54.4k
    return ZEND_DYNAMIC_PROPERTY_OFFSET;
391
54.4k
  }
392
393
28.3M
  property_info = (zend_property_info*)Z_PTR_P(zv);
394
28.3M
  flags = property_info->flags;
395
396
28.3M
  if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
397
28.3M
    const zend_class_entry *scope = get_fake_or_executed_scope();
398
399
28.3M
    if (property_info->ce != scope) {
400
622k
      if (flags & ZEND_ACC_CHANGED) {
401
412
        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
412
        if (p && (!(p->flags & ZEND_ACC_STATIC) || (flags & ZEND_ACC_STATIC))) {
408
315
          property_info = p;
409
315
          flags = property_info->flags;
410
315
          goto found;
411
315
        } else if (flags & ZEND_ACC_PUBLIC) {
412
45
          goto found;
413
45
        }
414
412
      }
415
622k
      if (flags & ZEND_ACC_PRIVATE) {
416
364
        if (property_info->ce != ce) {
417
63
          goto dynamic;
418
301
        } else {
419
571
wrong:
420
          /* Information was available, but we were denied access.  Error out. */
421
571
          if (!silent) {
422
91
            zend_bad_property_access(property_info, ce, member);
423
91
          }
424
571
          return ZEND_WRONG_PROPERTY_OFFSET;
425
301
        }
426
622k
      } else {
427
622k
        ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
428
622k
        if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
429
270
          goto wrong;
430
270
        }
431
622k
      }
432
622k
    }
433
28.3M
  }
434
435
28.3M
found:
436
28.3M
  if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
437
125
    if (!silent) {
438
74
      zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
439
74
    }
440
125
    return ZEND_DYNAMIC_PROPERTY_OFFSET;
441
125
  }
442
443
28.3M
  if (property_info->hooks) {
444
4.27k
    *info_ptr = property_info;
445
4.27k
    if (cache_slot) {
446
2.34k
      CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_HOOKED_PROPERTY_OFFSET);
447
2.34k
      CACHE_PTR_EX(cache_slot + 2, property_info);
448
2.34k
    }
449
4.27k
    return ZEND_HOOKED_PROPERTY_OFFSET;
450
4.27k
  }
451
452
28.3M
  offset = property_info->offset;
453
28.3M
  if (EXPECTED(!ZEND_TYPE_IS_SET(property_info->type))) {
454
820k
    property_info = NULL;
455
27.5M
  } else {
456
27.5M
    *info_ptr = property_info;
457
27.5M
  }
458
28.3M
  if (cache_slot) {
459
17.8k
    CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset);
460
17.8k
    CACHE_PTR_EX(cache_slot + 2, property_info);
461
17.8k
  }
462
28.3M
  return offset;
463
28.3M
}
464
/* }}} */
465
466
static ZEND_COLD void zend_wrong_offset(zend_class_entry *ce, zend_string *member) /* {{{ */
467
39
{
468
39
  const zend_property_info *dummy;
469
470
  /* Trigger the correct error */
471
39
  zend_get_property_offset(ce, member, 0, NULL, &dummy);
472
39
}
473
/* }}} */
474
475
ZEND_API zend_property_info *zend_get_property_info(const zend_class_entry *ce, zend_string *member, int silent) /* {{{ */
476
2.50M
{
477
2.50M
  zval *zv;
478
2.50M
  zend_property_info *property_info;
479
2.50M
  uint32_t flags;
480
481
2.50M
  if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)
482
2.49M
   || EXPECTED((zv = zend_hash_find(&ce->properties_info, member)) == NULL)) {
483
9.64k
    if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0') && ZSTR_LEN(member) != 0) {
484
1.03k
      if (!silent) {
485
0
        zend_bad_property_name();
486
0
      }
487
1.03k
      return ZEND_WRONG_PROPERTY_INFO;
488
1.03k
    }
489
8.71k
dynamic:
490
8.71k
    return NULL;
491
9.64k
  }
492
493
2.49M
  property_info = (zend_property_info*)Z_PTR_P(zv);
494
2.49M
  flags = property_info->flags;
495
496
2.49M
  if (flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
497
2.49M
    const zend_class_entry *scope = get_fake_or_executed_scope();
498
2.49M
    if (property_info->ce != scope) {
499
679
      if (flags & ZEND_ACC_CHANGED) {
500
186
        zend_property_info *p = zend_get_parent_private_property(scope, ce, member);
501
502
186
        if (p) {
503
116
          property_info = p;
504
116
          flags = property_info->flags;
505
116
          goto found;
506
116
        } else if (flags & ZEND_ACC_PUBLIC) {
507
14
          goto found;
508
14
        }
509
186
      }
510
549
      if (flags & ZEND_ACC_PRIVATE) {
511
494
        if (property_info->ce != ce) {
512
108
          goto dynamic;
513
386
        } else {
514
413
wrong:
515
          /* Information was available, but we were denied access.  Error out. */
516
413
          if (!silent) {
517
0
            zend_bad_property_access(property_info, ce, member);
518
0
          }
519
413
          return ZEND_WRONG_PROPERTY_INFO;
520
386
        }
521
494
      } else {
522
55
        ZEND_ASSERT(flags & ZEND_ACC_PROTECTED);
523
55
        if (UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
524
27
          goto wrong;
525
27
        }
526
55
      }
527
549
    }
528
2.49M
  }
529
530
2.49M
found:
531
2.49M
  if (UNEXPECTED(flags & ZEND_ACC_STATIC)) {
532
1.96k
    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
1.96k
  }
536
2.49M
  return property_info;
537
2.49M
}
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.41k
{
542
2.41k
  zend_property_info *property_info;
543
2.41k
  const char *class_name = NULL;
544
2.41k
  const char *prop_name;
545
2.41k
  zend_string *member;
546
2.41k
  size_t prop_name_len;
547
548
2.41k
  if (ZSTR_VAL(prop_info_name)[0] == 0) {
549
808
    if (is_dynamic) {
550
0
      return SUCCESS;
551
0
    }
552
553
808
    zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len);
554
808
    member = zend_string_init(prop_name, prop_name_len, 0);
555
808
    property_info = zend_get_property_info(zobj->ce, member, 1);
556
808
    zend_string_release_ex(member, 0);
557
808
    if (property_info == NULL || property_info == ZEND_WRONG_PROPERTY_INFO) {
558
466
      return FAILURE;
559
466
    }
560
561
342
    if (class_name[0] != '*') {
562
261
      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
7
        return FAILURE;
565
254
      } 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
53
        return FAILURE;
568
53
      }
569
261
    } else {
570
      /* We were looking for a protected property but found a private one
571
       * belonging to the parent class. */
572
81
      if (property_info->flags & ZEND_ACC_PRIVATE) {
573
6
        return FAILURE;
574
6
      }
575
75
      ZEND_ASSERT(property_info->flags & ZEND_ACC_PROTECTED);
576
75
    }
577
276
    return SUCCESS;
578
1.61k
  } else {
579
1.61k
    property_info = zend_get_property_info(zobj->ce, prop_info_name, 1);
580
1.61k
    if (property_info == NULL) {
581
181
      ZEND_ASSERT(is_dynamic);
582
181
      return SUCCESS;
583
1.43k
    } else if (property_info == ZEND_WRONG_PROPERTY_INFO) {
584
0
      return FAILURE;
585
0
    }
586
1.43k
    return (property_info->flags & ZEND_ACC_PUBLIC) ? SUCCESS : FAILURE;
587
1.61k
  }
588
2.41k
}
589
/* }}} */
590
591
3.46k
ZEND_API bool ZEND_FASTCALL zend_asymmetric_property_has_set_access(const zend_property_info *prop_info) {
592
3.46k
  ZEND_ASSERT(prop_info->flags & ZEND_ACC_PPP_SET_MASK);
593
3.46k
  ZEND_ASSERT(!(prop_info->flags & ZEND_ACC_PUBLIC_SET));
594
3.46k
  const zend_class_entry *scope = get_fake_or_executed_scope();
595
3.46k
  if (prop_info->ce == scope) {
596
2.62k
    return true;
597
2.62k
  }
598
840
  return EXPECTED((prop_info->flags & ZEND_ACC_PROTECTED_SET)
599
3.46k
    && is_protected_compatible_scope(prop_info->prototype->ce, scope));
600
3.46k
}
601
602
702
static void zend_property_guard_dtor(zval *el) /* {{{ */ {
603
702
  uint32_t *ptr = (uint32_t*)Z_PTR_P(el);
604
702
  if (EXPECTED(!(((uintptr_t)ptr) & 1))) {
605
455
    efree_size(ptr, sizeof(uint32_t));
606
455
  }
607
702
}
608
/* }}} */
609
610
static zend_always_inline zval *zend_get_guard_value(zend_object *zobj)
611
7.28k
{
612
7.28k
  return zobj->properties_table + zobj->ce->default_properties_count;
613
7.28k
}
614
615
ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */
616
6.42k
{
617
6.42k
  HashTable *guards;
618
6.42k
  zval *zv;
619
6.42k
  uint32_t *ptr;
620
621
622
6.42k
  ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS);
623
6.42k
  zv = zend_get_guard_value(zobj);
624
6.42k
  if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
625
3.20k
    zend_string *str = Z_STR_P(zv);
626
3.20k
    if (EXPECTED(str == member) ||
627
        /* str and member don't necessarily have a pre-calculated hash value here */
628
2.20k
        EXPECTED(zend_string_equal_content(str, member))) {
629
2.20k
      return &Z_GUARD_P(zv);
630
2.20k
    } else if (EXPECTED(Z_GUARD_P(zv) == 0)) {
631
757
      zval_ptr_dtor_str(zv);
632
757
      ZVAL_STR_COPY(zv, member);
633
757
      return &Z_GUARD_P(zv);
634
757
    } else {
635
247
      ALLOC_HASHTABLE(guards);
636
247
      zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0);
637
      /* mark pointer as "special" using low bit */
638
247
      zend_hash_add_new_ptr(guards, str,
639
247
        (void*)(((uintptr_t)&Z_GUARD_P(zv)) | 1));
640
247
      zval_ptr_dtor_str(zv);
641
247
      ZVAL_ARR(zv, guards);
642
247
    }
643
3.22k
  } else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
644
1.75k
    guards = Z_ARRVAL_P(zv);
645
1.75k
    ZEND_ASSERT(guards != NULL);
646
1.75k
    zv = zend_hash_find(guards, member);
647
1.75k
    if (zv != NULL) {
648
1.54k
      return (uint32_t*)(((uintptr_t)Z_PTR_P(zv)) & ~1);
649
1.54k
    }
650
1.75k
  } else {
651
1.46k
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF);
652
1.46k
    ZVAL_STR_COPY(zv, member);
653
1.46k
    Z_GUARD_P(zv) &= ~ZEND_GUARD_PROPERTY_MASK;
654
1.46k
    return &Z_GUARD_P(zv);
655
1.46k
  }
656
  /* we have to allocate uint32_t separately because ht->arData may be reallocated */
657
455
  ptr = (uint32_t*)emalloc(sizeof(uint32_t));
658
455
  *ptr = 0;
659
455
  return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr);
660
6.42k
}
661
/* }}} */
662
663
ZEND_API uint32_t *zend_get_recursion_guard(zend_object *zobj)
664
18.4k
{
665
18.4k
  if (!(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
666
17.5k
    return NULL;
667
17.5k
  }
668
861
  zval *zv = zend_get_guard_value(zobj);
669
861
  return &Z_GUARD_P(zv);
670
18.4k
}
671
672
ZEND_COLD static void zend_typed_property_uninitialized_access(const zend_property_info *prop_info, zend_string *name)
673
268
{
674
268
  zend_throw_error(NULL, "Typed property %s::$%s must not be accessed before initialization",
675
268
    ZSTR_VAL(prop_info->ce->name),
676
268
    ZSTR_VAL(name));
677
268
}
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
9.39k
{
684
9.39k
  const zend_execute_data *execute_data = EG(current_execute_data);
685
9.39k
  if (!execute_data || !EX(func) || !EX(func)->common.prop_info) {
686
6.67k
    return false;
687
6.67k
  }
688
689
2.72k
  const zend_property_info *parent_info = EX(func)->common.prop_info;
690
2.72k
  ZEND_ASSERT(prop_info->prototype && parent_info->prototype);
691
2.72k
  return prop_info->prototype == parent_info->prototype;
692
2.72k
}
693
694
static bool zend_should_call_hook(const zend_property_info *prop_info, const zend_object *obj)
695
8.88k
{
696
8.88k
  if (!zend_is_in_hook(prop_info)) {
697
7.66k
    return true;
698
7.66k
  }
699
700
  /* execute_data and This are guaranteed to be set if zend_is_in_hook() returns true. */
701
1.21k
  zend_object *parent_obj = Z_OBJ(EG(current_execute_data)->This);
702
1.21k
  if (parent_obj == obj) {
703
1.16k
    return false;
704
1.16k
  }
705
706
52
  if (zend_object_is_lazy_proxy(parent_obj)
707
38
   && zend_lazy_object_initialized(parent_obj)
708
38
   && zend_lazy_object_get_instance(parent_obj) == obj) {
709
38
    return false;
710
38
  }
711
712
14
  return true;
713
52
}
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.88k
{
726
5.88k
  if (!zend_should_call_hook(prop_info, zobj)) {
727
699
    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
699
    return false;
731
699
  }
732
733
5.18k
  GC_ADDREF(zobj);
734
5.18k
  zend_call_known_instance_method_with_0_params(get, zobj, rv);
735
5.18k
  OBJ_RELEASE(zobj);
736
737
5.18k
  return true;
738
5.88k
}
739
740
ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int type, void **cache_slot, zval *rv) /* {{{ */
741
27.5M
{
742
27.5M
  zval *retval;
743
27.5M
  uintptr_t property_offset;
744
27.5M
  const zend_property_info *prop_info = NULL;
745
27.5M
  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
27.5M
  property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot, &prop_info);
753
754
27.5M
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
755
27.4M
try_again:
756
27.4M
    retval = OBJ_PROP(zobj, property_offset);
757
758
27.4M
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))
759
2.33k
     && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
760
416
     && ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info))) {
761
416
      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
152
        ZVAL_COPY(rv, retval);
766
152
        retval = rv;
767
152
        goto exit;
768
264
      } else if (Z_TYPE_P(retval) == IS_UNDEF && type == BP_VAR_UNSET) {
769
57
        retval = &EG(uninitialized_zval);
770
57
        goto exit;
771
57
      }
772
207
      if (prop_info->flags & ZEND_ACC_READONLY) {
773
128
        zend_readonly_property_indirect_modification_error(prop_info);
774
128
      } else {
775
79
        zend_asymmetric_visibility_property_modification_error(prop_info, "indirectly modify");
776
79
      }
777
207
      retval = &EG(uninitialized_zval);
778
207
      goto exit;
779
416
    }
780
27.4M
    if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) {
781
27.4M
      goto exit;
782
27.4M
    }
783
768
    if (UNEXPECTED(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT)) {
784
      /* Skip __get() for uninitialized typed properties */
785
600
      goto uninit_error;
786
600
    }
787
15.7k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
788
9.44k
    if (EXPECTED(zobj->properties != NULL)) {
789
1.98k
      if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
790
10
        uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
791
792
10
        if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
793
10
          Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
794
795
10
          if (EXPECTED(p->key == name) ||
796
1
                (EXPECTED(p->h == ZSTR_H(name)) &&
797
1
                 EXPECTED(p->key != NULL) &&
798
10
                 EXPECTED(zend_string_equal_content(p->key, name)))) {
799
10
            retval = &p->val;
800
10
            goto exit;
801
10
          }
802
10
        }
803
0
        CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
804
0
      }
805
1.97k
      retval = zend_hash_find(zobj->properties, name);
806
1.97k
      if (EXPECTED(retval)) {
807
913
        if (cache_slot) {
808
597
          uintptr_t idx = (char*)retval - (char*)zobj->properties->arData;
809
597
          CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
810
597
        }
811
913
        goto exit;
812
913
      }
813
1.97k
    }
814
9.44k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
815
6.08k
    zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET];
816
6.08k
    if (!get) {
817
268
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
818
9
        zend_throw_error(NULL, "Cannot read from set-only virtual property %s::$%s",
819
9
          ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
820
9
        return &EG(uninitialized_zval);
821
9
      }
822
      /* Cache the fact that this hook has trivial read. This only applies to
823
       * BP_VAR_R and BP_VAR_IS fetches. */
824
259
      ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
825
826
259
      retval = OBJ_PROP(zobj, prop_info->offset);
827
259
      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
37
        ZEND_ASSERT(Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT);
831
37
        goto uninit_error;
832
37
      }
833
834
222
      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
203
      goto exit;
844
222
    }
845
846
5.81k
    const zend_class_entry *ce = zobj->ce;
847
848
5.81k
    if (!zend_call_get_hook(prop_info, name, get, zobj, rv)) {
849
694
      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
694
      const zend_execute_data *execute_data = EG(current_execute_data);
855
694
      if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_FETCH_OBJ_R && EX(opline)->op1_type == IS_UNUSED) {
856
372
        ZEND_SET_PROPERTY_HOOK_SIMPLE_READ(cache_slot);
857
372
      }
858
859
694
      property_offset = prop_info->offset;
860
694
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
861
585
        prop_info = NULL;
862
585
      }
863
694
      goto try_again;
864
694
    }
865
866
5.11k
    if (EXPECTED(cache_slot
867
5.11k
     && zend_execute_ex == execute_ex
868
5.11k
     && ce->default_object_handlers->read_property == zend_std_read_property
869
5.11k
     && !ce->create_object
870
5.11k
     && !zend_is_in_hook(prop_info)
871
5.11k
     && !(prop_info->hooks[ZEND_PROPERTY_HOOK_GET]->common.fn_flags & ZEND_ACC_RETURN_REFERENCE))) {
872
460
      ZEND_SET_PROPERTY_HOOK_SIMPLE_GET(cache_slot);
873
460
    }
874
875
5.11k
    if (Z_TYPE_P(rv) != IS_UNDEF) {
876
2.22k
      retval = rv;
877
2.22k
      if (!Z_ISREF_P(rv)
878
1.92k
       && (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
879
82
       && UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
880
51
        zend_throw_error(NULL, "Indirect modification of %s::$%s is not allowed",
881
51
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
882
51
      }
883
2.89k
    } else {
884
2.89k
      retval = &EG(uninitialized_zval);
885
2.89k
    }
886
887
5.11k
    goto exit;
888
5.81k
  } else if (UNEXPECTED(EG(exception))) {
889
40
    retval = &EG(uninitialized_zval);
890
40
    goto exit;
891
40
  }
892
893
8.92k
  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.92k
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
900
8.92k
      && zend_lazy_object_initialized(zobj))) {
901
83
    zend_object *instance = zend_lazy_object_get_instance(zobj);
902
83
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
903
66
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
904
66
      uint32_t guard_type = ((type == BP_VAR_IS) && zobj->ce->__isset)
905
66
        ? IN_ISSET : IN_GET;
906
66
      if ((*instance_guard) & guard_type) {
907
25
        retval = zend_std_read_property(instance, name, type, cache_slot, rv);
908
25
        if (retval == &EG(uninitialized_zval)) {
909
15
          ZVAL_NULL(rv);
910
15
          retval = rv;
911
15
        }
912
25
        return retval;
913
25
      }
914
66
    }
915
83
  }
916
917
  /* magic isset */
918
8.89k
  if ((type == BP_VAR_IS) && zobj->ce->__isset) {
919
320
    zval tmp_result;
920
320
    guard = zend_get_property_guard(zobj, name);
921
922
320
    if (!((*guard) & IN_ISSET)) {
923
233
      GC_ADDREF(zobj);
924
925
233
      *guard |= IN_ISSET;
926
233
      zend_std_call_issetter(zobj, name, &tmp_result);
927
233
      *guard &= ~IN_ISSET;
928
929
233
      if (!zend_is_true(&tmp_result)) {
930
138
        retval = &EG(uninitialized_zval);
931
138
        OBJ_RELEASE(zobj);
932
138
        zval_ptr_dtor(&tmp_result);
933
138
        goto exit;
934
138
      }
935
936
95
      zval_ptr_dtor(&tmp_result);
937
95
      if (zobj->ce->__get && !((*guard) & IN_GET)) {
938
55
        goto call_getter;
939
55
      }
940
40
      OBJ_RELEASE(zobj);
941
87
    } else if (zobj->ce->__get && !((*guard) & IN_GET)) {
942
29
      goto call_getter_addref;
943
29
    }
944
8.57k
  } else if (zobj->ce->__get) {
945
    /* magic get */
946
2.90k
    guard = zend_get_property_guard(zobj, name);
947
2.90k
    if (!((*guard) & IN_GET)) {
948
      /* have getter - try with it! */
949
2.39k
call_getter_addref:
950
2.39k
      GC_ADDREF(zobj);
951
2.44k
call_getter:
952
2.44k
      *guard |= IN_GET; /* prevent circular getting */
953
2.44k
      zend_std_call_getter(zobj, name, rv);
954
2.44k
      *guard &= ~IN_GET;
955
956
2.44k
      if (Z_TYPE_P(rv) != IS_UNDEF) {
957
2.17k
        retval = rv;
958
2.17k
        if (!Z_ISREF_P(rv) &&
959
1.45k
            (type == BP_VAR_W || type == BP_VAR_RW  || type == BP_VAR_UNSET)) {
960
357
          if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
961
296
            zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
962
296
          }
963
357
        }
964
2.17k
      } else {
965
273
        retval = &EG(uninitialized_zval);
966
273
      }
967
968
2.44k
      if (prop_info) {
969
45
        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
45
      }
971
972
2.44k
      OBJ_RELEASE(zobj);
973
2.44k
      goto exit;
974
2.39k
    } 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.90k
  }
982
983
6.93k
uninit_error:
984
6.93k
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
985
581
    if (!prop_info || (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY)) {
986
566
      zend_object *instance = zend_lazy_object_init(zobj);
987
566
      if (!instance) {
988
89
        retval = &EG(uninitialized_zval);
989
89
        goto exit;
990
89
      }
991
992
477
      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
38
        uint32_t guard_type = (type == BP_VAR_IS) && zobj->ce->__isset
996
38
          ? IN_ISSET : IN_GET;
997
38
        guard = zend_get_property_guard(instance, name);
998
38
        if (!((*guard) & guard_type)) {
999
28
          (*guard) |= guard_type;
1000
28
          retval = zend_std_read_property(instance, name, type, cache_slot, rv);
1001
28
          (*guard) &= ~guard_type;
1002
28
          return retval;
1003
28
        }
1004
38
      }
1005
1006
449
      return zend_std_read_property(instance, name, type, cache_slot, rv);
1007
477
    }
1008
581
  }
1009
6.36k
  if (type != BP_VAR_IS) {
1010
5.80k
    if (prop_info) {
1011
175
      zend_typed_property_uninitialized_access(prop_info, name);
1012
5.63k
    } else {
1013
5.63k
      zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1014
5.63k
    }
1015
5.80k
  }
1016
6.36k
  retval = &EG(uninitialized_zval);
1017
1018
27.5M
exit:
1019
27.5M
  return retval;
1020
6.36k
}
1021
/* }}} */
1022
1023
1.06M
static zend_always_inline bool property_uses_strict_types(void) {
1024
1.06M
  const zend_execute_data *execute_data = EG(current_execute_data);
1025
1.06M
  return execute_data
1026
895k
    && execute_data->func
1027
895k
    && ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data));
1028
1.06M
}
1029
1030
static zval *forward_write_to_lazy_object(zend_object *zobj,
1031
    zend_string *name, zval *value, void **cache_slot, bool guarded)
1032
246
{
1033
246
  zval *variable_ptr;
1034
1035
  /* backup value as it may change during initialization */
1036
246
  zval backup;
1037
246
  ZVAL_COPY(&backup, value);
1038
1039
246
  zend_object *instance = zend_lazy_object_init(zobj);
1040
246
  if (UNEXPECTED(!instance)) {
1041
57
    zval_ptr_dtor(&backup);
1042
57
    return &EG(error_zval);
1043
57
  }
1044
1045
189
  if (UNEXPECTED(guarded && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS))) {
1046
24
    uint32_t *guard = zend_get_property_guard(instance, name);
1047
24
    if (!((*guard) & IN_SET)) {
1048
18
      (*guard) |= IN_SET;
1049
18
      variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot);
1050
18
      (*guard) &= ~IN_SET;
1051
18
      goto exit;
1052
18
    }
1053
24
  }
1054
1055
171
  variable_ptr = zend_std_write_property(instance, name, &backup, cache_slot);
1056
1057
189
exit:
1058
189
  zval_ptr_dtor(&backup);
1059
1060
189
  if (variable_ptr == &backup) {
1061
0
    variable_ptr = value;
1062
0
  }
1063
1064
189
  return variable_ptr;
1065
171
}
1066
1067
ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zval *value, void **cache_slot) /* {{{ */
1068
946k
{
1069
946k
  zval *variable_ptr, tmp;
1070
946k
  uintptr_t property_offset;
1071
946k
  const zend_property_info *prop_info = NULL;
1072
946k
  uint32_t *guard = NULL;
1073
946k
  ZEND_ASSERT(!Z_ISREF_P(value));
1074
1075
946k
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__set != NULL), cache_slot, &prop_info);
1076
1077
946k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1078
898k
try_again:
1079
898k
    variable_ptr = OBJ_PROP(zobj, property_offset);
1080
1081
898k
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1082
3.28k
      bool error;
1083
3.28k
      if (Z_TYPE_P(variable_ptr) != IS_UNDEF || (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) || !zobj->ce->__set) {
1084
3.26k
        error = true;
1085
3.26k
      } else {
1086
22
        guard = zend_get_property_guard(zobj, name);
1087
22
        error = (*guard) & IN_SET;
1088
22
      }
1089
3.28k
      if (error) {
1090
3.27k
        if ((prop_info->flags & ZEND_ACC_READONLY)
1091
2.45k
         && Z_TYPE_P(variable_ptr) != IS_UNDEF
1092
287
         && !(Z_PROP_FLAG_P(variable_ptr) & IS_PROP_REINITABLE)) {
1093
222
          zend_readonly_property_modification_error(prop_info);
1094
222
          variable_ptr = &EG(error_zval);
1095
222
          goto exit;
1096
222
        }
1097
3.04k
        if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) {
1098
180
          zend_asymmetric_visibility_property_modification_error(prop_info, "modify");
1099
180
          variable_ptr = &EG(error_zval);
1100
180
          goto exit;
1101
180
        }
1102
3.04k
      }
1103
3.28k
    }
1104
1105
897k
    if (Z_TYPE_P(variable_ptr) != IS_UNDEF) {
1106
890k
      Z_TRY_ADDREF_P(value);
1107
1108
890k
      if (prop_info) {
1109
160k
typed_property:
1110
160k
        ZVAL_COPY_VALUE(&tmp, value);
1111
        // Increase refcount to prevent object from being released in __toString()
1112
160k
        GC_ADDREF(zobj);
1113
160k
        bool type_matched = zend_verify_property_type(prop_info, &tmp, property_uses_strict_types());
1114
160k
        if (UNEXPECTED(GC_DELREF(zobj) == 0)) {
1115
18
          zend_object_released_while_assigning_to_property_error(prop_info);
1116
18
          zend_objects_store_del(zobj);
1117
18
          zval_ptr_dtor(&tmp);
1118
18
          variable_ptr = &EG(error_zval);
1119
18
          goto exit;
1120
18
        }
1121
160k
        if (UNEXPECTED(!type_matched)) {
1122
399
          zval_ptr_dtor(&tmp);
1123
399
          variable_ptr = &EG(error_zval);
1124
399
          goto exit;
1125
399
        }
1126
160k
        Z_PROP_FLAG_P(variable_ptr) &= ~(IS_PROP_UNINIT|IS_PROP_REINITABLE);
1127
160k
        value = &tmp;
1128
160k
      }
1129
1130
907k
found:;
1131
907k
      zend_refcounted *garbage = NULL;
1132
1133
907k
      variable_ptr = zend_assign_to_variable_ex(
1134
907k
        variable_ptr, value, IS_TMP_VAR, property_uses_strict_types(), &garbage);
1135
1136
907k
      if (garbage) {
1137
5.57k
        if (GC_DELREF(garbage) == 0) {
1138
3.34k
          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.34k
          if (execute_data
1141
882
           && EX(func)
1142
882
           && ZEND_USER_CODE(EX(func)->common.type)
1143
261
           && EX(opline)
1144
261
           && EX(opline)->opcode == ZEND_ASSIGN_OBJ
1145
261
           && EX(opline)->result_type) {
1146
73
            ZVAL_COPY_DEREF(EX_VAR(EX(opline)->result.var), variable_ptr);
1147
73
            variable_ptr = NULL;
1148
73
          }
1149
3.34k
          rc_dtor_func(garbage);
1150
3.34k
        } else {
1151
2.23k
          gc_check_possible_root_no_ref(garbage);
1152
2.23k
        }
1153
5.57k
      }
1154
907k
      goto exit;
1155
890k
    }
1156
7.58k
    if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_UNINIT) {
1157
7.43k
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1158
762
        if (Z_PROP_FLAG_P(variable_ptr) & IS_PROP_LAZY) {
1159
204
          goto lazy_init;
1160
204
        }
1161
762
      }
1162
      /* Writes to uninitialized typed properties bypass __set(). */
1163
7.22k
      goto write_std_property;
1164
7.43k
    }
1165
49.0k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
1166
46.1k
    if (EXPECTED(zobj->properties != NULL)) {
1167
22.8k
      if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1168
54
        if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1169
54
          GC_DELREF(zobj->properties);
1170
54
        }
1171
54
        zobj->properties = zend_array_dup(zobj->properties);
1172
54
      }
1173
22.8k
      if ((variable_ptr = zend_hash_find(zobj->properties, name)) != NULL) {
1174
10.2k
        Z_TRY_ADDREF_P(value);
1175
10.2k
        goto found;
1176
10.2k
      }
1177
22.8k
    }
1178
46.1k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1179
2.68k
    zend_function *set = prop_info->hooks[ZEND_PROPERTY_HOOK_SET];
1180
1181
2.68k
    if (!set) {
1182
59
      if (prop_info->flags & ZEND_ACC_VIRTUAL) {
1183
12
        zend_throw_error(NULL, "Cannot write to get-only virtual property %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1184
12
        variable_ptr = &EG(error_zval);
1185
12
        goto exit;
1186
12
      }
1187
47
      ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot);
1188
47
      property_offset = prop_info->offset;
1189
47
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1190
31
        prop_info = NULL;
1191
31
      }
1192
47
      goto try_again;
1193
59
    }
1194
1195
2.62k
    if (!zend_should_call_hook(prop_info, zobj)) {
1196
365
      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
365
      zend_execute_data *execute_data = EG(current_execute_data);
1204
365
      if (cache_slot && EX(opline) && EX(opline)->opcode == ZEND_ASSIGN_OBJ && EX(opline)->op1_type == IS_UNUSED) {
1205
342
        ZEND_SET_PROPERTY_HOOK_SIMPLE_WRITE(cache_slot);
1206
342
      }
1207
1208
365
      property_offset = prop_info->offset;
1209
365
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1210
186
        prop_info = NULL;
1211
186
      }
1212
365
      goto try_again;
1213
365
    }
1214
1215
2.26k
    if (UNEXPECTED(prop_info->flags & ZEND_ACC_PPP_SET_MASK
1216
2.26k
     && !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.25k
    GC_ADDREF(zobj);
1223
2.25k
    zend_call_known_instance_method_with_1_params(set, zobj, NULL, value);
1224
2.25k
    OBJ_RELEASE(zobj);
1225
1226
2.25k
    variable_ptr = value;
1227
2.25k
    goto exit;
1228
2.26k
  } else if (UNEXPECTED(EG(exception))) {
1229
46
    variable_ptr = &EG(error_zval);
1230
46
    goto exit;
1231
46
  }
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
36.2k
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
1237
36.2k
      && zend_lazy_object_initialized(zobj))) {
1238
41
    zend_object *instance = zend_lazy_object_get_instance(zobj);
1239
41
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
1240
41
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
1241
41
      if ((*instance_guard) & IN_SET) {
1242
22
        return zend_std_write_property(instance, name, value, cache_slot);
1243
22
      }
1244
41
    }
1245
41
  }
1246
1247
  /* magic set */
1248
36.2k
  if (zobj->ce->__set) {
1249
1.44k
    if (!guard) {
1250
1.42k
      guard = zend_get_property_guard(zobj, name);
1251
1.42k
    }
1252
1253
1.44k
    if (!((*guard) & IN_SET)) {
1254
1.24k
      GC_ADDREF(zobj);
1255
1.24k
      (*guard) |= IN_SET; /* prevent circular setting */
1256
1.24k
      zend_std_call_setter(zobj, name, value);
1257
1.24k
      (*guard) &= ~IN_SET;
1258
1.24k
      OBJ_RELEASE(zobj);
1259
1.24k
      variable_ptr = value;
1260
1.24k
    } else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) {
1261
181
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1262
24
        return forward_write_to_lazy_object(zobj, name, value,
1263
24
            cache_slot, /* guarded */ true);
1264
24
      }
1265
1266
157
      goto write_std_property;
1267
181
    } else {
1268
      /* Trigger the correct error */
1269
15
      zend_wrong_offset(zobj->ce, name);
1270
15
      ZEND_ASSERT(EG(exception));
1271
15
      variable_ptr = &EG(error_zval);
1272
15
      goto exit;
1273
15
    }
1274
34.7k
  } else {
1275
34.7k
    ZEND_ASSERT(!IS_WRONG_PROPERTY_OFFSET(property_offset));
1276
34.7k
    if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1277
18
      goto lazy_init;
1278
18
    }
1279
42.1k
write_std_property:
1280
42.1k
    if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1281
7.32k
      variable_ptr = OBJ_PROP(zobj, property_offset);
1282
1283
7.32k
      Z_TRY_ADDREF_P(value);
1284
7.32k
      if (prop_info) {
1285
6.93k
        goto typed_property;
1286
6.93k
      }
1287
1288
387
      ZVAL_COPY_VALUE(variable_ptr, value);
1289
34.8k
    } else {
1290
34.8k
      if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1291
53
        zend_forbidden_dynamic_property(zobj->ce, name);
1292
53
        variable_ptr = &EG(error_zval);
1293
53
        goto exit;
1294
53
      }
1295
34.7k
      if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) {
1296
13.2k
        if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) {
1297
0
          variable_ptr = &EG(error_zval);
1298
0
          goto exit;
1299
0
        }
1300
13.2k
      }
1301
1302
34.7k
      Z_TRY_ADDREF_P(value);
1303
34.7k
      variable_ptr = zend_hash_add_new(zend_std_get_properties(zobj), name, value);
1304
34.7k
    }
1305
42.1k
  }
1306
1307
945k
exit:
1308
945k
  return variable_ptr;
1309
1310
222
lazy_init:
1311
222
  return forward_write_to_lazy_object(zobj, name, value, cache_slot,
1312
222
      /* guarded */ false);
1313
36.2k
}
1314
/* }}} */
1315
1316
static ZEND_COLD zend_never_inline void zend_bad_array_access(const zend_class_entry *ce) /* {{{ */
1317
43
{
1318
43
  zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
1319
43
}
1320
/* }}} */
1321
1322
ZEND_API zval *zend_std_read_dimension(zend_object *object, zval *offset, int type, zval *rv) /* {{{ */
1323
1.67k
{
1324
1.67k
  const zend_class_entry *ce = object->ce;
1325
1.67k
  zval tmp_offset;
1326
1327
  /* arrayaccess_funcs_ptr is set if (and only if) the class implements zend_ce_arrayaccess */
1328
1.67k
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1329
1.67k
  if (EXPECTED(funcs)) {
1330
1.64k
    if (offset == NULL) {
1331
      /* [] construct */
1332
12
      ZVAL_NULL(&tmp_offset);
1333
1.63k
    } else {
1334
1.63k
      ZVAL_COPY_DEREF(&tmp_offset, offset);
1335
1.63k
    }
1336
1337
1.64k
    GC_ADDREF(object);
1338
1.64k
    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
83
        OBJ_RELEASE(object);
1347
83
        zval_ptr_dtor(&tmp_offset);
1348
83
        zval_ptr_dtor(rv);
1349
83
        return &EG(uninitialized_zval);
1350
83
      }
1351
76
      zval_ptr_dtor(rv);
1352
76
    }
1353
1354
1.55k
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetget, object, rv, &tmp_offset);
1355
1356
1.55k
    OBJ_RELEASE(object);
1357
1.55k
    zval_ptr_dtor(&tmp_offset);
1358
1359
1.55k
    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.54k
    return rv;
1366
1.55k
  } else {
1367
33
      zend_bad_array_access(ce);
1368
33
    return NULL;
1369
33
  }
1370
1.67k
}
1371
/* }}} */
1372
1373
ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
1374
351
{
1375
351
  const zend_class_entry *ce = object->ce;
1376
351
  zval tmp_offset;
1377
1378
351
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1379
351
  if (EXPECTED(funcs)) {
1380
346
    if (!offset) {
1381
28
      ZVAL_NULL(&tmp_offset);
1382
318
    } else {
1383
318
      ZVAL_COPY_DEREF(&tmp_offset, offset);
1384
318
    }
1385
346
    GC_ADDREF(object);
1386
346
    zend_call_known_instance_method_with_2_params(funcs->zf_offsetset, object, NULL, &tmp_offset, value);
1387
346
    OBJ_RELEASE(object);
1388
346
    zval_ptr_dtor(&tmp_offset);
1389
346
  } else {
1390
5
      zend_bad_array_access(ce);
1391
5
  }
1392
351
}
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
214
{
1398
214
  const zend_class_entry *ce = object->ce;
1399
214
  zval retval, tmp_offset;
1400
214
  bool result;
1401
1402
214
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1403
214
  if (EXPECTED(funcs)) {
1404
214
    ZVAL_COPY_DEREF(&tmp_offset, offset);
1405
214
    GC_ADDREF(object);
1406
214
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetexists, object, &retval, &tmp_offset);
1407
214
    result = i_zend_is_true(&retval);
1408
214
    zval_ptr_dtor(&retval);
1409
214
    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
214
    OBJ_RELEASE(object);
1415
214
    zval_ptr_dtor(&tmp_offset);
1416
214
  } else {
1417
0
      zend_bad_array_access(ce);
1418
0
    return 0;
1419
0
  }
1420
1421
214
  return result;
1422
214
}
1423
/* }}} */
1424
1425
ZEND_API zval *zend_std_get_property_ptr_ptr(zend_object *zobj, zend_string *name, int type, void **cache_slot) /* {{{ */
1426
10.0k
{
1427
10.0k
  zval *retval = NULL;
1428
10.0k
  uintptr_t property_offset;
1429
10.0k
  const zend_property_info *prop_info = NULL;
1430
1431
10.0k
  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
10.0k
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__get != NULL), cache_slot, &prop_info);
1438
1439
10.0k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1440
5.87k
try_again:
1441
5.87k
    retval = OBJ_PROP(zobj, property_offset);
1442
5.87k
    if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
1443
801
      if (EXPECTED(!zobj->ce->__get) ||
1444
36
          UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET) ||
1445
772
          UNEXPECTED(prop_info && (Z_PROP_FLAG_P(retval) & IS_PROP_UNINIT))) {
1446
772
        if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(retval) & IS_PROP_LAZY))) {
1447
187
          bool guarded = zobj->ce->__get
1448
0
            && (*zend_get_property_guard(zobj, name) & IN_GET);
1449
187
          zend_object *instance = zend_lazy_object_init(zobj);
1450
187
          if (!instance) {
1451
15
            return &EG(error_zval);
1452
15
          }
1453
1454
172
          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
172
          return zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1465
172
        }
1466
585
        if (UNEXPECTED(type == BP_VAR_RW)) {
1467
109
          if (prop_info) {
1468
93
            zend_typed_property_uninitialized_access(prop_info, name);
1469
93
            retval = &EG(error_zval);
1470
93
          } 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
476
        } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1478
169
          if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) {
1479
145
            retval = NULL;
1480
145
          }
1481
307
        } else if (!prop_info || !ZEND_TYPE_IS_SET(prop_info->type)) {
1482
16
          ZVAL_NULL(retval);
1483
16
        }
1484
585
      } else {
1485
        /* we do have getter - fail and let it try again with usual get/set */
1486
29
        retval = NULL;
1487
29
      }
1488
5.07k
    } else if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1489
512
      if ((prop_info->flags & ZEND_ACC_READONLY) || !zend_asymmetric_property_has_set_access(prop_info)) {
1490
467
        retval = NULL;
1491
467
      }
1492
512
    }
1493
5.87k
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
1494
3.73k
    if (EXPECTED(zobj->properties)) {
1495
1.77k
      if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1496
31
        if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1497
31
          GC_DELREF(zobj->properties);
1498
31
        }
1499
31
        zobj->properties = zend_array_dup(zobj->properties);
1500
31
      }
1501
1.77k
        if (EXPECTED((retval = zend_hash_find(zobj->properties, name)) != NULL)) {
1502
1.26k
        return retval;
1503
1.26k
        }
1504
1.77k
    }
1505
2.47k
    if (EXPECTED(!zobj->ce->__get) ||
1506
1.66k
        UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) {
1507
1.66k
      if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1508
101
        bool guarded = (zobj->ce->__get != NULL);
1509
101
        zend_object *instance = zend_lazy_object_init(zobj);
1510
101
        if (!instance) {
1511
12
          return &EG(error_zval);
1512
12
        }
1513
1514
89
        if (guarded && (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
1515
26
          uint32_t *guard = zend_get_property_guard(instance, name);
1516
26
          if (!(*guard & IN_GET)) {
1517
26
            (*guard) |= IN_GET;
1518
26
            retval = zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1519
26
            (*guard) &= ~IN_GET;
1520
26
            return retval;
1521
26
          }
1522
26
        }
1523
1524
63
        return zend_std_get_property_ptr_ptr(instance, name, type, cache_slot);
1525
89
      }
1526
1.56k
      if (UNEXPECTED(zobj->ce->ce_flags & ZEND_ACC_NO_DYNAMIC_PROPERTIES)) {
1527
16
        zend_forbidden_dynamic_property(zobj->ce, name);
1528
16
        return &EG(error_zval);
1529
16
      }
1530
1.54k
      if (UNEXPECTED(!(zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES))) {
1531
831
        if (UNEXPECTED(!zend_deprecated_dynamic_property(zobj, name))) {
1532
0
          return &EG(error_zval);
1533
0
        }
1534
831
      }
1535
1.54k
      if (UNEXPECTED(!zobj->properties)) {
1536
1.11k
        rebuild_object_properties_internal(zobj);
1537
1.11k
      }
1538
1.54k
      if (UNEXPECTED(type == BP_VAR_RW)) {
1539
280
        zend_error(E_WARNING, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1540
280
      }
1541
1.54k
      retval = zend_hash_add(zobj->properties, name, &EG(uninitialized_zval));
1542
1.54k
    }
1543
2.47k
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1544
471
    if (!(prop_info->flags & ZEND_ACC_VIRTUAL) && !zend_should_call_hook(prop_info, zobj)) {
1545
141
      property_offset = prop_info->offset;
1546
141
      if (!ZEND_TYPE_IS_SET(prop_info->type)) {
1547
135
        prop_info = NULL;
1548
135
      }
1549
141
      goto try_again;
1550
141
    }
1551
471
  } else if (zobj->ce->__get == NULL) {
1552
10
    retval = &EG(error_zval);
1553
10
  }
1554
1555
8.47k
  return retval;
1556
10.0k
}
1557
/* }}} */
1558
1559
ZEND_API void zend_std_unset_property(zend_object *zobj, zend_string *name, void **cache_slot) /* {{{ */
1560
1.33k
{
1561
1.33k
  uintptr_t property_offset;
1562
1.33k
  const zend_property_info *prop_info = NULL;
1563
1.33k
  uint32_t *guard = NULL;
1564
1565
1.33k
  property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__unset != NULL), cache_slot, &prop_info);
1566
1567
1.33k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
1568
801
    zval *slot = OBJ_PROP(zobj, property_offset);
1569
1570
801
    if (prop_info && UNEXPECTED(prop_info->flags & (ZEND_ACC_READONLY|ZEND_ACC_PPP_SET_MASK))) {
1571
240
      bool error;
1572
240
      if (Z_TYPE_P(slot) != IS_UNDEF || Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT || !zobj->ce->__unset) {
1573
213
        error = true;
1574
213
      } else {
1575
27
        guard = zend_get_property_guard(zobj, name);
1576
27
        error = (*guard) & IN_UNSET;
1577
27
      }
1578
240
      if (error) {
1579
219
        if ((prop_info->flags & ZEND_ACC_READONLY)
1580
76
         && Z_TYPE_P(slot) != IS_UNDEF
1581
41
         && !(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
184
        if ((prop_info->flags & ZEND_ACC_PPP_SET_MASK) && !zend_asymmetric_property_has_set_access(prop_info)) {
1586
85
          zend_asymmetric_visibility_property_modification_error(prop_info, "unset");
1587
85
          return;
1588
85
        }
1589
184
      }
1590
240
    }
1591
1592
681
    if (Z_TYPE_P(slot) != IS_UNDEF) {
1593
536
      if (UNEXPECTED(Z_ISREF_P(slot)) &&
1594
87
          (ZEND_DEBUG || ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(slot)))) {
1595
87
        if (prop_info) {
1596
79
          ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(slot), prop_info);
1597
79
        }
1598
87
      }
1599
536
      zval tmp;
1600
536
      ZVAL_COPY_VALUE(&tmp, slot);
1601
536
      ZVAL_UNDEF(slot);
1602
536
      zval_ptr_dtor(&tmp);
1603
536
      if (zobj->properties) {
1604
192
        HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
1605
192
      }
1606
536
      return;
1607
536
    }
1608
145
    if (UNEXPECTED(Z_PROP_FLAG_P(slot) & IS_PROP_UNINIT)) {
1609
119
      if (UNEXPECTED(zend_lazy_object_must_init(zobj) && (Z_PROP_FLAG_P(slot) & IS_PROP_LAZY))) {
1610
24
        zobj = zend_lazy_object_init(zobj);
1611
24
        if (!zobj) {
1612
2
          return;
1613
2
        }
1614
22
        zend_std_unset_property(zobj, name, cache_slot);
1615
22
        return;
1616
24
      }
1617
1618
      /* Reset the IS_PROP_UNINIT flag, if it exists and bypass __unset(). */
1619
95
      Z_PROP_FLAG_P(slot) = 0;
1620
95
      return;
1621
119
    }
1622
530
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))
1623
472
   && EXPECTED(zobj->properties != NULL)) {
1624
320
    if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1625
3
      if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1626
3
        GC_DELREF(zobj->properties);
1627
3
      }
1628
3
      zobj->properties = zend_array_dup(zobj->properties);
1629
3
    }
1630
320
    if (EXPECTED(zend_hash_del(zobj->properties, name) != FAILURE)) {
1631
205
      return;
1632
205
    }
1633
320
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
1634
35
    zend_throw_error(NULL, "Cannot unset hooked property %s::$%s",
1635
35
      ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
1636
35
    return;
1637
175
  } 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
313
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
1645
313
      && 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
308
  if (zobj->ce->__unset) {
1658
164
    if (!guard) {
1659
143
      guard = zend_get_property_guard(zobj, name);
1660
143
    }
1661
164
    if (!((*guard) & IN_UNSET)) {
1662
      /* have unsetter - try with it! */
1663
105
      GC_ADDREF(zobj);
1664
105
      (*guard) |= IN_UNSET; /* prevent circular unsetting */
1665
105
      zend_std_call_unsetter(zobj, name);
1666
105
      (*guard) &= ~IN_UNSET;
1667
105
      OBJ_RELEASE(zobj);
1668
105
      return;
1669
105
    } 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
50
    } else {
1675
      /* Nothing to do: The property already does not exist. */
1676
50
    }
1677
164
  }
1678
1679
194
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
1680
48
    zobj = zend_lazy_object_init(zobj);
1681
48
    if (!zobj) {
1682
2
      return;
1683
2
    }
1684
1685
46
    if (UNEXPECTED(guard && zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS)) {
1686
16
      guard = zend_get_property_guard(zobj, name);
1687
16
      if (!((*guard) & IN_UNSET)) {
1688
11
        (*guard) |= IN_UNSET;
1689
11
        zend_std_unset_property(zobj, name, cache_slot);
1690
11
        (*guard) &= ~IN_UNSET;
1691
11
        return;
1692
11
      }
1693
16
    }
1694
1695
35
    zend_std_unset_property(zobj, name, cache_slot);
1696
35
    return;
1697
46
  }
1698
194
}
1699
/* }}} */
1700
1701
ZEND_API void zend_std_unset_dimension(zend_object *object, zval *offset) /* {{{ */
1702
189
{
1703
189
  const zend_class_entry *ce = object->ce;
1704
189
  zval tmp_offset;
1705
1706
189
  zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
1707
189
  if (EXPECTED(funcs)) {
1708
184
    ZVAL_COPY_DEREF(&tmp_offset, offset);
1709
184
    GC_ADDREF(object);
1710
184
    zend_call_known_instance_method_with_1_params(funcs->zf_offsetunset, object, NULL, &tmp_offset);
1711
184
    OBJ_RELEASE(object);
1712
184
    zval_ptr_dtor(&tmp_offset);
1713
184
  } else {
1714
5
      zend_bad_array_access(ce);
1715
5
  }
1716
189
}
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
65
{
1721
65
  zval *func;
1722
65
  zend_function *fbc;
1723
1724
65
  if (scope != ce && scope && is_derived_class(ce, scope)) {
1725
49
    func = zend_hash_find(&scope->function_table, function_name);
1726
49
    if (func != NULL) {
1727
49
      fbc = Z_FUNC_P(func);
1728
49
      if ((fbc->common.fn_flags & ZEND_ACC_PRIVATE)
1729
49
       && fbc->common.scope == scope) {
1730
38
        return fbc;
1731
38
      }
1732
49
    }
1733
49
  }
1734
27
  return NULL;
1735
65
}
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
749
{
1742
749
  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.45k
  while (fbc_scope) {
1748
850
    if (fbc_scope==scope) {
1749
142
      return 1;
1750
142
    }
1751
708
    fbc_scope = fbc_scope->parent;
1752
708
  }
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
1.03k
  while (scope) {
1758
748
    if (scope==ce) {
1759
317
      return 1;
1760
317
    }
1761
431
    scope = scope->parent;
1762
431
  }
1763
290
  return 0;
1764
607
}
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.70k
{
1770
5.70k
  size_t mname_len;
1771
5.70k
  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.70k
  static const void *dummy = (void*)(intptr_t)2;
1776
1777
5.70k
  if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
1778
4.36k
    func = &EG(trampoline).op_array;
1779
4.36k
  } else {
1780
1.33k
    func = ecalloc(1, sizeof(zend_op_array));
1781
1.33k
  }
1782
1783
5.70k
  func->type = ZEND_USER_FUNCTION;
1784
5.70k
  func->arg_flags[0] = 0;
1785
5.70k
  func->arg_flags[1] = 0;
1786
5.70k
  func->arg_flags[2] = 0;
1787
5.70k
  func->fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE
1788
5.70k
    | ZEND_ACC_PUBLIC
1789
5.70k
    | ZEND_ACC_VARIADIC
1790
5.70k
    | (fbc->common.fn_flags & (ZEND_ACC_RETURN_REFERENCE|ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_NODISCARD|ZEND_ACC_STATIC));
1791
5.70k
  func->fn_flags2 = 0;
1792
  /* Attributes outlive the trampoline because they are created by the compiler. */
1793
5.70k
  func->attributes = fbc->common.attributes;
1794
5.70k
  func->opcodes = &EG(call_trampoline_op);
1795
5.70k
  ZEND_MAP_PTR_INIT(func->run_time_cache, (void**)dummy);
1796
5.70k
  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.70k
  func->last_var = 0;
1804
5.70k
  uint32_t min_T = 2 + ZEND_OBSERVER_ENABLED;
1805
5.70k
  func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, min_T) : min_T;
1806
5.70k
  func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC();
1807
5.70k
  func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0;
1808
5.70k
  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.70k
  if (UNEXPECTED((mname_len = strlen(ZSTR_VAL(method_name))) != ZSTR_LEN(method_name))) {
1813
51
    func->function_name = zend_string_init(ZSTR_VAL(method_name), mname_len, 0);
1814
5.64k
  } else {
1815
5.64k
    func->function_name = zend_string_copy(method_name);
1816
5.64k
  }
1817
1818
5.70k
  func->prototype = NULL;
1819
5.70k
  func->prop_info = NULL;
1820
5.70k
  func->num_args = 0;
1821
5.70k
  func->required_num_args = 0;
1822
5.70k
  func->arg_info = zend_call_trampoline_arginfo;
1823
1824
5.70k
  return (zend_function*)func;
1825
5.70k
}
1826
/* }}} */
1827
1828
static ZEND_FUNCTION(zend_parent_hook_get_trampoline)
1829
298
{
1830
298
  zend_object *obj = Z_PTR_P(ZEND_THIS);
1831
298
  zend_string *prop_name = EX(func)->internal_function.reserved[0];
1832
1833
298
  if (UNEXPECTED(ZEND_NUM_ARGS() != 0)) {
1834
14
    zend_wrong_parameters_none_error();
1835
14
    goto clean;
1836
14
  }
1837
1838
284
  zval rv;
1839
284
  const zval *retval = obj->handlers->read_property(obj, prop_name, BP_VAR_R, NULL, &rv);
1840
284
  if (retval == &rv) {
1841
0
    RETVAL_COPY_VALUE(retval);
1842
284
  } else {
1843
284
    RETVAL_COPY(retval);
1844
284
  }
1845
1846
298
clean:
1847
298
  zend_string_release(EX(func)->common.function_name);
1848
298
  zend_free_trampoline(EX(func));
1849
298
  EX(func) = NULL;
1850
298
}
1851
1852
static ZEND_FUNCTION(zend_parent_hook_set_trampoline)
1853
37
{
1854
37
  zend_object *obj = Z_PTR_P(ZEND_THIS);
1855
37
  zend_string *prop_name = EX(func)->internal_function.reserved[0];
1856
1857
37
  zval *value;
1858
1859
97
  ZEND_PARSE_PARAMETERS_START(1, 1)
1860
97
    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
37
clean:
1866
37
  zend_string_release(EX(func)->common.function_name);
1867
37
  zend_free_trampoline(EX(func));
1868
37
  EX(func) = NULL;
1869
37
}
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
340
{
1875
340
  zend_function *func;
1876
340
  if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
1877
340
    func = &EG(trampoline);
1878
340
  } else {
1879
0
    func = (zend_function *)(uintptr_t)ecalloc(1, sizeof(zend_internal_function));
1880
0
  }
1881
340
  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
340
  func->common.T = 0;
1885
340
  func->common.arg_flags[0] = 0;
1886
340
  func->common.arg_flags[1] = 0;
1887
340
  func->common.arg_flags[2] = 0;
1888
340
  func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
1889
340
  func->common.fn_flags2 = 0;
1890
340
  func->common.function_name = zend_string_concat3(
1891
340
    "$", 1, ZSTR_VAL(prop_name), ZSTR_LEN(prop_name),
1892
340
    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
340
  uint32_t args = kind == ZEND_PROPERTY_HOOK_GET ? 0 : 1;
1895
340
  func->common.num_args = args;
1896
340
  func->common.required_num_args = args;
1897
340
  func->common.scope = prop_info->ce;
1898
340
  func->common.prototype = NULL;
1899
340
  func->common.prop_info = prop_info;
1900
340
  func->common.arg_info = zend_property_hook_arginfo;
1901
340
  func->internal_function.handler = kind == ZEND_PROPERTY_HOOK_GET
1902
340
    ? ZEND_FN(zend_parent_hook_get_trampoline)
1903
340
    : ZEND_FN(zend_parent_hook_set_trampoline);
1904
340
  func->internal_function.module = NULL;
1905
1906
340
  func->internal_function.reserved[0] = prop_name;
1907
340
  func->internal_function.reserved[1] = NULL;
1908
1909
340
  return func;
1910
340
}
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
65
{
1914
65
  zend_throw_error(NULL, "Call to %s method %s::%s() from %s%s",
1915
65
    zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name),
1916
65
    scope ? "scope " : "global scope",
1917
65
    scope ? ZSTR_VAL(scope->name) : ""
1918
65
  );
1919
65
}
1920
/* }}} */
1921
1922
ZEND_API ZEND_COLD zend_never_inline void zend_abstract_method_call(const zend_function *fbc) /* {{{ */
1923
34
{
1924
34
  zend_throw_error(NULL, "Cannot call abstract method %s::%s()",
1925
34
    ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
1926
34
}
1927
/* }}} */
1928
1929
ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
1930
92.3k
{
1931
92.3k
  zend_object *zobj = *obj_ptr;
1932
92.3k
  zval *func;
1933
92.3k
  zend_function *fbc;
1934
92.3k
  zend_string *lc_method_name;
1935
92.3k
  ALLOCA_FLAG(use_heap);
1936
1937
92.3k
  if (EXPECTED(key != NULL)) {
1938
87.6k
    lc_method_name = Z_STR_P(key);
1939
#ifdef ZEND_ALLOCA_MAX_SIZE
1940
    use_heap = 0;
1941
#endif
1942
87.6k
  } else {
1943
4.64k
    ZSTR_ALLOCA_ALLOC(lc_method_name, ZSTR_LEN(method_name), use_heap);
1944
4.64k
    zend_str_tolower_copy(ZSTR_VAL(lc_method_name), ZSTR_VAL(method_name), ZSTR_LEN(method_name));
1945
4.64k
  }
1946
1947
92.3k
  if (UNEXPECTED((func = zend_hash_find(&zobj->ce->function_table, lc_method_name)) == NULL)) {
1948
4.99k
    if (UNEXPECTED(!key)) {
1949
3.84k
      ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1950
3.84k
    }
1951
4.99k
    if (zobj->ce->__call) {
1952
4.24k
      return zend_get_call_trampoline_func(zobj->ce->__call, method_name);
1953
4.24k
    } else {
1954
749
      return NULL;
1955
749
    }
1956
4.99k
  }
1957
1958
87.3k
  fbc = Z_FUNC_P(func);
1959
1960
  /* Check access level */
1961
87.3k
  if (fbc->op_array.fn_flags & (ZEND_ACC_CHANGED|ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED)) {
1962
511
    const zend_class_entry *scope = zend_get_executed_scope();
1963
1964
511
    if (fbc->common.scope != scope) {
1965
225
      if (fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
1966
65
        zend_function *updated_fbc = zend_get_parent_private_method(scope, zobj->ce, lc_method_name);
1967
1968
65
        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
21
          goto exit;
1973
21
        }
1974
65
      }
1975
166
      if (UNEXPECTED(fbc->op_array.fn_flags & ZEND_ACC_PRIVATE)
1976
110
       || UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
1977
91
        if (zobj->ce->__call) {
1978
57
          fbc = zend_get_call_trampoline_func(zobj->ce->__call, method_name);
1979
57
        } else {
1980
34
          zend_bad_method_call(fbc, method_name, scope);
1981
34
          fbc = NULL;
1982
34
        }
1983
91
      }
1984
166
    }
1985
511
  }
1986
1987
87.3k
exit:
1988
87.3k
  if (fbc && UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
1989
0
    zend_abstract_method_call(fbc);
1990
0
    fbc = NULL;
1991
0
  }
1992
87.3k
  if (UNEXPECTED(!key)) {
1993
799
    ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
1994
799
  }
1995
87.3k
  return fbc;
1996
87.3k
}
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.58k
{
2002
1.58k
  zend_object *object;
2003
1.58k
  if (ce->__call &&
2004
927
    (object = zend_get_this_object(EG(current_execute_data))) != NULL &&
2005
625
    instanceof_function(object->ce, ce)) {
2006
    /* Call the top-level defined __call().
2007
     * see: tests/classes/__call_004.phpt  */
2008
2009
617
    ZEND_ASSERT(object->ce->__call);
2010
617
    return zend_get_call_trampoline_func(object->ce->__call, function_name);
2011
963
  } else if (ce->__callstatic) {
2012
770
    return zend_get_call_trampoline_func(ce->__callstatic, function_name);
2013
770
  } else {
2014
193
    return NULL;
2015
193
  }
2016
1.58k
}
2017
2018
ZEND_API zend_function *zend_std_get_static_method(const zend_class_entry *ce, zend_string *function_name, const zval *key) /* {{{ */
2019
6.24k
{
2020
6.24k
  zend_string *lc_function_name;
2021
6.24k
  if (EXPECTED(key != NULL)) {
2022
5.54k
    lc_function_name = Z_STR_P(key);
2023
5.54k
  } else {
2024
701
    lc_function_name = zend_string_tolower(function_name);
2025
701
  }
2026
2027
6.24k
  zend_function *fbc;
2028
6.24k
  zval *func = zend_hash_find(&ce->function_table, lc_function_name);
2029
6.24k
  if (EXPECTED(func)) {
2030
4.70k
    fbc = Z_FUNC_P(func);
2031
4.70k
    if (!(fbc->common.fn_flags & ZEND_ACC_PUBLIC)) {
2032
161
      const zend_class_entry *scope = zend_get_executed_scope();
2033
161
      ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_PUBLIC));
2034
161
      if (!zend_check_method_accessible(fbc, scope)) {
2035
40
        zend_function *fallback_fbc = get_static_method_fallback(ce, function_name);
2036
40
        if (!fallback_fbc) {
2037
20
          zend_bad_method_call(fbc, function_name, scope);
2038
20
        }
2039
40
        fbc = fallback_fbc;
2040
40
      }
2041
161
    }
2042
4.70k
  } else {
2043
1.54k
    fbc = get_static_method_fallback(ce, function_name);
2044
1.54k
  }
2045
2046
6.24k
  if (UNEXPECTED(!key)) {
2047
701
    zend_string_release_ex(lc_function_name, 0);
2048
701
  }
2049
2050
6.24k
  if (EXPECTED(fbc)) {
2051
6.05k
    if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
2052
28
      zend_abstract_method_call(fbc);
2053
28
      goto fail;
2054
6.02k
    } 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
6.05k
  }
2064
2065
6.21k
  return fbc;
2066
2067
28
 fail:
2068
28
  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
28
  return NULL;
2074
6.24k
}
2075
/* }}} */
2076
2077
ZEND_API void zend_class_init_statics(zend_class_entry *class_type) /* {{{ */
2078
1.48k
{
2079
1.48k
  zval *p;
2080
2081
1.48k
  if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
2082
1.39k
    if (class_type->parent) {
2083
128
      zend_class_init_statics(class_type->parent);
2084
128
    }
2085
2086
1.39k
    ZEND_MAP_PTR_SET(class_type->static_members_table, emalloc(sizeof(zval) * class_type->default_static_members_count));
2087
3.89k
    for (uint32_t i = 0; i < class_type->default_static_members_count; i++) {
2088
2.50k
      p = &class_type->default_static_members_table[i];
2089
2.50k
      if (Z_TYPE_P(p) == IS_INDIRECT) {
2090
135
        zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
2091
135
        ZVAL_DEINDIRECT(q);
2092
135
        ZVAL_INDIRECT(&CE_STATIC_MEMBERS(class_type)[i], q);
2093
2.36k
      } else {
2094
2.36k
        ZVAL_COPY_OR_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
2095
2.36k
      }
2096
2.50k
    }
2097
1.39k
  }
2098
1.48k
} /* }}} */
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.63k
{
2102
3.63k
  zval *ret;
2103
3.63k
  zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property_name);
2104
3.63k
  *property_info_ptr = property_info;
2105
2106
3.63k
  if (UNEXPECTED(property_info == NULL)) {
2107
254
    goto undeclared_property;
2108
254
  }
2109
2110
3.38k
  if (!(property_info->flags & ZEND_ACC_PUBLIC)) {
2111
955
    const zend_class_entry *scope = get_fake_or_executed_scope();
2112
955
    if (property_info->ce != scope) {
2113
162
      if (UNEXPECTED(property_info->flags & ZEND_ACC_PRIVATE)
2114
112
       || UNEXPECTED(!is_protected_compatible_scope(property_info->prototype->ce, scope))) {
2115
112
        if (type != BP_VAR_IS) {
2116
20
          zend_bad_property_access(property_info, ce, property_name);
2117
20
        }
2118
112
        return NULL;
2119
112
      }
2120
162
    }
2121
955
  }
2122
2123
3.27k
  if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
2124
291
undeclared_property:
2125
291
    if (type != BP_VAR_IS) {
2126
211
      zend_throw_error(NULL, "Access to undeclared static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
2127
211
    }
2128
291
    return NULL;
2129
37
  }
2130
2131
3.23k
  if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
2132
281
    if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
2133
25
      return NULL;
2134
25
    }
2135
281
  }
2136
2137
  /* Ensure static properties are initialized. */
2138
3.20k
  if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL)) {
2139
1.22k
    zend_class_init_statics(ce);
2140
1.22k
  }
2141
2142
3.20k
  ret = CE_STATIC_MEMBERS(ce) + property_info->offset;
2143
3.20k
  ZVAL_DEINDIRECT(ret);
2144
2145
3.20k
  if (UNEXPECTED((type == BP_VAR_R || type == BP_VAR_RW)
2146
3.20k
        && Z_TYPE_P(ret) == IS_UNDEF && ZEND_TYPE_IS_SET(property_info->type))) {
2147
17
    zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
2148
17
      ZSTR_VAL(property_info->ce->name), ZSTR_VAL(property_name));
2149
17
    return NULL;
2150
17
  }
2151
2152
3.19k
  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.19k
  return ret;
2160
3.20k
}
2161
/* }}} */
2162
2163
ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, int type) /* {{{ */
2164
13
{
2165
13
  zend_property_info *prop_info;
2166
13
  return zend_std_get_static_property_with_info(ce, property_name, type, &prop_info);
2167
13
}
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
44
{
2178
44
  if (scope) {
2179
11
    zend_throw_error(NULL, "Call to %s %s::__construct() from scope %s",
2180
11
      zend_visibility_string(constructor->common.fn_flags),
2181
11
      ZSTR_VAL(constructor->common.scope->name),
2182
11
      ZSTR_VAL(scope->name)
2183
11
    );
2184
33
  } else {
2185
33
    zend_throw_error(NULL, "Call to %s %s::__construct() from global scope",
2186
33
      zend_visibility_string(constructor->common.fn_flags),
2187
33
      ZSTR_VAL(constructor->common.scope->name)
2188
33
    );
2189
33
  }
2190
44
}
2191
/* }}} */
2192
2193
ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
2194
465k
{
2195
465k
  zend_function *constructor = zobj->ce->constructor;
2196
2197
465k
  if (constructor) {
2198
366k
    if (UNEXPECTED(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC))) {
2199
87
      const zend_class_entry *scope = get_fake_or_executed_scope();
2200
87
      ZEND_ASSERT(!(constructor->common.fn_flags & ZEND_ACC_PUBLIC));
2201
87
      if (!zend_check_method_accessible(constructor, scope)) {
2202
44
        zend_bad_constructor_call(constructor, scope);
2203
44
        zend_object_store_ctor_failed(zobj);
2204
44
        constructor = NULL;
2205
44
      }
2206
87
    }
2207
366k
  }
2208
2209
465k
  return constructor;
2210
465k
}
2211
/* }}} */
2212
2213
ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
2214
763
{
2215
763
  zend_object *zobj1, *zobj2;
2216
2217
763
  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
763
  if (Z_TYPE_P(o1) != Z_TYPE_P(o2)) {
2223
    /* Object and non-object */
2224
572
    zval *object;
2225
572
    zval *value;
2226
572
    zval casted;
2227
572
    bool object_lhs;
2228
572
    if (Z_TYPE_P(o1) == IS_OBJECT) {
2229
442
      object = o1;
2230
442
      value = o2;
2231
442
      object_lhs = true;
2232
442
    } else {
2233
130
      object = o2;
2234
130
      value = o1;
2235
130
      object_lhs = false;
2236
130
    }
2237
572
    ZEND_ASSERT(Z_TYPE_P(value) != IS_OBJECT);
2238
572
    uint8_t target_type = Z_TYPE_P(value);
2239
    /* Should be handled in zend_compare(). */
2240
572
    ZEND_ASSERT(target_type != IS_FALSE && target_type != IS_TRUE);
2241
572
    if (Z_OBJ_HT_P(object)->cast_object(Z_OBJ_P(object), &casted, target_type) == FAILURE) {
2242
      // TODO: Less crazy.
2243
518
      if (target_type == IS_LONG || target_type == IS_DOUBLE) {
2244
289
        zend_error(E_NOTICE, "Object of class %s could not be converted to %s",
2245
289
               ZSTR_VAL(Z_OBJCE_P(object)->name), zend_get_type_by_const(target_type));
2246
289
        if (target_type == IS_LONG) {
2247
253
          ZVAL_LONG(&casted, 1);
2248
253
        } else {
2249
36
          ZVAL_DOUBLE(&casted, 1.0);
2250
36
        }
2251
289
      } else {
2252
229
        return object_lhs ? 1 : -1;
2253
229
      }
2254
518
    }
2255
343
    int ret = object_lhs ? zend_compare(&casted, value) : zend_compare(value, &casted);
2256
343
    zval_ptr_dtor(&casted);
2257
343
    return ret;
2258
572
  }
2259
2260
191
  zobj1 = Z_OBJ_P(o1);
2261
191
  zobj2 = Z_OBJ_P(o2);
2262
2263
191
  if (zobj1 == zobj2) {
2264
0
    return 0; /* the same object */
2265
0
  }
2266
191
  if (zobj1->ce != zobj2->ce) {
2267
16
    return ZEND_UNCOMPARABLE; /* different classes */
2268
16
  }
2269
175
  if (!zobj1->properties && !zobj2->properties
2270
139
      && !zend_object_is_lazy(zobj1) && !zend_object_is_lazy(zobj2)) {
2271
106
    zend_property_info *info;
2272
106
    int i;
2273
2274
106
    if (!zobj1->ce->default_properties_count) {
2275
57
      return 0;
2276
57
    }
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
49
    if (UNEXPECTED(Z_IS_RECURSIVE_P(o1))) {
2284
6
      zend_throw_error(NULL, "Nesting level too deep - recursive dependency?");
2285
6
      return ZEND_UNCOMPARABLE;
2286
6
    }
2287
43
    Z_PROTECT_RECURSION_P(o1);
2288
2289
43
    GC_ADDREF(zobj1);
2290
43
    GC_ADDREF(zobj2);
2291
43
    int ret;
2292
2293
66
    for (i = 0; i < zobj1->ce->default_properties_count; i++) {
2294
60
      zval *p1, *p2;
2295
2296
60
      info = zobj1->ce->properties_info_table[i];
2297
2298
60
      if (!info) {
2299
7
        continue;
2300
7
      }
2301
2302
53
      p1 = OBJ_PROP(zobj1, info->offset);
2303
53
      p2 = OBJ_PROP(zobj2, info->offset);
2304
2305
53
      if (Z_TYPE_P(p1) != IS_UNDEF) {
2306
53
        if (Z_TYPE_P(p2) != IS_UNDEF) {
2307
53
          ret = zend_compare(p1, p2);
2308
53
          if (ret != 0) {
2309
37
            Z_UNPROTECT_RECURSION_P(o1);
2310
37
            goto done;
2311
37
          }
2312
53
        } else {
2313
0
          Z_UNPROTECT_RECURSION_P(o1);
2314
0
          ret = 1;
2315
0
          goto done;
2316
0
        }
2317
53
      } 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
53
    }
2325
2326
6
    Z_UNPROTECT_RECURSION_P(o1);
2327
6
    ret = 0;
2328
2329
43
done:
2330
43
    OBJ_RELEASE(zobj1);
2331
43
    OBJ_RELEASE(zobj2);
2332
2333
43
    return ret;
2334
69
  } else {
2335
69
    GC_ADDREF(zobj1);
2336
69
    GC_ADDREF(zobj2);
2337
2338
69
    int ret = zend_compare_symbol_tables(
2339
69
        zend_std_get_properties_ex(zobj1),
2340
69
        zend_std_get_properties_ex(zobj2));
2341
2342
69
    OBJ_RELEASE(zobj1);
2343
69
    OBJ_RELEASE(zobj2);
2344
2345
69
    return ret;
2346
69
  }
2347
175
}
2348
/* }}} */
2349
2350
ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
2351
594
{
2352
594
  return ZEND_UNCOMPARABLE;
2353
594
}
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.05k
{
2358
1.05k
  bool result;
2359
1.05k
  zval *value = NULL;
2360
1.05k
  uintptr_t property_offset;
2361
1.05k
  const zend_property_info *prop_info = NULL;
2362
2363
1.05k
  property_offset = zend_get_property_offset(zobj->ce, name, 1, cache_slot, &prop_info);
2364
2365
1.05k
  if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
2366
267
try_again:
2367
267
    value = OBJ_PROP(zobj, property_offset);
2368
267
    if (Z_TYPE_P(value) != IS_UNDEF) {
2369
142
      goto found;
2370
142
    }
2371
125
    if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) {
2372
      /* Skip __isset() for uninitialized typed properties */
2373
77
      goto lazy_init;
2374
77
    }
2375
803
  } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
2376
686
    if (EXPECTED(zobj->properties != NULL)) {
2377
247
      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
241
      value = zend_hash_find(zobj->properties, name);
2394
241
      if (value) {
2395
187
        if (cache_slot) {
2396
42
          uintptr_t idx = (char*)value - (char*)zobj->properties->arData;
2397
42
          CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
2398
42
        }
2399
335
found:
2400
335
        if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
2401
15
          result = zend_is_true(value);
2402
320
        } else if (has_set_exists < ZEND_PROPERTY_NOT_EMPTY) {
2403
272
          ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
2404
272
          ZVAL_DEREF(value);
2405
272
          result = (Z_TYPE_P(value) != IS_NULL);
2406
272
        } else {
2407
48
          ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
2408
48
          result = true;
2409
48
        }
2410
335
        goto exit;
2411
335
      }
2412
241
    }
2413
686
  } else if (IS_HOOKED_PROPERTY_OFFSET(property_offset)) {
2414
83
    zend_function *get = prop_info->hooks[ZEND_PROPERTY_HOOK_GET];
2415
2416
83
    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
83
    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
72
    zval rv;
2436
72
    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
67
    if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
2445
16
      result = zend_is_true(&rv);
2446
51
    } else {
2447
51
      ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
2448
51
      result = Z_TYPE(rv) != IS_NULL
2449
35
        && (Z_TYPE(rv) != IS_REFERENCE || Z_TYPE_P(Z_REFVAL(rv)) != IS_NULL);
2450
51
    }
2451
67
    zval_ptr_dtor(&rv);
2452
67
    return result;
2453
67
  } 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
575
  if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
2462
575
      && zend_lazy_object_initialized(zobj))) {
2463
10
    zend_object *instance = zend_lazy_object_get_instance(zobj);
2464
10
    if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
2465
10
      uint32_t *instance_guard = zend_get_property_guard(instance, name);
2466
10
      if ((*instance_guard) & IN_ISSET) {
2467
10
        return zend_std_has_property(instance, name, has_set_exists, cache_slot);
2468
10
      }
2469
10
    }
2470
10
  }
2471
2472
565
  if (!zobj->ce->__isset) {
2473
166
    goto lazy_init;
2474
166
  }
2475
2476
399
  result = false;
2477
399
  if (has_set_exists != ZEND_PROPERTY_EXISTS) {
2478
396
    uint32_t *guard = zend_get_property_guard(zobj, name);
2479
2480
396
    if (!((*guard) & IN_ISSET)) {
2481
302
      zval rv;
2482
2483
      /* have issetter - try with it! */
2484
302
      GC_ADDREF(zobj);
2485
302
      (*guard) |= IN_ISSET; /* prevent circular getting */
2486
302
      zend_std_call_issetter(zobj, name, &rv);
2487
302
      result = zend_is_true(&rv);
2488
302
      zval_ptr_dtor(&rv);
2489
302
      if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY && result) {
2490
32
        if (EXPECTED(!EG(exception)) && zobj->ce->__get && !((*guard) & IN_GET)) {
2491
27
          (*guard) |= IN_GET;
2492
27
          zend_std_call_getter(zobj, name, &rv);
2493
27
          (*guard) &= ~IN_GET;
2494
27
          result = i_zend_is_true(&rv);
2495
27
          zval_ptr_dtor(&rv);
2496
27
        } else {
2497
5
          result = false;
2498
5
        }
2499
32
      }
2500
302
      (*guard) &= ~IN_ISSET;
2501
302
      OBJ_RELEASE(zobj);
2502
302
    } else {
2503
94
      goto lazy_init;
2504
94
    }
2505
396
  }
2506
2507
959
exit:
2508
959
  return result;
2509
2510
337
lazy_init:
2511
337
  if (UNEXPECTED(zend_lazy_object_must_init(zobj))) {
2512
18
    if (!value || (Z_PROP_FLAG_P(value) & IS_PROP_LAZY)) {
2513
18
      zobj = zend_lazy_object_init(zobj);
2514
18
      if (!zobj) {
2515
0
        result = false;
2516
0
        goto exit;
2517
0
      }
2518
2519
18
      if (UNEXPECTED(zobj->ce->__isset)) {
2520
8
        uint32_t *guard = zend_get_property_guard(zobj, name);
2521
8
        if (!((*guard) & IN_ISSET)) {
2522
8
          (*guard) |= IN_ISSET;
2523
8
          result = zend_std_has_property(zobj, name, has_set_exists, cache_slot);
2524
8
          (*guard) &= ~IN_ISSET;
2525
8
          return result;
2526
8
        }
2527
8
      }
2528
2529
10
      return zend_std_has_property(zobj, name, has_set_exists, cache_slot);
2530
18
    }
2531
18
  }
2532
2533
319
  result = false;
2534
319
  goto exit;
2535
337
}
2536
/* }}} */
2537
2538
ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj) /* {{{ */
2539
18.8k
{
2540
18.8k
  return zend_string_copy(zobj->ce->name);
2541
18.8k
}
2542
/* }}} */
2543
2544
ZEND_API zend_result zend_std_cast_object_tostring(zend_object *readobj, zval *writeobj, int type) /* {{{ */
2545
10.2k
{
2546
10.2k
  switch (type) {
2547
8.67k
    case IS_STRING: {
2548
8.67k
      const zend_class_entry *ce = readobj->ce;
2549
8.67k
      if (ce->__tostring) {
2550
7.61k
        zval retval;
2551
7.61k
        GC_ADDREF(readobj);
2552
7.61k
        zend_call_known_instance_method_with_0_params(ce->__tostring, readobj, &retval);
2553
7.61k
        zend_object_release(readobj);
2554
7.61k
        if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
2555
5.07k
is_string:
2556
5.07k
          ZVAL_COPY_VALUE(writeobj, &retval);
2557
5.07k
          return SUCCESS;
2558
5.07k
        } 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.59k
      return FAILURE;
2568
8.67k
    }
2569
419
    case _IS_BOOL:
2570
419
      ZVAL_TRUE(writeobj);
2571
419
      return SUCCESS;
2572
1.11k
    default:
2573
1.11k
      return FAILURE;
2574
10.2k
  }
2575
10.2k
}
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
283
{
2580
283
  zend_class_entry *ce = obj->ce;
2581
283
  const zval *func = zend_hash_find_known_hash(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE));
2582
2583
283
  if (func == NULL) {
2584
31
    return FAILURE;
2585
31
  }
2586
252
  *fptr_ptr = Z_FUNC_P(func);
2587
252
  *ce_ptr = ce;
2588
252
  *obj_ptr = obj;
2589
2590
252
  return SUCCESS;
2591
283
}
2592
/* }}} */
2593
2594
18.5k
ZEND_API HashTable *zend_std_get_properties_for(zend_object *obj, zend_prop_purpose purpose) {
2595
18.5k
  HashTable *ht;
2596
18.5k
  switch (purpose) {
2597
17.5k
    case ZEND_PROP_PURPOSE_DEBUG:
2598
17.5k
      if (obj->handlers->get_debug_info) {
2599
17.5k
        int is_temp;
2600
17.5k
        ht = obj->handlers->get_debug_info(obj, &is_temp);
2601
17.5k
        if (ht && !is_temp) {
2602
15.7k
          GC_TRY_ADDREF(ht);
2603
15.7k
        }
2604
17.5k
        return ht;
2605
17.5k
      }
2606
0
      ZEND_FALLTHROUGH;
2607
243
    case ZEND_PROP_PURPOSE_JSON:
2608
527
    case ZEND_PROP_PURPOSE_GET_OBJECT_VARS:
2609
691
    case ZEND_PROP_PURPOSE_VAR_EXPORT:
2610
691
      if (obj->ce->num_hooked_props) {
2611
413
        return zend_hooked_object_build_properties(obj);
2612
413
      }
2613
278
      ht = obj->handlers->get_properties(obj);
2614
278
      if (ht) {
2615
278
        GC_TRY_ADDREF(ht);
2616
278
      }
2617
278
      return ht;
2618
170
    case ZEND_PROP_PURPOSE_ARRAY_CAST:
2619
170
      ht = zend_get_properties_no_lazy_init(obj);
2620
170
      if (ht) {
2621
170
        GC_TRY_ADDREF(ht);
2622
170
      }
2623
170
      return ht;
2624
181
    case ZEND_PROP_PURPOSE_SERIALIZE: {
2625
181
      if (zend_object_is_lazy(obj)
2626
124
          && !zend_lazy_object_initialize_on_serialize(obj)) {
2627
35
        ht = zend_get_properties_no_lazy_init(obj);
2628
146
      } else {
2629
146
        ht = obj->handlers->get_properties(obj);
2630
146
      }
2631
181
      if (ht) {
2632
181
        GC_TRY_ADDREF(ht);
2633
181
      }
2634
181
      return ht;
2635
691
    }
2636
0
    default:
2637
0
      ZEND_UNREACHABLE();
2638
0
      return NULL;
2639
18.5k
  }
2640
18.5k
}
2641
2642
19.0k
ZEND_API HashTable *zend_get_properties_for(zval *obj, zend_prop_purpose purpose) {
2643
19.0k
  zend_object *zobj = Z_OBJ_P(obj);
2644
2645
19.0k
  if (zobj->handlers->get_properties_for) {
2646
556
    return zobj->handlers->get_properties_for(zobj, purpose);
2647
556
  }
2648
2649
18.4k
  return zend_std_get_properties_for(zobj, purpose);
2650
19.0k
}
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
}