asan_poisoning.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. REAL(memset)(beg.chunk, 0, end.chunk - beg.chunk);
  149. if (end.offset > 0 && end.value != 0) {
  150. *end.chunk = Max(end.value, end.offset);
  151. }
  152. }
  153. int __asan_address_is_poisoned(void const volatile *addr) {
  154. return __asan::AddressIsPoisoned((uptr)addr);
  155. }
  156. uptr __asan_region_is_poisoned(uptr beg, uptr size) {
  157. if (!size)
  158. return 0;
  159. uptr end = beg + size;
  160. if (!AddrIsInMem(beg))
  161. return beg;
  162. if (!AddrIsInMem(end))
  163. return end;
  164. CHECK_LT(beg, end);
  165. uptr aligned_b = RoundUpTo(beg, ASAN_SHADOW_GRANULARITY);
  166. uptr aligned_e = RoundDownTo(end, ASAN_SHADOW_GRANULARITY);
  167. uptr shadow_beg = MemToShadow(aligned_b);
  168. uptr shadow_end = MemToShadow(aligned_e);
  169. // First check the first and the last application bytes,
  170. // then check the ASAN_SHADOW_GRANULARITY-aligned region by calling
  171. // mem_is_zero on the corresponding shadow.
  172. if (!__asan::AddressIsPoisoned(beg) && !__asan::AddressIsPoisoned(end - 1) &&
  173. (shadow_end <= shadow_beg ||
  174. __sanitizer::mem_is_zero((const char *)shadow_beg,
  175. shadow_end - shadow_beg)))
  176. return 0;
  177. // The fast check failed, so we have a poisoned byte somewhere.
  178. // Find it slowly.
  179. for (; beg < end; beg++)
  180. if (__asan::AddressIsPoisoned(beg))
  181. return beg;
  182. UNREACHABLE("mem_is_zero returned false, but poisoned byte was not found");
  183. return 0;
  184. }
  185. #define CHECK_SMALL_REGION(p, size, isWrite) \
  186. do { \
  187. uptr __p = reinterpret_cast<uptr>(p); \
  188. uptr __size = size; \
  189. if (UNLIKELY(__asan::AddressIsPoisoned(__p) || \
  190. __asan::AddressIsPoisoned(__p + __size - 1))) { \
  191. GET_CURRENT_PC_BP_SP; \
  192. uptr __bad = __asan_region_is_poisoned(__p, __size); \
  193. __asan_report_error(pc, bp, sp, __bad, isWrite, __size, 0);\
  194. } \
  195. } while (false)
  196. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  197. u16 __sanitizer_unaligned_load16(const uu16 *p) {
  198. CHECK_SMALL_REGION(p, sizeof(*p), false);
  199. return *p;
  200. }
  201. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  202. u32 __sanitizer_unaligned_load32(const uu32 *p) {
  203. CHECK_SMALL_REGION(p, sizeof(*p), false);
  204. return *p;
  205. }
  206. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  207. u64 __sanitizer_unaligned_load64(const uu64 *p) {
  208. CHECK_SMALL_REGION(p, sizeof(*p), false);
  209. return *p;
  210. }
  211. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  212. void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
  213. CHECK_SMALL_REGION(p, sizeof(*p), true);
  214. *p = x;
  215. }
  216. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  217. void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
  218. CHECK_SMALL_REGION(p, sizeof(*p), true);
  219. *p = x;
  220. }
  221. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  222. void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
  223. CHECK_SMALL_REGION(p, sizeof(*p), true);
  224. *p = x;
  225. }
  226. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  227. void __asan_poison_cxx_array_cookie(uptr p) {
  228. if (SANITIZER_WORDSIZE != 64) return;
  229. if (!flags()->poison_array_cookie) return;
  230. uptr s = MEM_TO_SHADOW(p);
  231. *reinterpret_cast<u8*>(s) = kAsanArrayCookieMagic;
  232. }
  233. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  234. uptr __asan_load_cxx_array_cookie(uptr *p) {
  235. if (SANITIZER_WORDSIZE != 64) return *p;
  236. if (!flags()->poison_array_cookie) return *p;
  237. uptr s = MEM_TO_SHADOW(reinterpret_cast<uptr>(p));
  238. u8 sval = *reinterpret_cast<u8*>(s);
  239. if (sval == kAsanArrayCookieMagic) return *p;
  240. // If sval is not kAsanArrayCookieMagic it can only be freed memory,
  241. // which means that we are going to get double-free. So, return 0 to avoid
  242. // infinite loop of destructors. We don't want to report a double-free here
  243. // though, so print a warning just in case.
  244. // CHECK_EQ(sval, kAsanHeapFreeMagic);
  245. if (sval == kAsanHeapFreeMagic) {
  246. Report("AddressSanitizer: loaded array cookie from free-d memory; "
  247. "expect a double-free report\n");
  248. return 0;
  249. }
  250. // The cookie may remain unpoisoned if e.g. it comes from a custom
  251. // operator new defined inside a class.
  252. return *p;
  253. }
  254. // This is a simplified version of __asan_(un)poison_memory_region, which
  255. // assumes that left border of region to be poisoned is properly aligned.
  256. static void PoisonAlignedStackMemory(uptr addr, uptr size, bool do_poison) {
  257. if (size == 0) return;
  258. uptr aligned_size = size & ~(ASAN_SHADOW_GRANULARITY - 1);
  259. PoisonShadow(addr, aligned_size,
  260. do_poison ? kAsanStackUseAfterScopeMagic : 0);
  261. if (size == aligned_size)
  262. return;
  263. s8 end_offset = (s8)(size - aligned_size);
  264. s8* shadow_end = (s8*)MemToShadow(addr + aligned_size);
  265. s8 end_value = *shadow_end;
  266. if (do_poison) {
  267. // If possible, mark all the bytes mapping to last shadow byte as
  268. // unaddressable.
  269. if (end_value > 0 && end_value <= end_offset)
  270. *shadow_end = (s8)kAsanStackUseAfterScopeMagic;
  271. } else {
  272. // If necessary, mark few first bytes mapping to last shadow byte
  273. // as addressable
  274. if (end_value != 0)
  275. *shadow_end = Max(end_value, end_offset);
  276. }
  277. }
  278. void __asan_set_shadow_00(uptr addr, uptr size) {
  279. REAL(memset)((void *)addr, 0, size);
  280. }
  281. void __asan_set_shadow_01(uptr addr, uptr size) {
  282. REAL(memset)((void *)addr, 0x01, size);
  283. }
  284. void __asan_set_shadow_02(uptr addr, uptr size) {
  285. REAL(memset)((void *)addr, 0x02, size);
  286. }
  287. void __asan_set_shadow_03(uptr addr, uptr size) {
  288. REAL(memset)((void *)addr, 0x03, size);
  289. }
  290. void __asan_set_shadow_04(uptr addr, uptr size) {
  291. REAL(memset)((void *)addr, 0x04, size);
  292. }
  293. void __asan_set_shadow_05(uptr addr, uptr size) {
  294. REAL(memset)((void *)addr, 0x05, size);
  295. }
  296. void __asan_set_shadow_06(uptr addr, uptr size) {
  297. REAL(memset)((void *)addr, 0x06, size);
  298. }
  299. void __asan_set_shadow_07(uptr addr, uptr size) {
  300. REAL(memset)((void *)addr, 0x07, size);
  301. }
  302. void __asan_set_shadow_f1(uptr addr, uptr size) {
  303. REAL(memset)((void *)addr, 0xf1, size);
  304. }
  305. void __asan_set_shadow_f2(uptr addr, uptr size) {
  306. REAL(memset)((void *)addr, 0xf2, size);
  307. }
  308. void __asan_set_shadow_f3(uptr addr, uptr size) {
  309. REAL(memset)((void *)addr, 0xf3, size);
  310. }
  311. void __asan_set_shadow_f5(uptr addr, uptr size) {
  312. REAL(memset)((void *)addr, 0xf5, size);
  313. }
  314. void __asan_set_shadow_f8(uptr addr, uptr size) {
  315. REAL(memset)((void *)addr, 0xf8, size);
  316. }
  317. void __asan_poison_stack_memory(uptr addr, uptr size) {
  318. VReport(1, "poisoning: %p %zx\n", (void *)addr, size);
  319. PoisonAlignedStackMemory(addr, size, true);
  320. }
  321. void __asan_unpoison_stack_memory(uptr addr, uptr size) {
  322. VReport(1, "unpoisoning: %p %zx\n", (void *)addr, size);
  323. PoisonAlignedStackMemory(addr, size, false);
  324. }
  325. static void FixUnalignedStorage(uptr storage_beg, uptr storage_end,
  326. uptr &old_beg, uptr &old_end, uptr &new_beg,
  327. uptr &new_end) {
  328. constexpr uptr granularity = ASAN_SHADOW_GRANULARITY;
  329. if (UNLIKELY(!AddrIsAlignedByGranularity(storage_end))) {
  330. uptr end_down = RoundDownTo(storage_end, granularity);
  331. // Ignore the last unaligned granule if the storage is followed by
  332. // unpoisoned byte, because we can't poison the prefix anyway. Don't call
  333. // AddressIsPoisoned at all if container changes does not affect the last
  334. // granule at all.
  335. if ((((old_end != new_end) && Max(old_end, new_end) > end_down) ||
  336. ((old_beg != new_beg) && Max(old_beg, new_beg) > end_down)) &&
  337. !AddressIsPoisoned(storage_end)) {
  338. old_beg = Min(end_down, old_beg);
  339. old_end = Min(end_down, old_end);
  340. new_beg = Min(end_down, new_beg);
  341. new_end = Min(end_down, new_end);
  342. }
  343. }
  344. // Handle misaligned begin and cut it off.
  345. if (UNLIKELY(!AddrIsAlignedByGranularity(storage_beg))) {
  346. uptr beg_up = RoundUpTo(storage_beg, granularity);
  347. // The first unaligned granule needs special handling only if we had bytes
  348. // there before and will have none after.
  349. if ((new_beg == new_end || new_beg >= beg_up) && old_beg != old_end &&
  350. old_beg < beg_up) {
  351. // Keep granule prefix outside of the storage unpoisoned.
  352. uptr beg_down = RoundDownTo(storage_beg, granularity);
  353. *(u8 *)MemToShadow(beg_down) = storage_beg - beg_down;
  354. old_beg = Max(beg_up, old_beg);
  355. old_end = Max(beg_up, old_end);
  356. new_beg = Max(beg_up, new_beg);
  357. new_end = Max(beg_up, new_end);
  358. }
  359. }
  360. }
  361. void __sanitizer_annotate_contiguous_container(const void *beg_p,
  362. const void *end_p,
  363. const void *old_mid_p,
  364. const void *new_mid_p) {
  365. if (!flags()->detect_container_overflow)
  366. return;
  367. VPrintf(2, "contiguous_container: %p %p %p %p\n", beg_p, end_p, old_mid_p,
  368. new_mid_p);
  369. uptr storage_beg = reinterpret_cast<uptr>(beg_p);
  370. uptr storage_end = reinterpret_cast<uptr>(end_p);
  371. uptr old_end = reinterpret_cast<uptr>(old_mid_p);
  372. uptr new_end = reinterpret_cast<uptr>(new_mid_p);
  373. uptr old_beg = storage_beg;
  374. uptr new_beg = storage_beg;
  375. uptr granularity = ASAN_SHADOW_GRANULARITY;
  376. if (!(storage_beg <= old_end && storage_beg <= new_end &&
  377. old_end <= storage_end && new_end <= storage_end)) {
  378. GET_STACK_TRACE_FATAL_HERE;
  379. ReportBadParamsToAnnotateContiguousContainer(storage_beg, storage_end,
  380. old_end, new_end, &stack);
  381. }
  382. CHECK_LE(storage_end - storage_beg,
  383. FIRST_32_SECOND_64(1UL << 30, 1ULL << 40)); // Sanity check.
  384. if (old_end == new_end)
  385. return; // Nothing to do here.
  386. FixUnalignedStorage(storage_beg, storage_end, old_beg, old_end, new_beg,
  387. new_end);
  388. uptr a = RoundDownTo(Min(old_end, new_end), granularity);
  389. uptr c = RoundUpTo(Max(old_end, new_end), granularity);
  390. uptr d1 = RoundDownTo(old_end, granularity);
  391. // uptr d2 = RoundUpTo(old_mid, granularity);
  392. // Currently we should be in this state:
  393. // [a, d1) is good, [d2, c) is bad, [d1, d2) is partially good.
  394. // Make a quick sanity check that we are indeed in this state.
  395. //
  396. // FIXME: Two of these three checks are disabled until we fix
  397. // https://github.com/google/sanitizers/issues/258.
  398. // if (d1 != d2)
  399. // DCHECK_EQ(*(u8*)MemToShadow(d1), old_mid - d1);
  400. //
  401. // NOTE: curly brackets for the "if" below to silence a MSVC warning.
  402. if (a + granularity <= d1) {
  403. DCHECK_EQ(*(u8 *)MemToShadow(a), 0);
  404. }
  405. // if (d2 + granularity <= c && c <= end)
  406. // DCHECK_EQ(*(u8 *)MemToShadow(c - granularity),
  407. // kAsanContiguousContainerOOBMagic);
  408. uptr b1 = RoundDownTo(new_end, granularity);
  409. uptr b2 = RoundUpTo(new_end, granularity);
  410. // New state:
  411. // [a, b1) is good, [b2, c) is bad, [b1, b2) is partially good.
  412. if (b1 > a)
  413. PoisonShadow(a, b1 - a, 0);
  414. else if (c > b2)
  415. PoisonShadow(b2, c - b2, kAsanContiguousContainerOOBMagic);
  416. if (b1 != b2) {
  417. CHECK_EQ(b2 - b1, granularity);
  418. *(u8 *)MemToShadow(b1) = static_cast<u8>(new_end - b1);
  419. }
  420. }
  421. // Annotates a double ended contiguous memory area like std::deque's chunk.
  422. // It allows detecting buggy accesses to allocated but not used begining
  423. // or end items of such a container.
  424. void __sanitizer_annotate_double_ended_contiguous_container(
  425. const void *storage_beg_p, const void *storage_end_p,
  426. const void *old_container_beg_p, const void *old_container_end_p,
  427. const void *new_container_beg_p, const void *new_container_end_p) {
  428. if (!flags()->detect_container_overflow)
  429. return;
  430. VPrintf(2, "contiguous_container: %p %p %p %p %p %p\n", storage_beg_p,
  431. storage_end_p, old_container_beg_p, old_container_end_p,
  432. new_container_beg_p, new_container_end_p);
  433. uptr storage_beg = reinterpret_cast<uptr>(storage_beg_p);
  434. uptr storage_end = reinterpret_cast<uptr>(storage_end_p);
  435. uptr old_beg = reinterpret_cast<uptr>(old_container_beg_p);
  436. uptr old_end = reinterpret_cast<uptr>(old_container_end_p);
  437. uptr new_beg = reinterpret_cast<uptr>(new_container_beg_p);
  438. uptr new_end = reinterpret_cast<uptr>(new_container_end_p);
  439. constexpr uptr granularity = ASAN_SHADOW_GRANULARITY;
  440. if (!(old_beg <= old_end && new_beg <= new_end) ||
  441. !(storage_beg <= new_beg && new_end <= storage_end) ||
  442. !(storage_beg <= old_beg && old_end <= storage_end)) {
  443. GET_STACK_TRACE_FATAL_HERE;
  444. ReportBadParamsToAnnotateDoubleEndedContiguousContainer(
  445. storage_beg, storage_end, old_beg, old_end, new_beg, new_end, &stack);
  446. }
  447. CHECK_LE(storage_end - storage_beg,
  448. FIRST_32_SECOND_64(1UL << 30, 1ULL << 40)); // Sanity check.
  449. if ((old_beg == old_end && new_beg == new_end) ||
  450. (old_beg == new_beg && old_end == new_end))
  451. return; // Nothing to do here.
  452. FixUnalignedStorage(storage_beg, storage_end, old_beg, old_end, new_beg,
  453. new_end);
  454. // Handle non-intersecting new/old containers separately have simpler
  455. // intersecting case.
  456. if (old_beg == old_end || new_beg == new_end || new_end <= old_beg ||
  457. old_end <= new_beg) {
  458. if (old_beg != old_end) {
  459. // Poisoning the old container.
  460. uptr a = RoundDownTo(old_beg, granularity);
  461. uptr b = RoundUpTo(old_end, granularity);
  462. PoisonShadow(a, b - a, kAsanContiguousContainerOOBMagic);
  463. }
  464. if (new_beg != new_end) {
  465. // Unpoisoning the new container.
  466. uptr a = RoundDownTo(new_beg, granularity);
  467. uptr b = RoundDownTo(new_end, granularity);
  468. PoisonShadow(a, b - a, 0);
  469. if (!AddrIsAlignedByGranularity(new_end))
  470. *(u8 *)MemToShadow(b) = static_cast<u8>(new_end - b);
  471. }
  472. return;
  473. }
  474. // Intersection of old and new containers is not empty.
  475. CHECK_LT(new_beg, old_end);
  476. CHECK_GT(new_end, old_beg);
  477. if (new_beg < old_beg) {
  478. // Round down because we can't poison prefixes.
  479. uptr a = RoundDownTo(new_beg, granularity);
  480. // Round down and ignore the [c, old_beg) as its state defined by unchanged
  481. // [old_beg, old_end).
  482. uptr c = RoundDownTo(old_beg, granularity);
  483. PoisonShadow(a, c - a, 0);
  484. } else if (new_beg > old_beg) {
  485. // Round down and poison [a, old_beg) because it was unpoisoned only as a
  486. // prefix.
  487. uptr a = RoundDownTo(old_beg, granularity);
  488. // Round down and ignore the [c, new_beg) as its state defined by unchanged
  489. // [new_beg, old_end).
  490. uptr c = RoundDownTo(new_beg, granularity);
  491. PoisonShadow(a, c - a, kAsanContiguousContainerOOBMagic);
  492. }
  493. if (new_end > old_end) {
  494. // Round down to poison the prefix.
  495. uptr a = RoundDownTo(old_end, granularity);
  496. // Round down and handle remainder below.
  497. uptr c = RoundDownTo(new_end, granularity);
  498. PoisonShadow(a, c - a, 0);
  499. if (!AddrIsAlignedByGranularity(new_end))
  500. *(u8 *)MemToShadow(c) = static_cast<u8>(new_end - c);
  501. } else if (new_end < old_end) {
  502. // Round up and handle remained below.
  503. uptr a2 = RoundUpTo(new_end, granularity);
  504. // Round up to poison entire granule as we had nothing in [old_end, c2).
  505. uptr c2 = RoundUpTo(old_end, granularity);
  506. PoisonShadow(a2, c2 - a2, kAsanContiguousContainerOOBMagic);
  507. if (!AddrIsAlignedByGranularity(new_end)) {
  508. uptr a = RoundDownTo(new_end, granularity);
  509. *(u8 *)MemToShadow(a) = static_cast<u8>(new_end - a);
  510. }
  511. }
  512. }
  513. static const void *FindBadAddress(uptr begin, uptr end, bool poisoned) {
  514. CHECK_LE(begin, end);
  515. constexpr uptr kMaxRangeToCheck = 32;
  516. if (end - begin > kMaxRangeToCheck * 2) {
  517. if (auto *bad = FindBadAddress(begin, begin + kMaxRangeToCheck, poisoned))
  518. return bad;
  519. if (auto *bad = FindBadAddress(end - kMaxRangeToCheck, end, poisoned))
  520. return bad;
  521. }
  522. for (uptr i = begin; i < end; ++i)
  523. if (AddressIsPoisoned(i) != poisoned)
  524. return reinterpret_cast<const void *>(i);
  525. return nullptr;
  526. }
  527. const void *__sanitizer_contiguous_container_find_bad_address(
  528. const void *beg_p, const void *mid_p, const void *end_p) {
  529. if (!flags()->detect_container_overflow)
  530. return nullptr;
  531. uptr granularity = ASAN_SHADOW_GRANULARITY;
  532. uptr beg = reinterpret_cast<uptr>(beg_p);
  533. uptr end = reinterpret_cast<uptr>(end_p);
  534. uptr mid = reinterpret_cast<uptr>(mid_p);
  535. CHECK_LE(beg, mid);
  536. CHECK_LE(mid, end);
  537. // If the byte after the storage is unpoisoned, everything in the granule
  538. // before must stay unpoisoned.
  539. uptr annotations_end =
  540. (!AddrIsAlignedByGranularity(end) && !AddressIsPoisoned(end))
  541. ? RoundDownTo(end, granularity)
  542. : end;
  543. beg = Min(beg, annotations_end);
  544. mid = Min(mid, annotations_end);
  545. if (auto *bad = FindBadAddress(beg, mid, false))
  546. return bad;
  547. if (auto *bad = FindBadAddress(mid, annotations_end, true))
  548. return bad;
  549. return FindBadAddress(annotations_end, end, false);
  550. }
  551. int __sanitizer_verify_contiguous_container(const void *beg_p,
  552. const void *mid_p,
  553. const void *end_p) {
  554. return __sanitizer_contiguous_container_find_bad_address(beg_p, mid_p,
  555. end_p) == nullptr;
  556. }
  557. const void *__sanitizer_double_ended_contiguous_container_find_bad_address(
  558. const void *storage_beg_p, const void *container_beg_p,
  559. const void *container_end_p, const void *storage_end_p) {
  560. if (!flags()->detect_container_overflow)
  561. return nullptr;
  562. uptr granularity = ASAN_SHADOW_GRANULARITY;
  563. uptr storage_beg = reinterpret_cast<uptr>(storage_beg_p);
  564. uptr storage_end = reinterpret_cast<uptr>(storage_end_p);
  565. uptr beg = reinterpret_cast<uptr>(container_beg_p);
  566. uptr end = reinterpret_cast<uptr>(container_end_p);
  567. // The prefix of the firs granule of the container is unpoisoned.
  568. if (beg != end)
  569. beg = Max(storage_beg, RoundDownTo(beg, granularity));
  570. // If the byte after the storage is unpoisoned, the prefix of the last granule
  571. // is unpoisoned.
  572. uptr annotations_end = (!AddrIsAlignedByGranularity(storage_end) &&
  573. !AddressIsPoisoned(storage_end))
  574. ? RoundDownTo(storage_end, granularity)
  575. : storage_end;
  576. storage_beg = Min(storage_beg, annotations_end);
  577. beg = Min(beg, annotations_end);
  578. end = Min(end, annotations_end);
  579. if (auto *bad = FindBadAddress(storage_beg, beg, true))
  580. return bad;
  581. if (auto *bad = FindBadAddress(beg, end, false))
  582. return bad;
  583. if (auto *bad = FindBadAddress(end, annotations_end, true))
  584. return bad;
  585. return FindBadAddress(annotations_end, storage_end, false);
  586. }
  587. int __sanitizer_verify_double_ended_contiguous_container(
  588. const void *storage_beg_p, const void *container_beg_p,
  589. const void *container_end_p, const void *storage_end_p) {
  590. return __sanitizer_double_ended_contiguous_container_find_bad_address(
  591. storage_beg_p, container_beg_p, container_end_p, storage_end_p) ==
  592. nullptr;
  593. }
  594. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  595. void __asan_poison_intra_object_redzone(uptr ptr, uptr size) {
  596. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, true);
  597. }
  598. extern "C" SANITIZER_INTERFACE_ATTRIBUTE
  599. void __asan_unpoison_intra_object_redzone(uptr ptr, uptr size) {
  600. AsanPoisonOrUnpoisonIntraObjectRedzone(ptr, size, false);
  601. }
  602. // --- Implementation of LSan-specific functions --- {{{1
  603. namespace __lsan {
  604. bool WordIsPoisoned(uptr addr) {
  605. return (__asan_region_is_poisoned(addr, sizeof(uptr)) != 0);
  606. }
  607. }