Skip to content

Commit 626c4b5

Browse files
authored
[SYCL] Report source code info(file/line number) where null pointer exception occurred in memcpy (#10539)
1 parent 6e4584d commit 626c4b5

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

sycl/source/detail/queue_impl.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,23 @@ event queue_impl::memset(const std::shared_ptr<detail::queue_impl> &Self,
121121
return MDiscardEvents ? createDiscardedEvent() : ResEvent;
122122
}
123123

124+
void report(const code_location &CodeLoc) {
125+
std::cout << "Exception caught at ";
126+
if (CodeLoc.fileName())
127+
std::cout << "File: " << CodeLoc.fileName();
128+
if (CodeLoc.functionName())
129+
std::cout << " | Function: " << CodeLoc.functionName();
130+
if (CodeLoc.lineNumber())
131+
std::cout << " | Line: " << CodeLoc.lineNumber();
132+
if (CodeLoc.columnNumber())
133+
std::cout << " | Column: " << CodeLoc.columnNumber();
134+
std::cout << '\n';
135+
}
136+
124137
event queue_impl::memcpy(const std::shared_ptr<detail::queue_impl> &Self,
125138
void *Dest, const void *Src, size_t Count,
126-
const std::vector<event> &DepEvents) {
139+
const std::vector<event> &DepEvents,
140+
const code_location &CodeLoc) {
127141
#if XPTI_ENABLE_INSTRUMENTATION
128142
// We need a code pointer value and we duse the object ptr; If code location
129143
// is available, we use the source file information along with the object
@@ -155,6 +169,11 @@ event queue_impl::memcpy(const std::shared_ptr<detail::queue_impl> &Self,
155169
},
156170
Self, {});
157171
}
172+
if ((!Src || !Dest) && Count != 0) {
173+
report(CodeLoc);
174+
throw runtime_error("NULL pointer argument in memory copy operation.",
175+
PI_ERROR_INVALID_VALUE);
176+
}
158177
if (MHasDiscardEventsSupport) {
159178
MemoryManager::copy_usm(Src, Self, Count, Dest,
160179
getOrWaitEvents(DepEvents, MContext), nullptr);

sycl/source/detail/queue_impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ class queue_impl {
616616
/// \return an event representing copy operation.
617617
event memcpy(const std::shared_ptr<queue_impl> &Self, void *Dest,
618618
const void *Src, size_t Count,
619-
const std::vector<event> &DepEvents);
619+
const std::vector<event> &DepEvents,
620+
const code_location &CodeLoc);
620621
/// Provides additional information to the underlying runtime about how
621622
/// different allocations are used.
622623
///

sycl/source/queue.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,20 @@ event queue::memset(void *Ptr, int Value, size_t Count,
112112
event queue::memcpy(void *Dest, const void *Src, size_t Count,
113113
const detail::code_location &CodeLoc) {
114114
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
115-
return impl->memcpy(impl, Dest, Src, Count, {});
115+
return impl->memcpy(impl, Dest, Src, Count, {}, CodeLoc);
116116
}
117117

118118
event queue::memcpy(void *Dest, const void *Src, size_t Count, event DepEvent,
119119
const detail::code_location &CodeLoc) {
120120
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
121-
return impl->memcpy(impl, Dest, Src, Count, {DepEvent});
121+
return impl->memcpy(impl, Dest, Src, Count, {DepEvent}, CodeLoc);
122122
}
123123

124124
event queue::memcpy(void *Dest, const void *Src, size_t Count,
125125
const std::vector<event> &DepEvents,
126126
const detail::code_location &CodeLoc) {
127127
detail::tls_code_loc_t TlsCodeLocCapture(CodeLoc);
128-
return impl->memcpy(impl, Dest, Src, Count, DepEvents);
128+
return impl->memcpy(impl, Dest, Src, Count, DepEvents, CodeLoc);
129129
}
130130

131131
event queue::mem_advise(const void *Ptr, size_t Length, pi_mem_advice Advice,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*This test checks that source information where an exception occured is
2+
* reported*/
3+
4+
// RUN: %{build} -o %t.out
5+
// RUN: env ZE_DEBUG=1 %{run} %t.out 2>&1 | FileCheck %s
6+
7+
#include <sycl/sycl.hpp>
8+
using namespace sycl;
9+
#define XFLOAT float
10+
#define mdlXYZ 1000
11+
12+
int main() {
13+
bool failed = true;
14+
XFLOAT *mdlImag;
15+
queue q{{property::queue::enable_profiling()}};
16+
mdlImag = sycl::malloc_device<XFLOAT>(mdlXYZ, q);
17+
try {
18+
q.memcpy(mdlImag, 0, sizeof(XFLOAT));
19+
} catch (...) {
20+
// CHECK: Exception caught at File: {{.*}}report_code_loc.cpp | Function: main | Line: 18 | Column: 5
21+
}
22+
}

0 commit comments

Comments
 (0)