[compiler-rt] 44d9bee - [rtsan][test] Prevent test check from being optimized out in LTO builds (#122524)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 11:55:25 PST 2025


Author: Paul Kirth
Date: 2025-01-14T11:55:19-08:00
New Revision: 44d9beef7d28f4a4d73acb12ea030bb642eff1d2

URL: https://github.com/llvm/llvm-project/commit/44d9beef7d28f4a4d73acb12ea030bb642eff1d2
DIFF: https://github.com/llvm/llvm-project/commit/44d9beef7d28f4a4d73acb12ea030bb642eff1d2.diff

LOG: [rtsan][test] Prevent test check from being optimized out in LTO builds (#122524)

In LTO builds, some test checks can be optimized away, since the
compiler can
see through the memory accesses after inlining across TUs. This causes
the existing death tests to fail, since the functions are completely
optimized out and things like copying a lambda will no longer occur and
trigger the sanitizer.

To prevent that, we can use an empty inline assembly block to tell the
compiler that memory is modified, and prevent it from doing that.

Added: 
    

Modified: 
    compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
index ef11b71f167e1b..e05d7ae78b5d99 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
@@ -145,13 +145,23 @@ TEST(TestRtsan, LaunchingAThreadDiesWhenRealtime) {
 
 namespace {
 void InvokeStdFunction(std::function<void()> &&function) { function(); }
+
+template <typename T> void HideMemoryFromCompiler(T *memory) {
+  // Pass the pointer to an empty assembly block as an input, and inform
+  // the compiler that memory is read to and possibly modified. This should not
+  // be architecture specific, since the asm block is empty.
+  __asm__ __volatile__("" ::"r"(memory) : "memory");
+}
 } // namespace
 
 TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
   std::array<float, 16> lots_of_data;
   auto LargeLambda = [lots_of_data]() mutable {
-    // Stop everything getting optimised out
     lots_of_data[3] = 0.25f;
+    // In LTO builds, this lambda can be optimized away, since the compiler can
+    // see through the memory accesses after inlining across TUs. Ensure it can
+    // no longer reason about the memory access, so that won't happen.
+    HideMemoryFromCompiler(&lots_of_data[3]);
     EXPECT_EQ(16u, lots_of_data.size());
     EXPECT_EQ(0.25f, lots_of_data[3]);
   };


        


More information about the llvm-commits mailing list