|
15 | 15 |
|
16 | 16 | #include "llvm/IR/Analysis.h"
|
17 | 17 | #include "llvm/IR/IRBuilder.h"
|
| 18 | +#include "llvm/IR/InstIterator.h" |
18 | 19 | #include "llvm/IR/Module.h"
|
19 | 20 |
|
| 21 | +#include "llvm/Demangle/Demangle.h" |
20 | 22 | #include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
|
21 | 23 |
|
22 | 24 | using namespace llvm;
|
23 | 25 |
|
| 26 | +static SmallVector<Type *> getArgTypes(ArrayRef<Value *> FunctionArgs) { |
| 27 | + SmallVector<Type *> Types; |
| 28 | + for (Value *Arg : FunctionArgs) |
| 29 | + Types.push_back(Arg->getType()); |
| 30 | + return Types; |
| 31 | +} |
| 32 | + |
24 | 33 | static void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
|
25 |
| - const char *FunctionName) { |
| 34 | + const char *FunctionName, |
| 35 | + ArrayRef<Value *> FunctionArgs) { |
26 | 36 | LLVMContext &Context = Fn.getContext();
|
27 |
| - FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context), false); |
| 37 | + FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context), |
| 38 | + getArgTypes(FunctionArgs), false); |
28 | 39 | FunctionCallee Func =
|
29 | 40 | Fn.getParent()->getOrInsertFunction(FunctionName, FuncType);
|
30 | 41 | IRBuilder<> Builder{&Instruction};
|
31 |
| - Builder.CreateCall(Func, {}); |
| 42 | + Builder.CreateCall(Func, FunctionArgs); |
32 | 43 | }
|
33 | 44 |
|
34 | 45 | static void insertCallAtFunctionEntryPoint(Function &Fn,
|
35 |
| - const char *InsertFnName) { |
36 |
| - |
37 |
| - insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName); |
| 46 | + const char *InsertFnName, |
| 47 | + ArrayRef<Value *> FunctionArgs) { |
| 48 | + insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName, |
| 49 | + FunctionArgs); |
38 | 50 | }
|
39 | 51 |
|
40 | 52 | static void insertCallAtAllFunctionExitPoints(Function &Fn,
|
41 |
| - const char *InsertFnName) { |
42 |
| - for (auto &BB : Fn) |
43 |
| - for (auto &I : BB) |
44 |
| - if (isa<ReturnInst>(&I)) |
45 |
| - insertCallBeforeInstruction(Fn, I, InsertFnName); |
| 53 | + const char *InsertFnName, |
| 54 | + ArrayRef<Value *> FunctionArgs) { |
| 55 | + for (auto &I : instructions(Fn)) |
| 56 | + if (isa<ReturnInst>(&I)) |
| 57 | + insertCallBeforeInstruction(Fn, I, InsertFnName, FunctionArgs); |
| 58 | +} |
| 59 | + |
| 60 | +static PreservedAnalyses rtsanPreservedCFGAnalyses() { |
| 61 | + PreservedAnalyses PA; |
| 62 | + PA.preserveSet<CFGAnalyses>(); |
| 63 | + return PA; |
| 64 | +} |
| 65 | + |
| 66 | +static PreservedAnalyses runSanitizeRealtime(Function &Fn) { |
| 67 | + insertCallAtFunctionEntryPoint(Fn, "__rtsan_realtime_enter", {}); |
| 68 | + insertCallAtAllFunctionExitPoints(Fn, "__rtsan_realtime_exit", {}); |
| 69 | + return rtsanPreservedCFGAnalyses(); |
| 70 | +} |
| 71 | + |
| 72 | +static PreservedAnalyses runSanitizeRealtimeUnsafe(Function &Fn) { |
| 73 | + IRBuilder<> Builder(&Fn.front().front()); |
| 74 | + Value *Name = Builder.CreateGlobalString(demangle(Fn.getName())); |
| 75 | + insertCallAtFunctionEntryPoint(Fn, "__rtsan_notify_blocking_call", {Name}); |
| 76 | + return rtsanPreservedCFGAnalyses(); |
46 | 77 | }
|
47 | 78 |
|
48 | 79 | RealtimeSanitizerPass::RealtimeSanitizerPass(
|
49 | 80 | const RealtimeSanitizerOptions &Options) {}
|
50 | 81 |
|
51 |
| -PreservedAnalyses RealtimeSanitizerPass::run(Function &F, |
| 82 | +PreservedAnalyses RealtimeSanitizerPass::run(Function &Fn, |
52 | 83 | AnalysisManager<Function> &AM) {
|
53 |
| - if (F.hasFnAttribute(Attribute::SanitizeRealtime)) { |
54 |
| - insertCallAtFunctionEntryPoint(F, "__rtsan_realtime_enter"); |
55 |
| - insertCallAtAllFunctionExitPoints(F, "__rtsan_realtime_exit"); |
56 |
| - |
57 |
| - PreservedAnalyses PA; |
58 |
| - PA.preserveSet<CFGAnalyses>(); |
59 |
| - return PA; |
60 |
| - } |
| 84 | + if (Fn.hasFnAttribute(Attribute::SanitizeRealtime)) |
| 85 | + return runSanitizeRealtime(Fn); |
| 86 | + |
| 87 | + if (Fn.hasFnAttribute(Attribute::SanitizeRealtimeUnsafe)) |
| 88 | + return runSanitizeRealtimeUnsafe(Fn); |
61 | 89 |
|
62 | 90 | return PreservedAnalyses::all();
|
63 | 91 | }
|
0 commit comments