From 5b9f5a6d9e58d0a21e4005962ac5d62c2a97efff Mon Sep 17 00:00:00 2001 From: Krystian Date: Fri, 9 Nov 2018 13:25:52 +0100 Subject: [PATCH] OCLDecoder PatchListSize fix Change-Id: I8e4f809464b88d06ff4b5611888f1e6965aa4f19 --- offline_compiler/decoder/binary_decoder.cpp | 16 +- .../decoder/decoder_tests.cpp | 147 ++++++++---------- unit_tests/test_files/patch_list.h | 12 +- 3 files changed, 83 insertions(+), 92 deletions(-) diff --git a/offline_compiler/decoder/binary_decoder.cpp b/offline_compiler/decoder/binary_decoder.cpp index 2d17a1a6de..1bf08e7733 100644 --- a/offline_compiler/decoder/binary_decoder.cpp +++ b/offline_compiler/decoder/binary_decoder.cpp @@ -213,9 +213,11 @@ int BinaryDecoder::processBinary(void *&ptr, std::ostream &ptmFile) { } dumpField(ptr, v, ptmFile); } - if (patchListSize == 0 || numberOfKernels == 0) { - printf("Error! Couldn't process program header.\n"); - return -1; + if (patchListSize == 0) { + printf("Warning! Program's patch list size is 0.\n"); + } + if (numberOfKernels == 0) { + printf("Warning! Number of Kernels is 0.\n"); } readPatchTokens(ptr, patchListSize, ptmFile); @@ -249,10 +251,7 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) { dumpField(ptr, v, ptmFile); } - if (KernelPatchListSize == 0) { - printf("Error! KernelPatchListSize was 0.\n"); - exit(1); - } else if (KernelNameSize == 0) { + if (KernelNameSize == 0) { printf("Error! KernelNameSize was 0.\n"); exit(1); } @@ -281,6 +280,9 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) { writeDataToFile(fileName.c_str(), ptr, SurfaceStateHeapSize); ptr = ptrOffset(ptr, SurfaceStateHeapSize); + if (KernelPatchListSize == 0) { + printf("Warning! Kernel's patch list size was 0.\n"); + } readPatchTokens(ptr, KernelPatchListSize, ptmFile); } diff --git a/unit_tests/offline_compiler/decoder/decoder_tests.cpp b/unit_tests/offline_compiler/decoder/decoder_tests.cpp index 918ff196ce..ff67416ba6 100644 --- a/unit_tests/offline_compiler/decoder/decoder_tests.cpp +++ b/unit_tests/offline_compiler/decoder/decoder_tests.cpp @@ -7,11 +7,25 @@ #include "mock/mock_decoder.h" #include "runtime/helpers/array_count.h" +#include "unit_tests/test_files/patch_list.h" #include "gmock/gmock.h" #include +SProgramBinaryHeader createProgramBinaryHeader(const uint32_t numberOfKernels, const uint32_t patchListSize) { + return SProgramBinaryHeader{MAGIC_CL, 0, 0, 0, numberOfKernels, 0, patchListSize}; +} + +SKernelBinaryHeaderCommon createKernelBinaryHeaderCommon(const uint32_t kernelNameSize, const uint32_t patchListSize) { + SKernelBinaryHeaderCommon kernelHeader = {}; + kernelHeader.CheckSum = 0xFFFFFFFF; + kernelHeader.ShaderHashCode = 0xFFFFFFFFFFFFFFFF; + kernelHeader.KernelNameSize = kernelNameSize; + kernelHeader.PatchListSize = patchListSize; + return kernelHeader; +} + namespace OCLRT { TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) { const char *argv[] = { @@ -203,100 +217,67 @@ TEST(DecoderTests, GivenValidBinaryWhenReadingPatchTokensFromBinaryThenBinaryIsR EXPECT_EQ(s, out.str()); } -TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPatchTokensAreReadCorrectly) { - std::string binaryString; +TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBinaryIsReadCorrectly) { + + auto programHeader = createProgramBinaryHeader(1, 0); + std::string kernelName("ExampleKernel"); + auto kernelHeader = createKernelBinaryHeaderCommon(static_cast(kernelName.size() + 1), 0); std::stringstream binarySS; - uint8_t byte; - uint32_t byte4; - uint64_t byte8; + binarySS.write(reinterpret_cast(&programHeader), sizeof(SProgramBinaryHeader)); + binarySS.write(reinterpret_cast(&kernelHeader), sizeof(SKernelBinaryHeaderCommon)); + binarySS.write(kernelName.c_str(), kernelHeader.KernelNameSize); + + std::stringstream ptmFile; + MockDecoder decoder; + decoder.pathToPatch = "test_files/"; + decoder.pathToDump = "non_existing_folder/"; + decoder.parseTokens(); + + std::string binaryString = binarySS.str(); + std::vector binary(binaryString.begin(), binaryString.end()); + auto ptr = reinterpret_cast(binary.data()); + int retVal = decoder.processBinary(ptr, ptmFile); + EXPECT_EQ(0, retVal); + + std::string expectedOutput = "ProgramBinaryHeader:\n\t4 Magic 1229870147\n\t4 Version 0\n\t4 Device 0\n\t4 GPUPointerSizeInBytes 0\n\t4 NumberOfKernels 1\n\t4 SteppingId 0\n\t4 PatchListSize 0\nKernel #0\nKernelBinaryHeader:\n\t4 CheckSum 4294967295\n\t8 ShaderHashCode 18446744073709551615\n\t4 KernelNameSize 14\n\t4 PatchListSize 0\n\t4 KernelHeapSize 0\n\t4 GeneralStateHeapSize 0\n\t4 DynamicStateHeapSize 0\n\t4 SurfaceStateHeapSize 0\n\t4 KernelUnpaddedSize 0\n\tKernelName ExampleKernel\n"; + EXPECT_EQ(expectedOutput, ptmFile.str()); +} + +TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPatchTokensAreReadCorrectly) { + std::stringstream binarySS; //ProgramBinaryHeader - byte4 = 1229870147; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 1042; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 12; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 4; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 1; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 2; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 30; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); + auto programHeader = createProgramBinaryHeader(1, 30); + binarySS.write(reinterpret_cast(&programHeader), sizeof(SProgramBinaryHeader)); //PATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO - byte4 = 42; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 16; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 14; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte = 0x48; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x65; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x6c; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x6c; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x6f; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x20; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x77; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x6f; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x72; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x6c; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x64; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x21; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0xa; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); - byte = 0x0; - binarySS.write(reinterpret_cast(&byte), sizeof(uint8_t)); + SPatchAllocateConstantMemorySurfaceProgramBinaryInfo patchAllocateConstantMemory; + patchAllocateConstantMemory.Token = 42; + patchAllocateConstantMemory.Size = 16; + patchAllocateConstantMemory.ConstantBufferIndex = 0; + patchAllocateConstantMemory.InlineDataSize = 14; + binarySS.write(reinterpret_cast(&patchAllocateConstantMemory), sizeof(patchAllocateConstantMemory)); + //InlineData + for (uint8_t i = 0; i < 14; ++i) { + binarySS.write(reinterpret_cast(&i), sizeof(uint8_t)); + } //KernelBinaryHeader - byte4 = 242806820; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte8 = 2860607076011376830; - binarySS.write(reinterpret_cast(&byte8), sizeof(uint64_t)); - byte4 = 12; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 12; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); + std::string kernelName("ExampleKernel"); + auto kernelHeader = createKernelBinaryHeaderCommon(static_cast(kernelName.size() + 1), 12); - std::string kernelName("HelloWorld"); - binarySS.write(kernelName.c_str(), 12); + binarySS.write(reinterpret_cast(&kernelHeader), sizeof(SKernelBinaryHeaderCommon)); + binarySS.write(kernelName.c_str(), kernelHeader.KernelNameSize); //PATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD - byte4 = 19; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 12; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - byte4 = 0; - binarySS.write(reinterpret_cast(&byte4), sizeof(uint32_t)); - binaryString = binarySS.str(); + SPatchMediaInterfaceDescriptorLoad patchMediaInterfaceDescriptorLoad; + patchMediaInterfaceDescriptorLoad.Token = 19; + patchMediaInterfaceDescriptorLoad.Size = 12; + patchMediaInterfaceDescriptorLoad.InterfaceDescriptorDataOffset = 0; + binarySS.write(reinterpret_cast(&patchMediaInterfaceDescriptorLoad), sizeof(SPatchMediaInterfaceDescriptorLoad)); + std::string binaryString = binarySS.str(); std::vector binary(binaryString.begin(), binaryString.end()); std::stringstream ptmFile; MockDecoder decoder; @@ -308,7 +289,7 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa int retVal = decoder.processBinary(ptr, ptmFile); EXPECT_EQ(0, retVal); - std::string expectedOutput = "ProgramBinaryHeader:\n\t4 Magic 1229870147\n\t4 Version 1042\n\t4 Device 12\n\t4 GPUPointerSizeInBytes 4\n\t4 NumberOfKernels 1\n\t4 SteppingId 2\n\t4 PatchListSize 30\nPATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO:\n\t4 Token 42\n\t4 Size 16\n\t4 ConstantBufferIndex 0\n\t4 InlineDataSize 14\n\tHex 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 a 0\nKernel #0\nKernelBinaryHeader:\n\t4 CheckSum 242806820\n\t8 ShaderHashCode 2860607076011376830\n\t4 KernelNameSize 12\n\t4 PatchListSize 12\n\t4 KernelHeapSize 0\n\t4 GeneralStateHeapSize 0\n\t4 DynamicStateHeapSize 0\n\t4 SurfaceStateHeapSize 0\n\t4 KernelUnpaddedSize 0\n\tKernelName HelloWorld\nPATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD:\n\t4 Token 19\n\t4 Size 12\n\t4 InterfaceDescriptorDataOffset 0\n"; + std::string expectedOutput = "ProgramBinaryHeader:\n\t4 Magic 1229870147\n\t4 Version 0\n\t4 Device 0\n\t4 GPUPointerSizeInBytes 0\n\t4 NumberOfKernels 1\n\t4 SteppingId 0\n\t4 PatchListSize 30\nPATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO:\n\t4 Token 42\n\t4 Size 16\n\t4 ConstantBufferIndex 0\n\t4 InlineDataSize 14\n\tHex 0 1 2 3 4 5 6 7 8 9 a b c d\nKernel #0\nKernelBinaryHeader:\n\t4 CheckSum 4294967295\n\t8 ShaderHashCode 18446744073709551615\n\t4 KernelNameSize 14\n\t4 PatchListSize 12\n\t4 KernelHeapSize 0\n\t4 GeneralStateHeapSize 0\n\t4 DynamicStateHeapSize 0\n\t4 SurfaceStateHeapSize 0\n\t4 KernelUnpaddedSize 0\n\tKernelName ExampleKernel\nPATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD:\n\t4 Token 19\n\t4 Size 12\n\t4 InterfaceDescriptorDataOffset 0\n"; EXPECT_EQ(expectedOutput, ptmFile.str()); } } // namespace OCLRT diff --git a/unit_tests/test_files/patch_list.h b/unit_tests/test_files/patch_list.h index 05ee6195d9..612afb1028 100644 --- a/unit_tests/test_files/patch_list.h +++ b/unit_tests/test_files/patch_list.h @@ -6,7 +6,10 @@ */ // clang-format off +#pragma once +#pragma pack( push, 1 ) +const uint32_t MAGIC_CL = 0x494E5443; struct SProgramBinaryHeader { uint32_t Magic; @@ -21,6 +24,7 @@ struct SProgramBinaryHeader uint32_t PatchListSize; }; +static_assert( sizeof( SProgramBinaryHeader ) == 28 , "The size of SProgramBinaryHeader is not what is expected" ); struct SKernelBinaryHeader { @@ -29,6 +33,7 @@ struct SKernelBinaryHeader uint32_t KernelNameSize; uint32_t PatchListSize; }; +static_assert( sizeof( SKernelBinaryHeader ) == 20 , "The size of SKernelBinaryHeader is not what is expected" ); struct SKernelBinaryHeaderCommon : SKernelBinaryHeader @@ -39,6 +44,7 @@ struct SKernelBinaryHeaderCommon : uint32_t SurfaceStateHeapSize; uint32_t KernelUnpaddedSize; }; +static_assert( sizeof( SKernelBinaryHeaderCommon ) == ( 20 + sizeof( SKernelBinaryHeader ) ) , "The size of SKernelBinaryHeaderCommon is not what is expected" ); enum PATCH_TOKEN { @@ -53,7 +59,7 @@ enum PATCH_TOKEN PATCH_TOKEN_BINDING_TABLE_STATE, // 8 @SPatchBindingTableState@ PATCH_TOKEN_ALLOCATE_SCRATCH_SURFACE, // 9 - (Unused) PATCH_TOKEN_ALLOCATE_SIP_SURFACE, // 10 @SPatchAllocateSystemThreadSurface@ - PATCH_TOKEN_GLOBAL_MEMORY_OBJECT_KERNEL_ARGUMENT, // 11 @SPatchGlobalMemoryObjectKernelArgument@ - OpenCL + PATCH_TOKEN_GLOBAL_MEMORY_OBJECT_KERNEL_ARGUMENT, // 11 @SPatchGlobalMemoryObjectKernelArgument@ - OpenCL PATCH_TOKEN_IMAGE_MEMORY_OBJECT_KERNEL_ARGUMENT, // 12 @SPatchImageMemoryObjectKernelArgument@ - OpenCL PATCH_TOKEN_CONSTANT_MEMORY_OBJECT_KERNEL_ARGUMENT, // 13 - (Unused) - OpenCL PATCH_TOKEN_ALLOCATE_SURFACE_WITH_INITIALIZATION, // 14 - (Unused) @@ -123,7 +129,7 @@ struct SPatchMediaInterfaceDescriptorLoad : { uint32_t InterfaceDescriptorDataOffset; }; - +static_assert( sizeof( SPatchMediaInterfaceDescriptorLoad ) == ( 4 + sizeof( SPatchItemHeader ) ) , "The size of SPatchMediaInterfaceDescriptorLoad is not what is expected" ); struct SPatchStateSIP : SPatchItemHeader { @@ -144,4 +150,6 @@ struct SPatchAllocateConstantMemorySurfaceProgramBinaryInfo : uint32_t ConstantBufferIndex; uint32_t InlineDataSize; }; +static_assert( sizeof( SPatchAllocateConstantMemorySurfaceProgramBinaryInfo ) == ( 8 + sizeof( SPatchItemHeader ) ) , "The size of SPatchAllocateConstantMemorySurfaceProgramBinaryInfo is not what is expected" ); +#pragma pack( pop ) // clang-format on \ No newline at end of file