Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_poisoning.h
35233 views
//===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//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//===----------------------------------------------------------------------===//1213#include "asan_interceptors.h"14#include "asan_internal.h"15#include "asan_mapping.h"16#include "sanitizer_common/sanitizer_flags.h"17#include "sanitizer_common/sanitizer_platform.h"1819namespace __asan {2021// Enable/disable memory poisoning.22void SetCanPoisonMemory(bool value);23bool CanPoisonMemory();2425// Poisons the shadow memory for "size" bytes starting from "addr".26void PoisonShadow(uptr addr, uptr size, u8 value);2728// Poisons the shadow memory for "redzone_size" bytes starting from29// "addr + size".30void PoisonShadowPartialRightRedzone(uptr addr,31uptr size,32uptr redzone_size,33u8 value);3435// Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that36// assume that memory addresses are properly aligned. Use in37// performance-critical code with care.38ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,39u8 value) {40DCHECK(!value || CanPoisonMemory());41#if SANITIZER_FUCHSIA42__sanitizer_fill_shadow(aligned_beg, aligned_size, value,43common_flags()->clear_shadow_mmap_threshold);44#else45uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);46uptr shadow_end =47MEM_TO_SHADOW(aligned_beg + aligned_size - ASAN_SHADOW_GRANULARITY) + 1;48// FIXME: Page states are different on Windows, so using the same interface49// for mapping shadow and zeroing out pages doesn't "just work", so we should50// probably provide higher-level interface for these operations.51// For now, just memset on Windows.52if (value || SANITIZER_WINDOWS == 1 ||53shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {54REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);55} else {56uptr page_size = GetPageSizeCached();57uptr page_beg = RoundUpTo(shadow_beg, page_size);58uptr page_end = RoundDownTo(shadow_end, page_size);5960if (page_beg >= page_end) {61REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);62} else {63if (page_beg != shadow_beg) {64REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);65}66if (page_end != shadow_end) {67REAL(memset)((void *)page_end, 0, shadow_end - page_end);68}69ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);70}71}72#endif // SANITIZER_FUCHSIA73}7475ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(76uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {77DCHECK(CanPoisonMemory());78bool poison_partial = flags()->poison_partial;79u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);80for (uptr i = 0; i < redzone_size; i += ASAN_SHADOW_GRANULARITY, shadow++) {81if (i + ASAN_SHADOW_GRANULARITY <= size) {82*shadow = 0; // fully addressable83} else if (i >= size) {84*shadow =85(ASAN_SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable86} else {87// first size-i bytes are addressable88*shadow = poison_partial ? static_cast<u8>(size - i) : 0;89}90}91}9293// Calls __sanitizer::ReleaseMemoryPagesToOS() on94// [MemToShadow(p), MemToShadow(p+size)].95void FlushUnneededASanShadowMemory(uptr p, uptr size);9697} // namespace __asan9899100