[llvm] [llvm][rtsan] Add transform pass for sanitize_realtime_unsafe (PR #109543)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 25 14:43:33 PDT 2024
================
@@ -17,47 +17,75 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
+#include "llvm/Demangle/Demangle.h"
#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
using namespace llvm;
+static SmallVector<Type *> getArgTypes(ArrayRef<Value *> FunctionArgs) {
+ SmallVector<Type *> Types;
+ for (Value *Arg : FunctionArgs)
+ Types.push_back(Arg->getType());
+ return Types;
+}
+
static void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
- const char *FunctionName) {
+ const char *FunctionName,
+ ArrayRef<Value *> FunctionArgs) {
LLVMContext &Context = Fn.getContext();
- FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context), false);
+ FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context),
+ getArgTypes(FunctionArgs), false);
FunctionCallee Func =
Fn.getParent()->getOrInsertFunction(FunctionName, FuncType);
IRBuilder<> Builder{&Instruction};
- Builder.CreateCall(Func, {});
+ Builder.CreateCall(Func, FunctionArgs);
}
static void insertCallAtFunctionEntryPoint(Function &Fn,
- const char *InsertFnName) {
-
- insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName);
+ const char *InsertFnName,
+ ArrayRef<Value *> FunctionArgs) {
+ insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName,
+ FunctionArgs);
}
static void insertCallAtAllFunctionExitPoints(Function &Fn,
- const char *InsertFnName) {
+ const char *InsertFnName,
+ ArrayRef<Value *> FunctionArgs) {
for (auto &BB : Fn)
for (auto &I : BB)
if (isa<ReturnInst>(&I))
- insertCallBeforeInstruction(Fn, I, InsertFnName);
+ insertCallBeforeInstruction(Fn, I, InsertFnName, FunctionArgs);
+}
+
+static PreservedAnalyses rtsanPreservedCFGAnalyses() {
+ PreservedAnalyses PA;
+ PA.preserveSet<CFGAnalyses>();
+ return PA;
+}
+
+static PreservedAnalyses runSanitizeRealtime(Function &Fn) {
+ insertCallAtFunctionEntryPoint(Fn, "__rtsan_realtime_enter", {});
+ insertCallAtAllFunctionExitPoints(Fn, "__rtsan_realtime_exit", {});
+ return rtsanPreservedCFGAnalyses();
+}
+
+static PreservedAnalyses runSanitizeRealtimeUnsafe(Function &Fn) {
+ IRBuilder<> Builder(&Fn.front().front());
+ Value *Name = Builder.CreateGlobalString(demangle(Fn.getName()));
----------------
davidtrevelyan wrote:
That's a really great question. In all honesty I didn't originally think about the possibility of de-mangling during the report stage. After thinking about it further, I have an idea on which one will probably be less work overall, but I'd need advice on whether the distribution of work is appropriate.
Here's how I see the work involved:
- for demangling in the pass:
- no run-time work: demangles once at compile time, but
- could increase compile-time work (marginally?)
- for demangling in the reporting:
- adds to run-time work: demangles every time an error is registered (which could be many times if `halt_on_error` is off)
- won't add to compile-time work
My very naive intuition is that demangling in the pass is preferable, but very happy to move it to the reporting if I have my priorities a bit upside down. Thanks in advance for any suggestions here.
https://github.com/llvm/llvm-project/pull/109543
More information about the llvm-commits
mailing list