Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions source/loader/layers/sanitizer/asan/asan_ddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ ur_result_t setupContext(ur_context_handle_t Context, uint32_t numDevices,
bool isInstrumentedKernel(ur_kernel_handle_t hKernel) {
auto hProgram = GetProgram(hKernel);
auto PI = getAsanInterceptor()->getProgramInfo(hProgram);
if (PI == nullptr) {
return false;
}
return PI->isKernelInstrumented(hKernel);
}

Expand Down Expand Up @@ -290,8 +293,9 @@ __urdlllocal ur_result_t UR_APICALL urProgramRetain(
UR_CALL(pfnRetain(hProgram));

auto ProgramInfo = getAsanInterceptor()->getProgramInfo(hProgram);
UR_ASSERT(ProgramInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE);
ProgramInfo->RefCount++;
if (ProgramInfo != nullptr) {
ProgramInfo->RefCount++;
}

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -364,6 +368,7 @@ __urdlllocal ur_result_t UR_APICALL urProgramLink(

UR_CALL(pfnProgramLink(hContext, count, phPrograms, pOptions, phProgram));

UR_CALL(getAsanInterceptor()->insertProgram(*phProgram));
UR_CALL(getAsanInterceptor()->registerProgram(*phProgram));

return UR_RESULT_SUCCESS;
Expand Down Expand Up @@ -395,6 +400,7 @@ ur_result_t UR_APICALL urProgramLinkExp(
UR_CALL(pfnProgramLinkExp(hContext, numDevices, phDevices, count,
phPrograms, pOptions, phProgram));

UR_CALL(getAsanInterceptor()->insertProgram(*phProgram));
UR_CALL(getAsanInterceptor()->registerProgram(*phProgram));

return UR_RESULT_SUCCESS;
Expand All @@ -417,8 +423,7 @@ ur_result_t UR_APICALL urProgramRelease(
UR_CALL(pfnProgramRelease(hProgram));

auto ProgramInfo = getAsanInterceptor()->getProgramInfo(hProgram);
UR_ASSERT(ProgramInfo != nullptr, UR_RESULT_ERROR_INVALID_VALUE);
if (--ProgramInfo->RefCount == 0) {
if (ProgramInfo != nullptr && --ProgramInfo->RefCount == 0) {
UR_CALL(getAsanInterceptor()->unregisterProgram(hProgram));
UR_CALL(getAsanInterceptor()->eraseProgram(hProgram));
}
Expand Down
24 changes: 15 additions & 9 deletions source/loader/layers/sanitizer/asan/asan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,28 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
if (ReleaseList.size()) {
std::scoped_lock<ur_shared_mutex> Guard(m_AllocationMapMutex);
for (auto &It : ReleaseList) {
auto ToFreeAllocInfo = It->second;
getContext()->logger.info("Quarantine Free: {}",
(void *)It->second->AllocBegin);
(void *)ToFreeAllocInfo->AllocBegin);

ContextInfo->Stats.UpdateUSMRealFreed(AllocInfo->AllocSize,
AllocInfo->getRedzoneSize());
ContextInfo->Stats.UpdateUSMRealFreed(
ToFreeAllocInfo->AllocSize, ToFreeAllocInfo->getRedzoneSize());

m_AllocationMap.erase(It);
if (AllocInfo->Type == AllocType::HOST_USM) {
if (ToFreeAllocInfo->Type == AllocType::HOST_USM) {
for (auto &Device : ContextInfo->DeviceList) {
UR_CALL(getDeviceInfo(Device)->Shadow->ReleaseShadow(
AllocInfo));
ToFreeAllocInfo));
}
} else {
UR_CALL(getDeviceInfo(AllocInfo->Device)
->Shadow->ReleaseShadow(AllocInfo));
UR_CALL(getDeviceInfo(ToFreeAllocInfo->Device)
->Shadow->ReleaseShadow(ToFreeAllocInfo));
}

UR_CALL(getContext()->urDdiTable.USM.pfnFree(
Context, (void *)(It->second->AllocBegin)));
Context, (void *)(ToFreeAllocInfo->AllocBegin)));

// Erase it at last to avoid use-after-free.
m_AllocationMap.erase(It);
}
}
ContextInfo->Stats.UpdateUSMFreed(AllocInfo->AllocSize);
Expand Down Expand Up @@ -433,6 +436,7 @@ ur_result_t AsanInterceptor::registerProgram(ur_program_handle_t Program) {

ur_result_t AsanInterceptor::unregisterProgram(ur_program_handle_t Program) {
auto ProgramInfo = getProgramInfo(Program);
assert(ProgramInfo != nullptr && "unregistered program!");

for (auto AI : ProgramInfo->AllocInfoForGlobals) {
UR_CALL(getDeviceInfo(AI->Device)->Shadow->ReleaseShadow(AI));
Expand Down Expand Up @@ -480,6 +484,7 @@ ur_result_t AsanInterceptor::registerSpirKernels(ur_program_handle_t Program) {
}

auto PI = getProgramInfo(Program);
assert(PI != nullptr && "unregistered program!");
for (const auto &SKI : SKInfo) {
if (SKI.Size == 0) {
continue;
Expand Down Expand Up @@ -516,6 +521,7 @@ AsanInterceptor::registerDeviceGlobals(ur_program_handle_t Program) {
auto Context = GetContext(Program);
auto ContextInfo = getContextInfo(Context);
auto ProgramInfo = getProgramInfo(Program);
assert(ProgramInfo != nullptr && "unregistered program!");

for (auto Device : Devices) {
ManagedQueue Queue(Context, Device);
Expand Down
6 changes: 4 additions & 2 deletions source/loader/layers/sanitizer/asan/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ class AsanInterceptor {

std::shared_ptr<ProgramInfo> getProgramInfo(ur_program_handle_t Program) {
std::shared_lock<ur_shared_mutex> Guard(m_ProgramMapMutex);
assert(m_ProgramMap.find(Program) != m_ProgramMap.end());
return m_ProgramMap[Program];
if (m_ProgramMap.find(Program) != m_ProgramMap.end()) {
return m_ProgramMap[Program];
}
return nullptr;
}

std::shared_ptr<KernelInfo> getKernelInfo(ur_kernel_handle_t Kernel) {
Expand Down
1 change: 1 addition & 0 deletions source/loader/layers/sanitizer/asan/asan_shadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ ur_result_t ShadowMemoryGPU::ReleaseShadow(std::shared_ptr<AllocInfo> AI) {
getContext()->logger.debug("urVirtualMemUnmap: {} ~ {}",
(void *)MappedPtr,
(void *)(MappedPtr + PageSize - 1));
VirtualMemMaps.erase(MappedPtr);
}
}

Expand Down
Loading