Skip to content

Commit 47c23e5

Browse files
committed
Fix GH-24361: Restore GC after PHP opcache_compile_file errors
Record fatal error origin before bailout and restore GC only for direct compiler failures, preserving runtime fatal protection Cover manual and automatic collection, error handlers and fatal callbacks
1 parent 7c0b900 commit 47c23e5

10 files changed

Lines changed: 236 additions & 0 deletions

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ PHP NEWS
1515
is exhausted while compiling side traces). (Ilia Alshanetsky)
1616
. Fixed bug GH-25164 (Function JIT loop calling a user function runs once
1717
or never terminates). (Ilia Alshanetsky)
18+
. Fixed bug GH-24361 (opcache_compile_file() disables cycle collection after
19+
a compile error). (akasakariko)
1820

1921
19 Nov 2026, PHP 8.4.28
2022

‎ext/opcache/ZendAccelerator.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,8 @@ static int accel_startup(zend_extension *extension)
32413241
/* Prevent unloading */
32423242
extension->handle = 0;
32433243

3244+
zend_observer_error_register(zend_accel_record_fatal_error);
3245+
32443246
return SUCCESS;
32453247
}
32463248

‎ext/opcache/tests/gh24361.inc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
break 2;

‎ext/opcache/tests/gh24361.phpt‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
GH-24361 (opcache_compile_file() disables cycle collection after a compile error)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.file_update_protection=0
9+
--FILE--
10+
<?php
11+
var_dump(gc_status()['protected']);
12+
var_dump(@opcache_compile_file(__DIR__ . '/gh24361.inc'));
13+
var_dump(gc_status()['protected']);
14+
15+
$node = new stdClass;
16+
$node->self = $node;
17+
unset($node);
18+
var_dump(gc_collect_cycles());
19+
20+
for ($i = 0; $i < 100000; $i++) {
21+
$node = new stdClass;
22+
$node->self = $node;
23+
}
24+
unset($node);
25+
var_dump(gc_status()['runs'] > 1);
26+
gc_collect_cycles();
27+
var_dump(gc_status()['collected']);
28+
29+
ini_set('opcache.enable', '0');
30+
gc_disable();
31+
var_dump(@opcache_compile_file(__DIR__ . '/gh24361.inc'));
32+
var_dump(gc_status()['protected']);
33+
var_dump(gc_enabled());
34+
$node = new stdClass;
35+
$node->self = $node;
36+
unset($node);
37+
var_dump(gc_collect_cycles());
38+
?>
39+
--EXPECTF--
40+
bool(false)
41+
42+
Fatal error: 'break' not in the 'loop' or 'switch' context in %sgh24361.inc on line 2
43+
bool(false)
44+
bool(false)
45+
int(1)
46+
bool(true)
47+
int(100001)
48+
49+
Fatal error: 'break' not in the 'loop' or 'switch' context in %sgh24361.inc on line 2
50+
bool(false)
51+
bool(false)
52+
bool(false)
53+
int(1)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
GH-24361 (GC is restored before reporting the opcache_compile_file() warning)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.file_update_protection=0
9+
--FILE--
10+
<?php
11+
set_error_handler(function ($severity, $message) {
12+
var_dump($severity === E_WARNING);
13+
var_dump(gc_status()['protected']);
14+
$node = new stdClass;
15+
$node->self = $node;
16+
unset($node);
17+
var_dump(gc_collect_cycles());
18+
throw new RuntimeException('compile warning');
19+
});
20+
21+
try {
22+
opcache_compile_file(__DIR__ . '/gh24361.inc');
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), "\n";
25+
}
26+
var_dump(gc_status()['protected']);
27+
?>
28+
--EXPECTF--
29+
Fatal error: 'break' not in the 'loop' or 'switch' context in %sgh24361.inc on line 2
30+
bool(true)
31+
bool(false)
32+
int(1)
33+
RuntimeException: compile warning
34+
bool(false)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
GH-24361 (memory exhaustion while opening a script keeps GC protected)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.file_update_protection=0
9+
memory_limit=16M
10+
--SKIPIF--
11+
<?php
12+
if (getenv('USE_ZEND_ALLOC') === '0') {
13+
die('skip Zend MM disabled');
14+
}
15+
?>
16+
--FILE--
17+
<?php
18+
class MemoryLimitStream {
19+
public $context;
20+
21+
public function stream_open($path, $mode, $options, &$opened_path) {
22+
$memory = [];
23+
for ($i = 0; $i < 64; $i++) {
24+
$memory[] = str_repeat('x', 1024 * 1024);
25+
}
26+
}
27+
}
28+
stream_wrapper_register('memory-limit', MemoryLimitStream::class);
29+
register_shutdown_function(static function () {
30+
var_dump(gc_status()['protected']);
31+
});
32+
33+
var_dump(gc_status()['protected']);
34+
opcache_compile_file('memory-limit://test');
35+
echo "Unreachable\n";
36+
?>
37+
--EXPECTF--
38+
bool(false)
39+
40+
Fatal error: Allowed memory size of %d bytes exhausted %s in %s on line %d
41+
bool(true)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-24361 (bailouts from user code and existing GC protection are preserved)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.file_update_protection=0
9+
--FILE--
10+
<?php
11+
class CompileErrorStream {
12+
public $context;
13+
14+
public function stream_open($path, $mode, $options, &$opened_path) {
15+
eval('break 2;');
16+
}
17+
}
18+
stream_wrapper_register('compile-error', CompileErrorStream::class);
19+
20+
var_dump(gc_status()['protected']);
21+
var_dump(@opcache_compile_file('compile-error://test'));
22+
var_dump(gc_status()['protected']);
23+
var_dump(@opcache_compile_file(__DIR__ . '/gh24361.inc'));
24+
var_dump(gc_status()['protected']);
25+
?>
26+
--EXPECTF--
27+
bool(false)
28+
29+
Fatal error: 'break' not in the 'loop' or 'switch' context in %s on line 1
30+
bool(false)
31+
bool(true)
32+
33+
Fatal error: 'break' not in the 'loop' or 'switch' context in %sgh24361.inc on line 2
34+
bool(false)
35+
bool(true)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
GH-24361 (GC recovery does not rely on the last reported error)
3+
--EXTENSIONS--
4+
opcache
5+
--INI--
6+
opcache.enable=1
7+
opcache.enable_cli=1
8+
opcache.file_update_protection=0
9+
ignore_repeated_errors=1
10+
ignore_repeated_source=1
11+
display_errors=0
12+
log_errors=0
13+
--FILE--
14+
<?php
15+
class RepeatedErrorStream {
16+
public $context;
17+
18+
public function stream_open($path, $mode, $options, &$opened_path) {
19+
trigger_error("'break' not in the 'loop' or 'switch' context", E_USER_ERROR);
20+
}
21+
}
22+
stream_wrapper_register('repeated-error', RepeatedErrorStream::class);
23+
set_error_handler(static fn() => true, E_WARNING | E_DEPRECATED);
24+
for ($i = 0; $i < 2; $i++) {
25+
var_dump(opcache_compile_file(__DIR__ . '/gh24361.inc'));
26+
var_dump(gc_status()['protected']);
27+
}
28+
var_dump(opcache_compile_file('repeated-error://test'));
29+
var_dump(gc_status()['protected']);
30+
var_dump(error_get_last()['type'] === E_COMPILE_ERROR);
31+
?>
32+
--EXPECT--
33+
bool(false)
34+
bool(false)
35+
bool(false)
36+
bool(false)
37+
bool(false)
38+
bool(true)
39+
bool(true)

