• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Shadow memory poisoning by ASan RTL and by user application.
13 //===----------------------------------------------------------------------===//
14 
15 #include "asan_interceptors.h"
16 #include "asan_internal.h"
17 #include "asan_mapping.h"
18 #include "sanitizer_common/sanitizer_flags.h"
19 
20 namespace __asan {
21 
22 // Enable/disable memory poisoning.
23 void SetCanPoisonMemory(bool value);
24 bool CanPoisonMemory();
25 
26 // Poisons the shadow memory for "size" bytes starting from "addr".
27 void PoisonShadow(uptr addr, uptr size, u8 value);
28 
29 // Poisons the shadow memory for "redzone_size" bytes starting from
30 // "addr + size".
31 void PoisonShadowPartialRightRedzone(uptr addr,
32                                      uptr size,
33                                      uptr redzone_size,
34                                      u8 value);
35 
36 // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
37 // assume that memory addresses are properly aligned. Use in
38 // performance-critical code with care.
FastPoisonShadow(uptr aligned_beg,uptr aligned_size,u8 value)39 ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
40                                     u8 value) {
41   DCHECK(CanPoisonMemory());
42   uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
43   uptr shadow_end = MEM_TO_SHADOW(
44       aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
45   // FIXME: Page states are different on Windows, so using the same interface
46   // for mapping shadow and zeroing out pages doesn't "just work", so we should
47   // probably provide higher-level interface for these operations.
48   // For now, just memset on Windows.
49   if (value ||
50       SANITIZER_WINDOWS == 1 ||
51       shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
52     REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
53   } else {
54     uptr page_size = GetPageSizeCached();
55     uptr page_beg = RoundUpTo(shadow_beg, page_size);
56     uptr page_end = RoundDownTo(shadow_end, page_size);
57 
58     if (page_beg >= page_end) {
59       REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
60     } else {
61       if (page_beg != shadow_beg) {
62         REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
63       }
64       if (page_end != shadow_end) {
65         REAL(memset)((void *)page_end, 0, shadow_end - page_end);
66       }
67       ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);
68     }
69   }
70 }
71 
FastPoisonShadowPartialRightRedzone(uptr aligned_addr,uptr size,uptr redzone_size,u8 value)72 ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(
73     uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {
74   DCHECK(CanPoisonMemory());
75   bool poison_partial = flags()->poison_partial;
76   u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);
77   for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) {
78     if (i + SHADOW_GRANULARITY <= size) {
79       *shadow = 0;  // fully addressable
80     } else if (i >= size) {
81       *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value;  // unaddressable
82     } else {
83       // first size-i bytes are addressable
84       *shadow = poison_partial ? static_cast<u8>(size - i) : 0;
85     }
86   }
87 }
88 
89 // Calls __sanitizer::FlushUnneededShadowMemory() on
90 // [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
91 void FlushUnneededASanShadowMemory(uptr p, uptr size);
92 
93 }  // namespace __asan
94