Chromium Code Reviews| 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 | |
| 13 #include "webrtc/rtc_base/checks.h" | |
| 14 #include "webrtc/rtc_base/nullsocketserver.h" | |
| 15 #include "webrtc/rtc_base/thread.h" | |
| 16 #include "webrtc/test/gtest.h" | |
| 17 | |
| 18 namespace rtc { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 #if defined(MEMORY_SANITIZER) | |
|
kwiberg-webrtc
2017/09/13 11:59:23
There are preprocessor symbols here [https://chrom
oprypin_webrtc
2017/09/13 14:48:15
I didn't know about these. I got my example from h
kwiberg-webrtc
2017/09/14 02:15:02
OK, FOO_SANITIZER seems to be set by Chromium's GN
| |
| 23 void UseOfUninitializedValue() { | |
| 24 int* buf = new int[2]; | |
| 25 if (buf[1]) { | |
| 26 ++buf[0]; | |
| 27 } | |
| 28 delete[] buf; | |
| 29 } | |
| 30 | |
| 31 TEST(SanitizersDeathTest, MemorySanitizer) { | |
| 32 UseOfUninitializedValue(); | |
|
kwiberg-webrtc
2017/09/13 11:59:23
Hmm. Why don't we expect to die here?
oprypin_webrtc
2017/09/13 14:48:15
Hmm indeed. It was incorrect. Had to rework this.
| |
| 33 } | |
| 34 #endif | |
| 35 | |
| 36 #if defined(ADDRESS_SANITIZER) | |
| 37 void HeapUseAfterFree() { | |
| 38 char *buf = new char[2]; | |
| 39 delete[] buf; | |
| 40 buf[0] = buf[1]; | |
| 41 } | |
| 42 | |
| 43 TEST(SanitizersDeathTest, AddressSanitizer) { | |
| 44 EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free"); | |
| 45 } | |
| 46 #endif | |
| 47 | |
| 48 #if defined(UNDEFINED_SANITIZER) | |
| 49 // For ubsan: | |
| 50 void SignedIntegerOverflow() { | |
| 51 int32_t x = 1234567890; | |
| 52 x *= 2; | |
| 53 } | |
| 54 | |
| 55 // For ubsan_vptr: | |
| 56 struct Base { | |
| 57 virtual void f() = 0; | |
| 58 virtual ~Base() {} | |
| 59 }; | |
| 60 struct Derived : public Base { | |
| 61 virtual void f() {} | |
| 62 }; | |
|
kwiberg-webrtc
2017/09/14 02:15:02
IIUC, you don't need the subclass or a virtual des
oprypin_webrtc
2017/09/14 06:48:33
OK, I used the alternative implementation. It's ni
kwiberg-webrtc
2017/09/14 08:23:22
Acknowledged.
| |
| 63 | |
| 64 void InvalidVptr() { | |
| 65 Base* ptr = new Derived; | |
| 66 delete ptr; | |
| 67 ptr->f(); | |
| 68 } | |
| 69 | |
| 70 TEST(SanitizersDeathTest, UndefinedSanitizer) { | |
| 71 EXPECT_DEATH({ SignedIntegerOverflow(); InvalidVptr(); }, | |
|
kwiberg-webrtc
2017/09/13 11:59:23
Why are you doing two lethal things here?
oprypin_webrtc
2017/09/13 14:48:15
ubsan fails on only one of them, ubsan_vptr only f
kwiberg-webrtc
2017/09/14 02:15:02
Hmm, yeah, I see. And it looks like it'll be messy
| |
| 72 "runtime error"); | |
| 73 } | |
| 74 #endif | |
| 75 | |
| 76 #if defined(THREAD_SANITIZER) | |
| 77 class IncrementThread : public Thread { | |
| 78 public: | |
| 79 explicit IncrementThread(int* value) | |
| 80 : Thread(std::unique_ptr<SocketServer>(new NullSocketServer())), | |
|
kwiberg-webrtc
2017/09/13 11:59:23
rtc::MakeUnique<NullSocketServer>()
oprypin_webrtc
2017/09/13 14:48:15
Done.
| |
| 81 value_(value) {} | |
| 82 | |
| 83 void Run() override { | |
| 84 ++*value_; | |
| 85 Thread::Current()->SleepMs(100); | |
| 86 } | |
| 87 | |
| 88 // Un-protect Thread::Join for the test. | |
| 89 void Join() { | |
| 90 Thread::Join(); | |
| 91 } | |
| 92 | |
| 93 private: | |
| 94 int* value_; | |
| 95 | |
| 96 RTC_DISALLOW_COPY_AND_ASSIGN(IncrementThread); | |
| 97 }; | |
| 98 | |
| 99 void DataRace() { | |
| 100 int value = 0; | |
| 101 IncrementThread thread1(&value); | |
| 102 IncrementThread thread2(&value); | |
| 103 thread1.Start(); | |
| 104 thread2.Start(); | |
| 105 thread1.Join(); | |
| 106 thread2.Join(); | |
| 107 // TSan seems to mess with gtest's death detection. | |
| 108 // Fail intentionally, and rely on detecting the error message. | |
| 109 RTC_CHECK(false); | |
| 110 } | |
| 111 | |
| 112 TEST(SanitizersDeathTest, ThreadSanitizer) { | |
| 113 EXPECT_DEATH(DataRace(), "data race"); | |
| 114 } | |
| 115 #endif | |
| 116 | |
| 117 } // namespace | |
| 118 | |
| 119 } // namespace rtc | |
| OLD | NEW |