11#include " llvm/Passes/PassPlugin.h"
22#include " llvm/Passes/PassBuilder.h"
3- #include " llvm/IR/Constants.h"
43#include " llvm/IR/IRBuilder.h"
5- #include " llvm/IR/GlobalValue.h"
6- #include " llvm/Support/raw_ostream.h"
74
8- namespace {
5+ using namespace llvm ;
96
10- // Module transformation to modify main function
11- class ModuleTransform : PassInfoMixin<ModuleTransform> {
12- public:
13- PreservedAnalyses run (llvm::Module& Module, llvm::ModuleAnalysisManager& AM) {
14- // Debug constant value
15- const uint32_t DEBUG_MAGIC = 48763 ;
16-
17- // Get necessary types
18- auto & Context = Module.getContext ();
19- auto * Int32Type = llvm::Type::getInt32Ty (Context);
20- auto * CharPtrType = llvm::Type::getInt8PtrTy (Context);
21-
22- // Create debug function declaration
23- auto * DebugFuncType = llvm::FunctionType::get (
24- llvm::Type::getVoidTy (Context),
25- {Int32Type},
26- false
27- );
28- auto DebugFunction = Module.getOrInsertFunction (" debug" , DebugFuncType);
29-
30- // Create magic number constant
31- auto * MagicNumber = llvm::ConstantInt::get (Int32Type, DEBUG_MAGIC);
32-
33- // Create string constant
34- auto * HiddenMessage = llvm::ConstantDataArray::getString (
35- Context,
36- " hayaku... motohayaku!" ,
37- true
38- );
39-
40- // Create global variable for the string
41- auto * StringGlobal = new llvm::GlobalVariable (
42- Module,
43- HiddenMessage->getType (),
44- true ,
45- llvm::GlobalValue::PrivateLinkage,
46- HiddenMessage,
47- " .str.hayaku"
48- );
49-
50- // Create GEP indices for string pointer
51- auto * ZeroIndex = llvm::ConstantInt::get (Int32Type, 0 );
52- llvm::Constant* Indices[2 ] = {ZeroIndex, ZeroIndex};
53-
54- // Get pointer to string
55- auto * StringPointer = llvm::ConstantExpr::getGetElementPtr (
56- HiddenMessage->getType (),
57- StringGlobal,
58- Indices
59- );
60-
61- // Find and instrument main function
62- for (auto & Func : Module) {
63- if (Func.getName () != " main" )
64- continue ;
65-
66- llvm::errs () << " Found and instrumenting: " << Func.getName () << " \n " ;
67-
68- // Create builder at entry point
69- llvm::IRBuilder<> Builder (&Func.getEntryBlock ().front ());
70-
71- // Insert debug call
72- Builder.CreateCall (DebugFunction, {MagicNumber});
73-
74- // Get function args
75- auto Args = Func.arg_begin ();
76- auto * ArgCount = Args++;
77- auto * ArgVector = Args;
78-
79- // Modify argv[1] to point to our string
80- auto * SecondArgPtr = Builder.CreateGEP (CharPtrType, ArgVector,
81- llvm::ConstantInt::get (Int32Type, 1 ));
82- Builder.CreateStore (StringPointer, SecondArgPtr);
83-
84- // Replace all uses of argc with our magic value
85- ArgCount->replaceAllUsesWith (MagicNumber);
86- }
87-
88- // Mark all analyses as invalidated
89- return PreservedAnalyses::none ();
90- }
7+ struct LLVMPass : public PassInfoMixin <LLVMPass> {
8+ PreservedAnalyses run (Module &M, ModuleAnalysisManager &MAM);
919};
9210
93- } // anonymous namespace
11+ PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM) {
12+ LLVMContext &Ctx = M.getContext ();
13+ IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
14+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
15+ ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
16+
17+ for (auto &F : M) {
18+ errs () << " func: " << F.getName () << " \n " ;
9419
95- // Plugin registration
96- extern " C " LLVM_ATTRIBUTE_WEAK
97- ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo () {
98- return {
99- LLVM_PLUGIN_API_VERSION,
100- " ModuleInstrumenter " , // plugin name
101- " v1 .0" , // plugin version
102- [](llvm:: PassBuilder &PB) {
20+ }
21+ return PreservedAnalyses::none ();
22+ }
23+
24+ extern " C " ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25+ llvmGetPassPluginInfo () {
26+ return {LLVM_PLUGIN_API_VERSION, " LLVMPass " , " 1 .0" ,
27+ [](PassBuilder &PB) {
10328 PB.registerOptimizerLastEPCallback (
104- [](llvm::ModulePassManager &MPM, llvm::OptimizationLevel Level) {
105- MPM.addPass (ModuleTransform ());
106- }
107- );
108- }
109- };
110- }
29+ [](ModulePassManager &MPM, OptimizationLevel OL) {
30+ MPM.addPass (LLVMPass ());
31+ });
32+ }};
33+ }
34+
0 commit comments