Skip to content

Commit be89a5c

Browse files
committed
Fixed bug #78950: Preloading trait method with static variables
We need to make sure that trait methods with static variables allocate a separate MAP slot for the static variables pointer, rather than working in-place.
1 parent 2d03b63 commit be89a5c

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ PHP NEWS
33

44
?? ??? ????, PHP 7.4.2
55

6+
- OPcache:
7+
. Fixed bug #78950 (Preloading trait method with static variables). (Nikita)
68

79
19 Dec 2019, PHP 7.4.1
810

Zend/zend_compile.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,16 @@ ZEND_API void function_add_ref(zend_function *function) /* {{{ */
10051005
GC_ADDREF(op_array->static_variables);
10061006
}
10071007
}
1008-
ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &op_array->static_variables);
1009-
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
1010-
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
1008+
1009+
if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
1010+
ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_PRELOADED);
1011+
ZEND_MAP_PTR_NEW(op_array->run_time_cache);
1012+
ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
1013+
} else {
1014+
ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &op_array->static_variables);
1015+
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
1016+
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
1017+
}
10111018
} else if (function->type == ZEND_INTERNAL_FUNCTION) {
10121019
if (function->common.function_name) {
10131020
zend_string_addref(function->common.function_name);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
trait Foo {
4+
public function test() {
5+
static $bar;
6+
var_dump($bar);
7+
}
8+
}
9+
10+
class Bar {
11+
use Foo;
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Preload trait with static variables in method
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.optimization_level=-1
7+
opcache.preload={PWD}/preload_trait_static.inc
8+
--SKIPIF--
9+
<?php require_once('skipif.inc'); ?>
10+
--FILE--
11+
<?php
12+
$bar = new Bar;
13+
$bar->test();
14+
?>
15+
--EXPECT--
16+
NULL

0 commit comments

Comments
 (0)