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 template <typename T>
CheckRepresentation(T value)26 static void CheckRepresentation(T value) {
27 SCOPED_TRACE(value);
28
29 // Convert to the corresponding two's-complement unsigned value. We use an
30 // unsigned value so the right-shift below has defined value. Right-shifts of
31 // negative numbers in C are implementation defined.
32 //
33 // If |T| is already unsigned, this is a no-op, as desired.
34 //
35 // If |T| is signed, conversion to unsigned is defined to repeatedly add or
36 // subtract (numerically, not within |T|) one more than the unsigned type's
37 // maximum value until it fits (this must be a power of two). This is the
38 // conversion we want.
39 using UnsignedT = typename std::make_unsigned<T>::type;
40 UnsignedT value_u = static_cast<UnsignedT>(value);
41 EXPECT_EQ(sizeof(UnsignedT), sizeof(T));
42
43 // Integers must be little-endian.
44 uint8_t expected[sizeof(UnsignedT)];
45 for (size_t i = 0; i < sizeof(UnsignedT); i++) {
46 expected[i] = static_cast<uint8_t>(value_u);
47 // Divide instead of right-shift to appease compilers that warn if |T| is a
48 // char. The explicit cast is also needed to appease MSVC if integer
49 // promotion happened.
50 value_u = static_cast<UnsignedT>(value_u / 256);
51 }
52 EXPECT_EQ(0u, value_u);
53
54 // Check that |value| has the expected representation.
55 EXPECT_EQ(Bytes(expected),
56 Bytes(reinterpret_cast<const uint8_t *>(&value), sizeof(value)));
57 }
58
TEST(CompilerTest,IntegerRepresentation)59 TEST(CompilerTest, IntegerRepresentation) {
60 EXPECT_EQ(8, CHAR_BIT);
61 EXPECT_EQ(0xff, static_cast<int>(UCHAR_MAX));
62
63 // uint8_t is assumed to be unsigned char. I.e., casting to uint8_t should be
64 // as good as unsigned char for strict aliasing purposes.
65 uint8_t u8 = 0;
66 unsigned char *ptr = &u8;
67 (void)ptr;
68
69 // Sized integers have the expected size.
70 EXPECT_EQ(1u, sizeof(uint8_t));
71 EXPECT_EQ(2u, sizeof(uint16_t));
72 EXPECT_EQ(4u, sizeof(uint32_t));
73 EXPECT_EQ(8u, sizeof(uint64_t));
74
75 // size_t does not exceed uint64_t.
76 EXPECT_LE(sizeof(size_t), 8u);
77
78 // int must be 32-bit or larger.
79 EXPECT_LE(0x7fffffff, INT_MAX);
80 EXPECT_LE(0xffffffffu, UINT_MAX);
81
82 CheckRepresentation(static_cast<signed char>(127));
83 CheckRepresentation(static_cast<signed char>(1));
84 CheckRepresentation(static_cast<signed char>(0));
85 CheckRepresentation(static_cast<signed char>(-1));
86 CheckRepresentation(static_cast<signed char>(-42));
87 CheckRepresentation(static_cast<signed char>(-128));
88
89 CheckRepresentation(static_cast<int>(INT_MAX));
90 CheckRepresentation(static_cast<int>(0x12345678));
91 CheckRepresentation(static_cast<int>(1));
92 CheckRepresentation(static_cast<int>(0));
93 CheckRepresentation(static_cast<int>(-1));
94 CheckRepresentation(static_cast<int>(-0x12345678));
95 CheckRepresentation(static_cast<int>(INT_MIN));
96
97 CheckRepresentation(static_cast<unsigned>(UINT_MAX));
98 CheckRepresentation(static_cast<unsigned>(0x12345678));
99 CheckRepresentation(static_cast<unsigned>(1));
100 CheckRepresentation(static_cast<unsigned>(0));
101
102 CheckRepresentation(static_cast<long>(LONG_MAX));
103 CheckRepresentation(static_cast<long>(0x12345678));
104 CheckRepresentation(static_cast<long>(1));
105 CheckRepresentation(static_cast<long>(0));
106 CheckRepresentation(static_cast<long>(-1));
107 CheckRepresentation(static_cast<long>(-0x12345678));
108 CheckRepresentation(static_cast<long>(LONG_MIN));
109
110 CheckRepresentation(static_cast<unsigned long>(ULONG_MAX));
111 CheckRepresentation(static_cast<unsigned long>(0x12345678));
112 CheckRepresentation(static_cast<unsigned long>(1));
113 CheckRepresentation(static_cast<unsigned long>(0));
114
115 CheckRepresentation(static_cast<int16_t>(0x7fff));
116 CheckRepresentation(static_cast<int16_t>(0x1234));
117 CheckRepresentation(static_cast<int16_t>(1));
118 CheckRepresentation(static_cast<int16_t>(0));
119 CheckRepresentation(static_cast<int16_t>(-1));
120 CheckRepresentation(static_cast<int16_t>(-0x7fff - 1));
121
122 CheckRepresentation(static_cast<uint16_t>(0xffff));
123 CheckRepresentation(static_cast<uint16_t>(0x1234));
124 CheckRepresentation(static_cast<uint16_t>(1));
125 CheckRepresentation(static_cast<uint16_t>(0));
126
127 CheckRepresentation(static_cast<int32_t>(0x7fffffff));
128 CheckRepresentation(static_cast<int32_t>(0x12345678));
129 CheckRepresentation(static_cast<int32_t>(1));
130 CheckRepresentation(static_cast<int32_t>(0));
131 CheckRepresentation(static_cast<int32_t>(-1));
132 CheckRepresentation(static_cast<int32_t>(-0x7fffffff - 1));
133
134 CheckRepresentation(static_cast<uint32_t>(0xffffffff));
135 CheckRepresentation(static_cast<uint32_t>(0x12345678));
136 CheckRepresentation(static_cast<uint32_t>(1));
137 CheckRepresentation(static_cast<uint32_t>(0));
138
139 CheckRepresentation(static_cast<int64_t>(0x7fffffffffffffff));
140 CheckRepresentation(static_cast<int64_t>(0x123456789abcdef0));
141 CheckRepresentation(static_cast<int64_t>(1));
142 CheckRepresentation(static_cast<int64_t>(0));
143 CheckRepresentation(static_cast<int64_t>(-1));
144 CheckRepresentation(static_cast<int64_t>(-0x7fffffffffffffff - 1));
145
146 CheckRepresentation(static_cast<uint64_t>(0xffffffffffffffff));
147 CheckRepresentation(static_cast<uint64_t>(0x12345678abcdef0));
148 CheckRepresentation(static_cast<uint64_t>(1));
149 CheckRepresentation(static_cast<uint64_t>(0));
150 }
151
TEST(CompilerTest,PointerRepresentation)152 TEST(CompilerTest, PointerRepresentation) {
153 // Converting pointers to integers and doing arithmetic on those values are
154 // both defined. Converting those values back into pointers is undefined,
155 // but, for aliasing checks, we require that the implementation-defined
156 // result of that computation commutes with pointer arithmetic.
157 char chars[256];
158 for (size_t i = 0; i < sizeof(chars); i++) {
159 EXPECT_EQ(reinterpret_cast<uintptr_t>(chars) + i,
160 reinterpret_cast<uintptr_t>(chars + i));
161 }
162
163 int ints[256];
164 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(ints); i++) {
165 EXPECT_EQ(reinterpret_cast<uintptr_t>(ints) + i * sizeof(int),
166 reinterpret_cast<uintptr_t>(ints + i));
167 }
168
169 // nullptr must be represented by all zeros in memory. This is necessary so
170 // structs may be initialized by memset(0).
171 int *null = nullptr;
172 uint8_t bytes[sizeof(null)] = {0};
173 EXPECT_EQ(Bytes(bytes),
174 Bytes(reinterpret_cast<uint8_t *>(&null), sizeof(null)));
175 }
176