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
getMemoryBuffer(uint8_t Data[X])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
TEST(DXCFile,IdentifyMagic)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
TEST(DXCFile,ParseHeaderErrors)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
TEST(DXCFile,EmptyFile)42 TEST(DXCFile, EmptyFile) {
43 EXPECT_THAT_EXPECTED(
44 DXContainer::create(MemoryBufferRef(StringRef("", 0), "")),
45 FailedWithMessage("Reading structure out of file bounds"));
46 }
47
TEST(DXCFile,ParseHeader)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
TEST(DXCFile,ParsePartMissingOffsets)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
TEST(DXCFile,ParsePartInvalidOffsets)73 TEST(DXCFile, ParsePartInvalidOffsets) {
74 uint8_t Buffer[] = {
75 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
77 0x70, 0x0D, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
78 };
79 EXPECT_THAT_EXPECTED(
80 DXContainer::create(getMemoryBuffer<36>(Buffer)),
81 FailedWithMessage("Part offset points beyond boundary of the file"));
82 }
83
TEST(DXCFile,ParseEmptyParts)84 TEST(DXCFile, ParseEmptyParts) {
85 uint8_t Buffer[] = {
86 0x44, 0x58, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
88 0x70, 0x0D, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00,
89 0x44, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
90 0x5C, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00,
91 0x53, 0x46, 0x49, 0x30, 0x00, 0x00, 0x00, 0x00, 0x49, 0x53, 0x47, 0x31,
92 0x00, 0x00, 0x00, 0x00, 0x4F, 0x53, 0x47, 0x31, 0x00, 0x00, 0x00, 0x00,
93 0x50, 0x53, 0x56, 0x30, 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x41, 0x54,
94 0x00, 0x00, 0x00, 0x00, 0x43, 0x58, 0x49, 0x4C, 0x00, 0x00, 0x00, 0x00,
95 0x44, 0x45, 0x41, 0x44, 0x00, 0x00, 0x00, 0x00,
96 };
97 DXContainer C =
98 llvm::cantFail(DXContainer::create(getMemoryBuffer<116>(Buffer)));
99 EXPECT_EQ(C.getHeader().PartCount, 7u);
100
101 // All the part sizes are 0, which makes a nice test of the range based for
102 int ElementsVisited = 0;
103 for (auto Part : C) {
104 EXPECT_EQ(Part.Part.Size, 0u);
105 EXPECT_EQ(Part.Data.size(), 0u);
106 ++ElementsVisited;
107 }
108 EXPECT_EQ(ElementsVisited, 7);
109
110 {
111 auto It = C.begin();
112 EXPECT_TRUE(memcmp(It->Part.Name, "SFI0", 4) == 0);
113 ++It;
114 EXPECT_TRUE(memcmp(It->Part.Name, "ISG1", 4) == 0);
115 ++It;
116 EXPECT_TRUE(memcmp(It->Part.Name, "OSG1", 4) == 0);
117 ++It;
118 EXPECT_TRUE(memcmp(It->Part.Name, "PSV0", 4) == 0);
119 ++It;
120 EXPECT_TRUE(memcmp(It->Part.Name, "STAT", 4) == 0);
121 ++It;
122 EXPECT_TRUE(memcmp(It->Part.Name, "CXIL", 4) == 0);
123 ++It;
124 EXPECT_TRUE(memcmp(It->Part.Name, "DEAD", 4) == 0);
125 ++It; // Don't increment past the end
126 EXPECT_TRUE(memcmp(It->Part.Name, "DEAD", 4) == 0);
127 }
128 }
129