miscompile_with_no_unique_address_test.cc
Go to the documentation of this file.
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 
16 
17 #include <gtest/gtest.h>
18 
19 // Make a template argument to test which bit pattern remains in A's destructor
20 // to try and detect similar bugs in non-MSAN builds (none have been detected
21 // yet thankfully)
22 template <int kInit>
23 class A {
24  public:
25  ~A() { EXPECT_EQ(a_, kInit); }
26  int a_ = kInit;
27 };
28 template <class T, int kInit>
29 class P : A<kInit> {
30  public:
31  explicit P(T b) : b_(b) {}
32  // clang 11 with MSAN miscompiles this and marks A::a_ as uninitialized during
33  // P::~P() if GPR_NO_UNIQUE_ADDRESS is [[no_unique_address]] - so this test
34  // stands to ensure that we have a working definition for this compiler so
35  // that we don't flag false negatives elsewhere in the codebase.
37 };
38 
39 template <int kInit, class T>
40 void c(T a) {
41  P<T, kInit> _(a);
42 }
43 
44 TEST(Miscompile, Zero) {
45  c<0>([] {});
46 }
47 
48 TEST(Miscompile, One) {
49  c<1>([] {});
50 }
51 
52 TEST(Miscompile, MinusOne) {
53  c<-1>([] {});
54 }
55 
56 int main(int argc, char** argv) {
57  ::testing::InitGoogleTest(&argc, argv);
58  return RUN_ALL_TESTS();
59 }
TEST
TEST(Miscompile, Zero)
Definition: miscompile_with_no_unique_address_test.cc:44
main
int main(int argc, char **argv)
Definition: miscompile_with_no_unique_address_test.cc:56
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
T
#define T(upbtypeconst, upbtype, ctype, default_value)
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
P::P
P(T b)
Definition: miscompile_with_no_unique_address_test.cc:31
P::b_
GPR_NO_UNIQUE_ADDRESS T b_
Definition: miscompile_with_no_unique_address_test.cc:36
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GPR_NO_UNIQUE_ADDRESS
#define GPR_NO_UNIQUE_ADDRESS
Definition: impl/codegen/port_platform.h:692
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
P
Definition: miscompile_with_no_unique_address_test.cc:29
A::~A
~A()
Definition: miscompile_with_no_unique_address_test.cc:25
port_platform.h
A
Definition: miscompile_with_no_unique_address_test.cc:23
A::a_
int a_
Definition: miscompile_with_no_unique_address_test.cc:26


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:29