@@ -221,25 +221,28 @@ ur_result_t AsanInterceptor::releaseMemory(ur_context_handle_t Context,
221221 if (ReleaseList.size ()) {
222222 std::scoped_lock<ur_shared_mutex> Guard (m_AllocationMapMutex);
223223 for (auto &It : ReleaseList) {
224+ auto ToFreeAllocInfo = It->second ;
224225 getContext ()->logger .info (" Quarantine Free: {}" ,
225- (void *)It-> second ->AllocBegin );
226+ (void *)ToFreeAllocInfo ->AllocBegin );
226227
227- ContextInfo->Stats .UpdateUSMRealFreed (AllocInfo-> AllocSize ,
228- AllocInfo ->getRedzoneSize ());
228+ ContextInfo->Stats .UpdateUSMRealFreed (
229+ ToFreeAllocInfo-> AllocSize , ToFreeAllocInfo ->getRedzoneSize ());
229230
230- m_AllocationMap.erase (It);
231- if (AllocInfo->Type == AllocType::HOST_USM) {
231+ if (ToFreeAllocInfo->Type == AllocType::HOST_USM) {
232232 for (auto &Device : ContextInfo->DeviceList ) {
233233 UR_CALL (getDeviceInfo (Device)->Shadow ->ReleaseShadow (
234- AllocInfo ));
234+ ToFreeAllocInfo ));
235235 }
236236 } else {
237- UR_CALL (getDeviceInfo (AllocInfo ->Device )
238- ->Shadow ->ReleaseShadow (AllocInfo ));
237+ UR_CALL (getDeviceInfo (ToFreeAllocInfo ->Device )
238+ ->Shadow ->ReleaseShadow (ToFreeAllocInfo ));
239239 }
240240
241241 UR_CALL (getContext ()->urDdiTable .USM .pfnFree (
242- Context, (void *)(It->second ->AllocBegin )));
242+ Context, (void *)(ToFreeAllocInfo->AllocBegin )));
243+
244+ // Erase it at last to avoid use-after-free.
245+ m_AllocationMap.erase (It);
243246 }
244247 }
245248 ContextInfo->Stats .UpdateUSMFreed (AllocInfo->AllocSize );
@@ -431,6 +434,7 @@ ur_result_t AsanInterceptor::registerProgram(ur_program_handle_t Program) {
431434
432435ur_result_t AsanInterceptor::unregisterProgram (ur_program_handle_t Program) {
433436 auto ProgramInfo = getProgramInfo (Program);
437+ assert (ProgramInfo != nullptr && " unregistered program!" );
434438
435439 for (auto AI : ProgramInfo->AllocInfoForGlobals ) {
436440 UR_CALL (getDeviceInfo (AI->Device )->Shadow ->ReleaseShadow (AI));
@@ -475,6 +479,7 @@ ur_result_t AsanInterceptor::registerSpirKernels(ur_program_handle_t Program) {
475479 }
476480
477481 auto PI = getProgramInfo (Program);
482+ assert (PI != nullptr && " unregistered program!" );
478483 for (const auto &SKI : SKInfo) {
479484 if (SKI.Size == 0 ) {
480485 continue ;
@@ -511,6 +516,7 @@ AsanInterceptor::registerDeviceGlobals(ur_program_handle_t Program) {
511516 auto Context = GetContext (Program);
512517 auto ContextInfo = getContextInfo (Context);
513518 auto ProgramInfo = getProgramInfo (Program);
519+ assert (ProgramInfo != nullptr && " unregistered program!" );
514520
515521 for (auto Device : Devices) {
516522 ManagedQueue Queue (Context, Device);
0 commit comments