Skip to content

Commit 7247207

Browse files
Added arg checking to functions in dpctl_sycl_usm_interface.cpp
1 parent 8fed78b commit 7247207

File tree

1 file changed

+93
-12
lines changed

1 file changed

+93
-12
lines changed

dpctl-capi/source/dpctl_sycl_usm_interface.cpp

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,48 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPCTLSyclUSMRef)
4444
__dpctl_give DPCTLSyclUSMRef
4545
DPCTLmalloc_shared(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
4646
{
47-
auto Q = unwrap(QRef);
48-
auto Ptr = malloc_shared(size, *Q);
49-
return wrap(Ptr);
47+
if (!QRef) {
48+
std::cerr << "Input QRef is nullptr\n";
49+
return nullptr;
50+
}
51+
try {
52+
auto Q = unwrap(QRef);
53+
auto Ptr = malloc_shared(size, *Q);
54+
return wrap(Ptr);
55+
} catch (feature_not_supported const &fns) {
56+
std::cerr << fns.what() << '\n';
57+
return nullptr;
58+
}
5059
}
5160

5261
__dpctl_give DPCTLSyclUSMRef
5362
DPCTLaligned_alloc_shared(size_t alignment,
5463
size_t size,
5564
__dpctl_keep const DPCTLSyclQueueRef QRef)
5665
{
57-
auto Q = unwrap(QRef);
58-
auto Ptr = aligned_alloc_shared(alignment, size, *Q);
59-
return wrap(Ptr);
66+
if (!QRef) {
67+
std::cerr << "Input QRef is nullptr\n";
68+
return nullptr;
69+
}
70+
try {
71+
auto Q = unwrap(QRef);
72+
auto Ptr = aligned_alloc_shared(alignment, size, *Q);
73+
return wrap(Ptr);
74+
} catch (feature_not_supported const &fns) {
75+
std::cerr << fns.what() << '\n';
76+
return nullptr;
77+
}
6078
}
6179

6280
__dpctl_give DPCTLSyclUSMRef
6381
DPCTLmalloc_host(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
6482
{
83+
if (!QRef) {
84+
std::cerr << "Input QRef is nullptr\n";
85+
return nullptr;
86+
}
87+
// SYCL 2020 spec: for devices without aspect::usm_host_allocations:
88+
// undefined behavior
6589
auto Q = unwrap(QRef);
6690
auto Ptr = malloc_host(size, *Q);
6791
return wrap(Ptr);
@@ -72,6 +96,12 @@ DPCTLaligned_alloc_host(size_t alignment,
7296
size_t size,
7397
__dpctl_keep const DPCTLSyclQueueRef QRef)
7498
{
99+
if (!QRef) {
100+
std::cerr << "Input QRef is nullptr\n";
101+
return nullptr;
102+
}
103+
// SYCL 2020 spec: for devices without aspect::usm_host_allocations:
104+
// undefined behavior
75105
auto Q = unwrap(QRef);
76106
auto Ptr = aligned_alloc_host(alignment, size, *Q);
77107
return wrap(Ptr);
@@ -80,24 +110,50 @@ DPCTLaligned_alloc_host(size_t alignment,
80110
__dpctl_give DPCTLSyclUSMRef
81111
DPCTLmalloc_device(size_t size, __dpctl_keep const DPCTLSyclQueueRef QRef)
82112
{
83-
auto Q = unwrap(QRef);
84-
auto Ptr = malloc_device(size, *Q);
85-
return wrap(Ptr);
113+
if (!QRef) {
114+
std::cerr << "Input QRef is nullptr\n";
115+
return nullptr;
116+
}
117+
try {
118+
auto Q = unwrap(QRef);
119+
auto Ptr = malloc_device(size, *Q);
120+
return wrap(Ptr);
121+
} catch (feature_not_supported const &fns) {
122+
std::cerr << fns.what() << '\n';
123+
return nullptr;
124+
}
86125
}
87126

88127
__dpctl_give DPCTLSyclUSMRef
89128
DPCTLaligned_alloc_device(size_t alignment,
90129
size_t size,
91130
__dpctl_keep const DPCTLSyclQueueRef QRef)
92131
{
93-
auto Q = unwrap(QRef);
94-
auto Ptr = aligned_alloc_device(alignment, size, *Q);
95-
return wrap(Ptr);
132+
if (!QRef) {
133+
std::cerr << "Input QRef is nullptr\n";
134+
return nullptr;
135+
}
136+
try {
137+
auto Q = unwrap(QRef);
138+
auto Ptr = aligned_alloc_device(alignment, size, *Q);
139+
return wrap(Ptr);
140+
} catch (feature_not_supported const &fns) {
141+
std::cerr << fns.what() << '\n';
142+
return nullptr;
143+
}
96144
}
97145

98146
void DPCTLfree_with_queue(__dpctl_take DPCTLSyclUSMRef MRef,
99147
__dpctl_keep const DPCTLSyclQueueRef QRef)
100148
{
149+
if (!QRef) {
150+
std::cerr << "Input QRef is nullptr\n";
151+
return;
152+
}
153+
if (!MRef) {
154+
std::cerr << "Input MRef is nullptr, nothing to free\n";
155+
return;
156+
}
101157
auto Ptr = unwrap(MRef);
102158
auto Q = unwrap(QRef);
103159
free(Ptr, *Q);
@@ -106,6 +162,14 @@ void DPCTLfree_with_queue(__dpctl_take DPCTLSyclUSMRef MRef,
106162
void DPCTLfree_with_context(__dpctl_take DPCTLSyclUSMRef MRef,
107163
__dpctl_keep const DPCTLSyclContextRef CRef)
108164
{
165+
if (!CRef) {
166+
std::cerr << "Input CRef is nullptr\n";
167+
return;
168+
}
169+
if (!MRef) {
170+
std::cerr << "Input MRef is nullptr, nothing to free\n";
171+
return;
172+
}
109173
auto Ptr = unwrap(MRef);
110174
auto C = unwrap(CRef);
111175
free(Ptr, *C);
@@ -114,6 +178,14 @@ void DPCTLfree_with_context(__dpctl_take DPCTLSyclUSMRef MRef,
114178
const char *DPCTLUSM_GetPointerType(__dpctl_keep const DPCTLSyclUSMRef MRef,
115179
__dpctl_keep const DPCTLSyclContextRef CRef)
116180
{
181+
if (!CRef) {
182+
std::cerr << "Input CRef is nullptr\n";
183+
return "unknown";
184+
}
185+
if (!MRef) {
186+
std::cerr << "Input MRef is nullptr\n";
187+
return "unknown";
188+
}
117189
auto Ptr = unwrap(MRef);
118190
auto C = unwrap(CRef);
119191

@@ -134,6 +206,15 @@ DPCTLSyclDeviceRef
134206
DPCTLUSM_GetPointerDevice(__dpctl_keep const DPCTLSyclUSMRef MRef,
135207
__dpctl_keep const DPCTLSyclContextRef CRef)
136208
{
209+
if (!CRef) {
210+
std::cerr << "Input CRef is nullptr\n";
211+
return nullptr;
212+
}
213+
if (!MRef) {
214+
std::cerr << "Input MRef is nullptr\n";
215+
return nullptr;
216+
}
217+
137218
auto Ptr = unwrap(MRef);
138219
auto C = unwrap(CRef);
139220

0 commit comments

Comments
 (0)