From 8567f092ee6ab8fd7213fc894a0b2c72da41348e Mon Sep 17 00:00:00 2001 From: JyJyJcr <82190170+JyJyJcr@users.noreply.github.com> Date: Sun, 7 Apr 2024 06:05:07 +0900 Subject: [PATCH 1/3] fix: remove shared ref to static mut --- examples/async.rs | 9 +++++---- examples/awssig.rs | 5 +++-- examples/curl.rs | 5 +++-- examples/upstream.rs | 7 ++++--- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/examples/async.rs b/examples/async.rs index 6b0c1e1..2f88c53 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -7,6 +7,7 @@ use ngx::ffi::{ use ngx::http::MergeConfigError; use ngx::{core, core::Status, http, http::HTTPModule}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; +use std::borrow::{Borrow, BorrowMut}; use std::os::raw::{c_char, c_void}; use std::sync::atomic::AtomicBool; use std::sync::Arc; @@ -21,7 +22,7 @@ impl http::HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module); + let cmcf = http::ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -131,7 +132,7 @@ unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) { // this doesn't have have good performance but works as a simple thread-safe example and doesn't causes // segfault. The best method that provides both thread-safety and performance requires // an nginx patch. - post_event(event, &ngx_posted_events as *const _ as _); + post_event(event, ngx_posted_events.borrow_mut()); } } @@ -162,7 +163,7 @@ unsafe fn post_event(event: *mut ngx_event_t, queue: *mut ngx_queue_s) { } http_request_handler!(async_access_handler, |request: &mut http::Request| { - let co = unsafe { request.get_module_loc_conf::(&ngx_http_async_module) }; + let co = unsafe { request.get_module_loc_conf::(ngx_http_async_module.borrow()) }; let co = co.expect("module config is none"); if !co.enable { return core::Status::NGX_DECLINED; @@ -203,7 +204,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| { event.data = Arc::into_raw(event_data.clone()) as _; event.log = (*ngx_cycle).log; - post_event(event, &ngx_posted_events as *const _ as _); + post_event(event, ngx_posted_events.borrow_mut()); } ngx_log_debug_http!(request, "async module enabled: {}", co.enable); diff --git a/examples/awssig.rs b/examples/awssig.rs index e5aa70d..be9b0bf 100644 --- a/examples/awssig.rs +++ b/examples/awssig.rs @@ -7,6 +7,7 @@ use ngx::ffi::{ }; use ngx::{core, core::Status, http::*}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; +use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; struct Module; @@ -17,7 +18,7 @@ impl HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module); + let cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -269,7 +270,7 @@ extern "C" fn ngx_http_awssigv4_commands_set_s3_endpoint( http_request_handler!(awssigv4_header_handler, |request: &mut Request| { // get Module Config from request - let conf = unsafe { request.get_module_loc_conf::(&ngx_http_awssigv4_module) }; + let conf = unsafe { request.get_module_loc_conf::(ngx_http_awssigv4_module.borrow()) }; let conf = conf.unwrap(); ngx_log_debug_http!(request, "AWS signature V4 module {}", { if conf.enable { diff --git a/examples/curl.rs b/examples/curl.rs index df4ab71..647dab6 100644 --- a/examples/curl.rs +++ b/examples/curl.rs @@ -7,6 +7,7 @@ use ngx::ffi::{ use ngx::http::MergeConfigError; use ngx::{core, core::Status, http, http::HTTPModule}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; +use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; struct Module; @@ -17,7 +18,7 @@ impl http::HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module); + let cmcf = http::ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -104,7 +105,7 @@ impl http::Merge for ModuleConfig { } http_request_handler!(curl_access_handler, |request: &mut http::Request| { - let co = unsafe { request.get_module_loc_conf::(&ngx_http_curl_module) }; + let co = unsafe { request.get_module_loc_conf::(ngx_http_curl_module.borrow()) }; let co = co.expect("module config is none"); ngx_log_debug_http!(request, "curl module enabled: {}", co.enable); diff --git a/examples/upstream.rs b/examples/upstream.rs index 7d87d3d..4525160 100644 --- a/examples/upstream.rs +++ b/examples/upstream.rs @@ -25,6 +25,7 @@ use ngx::{ ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string, }; use std::{ + borrow::Borrow, mem, os::raw::{c_char, c_void}, slice, @@ -152,7 +153,7 @@ http_upstream_init_peer_pt!( } let maybe_conf: Option<*const SrvConfig> = - unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, &ngx_http_upstream_custom_module) }; + unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, ngx_http_upstream_custom_module.borrow()) }; if maybe_conf.is_none() { return Status::NGX_ERROR; } @@ -244,7 +245,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom( ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream"); let maybe_conf: Option<*mut SrvConfig> = - ngx_http_conf_upstream_srv_conf_mutable(us, &ngx_http_upstream_custom_module); + ngx_http_conf_upstream_srv_conf_mutable(us, ngx_http_upstream_custom_module.borrow()); if maybe_conf.is_none() { ngx_conf_log_error( NGX_LOG_EMERG as usize, @@ -309,7 +310,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom( } let uscf: *mut ngx_http_upstream_srv_conf_t = - ngx_http_conf_get_module_srv_conf(cf, &ngx_http_upstream_module) as *mut ngx_http_upstream_srv_conf_t; + ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module.borrow()) as *mut ngx_http_upstream_srv_conf_t; ccf.original_init_upstream = if (*uscf).peer.init_upstream.is_some() { (*uscf).peer.init_upstream From 9d3f0a1da1f72ca470e27de447db13a6c8cc946e Mon Sep 17 00:00:00 2001 From: JyJyJcr <82190170+JyJyJcr@users.noreply.github.com> Date: Sun, 7 Apr 2024 06:35:17 +0900 Subject: [PATCH 2/3] refactor: use addr_of_mut()! if possible --- examples/async.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/async.rs b/examples/async.rs index 2f88c53..2547605 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -7,8 +7,9 @@ use ngx::ffi::{ use ngx::http::MergeConfigError; use ngx::{core, core::Status, http, http::HTTPModule}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; -use std::borrow::{Borrow, BorrowMut}; +use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; +use std::ptr::addr_of_mut; use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::time::Instant; @@ -132,7 +133,7 @@ unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) { // this doesn't have have good performance but works as a simple thread-safe example and doesn't causes // segfault. The best method that provides both thread-safety and performance requires // an nginx patch. - post_event(event, ngx_posted_events.borrow_mut()); + post_event(event, addr_of_mut!(ngx_posted_events)); } } @@ -204,7 +205,7 @@ http_request_handler!(async_access_handler, |request: &mut http::Request| { event.data = Arc::into_raw(event_data.clone()) as _; event.log = (*ngx_cycle).log; - post_event(event, ngx_posted_events.borrow_mut()); + post_event(event, addr_of_mut!(ngx_posted_events)); } ngx_log_debug_http!(request, "async module enabled: {}", co.enable); From 0ce5759fe6bccc1ae40c0528873f98411fb9ba28 Mon Sep 17 00:00:00 2001 From: JyJyJcr <82190170+JyJyJcr@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:14:36 +0900 Subject: [PATCH 3/3] fix: replace `borrow()` by `&*addr_of!()` --- examples/async.rs | 7 +++---- examples/awssig.rs | 6 +++--- examples/curl.rs | 6 +++--- examples/upstream.rs | 9 +++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/async.rs b/examples/async.rs index 2547605..b82f3e8 100644 --- a/examples/async.rs +++ b/examples/async.rs @@ -7,9 +7,8 @@ use ngx::ffi::{ use ngx::http::MergeConfigError; use ngx::{core, core::Status, http, http::HTTPModule}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; -use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; -use std::ptr::addr_of_mut; +use std::ptr::{addr_of, addr_of_mut}; use std::sync::atomic::AtomicBool; use std::sync::Arc; use std::time::Instant; @@ -23,7 +22,7 @@ impl http::HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = http::ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); + let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module)); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -164,7 +163,7 @@ unsafe fn post_event(event: *mut ngx_event_t, queue: *mut ngx_queue_s) { } http_request_handler!(async_access_handler, |request: &mut http::Request| { - let co = unsafe { request.get_module_loc_conf::(ngx_http_async_module.borrow()) }; + let co = unsafe { request.get_module_loc_conf::(&*addr_of!(ngx_http_async_module)) }; let co = co.expect("module config is none"); if !co.enable { return core::Status::NGX_DECLINED; diff --git a/examples/awssig.rs b/examples/awssig.rs index be9b0bf..aa37eae 100644 --- a/examples/awssig.rs +++ b/examples/awssig.rs @@ -7,8 +7,8 @@ use ngx::ffi::{ }; use ngx::{core, core::Status, http::*}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; -use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; +use std::ptr::addr_of; struct Module; @@ -18,7 +18,7 @@ impl HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); + let cmcf = ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module)); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -270,7 +270,7 @@ extern "C" fn ngx_http_awssigv4_commands_set_s3_endpoint( http_request_handler!(awssigv4_header_handler, |request: &mut Request| { // get Module Config from request - let conf = unsafe { request.get_module_loc_conf::(ngx_http_awssigv4_module.borrow()) }; + let conf = unsafe { request.get_module_loc_conf::(&*addr_of!(ngx_http_awssigv4_module)) }; let conf = conf.unwrap(); ngx_log_debug_http!(request, "AWS signature V4 module {}", { if conf.enable { diff --git a/examples/curl.rs b/examples/curl.rs index 647dab6..0dcda82 100644 --- a/examples/curl.rs +++ b/examples/curl.rs @@ -7,8 +7,8 @@ use ngx::ffi::{ use ngx::http::MergeConfigError; use ngx::{core, core::Status, http, http::HTTPModule}; use ngx::{http_request_handler, ngx_log_debug_http, ngx_modules, ngx_null_command, ngx_string}; -use std::borrow::Borrow; use std::os::raw::{c_char, c_void}; +use std::ptr::addr_of; struct Module; @@ -18,7 +18,7 @@ impl http::HTTPModule for Module { type LocConf = ModuleConfig; unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t { - let cmcf = http::ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module.borrow()); + let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &*addr_of!(ngx_http_core_module)); let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers) as *mut ngx_http_handler_pt; @@ -105,7 +105,7 @@ impl http::Merge for ModuleConfig { } http_request_handler!(curl_access_handler, |request: &mut http::Request| { - let co = unsafe { request.get_module_loc_conf::(ngx_http_curl_module.borrow()) }; + let co = unsafe { request.get_module_loc_conf::(&*addr_of!(ngx_http_curl_module)) }; let co = co.expect("module config is none"); ngx_log_debug_http!(request, "curl module enabled: {}", co.enable); diff --git a/examples/upstream.rs b/examples/upstream.rs index 4525160..5a48792 100644 --- a/examples/upstream.rs +++ b/examples/upstream.rs @@ -25,9 +25,9 @@ use ngx::{ ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string, }; use std::{ - borrow::Borrow, mem, os::raw::{c_char, c_void}, + ptr::addr_of, slice, }; @@ -153,7 +153,7 @@ http_upstream_init_peer_pt!( } let maybe_conf: Option<*const SrvConfig> = - unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, ngx_http_upstream_custom_module.borrow()) }; + unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, &*addr_of!(ngx_http_upstream_custom_module)) }; if maybe_conf.is_none() { return Status::NGX_ERROR; } @@ -245,7 +245,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom( ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream"); let maybe_conf: Option<*mut SrvConfig> = - ngx_http_conf_upstream_srv_conf_mutable(us, ngx_http_upstream_custom_module.borrow()); + ngx_http_conf_upstream_srv_conf_mutable(us, &*addr_of!(ngx_http_upstream_custom_module)); if maybe_conf.is_none() { ngx_conf_log_error( NGX_LOG_EMERG as usize, @@ -310,7 +310,8 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom( } let uscf: *mut ngx_http_upstream_srv_conf_t = - ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module.borrow()) as *mut ngx_http_upstream_srv_conf_t; + ngx_http_conf_get_module_srv_conf(cf, &*addr_of!(ngx_http_upstream_module)) + as *mut ngx_http_upstream_srv_conf_t; ccf.original_init_upstream = if (*uscf).peer.init_upstream.is_some() { (*uscf).peer.init_upstream