|
| 1 | +// Copyright (c) 2009-2010 Satoshi Nakamoto |
| 2 | +// Copyright (c) 2009-2020 The Bitcoin Core developers |
| 3 | +// Distributed under the MIT software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +// Compile-time verification of assumptions we make. |
| 7 | + |
| 8 | +#ifndef BITCOIN_COMPAT_ASSUMPTIONS_H |
| 9 | +#define BITCOIN_COMPAT_ASSUMPTIONS_H |
| 10 | + |
| 11 | +#include <limits> |
| 12 | + |
| 13 | +// Assumption: We assume that the macro NDEBUG is not defined. |
| 14 | +// Example(s): We use assert(...) extensively with the assumption of it never |
| 15 | +// being a noop at runtime. |
| 16 | +#if defined(NDEBUG) |
| 17 | +# error "Bitcoin cannot be compiled without assertions." |
| 18 | +#endif |
| 19 | + |
| 20 | +// Assumption: We assume a C++17 (ISO/IEC 14882:2017) compiler (minimum requirement). |
| 21 | +// Example(s): We assume the presence of C++17 features everywhere :-) |
| 22 | +// ISO Standard C++17 [cpp.predefined]p1: |
| 23 | +// "The name __cplusplus is defined to the value 201703L when compiling a C++ |
| 24 | +// translation unit." |
| 25 | +static_assert(__cplusplus >= 201703L, "C++17 standard assumed"); |
| 26 | + |
| 27 | +// Assumption: We assume the floating-point types to fulfill the requirements of |
| 28 | +// IEC 559 (IEEE 754) standard. |
| 29 | +// Example(s): Floating-point division by zero in ConnectBlock, CreateTransaction |
| 30 | +// and EstimateMedianVal. |
| 31 | +static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 float assumed"); |
| 32 | +static_assert(std::numeric_limits<double>::is_iec559, "IEEE 754 double assumed"); |
| 33 | + |
| 34 | +// Assumption: We assume eight bits per byte (obviously, but remember: don't |
| 35 | +// trust -- verify!). |
| 36 | +// Example(s): Everywhere :-) |
| 37 | +static_assert(std::numeric_limits<unsigned char>::digits == 8, "8-bit byte assumed"); |
| 38 | + |
| 39 | +// Assumption: We assume floating-point widths. |
| 40 | +// Example(s): Type punning in serialization code (ser_{float,double}_to_uint{32,64}). |
| 41 | +static_assert(sizeof(float) == 4, "32-bit float assumed"); |
| 42 | +static_assert(sizeof(double) == 8, "64-bit double assumed"); |
| 43 | + |
| 44 | +// Assumption: We assume integer widths. |
| 45 | +// Example(s): GetSizeOfCompactSize and WriteCompactSize in the serialization |
| 46 | +// code. |
| 47 | +static_assert(sizeof(short) == 2, "16-bit short assumed"); |
| 48 | +static_assert(sizeof(int) == 4, "32-bit int assumed"); |
| 49 | +static_assert(sizeof(unsigned) == 4, "32-bit unsigned assumed"); |
| 50 | + |
| 51 | +// Assumption: We assume size_t to be 32-bit or 64-bit. |
| 52 | +// Example(s): size_t assumed to be at least 32-bit in ecdsa_signature_parse_der_lax(...). |
| 53 | +// size_t assumed to be 32-bit or 64-bit in MallocUsage(...). |
| 54 | +static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t assumed to be 32-bit or 64-bit"); |
| 55 | +static_assert(sizeof(size_t) == sizeof(void*), "Sizes of size_t and void* assumed to be equal"); |
| 56 | + |
| 57 | +// Some important things we are NOT assuming (non-exhaustive list): |
| 58 | +// * We are NOT assuming a specific value for std::endian::native. |
| 59 | +// * We are NOT assuming a specific value for std::locale("").name(). |
| 60 | +// * We are NOT assuming a specific value for std::numeric_limits<char>::is_signed. |
| 61 | + |
| 62 | +#endif // BITCOIN_COMPAT_ASSUMPTIONS_H |
0 commit comments