10b57cec5SDimitry Andric //===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Shadow memory poisoning by ASan RTL and by user application.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "asan_interceptors.h"
150b57cec5SDimitry Andric #include "asan_internal.h"
160b57cec5SDimitry Andric #include "asan_mapping.h"
170b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_flags.h"
180b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_platform.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric namespace __asan {
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric // Enable/disable memory poisoning.
230b57cec5SDimitry Andric void SetCanPoisonMemory(bool value);
240b57cec5SDimitry Andric bool CanPoisonMemory();
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric // Poisons the shadow memory for "size" bytes starting from "addr".
270b57cec5SDimitry Andric void PoisonShadow(uptr addr, uptr size, u8 value);
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric // Poisons the shadow memory for "redzone_size" bytes starting from
300b57cec5SDimitry Andric // "addr + size".
310b57cec5SDimitry Andric void PoisonShadowPartialRightRedzone(uptr addr,
320b57cec5SDimitry Andric uptr size,
330b57cec5SDimitry Andric uptr redzone_size,
340b57cec5SDimitry Andric u8 value);
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric // Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
370b57cec5SDimitry Andric // assume that memory addresses are properly aligned. Use in
380b57cec5SDimitry Andric // performance-critical code with care.
FastPoisonShadow(uptr aligned_beg,uptr aligned_size,u8 value)390b57cec5SDimitry Andric ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
400b57cec5SDimitry Andric u8 value) {
410b57cec5SDimitry Andric DCHECK(!value || CanPoisonMemory());
420b57cec5SDimitry Andric #if SANITIZER_FUCHSIA
430b57cec5SDimitry Andric __sanitizer_fill_shadow(aligned_beg, aligned_size, value,
440b57cec5SDimitry Andric common_flags()->clear_shadow_mmap_threshold);
450b57cec5SDimitry Andric #else
460b57cec5SDimitry Andric uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
47*0eae32dcSDimitry Andric uptr shadow_end =
48*0eae32dcSDimitry Andric MEM_TO_SHADOW(aligned_beg + aligned_size - ASAN_SHADOW_GRANULARITY) + 1;
490b57cec5SDimitry Andric // FIXME: Page states are different on Windows, so using the same interface
500b57cec5SDimitry Andric // for mapping shadow and zeroing out pages doesn't "just work", so we should
510b57cec5SDimitry Andric // probably provide higher-level interface for these operations.
520b57cec5SDimitry Andric // For now, just memset on Windows.
530b57cec5SDimitry Andric if (value || SANITIZER_WINDOWS == 1 ||
540b57cec5SDimitry Andric shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
550b57cec5SDimitry Andric REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
560b57cec5SDimitry Andric } else {
570b57cec5SDimitry Andric uptr page_size = GetPageSizeCached();
580b57cec5SDimitry Andric uptr page_beg = RoundUpTo(shadow_beg, page_size);
590b57cec5SDimitry Andric uptr page_end = RoundDownTo(shadow_end, page_size);
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric if (page_beg >= page_end) {
620b57cec5SDimitry Andric REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
630b57cec5SDimitry Andric } else {
640b57cec5SDimitry Andric if (page_beg != shadow_beg) {
650b57cec5SDimitry Andric REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric if (page_end != shadow_end) {
680b57cec5SDimitry Andric REAL(memset)((void *)page_end, 0, shadow_end - page_end);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric #endif // SANITIZER_FUCHSIA
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric
FastPoisonShadowPartialRightRedzone(uptr aligned_addr,uptr size,uptr redzone_size,u8 value)760b57cec5SDimitry Andric ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(
770b57cec5SDimitry Andric uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {
780b57cec5SDimitry Andric DCHECK(CanPoisonMemory());
790b57cec5SDimitry Andric bool poison_partial = flags()->poison_partial;
800b57cec5SDimitry Andric u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);
81*0eae32dcSDimitry Andric for (uptr i = 0; i < redzone_size; i += ASAN_SHADOW_GRANULARITY, shadow++) {
82*0eae32dcSDimitry Andric if (i + ASAN_SHADOW_GRANULARITY <= size) {
830b57cec5SDimitry Andric *shadow = 0; // fully addressable
840b57cec5SDimitry Andric } else if (i >= size) {
85*0eae32dcSDimitry Andric *shadow =
86*0eae32dcSDimitry Andric (ASAN_SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable
870b57cec5SDimitry Andric } else {
880b57cec5SDimitry Andric // first size-i bytes are addressable
890b57cec5SDimitry Andric *shadow = poison_partial ? static_cast<u8>(size - i) : 0;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric // Calls __sanitizer::ReleaseMemoryPagesToOS() on
950b57cec5SDimitry Andric // [MemToShadow(p), MemToShadow(p+size)].
960b57cec5SDimitry Andric void FlushUnneededASanShadowMemory(uptr p, uptr size);
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric } // namespace __asan
99