From 1ce3898400ce0574c66ab50d35e9f0921b3a1cb5 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Tue, 8 Jan 2019 11:01:10 +0100 Subject: [PATCH] Improve checkMemory validation. - check that proper flags are passed if hostPtr is presented. - fix a bug in buffer fixture. - fix some bugs in other tests. Change-Id: If708fd06598e5f3d8a94b3e24fb83f689f6b52c7 --- Jenkinsfile | 2 +- runtime/mem_obj/buffer.cpp | 7 +++++++ unit_tests/fixtures/buffer_fixture.h | 22 ++++------------------ unit_tests/mem_obj/buffer_tests.cpp | 17 +++++++++++------ 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index d9e371dc87..cafc9cf24c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,5 @@ #!groovy dependenciesRevision='d765327562b51c0cf3a4702b2f80bf81ca711af1-1180' strategy='EQUAL' -allowedCD=272 +allowedCD=273 allowedF=4 diff --git a/runtime/mem_obj/buffer.cpp b/runtime/mem_obj/buffer.cpp index 15f66a30ce..7c9043b5f5 100644 --- a/runtime/mem_obj/buffer.cpp +++ b/runtime/mem_obj/buffer.cpp @@ -287,6 +287,13 @@ void Buffer::checkMemory(cl_mem_flags flags, minAddress = memRestrictions->minAddress; } + if (hostPtr) { + if (!(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) { + errcodeRet = CL_INVALID_HOST_PTR; + return; + } + } + if (flags & CL_MEM_USE_HOST_PTR) { if (hostPtr) { auto fragment = memoryManager->getHostPtrManager()->getFragment(hostPtr); diff --git a/unit_tests/fixtures/buffer_fixture.h b/unit_tests/fixtures/buffer_fixture.h index 542264501e..8df395ddf1 100644 --- a/unit_tests/fixtures/buffer_fixture.h +++ b/unit_tests/fixtures/buffer_fixture.h @@ -1,23 +1,8 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (C) 2017-2019 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: + * SPDX-License-Identifier: MIT * - * 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 @@ -57,11 +42,12 @@ struct BufferHelper { static Buffer *create(Context *context = Traits::context) { auto retVal = CL_SUCCESS; + auto hostPtr = Traits::flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR) ? Traits::hostPtr : nullptr; auto buffer = Buffer::create( context ? context : std::shared_ptr(new MockContext).get(), Traits::flags, Traits::sizeInBytes, - Traits::hostPtr, + hostPtr, retVal); assert(buffer != nullptr); return buffer; diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index 61c2351d8f..92e62d2305 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -43,6 +43,14 @@ TEST(Buffer, giveBufferWhenAskedForPtrOffsetForMappingThenReturnCorrectValue) { EXPECT_EQ(offset[0], retOffset); } +TEST(Buffer, giveBufferCreateWithHostPtrButWithoutProperFlagsWhenCreatedThenErrorIsReturned) { + MockContext ctx; + cl_int retVal; + auto hostPtr = reinterpret_cast(0x1774); + std::unique_ptr buffer(Buffer::create(&ctx, CL_MEM_READ_WRITE, 1, hostPtr, retVal)); + EXPECT_EQ(retVal, CL_INVALID_HOST_PTR); +} + TEST(Buffer, givenBufferWhenAskedForPtrLengthThenReturnCorrectValue) { MockContext ctx; cl_int retVal; @@ -273,14 +281,11 @@ TEST(Buffer, givenAllocHostPtrFlagPassedToBufferCreateWhenNoSharedContextOrRende cl_int retVal = 0; cl_mem_flags flags = CL_MEM_ALLOC_HOST_PTR; - size_t size = MemoryConstants::pageSize; - void *hostPtr = alignedMalloc(size, MemoryConstants::pageSize); - std::unique_ptr buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, hostPtr, retVal)); + std::unique_ptr buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, nullptr, retVal)); ASSERT_NE(nullptr, buffer.get()); EXPECT_EQ(GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, buffer->getGraphicsAllocation()->getAllocationType()); - alignedFree(hostPtr); } TEST(Buffer, givenRenderCompressedBuffersEnabledWhenAllocationTypeIsQueriedThenBufferCompressedTypeIsReturned) { @@ -448,14 +453,14 @@ TEST_F(RenderCompressedBuffersTests, givenBufferCompressedAllocationWhenSharedCo uint32_t hostPtr = 0; - buffer.reset(Buffer::create(context.get(), 0, sizeof(uint32_t), &hostPtr, retVal)); + buffer.reset(Buffer::create(context.get(), CL_MEM_READ_WRITE, sizeof(uint32_t), nullptr, retVal)); if (is32bit) { EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); } else { EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); } context->isSharedContext = true; - buffer.reset(Buffer::create(context.get(), 0, sizeof(uint32_t), &hostPtr, retVal)); + buffer.reset(Buffer::create(context.get(), CL_MEM_USE_HOST_PTR, sizeof(uint32_t), &hostPtr, retVal)); EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY); }