]> git.ipfire.org Git - thirdparty/gcc.git/blame - libsanitizer/asan/asan_poisoning.h
All source files: Merge from upstream 285547.
[thirdparty/gcc.git] / libsanitizer / asan / asan_poisoning.h
CommitLineData
ef1b3fda
KS
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"
dee5ea7a 16#include "sanitizer_common/sanitizer_flags.h"
ef1b3fda
KS
17
18namespace __asan {
19
696d846a
MO
20// Enable/disable memory poisoning.
21void SetCanPoisonMemory(bool value);
22bool CanPoisonMemory();
23
ef1b3fda
KS
24// Poisons the shadow memory for "size" bytes starting from "addr".
25void PoisonShadow(uptr addr, uptr size, u8 value);
26
27// Poisons the shadow memory for "redzone_size" bytes starting from
28// "addr + size".
29void PoisonShadowPartialRightRedzone(uptr addr,
30 uptr size,
31 uptr redzone_size,
32 u8 value);
33
34// Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
35// assume that memory addresses are properly aligned. Use in
36// performance-critical code with care.
37ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
38 u8 value) {
696d846a 39 DCHECK(CanPoisonMemory());
ef1b3fda
KS
40 uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
41 uptr shadow_end = MEM_TO_SHADOW(
42 aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
dee5ea7a
KS
43 // FIXME: Page states are different on Windows, so using the same interface
44 // for mapping shadow and zeroing out pages doesn't "just work", so we should
45 // probably provide higher-level interface for these operations.
46 // For now, just memset on Windows.
47 if (value ||
48 SANITIZER_WINDOWS == 1 ||
49 shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
50 REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
51 } else {
866e32ad
KS
52 uptr page_size = GetPageSizeCached();
53 uptr page_beg = RoundUpTo(shadow_beg, page_size);
54 uptr page_end = RoundDownTo(shadow_end, page_size);
dee5ea7a
KS
55
56 if (page_beg >= page_end) {
57 REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
58 } else {
59 if (page_beg != shadow_beg) {
60 REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
61 }
62 if (page_end != shadow_end) {
63 REAL(memset)((void *)page_end, 0, shadow_end - page_end);
64 }
696d846a 65 ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);
dee5ea7a
KS
66 }
67 }
ef1b3fda
KS
68}
69
70ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(
71 uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {
696d846a 72 DCHECK(CanPoisonMemory());
df77f0e4 73 bool poison_partial = flags()->poison_partial;
ef1b3fda
KS
74 u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);
75 for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) {
76 if (i + SHADOW_GRANULARITY <= size) {
77 *shadow = 0; // fully addressable
78 } else if (i >= size) {
79 *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable
80 } else {
81 // first size-i bytes are addressable
df77f0e4 82 *shadow = poison_partial ? static_cast<u8>(size - i) : 0;
ef1b3fda
KS
83 }
84 }
85}
86
10189819 87// Calls __sanitizer::ReleaseMemoryToOS() on
dee5ea7a
KS
88// [MemToShadow(p), MemToShadow(p+size)] with proper rounding.
89void FlushUnneededASanShadowMemory(uptr p, uptr size);
90
ef1b3fda 91} // namespace __asan