OCLDecoder PatchListSize fix

Change-Id: I8e4f809464b88d06ff4b5611888f1e6965aa4f19
This commit is contained in:
Krystian
2018-11-09 13:25:52 +01:00
committed by sys_ocldev
parent 66492a53a4
commit 5b9f5a6d9e
3 changed files with 83 additions and 92 deletions

View File

@@ -213,9 +213,11 @@ int BinaryDecoder::processBinary(void *&ptr, std::ostream &ptmFile) {
} }
dumpField(ptr, v, ptmFile); dumpField(ptr, v, ptmFile);
} }
if (patchListSize == 0 || numberOfKernels == 0) { if (patchListSize == 0) {
printf("Error! Couldn't process program header.\n"); printf("Warning! Program's patch list size is 0.\n");
return -1; }
if (numberOfKernels == 0) {
printf("Warning! Number of Kernels is 0.\n");
} }
readPatchTokens(ptr, patchListSize, ptmFile); readPatchTokens(ptr, patchListSize, ptmFile);
@@ -249,10 +251,7 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) {
dumpField(ptr, v, ptmFile); dumpField(ptr, v, ptmFile);
} }
if (KernelPatchListSize == 0) { if (KernelNameSize == 0) {
printf("Error! KernelPatchListSize was 0.\n");
exit(1);
} else if (KernelNameSize == 0) {
printf("Error! KernelNameSize was 0.\n"); printf("Error! KernelNameSize was 0.\n");
exit(1); exit(1);
} }
@@ -281,6 +280,9 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) {
writeDataToFile(fileName.c_str(), ptr, SurfaceStateHeapSize); writeDataToFile(fileName.c_str(), ptr, SurfaceStateHeapSize);
ptr = ptrOffset(ptr, SurfaceStateHeapSize); ptr = ptrOffset(ptr, SurfaceStateHeapSize);
if (KernelPatchListSize == 0) {
printf("Warning! Kernel's patch list size was 0.\n");
}
readPatchTokens(ptr, KernelPatchListSize, ptmFile); readPatchTokens(ptr, KernelPatchListSize, ptmFile);
} }

View File

@@ -7,11 +7,25 @@
#include "mock/mock_decoder.h" #include "mock/mock_decoder.h"
#include "runtime/helpers/array_count.h" #include "runtime/helpers/array_count.h"
#include "unit_tests/test_files/patch_list.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include <fstream> #include <fstream>
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 { namespace OCLRT {
TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) { TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) {
const char *argv[] = { const char *argv[] = {
@@ -203,100 +217,67 @@ TEST(DecoderTests, GivenValidBinaryWhenReadingPatchTokensFromBinaryThenBinaryIsR
EXPECT_EQ(s, out.str()); EXPECT_EQ(s, out.str());
} }
TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPatchTokensAreReadCorrectly) { TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBinaryIsReadCorrectly) {
std::string binaryString;
auto programHeader = createProgramBinaryHeader(1, 0);
std::string kernelName("ExampleKernel");
auto kernelHeader = createKernelBinaryHeaderCommon(static_cast<uint32_t>(kernelName.size() + 1), 0);
std::stringstream binarySS; std::stringstream binarySS;
uint8_t byte; binarySS.write(reinterpret_cast<char *>(&programHeader), sizeof(SProgramBinaryHeader));
uint32_t byte4; binarySS.write(reinterpret_cast<char *>(&kernelHeader), sizeof(SKernelBinaryHeaderCommon));
uint64_t byte8; 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<unsigned char> binary(binaryString.begin(), binaryString.end());
auto ptr = reinterpret_cast<void *>(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 //ProgramBinaryHeader
byte4 = 1229870147; auto programHeader = createProgramBinaryHeader(1, 30);
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); binarySS.write(reinterpret_cast<const char *>(&programHeader), sizeof(SProgramBinaryHeader));
byte4 = 1042;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 12;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 4;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 1;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 2;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 30;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
//PATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO //PATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO
byte4 = 42; SPatchAllocateConstantMemorySurfaceProgramBinaryInfo patchAllocateConstantMemory;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); patchAllocateConstantMemory.Token = 42;
byte4 = 16; patchAllocateConstantMemory.Size = 16;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); patchAllocateConstantMemory.ConstantBufferIndex = 0;
byte4 = 0; patchAllocateConstantMemory.InlineDataSize = 14;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); binarySS.write(reinterpret_cast<const char *>(&patchAllocateConstantMemory), sizeof(patchAllocateConstantMemory));
byte4 = 14; //InlineData
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); for (uint8_t i = 0; i < 14; ++i) {
byte = 0x48; binarySS.write(reinterpret_cast<char *>(&i), sizeof(uint8_t));
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t)); }
byte = 0x65;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x6c;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x6c;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x6f;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x20;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x77;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x6f;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x72;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x6c;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x64;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x21;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0xa;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
byte = 0x0;
binarySS.write(reinterpret_cast<char *>(&byte), sizeof(uint8_t));
//KernelBinaryHeader //KernelBinaryHeader
byte4 = 242806820; std::string kernelName("ExampleKernel");
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); auto kernelHeader = createKernelBinaryHeaderCommon(static_cast<uint32_t>(kernelName.size() + 1), 12);
byte8 = 2860607076011376830;
binarySS.write(reinterpret_cast<char *>(&byte8), sizeof(uint64_t));
byte4 = 12;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 12;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 0;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 0;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 0;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 0;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
byte4 = 0;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
std::string kernelName("HelloWorld"); binarySS.write(reinterpret_cast<const char *>(&kernelHeader), sizeof(SKernelBinaryHeaderCommon));
binarySS.write(kernelName.c_str(), 12); binarySS.write(kernelName.c_str(), kernelHeader.KernelNameSize);
//PATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD //PATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD
byte4 = 19; SPatchMediaInterfaceDescriptorLoad patchMediaInterfaceDescriptorLoad;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); patchMediaInterfaceDescriptorLoad.Token = 19;
byte4 = 12; patchMediaInterfaceDescriptorLoad.Size = 12;
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t)); patchMediaInterfaceDescriptorLoad.InterfaceDescriptorDataOffset = 0;
byte4 = 0; binarySS.write(reinterpret_cast<const char *>(&patchMediaInterfaceDescriptorLoad), sizeof(SPatchMediaInterfaceDescriptorLoad));
binarySS.write(reinterpret_cast<char *>(&byte4), sizeof(uint32_t));
binaryString = binarySS.str();
std::string binaryString = binarySS.str();
std::vector<char> binary(binaryString.begin(), binaryString.end()); std::vector<char> binary(binaryString.begin(), binaryString.end());
std::stringstream ptmFile; std::stringstream ptmFile;
MockDecoder decoder; MockDecoder decoder;
@@ -308,7 +289,7 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa
int retVal = decoder.processBinary(ptr, ptmFile); int retVal = decoder.processBinary(ptr, ptmFile);
EXPECT_EQ(0, retVal); 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()); EXPECT_EQ(expectedOutput, ptmFile.str());
} }
} // namespace OCLRT } // namespace OCLRT

