Skip to content

Conversation

@davidtrevelyan
Copy link
Contributor

@davidtrevelyan davidtrevelyan commented Nov 25, 2025

Fixes #169377.

Previously, the RealtimeSanitizer pass only handled attributed function definitions in IR, and we have recently found that attributed function declarations caused it to crash. To fix the issue, we must check whether the IR function is empty before attempting to do any manipulation of its instructions.

This PR:

  • Adds checks for whether IR Functions are empty() in each relevant at the top-level RTSan pass routine
  • Removes the utility function rtsanPreservedCFGAnalyses from the pass, whose result was unused and which would otherwise have complicated the fix

@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (davidtrevelyan)

Changes

Addresses #169377.

Previously, the RealtimeSanitizer pass only handled attributed function definitions in IR, and attributed function declarations caused it to crash. To fix the issue, we must check whether the IR function is empty before attempting to do any manipulation of its instructions.

This PR:

  • Adds checks for whether IR Functions are empty() in each relevant RTSan pass routine
  • Removes the utility function rtsanPreservedCFGAnalyses from the pass, whose result was unused and which would otherwise have complicated the fix

Full diff: https://github.com/llvm/llvm-project/pull/169577.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp (+7-9)
  • (added) llvm/test/Instrumentation/RealtimeSanitizer/rtsan_attrib_declare.ll (+12)
diff --git a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
index 5ef6ffb58a7c1..adb0e518c6074 100644
--- a/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp
@@ -61,23 +61,21 @@ static void insertCallAtAllFunctionExitPoints(Function &Fn,
       insertCallBeforeInstruction(Fn, I, InsertFnName, FunctionArgs);
 }
 
