Skip to content

Commit 987901c

Browse files
author
joaosaffran
committed
adding few more tests
1 parent ca21878 commit 987901c

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
#define LLVM_BINARYFORMAT_DXCONTAINER_H
1515

1616
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Object/Error.h"
18+
#include "llvm/Support/Error.h"
1719
#include "llvm/Support/SwapByteOrder.h"
1820
#include "llvm/TargetParser/Triple.h"
1921

22+
#include <cstdint>
2023
#include <stdint.h>
2124

2225
namespace llvm {
@@ -63,15 +66,6 @@ struct ShaderHash {
6366
void swapBytes() { sys::swapByteOrder(Flags); }
6467
};
6568

66-
struct RootSignatureDesc {
67-
uint32_t Size;
68-
uint32_t Flags;
69-
70-
void swapBytes() {
71-
sys::swapByteOrder(Size);
72-
sys::swapByteOrder(Flags);
73-
}
74-
};
7569

7670
struct ContainerVersion {
7771
uint16_t Major;
@@ -556,6 +550,21 @@ struct ProgramSignatureElement {
556550
static_assert(sizeof(ProgramSignatureElement) == 32,
557551
"ProgramSignatureElement is misaligned");
558552

553+
struct RootSignatureValidations {
554+
555+
static Expected<uint32_t> validateRootFlag(uint32_t Flags) {
556+
if ((Flags & ~0x80000fff) != 0)
557+
return llvm::make_error<object::GenericBinaryError>("Invalid flag");
558+
return Flags;
559+
}
560+
561+
static Expected<uint32_t> validateVersion(uint32_t Version) {
562+
if (Version < 1 || Version > 2)
563+
return llvm::make_error<object::GenericBinaryError>("Invalid Version");
564+
return Version;
565+
}
566+
};
567+
559568
} // namespace dxbc
560569
} // namespace llvm
561570

llvm/include/llvm/Object/DXContainer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/ADT/StringRef.h"
2020
#include "llvm/BinaryFormat/DXContainer.h"
21+
#include "llvm/Object/Error.h"
2122
#include "llvm/Support/Error.h"
2223
#include "llvm/Support/MemoryBufferRef.h"
2324
#include "llvm/TargetParser/Triple.h"

llvm/lib/Object/DXContainer.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include "llvm/Object/Error.h"
1313
#include "llvm/Support/Alignment.h"
1414
#include "llvm/Support/Endian.h"
15+
#include "llvm/Support/Error.h"
1516
#include "llvm/Support/FormatVariadic.h"
17+
#include <cstdint>
1618

1719
using namespace llvm;
1820
using namespace llvm::object;
@@ -246,14 +248,20 @@ void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
246248
Error DirectX::RootSignature::parse(StringRef Data) {
247249
const char *Current = Data.begin();
248250

251+
249252
// Root Signature headers expects 6 integers to be present.
250253
if (Data.size() < 6 * sizeof(uint32_t)) {
251254
return parseFailed("Invalid data. Too small.");
252255
}
253256

254-
Version = support::endian::read<uint32_t, llvm::endianness::little>(Current);
257+
uint32_t VValue = support::endian::read<uint32_t, llvm::endianness::little>(Current);
255258
Current += sizeof(uint32_t);
256259

260+
Expected<uint32_t> MaybeVersion = dxbc::RootSignatureValidations::validateVersion(VValue);
261+
if(Error E = MaybeVersion.takeError())
262+
return E;
263+
Version = MaybeVersion.get();
264+
257265
NumParameters =
258266
support::endian::read<uint32_t, llvm::endianness::little>(Current);
259267
Current += sizeof(uint32_t);
@@ -270,9 +278,14 @@ Error DirectX::RootSignature::parse(StringRef Data) {
270278
support::endian::read<uint32_t, llvm::endianness::little>(Current);
271279
Current += sizeof(uint32_t);
272280

273-
Flags = support::endian::read<uint32_t, llvm::endianness::little>(Current);
281+
uint32_t FValue = support::endian::read<uint32_t, llvm::endianness::little>(Current);
274282
Current += sizeof(uint32_t);
275283

284+
Expected<uint32_t> MaybeFlag = dxbc::RootSignatureValidations::validateRootFlag(FValue);
285+
if(Error E = MaybeFlag.takeError())
286+
return E;
287+
Flags = MaybeFlag.get();
288+
276289
return Error::success();
277290
}
278291

llvm/unittests/Object/DXContainerTest.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ TEST(RootSignature, ParseRootFlags) {
833833
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
834834
};
835835
DXContainer C =
836-
llvm::cantFail(DXContainer::create(getMemoryBuffer<180>(Buffer)));
836+
llvm::cantFail(DXContainer::create(getMemoryBuffer<68>(Buffer)));
837837

838838
const auto &RS = C.getRootSignature();
839839
ASSERT_TRUE(RS.has_value());
@@ -855,7 +855,33 @@ TEST(RootSignature, ParseRootFlags) {
855855
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
856856
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
857857
};
858-
EXPECT_THAT_EXPECTED(DXContainer::create(getMemoryBuffer<44>(Buffer)),
858+
EXPECT_THAT_EXPECTED(DXContainer::create(getMemoryBuffer<64>(Buffer)),
859859
FailedWithMessage("Invalid data. Too small."));
860860
}
861+
{
862+
// Version has been changed to an invalid number.
863+
uint8_t Buffer[] = {
864+
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F,
865+
0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00, 0x00, 0x00,
866+
0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
867+
0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
868+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
869+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
870+
};
871+
EXPECT_THAT_EXPECTED(DXContainer::create(getMemoryBuffer<68>(Buffer)),
872+
FailedWithMessage("Invalid Version"));
873+
}
874+
{
875+
// Flag has been set to an invalid value
876+
uint8_t Buffer[] = {
877+
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F,
878+
0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00, 0x00, 0x00,
879+
0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
880+
0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
881+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
882+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xFF,
883+
};
884+
EXPECT_THAT_EXPECTED(DXContainer::create(getMemoryBuffer<68>(Buffer)),
885+
FailedWithMessage("Invalid flag"));
886+
}
861887
}

0 commit comments

Comments
 (0)