Skip to content

Extend map_ptr before copying class table #9188

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

Merged
merged 1 commit into from
Aug 1, 2022
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: 1 addition & 0 deletions Zend/zend_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {

#define ZSTR_SET_CE_CACHE_EX(s, ce, validate) do { \
if (!(validate) || ZSTR_VALID_CE_CACHE(s)) { \
ZEND_ASSERT((validate) || ZSTR_VALID_CE_CACHE(s)); \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand what do you do with this assertion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's asserting that the ce cache is valid, even if validation is skipped.

SET_CE_CACHE(GC_REFCOUNT(s), ce); \
} \
} while (0)
Expand Down
46 changes: 46 additions & 0 deletions ext/opcache/tests/gh9164.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Bug GH-9164: Segfault in zend_accel_class_hash_copy
--EXTENSIONS--
opcache
pcntl
--INI--
opcache.enable_cli=1
--FILE--
<?php

$incfile = __DIR__.'/gh9164.inc';

// Generate enough classes so that the out of bound access will cause a crash
// without relying on assertion
$fd = fopen($incfile, 'w');
fwrite($fd, "<?php\n");
for ($i = 0; $i < 4096; $i++) {
fprintf($fd, "class FooBar%04d {}\n", $i);
}
fclose($fd);

// Ensure cacheability
touch($incfile, time() - 3600);

$pid = pcntl_fork();
if ($pid == 0) {
// Child: Declare classes to allocate CE cache slots.
require $incfile;
} else if ($pid > 0) {
pcntl_wait($status);
// Ensure that file has been cached. If not, this is testing nothing anymore.
if (!isset(opcache_get_status()['scripts'][$incfile])) {
print "File not cached\n";
}
// Populates local cache
require $incfile;
var_dump(new FooBar4095);
unlink($incfile);
} else {
echo "pcntl_fork() failed\n";
}

?>
--EXPECTF--
object(FooBar4095)#%d (0) {
}
22 changes: 11 additions & 11 deletions ext/opcache/zend_accelerator_util_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,11 @@ zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script,
op_array = (zend_op_array *) emalloc(sizeof(zend_op_array));
*op_array = persistent_script->script.main_op_array;

if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
}

if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
}

if (EXPECTED(from_shared_memory)) {
if (ZCSG(map_ptr_last) > CG(map_ptr_last)) {
zend_map_ptr_extend(ZCSG(map_ptr_last));
}
Comment on lines 350 to +353
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just moving the if (EXPECTED(from_shared_memory)) { block to the beginning of the function, and the if (ZCSG(map_ptr_last) > CG(map_ptr_last)) { block at the beginning of the block.


/* Register __COMPILER_HALT_OFFSET__ constant */
if (persistent_script->compiler_halt_offset != 0 &&
persistent_script->script.filename) {
Expand All @@ -368,10 +364,14 @@ zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script,
}
zend_string_release_ex(name, 0);
}
}

if (ZCSG(map_ptr_last) > CG(map_ptr_last)) {
zend_map_ptr_extend(ZCSG(map_ptr_last));
}
if (zend_hash_num_elements(&persistent_script->script.function_table) > 0) {
zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
}

if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
}

if (persistent_script->num_early_bindings) {
Expand Down