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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions compiler-rt/lib/rtsan/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); };
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 0 additions & 11 deletions compiler-rt/test/rtsan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
21 changes: 21 additions & 0 deletions compiler-rt/test/rtsan/basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 <stdio.h>
#include <stdlib.h>

void violation() [[clang::nonblocking]] {
void *ptr = malloc(2);
printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
}

int main() {
violation();
return 0;
// CHECK: {{.*Real-time violation.*}}
// CHECK: {{.*malloc*}}
}
26 changes: 26 additions & 0 deletions compiler-rt/test/rtsan/inactive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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);
printf("ptr: %p\n", ptr); // ensure we don't optimize out the malloc
}

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.*}}
}
3 changes: 3 additions & 0 deletions compiler-rt/test/sanitizer_common/lit.common.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading