xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_platform.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 //===-- dfsan_platform.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 // This file is a part of DataFlowSanitizer.
10 //
11 // Platform specific information for DFSan.
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef DFSAN_PLATFORM_H
15 #define DFSAN_PLATFORM_H
16 
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_platform.h"
19 
20 namespace __dfsan {
21 
22 using __sanitizer::uptr;
23 
24 // TODO: The memory mapping code to setup a 1:1 shadow is based on msan.
25 // Consider refactoring these into a shared implementation.
26 
27 struct MappingDesc {
28   uptr start;
29   uptr end;
30   enum Type { INVALID, APP, SHADOW, ORIGIN } type;
31   const char *name;
32 };
33 
34 #if SANITIZER_LINUX && SANITIZER_WORDSIZE == 64
35 
36 #  if defined(__aarch64__)
37 // The mapping assumes 48-bit VMA. AArch64 maps:
38 // - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments
39 // - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments
40 //   Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the
41 //   maximum ASLR region for 48-bit VMA) but it is too hard to fit in
42 //   the larger app/shadow/origin regions.
43 // - 0x0e00000000000-0x1000000000000: 48-bits libraries segments
44 const MappingDesc kMemoryLayout[] = {
45     {0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},
46     {0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},
47     {0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},
48     {0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},
49     {0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},
50     {0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},
51     {0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},
52     {0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},
53     {0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},
54     {0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},
55     {0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},
56     {0X0E00000000000, 0X1000000000000, MappingDesc::APP, "app-15"},
57 };
58 #    define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)
59 #    define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)
60 
61 #  else
62 // All of the following configurations are supported.
63 // ASLR disabled: main executable and DSOs at 0x555550000000
64 // PIE and ASLR: main executable and DSOs at 0x7f0000000000
65 // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
66 // Heap at 0x700000000000.
67 const MappingDesc kMemoryLayout[] = {
68     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
69     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
70     {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
71     {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
72     {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
73     {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
74     {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
75     {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
76     {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
77     {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
78     {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
79     {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
80 #    define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
81 #    define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
82 #  endif
83 
84 #else
85 #  error "Unsupported platform"
86 #endif
87 
88 const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
89 
90 #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
91 
92 #ifndef __clang__
93 __attribute__((optimize("unroll-loops")))
94 #endif
95 inline bool
96 addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
97 // It is critical for performance that this loop is unrolled (because then it is
98 // simplified into just a few constant comparisons).
99 #ifdef __clang__
100 #  pragma unroll
101 #endif
102   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
103     if (kMemoryLayout[i].type == mapping_type &&
104         addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
105       return true;
106   return false;
107 }
108 
109 #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
110 #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
111 #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
112 
113 }  // namespace __dfsan
114 
115 #endif
116