xref: /aosp_15_r20/external/boringssl/src/crypto/compiler_test.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <limits.h>
16 #include <stdint.h>
17 
18 #include <type_traits>
19 
20 #include <gtest/gtest.h>
21 
22 #include "test/test_util.h"
23 
24 
25 // C and C++ have two forms of unspecified behavior: undefined behavior and
26 // implementation-defined behavior.
27 //
28 // Programs that exhibit undefined behavior are invalid. Compilers are
29 // permitted to, and often do, arbitrarily miscompile them. BoringSSL thus aims
30 // to avoid undefined behavior.
31 //
32 // Implementation-defined behavior is left up to the compiler to define (or
33 // leave undefined). These are often platform-specific details, such as how big
34 // |int| is or how |uintN_t| is implemented. Programs that depend on
35 // implementation-defined behavior are not necessarily invalid, merely less
36 // portable. A compiler that provides some implementation-defined behavior is
37 // not permitted to miscompile code that depends on it.
38 //
39 // C allows a much wider range of platform behaviors than would be practical
40 // for us to support, so we make some assumptions on implementation-defined
41 // behavior. Platforms that violate those assumptions are not supported. This
42 // file aims to document and test these assumptions, so that platforms outside
43 // our scope are flagged.
44 
45 template <typename T>
CheckRepresentation(T value)46 static void CheckRepresentation(T value) {
47   SCOPED_TRACE(value);
48 
49   // Convert to the corresponding two's-complement unsigned value. We use an
50   // unsigned value so the right-shift below has defined value. Right-shifts of
51   // negative numbers in C are implementation defined.
52   //
53   // If |T| is already unsigned, this is a no-op, as desired.
54   //
55   // If |T| is signed, conversion to unsigned is defined to repeatedly add or
56   // subtract (numerically, not within |T|) one more than the unsigned type's
57   // maximum value until it fits (this must be a power of two). This is the
58   // conversion we want.
59   using UnsignedT = typename std::make_unsigned<T>::type;
60   UnsignedT value_u = static_cast<UnsignedT>(value);
61   EXPECT_EQ(sizeof(UnsignedT), sizeof(T));
62 
63   // Integers must be little-endian.
64   uint8_t expected[sizeof(UnsignedT)];
65   for (size_t i = 0; i < sizeof(UnsignedT); i++) {
66     expected[i] = static_cast<uint8_t>(value_u);
67     // Divide instead of right-shift to appease compilers that warn if |T| is a
68     // char. The explicit cast is also needed to appease MSVC if integer
69     // promotion happened.
70     value_u = static_cast<UnsignedT>(value_u / 256);
71   }
72   EXPECT_EQ(0u, value_u);
73 
74   // Check that |value| has the expected representation.
75   EXPECT_EQ(Bytes(expected),
76             Bytes(reinterpret_cast<const uint8_t *>(&value), sizeof(value)));
77 }
78 
TEST(CompilerTest,IntegerRepresentation)79 TEST(CompilerTest, IntegerRepresentation) {
80   static_assert(CHAR_BIT == 8, "BoringSSL only supports 8-bit chars");
81   static_assert(UCHAR_MAX == 0xff, "BoringSSL only supports 8-bit chars");
82 
83   // Require that |unsigned char| and |uint8_t| be the same type. We require
84   // that type-punning through |uint8_t| is not a strict aliasing violation. In
85   // principle, type-punning should be done with |memcpy|, which would make this
86   // moot.
87   //
88   // However, C made too many historical mistakes with the types and signedness
89   // of character strings. As a result, aliasing between all variations on 8-bit
90   // chars are a practical necessity for all real C code. We do not support
91   // toolchains that break this assumption.
92   static_assert(
93       std::is_same<unsigned char, uint8_t>::value,
94       "BoringSSL requires uint8_t and unsigned char be the same type");
95   uint8_t u8 = 0;
96   unsigned char *ptr = &u8;
97   (void)ptr;
98 
99   // Sized integers have the expected size.
100   static_assert(sizeof(uint8_t) == 1u, "uint8_t has the wrong size");
101   static_assert(sizeof(uint16_t) == 2u, "uint16_t has the wrong size");
102   static_assert(sizeof(uint32_t) == 4u, "uint32_t has the wrong size");
103   static_assert(sizeof(uint64_t) == 8u, "uint64_t has the wrong size");
104 
105   // size_t does not exceed uint64_t.
106   static_assert(sizeof(size_t) <= 8u, "size_t must not exceed uint64_t");
107 
108   // Require that |int| be exactly 32 bits. OpenSSL historically mixed up
109   // |unsigned| and |uint32_t|, so we require it be at least 32 bits. Requiring
110   // at most 32-bits is a bit more subtle. C promotes arithemetic operands to
111   // |int| when they fit. But this means, if |int| is 2N bits wide, multiplying
112   // two maximum-sized |uintN_t|s is undefined by integer overflow!
113   //
114   // We attempt to handle this for |uint16_t|, assuming a 32-bit |int|, but we
115   // make no attempts to correct for this with |uint32_t| for a 64-bit |int|.
116   // Thus BoringSSL does not support ILP64 platforms.
117   //
118   // This test is on |INT_MAX| and |INT32_MAX| rather than sizeof because it is
119   // theoretically allowed for sizeof(int) to be 4 but include padding bits.
120   static_assert(INT_MAX == INT32_MAX, "BoringSSL requires int be 32-bit");
121   static_assert(UINT_MAX == UINT32_MAX,
122                 "BoringSSL requires unsigned be 32-bit");
123 
124   CheckRepresentation(static_cast<signed char>(127));
125   CheckRepresentation(static_cast<signed char>(1));
126   CheckRepresentation(static_cast<signed char>(0));
127   CheckRepresentation(static_cast<signed char>(-1));
128   CheckRepresentation(static_cast<signed char>(-42));
129   CheckRepresentation(static_cast<signed char>(-128));
130 
131   CheckRepresentation(static_cast<int>(INT_MAX));
132   CheckRepresentation(static_cast<int>(0x12345678));
133   CheckRepresentation(static_cast<int>(1));
134   CheckRepresentation(static_cast<int>(0));
135   CheckRepresentation(static_cast<int>(-1));
136   CheckRepresentation(static_cast<int>(-0x12345678));
137   CheckRepresentation(static_cast<int>(INT_MIN));
138 
139   CheckRepresentation(static_cast<unsigned>(UINT_MAX));
140   CheckRepresentation(static_cast<unsigned>(0x12345678));
141   CheckRepresentation(static_cast<unsigned>(1));
142   CheckRepresentation(static_cast<unsigned>(0));
143 
144   CheckRepresentation(static_cast<long>(LONG_MAX));
145   CheckRepresentation(static_cast<long>(0x12345678));
146   CheckRepresentation(static_cast<long>(1));
147   CheckRepresentation(static_cast<long>(0));
148   CheckRepresentation(static_cast<long>(-1));
149   CheckRepresentation(static_cast<long>(-0x12345678));
150   CheckRepresentation(static_cast<long>(LONG_MIN));
151 
152   CheckRepresentation(static_cast<unsigned long>(ULONG_MAX));
153   CheckRepresentation(static_cast<unsigned long>(0x12345678));
154   CheckRepresentation(static_cast<unsigned long>(1));
155   CheckRepresentation(static_cast<unsigned long>(0));
156 
157   CheckRepresentation(static_cast<int16_t>(0x7fff));
158   CheckRepresentation(static_cast<int16_t>(0x1234));
159   CheckRepresentation(static_cast<int16_t>(1));
160   CheckRepresentation(static_cast<int16_t>(0));
161   CheckRepresentation(static_cast<int16_t>(-1));
162   CheckRepresentation(static_cast<int16_t>(-0x7fff - 1));
163 
164   CheckRepresentation(static_cast<uint16_t>(0xffff));
165   CheckRepresentation(static_cast<uint16_t>(0x1234));
166   CheckRepresentation(static_cast<uint16_t>(1));
167   CheckRepresentation(static_cast<uint16_t>(0));
168 
169   CheckRepresentation(static_cast<int32_t>(0x7fffffff));
170   CheckRepresentation(static_cast<int32_t>(0x12345678));
171   CheckRepresentation(static_cast<int32_t>(1));
172   CheckRepresentation(static_cast<int32_t>(0));
173   CheckRepresentation(static_cast<int32_t>(-1));
174   CheckRepresentation(static_cast<int32_t>(-0x7fffffff - 1));
175 
176   CheckRepresentation(static_cast<uint32_t>(0xffffffff));
177   CheckRepresentation(static_cast<uint32_t>(0x12345678));
178   CheckRepresentation(static_cast<uint32_t>(1));
179   CheckRepresentation(static_cast<uint32_t>(0));
180 
181   CheckRepresentation(static_cast<int64_t>(0x7fffffffffffffff));
182   CheckRepresentation(static_cast<int64_t>(0x123456789abcdef0));
183   CheckRepresentation(static_cast<int64_t>(1));
184   CheckRepresentation(static_cast<int64_t>(0));
185   CheckRepresentation(static_cast<int64_t>(-1));
186   CheckRepresentation(static_cast<int64_t>(-0x7fffffffffffffff - 1));
187 
188   CheckRepresentation(static_cast<uint64_t>(0xffffffffffffffff));
189   CheckRepresentation(static_cast<uint64_t>(0x12345678abcdef0));
190   CheckRepresentation(static_cast<uint64_t>(1));
191   CheckRepresentation(static_cast<uint64_t>(0));
192 }
193 
TEST(CompilerTest,PointerRepresentation)194 TEST(CompilerTest, PointerRepresentation) {
195   // Converting pointers to integers and doing arithmetic on those values are
196   // both defined. Converting those values back into pointers is undefined,
197   // but, for aliasing checks, we require that the implementation-defined
198   // result of that computation commutes with pointer arithmetic.
199   char chars[256];
200   for (size_t i = 0; i < sizeof(chars); i++) {
201     EXPECT_EQ(reinterpret_cast<uintptr_t>(chars) + i,
202               reinterpret_cast<uintptr_t>(chars + i));
203   }
204 
205   int ints[256];
206   for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(ints); i++) {
207     EXPECT_EQ(reinterpret_cast<uintptr_t>(ints) + i * sizeof(int),
208               reinterpret_cast<uintptr_t>(ints + i));
209   }
210 
211   // nullptr must be represented by all zeros in memory. This is necessary so
212   // structs may be initialized by memset(0).
213   int *null = nullptr;
214   uint8_t bytes[sizeof(null)] = {0};
215   EXPECT_EQ(Bytes(bytes),
216             Bytes(reinterpret_cast<uint8_t *>(&null), sizeof(null)));
217 }
218