Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Zend/tests/lazy_objects/gh18038-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ var_dump($obj->prop);
--EXPECT--
init
string(19) "RealInstance::__set"
string(12) "Proxy::__set"
int(2)
1 change: 0 additions & 1 deletion Zend/tests/lazy_objects/gh18038-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var_dump($real->prop);
--EXPECTF--
init
string(19) "RealInstance::__get"
string(12) "Proxy::__get"

Warning: Undefined property: RealInstance::$prop in %s on line %d
NULL
Expand Down
1 change: 0 additions & 1 deletion Zend/tests/lazy_objects/gh18038-007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ var_dump(isset($real->prop['']));
--EXPECT--
init
string(21) "RealInstance::__isset"
string(14) "Proxy::__isset"
bool(false)
bool(false)
1 change: 0 additions & 1 deletion Zend/tests/lazy_objects/gh18038-009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ var_dump(isset($real->prop));
--EXPECT--
init
string(21) "RealInstance::__isset"
string(14) "Proxy::__isset"
bool(false)
bool(false)
8 changes: 0 additions & 8 deletions Zend/tests/lazy_objects/gh20875.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ Warning: Undefined variable $a in %s on line %d

Warning: Undefined variable $v in %s on line %d

Notice: Indirect modification of overloaded property A::$b has no effect in %s on line %d

Warning: Undefined variable $x in %s on line %d

Notice: Object of class stdClass could not be converted to int in %s on line %d

Warning: Undefined variable $v in %s on line %d

Notice: Indirect modification of overloaded property A::$f has no effect in %s on line %d

Fatal error: Uncaught Error: Cannot assign by reference to overloaded object in %s:%d
Expand Down
30 changes: 30 additions & 0 deletions Zend/tests/lazy_objects/gh21478-isset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-21478: __isset on lazy proxy should not double-invoke when real instance guard is set
--FILE--
<?php

class Foo {
public $_;

public function __isset($name) {
global $proxy;
printf("__isset(\$%s) on %s\n", $name, $this::class);
return isset($proxy->{$name});
}
}

class Bar extends Foo {}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
echo "Init\n";
return new Foo();
});

$real = $rc->initializeLazyObject($proxy);
isset($real->x);

?>
--EXPECT--
Init
__isset($x) on Foo
30 changes: 30 additions & 0 deletions Zend/tests/lazy_objects/gh21478-proxy-get-override.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-21478: Proxy's own __get runs when accessed directly (not from real instance)
--FILE--
<?php

class Foo {
private $_;

public function __get($name) {
echo __CLASS__, " ", $name, "\n";
}
}

class Bar extends Foo {
public function __get($name) {
echo __CLASS__, " ", $name, "\n";
}
}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
return new Foo();
});
$rc->initializeLazyObject($proxy);

$proxy->x;

?>
--EXPECT--
Bar x
32 changes: 32 additions & 0 deletions Zend/tests/lazy_objects/gh21478-proxy-get-ref-forward.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-21478: No assertion failure when &__get forwards through initialized lazy proxy
--FILE--
<?php
class Foo {
public $_;

public function &__get($name) {
global $proxy;
printf("%s(\$%s) on %s\n", __METHOD__, $name, $this::class);
return $proxy->{$name};
}
}

class Bar extends Foo {}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
echo "Init\n";
return new Foo();
});

$real = $rc->initializeLazyObject($proxy);
$a = &$real->x;
var_dump($a);
?>
--EXPECTF--
Init
Foo::__get($x) on Foo

Warning: Undefined property: Foo::$x in %s on line %d
NULL
32 changes: 32 additions & 0 deletions Zend/tests/lazy_objects/gh21478-set.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-21478: __set on lazy proxy should not double-invoke when real instance guard is set
--FILE--
<?php

#[AllowDynamicProperties]
class Foo {
public $_;

public function __set($name, $value) {
global $proxy;
printf("__set(\$%s) on %s\n", $name, $this::class);
$proxy->{$name} = $value;
}
}

#[AllowDynamicProperties]
class Bar extends Foo {}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
echo "Init\n";
return new Foo();
});

$real = $rc->initializeLazyObject($proxy);
$real->x = 1;

?>
--EXPECT--
Init
__set($x) on Foo
30 changes: 30 additions & 0 deletions Zend/tests/lazy_objects/gh21478-unset.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-21478: __unset on lazy proxy should not double-invoke when real instance guard is set
--FILE--
<?php

class Foo {
public $_;

public function __unset($name) {
global $proxy;
printf("__unset(\$%s) on %s\n", $name, $this::class);
unset($proxy->{$name});
}
}

class Bar extends Foo {}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
echo "Init\n";
return new Foo();
});

$real = $rc->initializeLazyObject($proxy);
unset($real->x);

?>
--EXPECT--
Init
__unset($x) on Foo
32 changes: 32 additions & 0 deletions Zend/tests/lazy_objects/gh21478.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-21478 (Property access on lazy proxy may invoke magic method despite real instance guards)
--FILE--
<?php

class Foo {
public $_;

public function __get($name) {
global $proxy;
printf("__get(\$%s) on %s\n", $name, $this::class);
return $proxy->{$name};
}
}

class Bar extends Foo {}

$rc = new ReflectionClass(Bar::class);
$proxy = $rc->newLazyProxy(function () {
echo "Init\n";
return new Foo();
});

$real = $rc->initializeLazyObject($proxy);
$real->x;

