diff --git a/scripts/templates/valddi.cpp.mako b/scripts/templates/valddi.cpp.mako index 3e7d039c4b..329a556971 100644 --- a/scripts/templates/valddi.cpp.mako +++ b/scripts/templates/valddi.cpp.mako @@ -74,6 +74,21 @@ namespace ur_validation_layer } + if( context.enableLifetimeValidation ) { + %for tp in tracked_params: + <% + tp_input_handle_funcs = next((hf for hf in handle_create_get_retain_release_funcs if th.subt(n, tags, tp['type']) == hf['handle'] and "[in]" in tp['desc']), {}) + is_related_create_get_retain_release_func = any(func_name in funcs for funcs in tp_input_handle_funcs.values()) + %> + %if tp_input_handle_funcs and not is_related_create_get_retain_release_func: + if (!refCountContext.isReferenceValid(${tp['name']})) { + refCountContext.logInvalidReference(${tp['name']}); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + %endif + %endfor + } + ${x}_result_t result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); %for tp in tracked_params: @@ -167,6 +182,7 @@ namespace ur_validation_layer if (enabledLayerNames.count(nameFullValidation)) { enableParameterValidation = true; enableLeakChecking = true; + enableLifetimeValidation = true; } else { if (enabledLayerNames.count(nameParameterValidation)) { enableParameterValidation = true; @@ -174,6 +190,10 @@ namespace ur_validation_layer if (enabledLayerNames.count(nameLeakChecking)) { enableLeakChecking = true; } + if (enabledLayerNames.count(nameLifetimeValidation)) { + // Handle lifetime validation requires leak checking feature. + enableLifetimeValidation = enableLeakChecking; + } } if(!enableParameterValidation && !enableLeakChecking) { diff --git a/source/loader/layers/validation/ur_leak_check.hpp b/source/loader/layers/validation/ur_leak_check.hpp index 3f3fb80b0b..b7cb487f88 100644 --- a/source/loader/layers/validation/ur_leak_check.hpp +++ b/source/loader/layers/validation/ur_leak_check.hpp @@ -115,6 +115,8 @@ struct RefCountContext { void clear() { counts.clear(); } + bool isReferenceValid(void *ptr) { return counts.count(ptr) > 0; } + void logInvalidReferences() { for (auto &[ptr, refRuntimeInfo] : counts) { context.logger.error("Retained {} reference(s) to handle {}", @@ -128,6 +130,10 @@ struct RefCountContext { } } + void logInvalidReference(void *ptr) { + context.logger.error("There are no valid references to handle {}", ptr); + } + } refCountContext; } // namespace ur_validation_layer diff --git a/source/loader/layers/validation/ur_valddi.cpp b/source/loader/layers/validation/ur_valddi.cpp index e4585280e0..18b70dc6d1 100644 --- a/source/loader/layers/validation/ur_valddi.cpp +++ b/source/loader/layers/validation/ur_valddi.cpp @@ -38,6 +38,9 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGet( if (context.enableParameterValidation) { } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAdapterGet(NumEntries, phAdapters, pNumAdapters); if (context.enableLeakChecking && phAdapters && @@ -67,6 +70,9 @@ __urdlllocal ur_result_t UR_APICALL urAdapterRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAdapterRelease(hAdapter); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -93,6 +99,9 @@ __urdlllocal ur_result_t UR_APICALL urAdapterRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAdapterRetain(hAdapter); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -134,6 +143,14 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGetLastError( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hAdapter)) { + refCountContext.logInvalidReference(hAdapter); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAdapterGetLastError(hAdapter, ppMessage, pError); return result; @@ -182,6 +199,14 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hAdapter)) { + refCountContext.logInvalidReference(hAdapter); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAdapterGetInfo(hAdapter, propName, propSize, pPropValue, pPropSizeRet); @@ -222,6 +247,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGet( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGet(phAdapters, NumAdapters, NumEntries, phPlatforms, pNumPlatforms); @@ -271,6 +299,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetInfo( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGetInfo(hPlatform, propName, propSize, pPropValue, pPropSizeRet); @@ -299,6 +330,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetApiVersion( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGetApiVersion(hPlatform, pVersion); return result; @@ -327,6 +361,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetNativeHandle( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGetNativeHandle(hPlatform, phNativePlatform); return result; @@ -355,6 +392,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnCreateWithNativeHandle(hNativePlatform, pProperties, phPlatform); @@ -391,6 +431,9 @@ __urdlllocal ur_result_t UR_APICALL urPlatformGetBackendOption( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGetBackendOption(hPlatform, pFrontendOption, ppPlatformOption); @@ -438,6 +481,9 @@ __urdlllocal ur_result_t UR_APICALL urDeviceGet( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnGet(hPlatform, DeviceType, NumEntries, phDevices, pNumDevices); @@ -495,6 +541,14 @@ __urdlllocal ur_result_t UR_APICALL urDeviceGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -519,6 +573,9 @@ __urdlllocal ur_result_t UR_APICALL urDeviceRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hDevice); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -545,6 +602,9 @@ __urdlllocal ur_result_t UR_APICALL urDeviceRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hDevice); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -589,6 +649,14 @@ __urdlllocal ur_result_t UR_APICALL urDevicePartition( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnPartition(hDevice, pProperties, NumDevices, phSubDevices, pNumDevicesRet); @@ -633,6 +701,14 @@ __urdlllocal ur_result_t UR_APICALL urDeviceSelectBinary( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSelectBinary(hDevice, pBinaries, NumBinaries, pSelectedBinary); @@ -662,6 +738,14 @@ __urdlllocal ur_result_t UR_APICALL urDeviceGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hDevice, phNativeDevice); return result; @@ -695,6 +779,9 @@ __urdlllocal ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnCreateWithNativeHandle(hNativeDevice, hPlatform, pProperties, phDevice); @@ -729,6 +816,14 @@ __urdlllocal ur_result_t UR_APICALL urDeviceGetGlobalTimestamps( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetGlobalTimestamps(hDevice, pDeviceTimestamp, pHostTimestamp); @@ -766,6 +861,9 @@ __urdlllocal ur_result_t UR_APICALL urContextCreate( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnCreate(DeviceCount, phDevices, pProperties, phContext); @@ -794,6 +892,9 @@ __urdlllocal ur_result_t UR_APICALL urContextRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hContext); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -820,6 +921,9 @@ __urdlllocal ur_result_t UR_APICALL urContextRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hContext); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -874,6 +978,14 @@ __urdlllocal ur_result_t UR_APICALL urContextGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hContext, propName, propSize, pPropValue, pPropSizeRet); @@ -903,6 +1015,14 @@ __urdlllocal ur_result_t UR_APICALL urContextGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hContext, phNativeContext); return result; @@ -938,6 +1058,9 @@ __urdlllocal ur_result_t UR_APICALL urContextCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnCreateWithNativeHandle( hNativeContext, numDevices, phDevices, pProperties, phContext); @@ -974,6 +1097,14 @@ __urdlllocal ur_result_t UR_APICALL urContextSetExtendedDeleter( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetExtendedDeleter(hContext, pfnDeleter, pUserData); return result; @@ -1034,6 +1165,14 @@ __urdlllocal ur_result_t UR_APICALL urMemImageCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageCreate(hContext, flags, pImageFormat, pImageDesc, pHost, phMem); @@ -1093,6 +1232,14 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnBufferCreate(hContext, flags, size, pProperties, phBuffer); @@ -1120,6 +1267,9 @@ __urdlllocal ur_result_t UR_APICALL urMemRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hMem); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1146,6 +1296,9 @@ __urdlllocal ur_result_t UR_APICALL urMemRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hMem); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1195,6 +1348,14 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferPartition( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnBufferPartition(hBuffer, flags, bufferCreateType, pRegion, phMem); @@ -1224,6 +1385,14 @@ __urdlllocal ur_result_t UR_APICALL urMemGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hMem)) { + refCountContext.logInvalidReference(hMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hMem, phNativeMem); return result; @@ -1257,6 +1426,14 @@ __urdlllocal ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnBufferCreateWithNativeHandle(hNativeMem, hContext, pProperties, phMem); @@ -1306,6 +1483,14 @@ __urdlllocal ur_result_t UR_APICALL urMemImageCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageCreateWithNativeHandle( hNativeMem, hContext, pImageFormat, pImageDesc, pProperties, phMem); @@ -1361,6 +1546,14 @@ __urdlllocal ur_result_t UR_APICALL urMemGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hMemory)) { + refCountContext.logInvalidReference(hMemory); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hMemory, propName, propSize, pPropValue, pPropSizeRet); @@ -1411,6 +1604,14 @@ __urdlllocal ur_result_t UR_APICALL urMemImageGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hMemory)) { + refCountContext.logInvalidReference(hMemory); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageGetInfo(hMemory, propName, propSize, pPropValue, pPropSizeRet); @@ -1454,6 +1655,14 @@ __urdlllocal ur_result_t UR_APICALL urSamplerCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreate(hContext, pDesc, phSampler); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1481,6 +1690,9 @@ __urdlllocal ur_result_t UR_APICALL urSamplerRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hSampler); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1508,6 +1720,9 @@ __urdlllocal ur_result_t UR_APICALL urSamplerRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hSampler); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1558,6 +1773,14 @@ __urdlllocal ur_result_t UR_APICALL urSamplerGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hSampler)) { + refCountContext.logInvalidReference(hSampler); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hSampler, propName, propSize, pPropValue, pPropSizeRet); @@ -1587,6 +1810,14 @@ __urdlllocal ur_result_t UR_APICALL urSamplerGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hSampler)) { + refCountContext.logInvalidReference(hSampler); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hSampler, phNativeSampler); return result; @@ -1620,6 +1851,14 @@ __urdlllocal ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithNativeHandle(hNativeSampler, hContext, pProperties, phSampler); @@ -1671,6 +1910,19 @@ __urdlllocal ur_result_t UR_APICALL urUSMHostAlloc( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(pool)) { + refCountContext.logInvalidReference(pool); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnHostAlloc(hContext, pUSMDesc, pool, size, ppMem); return result; @@ -1722,6 +1974,24 @@ __urdlllocal ur_result_t UR_APICALL urUSMDeviceAlloc( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(pool)) { + refCountContext.logInvalidReference(pool); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnDeviceAlloc(hContext, hDevice, pUSMDesc, pool, size, ppMem); @@ -1774,6 +2044,24 @@ __urdlllocal ur_result_t UR_APICALL urUSMSharedAlloc( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(pool)) { + refCountContext.logInvalidReference(pool); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSharedAlloc(hContext, hDevice, pUSMDesc, pool, size, ppMem); @@ -1802,6 +2090,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMFree( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnFree(hContext, pMem); return result; @@ -1842,6 +2138,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMGetMemAllocInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetMemAllocInfo(hContext, pMem, propName, propSize, pPropValue, pPropSizeRet); @@ -1881,6 +2185,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMPoolCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnPoolCreate(hContext, pPoolDesc, ppPool); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1907,6 +2219,9 @@ __urdlllocal ur_result_t UR_APICALL urUSMPoolRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnPoolRetain(pPool); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1933,6 +2248,9 @@ __urdlllocal ur_result_t UR_APICALL urUSMPoolRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnPoolRelease(pPool); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -1982,6 +2300,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMPoolGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hPool)) { + refCountContext.logInvalidReference(hPool); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnPoolGetInfo(hPool, propName, propSize, pPropValue, pPropSizeRet); @@ -2036,6 +2362,19 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemGranularityGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGranularityGetInfo( hContext, hDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -2072,6 +2411,14 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemReserve( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnReserve(hContext, pStart, size, ppStart); return result; @@ -2101,6 +2448,14 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemFree( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnFree(hContext, pStart, size); return result; @@ -2144,6 +2499,19 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemMap( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hPhysicalMem)) { + refCountContext.logInvalidReference(hPhysicalMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMap(hContext, pStart, size, hPhysicalMem, offset, flags); @@ -2174,6 +2542,14 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemUnmap( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUnmap(hContext, pStart, size); return result; @@ -2209,6 +2585,14 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemSetAccess( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetAccess(hContext, pStart, size, flags); return result; @@ -2252,6 +2636,14 @@ __urdlllocal ur_result_t UR_APICALL urVirtualMemGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hContext, pStart, size, propName, propSize, pPropValue, pPropSizeRet); @@ -2296,6 +2688,19 @@ __urdlllocal ur_result_t UR_APICALL urPhysicalMemCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreate(hContext, hDevice, size, pProperties, phPhysicalMem); @@ -2324,6 +2729,9 @@ __urdlllocal ur_result_t UR_APICALL urPhysicalMemRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hPhysicalMem); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -2351,6 +2759,9 @@ __urdlllocal ur_result_t UR_APICALL urPhysicalMemRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hPhysicalMem); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -2405,6 +2816,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithIL( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithIL(hContext, pIL, length, pProperties, phProgram); @@ -2462,6 +2881,19 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithBinary( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithBinary(hContext, hDevice, size, pBinary, pProperties, phProgram); @@ -2496,6 +2928,19 @@ __urdlllocal ur_result_t UR_APICALL urProgramBuild( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnBuild(hContext, hProgram, pOptions); return result; @@ -2526,6 +2971,19 @@ __urdlllocal ur_result_t UR_APICALL urProgramCompile( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCompile(hContext, hProgram, pOptions); return result; @@ -2567,6 +3025,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramLink( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnLink(hContext, count, phPrograms, pOptions, phProgram); @@ -2590,6 +3056,9 @@ __urdlllocal ur_result_t UR_APICALL urProgramRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hProgram); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -2616,6 +3085,9 @@ __urdlllocal ur_result_t UR_APICALL urProgramRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hProgram); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -2664,6 +3136,19 @@ __urdlllocal ur_result_t UR_APICALL urProgramGetFunctionPointer( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetFunctionPointer(hDevice, hProgram, pFunctionName, ppFunctionPointer); @@ -2714,6 +3199,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hProgram, propName, propSize, pPropValue, pPropSizeRet); @@ -2758,6 +3251,19 @@ __urdlllocal ur_result_t UR_APICALL urProgramGetBuildInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetBuildInfo(hProgram, hDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -2794,6 +3300,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramSetSpecializationConstants( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetSpecializationConstants(hProgram, count, pSpecConstants); @@ -2823,6 +3337,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hProgram, phNativeProgram); return result; @@ -2856,6 +3378,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithNativeHandle(hNativeProgram, hContext, pProperties, phProgram); @@ -2894,6 +3424,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreate(hProgram, pKernelName, phKernel); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -2930,6 +3468,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgValue( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetArgValue(hKernel, argIndex, argSize, pProperties, pArgValue); @@ -2958,6 +3504,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgLocal( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetArgLocal(hKernel, argIndex, argSize, pProperties); @@ -3009,6 +3563,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hKernel, propName, propSize, pPropValue, pPropSizeRet); @@ -3050,6 +3612,19 @@ __urdlllocal ur_result_t UR_APICALL urKernelGetGroupInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetGroupInfo(hKernel, hDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -3091,6 +3666,19 @@ __urdlllocal ur_result_t UR_APICALL urKernelGetSubGroupInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetSubGroupInfo(hKernel, hDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -3114,6 +3702,9 @@ __urdlllocal ur_result_t UR_APICALL urKernelRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hKernel); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3140,6 +3731,9 @@ __urdlllocal ur_result_t UR_APICALL urKernelRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hKernel); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3172,6 +3766,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgPointer( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetArgPointer(hKernel, argIndex, pProperties, pArgValue); @@ -3210,6 +3812,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetExecInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetExecInfo(hKernel, propName, propSize, pProperties, pPropValue); @@ -3241,6 +3851,19 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgSampler( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hArgValue)) { + refCountContext.logInvalidReference(hArgValue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetArgSampler(hKernel, argIndex, pProperties, hArgValue); @@ -3273,6 +3896,19 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetArgMemObj( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hArgValue)) { + refCountContext.logInvalidReference(hArgValue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetArgMemObj(hKernel, argIndex, pProperties, hArgValue); @@ -3308,6 +3944,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSetSpecializationConstants( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetSpecializationConstants(hKernel, count, pSpecConstants); @@ -3337,6 +3981,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hKernel, phNativeKernel); return result; @@ -3376,6 +4028,19 @@ __urdlllocal ur_result_t UR_APICALL urKernelCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithNativeHandle( hNativeKernel, hContext, hProgram, pProperties, phKernel); @@ -3427,6 +4092,14 @@ __urdlllocal ur_result_t UR_APICALL urQueueGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hQueue, propName, propSize, pPropValue, pPropSizeRet); @@ -3479,6 +4152,19 @@ __urdlllocal ur_result_t UR_APICALL urQueueCreate( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreate(hContext, hDevice, pProperties, phQueue); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3505,6 +4191,9 @@ __urdlllocal ur_result_t UR_APICALL urQueueRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hQueue); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3531,6 +4220,9 @@ __urdlllocal ur_result_t UR_APICALL urQueueRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hQueue); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3565,6 +4257,14 @@ __urdlllocal ur_result_t UR_APICALL urQueueGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hQueue, pDesc, phNativeQueue); return result; @@ -3603,6 +4303,19 @@ __urdlllocal ur_result_t UR_APICALL urQueueCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithNativeHandle( hNativeQueue, hContext, hDevice, pProperties, phQueue); @@ -3630,6 +4343,14 @@ __urdlllocal ur_result_t UR_APICALL urQueueFinish( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnFinish(hQueue); return result; @@ -3652,6 +4373,14 @@ __urdlllocal ur_result_t UR_APICALL urQueueFlush( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnFlush(hQueue); return result; @@ -3696,6 +4425,14 @@ __urdlllocal ur_result_t UR_APICALL urEventGetInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hEvent)) { + refCountContext.logInvalidReference(hEvent); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetInfo(hEvent, propName, propSize, pPropValue, pPropSizeRet); @@ -3736,6 +4473,14 @@ __urdlllocal ur_result_t UR_APICALL urEventGetProfilingInfo( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hEvent)) { + refCountContext.logInvalidReference(hEvent); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetProfilingInfo(hEvent, propName, propSize, pPropValue, pPropSizeRet); @@ -3766,6 +4511,9 @@ __urdlllocal ur_result_t UR_APICALL urEventWait( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnWait(numEvents, phEventWaitList); return result; @@ -3788,6 +4536,9 @@ __urdlllocal ur_result_t UR_APICALL urEventRetain( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetain(hEvent); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3814,6 +4565,9 @@ __urdlllocal ur_result_t UR_APICALL urEventRelease( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRelease(hEvent); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { @@ -3846,6 +4600,14 @@ __urdlllocal ur_result_t UR_APICALL urEventGetNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hEvent)) { + refCountContext.logInvalidReference(hEvent); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnGetNativeHandle(hEvent, phNativeEvent); return result; @@ -3879,6 +4641,14 @@ __urdlllocal ur_result_t UR_APICALL urEventCreateWithNativeHandle( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateWithNativeHandle(hNativeEvent, hContext, pProperties, phEvent); @@ -3922,6 +4692,14 @@ __urdlllocal ur_result_t UR_APICALL urEventSetCallback( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hEvent)) { + refCountContext.logInvalidReference(hEvent); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSetCallback(hEvent, execStatus, pfnNotify, pUserData); @@ -3999,6 +4777,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueKernelLaunch( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnKernelLaunch( hQueue, hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numEventsInWaitList, phEventWaitList, phEvent); @@ -4049,6 +4840,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueEventsWait( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnEventsWait(hQueue, numEventsInWaitList, phEventWaitList, phEvent); @@ -4099,6 +4898,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnEventsWaitWithBarrier(hQueue, numEventsInWaitList, phEventWaitList, phEvent); @@ -4160,6 +4967,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferRead( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferRead(hQueue, hBuffer, blockingRead, offset, size, pDst, numEventsInWaitList, phEventWaitList, phEvent); @@ -4224,6 +5044,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferWrite( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferWrite(hQueue, hBuffer, blockingWrite, offset, size, pSrc, numEventsInWaitList, phEventWaitList, phEvent); @@ -4338,6 +5171,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferReadRect( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferReadRect( hQueue, hBuffer, blockingRead, bufferOrigin, hostOrigin, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst, @@ -4457,6 +5303,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferWriteRect( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferWriteRect( hQueue, hBuffer, blockingWrite, bufferOrigin, hostOrigin, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc, @@ -4520,6 +5379,24 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferCopy( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBufferSrc)) { + refCountContext.logInvalidReference(hBufferSrc); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBufferDst)) { + refCountContext.logInvalidReference(hBufferDst); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferCopy(hQueue, hBufferSrc, hBufferDst, srcOffset, dstOffset, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -4627,6 +5504,24 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferCopyRect( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBufferSrc)) { + refCountContext.logInvalidReference(hBufferSrc); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBufferDst)) { + refCountContext.logInvalidReference(hBufferDst); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferCopyRect( hQueue, hBufferSrc, hBufferDst, srcOrigin, dstOrigin, region, srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch, @@ -4710,6 +5605,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferFill( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferFill(hQueue, hBuffer, pPattern, patternSize, offset, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -4781,6 +5689,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemImageRead( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hImage)) { + refCountContext.logInvalidReference(hImage); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemImageRead( hQueue, hImage, blockingRead, origin, region, rowPitch, slicePitch, pDst, numEventsInWaitList, phEventWaitList, phEvent); @@ -4853,6 +5774,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemImageWrite( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hImage)) { + refCountContext.logInvalidReference(hImage); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemImageWrite( hQueue, hImage, blockingWrite, origin, region, rowPitch, slicePitch, pSrc, numEventsInWaitList, phEventWaitList, phEvent); @@ -4925,6 +5859,24 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemImageCopy( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hImageSrc)) { + refCountContext.logInvalidReference(hImageSrc); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hImageDst)) { + refCountContext.logInvalidReference(hImageDst); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemImageCopy(hQueue, hImageSrc, hImageDst, srcOrigin, dstOrigin, region, numEventsInWaitList, phEventWaitList, phEvent); @@ -4993,6 +5945,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemBufferMap( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemBufferMap(hQueue, hBuffer, blockingMap, mapFlags, offset, size, numEventsInWaitList, phEventWaitList, phEvent, ppRetMap); @@ -5053,6 +6018,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueMemUnmap( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hMem)) { + refCountContext.logInvalidReference(hMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMemUnmap(hQueue, hMem, pMappedPtr, numEventsInWaitList, phEventWaitList, phEvent); @@ -5134,6 +6112,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMFill( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMFill(hQueue, ptr, patternSize, pPattern, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -5199,6 +6185,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMMemcpy( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMMemcpy(hQueue, blocking, pDst, pSrc, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -5263,6 +6257,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMPrefetch( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMPrefetch(hQueue, pMem, size, flags, numEventsInWaitList, phEventWaitList, phEvent); @@ -5305,6 +6307,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMAdvise( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMAdvise(hQueue, pMem, size, advice, phEvent); return result; @@ -5404,6 +6414,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMFill2D( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMFill2D(hQueue, pMem, pitch, patternSize, pPattern, width, height, numEventsInWaitList, phEventWaitList, phEvent); @@ -5490,6 +6508,14 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueUSMMemcpy2D( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUSMMemcpy2D(hQueue, blocking, pDst, dstPitch, pSrc, srcPitch, width, height, numEventsInWaitList, phEventWaitList, phEvent); @@ -5561,6 +6587,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnDeviceGlobalVariableWrite( hQueue, hProgram, name, blockingWrite, count, offset, pSrc, numEventsInWaitList, phEventWaitList, phEvent); @@ -5632,6 +6671,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnDeviceGlobalVariableRead( hQueue, hProgram, name, blockingRead, count, offset, pDst, numEventsInWaitList, phEventWaitList, phEvent); @@ -5706,6 +6758,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueReadHostPipe( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnReadHostPipe(hQueue, hProgram, pipe_symbol, blocking, pDst, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -5780,6 +6845,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueWriteHostPipe( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnWriteHostPipe(hQueue, hProgram, pipe_symbol, blocking, pSrc, size, numEventsInWaitList, phEventWaitList, phEvent); @@ -5841,6 +6919,24 @@ __urdlllocal ur_result_t UR_APICALL urUSMPitchedAllocExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(pool)) { + refCountContext.logInvalidReference(pool); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnPitchedAllocExp(hContext, hDevice, pUSMDesc, pool, widthInBytes, height, elementSizeBytes, ppMem, pResultPitch); @@ -5878,6 +6974,19 @@ urBindlessImagesUnsampledImageHandleDestroyExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUnsampledImageHandleDestroyExp(hContext, hDevice, hImage); @@ -5914,6 +7023,19 @@ urBindlessImagesSampledImageHandleDestroyExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSampledImageHandleDestroyExp(hContext, hDevice, hImage); @@ -5964,6 +7086,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesImageAllocateExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageAllocateExp(hContext, hDevice, pImageFormat, pImageDesc, phImageMem); @@ -5998,6 +7133,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesImageFreeExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageFreeExp(hContext, hDevice, hImageMem); return result; @@ -6058,6 +7206,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesUnsampledImageCreateExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnUnsampledImageCreateExp( hContext, hDevice, hImageMem, pImageFormat, pImageDesc, phMem, phImage); @@ -6124,6 +7285,24 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesSampledImageCreateExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hSampler)) { + refCountContext.logInvalidReference(hSampler); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSampledImageCreateExp(hContext, hDevice, hImageMem, pImageFormat, pImageDesc, hSampler, phMem, phImage); @@ -6209,6 +7388,14 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesImageCopyExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImageCopyExp( hQueue, pDst, pSrc, pImageFormat, pImageDesc, imageCopyFlags, srcOffset, dstOffset, copyExtent, hostExtent, numEventsInWaitList, phEventWaitList, @@ -6246,6 +7433,9 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesImageGetInfoExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnImageGetInfoExp(hImageMem, propName, pPropValue, pPropSizeRet); @@ -6288,6 +7478,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesMipmapGetLevelExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMipmapGetLevelExp(hContext, hDevice, hImageMem, mipmapLevel, phImageMem); @@ -6322,6 +7525,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesMipmapFreeExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMipmapFreeExp(hContext, hDevice, hMem); return result; @@ -6363,6 +7579,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesImportOpaqueFDExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImportOpaqueFDExp(hContext, hDevice, size, pInteropMemDesc, phInteropMem); @@ -6419,6 +7648,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesMapExternalArrayExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnMapExternalArrayExp( hContext, hDevice, pImageFormat, pImageDesc, hInteropMem, phImageMem); @@ -6454,6 +7696,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesReleaseInteropExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnReleaseInteropExp(hContext, hDevice, hInteropMem); return result; @@ -6496,6 +7751,19 @@ urBindlessImagesImportExternalSemaphoreOpaqueFDExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImportExternalSemaphoreOpaqueFDExp( hContext, hDevice, pInteropSemaphoreDesc, phInteropSemaphore); @@ -6531,6 +7799,19 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesDestroyExternalSemaphoreExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnDestroyExternalSemaphoreExp(hContext, hDevice, hInteropSemaphore); @@ -6579,6 +7860,14 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesWaitExternalSemaphoreExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnWaitExternalSemaphoreExp( hQueue, hSemaphore, numEventsInWaitList, phEventWaitList, phEvent); @@ -6627,6 +7916,14 @@ __urdlllocal ur_result_t UR_APICALL urBindlessImagesSignalExternalSemaphoreExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSignalExternalSemaphoreExp( hQueue, hSemaphore, numEventsInWaitList, phEventWaitList, phEvent); @@ -6663,6 +7960,19 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferCreateExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDevice)) { + refCountContext.logInvalidReference(hDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCreateExp(hContext, hDevice, pCommandBufferDesc, phCommandBuffer); @@ -6687,6 +7997,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferRetainExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnRetainExp(hCommandBuffer); return result; @@ -6710,6 +8023,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferReleaseExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnReleaseExp(hCommandBuffer); return result; @@ -6733,6 +8049,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferFinalizeExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnFinalizeExp(hCommandBuffer); return result; @@ -6795,6 +8114,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendKernelLaunchExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendKernelLaunchExp( hCommandBuffer, hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numSyncPointsInWaitList, pSyncPointWaitList, @@ -6851,6 +8178,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendUSMMemcpyExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAppendUSMMemcpyExp(hCommandBuffer, pDst, pSrc, size, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -6920,6 +8250,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendUSMFillExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAppendUSMFillExp( hCommandBuffer, pMemory, pPattern, patternSize, size, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -6973,6 +8306,19 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hSrcMem)) { + refCountContext.logInvalidReference(hSrcMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDstMem)) { + refCountContext.logInvalidReference(hDstMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferCopyExp( hCommandBuffer, hSrcMem, hDstMem, srcOffset, dstOffset, size, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7026,6 +8372,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferWriteExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferWriteExp( hCommandBuffer, hBuffer, offset, size, pSrc, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7078,6 +8432,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferReadExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferReadExp( hCommandBuffer, hBuffer, offset, size, pDst, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7138,6 +8500,19 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferCopyRectExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hSrcMem)) { + refCountContext.logInvalidReference(hSrcMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hDstMem)) { + refCountContext.logInvalidReference(hDstMem); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferCopyRectExp( hCommandBuffer, hSrcMem, hDstMem, srcOrigin, dstOrigin, region, srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch, @@ -7205,6 +8580,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferWriteRectExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferWriteRectExp( hCommandBuffer, hBuffer, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc, @@ -7270,6 +8653,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferReadRectExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferReadRectExp( hCommandBuffer, hBuffer, bufferOffset, hostOffset, region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst, @@ -7325,6 +8716,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendMemBufferFillExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hBuffer)) { + refCountContext.logInvalidReference(hBuffer); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnAppendMemBufferFillExp( hCommandBuffer, hBuffer, pPattern, patternSize, offset, size, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7380,6 +8779,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendUSMPrefetchExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAppendUSMPrefetchExp( hCommandBuffer, pMemory, size, flags, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7435,6 +8837,9 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferAppendUSMAdviseExp( } } + if (context.enableLifetimeValidation) { + } + ur_result_t result = pfnAppendUSMAdviseExp(hCommandBuffer, pMemory, size, advice, numSyncPointsInWaitList, pSyncPointWaitList, pSyncPoint); @@ -7491,6 +8896,14 @@ __urdlllocal ur_result_t UR_APICALL urCommandBufferEnqueueExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnEnqueueExp( hCommandBuffer, hQueue, numEventsInWaitList, phEventWaitList, phEvent); @@ -7569,6 +8982,19 @@ __urdlllocal ur_result_t UR_APICALL urEnqueueCooperativeKernelLaunchExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hQueue)) { + refCountContext.logInvalidReference(hQueue); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCooperativeKernelLaunchExp( hQueue, hKernel, workDim, pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numEventsInWaitList, phEventWaitList, phEvent); @@ -7599,6 +9025,14 @@ __urdlllocal ur_result_t UR_APICALL urKernelSuggestMaxCooperativeGroupCountExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hKernel)) { + refCountContext.logInvalidReference(hKernel); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnSuggestMaxCooperativeGroupCountExp(hKernel, pGroupCountRet); @@ -7631,6 +9065,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramBuildExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnBuildExp(hProgram, numDevices, phDevices, pOptions); return result; @@ -7663,6 +9105,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramCompileExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hProgram)) { + refCountContext.logInvalidReference(hProgram); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnCompileExp(hProgram, numDevices, phDevices, pOptions); @@ -7712,6 +9162,14 @@ __urdlllocal ur_result_t UR_APICALL urProgramLinkExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnLinkExp(hContext, numDevices, phDevices, count, phPrograms, pOptions, phProgram); @@ -7741,6 +9199,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMImportExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnImportExp(hContext, pMem, size); return result; @@ -7768,6 +9234,14 @@ __urdlllocal ur_result_t UR_APICALL urUSMReleaseExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(hContext)) { + refCountContext.logInvalidReference(hContext); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnReleaseExp(hContext, pMem); return result; @@ -7797,6 +9271,19 @@ __urdlllocal ur_result_t UR_APICALL urUsmP2PEnablePeerAccessExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(commandDevice)) { + refCountContext.logInvalidReference(commandDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(peerDevice)) { + refCountContext.logInvalidReference(peerDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnEnablePeerAccessExp(commandDevice, peerDevice); return result; @@ -7826,6 +9313,19 @@ __urdlllocal ur_result_t UR_APICALL urUsmP2PDisablePeerAccessExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(commandDevice)) { + refCountContext.logInvalidReference(commandDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(peerDevice)) { + refCountContext.logInvalidReference(peerDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnDisablePeerAccessExp(commandDevice, peerDevice); return result; @@ -7882,6 +9382,19 @@ __urdlllocal ur_result_t UR_APICALL urUsmP2PPeerAccessGetInfoExp( } } + if (context.enableLifetimeValidation) { + + if (!refCountContext.isReferenceValid(commandDevice)) { + refCountContext.logInvalidReference(commandDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + + if (!refCountContext.isReferenceValid(peerDevice)) { + refCountContext.logInvalidReference(peerDevice); + return UR_RESULT_ERROR_INVALID_ARGUMENT; + } + } + ur_result_t result = pfnPeerAccessGetInfoExp(commandDevice, peerDevice, propName, propSize, pPropValue, pPropSizeRet); @@ -9160,6 +10673,7 @@ ur_result_t context_t::init(ur_dditable_t *dditable, if (enabledLayerNames.count(nameFullValidation)) { enableParameterValidation = true; enableLeakChecking = true; + enableLifetimeValidation = true; } else { if (enabledLayerNames.count(nameParameterValidation)) { enableParameterValidation = true; @@ -9167,6 +10681,10 @@ ur_result_t context_t::init(ur_dditable_t *dditable, if (enabledLayerNames.count(nameLeakChecking)) { enableLeakChecking = true; } + if (enabledLayerNames.count(nameLifetimeValidation)) { + // Handle lifetime validation requires leak checking feature. + enableLifetimeValidation = enableLeakChecking; + } } if (!enableParameterValidation && !enableLeakChecking) { diff --git a/source/loader/layers/validation/ur_validation_layer.hpp b/source/loader/layers/validation/ur_validation_layer.hpp index e41c621dc8..3ac039e536 100644 --- a/source/loader/layers/validation/ur_validation_layer.hpp +++ b/source/loader/layers/validation/ur_validation_layer.hpp @@ -22,6 +22,7 @@ class __urdlllocal context_t : public proxy_layer_context_t { public: bool enableParameterValidation = false; bool enableLeakChecking = false; + bool enableLifetimeValidation = false; logger::Logger logger; ur_dditable_t urDdiTable = {}; @@ -31,7 +32,8 @@ class __urdlllocal context_t : public proxy_layer_context_t { bool isAvailable() const override { return true; } std::vector getNames() const override { - return {nameFullValidation, nameParameterValidation, nameLeakChecking}; + return {nameFullValidation, nameParameterValidation, nameLeakChecking, + nameLifetimeValidation}; } ur_result_t init(ur_dditable_t *dditable, const std::set &enabledLayerNames, @@ -42,6 +44,7 @@ class __urdlllocal context_t : public proxy_layer_context_t { const std::string nameFullValidation = "UR_LAYER_FULL_VALIDATION"; const std::string nameParameterValidation = "UR_LAYER_PARAMETER_VALIDATION"; const std::string nameLeakChecking = "UR_LAYER_LEAK_CHECKING"; + const std::string nameLifetimeValidation = "UR_LAYER_LIFETIME_VALIDATION"; }; extern context_t context;