Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2024

Conversation

cjappl
Copy link
Contributor

@cjappl cjappl commented Aug 22, 2024

We can run our full end to end test suite now that #102622 has been merged

@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2024

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

Author: Chris Apple (cjappl)

Changes

We can run our full end to end test suite now that #102622 has been merged


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

5 Files Affected:

  • (modified) compiler-rt/lib/rtsan/tests/CMakeLists.txt (+7-8)
  • (modified) compiler-rt/test/rtsan/CMakeLists.txt (-11)
  • (added) compiler-rt/test/rtsan/test_rtsan.cpp (+17)
  • (added) compiler-rt/test/rtsan/test_rtsan_inactive.cpp (+23)
  • (modified) compiler-rt/test/sanitizer_common/lit.common.cfg.py (+3)
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"

@cjappl
Copy link
Contributor Author

cjappl commented Aug 22, 2024

(trying some new reviewers I saw were on the codeowners group for compiler-rt sanitizers, just let me know if you would not like to be requested in the future!)

@cjappl
Copy link
Contributor Author

cjappl commented Aug 22, 2024

CC for review @davidtrevelyan

@cjappl cjappl requested a review from MaskRay August 23, 2024 03:57
@vitalybuka
Copy link
Collaborator

Please fix these warnings, or some bots will fail:

rtsan_test_functional.cpp:159:44: warning: unused variable 'x'
rtsan_test_functional.cpp:163:28: warning: unused variable 'x'

gtest.h:1379:11: warning: comparison of integers of different signs
from rtsan_test_functional.cpp:148:5:

rtsan_test_interceptors.cpp:325:59: warning: variable 'thread_info' is uninitialized when used here

@cjappl
Copy link
Contributor Author

cjappl commented Aug 24, 2024

Please fix these warnings, or some bots will fail:

rtsan_test_functional.cpp:159:44: warning: unused variable 'x' rtsan_test_functional.cpp:163:28: warning: unused variable 'x'

gtest.h:1379:11: warning: comparison of integers of different signs from rtsan_test_functional.cpp:148:5:

rtsan_test_interceptors.cpp:325:59: warning: variable 'thread_info' is uninitialized when used here

Thank you for catching these @vitalybuka! That saved me a lot of headache.

Did you pull these warnings from somewhere specific, or just building on your own machine? I want to know so I can catch these before I put up any future reviews, and I don't see them on my local mac or ubuntu builds (or the build machine logs)

@cjappl
Copy link
Contributor Author

cjappl commented Aug 24, 2024

Wait, scratch that, they did turn up!! I will ensure they are fixed in just a sec

@cjappl cjappl merged commit ca95bee into llvm:main Aug 26, 2024
7 checks passed
@cjappl cjappl deleted the rtsan_lit_tests branch August 26, 2024 13:39
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 26, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-gcc-ubuntu running on sie-linux-worker3 while building compiler-rt at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/174/builds/3947

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'RealtimeSanitizer-Unit :: ./Rtsan-x86_64-Test/15/35' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test-RealtimeSanitizer-Unit-2029597-15-35.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=35 GTEST_SHARD_INDEX=15 /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test
--

Script:
--
/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test --gtest_filter=RtsanFileTest.OpenCreatesFileWithProperMode
--
/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp:198: Failure
Value of: st.st_mode & 0777
Expected: is equal to 420
  Actual: 384


/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp:198
Value of: st.st_mode & 0777
Expected: is equal to 420
  Actual: 384



********************


@cjappl
Copy link
Contributor Author

cjappl commented Aug 26, 2024

I am in the process of reverting this test for the time being to buy myself time to investigate further, will update this comment with the reverted commit

@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 26, 2024

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building compiler-rt at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/6984

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'RealtimeSanitizer-Unit :: ./Rtsan-x86_64-Test/50/69' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/build/buildbot/premerge-monolithic-linux/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test-RealtimeSanitizer-Unit-1668989-50-69.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=69 GTEST_SHARD_INDEX=50 /build/buildbot/premerge-monolithic-linux/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test
--

