github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/executor/_include/flatbuffers/base.h (about) 1 #ifndef FLATBUFFERS_BASE_H_ 2 #define FLATBUFFERS_BASE_H_ 3 4 // clang-format off 5 6 // If activate should be declared and included first. 7 #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \ 8 defined(_MSC_VER) && defined(_DEBUG) 9 // The _CRTDBG_MAP_ALLOC inside <crtdbg.h> will replace 10 // calloc/free (etc) to its debug version using #define directives. 11 #define _CRTDBG_MAP_ALLOC 12 #include <stdlib.h> 13 #include <crtdbg.h> 14 // Replace operator new by trace-enabled version. 15 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) 16 #define new DEBUG_NEW 17 #endif 18 19 #if !defined(FLATBUFFERS_ASSERT) 20 #include <assert.h> 21 #define FLATBUFFERS_ASSERT assert 22 #elif defined(FLATBUFFERS_ASSERT_INCLUDE) 23 // Include file with forward declaration 24 #include FLATBUFFERS_ASSERT_INCLUDE 25 #endif 26 27 #ifndef ARDUINO 28 #include <cstdint> 29 #endif 30 31 #include <cstddef> 32 #include <cstdlib> 33 #include <cstring> 34 35 #if defined(ARDUINO) && !defined(ARDUINOSTL_M_H) && defined(__AVR__) 36 #include <utility.h> 37 #else 38 #include <utility> 39 #endif 40 41 #include <string> 42 #include <type_traits> 43 #include <vector> 44 #include <set> 45 #include <algorithm> 46 #include <limits> 47 #include <iterator> 48 #include <memory> 49 50 #if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT) 51 #include <unistd.h> 52 #endif 53 54 #ifdef __ANDROID__ 55 #include <android/api-level.h> 56 #endif 57 58 #if defined(__ICCARM__) 59 #include <intrinsics.h> 60 #endif 61 62 // Note the __clang__ check is needed, because clang presents itself 63 // as an older GNUC compiler (4.2). 64 // Clang 3.3 and later implement all of the ISO C++ 2011 standard. 65 // Clang 3.4 and later implement all of the ISO C++ 2014 standard. 66 // http://clang.llvm.org/cxx_status.html 67 68 // Note the MSVC value '__cplusplus' may be incorrect: 69 // The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L, 70 // indicating (erroneously!) that the compiler conformed to the C++98 Standard. 71 // This value should be correct starting from MSVC2017-15.7-Preview-3. 72 // The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set. 73 // Workaround (for details see MSDN): 74 // Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility. 75 // The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch. 76 77 #if defined(__GNUC__) && !defined(__clang__) 78 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) 79 #else 80 #define FLATBUFFERS_GCC 0 81 #endif 82 83 #if defined(__clang__) 84 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) 85 #else 86 #define FLATBUFFERS_CLANG 0 87 #endif 88 89 /// @cond FLATBUFFERS_INTERNAL 90 #if __cplusplus <= 199711L && \ 91 (!defined(_MSC_VER) || _MSC_VER < 1600) && \ 92 (!defined(__GNUC__) || \ 93 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400)) 94 #error A C++11 compatible compiler with support for the auto typing is \ 95 required for FlatBuffers. 96 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ 97 #endif 98 99 #if !defined(__clang__) && \ 100 defined(__GNUC__) && \ 101 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600) 102 // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr 103 // and constexpr keywords. Note the __clang__ check is needed, because clang 104 // presents itself as an older GNUC compiler. 105 #ifndef nullptr_t 106 const class nullptr_t { 107 public: 108 template<class T> inline operator T*() const { return 0; } 109 private: 110 void operator&() const; 111 } nullptr = {}; 112 #endif 113 #ifndef constexpr 114 #define constexpr const 115 #endif 116 #endif 117 118 // The wire format uses a little endian encoding (since that's efficient for 119 // the common platforms). 120 #if defined(__s390x__) 121 #define FLATBUFFERS_LITTLEENDIAN 0 122 #endif // __s390x__ 123 #if !defined(FLATBUFFERS_LITTLEENDIAN) 124 #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__) 125 #if (defined(__BIG_ENDIAN__) || \ 126 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) 127 #define FLATBUFFERS_LITTLEENDIAN 0 128 #else 129 #define FLATBUFFERS_LITTLEENDIAN 1 130 #endif // __BIG_ENDIAN__ 131 #elif defined(_MSC_VER) 132 #if defined(_M_PPC) 133 #define FLATBUFFERS_LITTLEENDIAN 0 134 #else 135 #define FLATBUFFERS_LITTLEENDIAN 1 136 #endif 137 #else 138 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN. 139 #endif 140 #endif // !defined(FLATBUFFERS_LITTLEENDIAN) 141 142 #define FLATBUFFERS_VERSION_MAJOR 23 143 #define FLATBUFFERS_VERSION_MINOR 5 144 #define FLATBUFFERS_VERSION_REVISION 26 145 #define FLATBUFFERS_STRING_EXPAND(X) #X 146 #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) 147 namespace flatbuffers { 148 // Returns version as string "MAJOR.MINOR.REVISION". 149 const char* FLATBUFFERS_VERSION(); 150 } 151 152 #if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ 153 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \ 154 defined(__clang__) 155 #define FLATBUFFERS_FINAL_CLASS final 156 #define FLATBUFFERS_OVERRIDE override 157 #define FLATBUFFERS_EXPLICIT_CPP11 explicit 158 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t 159 #else 160 #define FLATBUFFERS_FINAL_CLASS 161 #define FLATBUFFERS_OVERRIDE 162 #define FLATBUFFERS_EXPLICIT_CPP11 163 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE 164 #endif 165 166 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ 167 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ 168 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704) 169 #define FLATBUFFERS_CONSTEXPR constexpr 170 #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr 171 #define FLATBUFFERS_CONSTEXPR_DEFINED 172 #else 173 #define FLATBUFFERS_CONSTEXPR const 174 #define FLATBUFFERS_CONSTEXPR_CPP11 175 #endif 176 177 #if (defined(__cplusplus) && __cplusplus >= 201402L) || \ 178 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304) 179 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11 180 #else 181 #define FLATBUFFERS_CONSTEXPR_CPP14 182 #endif 183 184 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \ 185 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \ 186 defined(__clang__) 187 #define FLATBUFFERS_NOEXCEPT noexcept 188 #else 189 #define FLATBUFFERS_NOEXCEPT 190 #endif 191 192 // NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to 193 // private, so be sure to put it at the end or reset access mode explicitly. 194 #if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \ 195 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \ 196 defined(__clang__) 197 #define FLATBUFFERS_DELETE_FUNC(func) func = delete 198 #else 199 #define FLATBUFFERS_DELETE_FUNC(func) private: func 200 #endif 201 202 #if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ 203 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ 204 defined(__clang__) 205 #define FLATBUFFERS_DEFAULT_DECLARATION 206 #endif 207 208 // Check if we can use template aliases 209 // Not possible if Microsoft Compiler before 2012 210 // Possible is the language feature __cpp_alias_templates is defined well 211 // Or possible if the C++ std is C+11 or newer 212 #if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \ 213 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \ 214 || (defined(__cplusplus) && __cplusplus >= 201103L) 215 #define FLATBUFFERS_TEMPLATES_ALIASES 216 #endif 217 218 #ifndef FLATBUFFERS_HAS_STRING_VIEW 219 // Only provide flatbuffers::string_view if __has_include can be used 220 // to detect a header that provides an implementation 221 #if defined(__has_include) 222 // Check for std::string_view (in c++17) 223 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17)) 224 #include <string_view> 225 namespace flatbuffers { 226 typedef std::string_view string_view; 227 } 228 #define FLATBUFFERS_HAS_STRING_VIEW 1 229 // Check for std::experimental::string_view (in c++14, compiler-dependent) 230 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411) 231 #include <experimental/string_view> 232 namespace flatbuffers { 233 typedef std::experimental::string_view string_view; 234 } 235 #define FLATBUFFERS_HAS_STRING_VIEW 1 236 // Check for absl::string_view 237 #elif __has_include("absl/strings/string_view.h") && \ 238 __has_include("absl/base/config.h") && \ 239 (__cplusplus >= 201411) 240 #include "absl/base/config.h" 241 #if !defined(ABSL_USES_STD_STRING_VIEW) 242 #include "absl/strings/string_view.h" 243 namespace flatbuffers { 244 typedef absl::string_view string_view; 245 } 246 #define FLATBUFFERS_HAS_STRING_VIEW 1 247 #endif 248 #endif 249 #endif // __has_include 250 #endif // !FLATBUFFERS_HAS_STRING_VIEW 251 252 #ifndef FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 253 // Allow heap allocations to be used 254 #define FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 1 255 #endif // !FLATBUFFERS_GENERAL_HEAP_ALLOC_OK 256 257 #ifndef FLATBUFFERS_HAS_NEW_STRTOD 258 // Modern (C++11) strtod and strtof functions are available for use. 259 // 1) nan/inf strings as argument of strtod; 260 // 2) hex-float as argument of strtod/strtof. 261 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \ 262 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \ 263 (defined(__clang__)) 264 #define FLATBUFFERS_HAS_NEW_STRTOD 1 265 #endif 266 #endif // !FLATBUFFERS_HAS_NEW_STRTOD 267 268 #ifndef FLATBUFFERS_LOCALE_INDEPENDENT 269 // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, 270 // strtoull_l}. 271 #if (defined(_MSC_VER) && _MSC_VER >= 1800) || \ 272 (defined(__ANDROID_API__) && __ANDROID_API__>= 21) || \ 273 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700)) && \ 274 (!defined(__Fuchsia__) && !defined(__ANDROID_API__)) 275 #define FLATBUFFERS_LOCALE_INDEPENDENT 1 276 #else 277 #define FLATBUFFERS_LOCALE_INDEPENDENT 0 278 #endif 279 #endif // !FLATBUFFERS_LOCALE_INDEPENDENT 280 281 // Suppress Undefined Behavior Sanitizer (recoverable only). Usage: 282 // - FLATBUFFERS_SUPPRESS_UBSAN("undefined") 283 // - FLATBUFFERS_SUPPRESS_UBSAN("signed-integer-overflow") 284 #if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7)) 285 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize(type))) 286 #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409) 287 #define FLATBUFFERS_SUPPRESS_UBSAN(type) __attribute__((no_sanitize_undefined)) 288 #else 289 #define FLATBUFFERS_SUPPRESS_UBSAN(type) 290 #endif 291 292 // This is constexpr function used for checking compile-time constants. 293 // Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`. 294 template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) { 295 return !!t; 296 } 297 298 // Enable C++ attribute [[]] if std:c++17 or higher. 299 #if ((__cplusplus >= 201703L) \ 300 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L))) 301 // All attributes unknown to an implementation are ignored without causing an error. 302 #define FLATBUFFERS_ATTRIBUTE(attr) attr 303 304 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]] 305 #else 306 #define FLATBUFFERS_ATTRIBUTE(attr) 307 308 #if FLATBUFFERS_CLANG >= 30800 309 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]] 310 #elif FLATBUFFERS_GCC >= 70300 311 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]] 312 #else 313 #define FLATBUFFERS_FALLTHROUGH() 314 #endif 315 #endif 316 317 /// @endcond 318 319 /// @file 320 namespace flatbuffers { 321 322 /// @cond FLATBUFFERS_INTERNAL 323 // Our default offset / size type, 32bit on purpose on 64bit systems. 324 // Also, using a consistent offset type maintains compatibility of serialized 325 // offset values between 32bit and 64bit systems. 326 typedef uint32_t uoffset_t; 327 typedef uint64_t uoffset64_t; 328 329 // Signed offsets for references that can go in both directions. 330 typedef int32_t soffset_t; 331 typedef int64_t soffset64_t; 332 333 // Offset/index used in v-tables, can be changed to uint8_t in 334 // format forks to save a bit of space if desired. 335 typedef uint16_t voffset_t; 336 337 typedef uintmax_t largest_scalar_t; 338 339 // In 32bits, this evaluates to 2GB - 1 340 #define FLATBUFFERS_MAX_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset_t>::max() 341 #define FLATBUFFERS_MAX_64_BUFFER_SIZE std::numeric_limits<::flatbuffers::soffset64_t>::max() 342 343 // The minimum size buffer that can be a valid flatbuffer. 344 // Includes the offset to the root table (uoffset_t), the offset to the vtable 345 // of the root table (soffset_t), the size of the vtable (uint16_t), and the 346 // size of the referring table (uint16_t). 347 #define FLATBUFFERS_MIN_BUFFER_SIZE sizeof(uoffset_t) + sizeof(soffset_t) + \ 348 sizeof(uint16_t) + sizeof(uint16_t) 349 350 // We support aligning the contents of buffers up to this size. 351 #ifndef FLATBUFFERS_MAX_ALIGNMENT 352 #define FLATBUFFERS_MAX_ALIGNMENT 32 353 #endif 354 355 /// @brief The length of a FlatBuffer file header. 356 static const size_t kFileIdentifierLength = 4; 357 358 inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) { 359 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) && 360 (align & (align - 1)) == 0; // must be power of 2 361 } 362 363 #if defined(_MSC_VER) 364 #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized 365 #pragma warning(push) 366 #pragma warning(disable: 4127) // C4127: conditional expression is constant 367 #endif 368 369 template<typename T> T EndianSwap(T t) { 370 #if defined(_MSC_VER) 371 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort 372 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong 373 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64 374 #elif defined(__ICCARM__) 375 #define FLATBUFFERS_BYTESWAP16 __REV16 376 #define FLATBUFFERS_BYTESWAP32 __REV 377 #define FLATBUFFERS_BYTESWAP64(x) \ 378 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U) 379 #else 380 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__) 381 // __builtin_bswap16 was missing prior to GCC 4.8. 382 #define FLATBUFFERS_BYTESWAP16(x) \ 383 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16)) 384 #else 385 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16 386 #endif 387 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32 388 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64 389 #endif 390 if (sizeof(T) == 1) { // Compile-time if-then's. 391 return t; 392 } else if (sizeof(T) == 2) { 393 union { T t; uint16_t i; } u = { t }; 394 u.i = FLATBUFFERS_BYTESWAP16(u.i); 395 return u.t; 396 } else if (sizeof(T) == 4) { 397 union { T t; uint32_t i; } u = { t }; 398 u.i = FLATBUFFERS_BYTESWAP32(u.i); 399 return u.t; 400 } else if (sizeof(T) == 8) { 401 union { T t; uint64_t i; } u = { t }; 402 u.i = FLATBUFFERS_BYTESWAP64(u.i); 403 return u.t; 404 } else { 405 FLATBUFFERS_ASSERT(0); 406 return t; 407 } 408 } 409 410 #if defined(_MSC_VER) 411 #pragma warning(pop) 412 #endif 413 414 415 template<typename T> T EndianScalar(T t) { 416 #if FLATBUFFERS_LITTLEENDIAN 417 return t; 418 #else 419 return EndianSwap(t); 420 #endif 421 } 422 423 template<typename T> 424 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. 425 FLATBUFFERS_SUPPRESS_UBSAN("alignment") 426 T ReadScalar(const void *p) { 427 return EndianScalar(*reinterpret_cast<const T *>(p)); 428 } 429 430 // See https://github.com/google/flatbuffers/issues/5950 431 432 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) 433 #pragma GCC diagnostic push 434 #pragma GCC diagnostic ignored "-Wstringop-overflow" 435 #endif 436 437 template<typename T> 438 // UBSAN: C++ aliasing type rules, see std::bit_cast<> for details. 439 FLATBUFFERS_SUPPRESS_UBSAN("alignment") 440 void WriteScalar(void *p, T t) { 441 *reinterpret_cast<T *>(p) = EndianScalar(t); 442 } 443 444 template<typename T> struct Offset; 445 template<typename T> FLATBUFFERS_SUPPRESS_UBSAN("alignment") void WriteScalar(void *p, Offset<T> t) { 446 *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o); 447 } 448 449 #if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000) 450 #pragma GCC diagnostic pop 451 #endif 452 453 // Computes how many bytes you'd have to pad to be able to write an 454 // "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in 455 // memory). 456 FLATBUFFERS_SUPPRESS_UBSAN("unsigned-integer-overflow") 457 inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { 458 return ((~buf_size) + 1) & (scalar_size - 1); 459 } 460 461 // Generic 'operator==' with conditional specialisations. 462 // T e - new value of a scalar field. 463 // T def - default of scalar (is known at compile-time). 464 template<typename T> inline bool IsTheSameAs(T e, T def) { return e == def; } 465 466 #if defined(FLATBUFFERS_NAN_DEFAULTS) && \ 467 defined(FLATBUFFERS_HAS_NEW_STRTOD) && (FLATBUFFERS_HAS_NEW_STRTOD > 0) 468 // Like `operator==(e, def)` with weak NaN if T=(float|double). 469 template<typename T> inline bool IsFloatTheSameAs(T e, T def) { 470 return (e == def) || ((def != def) && (e != e)); 471 } 472 template<> inline bool IsTheSameAs<float>(float e, float def) { 473 return IsFloatTheSameAs(e, def); 474 } 475 template<> inline bool IsTheSameAs<double>(double e, double def) { 476 return IsFloatTheSameAs(e, def); 477 } 478 #endif 479 480 // Check 'v' is out of closed range [low; high]. 481 // Workaround for GCC warning [-Werror=type-limits]: 482 // comparison is always true due to limited range of data type. 483 template<typename T> 484 inline bool IsOutRange(const T &v, const T &low, const T &high) { 485 return (v < low) || (high < v); 486 } 487 488 // Check 'v' is in closed range [low; high]. 489 template<typename T> 490 inline bool IsInRange(const T &v, const T &low, const T &high) { 491 return !IsOutRange(v, low, high); 492 } 493 494 } // namespace flatbuffers 495 #endif // FLATBUFFERS_BASE_H_