asan_poisoning.h revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
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
20namespace __asan {
21
22// Poisons the shadow memory for "size" bytes starting from "addr".
23void PoisonShadow(uptr addr, uptr size, u8 value);
24
25// Poisons the shadow memory for "redzone_size" bytes starting from
26// "addr + size".
27void PoisonShadowPartialRightRedzone(uptr addr,
28                                     uptr size,
29                                     uptr redzone_size,
30                                     u8 value);
31
32// Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
33// assume that memory addresses are properly aligned. Use in
34// performance-critical code with care.
35ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
36                                    u8 value) {
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.
46  if (value ||
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);
50  } else {
51    uptr page_beg = RoundUpTo(shadow_beg, PageSize);
52    uptr page_end = RoundDownTo(shadow_end, PageSize);
53
54    if (page_beg >= page_end) {
55      REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
56    } else {
57      if (page_beg != shadow_beg) {
58        REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
59      }
60      if (page_end != shadow_end) {
61        REAL(memset)((void *)page_end, 0, shadow_end - page_end);
62      }
63      void *res = MmapFixedNoReserve(page_beg, page_end - page_beg);
64      CHECK_EQ(page_beg, res);
65    }
66  }
67}
68
69ALWAYS_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
79    } else {
80      // first size-i bytes are addressable
81      *shadow = poison_partial ? static_cast<u8>(size - i) : 0;
82    }
83  }
84}
85
86// Calls __sanitizer::FlushUnneededShadowMemory() on
87// [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
88void FlushUnneededASanShadowMemory(uptr p, uptr size);
89
90}  // namespace __asan
91