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 // Enable/disable memory poisoning.
21 void SetCanPoisonMemory(bool value
);
22 bool CanPoisonMemory();
24 // Poisons the shadow memory for "size" bytes starting from "addr".
25 void PoisonShadow(uptr addr
, uptr size
, u8 value
);
27 // Poisons the shadow memory for "redzone_size" bytes starting from
29 void PoisonShadowPartialRightRedzone(uptr addr
,
34 // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
35 // assume that memory addresses are properly aligned. Use in
36 // performance-critical code with care.
37 ALWAYS_INLINE
void FastPoisonShadow(uptr aligned_beg
, uptr aligned_size
,
39 DCHECK(CanPoisonMemory());
40 uptr shadow_beg
= MEM_TO_SHADOW(aligned_beg
);
41 uptr shadow_end
= MEM_TO_SHADOW(
42 aligned_beg
+ aligned_size
- SHADOW_GRANULARITY
) + 1;
43 // FIXME: Page states are different on Windows, so using the same interface
44 // for mapping shadow and zeroing out pages doesn't "just work", so we should
45 // probably provide higher-level interface for these operations.
46 // For now, just memset on Windows.
48 SANITIZER_WINDOWS
== 1 ||
49 shadow_end
- shadow_beg
< common_flags()->clear_shadow_mmap_threshold
) {
50 REAL(memset
)((void*)shadow_beg
, value
, shadow_end
- shadow_beg
);
52 uptr page_size
= GetPageSizeCached();
53 uptr page_beg
= RoundUpTo(shadow_beg
, page_size
);
54 uptr page_end
= RoundDownTo(shadow_end
, page_size
);
56 if (page_beg
>= page_end
) {
57 REAL(memset
)((void *)shadow_beg
, 0, shadow_end
- shadow_beg
);
59 if (page_beg
!= shadow_beg
) {
60 REAL(memset
)((void *)shadow_beg
, 0, page_beg
- shadow_beg
);
62 if (page_end
!= shadow_end
) {
63 REAL(memset
)((void *)page_end
, 0, shadow_end
- page_end
);
65 ReserveShadowMemoryRange(page_beg
, page_end
- 1, nullptr);
70 ALWAYS_INLINE
void FastPoisonShadowPartialRightRedzone(
71 uptr aligned_addr
, uptr size
, uptr redzone_size
, u8 value
) {
72 DCHECK(CanPoisonMemory());
73 bool poison_partial
= flags()->poison_partial
;
74 u8
*shadow
= (u8
*)MEM_TO_SHADOW(aligned_addr
);
75 for (uptr i
= 0; i
< redzone_size
; i
+= SHADOW_GRANULARITY
, shadow
++) {
76 if (i
+ SHADOW_GRANULARITY
<= size
) {
77 *shadow
= 0; // fully addressable
78 } else if (i
>= size
) {
79 *shadow
= (SHADOW_GRANULARITY
== 128) ? 0xff : value
; // unaddressable
81 // first size-i bytes are addressable
82 *shadow
= poison_partial
? static_cast<u8
>(size
- i
) : 0;
87 // Calls __sanitizer::FlushUnneededShadowMemory() on
88 // [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
89 void FlushUnneededASanShadowMemory(uptr p
, uptr size
);