Skip to content

Commit

Permalink
add extension API function pointer checks (KhronosGroup#196)
Browse files Browse the repository at this point in the history
* add extension API function pointer checks

This PR adds checks to ensure that any function pointer in the
ICD dispatch table is non-NULL before calling it.  If a function
pointer is NULL then CL_INVALID_VALUE is returned.  This
improves robustness for applications that assume they can safely
call a non-NULL function pointer returned by
clGetExtensionFunctionAddress or clGetExtensionFunctionAddressForPlatform.

* fix a few more copyright dates
  • Loading branch information
bashbaug authored Apr 7, 2023
1 parent ba84239 commit 617580b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ set (OPENCL_COMPILE_DEFINITIONS
CL_NO_NON_ICD_DISPATCH_EXTENSION_PROTOTYPES
OPENCL_ICD_LOADER_VERSION_MAJOR=3
OPENCL_ICD_LOADER_VERSION_MINOR=0
OPENCL_ICD_LOADER_VERSION_REV=5
OPENCL_ICD_LOADER_VERSION_REV=6
$<$<BOOL:${ENABLE_OPENCL_LAYERS}>:CL_ENABLE_LAYERS>
)

Expand Down
59 changes: 38 additions & 21 deletions loader/icd.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,46 @@ do \
#define KHR_ICD_WIDE_TRACE(...)
#endif

// if handle is NULL then return invalid_handle_error_code
#define KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(handle,invalid_handle_error_code) \
do \
{ \
if (!handle) \
{ \
return invalid_handle_error_code; \
} \
// Check if the passed-in handle is NULL, and if it is, return the error.
#define KHR_ICD_VALIDATE_HANDLE_RETURN_ERROR(_handle, _error) \
do { \
if (!_handle) { \
return _error; \
} \
} while (0)

// if handle is NULL then set errcode_ret to invalid_handle_error and return NULL
// (NULL being an invalid handle)
#define KHR_ICD_VALIDATE_HANDLE_RETURN_HANDLE(handle,invalid_handle_error) \
do \
{ \
if (!handle) \
{ \
if (errcode_ret) \
{ \
*errcode_ret = invalid_handle_error; \
} \
return NULL; \
} \
// Check if the passed-in handle is NULL, and if it is, first check and set
// errcode_ret to the error, then return NULL (NULL being an invalid handle).
#define KHR_ICD_VALIDATE_HANDLE_RETURN_HANDLE(_handle, _error) \
do { \
if (!_handle) { \
if (errcode_ret) { \
*errcode_ret = _error; \
} \
return NULL; \
} \
} while (0)

// Check if the passed-in function pointer is NULL, and if it is, return
// CL_INVALID_OPERATION.
#define KHR_ICD_VALIDATE_POINTER_RETURN_ERROR(_pointer) \
do { \
if (!_pointer) { \
return CL_INVALID_OPERATION; \
} \
} while (0)

// Check if the passed-in function pointer is NULL, and if it is, first
// check and set errcode_ret to CL_INVALID_OPERATION, then return NULL
// (NULL being an invalid handle).
#define KHR_ICD_VALIDATE_POINTER_RETURN_HANDLE(_pointer) \
do { \
if (!_pointer) { \
if (errcode_ret) { \
*errcode_ret = CL_INVALID_OPERATION; \
} \
return NULL; \
} \
} while (0)

#endif
Loading

0 comments on commit 617580b

Please sign in to comment.