Skip to content

Commit fad405c

Browse files
[SYCL] Fix uncaught exceptions and null dereference (#15173)
Added proper handling of exceptions propagated to the outermost level. --------- Co-authored-by: Dmitry Vodopyanov <[email protected]>
1 parent 45b2ce3 commit fad405c

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

sycl/source/detail/device_image_impl.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ class device_image_impl {
9595

9696
// Otherwise, if the device candidate is a sub-device it is also valid if
9797
// its parent is valid.
98-
if (!getSyclObjImpl(DeviceCand)->isRootDevice())
99-
return has_kernel(KernelIDCand,
100-
DeviceCand.get_info<info::device::parent_device>());
101-
98+
if (!getSyclObjImpl(DeviceCand)->isRootDevice()) {
99+
try {
100+
return has_kernel(KernelIDCand,
101+
DeviceCand.get_info<info::device::parent_device>());
102+
} catch (std::exception &e) {
103+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in has_kernel", e);
104+
}
105+
}
102106
return false;
103107
}
104108

@@ -270,10 +274,15 @@ class device_image_impl {
270274
// TODO consider changing the lifetime of device_image_impl instead
271275
ur_buffer_properties_t Properties = {UR_STRUCTURE_TYPE_BUFFER_PROPERTIES,
272276
nullptr, MSpecConstsBlob.data()};
273-
memBufferCreateHelper(
274-
Plugin, detail::getSyclObjImpl(MContext)->getHandleRef(),
275-
UR_MEM_FLAG_READ_WRITE | UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER,
276-
MSpecConstsBlob.size(), &MSpecConstsBuffer, &Properties);
277+
try {
278+
memBufferCreateHelper(
279+
Plugin, detail::getSyclObjImpl(MContext)->getHandleRef(),
280+
UR_MEM_FLAG_READ_WRITE | UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER,
281+
MSpecConstsBlob.size(), &MSpecConstsBuffer, &Properties);
282+
} catch (std::exception &e) {
283+
__SYCL_REPORT_EXCEPTION_TO_STREAM(
284+
"exception in get_spec_const_buffer_ref", e);
285+
}
277286
}
278287
return MSpecConstsBuffer;
279288
}

sycl/source/detail/global_handler.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,14 @@ void shutdown_late() {
355355
extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
356356
DWORD fdwReason,
357357
LPVOID lpReserved) {
358-
bool PrintUrTrace =
359-
sycl::detail::ur::trace(sycl::detail::ur::TraceLevel::TRACE_CALLS);
358+
bool PrintUrTrace = false;
359+
try {
360+
PrintUrTrace =
361+
sycl::detail::ur::trace(sycl::detail::ur::TraceLevel::TRACE_CALLS);
362+
} catch (std::exception &e) {
363+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in DllMain", e);
364+
return FALSE;
365+
}
360366

361367
// Perform actions based on the reason for calling.
362368
switch (fdwReason) {
@@ -367,7 +373,8 @@ extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
367373
#ifdef XPTI_ENABLE_INSTRUMENTATION
368374
if (xptiTraceEnabled())
369375
return TRUE; // When doing xpti tracing, we can't safely call shutdown.
370-
// TODO: figure out what XPTI is doing that prevents release.
376+
// TODO: figure out what XPTI is doing that prevents
377+
// release.
371378
#endif
372379

373380
shutdown_win();

sycl/source/detail/graph_impl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,16 +1625,17 @@ void modifiable_command_graph::end_recording() {
16251625

16261626
void modifiable_command_graph::end_recording(queue &RecordingQueue) {
16271627
auto QueueImpl = sycl::detail::getSyclObjImpl(RecordingQueue);
1628-
if (QueueImpl && QueueImpl->getCommandGraph() == impl) {
1628+
if (!QueueImpl)
1629+
return;
1630+
if (QueueImpl->getCommandGraph() == impl) {
16291631
QueueImpl->setCommandGraph(nullptr);
16301632
graph_impl::WriteLock Lock(impl->MMutex);
16311633
impl->removeQueue(QueueImpl);
16321634
}
1633-
if (QueueImpl->getCommandGraph() != nullptr) {
1635+
if (QueueImpl->getCommandGraph() != nullptr)
16341636
throw sycl::exception(sycl::make_error_code(errc::invalid),
16351637
"end_recording called for a queue which is recording "
16361638
"to a different graph.");
1637-
}
16381639
}
16391640

16401641
void modifiable_command_graph::end_recording(

sycl/source/detail/image_impl.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ class image_impl final : public SYCLMemObjT {
238238

239239
// Returns the total number of elements in the image
240240
size_t get_count() const { return size(); }
241-
size_t size() const noexcept { return MRange.size(); }
241+
size_t size() const noexcept try {
242+
return MRange.size();
243+
} catch (std::exception &e) {
244+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in size", e);
245+
std::abort();
246+
}
242247

243248
void *allocateMem(ContextImplPtr Context, bool InitFromUserData,
244249
void *HostPtr, ur_event_handle_t &OutEventToWait) override;

sycl/source/event.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ event::get_profiling_info() const {
117117

118118
#undef __SYCL_PARAM_TRAITS_SPEC
119119

120-
backend event::get_backend() const noexcept { return getImplBackend(impl); }
120+
backend event::get_backend() const noexcept try {
121+
return getImplBackend(impl);
122+
} catch (std::exception &e) {
123+
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in get_backend", e);
124+
std::abort();
125+
}
121126

122127
ur_native_handle_t event::getNative() const { return impl->getNative(); }
123128

0 commit comments

Comments
 (0)