1 //===- DXContainerTest.cpp - Tests for DXContainerFile --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Object/DXContainer.h" 10 #include "llvm/ADT/StringRef.h" 11 #include "llvm/BinaryFormat/Magic.h" 12 #include "llvm/Support/MemoryBufferRef.h" 13 #include "llvm/Testing/Support/Error.h" 14 #include "gtest/gtest.h" 15 16 using namespace llvm; 17 using namespace llvm::object; 18 19 template <std::size_t X> MemoryBufferRef getMemoryBuffer(uint8_t Data[X]) { 20 StringRef Obj(reinterpret_cast<char *>(&Data[0]), X); 21 return MemoryBufferRef(Obj, ""); 22 } 23 24 TEST(DXCFile, IdentifyMagic) { 25 { 26 StringRef Buffer("DXBC"); 27 EXPECT_EQ(identify_magic(Buffer), file_magic::dxcontainer_object); 28 } 29 { 30 StringRef Buffer("DXBCBlahBlahBlah"); 31 EXPECT_EQ(identify_magic(Buffer), file_magic::dxcontainer_object); 32 } 33 } 34 35 TEST(DXCFile, ParseHeaderErrors) { 36 uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43}; 37 EXPECT_THAT_EXPECTED( 38 DXContainer::create(getMemoryBuffer<4>(Buffer)), 39 FailedWithMessage("Reading structure out of file bounds")); 40 } 41 42 TEST(DXCFile, EmptyFile) { 43 EXPECT_THAT_EXPECTED( 44 DXContainer::create(MemoryBufferRef(StringRef("", 0), "")), 45 FailedWithMessage("Reading structure out of file bounds")); 46 } 47 48 TEST(DXCFile, ParseHeader) { 49 uint8_t Buffer[] = {0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 52 0x70, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 53 DXContainer C = 54 llvm::cantFail(DXContainer::create(getMemoryBuffer<32>(Buffer))); 55 EXPECT_TRUE(memcmp(C.getHeader().Magic, "DXBC", 4) == 0); 56 EXPECT_TRUE(memcmp(C.getHeader().FileHash.Digest, 57 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0); 58 EXPECT_EQ(C.getHeader().Version.Major, 1u); 59 EXPECT_EQ(C.getHeader().Version.Minor, 0u); 60 } 61 62 TEST(DXCFile, ParsePartMissingOffsets) { 63 uint8_t Buffer[] = { 64 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 66 0x00, 0x00, 0x70, 0x0D, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 67 }; 68 EXPECT_THAT_EXPECTED( 69 DXContainer::create(getMemoryBuffer<32>(Buffer)), 70 FailedWithMessage("Reading structure out of file bounds")); 71 } 72 73 #if defined(__arm__) 74 TEST(DXCFile, DISABLED_ParsePartInvalidOffsets) { 75 #else 76 TEST(DXCFile, ParsePartInvalidOffsets) { 77 #endif 78 uint8_t Buffer[] = { 79 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 81 0x70, 0x0D, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 82 }; 83 EXPECT_THAT_EXPECTED( 84 DXContainer::create(getMemoryBuffer<36>(Buffer)), 85 FailedWithMessage("Part offset points beyond boundary of the file")); 86 } 87 88 TEST(DXCFile, ParseEmptyParts) { 89 uint8_t Buffer[] = { 90 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 92 0x70, 0x0D, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 93 0x44, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 94 0x5C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 95 0x53, 0x46, 0x49, 0x30, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31, 96 0x00, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x00, 0x00, 0x00, 0x00, 97 0x50, 0x53, 0x56, 0x30, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54, 98 0x00, 0x00, 0x00, 0x00, 0x43, 0x58, 0x49, 0x4C, 0x00, 0x00, 0x00, 0x00, 99 0x44, 0x45, 0x41, 0x44, 0x00, 0x00, 0x00, 0x00, 100 }; 101 DXContainer C = 102 llvm::cantFail(DXContainer::create(getMemoryBuffer<116>(Buffer))); 103 EXPECT_EQ(C.getHeader().PartCount, 7u); 104 105 // All the part sizes are 0, which makes a nice test of the range based for 106 int ElementsVisited = 0; 107 for (auto Part : C) { 108 EXPECT_EQ(Part.Part.Size, 0u); 109 EXPECT_EQ(Part.Data.size(), 0u); 110 ++ElementsVisited; 111 } 112 EXPECT_EQ(ElementsVisited, 7); 113 114 { 115 auto It = C.begin(); 116 EXPECT_TRUE(memcmp(It->Part.Name, "SFI0", 4) == 0); 117 ++It; 118 EXPECT_TRUE(memcmp(It->Part.Name, "ISG1", 4) == 0); 119 ++It; 120 EXPECT_TRUE(memcmp(It->Part.Name, "OSG1", 4) == 0); 121 ++It; 122 EXPECT_TRUE(memcmp(It->Part.Name, "PSV0", 4) == 0); 123 ++It; 124 EXPECT_TRUE(memcmp(It->Part.Name, "STAT", 4) == 0); 125 ++It; 126 EXPECT_TRUE(memcmp(It->Part.Name, "CXIL", 4) == 0); 127 ++It; 128 EXPECT_TRUE(memcmp(It->Part.Name, "DEAD", 4) == 0); 129 ++It; // Don't increment past the end 130 EXPECT_TRUE(memcmp(It->Part.Name, "DEAD", 4) == 0); 131 } 132 } 133