xref: /freebsd-src/contrib/llvm-project/llvm/lib/Object/DXContainer.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
181ad6265SDimitry Andric //===- DXContainer.cpp - DXContainer object file implementation -----------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #include "llvm/Object/DXContainer.h"
1081ad6265SDimitry Andric #include "llvm/BinaryFormat/DXContainer.h"
1181ad6265SDimitry Andric #include "llvm/Object/Error.h"
12*bdd1243dSDimitry Andric #include "llvm/Support/FormatVariadic.h"
1381ad6265SDimitry Andric 
1481ad6265SDimitry Andric using namespace llvm;
1581ad6265SDimitry Andric using namespace llvm::object;
1681ad6265SDimitry Andric 
1781ad6265SDimitry Andric static Error parseFailed(const Twine &Msg) {
1881ad6265SDimitry Andric   return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
1981ad6265SDimitry Andric }
2081ad6265SDimitry Andric 
2181ad6265SDimitry Andric template <typename T>
2281ad6265SDimitry Andric static Error readStruct(StringRef Buffer, const char *Src, T &Struct) {
2381ad6265SDimitry Andric   // Don't read before the beginning or past the end of the file
2481ad6265SDimitry Andric   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
2581ad6265SDimitry Andric     return parseFailed("Reading structure out of file bounds");
2681ad6265SDimitry Andric 
2781ad6265SDimitry Andric   memcpy(&Struct, Src, sizeof(T));
2881ad6265SDimitry Andric   // DXContainer is always little endian
2981ad6265SDimitry Andric   if (sys::IsBigEndianHost)
3081ad6265SDimitry Andric     Struct.swapBytes();
3181ad6265SDimitry Andric   return Error::success();
3281ad6265SDimitry Andric }
3381ad6265SDimitry Andric 
3481ad6265SDimitry Andric template <typename T>
35*bdd1243dSDimitry Andric static Error readInteger(StringRef Buffer, const char *Src, T &Val,
36*bdd1243dSDimitry Andric                          Twine Str = "structure") {
37*bdd1243dSDimitry Andric   static_assert(std::is_integral_v<T>,
3881ad6265SDimitry Andric                 "Cannot call readInteger on non-integral type.");
3981ad6265SDimitry Andric   // Don't read before the beginning or past the end of the file
4081ad6265SDimitry Andric   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
41*bdd1243dSDimitry Andric     return parseFailed(Twine("Reading ") + Str + " out of file bounds");
4281ad6265SDimitry Andric 
43*bdd1243dSDimitry Andric   // The DXContainer offset table is comprised of uint32_t values but not padded
44*bdd1243dSDimitry Andric   // to a 64-bit boundary. So Parts may start unaligned if there is an odd
45*bdd1243dSDimitry Andric   // number of parts and part data itself is not required to be padded.
46*bdd1243dSDimitry Andric   if (reinterpret_cast<uintptr_t>(Src) % alignof(T) != 0)
47*bdd1243dSDimitry Andric     memcpy(reinterpret_cast<char *>(&Val), Src, sizeof(T));
48*bdd1243dSDimitry Andric   else
4981ad6265SDimitry Andric     Val = *reinterpret_cast<const T *>(Src);
5081ad6265SDimitry Andric   // DXContainer is always little endian
5181ad6265SDimitry Andric   if (sys::IsBigEndianHost)
5281ad6265SDimitry Andric     sys::swapByteOrder(Val);
5381ad6265SDimitry Andric   return Error::success();
5481ad6265SDimitry Andric }
5581ad6265SDimitry Andric 
5681ad6265SDimitry Andric DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {}
5781ad6265SDimitry Andric 
5881ad6265SDimitry Andric Error DXContainer::parseHeader() {
5981ad6265SDimitry Andric   return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
6081ad6265SDimitry Andric }
6181ad6265SDimitry Andric 
62*bdd1243dSDimitry Andric Error DXContainer::parseDXILHeader(StringRef Part) {
6381ad6265SDimitry Andric   if (DXIL)
6481ad6265SDimitry Andric     return parseFailed("More than one DXIL part is present in the file");
65*bdd1243dSDimitry Andric   const char *Current = Part.begin();
6681ad6265SDimitry Andric   dxbc::ProgramHeader Header;
67*bdd1243dSDimitry Andric   if (Error Err = readStruct(Part, Current, Header))
6881ad6265SDimitry Andric     return Err;
6981ad6265SDimitry Andric   Current += offsetof(dxbc::ProgramHeader, Bitcode) + Header.Bitcode.Offset;
7081ad6265SDimitry Andric   DXIL.emplace(std::make_pair(Header, Current));
7181ad6265SDimitry Andric   return Error::success();
7281ad6265SDimitry Andric }
7381ad6265SDimitry Andric 
74*bdd1243dSDimitry Andric Error DXContainer::parseShaderFlags(StringRef Part) {
75*bdd1243dSDimitry Andric   if (ShaderFlags)
76*bdd1243dSDimitry Andric     return parseFailed("More than one SFI0 part is present in the file");
77*bdd1243dSDimitry Andric   uint64_t FlagValue = 0;
78*bdd1243dSDimitry Andric   if (Error Err = readInteger(Part, Part.begin(), FlagValue))
79*bdd1243dSDimitry Andric     return Err;
80*bdd1243dSDimitry Andric   ShaderFlags = FlagValue;
81*bdd1243dSDimitry Andric   return Error::success();
82*bdd1243dSDimitry Andric }
83*bdd1243dSDimitry Andric 
84*bdd1243dSDimitry Andric Error DXContainer::parseHash(StringRef Part) {
85*bdd1243dSDimitry Andric   if (Hash)
86*bdd1243dSDimitry Andric     return parseFailed("More than one HASH part is present in the file");
87*bdd1243dSDimitry Andric   dxbc::ShaderHash ReadHash;
88*bdd1243dSDimitry Andric   if (Error Err = readStruct(Part, Part.begin(), ReadHash))
89*bdd1243dSDimitry Andric     return Err;
90*bdd1243dSDimitry Andric   Hash = ReadHash;
91*bdd1243dSDimitry Andric   return Error::success();
92*bdd1243dSDimitry Andric }
93*bdd1243dSDimitry Andric 
9481ad6265SDimitry Andric Error DXContainer::parsePartOffsets() {
95*bdd1243dSDimitry Andric   uint32_t LastOffset =
96*bdd1243dSDimitry Andric       sizeof(dxbc::Header) + (Header.PartCount * sizeof(uint32_t));
9781ad6265SDimitry Andric   const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header);
9881ad6265SDimitry Andric   for (uint32_t Part = 0; Part < Header.PartCount; ++Part) {
9981ad6265SDimitry Andric     uint32_t PartOffset;
10081ad6265SDimitry Andric     if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset))
10181ad6265SDimitry Andric       return Err;
102*bdd1243dSDimitry Andric     if (PartOffset < LastOffset)
103*bdd1243dSDimitry Andric       return parseFailed(
104*bdd1243dSDimitry Andric           formatv(
105*bdd1243dSDimitry Andric               "Part offset for part {0} begins before the previous part ends",
106*bdd1243dSDimitry Andric               Part)
107*bdd1243dSDimitry Andric               .str());
10881ad6265SDimitry Andric     Current += sizeof(uint32_t);
109*bdd1243dSDimitry Andric     if (PartOffset >= Data.getBufferSize())
11081ad6265SDimitry Andric       return parseFailed("Part offset points beyond boundary of the file");
111*bdd1243dSDimitry Andric     // To prevent overflow when reading the part name, we subtract the part name
112*bdd1243dSDimitry Andric     // size from the buffer size, rather than adding to the offset. Since the
113*bdd1243dSDimitry Andric     // file header is larger than the part header we can't reach this code
114*bdd1243dSDimitry Andric     // unless the buffer is at least as large as a part header, so this
115*bdd1243dSDimitry Andric     // subtraction can't underflow.
116*bdd1243dSDimitry Andric     if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name))
117*bdd1243dSDimitry Andric       return parseFailed("File not large enough to read part name");
11881ad6265SDimitry Andric     PartOffsets.push_back(PartOffset);
11981ad6265SDimitry Andric 
120*bdd1243dSDimitry Andric     dxbc::PartType PT =
121*bdd1243dSDimitry Andric         dxbc::parsePartType(Data.getBuffer().substr(PartOffset, 4));
122*bdd1243dSDimitry Andric     uint32_t PartDataStart = PartOffset + sizeof(dxbc::PartHeader);
123*bdd1243dSDimitry Andric     uint32_t PartSize;
124*bdd1243dSDimitry Andric     if (Error Err = readInteger(Data.getBuffer(),
125*bdd1243dSDimitry Andric                                 Data.getBufferStart() + PartOffset + 4,
126*bdd1243dSDimitry Andric                                 PartSize, "part size"))
12781ad6265SDimitry Andric       return Err;
128*bdd1243dSDimitry Andric     StringRef PartData = Data.getBuffer().substr(PartDataStart, PartSize);
129*bdd1243dSDimitry Andric     LastOffset = PartOffset + PartSize;
130*bdd1243dSDimitry Andric     switch (PT) {
131*bdd1243dSDimitry Andric     case dxbc::PartType::DXIL:
132*bdd1243dSDimitry Andric       if (Error Err = parseDXILHeader(PartData))
133*bdd1243dSDimitry Andric         return Err;
134*bdd1243dSDimitry Andric       break;
135*bdd1243dSDimitry Andric     case dxbc::PartType::SFI0:
136*bdd1243dSDimitry Andric       if (Error Err = parseShaderFlags(PartData))
137*bdd1243dSDimitry Andric         return Err;
138*bdd1243dSDimitry Andric       break;
139*bdd1243dSDimitry Andric     case dxbc::PartType::HASH:
140*bdd1243dSDimitry Andric       if (Error Err = parseHash(PartData))
141*bdd1243dSDimitry Andric         return Err;
142*bdd1243dSDimitry Andric       break;
143*bdd1243dSDimitry Andric     case dxbc::PartType::Unknown:
144*bdd1243dSDimitry Andric       break;
145*bdd1243dSDimitry Andric     }
14681ad6265SDimitry Andric   }
14781ad6265SDimitry Andric   return Error::success();
14881ad6265SDimitry Andric }
14981ad6265SDimitry Andric 
15081ad6265SDimitry Andric Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) {
15181ad6265SDimitry Andric   DXContainer Container(Object);
15281ad6265SDimitry Andric   if (Error Err = Container.parseHeader())
15381ad6265SDimitry Andric     return std::move(Err);
15481ad6265SDimitry Andric   if (Error Err = Container.parsePartOffsets())
15581ad6265SDimitry Andric     return std::move(Err);
15681ad6265SDimitry Andric   return Container;
15781ad6265SDimitry Andric }
15881ad6265SDimitry Andric 
15981ad6265SDimitry Andric void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
16081ad6265SDimitry Andric   StringRef Buffer = Container.Data.getBuffer();
16181ad6265SDimitry Andric   const char *Current = Buffer.data() + Offset;
16281ad6265SDimitry Andric   // Offsets are validated during parsing, so all offsets in the container are
16381ad6265SDimitry Andric   // valid and contain enough readable data to read a header.
16481ad6265SDimitry Andric   cantFail(readStruct(Buffer, Current, IteratorState.Part));
16581ad6265SDimitry Andric   IteratorState.Data =
16681ad6265SDimitry Andric       StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size);
16781ad6265SDimitry Andric   IteratorState.Offset = Offset;
16881ad6265SDimitry Andric }
169