Script:
--
/build/buildbot/premerge-monolithic-linux/build/runtimes/runtimes-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test --gtest_filter=RtsanFileTest.OpenCreatesFileWithProperMode
--
/build/buildbot/premerge-monolithic-linux/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp:198: Failure
Value of: st.st_mode & 0777
Expected: is equal to 420
  Actual: 384


/build/buildbot/premerge-monolithic-linux/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp:198
Value of: st.st_mode & 0777
Expected: is equal to 420
  Actual: 384



********************


@cjappl
Copy link
Contributor Author

cjappl commented Aug 26, 2024

Disabled in 11ba2ee, #106079

@Prabhuk
Copy link
Contributor

Prabhuk commented Aug 27, 2024

Hi! This patch seems to break our toolchain builders.

FAILED: compiler-rt/lib/rtsan/tests/Rtsan-x86_64-Test /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-x86_64-unknown-linux-gnu-bins/compiler-rt/lib/rtsan/tests/Rtsan-x86_64-Test 
cd /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-x86_64-unknown-linux-gnu-bins/compiler-rt/lib/rtsan/tests && /b/s/w/ir/x/w/llvm_build/./bin/clang++ RtsanTestObjects.rtsan_test_functional.cpp.x86_64.o RtsanTestObjects.rtsan_test_interceptors.cpp.x86_64.o RtsanTestObjects.rtsan_test_main.cpp.x86_64.o RtsanTestObjects.gtest-all.cc.x86_64.o RtsanTestObjects.gmock-all.cc.x86_64.o -o /b/s/w/ir/x/w/llvm_build/runtimes/runtimes-x86_64-unknown-linux-gnu-bins/compiler-rt/lib/rtsan/tests/./Rtsan-x86_64-Test -fuse-ld=lld -fuse-ld=lld -Wl,--color-diagnostics -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -resource-dir=/b/s/w/ir/x/w/llvm_build/./lib/../lib/clang/20 --unwindlib=none -nostdlib++ /b/s/w/ir/x/w/llvm_build/lib/x86_64-unknown-linux-gnu/libunwind.a /b/s/w/ir/x/w/llvm_build/lib/x86_64-unknown-linux-gnu/libc++.a -no-pie -ldl -lrt -lm -pthread -fsanitize=realtime
ld.lld: error: undefined symbol: __atomic_is_lock_free
>>> referenced by atomic_base.h:40 (/b/s/w/ir/x/w/llvm_build/include/c++/v1/__atomic/atomic_base.h:40)
>>>               RtsanTestObjects.rtsan_test_functional.cpp.x86_64.o:(TestRtsan_AccessingALargeAtomicVariableDiesWhenRealtime_Test::TestBody())

ld.lld: error: undefined symbol: __atomic_load
>>> referenced by cxx_atomic_impl.h:317 (/b/s/w/ir/x/w/llvm_build/include/c++/v1/__atomic/cxx_atomic_impl.h:317)
>>>               RtsanTestObjects.rtsan_test_functional.cpp.x86_64.o:(TestRtsan_AccessingALargeAtomicVariableDiesWhenRealtime_Test::TestBody())
>>> referenced by cxx_atomic_impl.h:317 (/b/s/w/ir/x/w/llvm_build/include/c++/v1/__atomic/cxx_atomic_impl.h:317)
>>>               RtsanTestObjects.rtsan_test_functional.cpp.x86_64.o:(TestRtsan_AccessingALargeAtomicVariableDiesWhenRealtime_Test::TestBody())
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
[19

Link to our full build log file: https://logs.chromium.org/logs/fuchsia/buildbucket/cr-buildbucket/8738529178618519553/+/u/clang/test/stdout

@Prabhuk
Copy link
Contributor

Prabhuk commented Aug 27, 2024

Some observations from our team in our early investigation:

We suspect the problem here could be with detection of COMPILER_RT_HAS_LIBATOMIC.

@cjappl
Copy link
Contributor Author

cjappl commented Aug 27, 2024

Hi @Prabhuk,

Thanks for the early legwork on this, and sorry for the breakage. I am more than happy to help you get things back to green.

Some observations from our team in our early investigation:
libclang_rt.atomic.so is not linked in for the test binaries.
We don't see -latomic in the link even though cmake file for the test seems to try to set it:

llvm-project/compiler-rt/lib/rtsan/tests/CMakeLists.txt

Line 39 in 0359b9a

append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS) 

