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
15using namespace llvm;
16using namespace llvm::object;
17
18static Error parseFailed(const Twine &Msg) {
19 return make_error<GenericBinaryError>(Args: Msg.str(), Args: object_error::parse_failed);
20}
21
22template <typename T>
23static 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(Msg: "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
35template <typename T>
36static 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(Msg: 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(dest: reinterpret_cast<char *>(&Val), src: Src, n: 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
57DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {}
58
59Error DXContainer::parseHeader() {
60 return readStruct(Buffer: Data.getBuffer(), Src: Data.getBuffer().data(), Struct&: Header);
61}
62
63Error DXContainer::parseDXILHeader(StringRef Part) {
64 if (DXIL)
65 return parseFailed(Msg: "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(Buffer: Part, Src: Current, Struct&: Header))
69 return Err;
70 Current += offsetof(dxbc::ProgramHeader, Bitcode) + Header.Bitcode.Offset;
71 DXIL.emplace(args: std::make_pair(x&: Header, y&: Current));
72 return Error::success();
73}
74
75Error DXContainer::parseShaderFeatureFlags(StringRef Part) {
76 if (ShaderFeatureFlags)
77 return parseFailed(Msg: "More than one SFI0 part is present in the file");
78 uint64_t FlagValue = 0;
79 if (Error Err = readInteger(Buffer: Part, Src: Part.begin(), Val&: FlagValue))
80 return Err;
81 ShaderFeatureFlags = FlagValue;
82 return Error::success();
83}
84
85Error DXContainer::parseHash(StringRef Part) {
86 if (Hash)
87 return parseFailed(Msg: "More than one HASH part is present in the file");
88 dxbc::ShaderHash ReadHash;
89 if (Error Err = readStruct(Buffer: Part, Src: Part.begin(), Struct&: ReadHash))
90 return Err;
91 Hash = ReadHash;
92 return Error::success();
93}
94
95Error DXContainer::parsePSVInfo(StringRef Part) {
96 if (PSVInfo)
97 return parseFailed(Msg: "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
104Error DirectX::Signature::initialize(StringRef Part) {
105 dxbc::ProgramSignatureHeader SigHeader;
106 if (Error Err = readStruct(Buffer: Part, Src: Part.begin(), Struct&: SigHeader))
107 return Err;
108 size_t Size = sizeof(dxbc::ProgramSignatureElement) * SigHeader.ParamCount;
109
110 if (Part.size() < Size + SigHeader.FirstParamOffset)
111 return parseFailed(Msg: "Signature parameters extend beyond the part boundary");
112
113 Parameters.Data = Part.substr(Start: SigHeader.FirstParamOffset, N: Size);
114
115 StringTableOffset = SigHeader.FirstParamOffset + static_cast<uint32_t>(Size);
116 StringTable = Part.substr(Start: SigHeader.FirstParamOffset + Size);
117
118 for (const auto &Param : Parameters) {
119 if (Param.NameOffset < StringTableOffset)
120 return parseFailed(Msg: "Invalid parameter name offset: name starts before "
121 "the first name offset");
122 if (Param.NameOffset - StringTableOffset > StringTable.size())
123 return parseFailed(Msg: "Invalid parameter name offset: name starts after the "
124 "end of the part data");
125 }
126 return Error::success();
127}
128
129Error DXContainer::parsePartOffsets() {
130 uint32_t LastOffset =
131 sizeof(dxbc::Header) + (Header.PartCount * sizeof(uint32_t));
132 const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header);
133 for (uint32_t Part = 0; Part < Header.PartCount; ++Part) {
134 uint32_t PartOffset;
135 if (Error Err = readInteger(Buffer: Data.getBuffer(), Src: Current, Val&: PartOffset))
136 return Err;
137 if (PartOffset < LastOffset)
138 return parseFailed(
139 Msg: formatv(
140 Fmt: "Part offset for part {0} begins before the previous part ends",
141 Vals&: Part)
142 .str());
143 Current += sizeof(uint32_t);
144 if (PartOffset >= Data.getBufferSize())
145 return parseFailed(Msg: "Part offset points beyond boundary of the file");
146 // To prevent overflow when reading the part name, we subtract the part name
147 // size from the buffer size, rather than adding to the offset. Since the
148 // file header is larger than the part header we can't reach this code
149 // unless the buffer is at least as large as a part header, so this
150 // subtraction can't underflow.
151 if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name))
152 return parseFailed(Msg: "File not large enough to read part name");
153 PartOffsets.push_back(Elt: PartOffset);
154
155 dxbc::PartType PT =
156 dxbc::parsePartType(S: Data.getBuffer().substr(Start: PartOffset, N: 4));
157 uint32_t PartDataStart = PartOffset + sizeof(dxbc::PartHeader);
158 uint32_t PartSize;
159 if (Error Err = readInteger(Buffer: Data.getBuffer(),
160 Src: Data.getBufferStart() + PartOffset + 4,
161 Val&: PartSize, Str: "part size"))
162 return Err;
163 StringRef PartData = Data.getBuffer().substr(Start: PartDataStart, N: PartSize);
164 LastOffset = PartOffset + PartSize;
165 switch (PT) {
166 case dxbc::PartType::DXIL:
167 if (Error Err = parseDXILHeader(Part: PartData))
168 return Err;
169 break;
170 case dxbc::PartType::SFI0:
171 if (Error Err = parseShaderFeatureFlags(Part: PartData))
172 return Err;
173 break;
174 case dxbc::PartType::HASH:
175 if (Error Err = parseHash(Part: PartData))
176 return Err;
177 break;
178 case dxbc::PartType::PSV0:
179 if (Error Err = parsePSVInfo(Part: PartData))
180 return Err;
181 break;
182 case dxbc::PartType::ISG1:
183 if (Error Err = InputSignature.initialize(Part: PartData))
184 return Err;
185 break;
186 case dxbc::PartType::OSG1:
187 if (Error Err = OutputSignature.initialize(Part: PartData))
188 return Err;
189 break;
190 case dxbc::PartType::PSG1:
191 if (Error Err = PatchConstantSignature.initialize(Part: PartData))
192 return Err;
193 break;
194 case dxbc::PartType::Unknown:
195 break;
196 }
197 }
198
199 // Fully parsing the PSVInfo requires knowing the shader kind which we read
200 // out of the program header in the DXIL part.
201 if (PSVInfo) {
202 if (!DXIL)
203 return parseFailed(Msg: "Cannot fully parse pipeline state validation "
204 "information without DXIL part.");
205 if (Error Err = PSVInfo->parse(ShaderKind: DXIL->first.ShaderKind))
206 return Err;
207 }
208 return Error::success();
209}
210
211Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) {
212 DXContainer Container(Object);
213 if (Error Err = Container.parseHeader())
214 return std::move(Err);
215 if (Error Err = Container.parsePartOffsets())
216 return std::move(Err);
217 return Container;
218}
219
220void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
221 StringRef Buffer = Container.Data.getBuffer();
222 const char *Current = Buffer.data() + Offset;
223 // Offsets are validated during parsing, so all offsets in the container are
224 // valid and contain enough readable data to read a header.
225 cantFail(Err: readStruct(Buffer, Src: Current, Struct&: IteratorState.Part));
226 IteratorState.Data =
227 StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size);
228 IteratorState.Offset = Offset;
229}
230
231Error DirectX::PSVRuntimeInfo::parse(uint16_t ShaderKind) {
232 Triple::EnvironmentType ShaderStage = dxbc::getShaderStage(Kind: ShaderKind);
233
234 const char *Current = Data.begin();
235 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: Size))
236 return Err;
237 Current += sizeof(uint32_t);
238
239 StringRef PSVInfoData = Data.substr(Start: sizeof(uint32_t), N: Size);
240
241 if (PSVInfoData.size() < Size)
242 return parseFailed(
243 Msg: "Pipeline state data extends beyond the bounds of the part");
244
245 using namespace dxbc::PSV;
246
247 const uint32_t PSVVersion = getVersion();
248
249 // Detect the PSVVersion by looking at the size field.
250 if (PSVVersion == 3) {
251 v3::RuntimeInfo Info;
252 if (Error Err = readStruct(Buffer: PSVInfoData, Src: Current, Struct&: Info))
253 return Err;
254 if (sys::IsBigEndianHost)
255 Info.swapBytes(Stage: ShaderStage);
256 BasicInfo = Info;
257 } else if (PSVVersion == 2) {
258 v2::RuntimeInfo Info;
259 if (Error Err = readStruct(Buffer: PSVInfoData, Src: Current, Struct&: Info))
260 return Err;
261 if (sys::IsBigEndianHost)
262 Info.swapBytes(Stage: ShaderStage);
263 BasicInfo = Info;
264 } else if (PSVVersion == 1) {
265 v1::RuntimeInfo Info;
266 if (Error Err = readStruct(Buffer: PSVInfoData, Src: Current, Struct&: Info))
267 return Err;
268 if (sys::IsBigEndianHost)
269 Info.swapBytes(Stage: ShaderStage);
270 BasicInfo = Info;
271 } else if (PSVVersion == 0) {
272 v0::RuntimeInfo Info;
273 if (Error Err = readStruct(Buffer: PSVInfoData, Src: Current, Struct&: Info))
274 return Err;
275 if (sys::IsBigEndianHost)
276 Info.swapBytes(Stage: ShaderStage);
277 BasicInfo = Info;
278 } else
279 return parseFailed(
280 Msg: "Cannot read PSV Runtime Info, unsupported PSV version.");
281
282 Current += Size;
283
284 uint32_t ResourceCount = 0;
285 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: ResourceCount))
286 return Err;
287 Current += sizeof(uint32_t);
288
289 if (ResourceCount > 0) {
290 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: Resources.Stride))
291 return Err;
292 Current += sizeof(uint32_t);
293
294 size_t BindingDataSize = Resources.Stride * ResourceCount;
295 Resources.Data = Data.substr(Start: Current - Data.begin(), N: BindingDataSize);
296
297 if (Resources.Data.size() < BindingDataSize)
298 return parseFailed(
299 Msg: "Resource binding data extends beyond the bounds of the part");
300
301 Current += BindingDataSize;
302 } else
303 Resources.Stride = sizeof(v2::ResourceBindInfo);
304
305 // PSV version 0 ends after the resource bindings.
306 if (PSVVersion == 0)
307 return Error::success();
308
309 // String table starts at a 4-byte offset.
310 Current = reinterpret_cast<const char *>(
311 alignTo<4>(Value: reinterpret_cast<uintptr_t>(Current)));
312
313 uint32_t StringTableSize = 0;
314 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: StringTableSize))
315 return Err;
316 if (StringTableSize % 4 != 0)
317 return parseFailed(Msg: "String table misaligned");
318 Current += sizeof(uint32_t);
319 StringTable = StringRef(Current, StringTableSize);
320
321 Current += StringTableSize;
322
323 uint32_t SemanticIndexTableSize = 0;
324 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: SemanticIndexTableSize))
325 return Err;
326 Current += sizeof(uint32_t);
327
328 SemanticIndexTable.reserve(N: SemanticIndexTableSize);
329 for (uint32_t I = 0; I < SemanticIndexTableSize; ++I) {
330 uint32_t Index = 0;
331 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: Index))
332 return Err;
333 Current += sizeof(uint32_t);
334 SemanticIndexTable.push_back(Elt: Index);
335 }
336
337 uint8_t InputCount = getSigInputCount();
338 uint8_t OutputCount = getSigOutputCount();
339 uint8_t PatchOrPrimCount = getSigPatchOrPrimCount();
340
341 uint32_t ElementCount = InputCount + OutputCount + PatchOrPrimCount;
342
343 if (ElementCount > 0) {
344 if (Error Err = readInteger(Buffer: Data, Src: Current, Val&: SigInputElements.Stride))
345 return Err;
346 Current += sizeof(uint32_t);
347 // Assign the stride to all the arrays.
348 SigOutputElements.Stride = SigPatchOrPrimElements.Stride =
349 SigInputElements.Stride;
350
351 if (Data.end() - Current <
352 (ptrdiff_t)(ElementCount * SigInputElements.Stride))
353 return parseFailed(
354 Msg: "Signature elements extend beyond the size of the part");
355
356 size_t InputSize = SigInputElements.Stride * InputCount;
357 SigInputElements.Data = Data.substr(Start: Current - Data.begin(), N: InputSize);
358 Current += InputSize;
359
360 size_t OutputSize = SigOutputElements.Stride * OutputCount;
361 SigOutputElements.Data = Data.substr(Start: Current - Data.begin(), N: OutputSize);
362 Current += OutputSize;
363
364 size_t PSize = SigPatchOrPrimElements.Stride * PatchOrPrimCount;
365 SigPatchOrPrimElements.Data = Data.substr(Start: Current - Data.begin(), N: PSize);
366 Current += PSize;
367 }
368
369 ArrayRef<uint8_t> OutputVectorCounts = getOutputVectorCounts();
370 uint8_t PatchConstOrPrimVectorCount = getPatchConstOrPrimVectorCount();
371 uint8_t InputVectorCount = getInputVectorCount();
372
373 auto maskDwordSize = [](uint8_t Vector) {
374 return (static_cast<uint32_t>(Vector) + 7) >> 3;
375 };
376
377 auto mapTableSize = [maskDwordSize](uint8_t X, uint8_t Y) {
378 return maskDwordSize(Y) * X * 4;
379 };
380
381 if (usesViewID()) {
382 for (uint32_t I = 0; I < OutputVectorCounts.size(); ++I) {
383 // The vector mask is one bit per component and 4 components per vector.
384 // We can compute the number of dwords required by rounding up to the next
385 // multiple of 8.
386 uint32_t NumDwords =
387 maskDwordSize(static_cast<uint32_t>(OutputVectorCounts[I]));
388 size_t NumBytes = NumDwords * sizeof(uint32_t);
389 OutputVectorMasks[I].Data = Data.substr(Start: Current - Data.begin(), N: NumBytes);
390 Current += NumBytes;
391 }
392
393 if (ShaderStage == Triple::Hull && PatchConstOrPrimVectorCount > 0) {
394 uint32_t NumDwords = maskDwordSize(PatchConstOrPrimVectorCount);
395 size_t NumBytes = NumDwords * sizeof(uint32_t);
396 PatchOrPrimMasks.Data = Data.substr(Start: Current - Data.begin(), N: NumBytes);
397 Current += NumBytes;
398 }
399 }
400
401 // Input/Output mapping table
402 for (uint32_t I = 0; I < OutputVectorCounts.size(); ++I) {
403 if (InputVectorCount == 0 || OutputVectorCounts[I] == 0)
404 continue;
405 uint32_t NumDwords = mapTableSize(InputVectorCount, OutputVectorCounts[I]);
406 size_t NumBytes = NumDwords * sizeof(uint32_t);
407 InputOutputMap[I].Data = Data.substr(Start: Current - Data.begin(), N: NumBytes);
408 Current += NumBytes;
409 }
410
411 // Hull shader: Input/Patch mapping table
412 if (ShaderStage == Triple::Hull && PatchConstOrPrimVectorCount > 0 &&
413 InputVectorCount > 0) {
414 uint32_t NumDwords =
415 mapTableSize(InputVectorCount, PatchConstOrPrimVectorCount);
416 size_t NumBytes = NumDwords * sizeof(uint32_t);
417 InputPatchMap.Data = Data.substr(Start: Current - Data.begin(), N: NumBytes);
418 Current += NumBytes;
419 }
420
421 // Domain Shader: Patch/Output mapping table
422 if (ShaderStage == Triple::Domain && PatchConstOrPrimVectorCount > 0 &&
423 OutputVectorCounts[0] > 0) {
424 uint32_t NumDwords =
425 mapTableSize(PatchConstOrPrimVectorCount, OutputVectorCounts[0]);
426 size_t NumBytes = NumDwords * sizeof(uint32_t);
427 PatchOutputMap.Data = Data.substr(Start: Current - Data.begin(), N: NumBytes);
428 Current += NumBytes;
429 }
430
431 return Error::success();
432}
433
434uint8_t DirectX::PSVRuntimeInfo::getSigInputCount() const {
435 if (const auto *P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(ptr: &BasicInfo))
436 return P->SigInputElements;
437 if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(ptr: &BasicInfo))
438 return P->SigInputElements;
439 if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(ptr: &BasicInfo))
440 return P->SigInputElements;
441 return 0;
442}
443
444uint8_t DirectX::PSVRuntimeInfo::getSigOutputCount() const {
445 if (const auto *P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(ptr: &BasicInfo))
446 return P->SigOutputElements;
447 if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(ptr: &BasicInfo))
448 return P->SigOutputElements;
449 if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(ptr: &BasicInfo))
450 return P->SigOutputElements;
451 return 0;
452}
453
454uint8_t DirectX::PSVRuntimeInfo::getSigPatchOrPrimCount() const {
455 if (const auto *P = std::get_if<dxbc::PSV::v3::RuntimeInfo>(ptr: &BasicInfo))
456 return P->SigPatchOrPrimElements;
457 if (const auto *P = std::get_if<dxbc::PSV::v2::RuntimeInfo>(ptr: &BasicInfo))
458 return P->SigPatchOrPrimElements;
459 if (const auto *P = std::get_if<dxbc::PSV::v1::RuntimeInfo>(ptr: &BasicInfo))
460 return P->SigPatchOrPrimElements;
461 return 0;
462}
463