[llvm] [llvm][rtsan] Add transform pass for sanitize_realtime_unsafe (PR #109543)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 3 03:08:06 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:
> Confirming that this also meant that the whole stack trace symbolization was gone, but that was okay.
Yes - thanks - that was indeed my intended meaning.
> I don't understand. To symbolize a PC, we don't need to unwind the stack. From the runtime CLs I saw go buy, it seems like you unwind the stack on error regardless though
That's correct, my mistake. We can discount the runtime cost of unwinding the stack and just concentrate on the functionality/binary size trade-off. After some more thought, we also realised that the symbolised PC approach would make suppression of these errors difficult to implement, because we would no longer have a function name to match against a suppression list. For this reason, we're leaning more strongly towards the existing implmentation and taking the small hit on binary size. I noticed that you approved this PR last night, so I guess you're happy enough with it as it is - please let me know if you have any further concerns. Thanks again for the review 👍
https://github.com/llvm/llvm-project/pull/109543
More information about the llvm-commits
mailing list