Skip to content

Commit ea035a2

Browse files
authored
[SYCL] Fix Coverity null reference hits, divide by 0 hits (#15247)
This PR fixes many Coverity hits: - Adds checks ensuring potentially nullable values are not dereferenced - Adds a sanity check on get_mem_granularity, ensuring memory granularity can never be 0 - This is done to fix a divide by 0 coverity hit, as the return result of get_mem_granularity is used directly in division.
1 parent 2fce71b commit ea035a2

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

sycl/source/backend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ make_kernel_bundle(ur_native_handle_t NativeHandle,
198198

199199
Plugin->call(urProgramCreateWithNativeHandle, NativeHandle,
200200
ContextImpl->getHandleRef(), &Properties, &UrProgram);
201+
if (UrProgram == nullptr)
202+
throw sycl::exception(
203+
sycl::make_error_code(sycl::errc::invalid),
204+
"urProgramCreateWithNativeHandle resulted in a null program handle.");
205+
201206
if (ContextImpl->getBackend() == backend::opencl)
202207
Plugin->call(urProgramRetain, UrProgram);
203208

sycl/source/detail/kernel_bundle_impl.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ class kernel_bundle_impl {
455455
Plugin->call(urProgramCreateWithIL, ContextImpl->getHandleRef(),
456456
spirv.data(), spirv.size(), nullptr, &UrProgram);
457457
// program created by urProgramCreateWithIL is implicitly retained.
458+
if (UrProgram == nullptr)
459+
throw sycl::exception(
460+
sycl::make_error_code(errc::invalid),
461+
"urProgramCreateWithIL resulted in a null program handle.");
458462

459463
std::string XsFlags = extractXsFlags(BuildOptions);
460464
auto Res =

sycl/source/detail/plugin.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class plugin {
6666
ur_result = call_nocheck(urAdapterGetLastError, MAdapter, &message, &adapter_error);
6767

6868
// If the warning level is greater then 2 emit the message
69-
if (detail::SYCLConfig<detail::SYCL_RT_WARNING_LEVEL>::get() >= 2) {
69+
if (message != nullptr &&
70+
detail::SYCLConfig<detail::SYCL_RT_WARNING_LEVEL>::get() >= 2) {
7071
std::clog << message << std::endl;
7172
}
7273

sycl/source/virtual_mem.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ __SYCL_EXPORT size_t get_mem_granularity(const device &SyclDevice,
5858
Plugin->call(urVirtualMemGranularityGetInfo, ContextImpl->getHandleRef(),
5959
DeviceImpl->getHandleRef(), GranularityQuery, sizeof(size_t),
6060
&Granularity, nullptr);
61+
if (Granularity == 0)
62+
throw sycl::exception(
63+
sycl::make_error_code(sycl::errc::invalid),
64+
"Unexpected granularity result: memory granularity shouldn't be 0.");
6165
return Granularity;
6266
}
6367

0 commit comments

Comments
 (0)