?>
--EXPECTF--
Init
__get($x) on Foo

Warning: Undefined property: Foo::$x in %s on line %d
2 changes: 1 addition & 1 deletion Zend/zend_atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ZEND_API void zend_atomic_int_store(zend_atomic_int *obj, int desired) {
zend_atomic_int_store_ex(obj, desired);
}

#if defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)
#if (defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)) && !defined(HAVE_C11_ATOMICS)
/* On these platforms it is non-const due to underlying APIs. */
ZEND_API bool zend_atomic_bool_load(zend_atomic_bool *obj) {
return zend_atomic_bool_load_ex(obj);
Expand Down
8 changes: 4 additions & 4 deletions Zend/zend_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* and alignment purposes.
*/

#if defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)
#if (defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)) && !defined(HAVE_C11_ATOMICS)
typedef struct zend_atomic_bool_s {
volatile char value;
} zend_atomic_bool;
Expand Down Expand Up @@ -68,7 +68,7 @@ typedef struct zend_atomic_int_s {

BEGIN_EXTERN_C()

#ifdef ZEND_WIN32
#if defined(ZEND_WIN32) && !defined(HAVE_C11_ATOMICS)

#ifndef InterlockedExchange8
#define InterlockedExchange8 _InterlockedExchange8
Expand Down Expand Up @@ -123,7 +123,7 @@ static zend_always_inline bool zend_atomic_int_compare_exchange_ex(zend_atomic_i
}
}

/* On this platform it is non-const due to Iterlocked API*/
/* On this platform it is non-const due to Interlocked API */
static zend_always_inline bool zend_atomic_bool_load_ex(zend_atomic_bool *obj) {
/* Or'ing with false won't change the value. */
return InterlockedOr8(&obj->value, false);
Expand Down Expand Up @@ -376,7 +376,7 @@ ZEND_API bool zend_atomic_int_compare_exchange(zend_atomic_int *obj, int *expect
ZEND_API void zend_atomic_bool_store(zend_atomic_bool *obj, bool desired);
ZEND_API void zend_atomic_int_store(zend_atomic_int *obj, int desired);

#if defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)
#if (defined(ZEND_WIN32) && !defined(HAVE_C11_ATOMICS)) || defined(HAVE_SYNC_ATOMICS)
/* On these platforms it is non-const due to underlying APIs. */
ZEND_API bool zend_atomic_bool_load(zend_atomic_bool *obj);
ZEND_API int zend_atomic_int_load(zend_atomic_int *obj);
Expand Down
65 changes: 65 additions & 0 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,28 @@ ZEND_API zval *zend_std_read_property(zend_object *zobj, zend_string *name, int

retval = &EG(uninitialized_zval);

/* For initialized lazy proxies: if the real instance's magic method
* guard is already set for this property, we are inside a recursive
* call from the real instance's __get/__isset. Forward directly to
* the real instance to avoid double invocation. (GH-21478) */
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
uint32_t *instance_guard = zend_get_property_guard(instance, name);
uint32_t guard_type = ((type == BP_VAR_IS) && zobj->ce->__isset)
? IN_ISSET : IN_GET;
if ((*instance_guard) & guard_type) {
retval = zend_std_read_property(instance, name, type, cache_slot, rv);
if (retval == &EG(uninitialized_zval)) {
ZVAL_NULL(rv);
retval = rv;
}
return retval;
}
}
}

/* magic isset */
if ((type == BP_VAR_IS) && zobj->ce->__isset) {
zval tmp_result;
Expand Down Expand Up @@ -1209,6 +1231,20 @@ found:;
goto exit;
}

/* For initialized lazy proxies: if the real instance's __set guard
* is already set, we are inside a recursive call from the real
* instance's __set. Forward directly to avoid double invocation. */
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
uint32_t *instance_guard = zend_get_property_guard(instance, name);
if ((*instance_guard) & IN_SET) {
return zend_std_write_property(instance, name, value, cache_slot);
}
}
}

/* magic set */
if (zobj->ce->__set) {
if (!guard) {
Expand Down Expand Up @@ -1603,6 +1639,21 @@ ZEND_API void zend_std_unset_property(zend_object *zobj, zend_string *name, void
return;
}

/* For initialized lazy proxies: if the real instance's __unset guard
* is already set, we are inside a recursive call from the real
* instance's __unset. Forward directly to avoid double invocation. */
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
uint32_t *instance_guard = zend_get_property_guard(instance, name);
if ((*instance_guard) & IN_UNSET) {
zend_std_unset_property(instance, name, cache_slot);
return;
}
}
}

/* magic unset */
if (zobj->ce->__unset) {
if (!guard) {
Expand Down Expand Up @@ -2399,6 +2450,20 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
goto exit;
}

/* For initialized lazy proxies: if the real instance's __isset guard
* is already set, we are inside a recursive call from the real
* instance's __isset. Forward directly to avoid double invocation. */
if (UNEXPECTED(zend_object_is_lazy_proxy(zobj)
&& zend_lazy_object_initialized(zobj))) {
zend_object *instance = zend_lazy_object_get_instance(zobj);
if (instance->ce->ce_flags & ZEND_ACC_USE_GUARDS) {
uint32_t *instance_guard = zend_get_property_guard(instance, name);
if ((*instance_guard) & IN_ISSET) {
return zend_std_has_property(instance, name, has_set_exists, cache_slot);
}
}
}

if (!zobj->ce->__isset) {
goto lazy_init;
}
Expand Down