-static PreservedAnalyses rtsanPreservedCFGAnalyses() {
-  PreservedAnalyses PA;
-  PA.preserveSet<CFGAnalyses>();
-  return PA;
-}
+static void runSanitizeRealtime(Function &Fn) {
+  if (Fn.empty())
+    return;
 
-static PreservedAnalyses runSanitizeRealtime(Function &Fn) {
   insertCallAtFunctionEntryPoint(Fn, "__rtsan_realtime_enter", {});
   insertCallAtAllFunctionExitPoints(Fn, "__rtsan_realtime_exit", {});
-  return rtsanPreservedCFGAnalyses();
 }
 
-static PreservedAnalyses runSanitizeRealtimeBlocking(Function &Fn) {
+static void runSanitizeRealtimeBlocking(Function &Fn) {
+  if (Fn.empty())
+    return;
+
   IRBuilder<> Builder(&Fn.front().front());
   Value *Name = Builder.CreateGlobalString(demangle(Fn.getName()));
   insertCallAtFunctionEntryPoint(Fn, "__rtsan_notify_blocking_call", {Name});
-  return rtsanPreservedCFGAnalyses();
 }
 
 PreservedAnalyses RealtimeSanitizerPass::run(Module &M,
diff --git a/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_attrib_declare.ll b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_attrib_declare.ll
new file mode 100644
index 0000000000000..a5336b9e9e491
--- /dev/null
+++ b/llvm/test/Instrumentation/RealtimeSanitizer/rtsan_attrib_declare.ll
@@ -0,0 +1,12 @@
+; RUN: opt < %s -passes='rtsan' -S | FileCheck %s
+
+declare void @declared_realtime_function() sanitize_realtime #0
+
+declare void @declared_blocking_function() sanitize_realtime_blocking #0
+
+; RealtimeSanitizer pass should ignore attributed functions that are just declarations
+; CHECK: declared_realtime_function
+; CHECK-EMPTY:
+; CHECK: declared_blocking_function
+; CHECK-EMPTY:
+

Copy link
Contributor

@cjappl cjappl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable enough, just have some questions before I approve

if (Fn.empty())
return;

static PreservedAnalyses runSanitizeRealtime(Function &Fn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not returning these preserved analyses going to bite us elsewhere? I remember most of the sanitizer transform passes (or transform passes generally) returning these.

Copy link
Contributor Author

@davidtrevelyan davidtrevelyan Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps! But the issue is we're already not returning these preserved analyses - they're sadly discarded at the moment. I would advocate for this NFC because it makes the current behaviour explicit - even if it's not correct. I bundled this change in with this PR because it made the implementation of the checking a bit easier, but I'd be happy to submit this in a separate preparatory PR first - if that's easier to follow - just let me know.

}
static void runSanitizeRealtime(Function &Fn) {
if (Fn.empty())
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there precedence in other sanitizer passes for doing this? (Curious how they solved this problem)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the good idea. I hadn't looked before drafting this PR, but it appears like ASan solves it a similar way at one level higher - at the top level of the pass's run method:

ThreadSanitizer has a bool check for whether it should sanitizeFunction, which returns false if there are no calls in it. MemorySanitizer does the same thing as AddressSanitizer with a continue at the top level if the function is .empty().

If you prefer the ASan/MSan semantics I'm very happy to switch to it 👍

@inkreasing
Copy link

Looks good! doesn't crash anymore and the rtsan tests i added to rustc also still pass.

@zwuis
Copy link
Contributor

zwuis commented Nov 26, 2025

Addresses #169377.

You can use this format.

Copy link
Contributor

@cjappl cjappl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved as written! I lean slightly to breaking the NFC into another commit but not strongly enough to block based on it

Thanks for investigating the other sanitizers. Looks like there is good precedence for this type of check

Addresses llvm#169377.

Previously, the RealtimeSanitizer pass only handled attributed function
definitions in IR, and attributed function declarations caused it to
crash. To fix the issue, we must check whether the IR function is empty
before attempting to do any manipulation of its instructions.
@davidtrevelyan davidtrevelyan force-pushed the rtsan/handle-attributed-ir-declarations branch from 36ac559 to 0ca2359 Compare December 1, 2025 09:47
@davidtrevelyan
Copy link
Contributor Author

Approved as written! I lean slightly to breaking the NFC into another commit but not strongly enough to block based on it

Thanks for investigating the other sanitizers. Looks like there is good precedence for this type of check

@cjappl agreed on breaking the NFC into another commit. I've amended this branch to only do enough to fix the bug, adding just the check for the empty function, and we'll tackle the preserved analyses in a separate PR. I think it warrants a little more thought. Thanks for the suggestion!

@davidtrevelyan davidtrevelyan requested a review from cjappl December 1, 2025 10:50
Copy link
Contributor

@cjappl cjappl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks

@davidtrevelyan davidtrevelyan merged commit 5d4c441 into llvm:main Dec 1, 2025
10 checks passed
@davidtrevelyan davidtrevelyan deleted the rtsan/handle-attributed-ir-declarations branch December 1, 2025 20:57
@davidtrevelyan davidtrevelyan added this to the LLVM 21.x Release milestone Dec 4, 2025
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in LLVM Release Status Dec 4, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Dec 4, 2025
@davidtrevelyan
Copy link
Contributor Author

/cherry-pick 5d4c441

@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2025

/pull-request #170641

kcloudy0717 pushed a commit to kcloudy0717/llvm-project that referenced this pull request Dec 4, 2025
Addresses llvm#169377.

Previously, the RealtimeSanitizer pass only handled attributed function
_definitions_ in IR, and we have recently found that attributed function
_declarations_ caused it to crash. To fix the issue, we must check
whether the IR function is empty before attempting to do any
manipulation of its instructions.

This PR:

- Adds checks for whether IR `Function`s are `empty()` ~~in each
relevant~~ at the top-level RTSan pass routine
- ~~Removes the utility function `rtsanPreservedCFGAnalyses` from the
pass, whose result was unused and which would otherwise have complicated
the fix~~
honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Dec 9, 2025
Addresses llvm#169377.

Previously, the RealtimeSanitizer pass only handled attributed function
_definitions_ in IR, and we have recently found that attributed function
_declarations_ caused it to crash. To fix the issue, we must check
whether the IR function is empty before attempting to do any
manipulation of its instructions.

This PR:

- Adds checks for whether IR `Function`s are `empty()` ~~in each
relevant~~ at the top-level RTSan pass routine
- ~~Removes the utility function `rtsanPreservedCFGAnalyses` from the
pass, whose result was unused and which would otherwise have complicated
the fix~~
dyung pushed a commit that referenced this pull request Dec 10, 2025
Addresses #169377.

Previously, the RealtimeSanitizer pass only handled attributed function
_definitions_ in IR, and we have recently found that attributed function
_declarations_ caused it to crash. To fix the issue, we must check
whether the IR function is empty before attempting to do any
manipulation of its instructions.

This PR:

- Adds checks for whether IR `Function`s are `empty()` ~~in each
relevant~~ at the top-level RTSan pass routine
- ~~Removes the utility function `rtsanPreservedCFGAnalyses` from the
pass, whose result was unused and which would otherwise have complicated
the fix~~

(cherry picked from commit 5d4c441)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

rtsan pass triggers assertion failure

5 participants