Skip to content

Commit 72c54ee

Browse files
[SYCL] Fix coverity issues 11/11 2025 (#20614)
This commit makes the following changes to sate Coverity: 1. Initializes uninitialized variables. This mainly affects UR handles that are expected to be set after their corresponding UR calls. 2. Catch exceptions in dtors, similar to how we do for `*_impl` classes. 3. Fix use-after-move cases and set more appropriate ownerships. 4. Change a selection of arguments to const references. --------- Signed-off-by: Larsen, Steffen <steffen.larsen@intel.com>
1 parent 7dc6a5e commit 72c54ee

File tree

11 files changed

+26
-15
lines changed

11 files changed

+26
-15
lines changed

sycl/source/backend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ make_kernel_bundle(ur_native_handle_t NativeHandle,
223223
sizeof(ur_device_handle_t) * NumDevices, ProgramDevices.data(), nullptr);
224224

225225
for (auto &Dev : ProgramDevices) {
226-
ur_program_binary_type_t BinaryType;
226+
ur_program_binary_type_t BinaryType = UR_PROGRAM_BINARY_TYPE_NONE;
227227
Adapter.call<UrApiKind::urProgramGetBuildInfo>(
228228
UrProgram, Dev, UR_PROGRAM_BUILD_INFO_BINARY_TYPE,
229229
sizeof(ur_program_binary_type_t), &BinaryType, nullptr);

sycl/source/detail/adapter_impl.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ template <typename URResource> class Managed {
296296
if (!R)
297297
return;
298298

299-
Adapter->call<Release>(R);
299+
try {
300+
Adapter->call<Release>(R);
301+
} catch (std::exception &e) {
302+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~Managed", e);
303+
}
300304
}
301305

302306
Managed retain() {

sycl/source/detail/context_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ std::vector<ur_event_handle_t> context_impl::initializeDeviceGlobals(
417417
}
418418
// Write the pointer to the device global and store the event in the
419419
// initialize events list.
420-
ur_event_handle_t InitEvent;
420+
ur_event_handle_t InitEvent = nullptr;
421421
void *const &USMPtr = DeviceGlobalUSM.getPtr();
422422
Adapter.call<UrApiKind::urEnqueueDeviceGlobalVariableWrite>(
423423
QueueImpl.getHandleRef(), NativePrg,

sycl/source/detail/device_binary_image.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ mergeDeviceRequirements(const std::vector<const RTDeviceBinaryImage *> &Imgs) {
451451
size_t Pos = 0;
452452
do {
453453
const size_t NextPos = Contents.find(';', Pos);
454+
if (NextPos == std::string::npos) {
455+
Set.emplace(Contents.substr(Pos));
456+
break;
457+
}
454458
if (NextPos != Pos)
455459
Set.emplace(Contents.substr(Pos, NextPos - Pos));
456460
Pos = NextPos + 1;

sycl/source/detail/device_global_map_entry.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ DeviceGlobalUSMMem::~DeviceGlobalUSMMem() {
3838
} catch (std::exception &e) {
3939
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in ~DeviceGlobalUSMMem", e);
4040
}
41-
42-
assert(MPtr == nullptr && "MPtr has not been cleaned up.");
43-
assert(MInitEvent == nullptr && "MInitEvent has not been cleaned up.");
4441
}
4542

4643
OwnedUrEvent DeviceGlobalUSMMem::getInitEvent(adapter_impl &Adapter) {
@@ -86,7 +83,7 @@ DeviceGlobalMapEntry::getOrAllocateDeviceGlobalUSM(queue_impl &QueueImpl) {
8683
// Initialize here and save the event.
8784
{
8885
std::lock_guard<std::mutex> Lock(NewAlloc.MInitEventMutex);
89-
ur_event_handle_t InitEvent;
86+
ur_event_handle_t InitEvent = nullptr;
9087
if (MDeviceGlobalPtr) {
9188
// C++ guarantees members appear in memory in the order they are declared,
9289
// so since the member variable that contains the initial contents of the

sycl/source/detail/device_image_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class device_image_impl
326326
updateSpecConstSymMap();
327327
}
328328

329-
device_image_impl(const std::string &Src, context Context,
329+
device_image_impl(const std::string &Src, const context &Context,
330330
devices_range Devices, syclex::source_language Lang,
331331
include_pairs_t &&IncludePairsVec, private_tag)
332332
: MBinImage(Src), MContext(std::move(Context)),

sycl/source/detail/device_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
190190
return {Error};
191191
return {Result};
192192
} else {
193-
ur_ret_t Result;
193+
ur_ret_t Result{};
194194
ur_result_t Error = getAdapter().call_nocheck<UrApiKind::urDeviceGetInfo>(
195195
getHandleRef(), Desc, sizeof(Result), &Result, nullptr);
196196
if (Error == UR_RESULT_SUCCESS)
@@ -220,7 +220,7 @@ class device_impl : public std::enable_shared_from_this<device_impl> {
220220
getHandleRef(), Desc, ResultSize, Result.data(), nullptr);
221221
return Result;
222222
} else {
223-
ur_ret_t Result;
223+
ur_ret_t Result{};
224224
getAdapter().call<UrApiKind::urDeviceGetInfo>(
225225
getHandleRef(), Desc, sizeof(Result), &Result, nullptr);
226226
return Result;

sycl/source/detail/event_impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ class event_impl {
318318
ur_exp_command_buffer_sync_point_t getSyncPoint() const { return MSyncPoint; }
319319

320320
void setCommandGraph(
321-
std::shared_ptr<ext::oneapi::experimental::detail::graph_impl> Graph) {
321+
const std::shared_ptr<ext::oneapi::experimental::detail::graph_impl>
322+
&Graph) {
322323
MGraph = Graph;
323324
}
324325

sycl/source/detail/memory_manager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void *MemoryManager::allocateMemSubBuffer(context_impl *TargetContext,
445445
ur_result_t Error = UR_RESULT_SUCCESS;
446446
ur_buffer_region_t Region = {UR_STRUCTURE_TYPE_BUFFER_REGION, nullptr, Offset,
447447
SizeInBytes};
448-
ur_mem_handle_t NewMem;
448+
ur_mem_handle_t NewMem = nullptr;
449449
adapter_impl &Adapter = TargetContext->getAdapter();
450450
Error = Adapter.call_nocheck<UrApiKind::urMemBufferPartition>(
451451
ur::cast<ur_mem_handle_t>(ParentMemObj), UR_MEM_FLAG_READ_WRITE,

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,10 @@ ProgramManager::collectDeviceImageDepsForImportedSymbols(
689689
throw exception(make_error_code(errc::feature_not_supported),
690690
"Cannot resolve external symbols, linking is unsupported "
691691
"for the backend");
692+
693+
// Access to m_ExportedSymbolImages must be guarded by m_KernelIDsMutex.
694+
std::lock_guard<std::mutex> KernelIDsGuard(m_KernelIDsMutex);
695+
692696
while (!WorkList.empty()) {
693697
std::string Symbol = WorkList.front();
694698
WorkList.pop();

0 commit comments

Comments
 (0)