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#else
56#error "TySan not supported for this platform!"
57#endif
58
59#if TYSAN_RUNTIME_VMA
60extern int vmaSize;
61#endif
62
63enum MappingType {
64 MAPPING_SHADOW_ADDR,
65 MAPPING_APP_ADDR,
66 MAPPING_APP_MASK,
67 MAPPING_PTR_SHIFT
68};
69
70template <typename Mapping, int Type> uptr MappingImpl(void) {
71 switch (Type) {
72 case MAPPING_SHADOW_ADDR:
73 return Mapping::kShadowAddr;
74 case MAPPING_APP_ADDR:
75 return Mapping::kAppAddr;
76 case MAPPING_APP_MASK:
77 return Mapping::kAppMemMsk;
78 case MAPPING_PTR_SHIFT:
79 return Mapping::kPtrShift;
80 }
81}
82
83template <int Type> uptr MappingArchImpl(void) {
84#if defined(__aarch64__) && !SANITIZER_APPLE
85 switch (vmaSize) {
86 case 39:
87 return MappingImpl<Mapping39, Type>();
88 case 42:
89 return MappingImpl<Mapping42, Type>();
90 case 48:
91 return MappingImpl<Mapping48, Type>();
92 }
93 DCHECK(0);
94 return 0;
95#else
96 return MappingImpl<Mapping, Type>();
97#endif
98}
99
100ALWAYS_INLINE
101uptr ShadowAddr() { return MappingArchImpl<MAPPING_SHADOW_ADDR>(); }
102
103ALWAYS_INLINE
104uptr AppAddr() { return MappingArchImpl<MAPPING_APP_ADDR>(); }
105
106ALWAYS_INLINE
107uptr AppMask() { return MappingArchImpl<MAPPING_APP_MASK>(); }
108
109ALWAYS_INLINE
110uptr PtrShift() { return MappingArchImpl<MAPPING_PTR_SHIFT>(); }
111
112} // namespace __tysan
113
114#endif
115