xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan_platform.h (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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 namespace __dfsan {
18 
19 #if defined(__x86_64__)
20 struct Mapping {
21   static const uptr kShadowAddr = 0x10000;
22   static const uptr kUnionTableAddr = 0x200000000000;
23   static const uptr kAppAddr = 0x700000008000;
24   static const uptr kShadowMask = ~0x700000000000;
25 };
26 #elif defined(__mips64)
27 struct Mapping {
28   static const uptr kShadowAddr = 0x10000;
29   static const uptr kUnionTableAddr = 0x2000000000;
30   static const uptr kAppAddr = 0xF000008000;
31   static const uptr kShadowMask = ~0xF000000000;
32 };
33 #elif defined(__aarch64__)
34 struct Mapping39 {
35   static const uptr kShadowAddr = 0x10000;
36   static const uptr kUnionTableAddr = 0x1000000000;
37   static const uptr kAppAddr = 0x7000008000;
38   static const uptr kShadowMask = ~0x7800000000;
39 };
40 
41 struct Mapping42 {
42   static const uptr kShadowAddr = 0x10000;
43   static const uptr kUnionTableAddr = 0x8000000000;
44   static const uptr kAppAddr = 0x3ff00008000;
45   static const uptr kShadowMask = ~0x3c000000000;
46 };
47 
48 struct Mapping48 {
49   static const uptr kShadowAddr = 0x10000;
50   static const uptr kUnionTableAddr = 0x8000000000;
51   static const uptr kAppAddr = 0xffff00008000;
52   static const uptr kShadowMask = ~0xfffff0000000;
53 };
54 
55 extern int vmaSize;
56 # define DFSAN_RUNTIME_VMA 1
57 #else
58 # error "DFSan not supported for this platform!"
59 #endif
60 
61 enum MappingType {
62   MAPPING_SHADOW_ADDR,
63   MAPPING_UNION_TABLE_ADDR,
64   MAPPING_APP_ADDR,
65   MAPPING_SHADOW_MASK
66 };
67 
68 template<typename Mapping, int Type>
69 uptr MappingImpl(void) {
70   switch (Type) {
71     case MAPPING_SHADOW_ADDR: return Mapping::kShadowAddr;
72     case MAPPING_UNION_TABLE_ADDR: return Mapping::kUnionTableAddr;
73     case MAPPING_APP_ADDR: return Mapping::kAppAddr;
74     case MAPPING_SHADOW_MASK: return Mapping::kShadowMask;
75   }
76 }
77 
78 template<int Type>
79 uptr MappingArchImpl(void) {
80 #ifdef __aarch64__
81   switch (vmaSize) {
82     case 39: return MappingImpl<Mapping39, Type>();
83     case 42: return MappingImpl<Mapping42, Type>();
84     case 48: return MappingImpl<Mapping48, Type>();
85   }
86   DCHECK(0);
87   return 0;
88 #else
89   return MappingImpl<Mapping, Type>();
90 #endif
91 }
92 
93 ALWAYS_INLINE
94 uptr ShadowAddr() {
95   return MappingArchImpl<MAPPING_SHADOW_ADDR>();
96 }
97 
98 ALWAYS_INLINE
99 uptr UnionTableAddr() {
100   return MappingArchImpl<MAPPING_UNION_TABLE_ADDR>();
101 }
102 
103 ALWAYS_INLINE
104 uptr AppAddr() {
105   return MappingArchImpl<MAPPING_APP_ADDR>();
106 }
107 
108 ALWAYS_INLINE
109 uptr ShadowMask() {
110   return MappingArchImpl<MAPPING_SHADOW_MASK>();
111 }
112 
113 }  // namespace __dfsan
114 
115 #endif
116