Skip to content

Commit 2f6a06c

Browse files
beberleicmb69
authored andcommitted
Fix #81430: Attribute instantiation leaves dangling pointer
By switching attribute constructor stackframe to be called via trampoline the stack allocation is not causing dangling pointers in the zend_observer API anymore. Co-Authored-By: Florian Sowade <f.sowade@suora.com> Co-Authored-By: Christopher Becker <cmbecker69@gmx.de> Co-Authored-By: Dmitry Stogov <dmitry@zend.com> Closes GH-7885.
1 parent c99a026 commit 2f6a06c

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2022, PHP 8.0.16
44

5+
- Core:
6+
. Fixed bug #81430 (Attribute instantiation leaves dangling pointer).
7+
(beberlei)
8+
59
- FPM:
610
. Fixed memory leak on invalid port. (David Carlier)
711

ext/reflection/php_reflection.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6315,6 +6315,7 @@ static int call_attribute_constructor(
63156315
dummy_func.type = ZEND_USER_FUNCTION;
63166316
dummy_func.common.fn_flags =
63176317
attr->flags & ZEND_ATTRIBUTE_STRICT_TYPES ? ZEND_ACC_STRICT_TYPES : 0;
6318+
dummy_func.common.fn_flags |= ZEND_ACC_CALL_VIA_TRAMPOLINE;
63186319
dummy_func.op_array.filename = filename;
63196320

63206321
dummy_opline.opcode = ZEND_DO_FCALL;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Bug #81430 (Attribute instantiation frame accessing invalid frame pointer)
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
memory_limit=20M
7+
zend_test.observer.enabled=1
8+
zend_test.observer.observe_all=1
9+
--FILE--
10+
<?php
11+
12+
#[\Attribute]
13+
class A {
14+
private $a;
15+
public function __construct() {
16+
}
17+
}
18+
19+
#[A]
20+
function B() {}
21+
22+
$r = new \ReflectionFunction("B");
23+
call_user_func([$r->getAttributes(A::class)[0], 'newInstance']);
24+
?>
25+
--EXPECTF--
26+
<!-- init '%s' -->
27+
<file '%s'>
28+
<!-- init A::__construct() -->
29+
<A::__construct>
30+
</A::__construct>
31+
</file '%s'>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Bug #81430 (Attribute instantiation leaves dangling execute_data pointer)
3+
--EXTENSIONS--
4+
zend_test
5+
--INI--
6+
memory_limit=20M
7+
zend_test.observer.enabled=1
8+
zend_test.observer.observe_all=1
9+
--FILE--
10+
<?php
11+
12+
#[\Attribute]
13+
class A {
14+
public function __construct() {
15+
array_map("str_repeat", ["\xFF"], [100000000]); // cause a bailout
16+
}
17+
}
18+
19+
#[A]
20+
function B() {}
21+
22+
$r = new \ReflectionFunction("B");
23+
call_user_func([$r->getAttributes(A::class)[0], 'newInstance']);
24+
?>
25+
--EXPECTF--
26+
<!-- init '%s' -->
27+
<file '%s'>
28+
<!-- init A::__construct() -->
29+
<A::__construct>
30+
31+
Fatal error: Allowed memory size of %d bytes exhausted %s in %s on line %d
32+
</A::__construct>
33+
</file '%s'>

0 commit comments

Comments
 (0)