1//===-- hwasan_mapping.h ----------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file is a part of HWAddressSanitizer and defines memory mapping.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef HWASAN_MAPPING_H
15#define HWASAN_MAPPING_H
16
17#include "sanitizer_common/sanitizer_internal_defs.h"
18#include "hwasan_interface_internal.h"
19
20// Typical mapping on Linux/x86_64:
21// with dynamic shadow mapped at [0x770d59f40000, 0x7f0d59f40000]:
22// || [0x7f0d59f40000, 0x7fffffffffff] || HighMem ||
23// || [0x7efe2f934000, 0x7f0d59f3ffff] || HighShadow ||
24// || [0x7e7e2f934000, 0x7efe2f933fff] || ShadowGap ||
25// || [0x770d59f40000, 0x7e7e2f933fff] || LowShadow ||
26// || [0x000000000000, 0x770d59f3ffff] || LowMem ||
27
28// Typical mapping on Android/AArch64
29// with dynamic shadow mapped: [0x007477480000, 0x007c77480000]:
30// || [0x007c77480000, 0x007fffffffff] || HighMem ||
31// || [0x007c3ebc8000, 0x007c7747ffff] || HighShadow ||
32// || [0x007bbebc8000, 0x007c3ebc7fff] || ShadowGap ||
33// || [0x007477480000, 0x007bbebc7fff] || LowShadow ||
34// || [0x000000000000, 0x00747747ffff] || LowMem ||
35
36// Reasonable values are 4 (for 1/16th shadow) and 6 (for 1/64th).
37constexpr uptr kShadowScale = 4;
38constexpr uptr kShadowAlignment = 1ULL << kShadowScale;
39
40namespace __hwasan {
41
42extern uptr kLowMemStart;
43extern uptr kLowMemEnd;
44extern uptr kLowShadowEnd;
45extern uptr kLowShadowStart;
46extern uptr kHighShadowStart;
47extern uptr kHighShadowEnd;
48extern uptr kHighMemStart;
49extern uptr kHighMemEnd;
50
51inline uptr GetShadowOffset() {
52 return SANITIZER_FUCHSIA ? 0 : __hwasan_shadow_memory_dynamic_address;
53}
54inline uptr MemToShadow(uptr untagged_addr) {
55 return (untagged_addr >> kShadowScale) + GetShadowOffset();
56}
57inline uptr ShadowToMem(uptr shadow_addr) {
58 return (shadow_addr - GetShadowOffset()) << kShadowScale;
59}
60inline uptr MemToShadowSize(uptr size) {
61 return size >> kShadowScale;
62}
63
64bool MemIsApp(uptr p);
65
66inline bool MemIsShadow(uptr p) {
67 return (kLowShadowStart <= p && p <= kLowShadowEnd) ||
68 (kHighShadowStart <= p && p <= kHighShadowEnd);
69}
70
71uptr GetAliasRegionStart();
72
73} // namespace __hwasan
74
75#endif // HWASAN_MAPPING_H
76