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
Changes from 2 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
23 changes: 15 additions & 8 deletions rayaop.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ static const zend_function_entry rayaop_functions[] = {
PHP_FE_END
};



/* Module globals initializer */
static void php_rayaop_init_globals(zend_rayaop_globals *globals) {
globals->intercept_ht = NULL;
Expand Down Expand Up @@ -115,7 +113,9 @@ PHP_RAYAOP_API 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) {
char *key = NULL;
*key_len = spprintf(&key, 0, "%s::%s", ZSTR_VAL(class_name), ZSTR_VAL(method_name));
#ifdef RAYAOP_DEBUG
PHP_RAYAOP_DEBUG_PRINT("Generated key: %s", key);
#endif
return key;
}

Expand All @@ -129,14 +129,13 @@ PHP_RAYAOP_API php_rayaop_intercept_info *php_rayaop_find_intercept_info(const c
return info;
}

/* Parameter preparation and cleanup */
static bool prepare_intercept_params(zend_execute_data *execute_data, zval *params, php_rayaop_intercept_info *info) {
if (!execute_data->This.value.obj) {
if (!Z_OBJ(execute_data->This)) {
php_rayaop_handle_error(RAYAOP_E_INVALID_HANDLER, "Object instance is NULL");
return false;
}

ZVAL_OBJ(&params[0], execute_data->This.value.obj);
ZVAL_OBJ(&params[0], Z_OBJ(execute_data->This));
ZVAL_STR_COPY(&params[1], info->method_name);

array_init(&params[2]);
Expand Down Expand Up @@ -186,15 +185,19 @@ static void rayaop_execute_ex(zend_execute_data *execute_data) {
}

RAYAOP_G(execution_depth)++;
#ifdef RAYAOP_DEBUG
PHP_RAYAOP_DEBUG_PRINT("Execution depth: %d", RAYAOP_G(execution_depth));
#endif

zend_function *func = execute_data->func;
size_t key_len;
char *key = php_rayaop_generate_key(func->common.scope->name, func->common.function_name, &key_len);

php_rayaop_intercept_info *info = php_rayaop_find_intercept_info(key, key_len);
if (info && info->is_enabled) {
#ifdef RAYAOP_DEBUG
PHP_RAYAOP_DEBUG_PRINT("Executing intercept for %s", key);
#endif

if (Z_TYPE(info->handler) != IS_OBJECT) {
php_rayaop_handle_error(RAYAOP_E_INVALID_HANDLER, "Invalid interceptor type");
Expand All @@ -205,7 +208,6 @@ static void rayaop_execute_ex(zend_execute_data *execute_data) {
zval retval;
zval params[3];

prepare_intercept_params(execute_data, params, info);
if (!prepare_intercept_params(execute_data, params, info)) {
cleanup_intercept_params(params);
RAYAOP_G(is_intercepting) = 0;
Expand All @@ -218,8 +220,8 @@ static void rayaop_execute_ex(zend_execute_data *execute_data) {
RAYAOP_G(execution_depth)--;
return;
}
RAYAOP_G(is_intercepting) = 1;

RAYAOP_G(is_intercepting) = 1;
ZVAL_UNDEF(&retval);
if (execute_intercept_handler(&info->handler, params, &retval)) {
if (!Z_ISUNDEF(retval) && execute_data->return_value) {
Expand All @@ -240,6 +242,7 @@ static void rayaop_execute_ex(zend_execute_data *execute_data) {
efree(key);
RAYAOP_G(execution_depth)--;
}

/* Implementation of method_intercept function */
PHP_FUNCTION(method_intercept) {
char *class_name, *method_name;
Expand Down Expand Up @@ -310,10 +313,14 @@ PHP_FUNCTION(method_intercept_enable) {
RAYAOP_G(method_intercept_enabled) = enable;
if (enable) {
zend_execute_ex = rayaop_execute_ex;
#ifdef RAYAOP_DEBUG
PHP_RAYAOP_DEBUG_PRINT("Method intercept enabled");
#endif
} else {
zend_execute_ex = php_rayaop_original_execute_ex;
#ifdef RAYAOP_DEBUG
PHP_RAYAOP_DEBUG_PRINT("Method intercept disabled");
#endif
}
RAYAOP_G_UNLOCK();
}
Expand Down Expand Up @@ -442,7 +449,7 @@ void php_rayaop_debug_print_zval(zval *value) {
php_printf("float(%g)\n", Z_DVAL_P(value));
break;
case IS_STRING:
php_printf("string(%d) \"%s\"\n", Z_STRLEN_P(value), Z_STRVAL_P(value));
php_printf("string(%d) \"%s\"\n", (int)Z_STRLEN_P(value), Z_STRVAL_P(value));
break;
case IS_ARRAY:
php_printf("array(%d) {...}\n", zend_hash_num_elements(Z_ARRVAL_P(value)));
Expand Down
Loading