Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fixes: Refined logic for managing execution depth and interception state #9

Merged
merged 31 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d052a53
Refactor object access for parameter preparation.
koriym Dec 6, 2024
eb4b10e
Add debug print statements within conditional compilation
koriym Dec 6, 2024
efdb2bb
Refactor RayAOP to improve error handling and performance
koriym Dec 7, 2024
efc8bbe
Simplify exception and interception handling logic
koriym Dec 7, 2024
9faf1be
Enhance execution data validation and error handling
koriym Dec 7, 2024
5bae335
Refactor function names for consistency
koriym Dec 7, 2024
d0a05a0
Refactor interception logic and improve exception handling
koriym Dec 7, 2024
7bffe61
Configure build with debug flags.
koriym Dec 7, 2024
e9e3c72
Enable method interception in rayaop test
koriym Dec 7, 2024
42ae90b
Refactor variable naming in thread safety test
koriym Dec 7, 2024
b92b3f3
fixup! Enable method interception in rayaop test
koriym Dec 7, 2024
1af55cc
Improve test result validation and messaging
koriym Dec 8, 2024
77810d3
Free thread-safe global ID after allocation
koriym Dec 8, 2024
39d00dd
fixup! Improve test result validation and messaging
koriym Dec 8, 2024
ef8af08
Handle void returns in user function calls
koriym Dec 8, 2024
865d6e6
Enable method intercept in thread safety test
koriym Dec 8, 2024
535b748
Add test for RayAOP void return handling.
koriym Dec 8, 2024
be7f484
Improve error handling in interceptor execution
koriym Dec 8, 2024
c1397d6
Update rayaop.cFix potential use-after-free in thread safety initiali…
koriym Dec 8, 2024
1a6f8c0
Add demo and debug functions to build script
koriym Dec 8, 2024
3f2e7b4
Add comprehensive error handling and debug options
koriym Dec 8, 2024
a16f5f7
Add demo using Ray.Aop for Aspect-Oriented Programming
koriym Dec 9, 2024
0135526
Add tests and memory checks for demo directory
koriym Dec 9, 2024
e93e054
fixup! Add demo using Ray.Aop for Aspect-Oriented Programming
koriym Dec 9, 2024
458c2d8
Add support for PHP 8.4 in GitHub Actions workflow
koriym Dec 9, 2024
dccadcb
Improve error messaging for interceptor failures
koriym Dec 9, 2024
3d5abd3
Remove redundant mutex allocation check
koriym Dec 9, 2024
3344101
Update demo/src/BillingService.php
koriym Dec 9, 2024
dea7e8e
Check for spprintf failure in key generation
koriym Dec 9, 2024
9e8d774
fix: potential memory leak during parameter preparation
koriym Dec 9, 2024
cf5ffbf
Prevent interception of internal functions
koriym Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clean() {
prepare() {
echo "Preparing..."
phpize
./configure
./configure CFLAGS="-g -O0"
}

build() {
Expand Down
130 changes: 37 additions & 93 deletions php_rayaop.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
/* Header guard */
#ifndef PHP_RAYAOP_H
#define PHP_RAYAOP_H

// #define RAYAOP_DEBUG 1

/* Configuration header */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Required system headers first */
#include <stdint.h>

/* PHP Core headers in correct order */
#include "php.h"
#include "zend.h"
#include "zend_types.h"
#include "zend_API.h"
#include "zend_types.h"
#include "zend_ini.h"
#include "php_ini.h"
#include "ext/standard/info.h"
Expand All @@ -27,129 +19,81 @@
#include "TSRM.h"
#endif

/* Export macros */
#ifdef PHP_WIN32
# define PHP_RAYAOP_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
# define PHP_RAYAOP_API __attribute__ ((visibility("default")))
#else
# define PHP_RAYAOP_API
#endif

/* Constants */
#define MAX_EXECUTION_DEPTH 100
#define PHP_RAYAOP_VERSION "1.0.0"
#define RAYAOP_NS "Ray\\Aop\\"

/* Error codes */
#define RAYAOP_E_MEMORY_ALLOCATION 1
#define RAYAOP_E_HASH_UPDATE 2
#define RAYAOP_E_INVALID_HANDLER 3
#define RAYAOP_E_MAX_DEPTH_EXCEEDED 4

/* Argument information declarations */
ZEND_BEGIN_ARG_INFO_EX(arginfo_method_intercept, 0, 0, 3)
ZEND_ARG_TYPE_INFO(0, class_name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, method_name, IS_STRING, 0)
ZEND_ARG_OBJ_INFO(0, interceptor, Ray\\Aop\\MethodInterceptorInterface, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_method_intercept_init, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_method_intercept_enable, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, enable, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

/* Debug mode configuration */
#ifdef RAYAOP_DEBUG
#define PHP_RAYAOP_DEBUG_PRINT(fmt, ...) \
do { \
php_printf("RAYAOP DEBUG [%s:%d]: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
#else
#define PHP_RAYAOP_DEBUG_PRINT(fmt, ...)
#endif

/* Declare mutex type */
#if defined(WIN32)
#define MUTEX_T HANDLE
#else
#define MUTEX_T pthread_mutex_t*
#endif
extern MUTEX_T rayaop_mutex;
#define RAYAOP_E_MEMORY_ALLOCATION 1
#define RAYAOP_E_HASH_UPDATE 2
#define RAYAOP_E_INVALID_HANDLER 3
#define RAYAOP_E_MAX_DEPTH_EXCEEDED 4
#define RAYAOP_E_NULL_POINTER 5
#define RAYAOP_E_INVALID_STATE 6

#ifdef ZTS
#define RAYAOP_G_LOCK() tsrm_mutex_lock(rayaop_mutex)
extern MUTEX_T rayaop_mutex;
#define RAYAOP_G_LOCK() tsrm_mutex_lock(rayaop_mutex)
#define RAYAOP_G_UNLOCK() tsrm_mutex_unlock(rayaop_mutex)
#else
#define RAYAOP_G_LOCK()
#define RAYAOP_G_UNLOCK()
#endif

/* Module entry */
extern zend_module_entry rayaop_module_entry;
#define phpext_rayaop_ptr &rayaop_module_entry

/* Interface class entry */
extern zend_class_entry *ray_aop_method_interceptor_interface_ce;

/* Windows DLL export */
#ifdef PHP_WIN32
#define PHP_RAYAOP_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#define PHP_RAYAOP_API __attribute__ ((visibility("default")))
#else
#define PHP_RAYAOP_API
#endif

/* Intercept information structure */
typedef struct _php_rayaop_intercept_info {
zend_string *class_name; /* Class name to intercept */
zend_string *method_name; /* Method name to intercept */
zval handler; /* Intercept handler */
zend_bool is_enabled; /* Flag to enable/disable interception */
zend_string *class_name; /* Class name */
zend_string *method_name; /* Method name */
zval handler; /* Intercept handler object (only one) */
zend_bool is_enabled; /* Enabled flag */
} php_rayaop_intercept_info;

/* Module globals structure */
ZEND_BEGIN_MODULE_GLOBALS(rayaop)
HashTable *intercept_ht; /* Intercept hash table */
zend_bool is_intercepting; /* Intercepting flag */
uint32_t execution_depth; /* Execution depth counter */
zend_bool method_intercept_enabled; /* Global interception enable flag */
uint32_t debug_level; /* Debug level */
HashTable *intercept_ht; /* Hash table for intercept information */
zend_bool is_intercepting; /* Flag indicating if currently intercepting */
uint32_t execution_depth; /* Nesting depth of interception */
zend_bool method_intercept_enabled; /* Global enable/disable flag */
uint32_t debug_level; /* Debug level */
ZEND_END_MODULE_GLOBALS(rayaop)

/* Global initializer */
static void php_rayaop_init_globals(zend_rayaop_globals *globals);

/* Globals access macro */
#ifdef ZTS
#define RAYAOP_G(v) TSRMG(rayaop_globals_id, zend_rayaop_globals *, v)
#else
#define RAYAOP_G(v) (rayaop_globals.v)
#endif

/* Module functions */
extern zend_module_entry rayaop_module_entry;
#define phpext_rayaop_ptr &rayaop_module_entry

extern zend_class_entry *ray_aop_method_interceptor_interface_ce;

/* PHP lifecycle hooks */
PHP_MINIT_FUNCTION(rayaop);
PHP_MSHUTDOWN_FUNCTION(rayaop);
PHP_RINIT_FUNCTION(rayaop);
PHP_RSHUTDOWN_FUNCTION(rayaop);
PHP_MINFO_FUNCTION(rayaop);

/* Extension functions */
/* PHP functions */
PHP_FUNCTION(method_intercept);
PHP_FUNCTION(method_intercept_init);
PHP_FUNCTION(method_intercept_enable);

/* Utility functions */
/* API functions */
PHP_RAYAOP_API void php_rayaop_handle_error(int error_code, const char *message);
PHP_RAYAOP_API bool php_rayaop_should_intercept(zend_execute_data *execute_data);
PHP_RAYAOP_API zend_bool php_rayaop_should_intercept(zend_execute_data *execute_data);
PHP_RAYAOP_API char *php_rayaop_generate_key(zend_string *class_name, zend_string *method_name, size_t *key_len);
PHP_RAYAOP_API php_rayaop_intercept_info *php_rayaop_find_intercept_info(const char *key, size_t key_len);

/* Memory management functions */
PHP_RAYAOP_API void php_rayaop_free_intercept_info(zval *zv);
PHP_RAYAOP_API php_rayaop_intercept_info *php_rayaop_create_intercept_info(void);

/* Debug functions */
#ifdef RAYAOP_DEBUG
void php_rayaop_debug_dump_intercept_info(void);
void php_rayaop_debug_print_zval(zval *value);
#endif

/* Module globals declaration */
ZEND_EXTERN_MODULE_GLOBALS(rayaop)

#endif /* PHP_RAYAOP_H */
#endif /* PHP_RAYAOP_H */
Loading
Loading