Skip to content

Fix GH-18534: FPM exit code 70 with enabled opcache and hooked properties in traits #18586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion Zend/Optimizer/zend_optimizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ void zend_foreach_op_array(zend_script *script, zend_op_array_func_t func, void
if (property->ce == ce && property->hooks) {
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
zend_function *hook = hooks[i];
if (hook && hook->common.scope == ce) {
if (hook && hook->common.scope == ce && !(hooks[i]->op_array.fn_flags & ZEND_ACC_TRAIT_CLONE)) {
zend_foreach_op_array_helper(&hooks[i]->op_array, func, context);
}
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,7 @@ static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_ent
}
}
ce->ce_flags |= ZEND_ACC_USE_GUARDS;
ce->num_hooked_props++;
}
} ZEND_HASH_FOREACH_END();
}
Expand Down
71 changes: 55 additions & 16 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -4202,36 +4202,75 @@ static void preload_remove_empty_includes(void)

static void preload_register_trait_methods(zend_class_entry *ce) {
zend_op_array *op_array;
zend_property_info *info;

ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
if (!(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
ZEND_ASSERT(op_array->refcount && "Must have refcount pointer");
zend_shared_alloc_register_xlat_entry(op_array->refcount, op_array);
}
} ZEND_HASH_FOREACH_END();

if (ce->num_hooked_props > 0) {
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, info) {
if (info->hooks) {
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
if (info->hooks[i]) {
op_array = &info->hooks[i]->op_array;
if (!(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
ZEND_ASSERT(op_array->refcount && "Must have refcount pointer");
zend_shared_alloc_register_xlat_entry(op_array->refcount, op_array);
}
}
}
}
} ZEND_HASH_FOREACH_END();
}
}

static void preload_fix_trait_op_array(zend_op_array *op_array)
{
if (!(op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
return;
}

zend_op_array *orig_op_array = zend_shared_alloc_get_xlat_entry(op_array->refcount);
ZEND_ASSERT(orig_op_array && "Must be in xlat table");

zend_string *function_name = op_array->function_name;
zend_class_entry *scope = op_array->scope;
uint32_t fn_flags = op_array->fn_flags;
zend_function *prototype = op_array->prototype;
HashTable *ht = op_array->static_variables;
*op_array = *orig_op_array;
op_array->function_name = function_name;
op_array->scope = scope;
op_array->fn_flags = fn_flags;
op_array->prototype = prototype;
op_array->static_variables = ht;
}

static void preload_fix_trait_methods(zend_class_entry *ce)
{
zend_op_array *op_array;

ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
if (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE) {
zend_op_array *orig_op_array = zend_shared_alloc_get_xlat_entry(op_array->refcount);
ZEND_ASSERT(orig_op_array && "Must be in xlat table");

zend_string *function_name = op_array->function_name;
zend_class_entry *scope = op_array->scope;
uint32_t fn_flags = op_array->fn_flags;
zend_function *prototype = op_array->prototype;
HashTable *ht = op_array->static_variables;
*op_array = *orig_op_array;
op_array->function_name = function_name;
op_array->scope = scope;
op_array->fn_flags = fn_flags;
op_array->prototype = prototype;
op_array->static_variables = ht;
}
preload_fix_trait_op_array(op_array);
} ZEND_HASH_FOREACH_END();

if (ce->num_hooked_props > 0) {
zend_property_info *info;
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, info) {
if (info->hooks) {
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
if (info->hooks[i]) {
op_array = &info->hooks[i]->op_array;
preload_fix_trait_op_array(op_array);
}
}
}
} ZEND_HASH_FOREACH_END();
}
}

static void preload_optimize(zend_persistent_script *script)
Expand Down
21 changes: 21 additions & 0 deletions ext/opcache/tests/gh18534.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
GH-18534 (FPM exit code 70 with enabled opcache and hooked properties in traits)
--EXTENSIONS--
opcache
--SKIPIF--
<?php if (PHP_OS_FAMILY === 'Windows') die('skip preloading does not work on Windows'); ?>
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.preload={PWD}/gh18534_preload.inc
--FILE--
<?php

require_once __DIR__ . '/gh18534_preload.inc';

$test = new DummyModel;
var_dump($test->dummyProperty2);

?>
--EXPECT--
NULL
13 changes: 13 additions & 0 deletions ext/opcache/tests/gh18534_preload.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

trait DummyTrait
{
public ?string $dummyProperty2 {
get => null;
}
}

class DummyModel
{
use DummyTrait;
}