1//===------------------------ tysan_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 TypeSanitizer.
10//
11// Platform specific information for TySan.
12//===----------------------------------------------------------------------===//
13
14#ifndef TYSAN_PLATFORM_H
15#define TYSAN_PLATFORM_H
16
17namespace __tysan {
18
19#if defined(__x86_64__) || SANITIZER_APPLE
20struct Mapping {
21 static const uptr kShadowAddr = 0x010000000000ull;
22 static const uptr kAppAddr = 0x550000000000ull;
23 static const uptr kAppMemMsk = ~0x780000000000ull;
24 static const uptr kPtrShift = 3;
25};
26#elif defined(__aarch64__)
27struct Mapping39 {
28 static const uptr kShadowAddr = 0x0800000000ull;
29 static const uptr kAppAddr = 0x5500000000ull;
30 static const uptr kAppMemMsk = ~0x7800000000ull;
31 static const uptr kPtrShift = 3;
32};
33
34struct Mapping42 {
35 static const uptr kShadowAddr = 0x10000000000ull;
36 static const uptr kAppAddr = 0x2aa00000000ull;
37 static const uptr kAppMemMsk = ~0x3c000000000ull;
38 static const uptr kPtrShift = 3;
39};
40
41struct Mapping48 {
42 static const uptr kShadowAddr = 0x0002000000000ull;
43 static const uptr kAppAddr = 0x0aaaa00000000ull;
44 static const uptr kAppMemMsk = ~0x0fff800000000ull;
45 static const uptr kPtrShift = 3;
46};
47#define TYSAN_RUNTIME_VMA 1
48#elif defined(__s390x__)
49struct Mapping {
50 static const uptr kShadowAddr = 0x080000000000ULL;
51 static const uptr kAppAddr = 0x460000000000ULL;
52 static const uptr kAppMemMsk = ~0xC00000000000ULL;
53 static const uptr kPtrShift = 3;
54};
55#elif defined(__hexagon__)
56// Hexagon is a 32-bit architecture. Each shadow entry is a 4-byte pointer
57// (PtrShift=2). The shadow occupies 4x the masked app memory range.
58// With a 28-bit mask (256MB of app addresses), the shadow is 1GB at
59// 0x80000000-0xBFFFFFFF. App addresses that differ only in bits 28-31
60// will alias in the shadow; in practice this means code/heap (low addresses)
61// and stack (~0x40000000) may share shadow entries, which can cause
62// false positives in rare cases.
63struct Mapping {
64 static const uptr kShadowAddr = 0x80000000u;
65 static const uptr kAppAddr = 0xC0000000u;
66 static const uptr kAppMemMsk = ~0xF0000000u;
67 static const uptr kPtrShift = 2;
68};
69#else
70#error "TySan not supported for this platform!"
71#endif
72
73#if TYSAN_RUNTIME_VMA
74extern int vmaSize;
75#endif
76
77enum MappingType {
78 MAPPING_SHADOW_ADDR,
79 MAPPING_APP_ADDR,
80 MAPPING_APP_MASK,
81 MAPPING_PTR_SHIFT
82};
83
84template <typename Mapping, int Type> uptr MappingImpl(void) {
85 switch (Type) {
86 case MAPPING_SHADOW_ADDR:
87 return Mapping::kShadowAddr;
88 case MAPPING_APP_ADDR:
89 return Mapping::kAppAddr;
90 case MAPPING_APP_MASK:
91 return Mapping::kAppMemMsk;
92 case MAPPING_PTR_SHIFT:
93 return Mapping::kPtrShift;
94 }
95}
96
97template <int Type> uptr MappingArchImpl(void) {
98#if defined(__aarch64__) && !SANITIZER_APPLE
99 switch (vmaSize) {
100 case 39:
101 return MappingImpl<Mapping39, Type>();
102 case 42:
103 return MappingImpl<Mapping42, Type>();
104 case 48:
105 return MappingImpl<Mapping48, Type>();
106 }
107 DCHECK(0);
108 return 0;
109#else
110 return MappingImpl<Mapping, Type>();
111#endif
112}
113
114ALWAYS_INLINE
115uptr ShadowAddr() { return MappingArchImpl<MAPPING_SHADOW_ADDR>(); }
116
117ALWAYS_INLINE
118uptr AppAddr() { return MappingArchImpl<MAPPING_APP_ADDR>(); }
119
120ALWAYS_INLINE
121uptr AppMask() { return MappingArchImpl<MAPPING_APP_MASK>(); }
122
123ALWAYS_INLINE
124uptr PtrShift() { return MappingArchImpl<MAPPING_PTR_SHIFT>(); }
125
126} // namespace __tysan
127
128#endif
129