Skip to content

Commit a634393

Browse files
committed
Merge pull request #2585 from yingcong-wu/yc-test-main/0120-on-shadow-per-type
[DeivceASAN] Make ShadowMemory one instance per type
1 parent ef7829e commit a634393

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,36 @@ ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices,
2828
const ur_device_handle_t *phDevices) {
2929
std::shared_ptr<ContextInfo> CI;
3030
UR_CALL(getAsanInterceptor()->insertContext(Context, CI));
31-
for (uint32_t i = 0; i < numDevices; ++i) {
32-
auto hDevice = phDevices[i];
33-
std::shared_ptr<DeviceInfo> DI;
34-
UR_CALL(getAsanInterceptor()->insertDevice(hDevice, DI));
35-
DI->Type = GetDeviceType(Context, hDevice);
36-
if (DI->Type == DeviceType::UNKNOWN) {
37-
getContext()->logger.error("Unsupport device");
38-
return UR_RESULT_ERROR_INVALID_DEVICE;
39-
}
40-
getContext()->logger.info(
41-
"DeviceInfo {} (Type={}, IsSupportSharedSystemUSM={})",
42-
(void *)DI->Handle, ToString(DI->Type),
43-
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));
31+
32+
if (numDevices > 0) {
33+
auto DeviceType = GetDeviceType(Context, phDevices[0]);
34+
auto ShadowMemory = getAsanInterceptor()->getOrCreateShadowMemory(
35+
phDevices[0], DeviceType);
36+
37+
for (uint32_t i = 0; i < numDevices; ++i) {
38+
auto hDevice = phDevices[i];
39+
std::shared_ptr<DeviceInfo> DI;
40+
UR_CALL(getAsanInterceptor()->insertDevice(hDevice, DI));
41+
DI->Type = GetDeviceType(Context, hDevice);
42+
if (DI->Type == DeviceType::UNKNOWN) {
43+
getContext()->logger.error("Unsupport device");
44+
return UR_RESULT_ERROR_INVALID_DEVICE;
45+
}
46+
if (DI->Type != DeviceType) {
47+
getContext()->logger.error(
48+
"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),
54+
DI->IsSupportSharedSystemUSM);
55+
getContext()->logger.info("Add {} into context {}",
56+
(void *)DI->Handle, (void *)Context);
57+
DI->Shadow = ShadowMemory;
58+
CI->DeviceList.emplace_back(hDevice);
59+
CI->AllocInfosMap[hDevice];
4860
}
49-
CI->DeviceList.emplace_back(hDevice);
50-
CI->AllocInfosMap[hDevice];
5161
}
5262
return UR_RESULT_SUCCESS;
5363
}

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

Lines changed: 24 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 &[_, ShadowMemory] : m_ShadowMap) {
51+
ShadowMemory->Destory();
52+
getContext()->urDdiTable.Context.pfnRelease(ShadowMemory->Context);
53+
}
54+
5155
for (auto Adapter : m_Adapters) {
5256
getContext()->urDdiTable.Global.pfnAdapterRelease(Adapter);
5357
}
@@ -301,14 +305,24 @@ ur_result_t AsanInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
301305
return Result;
302306
}
303307

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

314328
/// 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
@@ -56,8 +56,6 @@ struct DeviceInfo {
5656
// Device handles are special and alive in the whole process lifetime,
5757
// so we needn't retain&release here.
5858
explicit DeviceInfo(ur_device_handle_t Device) : Handle(Device) {}
59-
60-
ur_result_t allocShadowMemory(ur_context_handle_t Context);
6159
};
6260

6361
struct QueueInfo {
@@ -359,6 +357,9 @@ class AsanInterceptor {
359357

360358
bool isNormalExit() { return m_NormalExit; }
361359

360+
std::shared_ptr<ShadowMemory>
361+
getOrCreateShadowMemory(ur_device_handle_t Device, DeviceType Type);
362+
362363
private:
363364
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
364365
std::shared_ptr<DeviceInfo> &DeviceInfo,
@@ -375,9 +376,6 @@ class AsanInterceptor {
375376
ur_kernel_handle_t Kernel,
376377
LaunchInfo &LaunchInfo);
377378

378-
ur_result_t allocShadowMemory(ur_context_handle_t Context,
379-
std::shared_ptr<DeviceInfo> &DeviceInfo);
380-
381379
ur_result_t registerDeviceGlobals(ur_program_handle_t Program);
382380
ur_result_t registerSpirKernels(ur_program_handle_t Program);
383381

@@ -413,6 +411,9 @@ class AsanInterceptor {
413411
ur_shared_mutex m_AdaptersMutex;
414412

415413
bool m_NormalExit = true;
414+
415+
std::unordered_map<DeviceType, std::shared_ptr<ShadowMemory>> m_ShadowMap;
416+
ur_shared_mutex m_ShadowMapMutex;
416417
};
417418

418419
} // namespace asan

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ DeviceType GetDeviceType(ur_context_handle_t Context,
154154
// FIXME: There's no API querying the address bits of device, so we guess it by the
155155
// value of device USM pointer (see "USM Allocation Range" in asan_shadow.cpp)
156156
auto Type = DeviceType::UNKNOWN;
157-
if (Ptr >> 48 == 0xff00U) {
157+
158+
// L0 changes their VA layout.
159+
// TODO: update our shadow memory layout/algorithms to accordingly.
160+
if (((Ptr >> 52) & 0xff0U) == 0xff0U) {
158161
Type = DeviceType::GPU_PVC;
159162
} else {
160163
Type = DeviceType::GPU_DG2;

0 commit comments

Comments
 (0)