‎ext/opcache/zend_accelerator_module.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "zend_closures.h"
2828
#include "zend_shared_alloc.h"
2929
#include "zend_accelerator_blacklist.h"
30+
#include "zend_accelerator_module.h"
3031
#include "php_ini.h"
3132
#include "SAPI.h"
3233
#include "zend_virtual_cwd.h"
@@ -59,6 +60,22 @@ static zif_handler orig_file_exists = NULL;
5960
static zif_handler orig_is_file = NULL;
6061
static zif_handler orig_is_readable = NULL;
6162

63+
ZEND_TLS int accel_last_fatal_error_type;
64+
ZEND_TLS zend_execute_data *accel_last_fatal_error_execute_data;
65+
ZEND_TLS bool accel_last_fatal_error_gc_protected;
66+
67+
void zend_accel_record_fatal_error(
68+
int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
69+
{
70+
if (type & E_FATAL_ERRORS) {
71+
/* Record the origin before zend_bailout() clears the current execution frame.
72+
* Unlike last_error_type, notifications also cover ignored repeated errors. */
73+
accel_last_fatal_error_type = type;
74+
accel_last_fatal_error_execute_data = EG(current_execute_data);
75+
accel_last_fatal_error_gc_protected = gc_protected();
76+
}
77+
}
78+
6279
static int validate_api_restriction(void)
6380
{
6481
if (ZCG(accel_directives).restrict_api && *ZCG(accel_directives).restrict_api) {
@@ -985,10 +1002,18 @@ ZEND_FUNCTION(opcache_compile_file)
9851002
* state. */
9861003
op_array = persistent_compile_file(&handle, ZEND_INCLUDE);
9871004
} else {
1005+
accel_last_fatal_error_type = 0;
9881006
zend_try {
9891007
op_array = persistent_compile_file(&handle, ZEND_INCLUDE);
9901008
} zend_catch {
9911009
EG(current_execute_data) = orig_execute_data;
1010+
/* Only recover GC after a compile error in this frame, not a fatal error
1011+
* that unwound user code called by a stream wrapper or another hook. */
1012+
if (accel_last_fatal_error_type == E_COMPILE_ERROR
1013+
&& accel_last_fatal_error_execute_data == orig_execute_data
1014+
&& !ZEND_HAS_OOM()) {
1015+
gc_protect(accel_last_fatal_error_gc_protected);
1016+
}
9921017
zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " could not compile file %s", ZSTR_VAL(handle.filename));
9931018
} zend_end_try();
9941019
}

‎ext/opcache/zend_accelerator_module.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ int start_accel_module(void);
2626

2727
void zend_accel_override_file_functions(void);
2828

29+
void zend_accel_record_fatal_error(
30+
int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message);
31+
2932
#endif /* _ZEND_ACCELERATOR_MODULE_H */

0 commit comments

Comments
 (0)