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(!value || 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 // RTEMS doesn't have have pages, let alone a fast way to zero 53 // them, so default to memset. 54 SANITIZER_RTEMS == 1 || 55 shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) { 56 REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg); 57 } else { 58 uptr page_size = GetPageSizeCached(); 59 uptr page_beg = RoundUpTo(shadow_beg, page_size); 60 uptr page_end = RoundDownTo(shadow_end, page_size); 61 62 if (page_beg >= page_end) { 63 REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg); 64 } else { 65 if (page_beg != shadow_beg) { 66 REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg); 67 } 68 if (page_end != shadow_end) { 69 REAL(memset)((void *)page_end, 0, shadow_end - page_end); 70 } 71 ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr); 72 } 73 } 74 } 75 76 ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone( 77 uptr aligned_addr, uptr size, uptr redzone_size, u8 value) { 78 DCHECK(CanPoisonMemory()); 79 bool poison_partial = flags()->poison_partial; 80 u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr); 81 for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) { 82 if (i + SHADOW_GRANULARITY <= size) { 83 *shadow = 0; // fully addressable 84 } else if (i >= size) { 85 *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable 86 } else { 87 // first size-i bytes are addressable 88 *shadow = poison_partial ? static_cast<u8>(size - i) : 0; 89 } 90 } 91 } 92 93 // Calls __sanitizer::ReleaseMemoryPagesToOS() on 94 // [MemToShadow(p), MemToShadow(p+size)]. 95 void FlushUnneededASanShadowMemory(uptr p, uptr size); 96 97 } // namespace __asan 98