xref: /llvm-project/llvm/lib/Object/DXContainer.cpp (revision 0c3f51c0427022f16129a6ac4d2a11ad83ae7703)
1 //===- DXContainer.cpp - DXContainer object file implementation -----------===//
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/BinaryFormat/DXContainer.h"
11 #include "llvm/Object/Error.h"
12 #include "llvm/Support/Alignment.h"
13 #include "llvm/Support/FormatVariadic.h"
14 
15 using namespace llvm;
16 using namespace llvm::object;
17 
18 static Error parseFailed(const Twine &Msg) {
19   return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
20 }
21 
22 template <typename T>
23 static Error readStruct(StringRef Buffer, const char *Src, T &Struct) {
24   // Don't read before the beginning or past the end of the file
25   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
26     return parseFailed("Reading structure out of file bounds");
27 
28   memcpy(&Struct, Src, sizeof(T));
29   // DXContainer is always little endian
30   if (sys::IsBigEndianHost)
31     Struct.swapBytes();
32   return Error::success();
33 }
34 
35 template <typename T>
36 static Error readInteger(StringRef Buffer, const char *Src, T &Val,
37                          Twine Str = "structure") {
38   static_assert(std::is_integral_v<T>,
39                 "Cannot call readInteger on non-integral type.");
40   // Don't read before the beginning or past the end of the file
41   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
42     return parseFailed(Twine("Reading ") + Str + " out of file bounds");
43 
44   // The DXContainer offset table is comprised of uint32_t values but not padded
45   // to a 64-bit boundary. So Parts may start unaligned if there is an odd
46   // number of parts and part data itself is not required to be padded.
47   if (reinterpret_cast<uintptr_t>(Src) % alignof(T) != 0)
48     memcpy(reinterpret_cast<char *>(&Val), Src, sizeof(T));
49   else
50     Val = *reinterpret_cast<const T *>(Src);
51   // DXContainer is always little endian
52   if (sys::IsBigEndianHost)
53     sys::swapByteOrder(Val);
54   return Error::success();
55 }
56 
57 DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {}
58 
59 Error DXContainer::parseHeader() {
60   return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
61 }
62 
63 Error DXContainer::parseDXILHeader(StringRef Part) {
64   if (DXIL)
65     return parseFailed("More than one DXIL part is present in the file");
66   const char *Current = Part.begin();
67   dxbc::ProgramHeader Header;
68   if (Error Err = readStruct(Part, Current, Header))
69     return Err;
70   Current += offsetof(dxbc::ProgramHeader, Bitcode) + Header.Bitcode.Offset;
71   DXIL.emplace(std::make_pair(Header, Current));
72   return Error::success();
73 }
74 
75 Error DXContainer::parseShaderFlags(StringRef Part) {
76   if (ShaderFlags)
77     return parseFailed("More than one SFI0 part is present in the file");
78   uint64_t FlagValue = 0;
79   if (Error Err = readInteger(Part, Part.begin(), FlagValue))
80     return Err;
81   ShaderFlags = FlagValue;
82   return Error::success();
83 }
84 
85 Error DXContainer::parseHash(StringRef Part) {
86   if (Hash)
87     return parseFailed("More than one HASH part is present in the file");
88   dxbc::ShaderHash ReadHash;
89   if (Error Err = readStruct(Part, Part.begin(), ReadHash))
90     return Err;
91   Hash = ReadHash;
92   return Error::success();
93 }
94 
95 Error DXContainer::parsePSVInfo(StringRef Part) {
96   if (PSVInfo)
97     return parseFailed("More than one PSV0 part is present in the file");
98   PSVInfo = DirectX::PSVRuntimeInfo(Part);
99   // Parsing the PSVRuntime info occurs late because we need to read data from
100   // other parts first.
101   return Error::success();
102 }
103 
104 Error DXContainer::parsePartOffsets() {
105   uint32_t LastOffset =
106       sizeof(dxbc::Header) + (Header.PartCount * sizeof(uint32_t));
107   const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header);
108   for (uint32_t Part = 0; Part < Header.PartCount; ++Part) {
109     uint32_t PartOffset;
110     if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset))
111       return Err;
112     if (PartOffset < LastOffset)
113       return parseFailed(
114           formatv(
115               "Part offset for part {0} begins before the previous part ends",
116               Part)
117               .str());
118     Current += sizeof(uint32_t);
119     if (PartOffset >= Data.getBufferSize())
120       return parseFailed("Part offset points beyond boundary of the file");
121     // To prevent overflow when reading the part name, we subtract the part name
122     // size from the buffer size, rather than adding to the offset. Since the
123     // file header is larger than the part header we can't reach this code
124     // unless the buffer is at least as large as a part header, so this
125     // subtraction can't underflow.
126     if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name))
127       return parseFailed("File not large enough to read part name");
128     PartOffsets.push_back(PartOffset);
129 
130     dxbc::PartType PT =
131         dxbc::parsePartType(Data.getBuffer().substr(PartOffset, 4));
132     uint32_t PartDataStart = PartOffset + sizeof(dxbc::PartHeader);
133     uint32_t PartSize;
134     if (Error Err = readInteger(Data.getBuffer(),
135                                 Data.getBufferStart() + PartOffset + 4,
136                                 PartSize, "part size"))
137       return Err;
138     StringRef PartData = Data.getBuffer().substr(PartDataStart, PartSize);
139     LastOffset = PartOffset + PartSize;
140     switch (PT) {
141     case dxbc::PartType::DXIL:
142       if (Error Err = parseDXILHeader(PartData))
143         return Err;
144       break;
145     case dxbc::PartType::SFI0:
146       if (Error Err = parseShaderFlags(PartData))
147         return Err;
148       break;
149     case dxbc::PartType::HASH:
150       if (Error Err = parseHash(PartData))
151         return Err;
152       break;
153     case dxbc::PartType::PSV0:
154       if (Error Err = parsePSVInfo(PartData))
155         return Err;
156       break;
157     case dxbc::PartType::Unknown:
158       break;
159     }
160   }
161 
162   // Fully parsing the PSVInfo requires knowing the shader kind which we read
163   // out of the program header in the DXIL part.
164   if (PSVInfo) {
165     if (!DXIL)
166       return parseFailed("Cannot fully parse pipeline state validation "
167                          "information without DXIL part.");
168     if (Error Err = PSVInfo->parse(DXIL->first.ShaderKind))
169       return Err;
170   }
171   return Error::success();
172 }
173 
174 Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) {
175   DXContainer Container(Object);
176   if (Error Err = Container.parseHeader())
177     return std::move(Err);
178   if (Error Err = Container.parsePartOffsets())
179     return std::move(Err);
180   return Container;
181 }
182 
183 void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
184   StringRef Buffer = Container.Data.getBuffer();
185   const char *Current = Buffer.data() + Offset;
186   // Offsets are validated during parsing, so all offsets in the container are
187   // valid and contain enough readable data to read a header.
188   cantFail(readStruct(Buffer, Current, IteratorState.Part));
189   IteratorState.Data =
190       StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size);
191   IteratorState.Offset = Offset;
192 }
193 
194 Error DirectX::PSVRuntimeInfo::parse(uint16_t ShaderKind) {
195   Triple::EnvironmentType ShaderStage = dxbc::getShaderStage(ShaderKind);
196 
197   const char *Current = Data.begin();
198   if (Error Err = readInteger(Data, Current, Size))
199     return Err;
200   Current += sizeof(uint32_t);
201 
202   StringRef PSVInfoData = Data.substr(sizeof(uint32_t), Size);
203 
204   if (PSVInfoData.size() < Size)
205     return parseFailed(
206         "Pipeline state data extends beyond the bounds of the part");
207 
208   using namespace dxbc::PSV;
209 
210   const uint32_t PSVVersion = getVersion();
211 
212   // Detect the PSVVersion by looking at the size field.
213   if (PSVVersion == 2) {
214     v2::RuntimeInfo Info;
215     if (Error Err = readStruct(PSVInfoData, Current, Info))
216       return Err;
217     if (sys::IsBigEndianHost)
218       Info.swapBytes(ShaderStage);
219     BasicInfo = Info;
220   } else if (PSVVersion == 1) {
221     v1::RuntimeInfo Info;
222     if (Error Err = readStruct(PSVInfoData, Current, Info))
223       return Err;
224     if (sys::IsBigEndianHost)
225       Info.swapBytes(ShaderStage);
226     BasicInfo = Info;
227   } else if (PSVVersion == 0) {
228     v0::RuntimeInfo Info;
229     if (Error Err = readStruct(PSVInfoData, Current, Info))
230       return Err;
231     if (sys::IsBigEndianHost)
232       Info.swapBytes(ShaderStage);
233     BasicInfo = Info;
234   } else
235     return parseFailed(
236         "Cannot read PSV Runtime Info, unsupported PSV version.");
237 
238   Current += Size;
239 
240   uint32_t ResourceCount = 0;
241   if (Error Err = readInteger(Data, Current, ResourceCount))
242     return Err;
243   Current += sizeof(uint32_t);
244 
245   if (ResourceCount > 0) {
246     if (Error Err = readInteger(Data, Current, Resources.Stride))
247       return Err;
248     Current += sizeof(uint32_t);
249 
250     size_t BindingDataSize = Resources.Stride * ResourceCount;
251     Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
252 
253     if (Resources.Data.size() < BindingDataSize)
254       return parseFailed(
255           "Resource binding data extends beyond the bounds of the part");
256 
257     Current += BindingDataSize;
258   } else
259     Resources.Stride = sizeof(v2::ResourceBindInfo);
260 
261   // PSV version 0 ends after the resource bindings.
262   if (PSVVersion == 0)
263     return Error::success();
264 
265   // String table starts at a 4-byte offset.
266   Current = reinterpret_cast<const char *>(
267       alignTo<4>(reinterpret_cast<const uintptr_t>(Current)));
268 
269   uint32_t StringTableSize = 0;
270   if (Error Err = readInteger(Data, Current, StringTableSize))
271     return Err;
272   if (StringTableSize % 4 != 0)
273     return parseFailed("String table misaligned");
274   Current += sizeof(uint32_t);
275   StringTable = StringRef(Current, StringTableSize);
276 
277   Current += StringTableSize;
278 
279   uint32_t SemanticIndexTableSize = 0;
280   if (Error Err = readInteger(Data, Current, SemanticIndexTableSize))
281     return Err;
282   Current += sizeof(uint32_t);
283 
284   SemanticIndexTable.reserve(SemanticIndexTableSize);
285   for (uint32_t I = 0; I < SemanticIndexTableSize; ++I) {
286     uint32_t Index = 0;
287     if (Error Err = readInteger(Data, Current, Index))
288       return Err;
289     Current += sizeof(uint32_t);
290     SemanticIndexTable.push_back(Index);
291   }
292 
293   uint8_t InputCount = getSigInputCount();
294   uint8_t OutputCount = getSigOutputCount();
295   uint8_t PatchOrPrimCount = getSigPatchOrPrimCount();
296 
297   uint32_t ElementCount = InputCount + OutputCount + PatchOrPrimCount;
298 
299   if (ElementCount > 0) {
300     if (Error Err = readInteger(Data, Current, SigInputElements.Stride))
301       return Err;
302     Current += sizeof(uint32_t);
303     // Assign the stride to all the arrays.
304     SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
305         SigInputElements.Stride;
306 
307     if (Data.end() - Current < ElementCount * SigInputElements.Stride)
308       return parseFailed(
309           "Signature elements extend beyond the size of the part");
310 
311     size_t InputSize = SigInputElements.Stride * InputCount;
312     SigInputElements.Data = Data.substr(Current - Data.begin(), InputSize);
313     Current += InputSize;
314 
315     size_t OutputSize = SigOutputElements.Stride * OutputCount;
316     SigOutputElements.Data = Data.substr(Current - Data.begin(), OutputSize);
317     Current += OutputSize;
318 
319     size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
320     SigPatchOrPrimElements.Data = Data.substr(Current - Data.begin(), PSize);
321     Current += PSize;
322   }
323 
324   return Error::success();
325 }
326 
327 uint8_t DirectX::PSVRuntimeInfo::getSigInputCount() const {
328   if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
329     return P->SigInputElements;
330   if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
331     return P->SigInputElements;
332   return 0;
333 }
334 
335 uint8_t DirectX::PSVRuntimeInfo::getSigOutputCount() const {
336   if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
337     return P->SigOutputElements;
338   if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
339     return P->SigOutputElements;
340   return 0;
341 }
342 
343 uint8_t DirectX::PSVRuntimeInfo::getSigPatchOrPrimCount() const {
344   if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(&BasicInfo))
345     return P->SigPatchOrPrimElements;
346   if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(&BasicInfo))
347     return P->SigPatchOrPrimElements;
348   return 0;
349 }
350