Skip to content

Commit 111182a

Browse files
authored
Merge pull request #2292 from zhaomaosu/disable-leak-check-exit-with-error
[DeviceASAN] Disable memory leak detection when asan exit with errors
2 parents 8ea84b0 + 3f77847 commit 111182a

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

source/loader/layers/sanitizer/asan_interceptor.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ SanitizerInterceptor::~SanitizerInterceptor() {
3939

4040
m_Quarantine = nullptr;
4141
m_MemBufferMap.clear();
42-
m_AllocationMap.clear();
4342
m_KernelMap.clear();
4443
m_ContextMap.clear();
44+
// AllocationMap need to be cleared after ContextMap because memory leak
45+
// detection depends on it.
46+
m_AllocationMap.clear();
4547

4648
for (auto Adapter : m_Adapters) {
4749
getContext()->urDdiTable.Global.pfnAdapterRelease(Adapter);
@@ -290,7 +292,7 @@ ur_result_t SanitizerInterceptor::postLaunchKernel(ur_kernel_handle_t Kernel,
290292
ReportFatalError(AH);
291293
}
292294
if (!AH.IsRecover) {
293-
exit(1);
295+
exitWithErrors();
294296
}
295297
}
296298
}
@@ -616,7 +618,7 @@ ur_result_t SanitizerInterceptor::prepareLaunch(
616618
ContextInfo->Handle, DeviceInfo->Handle, (uptr)Ptr)) {
617619
ReportInvalidKernelArgument(Kernel, ArgIndex, (uptr)Ptr,
618620
ValidateResult, PtrPair.second);
619-
exit(1);
621+
exitWithErrors();
620622
}
621623
}
622624
}
@@ -838,12 +840,14 @@ ContextInfo::~ContextInfo() {
838840
assert(Result == UR_RESULT_SUCCESS);
839841

840842
// check memory leaks
841-
std::vector<AllocationIterator> AllocInfos =
842-
getContext()->interceptor->findAllocInfoByContext(Handle);
843-
for (const auto &It : AllocInfos) {
844-
const auto &[_, AI] = *It;
845-
if (!AI->IsReleased) {
846-
ReportMemoryLeak(AI);
843+
if (getContext()->interceptor->isNormalExit()) {
844+
std::vector<AllocationIterator> AllocInfos =
845+
getContext()->interceptor->findAllocInfoByContext(Handle);
846+
for (const auto &It : AllocInfos) {
847+
const auto &[_, AI] = *It;
848+
if (!AI->IsReleased) {
849+
ReportMemoryLeak(AI);
850+
}
847851
}
848852
}
849853
}

source/loader/layers/sanitizer/asan_interceptor.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ class SanitizerInterceptor {
271271

272272
const AsanOptions &getOptions() { return m_Options; }
273273

274+
void exitWithErrors() {
275+
m_NormalExit = false;
276+
exit(1);
277+
}
278+
279+
bool isNormalExit() { return m_NormalExit; }
280+
274281
private:
275282
ur_result_t updateShadowMemory(std::shared_ptr<ContextInfo> &ContextInfo,
276283
std::shared_ptr<DeviceInfo> &DeviceInfo,
@@ -320,6 +327,8 @@ class SanitizerInterceptor {
320327

321328
std::unordered_set<ur_adapter_handle_t> m_Adapters;
322329
ur_shared_mutex m_AdaptersMutex;
330+
331+
bool m_NormalExit = true;
323332
};
324333

325334
} // namespace ur_sanitizer_layer

0 commit comments

Comments
 (0)