From a9c6877641719a7cf3a192a063e93f2d8930daa6 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 11:33:28 +0200 Subject: [PATCH 1/5] Add function for requesting VM interrupt This needs to handle older versions of PHP that didn't use an atomic bool. --- allowed_bindings.rs | 4 +++- docsrs_bindings.rs | 4 ++++ src/zend/globals.rs | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/allowed_bindings.rs b/allowed_bindings.rs index b1e2748fb..0cd8270c1 100644 --- a/allowed_bindings.rs +++ b/allowed_bindings.rs @@ -243,5 +243,7 @@ bind! { php_printf, __zend_malloc, tsrm_get_ls_cache, - executor_globals_offset + executor_globals_offset, + zend_atomic_bool_store, + zend_interrupt_function } diff --git a/docsrs_bindings.rs b/docsrs_bindings.rs index 1888949c3..ab28fa2d3 100644 --- a/docsrs_bindings.rs +++ b/docsrs_bindings.rs @@ -670,6 +670,10 @@ pub struct _zend_class_entry__bindgen_ty_4__bindgen_ty_2 { pub builtin_functions: *const _zend_function_entry, pub module: *mut _zend_module_entry, } +extern "C" { + pub static mut zend_interrupt_function: + ::std::option::Option; +} extern "C" { pub static mut zend_standard_class_def: *mut zend_class_entry; } diff --git a/src/zend/globals.rs b/src/zend/globals.rs index 0f2cae8e5..ca7b12147 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut}; use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; -use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals}; +use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals, zend_atomic_bool_store}; use crate::types::{ZendHashTable, ZendObject}; @@ -70,6 +70,20 @@ impl ExecutorGlobals { // SAFETY: `as_mut` checks for null. Some(unsafe { ZBox::from_raw(exception_ptr.as_mut()?) }) } + + /// Request an interrupt of the PHP VM. This will call the registered interrupt handler function. + /// set with [`crate::ffi::zend_interrupt_function`]. + pub fn request_interrupt(&mut self) { + cfg_if::cfg_if! { + if #[cfg(any(php82))] { + unsafe { + zend_atomic_bool_store(&mut self.vm_interrupt, true); + } + } else { + self.vm_interrupt = true; + } + } + } } /// Executor globals rwlock. From 7a3360032983d64b3194e0ce118801cc455186eb Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 11:50:19 +0200 Subject: [PATCH 2/5] Conditional use --- src/zend/globals.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/zend/globals.rs b/src/zend/globals.rs index ca7b12147..8905b76a6 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -5,8 +5,9 @@ use std::ops::{Deref, DerefMut}; use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; -use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals, zend_atomic_bool_store}; - +use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals}; +#[cfg(any(php82))] +use crate::ffi::zend_atomic_bool_store; use crate::types::{ZendHashTable, ZendObject}; /// Stores global variables used in the PHP executor. From 994455e0ef73167d828e11edd2cb309be7315b66 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 12:16:53 +0200 Subject: [PATCH 3/5] fmt --- src/builders/module.rs | 3 ++- src/zend/globals.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/builders/module.rs b/src/builders/module.rs index 01bc9e4eb..ee2b219fd 100644 --- a/src/builders/module.rs +++ b/src/builders/module.rs @@ -131,7 +131,8 @@ impl ModuleBuilder { /// This function can be useful if you need to do any final cleanup at the /// very end of a request, after all other resources have been released. For /// example, if your extension creates any persistent resources that last - /// beyond a single request, you could use this function to clean those up. # Arguments + /// beyond a single request, you could use this function to clean those up. + /// # Arguments /// /// * `func` - The function to be called when shutdown is requested. pub fn post_deactivate_function(mut self, func: extern "C" fn() -> i32) -> Self { diff --git a/src/zend/globals.rs b/src/zend/globals.rs index 8905b76a6..550160a41 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -5,9 +5,9 @@ use std::ops::{Deref, DerefMut}; use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; -use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals}; #[cfg(any(php82))] use crate::ffi::zend_atomic_bool_store; +use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals}; use crate::types::{ZendHashTable, ZendObject}; /// Stores global variables used in the PHP executor. @@ -72,7 +72,8 @@ impl ExecutorGlobals { Some(unsafe { ZBox::from_raw(exception_ptr.as_mut()?) }) } - /// Request an interrupt of the PHP VM. This will call the registered interrupt handler function. + /// Request an interrupt of the PHP VM. This will call the registered + /// interrupt handler function. /// set with [`crate::ffi::zend_interrupt_function`]. pub fn request_interrupt(&mut self) { cfg_if::cfg_if! { From 52bd71c4ac5d13036a247734b0119b30e13f785c Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 12:30:18 +0200 Subject: [PATCH 4/5] uneccesary any --- src/zend/globals.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/zend/globals.rs b/src/zend/globals.rs index 550160a41..c9a7a0d91 100644 --- a/src/zend/globals.rs +++ b/src/zend/globals.rs @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut}; use parking_lot::{const_rwlock, RwLock, RwLockReadGuard, RwLockWriteGuard}; use crate::boxed::ZBox; -#[cfg(any(php82))] +#[cfg(php82)] use crate::ffi::zend_atomic_bool_store; use crate::ffi::{_zend_executor_globals, ext_php_rs_executor_globals}; use crate::types::{ZendHashTable, ZendObject}; @@ -77,7 +77,7 @@ impl ExecutorGlobals { /// set with [`crate::ffi::zend_interrupt_function`]. pub fn request_interrupt(&mut self) { cfg_if::cfg_if! { - if #[cfg(any(php82))] { + if #[cfg(php82)] { unsafe { zend_atomic_bool_store(&mut self.vm_interrupt, true); } From fe186ae00362139d2b032b48e9b58c24ff91b7dc Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 19 Jul 2023 12:56:28 +0200 Subject: [PATCH 5/5] Add zend_atomic_store_bool --- docsrs_bindings.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docsrs_bindings.rs b/docsrs_bindings.rs index ab28fa2d3..a7cc5a163 100644 --- a/docsrs_bindings.rs +++ b/docsrs_bindings.rs @@ -1057,6 +1057,9 @@ pub struct zend_atomic_bool_s { pub value: u8, } pub type zend_atomic_bool = zend_atomic_bool_s; +extern "C" { + pub fn zend_atomic_bool_store(obj: *mut zend_atomic_bool, desired: bool); +} #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct _zend_stack {