Skip to content

Commit 44532d6

Browse files
author
joaosaffran
committed
adding fail test
1 parent 751cbdc commit 44532d6

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ namespace DirectX {
119119

120120
class RootSignature {
121121
private:
122-
StringRef Data;
123122
uint32_t Version;
124123
uint32_t NumParameters;
125124
uint32_t RootParametersOffset;
@@ -128,9 +127,9 @@ class RootSignature {
128127
uint32_t Flags;
129128

130129
public:
131-
RootSignature(StringRef Data) : Data(Data) {}
130+
RootSignature() {}
132131

133-
Error parse();
132+
Error parse(StringRef Data);
134133
uint32_t getVersion() const { return Version; }
135134
uint32_t getNumParameters() const { return NumParameters; }
136135
uint32_t getRootParametersOffset() const { return RootParametersOffset; }

llvm/lib/Object/DXContainer.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Object/DXContainer.h"
10+
#include "llvm/ADT/StringRef.h"
1011
#include "llvm/BinaryFormat/DXContainer.h"
1112
#include "llvm/Object/Error.h"
1213
#include "llvm/Support/Alignment.h"
@@ -96,8 +97,8 @@ Error DXContainer::parseHash(StringRef Part) {
9697
Error DXContainer::parseRootSignature(StringRef Part) {
9798
if (RootSignature)
9899
return parseFailed("More than one RTS0 part is present in the file");
99-
RootSignature = DirectX::RootSignature(Part);
100-
if (Error Err = RootSignature->parse())
100+
RootSignature = DirectX::RootSignature();
101+
if (Error Err = RootSignature->parse(Part))
101102
return Err;
102103
return Error::success();
103104
}
@@ -242,9 +243,14 @@ void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
242243
IteratorState.Offset = Offset;
243244
}
244245

245-
Error DirectX::RootSignature::parse() {
246+
Error DirectX::RootSignature::parse(StringRef Data) {
246247
const char *Current = Data.begin();
247248

249+
// Root Signature headers expects 6 integers to be present.
250+
if (Data.size() < 6 * sizeof(uint32_t)) {
251+
return parseFailed("Invalid data. Too small.");
252+
}
253+
248254
Version = support::endian::read<uint32_t, llvm::endianness::little>(Current);
249255
Current += sizeof(uint32_t);
250256

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -823,23 +823,38 @@ TEST(DXCFile, MalformedSignature) {
823823
}
824824

825825
TEST(RootSignature, ParseRootFlags) {
826-
uint8_t Buffer[] = {
827-
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F,
828-
0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00, 0x00, 0x00,
829-
0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
830-
0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
831-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
832-
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
833-
};
834-
DXContainer C =
835-
llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
826+
{
827+
uint8_t Buffer[] = {
828+
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F,
829+
0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00, 0x00, 0x00,
830+
0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
831+
0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
832+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
833+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
834+
};
835+
DXContainer C =
836+
llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
837+
838+
const auto &RS = C.getRootSignature();
839+
ASSERT_TRUE(RS.has_value());
840+
ASSERT_EQ(RS->getVersion(), 2);
841+
ASSERT_EQ(RS->getNumParameters(), 0);
842+
ASSERT_EQ(RS->getRootParametersOffset(), 0);
843+
ASSERT_EQ(RS->getNumStaticSamplers(), 0);
844+
ASSERT_EQ(RS->getStaticSamplersOffset(), 0);
845+
ASSERT_EQ(RS->getFlags(), 0x01);
846+
}
836847

837-
const auto &RS = C.getRootSignature();
838-
ASSERT_TRUE(RS.has_value());
839-
ASSERT_EQ(RS->getVersion(), 2);
840-
ASSERT_EQ(RS->getNumParameters(), 0);
841-
ASSERT_EQ(RS->getRootParametersOffset(), 0);
842-
ASSERT_EQ(RS->getNumStaticSamplers(), 0);
843-
ASSERT_EQ(RS->getStaticSamplersOffset(), 0);
844-
ASSERT_EQ(RS->getFlags(), 0x01);
848+
{
849+
uint8_t Buffer[] = {
850+
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35,
851+
0x6F, 0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00,
852+
0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24,
853+
0x00, 0x00, 0x00, 0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00,
854+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
855+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
856+
};
857+
EXPECT_THAT_EXPECTED(DXContainer::create(getMemoryBuffer<44>(Buffer)),
858+
FailedWithMessage("Invalid data. Too small."));
859+
}
845860
}

0 commit comments

Comments
 (0)