1 //===-- sanitizer_platform.h ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Common platform macros. 10 //===----------------------------------------------------------------------===// 11 12 #ifndef SANITIZER_PLATFORM_H 13 #define SANITIZER_PLATFORM_H 14 15 #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \ 16 !defined(__APPLE__) && !defined(_WIN32) && !defined(__Fuchsia__) && \ 17 !(defined(__sun__) && defined(__svr4__)) && !defined(__OpenBSD__) 18 # error "This operating system is not supported" 19 #endif 20 21 // Get __GLIBC__ on a glibc platform. Exclude Android: features.h includes C 22 // function declarations into a .S file which doesn't compile. 23 // https://crbug.com/1162741 24 #if __has_include(<features.h>) && !defined(__ANDROID__) 25 # include <features.h> 26 #endif 27 28 #if defined(__linux__) 29 # define SANITIZER_LINUX 1 30 #else 31 # define SANITIZER_LINUX 0 32 #endif 33 34 #if defined(__GLIBC__) 35 # define SANITIZER_GLIBC 1 36 #else 37 # define SANITIZER_GLIBC 0 38 #endif 39 40 #if defined(__FreeBSD__) 41 # define SANITIZER_FREEBSD 1 42 #else 43 # define SANITIZER_FREEBSD 0 44 #endif 45 46 #if defined(__NetBSD__) 47 # define SANITIZER_NETBSD 1 48 #else 49 # define SANITIZER_NETBSD 0 50 #endif 51 52 #if defined(__sun__) && defined(__svr4__) 53 # define SANITIZER_SOLARIS 1 54 #else 55 # define SANITIZER_SOLARIS 0 56 #endif 57 58 #if defined(__OpenBSD__) 59 # define SANITIZER_OPENBSD 1 60 #else 61 # define SANITIZER_OPENBSD 0 62 #endif 63 64 // - SANITIZER_APPLE: all Apple code 65 // - TARGET_OS_OSX: macOS 66 // - SANITIZER_IOS: devices (iOS and iOS-like) 67 // - SANITIZER_WATCHOS 68 // - SANITIZER_TVOS 69 // - SANITIZER_IOSSIM: simulators (iOS and iOS-like) 70 // - SANITIZER_DRIVERKIT 71 #if defined(__APPLE__) 72 # define SANITIZER_APPLE 1 73 # include <TargetConditionals.h> 74 # if TARGET_OS_OSX 75 # define SANITIZER_OSX 1 76 # else 77 # define SANITIZER_OSX 0 78 # endif 79 # if TARGET_OS_IPHONE 80 # define SANITIZER_IOS 1 81 # else 82 # define SANITIZER_IOS 0 83 # endif 84 # if TARGET_OS_WATCH 85 # define SANITIZER_WATCHOS 1 86 # else 87 # define SANITIZER_WATCHOS 0 88 # endif 89 # if TARGET_OS_TV 90 # define SANITIZER_TVOS 1 91 # else 92 # define SANITIZER_TVOS 0 93 # endif 94 # if TARGET_OS_SIMULATOR 95 # define SANITIZER_IOSSIM 1 96 # else 97 # define SANITIZER_IOSSIM 0 98 # endif 99 # if defined(TARGET_OS_DRIVERKIT) && TARGET_OS_DRIVERKIT 100 # define SANITIZER_DRIVERKIT 1 101 # else 102 # define SANITIZER_DRIVERKIT 0 103 # endif 104 #else 105 # define SANITIZER_APPLE 0 106 # define SANITIZER_OSX 0 107 # define SANITIZER_IOS 0 108 # define SANITIZER_WATCHOS 0 109 # define SANITIZER_TVOS 0 110 # define SANITIZER_IOSSIM 0 111 # define SANITIZER_DRIVERKIT 0 112 #endif 113 114 #if defined(_WIN32) 115 # define SANITIZER_WINDOWS 1 116 #else 117 # define SANITIZER_WINDOWS 0 118 #endif 119 120 #if defined(_WIN64) 121 # define SANITIZER_WINDOWS64 1 122 #else 123 # define SANITIZER_WINDOWS64 0 124 #endif 125 126 #if defined(__ANDROID__) 127 # define SANITIZER_ANDROID 1 128 #else 129 # define SANITIZER_ANDROID 0 130 #endif 131 132 #if defined(__Fuchsia__) 133 # define SANITIZER_FUCHSIA 1 134 #else 135 # define SANITIZER_FUCHSIA 0 136 #endif 137 138 // Assume linux that is not glibc or android is musl libc. 139 #if SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID 140 # define SANITIZER_MUSL 1 141 #else 142 # define SANITIZER_MUSL 0 143 #endif 144 145 #define SANITIZER_POSIX \ 146 (SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_APPLE || \ 147 SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_OPENBSD) 148 149 #if __LP64__ || defined(_WIN64) 150 # define SANITIZER_WORDSIZE 64 151 #else 152 # define SANITIZER_WORDSIZE 32 153 #endif 154 155 #if SANITIZER_WORDSIZE == 64 156 # define FIRST_32_SECOND_64(a, b) (b) 157 #else 158 # define FIRST_32_SECOND_64(a, b) (a) 159 #endif 160 161 #if defined(__x86_64__) && !defined(_LP64) 162 # define SANITIZER_X32 1 163 #else 164 # define SANITIZER_X32 0 165 #endif 166 167 #if defined(__x86_64__) || defined(_M_X64) 168 # define SANITIZER_X64 1 169 #else 170 # define SANITIZER_X64 0 171 #endif 172 173 #if defined(__i386__) || defined(_M_IX86) 174 # define SANITIZER_I386 1 175 #else 176 # define SANITIZER_I386 0 177 #endif 178 179 #if defined(__mips__) 180 # define SANITIZER_MIPS 1 181 # if defined(__mips64) && _MIPS_SIM == _ABI64 182 # define SANITIZER_MIPS32 0 183 # define SANITIZER_MIPS64 1 184 # else 185 # define SANITIZER_MIPS32 1 186 # define SANITIZER_MIPS64 0 187 # endif 188 #else 189 # define SANITIZER_MIPS 0 190 # define SANITIZER_MIPS32 0 191 # define SANITIZER_MIPS64 0 192 #endif 193 194 #if defined(__s390__) 195 # define SANITIZER_S390 1 196 # if defined(__s390x__) 197 # define SANITIZER_S390_31 0 198 # define SANITIZER_S390_64 1 199 # else 200 # define SANITIZER_S390_31 1 201 # define SANITIZER_S390_64 0 202 # endif 203 #else 204 # define SANITIZER_S390 0 205 # define SANITIZER_S390_31 0 206 # define SANITIZER_S390_64 0 207 #endif 208 209 #if defined(__sparc__) 210 # define SANITIZER_SPARC 1 211 # if defined(__arch64__) 212 # define SANITIZER_SPARC32 0 213 # define SANITIZER_SPARC64 1 214 # else 215 # define SANITIZER_SPARC32 1 216 # define SANITIZER_SPARC64 0 217 # endif 218 #else 219 # define SANITIZER_SPARC 0 220 # define SANITIZER_SPARC32 0 221 # define SANITIZER_SPARC64 0 222 #endif 223 224 #if defined(__powerpc__) 225 # define SANITIZER_PPC 1 226 # if defined(__powerpc64__) 227 # define SANITIZER_PPC32 0 228 # define SANITIZER_PPC64 1 229 // 64-bit PPC has two ABIs (v1 and v2). The old powerpc64 target is 230 // big-endian, and uses v1 ABI (known for its function descriptors), 231 // while the new powerpc64le target is little-endian and uses v2. 232 // In theory, you could convince gcc to compile for their evil twins 233 // (eg. big-endian v2), but you won't find such combinations in the wild 234 // (it'd require bootstrapping a whole system, which would be quite painful 235 // - there's no target triple for that). LLVM doesn't support them either. 236 # if _CALL_ELF == 2 237 # define SANITIZER_PPC64V1 0 238 # define SANITIZER_PPC64V2 1 239 # else 240 # define SANITIZER_PPC64V1 1 241 # define SANITIZER_PPC64V2 0 242 # endif 243 # else 244 # define SANITIZER_PPC32 1 245 # define SANITIZER_PPC64 0 246 # define SANITIZER_PPC64V1 0 247 # define SANITIZER_PPC64V2 0 248 # endif 249 #else 250 # define SANITIZER_PPC 0 251 # define SANITIZER_PPC32 0 252 # define SANITIZER_PPC64 0 253 # define SANITIZER_PPC64V1 0 254 # define SANITIZER_PPC64V2 0 255 #endif 256 257 #if defined(__arm__) || defined(_M_ARM) 258 # define SANITIZER_ARM 1 259 #else 260 # define SANITIZER_ARM 0 261 #endif 262 263 #if defined(__aarch64__) || defined(_M_ARM64) 264 # define SANITIZER_ARM64 1 265 #else 266 # define SANITIZER_ARM64 0 267 #endif 268 269 #if SANITIZER_SOLARIS && SANITIZER_WORDSIZE == 32 270 # define SANITIZER_SOLARIS32 1 271 #else 272 # define SANITIZER_SOLARIS32 0 273 #endif 274 275 #if defined(__riscv) && (__riscv_xlen == 64) 276 # define SANITIZER_RISCV64 1 277 #else 278 # define SANITIZER_RISCV64 0 279 #endif 280 281 #if defined(__loongarch_lp64) 282 # define SANITIZER_LOONGARCH64 1 283 #else 284 # define SANITIZER_LOONGARCH64 0 285 #endif 286 287 // By default we allow to use SizeClassAllocator64 on 64-bit platform. 288 // But in some cases SizeClassAllocator64 does not work well and we need to 289 // fallback to SizeClassAllocator32. 290 // For such platforms build this code with -DSANITIZER_CAN_USE_ALLOCATOR64=0 or 291 // change the definition of SANITIZER_CAN_USE_ALLOCATOR64 here. 292 #ifndef SANITIZER_CAN_USE_ALLOCATOR64 293 # if SANITIZER_RISCV64 || SANITIZER_IOS 294 # define SANITIZER_CAN_USE_ALLOCATOR64 0 295 # elif defined(__mips64) || defined(__hexagon__) 296 # define SANITIZER_CAN_USE_ALLOCATOR64 0 297 # else 298 # define SANITIZER_CAN_USE_ALLOCATOR64 (SANITIZER_WORDSIZE == 64) 299 # endif 300 #endif 301 302 // The range of addresses which can be returned my mmap. 303 // FIXME: this value should be different on different platforms. Larger values 304 // will still work but will consume more memory for TwoLevelByteMap. 305 #if defined(__mips__) 306 # if SANITIZER_GO && defined(__mips64) 307 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47) 308 # else 309 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 40) 310 # endif 311 #elif SANITIZER_RISCV64 312 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 38) 313 #elif defined(__aarch64__) 314 # if SANITIZER_APPLE 315 # if SANITIZER_OSX || SANITIZER_IOSSIM 316 # define SANITIZER_MMAP_RANGE_SIZE \ 317 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47) 318 # else 319 // Darwin iOS/ARM64 has a 36-bit VMA, 64GiB VM 320 # define SANITIZER_MMAP_RANGE_SIZE \ 321 FIRST_32_SECOND_64(1ULL << 32, 1ULL << 36) 322 # endif 323 # else 324 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 48) 325 # endif 326 #elif defined(__sparc__) 327 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 52) 328 #else 329 # define SANITIZER_MMAP_RANGE_SIZE FIRST_32_SECOND_64(1ULL << 32, 1ULL << 47) 330 #endif 331 332 // Whether the addresses are sign-extended from the VMA range to the word. 333 // The SPARC64 Linux port implements this to split the VMA space into two 334 // non-contiguous halves with a huge hole in the middle. 335 #if defined(__sparc__) && SANITIZER_WORDSIZE == 64 336 # define SANITIZER_SIGN_EXTENDED_ADDRESSES 1 337 #else 338 # define SANITIZER_SIGN_EXTENDED_ADDRESSES 0 339 #endif 340 341 // udi16 syscalls can only be used when the following conditions are 342 // met: 343 // * target is one of arm32, x86-32, sparc32, sh or m68k 344 // * libc version is libc5, glibc-2.0, glibc-2.1 or glibc-2.2 to 2.15 345 // built against > linux-2.2 kernel headers 346 // Since we don't want to include libc headers here, we check the 347 // target only. 348 #if defined(__arm__) || SANITIZER_X32 || defined(__sparc__) 349 # define SANITIZER_USES_UID16_SYSCALLS 1 350 #else 351 # define SANITIZER_USES_UID16_SYSCALLS 0 352 #endif 353 354 #if defined(__mips__) 355 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 10) 356 #else 357 # define SANITIZER_POINTER_FORMAT_LENGTH FIRST_32_SECOND_64(8, 12) 358 #endif 359 360 /// \macro MSC_PREREQ 361 /// \brief Is the compiler MSVC of at least the specified version? 362 /// The common \param version values to check for are: 363 /// * 1800: Microsoft Visual Studio 2013 / 12.0 364 /// * 1900: Microsoft Visual Studio 2015 / 14.0 365 #ifdef _MSC_VER 366 # define MSC_PREREQ(version) (_MSC_VER >= (version)) 367 #else 368 # define MSC_PREREQ(version) 0 369 #endif 370 371 #if SANITIZER_APPLE && defined(__x86_64__) 372 # define SANITIZER_NON_UNIQUE_TYPEINFO 0 373 #else 374 # define SANITIZER_NON_UNIQUE_TYPEINFO 1 375 #endif 376 377 // On linux, some architectures had an ABI transition from 64-bit long double 378 // (ie. same as double) to 128-bit long double. On those, glibc symbols 379 // involving long doubles come in two versions, and we need to pass the 380 // correct one to dlvsym when intercepting them. 381 #if SANITIZER_LINUX && (SANITIZER_S390 || SANITIZER_PPC32 || SANITIZER_PPC64V1) 382 # define SANITIZER_NLDBL_VERSION "GLIBC_2.4" 383 #endif 384 385 #if SANITIZER_GO == 0 386 # define SANITIZER_GO 0 387 #endif 388 389 // On PowerPC and ARM Thumb, calling pthread_exit() causes LSan to detect leaks. 390 // pthread_exit() performs unwinding that leads to dlopen'ing libgcc_s.so. 391 // dlopen mallocs "libgcc_s.so" string which confuses LSan, it fails to realize 392 // that this allocation happens in dynamic linker and should be ignored. 393 #if SANITIZER_PPC || defined(__thumb__) 394 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 1 395 #else 396 # define SANITIZER_SUPPRESS_LEAK_ON_PTHREAD_EXIT 0 397 #endif 398 399 #if SANITIZER_FREEBSD || SANITIZER_APPLE || SANITIZER_NETBSD || SANITIZER_SOLARIS 400 # define SANITIZER_MADVISE_DONTNEED MADV_FREE 401 #else 402 # define SANITIZER_MADVISE_DONTNEED MADV_DONTNEED 403 #endif 404 405 // Older gcc have issues aligning to a constexpr, and require an integer. 406 // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56859 among others. 407 #if defined(__powerpc__) || defined(__powerpc64__) 408 # define SANITIZER_CACHE_LINE_SIZE 128 409 #else 410 # define SANITIZER_CACHE_LINE_SIZE 64 411 #endif 412 413 // Enable offline markup symbolizer for Fuchsia. 414 #if SANITIZER_FUCHSIA 415 # define SANITIZER_SYMBOLIZER_MARKUP 1 416 #else 417 # define SANITIZER_SYMBOLIZER_MARKUP 0 418 #endif 419 420 // Enable ability to support sanitizer initialization that is 421 // compatible with the sanitizer library being loaded via 422 // `dlopen()`. 423 #if SANITIZER_APPLE 424 # define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 1 425 #else 426 # define SANITIZER_SUPPORTS_INIT_FOR_DLOPEN 0 427 #endif 428 429 // SANITIZER_SUPPORTS_THREADLOCAL 430 // 1 - THREADLOCAL macro is supported by target 431 // 0 - THREADLOCAL macro is not supported by target 432 #ifndef __has_feature 433 // TODO: Support other compilers here 434 # define SANITIZER_SUPPORTS_THREADLOCAL 1 435 #else 436 # if __has_feature(tls) 437 # define SANITIZER_SUPPORTS_THREADLOCAL 1 438 # else 439 # define SANITIZER_SUPPORTS_THREADLOCAL 0 440 # endif 441 #endif 442 443 #if defined(__thumb__) && defined(__linux__) 444 // Workaround for 445 // https://lab.llvm.org/buildbot/#/builders/clang-thumbv7-full-2stage 446 // or 447 // https://lab.llvm.org/staging/#/builders/clang-thumbv7-full-2stage 448 // It fails *rss_limit_mb_test* without meaningful errors. 449 # define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 1 450 #else 451 # define SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL 0 452 #endif 453 454 #endif // SANITIZER_PLATFORM_H 455