[llvm] [LLVM][rtsan] Add RealtimeSanitizer transform pass (PR #101232)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 16:09:29 PDT 2024


https://github.com/cjappl updated https://github.com/llvm/llvm-project/pull/101232

>From 2a3929d05285fa4a79c905820aa5f272a107a24a Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Tue, 30 Jul 2024 12:18:43 -0700
Subject: [PATCH 1/7] [LLVM][rtsan] Add RealtimeSanitizer transform pass

---
 .../Instrumentation/RealtimeSanitizer.h       | 35 +++++++++++++
 .../RealtimeSanitizerOptions.h                | 17 +++++++
 llvm/lib/Passes/PassBuilder.cpp               |  6 +++
 llvm/lib/Passes/PassRegistry.def              |  4 ++
 .../Transforms/Instrumentation/CMakeLists.txt |  1 +
 .../Instrumentation/RealtimeSanitizer.cpp     | 50 +++++++++++++++++++
 .../RealtimeSanitizer/rtsan.ll                | 35 +++++++++++++
 7 files changed, 148 insertions(+)
 create mode 100644 llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
 create mode 100644 llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h
 create mode 100644 llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
 create mode 100644 llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll

diff --git a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
new file mode 100644
index 00000000000000..9cf24483616082
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
@@ -0,0 +1,35 @@
+//===--------- Definition of the RealtimeSanitizer class ---------*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
+#define LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
+
+#include "llvm/IR/PassManager.h"
+#include "llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h"
+
+namespace llvm {
+
+struct RealtimeSanitizerOptions {};
+
+class RealtimeSanitizerPass : public PassInfoMixin<RealtimeSanitizerPass> {
+public:
+  RealtimeSanitizerPass(const RealtimeSanitizerOptions &Options);
+  PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
+
+  static bool isRequired() { return true; }
+
+private:
+  RealtimeSanitizerOptions Options{};
+};
+
+} // namespace llvm
+
+#endif // LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
diff --git a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h
new file mode 100644
index 00000000000000..35376a5647c605
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h
@@ -0,0 +1,17 @@
+//===--------- Definition of the RealtimeSanitizer options -------*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// This file defines data types used to set Realtime Sanitizer options.
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZEROPTIONS_H
+#define LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZEROPTIONS_H
+
+namespace llvm {} // namespace llvm
+
+#endif
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index bcc69d5ac3db67..7bc1c870ce5191 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -199,6 +199,7 @@
 #include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
 #include "llvm/Transforms/Instrumentation/PoisonChecking.h"
+#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
@@ -1210,6 +1211,11 @@ parseRegAllocFastPassOptions(PassBuilder &PB, StringRef Params) {
   return Opts;
 }
 
+Expected<RealtimeSanitizerOptions> parseRtSanPassOptions(StringRef Params) {
+  RealtimeSanitizerOptions Result;
+  return Result;
+}
+
 } // namespace
 
 /// Tests whether a pass name starts with a valid prefix for a default pipeline
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 61a5bab92927fe..95842d15a35bf6 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -592,6 +592,10 @@ FUNCTION_PASS_WITH_PARAMS(
       return WinEHPreparePass(DemoteCatchSwitchPHIOnly);
     },
     parseWinEHPrepareOptions, "demote-catchswitch-only")
+FUNCTION_PASS_WITH_PARAMS(
+    "rtsan", "RealtimeSanitizerPass",
+    [](RealtimeSanitizerOptions Opts) { return RealtimeSanitizerPass(Opts); },
+    parseRtSanPassOptions, "")
 #undef FUNCTION_PASS_WITH_PARAMS
 
 #ifndef LOOPNEST_PASS