We suspect the problem here could be with detection of COMPILER_RT_HAS_LIBATOMIC.

This theory has a lot of weight for me, when I compile on ubuntu, for instance, and apply this patch:

diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 0320bbad5921..c31370fa01be 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -37,6 +37,7 @@ set(RTSAN_UNITTEST_LINK_FLAGS
   -no-pie)

 append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS)
+message(FATAL_ERROR "COMPILER_RT_HAS_LIBATOMIC: ${COMPILER_RT_HAS_LIBATOMIC}")
 append_list_if(COMPILER_RT_HAS_LIBDL -ldl RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS)

I get, as expected:

-- For aarch64 builtins preferring aarch64/fp_mode.c to fp_mode.c
CMake Error at /test_radsan/llvm-project/compiler-rt/lib/rtsan/tests/CMakeLists.txt:40 (message):
  COMPILER_RT_HAS_LIBATOMIC: 1

Can you describe what OS and arch you are compiling for? (I see the build logs, but I don't want to assume anything). I typically run my tests locally against MacOS and ubuntu, which is why I may not have seen this error in my development.

If you explicitly hack to add the linker flag, such as:

diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 0320bbad5921..b9755786965a 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -36,7 +36,7 @@ set(RTSAN_UNITTEST_LINK_FLAGS
   ${SANITIZER_TEST_CXX_LIBRARIES}
   -no-pie)

-append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS)
+append_list_if(ON -latomic RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBDL -ldl RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS)

Does the flag show up, and it compiles? I think this would be another indication the detection of COMPILER_RT_HAS_LIBATOMIC is funky for this specific config.

@cjappl
Copy link
Contributor Author

cjappl commented Aug 27, 2024

If you are in need of debugging very quickly, I am happy to talk on discord (username apple412) or email (in the commit messages), just shout if I can be of assistance.

@petrhosek
Copy link
Member

We don't install libatomic on our builders (which is a part of libgcc), instead we use libclang_rt.atomic that comes directly from compiler-rt. I think we need to update the following logic to properly handle both libatomic and libclang_rt.atomic:

if (COMPILER_RT_USE_BUILTINS_LIBRARY)
include(HandleCompilerRT)
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY
FLAGS ${SANITIZER_COMMON_FLAGS})
# TODO(PR51389): We should check COMPILER_RT_BUILTINS_LIBRARY and report an
# error if the value is NOTFOUND rather than silenty continuing but we first
# need to fix find_compiler_rt_library on Darwin.
else()
if (ANDROID)
check_library_exists(gcc __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_LIB)
else()
check_library_exists(gcc_s __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_S_LIB)
endif()
endif()

@petrhosek
Copy link
Member

We don't install libatomic on our builders (which is a part of libgcc), instead we use libclang_rt.atomic that comes directly from compiler-rt. I think we need to update the following logic to properly handle both libatomic and libclang_rt.atomic:

if (COMPILER_RT_USE_BUILTINS_LIBRARY)
include(HandleCompilerRT)
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY
FLAGS ${SANITIZER_COMMON_FLAGS})
# TODO(PR51389): We should check COMPILER_RT_BUILTINS_LIBRARY and report an
# error if the value is NOTFOUND rather than silenty continuing but we first
# need to fix find_compiler_rt_library on Darwin.
else()
if (ANDROID)
check_library_exists(gcc __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_LIB)
else()
check_library_exists(gcc_s __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_S_LIB)
endif()
endif()

#106603 implements support for using compiler-rt atomic library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants