asan_mapping.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //===-- asan_mapping.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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Defines ASan memory mapping.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef ASAN_MAPPING_H
  14. #define ASAN_MAPPING_H
  15. #include "sanitizer_common/sanitizer_platform.h"
  16. // The full explanation of the memory mapping could be found here:
  17. // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm
  18. //
  19. // Typical shadow mapping on Linux/x86_64 with SHADOW_OFFSET == 0x00007fff8000:
  20. // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
  21. // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
  22. // || `[0x00008fff7000, 0x02008fff6fff]` || ShadowGap ||
  23. // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
  24. // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
  25. //
  26. // When SHADOW_OFFSET is zero (-pie):
  27. // || `[0x100000000000, 0x7fffffffffff]` || HighMem ||
  28. // || `[0x020000000000, 0x0fffffffffff]` || HighShadow ||
  29. // || `[0x000000040000, 0x01ffffffffff]` || ShadowGap ||
  30. //
  31. // Special case when something is already mapped between
  32. // 0x003000000000 and 0x005000000000 (e.g. when prelink is installed):
  33. // || `[0x10007fff8000, 0x7fffffffffff]` || HighMem ||
  34. // || `[0x02008fff7000, 0x10007fff7fff]` || HighShadow ||
  35. // || `[0x005000000000, 0x02008fff6fff]` || ShadowGap3 ||
  36. // || `[0x003000000000, 0x004fffffffff]` || MidMem ||
  37. // || `[0x000a7fff8000, 0x002fffffffff]` || ShadowGap2 ||
  38. // || `[0x00067fff8000, 0x000a7fff7fff]` || MidShadow ||
  39. // || `[0x00008fff7000, 0x00067fff7fff]` || ShadowGap ||
  40. // || `[0x00007fff8000, 0x00008fff6fff]` || LowShadow ||
  41. // || `[0x000000000000, 0x00007fff7fff]` || LowMem ||
  42. //
  43. // Default Linux/i386 mapping on x86_64 machine:
  44. // || `[0x40000000, 0xffffffff]` || HighMem ||
  45. // || `[0x28000000, 0x3fffffff]` || HighShadow ||
  46. // || `[0x24000000, 0x27ffffff]` || ShadowGap ||
  47. // || `[0x20000000, 0x23ffffff]` || LowShadow ||
  48. // || `[0x00000000, 0x1fffffff]` || LowMem ||
  49. //
  50. // Default Linux/i386 mapping on i386 machine
  51. // (addresses starting with 0xc0000000 are reserved
  52. // for kernel and thus not sanitized):
  53. // || `[0x38000000, 0xbfffffff]` || HighMem ||
  54. // || `[0x27000000, 0x37ffffff]` || HighShadow ||
  55. // || `[0x24000000, 0x26ffffff]` || ShadowGap ||
  56. // || `[0x20000000, 0x23ffffff]` || LowShadow ||
  57. // || `[0x00000000, 0x1fffffff]` || LowMem ||
  58. //
  59. // Default Linux/MIPS32 mapping:
  60. // || `[0x2aaa0000, 0xffffffff]` || HighMem ||
  61. // || `[0x0fff4000, 0x2aa9ffff]` || HighShadow ||
  62. // || `[0x0bff4000, 0x0fff3fff]` || ShadowGap ||
  63. // || `[0x0aaa0000, 0x0bff3fff]` || LowShadow ||
  64. // || `[0x00000000, 0x0aa9ffff]` || LowMem ||
  65. //
  66. // Default Linux/MIPS64 mapping:
  67. // || `[0x4000000000, 0xffffffffff]` || HighMem ||
  68. // || `[0x2800000000, 0x3fffffffff]` || HighShadow ||
  69. // || `[0x2400000000, 0x27ffffffff]` || ShadowGap ||
  70. // || `[0x2000000000, 0x23ffffffff]` || LowShadow ||
  71. // || `[0x0000000000, 0x1fffffffff]` || LowMem ||
  72. //
  73. // Default Linux/RISCV64 Sv39 mapping:
  74. // || `[0x1555550000, 0x3fffffffff]` || HighMem ||
  75. // || `[0x0fffffa000, 0x1555555fff]` || HighShadow ||
  76. // || `[0x0effffa000, 0x0fffff9fff]` || ShadowGap ||
  77. // || `[0x0d55550000, 0x0effff9fff]` || LowShadow ||
  78. // || `[0x0000000000, 0x0d5554ffff]` || LowMem ||
  79. //
  80. // Default Linux/AArch64 (39-bit VMA) mapping:
  81. // || `[0x2000000000, 0x7fffffffff]` || highmem ||
  82. // || `[0x1400000000, 0x1fffffffff]` || highshadow ||
  83. // || `[0x1200000000, 0x13ffffffff]` || shadowgap ||
  84. // || `[0x1000000000, 0x11ffffffff]` || lowshadow ||
  85. // || `[0x0000000000, 0x0fffffffff]` || lowmem ||
  86. //
  87. // Default Linux/AArch64 (42-bit VMA) mapping:
  88. // || `[0x10000000000, 0x3ffffffffff]` || highmem ||
  89. // || `[0x0a000000000, 0x0ffffffffff]` || highshadow ||
  90. // || `[0x09000000000, 0x09fffffffff]` || shadowgap ||
  91. // || `[0x08000000000, 0x08fffffffff]` || lowshadow ||
  92. // || `[0x00000000000, 0x07fffffffff]` || lowmem ||
  93. //
  94. // Default Linux/S390 mapping:
  95. // || `[0x30000000, 0x7fffffff]` || HighMem ||
  96. // || `[0x26000000, 0x2fffffff]` || HighShadow ||
  97. // || `[0x24000000, 0x25ffffff]` || ShadowGap ||
  98. // || `[0x20000000, 0x23ffffff]` || LowShadow ||
  99. // || `[0x00000000, 0x1fffffff]` || LowMem ||
  100. //
  101. // Default Linux/SystemZ mapping:
  102. // || `[0x14000000000000, 0x1fffffffffffff]` || HighMem ||
  103. // || `[0x12800000000000, 0x13ffffffffffff]` || HighShadow ||
  104. // || `[0x12000000000000, 0x127fffffffffff]` || ShadowGap ||
  105. // || `[0x10000000000000, 0x11ffffffffffff]` || LowShadow ||
  106. // || `[0x00000000000000, 0x0fffffffffffff]` || LowMem ||
  107. //
  108. // Default Linux/SPARC64 (52-bit VMA) mapping:
  109. // || `[0x8000000000000, 0xfffffffffffff]` || HighMem ||
  110. // || `[0x1080000000000, 0x207ffffffffff]` || HighShadow ||
  111. // || `[0x0090000000000, 0x107ffffffffff]` || ShadowGap ||
  112. // || `[0x0080000000000, 0x008ffffffffff]` || LowShadow ||
  113. // || `[0x0000000000000, 0x007ffffffffff]` || LowMem ||
  114. //
  115. // Shadow mapping on FreeBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
  116. // || `[0x500000000000, 0x7fffffffffff]` || HighMem ||
  117. // || `[0x4a0000000000, 0x4fffffffffff]` || HighShadow ||
  118. // || `[0x480000000000, 0x49ffffffffff]` || ShadowGap ||
  119. // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
  120. // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
  121. //
  122. // Shadow mapping on FreeBSD/i386 with SHADOW_OFFSET == 0x40000000:
  123. // || `[0x60000000, 0xffffffff]` || HighMem ||
  124. // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
  125. // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
  126. // || `[0x40000000, 0x47ffffff]` || LowShadow ||
  127. // || `[0x00000000, 0x3fffffff]` || LowMem ||
  128. //
  129. // Shadow mapping on NetBSD/x86-64 with SHADOW_OFFSET == 0x400000000000:
  130. // || `[0x4feffffffe01, 0x7f7ffffff000]` || HighMem ||
  131. // || `[0x49fdffffffc0, 0x4feffffffe00]` || HighShadow ||
  132. // || `[0x480000000000, 0x49fdffffffbf]` || ShadowGap ||
  133. // || `[0x400000000000, 0x47ffffffffff]` || LowShadow ||
  134. // || `[0x000000000000, 0x3fffffffffff]` || LowMem ||
  135. //
  136. // Shadow mapping on NetBSD/i386 with SHADOW_OFFSET == 0x40000000:
  137. // || `[0x60000000, 0xfffff000]` || HighMem ||
  138. // || `[0x4c000000, 0x5fffffff]` || HighShadow ||
  139. // || `[0x48000000, 0x4bffffff]` || ShadowGap ||
  140. // || `[0x40000000, 0x47ffffff]` || LowShadow ||
  141. // || `[0x00000000, 0x3fffffff]` || LowMem ||
  142. //
  143. // Default Windows/i386 mapping:
  144. // (the exact location of HighShadow/HighMem may vary depending
  145. // on WoW64, /LARGEADDRESSAWARE, etc).
  146. // || `[0x50000000, 0xffffffff]` || HighMem ||
  147. // || `[0x3a000000, 0x4fffffff]` || HighShadow ||
  148. // || `[0x36000000, 0x39ffffff]` || ShadowGap ||
  149. // || `[0x30000000, 0x35ffffff]` || LowShadow ||
  150. // || `[0x00000000, 0x2fffffff]` || LowMem ||
  151. #define ASAN_SHADOW_SCALE 3
  152. #if SANITIZER_FUCHSIA
  153. # define ASAN_SHADOW_OFFSET_CONST (0)
  154. #elif SANITIZER_WORDSIZE == 32
  155. # if SANITIZER_ANDROID
  156. # define ASAN_SHADOW_OFFSET_DYNAMIC
  157. # elif defined(__mips__)
  158. # define ASAN_SHADOW_OFFSET_CONST 0x0aaa0000
  159. # elif SANITIZER_FREEBSD
  160. # define ASAN_SHADOW_OFFSET_CONST 0x40000000
  161. # elif SANITIZER_NETBSD
  162. # define ASAN_SHADOW_OFFSET_CONST 0x40000000
  163. # elif SANITIZER_WINDOWS
  164. # define ASAN_SHADOW_OFFSET_CONST 0x30000000
  165. # elif SANITIZER_IOS
  166. # define ASAN_SHADOW_OFFSET_DYNAMIC
  167. # else
  168. # define ASAN_SHADOW_OFFSET_CONST 0x20000000
  169. # endif
  170. #else
  171. # if SANITIZER_IOS
  172. # define ASAN_SHADOW_OFFSET_DYNAMIC
  173. # elif SANITIZER_MAC && defined(__aarch64__)
  174. # define ASAN_SHADOW_OFFSET_DYNAMIC
  175. # elif SANITIZER_RISCV64
  176. # define ASAN_SHADOW_OFFSET_CONST 0x0000000d55550000
  177. # elif defined(__aarch64__)
  178. # define ASAN_SHADOW_OFFSET_CONST 0x0000001000000000
  179. # elif defined(__powerpc64__)
  180. # define ASAN_SHADOW_OFFSET_CONST 0x0000100000000000
  181. # elif defined(__s390x__)
  182. # define ASAN_SHADOW_OFFSET_CONST 0x0010000000000000
  183. # elif SANITIZER_FREEBSD
  184. # define ASAN_SHADOW_OFFSET_CONST 0x0000400000000000
  185. # elif SANITIZER_NETBSD
  186. # define ASAN_SHADOW_OFFSET_CONST 0x0000400000000000
  187. # elif SANITIZER_MAC
  188. # define ASAN_SHADOW_OFFSET_CONST 0x0000100000000000
  189. # elif defined(__mips64)
  190. # define ASAN_SHADOW_OFFSET_CONST 0x0000002000000000
  191. # elif defined(__sparc__)
  192. # define ASAN_SHADOW_OFFSET_CONST 0x0000080000000000
  193. # elif SANITIZER_WINDOWS64
  194. # define ASAN_SHADOW_OFFSET_DYNAMIC
  195. # else
  196. # if ASAN_SHADOW_SCALE != 3
  197. # error "Value below is based on shadow scale = 3."
  198. # error "Original formula was: 0x7FFFFFFF & (~0xFFFULL << SHADOW_SCALE)."
  199. # endif
  200. # define ASAN_SHADOW_OFFSET_CONST 0x000000007fff8000
  201. # endif
  202. #endif
  203. #if defined(__cplusplus)
  204. # include "asan_internal.h"
  205. static const u64 kDefaultShadowSentinel = ~(uptr)0;
  206. # if defined(ASAN_SHADOW_OFFSET_CONST)
  207. static const u64 kConstShadowOffset = ASAN_SHADOW_OFFSET_CONST;
  208. # define ASAN_SHADOW_OFFSET kConstShadowOffset
  209. # elif defined(ASAN_SHADOW_OFFSET_DYNAMIC)
  210. # define ASAN_SHADOW_OFFSET __asan_shadow_memory_dynamic_address
  211. # else
  212. # error "ASAN_SHADOW_OFFSET can't be determined."
  213. # endif
  214. # if SANITIZER_ANDROID && defined(__arm__)
  215. # define ASAN_PREMAP_SHADOW 1
  216. # else
  217. # define ASAN_PREMAP_SHADOW 0
  218. # endif
  219. # define ASAN_SHADOW_GRANULARITY (1ULL << ASAN_SHADOW_SCALE)
  220. # define DO_ASAN_MAPPING_PROFILE 0 // Set to 1 to profile the functions below.
  221. # if DO_ASAN_MAPPING_PROFILE
  222. # define PROFILE_ASAN_MAPPING() AsanMappingProfile[__LINE__]++;
  223. # else
  224. # define PROFILE_ASAN_MAPPING()
  225. # endif
  226. // If 1, all shadow boundaries are constants.
  227. // Don't set to 1 other than for testing.
  228. # define ASAN_FIXED_MAPPING 0
  229. namespace __asan {
  230. extern uptr AsanMappingProfile[];
  231. # if ASAN_FIXED_MAPPING
  232. // Fixed mapping for 64-bit Linux. Mostly used for performance comparison
  233. // with non-fixed mapping. As of r175253 (Feb 2013) the performance
  234. // difference between fixed and non-fixed mapping is below the noise level.
  235. static uptr kHighMemEnd = 0x7fffffffffffULL;
  236. static uptr kMidMemBeg = 0x3000000000ULL;
  237. static uptr kMidMemEnd = 0x4fffffffffULL;
  238. # else
  239. extern uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; // Initialized in __asan_init.
  240. # endif
  241. } // namespace __asan
  242. # if defined(__sparc__) && SANITIZER_WORDSIZE == 64
  243. # include "asan_mapping_sparc64.h"
  244. # else
  245. # define MEM_TO_SHADOW(mem) \
  246. (((mem) >> ASAN_SHADOW_SCALE) + (ASAN_SHADOW_OFFSET))
  247. # define kLowMemBeg 0
  248. # define kLowMemEnd (ASAN_SHADOW_OFFSET ? ASAN_SHADOW_OFFSET - 1 : 0)
  249. # define kLowShadowBeg ASAN_SHADOW_OFFSET
  250. # define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd)
  251. # define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1)
  252. # define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg)
  253. # define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd)
  254. # define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg)
  255. # define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd)
  256. // With the zero shadow base we can not actually map pages starting from 0.
  257. // This constant is somewhat arbitrary.
  258. # define kZeroBaseShadowStart 0
  259. # define kZeroBaseMaxShadowStart (1 << 18)
  260. # define kShadowGapBeg \
  261. (kLowShadowEnd ? kLowShadowEnd + 1 : kZeroBaseShadowStart)
  262. # define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1)
  263. # define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0)
  264. # define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0)
  265. # define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0)
  266. # define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0)
  267. namespace __asan {
  268. static inline bool AddrIsInLowMem(uptr a) {
  269. PROFILE_ASAN_MAPPING();
  270. return a <= kLowMemEnd;
  271. }
  272. static inline bool AddrIsInLowShadow(uptr a) {
  273. PROFILE_ASAN_MAPPING();
  274. return a >= kLowShadowBeg && a <= kLowShadowEnd;
  275. }
  276. static inline bool AddrIsInMidMem(uptr a) {
  277. PROFILE_ASAN_MAPPING();
  278. return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd;
  279. }
  280. static inline bool AddrIsInMidShadow(uptr a) {
  281. PROFILE_ASAN_MAPPING();
  282. return kMidMemBeg && a >= kMidShadowBeg && a <= kMidShadowEnd;
  283. }
  284. static inline bool AddrIsInHighMem(uptr a) {
  285. PROFILE_ASAN_MAPPING();
  286. return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd;
  287. }
  288. static inline bool AddrIsInHighShadow(uptr a) {
  289. PROFILE_ASAN_MAPPING();
  290. return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd;
  291. }
  292. static inline bool AddrIsInShadowGap(uptr a) {
  293. PROFILE_ASAN_MAPPING();
  294. if (kMidMemBeg) {
  295. if (a <= kShadowGapEnd)
  296. return ASAN_SHADOW_OFFSET == 0 || a >= kShadowGapBeg;
  297. return (a >= kShadowGap2Beg && a <= kShadowGap2End) ||
  298. (a >= kShadowGap3Beg && a <= kShadowGap3End);
  299. }
  300. // In zero-based shadow mode we treat addresses near zero as addresses
  301. // in shadow gap as well.
  302. if (ASAN_SHADOW_OFFSET == 0)
  303. return a <= kShadowGapEnd;
  304. return a >= kShadowGapBeg && a <= kShadowGapEnd;
  305. }
  306. } // namespace __asan
  307. # endif
  308. namespace __asan {
  309. static inline uptr MemToShadowSize(uptr size) {
  310. return size >> ASAN_SHADOW_SCALE;
  311. }
  312. static inline bool AddrIsInMem(uptr a) {
  313. PROFILE_ASAN_MAPPING();
  314. return AddrIsInLowMem(a) || AddrIsInMidMem(a) || AddrIsInHighMem(a) ||
  315. (flags()->protect_shadow_gap == 0 && AddrIsInShadowGap(a));
  316. }
  317. static inline uptr MemToShadow(uptr p) {
  318. PROFILE_ASAN_MAPPING();
  319. CHECK(AddrIsInMem(p));
  320. return MEM_TO_SHADOW(p);
  321. }
  322. static inline bool AddrIsInShadow(uptr a) {
  323. PROFILE_ASAN_MAPPING();
  324. return AddrIsInLowShadow(a) || AddrIsInMidShadow(a) || AddrIsInHighShadow(a);
  325. }
  326. static inline bool AddrIsAlignedByGranularity(uptr a) {
  327. PROFILE_ASAN_MAPPING();
  328. return (a & (ASAN_SHADOW_GRANULARITY - 1)) == 0;
  329. }
  330. static inline bool AddressIsPoisoned(uptr a) {
  331. PROFILE_ASAN_MAPPING();
  332. const uptr kAccessSize = 1;
  333. u8 *shadow_address = (u8 *)MEM_TO_SHADOW(a);
  334. s8 shadow_value = *shadow_address;
  335. if (shadow_value) {
  336. u8 last_accessed_byte =
  337. (a & (ASAN_SHADOW_GRANULARITY - 1)) + kAccessSize - 1;
  338. return (last_accessed_byte >= shadow_value);
  339. }
  340. return false;
  341. }
  342. // Must be after all calls to PROFILE_ASAN_MAPPING().
  343. static const uptr kAsanMappingProfileSize = __LINE__;
  344. } // namespace __asan
  345. #endif // __cplusplus
  346. #endif // ASAN_MAPPING_H