tsan_platform.h 36 KB

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