Skip to content

Commit 9fd27bf

Browse files
committed
Added ZEND_ASSIGN_OBJ fast path for the first write to an uninitialized typed property
1 parent ef99f42 commit 9fd27bf

5 files changed

Lines changed: 814 additions & 0 deletions

File tree

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
--TEST--
2+
First write to an uninitialized typed property (ZEND_ASSIGN_OBJ fast path)
3+
--FILE--
4+
<?php
5+
6+
class Foo {}
7+
class Bar extends Foo {}
8+
9+
class Plain {
10+
public int $i;
11+
public string $s;
12+
public ?Foo $foo;
13+
public int|string $u;
14+
public array $a;
15+
public float $f;
16+
public mixed $m;
17+
public ?int $n;
18+
}
19+
20+
/* Each helper is called repeatedly so that the cache slot is primed and the
21+
* fast path is taken from the second call on. */
22+
function set_i(Plain $o, $v) { $o->i = $v; }
23+
function set_s(Plain $o, $v) { $o->s = $v; }
24+
function set_foo(Plain $o, $v) { $o->foo = $v; }
25+
function set_u(Plain $o, $v) { $o->u = $v; }
26+
function set_a(Plain $o, $v) { $o->a = $v; }
27+
function set_f(Plain $o, $v) { $o->f = $v; }
28+
function ret(Plain $o, $v) { return $o->i = $v; }
29+
function chain(Plain $o, $v) { return $o->s = $o->i = $v; }
30+
31+
function attempt(callable $f, ...$args) {
32+
try {
33+
$r = $f(...$args);
34+
var_dump($r);
35+
} catch (Throwable $e) {
36+
echo get_class($e), ": ", $e->getMessage(), "\n";
37+
}
38+
}
39+
40+
echo "--- coercion, weak mode\n";
41+
for ($k = 0; $k < 3; $k++) {
42+
$o = new Plain; attempt('set_i', $o, "2"); var_dump($o->i);
43+
$o = new Plain; attempt('set_i', $o, 2.0); var_dump($o->i);
44+
$o = new Plain; attempt('set_i', $o, true); var_dump($o->i);
45+
$o = new Plain; attempt('set_i', $o, "abc"); var_dump(isset($o->i));
46+
$o = new Plain; attempt('set_i', $o, null); var_dump(isset($o->i));
47+
$o = new Plain; attempt('set_s', $o, 12); var_dump($o->s);
48+
}
49+
50+
echo "--- union, class and nullable types\n";
51+
for ($k = 0; $k < 3; $k++) {
52+
$o = new Plain; attempt('set_u', $o, true); var_dump($o->u);
53+
$o = new Plain; attempt('set_u', $o, []); var_dump(isset($o->u));
54+
$o = new Plain; attempt('set_foo', $o, new Bar); var_dump($o->foo instanceof Bar);
55+
$o = new Plain; attempt('set_foo', $o, null); var_dump($o->foo);
56+
$o = new Plain; attempt('set_foo', $o, new stdClass); var_dump(isset($o->foo));
57+
$o = new Plain; attempt('set_f', $o, 3); var_dump($o->f);
58+
}
59+
60+
echo "--- result value\n";
61+
for ($k = 0; $k < 3; $k++) {
62+
$o = new Plain; attempt('ret', $o, "5");
63+
$o = new Plain; attempt('ret', $o, "x");
64+
$o = new Plain; attempt('chain', $o, "7"); var_dump($o->s, $o->i);
65+
}
66+
67+
echo "--- reference and refcounted values\n";
68+
for ($k = 0; $k < 3; $k++) {
69+
$o = new Plain;
70+
$str = str_repeat("a", 3) . $k;
71+
$arr = [$k, [$k]];
72+
$ref = &$arr;
73+
set_s($o, $str);
74+
set_a($o, $ref);
75+
$arr[] = 1;
76+
var_dump($o->s === $str, count($o->a), count($arr));
77+
unset($ref, $arr, $str);
78+
var_dump($o->s, $o->a);
79+
}
80+
81+
echo "--- strict_types is taken from the calling file\n";
82+
eval('declare(strict_types=1); function set_i_strict(Plain $o, $v) { $o->i = $v; }');
83+
for ($k = 0; $k < 3; $k++) {
84+
$o = new Plain; attempt('set_i_strict', $o, "2"); var_dump(isset($o->i));
85+
$o = new Plain; attempt('set_i_strict', $o, 2); var_dump($o->i);
86+
}
87+
88+
echo "--- unset() property goes through __set()\n";
89+
class Magic {
90+
public int $x;
91+
public function __set($n, $v) { echo "__set($n)\n"; }
92+
public function drop() { unset($this->x); }
93+
}
94+
function set_x(Magic $o, $v) { $o->x = $v; }
95+
for ($k = 0; $k < 3; $k++) {
96+
$o = new Magic; set_x($o, 1); var_dump($o->x);
97+
$o = new Magic; $o->drop(); set_x($o, 2); var_dump(isset($o->x));
98+
}
99+
100+
echo "--- object released while coercing the value\n";
101+
class Releaser {
102+
public function __toString(): string { unset($GLOBALS['victim']); return "5"; }
103+
}
104+
class Victim { public string $s; public function set($v) { $this->s = $v; } }
105+
for ($k = 0; $k < 3; $k++) {
106+
$victim = new Victim;
107+
try {
108+
$victim->s = new Releaser;
109+
echo "assigned\n";
110+
} catch (Error $e) {
111+
echo $e->getMessage(), "\n";
112+
}
113+
}
114+
for ($k = 0; $k < 3; $k++) {
115+
$victim = new Victim;
116+
try {
117+
$victim->set(new Releaser);
118+
echo "assigned\n";
119+
} catch (Error $e) {
120+
echo $e->getMessage(), "\n";
121+
}
122+
}
123+
124+
?>
125+
--EXPECT--
126+
--- coercion, weak mode
127+
NULL
128+
int(2)
129+
NULL
130+
int(2)
131+
NULL
132+
int(1)
133+
TypeError: Cannot assign string to property Plain::$i of type int
134+
bool(false)
135+
TypeError: Cannot assign null to property Plain::$i of type int
136+
bool(false)
137+
NULL
138+
string(2) "12"
139+
NULL
140+
int(2)
141+
NULL
142+
int(2)
143+
NULL
144+
int(1)
145+
TypeError: Cannot assign string to property Plain::$i of type int
146+
bool(false)
147+
TypeError: Cannot assign null to property Plain::$i of type int
148+
bool(false)
149+
NULL
150+
string(2) "12"
151+
NULL
152+
int(2)
153+
NULL
154+
int(2)
155+
NULL
156+
int(1)
157+
TypeError: Cannot assign string to property Plain::$i of type int
158+
bool(false)
159+
TypeError: Cannot assign null to property Plain::$i of type int
160+
bool(false)
161+
NULL
162+
string(2) "12"
163+
--- union, class and nullable types
164+
NULL
165+
int(1)
166+
TypeError: Cannot assign array to property Plain::$u of type string|int
167+
bool(false)
168+
NULL
169+
bool(true)
170+
NULL
171+
NULL
172+
TypeError: Cannot assign stdClass to property Plain::$foo of type ?Foo
173+
bool(false)
174+
NULL
175+
float(3)
176+
NULL
177+
int(1)
178+
TypeError: Cannot assign array to property Plain::$u of type string|int
179+
bool(false)
180+
NULL
181+
bool(true)
182+
NULL
183+
NULL
184+
TypeError: Cannot assign stdClass to property Plain::$foo of type ?Foo
185+
bool(false)
186+
NULL
187+
float(3)
188+
NULL
189+
int(1)
190+
TypeError: Cannot assign array to property Plain::$u of type string|int
191+
bool(false)
192+
NULL
193+
bool(true)
194+
NULL
195+
NULL
196+
TypeError: Cannot assign stdClass to property Plain::$foo of type ?Foo
197+
bool(false)
198+
NULL
199+
float(3)
200+
--- result value
201+
int(5)
202+
TypeError: Cannot assign string to property Plain::$i of type int
203+
string(1) "7"
204+
string(1) "7"
205+
int(7)
206+
int(5)
207+
TypeError: Cannot assign string to property Plain::$i of type int
208+
string(1) "7"
209+
string(1) "7"
210+
int(7)
211+
int(5)
212+
TypeError: Cannot assign string to property Plain::$i of type int
213+
string(1) "7"
214+
string(1) "7"
215+
int(7)
216+
--- reference and refcounted values
217+
bool(true)
218+
int(2)
219+
int(3)
220+
string(4) "aaa0"
221+
array(2) {
222+
[0]=>
223+
int(0)
224+
[1]=>
225+
array(1) {
226+
[0]=>
227+
int(0)
228+
}
229+
}
230+
bool(true)
231+
int(2)
232+
int(3)
233+
string(4) "aaa1"
234+
array(2) {
235+
[0]=>
236+
int(1)
237+
[1]=>
238+
array(1) {
239+
[0]=>
240+
int(1)
241+
}
242+
}
243+
bool(true)
244+
int(2)
245+
int(3)
246+
string(4) "aaa2"
247+
array(2) {
248+
[0]=>
249+
int(2)
250+
[1]=>
251+
array(1) {
252+
[0]=>
253+
int(2)
254+
}
255+
}
256+
--- strict_types is taken from the calling file
257+
TypeError: Cannot assign string to property Plain::$i of type int
258+
bool(false)
259+
NULL
260+
int(2)
261+
TypeError: Cannot assign string to property Plain::$i of type int
262+
bool(false)
263+
NULL
264+
int(2)
265+
TypeError: Cannot assign string to property Plain::$i of type int
266+
bool(false)
267+
NULL
268+
int(2)
269+
--- unset() property goes through __set()
270+
int(1)
271+
__set(x)
272+
bool(false)
273+
int(1)
274+
__set(x)
275+
bool(false)
276+
int(1)
277+
__set(x)
278+
bool(false)
279+
--- object released while coercing the value
280+
Object was released while assigning to property Victim::$s
281+
Object was released while assigning to property Victim::$s
282+
Object was released while assigning to property Victim::$s
283+
assigned
284+
assigned
285+
assigned

0 commit comments

Comments
 (0)