xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_platform.h (revision 3e8eb5c7f4909209c042403ddee340b2ee7003a5)
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 // All of the following configurations are supported.
37 // ASLR disabled: main executable and DSOs at 0x555550000000
38 // PIE and ASLR: main executable and DSOs at 0x7f0000000000
39 // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
40 // Heap at 0x700000000000.
41 const MappingDesc kMemoryLayout[] = {
42     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
43     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
44     {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
45     {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
46     {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
47     {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
48     {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
49     {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
50     {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
51     {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
52     {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
53     {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
54 #  define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
55 #  define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
56 
57 #else
58 #  error "Unsupported platform"
59 #endif
60 
61 const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
62 
63 #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
64 
65 #ifndef __clang__
66 __attribute__((optimize("unroll-loops")))
67 #endif
68 inline bool
69 addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
70 // It is critical for performance that this loop is unrolled (because then it is
71 // simplified into just a few constant comparisons).
72 #ifdef __clang__
73 #  pragma unroll
74 #endif
75   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
76     if (kMemoryLayout[i].type == mapping_type &&
77         addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
78       return true;
79   return false;
80 }
81 
82 #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
83 #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
84 #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
85 
86 }  // namespace __dfsan
87 
88 #endif
89