asan_poisoning.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //===-- asan_poisoning.cpp ------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Shadow memory poisoning by ASan RTL and by user application.
  12. //===----------------------------------------------------------------------===//
  13. #include "asan_poisoning.h"
  14. #include "asan_report.h"
  15. #include "asan_stack.h"
  16. #include "sanitizer_common/sanitizer_atomic.h"
  17. #include "sanitizer_common/sanitizer_flags.h"
  18. #include "sanitizer_common/sanitizer_interface_internal.h"
  19. #include "sanitizer_common/sanitizer_libc.h"
  20. namespace __asan {
  21. static atomic_uint8_t can_poison_memory;
  22. void SetCanPoisonMemory(bool value) {
  23. atomic_store(&can_poison_memory, value, memory_order_release);
  24. }
  25. bool CanPoisonMemory() {
  26. return atomic_load(&can_poison_memory, memory_order_acquire);
  27. }
  28. void PoisonShadow(uptr addr, uptr size, u8 value) {
  29. if (value && !CanPoisonMemory()) return;
  30. CHECK(AddrIsAlignedByGranularity(addr));
  31. CHECK(AddrIsInMem(addr));
  32. CHECK(AddrIsAlignedByGranularity(addr + size));
  33. CHECK(AddrIsInMem(addr + size - ASAN_SHADOW_GRANULARITY));
  34. CHECK(REAL(memset));
  35. FastPoisonShadow(addr, size, value);
  36. }
  37. void PoisonShadowPartialRightRedzone(uptr addr,
  38. uptr size,
  39. uptr redzone_size,
  40. u8 value) {
  41. if (!CanPoisonMemory()) return;
  42. CHECK(AddrIsAlignedByGranularity(addr));
  43. CHECK(AddrIsInMem(addr));
  44. FastPoisonShadowPartialRightRedzone(addr, size, redzone_size, value);
  45. }
  46. struct ShadowSegmentEndpoint {
  47. u8 *chunk;
  48. s8 offset; // in [0, ASAN_SHADOW_GRANULARITY)
  49. s8 value; // = *chunk;
  50. explicit ShadowSegmentEndpoint(uptr address) {
  51. chunk = (u8*)MemToShadow(address);
  52. offset = address & (ASAN_SHADOW_GRANULARITY - 1);
  53. value = *chunk;
  54. }
  55. };
  56. void AsanPoisonOrUnpoisonIntraObjectRedzone(uptr ptr, uptr size, bool poison) {
  57. uptr end = ptr + size;
  58. if (Verbosity()) {
  59. Printf("__asan_%spoison_intra_object_redzone [%p,%p) %zd\n",
  60. poison ? "" : "un", (void *)ptr, (void *)end, size);
  61. if (Verbosity() >= 2)
  62. PRINT_CURRENT_STACK();
  63. }
  64. CHECK(size);
  65. CHECK_LE(size, 4096);
  66. CHECK(IsAligned(end, ASAN_SHADOW_GRANULARITY));
  67. if (!IsAligned(ptr, ASAN_SHADOW_GRANULARITY)) {
  68. *(u8 *)MemToShadow(ptr) =
  69. poison ? static_cast<u8>(ptr % ASAN_SHADOW_GRANULARITY) : 0;
  70. ptr |= ASAN_SHADOW_GRANULARITY - 1;
  71. ptr++;
  72. }
  73. for (; ptr < end; ptr += ASAN_SHADOW_GRANULARITY)
  74. *(u8*)MemToShadow(ptr) = poison ? kAsanIntraObjectRedzone : 0;
  75. }
  76. } // namespace __asan
  77. // ---------------------- Interface ---------------- {{{1
  78. using namespace __asan;
  79. // Current implementation of __asan_(un)poison_memory_region doesn't check
  80. // that user program (un)poisons the memory it owns. It poisons memory
  81. // conservatively, and unpoisons progressively to make sure asan shadow
  82. // mapping invariant is preserved (see detailed mapping description here:
  83. // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm).
  84. //
  85. // * if user asks to poison region [left, right), the program poisons
  86. // at least [left, AlignDown(right)).
  87. // * if user asks to unpoison region [left, right), the program unpoisons
  88. // at most [AlignDown(left), right).
  89. void __asan_poison_memory_region(void const volatile *addr, uptr size) {
  90. if (!flags()->allow_user_poisoning || size == 0) return;
  91. uptr beg_addr = (uptr)addr;
  92. uptr end_addr = beg_addr + size;
  93. VPrintf(3, "Trying to poison memory region [%p, %p)\n", (void *)beg_addr,
  94. (void *)end_addr);
  95. ShadowSegmentEndpoint beg(beg_addr);
  96. ShadowSegmentEndpoint end(end_addr);
  97. if (beg.chunk == end.chunk) {
  98. CHECK_LT(beg.offset, end.offset);
  99. s8 value = beg.value;
  100. CHECK_EQ(value, end.value);
  101. // We can only poison memory if the byte in end.offset is unaddressable.
  102. // No need to re-poison memory if it is poisoned already.
  103. if (value > 0 && value <= end.offset) {
  104. if (beg.offset > 0) {
  105. *beg.chunk = Min(value, beg.offset);
  106. } else {
  107. *beg.chunk = kAsanUserPoisonedMemoryMagic;
  108. }
  109. }
  110. return;
  111. }
  112. CHECK_LT(beg.chunk, end.chunk);
  113. if (beg.offset > 0) {
  114. // Mark bytes from beg.offset as unaddressable.
  115. if (beg.value == 0) {
  116. *beg.chunk = beg.offset;
  117. } else {
  118. *beg.chunk = Min(beg.value, beg.offset);
  119. }
  120. beg.chunk++;
  121. }
  122. REAL(memset)(beg.chunk, kAsanUserPoisonedMemoryMagic, end.chunk - beg.chunk);
  123. // Poison if byte in end.offset is unaddressable.
  124. if (end.value > 0 && end.value <= end.offset) {
  125. *end.chunk = kAsanUserPoisonedMemoryMagic;
  126. }
  127. }
  128. void __asan_unpoison_memory_region(void const volatile *addr, uptr size) {
  129. if (!flags()->allow_user_poisoning || size == 0) return;
  130. uptr beg_addr = (uptr)addr;
  131. uptr end_addr = beg_addr + size;
  132. VPrintf(3, "Trying to unpoison memory region [%p, %p)\n", (void *)beg_addr,
  133. (void *)end_addr);
  134. ShadowSegmentEndpoint beg(beg_addr);
  135. ShadowSegmentEndpoint end(end_addr);
  136. if (beg.chunk == end.chunk) {
  137. CHECK_LT(beg.offset, end.offset);
  138. s8 value = beg.value;
  139. CHECK_EQ(value, end.value);
  140. // We unpoison memory bytes up to enbytes up to end.offset if it is not
  141. // unpoisoned already.
  142. if (value != 0) {
  143. *beg.chunk = Max(value, end.offset);
  144. }
  145. return;
  146. }
  147. CHECK_LT(beg.chunk, end.chunk);
  148. if (beg.offset > 0) {
  149. *beg.chunk = 0;
  150. beg.chunk++;
  151. }
  152. REAL(memset)(beg.chunk, 0, end.chunk - beg.chunk);
  153. if (end.offset > 0 && end.value != 0) {
  154. *end.chunk = Max(end.value, end.offset);
  155. }
  156. }
  157. int __asan_address_is_poisoned(void const volatile *addr) {
  158. return __asan::AddressIsPoisoned((uptr)addr);
  159. }
  160. uptr __asan_region_is_poisoned(uptr beg, uptr size) {
  161. if (!size)
  162. return 0;
  163. uptr end = beg + size;
  164. if (!AddrIsInMem(beg))
  165. return beg;
  166. if (!AddrIsInMem(end))
  167. return end;
  168. CHECK_LT(beg, end);
  169. uptr aligned_b = RoundUpTo(beg, ASAN_SHADOW_GRANULARITY);
  170. uptr aligned_e = RoundDownTo(end, ASAN_SHADOW_GRANULARITY);
  171. uptr shadow_beg = MemToShadow(aligned_b);
  172. uptr shadow_end = MemToShadow(aligned_e);
  173. // First check the first and the last application bytes,
  174. // then check the ASAN_SHADOW_GRANULARITY-aligned region by calling
  175. // mem_is_zero on the corresponding shadow.
  176. if (!__asan::AddressIsPoisoned(beg) && !__asan::AddressIsPoisoned(end - 1) &&
  177. (shadow_end <= shadow_beg ||
  178. __sanitizer::mem_is_zero((const char *)shadow_beg,
  179. shadow_end - shadow_beg)))
  180. return 0;
  181. // The fast check failed, so we have a poisoned byte somewhere.
  182. // Find it slowly.
  183. for (; beg < end; beg++)
  184. if (__asan::AddressIsPoisoned(beg))
  185. return beg;
  186. UNREACHABLE("mem_is_zero returned false, but poisoned byte was not found");
  187. return 0;
  188. }
  189. #define CHECK_SMALL_REGION(p, size, isWrite) \
  190. do { \
  191. uptr __p = reinterpret_cast<uptr>(p); \
  192. uptr __size = size; \
  193. if (UNLIKELY(__asan::AddressIsPoisoned(__p) || \
  194. __asan::AddressIsPoisoned(__p + __size - 1))) { \
  195. GET_CURRENT_PC_BP_SP; \
  196. uptr __bad = __asan_region_is_poisoned(__p, __size); \
  197. __asan_report_error(pc, bp, sp, __bad, isWrite, __size, 0);\
  198. } \
  199. } while (false)
  200. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  201. u16 __sanitizer_unaligned_load16(const uu16 *p) {
  202. CHECK_SMALL_REGION(p, sizeof(*p), false);
  203. return *p;
  204. }
  205. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  206. u32 __sanitizer_unaligned_load32(const uu32 *p) {
  207. CHECK_SMALL_REGION(p, sizeof(*p), false);
  208. return *p;
  209. }
  210. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  211. u64 __sanitizer_unaligned_load64(const uu64 *p) {
  212. CHECK_SMALL_REGION(p, sizeof(*p), false);
  213. return *p;
  214. }
  215. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  216. void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
  217. CHECK_SMALL_REGION(p, sizeof(*p), true);
  218. *p = x;
  219. }
  220. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  221. void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
  222. CHECK_SMALL_REGION(p, sizeof(*p), true);
  223. *p = x;
  224. }
  225. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  226. void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
  227. CHECK_SMALL_REGION(p, sizeof(*p), true);
  228. *p = x;
  229. }
  230. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  231. void __asan_poison_cxx_array_cookie(uptr p) {
  232. if (SANITIZER_WORDSIZE != 64) return;
  233. if (!flags()->poison_array_cookie) return;
  234. uptr s = MEM_TO_SHADOW(p);
  235. *reinterpret_cast<u8*>(s) = kAsanArrayCookieMagic;
  236. }
  237. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  238. uptr __asan_load_cxx_array_cookie(uptr *p) {
  239. if (SANITIZER_WORDSIZE != 64) return *p;
  240. if (!flags()->poison_array_cookie) return *p;
  241. uptr s = MEM_TO_SHADOW(reinterpret_cast<uptr>(p));
  242. u8 sval = *reinterpret_cast<u8*>(s);
  243. if (sval == kAsanArrayCookieMagic) return *p;
  244. // If sval is not kAsanArrayCookieMagic it can only be freed memory,
  245. // which means that we are going to get double-free. So, return 0 to avoid
  246. // infinite loop of destructors. We don't want to report a double-free here
  247. // though, so print a warning just in case.
  248. // CHECK_EQ(sval, kAsanHeapFreeMagic);
  249. if (sval == kAsanHeapFreeMagic) {
  250. Report("AddressSanitizer: loaded array cookie from free-d memory; "
  251. "expect a double-free report\n");
  252. return 0;
  253. }
  254. // The cookie may remain unpoisoned if e.g. it comes from a custom
  255. // operator new defined inside a class.
  256. return *p;
  257. }
  258. // This is a simplified version of __asan_(un)poison_memory_region, which
  259. // assumes that left border of region to be poisoned is properly aligned.
  260. static void PoisonAlignedStackMemory(uptr addr, uptr size, bool do_poison) {
  261. if (size == 0) return;
  262. uptr aligned_size = size & ~(ASAN_SHADOW_GRANULARITY - 1);
  263. PoisonShadow(addr, aligned_size,
  264. do_poison ? kAsanStackUseAfterScopeMagic : 0);
  265. if (size == aligned_size)
  266. return;
  267. s8 end_offset = (s8)(size - aligned_size);
  268. s8* shadow_end = (s8*)MemToShadow(addr + aligned_size);
  269. s8 end_value = *shadow_end;
  270. if (do_poison) {
  271. // If possible, mark all the bytes mapping to last shadow byte as
  272. // unaddressable.
  273. if (end_value > 0 && end_value <= end_offset)
  274. *shadow_end = (s8)kAsanStackUseAfterScopeMagic;
  275. } else {
  276. // If necessary, mark few first bytes mapping to last shadow byte
  277. // as addressable
  278. if (end_value != 0)
  279. *shadow_end = Max(end_value, end_offset);
  280. }
  281. }
  282. void __asan_set_shadow_00(uptr addr, uptr size) {
  283. REAL(memset)((void *)addr, 0, size);
  284. }
  285. void __asan_set_shadow_01(uptr addr, uptr size) {
  286. REAL(memset)((void *)addr, 0x01, size);
  287. }
  288. void __asan_set_shadow_02(uptr addr, uptr size) {
  289. REAL(memset)((void *)addr, 0x02, size);
  290. }
  291. void __asan_set_shadow_03(uptr addr, uptr size) {
  292. REAL(memset)((void *)addr, 0x03, size);
  293. }
  294. void __asan_set_shadow_04(uptr addr, uptr size) {
  295. REAL(memset)((void *)addr, 0x04, size);
  296. }
  297. void __asan_set_shadow_05(uptr addr, uptr size) {
  298. REAL(memset)((void *)addr, 0x05, size);
  299. }
  300. void __asan_set_shadow_06(uptr addr, uptr size) {
  301. REAL(memset)((void *)addr, 0x06, size);
  302. }
  303. void __asan_set_shadow_07(uptr addr, uptr size) {
  304. REAL(memset)((void *)addr, 0x07, size);
  305. }
  306. void __asan_set_shadow_f1(uptr addr, uptr size) {
  307. REAL(memset)((void *)addr, 0xf1, size);
  308. }
  309. void __asan_set_shadow_f2(uptr addr, uptr size) {
  310. REAL(memset)((void *)addr, 0xf2, size);
  311. }
  312. void __asan_set_shadow_f3(uptr addr, uptr size) {
  313. REAL(memset)((void *)addr, 0xf3, size);
  314. }
  315. void __asan_set_shadow_f5(uptr addr, uptr size) {
  316. REAL(memset)((void *)addr, 0xf5, size);
  317. }
  318. void __asan_set_shadow_f8(uptr addr, uptr size) {
  319. REAL(memset)((void *)addr, 0xf8, size);
  320. }
  321. void __asan_poison_stack_memory(uptr addr, uptr size) {
  322. VReport(1, "poisoning: %p %zx\n", (void *)addr, size);
  323. PoisonAlignedStackMemory(addr, size, true);
  324. }
  325. void __asan_unpoison_stack_memory(uptr addr, uptr size) {
  326. VReport(1, "unpoisoning: %p %zx\n", (void *)addr, size);
  327. PoisonAlignedStackMemory(addr, size, false);
  328. }
  329. static void FixUnalignedStorage(uptr storage_beg, uptr storage_end,
  330. uptr &old_beg, uptr &old_end, uptr &new_beg,
  331. uptr &new_end) {
  332. constexpr uptr granularity = ASAN_SHADOW_GRANULARITY;
  333. if (UNLIKELY(!AddrIsAlignedByGranularity(storage_end))) {
  334. uptr end_down = RoundDownTo(storage_end, granularity);
  335. // Ignore the last unaligned granule if the storage is followed by
  336. // unpoisoned byte, because we can't poison the prefix anyway. Don't call
  337. // AddressIsPoisoned at all if container changes does not affect the last
  338. // granule at all.
  339. if ((((old_end != new_end) && Max(old_end, new_end) > end_down) ||
  340. ((old_beg != new_beg) && Max(old_beg, new_beg) > end_down)) &&
  341. !AddressIsPoisoned(storage_end)) {
  342. old_beg = Min(end_down, old_beg);
  343. old_end = Min(end_down, old_end);
  344. new_beg = Min(end_down, new_beg);
  345. new_end = Min(end_down, new_end);
  346. }
  347. }
  348. // Handle misaligned begin and cut it off.
  349. if (UNLIKELY(!AddrIsAlignedByGranularity(storage_beg))) {
  350. uptr beg_up = RoundUpTo(storage_beg, granularity);
  351. // The first unaligned granule needs special handling only if we had bytes
  352. // there before and will have none after.
  353. if ((new_beg == new_end || new_beg >= beg_up) && old_beg != old_end &&
  354. old_beg < beg_up) {
  355. // Keep granule prefix outside of the storage unpoisoned.
  356. uptr beg_down = RoundDownTo(storage_beg, granularity);
  357. *(u8 *)MemToShadow(beg_down) = storage_beg - beg_down;
  358. old_beg = Max(beg_up, old_beg);
  359. old_end = Max(beg_up, old_end);
  360. new_beg = Max(beg_up, new_beg);
  361. new_end = Max(beg_up, new_end);
  362. }
  363. }
  364. }
  365. void __sanitizer_annotate_contiguous_container(const void *beg_p,
  366. const void *end_p,
  367. const void *old_mid_p,
  368. const void *new_mid_p) {
  369. if (!flags()->detect_container_overflow)
  370. return;
  371. VPrintf(2, "contiguous_container: %p %p %p %p\n", beg_p, end_p, old_mid_p,
  372. new_mid_p);
  373. uptr storage_beg = reinterpret_cast<uptr>(beg_p);
  374. uptr storage_end = reinterpret_cast<uptr>(end_p);
  375. uptr old_end = reinterpret_cast<uptr>(old_mid_p);
  376. uptr new_end = reinterpret_cast<uptr>(new_mid_p);
  377. uptr old_beg = storage_beg;
  378. uptr new_beg = storage_beg;
  379. uptr granularity = ASAN_SHADOW_GRANULARITY;
  380. if (!(storage_beg <= old_end && storage_beg <= new_end &&
  381. old_end <= storage_end && new_end <= storage_end)) {
  382. GET_STACK_TRACE_FATAL_HERE;
  383. ReportBadParamsToAnnotateContiguousContainer(storage_beg, storage_end,
  384. old_end, new_end, &stack);
  385. }
  386. CHECK_LE(storage_end - storage_beg,
  387. FIRST_32_SECOND_64(1UL << 30, 1ULL << 40)); // Sanity check.
  388. if (old_end == new_end)
  389. return; // Nothing to do here.
  390. FixUnalignedStorage(storage_beg, storage_end, old_beg, old_end, new_beg,
  391. new_end);
  392. uptr a = RoundDownTo(Min(old_end, new_end), granularity);
  393. uptr c = RoundUpTo(Max(old_end, new_end), granularity);
  394. uptr d1 = RoundDownTo(old_end, granularity);
  395. // uptr d2 = RoundUpTo(old_mid, granularity);
  396. // Currently we should be in this state:
  397. // [a, d1) is good, [d2, c) is bad, [d1, d2) is partially good.
  398. // Make a quick sanity check that we are indeed in this state.
  399. //
  400. // FIXME: Two of these three checks are disabled until we fix
  401. // https://github.com/google/sanitizers/issues/258.
  402. // if (d1 != d2)
  403. // CHECK_EQ(*(u8*)MemToShadow(d1), old_mid - d1);
  404. if (a + granularity <= d1)
  405. CHECK_EQ(*(u8 *)MemToShadow(a), 0);
  406. // if (d2 + granularity <= c && c <= end)
  407. // CHECK_EQ(*(u8 *)MemToShadow(c - granularity),
  408. // kAsanContiguousContainerOOBMagic);
  409. uptr b1 = RoundDownTo(new_end, granularity);
  410. uptr b2 = RoundUpTo(new_end, granularity);
  411. // New state:
  412. // [a, b1) is good, [b2, c) is bad, [b1, b2) is partially good.
  413. if (b1 > a)
  414. PoisonShadow(a, b1 - a, 0);
  415. else if (c > b2)
  416. PoisonShadow(b2, c - b2, kAsanContiguousContainerOOBMagic);
  417. if (b1 != b2) {
  418. CHECK_EQ(b2 - b1, granularity);
  419. *(u8 *)MemToShadow(b1) = static_cast<u8>(new_end - b1);
  420. }
  421. }
  422. // Annotates a double ended contiguous memory area like std::deque's chunk.
  423. // It allows detecting buggy accesses to allocated but not used begining
  424. // or end items of such a container.
  425. void __sanitizer_annotate_double_ended_contiguous_container(
  426. const void *storage_beg_p, const void *storage_end_p,
  427. const void *old_container_beg_p, const void *old_container_end_p,
  428. const void *new_container_beg_p, const void *new_container_end_p) {
  429. if (!flags()->detect_container_overflow)
  430. return;
  431. VPrintf(2, "contiguous_container: %p %p %p %p %p %p\n", storage_beg_p,
  432. storage_end_p, old_container_beg_p, old_container_end_p,
  433. new_container_beg_p, new_container_end_p);
  434. uptr storage_beg = reinterpret_cast<uptr>(storage_beg_p);
  435. uptr storage_end = reinterpret_cast<uptr>(storage_end_p);
  436. uptr old_beg = reinterpret_cast<uptr>(old_container_beg_p);
  437. uptr old_end = reinterpret_cast<uptr>(old_container_end_p);
  438. uptr new_beg = reinterpret_cast<uptr>(new_container_beg_p);
  439. uptr new_end = reinterpret_cast<uptr>(new_container_end_p);
  440. constexpr uptr granularity = ASAN_SHADOW_GRANULARITY;
  441. if (!(old_beg <= old_end && new_beg <= new_end) ||
  442. !(storage_beg <= new_beg && new_end <= storage_end) ||
  443. !(storage_beg <= old_beg && old_end <= storage_end)) {
  444. GET_STACK_TRACE_FATAL_HERE;
  445. ReportBadParamsToAnnotateDoubleEndedContiguousContainer(
  446. storage_beg, storage_end, old_beg, old_end, new_beg, new_end, &stack);
  447. }
  448. CHECK_LE(storage_end - storage_beg,
  449. FIRST_32_SECOND_64(1UL << 30, 1ULL << 40)); // Sanity check.
  450. if ((old_beg == old_end && new_beg == new_end) ||
  451. (old_beg == new_beg && old_end == new_end))
  452. return; // Nothing to do here.
  453. FixUnalignedStorage(storage_beg, storage_end, old_beg, old_end, new_beg,
  454. new_end);
  455. // Handle non-intersecting new/old containers separately have simpler
  456. // intersecting case.
  457. if (old_beg == old_end || new_beg == new_end || new_end <= old_beg ||
  458. old_end <= new_beg) {
  459. if (old_beg != old_end) {
  460. // Poisoning the old container.
  461. uptr a = RoundDownTo(old_beg, granularity);
  462. uptr b = RoundUpTo(old_end, granularity);
  463. PoisonShadow(a, b - a, kAsanContiguousContainerOOBMagic);
  464. }
  465. if (new_beg != new_end) {
  466. // Unpoisoning the new container.
  467. uptr a = RoundDownTo(new_beg, granularity);
  468. uptr b = RoundDownTo(new_end, granularity);
  469. PoisonShadow(a, b - a, 0);
  470. if (!AddrIsAlignedByGranularity(new_end))
  471. *(u8 *)MemToShadow(b) = static_cast<u8>(new_end - b);
  472. }
  473. return;
  474. }
  475. // Intersection of old and new containers is not empty.
  476. CHECK_LT(new_beg, old_end);
  477. CHECK_GT(new_end, old_beg);
  478. if (new_beg < old_beg) {
  479. // Round down because we can't poison prefixes.
  480. uptr a = RoundDownTo(new_beg, granularity);
  481. // Round down and ignore the [c, old_beg) as its state defined by unchanged
  482. // [old_beg, old_end).
  483. uptr c = RoundDownTo(old_beg, granularity);
  484. PoisonShadow(a, c - a, 0);
  485. } else if (new_beg > old_beg) {
  486. // Round down and poison [a, old_beg) because it was unpoisoned only as a
  487. // prefix.
  488. uptr a = RoundDownTo(old_beg, granularity);
  489. // Round down and ignore the [c, new_beg) as its state defined by unchanged
  490. // [new_beg, old_end).
  491. uptr c = RoundDownTo(new_beg, granularity);
  492. PoisonShadow(a, c - a, kAsanContiguousContainerOOBMagic);
  493. }
  494. if (new_end > old_end) {
  495. // Round down to poison the prefix.
  496. uptr a = RoundDownTo(old_end, granularity);
  497. // Round down and handle remainder below.
  498. uptr c = RoundDownTo(new_end, granularity);
  499. PoisonShadow(a, c - a, 0);
  500. if (!AddrIsAlignedByGranularity(new_end))
  501. *(u8 *)MemToShadow(c) = static_cast<u8>(new_end - c);
  502. } else if (new_end < old_end) {
  503. // Round up and handle remained below.
  504. uptr a2 = RoundUpTo(new_end, granularity);
  505. // Round up to poison entire granule as we had nothing in [old_end, c2).
  506. uptr c2 = RoundUpTo(old_end, granularity);
  507. PoisonShadow(a2, c2 - a2, kAsanContiguousContainerOOBMagic);
  508. if (!AddrIsAlignedByGranularity(new_end)) {
  509. uptr a = RoundDownTo(new_end, granularity);
  510. *(u8 *)MemToShadow(a) = static_cast<u8>(new_end - a);
  511. }
  512. }
  513. }
  514. static const void *FindBadAddress(uptr begin, uptr end, bool poisoned) {
  515. CHECK_LE(begin, end);
  516. constexpr uptr kMaxRangeToCheck = 32;
  517. if (end - begin > kMaxRangeToCheck * 2) {
  518. if (auto *bad = FindBadAddress(begin, begin + kMaxRangeToCheck, poisoned))
  519. return bad;
  520. if (auto *bad = FindBadAddress(end - kMaxRangeToCheck, end, poisoned))
  521. return bad;
  522. }
  523. for (uptr i = begin; i < end; ++i)
  524. if (AddressIsPoisoned(i) != poisoned)
  525. return reinterpret_cast<const void *>(i);
  526. return nullptr;
  527. }
  528. const void *__sanitizer_contiguous_container_find_bad_address(
  529. const void *beg_p, const void *mid_p, const void *end_p) {
  530. if (!flags()->detect_container_overflow)
  531. return nullptr;
  532. uptr granularity = ASAN_SHADOW_GRANULARITY;
  533. uptr beg = reinterpret_cast<uptr>(beg_p);
  534. uptr end = reinterpret_cast<uptr>(end_p);
  535. uptr mid = reinterpret_cast<uptr>(mid_p);
  536. CHECK_LE(beg, mid);
  537. CHECK_LE(mid, end);
  538. // If the byte after the storage is unpoisoned, everything in the granule
  539. // before must stay unpoisoned.
  540. uptr annotations_end =
  541. (!AddrIsAlignedByGranularity(end) && !AddressIsPoisoned(end))
  542. ? RoundDownTo(end, granularity)
  543. : end;
  544. beg = Min(beg, annotations_end);
  545. mid = Min(mid, annotations_end);
  546. if (auto *bad = FindBadAddress(beg, mid, false))
  547. return bad;
  548. if (auto *bad = FindBadAddress(mid, annotations_end, true))
  549. return bad;
  550. return FindBadAddress(annotations_end, end, false);
  551. }
  552. int __sanitizer_verify_contiguous_container(const void *beg_p,
  553. const void *mid_p,
  554. const void *end_p) {
  555. return __sanitizer_contiguous_container_find_bad_address(beg_p, mid_p,
  556. end_p) == nullptr;
  557. }
  558. const void *__sanitizer_double_ended_contiguous_container_find_bad_address(
  559. const void *storage_beg_p, const void *container_beg_p,
  560. const void *container_end_p, const void *storage_end_p) {
  561. if (!flags()->detect_container_overflow)
  562. return nullptr;
  563. uptr granularity = ASAN_SHADOW_GRANULARITY;
  564. uptr storage_beg = reinterpret_cast<uptr>(storage_beg_p);
  565. uptr storage_end = reinterpret_cast<uptr>(storage_end_p);
  566. uptr beg = reinterpret_cast<uptr>(container_beg_p);
  567. uptr end = reinterpret_cast<uptr>(container_end_p);
  568. // The prefix of the firs granule of the container is unpoisoned.
  569. if (beg != end)
  570. beg = Max(storage_beg, RoundDownTo(beg, granularity));
  571. // If the byte after the storage is unpoisoned, the prefix of the last granule
  572. // is unpoisoned.
  573. uptr annotations_end = (!AddrIsAlignedByGranularity(storage_end) &&
  574. !AddressIsPoisoned(storage_end))
  575. ? RoundDownTo(storage_end, granularity)
  576. : storage_end;
  577. storage_beg = Min(storage_beg, annotations_end);
  578. beg = Min(beg, annotations_end);
  579. end = Min(end, annotations_end);
  580. if (auto *bad = FindBadAddress(storage_beg, beg, true))
  581. return bad;
  582. if (auto *bad = FindBadAddress(beg, end, false))
  583. return bad;
  584. if (auto *bad = FindBadAddress(end, annotations_end, true))
  585. return bad;
  586. return FindBadAddress(annotations_end, storage_end, false);
  587. }
  588. int __sanitizer_verify_double_ended_contiguous_container(
  589. const void *storage_beg_p, const void *container_beg_p,
  590. const void *container_end_p, const void *storage_end_p) {
  591. return __sanitizer_double_ended_contiguous_container_find_bad_address(
  592. storage_beg_p, container_beg_p, container_end_p, storage_end_p) ==
  593. nullptr;
  594. }
  595. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  596. void __asan_poison_intra_object_redzone(uptr ptr, uptr size) {
  597. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, true);
  598. }
  599. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  600. void __asan_unpoison_intra_object_redzone(uptr ptr, uptr size) {
  601. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, false);
  602. }
  603. // --- Implementation of LSan-specific functions --- {{{1
  604. namespace __lsan {
  605. bool WordIsPoisoned(uptr addr) {
  606. return (__asan_region_is_poisoned(addr, sizeof(uptr)) != 0);
  607. }
  608. }