Skip to content

Commit 7830be3

Browse files
kgibalaCompute-Runtime-Automation
authored andcommitted
Do not create buffer when size is too big.
Resolves: NEO-3131 Change-Id: Icd37e7bc62719be5956b6a9435ab2fe7e0962c00 Signed-off-by: Krzysztof Gibala <[email protected]>
1 parent b98b51b commit 7830be3

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

runtime/mem_obj/buffer.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,14 @@ void Buffer::validateInputAndCreateBuffer(cl_context &context,
8282
void *hostPtr,
8383
cl_int &retVal,
8484
cl_mem &buffer) {
85-
if (size == 0) {
85+
Context *pContext = nullptr;
86+
retVal = validateObjects(WithCastToInternal(context, &pContext));
87+
if (retVal != CL_SUCCESS) {
88+
return;
89+
}
90+
91+
auto pDevice = pContext->getDevice(0);
92+
if (size == 0 || size > pDevice->getDeviceInfo().maxMemAllocSize) {
8693
retVal = CL_INVALID_BUFFER_SIZE;
8794
return;
8895
}
@@ -99,12 +106,6 @@ void Buffer::validateInputAndCreateBuffer(cl_context &context,
99106
return;
100107
}
101108

102-
Context *pContext = nullptr;
103-
retVal = validateObjects(WithCastToInternal(context, &pContext));
104-
if (retVal != CL_SUCCESS) {
105-
return;
106-
}
107-
108109
// create the buffer
109110
buffer = create(pContext, properties, size, hostPtr, retVal);
110111
}

unit_tests/api/cl_create_buffer_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ TEST_F(clCreateBufferTests, GivenMemWriteOnlyFlagAndMemReadWriteFlagWhenCreating
208208
ASSERT_EQ(CL_INVALID_VALUE, retVal);
209209
}
210210

211+
TEST_F(clCreateBufferTests, GivenBufferSizeOverMaxMemAllocSizeWhenCreatingBufferThenInvalidBufferSizeErrorIsReturned) {
212+
auto pDevice = pContext->getDevice(0);
213+
size_t size = static_cast<size_t>(pDevice->getDeviceInfo().maxMemAllocSize) + 1;
214+
215+
auto buffer = clCreateBuffer(pContext, CL_MEM_ALLOC_HOST_PTR, size, nullptr, &retVal);
216+
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, retVal);
217+
EXPECT_EQ(nullptr, buffer);
218+
}
219+
220+
TEST_F(clCreateBufferTests, GivenBufferSizeOverMaxMemAllocSizeWhenCreateBufferWithPropertiesINTELThenInvalidBufferSizeErrorIsReturned) {
221+
auto pDevice = pContext->getDevice(0);
222+
size_t size = static_cast<size_t>(pDevice->getDeviceInfo().maxMemAllocSize) + 1;
223+
224+
auto buffer = clCreateBufferWithPropertiesINTEL(pContext, nullptr, size, nullptr, &retVal);
225+
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, retVal);
226+
EXPECT_EQ(nullptr, buffer);
227+
}
228+
211229
TEST_F(clCreateBufferTests, GivenNullHostPointerAndMemCopyHostPtrFlagWhenCreatingBufferThenNullIsReturned) {
212230
cl_mem_flags flags = CL_MEM_USE_HOST_PTR;
213231
static const unsigned int bufferSize = 16;

0 commit comments

Comments
 (0)