Skip to content

Commit c2aa7cd

Browse files
committed
port
1 parent ed09541 commit c2aa7cd

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

source/loader/layers/sanitizer/asan/asan_ddi.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,34 @@ ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices,
2929
const ur_device_handle_t *phDevices) {
3030
std::shared_ptr<ContextInfo> CI;
3131
UR_CALL(getAsanInterceptor()->insertContext(Context, CI));
32-
for (uint32_t i = 0; i < numDevices; ++i) {
33-
auto hDevice = phDevices[i];
34-
std::shared_ptr<DeviceInfo> DI;
35-
UR_CALL(getAsanInterceptor()->insertDevice(hDevice, DI));
36-
DI->Type = GetDeviceType(Context, hDevice);
37-
if (DI->Type == DeviceType::UNKNOWN) {
38-
getContext()->logger.error("Unsupport device");
39-
return UR_RESULT_ERROR_INVALID_DEVICE;
40-
}
41-
getContext()->logger.info(
42-
"DeviceInfo {} (Type={}, IsSupportSharedSystemUSM={})",
43-
(void *)DI->Handle, ToString(DI->Type), DI->IsSupportSharedSystemUSM);
44-
getContext()->logger.info("Add {} into context {}", (void *)DI->Handle,
45-
(void *)Context);
46-
if (!DI->Shadow) {
47-
UR_CALL(DI->allocShadowMemory(Context));
32+
33+
if (numDevices > 0) {
34+
auto DeviceType = GetDeviceType(Context, phDevices[0]);
35+
auto ShadowMemory =
36+
getAsanInterceptor()->getOrCreateShadowMemory(phDevices[0], DeviceType);
37+
38+
for (uint32_t i = 0; i < numDevices; ++i) {
39+
auto hDevice = phDevices[i];
40+
std::shared_ptr<DeviceInfo> DI;
41+
UR_CALL(getAsanInterceptor()->insertDevice(hDevice, DI));
42+
DI->Type = GetDeviceType(Context, hDevice);
43+
if (DI->Type == DeviceType::UNKNOWN) {
44+
getContext()->logger.error("Unsupport device");
45+
return UR_RESULT_ERROR_INVALID_DEVICE;
46+
}
47+
if (DI->Type != DeviceType) {
48+
getContext()->logger.error("Different device type in the same context");
49+
return UR_RESULT_ERROR_INVALID_DEVICE;
50+
}
51+
getContext()->logger.info(
52+
"DeviceInfo {} (Type={}, IsSupportSharedSystemUSM={})",
53+
(void *)DI->Handle, ToString(DI->Type), DI->IsSupportSharedSystemUSM);
54+
getContext()->logger.info("Add {} into context {}", (void *)DI->Handle,
55+
(void *)Context);
56+
DI->Shadow = ShadowMemory;
57+
CI->DeviceList.emplace_back(hDevice);
58+
CI->AllocInfosMap[hDevice];
4859
}
49-
CI->DeviceList.emplace_back(hDevice);
50-
CI->AllocInfosMap[hDevice];
5160
}
5261
return UR_RESULT_SUCCESS;
5362
}

source/loader/layers/sanitizer/asan/asan_interceptor.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ AsanInterceptor::~AsanInterceptor() {
3636
// We must release these objects before releasing adapters, since
3737
// they may use the adapter in their destructor
3838
for (const auto &[_, DeviceInfo] : m_DeviceMap) {
39-
[[maybe_unused]] auto URes = DeviceInfo->Shadow->Destory();
40-
assert(URes == UR_RESULT_SUCCESS);
39+
DeviceInfo->Shadow = nullptr;
4140
}
4241

4342
m_Quarantine = nullptr;
@@ -48,6 +47,11 @@ AsanInterceptor::~AsanInterceptor() {
4847
// detection depends on it.
4948
m_AllocationMap.clear();
5049

50+
for (auto &[_, ShadowManager] : m_ShadowMap) {
51+
ShadowManager->Destory();
52+
getContext()->urDdiTable.Context.pfnRelease(ShadowManager->Context);
53+
}
54+
5155
for (auto Adapter : m_Adapters) {
5256
getContext()->urDdiTable.Global.pfnAdapterRelease(Adapter);
5357
}
@@ -303,14 +307,26 @@ ur_result_t AsanInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
303307
return Result;
304308
}
305309

306-
ur_result_t DeviceInfo::allocShadowMemory(ur_context_handle_t Context) {
307-
Shadow = GetShadowMemory(Context, Handle, Type);
308-
assert(Shadow && "Failed to get shadow memory");
309-
UR_CALL(Shadow->Setup());
310-
getContext()->logger.info("ShadowMemory(Global): {} - {}",
311-
(void *)Shadow->ShadowBegin,
312-
(void *)Shadow->ShadowEnd);
313-
return UR_RESULT_SUCCESS;
310+
std::shared_ptr<ShadowMemory>
311+
AsanInterceptor::getOrCreateShadowMemory(ur_device_handle_t Device,
312+
DeviceType Type) {
313+
if (m_ShadowMap.find(Type) == m_ShadowMap.end()) {
314+
std::scoped_lock<ur_shared_mutex> Guard(m_ShadowMapMutex);
315+
if (m_ShadowMap.find(Type) == m_ShadowMap.end()) {
316+
ur_context_handle_t InternalContext;
317+
auto Res = getContext()->urDdiTable.Context.pfnCreate(1, &Device, nullptr,
318+
&InternalContext);
319+
if (Res != UR_RESULT_SUCCESS) {
320+
getContext()->logger.error("Failed to create shadow context");
321+
return nullptr;
322+
}
323+
std::shared_ptr<ContextInfo> CI;
324+
insertContext(InternalContext, CI);
325+
m_ShadowMap[Type] = GetShadowMemory(InternalContext, Device, Type);
326+
m_ShadowMap[Type]->Setup();
327+
}
328+
}
329+
return m_ShadowMap[Type];
314330
}
315331

316332
/// Each 8 bytes of application memory are mapped into one byte of shadow memory

source/loader/layers/sanitizer/asan/asan_interceptor.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ struct DeviceInfo {
5757
// Device handles are special and alive in the whole process lifetime,
5858
// so we needn't retain&release here.
5959
explicit DeviceInfo(ur_device_handle_t Device) : Handle(Device) {}
60-
61-
ur_result_t allocShadowMemory(ur_context_handle_t Context);
6260
};
6361

6462
struct QueueInfo {
@@ -353,6 +351,9 @@ class AsanInterceptor {
353351

354352
bool isNormalExit() { return m_NormalExit; }
355353

354+
std::shared_ptr<ShadowMemory>
355+
getOrCreateShadowMemory(ur_device_handle_t Device, DeviceType Type);
356+
356357
private:
357358
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
358359
std::shared_ptr<DeviceInfo> &DeviceInfo,
@@ -368,9 +369,6 @@ class AsanInterceptor {
368369
ur_queue_handle_t Queue, ur_kernel_handle_t Kernel,
369370
LaunchInfo &LaunchInfo);
370371

371-
ur_result_t allocShadowMemory(ur_context_handle_t Context,
372-
std::shared_ptr<DeviceInfo> &DeviceInfo);
373-
374372
ur_result_t registerDeviceGlobals(ur_program_handle_t Program);
375373
ur_result_t registerSpirKernels(ur_program_handle_t Program);
376374

@@ -406,6 +404,9 @@ class AsanInterceptor {
406404
ur_shared_mutex m_AdaptersMutex;
407405

408406
bool m_NormalExit = true;
407+
408+
std::unordered_map<DeviceType, std::shared_ptr<ShadowMemory>> m_ShadowMap;
409+
ur_shared_mutex m_ShadowMapMutex;
409410
};
410411

411412
} // namespace asan

source/loader/layers/sanitizer/sanitizer_common/sanitizer_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ DeviceType GetDeviceType(ur_context_handle_t Context,
156156
// by the value of device USM pointer (see "USM Allocation Range" in
157157
// asan_shadow.cpp)
158158
auto Type = DeviceType::UNKNOWN;
159-
if (Ptr >> 48 == 0xff00U) {
159+
if (((Ptr >> 48) & 0xff00U) == 0xff00U) {
160160
Type = DeviceType::GPU_PVC;
161161
} else {
162162
Type = DeviceType::GPU_DG2;

0 commit comments

Comments
 (0)