tsan_platform.h 40 KB

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