1 //===-- asan_poisoning.cc -------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Shadow memory poisoning by ASan RTL and by user application.
11 //===----------------------------------------------------------------------===//
13 #include "asan_interceptors.h"
14 #include "asan_internal.h"
15 #include "asan_mapping.h"
16 #include "sanitizer/asan_interface.h"
17 #include "sanitizer_common/sanitizer_libc.h"
21 void PoisonShadow(uptr addr
, uptr size
, u8 value
) {
22 if (!flags()->poison_heap
) return;
23 CHECK(AddrIsAlignedByGranularity(addr
));
24 CHECK(AddrIsAlignedByGranularity(addr
+ size
));
25 uptr shadow_beg
= MemToShadow(addr
);
26 uptr shadow_end
= MemToShadow(addr
+ size
- SHADOW_GRANULARITY
) + 1;
27 CHECK(REAL(memset
) != 0);
28 REAL(memset
)((void*)shadow_beg
, value
, shadow_end
- shadow_beg
);
31 void PoisonShadowPartialRightRedzone(uptr addr
,
35 if (!flags()->poison_heap
) return;
36 CHECK(AddrIsAlignedByGranularity(addr
));
37 u8
*shadow
= (u8
*)MemToShadow(addr
);
38 for (uptr i
= 0; i
< redzone_size
;
39 i
+= SHADOW_GRANULARITY
, shadow
++) {
40 if (i
+ SHADOW_GRANULARITY
<= size
) {
41 *shadow
= 0; // fully addressable
42 } else if (i
>= size
) {
43 *shadow
= (SHADOW_GRANULARITY
== 128) ? 0xff : value
; // unaddressable
45 *shadow
= size
- i
; // first size-i bytes are addressable
51 struct ShadowSegmentEndpoint
{
53 s8 offset
; // in [0, SHADOW_GRANULARITY)
54 s8 value
; // = *chunk;
56 explicit ShadowSegmentEndpoint(uptr address
) {
57 chunk
= (u8
*)MemToShadow(address
);
58 offset
= address
& (SHADOW_GRANULARITY
- 1);
65 // ---------------------- Interface ---------------- {{{1
66 using namespace __asan
; // NOLINT
68 // Current implementation of __asan_(un)poison_memory_region doesn't check
69 // that user program (un)poisons the memory it owns. It poisons memory
70 // conservatively, and unpoisons progressively to make sure asan shadow
71 // mapping invariant is preserved (see detailed mapping description here:
72 // http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm).
74 // * if user asks to poison region [left, right), the program poisons
75 // at least [left, AlignDown(right)).
76 // * if user asks to unpoison region [left, right), the program unpoisons
77 // at most [AlignDown(left), right).
78 void __asan_poison_memory_region(void const volatile *addr
, uptr size
) {
79 if (!flags()->allow_user_poisoning
|| size
== 0) return;
80 uptr beg_addr
= (uptr
)addr
;
81 uptr end_addr
= beg_addr
+ size
;
82 if (flags()->verbosity
>= 1) {
83 Printf("Trying to poison memory region [%p, %p)\n",
84 (void*)beg_addr
, (void*)end_addr
);
86 ShadowSegmentEndpoint
beg(beg_addr
);
87 ShadowSegmentEndpoint
end(end_addr
);
88 if (beg
.chunk
== end
.chunk
) {
89 CHECK(beg
.offset
< end
.offset
);
91 CHECK(value
== end
.value
);
92 // We can only poison memory if the byte in end.offset is unaddressable.
93 // No need to re-poison memory if it is poisoned already.
94 if (value
> 0 && value
<= end
.offset
) {
96 *beg
.chunk
= Min(value
, beg
.offset
);
98 *beg
.chunk
= kAsanUserPoisonedMemoryMagic
;
103 CHECK(beg
.chunk
< end
.chunk
);
104 if (beg
.offset
> 0) {
105 // Mark bytes from beg.offset as unaddressable.
106 if (beg
.value
== 0) {
107 *beg
.chunk
= beg
.offset
;
109 *beg
.chunk
= Min(beg
.value
, beg
.offset
);
113 REAL(memset
)(beg
.chunk
, kAsanUserPoisonedMemoryMagic
, end
.chunk
- beg
.chunk
);
114 // Poison if byte in end.offset is unaddressable.
115 if (end
.value
> 0 && end
.value
<= end
.offset
) {
116 *end
.chunk
= kAsanUserPoisonedMemoryMagic
;
120 void __asan_unpoison_memory_region(void const volatile *addr
, uptr size
) {
121 if (!flags()->allow_user_poisoning
|| size
== 0) return;
122 uptr beg_addr
= (uptr
)addr
;
123 uptr end_addr
= beg_addr
+ size
;
124 if (flags()->verbosity
>= 1) {
125 Printf("Trying to unpoison memory region [%p, %p)\n",
126 (void*)beg_addr
, (void*)end_addr
);
128 ShadowSegmentEndpoint
beg(beg_addr
);
129 ShadowSegmentEndpoint
end(end_addr
);
130 if (beg
.chunk
== end
.chunk
) {
131 CHECK(beg
.offset
< end
.offset
);
132 s8 value
= beg
.value
;
133 CHECK(value
== end
.value
);
134 // We unpoison memory bytes up to enbytes up to end.offset if it is not
135 // unpoisoned already.
137 *beg
.chunk
= Max(value
, end
.offset
);
141 CHECK(beg
.chunk
< end
.chunk
);
142 if (beg
.offset
> 0) {
146 REAL(memset
)(beg
.chunk
, 0, end
.chunk
- beg
.chunk
);
147 if (end
.offset
> 0 && end
.value
!= 0) {
148 *end
.chunk
= Max(end
.value
, end
.offset
);
152 bool __asan_address_is_poisoned(void const volatile *addr
) {
153 return __asan::AddressIsPoisoned((uptr
)addr
);
156 uptr
__asan_region_is_poisoned(uptr beg
, uptr size
) {
158 uptr end
= beg
+ size
;
159 if (!AddrIsInMem(beg
)) return beg
;
160 if (!AddrIsInMem(end
)) return end
;
161 uptr aligned_b
= RoundUpTo(beg
, SHADOW_GRANULARITY
);
162 uptr aligned_e
= RoundDownTo(end
, SHADOW_GRANULARITY
);
163 uptr shadow_beg
= MemToShadow(aligned_b
);
164 uptr shadow_end
= MemToShadow(aligned_e
);
165 // First check the first and the last application bytes,
166 // then check the SHADOW_GRANULARITY-aligned region by calling
167 // mem_is_zero on the corresponding shadow.
168 if (!__asan::AddressIsPoisoned(beg
) &&
169 !__asan::AddressIsPoisoned(end
- 1) &&
170 (shadow_end
<= shadow_beg
||
171 __sanitizer::mem_is_zero((const char *)shadow_beg
,
172 shadow_end
- shadow_beg
)))
174 // The fast check failed, so we have a poisoned byte somewhere.
176 for (; beg
< end
; beg
++)
177 if (__asan::AddressIsPoisoned(beg
))
179 UNREACHABLE("mem_is_zero returned false, but poisoned byte was not found");
183 // This is a simplified version of __asan_(un)poison_memory_region, which
184 // assumes that left border of region to be poisoned is properly aligned.
185 static void PoisonAlignedStackMemory(uptr addr
, uptr size
, bool do_poison
) {
186 if (size
== 0) return;
187 uptr aligned_size
= size
& ~(SHADOW_GRANULARITY
- 1);
188 PoisonShadow(addr
, aligned_size
,
189 do_poison
? kAsanStackUseAfterScopeMagic
: 0);
190 if (size
== aligned_size
)
192 s8 end_offset
= (s8
)(size
- aligned_size
);
193 s8
* shadow_end
= (s8
*)MemToShadow(addr
+ aligned_size
);
194 s8 end_value
= *shadow_end
;
196 // If possible, mark all the bytes mapping to last shadow byte as
198 if (end_value
> 0 && end_value
<= end_offset
)
199 *shadow_end
= (s8
)kAsanStackUseAfterScopeMagic
;
201 // If necessary, mark few first bytes mapping to last shadow byte
204 *shadow_end
= Max(end_value
, end_offset
);
208 void __asan_poison_stack_memory(uptr addr
, uptr size
) {
209 if (flags()->verbosity
> 0)
210 Report("poisoning: %p %zx\n", (void*)addr
, size
);
211 PoisonAlignedStackMemory(addr
, size
, true);
214 void __asan_unpoison_stack_memory(uptr addr
, uptr size
) {
215 if (flags()->verbosity
> 0)
216 Report("unpoisoning: %p %zx\n", (void*)addr
, size
);
217 PoisonAlignedStackMemory(addr
, size
, false);