Skip to content

Commit 2a000d9

Browse files
jbrodmanbader
authored andcommitted
[SYCL][USM] Make host device return nullptr for bad mallocs (huge, 0, etc) (#988)
Host device wasn't properly handling alloc::unknown. Also, aligned allocator wasn't failing when it really should, so check to throw an error. Signed-off-by: James Brodman <[email protected]>
1 parent 8e190d7 commit 2a000d9

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

sycl/include/CL/sycl/detail/aligned_allocator.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ template <typename T> class aligned_allocator {
5353
pointer allocate(size_t Size) {
5454
size_t NumBytes = Size * sizeof(value_type);
5555
NumBytes = ((NumBytes - 1) | (MAlignment - 1)) + 1;
56+
if (Size > NumBytes)
57+
throw std::bad_alloc();
5658

5759
pointer Result = reinterpret_cast<pointer>(
5860
detail::OSUtil::alignedAlloc(MAlignment, NumBytes));

sycl/source/detail/usm/usm_impl.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,21 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
7171
const device &Dev, alloc Kind) {
7272
void *RetVal = nullptr;
7373
if (Ctxt.is_host()) {
74-
if (!Alignment) {
75-
// worst case default
76-
Alignment = 128;
77-
}
78-
79-
aligned_allocator<char> Alloc(Alignment);
80-
try {
81-
RetVal = Alloc.allocate(Size);
82-
} catch (const std::bad_alloc &) {
83-
// Conform with Specification behavior
74+
if (Kind == alloc::unknown) {
8475
RetVal = nullptr;
76+
} else {
77+
if (!Alignment) {
78+
// worst case default
79+
Alignment = 128;
80+
}
81+
82+
aligned_allocator<char> Alloc(Alignment);
83+
try {
84+
RetVal = Alloc.allocate(Size);
85+
} catch (const std::bad_alloc &) {
86+
// Conform with Specification behavior
87+
RetVal = nullptr;
88+
}
8589
}
8690
} else {
8791
std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);

sycl/test/usm/badmalloc.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// RUN: %clangxx -fsycl %s -o %t1.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t1.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t1.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t1.out
5+
6+
// UNSUPPORTED: windows
7+
8+
//==----------------- badmalloc.cpp - Bad Mallocs test ---------------------==//
9+
//
10+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
11+
// See https://llvm.org/LICENSE.txt for license information.
12+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
17+
// This test verifies that things fail in the proper way when they should.
18+
19+
#include <CL/sycl.hpp>
20+
#include <iostream>
21+
22+
using namespace cl::sycl;
23+
24+
int main(int argc, char *argv[]) {
25+
queue q;
26+
27+
// Good size, bad type
28+
auto p = malloc(8, q, usm::alloc::unknown);
29+
if (p != nullptr)
30+
return 1;
31+
32+
// Bad size, host
33+
p = malloc(-1, q, usm::alloc::host);
34+
std::cout << "p = " << p << std::endl;
35+
if (p != nullptr)
36+
return 2;
37+
p = malloc(-1, q, usm::alloc::device);
38+
std::cout << "p = " << p << std::endl;
39+
if (p != nullptr)
40+
return 3;
41+
p = malloc(-1, q, usm::alloc::shared);
42+
std::cout << "p = " << p << std::endl;
43+
if (p != nullptr)
44+
return 4;
45+
p = malloc(-1, q, usm::alloc::unknown);
46+
std::cout << "p = " << p << std::endl;
47+
if (p != nullptr)
48+
return 5;
49+
50+
// Bad size, auto aligned
51+
p = aligned_alloc(0, -1, q, usm::alloc::host);
52+
std::cout << "p = " << p << std::endl;
53+
if (p != nullptr)
54+
return 6;
55+
p = aligned_alloc(0, -1, q, usm::alloc::device);
56+
std::cout << "p = " << p << std::endl;
57+
if (p != nullptr)
58+
return 7;
59+
p = aligned_alloc(0, -1, q, usm::alloc::shared);
60+
std::cout << "p = " << p << std::endl;
61+
if (p != nullptr)
62+
return 8;
63+
p = aligned_alloc(0, -1, q, usm::alloc::unknown);
64+
std::cout << "p = " << p << std::endl;
65+
if (p != nullptr)
66+
return 9;
67+
68+
// Allocs of 0 undefined, but bad type
69+
p = aligned_alloc(4, 0, q, usm::alloc::unknown);
70+
std::cout << "p = " << p << std::endl;
71+
if (p != nullptr)
72+
return 10;
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)