2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
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.
11 #include "rtc_base/sanitizer.h"
15 #include "rtc_base/logging.h"
16 #include "test/gtest.h"
19 #include <sanitizer/msan_interface.h>
25 // Test sanitizer_impl::IsTriviallyCopyable (at compile time).
27 // Trivially copyable.
30 TrTrTr(const TrTrTr
&) = default;
31 TrTrTr
& operator=(const TrTrTr
&) = default;
34 static_assert(sanitizer_impl::IsTriviallyCopyable
<TrTrTr
>(), "");
37 TrDeTr(const TrDeTr
&) = default;
38 TrDeTr
& operator=(const TrDeTr
&) = delete;
41 static_assert(sanitizer_impl::IsTriviallyCopyable
<TrDeTr
>(), "");
43 // Non trivially copyable.
46 TrTrNt(const TrTrNt
&) = default;
47 TrTrNt
& operator=(const TrTrNt
&) = default;
50 static_assert(!sanitizer_impl::IsTriviallyCopyable
<TrTrNt
>(), "");
53 TrNtTr(const TrNtTr
&) = default;
54 TrNtTr
& operator=(const TrNtTr
&);
57 static_assert(!sanitizer_impl::IsTriviallyCopyable
<TrNtTr
>(), "");
60 TrNtNt(const TrNtNt
&) = default;
61 TrNtNt
& operator=(const TrNtNt
&);
64 static_assert(!sanitizer_impl::IsTriviallyCopyable
<TrNtNt
>(), "");
67 TrDeNt(const TrDeNt
&) = default;
68 TrDeNt
& operator=(const TrDeNt
&) = delete;
71 static_assert(!sanitizer_impl::IsTriviallyCopyable
<TrDeNt
>(), "");
74 NtTrTr(const NtTrTr
&);
75 NtTrTr
& operator=(const NtTrTr
&) = default;
78 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtTrTr
>(), "");
81 NtTrNt(const NtTrNt
&);
82 NtTrNt
& operator=(const NtTrNt
&) = default;
85 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtTrNt
>(), "");
88 NtNtTr(const NtNtTr
&);
89 NtNtTr
& operator=(const NtNtTr
&);
92 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtNtTr
>(), "");
95 NtNtNt(const NtNtNt
&);
96 NtNtNt
& operator=(const NtNtNt
&);
99 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtNtNt
>(), "");
102 NtDeTr(const NtDeTr
&);
103 NtDeTr
& operator=(const NtDeTr
&) = delete;
106 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtDeTr
>(), "");
109 NtDeNt(const NtDeNt
&);
110 NtDeNt
& operator=(const NtDeNt
&) = delete;
113 static_assert(!sanitizer_impl::IsTriviallyCopyable
<NtDeNt
>(), "");
115 // Trivially copyable types.
127 // Run the callback, and expect a crash if it *doesn't* make an uninitialized
128 // memory read. If MSan isn't on, just run the callback.
129 template <typename F
>
130 void MsanExpectUninitializedRead(F
&& f
) {
132 EXPECT_DEATH(f(), "");
140 TEST(SanitizerTest
, MsanUninitialized
) {
141 Bar bar
= MsanUninitialized
<Bar
>({});
142 // Check that a read after initialization is OK.
144 EXPECT_EQ(1u, bar
.ID
);
145 RTC_LOG(LS_INFO
) << "read after init passed";
146 // Check that other fields are uninitialized and equal to zero.
147 MsanExpectUninitializedRead([&] { EXPECT_EQ(0u, bar
.foo
.field1
); });
148 MsanExpectUninitializedRead([&] { EXPECT_EQ(0u, bar
.foo
.field2
); });
149 RTC_LOG(LS_INFO
) << "read with no init passed";