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"
20 void PoisonShadow(uptr addr
, uptr size
, u8 value
) {
21 CHECK(AddrIsAlignedByGranularity(addr
));
22 CHECK(AddrIsAlignedByGranularity(addr
+ size
));
23 uptr shadow_beg
= MemToShadow(addr
);
24 uptr shadow_end
= MemToShadow(addr
+ size
);
25 CHECK(REAL(memset
) != 0);
26 REAL(memset
)((void*)shadow_beg
, value
, shadow_end
- shadow_beg
);
29 void PoisonShadowPartialRightRedzone(uptr addr
,
33 CHECK(AddrIsAlignedByGranularity(addr
));
34 u8
*shadow
= (u8
*)MemToShadow(addr
);
35 for (uptr i
= 0; i
< redzone_size
;
36 i
+= SHADOW_GRANULARITY
, shadow
++) {
37 if (i
+ SHADOW_GRANULARITY
<= size
) {
38 *shadow
= 0; // fully addressable
39 } else if (i
>= size
) {
40 *shadow
= (SHADOW_GRANULARITY
== 128) ? 0xff : value
; // unaddressable
42 *shadow
= size
- i
; // first size-i bytes are addressable
48 struct ShadowSegmentEndpoint
{
50 s8 offset
; // in [0, SHADOW_GRANULARITY)
51 s8 value
; // = *chunk;
53 explicit ShadowSegmentEndpoint(uptr address
) {
54 chunk
= (u8
*)MemToShadow(address
);
55 offset
= address
& (SHADOW_GRANULARITY
- 1);
62 // ---------------------- Interface ---------------- {{{1
63 using namespace __asan
; // NOLINT
65 // Current implementation of __asan_(un)poison_memory_region doesn't check
66 // that user program (un)poisons the memory it owns. It poisons memory
67 // conservatively, and unpoisons progressively to make sure asan shadow
68 // mapping invariant is preserved (see detailed mapping description here:
69 // http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm).
71 // * if user asks to poison region [left, right), the program poisons
72 // at least [left, AlignDown(right)).
73 // * if user asks to unpoison region [left, right), the program unpoisons
74 // at most [AlignDown(left), right).
75 void __asan_poison_memory_region(void const volatile *addr
, uptr size
) {
76 if (!flags()->allow_user_poisoning
|| size
== 0) return;
77 uptr beg_addr
= (uptr
)addr
;
78 uptr end_addr
= beg_addr
+ size
;
79 if (flags()->verbosity
>= 1) {
80 Printf("Trying to poison memory region [%p, %p)\n",
81 (void*)beg_addr
, (void*)end_addr
);
83 ShadowSegmentEndpoint
beg(beg_addr
);
84 ShadowSegmentEndpoint
end(end_addr
);
85 if (beg
.chunk
== end
.chunk
) {
86 CHECK(beg
.offset
< end
.offset
);
88 CHECK(value
== end
.value
);
89 // We can only poison memory if the byte in end.offset is unaddressable.
90 // No need to re-poison memory if it is poisoned already.
91 if (value
> 0 && value
<= end
.offset
) {
93 *beg
.chunk
= Min(value
, beg
.offset
);
95 *beg
.chunk
= kAsanUserPoisonedMemoryMagic
;
100 CHECK(beg
.chunk
< end
.chunk
);
101 if (beg
.offset
> 0) {
102 // Mark bytes from beg.offset as unaddressable.
103 if (beg
.value
== 0) {
104 *beg
.chunk
= beg
.offset
;
106 *beg
.chunk
= Min(beg
.value
, beg
.offset
);
110 REAL(memset
)(beg
.chunk
, kAsanUserPoisonedMemoryMagic
, end
.chunk
- beg
.chunk
);
111 // Poison if byte in end.offset is unaddressable.
112 if (end
.value
> 0 && end
.value
<= end
.offset
) {
113 *end
.chunk
= kAsanUserPoisonedMemoryMagic
;
117 void __asan_unpoison_memory_region(void const volatile *addr
, uptr size
) {
118 if (!flags()->allow_user_poisoning
|| size
== 0) return;
119 uptr beg_addr
= (uptr
)addr
;
120 uptr end_addr
= beg_addr
+ size
;
121 if (flags()->verbosity
>= 1) {
122 Printf("Trying to unpoison memory region [%p, %p)\n",
123 (void*)beg_addr
, (void*)end_addr
);
125 ShadowSegmentEndpoint
beg(beg_addr
);
126 ShadowSegmentEndpoint
end(end_addr
);
127 if (beg
.chunk
== end
.chunk
) {
128 CHECK(beg
.offset
< end
.offset
);
129 s8 value
= beg
.value
;
130 CHECK(value
== end
.value
);
131 // We unpoison memory bytes up to enbytes up to end.offset if it is not
132 // unpoisoned already.
134 *beg
.chunk
= Max(value
, end
.offset
);
138 CHECK(beg
.chunk
< end
.chunk
);
139 if (beg
.offset
> 0) {
143 REAL(memset
)(beg
.chunk
, 0, end
.chunk
- beg
.chunk
);
144 if (end
.offset
> 0 && end
.value
!= 0) {
145 *end
.chunk
= Max(end
.value
, end
.offset
);
149 bool __asan_address_is_poisoned(void const volatile *addr
) {
150 return __asan::AddressIsPoisoned((uptr
)addr
);