LLVMPass快速入门(四):代码插桩

admin 2026-04-16 04:01:03 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详细介绍了使用LLVMPass实现代码插桩的技术方法,通过具体示例演示了如何在函数调用前插入printf语句来输出函数名。文章包含完整的Pass编写流程、CMake配置、编译构建步骤以及测试验证结果,提供了可操作的技术实现方案。 综合评分: 85 文章分类: 安全开发,二进制安全,安全工具,技术标准,其他


cover_image

LLVM Pass快速入门(四):代码插桩

原创

黑与白的边界 黑与白的边界

黑与白的边界

2026年2月4日 18:00 广东

在小说阅读器读本章

去阅读

代码插桩

项目需求:在函数运行时打印出运行的函数名

项目目录如下

/MyProject
├── CMakeLists.txt # CMake 配置文件
├── build/ #构建目录
│   └── test.c #测试编译代码
└── mypass3.cpp # pass 项目代码

一,测试代码示例

test.c

#include&nbsp;<stdio.h>

void&nbsp;func_A()&nbsp;{
&nbsp; &nbsp;&nbsp;int&nbsp;a =&nbsp;1;
}

void&nbsp;func_B()&nbsp;{
&nbsp; &nbsp; func_A();
}

int&nbsp;main()&nbsp;{
&nbsp; &nbsp;&nbsp;printf("hello world!\n");
&nbsp; &nbsp; func_A();
&nbsp; &nbsp; func_B();
&nbsp; &nbsp;&nbsp;return&nbsp;0;
}

二,编写Pass

其他的固定的模板之前文章注释有,这里我只注释当前项目重要的部分代码流程: 遍历指令并匹配ADD指令->替换为sub指令

#include&nbsp;"llvm/IR/PassManager.h"
#include&nbsp;"llvm/Passes/PassBuilder.h"
#include&nbsp;"llvm/Passes/PassPlugin.h"
#include&nbsp;"llvm/Support/raw_ostream.h"
#include&nbsp;"llvm/IR/Function.h"
#include&nbsp;"llvm/IR/BasicBlock.h"
#include&nbsp;"llvm/IR/Instruction.h"
#include&nbsp;"llvm/IR/Instructions.h"
#include&nbsp;"llvm/IR/IRBuilder.h"

using&nbsp;namespace&nbsp;llvm;

