Home | History | Annotate | Download | only in base
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_BASE_MACROS_H_
      6 #define V8_BASE_MACROS_H_
      7 
      8 #include <limits>
      9 
     10 #include "src/base/compiler-specific.h"
     11 #include "src/base/format-macros.h"
     12 #include "src/base/logging.h"
     13 
     14 // No-op macro which is used to work around MSVC's funky VA_ARGS support.
     15 #define EXPAND(x) x
     16 
     17 // TODO(all) Replace all uses of this macro with C++'s offsetof. To do that, we
     18 // have to make sure that only standard-layout types and simple field
     19 // designators are used.
     20 #define OFFSET_OF(type, field) \
     21   (reinterpret_cast<intptr_t>(&(reinterpret_cast<type*>(16)->field)) - 16)
     22 
     23 
     24 // The arraysize(arr) macro returns the # of elements in an array arr.
     25 // The expression is a compile-time constant, and therefore can be
     26 // used in defining new arrays, for example.  If you use arraysize on
     27 // a pointer by mistake, you will get a compile-time error.
     28 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
     29 
     30 
     31 // This template function declaration is used in defining arraysize.
     32 // Note that the function doesn't need an implementation, as we only
     33 // use its type.
     34 template <typename T, size_t N>
     35 char (&ArraySizeHelper(T (&array)[N]))[N];
     36 
     37 
     38 #if !V8_CC_MSVC
     39 // That gcc wants both of these prototypes seems mysterious. VC, for
     40 // its part, can't decide which to use (another mystery). Matching of
     41 // template overloads: the final frontier.
     42 template <typename T, size_t N>
     43 char (&ArraySizeHelper(const T (&array)[N]))[N];
     44 #endif
     45 
     46 // bit_cast<Dest,Source> is a template function that implements the
     47 // equivalent of "*reinterpret_cast<Dest*>(&source)".  We need this in
     48 // very low-level functions like the protobuf library and fast math
     49 // support.
     50 //
     51 //   float f = 3.14159265358979;
     52 //   int i = bit_cast<int32>(f);
     53 //   // i = 0x40490fdb
     54 //
     55 // The classical address-casting method is:
     56 //
     57 //   // WRONG
     58 //   float f = 3.14159265358979;            // WRONG
     59 //   int i = * reinterpret_cast<int*>(&f);  // WRONG
     60 //
     61 // The address-casting method actually produces undefined behavior
     62 // according to ISO C++ specification section 3.10 -15 -.  Roughly, this
     63 // section says: if an object in memory has one type, and a program
     64 // accesses it with a different type, then the result is undefined
     65 // behavior for most values of "different type".
     66 //
     67 // This is true for any cast syntax, either *(int*)&f or
     68 // *reinterpret_cast<int*>(&f).  And it is particularly true for
     69 // conversions between integral lvalues and floating-point lvalues.
     70 //
     71 // The purpose of 3.10 -15- is to allow optimizing compilers to assume
     72 // that expressions with different types refer to different memory.  gcc
     73 // 4.0.1 has an optimizer that takes advantage of this.  So a
     74 // non-conforming program quietly produces wildly incorrect output.
     75 //
     76 // The problem is not the use of reinterpret_cast.  The problem is type
     77 // punning: holding an object in memory of one type and reading its bits
     78 // back using a different type.
     79 //
     80 // The C++ standard is more subtle and complex than this, but that
     81 // is the basic idea.
     82 //
     83 // Anyways ...
     84 //
     85 // bit_cast<> calls memcpy() which is blessed by the standard,
     86 // especially by the example in section 3.9 .  Also, of course,
     87 // bit_cast<> wraps up the nasty logic in one place.
     88 //
     89 // Fortunately memcpy() is very fast.  In optimized mode, with a
     90 // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
     91 // code with the minimal amount of data movement.  On a 32-bit system,
     92 // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
     93 // compiles to two loads and two stores.
     94 //
     95 // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
     96 //
     97 // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
     98 // is likely to surprise you.
     99 template <class Dest, class Source>
    100 V8_INLINE Dest bit_cast(Source const& source) {
    101   static_assert(sizeof(Dest) == sizeof(Source),
    102                 "source and dest must be same size");
    103   Dest dest;
    104   memcpy(&dest, &source, sizeof(dest));
    105   return dest;
    106 }
    107 
    108 // Explicitly declare the assignment operator as deleted.
    109 #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete;
    110 
    111 // Explicitly declare the copy constructor and assignment operator as deleted.
    112 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
    113   TypeName(const TypeName&) = delete;      \
    114   DISALLOW_ASSIGN(TypeName)
    115 
    116 // Explicitly declare all copy/move constructors and assignments as deleted.
    117 #define DISALLOW_COPY_AND_MOVE_AND_ASSIGN(TypeName) \
    118   TypeName(TypeName&&) = delete;                    \
    119   TypeName& operator=(TypeName&&) = delete;         \
    120   DISALLOW_COPY_AND_ASSIGN(TypeName)
    121 
    122 // Explicitly declare all implicit constructors as deleted, namely the
    123 // default constructor, copy constructor and operator= functions.
    124 // This is especially useful for classes containing only static methods.
    125 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
    126   TypeName() = delete;                           \
    127   DISALLOW_COPY_AND_ASSIGN(TypeName)
    128 
    129 // Disallow copying a type, but provide default construction, move construction
    130 // and move assignment. Especially useful for move-only structs.
    131 #define MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(TypeName) \
    132   TypeName() = default;                               \
    133   MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(TypeName)
    134 
    135 // Disallow copying a type, and only provide move construction and move
    136 // assignment. Especially useful for move-only structs.
    137 #define MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(TypeName)       \
    138   TypeName(TypeName&&) V8_NOEXCEPT = default;            \
    139   TypeName& operator=(TypeName&&) V8_NOEXCEPT = default; \
    140   DISALLOW_COPY_AND_ASSIGN(TypeName)
    141 
    142 // A macro to disallow the dynamic allocation.
    143 // This should be used in the private: declarations for a class
    144 // Declaring operator new and delete as deleted is not spec compliant.
    145 // Extract from 3.2.2 of C++11 spec:
    146 //  [...] A non-placement deallocation function for a class is
    147 //  odr-used by the definition of the destructor of that class, [...]
    148 #define DISALLOW_NEW_AND_DELETE()                            \
    149   void* operator new(size_t) { base::OS::Abort(); }          \
    150   void* operator new[](size_t) { base::OS::Abort(); };       \
    151   void operator delete(void*, size_t) { base::OS::Abort(); } \
    152   void operator delete[](void*, size_t) { base::OS::Abort(); }
    153 
    154 // Define V8_USE_ADDRESS_SANITIZER macro.
    155 #if defined(__has_feature)
    156 #if __has_feature(address_sanitizer)
    157 #define V8_USE_ADDRESS_SANITIZER 1
    158 #endif
    159 #endif
    160 
    161 // Define DISABLE_ASAN macro.
    162 #ifdef V8_USE_ADDRESS_SANITIZER
    163 #define DISABLE_ASAN __attribute__((no_sanitize_address))
    164 #else
    165 #define DISABLE_ASAN
    166 #endif
    167 
    168 // Define V8_USE_MEMORY_SANITIZER macro.
    169 #if defined(__has_feature)
    170 #if __has_feature(memory_sanitizer)
    171 #define V8_USE_MEMORY_SANITIZER 1
    172 #endif
    173 #endif
    174 
    175 // Helper macro to define no_sanitize attributes only with clang.
    176 #if defined(__clang__) && defined(__has_attribute)
    177 #if __has_attribute(no_sanitize)
    178 #define CLANG_NO_SANITIZE(what) __attribute__((no_sanitize(what)))
    179 #endif
    180 #endif
    181 #if !defined(CLANG_NO_SANITIZE)
    182 #define CLANG_NO_SANITIZE(what)
    183 #endif
    184 
    185 // DISABLE_CFI_PERF -- Disable Control Flow Integrity checks for Perf reasons.
    186 #define DISABLE_CFI_PERF CLANG_NO_SANITIZE("cfi")
    187 
    188 // DISABLE_CFI_ICALL -- Disable Control Flow Integrity indirect call checks,
    189 // useful because calls into JITed code can not be CFI verified.
    190 #define DISABLE_CFI_ICALL CLANG_NO_SANITIZE("cfi-icall")
    191 
    192 #if V8_CC_GNU
    193 #define V8_IMMEDIATE_CRASH() __builtin_trap()
    194 #else
    195 #define V8_IMMEDIATE_CRASH() ((void(*)())0)()
    196 #endif
    197 
    198 
    199 // TODO(all) Replace all uses of this macro with static_assert, remove macro.
    200 #define STATIC_ASSERT(test) static_assert(test, #test)
    201 
    202 namespace v8 {
    203 namespace base {
    204 
    205 // Note that some implementations of std::is_trivially_copyable mandate that at
    206 // least one of the copy constructor, move constructor, copy assignment or move
    207 // assignment is non-deleted, while others do not. Be aware that also
    208 // base::is_trivially_copyable will differ for these cases.
    209 template <typename T>
    210 struct is_trivially_copyable {
    211 #if V8_CC_MSVC
    212   // Unfortunately, MSVC 2015 is broken in that std::is_trivially_copyable can
    213   // be false even though it should be true according to the standard.
    214   // (status at 2018-02-26, observed on the msvc waterfall bot).
    215   // Interestingly, the lower-level primitives used below are working as
    216   // intended, so we reimplement this according to the standard.
    217   // See also https://developercommunity.visualstudio.com/content/problem/
    218   //          170883/msvc-type-traits-stdis-trivial-is-bugged.html.
    219   static constexpr bool value =
    220       // Copy constructor is trivial or deleted.
    221       (std::is_trivially_copy_constructible<T>::value ||
    222        !std::is_copy_constructible<T>::value) &&
    223       // Copy assignment operator is trivial or deleted.
    224       (std::is_trivially_copy_assignable<T>::value ||
    225        !std::is_copy_assignable<T>::value) &&
    226       // Move constructor is trivial or deleted.
    227       (std::is_trivially_move_constructible<T>::value ||
    228        !std::is_move_constructible<T>::value) &&
    229       // Move assignment operator is trivial or deleted.
    230       (std::is_trivially_move_assignable<T>::value ||
    231        !std::is_move_assignable<T>::value) &&
    232       // (Some implementations mandate that one of the above is non-deleted, but
    233       // the standard does not, so let's skip this check.)
    234       // Trivial non-deleted destructor.
    235       std::is_trivially_destructible<T>::value;
    236 
    237 #elif defined(__GNUC__) && __GNUC__ < 5
    238   // WARNING:
    239   // On older libstdc++ versions, there is no way to correctly implement
    240   // is_trivially_copyable. The workaround below is an approximation (neither
    241   // over- nor underapproximation). E.g. it wrongly returns true if the move
    242   // constructor is non-trivial, and it wrongly returns false if the copy
    243   // constructor is deleted, but copy assignment is trivial.
    244   // TODO(rongjie) Remove this workaround once we require gcc >= 5.0
    245   static constexpr bool value =
    246       __has_trivial_copy(T) && __has_trivial_destructor(T);
    247 
    248 #else
    249   static constexpr bool value = std::is_trivially_copyable<T>::value;
    250 #endif
    251 };
    252 #if defined(__GNUC__) && __GNUC__ < 5
    253 // On older libstdc++ versions, base::is_trivially_copyable<T>::value is only an
    254 // approximation (see above), so make ASSERT_{NOT_,}TRIVIALLY_COPYABLE a noop.
    255 #define ASSERT_TRIVIALLY_COPYABLE(T) static_assert(true, "check disabled")
    256 #define ASSERT_NOT_TRIVIALLY_COPYABLE(T) static_assert(true, "check disabled")
    257 #else
    258 #define ASSERT_TRIVIALLY_COPYABLE(T)                         \
    259   static_assert(::v8::base::is_trivially_copyable<T>::value, \
    260                 #T " should be trivially copyable")
    261 #define ASSERT_NOT_TRIVIALLY_COPYABLE(T)                      \
    262   static_assert(!::v8::base::is_trivially_copyable<T>::value, \
    263                 #T " should not be trivially copyable")
    264 #endif
    265 
    266 // The USE(x, ...) template is used to silence C++ compiler warnings
    267 // issued for (yet) unused variables (typically parameters).
    268 // The arguments are guaranteed to be evaluated from left to right.
    269 struct Use {
    270   template <typename T>
    271   Use(T&&) {}  // NOLINT(runtime/explicit)
    272 };
    273 #define USE(...)                                                   \
    274   do {                                                             \
    275     ::v8::base::Use unused_tmp_array_for_use_macro[]{__VA_ARGS__}; \
    276     (void)unused_tmp_array_for_use_macro;                          \
    277   } while (false)
    278 
    279 }  // namespace base
    280 }  // namespace v8
    281 
    282 // implicit_cast<A>(x) triggers an implicit cast from {x} to type {A}. This is
    283 // useful in situations where static_cast<A>(x) would do too much.
    284 // Only use this for cheap-to-copy types, or use move semantics explicitly.
    285 template <class A>
    286 V8_INLINE A implicit_cast(A x) {
    287   return x;
    288 }
    289 
    290 // Define our own macros for writing 64-bit constants.  This is less fragile
    291 // than defining __STDC_CONSTANT_MACROS before including <stdint.h>, and it
    292 // works on compilers that don't have it (like MSVC).
    293 #if V8_CC_MSVC
    294 # if V8_HOST_ARCH_64_BIT
    295 #  define V8_PTR_PREFIX   "ll"
    296 # else
    297 #  define V8_PTR_PREFIX   ""
    298 # endif  // V8_HOST_ARCH_64_BIT
    299 #elif V8_CC_MINGW64
    300 # define V8_PTR_PREFIX    "I64"
    301 #elif V8_HOST_ARCH_64_BIT
    302 # define V8_PTR_PREFIX    "l"
    303 #else
    304 #if V8_OS_AIX
    305 #define V8_PTR_PREFIX "l"
    306 #else
    307 # define V8_PTR_PREFIX    ""
    308 #endif
    309 #endif
    310 
    311 #define V8PRIxPTR V8_PTR_PREFIX "x"
    312 #define V8PRIdPTR V8_PTR_PREFIX "d"
    313 #define V8PRIuPTR V8_PTR_PREFIX "u"
    314 
    315 #ifdef V8_TARGET_ARCH_64_BIT
    316 #define V8_PTR_HEX_DIGITS 12
    317 #define V8PRIxPTR_FMT "0x%012" V8PRIxPTR
    318 #else
    319 #define V8_PTR_HEX_DIGITS 8
    320 #define V8PRIxPTR_FMT "0x%08" V8PRIxPTR
    321 #endif
    322 
    323 // ptrdiff_t is 't' according to the standard, but MSVC uses 'I'.
    324 #if V8_CC_MSVC
    325 #define V8PRIxPTRDIFF "Ix"
    326 #define V8PRIdPTRDIFF "Id"
    327 #define V8PRIuPTRDIFF "Iu"
    328 #else
    329 #define V8PRIxPTRDIFF "tx"
    330 #define V8PRIdPTRDIFF "td"
    331 #define V8PRIuPTRDIFF "tu"
    332 #endif
    333 
    334 // Fix for Mac OS X defining uintptr_t as "unsigned long":
    335 #if V8_OS_MACOSX
    336 #undef V8PRIxPTR
    337 #define V8PRIxPTR "lx"
    338 #undef V8PRIdPTR
    339 #define V8PRIdPTR "ld"
    340 #undef V8PRIuPTR
    341 #define V8PRIuPTR "lxu"
    342 #endif
    343 
    344 // The following macro works on both 32 and 64-bit platforms.
    345 // Usage: instead of writing 0x1234567890123456
    346 //      write V8_2PART_UINT64_C(0x12345678,90123456);
    347 #define V8_2PART_UINT64_C(a, b) (((static_cast<uint64_t>(a) << 32) + 0x##b##u))
    348 
    349 
    350 // Compute the 0-relative offset of some absolute value x of type T.
    351 // This allows conversion of Addresses and integral types into
    352 // 0-relative int offsets.
    353 template <typename T>
    354 constexpr inline intptr_t OffsetFrom(T x) {
    355   return x - static_cast<T>(0);
    356 }
    357 
    358 
    359 // Compute the absolute value of type T for some 0-relative offset x.
    360 // This allows conversion of 0-relative int offsets into Addresses and
    361 // integral types.
    362 template <typename T>
    363 constexpr inline T AddressFrom(intptr_t x) {
    364   return static_cast<T>(static_cast<T>(0) + x);
    365 }
    366 
    367 
    368 // Return the largest multiple of m which is <= x.
    369 template <typename T>
    370 inline T RoundDown(T x, intptr_t m) {
    371   // m must be a power of two.
    372   DCHECK(m != 0 && ((m & (m - 1)) == 0));
    373   return AddressFrom<T>(OffsetFrom(x) & -m);
    374 }
    375 template <intptr_t m, typename T>
    376 constexpr inline T RoundDown(T x) {
    377   // m must be a power of two.
    378   STATIC_ASSERT(m != 0 && ((m & (m - 1)) == 0));
    379   return AddressFrom<T>(OffsetFrom(x) & -m);
    380 }
    381 
    382 // Return the smallest multiple of m which is >= x.
    383 template <typename T>
    384 inline T RoundUp(T x, intptr_t m) {
    385   return RoundDown<T>(static_cast<T>(x + m - 1), m);
    386 }
    387 template <intptr_t m, typename T>
    388 constexpr inline T RoundUp(T x) {
    389   return RoundDown<m, T>(static_cast<T>(x + m - 1));
    390 }
    391 
    392 inline void* AlignedAddress(void* address, size_t alignment) {
    393   // The alignment must be a power of two.
    394   DCHECK_EQ(alignment & (alignment - 1), 0u);
    395   return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) &
    396                                  ~static_cast<uintptr_t>(alignment - 1));
    397 }
    398 
    399 // Bounds checks for float to integer conversions, which does truncation. Hence,
    400 // the range of legal values is (min - 1, max + 1).
    401 template <typename int_t, typename float_t, typename biggest_int_t = int64_t>
    402 bool is_inbounds(float_t v) {
    403   static_assert(sizeof(int_t) < sizeof(biggest_int_t),
    404                 "int_t can't be bounds checked by the compiler");
    405   constexpr float_t kLowerBound =
    406       static_cast<float_t>(std::numeric_limits<int_t>::min()) - 1;
    407   constexpr float_t kUpperBound =
    408       static_cast<float_t>(std::numeric_limits<int_t>::max()) + 1;
    409   constexpr bool kLowerBoundIsMin =
    410       static_cast<biggest_int_t>(kLowerBound) ==
    411       static_cast<biggest_int_t>(std::numeric_limits<int_t>::min());
    412   constexpr bool kUpperBoundIsMax =
    413       static_cast<biggest_int_t>(kUpperBound) ==
    414       static_cast<biggest_int_t>(std::numeric_limits<int_t>::max());
    415   return (kLowerBoundIsMin ? (kLowerBound <= v) : (kLowerBound < v)) &&
    416          (kUpperBoundIsMax ? (v <= kUpperBound) : (v < kUpperBound));
    417 }
    418 
    419 #ifdef V8_OS_WIN
    420 
    421 // Setup for Windows shared library export.
    422 #ifdef BUILDING_V8_SHARED
    423 #define V8_EXPORT_PRIVATE __declspec(dllexport)
    424 #elif USING_V8_SHARED
    425 #define V8_EXPORT_PRIVATE __declspec(dllimport)
    426 #else
    427 #define V8_EXPORT_PRIVATE
    428 #endif  // BUILDING_V8_SHARED
    429 
    430 #else  // V8_OS_WIN
    431 
    432 // Setup for Linux shared library export.
    433 #if V8_HAS_ATTRIBUTE_VISIBILITY
    434 #ifdef BUILDING_V8_SHARED
    435 #define V8_EXPORT_PRIVATE __attribute__((visibility("default")))
    436 #else
    437 #define V8_EXPORT_PRIVATE
    438 #endif
    439 #else
    440 #define V8_EXPORT_PRIVATE
    441 #endif
    442 
    443 #endif  // V8_OS_WIN
    444 
    445 #endif  // V8_BASE_MACROS_H_
    446