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 18 namespace __asan { 19 20 // Enable/disable memory poisoning. 21 void SetCanPoisonMemory(bool value); 22 bool CanPoisonMemory(); 23 24 // Poisons the shadow memory for "size" bytes starting from "addr". 25 void PoisonShadow(uptr addr, uptr size, u8 value); 26 27 // Poisons the shadow memory for "redzone_size" bytes starting from 28 // "addr + size". 29 void 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. 37 ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size, 38 u8 value) { 39 DCHECK(CanPoisonMemory()); 40 uptr shadow_beg = MEM_TO_SHADOW(aligned_beg); 41 uptr shadow_end = MEM_TO_SHADOW( 42 aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1; 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 || SANITIZER_WINDOWS == 1 || 48 // TODO(mcgrathr): Fuchsia doesn't allow the shadow mapping to be 49 // changed at all. It doesn't currently have an efficient means 50 // to zero a bunch of pages, but maybe we should add one. 51 SANITIZER_FUCHSIA == 1 || 52 shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) { 53 REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg); 54 } else { 55 uptr page_size = GetPageSizeCached(); 56 uptr page_beg = RoundUpTo(shadow_beg, page_size); 57 uptr page_end = RoundDownTo(shadow_end, page_size); 58 59 if (page_beg >= page_end) { 60 REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg); 61 } else { 62 if (page_beg != shadow_beg) { 63 REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg); 64 } 65 if (page_end != shadow_end) { 66 REAL(memset)((void *)page_end, 0, shadow_end - page_end); 67 } 68 ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr); 69 } 70 } 71 } 72 73 ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone( 74 uptr aligned_addr, uptr size, uptr redzone_size, u8 value) { 75 DCHECK(CanPoisonMemory()); 76 bool poison_partial = flags()->poison_partial; 77 u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr); 78 for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) { 79 if (i + SHADOW_GRANULARITY <= size) { 80 *shadow = 0; // fully addressable 81 } else if (i >= size) { 82 *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable 83 } else { 84 // first size-i bytes are addressable 85 *shadow = poison_partial ? static_cast<u8>(size - i) : 0; 86 } 87 } 88 } 89 90 // Calls __sanitizer::ReleaseMemoryPagesToOS() on 91 // [MemToShadow(p), MemToShadow(p+size)]. 92 void FlushUnneededASanShadowMemory(uptr p, uptr size); 93 94 } // namespace __asan 95