tsan_platform.h 33 KB

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