20
20
namespace ur_sanitizer_layer {
21
21
namespace asan {
22
22
23
- std::shared_ptr<ShadowMemory> CreateShadowMemory (ur_context_handle_t Context,
24
- ur_device_handle_t Device,
23
+ std::shared_ptr<ShadowMemory> CreateShadowMemory (ur_device_handle_t Device,
25
24
DeviceType Type) {
26
- if (Type == DeviceType::CPU ) {
27
- return std::make_shared<ShadowMemoryCPU>(Context, Device);
28
- } else if (Type == DeviceType::GPU_PVC) {
29
- return std::make_shared<ShadowMemoryPVC>(Context, Device);
30
- } else if (Type == DeviceType::GPU_DG2) {
31
- return std::make_shared<ShadowMemoryDG2>(Context, Device);
32
- } else {
33
- getContext ()-> logger . error ( " Unsupport device type " );
34
- return nullptr ;
25
+ switch (Type) {
26
+ case DeviceType::CPU:
27
+ return std::make_shared<ShadowMemoryCPU>(Device);
28
+ case DeviceType::GPU_PVC:
29
+ return std::make_shared<ShadowMemoryPVC>(Device);
30
+ case DeviceType::GPU_DG2:
31
+ return std::make_shared<ShadowMemoryDG2>(Device);
32
+ default :
33
+ die ( " CreateShadowMemory: Unsupport device type " ) ;
35
34
}
36
35
}
37
36
38
37
ur_result_t ShadowMemoryCPU::Setup () {
39
- static ur_result_t Result = [this ]() {
40
- size_t ShadowSize = GetShadowSize ();
41
- ShadowBegin = MmapNoReserve (0 , ShadowSize);
42
- if (ShadowBegin == 0 ) {
43
- return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
44
- }
45
- DontCoredumpRange (ShadowBegin, ShadowSize);
46
- ShadowEnd = ShadowBegin + ShadowSize;
47
-
48
- // Set shadow memory for null pointer
49
- // For CPU, we use a typical page size of 4K bytes.
50
- constexpr size_t NullptrRedzoneSize = 4096 ;
51
- auto URes = EnqueuePoisonShadow ({}, 0 , NullptrRedzoneSize,
52
- kNullPointerRedzoneMagic );
53
- if (URes != UR_RESULT_SUCCESS) {
54
- getContext ()->logger .error (" EnqueuePoisonShadow(NullPointerRZ): {}" ,
55
- URes);
56
- return URes;
57
- }
38
+ size_t ShadowSize = GetShadowSize ();
39
+ ShadowBegin = MmapNoReserve (0 , ShadowSize);
40
+ if (ShadowBegin == 0 ) {
41
+ return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
42
+ }
43
+ DontCoredumpRange (ShadowBegin, ShadowSize);
44
+ ShadowEnd = ShadowBegin + ShadowSize;
45
+
46
+ // Set shadow memory for null pointer
47
+ // For CPU, we use a typical page size of 4K bytes.
48
+ constexpr size_t NullptrRedzoneSize = 4096 ;
49
+ auto URes =
50
+ EnqueuePoisonShadow ({}, 0 , NullptrRedzoneSize, kNullPointerRedzoneMagic );
51
+ if (URes != UR_RESULT_SUCCESS) {
52
+ getContext ()->logger .error (" EnqueuePoisonShadow(NullPointerRZ): {}" , URes);
58
53
return URes;
59
- }();
60
- return Result ;
54
+ }
55
+ return URes ;
61
56
}
62
57
63
58
ur_result_t ShadowMemoryCPU::Destory () {
@@ -99,39 +94,33 @@ ur_result_t ShadowMemoryGPU::Setup() {
99
94
// we reserve shadow memory for each contexts, this will cause out-of-resource
100
95
// error when user uses multiple contexts. Therefore, we just create one
101
96
// shadow memory here.
102
- static ur_result_t Result = [this ]() {
103
- const size_t ShadowSize = GetShadowSize ();
104
- // To reserve very large amount of GPU virtual memroy, the pStart param
105
- // should be beyond the SVM range, so that GFX driver will automatically
106
- // switch to reservation on the GPU heap.
107
- const void *StartAddress = (void *)(0x100'0000'0000'0000ULL );
108
- // TODO: Protect Bad Zone
109
- auto Result = getContext ()->urDdiTable .VirtualMem .pfnReserve (
110
- Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
111
- if (Result != UR_RESULT_SUCCESS) {
112
- getContext ()->logger .error (
113
- " Shadow memory reserved failed with size {}: {}" , (void *)ShadowSize,
114
- Result);
115
- return Result;
116
- }
117
- ShadowEnd = ShadowBegin + ShadowSize;
118
- // Retain the context which reserves shadow memory
119
- getContext ()->urDdiTable .Context .pfnRetain (Context);
120
-
121
- // Set shadow memory for null pointer
122
- // For GPU, wu use up to 1 page of shadow memory
123
- const size_t NullptrRedzoneSize = GetVirtualMemGranularity (Context, Device)
124
- << ASAN_SHADOW_SCALE;
125
- ManagedQueue Queue (Context, Device);
126
- Result = EnqueuePoisonShadow (Queue, 0 , NullptrRedzoneSize,
127
- kNullPointerRedzoneMagic );
128
- if (Result != UR_RESULT_SUCCESS) {
129
- getContext ()->logger .error (" EnqueuePoisonShadow(NullPointerRZ): {}" ,
130
- Result);
131
- return Result;
132
- }
97
+ const size_t ShadowSize = GetShadowSize ();
98
+ // To reserve very large amount of GPU virtual memroy, the pStart param
99
+ // should be beyond the SVM range, so that GFX driver will automatically
100
+ // switch to reservation on the GPU heap.
101
+ const void *StartAddress = (void *)(0x100'0000'0000'0000ULL );
102
+ // TODO: Protect Bad Zone
103
+ auto Result = getContext ()->urDdiTable .VirtualMem .pfnReserve (
104
+ Context, StartAddress, ShadowSize, (void **)&ShadowBegin);
105
+ if (Result != UR_RESULT_SUCCESS) {
106
+ getContext ()->logger .error (" Shadow memory reserved failed with size {}: {}" ,
107
+ (void *)ShadowSize, Result);
133
108
return Result;
134
- }();
109
+ }
110
+ ShadowEnd = ShadowBegin + ShadowSize;
111
+
112
+ // Set shadow memory for null pointer
113
+ // For GPU, wu use up to 1 page of shadow memory
114
+ const size_t NullptrRedzoneSize = GetVirtualMemGranularity (Context, Device)
115
+ << ASAN_SHADOW_SCALE;
116
+ ManagedQueue Queue (Context, Device);
117
+ Result = EnqueuePoisonShadow (Queue, 0 , NullptrRedzoneSize,
118
+ kNullPointerRedzoneMagic );
119
+ if (Result != UR_RESULT_SUCCESS) {
120
+ getContext ()->logger .error (" EnqueuePoisonShadow(NullPointerRZ): {}" ,
121
+ Result);
122
+ return Result;
123
+ }
135
124
return Result;
136
125
}
137
126
@@ -142,7 +131,13 @@ ur_result_t ShadowMemoryGPU::Destory() {
142
131
PrivateShadowOffset = 0 ;
143
132
}
144
133
145
- static ur_result_t Result = [this ]() {
134
+ if (LocalShadowOffset != 0 ) {
135
+ UR_CALL (getContext ()->urDdiTable .USM .pfnFree (Context,
136
+ (void *)LocalShadowOffset));
137
+ LocalShadowOffset = 0 ;
138
+ }
139
+
140
+ {
146
141
const size_t PageSize = GetVirtualMemGranularity (Context, Device);
147
142
for (auto [MappedPtr, PhysicalMem] : VirtualMemMaps) {
148
143
UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnUnmap (
@@ -151,24 +146,14 @@ ur_result_t ShadowMemoryGPU::Destory() {
151
146
}
152
147
UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnFree (
153
148
Context, (const void *)ShadowBegin, GetShadowSize ()));
154
- UR_CALL (getContext ()->urDdiTable .Context .pfnRelease (Context));
155
- return UR_RESULT_SUCCESS;
156
- }();
157
- if (!Result) {
158
- return Result;
159
- }
160
149
161
- if (LocalShadowOffset != 0 ) {
162
- UR_CALL (getContext ()->urDdiTable .USM .pfnFree (Context,
163
- (void *)LocalShadowOffset));
164
- LocalShadowOffset = 0 ;
165
- }
166
- if (ShadowBegin != 0 ) {
167
- UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnFree (
168
- Context, (const void *)ShadowBegin, GetShadowSize ()));
169
- UR_CALL (getContext ()->urDdiTable .Context .pfnRelease (Context));
170
- ShadowBegin = ShadowEnd = 0 ;
150
+ if (ShadowBegin != 0 ) {
151
+ UR_CALL (getContext ()->urDdiTable .VirtualMem .pfnFree (
152
+ Context, (const void *)ShadowBegin, GetShadowSize ()));
153
+ ShadowBegin = ShadowEnd = 0 ;
154
+ }
171
155
}
156
+
172
157
return UR_RESULT_SUCCESS;
173
158
}
174
159
@@ -248,7 +233,8 @@ ur_result_t ShadowMemoryGPU::AllocLocalShadow(ur_queue_handle_t Queue,
248
233
(NumWG * LocalMemorySize) >> ASAN_SHADOW_SCALE;
249
234
static size_t LastAllocedSize = 0 ;
250
235
if (RequiredShadowSize > LastAllocedSize) {
251
- auto ContextInfo = getAsanInterceptor ()->getContextInfo (Context);
236
+ ur_context_handle_t QueueContext = GetContext (Queue);
237
+ auto ContextInfo = getAsanInterceptor ()->getContextInfo (QueueContext);
252
238
if (LocalShadowOffset) {
253
239
UR_CALL (getContext ()->urDdiTable .USM .pfnFree (Context,
254
240
(void *)LocalShadowOffset));
@@ -288,7 +274,8 @@ ur_result_t ShadowMemoryGPU::AllocPrivateShadow(ur_queue_handle_t Queue,
288
274
(NumWG * ASAN_PRIVATE_SIZE) >> ASAN_SHADOW_SCALE;
289
275
static size_t LastAllocedSize = 0 ;
290
276
if (RequiredShadowSize > LastAllocedSize) {
291
- auto ContextInfo = getAsanInterceptor ()->getContextInfo (Context);
277
+ ur_context_handle_t QueueContext = GetContext (Queue);
278
+ auto ContextInfo = getAsanInterceptor ()->getContextInfo (QueueContext);
292
279
if (PrivateShadowOffset) {
293
280
UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
294
281
Context, (void *)PrivateShadowOffset));
0 commit comments