Skip to content
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

opcache: Do not disable userland error handlers in opcache_compile_file() #17627

Open
wants to merge 2 commits into
base: PHP-8.3
Choose a base branch
from
Open
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
4 changes: 0 additions & 4 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,6 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl
zend_persistent_script *new_persistent_script;
uint32_t orig_functions_count, orig_class_count;
zend_op_array *orig_active_op_array;
zval orig_user_error_handler;
zend_op_array *op_array;
bool do_bailout = false;
accel_time_t timestamp = 0;
Expand Down Expand Up @@ -1796,10 +1795,8 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl
orig_active_op_array = CG(active_op_array);
orig_functions_count = EG(function_table)->nNumUsed;
orig_class_count = EG(class_table)->nNumUsed;
ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));

/* Override them with ours */
ZVAL_UNDEF(&EG(user_error_handler));
if (ZCG(accel_directives).record_warnings) {
zend_begin_record_errors();
}
Expand All @@ -1825,7 +1822,6 @@ static zend_persistent_script *opcache_compile_file(zend_file_handle *file_handl

/* Restore originals */
CG(active_op_array) = orig_active_op_array;
EG(user_error_handler) = orig_user_error_handler;
EG(record_errors) = 0;

if (!op_array) {
Expand Down
32 changes: 32 additions & 0 deletions ext/opcache/tests/gh17422/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-17422 (OPcache bypasses the user-defined error handler for deprecations)
--INI--
opcache.enable=1
opcache.enable_cli=1
--EXTENSIONS--
opcache
--FILE--
<?php

require __DIR__ . "/shutdown.inc";

set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
echo "set_error_handler: {$errstr}", PHP_EOL;
});

require __DIR__ . "/warning.inc";

warning();

?>
--EXPECT--
set_error_handler: "continue" targeting switch is equivalent to "break"
OK: warning
array(3) {
[0]=>
string(7) "001.php"
[1]=>
string(12) "shutdown.inc"
[2]=>
string(11) "warning.inc"
}
36 changes: 36 additions & 0 deletions ext/opcache/tests/gh17422/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-17422 (OPcache bypasses the user-defined error handler for deprecations) - Throwing error handler
--INI--
opcache.enable=1
opcache.enable_cli=1
--EXTENSIONS--
opcache
--FILE--
<?php

require __DIR__ . "/shutdown.inc";

set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});

try {
require __DIR__ . "/warning.inc";
} catch (\Exception $e) {
echo "Caught: ", $e->getMessage(), PHP_EOL;
}

warning();

?>
--EXPECT--
Caught: "continue" targeting switch is equivalent to "break"
OK: warning
array(3) {
[0]=>
string(7) "002.php"
[1]=>
string(12) "shutdown.inc"
[2]=>
string(11) "warning.inc"
}
30 changes: 30 additions & 0 deletions ext/opcache/tests/gh17422/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-17422 (OPcache bypasses the user-defined error handler for deprecations) - Fatal Error
--INI--
opcache.enable=1
opcache.enable_cli=1
memory_limit=2M
--EXTENSIONS--
opcache
--FILE--
<?php

require __DIR__ . "/shutdown.inc";

set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
str_repeat('x', 1024 * 1024 * 1024);
});

require __DIR__ . "/warning.inc";

warning();

?>
--EXPECTF--
Fatal error: Allowed memory size of 2097152 bytes exhausted %s on line 6
array(2) {
[0]=>
string(7) "003.php"
[1]=>
string(12) "shutdown.inc"
}
36 changes: 36 additions & 0 deletions ext/opcache/tests/gh17422/004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
GH-17422 (OPcache bypasses the user-defined error handler for deprecations) - eval
--INI--
opcache.enable=1
opcache.enable_cli=1
memory_limit=2M
--EXTENSIONS--
opcache
--FILE--
<?php

require __DIR__ . "/shutdown.inc";

set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
eval(
<<<'PHP'
function warning() {
echo "NOK", PHP_EOL;
}
PHP
);
});

require __DIR__ . "/warning.inc";

warning();

?>
--EXPECTF--
Fatal error: Cannot redeclare %Swarning() (previously declared in %s(8) : eval()'d code:1) in %swarning.inc on line 2
array(2) {
[0]=>
string(7) "004.php"
[1]=>
string(12) "shutdown.inc"
}
34 changes: 34 additions & 0 deletions ext/opcache/tests/gh17422/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
GH-17422 (OPcache bypasses the user-defined error handler for deprecations) - require
--INI--
opcache.enable=1
opcache.enable_cli=1
memory_limit=2M
--EXTENSIONS--
opcache
--FILE--
<?php

require __DIR__ . "/shutdown.inc";

set_error_handler(static function (int $errno, string $errstr, string $errfile, int $errline) {
require_once __DIR__ . "/dummy.inc";
});

require __DIR__ . "/warning.inc";

dummy();

?>
--EXPECT--
OK: dummy
array(4) {
[0]=>
string(7) "005.php"
[1]=>
string(9) "dummy.inc"
[2]=>
string(12) "shutdown.inc"
[3]=>
string(11) "warning.inc"
}
4 changes: 4 additions & 0 deletions ext/opcache/tests/gh17422/dummy.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
function dummy() {
echo "OK: ", __FUNCTION__, PHP_EOL;
}
6 changes: 6 additions & 0 deletions ext/opcache/tests/gh17422/shutdown.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
register_shutdown_function(static function() {
$scripts = array_map(basename(...), array_keys(opcache_get_status()['scripts'] ?? []));
sort($scripts);
var_dump($scripts);
});
8 changes: 8 additions & 0 deletions ext/opcache/tests/gh17422/warning.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
function warning() {
switch (1) {
case 1:
echo "OK: ", __FUNCTION__, PHP_EOL;
continue;
}
}
Loading