Skip to content

Commit c0a84ca

Browse files
use validateObject function for memObjs in win api
Change-Id: I1b54096b5a6fdb43a38c25a37560e88218671bd2
1 parent 180de34 commit c0a84ca

File tree

4 files changed

+101
-54
lines changed

4 files changed

+101
-54
lines changed

runtime/helpers/validators.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a
5-
* copy of this software and associated documentation files (the "Software"),
6-
* to deal in the Software without restriction, including without limitation
7-
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8-
* and/or sell copies of the Software, and to permit persons to whom the
9-
* Software is furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included
12-
* in all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18-
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19-
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20-
* OTHER DEALINGS IN THE SOFTWARE.
21-
*/
2+
* Copyright (c) 2017 - 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
2222

2323
#include "runtime/helpers/base_object.h"
2424
#include "runtime/helpers/validators.h"
@@ -118,6 +118,18 @@ cl_int validateObject(const DeviceList &deviceList) {
118118
return CL_SUCCESS;
119119
}
120120

121+
cl_int validateObject(const MemObjList &memObjList) {
122+
if ((!memObjList.first) != (!memObjList.second))
123+
return CL_INVALID_VALUE;
124+
125+
for (cl_uint i = 0; i < memObjList.first; i++) {
126+
if (validateObject(memObjList.second[i]) != CL_SUCCESS)
127+
return CL_INVALID_MEM_OBJECT;
128+
}
129+
130+
return CL_SUCCESS;
131+
}
132+
121133
cl_int validateObject(const NonZeroBufferSize &nzbs) {
122134
return nzbs ? CL_SUCCESS : CL_INVALID_BUFFER_SIZE;
123135
}
@@ -157,4 +169,4 @@ bool IsPackedYuvImage(const cl_image_format *imageFormat) {
157169
bool IsNV12Image(const cl_image_format *imageFormat) {
158170
return imageFormat->image_channel_order == CL_NV12_INTEL;
159171
}
160-
}
172+
} // namespace OCLRT

runtime/helpers/validators.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a
5-
* copy of this software and associated documentation files (the "Software"),
6-
* to deal in the Software without restriction, including without limitation
7-
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8-
* and/or sell copies of the Software, and to permit persons to whom the
9-
* Software is furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included
12-
* in all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18-
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19-
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20-
* OTHER DEALINGS IN THE SOFTWARE.
21-
*/
2+
* Copyright (c) 2017 - 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
2222

2323
#pragma once
2424
#include "runtime/api/cl_types.h"
@@ -31,6 +31,7 @@ namespace OCLRT {
3131
// Provide some aggregators...
3232
typedef std::pair<uint32_t, const cl_event *> EventWaitList;
3333
typedef std::pair<uint32_t, const cl_device_id *> DeviceList;
34+
typedef std::pair<uint32_t, const cl_mem *> MemObjList;
3435

3536
// Custom validators
3637
enum NonZeroBufferSize : size_t;
@@ -62,6 +63,7 @@ cl_int validateObject(cl_program program);
6263
cl_int validateObject(cl_kernel kernel);
6364
cl_int validateObject(const EventWaitList &eventWaitList);
6465
cl_int validateObject(const DeviceList &deviceList);
66+
cl_int validateObject(const MemObjList &memObjList);
6567
cl_int validateObject(const NonZeroBufferSize &nzbs);
6668
cl_int validateObject(const PatternSize &ps);
6769

@@ -98,4 +100,4 @@ bool areNotNullptr(T t, RT... rt) {
98100
cl_int validateYuvOperation(const size_t *origin, const size_t *region);
99101
bool IsPackedYuvImage(const cl_image_format *imageFormat);
100102
bool IsNV12Image(const cl_image_format *imageFormat);
101-
}
103+
} // namespace OCLRT

runtime/os_interface/windows/api.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ cl_int CL_API_CALL clEnqueueAcquireD3D10ObjectsKHR(cl_command_queue commandQueue
343343
return retVal;
344344
}
345345

