asan_poisoning.cc revision 7e8434940a1fe7dce531d4c458ccd714da48f609
1//===-- asan_poisoning.cc -------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Shadow memory poisoning by ASan RTL and by user application.
13//===----------------------------------------------------------------------===//
14
15#include "asan_poisoning.h"
16#include "sanitizer_common/sanitizer_libc.h"
17
18namespace __asan {
19
20void PoisonShadow(uptr addr, uptr size, u8 value) {
21  if (!flags()->poison_heap) return;
22  CHECK(AddrIsAlignedByGranularity(addr));
23  CHECK(AddrIsInMem(addr));
24  CHECK(AddrIsAlignedByGranularity(addr + size));
25  CHECK(AddrIsInMem(addr + size - SHADOW_GRANULARITY));
26  CHECK(REAL(memset) != 0);
27  FastPoisonShadow(addr, size, value);
28}
29
30void PoisonShadowPartialRightRedzone(uptr addr,
31                                     uptr size,
32                                     uptr redzone_size,
33                                     u8 value) {
34  if (!flags()->poison_heap) return;
35  CHECK(AddrIsAlignedByGranularity(addr));
36  CHECK(AddrIsInMem(addr));
37  FastPoisonShadowPartialRightRedzone(addr, size, redzone_size, value);
38}
39
40struct ShadowSegmentEndpoint {
41  u8 *chunk;
42  s8 offset;  // in [0, SHADOW_GRANULARITY)
43  s8 value;  // = *chunk;
44
45  explicit ShadowSegmentEndpoint(uptr address) {
46    chunk = (u8*)MemToShadow(address);
47    offset = address & (SHADOW_GRANULARITY - 1);
48    value = *chunk;
49  }
50};
51
52}  // namespace __asan
53
54// ---------------------- Interface ---------------- {{{1
55using namespace __asan;  // NOLINT
56
57// Current implementation of __asan_(un)poison_memory_region doesn't check
58// that user program (un)poisons the memory it owns. It poisons memory
59// conservatively, and unpoisons progressively to make sure asan shadow
60// mapping invariant is preserved (see detailed mapping description here:
61// http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm).
62//
63// * if user asks to poison region [left, right), the program poisons
64// at least [left, AlignDown(right)).
65// * if user asks to unpoison region [left, right), the program unpoisons
66// at most [AlignDown(left), right).
67void __asan_poison_memory_region(void const volatile *addr, uptr size) {
68  if (!flags()->allow_user_poisoning || size == 0) return;
69  uptr beg_addr = (uptr)addr;
70  uptr end_addr = beg_addr + size;
71  if (flags()->verbosity >= 1) {
72    Printf("Trying to poison memory region [%p, %p)\n",
73           (void*)beg_addr, (void*)end_addr);
74  }
75  ShadowSegmentEndpoint beg(beg_addr);
76  ShadowSegmentEndpoint end(end_addr);
77  if (beg.chunk == end.chunk) {
78    CHECK(beg.offset < end.offset);
79    s8 value = beg.value;
80    CHECK(value == end.value);
81    // We can only poison memory if the byte in end.offset is unaddressable.
82    // No need to re-poison memory if it is poisoned already.
83    if (value > 0 && value <= end.offset) {
84      if (beg.offset > 0) {
85        *beg.chunk = Min(value, beg.offset);
86      } else {
87        *beg.chunk = kAsanUserPoisonedMemoryMagic;
88      }
89    }
90    return;
91  }
92  CHECK(beg.chunk < end.chunk);
93  if (beg.offset > 0) {
94    // Mark bytes from beg.offset as unaddressable.
95    if (beg.value == 0) {
96      *beg.chunk = beg.offset;
97    } else {
98      *beg.chunk = Min(beg.value, beg.offset);
99    }
100    beg.chunk++;
101  }
102  REAL(memset)(beg.chunk, kAsanUserPoisonedMemoryMagic, end.chunk - beg.chunk);
103  // Poison if byte in end.offset is unaddressable.
104  if (end.value > 0 && end.value <= end.offset) {
105    *end.chunk = kAsanUserPoisonedMemoryMagic;
106  }
107}
108
109void __asan_unpoison_memory_region(void const volatile *addr, uptr size) {
110  if (!flags()->allow_user_poisoning || size == 0) return;
111  uptr beg_addr = (uptr)addr;
112  uptr end_addr = beg_addr + size;
113  if (flags()->verbosity >= 1) {
114    Printf("Trying to unpoison memory region [%p, %p)\n",
115           (void*)beg_addr, (void*)end_addr);
116  }
117  ShadowSegmentEndpoint beg(beg_addr);
118  ShadowSegmentEndpoint end(end_addr);
119  if (beg.chunk == end.chunk) {
120    CHECK(beg.offset < end.offset);
121    s8 value = beg.value;
122    CHECK(value == end.value);
123    // We unpoison memory bytes up to enbytes up to end.offset if it is not
124    // unpoisoned already.
125    if (value != 0) {
126      *beg.chunk = Max(value, end.offset);
127    }
128    return;
129  }
130  CHECK(beg.chunk < end.chunk);
131  if (beg.offset > 0) {
132    *beg.chunk = 0;
133    beg.chunk++;
134  }
135  REAL(memset)(beg.chunk, 0, end.chunk - beg.chunk);
136  if (end.offset > 0 && end.value != 0) {
137    *end.chunk = Max(end.value, end.offset);
138  }
139}
140
141bool __asan_address_is_poisoned(void const volatile *addr) {
142  return __asan::AddressIsPoisoned((uptr)addr);
143}
144
145uptr __asan_region_is_poisoned(uptr beg, uptr size) {
146  if (!size) return 0;
147  uptr end = beg + size;
148  if (!AddrIsInMem(beg)) return beg;
149  if (!AddrIsInMem(end)) return end;
150  uptr aligned_b = RoundUpTo(beg, SHADOW_GRANULARITY);
151  uptr aligned_e = RoundDownTo(end, SHADOW_GRANULARITY);
152  uptr shadow_beg = MemToShadow(aligned_b);
153  uptr shadow_end = MemToShadow(aligned_e);
154  // First check the first and the last application bytes,
155  // then check the SHADOW_GRANULARITY-aligned region by calling
156  // mem_is_zero on the corresponding shadow.
157  if (!__asan::AddressIsPoisoned(beg) &&
158      !__asan::AddressIsPoisoned(end - 1) &&
159      (shadow_end <= shadow_beg ||
160       __sanitizer::mem_is_zero((const char *)shadow_beg,
161                                shadow_end - shadow_beg)))
162    return 0;
163  // The fast check failed, so we have a poisoned byte somewhere.
164  // Find it slowly.
165  for (; beg < end; beg++)
166    if (__asan::AddressIsPoisoned(beg))
167      return beg;
168  UNREACHABLE("mem_is_zero returned false, but poisoned byte was not found");
169  return 0;
170}
171
172// This is a simplified version of __asan_(un)poison_memory_region, which
173// assumes that left border of region to be poisoned is properly aligned.
174static void PoisonAlignedStackMemory(uptr addr, uptr size, bool do_poison) {
175  if (size == 0) return;
176  uptr aligned_size = size & ~(SHADOW_GRANULARITY - 1);
177  PoisonShadow(addr, aligned_size,
178               do_poison ? kAsanStackUseAfterScopeMagic : 0);
179  if (size == aligned_size)
180    return;
181  s8 end_offset = (s8)(size - aligned_size);
182  s8* shadow_end = (s8*)MemToShadow(addr + aligned_size);
183  s8 end_value = *shadow_end;
184  if (do_poison) {
185    // If possible, mark all the bytes mapping to last shadow byte as
186    // unaddressable.
187    if (end_value > 0 && end_value <= end_offset)
188      *shadow_end = (s8)kAsanStackUseAfterScopeMagic;
189  } else {
190    // If necessary, mark few first bytes mapping to last shadow byte
191    // as addressable
192    if (end_value != 0)
193      *shadow_end = Max(end_value, end_offset);
194  }
195}
196
197void __asan_poison_stack_memory(uptr addr, uptr size) {
198  if (flags()->verbosity > 0)
199    Report("poisoning: %p %zx\n", (void*)addr, size);
200  PoisonAlignedStackMemory(addr, size, true);
201}
202
203void __asan_unpoison_stack_memory(uptr addr, uptr size) {
204  if (flags()->verbosity > 0)
205    Report("unpoisoning: %p %zx\n", (void*)addr, size);
206  PoisonAlignedStackMemory(addr, size, false);
207}
208