]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_poisoning.h
Update libucl to git version 8d3b186
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_poisoning.h
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 // Poisons the shadow memory for "size" bytes starting from "addr".
23 void PoisonShadow(uptr addr, uptr size, u8 value);
24
25 // Poisons the shadow memory for "redzone_size" bytes starting from
26 // "addr + size".
27 void 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.
35 ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
36                                     u8 value) {
37   DCHECK(flags()->poison_heap);
38   uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
39   uptr shadow_end = MEM_TO_SHADOW(
40       aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
41   // FIXME: Page states are different on Windows, so using the same interface
42   // for mapping shadow and zeroing out pages doesn't "just work", so we should
43   // probably provide higher-level interface for these operations.
44   // For now, just memset on Windows.
45   if (value ||
46       SANITIZER_WINDOWS == 1 ||
47       shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
48     REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
49   } else {
50     uptr page_size = GetPageSizeCached();
51     uptr page_beg = RoundUpTo(shadow_beg, page_size);
52     uptr page_end = RoundDownTo(shadow_end, page_size);
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
69 ALWAYS_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.
88 void FlushUnneededASanShadowMemory(uptr p, uptr size);
89
90 }  // namespace __asan