Skip to content

Commit 77a0422

Browse files
committed
sycn with latest code
1 parent 2f15623 commit 77a0422

11 files changed

+270
-179
lines changed

source/loader/layers/sanitizer/asan_allocator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace ur_sanitizer_layer {
1717

1818
void AllocInfo::print() {
1919
context.logger.info(
20-
"AllocInfo(AllocBegin={}, User={}-{}, AllocSize={}, Type={})",
21-
(void *)AllocBegin, (void *)UserBegin, (void *)UserEnd, AllocSize,
22-
ToString(Type));
20+
"AllocInfo(Alloc=[{}-{}), User=[{}-{}), AllocSize={}, Type={})",
21+
(void *)AllocBegin, (void *)(AllocBegin + AllocSize), (void *)UserBegin,
22+
(void *)(UserEnd), AllocSize, ToString(Type));
2323
}
2424

2525
} // namespace ur_sanitizer_layer

source/loader/layers/sanitizer/asan_interceptor.cpp

Lines changed: 121 additions & 115 deletions
Large diffs are not rendered by default.

source/loader/layers/sanitizer/asan_interceptor.hpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#pragma once
1414

1515
#include "asan_allocator.hpp"
16+
#include "asan_libdevice.hpp"
1617
#include "common.hpp"
17-
#include "device_sanitizer_report.hpp"
1818
#include "ur_sanitizer_layer.hpp"
1919

2020
#include <memory>
@@ -61,7 +61,8 @@ struct DeviceInfo {
6161

6262
struct QueueInfo {
6363
ur_queue_handle_t Handle;
64-
ur_mutex Mutex;
64+
65+
ur_shared_mutex Mutex;
6566
ur_event_handle_t LastEvent;
6667

6768
explicit QueueInfo(ur_queue_handle_t Queue)
@@ -112,9 +113,14 @@ struct LaunchInfo {
112113
DeviceSanitizerReport SPIR_DeviceSanitizerReportMem;
113114

114115
ur_context_handle_t Context = nullptr;
115-
size_t LocalWorkSize[3] = {0};
116-
117-
explicit LaunchInfo(ur_context_handle_t Context);
116+
const size_t *GlobalWorkSize = nullptr;
117+
const size_t *GlobalWorkOffset = nullptr;
118+
std::vector<size_t> LocalWorkSize;
119+
uint32_t WorkDim = 0;
120+
121+
LaunchInfo(ur_context_handle_t Context, const size_t *GlobalWorkSize,
122+
const size_t *LocalWorkSize, const size_t *GlobalWorkOffset,
123+
uint32_t WorkDim);
118124
~LaunchInfo();
119125
};
120126

@@ -134,17 +140,20 @@ class SanitizerInterceptor {
134140
ur_device_handle_t Device,
135141
const ur_usm_desc_t *Properties,
136142
ur_usm_pool_handle_t Pool, size_t Size,
137-
void **ResultPtr, AllocType Type);
143+
AllocType Type, void **ResultPtr);
138144
ur_result_t releaseMemory(ur_context_handle_t Context, void *Ptr);
139145

140146
ur_result_t registerDeviceGlobals(ur_context_handle_t Context,
141147
ur_program_handle_t Program);
142148

143149
ur_result_t preLaunchKernel(ur_kernel_handle_t Kernel,
144-
ur_queue_handle_t Queue, LaunchInfo &LaunchInfo,
145-
uint32_t numWorkgroup);
146-
void postLaunchKernel(ur_kernel_handle_t Kernel, ur_queue_handle_t Queue,
147-
ur_event_handle_t &Event, LaunchInfo &LaunchInfo);
150+
ur_queue_handle_t Queue,
151+
LaunchInfo &LaunchInfo);
152+
153+
ur_result_t postLaunchKernel(ur_kernel_handle_t Kernel,
154+
ur_queue_handle_t Queue,
155+
ur_event_handle_t &Event,
156+
LaunchInfo &LaunchInfo);
148157

