OLD | NEW |
---|---|
(Empty) | |
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> | |
13 #include <random> | |
14 | |
15 #include "webrtc/rtc_base/checks.h" | |
16 #include "webrtc/rtc_base/nullsocketserver.h" | |
17 #include "webrtc/rtc_base/ptr_util.h" | |
18 #include "webrtc/rtc_base/thread.h" | |
19 #include "webrtc/test/gtest.h" | |
20 | |
21 namespace rtc { | |
22 | |
23 namespace { | |
24 | |
25 #if defined(MEMORY_SANITIZER) | |
26 void UseOfUninitializedValue() { | |
27 int* buf = new int[2]; | |
28 std::random_device engine; | |
29 if (buf[engine() % 2]) { // Non-deterministic conditional. | |
30 printf("Externally visible action."); | |
31 } | |
32 delete[] buf; | |
33 } | |
34 | |
35 TEST(SanitizersDeathTest, MemorySanitizer) { | |
36 EXPECT_DEATH(UseOfUninitializedValue(), "use-of-uninitialized-value"); | |
37 } | |
38 #endif | |
39 | |
40 #if defined(ADDRESS_SANITIZER) | |
41 void HeapUseAfterFree() { | |
42 char *buf = new char[2]; | |
43 delete[] buf; | |
44 buf[0] = buf[1]; | |
45 } | |
46 | |
47 TEST(SanitizersDeathTest, AddressSanitizer) { | |
48 EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free"); | |
49 } | |
50 #endif | |
51 | |
52 #if defined(UNDEFINED_SANITIZER) | |
53 // For ubsan: | |
54 void SignedIntegerOverflow() { | |
55 int32_t x = 1234567890; | |
56 x *= 2; | |
57 } | |
58 | |
59 // For ubsan_vptr: | |
60 struct Base { | |
61 virtual void f() {} | |
62 virtual ~Base() {} | |
kwiberg-webrtc
2017/09/14 08:23:22
Nit: You don't need the virtual destructor. Normal
oprypin_webrtc
2017/09/14 08:33:04
error: 'rtc::(anonymous namespace)::Base' has virt
kwiberg-webrtc
2017/09/14 08:48:27
Oh, right, we have a machine that checks that for
| |
63 }; | |
64 struct Derived : public Base { | |
65 }; | |
66 | |
67 void InvalidVptr() { | |
68 Base b; | |
69 auto* d = static_cast<Derived*>(&b); // Bad downcast. | |
70 d->f(); // Virtual function call with object of wrong dynamic type. | |
71 } | |
72 | |
73 TEST(SanitizersDeathTest, UndefinedSanitizer) { | |
74 EXPECT_DEATH({ SignedIntegerOverflow(); InvalidVptr(); }, "runtime error"); | |
75 } | |
76 #endif | |
77 | |
78 #if defined(THREAD_SANITIZER) | |
79 class IncrementThread : public Thread { | |
80 public: | |
81 explicit IncrementThread(int* value) | |
82 : Thread(rtc::MakeUnique<NullSocketServer>()), | |
83 value_(value) {} | |
84 | |
85 void Run() override { | |
86 ++*value_; | |
87 Thread::Current()->SleepMs(100); | |
88 } | |
89 | |
90 // Un-protect Thread::Join for the test. | |
91 void Join() { | |
92 Thread::Join(); | |
93 } | |
94 | |
95 private: | |
96 int* value_; | |
97 | |
98 RTC_DISALLOW_COPY_AND_ASSIGN(IncrementThread); | |
99 }; | |
100 | |
101 void DataRace() { | |
102 int value = 0; | |
103 IncrementThread thread1(&value); | |
104 IncrementThread thread2(&value); | |
105 thread1.Start(); | |
106 thread2.Start(); | |
107 thread1.Join(); | |
108 thread2.Join(); | |
109 // TSan seems to mess with gtest's death detection. | |
110 // Fail intentionally, and rely on detecting the error message. | |
111 RTC_CHECK(false); | |
112 } | |
113 | |
114 TEST(SanitizersDeathTest, ThreadSanitizer) { | |
115 EXPECT_DEATH(DataRace(), "data race"); | |
116 } | |
117 #endif | |
118 | |
119 } // namespace | |
120 | |
121 } // namespace rtc | |
OLD | NEW |