Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <stddef.h> |
| 12 | #include <stdio.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 13 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 14 | #include <memory> |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 15 | #include <random> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/null_socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "rtc_base/thread.h" |
| 20 | #include "test/gtest.h" |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | #if defined(MEMORY_SANITIZER) |
| 27 | void UseOfUninitializedValue() { |
| 28 | int* buf = new int[2]; |
| 29 | std::random_device engine; |
| 30 | if (buf[engine() % 2]) { // Non-deterministic conditional. |
| 31 | printf("Externally visible action."); |
| 32 | } |
| 33 | delete[] buf; |
| 34 | } |
| 35 | |
| 36 | TEST(SanitizersDeathTest, MemorySanitizer) { |
| 37 | EXPECT_DEATH(UseOfUninitializedValue(), "use-of-uninitialized-value"); |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | #if defined(ADDRESS_SANITIZER) |
| 42 | void HeapUseAfterFree() { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 43 | char* buf = new char[2]; |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 44 | delete[] buf; |
| 45 | buf[0] = buf[1]; |
| 46 | } |
| 47 | |
| 48 | TEST(SanitizersDeathTest, AddressSanitizer) { |
| 49 | EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free"); |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | #if defined(UNDEFINED_SANITIZER) |
| 54 | // For ubsan: |
| 55 | void SignedIntegerOverflow() { |
| 56 | int32_t x = 1234567890; |
| 57 | x *= 2; |
Mirko Bonadei | 3291375 | 2021-10-30 15:56:24 | [diff] [blame] | 58 | (void)x; |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // For ubsan_vptr: |
| 62 | struct Base { |
| 63 | virtual void f() {} |
| 64 | virtual ~Base() {} |
| 65 | }; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 66 | struct Derived : public Base {}; |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 67 | |
| 68 | void InvalidVptr() { |
| 69 | Base b; |
| 70 | auto* d = static_cast<Derived*>(&b); // Bad downcast. |
| 71 | d->f(); // Virtual function call with object of wrong dynamic type. |
| 72 | } |
| 73 | |
| 74 | TEST(SanitizersDeathTest, UndefinedSanitizer) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 75 | EXPECT_DEATH( |
| 76 | { |
| 77 | SignedIntegerOverflow(); |
| 78 | InvalidVptr(); |
| 79 | }, |
| 80 | "runtime error"); |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 81 | } |
| 82 | #endif |
| 83 | |
| 84 | #if defined(THREAD_SANITIZER) |
| 85 | class IncrementThread : public Thread { |
| 86 | public: |
| 87 | explicit IncrementThread(int* value) |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 88 | : Thread(std::make_unique<NullSocketServer>()), value_(value) {} |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 89 | |
Artem Titov | 6cae2d5 | 2022-01-26 15:01:10 | [diff] [blame] | 90 | IncrementThread(const IncrementThread&) = delete; |
| 91 | IncrementThread& operator=(const IncrementThread&) = delete; |
| 92 | |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 93 | void Run() override { |
| 94 | ++*value_; |
| 95 | Thread::Current()->SleepMs(100); |
| 96 | } |
| 97 | |
| 98 | // Un-protect Thread::Join for the test. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 99 | void Join() { Thread::Join(); } |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | int* value_; |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | void DataRace() { |
| 106 | int value = 0; |
| 107 | IncrementThread thread1(&value); |
| 108 | IncrementThread thread2(&value); |
| 109 | thread1.Start(); |
| 110 | thread2.Start(); |
| 111 | thread1.Join(); |
| 112 | thread2.Join(); |
| 113 | // TSan seems to mess with gtest's death detection. |
| 114 | // Fail intentionally, and rely on detecting the error message. |
Karl Wiberg | c95b939 | 2020-11-07 23:49:37 | [diff] [blame] | 115 | RTC_CHECK_NOTREACHED(); |
Oleh Prypin | 66ca7e3 | 2017-09-14 11:05:00 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TEST(SanitizersDeathTest, ThreadSanitizer) { |
| 119 | EXPECT_DEATH(DataRace(), "data race"); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | } // namespace rtc |