This commit is contained in:
hailin 2025-07-31 10:00:43 +08:00
parent 638b3f4c3a
commit 03ffa3f8a2
2 changed files with 50 additions and 14 deletions

View File

@ -4,9 +4,10 @@
#include "php_dec_interceptor.h"
#include <time.h>
/* 保存原始编译函数指针 */
/* 保存原始指针 */
zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type) = NULL;
zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename) = NULL;
void (*prev_execute_ex)(zend_execute_data *execute_data) = NULL;
/* hook zend_compile_file */
zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type)
@ -19,16 +20,6 @@ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type)
return prev_compile_file ? prev_compile_file(file_handle, type) : NULL;
}
PHP_RINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] RINIT: zend_compile_file = %p\n", (long)time(NULL), zend_compile_file);
fclose(f);
}
return SUCCESS;
}
/* hook zend_compile_string */
zend_op_array *hook_compile_string(zval *source_string, const char *filename)
{
@ -40,6 +31,26 @@ zend_op_array *hook_compile_string(zval *source_string, const char *filename)
return prev_compile_string ? prev_compile_string(source_string, filename) : NULL;
}
/* hook zend_execute_ex */
void hook_execute_ex(zend_execute_data *execute_data)
{
const zend_function *func = execute_data->func;
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
if (func && func->common.function_name) {
fprintf(f, "[%ld] hook_execute_ex: %s\n", (long)time(NULL), ZSTR_VAL(func->common.function_name));
} else {
fprintf(f, "[%ld] hook_execute_ex: (anonymous or internal)\n", (long)time(NULL));
}
fclose(f);
}
if (prev_execute_ex) {
prev_execute_ex(execute_data);
}
}
/* MINIT */
PHP_MINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
@ -49,6 +60,8 @@ PHP_MINIT_FUNCTION(dec_interceptor)
fprintf(f, " hook_compile_file addr: %p\n", hook_compile_file);
fprintf(f, " zend_compile_string addr: %p\n", zend_compile_string);
fprintf(f, " hook_compile_string addr: %p\n", hook_compile_string);
fprintf(f, " zend_execute_ex addr: %p\n", zend_execute_ex);
fprintf(f, " hook_execute_ex addr: %p\n", hook_execute_ex);
fclose(f);
}
@ -58,14 +71,29 @@ PHP_MINIT_FUNCTION(dec_interceptor)
prev_compile_string = zend_compile_string;
zend_compile_string = hook_compile_string;
prev_execute_ex = zend_execute_ex;
zend_execute_ex = hook_execute_ex;
return SUCCESS;
}
/* RINIT */
PHP_RINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
fprintf(f, "[%ld] RINIT: zend_compile_file = %p\n", (long)time(NULL), zend_compile_file);
fclose(f);
}
return SUCCESS;
}
/* MSHUTDOWN */
PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
{
zend_compile_file = prev_compile_file;
zend_compile_string = prev_compile_string;
zend_execute_ex = prev_execute_ex;
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
if (f) {
@ -76,6 +104,7 @@ PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
return SUCCESS;
}
/* MINFO */
PHP_MINFO_FUNCTION(dec_interceptor)
{
php_info_print_table_start();
@ -83,14 +112,15 @@ PHP_MINFO_FUNCTION(dec_interceptor)
php_info_print_table_end();
}
/* 模块定义 */
/* MODULE ENTRY */
zend_module_entry dec_interceptor_module_entry = {
STANDARD_MODULE_HEADER,
"dec_interceptor",
NULL,
PHP_MINIT(dec_interceptor),
PHP_MSHUTDOWN(dec_interceptor),
NULL, NULL,
PHP_RINIT(dec_interceptor),
NULL,
PHP_MINFO(dec_interceptor),
PHP_DEC_INTERCEPTOR_VERSION,
STANDARD_MODULE_PROPERTIES

View File

@ -3,21 +3,27 @@
#include "php.h"
#include "zend_compile.h"
#include "zend_execute.h"
#define PHP_DEC_INTERCEPTOR_VERSION "0.1.0"
extern zend_module_entry dec_interceptor_module_entry;
#define phpext_dec_interceptor_ptr &dec_interceptor_module_entry
/* 保存原始函数指针 */
extern zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type);
extern zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename);
extern void (*prev_execute_ex)(zend_execute_data *execute_data);
/* 拦截函数 */
zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type);
zend_op_array *hook_compile_string(zval *source_string, const char *filename);
void hook_execute_ex(zend_execute_data *execute_data);
PHP_RINIT_FUNCTION(dec_interceptor);
PHP_MINIT_FUNCTION(dec_interceptor);
PHP_MSHUTDOWN_FUNCTION(dec_interceptor);
PHP_RINIT_FUNCTION(dec_interceptor);
PHP_RSHUTDOWN_FUNCTION(dec_interceptor);
PHP_MINFO_FUNCTION(dec_interceptor);
#endif /* PHP_DEC_INTERCEPTOR_H */