From 6c521a7936fa5cc55b55cdf6f0bc94318c384c60 Mon Sep 17 00:00:00 2001 From: Kamil Diedrich Date: Tue, 14 Aug 2018 08:52:52 +0200 Subject: [PATCH] Mock method for reading elf binary file Change-Id: Ifda051bf93fca6b63c6e99ab9e91740c5049504c --- .../api/cl_get_program_build_info_tests.inl | 22 ++++------- unit_tests/elflib/elf_binary_simulator.h | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 unit_tests/elflib/elf_binary_simulator.h diff --git a/unit_tests/api/cl_get_program_build_info_tests.inl b/unit_tests/api/cl_get_program_build_info_tests.inl index 70abedb582..3d3ccbb542 100644 --- a/unit_tests/api/cl_get_program_build_info_tests.inl +++ b/unit_tests/api/cl_get_program_build_info_tests.inl @@ -26,6 +26,7 @@ #include "runtime/compiler_interface/compiler_interface.h" #include "runtime/helpers/file_io.h" #include "runtime/helpers/options.h" +#include "unit_tests/elflib/elf_binary_simulator.h" #include "unit_tests/helpers/test_files.h" #include "unit_tests/helpers/kernel_binary_helper.h" @@ -113,28 +114,21 @@ TEST_F(clGetProgramBuildInfoTests, givenSourceWhenclGetProgramBuildInfoIsCalledT TEST_F(clGetProgramBuildInfoTests, givenElfBinaryWhenclGetProgramBuildInfoIsCalledThenReturnClBuildNone) { cl_program pProgram = nullptr; cl_int binaryStatus = CL_INVALID_VALUE; - void *pBinary = nullptr; - size_t binarySize = 0; - std::string testFile; - retrieveBinaryKernelFilename(testFile, "CopyBuffer_simd8_", ".bin"); - ASSERT_EQ(true, fileExists(testFile)); - - binarySize = loadDataFromFile( - testFile.c_str(), - pBinary); - - ASSERT_NE(0u, binarySize); - ASSERT_NE(nullptr, pBinary); + CLElfLib::ElfBinaryStorage elfBinary; + MockElfBinary(elfBinary); + const size_t binarySize = elfBinary.size(); + const unsigned char *elfBinaryTemp = reinterpret_cast(elfBinary.data()); pProgram = clCreateProgramWithBinary( pContext, num_devices, devices, &binarySize, - (const unsigned char **)&pBinary, + &elfBinaryTemp, &binaryStatus, &retVal); + EXPECT_EQ(CL_SUCCESS, retVal); EXPECT_NE(nullptr, pProgram); EXPECT_EQ(CL_SUCCESS, binaryStatus); @@ -144,8 +138,6 @@ TEST_F(clGetProgramBuildInfoTests, givenElfBinaryWhenclGetProgramBuildInfoIsCall EXPECT_EQ(CL_SUCCESS, retVal); EXPECT_EQ(CL_BUILD_NONE, buildStatus); - deleteDataReadFromFile(pBinary); - retVal = clReleaseProgram(pProgram); EXPECT_EQ(CL_SUCCESS, retVal); CompilerInterface::shutdown(); diff --git a/unit_tests/elflib/elf_binary_simulator.h b/unit_tests/elflib/elf_binary_simulator.h new file mode 100644 index 0000000000..f4efc83c20 --- /dev/null +++ b/unit_tests/elflib/elf_binary_simulator.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 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 "elf/types.h" +#include "elf/writer.h" + +namespace OCLRT { + +void MockElfBinary(CLElfLib::ElfBinaryStorage &elfBinary) { + CLElfLib::CElfWriter elfWriter(CLElfLib::E_EH_TYPE::EH_TYPE_OPENCL_LIBRARY, CLElfLib::E_EH_MACHINE::EH_MACHINE_NONE, 0); + elfWriter.addSection(CLElfLib::SSectionNode(CLElfLib::E_SH_TYPE::SH_TYPE_OPENCL_OPTIONS, CLElfLib::E_SH_FLAG::SH_FLAG_NONE, "BuildOptions", "\0", 1u)); + + elfBinary.resize(elfWriter.getTotalBinarySize()); + + elfWriter.resolveBinary(elfBinary); +} +} // namespace OCLRT