346+
retVal = validateObjects(MemObjList(numObjects, memObjects));
347+
if (retVal != CL_SUCCESS) {
348+
return retVal;
349+
}
350+
346351
for (unsigned int object = 0; object < numObjects; object++) {
347352
auto memObj = castToObject<MemObj>(memObjects[object]);
348-
if (memObj == nullptr) {
349-
return CL_INVALID_MEM_OBJECT;
350-
}
351353
if (memObj->acquireCount >= 1) {
352354
return CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR;
353355
}
@@ -365,11 +367,13 @@ cl_int CL_API_CALL clEnqueueReleaseD3D10ObjectsKHR(cl_command_queue commandQueue
365367
return retVal;
366368
}
367369

370+
retVal = validateObjects(MemObjList(numObjects, memObjects));
371+
if (retVal != CL_SUCCESS) {
372+
return retVal;
373+
}
374+
368375
for (unsigned int object = 0; object < numObjects; object++) {
369376
auto memObject = castToObject<MemObj>(memObjects[object]);
370-
if (memObject == nullptr) {
371-
return CL_INVALID_MEM_OBJECT;
372-
}
373377
if (memObject->acquireCount == 0) {
374378
return CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR;
375379
}
@@ -504,11 +508,13 @@ cl_int CL_API_CALL clEnqueueAcquireD3D11ObjectsKHR(cl_command_queue commandQueue
504508
return retVal;
505509
}
506510

511+
retVal = validateObjects(MemObjList(numObjects, memObjects));
512+
if (retVal != CL_SUCCESS) {
513+
return retVal;
514+
}
515+
507516
for (unsigned int object = 0; object < numObjects; object++) {
508517
auto memObj = castToObject<MemObj>(memObjects[object]);
509-
if (memObj == nullptr) {
510-
return CL_INVALID_MEM_OBJECT;
511-
}
512518
if (memObj->acquireCount >= 1) {
513519
return CL_D3D11_RESOURCE_ALREADY_ACQUIRED_KHR;
514520
}
@@ -526,11 +532,13 @@ cl_int CL_API_CALL clEnqueueReleaseD3D11ObjectsKHR(cl_command_queue commandQueue
526532
return retVal;
527533
}
528534

535+
retVal = validateObjects(MemObjList(numObjects, memObjects));
536+
if (retVal != CL_SUCCESS) {
537+
return retVal;
538+
}
539+
529540
for (unsigned int object = 0; object < numObjects; object++) {
530541
auto memObject = castToObject<MemObj>(memObjects[object]);
531-
if (memObject == nullptr) {
532-
return CL_INVALID_MEM_OBJECT;
533-
}
534542
if (memObject->acquireCount == 0) {
535543
return CL_D3D11_RESOURCE_NOT_ACQUIRED_KHR;
536544
}

unit_tests/helpers/validator_tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "runtime/helpers/validators.h"
2828
#include "runtime/platform/platform.h"
2929
#include "unit_tests/mocks/mock_context.h"
30+
#include "unit_tests/mocks/mock_buffer.h"
3031
#include "gtest/gtest.h"
3132

3233
using namespace OCLRT;
@@ -134,6 +135,30 @@ TEST(DeviceList, nonZeroCount_noNullPointer) {
134135
EXPECT_EQ(CL_INVALID_DEVICE, validateObjects(DeviceList(1, &devList)));
135136
}
136137

138+
TEST(MemObjList, zeroCount_nonNullPointer) {
139+
cl_mem memList = static_cast<cl_mem>(ptrGarbage);
140+
EXPECT_EQ(CL_INVALID_VALUE, validateObjects(MemObjList(0, &memList)));
141+
}
142+
143+
TEST(MemObjList, zeroCount_nullPointer) {
144+
EXPECT_EQ(CL_SUCCESS, validateObjects(MemObjList(0, nullptr)));
145+
}
146+
147+
TEST(MemObjList, nonZeroCount_nullPointer) {
148+
EXPECT_EQ(CL_INVALID_VALUE, validateObjects(MemObjList(1, nullptr)));
149+
}
150+
151+
TEST(MemObjList, nonZeroCount_noNullPointer) {
152+
cl_mem memList = static_cast<cl_mem>(ptrGarbage);
153+
EXPECT_EQ(CL_INVALID_MEM_OBJECT, validateObjects(MemObjList(1, &memList)));
154+
}
155+
156+
TEST(MemObjList, nonZeroCount_validPointer) {
157+
std::unique_ptr<MockBuffer> buffer(new MockBuffer());
158+
cl_mem memList = static_cast<cl_mem>(buffer.get());
159+
EXPECT_EQ(CL_SUCCESS, validateObjects(MemObjList(1, &memList)));
160+
}
161+
137162
TEST(NonZeroBufferSizeValidator, zero) {
138163
auto bsv = (NonZeroBufferSize)0;
139164
EXPECT_EQ(CL_INVALID_BUFFER_SIZE, validateObjects(bsv));

0 commit comments

Comments
 (0)