Skip to content

Commit

Permalink
Add support for Zval IS_INDIRECT
Browse files Browse the repository at this point in the history
As mentioned in #219, I believe we're making incorrect use of IS_CALLABLE on Zval types right now. Zval type bits are never actually stored as IS_CALLABLE, which overlaps with the _actual_ type value of IS_INDIRECT. IS_INDIRECT is almost the same as IS_REFERENCE, but is a direct reference to another Zval rather than reference object. As `Zval::is_callable()` and `Zval::callable()` don't actually make use of `IS_CALLABLE` then I think it's safe to switch this out.
  • Loading branch information
joehoyle committed Nov 24, 2023
1 parent 1af2d54 commit 3cd7a8a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions allowed_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ bind! {
IS_CONSTANT_AST_EX,
IS_DOUBLE,
IS_FALSE,
IS_INDIRECT,
IS_INTERNED_STRING_EX,
IS_LONG,
IS_MIXED,
Expand Down
1 change: 1 addition & 0 deletions docsrs_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub const IS_CONSTANT_AST: u32 = 11;
pub const IS_CALLABLE: u32 = 12;
pub const IS_VOID: u32 = 14;
pub const IS_MIXED: u32 = 16;
pub const IS_INDIRECT: u32 = 12;
pub const IS_PTR: u32 = 13;
pub const _IS_BOOL: u32 = 18;
pub const Z_TYPE_FLAGS_SHIFT: u32 = 8;
Expand Down
28 changes: 16 additions & 12 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crate::ffi::{
CONST_CS, CONST_DEPRECATED, CONST_NO_FILE_CACHE, CONST_PERSISTENT, E_COMPILE_ERROR,
E_COMPILE_WARNING, E_CORE_ERROR, E_CORE_WARNING, E_DEPRECATED, E_ERROR, E_NOTICE, E_PARSE,
E_RECOVERABLE_ERROR, E_STRICT, E_USER_DEPRECATED, E_USER_ERROR, E_USER_NOTICE, E_USER_WARNING,
E_WARNING, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_LONG, IS_MIXED,
IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE, IS_TYPE_COLLECTABLE,
IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, PHP_INI_ALL, PHP_INI_PERDIR, PHP_INI_SYSTEM,
PHP_INI_USER, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS, ZEND_ACC_CALL_VIA_TRAMPOLINE,
ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED, ZEND_ACC_CTOR,
ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING, ZEND_ACC_FAKE_CLOSURE,
ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK, ZEND_ACC_HAS_RETURN_TYPE,
ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
E_WARNING, IS_ARRAY, IS_CALLABLE, IS_CONSTANT_AST, IS_DOUBLE, IS_FALSE, IS_INDIRECT, IS_LONG,
IS_MIXED, IS_NULL, IS_OBJECT, IS_PTR, IS_REFERENCE, IS_RESOURCE, IS_STRING, IS_TRUE,
IS_TYPE_COLLECTABLE, IS_TYPE_REFCOUNTED, IS_UNDEF, IS_VOID, PHP_INI_ALL, PHP_INI_PERDIR,
PHP_INI_SYSTEM, PHP_INI_USER, ZEND_ACC_ABSTRACT, ZEND_ACC_ANON_CLASS,
ZEND_ACC_CALL_VIA_TRAMPOLINE, ZEND_ACC_CHANGED, ZEND_ACC_CLOSURE, ZEND_ACC_CONSTANTS_UPDATED,
ZEND_ACC_CTOR, ZEND_ACC_DEPRECATED, ZEND_ACC_DONE_PASS_TWO, ZEND_ACC_EARLY_BINDING,
ZEND_ACC_FAKE_CLOSURE, ZEND_ACC_FINAL, ZEND_ACC_GENERATOR, ZEND_ACC_HAS_FINALLY_BLOCK,
ZEND_ACC_HAS_RETURN_TYPE, ZEND_ACC_HAS_TYPE_HINTS, ZEND_ACC_HEAP_RT_CACHE, ZEND_ACC_IMMUTABLE,
ZEND_ACC_IMPLICIT_ABSTRACT_CLASS, ZEND_ACC_INTERFACE, ZEND_ACC_LINKED, ZEND_ACC_NEARLY_LINKED,
ZEND_ACC_NEVER_CACHE, ZEND_ACC_NO_DYNAMIC_PROPERTIES, ZEND_ACC_PRELOADED, ZEND_ACC_PRIVATE,
ZEND_ACC_PROMOTED, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC, ZEND_ACC_RESOLVED_INTERFACES,
Expand Down Expand Up @@ -246,6 +246,7 @@ pub enum DataType {
Mixed,
Bool,
Ptr,
Indirect,
}

impl Default for DataType {
Expand All @@ -269,6 +270,7 @@ impl DataType {
DataType::Object(_) => IS_OBJECT,
DataType::Resource => IS_RESOURCE,
DataType::Reference => IS_RESOURCE,
DataType::Indirect => IS_INDIRECT,
DataType::Callable => IS_CALLABLE,
DataType::ConstantExpression => IS_CONSTANT_AST,
DataType::Void => IS_VOID,
Expand Down Expand Up @@ -337,6 +339,7 @@ impl From<u32> for DataType {

contains!(IS_VOID, Void);
contains!(IS_PTR, Ptr);
contains!(IS_INDIRECT, Indirect);
contains!(IS_CALLABLE, Callable);
contains!(IS_CONSTANT_AST, ConstantExpression);
contains!(IS_REFERENCE, Reference);
Expand Down Expand Up @@ -379,6 +382,7 @@ impl Display for DataType {
DataType::Bool => write!(f, "Bool"),
DataType::Mixed => write!(f, "Mixed"),
DataType::Ptr => write!(f, "Pointer"),
DataType::Indirect => write!(f, "Indirect"),
}
}
}
Expand All @@ -388,9 +392,9 @@ mod tests {
use super::DataType;
use crate::ffi::{
IS_ARRAY, IS_ARRAY_EX, IS_CALLABLE, IS_CONSTANT_AST, IS_CONSTANT_AST_EX, IS_DOUBLE,
IS_FALSE, IS_INTERNED_STRING_EX, IS_LONG, IS_NULL, IS_OBJECT, IS_OBJECT_EX, IS_PTR,
IS_REFERENCE, IS_REFERENCE_EX, IS_RESOURCE, IS_RESOURCE_EX, IS_STRING, IS_STRING_EX,
IS_TRUE, IS_UNDEF, IS_VOID,
IS_FALSE, IS_INDIRECT, IS_INTERNED_STRING_EX, IS_LONG, IS_NULL, IS_OBJECT, IS_OBJECT_EX,
IS_PTR, IS_REFERENCE, IS_REFERENCE_EX, IS_RESOURCE, IS_RESOURCE_EX, IS_STRING,
IS_STRING_EX, IS_TRUE, IS_UNDEF, IS_VOID,
};
use std::convert::TryFrom;

Expand All @@ -414,7 +418,7 @@ mod tests {
test!(IS_RESOURCE, Resource);
test!(IS_REFERENCE, Reference);
test!(IS_CONSTANT_AST, ConstantExpression);
test!(IS_CALLABLE, Callable);
test!(IS_INDIRECT, Indirect);
test!(IS_VOID, Void);
test!(IS_PTR, Ptr);

Expand Down
24 changes: 24 additions & 0 deletions src/types/zval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ impl Zval {
.try_call_method(name, params)
}

/// Returns the value of the zval if it is an internal indirect reference.
pub fn indirect(&self) -> Option<&Zval> {
if self.is_indirect() {
Some(unsafe { &*(self.value.zv as *mut Zval) })
} else {
None
}
}

/// Returns a mutable reference to the zval if it is an internal indirect reference.
pub fn indirect_mut(&self) -> Option<&mut Zval> {
if self.is_indirect() {
Some(unsafe { &mut *(self.value.zv as *mut Zval) })
} else {
None
}
}

/// Returns the value of the zval if it is a reference.
pub fn reference(&self) -> Option<&Zval> {
if self.is_reference() {
Expand Down Expand Up @@ -329,6 +347,11 @@ impl Zval {
self.get_type() == DataType::Reference
}

/// Returns true if the zval is a reference, false otherwise.
pub fn is_indirect(&self) -> bool {
self.get_type() == DataType::Indirect
}

/// Returns true if the zval is callable, false otherwise.
pub fn is_callable(&self) -> bool {
let ptr: *const Self = self;
Expand Down Expand Up @@ -601,6 +624,7 @@ impl Debug for Zval {
DataType::ConstantExpression => field!(Option::<()>::None),
DataType::Void => field!(Option::<()>::None),
DataType::Bool => field!(self.bool()),
DataType::Indirect => field!(self.indirect()),
// SAFETY: We are not accessing the pointer.
DataType::Ptr => field!(unsafe { self.ptr::<c_void>() }),
};
Expand Down

0 comments on commit 3cd7a8a

Please sign in to comment.