Coverage Report

Created: 2026-02-09 07:07

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