This commit is contained in:
hailin 2025-07-30 22:50:49 +08:00
parent a9021a3e7e
commit e12849a644
2 changed files with 52 additions and 68 deletions

View File

@ -1,6 +1,8 @@
dnl Enable or disable dec_interceptor as a Zend extension
PHP_ARG_ENABLE(dec_interceptor, whether to enable dec_interceptor support,
[ --enable-dec_interceptor Enable dec_interceptor support])
[ --enable-dec_interceptor Enable dec_interceptor Zend extension])
if test "$PHP_DEC_INTERCEPTOR" = "yes"; then
PHP_NEW_EXTENSION(dec_interceptor, dec_interceptor_string.c, $ext_shared)
if test "$PHP_DEC_INTERCEPTOR" != "no"; then
dnl 注册为 Zend 扩展,源码文件列表只需 dec_interceptor.c
ZEND_EXTENSION_ENTRY(dec_interceptor, dec_interceptor.c, $ext_shared)
fi

View File

@ -1,88 +1,70 @@
/* dec_interceptor.c */
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_compile.h"
#include "php_dec_interceptor.h"
#include <time.h>
#include <string.h>
#include <limits.h>
/* 保存原始函数指针 */
zend_op_array *(*original_compile_file)(zend_file_handle *file_handle, int type) = NULL;
zend_op_array *(*original_compile_string)(zval *source_string, const char *filename) = NULL;
/* 先声明保留所有可能的原始指针 */
static zend_op_array *(*prev_compile_file)(zend_file_handle *fh, int type);
static zend_op_array *(*prev_compile_string)(zval *src, const char *filename);
static zend_op_array *(*prev_compile)(zend_file_handle *fh, int type);
/* hook zend_compile_file */
zend_op_array *custom_compile_file(zend_file_handle *file_handle, int type)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
/* 1. 覆盖 zend_compile_file */
static zend_op_array *hook_compile_file(zend_file_handle *fh, int type) {
/* 记录到日志 */
FILE *f = fopen("/tmp/dec_interceptor.log","a");
if (f) {
time_t t = time(NULL);
fprintf(f, "[%ld] compile_file: %s\n", t,
file_handle->filename ? file_handle->filename : "(null)");
fprintf(f, "[%ld] compile_file: %s\n",
(long)time(NULL), fh->filename ? fh->filename : "(null)");
fclose(f);
}
return original_compile_file(file_handle, type);
/* 链式调用:先走上一个(可能是原生,也可能是 swoole_loader */
return prev_compile_file(fh, type);
}
/* hook zend_compile_string */
zend_op_array *custom_compile_string(zval *source_string, const char *filename)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
/* 2. 覆盖 zend_compile_string */
static zend_op_array *hook_compile_string(zval *src, const char *filename) {
FILE *f = fopen("/tmp/dec_interceptor.log","a");
if (f) {
time_t t = time(NULL);
fprintf(f, "[%ld] compile_string: %s\n", t, filename ? filename : "(null)");
if (Z_TYPE_P(source_string) == IS_STRING) {
fwrite(Z_STRVAL_P(source_string), 1, Z_STRLEN_P(source_string), f);
fprintf(f, "\n----\n");
fprintf(f, "[%ld] compile_string: %s\n",
(long)time(NULL), filename?filename:"(null)");
if (Z_TYPE_P(src)==IS_STRING) {
fwrite(Z_STRVAL_P(src),1,Z_STRLEN_P(src),f);
fprintf(f,"\n----\n");
}
fclose(f);
}
return original_compile_string(source_string, filename);
return prev_compile_string(src, filename);
}
PHP_MINIT_FUNCTION(dec_interceptor)
{
FILE *f = fopen("/tmp/dec_interceptor.log", "a");
/* Zend 扩展的启动函数 */
static void dec_interceptor_startup(zend_extension *extension) {
/* 链式 Hook把当前引擎指针交给 prev_* 保存,然后重写 */
prev_compile_file = zend_compile_file;
zend_compile_file = hook_compile_file;
prev_compile_string = zend_compile_string;
zend_compile_string = hook_compile_string;
/* 追加一条 MINIT 日志 */
FILE *f = fopen("/tmp/dec_interceptor.log","a");
if (f) {
time_t t = time(NULL);
fprintf(f, "[%ld] MINIT: hook setup\n", t);
fprintf(f, "[%ld] [ZendExt] hook initialized\n", (long)time(NULL));
fclose(f);
}
original_compile_file = zend_compile_file;
zend_compile_file = custom_compile_file;
original_compile_string = zend_compile_string;
zend_compile_string = custom_compile_string;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(dec_interceptor)
{
zend_compile_file = original_compile_file;
zend_compile_string = original_compile_string;
return SUCCESS;
}
PHP_MINFO_FUNCTION(dec_interceptor)
{
php_info_print_table_start();
php_info_print_table_row(2, "dec_interceptor support", "enabled");
php_info_print_table_end();
}
zend_module_entry dec_interceptor_module_entry = {
STANDARD_MODULE_HEADER,
"dec_interceptor",
NULL,
PHP_MINIT(dec_interceptor),
PHP_MSHUTDOWN_FUNCTION(dec_interceptor),
NULL,
NULL,
PHP_MINFO_FUNCTION(dec_interceptor),
PHP_DEC_INTERCEPTOR_VERSION,
STANDARD_MODULE_PROPERTIES
/* zend_extension_entry 结构 */
zend_extension dec_interceptor_zend_extension = {
.name = "dec_interceptor",
.version = PHP_DEC_INTERCEPTOR_VERSION,
.startup = dec_interceptor_startup,
.shutdown = NULL,
.activate = NULL,
.deactivate = NULL,
.message_handler = NULL,
.op_array_handler= NULL,
.resource_number = 0,
.build_id = ZEND_MODULE_API_NO,
.disable_zend_debug = 0
};
ZEND_GET_MODULE(dec_interceptor)