1 //===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Shadow memory poisoning by ASan RTL and by user application.
13 //===----------------------------------------------------------------------===//
15 #include "asan_interceptors.h"
16 #include "asan_internal.h"
17 #include "asan_mapping.h"
18 #include "sanitizer_common/sanitizer_flags.h"
22 // Poisons the shadow memory for "size" bytes starting from "addr".
23 void PoisonShadow(uptr addr
, uptr size
, u8 value
);
25 // Poisons the shadow memory for "redzone_size" bytes starting from
27 void PoisonShadowPartialRightRedzone(uptr addr
,
32 // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
33 // assume that memory addresses are properly aligned. Use in
34 // performance-critical code with care.
35 ALWAYS_INLINE
void FastPoisonShadow(uptr aligned_beg
, uptr aligned_size
,
37 DCHECK(flags()->poison_heap
);
38 uptr PageSize
= GetPageSizeCached();
39 uptr shadow_beg
= MEM_TO_SHADOW(aligned_beg
);
40 uptr shadow_end
= MEM_TO_SHADOW(
41 aligned_beg
+ aligned_size
- SHADOW_GRANULARITY
) + 1;
42 // FIXME: Page states are different on Windows, so using the same interface
43 // for mapping shadow and zeroing out pages doesn't "just work", so we should
44 // probably provide higher-level interface for these operations.
45 // For now, just memset on Windows.
47 SANITIZER_WINDOWS
== 1 ||
48 shadow_end
- shadow_beg
< common_flags()->clear_shadow_mmap_threshold
) {
49 REAL(memset
)((void*)shadow_beg
, value
, shadow_end
- shadow_beg
);
51 uptr page_beg
= RoundUpTo(shadow_beg
, PageSize
);
52 uptr page_end
= RoundDownTo(shadow_end
, PageSize
);
54 if (page_beg
>= page_end
) {
55 REAL(memset
)((void *)shadow_beg
, 0, shadow_end
- shadow_beg
);
57 if (page_beg
!= shadow_beg
) {
58 REAL(memset
)((void *)shadow_beg
, 0, page_beg
- shadow_beg
);
60 if (page_end
!= shadow_end
) {
61 REAL(memset
)((void *)page_end
, 0, shadow_end
- page_end
);
63 void *res
= MmapFixedNoReserve(page_beg
, page_end
- page_beg
);
64 CHECK_EQ(page_beg
, res
);
69 ALWAYS_INLINE
void FastPoisonShadowPartialRightRedzone(
70 uptr aligned_addr
, uptr size
, uptr redzone_size
, u8 value
) {
71 DCHECK(flags()->poison_heap
);
72 bool poison_partial
= flags()->poison_partial
;
73 u8
*shadow
= (u8
*)MEM_TO_SHADOW(aligned_addr
);
74 for (uptr i
= 0; i
< redzone_size
; i
+= SHADOW_GRANULARITY
, shadow
++) {
75 if (i
+ SHADOW_GRANULARITY
<= size
) {
76 *shadow
= 0; // fully addressable
77 } else if (i
>= size
) {
78 *shadow
= (SHADOW_GRANULARITY
== 128) ? 0xff : value
; // unaddressable
80 // first size-i bytes are addressable
81 *shadow
= poison_partial
? static_cast<u8
>(size
- i
) : 0;
86 // Calls __sanitizer::FlushUnneededShadowMemory() on
87 // [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
88 void FlushUnneededASanShadowMemory(uptr p
, uptr size
);