From de497463ab69b310b214b581865b0c3f5e53138c Mon Sep 17 00:00:00 2001 From: Katarzyna Kaczmarska Date: Fri, 12 Dec 2025 18:42:49 +0100 Subject: [PATCH 1/3] [UR][HIP] Make ur_context_handle_t_ destructor non-throwing UR_CHECK_ERROR may throw or abort, which is unsafe to use from a destructor. If an exception escapes during stack unwinding, std::terminate would be called. Replace UR_CHECK_ERROR in ur_context_handle_t_ destructor with a non-throwing, best-effort cleanup. In debug builds, failures are reported via assert/log, while release builds avoid throwing from the destructor. This fixes Coverity CID 519631, issue: [Coverity][UR] Potentially throwing function is called from a destructor --- unified-runtime/source/adapters/hip/context.hpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/unified-runtime/source/adapters/hip/context.hpp b/unified-runtime/source/adapters/hip/context.hpp index 120d5346d497f..f63239a066194 100644 --- a/unified-runtime/source/adapters/hip/context.hpp +++ b/unified-runtime/source/adapters/hip/context.hpp @@ -96,8 +96,19 @@ struct ur_context_handle_t_ : ur::hip::handle_base { UR_CHECK_ERROR(urAdapterRetain(ur::hip::adapter)); }; - ~ur_context_handle_t_() { - UR_CHECK_ERROR(urAdapterRelease(ur::hip::adapter)); + ~ur_context_handle_t_() noexcept { + try { + auto result = urAdapterRelease(ur::hip::adapter); + if (result != UR_RESULT_SUCCESS) { + UR_LOG(ERR, "Failed to release adapter in context destructor: {}", + result); + } + assert(result == UR_RESULT_SUCCESS && + "Adapter release failed in context destructor"); + } catch (...) { + UR_LOG(ERR, "Exception in context destructor"); + assert(false && "Exception in context destructor"); + } } ur_context_handle_t_(const ur_context_handle_t_ &) = delete; From ed4bf2bb3a1f75ac72f697a18f11780322c5ea01 Mon Sep 17 00:00:00 2001 From: Katarzyna Kaczmarska Date: Mon, 15 Dec 2025 16:30:28 +0100 Subject: [PATCH 2/3] [UR][CUDA] Avoid throwing from ur_context_handle_t_ destructor Make the CUDA context destructor non-throwing by catching exceptions and using best-effort cleanup with logging and debug asserts. Related to Coverity CID 519631. --- .../source/adapters/cuda/context.hpp | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/unified-runtime/source/adapters/cuda/context.hpp b/unified-runtime/source/adapters/cuda/context.hpp index 0837907b4f5a7..984eb4d8beb63 100644 --- a/unified-runtime/source/adapters/cuda/context.hpp +++ b/unified-runtime/source/adapters/cuda/context.hpp @@ -107,14 +107,25 @@ struct ur_context_handle_t_ : ur::cuda::handle_base { UR_CHECK_ERROR(urAdapterRetain(ur::cuda::adapter)); }; - ~ur_context_handle_t_() { - if (MemoryPoolHost) { - umfPoolDestroy(MemoryPoolHost); + ~ur_context_handle_t_() noexcept { + try { + if (MemoryPoolHost) { + umfPoolDestroy(MemoryPoolHost); + } + if (MemoryProviderHost) { + umfMemoryProviderDestroy(MemoryProviderHost); + } + auto result = urAdapterRelease(ur::cuda::adapter); + if (result != UR_RESULT_SUCCESS) { + UR_LOG(ERR, "Failed to release adapter in context destructor: {}", + result); + } + assert(result == UR_RESULT_SUCCESS && + "Adapter release failed in context destructor"); + } catch (...) { + UR_LOG(ERR, "Exception in context destructor"); + assert(false && "Exception in context destructor"); } - if (MemoryProviderHost) { - umfMemoryProviderDestroy(MemoryProviderHost); - } - UR_CHECK_ERROR(urAdapterRelease(ur::cuda::adapter)); } void invokeExtendedDeleters() { From 75e1a3563befb4d7c546a28e8ad3880deaba8537 Mon Sep 17 00:00:00 2001 From: Katarzyna Kaczmarska Date: Wed, 17 Dec 2025 09:53:16 +0100 Subject: [PATCH 3/3] Remove dead code from CUDA/HIP context destructors urAdapterRelease() always returns UR_RESULT_SUCCESS, making the error-checking code unreachable dead code. Exceptions from the call are already handled by the catch-all block. --- unified-runtime/source/adapters/cuda/context.hpp | 8 +------- unified-runtime/source/adapters/hip/context.hpp | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/unified-runtime/source/adapters/cuda/context.hpp b/unified-runtime/source/adapters/cuda/context.hpp index 984eb4d8beb63..737f5d64e1cb6 100644 --- a/unified-runtime/source/adapters/cuda/context.hpp +++ b/unified-runtime/source/adapters/cuda/context.hpp @@ -115,13 +115,7 @@ struct ur_context_handle_t_ : ur::cuda::handle_base { if (MemoryProviderHost) { umfMemoryProviderDestroy(MemoryProviderHost); } - auto result = urAdapterRelease(ur::cuda::adapter); - if (result != UR_RESULT_SUCCESS) { - UR_LOG(ERR, "Failed to release adapter in context destructor: {}", - result); - } - assert(result == UR_RESULT_SUCCESS && - "Adapter release failed in context destructor"); + urAdapterRelease(ur::cuda::adapter); } catch (...) { UR_LOG(ERR, "Exception in context destructor"); assert(false && "Exception in context destructor"); diff --git a/unified-runtime/source/adapters/hip/context.hpp b/unified-runtime/source/adapters/hip/context.hpp index f63239a066194..bcdb59d2c2c69 100644 --- a/unified-runtime/source/adapters/hip/context.hpp +++ b/unified-runtime/source/adapters/hip/context.hpp @@ -98,13 +98,7 @@ struct ur_context_handle_t_ : ur::hip::handle_base { ~ur_context_handle_t_() noexcept { try { - auto result = urAdapterRelease(ur::hip::adapter); - if (result != UR_RESULT_SUCCESS) { - UR_LOG(ERR, "Failed to release adapter in context destructor: {}", - result); - } - assert(result == UR_RESULT_SUCCESS && - "Adapter release failed in context destructor"); + urAdapterRelease(ur::hip::adapter); } catch (...) { UR_LOG(ERR, "Exception in context destructor"); assert(false && "Exception in context destructor");