Skip to content
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ PHP NEWS
. Fixed function JIT ASSIGN_DIM reporting an undefined value or key out of
order, or when the VM does not, on non-array containers. (Jorg Sowa)
. Fix JIT register allocation for ASSIGN to traced reference. (ndossche)
. Fixed opcache.jit_max_root_traces accepting values outside of its valid
range, which corrupted shared memory. (Marc Bennewitz)

- PDO_Firebird:
. Fixed stack buffer overflow with long connection user names, passwords,
Expand Down
9 changes: 7 additions & 2 deletions ext/opcache/jit/zend_jit_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ static zend_always_inline const char *zend_jit_trace_star_desc(uint8_t trace_fla
static void zend_jit_trace_startup(bool reattached)
{
if (!reattached) {
zend_jit_traces = (zend_jit_trace_info*)zend_shared_alloc(sizeof(zend_jit_trace_info) * JIT_G(max_root_traces));
size_t size = sizeof(zend_jit_trace_info) * (size_t) JIT_G(max_root_traces);

zend_jit_traces = (zend_jit_trace_info*)zend_shared_alloc(size);
if (!zend_jit_traces) {
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not allocate JIT root traces buffer!");
zend_accel_error_noreturn(ACCEL_LOG_FATAL,
"Could not allocate JIT root traces buffer of %zu bytes for opcache.jit_max_root_traces=" ZEND_LONG_FMT
", increase opcache.memory_consumption or decrease opcache.jit_max_root_traces",
size, JIT_G(max_root_traces));
}
zend_jit_exit_groups = (const void**)zend_shared_alloc(sizeof(void*) * (ZEND_JIT_TRACE_MAX_EXITS/ZEND_JIT_EXIT_POINTS_PER_GROUP));
if (!zend_jit_exit_groups) {
Expand Down
21 changes: 21 additions & 0 deletions ext/opcache/tests/jit/max_root_traces_negative.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
JIT: a negative opcache.jit_max_root_traces is rejected
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.jit_max_root_traces=-1
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if ((opcache_get_status()['jit']['on'] ?? false) === false) die('skip: JIT is not available');
?>
--FILE--
<?php
var_dump(ini_get("opcache.jit_max_root_traces"));
?>
--EXPECTF--
Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and %d in Unknown on line 0
string(4) "1024"
24 changes: 24 additions & 0 deletions ext/opcache/tests/jit/max_root_traces_no_memory.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
JIT: opcache.jit_max_root_traces whose root traces buffer does not fit into shared memory
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.memory_consumption=64
opcache.jit_max_root_traces=4294967295
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip 64-bit only");
/* The fatal error exits during startup, leaving everything allocated so far unfreed */
if (getenv('SKIP_ASAN')) die('xleak Leaks memory with ASAN');
if ((opcache_get_status()['jit']['on'] ?? false) === false) die('skip: JIT is not available');
?>
--FILE--
<?php
var_dump(ini_get("opcache.jit_max_root_traces"));
?>
--EXPECTF--
%sFatal Error Could not allocate JIT root traces buffer of %d bytes for opcache.jit_max_root_traces=4294967295, increase opcache.memory_consumption or decrease opcache.jit_max_root_traces
22 changes: 22 additions & 0 deletions ext/opcache/tests/jit/max_root_traces_too_large.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
JIT: opcache.jit_max_root_traces beyond the uint32_t trace ids is rejected
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.jit_max_root_traces=4294967296
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip 64-bit only");
if ((opcache_get_status()['jit']['on'] ?? false) === false) die('skip: JIT is not available');
?>
--FILE--
<?php
var_dump(ini_get("opcache.jit_max_root_traces"));
?>
--EXPECTF--
Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and 4294967295 in Unknown on line 0
string(4) "1024"
21 changes: 21 additions & 0 deletions ext/opcache/tests/jit/max_root_traces_zero.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
JIT: opcache.jit_max_root_traces=0 is rejected
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.jit=tracing
opcache.jit_buffer_size=16M
opcache.jit_max_root_traces=0
--EXTENSIONS--
opcache
--SKIPIF--
<?php
if ((opcache_get_status()['jit']['on'] ?? false) === false) die('skip: JIT is not available');
?>
--FILE--
<?php
var_dump(ini_get("opcache.jit_max_root_traces"));
?>
--EXPECTF--
Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and %d in Unknown on line 0
string(4) "1024"
21 changes: 20 additions & 1 deletion ext/opcache/zend_accelerator_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

#ifdef HAVE_JIT
#include "jit/zend_jit.h"
#include "Optimizer/zend_func_info.h"
#include "Optimizer/zend_call_graph.h"
#include "jit/zend_jit_internal.h"
#endif

#define STRING_NOT_NULL(s) (NULL == (s)?"":s)
Expand Down Expand Up @@ -271,6 +274,22 @@ static ZEND_INI_MH(OnUpdateUnrollL)
return FAILURE;
}

/* Trace ids are uint32_t, and the size of the root traces buffer has to fit size_t */
#define ZEND_JIT_MAX_ROOT_TRACES MIN(UINT32_MAX, SIZE_MAX / sizeof(zend_jit_trace_info))

static ZEND_INI_MH(OnUpdateMaxRootTraces)
{
zend_long val = zend_ini_parse_quantity_warn(new_value, entry->name);
if (val > 0 && val <= ZEND_JIT_MAX_ROOT_TRACES) {
zend_long *p = (zend_long *) ZEND_INI_GET_ADDR();
*p = val;
return SUCCESS;
}
zend_error(E_WARNING, "Invalid \"%s\" setting. Should be between 1 and %zu", ZSTR_VAL(entry->name),
(size_t) ZEND_JIT_MAX_ROOT_TRACES);
return FAILURE;
}

static ZEND_INI_MH(OnUpdateMaxTraceLength)
{
zend_long val = zend_ini_parse_quantity_warn(new_value, entry->name);
Expand Down Expand Up @@ -347,7 +366,7 @@ ZEND_INI_BEGIN()
STD_PHP_INI_ENTRY("opcache.jit_debug" , "0", PHP_INI_ALL, OnUpdateJitDebug, debug, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_bisect_limit" , "0", PHP_INI_ALL, OnUpdateLong, bisect_limit, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_prof_threshold" , "0.005", PHP_INI_ALL, OnUpdateReal, prof_threshold, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_root_traces" , "1024", PHP_INI_SYSTEM, OnUpdateLong, max_root_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_root_traces" , "1024", PHP_INI_SYSTEM, OnUpdateMaxRootTraces, max_root_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_side_traces" , "128", PHP_INI_SYSTEM, OnUpdateLong, max_side_traces, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_max_exit_counters" , "8192", PHP_INI_SYSTEM, OnUpdateLong, max_exit_counters, zend_jit_globals, jit_globals)
STD_PHP_INI_ENTRY("opcache.jit_hot_loop" , "64", PHP_INI_SYSTEM, OnUpdateCounter, hot_loop, zend_jit_globals, jit_globals)
Expand Down
Loading