[compiler-rt] [compiler-rt][rtsan] Introduce first end to end RTsan lit tests, enable instrumented unit tests (PR #105732)

Chris Apple via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 23 17:56:53 PDT 2024


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

>From 98173e2c88ebf7d48fabd38c87d38efab6492efa Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 18 Jul 2024 17:29:01 +0200
Subject: [PATCH 1/3] [compiler-rt][rtsan] Introduce RTsan lit tests

---
 compiler-rt/lib/rtsan/tests/CMakeLists.txt    | 15 ++++++------
 compiler-rt/test/rtsan/CMakeLists.txt         | 11 ---------
 compiler-rt/test/rtsan/test_rtsan.cpp         | 17 ++++++++++++++
 .../test/rtsan/test_rtsan_inactive.cpp        | 23 +++++++++++++++++++
 .../test/sanitizer_common/lit.common.cfg.py   |  3 +++
 5 files changed, 50 insertions(+), 19 deletions(-)
 create mode 100644 compiler-rt/test/rtsan/test_rtsan.cpp
 create mode 100644 compiler-rt/test/rtsan/test_rtsan_inactive.cpp

diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 3b783c90c26585..0320bbad592186 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -60,14 +60,13 @@ endif()
 foreach(arch ${RTSAN_TEST_ARCH})
   set(RtsanTestObjects)
 
-  # TODO: Re-enable once -fsanitize=realtime exists in clang driver
-  #generate_compiler_rt_tests(RtsanTestObjects
-  #  RtsanUnitTests "Rtsan-${arch}-Test" ${arch}
-  #  COMPILE_DEPS ${RTSAN_UNITTEST_HEADERS}
-  #  SOURCES ${RTSAN_INST_TEST_SOURCES} ${COMPILER_RT_GOOGLETEST_SOURCES}
-  #  DEPS rtsan
-  #  CFLAGS ${RTSAN_UNITTEST_CFLAGS} -fsanitize=realtime
-  #  LINK_FLAGS ${RTSAN_UNITTEST_LINK_FLAGS} -fsanitize=realtime)
+  generate_compiler_rt_tests(RtsanTestObjects
+    RtsanUnitTests "Rtsan-${arch}-Test" ${arch}
+    COMPILE_DEPS ${RTSAN_UNITTEST_HEADERS}
+    SOURCES ${RTSAN_INST_TEST_SOURCES} ${COMPILER_RT_GOOGLETEST_SOURCES}
+    DEPS rtsan
+    CFLAGS ${RTSAN_UNITTEST_CFLAGS} -fsanitize=realtime
+    LINK_FLAGS ${RTSAN_UNITTEST_LINK_FLAGS} -fsanitize=realtime)
 
   set(RTSAN_TEST_RUNTIME RTRtsanTest.${arch})
   if(APPLE)
diff --git a/compiler-rt/test/rtsan/CMakeLists.txt b/compiler-rt/test/rtsan/CMakeLists.txt
index e1f9eb39408dc1..59fc5a29703fea 100644
--- a/compiler-rt/test/rtsan/CMakeLists.txt
+++ b/compiler-rt/test/rtsan/CMakeLists.txt
@@ -1,14 +1,3 @@
-
-
-
-
-######
-# TODO: Full lit tests coming in a future review when we introduce the codegen
-######
-
-
-
-
 set(RTSAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
 set(RTSAN_TESTSUITES)
diff --git a/compiler-rt/test/rtsan/test_rtsan.cpp b/compiler-rt/test/rtsan/test_rtsan.cpp
new file mode 100644
index 00000000000000..101aadc56e9608
--- /dev/null
+++ b/compiler-rt/test/rtsan/test_rtsan.cpp
@@ -0,0 +1,17 @@
+// RUN: %clangxx -fsanitize=realtime %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: ios
+
+// Intent: Ensure that an intercepted call in a [[clang::nonblocking]] function
+//         is flagged as an error. Basic smoke test.
+
+#include <stdlib.h>
+
+void violation() [[clang::nonblocking]] { void *Ptr = malloc(2); }
+
+int main() {
+  violation();
+  return 0;
+  // CHECK: {{.*Real-time violation.*}}
+  // CHECK: {{.*malloc*}}
+}
diff --git a/compiler-rt/test/rtsan/test_rtsan_inactive.cpp b/compiler-rt/test/rtsan/test_rtsan_inactive.cpp
new file mode 100644
index 00000000000000..86907df6dfa161
--- /dev/null
+++ b/compiler-rt/test/rtsan/test_rtsan_inactive.cpp
@@ -0,0 +1,23 @@
+// RUN: %clangxx %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+// UNSUPPORTED: ios
+
+// Intent: Ensure [[clang::nonblocking]] has no impact if -fsanitize=realtime is not used
+
+#include <stdio.h>
+#include <stdlib.h>
+
+// In this test, we don't use the -fsanitize=realtime flag, so nothing
+// should happen here
+void violation() [[clang::nonblocking]] { void *Ptr = malloc(2); }
+
+int main() {
+  printf("Starting run\n");
+  violation();
+  printf("No violations ended the program\n");
+  return 0;
+  // CHECK: {{.*Starting run.*}}
+  // CHECK NOT: {{.*Real-time violation.*}}
+  // CHECK NOT: {{.*malloc*}}
+  // CHECK: {{.*No violations ended the program.*}}
+}
diff --git a/compiler-rt/test/sanitizer_common/lit.common.cfg.py b/compiler-rt/test/sanitizer_common/lit.common.cfg.py
index 04af4816eb6e78..5406e8838f2fcf 100644
--- a/compiler-rt/test/sanitizer_common/lit.common.cfg.py
+++ b/compiler-rt/test/sanitizer_common/lit.common.cfg.py
@@ -18,6 +18,9 @@
     tool_options = "HWASAN_OPTIONS"
     if not config.has_lld:
         config.unsupported = True
+elif config.tool_name == "rtsan":
+    tool_cflags = ["-fsanitize=realtime"]
+    tool_options = "RTSAN_OPTIONS"
 elif config.tool_name == "tsan":
     tool_cflags = ["-fsanitize=thread"]
     tool_options = "TSAN_OPTIONS"

>From 3507e334fe272a2d64f5789b9ea3509fa723e0e8 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Thu, 22 Aug 2024 20:44:14 -0700
Subject: [PATCH 2/3] [PR] Maskray - rename test files, fix variable case

---
 compiler-rt/test/rtsan/{test_rtsan.cpp => basic.cpp}        | 6 +++++-
 .../test/rtsan/{test_rtsan_inactive.cpp => inactive.cpp}    | 5 ++++-
 2 files changed, 9 insertions(+), 2 deletions(-)
 rename compiler-rt/test/rtsan/{test_rtsan.cpp => basic.cpp} (70%)
 rename compiler-rt/test/rtsan/{test_rtsan_inactive.cpp => inactive.cpp} (80%)

diff --git a/compiler-rt/test/rtsan/test_rtsan.cpp b/compiler-rt/test/rtsan/basic.cpp
similarity index 70%
rename from compiler-rt/test/rtsan/test_rtsan.cpp
rename to compiler-rt/test/rtsan/basic.cpp
index 101aadc56e9608..ec7382cb0ecaff 100644
--- a/compiler-rt/test/rtsan/test_rtsan.cpp
+++ b/compiler-rt/test/rtsan/basic.cpp
@@ -5,9 +5,13 @@
 // Intent: Ensure that an intercepted call in a [[clang::nonblocking]] function
 //         is flagged as an error. Basic smoke test.
 
+#include <stdio.h>
 #include <stdlib.h>
 
-void violation() [[clang::nonblocking]] { void *Ptr = malloc(2); }
+void violation() [[clang::nonblocking]] {
+  void *ptr = malloc(2);
+  printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
+}
 
 int main() {
   violation();
diff --git a/compiler-rt/test/rtsan/test_rtsan_inactive.cpp b/compiler-rt/test/rtsan/inactive.cpp
similarity index 80%
rename from compiler-rt/test/rtsan/test_rtsan_inactive.cpp
rename to compiler-rt/test/rtsan/inactive.cpp
index 86907df6dfa161..69edc63a4cfd41 100644
--- a/compiler-rt/test/rtsan/test_rtsan_inactive.cpp
+++ b/compiler-rt/test/rtsan/inactive.cpp
@@ -9,7 +9,10 @@
 
 // In this test, we don't use the -fsanitize=realtime flag, so nothing
 // should happen here
-void violation() [[clang::nonblocking]] { void *Ptr = malloc(2); }
+void violation() [[clang::nonblocking]] {
+  void *ptr = malloc(2);
+  printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
+}
 
 int main() {
   printf("Starting run\n");

>From 48bea277eb79aeae7e3b14e82cf07c548c112700 Mon Sep 17 00:00:00 2001
From: Chris Apple <cja-private at pm.me>
Date: Fri, 23 Aug 2024 17:19:29 -0700
Subject: [PATCH 3/3] [PR] vitalybuka - Fix warnings so build machines stay
 happy

---
 .../lib/rtsan/tests/rtsan_test_functional.cpp        | 12 +++++++++---
 .../lib/rtsan/tests/rtsan_test_interceptors.cpp      |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
index 97afb1eefb6401..6e7ab016a4c6b2 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
@@ -145,7 +145,7 @@ TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
   auto lambda = [lots_of_data]() mutable {
     // Stop everything getting optimised out
     lots_of_data[3] = 0.25f;
-    EXPECT_EQ(16, lots_of_data.size());
+    EXPECT_EQ(16u, lots_of_data.size());
     EXPECT_EQ(0.25f, lots_of_data[3]);
   };
   auto Func = [&]() { InvokeStdFunction(lambda); };
@@ -156,11 +156,17 @@ TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
 TEST(TestRtsan, AccessingALargeAtomicVariableDiesWhenRealtime) {
   std::atomic<float> small_atomic{0.0f};
   ASSERT_TRUE(small_atomic.is_lock_free());
-  RealtimeInvoke([&small_atomic]() { float x = small_atomic.load(); });
+  RealtimeInvoke([&small_atomic]() {
+    float x = small_atomic.load();
+    return x;
+  });
 
   std::atomic<std::array<float, 2048>> large_atomic;
   ASSERT_FALSE(large_atomic.is_lock_free());
-  auto Func = [&]() { auto x = large_atomic.load(); };
+  auto Func = [&]() {
+    std::array<float, 2048> x = large_atomic.load();
+    return x;
+  };
   ExpectRealtimeDeath(Func);
   ExpectNonRealtimeSurvival(Func);
 }
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
index 8861104068c8e9..47c07b3e47abd7 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
@@ -321,7 +321,7 @@ TEST(TestRtsanInterceptors, PthreadCreateDiesWhenRealtime) {
   auto Func = []() {
     pthread_t thread{};
     const pthread_attr_t attr{};
-    struct thread_info *thread_info;
+    struct thread_info *thread_info{};
     pthread_create(&thread, &attr, &FakeThreadEntryPoint, thread_info);
   };
   ExpectRealtimeDeath(Func, "pthread_create");



More information about the llvm-commits mailing list