namespace&nbsp;{

struct&nbsp;mypass3&nbsp;:&nbsp;public&nbsp;PassInfoMixin<mypass3> {

&nbsp; &nbsp;&nbsp;PreservedAnalyses&nbsp;run(Function &F, FunctionAnalysisManager &){
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//过滤函数
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//过滤掉printf和printf有关的函数,防止在printf中插入printf造成递归(死循环)
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if(F.isDeclaration() || F.getName().starts_with("_") || F.getName().contains("printf")){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;PreservedAnalyses::all();
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;errs() <<&nbsp;"handle func:"&nbsp;<< F.getName() <<&nbsp;"\n";

&nbsp; &nbsp;&nbsp;//获取模块
&nbsp; &nbsp; &nbsp; &nbsp; Module *M = F.getParent();
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//获取模块上下文
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//上下文中包含了数据的类型
&nbsp; &nbsp; &nbsp; &nbsp; LLVMContext &Ctx = M->getContext();

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//下面是创建函数,类比java反射,或者frida的hook
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//定义printf的参数类型,相当于函数括号中的内容,这里的PointerType是指针类型
&nbsp; &nbsp; &nbsp; &nbsp; std::vector<Type*> printfArgs = {PointerType::getUnqual(Ctx)};
&nbsp; &nbsp;&nbsp;//定义函数类型,这里相当于定义:int (void*, ...)
&nbsp; &nbsp; &nbsp; &nbsp; FunctionType *printfType = FunctionType::get(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type::getInt32Ty(Ctx),//函数返回值类型
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printfArgs,//函数的参数类型(vector)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;true//是否是可变参数
&nbsp; &nbsp; &nbsp; &nbsp; );
&nbsp; &nbsp;&nbsp;//如果printf存在则引用,如果不存在,则创建一个新的printf
&nbsp; &nbsp; &nbsp; &nbsp; FunctionCallee printfFunc = M->getOrInsertFunction("printf", printfType);

&nbsp; &nbsp;&nbsp;//下面是插入函数
&nbsp; &nbsp;&nbsp;//将修改的位置定位到要插桩函数的头部
&nbsp; &nbsp; &nbsp; &nbsp; IRBuilder<>&nbsp;builder(&F.getEntryBlock().front());
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//声明全局变量(这里是要传给printf的格式化字符串)
&nbsp; &nbsp; &nbsp; &nbsp; Value* formatStr = builder.CreateGlobalStringPtr(">> enter function %s <<\n",&nbsp;"my_format");
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//声明全局变量,这里定义了函数名称的字符串变量
&nbsp; &nbsp; &nbsp; &nbsp; Value* funcName = builder.CreateGlobalStringPtr(F.getName(),&nbsp;"my_func_name");
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//将上面定义的实际参数传入
&nbsp; &nbsp; &nbsp; &nbsp; std::vector<Value*> printfArgsVec = {formatStr, funcName};
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;//创建函数调用
&nbsp; &nbsp; &nbsp; &nbsp; builder.CreateCall(printfFunc, printfArgsVec);

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;PreservedAnalyses::none();
&nbsp; &nbsp; }
};

}

extern&nbsp;"C"&nbsp;LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo(){
&nbsp; &nbsp;&nbsp;return&nbsp;{
&nbsp; &nbsp; &nbsp; &nbsp; LLVM_PLUGIN_API_VERSION,
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"mypass3",
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;"v0.1",
&nbsp; &nbsp; &nbsp; &nbsp; [](PassBuilder &PB) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PB.registerPipelineParsingCallback(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [](StringRef Name, FunctionPassManager &FPM,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ArrayRef<PassBuilder::PipelineElement>) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if&nbsp;(Name ==&nbsp;"mypass3") {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FPM.addPass(mypass3());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
&nbsp; &nbsp; &nbsp; &nbsp; }};
}

三,Pass的构建

构建LLVM Pass需要写CMakeLists.txt构建声明

1. 配置CMake配置文件

CMakeLists.txt下面的cmake配置可以直接拿去用,我已经标注好需要修改的位置

#cmake&nbsp;版本,可通过 cmake --version 判断
cmake_minimum_required(VERSION&nbsp;4.1.1)&nbsp;#---->修改 cmake版本号
#项目名字
project(mypass3)&nbsp;#---->修改 项目名称

#导入项目的&nbsp;LLVM cmake 配置文件路径(如果根据我之前文章安装这里就相同)
set(LLVM_DIR&nbsp;"D:/LLVM/llvm-project/build/lib/cmake/llvm")#---->修改 llvm cmake配置路径
#寻找&nbsp;LLVM 的包文件
#REQUIRED&nbsp;找不到 LLVM 则停止构建
#强制使用&nbsp;LLVM 安装时生成的配置文件进行定位
find_package(LLVM REQUIRED CONFIG)
#将&nbsp;LLVM 的 CMake 模块路径添加到当前 CMake 搜索路径中,以便后续使用 include(AddLLVM)。
list(APPEND CMAKE_MODULE_PATH&nbsp;"${LLVM_CMAKE_DIR}")

#引入&nbsp;LLVM 提供的专用 CMake 宏
include(AddLLVM)
#将&nbsp;LLVM 的头文件目录(如 llvm/IR/Function.h)加入编译器的搜索路径
include_directories(${LLVM_INCLUDE_DIRS})
#导入&nbsp;LLVM 编译时使用的宏定义
add_definitions(${LLVM_DEFINITIONS})
#设置&nbsp;C++ 标准为 C++17。(这里如果不用17编译会报错)
set(CMAKE_CXX_STANDARD&nbsp;17)
#强制要求必须支持&nbsp;C++17,如果编译器不支持则失败。
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#创建一个模块化的库(.dll)
add_library(mypass3 MODULE mypass3.cpp)&nbsp;#---->修改 项目名称,文件名
#windows不用会报错:导出符号
#LLVM&nbsp;Pass 需要暴露一些特定的入口点(如 getAnalysisUsage)给 opt 工具调用。
set_target_properties(mypass3 PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)&nbsp;#---->修改 项目名称
# 指定该 Pass 需要链接的 LLVM 核心组件。
# LLVMCore: 提供 IR、Function、Module 等核心类。
# LLVMSupport: 提供各种辅助工具类(如 errs() 输出)。
target_link_libraries(mypass3 LLVMCore LLVMSupport)&nbsp;#---->修改 项目名称,文件名
# 为该目标设置特定的编译器选项。
# /utf-8: 告诉 MSVC 编译器使用 UTF-8 编码处理源代码,防止中文注释引起的乱码或编译错误。
target_compile_options(mypass3 PRIVATE /utf-8)#---->修改 项目名称,文件名
2.编译并构建Pass

打开visual studio的工作台,我这里是x64 Native Tools Command Prompt for VS 2022`

进到build目录

#构建项目
#其中-DCMAKE_BUILD_TYPE=RelWithDebInfo不选会报错,由于我之前编译的是带符号的relase版本
cmake -G&nbsp;"Ninja"&nbsp; -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
#编译
ninja

最后出现下面提示,即为编译成功

[2/2] Linking CXX shared module mypass3.dll

四,使用插桩Pass对源码进行插桩

进到build目录

#把.c文件编译为.ll
#-O1 使用O1优化(这里我尝试-O0不优化,会导致我的pass无法应用)
#-Xclang -disable-llvm-passes 不使用默认的pass优化
clang -S -emit-llvm -O1 -Xclang -disable-llvm-passes test.c -S -o test.ll

#使用pass
opt -load-pass-plugin=mypass3.dll -passes=mypass3 &nbsp;test.ll -S -o test_opt.ll

#编译使用pass后的exe
clang test_opt.ll -o test_opt.exe
#编译使用pass前的exe
clang test.ll -o test.exe

输出结果运行test.exe不使用pass,输出结果如下:

hello world!

运行test_opt.exe使用pass后,输出结果如下:

>> enter&nbsp;function&nbsp;main <<
hello world!
>> enter function func_A <<
>> enter function func_B <<
>> enter function func_A <<

我们成功在我们调用函数之前插桩,打印出调用的函数

如果❤喜欢❤本系列教程,就点个关注吧,后续不定期更新~


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:黑与白的边界 黑与白的边界 黑与白的边界《LLVM Pass快速入门(四):代码插桩》

评论:0   参与:  0