diff --git a/NEWS b/NEWS index 3cdfc7969523..6c1a0883d86b 100644 --- a/NEWS +++ b/NEWS @@ -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, diff --git a/ext/opcache/jit/zend_jit_trace.c b/ext/opcache/jit/zend_jit_trace.c index 36a0baa8c9ff..290e369160e1 100644 --- a/ext/opcache/jit/zend_jit_trace.c +++ b/ext/opcache/jit/zend_jit_trace.c @@ -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) { diff --git a/ext/opcache/tests/jit/max_root_traces_negative.phpt b/ext/opcache/tests/jit/max_root_traces_negative.phpt new file mode 100644 index 000000000000..e8b7d76b2ba1 --- /dev/null +++ b/ext/opcache/tests/jit/max_root_traces_negative.phpt @@ -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-- + +--FILE-- + +--EXPECTF-- +Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and %d in Unknown on line 0 +string(4) "1024" diff --git a/ext/opcache/tests/jit/max_root_traces_no_memory.phpt b/ext/opcache/tests/jit/max_root_traces_no_memory.phpt new file mode 100644 index 000000000000..48ea7473e7ad --- /dev/null +++ b/ext/opcache/tests/jit/max_root_traces_no_memory.phpt @@ -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-- + +--FILE-- + +--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 diff --git a/ext/opcache/tests/jit/max_root_traces_too_large.phpt b/ext/opcache/tests/jit/max_root_traces_too_large.phpt new file mode 100644 index 000000000000..0d8779d92f3e --- /dev/null +++ b/ext/opcache/tests/jit/max_root_traces_too_large.phpt @@ -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-- + +--FILE-- + +--EXPECTF-- +Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and 4294967295 in Unknown on line 0 +string(4) "1024" diff --git a/ext/opcache/tests/jit/max_root_traces_zero.phpt b/ext/opcache/tests/jit/max_root_traces_zero.phpt new file mode 100644 index 000000000000..de8f1cd0fca9 --- /dev/null +++ b/ext/opcache/tests/jit/max_root_traces_zero.phpt @@ -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-- + +--FILE-- + +--EXPECTF-- +Warning: Invalid "opcache.jit_max_root_traces" setting. Should be between 1 and %d in Unknown on line 0 +string(4) "1024" diff --git a/ext/opcache/zend_accelerator_module.c b/ext/opcache/zend_accelerator_module.c index 821dfbe39121..00707b732851 100644 --- a/ext/opcache/zend_accelerator_module.c +++ b/ext/opcache/zend_accelerator_module.c @@ -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) @@ -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 (EXPECTED(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); @@ -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)