diff --git a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
index 4e3f9e27e0c344..deab37801ff1df 100644
--- a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
+++ b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
@@ -25,6 +25,7 @@ add_llvm_component_library(LLVMInstrumentation
   ValueProfileCollector.cpp
   ThreadSanitizer.cpp
   HWAddressSanitizer.cpp
+  RealtimeSanitizer.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms
diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
new file mode 100644
index 00000000000000..2fa2389c4984fe
--- /dev/null
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -0,0 +1,50 @@
+#include "llvm/IR/Analysis.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Module.h"
+
+#include "llvm/Transforms/Instrumentation/RealtimeSanitizer.h"
+
+using namespace llvm;
+
+namespace {
+
+void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
+                                 const char *FunctionName) {
+  LLVMContext &Context = Fn.getContext();
+  FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context), false);
+  FunctionCallee Func =
+      Fn.getParent()->getOrInsertFunction(FunctionName, FuncType);
+  IRBuilder<> Builder{&Instruction};
+  Builder.CreateCall(Func, {});
+}
+
+void insertCallAtFunctionEntryPoint(Function &Fn, const char *InsertFnName) {
+
+  insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName);
+}
+
+void insertCallAtAllFunctionExitPoints(Function &Fn, const char *InsertFnName) {
+  for (auto &BB : Fn) {
+    for (auto &I : BB) {
+      if (auto *RI = dyn_cast<ReturnInst>(&I)) {
+        insertCallBeforeInstruction(Fn, I, InsertFnName);
+      }
+    }
+  }
+}
+} // namespace
+
+RealtimeSanitizerPass::RealtimeSanitizerPass(
+    const RealtimeSanitizerOptions &Options)
+    : Options{Options} {}
+
+PreservedAnalyses RealtimeSanitizerPass::run(Function &F,
+                                             AnalysisManager<Function> &AM) {
+  if (F.hasFnAttribute(Attribute::NonBlocking)) {
+    insertCallAtFunctionEntryPoint(F, "__rtsan_realtime_enter");
+    insertCallAtAllFunctionExitPoints(F, "__rtsan_realtime_exit");
+    return PreservedAnalyses::none();
+  }
+
+  return PreservedAnalyses::all();
+}
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
new file mode 100644
index 00000000000000..222df484273c7b
--- /dev/null
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
@@ -0,0 +1,35 @@
+; RUN: opt < %s -passes=rtsan -S | FileCheck %s
+
+; Function Attrs: mustprogress noinline nonblocking optnone ssp uwtable(sync)
+define void @violation() #0 {
+  %1 = alloca ptr, align 8
+  %2 = call ptr @malloc(i64 noundef 2) #3
+  store ptr %2, ptr %1, align 8
+  ret void
+}
+
+; Function Attrs: allocsize(0)
+declare ptr @malloc(i64 noundef) #1
+
+; Function Attrs: mustprogress noinline norecurse optnone ssp uwtable(sync)
+define noundef i32 @main() #2 {
+  %1 = alloca i32, align 4
+  store i32 0, ptr %1, align 4
+  call void @violation() #4
+  ret i32 0
+}
+
+attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
+attributes #1 = { allocsize(0) }
+attributes #2 = { mustprogress noinline norecurse optnone ssp uwtable(sync) }
+attributes #3 = { allocsize(0) }
+attributes #4 = { nonblocking }
+
+; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
+; CHECK: define{{.*}}violation
+; CHECK-NEXT: call{{.*}}__rtsan_realtime_enter
+
+; RealtimeSanitizer pass should insert __rtsan_realtime_exit right before function return
+; CHECK: call{{.*}}@__rtsan_realtime_exit
+; CHECK-NEXT: ret{{.*}}void
+

>From 6c67a4b4322ad28f1ae831352c481cc6d4b7d9c6 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 31 Jul 2024 13:26:05 -0700
Subject: [PATCH 2/7] [PR] Add multi-return IR test

---
 .../RealtimeSanitizer/rtsan_multi_return.ll   | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll

diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
new file mode 100644
index 00000000000000..bbba7996fdcd49
--- /dev/null
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
@@ -0,0 +1,29 @@
+; RUN: opt < %s -passes=rtsan -S | FileCheck %s
+
+define i32 @example(i32 %x) #0 {
+entry:
+    %retval = alloca i32
+    %cmp = icmp sgt i32 %x, 10
+    br i1 %cmp, label %then, label %else
+
+then:
+    ret i32 1
+
+else:
+    ret i32 0
+}
+
+attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
+
+; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
+; CHECK: entry:
+; CHECK-NEXT: call{{.*}}__rtsan_realtime_enter
+
+; RealtimeSanitizer pass should insert the call at both function returns
+; CHECK: then:
+; CHECK-NEXT: call{{.*}}@__rtsan_realtime_exit
+; CHECK-NEXT: ret i32 1
+
+; CHECK: else:
+; CHECK-NEXT: call{{.*}}@__rtsan_realtime_exit
+; CHECK-NEXT: ret i32 0

>From 44ee4bdb88050710a505e1120f2c22aadb1273d4 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 31 Jul 2024 13:28:46 -0700
Subject: [PATCH 3/7] [PR] Ditch anonymous namespace

---
 .../Instrumentation/RealtimeSanitizer.cpp           | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
index 2fa2389c4984fe..df4d12bcf2f5c7 100644
--- a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -6,10 +6,8 @@
 
 using namespace llvm;
 
