blob: 4cdf5fd56f281324cfaa95e1f570e948f10364a0 [file] [log] [blame]
Oleh Prypin66ca7e32017-09-14 13:05:00 +02001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
16#include "rtc_base/nullsocketserver.h"
17#include "rtc_base/ptr_util.h"
18#include "rtc_base/thread.h"
19#include "test/gtest.h"
Oleh Prypin66ca7e32017-09-14 13:05:00 +020020
21namespace rtc {
22
23namespace {
24
25#if defined(MEMORY_SANITIZER)
26void 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
35TEST(SanitizersDeathTest, MemorySanitizer) {
36 EXPECT_DEATH(UseOfUninitializedValue(), "use-of-uninitialized-value");
37}
38#endif
39
40#if defined(ADDRESS_SANITIZER)
41void HeapUseAfterFree() {
42 char *buf = new char[2];
43 delete[] buf;
44 buf[0] = buf[1];
45}
46
47TEST(SanitizersDeathTest, AddressSanitizer) {
48 EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free");
49}
50#endif
51
52#if defined(UNDEFINED_SANITIZER)
53// For ubsan:
54void SignedIntegerOverflow() {
55 int32_t x = 1234567890;
56 x *= 2;
57}
58
59// For ubsan_vptr:
60struct Base {
61 virtual void f() {}
62 virtual ~Base() {}
63};
64struct Derived : public Base {
65};
66
67void 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
73TEST(SanitizersDeathTest, UndefinedSanitizer) {
74 EXPECT_DEATH({ SignedIntegerOverflow(); InvalidVptr(); }, "runtime error");
75}
76#endif
77
78#if defined(THREAD_SANITIZER)
79class 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
101void 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
114TEST(SanitizersDeathTest, ThreadSanitizer) {
115 EXPECT_DEATH(DataRace(), "data race");
116}
117#endif
118
119} // namespace
120
121} // namespace rtc