diff --git a/runtime/helpers/validators.cpp b/runtime/helpers/validators.cpp index 98a5b327c2..738db5a9b7 100644 --- a/runtime/helpers/validators.cpp +++ b/runtime/helpers/validators.cpp @@ -1,24 +1,24 @@ /* - * Copyright (c) 2017, Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ +* Copyright (c) 2017 - 2018, Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +*/ #include "runtime/helpers/base_object.h" #include "runtime/helpers/validators.h" @@ -118,6 +118,18 @@ cl_int validateObject(const DeviceList &deviceList) { return CL_SUCCESS; } +cl_int validateObject(const MemObjList &memObjList) { + if ((!memObjList.first) != (!memObjList.second)) + return CL_INVALID_VALUE; + + for (cl_uint i = 0; i < memObjList.first; i++) { + if (validateObject(memObjList.second[i]) != CL_SUCCESS) + return CL_INVALID_MEM_OBJECT; + } + + return CL_SUCCESS; +} + cl_int validateObject(const NonZeroBufferSize &nzbs) { return nzbs ? CL_SUCCESS : CL_INVALID_BUFFER_SIZE; } @@ -157,4 +169,4 @@ bool IsPackedYuvImage(const cl_image_format *imageFormat) { bool IsNV12Image(const cl_image_format *imageFormat) { return imageFormat->image_channel_order == CL_NV12_INTEL; } -} +} // namespace OCLRT diff --git a/runtime/helpers/validators.h b/runtime/helpers/validators.h index 4bb2f9058b..96d1af3a93 100644 --- a/runtime/helpers/validators.h +++ b/runtime/helpers/validators.h @@ -1,24 +1,24 @@ /* - * Copyright (c) 2017, Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ +* Copyright (c) 2017 - 2018, Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +*/ #pragma once #include "runtime/api/cl_types.h" @@ -31,6 +31,7 @@ namespace OCLRT { // Provide some aggregators... typedef std::pair EventWaitList; typedef std::pair DeviceList; +typedef std::pair MemObjList; // Custom validators enum NonZeroBufferSize : size_t; @@ -62,6 +63,7 @@ cl_int validateObject(cl_program program); cl_int validateObject(cl_kernel kernel); cl_int validateObject(const EventWaitList &eventWaitList); cl_int validateObject(const DeviceList &deviceList); +cl_int validateObject(const MemObjList &memObjList); cl_int validateObject(const NonZeroBufferSize &nzbs); cl_int validateObject(const PatternSize &ps); @@ -98,4 +100,4 @@ bool areNotNullptr(T t, RT... rt) { cl_int validateYuvOperation(const size_t *origin, const size_t *region); bool IsPackedYuvImage(const cl_image_format *imageFormat); bool IsNV12Image(const cl_image_format *imageFormat); -} +} // namespace OCLRT diff --git a/runtime/os_interface/windows/api.cpp b/runtime/os_interface/windows/api.cpp index 2580c38c96..399bddc1dd 100644 --- a/runtime/os_interface/windows/api.cpp +++ b/runtime/os_interface/windows/api.cpp @@ -343,11 +343,13 @@ cl_int CL_API_CALL clEnqueueAcquireD3D10ObjectsKHR(cl_command_queue commandQueue return retVal; } + retVal = validateObjects(MemObjList(numObjects, memObjects)); + if (retVal != CL_SUCCESS) { + return retVal; + } + for (unsigned int object = 0; object < numObjects; object++) { auto memObj = castToObject(memObjects[object]); - if (memObj == nullptr) { - return CL_INVALID_MEM_OBJECT; - } if (memObj->acquireCount >= 1) { return CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR; } @@ -365,11 +367,13 @@ cl_int CL_API_CALL clEnqueueReleaseD3D10ObjectsKHR(cl_command_queue commandQueue return retVal; } + retVal = validateObjects(MemObjList(numObjects, memObjects)); + if (retVal != CL_SUCCESS) { + return retVal; + } + for (unsigned int object = 0; object < numObjects; object++) { auto memObject = castToObject(memObjects[object]); - if (memObject == nullptr) { - return CL_INVALID_MEM_OBJECT; - } if (memObject->acquireCount == 0) { return CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR; } @@ -504,11 +508,13 @@ cl_int CL_API_CALL clEnqueueAcquireD3D11ObjectsKHR(cl_command_queue commandQueue return retVal; } + retVal = validateObjects(MemObjList(numObjects, memObjects)); + if (retVal != CL_SUCCESS) { + return retVal; + } + for (unsigned int object = 0; object < numObjects; object++) { auto memObj = castToObject(memObjects[object]); - if (memObj == nullptr) { - return CL_INVALID_MEM_OBJECT; - } if (memObj->acquireCount >= 1) { return CL_D3D11_RESOURCE_ALREADY_ACQUIRED_KHR; } @@ -526,11 +532,13 @@ cl_int CL_API_CALL clEnqueueReleaseD3D11ObjectsKHR(cl_command_queue commandQueue return retVal; } + retVal = validateObjects(MemObjList(numObjects, memObjects)); + if (retVal != CL_SUCCESS) { + return retVal; + } + for (unsigned int object = 0; object < numObjects; object++) { auto memObject = castToObject(memObjects[object]); - if (memObject == nullptr) { - return CL_INVALID_MEM_OBJECT; - } if (memObject->acquireCount == 0) { return CL_D3D11_RESOURCE_NOT_ACQUIRED_KHR; } diff --git a/unit_tests/helpers/validator_tests.cpp b/unit_tests/helpers/validator_tests.cpp index ce019793d8..67552500a2 100644 --- a/unit_tests/helpers/validator_tests.cpp +++ b/unit_tests/helpers/validator_tests.cpp @@ -27,6 +27,7 @@ #include "runtime/helpers/validators.h" #include "runtime/platform/platform.h" #include "unit_tests/mocks/mock_context.h" +#include "unit_tests/mocks/mock_buffer.h" #include "gtest/gtest.h" using namespace OCLRT; @@ -134,6 +135,30 @@ TEST(DeviceList, nonZeroCount_noNullPointer) { EXPECT_EQ(CL_INVALID_DEVICE, validateObjects(DeviceList(1, &devList))); } +TEST(MemObjList, zeroCount_nonNullPointer) { + cl_mem memList = static_cast(ptrGarbage); + EXPECT_EQ(CL_INVALID_VALUE, validateObjects(MemObjList(0, &memList))); +} + +TEST(MemObjList, zeroCount_nullPointer) { + EXPECT_EQ(CL_SUCCESS, validateObjects(MemObjList(0, nullptr))); +} + +TEST(MemObjList, nonZeroCount_nullPointer) { + EXPECT_EQ(CL_INVALID_VALUE, validateObjects(MemObjList(1, nullptr))); +} + +TEST(MemObjList, nonZeroCount_noNullPointer) { + cl_mem memList = static_cast(ptrGarbage); + EXPECT_EQ(CL_INVALID_MEM_OBJECT, validateObjects(MemObjList(1, &memList))); +} + +TEST(MemObjList, nonZeroCount_validPointer) { + std::unique_ptr buffer(new MockBuffer()); + cl_mem memList = static_cast(buffer.get()); + EXPECT_EQ(CL_SUCCESS, validateObjects(MemObjList(1, &memList))); +} + TEST(NonZeroBufferSizeValidator, zero) { auto bsv = (NonZeroBufferSize)0; EXPECT_EQ(CL_INVALID_BUFFER_SIZE, validateObjects(bsv));