Skip to content

Commit

Permalink
[DeviceASAN] Disable memory leak detection when asan exit with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaomaosu committed Nov 7, 2024
1 parent 290bb93 commit 4d454fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 13 additions & 9 deletions source/loader/layers/sanitizer/asan_interceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ SanitizerInterceptor::~SanitizerInterceptor() {

m_Quarantine = nullptr;
m_MemBufferMap.clear();
m_AllocationMap.clear();
m_KernelMap.clear();
m_ContextMap.clear();
// AllocationMap need to be cleared after ContextMap because memory leak
// detection depends on it.
m_AllocationMap.clear();

for (auto Adapter : m_Adapters) {
getContext()->urDdiTable.Global.pfnAdapterRelease(Adapter);
Expand Down Expand Up @@ -290,7 +292,7 @@ ur_result_t SanitizerInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
ReportFatalError(AH);
}
if (!AH.IsRecover) {
exit(1);
exitWithErrors();
}
}
}
Expand Down Expand Up @@ -616,7 +618,7 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
ContextInfo->Handle, DeviceInfo->Handle, (uptr)Ptr)) {
ReportInvalidKernelArgument(Kernel, ArgIndex, (uptr)Ptr,
ValidateResult, PtrPair.second);
exit(1);
exitWithErrors();
}
}
}
Expand Down Expand Up @@ -838,12 +840,14 @@ ContextInfo::~ContextInfo() {
assert(Result == UR_RESULT_SUCCESS);

// check memory leaks
std::vector<AllocationIterator> AllocInfos =
getContext()->interceptor->findAllocInfoByContext(Handle);
for (const auto &It : AllocInfos) {
const auto &[_, AI] = *It;
if (!AI->IsReleased) {
ReportMemoryLeak(AI);
if (getContext()->interceptor->isNormalExit()) {
std::vector<AllocationIterator> AllocInfos =
getContext()->interceptor->findAllocInfoByContext(Handle);
for (const auto &It : AllocInfos) {
const auto &[_, AI] = *It;
if (!AI->IsReleased) {
ReportMemoryLeak(AI);
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions source/loader/layers/sanitizer/asan_interceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ class SanitizerInterceptor {

const AsanOptions &getOptions() { return m_Options; }

void exitWithErrors() {
m_NormalExit = false;
exit(1);
}

bool isNormalExit() { return m_NormalExit; }

private:
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
std::shared_ptr<DeviceInfo> &DeviceInfo,
Expand Down Expand Up @@ -320,6 +327,8 @@ class SanitizerInterceptor {

std::unordered_set<ur_adapter_handle_t> m_Adapters;
ur_shared_mutex m_AdaptersMutex;

bool m_NormalExit = true;
};

} // namespace ur_sanitizer_layer

0 comments on commit 4d454fa

Please sign in to comment.