Rebase.
[official-gcc.git] / libsanitizer / asan / asan_poisoning.h
blob326d9ba1b67be3477ecc547b2100d8fa1385d2c5
1 //===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
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"
18 namespace __asan {
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
24 // "addr + size".
25 void PoisonShadowPartialRightRedzone(uptr addr,
26 uptr size,
27 uptr redzone_size,
28 u8 value);
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,
34 u8 value) {
35 DCHECK(flags()->poison_heap);
36 uptr PageSize = GetPageSizeCached();
37 uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
38 uptr shadow_end = MEM_TO_SHADOW(
39 aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
40 // FIXME: Page states are different on Windows, so using the same interface
41 // for mapping shadow and zeroing out pages doesn't "just work", so we should
42 // probably provide higher-level interface for these operations.
43 // For now, just memset on Windows.
44 if (value ||
45 SANITIZER_WINDOWS == 1 ||
46 shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
47 REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
48 } else {
49 uptr page_beg = RoundUpTo(shadow_beg, PageSize);
50 uptr page_end = RoundDownTo(shadow_end, PageSize);
52 if (page_beg >= page_end) {
53 REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
54 } else {
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
77 } else {
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);
88 } // namespace __asan