149158
ur_result_t insertContext(ur_context_handle_t Context,
150159
std::shared_ptr<ContextInfo> &CI);
@@ -156,6 +165,12 @@ class SanitizerInterceptor {
156165

157166
std::optional<AllocationIterator> findAllocInfoByAddress(uptr Address);
158167

168+
std::shared_ptr<ContextInfo> getContextInfo(ur_context_handle_t Context) {
169+
std::shared_lock<ur_shared_mutex> Guard(m_ContextMapMutex);
170+
assert(m_ContextMap.find(Context) != m_ContextMap.end());
171+
return m_ContextMap[Context];
172+
}
173+
159174
private:
160175
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
161176
std::shared_ptr<DeviceInfo> &DeviceInfo,
@@ -169,8 +184,8 @@ class SanitizerInterceptor {
169184
ur_result_t prepareLaunch(ur_context_handle_t Context,
170185
std::shared_ptr<DeviceInfo> &DeviceInfo,
171186
ur_queue_handle_t Queue,
172-
ur_kernel_handle_t Kernel, LaunchInfo &LaunchInfo,
173-
uint32_t numWorkgroup);
187+
ur_kernel_handle_t Kernel,
188+
LaunchInfo &LaunchInfo);
174189

175190
ur_result_t allocShadowMemory(ur_context_handle_t Context,
176191
std::shared_ptr<DeviceInfo> &DeviceInfo);
@@ -179,12 +194,6 @@ class SanitizerInterceptor {
179194
ur_queue_handle_t Queue, uptr Addr,
180195
uptr Size, u8 Value);
181196

182-
std::shared_ptr<ContextInfo> getContextInfo(ur_context_handle_t Context) {
183-
std::shared_lock<ur_shared_mutex> Guard(m_ContextMapMutex);
184-
assert(m_ContextMap.find(Context) != m_ContextMap.end());
185-
return m_ContextMap[Context];
186-
}
187-
188197
std::shared_ptr<DeviceInfo> getDeviceInfo(ur_device_handle_t Device) {
189198
std::shared_lock<ur_shared_mutex> Guard(m_DeviceMapMutex);
190199
assert(m_DeviceMap.find(Device) != m_DeviceMap.end());
@@ -206,6 +215,7 @@ class SanitizerInterceptor {
206215

207216
uint64_t cl_Debug = 0;
208217
uint32_t cl_MaxQuarantineSizeMB = 0;
218+
bool cl_DetectLocals = true;
209219

210220
std::unique_ptr<Quarantine> m_Quarantine;
211221
};

source/loader/layers/sanitizer/device_sanitizer_report.hpp renamed to source/loader/layers/sanitizer/asan_libdevice.hpp

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum class DeviceSanitizerErrorType : int32_t {
2323
USE_AFTER_FREE,
2424
OUT_OF_SHADOW_BOUNDS,
2525
UNKNOWN_DEVICE,
26+
NULL_POINTER,
2627
};
2728

2829
enum class DeviceSanitizerMemoryType : int32_t {
@@ -39,8 +40,8 @@ enum class DeviceSanitizerMemoryType : int32_t {
3940
struct DeviceSanitizerReport {
4041
int Flag = 0;
4142

42-
char File[256 + 1] = "";
43-
char Func[256 + 1] = "";
43+
char File[256 + 1] = {};
44+
char Func[256 + 1] = {};
4445

4546
int32_t Line = 0;
4647

@@ -52,15 +53,53 @@ struct DeviceSanitizerReport {
5253
uint64_t LID1 = 0;
5354
uint64_t LID2 = 0;
5455

55-
uint64_t Addr = 0;
56+
uintptr_t Address = 0;
5657
bool IsWrite = false;
5758
uint32_t AccessSize = 0;
58-
DeviceSanitizerMemoryType MemoryType;
59-
DeviceSanitizerErrorType ErrorType;
59+
DeviceSanitizerMemoryType MemoryType = DeviceSanitizerMemoryType::UNKNOWN;
60+
DeviceSanitizerErrorType ErrorType = DeviceSanitizerErrorType::UNKNOWN;
6061

6162
bool IsRecover = false;
6263
};
6364

65+
constexpr unsigned ASAN_SHADOW_SCALE = 3;
66+
constexpr unsigned ASAN_SHADOW_GRANULARITY = 1ULL << ASAN_SHADOW_SCALE;
67+
68+
// Based on the observation, only the last 24 bits of the address of the private
69+
// variable have changed, we use 31 bits(2G) to be safe.
70+
constexpr std::size_t ASAN_PRIVATE_SIZE = 0x7fffffffULL + 1;
71+
72+
// These magic values are written to shadow for better error
73+
// reporting.
74+
constexpr int kUsmDeviceRedzoneMagic = (char)0x81;
75+
constexpr int kUsmHostRedzoneMagic = (char)0x82;
76+
constexpr int kUsmSharedRedzoneMagic = (char)0x83;
77+
constexpr int kMemBufferRedzoneMagic = (char)0x84;
78+
constexpr int kDeviceGlobalRedzoneMagic = (char)0x85;
79+
constexpr int kNullPointerRedzoneMagic = (char)0x86;
80+
81+
constexpr int kUsmDeviceDeallocatedMagic = (char)0x91;
82+
constexpr int kUsmHostDeallocatedMagic = (char)0x92;
83+
constexpr int kUsmSharedDeallocatedMagic = (char)0x93;
84+
constexpr int kMemBufferDeallocatedMagic = (char)0x93;
85+
86+
constexpr int kSharedLocalRedzoneMagic = (char)0xa1;
87+
88+
// Same with host ASan stack
89+
const int kPrivateLeftRedzoneMagic = (char)0xf1;
90+
const int kPrivateMidRedzoneMagic = (char)0xf2;
91+
const int kPrivateRightRedzoneMagic = (char)0xf3;
92+
93+
constexpr auto kSPIR_AsanShadowMemoryGlobalStart =
94+
"__AsanShadowMemoryGlobalStart";
95+
constexpr auto kSPIR_AsanShadowMemoryGlobalEnd = "__AsanShadowMemoryGlobalEnd";
96+
97+
constexpr auto kSPIR_DeviceType = "__DeviceType";
98+
constexpr auto kSPIR_AsanDebug = "__AsanDebug";
99+
100+
constexpr auto kSPIR_AsanDeviceGlobalCount = "__AsanDeviceGlobalCount";
101+
constexpr auto kSPIR_AsanDeviceGlobalMetadata = "__AsanDeviceGlobalMetadata";
102+
64103
inline const char *ToString(DeviceSanitizerMemoryType MemoryType) {
65104
switch (MemoryType) {
66105
case DeviceSanitizerMemoryType::USM_DEVICE:
@@ -94,6 +133,8 @@ inline const char *ToString(DeviceSanitizerErrorType ErrorType) {
94133
return "out-of-shadow-bounds-access";
95134
case DeviceSanitizerErrorType::UNKNOWN_DEVICE:
96135
return "unknown-device";
136+
case DeviceSanitizerErrorType::NULL_POINTER:
137+
return "null-pointer-access";
97138
default:
98139
return "unknown-error";
99140
}

source/loader/layers/sanitizer/asan_quarantine.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ std::vector<AllocationIterator> Quarantine::put(ur_device_handle_t Device,
1818
AllocationIterator &It) {
1919
auto &AI = It->second;
2020
auto AllocSize = AI->AllocSize;
21-
auto &Cache = m_Map[Device];
21+
auto &Cache = getCache(Device);
22+
2223
std::vector<AllocationIterator> DequeueList;
24+
std::scoped_lock<ur_mutex> Guard(Cache.Mutex);
2325
while (Cache.size() + AllocSize > m_MaxQuarantineSize) {
2426
auto ElementOp = Cache.dequeue();
2527
if (!ElementOp) {

source/loader/layers/sanitizer/asan_quarantine.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class QuarantineCache {
2626
using Element = AllocationIterator;
2727
using List = std::queue<Element>;
2828

29-
explicit QuarantineCache() {}
29+
// The following methods are not thread safe, use this lock
30+
ur_mutex Mutex;
3031

3132
// Total memory used, including internal accounting.
3233
uptr size() const { return m_Size; }
@@ -62,7 +63,13 @@ class Quarantine {
6263
AllocationIterator &Ptr);
6364

6465
private:
66+
QuarantineCache &getCache(ur_device_handle_t Device) {
67+
std::scoped_lock<ur_mutex> Guard(m_Mutex);
68+
return m_Map[Device];
69+
}
70+
6571
std::unordered_map<ur_device_handle_t, QuarantineCache> m_Map;
72+
ur_mutex m_Mutex;
6673
size_t m_MaxQuarantineSize;
6774
};
6875

source/loader/layers/sanitizer/asan_report.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
*/
1212

1313
#include "asan_report.hpp"
14+
1415
#include "asan_allocator.hpp"
1516
#include "asan_interceptor.hpp"
16-
#include "device_sanitizer_report.hpp"
17+
#include "asan_libdevice.hpp"
1718
#include "ur_sanitizer_layer.hpp"
1819
#include "ur_sanitizer_utils.hpp"
1920

@@ -35,7 +36,7 @@ void ReportBadFree(uptr Addr, const StackTrace &stack,
3536

3637
context.logger.always("{} is located inside of {} region [{}, {})",
3738
(void *)Addr, ToString(AI->Type),
38-
(void *)AI->UserBegin, (void *)(AI->UserEnd + 1));
39+
(void *)AI->UserBegin, (void *)AI->UserEnd);
3940
context.logger.always("allocated here:");
4041
AI->AllocStack.print();
4142

@@ -51,7 +52,7 @@ void ReportBadContext(uptr Addr, const StackTrace &stack,
5152

5253
context.logger.always("{} is located inside of {} region [{}, {})",
5354
(void *)Addr, ToString(AI->Type),
54-
(void *)AI->UserBegin, (void *)(AI->UserEnd + 1));
55+
(void *)AI->UserBegin, (void *)AI->UserEnd);
5556
context.logger.always("allocated here:");
5657
AI->AllocStack.print();
5758

@@ -72,7 +73,7 @@ void ReportDoubleFree(uptr Addr, const StackTrace &Stack,
7273

7374
context.logger.always("{} is located inside of {} region [{}, {})",
7475
(void *)Addr, ToString(AI->Type),
75-
(void *)AI->UserBegin, (void *)(AI->UserEnd + 1));
76+
(void *)AI->UserBegin, (void *)AI->UserEnd);
7677
context.logger.always("freed here:");
7778
AI->ReleaseStack.print();
7879
context.logger.always("previously allocated here:");
@@ -120,7 +121,7 @@ void ReportUseAfterFree(const DeviceSanitizerReport &Report,
120121
KernelName = DemangleName(KernelName);
121122

122123
context.logger.always("\n====ERROR: DeviceSanitizer: {} on address {}",
123-
ToString(Report.ErrorType), (void *)Report.Addr);
124+
ToString(Report.ErrorType), (void *)Report.Address);
124125
context.logger.always(
125126
"{} of size {} at kernel <{}> LID({}, {}, {}) GID({}, "
126127
"{}, {})",
@@ -131,24 +132,24 @@ void ReportUseAfterFree(const DeviceSanitizerReport &Report,
131132
context.logger.always("");
132133

133134
auto AllocInfoItOp =
134-
context.interceptor->findAllocInfoByAddress(Report.Addr);
135+
context.interceptor->findAllocInfoByAddress(Report.Address);
135136
if (!AllocInfoItOp) {
136137
context.logger.always("Failed to find which chunck {} is allocated",
137-
(void *)Report.Addr);
138+
(void *)Report.Address);
138139
return;
139140
}
140141

141142
auto &AllocInfo = (*AllocInfoItOp)->second;
142143
if (AllocInfo->Context != Context) {
143144
context.logger.always("Failed to find which chunck {} is allocated",
144-
(void *)Report.Addr);
145+
(void *)Report.Address);
145146
}
146147
assert(AllocInfo->IsReleased);
147148

148149
context.logger.always("{} is located inside of {} region [{}, {})",
149-
(void *)Report.Addr, ToString(AllocInfo->Type),
150+
(void *)Report.Address, ToString(AllocInfo->Type),
150151
(void *)AllocInfo->UserBegin,
151-
(void *)(AllocInfo->UserEnd + 1));
152+
(void *)AllocInfo->UserEnd);
152153
context.logger.always("allocated here:");
153154
AllocInfo->AllocStack.print();
154155
context.logger.always("released here:");

source/loader/layers/sanitizer/common.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ using uptr = uintptr_t;
2828
using u8 = unsigned char;
2929
using u32 = unsigned int;
3030

31-
constexpr unsigned ASAN_SHADOW_SCALE = 3;
32-
constexpr unsigned ASAN_SHADOW_GRANULARITY = 1ULL << ASAN_SHADOW_SCALE;
33-
3431
inline constexpr bool IsPowerOfTwo(uptr x) {
3532
return (x & (x - 1)) == 0 && x != 0;
3633
}

source/loader/layers/sanitizer/ur_sanddi.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMHostAlloc(
5858
context.logger.debug("==== urUSMHostAlloc");
5959

6060
return context.interceptor->allocateMemory(
61-
hContext, nullptr, pUSMDesc, pool, size, ppMem, AllocType::HOST_USM);
61+
hContext, nullptr, pUSMDesc, pool, size, AllocType::HOST_USM, ppMem);
6262
}
6363

6464
///////////////////////////////////////////////////////////////////////////////
@@ -83,7 +83,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMDeviceAlloc(
8383
context.logger.debug("==== urUSMDeviceAlloc");
8484

8585
return context.interceptor->allocateMemory(
86-
hContext, hDevice, pUSMDesc, pool, size, ppMem, AllocType::DEVICE_USM);
86+
hContext, hDevice, pUSMDesc, pool, size, AllocType::DEVICE_USM, ppMem);
8787
}
8888

8989
///////////////////////////////////////////////////////////////////////////////
@@ -108,7 +108,7 @@ __urdlllocal ur_result_t UR_APICALL urUSMSharedAlloc(
108108
context.logger.debug("==== urUSMSharedAlloc");
109109

110110
return context.interceptor->allocateMemory(
111-
hContext, hDevice, pUSMDesc, pool, size, ppMem, AllocType::SHARED_USM);
111+
hContext, hDevice, pUSMDesc, pool, size, AllocType::SHARED_USM, ppMem);
112112
}
113113

114114
///////////////////////////////////////////////////////////////////////////////
@@ -214,33 +214,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueKernelLaunch(
214214

215215
context.logger.debug("==== urEnqueueKernelLaunch");
216216

217-
LaunchInfo LaunchInfo(GetContext(hQueue));
218-
const size_t *pUserLocalWorkSize = pLocalWorkSize;
219-
if (!pUserLocalWorkSize) {
220-
pUserLocalWorkSize = LaunchInfo.LocalWorkSize;
221-
// FIXME: This is W/A until urKernelSuggestGroupSize is added
222-
LaunchInfo.LocalWorkSize[0] = 1;
223-
LaunchInfo.LocalWorkSize[1] = 1;
224-
LaunchInfo.LocalWorkSize[2] = 1;
225-
}
226-
227-
uint32_t numWork = 1;
228-
for (uint32_t dim = 0; dim < workDim; ++dim) {
229-
numWork *= (pGlobalWorkSize[dim] + pUserLocalWorkSize[dim] - 1) /
230-
pUserLocalWorkSize[dim];
231-
}
217+
LaunchInfo LaunchInfo(GetContext(hQueue), pGlobalWorkSize, pLocalWorkSize,
218+
pGlobalWorkOffset, workDim);
232219

233-
UR_CALL(context.interceptor->preLaunchKernel(hKernel, hQueue, LaunchInfo,
234-
numWork));
220+
UR_CALL(context.interceptor->preLaunchKernel(hKernel, hQueue, LaunchInfo));
235221

236222
ur_event_handle_t hEvent{};
237223
ur_result_t result = pfnKernelLaunch(
238224
hQueue, hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize,
239225
pLocalWorkSize, numEventsInWaitList, phEventWaitList, &hEvent);
240226

241227
if (result == UR_RESULT_SUCCESS) {
242-
context.interceptor->postLaunchKernel(hKernel, hQueue, hEvent,
243-
LaunchInfo);
228+
UR_CALL(context.interceptor->postLaunchKernel(hKernel, hQueue, hEvent,
229+
LaunchInfo));
244230
}
245231

246232
if (phEvent) {

0 commit comments

Comments
 (0)