Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Fix uncaught exceptions and null dereference #15173

Merged
merged 12 commits into from
Sep 4, 2024
27 changes: 18 additions & 9 deletions sycl/source/detail/device_image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ class device_image_impl {

// Otherwise, if the device candidate is a sub-device it is also valid if
// its parent is valid.
if (!getSyclObjImpl(DeviceCand)->isRootDevice())
return has_kernel(KernelIDCand,
DeviceCand.get_info<info::device::parent_device>());

if (!getSyclObjImpl(DeviceCand)->isRootDevice()) {
try {
return has_kernel(KernelIDCand,
DeviceCand.get_info<info::device::parent_device>());
} catch (std::exception &e) {
dm-vodopyanov marked this conversation as resolved.
Show resolved Hide resolved
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in has_kernel", e);
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false;
}

Expand Down Expand Up @@ -259,7 +263,7 @@ class device_image_impl {
return MSpecConstsBlob;
}

ur_mem_handle_t &get_spec_const_buffer_ref() noexcept {
ur_mem_handle_t &get_spec_const_buffer_ref() {
std::lock_guard<std::mutex> Lock{MSpecConstAccessMtx};
if (nullptr == MSpecConstsBuffer && !MSpecConstsBlob.empty()) {
const PluginPtr &Plugin = getSyclObjImpl(MContext)->getPlugin();
Expand All @@ -270,10 +274,15 @@ class device_image_impl {
// TODO consider changing the lifetime of device_image_impl instead
ur_buffer_properties_t Properties = {UR_STRUCTURE_TYPE_BUFFER_PROPERTIES,
nullptr, MSpecConstsBlob.data()};
memBufferCreateHelper(
Plugin, detail::getSyclObjImpl(MContext)->getHandleRef(),
UR_MEM_FLAG_READ_WRITE | UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER,
MSpecConstsBlob.size(), &MSpecConstsBuffer, &Properties);
try {
memBufferCreateHelper(
Plugin, detail::getSyclObjImpl(MContext)->getHandleRef(),
UR_MEM_FLAG_READ_WRITE | UR_MEM_FLAG_ALLOC_COPY_HOST_POINTER,
MSpecConstsBlob.size(), &MSpecConstsBuffer, &Properties);
} catch (std::exception &e) {
__SYCL_REPORT_EXCEPTION_TO_STREAM(
"exception in get_spec_const_buffer_ref", e);
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
}
}
return MSpecConstsBuffer;
}
Expand Down
48 changes: 27 additions & 21 deletions sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,33 +355,39 @@ void shutdown_late() {
extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpReserved) {
bool PrintUrTrace =
sycl::detail::ur::trace(sycl::detail::ur::TraceLevel::TRACE_CALLS);
try {
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
bool PrintUrTrace =
sycl::detail::ur::trace(sycl::detail::ur::TraceLevel::TRACE_CALLS);

// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_DETACH:
if (PrintUrTrace)
std::cout << "---> DLL_PROCESS_DETACH syclx.dll\n" << std::endl;
// Perform actions based on the reason for calling.
switch (fdwReason) {
case DLL_PROCESS_DETACH:
if (PrintUrTrace)
std::cout << "---> DLL_PROCESS_DETACH syclx.dll\n" << std::endl;

#ifdef XPTI_ENABLE_INSTRUMENTATION
if (xptiTraceEnabled())
return TRUE; // When doing xpti tracing, we can't safely call shutdown.
// TODO: figure out what XPTI is doing that prevents release.
if (xptiTraceEnabled())
return TRUE; // When doing xpti tracing, we can't safely call shutdown.
// TODO: figure out what XPTI is doing that prevents
// release.
#endif

shutdown_win();
break;
case DLL_PROCESS_ATTACH:
if (PrintUrTrace)
std::cout << "---> DLL_PROCESS_ATTACH syclx.dll\n" << std::endl;
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
shutdown_win();
break;
case DLL_PROCESS_ATTACH:
if (PrintUrTrace)
std::cout << "---> DLL_PROCESS_ATTACH syclx.dll\n" << std::endl;
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
} catch (std::exception &e) {
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in DllMain", e);
return FALSE;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
#else
// Setting low priority on destructor ensures it runs after all other global
Expand Down
21 changes: 12 additions & 9 deletions sycl/source/detail/graph_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1618,15 +1618,18 @@ void modifiable_command_graph::end_recording() {

void modifiable_command_graph::end_recording(queue &RecordingQueue) {
auto QueueImpl = sycl::detail::getSyclObjImpl(RecordingQueue);
if (QueueImpl && QueueImpl->getCommandGraph() == impl) {
QueueImpl->setCommandGraph(nullptr);
graph_impl::WriteLock Lock(impl->MMutex);
impl->removeQueue(QueueImpl);
}
if (QueueImpl->getCommandGraph() != nullptr) {
throw sycl::exception(sycl::make_error_code(errc::invalid),
"end_recording called for a queue which is recording "
"to a different graph.");
if (QueueImpl) {
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
if (QueueImpl->getCommandGraph() == impl) {
QueueImpl->setCommandGraph(nullptr);
graph_impl::WriteLock Lock(impl->MMutex);
impl->removeQueue(QueueImpl);
}
if (QueueImpl->getCommandGraph() != nullptr) {
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
throw sycl::exception(
sycl::make_error_code(errc::invalid),
"end_recording called for a queue which is recording "
"to a different graph.");
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion sycl/source/detail/image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,14 @@ class image_impl final : public SYCLMemObjT {

// Returns the total number of elements in the image
size_t get_count() const { return size(); }
size_t size() const noexcept { return MRange.size(); }
size_t size() const {
try {
return MRange.size();
} catch (std::exception &e) {
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in size", e);
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
std::abort();
}
}
bso-intel marked this conversation as resolved.
Show resolved Hide resolved

void *allocateMem(ContextImplPtr Context, bool InitFromUserData,
void *HostPtr, ur_event_handle_t &OutEventToWait) override;
Expand Down
9 changes: 8 additions & 1 deletion sycl/source/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ event::get_profiling_info() const {

#undef __SYCL_PARAM_TRAITS_SPEC

backend event::get_backend() const noexcept { return getImplBackend(impl); }
backend event::get_backend() const noexcept {
try {
return getImplBackend(impl);
} catch (std::exception &e) {
__SYCL_REPORT_EXCEPTION_TO_STREAM("exception in get_backend", e);
bso-intel marked this conversation as resolved.
Show resolved Hide resolved
std::abort();
}
}
bso-intel marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
Loading