sanitizer_platform.h 12 KB

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