compiler_test.cc 6.9 KB

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