tsan_platform.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. //===-- tsan_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. // This file is a part of ThreadSanitizer (TSan), a race detector.
  10. //
  11. // Platform-specific code.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef TSAN_PLATFORM_H
  14. #define TSAN_PLATFORM_H
  15. #if !defined(__LP64__) && !defined(_WIN64)
  16. # error "Only 64-bit is supported"
  17. #endif
  18. #include "tsan_defs.h"
  19. #include "tsan_trace.h"
  20. namespace __tsan {
  21. enum {
  22. // App memory is not mapped onto shadow memory range.
  23. kBrokenMapping = 1 << 0,
  24. // Mapping app memory and back does not produce the same address,
  25. // this can lead to wrong addresses in reports and potentially
  26. // other bad consequences.
  27. kBrokenReverseMapping = 1 << 1,
  28. // Mapping is non-linear for linear user range.
  29. // This is bad and can lead to unpredictable memory corruptions, etc
  30. // because range access functions assume linearity.
  31. kBrokenLinearity = 1 << 2,
  32. };
  33. /*
  34. C/C++ on linux/x86_64 and freebsd/x86_64
  35. 0000 0000 1000 - 0080 0000 0000: main binary and/or MAP_32BIT mappings (512GB)
  36. 0040 0000 0000 - 0100 0000 0000: -
  37. 0100 0000 0000 - 2000 0000 0000: shadow
  38. 2000 0000 0000 - 3000 0000 0000: -
  39. 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
  40. 4000 0000 0000 - 5500 0000 0000: -
  41. 5500 0000 0000 - 5680 0000 0000: pie binaries without ASLR or on 4.1+ kernels
  42. 5680 0000 0000 - 6000 0000 0000: -
  43. 6000 0000 0000 - 6200 0000 0000: traces
  44. 6200 0000 0000 - 7d00 0000 0000: -
  45. 7b00 0000 0000 - 7c00 0000 0000: heap
  46. 7c00 0000 0000 - 7e80 0000 0000: -
  47. 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
  48. C/C++ on netbsd/amd64 can reuse the same mapping:
  49. * The address space starts from 0x1000 (option with 0x0) and ends with
  50. 0x7f7ffffff000.
  51. * LoAppMem-kHeapMemEnd can be reused as it is.
  52. * No VDSO support.
  53. * No MidAppMem region.
  54. * No additional HeapMem region.
  55. * HiAppMem contains the stack, loader, shared libraries and heap.
  56. * Stack on NetBSD/amd64 has prereserved 128MB.
  57. * Heap grows downwards (top-down).
  58. * ASLR must be disabled per-process or globally.
  59. */
  60. struct Mapping48AddressSpace {
  61. static const uptr kMetaShadowBeg = 0x300000000000ull;
  62. static const uptr kMetaShadowEnd = 0x340000000000ull;
  63. static const uptr kTraceMemBeg = 0x600000000000ull;
  64. static const uptr kTraceMemEnd = 0x620000000000ull;
  65. static const uptr kShadowBeg = 0x010000000000ull;
  66. static const uptr kShadowEnd = 0x200000000000ull;
  67. static const uptr kHeapMemBeg = 0x7b0000000000ull;
  68. static const uptr kHeapMemEnd = 0x7c0000000000ull;
  69. static const uptr kLoAppMemBeg = 0x000000001000ull;
  70. static const uptr kLoAppMemEnd = 0x008000000000ull;
  71. static const uptr kMidAppMemBeg = 0x550000000000ull;
  72. static const uptr kMidAppMemEnd = 0x568000000000ull;
  73. static const uptr kHiAppMemBeg = 0x7e8000000000ull;
  74. static const uptr kHiAppMemEnd = 0x800000000000ull;
  75. static const uptr kShadowMsk = 0x780000000000ull;
  76. static const uptr kShadowXor = 0x040000000000ull;
  77. static const uptr kShadowAdd = 0x000000000000ull;
  78. static const uptr kVdsoBeg = 0xf000000000000000ull;
  79. };
  80. /*
  81. C/C++ on linux/mips64 (40-bit VMA)
  82. 0000 0000 00 - 0100 0000 00: - (4 GB)
  83. 0100 0000 00 - 0200 0000 00: main binary (4 GB)
  84. 0200 0000 00 - 2000 0000 00: - (120 GB)
  85. 2000 0000 00 - 4000 0000 00: shadow (128 GB)
  86. 4000 0000 00 - 5000 0000 00: metainfo (memory blocks and sync objects) (64 GB)
  87. 5000 0000 00 - aa00 0000 00: - (360 GB)
  88. aa00 0000 00 - ab00 0000 00: main binary (PIE) (4 GB)
  89. ab00 0000 00 - b000 0000 00: - (20 GB)
  90. b000 0000 00 - b200 0000 00: traces (8 GB)
  91. b200 0000 00 - fe00 0000 00: - (304 GB)
  92. fe00 0000 00 - ff00 0000 00: heap (4 GB)
  93. ff00 0000 00 - ff80 0000 00: - (2 GB)
  94. ff80 0000 00 - ffff ffff ff: modules and main thread stack (<2 GB)
  95. */
  96. struct MappingMips64_40 {
  97. static const uptr kMetaShadowBeg = 0x4000000000ull;
  98. static const uptr kMetaShadowEnd = 0x5000000000ull;
  99. static const uptr kTraceMemBeg = 0xb000000000ull;
  100. static const uptr kTraceMemEnd = 0xb200000000ull;
  101. static const uptr kShadowBeg = 0x2000000000ull;
  102. static const uptr kShadowEnd = 0x4000000000ull;
  103. static const uptr kHeapMemBeg = 0xfe00000000ull;
  104. static const uptr kHeapMemEnd = 0xff00000000ull;
  105. static const uptr kLoAppMemBeg = 0x0100000000ull;
  106. static const uptr kLoAppMemEnd = 0x0200000000ull;
  107. static const uptr kMidAppMemBeg = 0xaa00000000ull;
  108. static const uptr kMidAppMemEnd = 0xab00000000ull;
  109. static const uptr kHiAppMemBeg = 0xff80000000ull;
  110. static const uptr kHiAppMemEnd = 0xffffffffffull;
  111. static const uptr kShadowMsk = 0xf800000000ull;
  112. static const uptr kShadowXor = 0x0800000000ull;
  113. static const uptr kShadowAdd = 0x0000000000ull;
  114. static const uptr kVdsoBeg = 0xfffff00000ull;
  115. };
  116. /*
  117. C/C++ on Darwin/iOS/ARM64 (36-bit VMA, 64 GB VM)
  118. 0000 0000 00 - 0100 0000 00: - (4 GB)
  119. 0100 0000 00 - 0200 0000 00: main binary, modules, thread stacks (4 GB)
  120. 0200 0000 00 - 0300 0000 00: heap (4 GB)
  121. 0300 0000 00 - 0400 0000 00: - (4 GB)
  122. 0400 0000 00 - 0c00 0000 00: shadow memory (32 GB)
  123. 0c00 0000 00 - 0d00 0000 00: - (4 GB)
  124. 0d00 0000 00 - 0e00 0000 00: metainfo (4 GB)
  125. 0e00 0000 00 - 0f00 0000 00: - (4 GB)
  126. 0f00 0000 00 - 0fc0 0000 00: traces (3 GB)
  127. 0fc0 0000 00 - 1000 0000 00: -
  128. */
  129. struct MappingAppleAarch64 {
  130. static const uptr kLoAppMemBeg = 0x0100000000ull;
  131. static const uptr kLoAppMemEnd = 0x0200000000ull;
  132. static const uptr kHeapMemBeg = 0x0200000000ull;
  133. static const uptr kHeapMemEnd = 0x0300000000ull;
  134. static const uptr kShadowBeg = 0x0400000000ull;
  135. static const uptr kShadowEnd = 0x0c00000000ull;
  136. static const uptr kMetaShadowBeg = 0x0d00000000ull;
  137. static const uptr kMetaShadowEnd = 0x0e00000000ull;
  138. static const uptr kTraceMemBeg = 0x0f00000000ull;
  139. static const uptr kTraceMemEnd = 0x0fc0000000ull;
  140. static const uptr kHiAppMemBeg = 0x0fc0000000ull;
  141. static const uptr kHiAppMemEnd = 0x0fc0000000ull;
  142. static const uptr kShadowMsk = 0x0ull;
  143. static const uptr kShadowXor = 0x0ull;
  144. static const uptr kShadowAdd = 0x0ull;
  145. static const uptr kVdsoBeg = 0x7000000000000000ull;
  146. static const uptr kMidAppMemBeg = 0;
  147. static const uptr kMidAppMemEnd = 0;
  148. };
  149. /*
  150. C/C++ on linux/aarch64 (39-bit VMA)
  151. 0000 0010 00 - 0100 0000 00: main binary
  152. 0100 0000 00 - 0800 0000 00: -
  153. 0800 0000 00 - 2000 0000 00: shadow memory
  154. 2000 0000 00 - 3100 0000 00: -
  155. 3100 0000 00 - 3400 0000 00: metainfo
  156. 3400 0000 00 - 5500 0000 00: -
  157. 5500 0000 00 - 5600 0000 00: main binary (PIE)
  158. 5600 0000 00 - 6000 0000 00: -
  159. 6000 0000 00 - 6200 0000 00: traces
  160. 6200 0000 00 - 7d00 0000 00: -
  161. 7c00 0000 00 - 7d00 0000 00: heap
  162. 7d00 0000 00 - 7fff ffff ff: modules and main thread stack
  163. */
  164. struct MappingAarch64_39 {
  165. static const uptr kLoAppMemBeg = 0x0000001000ull;
  166. static const uptr kLoAppMemEnd = 0x0100000000ull;
  167. static const uptr kShadowBeg = 0x0800000000ull;
  168. static const uptr kShadowEnd = 0x2000000000ull;
  169. static const uptr kMetaShadowBeg = 0x3100000000ull;
  170. static const uptr kMetaShadowEnd = 0x3400000000ull;
  171. static const uptr kMidAppMemBeg = 0x5500000000ull;
  172. static const uptr kMidAppMemEnd = 0x5600000000ull;
  173. static const uptr kTraceMemBeg = 0x6000000000ull;
  174. static const uptr kTraceMemEnd = 0x6200000000ull;
  175. static const uptr kHeapMemBeg = 0x7c00000000ull;
  176. static const uptr kHeapMemEnd = 0x7d00000000ull;
  177. static const uptr kHiAppMemBeg = 0x7e00000000ull;
  178. static const uptr kHiAppMemEnd = 0x7fffffffffull;
  179. static const uptr kShadowMsk = 0x7800000000ull;
  180. static const uptr kShadowXor = 0x0200000000ull;
  181. static const uptr kShadowAdd = 0x0000000000ull;
  182. static const uptr kVdsoBeg = 0x7f00000000ull;
  183. };
  184. /*
  185. C/C++ on linux/aarch64 (42-bit VMA)
  186. 00000 0010 00 - 01000 0000 00: main binary
  187. 01000 0000 00 - 10000 0000 00: -
  188. 10000 0000 00 - 20000 0000 00: shadow memory
  189. 20000 0000 00 - 26000 0000 00: -
  190. 26000 0000 00 - 28000 0000 00: metainfo
  191. 28000 0000 00 - 2aa00 0000 00: -
  192. 2aa00 0000 00 - 2ab00 0000 00: main binary (PIE)
  193. 2ab00 0000 00 - 36200 0000 00: -
  194. 36200 0000 00 - 36240 0000 00: traces
  195. 36240 0000 00 - 3e000 0000 00: -
  196. 3e000 0000 00 - 3f000 0000 00: heap
  197. 3f000 0000 00 - 3ffff ffff ff: modules and main thread stack
  198. */
  199. struct MappingAarch64_42 {
  200. static const uptr kBroken = kBrokenReverseMapping;
  201. static const uptr kLoAppMemBeg = 0x00000001000ull;
  202. static const uptr kLoAppMemEnd = 0x01000000000ull;
  203. static const uptr kShadowBeg = 0x10000000000ull;
  204. static const uptr kShadowEnd = 0x20000000000ull;
  205. static const uptr kMetaShadowBeg = 0x26000000000ull;
  206. static const uptr kMetaShadowEnd = 0x28000000000ull;
  207. static const uptr kMidAppMemBeg = 0x2aa00000000ull;
  208. static const uptr kMidAppMemEnd = 0x2ab00000000ull;
  209. static const uptr kTraceMemBeg = 0x36200000000ull;
  210. static const uptr kTraceMemEnd = 0x36400000000ull;
  211. static const uptr kHeapMemBeg = 0x3e000000000ull;
  212. static const uptr kHeapMemEnd = 0x3f000000000ull;
  213. static const uptr kHiAppMemBeg = 0x3f000000000ull;
  214. static const uptr kHiAppMemEnd = 0x3ffffffffffull;
  215. static const uptr kShadowMsk = 0x3c000000000ull;
  216. static const uptr kShadowXor = 0x04000000000ull;
  217. static const uptr kShadowAdd = 0x00000000000ull;
  218. static const uptr kVdsoBeg = 0x37f00000000ull;
  219. };
  220. struct MappingAarch64_48 {
  221. static const uptr kLoAppMemBeg = 0x0000000001000ull;
  222. static const uptr kLoAppMemEnd = 0x0000200000000ull;
  223. static const uptr kShadowBeg = 0x0002000000000ull;
  224. static const uptr kShadowEnd = 0x0004000000000ull;
  225. static const uptr kMetaShadowBeg = 0x0005000000000ull;
  226. static const uptr kMetaShadowEnd = 0x0006000000000ull;
  227. static const uptr kMidAppMemBeg = 0x0aaaa00000000ull;
  228. static const uptr kMidAppMemEnd = 0x0aaaf00000000ull;
  229. static const uptr kTraceMemBeg = 0x0f06000000000ull;
  230. static const uptr kTraceMemEnd = 0x0f06200000000ull;
  231. static const uptr kHeapMemBeg = 0x0ffff00000000ull;
  232. static const uptr kHeapMemEnd = 0x0ffff00000000ull;
  233. static const uptr kHiAppMemBeg = 0x0ffff00000000ull;
  234. static const uptr kHiAppMemEnd = 0x1000000000000ull;
  235. static const uptr kShadowMsk = 0x0fff800000000ull;
  236. static const uptr kShadowXor = 0x0000800000000ull;
  237. static const uptr kShadowAdd = 0x0000000000000ull;
  238. static const uptr kVdsoBeg = 0xffff000000000ull;
  239. };
  240. /*
  241. C/C++ on linux/powerpc64 (44-bit VMA)
  242. 0000 0000 0100 - 0001 0000 0000: main binary
  243. 0001 0000 0000 - 0001 0000 0000: -
  244. 0001 0000 0000 - 0b00 0000 0000: shadow
  245. 0b00 0000 0000 - 0b00 0000 0000: -
  246. 0b00 0000 0000 - 0d00 0000 0000: metainfo (memory blocks and sync objects)
  247. 0d00 0000 0000 - 0d00 0000 0000: -
  248. 0d00 0000 0000 - 0f00 0000 0000: traces
  249. 0f00 0000 0000 - 0f00 0000 0000: -
  250. 0f00 0000 0000 - 0f50 0000 0000: heap
  251. 0f50 0000 0000 - 0f60 0000 0000: -
  252. 0f60 0000 0000 - 1000 0000 0000: modules and main thread stack
  253. */
  254. struct MappingPPC64_44 {
  255. static const uptr kBroken =
  256. kBrokenMapping | kBrokenReverseMapping | kBrokenLinearity;
  257. static const uptr kMetaShadowBeg = 0x0b0000000000ull;
  258. static const uptr kMetaShadowEnd = 0x0d0000000000ull;
  259. static const uptr kTraceMemBeg = 0x0d0000000000ull;
  260. static const uptr kTraceMemEnd = 0x0f0000000000ull;
  261. static const uptr kShadowBeg = 0x000100000000ull;
  262. static const uptr kShadowEnd = 0x0b0000000000ull;
  263. static const uptr kLoAppMemBeg = 0x000000000100ull;
  264. static const uptr kLoAppMemEnd = 0x000100000000ull;
  265. static const uptr kHeapMemBeg = 0x0f0000000000ull;
  266. static const uptr kHeapMemEnd = 0x0f5000000000ull;
  267. static const uptr kHiAppMemBeg = 0x0f6000000000ull;
  268. static const uptr kHiAppMemEnd = 0x100000000000ull; // 44 bits
  269. static const uptr kShadowMsk = 0x0f0000000000ull;
  270. static const uptr kShadowXor = 0x002100000000ull;
  271. static const uptr kShadowAdd = 0x000000000000ull;
  272. static const uptr kVdsoBeg = 0x3c0000000000000ull;
  273. static const uptr kMidAppMemBeg = 0;
  274. static const uptr kMidAppMemEnd = 0;
  275. };
  276. /*
  277. C/C++ on linux/powerpc64 (46-bit VMA)
  278. 0000 0000 1000 - 0100 0000 0000: main binary
  279. 0100 0000 0000 - 0200 0000 0000: -
  280. 0100 0000 0000 - 1000 0000 0000: shadow
  281. 1000 0000 0000 - 1000 0000 0000: -
  282. 1000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
  283. 2000 0000 0000 - 2000 0000 0000: -
  284. 2000 0000 0000 - 2200 0000 0000: traces
  285. 2200 0000 0000 - 3d00 0000 0000: -
  286. 3d00 0000 0000 - 3e00 0000 0000: heap
  287. 3e00 0000 0000 - 3e80 0000 0000: -
  288. 3e80 0000 0000 - 4000 0000 0000: modules and main thread stack
  289. */
  290. struct MappingPPC64_46 {
  291. static const uptr kMetaShadowBeg = 0x100000000000ull;
  292. static const uptr kMetaShadowEnd = 0x200000000000ull;
  293. static const uptr kTraceMemBeg = 0x200000000000ull;
  294. static const uptr kTraceMemEnd = 0x220000000000ull;
  295. static const uptr kShadowBeg = 0x010000000000ull;
  296. static const uptr kShadowEnd = 0x100000000000ull;
  297. static const uptr kHeapMemBeg = 0x3d0000000000ull;
  298. static const uptr kHeapMemEnd = 0x3e0000000000ull;
  299. static const uptr kLoAppMemBeg = 0x000000001000ull;
  300. static const uptr kLoAppMemEnd = 0x010000000000ull;
  301. static const uptr kHiAppMemBeg = 0x3e8000000000ull;
  302. static const uptr kHiAppMemEnd = 0x400000000000ull; // 46 bits
  303. static const uptr kShadowMsk = 0x3c0000000000ull;
  304. static const uptr kShadowXor = 0x020000000000ull;
  305. static const uptr kShadowAdd = 0x000000000000ull;
  306. static const uptr kVdsoBeg = 0x7800000000000000ull;
  307. static const uptr kMidAppMemBeg = 0;
  308. static const uptr kMidAppMemEnd = 0;
  309. };
  310. /*
  311. C/C++ on linux/powerpc64 (47-bit VMA)
  312. 0000 0000 1000 - 0100 0000 0000: main binary
  313. 0100 0000 0000 - 0200 0000 0000: -
  314. 0100 0000 0000 - 1000 0000 0000: shadow
  315. 1000 0000 0000 - 1000 0000 0000: -
  316. 1000 0000 0000 - 2000 0000 0000: metainfo (memory blocks and sync objects)
  317. 2000 0000 0000 - 2000 0000 0000: -
  318. 2000 0000 0000 - 2200 0000 0000: traces
  319. 2200 0000 0000 - 7d00 0000 0000: -
  320. 7d00 0000 0000 - 7e00 0000 0000: heap
  321. 7e00 0000 0000 - 7e80 0000 0000: -
  322. 7e80 0000 0000 - 8000 0000 0000: modules and main thread stack
  323. */
  324. struct MappingPPC64_47 {
  325. static const uptr kMetaShadowBeg = 0x100000000000ull;
  326. static const uptr kMetaShadowEnd = 0x200000000000ull;
  327. static const uptr kTraceMemBeg = 0x200000000000ull;
  328. static const uptr kTraceMemEnd = 0x220000000000ull;
  329. static const uptr kShadowBeg = 0x010000000000ull;
  330. static const uptr kShadowEnd = 0x100000000000ull;
  331. static const uptr kHeapMemBeg = 0x7d0000000000ull;
  332. static const uptr kHeapMemEnd = 0x7e0000000000ull;
  333. static const uptr kLoAppMemBeg = 0x000000001000ull;
  334. static const uptr kLoAppMemEnd = 0x010000000000ull;
  335. static const uptr kHiAppMemBeg = 0x7e8000000000ull;
  336. static const uptr kHiAppMemEnd = 0x800000000000ull; // 47 bits
  337. static const uptr kShadowMsk = 0x7c0000000000ull;
  338. static const uptr kShadowXor = 0x020000000000ull;
  339. static const uptr kShadowAdd = 0x000000000000ull;
  340. static const uptr kVdsoBeg = 0x7800000000000000ull;
  341. static const uptr kMidAppMemBeg = 0;
  342. static const uptr kMidAppMemEnd = 0;
  343. };
  344. /*
  345. C/C++ on linux/s390x
  346. While the kernel provides a 64-bit address space, we have to restrict ourselves
  347. to 48 bits due to how e.g. SyncVar::GetId() works.
  348. 0000 0000 1000 - 0e00 0000 0000: binary, modules, stacks - 14 TiB
  349. 0e00 0000 0000 - 4000 0000 0000: -
  350. 4000 0000 0000 - 8000 0000 0000: shadow - 64TiB (4 * app)
  351. 8000 0000 0000 - 9000 0000 0000: -
  352. 9000 0000 0000 - 9800 0000 0000: metainfo - 8TiB (0.5 * app)
  353. 9800 0000 0000 - a000 0000 0000: -
  354. a000 0000 0000 - b000 0000 0000: traces - 16TiB (max history * 128k threads)
  355. b000 0000 0000 - be00 0000 0000: -
  356. be00 0000 0000 - c000 0000 0000: heap - 2TiB (max supported by the allocator)
  357. */
  358. struct MappingS390x {
  359. static const uptr kMetaShadowBeg = 0x900000000000ull;
  360. static const uptr kMetaShadowEnd = 0x980000000000ull;
  361. static const uptr kTraceMemBeg = 0xa00000000000ull;
  362. static const uptr kTraceMemEnd = 0xb00000000000ull;
  363. static const uptr kShadowBeg = 0x400000000000ull;
  364. static const uptr kShadowEnd = 0x800000000000ull;
  365. static const uptr kHeapMemBeg = 0xbe0000000000ull;
  366. static const uptr kHeapMemEnd = 0xc00000000000ull;
  367. static const uptr kLoAppMemBeg = 0x000000001000ull;
  368. static const uptr kLoAppMemEnd = 0x0e0000000000ull;
  369. static const uptr kHiAppMemBeg = 0xc00000004000ull;
  370. static const uptr kHiAppMemEnd = 0xc00000004000ull;
  371. static const uptr kShadowMsk = 0xb00000000000ull;
  372. static const uptr kShadowXor = 0x100000000000ull;
  373. static const uptr kShadowAdd = 0x000000000000ull;
  374. static const uptr kVdsoBeg = 0xfffffffff000ull;
  375. static const uptr kMidAppMemBeg = 0;
  376. static const uptr kMidAppMemEnd = 0;
  377. };
  378. /* Go on linux, darwin and freebsd on x86_64
  379. 0000 0000 1000 - 0000 1000 0000: executable
  380. 0000 1000 0000 - 00c0 0000 0000: -
  381. 00c0 0000 0000 - 00e0 0000 0000: heap
  382. 00e0 0000 0000 - 2000 0000 0000: -
  383. 2000 0000 0000 - 2380 0000 0000: shadow
  384. 2380 0000 0000 - 3000 0000 0000: -
  385. 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
  386. 4000 0000 0000 - 6000 0000 0000: -
  387. 6000 0000 0000 - 6200 0000 0000: traces
  388. 6200 0000 0000 - 8000 0000 0000: -
  389. */
  390. struct MappingGo48 {
  391. static const uptr kMetaShadowBeg = 0x300000000000ull;
  392. static const uptr kMetaShadowEnd = 0x400000000000ull;
  393. static const uptr kTraceMemBeg = 0x600000000000ull;
  394. static const uptr kTraceMemEnd = 0x620000000000ull;
  395. static const uptr kShadowBeg = 0x200000000000ull;
  396. static const uptr kShadowEnd = 0x238000000000ull;
  397. static const uptr kLoAppMemBeg = 0x000000001000ull;
  398. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  399. static const uptr kMidAppMemBeg = 0;
  400. static const uptr kMidAppMemEnd = 0;
  401. static const uptr kHiAppMemBeg = 0;
  402. static const uptr kHiAppMemEnd = 0;
  403. static const uptr kHeapMemBeg = 0;
  404. static const uptr kHeapMemEnd = 0;
  405. static const uptr kVdsoBeg = 0;
  406. static const uptr kShadowMsk = 0;
  407. static const uptr kShadowXor = 0;
  408. static const uptr kShadowAdd = 0x200000000000ull;
  409. };
  410. /* Go on windows
  411. 0000 0000 1000 - 0000 1000 0000: executable
  412. 0000 1000 0000 - 00f8 0000 0000: -
  413. 00c0 0000 0000 - 00e0 0000 0000: heap
  414. 00e0 0000 0000 - 0100 0000 0000: -
  415. 0100 0000 0000 - 0500 0000 0000: shadow
  416. 0500 0000 0000 - 0700 0000 0000: traces
  417. 0700 0000 0000 - 0770 0000 0000: metainfo (memory blocks and sync objects)
  418. 07d0 0000 0000 - 8000 0000 0000: -
  419. */
  420. struct MappingGoWindows {
  421. static const uptr kMetaShadowBeg = 0x070000000000ull;
  422. static const uptr kMetaShadowEnd = 0x077000000000ull;
  423. static const uptr kTraceMemBeg = 0x050000000000ull;
  424. static const uptr kTraceMemEnd = 0x070000000000ull;
  425. static const uptr kShadowBeg = 0x010000000000ull;
  426. static const uptr kShadowEnd = 0x050000000000ull;
  427. static const uptr kLoAppMemBeg = 0x000000001000ull;
  428. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  429. static const uptr kMidAppMemBeg = 0;
  430. static const uptr kMidAppMemEnd = 0;
  431. static const uptr kHiAppMemBeg = 0;
  432. static const uptr kHiAppMemEnd = 0;
  433. static const uptr kHeapMemBeg = 0;
  434. static const uptr kHeapMemEnd = 0;
  435. static const uptr kVdsoBeg = 0;
  436. static const uptr kShadowMsk = 0;
  437. static const uptr kShadowXor = 0;
  438. static const uptr kShadowAdd = 0x010000000000ull;
  439. };
  440. /* Go on linux/powerpc64 (46-bit VMA)
  441. 0000 0000 1000 - 0000 1000 0000: executable
  442. 0000 1000 0000 - 00c0 0000 0000: -
  443. 00c0 0000 0000 - 00e0 0000 0000: heap
  444. 00e0 0000 0000 - 2000 0000 0000: -
  445. 2000 0000 0000 - 2380 0000 0000: shadow
  446. 2380 0000 0000 - 2400 0000 0000: -
  447. 2400 0000 0000 - 3400 0000 0000: metainfo (memory blocks and sync objects)
  448. 3400 0000 0000 - 3600 0000 0000: -
  449. 3600 0000 0000 - 3800 0000 0000: traces
  450. 3800 0000 0000 - 4000 0000 0000: -
  451. */
  452. struct MappingGoPPC64_46 {
  453. static const uptr kMetaShadowBeg = 0x240000000000ull;
  454. static const uptr kMetaShadowEnd = 0x340000000000ull;
  455. static const uptr kTraceMemBeg = 0x360000000000ull;
  456. static const uptr kTraceMemEnd = 0x380000000000ull;
  457. static const uptr kShadowBeg = 0x200000000000ull;
  458. static const uptr kShadowEnd = 0x238000000000ull;
  459. static const uptr kLoAppMemBeg = 0x000000001000ull;
  460. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  461. static const uptr kMidAppMemBeg = 0;
  462. static const uptr kMidAppMemEnd = 0;
  463. static const uptr kHiAppMemBeg = 0;
  464. static const uptr kHiAppMemEnd = 0;
  465. static const uptr kHeapMemBeg = 0;
  466. static const uptr kHeapMemEnd = 0;
  467. static const uptr kVdsoBeg = 0;
  468. static const uptr kShadowMsk = 0;
  469. static const uptr kShadowXor = 0;
  470. static const uptr kShadowAdd = 0x200000000000ull;
  471. };
  472. /* Go on linux/powerpc64 (47-bit VMA)
  473. 0000 0000 1000 - 0000 1000 0000: executable
  474. 0000 1000 0000 - 00c0 0000 0000: -
  475. 00c0 0000 0000 - 00e0 0000 0000: heap
  476. 00e0 0000 0000 - 2000 0000 0000: -
  477. 2000 0000 0000 - 3000 0000 0000: shadow
  478. 3000 0000 0000 - 3000 0000 0000: -
  479. 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
  480. 4000 0000 0000 - 6000 0000 0000: -
  481. 6000 0000 0000 - 6200 0000 0000: traces
  482. 6200 0000 0000 - 8000 0000 0000: -
  483. */
  484. struct MappingGoPPC64_47 {
  485. static const uptr kMetaShadowBeg = 0x300000000000ull;
  486. static const uptr kMetaShadowEnd = 0x400000000000ull;
  487. static const uptr kTraceMemBeg = 0x600000000000ull;
  488. static const uptr kTraceMemEnd = 0x620000000000ull;
  489. static const uptr kShadowBeg = 0x200000000000ull;
  490. static const uptr kShadowEnd = 0x300000000000ull;
  491. static const uptr kLoAppMemBeg = 0x000000001000ull;
  492. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  493. static const uptr kMidAppMemBeg = 0;
  494. static const uptr kMidAppMemEnd = 0;
  495. static const uptr kHiAppMemBeg = 0;
  496. static const uptr kHiAppMemEnd = 0;
  497. static const uptr kHeapMemBeg = 0;
  498. static const uptr kHeapMemEnd = 0;
  499. static const uptr kVdsoBeg = 0;
  500. static const uptr kShadowMsk = 0;
  501. static const uptr kShadowXor = 0;
  502. static const uptr kShadowAdd = 0x200000000000ull;
  503. };
  504. /* Go on linux/aarch64 (48-bit VMA) and darwin/aarch64 (47-bit VMA)
  505. 0000 0000 1000 - 0000 1000 0000: executable
  506. 0000 1000 0000 - 00c0 0000 0000: -
  507. 00c0 0000 0000 - 00e0 0000 0000: heap
  508. 00e0 0000 0000 - 2000 0000 0000: -
  509. 2000 0000 0000 - 3000 0000 0000: shadow
  510. 3000 0000 0000 - 3000 0000 0000: -
  511. 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
  512. 4000 0000 0000 - 6000 0000 0000: -
  513. 6000 0000 0000 - 6200 0000 0000: traces
  514. 6200 0000 0000 - 8000 0000 0000: -
  515. */
  516. struct MappingGoAarch64 {
  517. static const uptr kMetaShadowBeg = 0x300000000000ull;
  518. static const uptr kMetaShadowEnd = 0x400000000000ull;
  519. static const uptr kTraceMemBeg = 0x600000000000ull;
  520. static const uptr kTraceMemEnd = 0x620000000000ull;
  521. static const uptr kShadowBeg = 0x200000000000ull;
  522. static const uptr kShadowEnd = 0x300000000000ull;
  523. static const uptr kLoAppMemBeg = 0x000000001000ull;
  524. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  525. static const uptr kMidAppMemBeg = 0;
  526. static const uptr kMidAppMemEnd = 0;
  527. static const uptr kHiAppMemBeg = 0;
  528. static const uptr kHiAppMemEnd = 0;
  529. static const uptr kHeapMemBeg = 0;
  530. static const uptr kHeapMemEnd = 0;
  531. static const uptr kVdsoBeg = 0;
  532. static const uptr kShadowMsk = 0;
  533. static const uptr kShadowXor = 0;
  534. static const uptr kShadowAdd = 0x200000000000ull;
  535. };
  536. /*
  537. Go on linux/mips64 (47-bit VMA)
  538. 0000 0000 1000 - 0000 1000 0000: executable
  539. 0000 1000 0000 - 00c0 0000 0000: -
  540. 00c0 0000 0000 - 00e0 0000 0000: heap
  541. 00e0 0000 0000 - 2000 0000 0000: -
  542. 2000 0000 0000 - 3000 0000 0000: shadow
  543. 3000 0000 0000 - 3000 0000 0000: -
  544. 3000 0000 0000 - 4000 0000 0000: metainfo (memory blocks and sync objects)
  545. 4000 0000 0000 - 6000 0000 0000: -
  546. 6000 0000 0000 - 6200 0000 0000: traces
  547. 6200 0000 0000 - 8000 0000 0000: -
  548. */
  549. struct MappingGoMips64_47 {
  550. static const uptr kMetaShadowBeg = 0x300000000000ull;
  551. static const uptr kMetaShadowEnd = 0x400000000000ull;
  552. static const uptr kTraceMemBeg = 0x600000000000ull;
  553. static const uptr kTraceMemEnd = 0x620000000000ull;
  554. static const uptr kShadowBeg = 0x200000000000ull;
  555. static const uptr kShadowEnd = 0x300000000000ull;
  556. static const uptr kLoAppMemBeg = 0x000000001000ull;
  557. static const uptr kLoAppMemEnd = 0x00e000000000ull;
  558. static const uptr kMidAppMemBeg = 0;
  559. static const uptr kMidAppMemEnd = 0;
  560. static const uptr kHiAppMemBeg = 0;
  561. static const uptr kHiAppMemEnd = 0;
  562. static const uptr kHeapMemBeg = 0;
  563. static const uptr kHeapMemEnd = 0;
  564. static const uptr kVdsoBeg = 0;
  565. static const uptr kShadowMsk = 0;
  566. static const uptr kShadowXor = 0;
  567. static const uptr kShadowAdd = 0x200000000000ull;
  568. };
  569. /*
  570. Go on linux/s390x
  571. 0000 0000 1000 - 1000 0000 0000: executable and heap - 16 TiB
  572. 1000 0000 0000 - 4000 0000 0000: -
  573. 4000 0000 0000 - 8000 0000 0000: shadow - 64TiB (4 * app)
  574. 8000 0000 0000 - 9000 0000 0000: -
  575. 9000 0000 0000 - 9800 0000 0000: metainfo - 8TiB (0.5 * app)
  576. 9800 0000 0000 - a000 0000 0000: -
  577. a000 0000 0000 - b000 0000 0000: traces - 16TiB (max history * 128k threads)
  578. */
  579. struct MappingGoS390x {
  580. static const uptr kMetaShadowBeg = 0x900000000000ull;
  581. static const uptr kMetaShadowEnd = 0x980000000000ull;
  582. static const uptr kTraceMemBeg = 0xa00000000000ull;
  583. static const uptr kTraceMemEnd = 0xb00000000000ull;
  584. static const uptr kShadowBeg = 0x400000000000ull;
  585. static const uptr kShadowEnd = 0x800000000000ull;
  586. static const uptr kLoAppMemBeg = 0x000000001000ull;
  587. static const uptr kLoAppMemEnd = 0x100000000000ull;
  588. static const uptr kMidAppMemBeg = 0;
  589. static const uptr kMidAppMemEnd = 0;
  590. static const uptr kHiAppMemBeg = 0;
  591. static const uptr kHiAppMemEnd = 0;
  592. static const uptr kHeapMemBeg = 0;
  593. static const uptr kHeapMemEnd = 0;
  594. static const uptr kVdsoBeg = 0;
  595. static const uptr kShadowMsk = 0;
  596. static const uptr kShadowXor = 0;
  597. static const uptr kShadowAdd = 0x400000000000ull;
  598. };
  599. extern uptr vmaSize;
  600. template <typename Func, typename Arg>
  601. ALWAYS_INLINE auto SelectMapping(Arg arg) {
  602. #if SANITIZER_GO
  603. # if defined(__powerpc64__)
  604. switch (vmaSize) {
  605. case 46:
  606. return Func::template Apply<MappingGoPPC64_46>(arg);
  607. case 47:
  608. return Func::template Apply<MappingGoPPC64_47>(arg);
  609. }
  610. # elif defined(__mips64)
  611. return Func::template Apply<MappingGoMips64_47>(arg);
  612. # elif defined(__s390x__)
  613. return Func::template Apply<MappingGoS390x>(arg);
  614. # elif defined(__aarch64__)
  615. return Func::template Apply<MappingGoAarch64>(arg);
  616. # elif SANITIZER_WINDOWS
  617. return Func::template Apply<MappingGoWindows>(arg);
  618. # else
  619. return Func::template Apply<MappingGo48>(arg);
  620. # endif
  621. #else // SANITIZER_GO
  622. # if defined(__x86_64__) || SANITIZER_IOSSIM || SANITIZER_MAC && !SANITIZER_IOS
  623. return Func::template Apply<Mapping48AddressSpace>(arg);
  624. # elif defined(__aarch64__) && defined(__APPLE__)
  625. return Func::template Apply<MappingAppleAarch64>(arg);
  626. # elif defined(__aarch64__) && !defined(__APPLE__)
  627. switch (vmaSize) {
  628. case 39:
  629. return Func::template Apply<MappingAarch64_39>(arg);
  630. case 42:
  631. return Func::template Apply<MappingAarch64_42>(arg);
  632. case 48:
  633. return Func::template Apply<MappingAarch64_48>(arg);
  634. }
  635. # elif defined(__powerpc64__)
  636. switch (vmaSize) {
  637. case 44:
  638. return Func::template Apply<MappingPPC64_44>(arg);
  639. case 46:
  640. return Func::template Apply<MappingPPC64_46>(arg);
  641. case 47:
  642. return Func::template Apply<MappingPPC64_47>(arg);
  643. }
  644. # elif defined(__mips64)
  645. return Func::template Apply<MappingMips64_40>(arg);
  646. # elif defined(__s390x__)
  647. return Func::template Apply<MappingS390x>(arg);
  648. # else
  649. # error "unsupported platform"
  650. # endif
  651. #endif
  652. Die();
  653. }
  654. template <typename Func>
  655. void ForEachMapping() {
  656. Func::template Apply<Mapping48AddressSpace>();
  657. Func::template Apply<MappingMips64_40>();
  658. Func::template Apply<MappingAppleAarch64>();
  659. Func::template Apply<MappingAarch64_39>();
  660. Func::template Apply<MappingAarch64_42>();
  661. Func::template Apply<MappingAarch64_48>();
  662. Func::template Apply<MappingPPC64_44>();
  663. Func::template Apply<MappingPPC64_46>();
  664. Func::template Apply<MappingPPC64_47>();
  665. Func::template Apply<MappingS390x>();
  666. Func::template Apply<MappingGo48>();
  667. Func::template Apply<MappingGoWindows>();
  668. Func::template Apply<MappingGoPPC64_46>();
  669. Func::template Apply<MappingGoPPC64_47>();
  670. Func::template Apply<MappingGoAarch64>();
  671. Func::template Apply<MappingGoMips64_47>();
  672. Func::template Apply<MappingGoS390x>();
  673. }
  674. enum MappingType {
  675. kLoAppMemBeg,
  676. kLoAppMemEnd,
  677. kHiAppMemBeg,
  678. kHiAppMemEnd,
  679. kMidAppMemBeg,
  680. kMidAppMemEnd,
  681. kHeapMemBeg,
  682. kHeapMemEnd,
  683. kShadowBeg,
  684. kShadowEnd,
  685. kMetaShadowBeg,
  686. kMetaShadowEnd,
  687. kTraceMemBeg,
  688. kTraceMemEnd,
  689. kVdsoBeg,
  690. };
  691. struct MappingField {
  692. template <typename Mapping>
  693. static uptr Apply(MappingType type) {
  694. switch (type) {
  695. case kLoAppMemBeg:
  696. return Mapping::kLoAppMemBeg;
  697. case kLoAppMemEnd:
  698. return Mapping::kLoAppMemEnd;
  699. case kMidAppMemBeg:
  700. return Mapping::kMidAppMemBeg;
  701. case kMidAppMemEnd:
  702. return Mapping::kMidAppMemEnd;
  703. case kHiAppMemBeg:
  704. return Mapping::kHiAppMemBeg;
  705. case kHiAppMemEnd:
  706. return Mapping::kHiAppMemEnd;
  707. case kHeapMemBeg:
  708. return Mapping::kHeapMemBeg;
  709. case kHeapMemEnd:
  710. return Mapping::kHeapMemEnd;
  711. case kVdsoBeg:
  712. return Mapping::kVdsoBeg;
  713. case kShadowBeg:
  714. return Mapping::kShadowBeg;
  715. case kShadowEnd:
  716. return Mapping::kShadowEnd;
  717. case kMetaShadowBeg:
  718. return Mapping::kMetaShadowBeg;
  719. case kMetaShadowEnd:
  720. return Mapping::kMetaShadowEnd;
  721. case kTraceMemBeg:
  722. return Mapping::kTraceMemBeg;
  723. case kTraceMemEnd:
  724. return Mapping::kTraceMemEnd;
  725. }
  726. Die();
  727. }
  728. };
  729. ALWAYS_INLINE
  730. uptr LoAppMemBeg(void) { return SelectMapping<MappingField>(kLoAppMemBeg); }
  731. ALWAYS_INLINE
  732. uptr LoAppMemEnd(void) { return SelectMapping<MappingField>(kLoAppMemEnd); }
  733. ALWAYS_INLINE
  734. uptr MidAppMemBeg(void) { return SelectMapping<MappingField>(kMidAppMemBeg); }
  735. ALWAYS_INLINE
  736. uptr MidAppMemEnd(void) { return SelectMapping<MappingField>(kMidAppMemEnd); }
  737. ALWAYS_INLINE
  738. uptr HeapMemBeg(void) { return SelectMapping<MappingField>(kHeapMemBeg); }
  739. ALWAYS_INLINE
  740. uptr HeapMemEnd(void) { return SelectMapping<MappingField>(kHeapMemEnd); }
  741. ALWAYS_INLINE
  742. uptr HiAppMemBeg(void) { return SelectMapping<MappingField>(kHiAppMemBeg); }
  743. ALWAYS_INLINE
  744. uptr HiAppMemEnd(void) { return SelectMapping<MappingField>(kHiAppMemEnd); }
  745. ALWAYS_INLINE
  746. uptr VdsoBeg(void) { return SelectMapping<MappingField>(kVdsoBeg); }
  747. ALWAYS_INLINE
  748. uptr ShadowBeg(void) { return SelectMapping<MappingField>(kShadowBeg); }
  749. ALWAYS_INLINE
  750. uptr ShadowEnd(void) { return SelectMapping<MappingField>(kShadowEnd); }
  751. ALWAYS_INLINE
  752. uptr MetaShadowBeg(void) { return SelectMapping<MappingField>(kMetaShadowBeg); }
  753. ALWAYS_INLINE
  754. uptr MetaShadowEnd(void) { return SelectMapping<MappingField>(kMetaShadowEnd); }
  755. ALWAYS_INLINE
  756. uptr TraceMemBeg(void) { return SelectMapping<MappingField>(kTraceMemBeg); }
  757. ALWAYS_INLINE
  758. uptr TraceMemEnd(void) { return SelectMapping<MappingField>(kTraceMemEnd); }
  759. struct IsAppMemImpl {
  760. template <typename Mapping>
  761. static bool Apply(uptr mem) {
  762. return (mem >= Mapping::kHeapMemBeg && mem < Mapping::kHeapMemEnd) ||
  763. (mem >= Mapping::kMidAppMemBeg && mem < Mapping::kMidAppMemEnd) ||
  764. (mem >= Mapping::kLoAppMemBeg && mem < Mapping::kLoAppMemEnd) ||
  765. (mem >= Mapping::kHiAppMemBeg && mem < Mapping::kHiAppMemEnd);
  766. }
  767. };
  768. ALWAYS_INLINE
  769. bool IsAppMem(uptr mem) { return SelectMapping<IsAppMemImpl>(mem); }
  770. struct IsShadowMemImpl {
  771. template <typename Mapping>
  772. static bool Apply(uptr mem) {
  773. return mem >= Mapping::kShadowBeg && mem <= Mapping::kShadowEnd;
  774. }
  775. };
  776. ALWAYS_INLINE
  777. bool IsShadowMem(RawShadow *p) {
  778. return SelectMapping<IsShadowMemImpl>(reinterpret_cast<uptr>(p));
  779. }
  780. struct IsMetaMemImpl {
  781. template <typename Mapping>
  782. static bool Apply(uptr mem) {
  783. return mem >= Mapping::kMetaShadowBeg && mem <= Mapping::kMetaShadowEnd;
  784. }
  785. };
  786. ALWAYS_INLINE
  787. bool IsMetaMem(const u32 *p) {
  788. return SelectMapping<IsMetaMemImpl>(reinterpret_cast<uptr>(p));
  789. }
  790. struct MemToShadowImpl {
  791. template <typename Mapping>
  792. static uptr Apply(uptr x) {
  793. DCHECK(IsAppMemImpl::Apply<Mapping>(x));
  794. return (((x) & ~(Mapping::kShadowMsk | (kShadowCell - 1))) ^
  795. Mapping::kShadowXor) *
  796. kShadowMultiplier +
  797. Mapping::kShadowAdd;
  798. }
  799. };
  800. ALWAYS_INLINE
  801. RawShadow *MemToShadow(uptr x) {
  802. return reinterpret_cast<RawShadow *>(SelectMapping<MemToShadowImpl>(x));
  803. }
  804. struct MemToMetaImpl {
  805. template <typename Mapping>
  806. static u32 *Apply(uptr x) {
  807. DCHECK(IsAppMemImpl::Apply<Mapping>(x));
  808. return (u32 *)(((((x) & ~(Mapping::kShadowMsk | (kMetaShadowCell - 1)))) /
  809. kMetaShadowCell * kMetaShadowSize) |
  810. Mapping::kMetaShadowBeg);
  811. }
  812. };
  813. ALWAYS_INLINE
  814. u32 *MemToMeta(uptr x) { return SelectMapping<MemToMetaImpl>(x); }
  815. struct ShadowToMemImpl {
  816. template <typename Mapping>
  817. static uptr Apply(uptr sp) {
  818. if (!IsShadowMemImpl::Apply<Mapping>(sp))
  819. return 0;
  820. // The shadow mapping is non-linear and we've lost some bits, so we don't
  821. // have an easy way to restore the original app address. But the mapping is
  822. // a bijection, so we try to restore the address as belonging to
  823. // low/mid/high range consecutively and see if shadow->app->shadow mapping
  824. // gives us the same address.
  825. uptr p =
  826. ((sp - Mapping::kShadowAdd) / kShadowMultiplier) ^ Mapping::kShadowXor;
  827. if (p >= Mapping::kLoAppMemBeg && p < Mapping::kLoAppMemEnd &&
  828. MemToShadowImpl::Apply<Mapping>(p) == sp)
  829. return p;
  830. if (Mapping::kMidAppMemBeg) {
  831. uptr p_mid = p + (Mapping::kMidAppMemBeg & Mapping::kShadowMsk);
  832. if (p_mid >= Mapping::kMidAppMemBeg && p_mid < Mapping::kMidAppMemEnd &&
  833. MemToShadowImpl::Apply<Mapping>(p_mid) == sp)
  834. return p_mid;
  835. }
  836. return p | Mapping::kShadowMsk;
  837. }
  838. };
  839. ALWAYS_INLINE
  840. uptr ShadowToMem(RawShadow *s) {
  841. return SelectMapping<ShadowToMemImpl>(reinterpret_cast<uptr>(s));
  842. }
  843. // Compresses addr to kCompressedAddrBits stored in least significant bits.
  844. ALWAYS_INLINE uptr CompressAddr(uptr addr) {
  845. return addr & ((1ull << kCompressedAddrBits) - 1);
  846. }
  847. struct RestoreAddrImpl {
  848. typedef uptr Result;
  849. template <typename Mapping>
  850. static Result Apply(uptr addr) {
  851. // To restore the address we go over all app memory ranges and check if top
  852. // 3 bits of the compressed addr match that of the app range. If yes, we
  853. // assume that the compressed address come from that range and restore the
  854. // missing top bits to match the app range address.
  855. const uptr ranges[] = {
  856. Mapping::kLoAppMemBeg, Mapping::kLoAppMemEnd, Mapping::kMidAppMemBeg,
  857. Mapping::kMidAppMemEnd, Mapping::kHiAppMemBeg, Mapping::kHiAppMemEnd,
  858. Mapping::kHeapMemBeg, Mapping::kHeapMemEnd,
  859. };
  860. const uptr indicator = 0x0e0000000000ull;
  861. const uptr ind_lsb = 1ull << LeastSignificantSetBitIndex(indicator);
  862. for (uptr i = 0; i < ARRAY_SIZE(ranges); i += 2) {
  863. uptr beg = ranges[i];
  864. uptr end = ranges[i + 1];
  865. if (beg == end)
  866. continue;
  867. for (uptr p = beg; p < end; p = RoundDown(p + ind_lsb, ind_lsb)) {
  868. if ((addr & indicator) == (p & indicator))
  869. return addr | (p & ~(ind_lsb - 1));
  870. }
  871. }
  872. Printf("ThreadSanitizer: failed to restore address 0x%zx\n", addr);
  873. Die();
  874. }
  875. };
  876. // Restores compressed addr from kCompressedAddrBits to full representation.
  877. // This is called only during reporting and is not performance-critical.
  878. inline uptr RestoreAddr(uptr addr) {
  879. return SelectMapping<RestoreAddrImpl>(addr);
  880. }
  881. // The additional page is to catch shadow stack overflow as paging fault.
  882. // Windows wants 64K alignment for mmaps.
  883. const uptr kTotalTraceSize = (kTraceSize * sizeof(Event) + sizeof(Trace)
  884. + (64 << 10) + (64 << 10) - 1) & ~((64 << 10) - 1);
  885. struct GetThreadTraceImpl {
  886. template <typename Mapping>
  887. static uptr Apply(uptr tid) {
  888. uptr p = Mapping::kTraceMemBeg + tid * kTotalTraceSize;
  889. DCHECK_LT(p, Mapping::kTraceMemEnd);
  890. return p;
  891. }
  892. };
  893. ALWAYS_INLINE
  894. uptr GetThreadTrace(int tid) { return SelectMapping<GetThreadTraceImpl>(tid); }
  895. struct GetThreadTraceHeaderImpl {
  896. template <typename Mapping>
  897. static uptr Apply(uptr tid) {
  898. uptr p = Mapping::kTraceMemBeg + tid * kTotalTraceSize +
  899. kTraceSize * sizeof(Event);
  900. DCHECK_LT(p, Mapping::kTraceMemEnd);
  901. return p;
  902. }
  903. };
  904. ALWAYS_INLINE
  905. uptr GetThreadTraceHeader(int tid) {
  906. return SelectMapping<GetThreadTraceHeaderImpl>(tid);
  907. }
  908. void InitializePlatform();
  909. void InitializePlatformEarly();
  910. void CheckAndProtect();
  911. void InitializeShadowMemoryPlatform();
  912. void FlushShadowMemory();
  913. void WriteMemoryProfile(char *buf, uptr buf_size, u64 uptime_ns);
  914. int ExtractResolvFDs(void *state, int *fds, int nfd);
  915. int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
  916. uptr ExtractLongJmpSp(uptr *env);
  917. void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size);
  918. int call_pthread_cancel_with_cleanup(int (*fn)(void *arg),
  919. void (*cleanup)(void *arg), void *arg);
  920. void DestroyThreadState();
  921. void PlatformCleanUpThreadState(ThreadState *thr);
  922. } // namespace __tsan
  923. #endif // TSAN_PLATFORM_H