View File

@@ -6,7 +6,10 @@
*/ */
// clang-format off // clang-format off
#pragma once
#pragma pack( push, 1 )
const uint32_t MAGIC_CL = 0x494E5443;
struct SProgramBinaryHeader struct SProgramBinaryHeader
{ {
uint32_t Magic; uint32_t Magic;
@@ -21,6 +24,7 @@ struct SProgramBinaryHeader
uint32_t PatchListSize; uint32_t PatchListSize;
}; };
static_assert( sizeof( SProgramBinaryHeader ) == 28 , "The size of SProgramBinaryHeader is not what is expected" );
struct SKernelBinaryHeader struct SKernelBinaryHeader
{ {
@@ -29,6 +33,7 @@ struct SKernelBinaryHeader
uint32_t KernelNameSize; uint32_t KernelNameSize;
uint32_t PatchListSize; uint32_t PatchListSize;
}; };
static_assert( sizeof( SKernelBinaryHeader ) == 20 , "The size of SKernelBinaryHeader is not what is expected" );
struct SKernelBinaryHeaderCommon : struct SKernelBinaryHeaderCommon :
SKernelBinaryHeader SKernelBinaryHeader
@@ -39,6 +44,7 @@ struct SKernelBinaryHeaderCommon :
uint32_t SurfaceStateHeapSize; uint32_t SurfaceStateHeapSize;
uint32_t KernelUnpaddedSize; uint32_t KernelUnpaddedSize;
}; };
static_assert( sizeof( SKernelBinaryHeaderCommon ) == ( 20 + sizeof( SKernelBinaryHeader ) ) , "The size of SKernelBinaryHeaderCommon is not what is expected" );
enum PATCH_TOKEN enum PATCH_TOKEN
{ {
@@ -53,7 +59,7 @@ enum PATCH_TOKEN
PATCH_TOKEN_BINDING_TABLE_STATE, // 8 @SPatchBindingTableState@ PATCH_TOKEN_BINDING_TABLE_STATE, // 8 @SPatchBindingTableState@
PATCH_TOKEN_ALLOCATE_SCRATCH_SURFACE, // 9 - (Unused) PATCH_TOKEN_ALLOCATE_SCRATCH_SURFACE, // 9 - (Unused)
PATCH_TOKEN_ALLOCATE_SIP_SURFACE, // 10 @SPatchAllocateSystemThreadSurface@ 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_IMAGE_MEMORY_OBJECT_KERNEL_ARGUMENT, // 12 @SPatchImageMemoryObjectKernelArgument@ - OpenCL
PATCH_TOKEN_CONSTANT_MEMORY_OBJECT_KERNEL_ARGUMENT, // 13 - (Unused) - OpenCL PATCH_TOKEN_CONSTANT_MEMORY_OBJECT_KERNEL_ARGUMENT, // 13 - (Unused) - OpenCL
PATCH_TOKEN_ALLOCATE_SURFACE_WITH_INITIALIZATION, // 14 - (Unused) PATCH_TOKEN_ALLOCATE_SURFACE_WITH_INITIALIZATION, // 14 - (Unused)
@@ -123,7 +129,7 @@ struct SPatchMediaInterfaceDescriptorLoad :
{ {
uint32_t InterfaceDescriptorDataOffset; uint32_t InterfaceDescriptorDataOffset;
}; };
static_assert( sizeof( SPatchMediaInterfaceDescriptorLoad ) == ( 4 + sizeof( SPatchItemHeader ) ) , "The size of SPatchMediaInterfaceDescriptorLoad is not what is expected" );
struct SPatchStateSIP : struct SPatchStateSIP :
SPatchItemHeader SPatchItemHeader
{ {
@@ -144,4 +150,6 @@ struct SPatchAllocateConstantMemorySurfaceProgramBinaryInfo :
uint32_t ConstantBufferIndex; uint32_t ConstantBufferIndex;
uint32_t InlineDataSize; 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 // clang-format on