sanitizer_platform.h 12 KB

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