-namespace {
-
-void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
-                                 const char *FunctionName) {
+static void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
+                                        const char *FunctionName) {
   LLVMContext &Context = Fn.getContext();
   FunctionType *FuncType = FunctionType::get(Type::getVoidTy(Context), false);
   FunctionCallee Func =
@@ -18,12 +16,14 @@ void insertCallBeforeInstruction(Function &Fn, Instruction &Instruction,
   Builder.CreateCall(Func, {});
 }
 
-void insertCallAtFunctionEntryPoint(Function &Fn, const char *InsertFnName) {
+static void insertCallAtFunctionEntryPoint(Function &Fn,
+                                           const char *InsertFnName) {
 
   insertCallBeforeInstruction(Fn, Fn.front().front(), InsertFnName);
 }
 
-void insertCallAtAllFunctionExitPoints(Function &Fn, const char *InsertFnName) {
+static void insertCallAtAllFunctionExitPoints(Function &Fn,
+                                              const char *InsertFnName) {
   for (auto &BB : Fn) {
     for (auto &I : BB) {
       if (auto *RI = dyn_cast<ReturnInst>(&I)) {
@@ -32,7 +32,6 @@ void insertCallAtAllFunctionExitPoints(Function &Fn, const char *InsertFnName) {
     }
   }
 }
-} // namespace
 
 RealtimeSanitizerPass::RealtimeSanitizerPass(
     const RealtimeSanitizerOptions &Options)

>From 8faf8c49e63cfc7b97377e946e3e6965d1faee53 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 31 Jul 2024 14:08:14 -0700
Subject: [PATCH 4/7] [PR] MaskRay feedback

---
 .../Instrumentation/RealtimeSanitizer.h         |  3 +--
 .../Instrumentation/RealtimeSanitizerOptions.h  | 17 -----------------
 .../Instrumentation/RealtimeSanitizer/rtsan.ll  | 12 ++----------
 .../RealtimeSanitizer/rtsan_multi_return.ll     |  9 +++++----
 4 files changed, 8 insertions(+), 33 deletions(-)
 delete mode 100644 llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h

diff --git a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
index 9cf24483616082..1740cc0a3bcda7 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
@@ -1,4 +1,4 @@
-//===--------- Definition of the RealtimeSanitizer class ---------*- C++
+//=- RealtimeSanitizer.h - RealtimeSanitizer instrumentation ----*- C++ -*-==//
 //-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -13,7 +13,6 @@
 #define LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
 
 #include "llvm/IR/PassManager.h"
-#include "llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h"
 
 namespace llvm {
 
diff --git a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h
deleted file mode 100644
index 35376a5647c605..00000000000000
--- a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizerOptions.h
+++ /dev/null
@@ -1,17 +0,0 @@
-//===--------- Definition of the RealtimeSanitizer options -------*- C++
-//-*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-// This file defines data types used to set Realtime Sanitizer options.
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZEROPTIONS_H
-#define LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZEROPTIONS_H
-
-namespace llvm {} // namespace llvm
-
-#endif
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
index 222df484273c7b..1e91e1573b79eb 100644
--- a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
@@ -1,6 +1,5 @@
 ; RUN: opt < %s -passes=rtsan -S | FileCheck %s
 
-; Function Attrs: mustprogress noinline nonblocking optnone ssp uwtable(sync)
 define void @violation() #0 {
   %1 = alloca ptr, align 8
   %2 = call ptr @malloc(i64 noundef 2) #3
@@ -8,10 +7,8 @@ define void @violation() #0 {
   ret void
 }
 
-; Function Attrs: allocsize(0)
 declare ptr @malloc(i64 noundef) #1
 
-; Function Attrs: mustprogress noinline norecurse optnone ssp uwtable(sync)
 define noundef i32 @main() #2 {
   %1 = alloca i32, align 4
   store i32 0, ptr %1, align 4
@@ -20,16 +17,11 @@ define noundef i32 @main() #2 {
 }
 
 attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
-attributes #1 = { allocsize(0) }
-attributes #2 = { mustprogress noinline norecurse optnone ssp uwtable(sync) }
-attributes #3 = { allocsize(0) }
-attributes #4 = { nonblocking }
 
 ; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
-; CHECK: define{{.*}}violation
-; CHECK-NEXT: call{{.*}}__rtsan_realtime_enter
+; CHECK-LABEL: @violation()
+; CHECK-NEXT: call{{.*}}@__rtsan_realtime_enter
 
 ; RealtimeSanitizer pass should insert __rtsan_realtime_exit right before function return
 ; CHECK: call{{.*}}@__rtsan_realtime_exit
 ; CHECK-NEXT: ret{{.*}}void
-
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
index bbba7996fdcd49..724d42401ba6dd 100644
--- a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
@@ -16,14 +16,15 @@ else:
 attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
 
 ; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
-; CHECK: entry:
-; CHECK-NEXT: call{{.*}}__rtsan_realtime_enter
+; CHECK-LABEL: @example(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: call{{.*}}@__rtsan_realtime_enter
 
 ; RealtimeSanitizer pass should insert the call at both function returns
-; CHECK: then:
+; CHECK-LABEL: then:
 ; CHECK-NEXT: call{{.*}}@__rtsan_realtime_exit
 ; CHECK-NEXT: ret i32 1
 
-; CHECK: else:
+; CHECK-LABEL: else:
 ; CHECK-NEXT: call{{.*}}@__rtsan_realtime_exit
 ; CHECK-NEXT: ret i32 0

>From 40f876977da7b6fbf16817c513ee9698e214b6a3 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Wed, 31 Jul 2024 14:47:53 -0700
Subject: [PATCH 5/7] [PR] MaskRay feedback 2

---
 .../Transforms/Instrumentation/RealtimeSanitizer.h    | 11 +++++++++--
 .../Transforms/Instrumentation/RealtimeSanitizer.cpp  |  9 +++------
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
index 1740cc0a3bcda7..b141ad27996b7b 100644
--- a/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
+++ b/llvm/include/llvm/Transforms/Instrumentation/RealtimeSanitizer.h
@@ -1,5 +1,4 @@
-//=- RealtimeSanitizer.h - RealtimeSanitizer instrumentation ----*- C++ -*-==//
-//-*-===//
+//===- RealtimeSanitizer.h - RealtimeSanitizer instrumentation --*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,6 +6,14 @@
 //
 //===----------------------------------------------------------------------===//
 //
+// This file is a part of the RealtimeSanitizer, an LLVM transformation for
+// detecting and reporting realtime safety violations.
+//
+// The instrumentation pass inserts calls to __rtsan_realtime_enter and
+// __rtsan_realtime_exit at the entry and exit points of functions that are
+// marked with the appropriate attribute.
+//
+// See also: llvm-project/compiler-rt/lib/rtsan/
 //
 //===----------------------------------------------------------------------===//
 #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_REALTIMESANITIZER_H
diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
index df4d12bcf2f5c7..739e9d7037a33b 100644
--- a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -24,13 +24,10 @@ static void insertCallAtFunctionEntryPoint(Function &Fn,
 
 static void insertCallAtAllFunctionExitPoints(Function &Fn,
                                               const char *InsertFnName) {
-  for (auto &BB : Fn) {
-    for (auto &I : BB) {
-      if (auto *RI = dyn_cast<ReturnInst>(&I)) {
+  for (auto &BB : Fn)
+    for (auto &I : BB)
+      if (auto *RI = dyn_cast<ReturnInst>(&I))
         insertCallBeforeInstruction(Fn, I, InsertFnName);
-      }
-    }
-  }
 }
 
 RealtimeSanitizerPass::RealtimeSanitizerPass(

>From 6be30bbb69db1668e7181d10266c567c859fb81b Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 8 Aug 2024 07:03:13 -0700
Subject: [PATCH 6/7] Update attribute names to sanitize_realtime

---
 llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp       | 2 +-
 llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll            | 2 +-
 .../Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
index 739e9d7037a33b..d48a536bb1ef34 100644
--- a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -36,7 +36,7 @@ RealtimeSanitizerPass::RealtimeSanitizerPass(
 
 PreservedAnalyses RealtimeSanitizerPass::run(Function &F,
                                              AnalysisManager<Function> &AM) {
-  if (F.hasFnAttribute(Attribute::NonBlocking)) {
+  if (F.hasFnAttribute(Attribute::SanitizeRealtime)) {
     insertCallAtFunctionEntryPoint(F, "__rtsan_realtime_enter");
     insertCallAtAllFunctionExitPoints(F, "__rtsan_realtime_exit");
     return PreservedAnalyses::none();
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
index 1e91e1573b79eb..a0bc4aef2cc319 100644
--- a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan.ll
@@ -16,7 +16,7 @@ define noundef i32 @main() #2 {
   ret i32 0
 }
 
-attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
+attributes #0 = { mustprogress noinline sanitize_realtime optnone ssp uwtable(sync) }
 
 ; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
 ; CHECK-LABEL: @violation()
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
index 724d42401ba6dd..39a1ff0b7c442a 100644
--- a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_multi_return.ll
@@ -13,7 +13,7 @@ else:
     ret i32 0
 }
 
-attributes #0 = { mustprogress noinline nonblocking optnone ssp uwtable(sync) }
+attributes #0 = { mustprogress noinline sanitize_realtime optnone ssp uwtable(sync) }
 
 ; RealtimeSanitizer pass should insert __rtsan_realtime_enter right after function definition
 ; CHECK-LABEL: @example(

>From 06ce2d22f3d5e8c91029857542cf75ed5550ff40 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 8 Aug 2024 16:09:04 -0700
Subject: [PATCH 7/7] Add file header for RealtimeSanitizer.cpp

---
 .../Instrumentation/RealtimeSanitizer.cpp         | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
index d48a536bb1ef34..d3028ee97c3eb8 100644
--- a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -1,3 +1,18 @@
+//===- RealtimeSanitizer.cpp - RealtimeSanitizer instrumentation *- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of the RealtimeSanitizer, an LLVM transformation for
+// detecting and reporting realtime safety violations.
+//
+// See also: llvm-project/compiler-rt/lib/rtsan/
+//
+//===----------------------------------------------------------------------===//
+
 #include "llvm/IR/Analysis.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Module.h"



More information about the llvm-commits mailing list