From d69f23dc107ded0f1e5953e615f0ad676c78b864 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Tue, 10 Jul 2018 10:27:12 +0200 Subject: [PATCH] Enhance validation of properties when command queue is created. Change-Id: I1735ef8d9fbc17a09b9711f8f881c27c5de6b82c --- runtime/api/api.cpp | 32 +++++++++++++------ ...te_command_queue_with_properties_tests.cpp | 24 ++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/runtime/api/api.cpp b/runtime/api/api.cpp index b41b2dddd6..d0f768f63f 100644 --- a/runtime/api/api.cpp +++ b/runtime/api/api.cpp @@ -3507,23 +3507,38 @@ cl_command_queue CL_API_CALL clCreateCommandQueueWithProperties(cl_context conte auto minimumCreateDeviceQueueFlags = static_cast(CL_QUEUE_ON_DEVICE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE); + auto tokenValue = properties ? *properties : 0; + auto propertiesAddress = properties; + while (tokenValue != 0) { + if (tokenValue != CL_QUEUE_PROPERTIES && + tokenValue != CL_QUEUE_SIZE && + tokenValue != CL_QUEUE_PRIORITY_KHR && + tokenValue != CL_QUEUE_THROTTLE_KHR) { + err.set(CL_INVALID_VALUE); + return commandQueue; + } + propertiesAddress += 2; + tokenValue = *propertiesAddress; + } + + auto commandQueueProperties = getCmdQueueProperties(properties); uint32_t maxOnDeviceQueueSize = pDevice->getDeviceInfo().queueOnDeviceMaxSize; uint32_t maxOnDeviceQueues = pDevice->getDeviceInfo().maxOnDeviceQueues; - if (getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE)) { - if (!(getCmdQueueProperties(properties) & static_cast(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE))) { + if (commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE)) { + if (!(commandQueueProperties & static_cast(CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE))) { err.set(CL_INVALID_VALUE); return commandQueue; } } - if (getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE_DEFAULT)) { - if (!(getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE))) { + if (commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE_DEFAULT)) { + if (!(commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE))) { err.set(CL_INVALID_VALUE); return commandQueue; } - } else if (getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE)) { + } else if (commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE)) { if ((maxOnDeviceQueues == 0) || ((maxOnDeviceQueues == 1) && pContext->getDefaultDeviceQueue())) { err.set(CL_OUT_OF_RESOURCES); return commandQueue; @@ -3535,22 +3550,21 @@ cl_command_queue CL_API_CALL clCreateCommandQueueWithProperties(cl_context conte return commandQueue; } - if (getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE)) { + if (commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE)) { if (getCmdQueueProperties(properties, CL_QUEUE_PRIORITY_KHR)) { err.set(CL_INVALID_QUEUE_PROPERTIES); return commandQueue; } } - if (getCmdQueueProperties(properties) & static_cast(CL_QUEUE_ON_DEVICE)) { + if (commandQueueProperties & static_cast(CL_QUEUE_ON_DEVICE)) { if (getCmdQueueProperties(properties, CL_QUEUE_THROTTLE_KHR)) { err.set(CL_INVALID_QUEUE_PROPERTIES); return commandQueue; } } - auto maskedFlags = getCmdQueueProperties(properties) & - minimumCreateDeviceQueueFlags; + auto maskedFlags = commandQueueProperties & minimumCreateDeviceQueueFlags; if (maskedFlags == minimumCreateDeviceQueueFlags) { commandQueue = DeviceQueue::create( diff --git a/unit_tests/api/cl_create_command_queue_with_properties_tests.cpp b/unit_tests/api/cl_create_command_queue_with_properties_tests.cpp index 540ab10081..f405e8fe44 100644 --- a/unit_tests/api/cl_create_command_queue_with_properties_tests.cpp +++ b/unit_tests/api/cl_create_command_queue_with_properties_tests.cpp @@ -332,6 +332,30 @@ TEST_F(clCreateCommandQueueWithPropertiesApi, returnErrorOnDeviceWithMedPriority EXPECT_EQ(retVal, CL_INVALID_QUEUE_PROPERTIES); } +TEST_F(clCreateCommandQueueWithPropertiesApi, givenInvalidPropertiesWhenQueueIsCreatedThenReturnError) { + cl_int retVal = CL_SUCCESS; + cl_queue_properties properties = CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE; + auto commandQueue = clCreateCommandQueueWithProperties(pContext, devices[0], &properties, &retVal); + EXPECT_EQ(nullptr, commandQueue); + EXPECT_EQ(retVal, CL_INVALID_VALUE); +} + +TEST_F(clCreateCommandQueueWithPropertiesApi, givenInvalidPropertiesOnSubsequentTokenWhenQueueIsCreatedThenReturnError) { + cl_int retVal = CL_SUCCESS; + cl_queue_properties properties[] = {CL_QUEUE_PROPERTIES, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, CL_DEVICE_PARTITION_EQUALLY, CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, 0}; + auto commandQueue = clCreateCommandQueueWithProperties(pContext, devices[0], properties, &retVal); + EXPECT_EQ(nullptr, commandQueue); + EXPECT_EQ(retVal, CL_INVALID_VALUE); +} + +TEST_F(clCreateCommandQueueWithPropertiesApi, givenNullPropertiesWhenQueueIsCreatedThenReturnSuccess) { + cl_int retVal = CL_SUCCESS; + auto commandQueue = clCreateCommandQueueWithProperties(pContext, devices[0], nullptr, &retVal); + EXPECT_NE(nullptr, commandQueue); + EXPECT_EQ(retVal, CL_SUCCESS); + clReleaseCommandQueue(commandQueue); +} + TEST_F(clCreateCommandQueueWithPropertiesApi, returnSuccessOnQueueWithPriority) { cl_int retVal = CL_SUCCESS; cl_queue_properties ondevice[] = {CL_QUEUE_PRIORITY_KHR, CL_QUEUE_PRIORITY_LOW_KHR, 0};