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 
17 namespace __asan {
18 
19 // Poisons the shadow memory for "size" bytes starting from "addr".
20 void PoisonShadow(uptr addr, uptr size, u8 value);
21 
22 // Poisons the shadow memory for "redzone_size" bytes starting from
23 // "addr + size".
24 void PoisonShadowPartialRightRedzone(uptr addr,
25                                      uptr size,
26                                      uptr redzone_size,
27                                      u8 value);
28 
29 // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
30 // assume that memory addresses are properly aligned. Use in
31 // performance-critical code with care.
FastPoisonShadow(uptr aligned_beg,uptr aligned_size,u8 value)32 ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
33                                     u8 value) {
34   DCHECK(flags()->poison_heap);
35   uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
36   uptr shadow_end = MEM_TO_SHADOW(
37       aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
38   REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
39 }
40 
FastPoisonShadowPartialRightRedzone(uptr aligned_addr,uptr size,uptr redzone_size,u8 value)41 ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(
42     uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {
43   DCHECK(flags()->poison_heap);
44   bool poison_partial = flags()->poison_partial;
45   u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);
46   for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) {
47     if (i + SHADOW_GRANULARITY <= size) {
48       *shadow = 0;  // fully addressable
49     } else if (i >= size) {
50       *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value;  // unaddressable
51     } else {
52       // first size-i bytes are addressable
53       *shadow = poison_partial ? static_cast<u8>(size - i) : 0;
54     }
55   }
56 }
57 
58 }  // namespace __asan
59