Skip to content

Commit ca95bee

Browse files
authored
[compiler-rt][rtsan] Introduce first end to end RTsan lit tests, enable instrumented unit tests (llvm#105732)
1 parent 6f092e5 commit ca95bee

File tree

7 files changed

+67
-23
lines changed

7 files changed

+67
-23
lines changed

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

+7-8
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ endif()
6060
foreach(arch ${RTSAN_TEST_ARCH})
6161
set(RtsanTestObjects)
6262

63-
# TODO: Re-enable once -fsanitize=realtime exists in clang driver
64-
#generate_compiler_rt_tests(RtsanTestObjects
65-
# RtsanUnitTests "Rtsan-${arch}-Test" ${arch}
66-
# COMPILE_DEPS ${RTSAN_UNITTEST_HEADERS}
67-
# SOURCES ${RTSAN_INST_TEST_SOURCES} ${COMPILER_RT_GOOGLETEST_SOURCES}
68-
# DEPS rtsan
69-
# CFLAGS ${RTSAN_UNITTEST_CFLAGS} -fsanitize=realtime
70-
# LINK_FLAGS ${RTSAN_UNITTEST_LINK_FLAGS} -fsanitize=realtime)
63+
generate_compiler_rt_tests(RtsanTestObjects
64+
RtsanUnitTests "Rtsan-${arch}-Test" ${arch}
65+
COMPILE_DEPS ${RTSAN_UNITTEST_HEADERS}
66+
SOURCES ${RTSAN_INST_TEST_SOURCES} ${COMPILER_RT_GOOGLETEST_SOURCES}
67+
DEPS rtsan
68+
CFLAGS ${RTSAN_UNITTEST_CFLAGS} -fsanitize=realtime
69+
LINK_FLAGS ${RTSAN_UNITTEST_LINK_FLAGS} -fsanitize=realtime)
7170

7271
set(RTSAN_TEST_RUNTIME RTRtsanTest.${arch})
7372
if(APPLE)

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
145145
auto lambda = [lots_of_data]() mutable {
146146
// Stop everything getting optimised out
147147
lots_of_data[3] = 0.25f;
148-
EXPECT_EQ(16, lots_of_data.size());
148+
EXPECT_EQ(16u, lots_of_data.size());
149149
EXPECT_EQ(0.25f, lots_of_data[3]);
150150
};
151151
auto Func = [&]() { InvokeStdFunction(lambda); };
@@ -156,11 +156,17 @@ TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
156156
TEST(TestRtsan, AccessingALargeAtomicVariableDiesWhenRealtime) {
157157
std::atomic<float> small_atomic{0.0f};
158158
ASSERT_TRUE(small_atomic.is_lock_free());
159-
RealtimeInvoke([&small_atomic]() { float x = small_atomic.load(); });
159+
RealtimeInvoke([&small_atomic]() {
160+
float x = small_atomic.load();
161+
return x;
162+
});
160163

161164
std::atomic<std::array<float, 2048>> large_atomic;
162165
ASSERT_FALSE(large_atomic.is_lock_free());
163-
auto Func = [&]() { auto x = large_atomic.load(); };
166+
auto Func = [&]() {
167+
std::array<float, 2048> x = large_atomic.load();
168+
return x;
169+
};
164170
ExpectRealtimeDeath(Func);
165171
ExpectNonRealtimeSurvival(Func);
166172
}

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ TEST(TestRtsanInterceptors, PthreadCreateDiesWhenRealtime) {
321321
auto Func = []() {
322322
pthread_t thread{};
323323
const pthread_attr_t attr{};
324-
struct thread_info *thread_info;
324+
struct thread_info *thread_info{};
325325
pthread_create(&thread, &attr, &FakeThreadEntryPoint, thread_info);
326326
};
327327
ExpectRealtimeDeath(Func, "pthread_create");

compiler-rt/test/rtsan/CMakeLists.txt

-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
2-
3-
4-
5-
######
6-
# TODO: Full lit tests coming in a future review when we introduce the codegen
7-
######
8-
9-
10-
11-
121
set(RTSAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
132

143
set(RTSAN_TESTSUITES)

compiler-rt/test/rtsan/basic.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: not %run %t 2>&1 | FileCheck %s
3+
// UNSUPPORTED: ios
4+
5+
// Intent: Ensure that an intercepted call in a [[clang::nonblocking]] function
6+
// is flagged as an error. Basic smoke test.
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
void violation() [[clang::nonblocking]] {
12+
void *ptr = malloc(2);
13+
printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
14+
}
15+
16+
int main() {
17+
violation();
18+
return 0;
19+
// CHECK: {{.*Real-time violation.*}}
20+
// CHECK: {{.*malloc*}}
21+
}

compiler-rt/test/rtsan/inactive.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clangxx %s -o %t
2+
// RUN: %run %t 2>&1 | FileCheck %s
3+
// UNSUPPORTED: ios
4+
5+
// Intent: Ensure [[clang::nonblocking]] has no impact if -fsanitize=realtime is not used
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
// In this test, we don't use the -fsanitize=realtime flag, so nothing
11+
// should happen here
12+
void violation() [[clang::nonblocking]] {
13+
void *ptr = malloc(2);
14+
printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
15+
}
16+
17+
int main() {
18+
printf("Starting run\n");
19+
violation();
20+
printf("No violations ended the program\n");
21+
return 0;
22+
// CHECK: {{.*Starting run.*}}
23+
// CHECK NOT: {{.*Real-time violation.*}}
24+
// CHECK NOT: {{.*malloc*}}
25+
// CHECK: {{.*No violations ended the program.*}}
26+
}

compiler-rt/test/sanitizer_common/lit.common.cfg.py

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
tool_options = "HWASAN_OPTIONS"
1919
if not config.has_lld:
2020
config.unsupported = True
21+
elif config.tool_name == "rtsan":
22+
tool_cflags = ["-fsanitize=realtime"]
23+
tool_options = "RTSAN_OPTIONS"
2124
elif config.tool_name == "tsan":
2225
tool_cflags = ["-fsanitize=thread"]
2326
tool_options = "TSAN_OPTIONS"

0 commit comments

Comments
 (0)