forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsan_shadow.cpp
209 lines (182 loc) · 7.21 KB
/
tsan_shadow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
*
* Copyright (C) 2025 Intel Corporation
*
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
* Exceptions. See LICENSE.TXT
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
* @file tsan_shadow.cpp
*
*/
#include "tsan_shadow.hpp"
#include "sanitizer_common/sanitizer_utils.hpp"
namespace ur_sanitizer_layer {
namespace tsan {
std::shared_ptr<ShadowMemory> GetShadowMemory(ur_context_handle_t Context,
ur_device_handle_t Device,
DeviceType Type) {
// For CPU device, we only allocate once. But for GPU device, each device will
// have its own one.
if (Type == DeviceType::CPU) {
static std::shared_ptr<ShadowMemory> ShadowCPU =
std::make_shared<ShadowMemoryCPU>(Context, Device);
return ShadowCPU;
} else if (Type == DeviceType::GPU_PVC) {
return std::make_shared<ShadowMemoryPVC>(Context, Device);
} else {
getContext()->logger.error("Unsupport device type");
return nullptr;
}
}
ur_result_t ShadowMemoryCPU::Setup() {
static ur_result_t URes = [this]() {
ShadowBegin = 0x100000000000ULL;
ShadowEnd = 0x300000000000ULL;
if (MmapFixedNoReserve(ShadowBegin, ShadowEnd - ShadowBegin) == 0)
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
DontCoredumpRange(ShadowBegin, ShadowEnd - ShadowBegin);
return UR_RESULT_SUCCESS;
}();
return URes;
}
ur_result_t ShadowMemoryCPU::Destory() {
if (ShadowBegin == 0 && ShadowEnd == 0)
return UR_RESULT_SUCCESS;
static ur_result_t URes = [this]() {
if (!Munmap(ShadowBegin, ShadowEnd - ShadowBegin))
return UR_RESULT_ERROR_UNKNOWN;
ShadowBegin = ShadowEnd = 0;
return UR_RESULT_SUCCESS;
}();
return URes;
}
RawShadow *ShadowMemoryCPU::MemToShadow(uptr Addr) {
return reinterpret_cast<RawShadow *>(
((Addr) & ~(0x700000000000ULL | (kShadowCell - 1))) * kShadowMultiplier +
ShadowBegin);
}
ur_result_t ShadowMemoryCPU::CleanShadow(ur_queue_handle_t, uptr Ptr,
uptr Size) {
if (Size) {
Ptr = RoundDownTo(Ptr, kShadowCell);
Size = RoundUpTo(Size, kShadowCell);
RawShadow *Begin = MemToShadow(Ptr);
getContext()->logger.debug("CleanShadow(addr={}, count={})", (void *)Begin,
Size / kShadowCell);
memset((void *)Begin, 0, Size / kShadowCell * kShadowCnt * kShadowSize);
}
return UR_RESULT_SUCCESS;
}
ur_result_t ShadowMemoryGPU::Setup() {
if (ShadowBegin != 0)
return UR_RESULT_SUCCESS;
const size_t ShadowSize = GetShadowSize();
// To reserve very large amount of GPU virtual memroy, the pStart param
// should be beyond the SVM range, so that GFX driver will automatically
// switch to reservation on the GPU heap.
const void *StartAddress = (void *)GetStartAddress();
// TODO: Protect Bad Zone
auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve(
Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
if (Result != UR_RESULT_SUCCESS) {
getContext()->logger.error("Shadow memory reserved failed with size {}: {}",
(void *)ShadowSize, Result);
return Result;
}
ShadowEnd = ShadowBegin + ShadowSize;
// Retain the context which reserves shadow memory
getContext()->urDdiTable.Context.pfnRetain(Context);
return UR_RESULT_SUCCESS;
}
ur_result_t ShadowMemoryGPU::Destory() {
if (ShadowBegin == 0) {
return UR_RESULT_SUCCESS;
}
const size_t PageSize = GetVirtualMemGranularity(Context, Device);
for (auto [MappedPtr, PhysicalMem] : VirtualMemMaps) {
UR_CALL(getContext()->urDdiTable.VirtualMem.pfnUnmap(
Context, (void *)MappedPtr, PageSize));
UR_CALL(getContext()->urDdiTable.PhysicalMem.pfnRelease(PhysicalMem));
}
UR_CALL(getContext()->urDdiTable.VirtualMem.pfnFree(
Context, (const void *)ShadowBegin, GetShadowSize()));
UR_CALL(getContext()->urDdiTable.Context.pfnRelease(Context));
ShadowEnd = ShadowBegin = 0;
return UR_RESULT_SUCCESS;
}
ur_result_t ShadowMemoryGPU::CleanShadow(ur_queue_handle_t Queue, uptr Ptr,
uptr Size) {
if (Size == 0) {
return UR_RESULT_SUCCESS;
}
Ptr = RoundDownTo(Ptr, kShadowCell);
Size = RoundUpTo(Size, kShadowCell);
uptr Begin = (uptr)MemToShadow(Ptr);
uptr End = Begin + Size / kShadowCell * kShadowCnt * kShadowSize - 1;
{
static const size_t PageSize = GetVirtualMemGranularity(Context, Device);
ur_physical_mem_properties_t Desc{nullptr,
UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES, 0};
// Make sure [Ptr, Ptr + Size] is mapped to physical memory
for (auto MappedPtr = RoundDownTo((uptr)Begin, PageSize); MappedPtr <= End;
MappedPtr += PageSize) {
std::scoped_lock<ur_mutex> Guard(VirtualMemMapsMutex);
if (VirtualMemMaps.find(MappedPtr) == VirtualMemMaps.end()) {
ur_physical_mem_handle_t PhysicalMem{};
auto URes = getContext()->urDdiTable.PhysicalMem.pfnCreate(
Context, Device, PageSize, &Desc, &PhysicalMem);
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error("urPhysicalMemCreate(): {}", URes);
return URes;
}
URes = getContext()->urDdiTable.VirtualMem.pfnMap(
Context, (void *)MappedPtr, PageSize, PhysicalMem, 0,
UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE);
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error("urVirtualMemMap({}, {}): {}",
(void *)MappedPtr, PageSize, URes);
return URes;
}
getContext()->logger.debug("urVirtualMemMap: {} ~ {}",
(void *)MappedPtr,
(void *)(MappedPtr + PageSize - 1));
// Initialize to zero
URes = EnqueueUSMBlockingSet(Queue, (void *)MappedPtr, 0, PageSize);
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error("EnqueueUSMBlockingSet(): {}", URes);
return URes;
}
VirtualMemMaps[MappedPtr] = PhysicalMem;
}
}
}
auto URes = EnqueueUSMBlockingSet(
Queue, (void *)Begin, 0, Size / kShadowCell * kShadowCnt * kShadowSize);
if (URes != UR_RESULT_SUCCESS) {
getContext()->logger.error("EnqueueUSMBlockingSet(): {}", URes);
return URes;
}
getContext()->logger.debug("CleanShadow(addr={}, count={})", (void *)Begin,
Size / kShadowCell);
return UR_RESULT_SUCCESS;
}
RawShadow *ShadowMemoryPVC::MemToShadow(uptr Ptr) {
Ptr = RoundDownTo(Ptr, kShadowCell);
if (Ptr & 0xff00'0000'0000'0000ULL) {
// device usm
return Ptr < ShadowBegin
? reinterpret_cast<RawShadow *>(Ptr + (ShadowBegin +
0x200'0000'0000ULL -
0xff00'0000'0000'0000ULL))
: reinterpret_cast<RawShadow *>(
Ptr - (0xff00'ffff'ffff'ffffULL - ShadowEnd + 1));
} else {
// host & shared usm
return reinterpret_cast<RawShadow *>((Ptr & 0xffffffffffULL) + ShadowBegin +
((Ptr & 0x800000000000ULL) >> 7));
}
}
} // namespace tsan
} // namespace ur_sanitizer_layer