@@ -146,6 +146,8 @@ ur_result_t enqueueMemSetShadow(ur_context_handle_t Context,
146146
147147} // namespace
148148
149+ bool SanitizerInterceptor::AbnormalExit = false ;
150+
149151SanitizerInterceptor::SanitizerInterceptor (logger::Logger &logger)
150152 : logger(logger) {
151153 if (Options (logger).MaxQuarantineSizeMB ) {
@@ -159,15 +161,7 @@ SanitizerInterceptor::~SanitizerInterceptor() {
159161 DestroyShadowMemoryOnCPU ();
160162 DestroyShadowMemoryOnPVC ();
161163
162- std::shared_lock<ur_shared_mutex> Guard (m_AllocationMapMutex);
163- bool HasLeak = false ;
164- for (const auto &[_, AI] : m_AllocationMap) {
165- if (!AI->IsReleased ) {
166- HasLeak = true ;
167- ReportMemoryLeak (AI);
168- }
169- }
170- if (HasLeak) {
164+ if (AbnormalExit) {
171165 exit (1 );
172166 }
173167}
@@ -887,6 +881,40 @@ SanitizerInterceptor::findAllocInfoByAddress(uptr Address) {
887881 return It;
888882}
889883
884+ std::vector<AllocationIterator>
885+ SanitizerInterceptor::findAllocInfoByContext (ur_context_handle_t Context) {
886+ std::shared_lock<ur_shared_mutex> Guard (m_AllocationMapMutex);
887+ std::vector<AllocationIterator> AllocInfos;
888+ for (auto It = m_AllocationMap.begin (); It != m_AllocationMap.end (); It++) {
889+ const auto &[_, AI] = *It;
890+ if (AI->Context == Context) {
891+ AllocInfos.emplace_back (It);
892+ }
893+ }
894+ return AllocInfos;
895+ }
896+
897+ ContextInfo::~ContextInfo () {
898+ [[maybe_unused]] auto Result =
899+ getContext ()->urDdiTable .Context .pfnRelease (Handle);
900+ assert (Result == UR_RESULT_SUCCESS);
901+
902+ bool HasLeak = false ;
903+ std::vector<AllocationIterator> AllocInfos =
904+ getContext ()->interceptor ->findAllocInfoByContext (Handle);
905+ for (const auto &It : AllocInfos) {
906+ const auto &[_, AI] = *It;
907+ if (AI->IsReleased ) {
908+ ReportMemoryLeak (AI);
909+ HasLeak = true ;
910+ }
911+ }
912+
913+ if (HasLeak) {
914+ SanitizerInterceptor::AbnormalExit = true ;
915+ }
916+ }
917+
890918ur_result_t USMLaunchInfo::initialize () {
891919 UR_CALL (getContext ()->urDdiTable .Context .pfnRetain (Context));
892920 UR_CALL (getContext ()->urDdiTable .Device .pfnRetain (Device));
0 commit comments