From da40e6ecbf7f91fb4393c51c4936875b9a4e742b Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 29 Jun 2021 23:08:26 +0200 Subject: [PATCH 1/3] Fix #81206: Multiple PHP processes crash with JIT enabled The fix for bug 80175 (PR #6268) broke most SAPIs wrt. JIT, most notably cgi-fcgi, because for these SAPIs `SUCCESSFULLY_REATTACHED` actually has to set the `reattached` flag. --- ext/opcache/ZendAccelerator.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index ab56fc4d4f789..43d2ca50400f1 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3008,8 +3008,10 @@ static zend_result accel_post_startup(void) zend_accel_error(ACCEL_LOG_FATAL, "Failure to initialize shared memory structures - probably not enough shared memory."); return SUCCESS; case SUCCESSFULLY_REATTACHED: -#if defined(HAVE_JIT) && !defined(ZEND_WIN32) - reattached = 1; +#ifdef HAVE_JIT + if (sapi_module.name && strcmp(sapi_module.name, "apache2handler") == 0) { + reattached = 1; + } #endif zend_shared_alloc_lock(); accel_shared_globals = (zend_accel_shared_globals *) ZSMMG(app_shared_globals); From 7cd7e4a040cd4e8bd7ef8689ce7f29bcbbf1dc8c Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sun, 4 Jul 2021 17:25:48 +0200 Subject: [PATCH 2/3] Fix #81206: Multiple PHP processes crash with JIT enabled We need to avoid resetting the JIT for all SAPIs, but we need to initialize the JIT handlers even when only reattaching on Windows. --- ext/opcache/ZendAccelerator.c | 4 +--- ext/opcache/jit/zend_jit.c | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index 43d2ca50400f1..73be3509143f0 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -3009,9 +3009,7 @@ static zend_result accel_post_startup(void) return SUCCESS; case SUCCESSFULLY_REATTACHED: #ifdef HAVE_JIT - if (sapi_module.name && strcmp(sapi_module.name, "apache2handler") == 0) { - reattached = 1; - } + reattached = 1; #endif zend_shared_alloc_lock(); accel_shared_globals = (zend_accel_shared_globals *) ZSMMG(app_shared_globals); diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index b56918b4ce912..8072f825e9468 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -4044,6 +4044,17 @@ ZEND_EXT_API void zend_jit_protect(void) #endif } +static void zend_jit_init_non_hybrid_handlers(void) +{ + zend_jit_runtime_jit_handler = (const void*)zend_runtime_jit; + zend_jit_profile_jit_handler = (const void*)zend_jit_profile_helper; + zend_jit_func_hot_counter_handler = (const void*)zend_jit_func_counter_helper; + zend_jit_loop_hot_counter_handler = (const void*)zend_jit_loop_counter_helper; + zend_jit_func_trace_counter_handler = (const void*)zend_jit_func_trace_helper; + zend_jit_ret_trace_counter_handler = (const void*)zend_jit_ret_trace_helper; + zend_jit_loop_trace_counter_handler = (const void*)zend_jit_loop_trace_helper; +} + static int zend_jit_make_stubs(void) { dasm_State* dasm_state = NULL; @@ -4071,13 +4082,7 @@ static int zend_jit_make_stubs(void) zend_jit_ret_trace_counter_handler = dasm_labels[zend_lbhybrid_ret_trace_counter]; zend_jit_loop_trace_counter_handler = dasm_labels[zend_lbhybrid_loop_trace_counter]; } else { - zend_jit_runtime_jit_handler = (const void*)zend_runtime_jit; - zend_jit_profile_jit_handler = (const void*)zend_jit_profile_helper; - zend_jit_func_hot_counter_handler = (const void*)zend_jit_func_counter_helper; - zend_jit_loop_hot_counter_handler = (const void*)zend_jit_loop_counter_helper; - zend_jit_func_trace_counter_handler = (const void*)zend_jit_func_trace_helper; - zend_jit_ret_trace_counter_handler = (const void*)zend_jit_ret_trace_helper; - zend_jit_loop_trace_counter_handler = (const void*)zend_jit_loop_trace_helper; + zend_jit_init_non_hybrid_handlers(); } dasm_free(&dasm_state); @@ -4353,6 +4358,7 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, zend_bool reattached) #if _WIN32 /* restore global labels */ memcpy(dasm_labels, dasm_buf, sizeof(void*) * zend_lb_MAX); + zend_jit_init_non_hybrid_handlers(); #endif } From 39a1ad14dffe7abce61fe6ef02e249b2d8abb2b6 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 17 Jul 2021 13:27:45 +0200 Subject: [PATCH 3/3] Re-initialize ZEND_VM_KIND_HYBRID handlers too --- ext/opcache/jit/zend_jit.c | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/ext/opcache/jit/zend_jit.c b/ext/opcache/jit/zend_jit.c index 8072f825e9468..1b39f07a39937 100644 --- a/ext/opcache/jit/zend_jit.c +++ b/ext/opcache/jit/zend_jit.c @@ -4044,15 +4044,25 @@ ZEND_EXT_API void zend_jit_protect(void) #endif } -static void zend_jit_init_non_hybrid_handlers(void) +static void zend_jit_init_handlers(void) { - zend_jit_runtime_jit_handler = (const void*)zend_runtime_jit; - zend_jit_profile_jit_handler = (const void*)zend_jit_profile_helper; - zend_jit_func_hot_counter_handler = (const void*)zend_jit_func_counter_helper; - zend_jit_loop_hot_counter_handler = (const void*)zend_jit_loop_counter_helper; - zend_jit_func_trace_counter_handler = (const void*)zend_jit_func_trace_helper; - zend_jit_ret_trace_counter_handler = (const void*)zend_jit_ret_trace_helper; - zend_jit_loop_trace_counter_handler = (const void*)zend_jit_loop_trace_helper; + if (zend_jit_vm_kind == ZEND_VM_KIND_HYBRID) { + zend_jit_runtime_jit_handler = dasm_labels[zend_lbhybrid_runtime_jit]; + zend_jit_profile_jit_handler = dasm_labels[zend_lbhybrid_profile_jit]; + zend_jit_func_hot_counter_handler = dasm_labels[zend_lbhybrid_func_hot_counter]; + zend_jit_loop_hot_counter_handler = dasm_labels[zend_lbhybrid_loop_hot_counter]; + zend_jit_func_trace_counter_handler = dasm_labels[zend_lbhybrid_func_trace_counter]; + zend_jit_ret_trace_counter_handler = dasm_labels[zend_lbhybrid_ret_trace_counter]; + zend_jit_loop_trace_counter_handler = dasm_labels[zend_lbhybrid_loop_trace_counter]; + } else { + zend_jit_runtime_jit_handler = (const void*)zend_runtime_jit; + zend_jit_profile_jit_handler = (const void*)zend_jit_profile_helper; + zend_jit_func_hot_counter_handler = (const void*)zend_jit_func_counter_helper; + zend_jit_loop_hot_counter_handler = (const void*)zend_jit_loop_counter_helper; + zend_jit_func_trace_counter_handler = (const void*)zend_jit_func_trace_helper; + zend_jit_ret_trace_counter_handler = (const void*)zend_jit_ret_trace_helper; + zend_jit_loop_trace_counter_handler = (const void*)zend_jit_loop_trace_helper; + } } static int zend_jit_make_stubs(void) @@ -4073,17 +4083,7 @@ static int zend_jit_make_stubs(void) } } - if (zend_jit_vm_kind == ZEND_VM_KIND_HYBRID) { - zend_jit_runtime_jit_handler = dasm_labels[zend_lbhybrid_runtime_jit]; - zend_jit_profile_jit_handler = dasm_labels[zend_lbhybrid_profile_jit]; - zend_jit_func_hot_counter_handler = dasm_labels[zend_lbhybrid_func_hot_counter]; - zend_jit_loop_hot_counter_handler = dasm_labels[zend_lbhybrid_loop_hot_counter]; - zend_jit_func_trace_counter_handler = dasm_labels[zend_lbhybrid_func_trace_counter]; - zend_jit_ret_trace_counter_handler = dasm_labels[zend_lbhybrid_ret_trace_counter]; - zend_jit_loop_trace_counter_handler = dasm_labels[zend_lbhybrid_loop_trace_counter]; - } else { - zend_jit_init_non_hybrid_handlers(); - } + zend_jit_init_handlers(); dasm_free(&dasm_state); return 1; @@ -4358,7 +4358,7 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, zend_bool reattached) #if _WIN32 /* restore global labels */ memcpy(dasm_labels, dasm_buf, sizeof(void*) * zend_lb_MAX); - zend_jit_init_non_hybrid_handlers(); + zend_jit_init_handlers(); #endif }