1 //===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
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_common/sanitizer_flags.h"
20 // Poisons the shadow memory for "size" bytes starting from "addr".
21 void PoisonShadow(uptr addr
, uptr size
, u8 value
);
23 // Poisons the shadow memory for "redzone_size" bytes starting from
25 void PoisonShadowPartialRightRedzone(uptr addr
,
30 // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
31 // assume that memory addresses are properly aligned. Use in
32 // performance-critical code with care.
33 ALWAYS_INLINE
void FastPoisonShadow(uptr aligned_beg
, uptr aligned_size
,
35 DCHECK(flags()->poison_heap
);
36 uptr shadow_beg
= MEM_TO_SHADOW(aligned_beg
);
37 uptr shadow_end
= MEM_TO_SHADOW(
38 aligned_beg
+ aligned_size
- SHADOW_GRANULARITY
) + 1;
39 // FIXME: Page states are different on Windows, so using the same interface
40 // for mapping shadow and zeroing out pages doesn't "just work", so we should
41 // probably provide higher-level interface for these operations.
42 // For now, just memset on Windows.
44 SANITIZER_WINDOWS
== 1 ||
45 shadow_end
- shadow_beg
< common_flags()->clear_shadow_mmap_threshold
) {
46 REAL(memset
)((void*)shadow_beg
, value
, shadow_end
- shadow_beg
);
48 uptr page_size
= GetPageSizeCached();
49 uptr page_beg
= RoundUpTo(shadow_beg
, page_size
);
50 uptr page_end
= RoundDownTo(shadow_end
, page_size
);
52 if (page_beg
>= page_end
) {
53 REAL(memset
)((void *)shadow_beg
, 0, shadow_end
- shadow_beg
);
55 if (page_beg
!= shadow_beg
) {
56 REAL(memset
)((void *)shadow_beg
, 0, page_beg
- shadow_beg
);
58 if (page_end
!= shadow_end
) {
59 REAL(memset
)((void *)page_end
, 0, shadow_end
- page_end
);
61 void *res
= MmapFixedNoReserve(page_beg
, page_end
- page_beg
);
62 CHECK_EQ(page_beg
, res
);
67 ALWAYS_INLINE
void FastPoisonShadowPartialRightRedzone(
68 uptr aligned_addr
, uptr size
, uptr redzone_size
, u8 value
) {
69 DCHECK(flags()->poison_heap
);
70 bool poison_partial
= flags()->poison_partial
;
71 u8
*shadow
= (u8
*)MEM_TO_SHADOW(aligned_addr
);
72 for (uptr i
= 0; i
< redzone_size
; i
+= SHADOW_GRANULARITY
, shadow
++) {
73 if (i
+ SHADOW_GRANULARITY
<= size
) {
74 *shadow
= 0; // fully addressable
75 } else if (i
>= size
) {
76 *shadow
= (SHADOW_GRANULARITY
== 128) ? 0xff : value
; // unaddressable
78 // first size-i bytes are addressable
79 *shadow
= poison_partial
? static_cast<u8
>(size
- i
) : 0;
84 // Calls __sanitizer::FlushUnneededShadowMemory() on
85 // [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
86 void FlushUnneededASanShadowMemory(uptr p
, uptr size
);