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//===----------------------------------------------------------------------===//
12
13#include "asan_interceptors.h"
14#include "asan_internal.h"
15#include "asan_mapping.h"
16#include "sanitizer_common/sanitizer_flags.h"
17
18namespace __asan {
19
20// Poisons the shadow memory for "size" bytes starting from "addr".
21void PoisonShadow(uptr addr, uptr size, u8 value);
22
23// Poisons the shadow memory for "redzone_size" bytes starting from
24// "addr + size".
25void PoisonShadowPartialRightRedzone(uptr addr,
26                                     uptr size,
27                                     uptr redzone_size,
28                                     u8 value);
29
30// Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
31// assume that memory addresses are properly aligned. Use in
32// performance-critical code with care.
33ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
34                                    u8 value) {
35  DCHECK(flags()->poison_heap);
36  uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
37  uptr shadow_end = MEM_TO_SHADOW(
38      aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
39  // FIXME: Page states are different on Windows, so using the same interface
40  // for mapping shadow and zeroing out pages doesn't "just work", so we should
41  // probably provide higher-level interface for these operations.
42  // For now, just memset on Windows.
43  if (value ||
44      SANITIZER_WINDOWS == 1 ||
45      shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
46    REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
47  } else {
48    uptr page_size = GetPageSizeCached();
49    uptr page_beg = RoundUpTo(shadow_beg, page_size);
50    uptr page_end = RoundDownTo(shadow_end, page_size);
51
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);
57      }
58      if (page_end != shadow_end) {
59        REAL(memset)((void *)page_end, 0, shadow_end - page_end);
60      }
61      void *res = MmapFixedNoReserve(page_beg, page_end - page_beg);
62      CHECK_EQ(page_beg, res);
63    }
64  }
65}
66
67ALWAYS_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;
80    }
81  }
82}
83
84// Calls __sanitizer::FlushUnneededShadowMemory() on
85// [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
86void FlushUnneededASanShadowMemory(uptr p, uptr size);
87
88}  // namespace __asan
89