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
42inline uptr MemToShadow(uptr untagged_addr) {
43  return (untagged_addr >> kShadowScale) +
44         __hwasan_shadow_memory_dynamic_address;
45}
46inline uptr ShadowToMem(uptr shadow_addr) {
47  return (shadow_addr - __hwasan_shadow_memory_dynamic_address) << kShadowScale;
48}
49inline uptr MemToShadowSize(uptr size) {
50  return size >> kShadowScale;
51}
52
53bool MemIsApp(uptr p);
54
55}  // namespace __hwasan
56
57#endif  // HWASAN_MAPPING_H
58