blob: 91275465d7029acd00e113fe4ef88761927cfbc7 [file] [log] [blame]
Alexey Samsonov7e843492013-03-28 15:42:43 +00001//===-- asan_poisoning.h ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Shadow memory poisoning by ASan RTL and by user application.
13//===----------------------------------------------------------------------===//
14
15#include "asan_interceptors.h"
16#include "asan_internal.h"
17#include "asan_mapping.h"
18
19namespace __asan {
20
21// Poisons the shadow memory for "size" bytes starting from "addr".
22void PoisonShadow(uptr addr, uptr size, u8 value);
23
24// Poisons the shadow memory for "redzone_size" bytes starting from
25// "addr + size".
26void PoisonShadowPartialRightRedzone(uptr addr,
27 uptr size,
28 uptr redzone_size,
29 u8 value);
30
31// Fast versions of PoisonShadow and PoisonShadowPartialRightRedzone that
32// assume that memory addresses are properly aligned. Use in
33// performance-critical code with care.
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +000034ALWAYS_INLINE void FastPoisonShadow(uptr aligned_beg, uptr aligned_size,
Alexey Samsonov7e843492013-03-28 15:42:43 +000035 u8 value) {
36 DCHECK(flags()->poison_heap);
37 uptr shadow_beg = MEM_TO_SHADOW(aligned_beg);
38 uptr shadow_end = MEM_TO_SHADOW(
39 aligned_beg + aligned_size - SHADOW_GRANULARITY) + 1;
40 REAL(memset)((void*)shadow_beg, value, shadow_end - shadow_beg);
41}
42
Timur Iskhodzhanovabfdbdf2013-03-28 18:52:40 +000043ALWAYS_INLINE void FastPoisonShadowPartialRightRedzone(
Alexey Samsonov7e843492013-03-28 15:42:43 +000044 uptr aligned_addr, uptr size, uptr redzone_size, u8 value) {
45 DCHECK(flags()->poison_heap);
46 u8 *shadow = (u8*)MEM_TO_SHADOW(aligned_addr);
47 for (uptr i = 0; i < redzone_size; i += SHADOW_GRANULARITY, shadow++) {
48 if (i + SHADOW_GRANULARITY <= size) {
49 *shadow = 0; // fully addressable
50 } else if (i >= size) {
51 *shadow = (SHADOW_GRANULARITY == 128) ? 0xff : value; // unaddressable
52 } else {
Timur Iskhodzhanov5e97ba32013-05-29 14:11:44 +000053 *shadow = static_cast<u8>(size - i); // first size-i bytes are addressable
Alexey Samsonov7e843492013-03-28 15:42:43 +000054 }
55 }
56}
57
58} // namespace __asan