1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <memory>
16
17 #include "gtest/gtest.h"
18
19 #include <grpc/support/port_platform.h>
20
21 // Make a template argument to test which bit pattern remains in A's destructor
22 // to try and detect similar bugs in non-MSAN builds (none have been detected
23 // yet thankfully)
24 template <int kInit>
25 class A {
26 public:
~A()27 ~A() { EXPECT_EQ(a_, kInit); }
28 int a_ = kInit;
29 };
30 template <class T, int kInit>
31 class P : A<kInit> {
32 public:
P(T b)33 explicit P(T b) : b_(b) {}
34 // clang 11 with MSAN miscompiles this and marks A::a_ as uninitialized during
35 // P::~P() if GPR_NO_UNIQUE_ADDRESS is [[no_unique_address]] - so this test
36 // stands to ensure that we have a working definition for this compiler so
37 // that we don't flag false negatives elsewhere in the codebase.
38 GPR_NO_UNIQUE_ADDRESS T b_;
39 };
40
41 template <int kInit, class T>
c(T a)42 void c(T a) {
43 P<T, kInit> _(a);
44 }
45
TEST(Miscompile,Zero)46 TEST(Miscompile, Zero) {
47 c<0>([] {});
48 }
49
TEST(Miscompile,One)50 TEST(Miscompile, One) {
51 c<1>([] {});
52 }
53
TEST(Miscompile,MinusOne)54 TEST(Miscompile, MinusOne) {
55 c<-1>([] {});
56 }
57
main(int argc,char ** argv)58 int main(int argc, char** argv) {
59 ::testing::InitGoogleTest(&argc, argv);
60 return RUN_ALL_TESTS();
61 }
62