blob: b997bf0c23c6025d48c4f793885edfab67cd116d [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
Mirko Bonadei317a1f02019-09-17 17:06:18 +020014#include <memory>
Oleh Prypin66ca7e32017-09-14 13:05:00 +020015#include <random>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/null_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/thread.h"
20#include "test/gtest.h"
Oleh Prypin66ca7e32017-09-14 13:05:00 +020021
22namespace rtc {
23
24namespace {
25
26#if defined(MEMORY_SANITIZER)
27void 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
36TEST(SanitizersDeathTest, MemorySanitizer) {
37 EXPECT_DEATH(UseOfUninitializedValue(), "use-of-uninitialized-value");
38}
39#endif
40
41#if defined(ADDRESS_SANITIZER)
42void HeapUseAfterFree() {
Yves Gerey665174f2018-06-19 15:03:05 +020043 char* buf = new char[2];
Oleh Prypin66ca7e32017-09-14 13:05:00 +020044 delete[] buf;
45 buf[0] = buf[1];
46}
47
48TEST(SanitizersDeathTest, AddressSanitizer) {
49 EXPECT_DEATH(HeapUseAfterFree(), "heap-use-after-free");
50}
51#endif
52
53#if defined(UNDEFINED_SANITIZER)
54// For ubsan:
55void SignedIntegerOverflow() {
56 int32_t x = 1234567890;
57 x *= 2;
58}
59
60// For ubsan_vptr:
61struct Base {
62 virtual void f() {}
63 virtual ~Base() {}
64};
Yves Gerey665174f2018-06-19 15:03:05 +020065struct Derived : public Base {};
Oleh Prypin66ca7e32017-09-14 13:05:00 +020066
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) {
Yves Gerey665174f2018-06-19 15:03:05 +020074 EXPECT_DEATH(
75 {
76 SignedIntegerOverflow();
77 InvalidVptr();
78 },
79 "runtime error");
Oleh Prypin66ca7e32017-09-14 13:05:00 +020080}
81#endif
82
83#if defined(THREAD_SANITIZER)
84class IncrementThread : public Thread {
85 public:
86 explicit IncrementThread(int* value)
Mirko Bonadei317a1f02019-09-17 17:06:18 +020087 : Thread(std::make_unique<NullSocketServer>()), value_(value) {}
Oleh Prypin66ca7e32017-09-14 13:05:00 +020088
89 void Run() override {
90 ++*value_;
91 Thread::Current()->SleepMs(100);
92 }
93
94 // Un-protect Thread::Join for the test.
Yves Gerey665174f2018-06-19 15:03:05 +020095 void Join() { Thread::Join(); }
Oleh Prypin66ca7e32017-09-14 13:05:00 +020096
97 private:
98 int* value_;
99
100 RTC_DISALLOW_COPY_AND_ASSIGN(IncrementThread);
101};
102
103void DataRace() {
104 int value = 0;
105 IncrementThread thread1(&value);
106 IncrementThread thread2(&value);
107 thread1.Start();
108 thread2.Start();
109 thread1.Join();
110 thread2.Join();
111 // TSan seems to mess with gtest's death detection.
112 // Fail intentionally, and rely on detecting the error message.
113 RTC_CHECK(false);
114}
115
116TEST(SanitizersDeathTest, ThreadSanitizer) {
117 EXPECT_DEATH(DataRace(), "data race");
118}
119#endif
120
121} // namespace
122
123} // namespace rtc