Skip to content

Commit 1a49fd2

Browse files
[UR][L0] Added safety checks to prevent double urProgramRelease (#20257)
Users used to be able to call urProgramRelease on the same module, but with these changes, that would no longer be possible. --------- Signed-off-by: Zhang, Winston <[email protected]>
1 parent fe57abe commit 1a49fd2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

unified-runtime/source/adapters/level_zero/program.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,39 @@ ur_result_t urProgramRetain(
565565
ur_result_t urProgramRelease(
566566
/// [in] handle for the Program to release
567567
ur_program_handle_t Program) {
568+
if (!Program)
569+
return UR_RESULT_ERROR_INVALID_NULL_HANDLE;
570+
571+
// Detect double-release by attempting to safely access program members
572+
// and catching any access violations that indicate freed memory
573+
try {
574+
// First, try to access the RefCount to see if it's valid
575+
uint32_t currentRefCount = Program->RefCount.getCount();
576+
577+
if (currentRefCount == 0) {
578+
// This is a double-release - RefCount should never be 0 when
579+
// urProgramRelease is called
580+
return UR_RESULT_ERROR_INVALID_PROGRAM;
581+
}
582+
583+
if (Program->resourcesReleased) {
584+
// Resources already released, this is a double-release attempt
585+
return UR_RESULT_ERROR_INVALID_PROGRAM;
586+
}
587+
588+
if (!Program->Context) {
589+
// Check if the program has a valid context
590+
return UR_RESULT_ERROR_INVALID_PROGRAM;
591+
}
592+
593+
} catch (...) {
594+
// If we can't safely access the program members, it's likely
595+
// corrupted/freed This catches the case where urProgramRelease is called on
596+
// the same module twice
597+
return UR_RESULT_ERROR_INVALID_PROGRAM;
598+
}
599+
600+
// Perform the actual release
568601
if (!Program->RefCount.release())
569602
return UR_RESULT_SUCCESS;
570603

0 commit comments

Comments
 (0)