Alessio Bazzica | d31843e | 2018-04-06 16:18:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "rtc_base/sanitizer.h" |
| 12 | |
| 13 | #include "rtc_base/gunit.h" |
| 14 | #include "rtc_base/logging.h" |
| 15 | |
| 16 | #if RTC_HAS_MSAN |
| 17 | #include <sanitizer/msan_interface.h> |
| 18 | #endif |
| 19 | |
| 20 | namespace rtc { |
| 21 | namespace { |
| 22 | |
| 23 | // Test sanitizer_impl::IsTriviallyCopyable (at compile time). |
| 24 | |
| 25 | // Trivially copyable. |
| 26 | |
| 27 | struct TrTrTr { |
| 28 | TrTrTr(const TrTrTr&) = default; |
| 29 | TrTrTr& operator=(const TrTrTr&) = default; |
| 30 | ~TrTrTr() = default; |
| 31 | }; |
| 32 | static_assert(sanitizer_impl::IsTriviallyCopyable<TrTrTr>(), ""); |
| 33 | |
| 34 | struct TrDeTr { |
| 35 | TrDeTr(const TrDeTr&) = default; |
| 36 | TrDeTr& operator=(const TrDeTr&) = delete; |
| 37 | ~TrDeTr() = default; |
| 38 | }; |
| 39 | static_assert(sanitizer_impl::IsTriviallyCopyable<TrDeTr>(), ""); |
| 40 | |
| 41 | // Non trivially copyable. |
| 42 | |
| 43 | struct TrTrNt { |
| 44 | TrTrNt(const TrTrNt&) = default; |
| 45 | TrTrNt& operator=(const TrTrNt&) = default; |
| 46 | ~TrTrNt(); |
| 47 | }; |
| 48 | static_assert(!sanitizer_impl::IsTriviallyCopyable<TrTrNt>(), ""); |
| 49 | |
| 50 | struct TrNtTr { |
| 51 | TrNtTr(const TrNtTr&) = default; |
| 52 | TrNtTr& operator=(const TrNtTr&); |
| 53 | ~TrNtTr() = default; |
| 54 | }; |
| 55 | static_assert(!sanitizer_impl::IsTriviallyCopyable<TrNtTr>(), ""); |
| 56 | |
| 57 | struct TrNtNt { |
| 58 | TrNtNt(const TrNtNt&) = default; |
| 59 | TrNtNt& operator=(const TrNtNt&); |
| 60 | ~TrNtNt(); |
| 61 | }; |
| 62 | static_assert(!sanitizer_impl::IsTriviallyCopyable<TrNtNt>(), ""); |
| 63 | |
| 64 | struct TrDeNt { |
| 65 | TrDeNt(const TrDeNt&) = default; |
| 66 | TrDeNt& operator=(const TrDeNt&) = delete; |
| 67 | ~TrDeNt(); |
| 68 | }; |
| 69 | static_assert(!sanitizer_impl::IsTriviallyCopyable<TrDeNt>(), ""); |
| 70 | |
| 71 | struct NtTrTr { |
| 72 | NtTrTr(const NtTrTr&); |
| 73 | NtTrTr& operator=(const NtTrTr&) = default; |
| 74 | ~NtTrTr() = default; |
| 75 | }; |
| 76 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtTrTr>(), ""); |
| 77 | |
| 78 | struct NtTrNt { |
| 79 | NtTrNt(const NtTrNt&); |
| 80 | NtTrNt& operator=(const NtTrNt&) = default; |
| 81 | ~NtTrNt(); |
| 82 | }; |
| 83 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtTrNt>(), ""); |
| 84 | |
| 85 | struct NtNtTr { |
| 86 | NtNtTr(const NtNtTr&); |
| 87 | NtNtTr& operator=(const NtNtTr&); |
| 88 | ~NtNtTr() = default; |
| 89 | }; |
| 90 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtNtTr>(), ""); |
| 91 | |
| 92 | struct NtNtNt { |
| 93 | NtNtNt(const NtNtNt&); |
| 94 | NtNtNt& operator=(const NtNtNt&); |
| 95 | ~NtNtNt(); |
| 96 | }; |
| 97 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtNtNt>(), ""); |
| 98 | |
| 99 | struct NtDeTr { |
| 100 | NtDeTr(const NtDeTr&); |
| 101 | NtDeTr& operator=(const NtDeTr&) = delete; |
| 102 | ~NtDeTr() = default; |
| 103 | }; |
| 104 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtDeTr>(), ""); |
| 105 | |
| 106 | struct NtDeNt { |
| 107 | NtDeNt(const NtDeNt&); |
| 108 | NtDeNt& operator=(const NtDeNt&) = delete; |
| 109 | ~NtDeNt(); |
| 110 | }; |
| 111 | static_assert(!sanitizer_impl::IsTriviallyCopyable<NtDeNt>(), ""); |
| 112 | |
| 113 | // Trivially copyable types. |
| 114 | |
| 115 | struct Foo { |
| 116 | uint32_t field1; |
| 117 | uint16_t field2; |
| 118 | }; |
| 119 | |
| 120 | struct Bar { |
| 121 | uint32_t ID; |
| 122 | Foo foo; |
| 123 | }; |
| 124 | |
Mirko Bonadei | 1127fb9 | 2018-06-05 21:44:50 +0200 | [diff] [blame^] | 125 | // Run the callback, and expect a crash if it *doesn't* make an uninitialized |
| 126 | // memory read. If MSan isn't on, just run the callback. |
Alessio Bazzica | d31843e | 2018-04-06 16:18:21 +0200 | [diff] [blame] | 127 | template <typename F> |
| 128 | void MsanExpectUninitializedRead(F&& f) { |
| 129 | #if RTC_HAS_MSAN |
Mirko Bonadei | 1127fb9 | 2018-06-05 21:44:50 +0200 | [diff] [blame^] | 130 | EXPECT_DEATH(f(), ""); |
| 131 | #else |
Alessio Bazzica | d31843e | 2018-04-06 16:18:21 +0200 | [diff] [blame] | 132 | f(); |
Alessio Bazzica | d31843e | 2018-04-06 16:18:21 +0200 | [diff] [blame] | 133 | #endif |
| 134 | } |
| 135 | |
| 136 | } // namespace |
| 137 | |
Mirko Bonadei | 1127fb9 | 2018-06-05 21:44:50 +0200 | [diff] [blame^] | 138 | TEST(SanitizerTest, MsanUninitialized) { |
Alessio Bazzica | d31843e | 2018-04-06 16:18:21 +0200 | [diff] [blame] | 139 | Bar bar = MsanUninitialized<Bar>({}); |
| 140 | // Check that a read after initialization is OK. |
| 141 | bar.ID = 1; |
| 142 | EXPECT_EQ(1u, bar.ID); |
| 143 | RTC_LOG(LS_INFO) << "read after init passed"; |
| 144 | // Check that other fields are uninitialized and equal to zero. |
| 145 | MsanExpectUninitializedRead([&] { EXPECT_EQ(0u, bar.foo.field1); }); |
| 146 | MsanExpectUninitializedRead([&] { EXPECT_EQ(0u, bar.foo.field2); }); |
| 147 | RTC_LOG(LS_INFO) << "read with no init passed"; |
| 148 | } |
| 149 | |
| 150 | } // namespace rtc |