Skip to content

Commit 239831f

Browse files
fix: return error for 0 size usm allocations
according to both level zero and opencl specs, usm allocations with size=0 should return invalid/unsupported buffer size errors Signed-off-by: Dominik Dabek <[email protected]>
1 parent 08f7e7b commit 239831f

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

level_zero/core/source/context/context_imp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc,
8282
}
8383
}
8484

85-
if (relaxedSizeAllowed == false &&
86-
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize)) {
85+
if (size == 0 || (relaxedSizeAllowed == false &&
86+
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize))) {
8787
*ptr = nullptr;
8888
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
8989
}
@@ -164,8 +164,8 @@ ze_result_t ContextImp::checkMemSizeLimit(Device *inDevice, size_t size, bool re
164164
if (inDevice->isImplicitScalingCapable()) {
165165
enabledSubDeviceCount = static_cast<uint32_t>(neoDevice->getDeviceBitfield().count());
166166
}
167-
if (relaxedSizeAllowed == false &&
168-
(size > neoDevice->getDeviceInfo().maxMemAllocSize)) {
167+
if (size == 0 || (relaxedSizeAllowed == false &&
168+
(size > neoDevice->getDeviceInfo().maxMemAllocSize))) {
169169
*ptr = nullptr;
170170
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
171171
}

level_zero/core/test/unit_tests/sources/memory/test_memory.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,7 @@ TEST_F(MemoryRelaxedSizeTests,
24692469
}
24702470

24712471
TEST_F(MemoryRelaxedSizeTests,
2472-
givenCallToHostAllocWithLargerThanAllowedSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
2472+
givenCallToHostAllocWithLargerThanAllowedSizeOrZeroSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
24732473
size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1;
24742474
size_t alignment = 1u;
24752475
void *ptr = nullptr;
@@ -2479,6 +2479,11 @@ TEST_F(MemoryRelaxedSizeTests,
24792479
size, alignment, &ptr);
24802480
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
24812481
EXPECT_EQ(nullptr, ptr);
2482+
2483+
result = context->allocHostMem(&hostDesc,
2484+
0u, alignment, &ptr);
2485+
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
2486+
EXPECT_EQ(nullptr, ptr);
24822487
}
24832488

24842489
TEST_F(MemoryRelaxedSizeTests,
@@ -2575,7 +2580,7 @@ TEST_F(MemoryRelaxedSizeTests,
25752580
}
25762581

25772582
TEST_F(MemoryRelaxedSizeTests,
2578-
givenCallToDeviceAllocWithLargerThanAllowedSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
2583+
givenCallToDeviceAllocWithLargerThanAllowedSizeOrZeroSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
25792584
size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1;
25802585
size_t alignment = 1u;
25812586
void *ptr = nullptr;
@@ -2586,6 +2591,12 @@ TEST_F(MemoryRelaxedSizeTests,
25862591
size, alignment, &ptr);
25872592
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
25882593
EXPECT_EQ(nullptr, ptr);
2594+
2595+
result = context->allocDeviceMem(device->toHandle(),
2596+
&deviceDesc,
2597+
0u, alignment, &ptr);
2598+
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
2599+
EXPECT_EQ(nullptr, ptr);
25892600
}
25902601

25912602
TEST_F(MemoryRelaxedSizeTests,
@@ -2805,7 +2816,7 @@ TEST_F(MemoryRelaxedSizeTests,
28052816
}
28062817

28072818
TEST_F(MemoryRelaxedSizeTests,
2808-
givenCallToSharedAllocWithLargerThanAllowedSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
2819+
givenCallToSharedAllocWithLargerThanAllowedSizeOrZeroSizeAndWithoutRelaxedFlagThenAllocationIsNotMade) {
28092820
size_t size = device->getNEODevice()->getDeviceInfo().maxMemAllocSize + 1;
28102821
size_t alignment = 1u;
28112822
void *ptr = nullptr;
@@ -2818,6 +2829,13 @@ TEST_F(MemoryRelaxedSizeTests,
28182829
size, alignment, &ptr);
28192830
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
28202831
EXPECT_EQ(nullptr, ptr);
2832+
2833+
result = context->allocSharedMem(device->toHandle(),
2834+
&deviceDesc,
2835+
&hostDesc,
2836+
0u, alignment, &ptr);
2837+
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_SIZE, result);
2838+
EXPECT_EQ(nullptr, ptr);
28212839
}
28222840

28232841
TEST_F(MemoryRelaxedSizeTests,

opencl/source/api/api.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,7 +3897,8 @@ CL_API_ENTRY void *CL_API_CALL clHostMemAllocINTEL(
38973897
return nullptr;
38983898
}
38993899

3900-
if (size > neoContext->getDevice(0u)->getSharedDeviceInfo().maxMemAllocSize && !unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) {
3900+
if (size == 0 || (size > neoContext->getDevice(0u)->getSharedDeviceInfo().maxMemAllocSize &&
3901+
!unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize)) {
39013902
err.set(CL_INVALID_BUFFER_SIZE);
39023903
return nullptr;
39033904
}
@@ -3942,8 +3943,9 @@ CL_API_ENTRY void *CL_API_CALL clDeviceMemAllocINTEL(
39423943
return nullptr;
39433944
}
39443945

3945-
if (size > neoDevice->getDevice().getDeviceInfo().maxMemAllocSize &&
3946-
!unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) {
3946+
if (size == 0 ||
3947+
(size > neoDevice->getDevice().getDeviceInfo().maxMemAllocSize &&
3948+
!unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize)) {
39473949
err.set(CL_INVALID_BUFFER_SIZE);
39483950
return nullptr;
39493951
}
@@ -3999,7 +4001,9 @@ CL_API_ENTRY void *CL_API_CALL clSharedMemAllocINTEL(
39994001
return nullptr;
40004002
}
40014003

4002-
if (size > neoDevice->getSharedDeviceInfo().maxMemAllocSize && !unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) {
4004+
if (size == 0 ||
4005+
(size > neoDevice->getSharedDeviceInfo().maxMemAllocSize &&
4006+
!unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize)) {
40034007
err.set(CL_INVALID_BUFFER_SIZE);
40044008
return nullptr;
40054009
}

opencl/test/unit_test/api/cl_unified_shared_memory_tests.inl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ TEST(clUnifiedSharedMemoryTests, whenClHostMemAllocINTELisCalledWithoutContextTh
2525
EXPECT_EQ(CL_INVALID_CONTEXT, retVal);
2626
}
2727

28+
TEST(clUnifiedSharedMemoryTests, whenClHostMemAllocIntelIsCalledWithSizeZeroThenInvalidBufferSizeIsReturned) {
29+
MockContext mockContext;
30+
auto device = mockContext.getDevice(0u);
31+
REQUIRE_SVM_OR_SKIP(device);
32+
33+
cl_int retVal = CL_SUCCESS;
34+
auto unifiedMemoryHostAllocation = clHostMemAllocINTEL(&mockContext, nullptr, 0u, 0, &retVal);
35+
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, retVal);
36+
EXPECT_EQ(nullptr, unifiedMemoryHostAllocation);
37+
}
38+
2839
TEST(clUnifiedSharedMemoryTests, whenClHostMemAllocIntelIsCalledThenItAllocatesHostUnifiedMemoryAllocation) {
2940
MockContext mockContext;
3041
auto device = mockContext.getDevice(0u);
@@ -128,6 +139,17 @@ TEST(clUnifiedSharedMemoryTests, whenClDeviceMemAllocINTELisCalledWithWrongConte
128139
EXPECT_EQ(CL_INVALID_CONTEXT, retVal);
129140
}
130141

142+
TEST(clUnifiedSharedMemoryTests, whenClDeviceMemAllocIntelIsCalledWithSizeZeroThenItInvalidBufferSizeIsReturned) {
143+
MockContext mockContext;
144+
auto device = mockContext.getDevice(0u);
145+
REQUIRE_SVM_OR_SKIP(device);
146+
147+
cl_int retVal = CL_SUCCESS;
148+
auto unifiedMemoryDeviceAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 0u, 0, &retVal);
149+
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, retVal);
150+
EXPECT_EQ(nullptr, unifiedMemoryDeviceAllocation);
151+
}
152+
131153
TEST(clUnifiedSharedMemoryTests, whenClDeviceMemAllocIntelIsCalledThenItAllocatesDeviceUnifiedMemoryAllocation) {
132154
MockContext mockContext;
133155
cl_int retVal = CL_SUCCESS;
@@ -186,6 +208,17 @@ TEST(clUnifiedSharedMemoryTests, whenClSharedMemAllocINTELisCalledWithWrongConte
186208
EXPECT_EQ(CL_INVALID_CONTEXT, retVal);
187209
}
188210

211+
TEST(clUnifiedSharedMemoryTests, whenClSharedMemAllocIntelIsCalledWithSizeZeroThenInvalidBufferSizeIsReturned) {
212+
MockContext mockContext;
213+
auto device = mockContext.getDevice(0u);
214+
REQUIRE_SVM_OR_SKIP(device);
215+
216+
cl_int retVal = CL_SUCCESS;
217+
auto unifiedMemorySharedAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), nullptr, 0u, 0, &retVal);
218+
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, retVal);
219+
EXPECT_EQ(nullptr, unifiedMemorySharedAllocation);
220+
}
221+
189222
TEST(clUnifiedSharedMemoryTests, whenClSharedMemAllocINTELisCalledWithWrongDeviceThenInvalidDeviceErrorIsReturned) {
190223
cl_int retVal = CL_SUCCESS;
191224
MockContext context0;
@@ -1145,7 +1178,7 @@ TEST(clUnifiedSharedMemoryTests, givenInvalidMemPropertiesWhenClSharedMemAllocIn
11451178
EXPECT_EQ(nullptr, unifiedMemorySharedAllocation);
11461179
}
11471180

1148-
TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeAndClMemAllowUnrestrictedSizeFlagWhenCreateAllocationThenSuccesIsReturned) {
1181+
TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeAndClMemAllowUnrestrictedSizeFlagWhenCreateAllocationThenSuccessIsReturned) {
11491182
MockContext mockContext;
11501183
cl_int retVal = CL_SUCCESS;
11511184
cl_mem_properties_intel properties[] = {CL_MEM_FLAGS, CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL, 0};
@@ -1188,7 +1221,7 @@ TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxM
11881221
}
11891222
}
11901223

1191-
TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeAndDebugFlagSetWhenCreateAllocationThenSuccesIsReturned) {
1224+
TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeAndDebugFlagSetWhenCreateAllocationThenSuccessIsReturned) {
11921225
DebugManagerStateRestore restorer;
11931226
debugManager.flags.AllowUnrestrictedSize.set(1);
11941227
MockContext mockContext;

opencl/test/unit_test/command_queue/enqueue_svm_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ TEST_F(EnqueueSvmTest, GivenNullSvmPtrWhenFillingMemoryThenInvalidValueErrorIsRe
766766
EXPECT_EQ(CL_INVALID_VALUE, retVal);
767767
}
768768

769-
HWTEST_F(EnqueueSvmTest, givenSvmAllocWhenEnqueueSvmFillThenSuccesIsReturnedAndAddressIsProperlyAligned) {
769+
HWTEST_F(EnqueueSvmTest, givenSvmAllocWhenEnqueueSvmFillThenSuccessIsReturnedAndAddressIsProperlyAligned) {
770770
const float pattern[1] = {1.2345f};
771771
const size_t patternSize = sizeof(pattern);
772772
MockCommandQueueHw<FamilyType> myCmdQ(context, pClDevice, 0);

shared/test/unit_test/compiler_interface/compiler_interface_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ TEST_F(CompilerInterfaceTest, whenIgcTranlationContextCreationFailsThenErrorIsRe
12361236
EXPECT_EQ(TranslationOutput::ErrorCode::unknownError, err);
12371237
}
12381238

1239-
TEST_F(CompilerInterfaceTest, givenCompilerInterfaceWhenGetSpecializationConstantsThenSuccesIsReturned) {
1239+
TEST_F(CompilerInterfaceTest, givenCompilerInterfaceWhenGetSpecializationConstantsThenSuccessIsReturned) {
12401240
TranslationOutput translationOutput;
12411241
NEO::SpecConstantInfo specConstInfo;
12421242
auto err = pCompilerInterface->getSpecConstantsInfo(*pDevice, inputArgs.src, specConstInfo);

0 commit comments

Comments
 (0)