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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
/* php_dec_interceptor.h */
#ifndef PHP_DEC_INTERCEPTOR_H #ifndef PHP_DEC_INTERCEPTOR_H
#define PHP_DEC_INTERCEPTOR_H #define PHP_DEC_INTERCEPTOR_H
@ -9,13 +10,14 @@
extern zend_module_entry dec_interceptor_module_entry; extern zend_module_entry dec_interceptor_module_entry;
#define phpext_dec_interceptor_ptr &dec_interceptor_module_entry #define phpext_dec_interceptor_ptr &dec_interceptor_module_entry
/* ✅ 正确的函数指针类型 */ /* chain-hook originals */
extern zend_op_array *(*original_compile_string)(zval *source_string, const char *filename); 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);
/* ✅ 正确的 hook 函数声明 */ /* hooks */
zend_op_array *custom_compile_string(zval *source_string, const char *filename); 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);
/* 模块生命周期函数 */
PHP_MINIT_FUNCTION(dec_interceptor); PHP_MINIT_FUNCTION(dec_interceptor);
PHP_MSHUTDOWN_FUNCTION(dec_interceptor); PHP_MSHUTDOWN_FUNCTION(dec_interceptor);
PHP_MINFO_FUNCTION(dec_interceptor); PHP_MINFO_FUNCTION(dec_interceptor);