Path: blob/main/contrib/llvm-project/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
35266 views
//===--- rtsan_test.cpp - Realtime Sanitizer --------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Introduces basic functional tests for the realtime sanitizer.9// Not meant to be exhaustive, testing all interceptors, please see10// test_rtsan_interceptors.cpp for those tests.11//12//===----------------------------------------------------------------------===//1314#include "gtest/gtest.h"1516#include "rtsan_test_utilities.h"17#include <rtsan.h>18#include <sanitizer_common/sanitizer_platform.h>19#include <sanitizer_common/sanitizer_platform_interceptors.h>2021#include <array>22#include <atomic>23#include <chrono>24#include <fstream>25#include <mutex>26#include <shared_mutex>27#include <thread>2829#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \30__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 10120031#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 132#else33#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 034#endif3536#define RTSAN_TEST_SHARED_MUTEX (!(SI_MAC) || SI_MAC_DEPLOYMENT_AT_LEAST_10_12)3738using namespace testing;39using namespace rtsan_testing;40using namespace std::chrono_literals;4142TEST(TestRtsan, VectorPushBackAllocationDiesWhenRealtime) {43std::vector<float> vec;44auto Func = [&vec]() { vec.push_back(0.4f); };45ExpectRealtimeDeath(Func);46ASSERT_EQ(0u, vec.size());47ExpectNonRealtimeSurvival(Func);48ASSERT_EQ(1u, vec.size());49}5051TEST(TestRtsan, DestructionOfObjectOnHeapDiesWhenRealtime) {52auto allocated_ptr = std::make_unique<std::array<float, 256>>();53auto Func = [&allocated_ptr]() { allocated_ptr.reset(); };54ExpectRealtimeDeath(Func);55ASSERT_NE(nullptr, allocated_ptr.get());56ExpectNonRealtimeSurvival(Func);57ASSERT_EQ(nullptr, allocated_ptr.get());58}5960TEST(TestRtsan, SleepingAThreadDiesWhenRealtime) {61auto Func = []() { std::this_thread::sleep_for(1us); };62ExpectRealtimeDeath(Func);63ExpectNonRealtimeSurvival(Func);64}6566TEST(TestRtsan, IfstreamCreationDiesWhenRealtime) {67auto Func = []() { std::ifstream ifs{"./file.txt"}; };68ExpectRealtimeDeath(Func);69ExpectNonRealtimeSurvival(Func);70std::remove("./file.txt");71}7273TEST(TestRtsan, OfstreamCreationDiesWhenRealtime) {74auto Func = []() { std::ofstream ofs{"./file.txt"}; };75ExpectRealtimeDeath(Func);76ExpectNonRealtimeSurvival(Func);77std::remove("./file.txt");78}7980TEST(TestRtsan, LockingAMutexDiesWhenRealtime) {81std::mutex mutex;82auto Func = [&]() { mutex.lock(); };83ExpectRealtimeDeath(Func);84ExpectNonRealtimeSurvival(Func);85}8687TEST(TestRtsan, UnlockingAMutexDiesWhenRealtime) {88std::mutex mutex;89mutex.lock();90auto Func = [&]() { mutex.unlock(); };91ExpectRealtimeDeath(Func);92ExpectNonRealtimeSurvival(Func);93}9495#if RTSAN_TEST_SHARED_MUTEX9697TEST(TestRtsan, LockingASharedMutexDiesWhenRealtime) {98std::shared_mutex mutex;99auto Func = [&]() { mutex.lock(); };100ExpectRealtimeDeath(Func);101ExpectNonRealtimeSurvival(Func);102}103104TEST(TestRtsan, UnlockingASharedMutexDiesWhenRealtime) {105std::shared_mutex mutex;106mutex.lock();107auto Func = [&]() { mutex.unlock(); };108ExpectRealtimeDeath(Func);109ExpectNonRealtimeSurvival(Func);110}111112TEST(TestRtsan, SharedLockingASharedMutexDiesWhenRealtime) {113std::shared_mutex mutex;114auto Func = [&]() { mutex.lock_shared(); };115ExpectRealtimeDeath(Func);116ExpectNonRealtimeSurvival(Func);117}118119TEST(TestRtsan, SharedUnlockingASharedMutexDiesWhenRealtime) {120std::shared_mutex mutex;121mutex.lock_shared();122auto Func = [&]() { mutex.unlock_shared(); };123ExpectRealtimeDeath(Func);124ExpectNonRealtimeSurvival(Func);125}126127#endif // RTSAN_TEST_SHARED_MUTEX128129TEST(TestRtsan, LaunchingAThreadDiesWhenRealtime) {130auto Func = [&]() {131std::thread Thread{[]() {}};132Thread.join();133};134ExpectRealtimeDeath(Func);135ExpectNonRealtimeSurvival(Func);136}137138namespace {139void InvokeStdFunction(std::function<void()> &&function) { function(); }140} // namespace141142TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {143std::array<float, 16> lots_of_data;144auto lambda = [lots_of_data]() mutable {145// Stop everything getting optimised out146lots_of_data[3] = 0.25f;147EXPECT_EQ(16, lots_of_data.size());148EXPECT_EQ(0.25f, lots_of_data[3]);149};150auto Func = [&]() { InvokeStdFunction(lambda); };151ExpectRealtimeDeath(Func);152ExpectNonRealtimeSurvival(Func);153}154155TEST(TestRtsan, AccessingALargeAtomicVariableDiesWhenRealtime) {156std::atomic<float> small_atomic{0.0f};157ASSERT_TRUE(small_atomic.is_lock_free());158RealtimeInvoke([&small_atomic]() { float x = small_atomic.load(); });159160std::atomic<std::array<float, 2048>> large_atomic;161ASSERT_FALSE(large_atomic.is_lock_free());162auto Func = [&]() { auto x = large_atomic.load(); };163ExpectRealtimeDeath(Func);164ExpectNonRealtimeSurvival(Func);165}166167TEST(TestRtsan, FirstCoutDiesWhenRealtime) {168auto Func = []() { std::cout << "Hello, world!" << std::endl; };169ExpectRealtimeDeath(Func);170ExpectNonRealtimeSurvival(Func);171}172173TEST(TestRtsan, SecondCoutDiesWhenRealtime) {174std::cout << "Hello, world";175auto Func = []() { std::cout << "Hello, again!" << std::endl; };176ExpectRealtimeDeath(Func);177ExpectNonRealtimeSurvival(Func);178}179180TEST(TestRtsan, PrintfDiesWhenRealtime) {181auto Func = []() { printf("Hello, world!\n"); };182ExpectRealtimeDeath(Func);183ExpectNonRealtimeSurvival(Func);184}185186TEST(TestRtsan, ThrowingAnExceptionDiesWhenRealtime) {187auto Func = [&]() {188try {189throw std::exception();190} catch (std::exception &) {191}192};193ExpectRealtimeDeath(Func);194ExpectNonRealtimeSurvival(Func);195}196197TEST(TestRtsan, DoesNotDieIfTurnedOff) {198std::mutex mutex;199auto RealtimeUnsafeFunc = [&]() {200__rtsan_off();201mutex.lock();202mutex.unlock();203__rtsan_on();204};205RealtimeInvoke(RealtimeUnsafeFunc);206}207208209