Skip to content

Commit 3353988

Browse files
Implement sanitize_realtime_unsafe Pass
1 parent 6032fee commit 3353988

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ static void insertCallAtAllFunctionExitPoints(Function &Fn,
4545
insertCallBeforeInstruction(Fn, I, InsertFnName);
4646
}
4747

48+
static PreservedAnalyses rtsanPreservedAnalyses() {
49+
PreservedAnalyses PA;
50+
PA.preserveSet<CFGAnalyses>();
51+
return PA;
52+
}
53+
54+
static void transformRealtimeUnsafeFunction(Function &F) {
55+
IRBuilder<> Builder(&F.front().front());
56+
Value *NameArg = Builder.CreateGlobalString(F.getName());
57+
58+
FunctionType *FuncType =
59+
FunctionType::get(Type::getVoidTy(F.getContext()),
60+
{PointerType::getUnqual(F.getContext())}, false);
61+
62+
FunctionCallee Func = F.getParent()->getOrInsertFunction(
63+
"__rtsan_expect_not_realtime", FuncType);
64+
65+
Builder.CreateCall(Func, {NameArg});
66+
}
67+
4868
RealtimeSanitizerPass::RealtimeSanitizerPass(
4969
const RealtimeSanitizerOptions &Options) {}
5070

@@ -53,10 +73,12 @@ PreservedAnalyses RealtimeSanitizerPass::run(Function &F,
5373
if (F.hasFnAttribute(Attribute::SanitizeRealtime)) {
5474
insertCallAtFunctionEntryPoint(F, "__rtsan_realtime_enter");
5575
insertCallAtAllFunctionExitPoints(F, "__rtsan_realtime_exit");
76+
return rtsanPreservedAnalyses();
77+
}
5678

57-
PreservedAnalyses PA;
58-
PA.preserveSet<CFGAnalyses>();
59-
return PA;
79+
if (F.hasFnAttribute(Attribute::SanitizeRealtimeUnsafe)) {
80+
transformRealtimeUnsafeFunction(F);
81+
return rtsanPreservedAnalyses();
6082
}
6183

6284
return PreservedAnalyses::all();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: opt < %s -passes=rtsan -S | FileCheck %s
2+
3+
define void @blocking_function() #0 {
4+
ret void
5+
}
6+
7+
define noundef i32 @main() #2 {
8+
call void @blocking_function() #4
9+
ret i32 0
10+
}
11+
12+
attributes #0 = { mustprogress noinline sanitize_realtime_unsafe optnone ssp uwtable(sync) }
13+
14+
; RealtimeSanitizer pass should insert __rtsan_expect_not_realtime at function entrypoint
15+
; CHECK-LABEL: @blocking_function()
16+
; CHECK-NEXT: call{{.*}}@__rtsan_expect_not_realtime({{ptr .*}})

0 commit comments

Comments
 (0)