From a616c4b4db95a0c635c07ea71c37e7eeec9278f5 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 12:27:11 +0200 Subject: [PATCH] ZTS support for file globals --- src/ffi.rs | 1 + src/wrapper.c | 9 +++++++++ src/zend/globals.rs | 15 ++++++++------- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index cd3a81c..465b509 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -28,6 +28,7 @@ extern "C" { pub fn ext_php_rs_executor_globals() -> *mut zend_executor_globals; pub fn ext_php_rs_process_globals() -> *mut php_core_globals; pub fn ext_php_rs_sapi_globals() -> *mut sapi_globals_struct; + pub fn ext_php_rs_file_globals() -> *mut php_file_globals; } include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/src/wrapper.c b/src/wrapper.c index 514882b..c88e2da 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -64,3 +64,12 @@ sapi_globals_struct *ext_php_rs_sapi_globals() { return &sapi_globals; #endif } + + +php_file_globals *ext_php_rs_file_globals() { +#ifdef ZTS + return TSRMG_FAST_BULK(file_globals_id, php_file_globals *); +#else + return &file_globals; +#endif +} diff --git a/src/zend/globals.rs b/src/zend/globals.rs index fee0d09..bf68e24 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -8,11 +8,11 @@ use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; use crate::ffi::{ - _zend_executor_globals, ext_php_rs_executor_globals, ext_php_rs_process_globals, - ext_php_rs_sapi_globals, file_globals, php_core_globals, php_file_globals, sapi_globals_struct, - sapi_header_struct, sapi_headers_struct, sapi_request_info, zend_is_auto_global, - TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET, TRACK_VARS_POST, - TRACK_VARS_REQUEST, TRACK_VARS_SERVER, + _zend_executor_globals, ext_php_rs_executor_globals, ext_php_rs_file_globals, + ext_php_rs_process_globals, ext_php_rs_sapi_globals, php_core_globals, php_file_globals, + sapi_globals_struct, sapi_header_struct, sapi_headers_struct, sapi_request_info, + zend_is_auto_global, TRACK_VARS_COOKIE, TRACK_VARS_ENV, TRACK_VARS_FILES, TRACK_VARS_GET, + TRACK_VARS_POST, TRACK_VARS_REQUEST, TRACK_VARS_SERVER, }; use crate::types::{ZendHashTable, ZendObject, ZendStr}; @@ -378,7 +378,8 @@ impl FileGlobals { pub fn get() -> GlobalReadGuard { // SAFETY: PHP executor globals are statically declared therefore should never // return an invalid pointer. - let globals = unsafe { &file_globals }; + let globals = unsafe { ext_php_rs_file_globals().as_ref() } + .expect("Static file globals were invalid"); let guard = FILE_GLOBALS_LOCK.read(); GlobalReadGuard { globals, guard } } @@ -393,7 +394,7 @@ impl FileGlobals { pub fn get_mut() -> GlobalWriteGuard { // SAFETY: PHP executor globals are statically declared therefore should never // return an invalid pointer. - let globals = unsafe { &mut file_globals }; + let globals = unsafe { &mut *ext_php_rs_file_globals() }; let guard = SAPI_GLOBALS_LOCK.write(); GlobalWriteGuard { globals, guard } }