From 01060ff08e49f33710c7450848099b09c35a5805 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Fri, 7 Jul 2023 14:21:16 +0200 Subject: [PATCH] Support ZTS --- src/ffi.rs | 1 + src/wrapper.c | 12 ++++++++++++ src/wrapper.h | 3 ++- src/zend/globals.rs | 10 +++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 92614c4..a23fcb1 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -26,6 +26,7 @@ extern "C" { pub fn ext_php_rs_zend_object_alloc(obj_size: usize, ce: *mut zend_class_entry) -> *mut c_void; pub fn ext_php_rs_zend_object_release(obj: *mut zend_object); pub fn ext_php_rs_executor_globals() -> *mut zend_executor_globals; + pub fn ext_php_rs_process_globals() -> *mut php_core_globals; } include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/src/wrapper.c b/src/wrapper.c index faf585e..7a5eba5 100644 --- a/src/wrapper.c +++ b/src/wrapper.c @@ -39,3 +39,15 @@ zend_executor_globals *ext_php_rs_executor_globals() { return &executor_globals; #endif } + +php_core_globals *ext_php_rs_process_globals() { +#ifdef ZTS +#ifdef ZEND_ENABLE_STATIC_TSRMLS_CACHE + return TSRMG_FAST_BULK_STATIC(core_globals_offset, php_core_globals); +#else + return TSRMG_FAST_BULK(core_globals_offset, php_core_globals *); +#endif +#else + return &core_globals; +#endif +} diff --git a/src/wrapper.h b/src/wrapper.h index 2813263..c00ab5b 100644 --- a/src/wrapper.h +++ b/src/wrapper.h @@ -29,4 +29,5 @@ void ext_php_rs_set_known_valid_utf8(zend_string *zs); const char *ext_php_rs_php_build_id(); void *ext_php_rs_zend_object_alloc(size_t obj_size, zend_class_entry *ce); void ext_php_rs_zend_object_release(zend_object *obj); -zend_executor_globals *ext_php_rs_executor_globals(); \ No newline at end of file +zend_executor_globals *ext_php_rs_executor_globals(); +php_core_globals *ext_php_rs_process_globals(); diff --git a/src/zend/globals.rs b/src/zend/globals.rs index b241cee..7ed5eef 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -6,9 +6,9 @@ use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; use crate::ffi::{ - _zend_executor_globals, core_globals, ext_php_rs_executor_globals, php_core_globals, - 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_process_globals, + php_core_globals, 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}; @@ -85,7 +85,7 @@ impl ProcessGlobals { pub fn get() -> GlobalReadGuard { // SAFETY: PHP executor globals are statically declared therefore should never // return an invalid pointer. - let globals = unsafe { &core_globals }; + let globals = unsafe { &*ext_php_rs_process_globals() }; let guard = PROCESS_GLOBALS_LOCK.read(); GlobalReadGuard { globals, guard } } @@ -100,7 +100,7 @@ impl ProcessGlobals { pub fn get_mut() -> GlobalWriteGuard { // SAFETY: PHP executor globals are statically declared therefore should never // return an invalid pointer. - let globals = unsafe { &mut core_globals }; + let globals = unsafe { &mut *ext_php_rs_process_globals() }; let guard = PROCESS_GLOBALS_LOCK.write(); GlobalWriteGuard { globals, guard } }