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/FormatVariadic.h" 13 14 using namespace llvm; 15 using namespace llvm::object; 16 17 static Error parseFailed(const Twine &Msg) { 18 return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed); 19 } 20 21 template <typename T> 22 static Error readStruct(StringRef Buffer, const char *Src, T &Struct) { 23 // Don't read before the beginning or past the end of the file 24 if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end()) 25 return parseFailed("Reading structure out of file bounds"); 26 27 memcpy(&Struct, Src, sizeof(T)); 28 // DXContainer is always little endian 29 if (sys::IsBigEndianHost) 30 Struct.swapBytes(); 31 return Error::success(); 32 } 33 34 template <typename T> 35 static Error readInteger(StringRef Buffer, const char *Src, T &Val, 36 Twine Str = "structure") { 37 static_assert(std::is_integral_v<T>, 38 "Cannot call readInteger on non-integral type."); 39 // Don't read before the beginning or past the end of the file 40 if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end()) 41 return parseFailed(Twine("Reading ") + Str + " out of file bounds"); 42 43 // The DXContainer offset table is comprised of uint32_t values but not padded 44 // to a 64-bit boundary. So Parts may start unaligned if there is an odd 45 // number of parts and part data itself is not required to be padded. 46 if (reinterpret_cast<uintptr_t>(Src) % alignof(T) != 0) 47 memcpy(reinterpret_cast<char *>(&Val), Src, sizeof(T)); 48 else 49 Val = *reinterpret_cast<const T *>(Src); 50 // DXContainer is always little endian 51 if (sys::IsBigEndianHost) 52 sys::swapByteOrder(Val); 53 return Error::success(); 54 } 55 56 DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {} 57 58 Error DXContainer::parseHeader() { 59 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header); 60 } 61 62 Error DXContainer::parseDXILHeader(StringRef Part) { 63 if (DXIL) 64 return parseFailed("More than one DXIL part is present in the file"); 65 const char *Current = Part.begin(); 66 dxbc::ProgramHeader Header; 67 if (Error Err = readStruct(Part, Current, Header)) 68 return Err; 69 Current += offsetof(dxbc::ProgramHeader, Bitcode) + Header.Bitcode.Offset; 70 DXIL.emplace(std::make_pair(Header, Current)); 71 return Error::success(); 72 } 73 74 Error DXContainer::parseShaderFlags(StringRef Part) { 75 if (ShaderFlags) 76 return parseFailed("More than one SFI0 part is present in the file"); 77 uint64_t FlagValue = 0; 78 if (Error Err = readInteger(Part, Part.begin(), FlagValue)) 79 return Err; 80 ShaderFlags = FlagValue; 81 return Error::success(); 82 } 83 84 Error DXContainer::parseHash(StringRef Part) { 85 if (Hash) 86 return parseFailed("More than one HASH part is present in the file"); 87 dxbc::ShaderHash ReadHash; 88 if (Error Err = readStruct(Part, Part.begin(), ReadHash)) 89 return Err; 90 Hash = ReadHash; 91 return Error::success(); 92 } 93 94 Error DXContainer::parsePartOffsets() { 95 uint32_t LastOffset = 96 sizeof(dxbc::Header) + (Header.PartCount * sizeof(uint32_t)); 97 const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header); 98 for (uint32_t Part = 0; Part < Header.PartCount; ++Part) { 99 uint32_t PartOffset; 100 if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset)) 101 return Err; 102 if (PartOffset < LastOffset) 103 return parseFailed( 104 formatv( 105 "Part offset for part {0} begins before the previous part ends", 106 Part) 107 .str()); 108 Current += sizeof(uint32_t); 109 if (PartOffset >= Data.getBufferSize()) 110 return parseFailed("Part offset points beyond boundary of the file"); 111 // To prevent overflow when reading the part name, we subtract the part name 112 // size from the buffer size, rather than adding to the offset. Since the 113 // file header is larger than the part header we can't reach this code 114 // unless the buffer is at least as large as a part header, so this 115 // subtraction can't underflow. 116 if (PartOffset >= Data.getBufferSize() - sizeof(dxbc::PartHeader::Name)) 117 return parseFailed("File not large enough to read part name"); 118 PartOffsets.push_back(PartOffset); 119 120 dxbc::PartType PT = 121 dxbc::parsePartType(Data.getBuffer().substr(PartOffset, 4)); 122 uint32_t PartDataStart = PartOffset + sizeof(dxbc::PartHeader); 123 uint32_t PartSize; 124 if (Error Err = readInteger(Data.getBuffer(), 125 Data.getBufferStart() + PartOffset + 4, 126 PartSize, "part size")) 127 return Err; 128 StringRef PartData = Data.getBuffer().substr(PartDataStart, PartSize); 129 LastOffset = PartOffset + PartSize; 130 switch (PT) { 131 case dxbc::PartType::DXIL: 132 if (Error Err = parseDXILHeader(PartData)) 133 return Err; 134 break; 135 case dxbc::PartType::SFI0: 136 if (Error Err = parseShaderFlags(PartData)) 137 return Err; 138 break; 139 case dxbc::PartType::HASH: 140 if (Error Err = parseHash(PartData)) 141 return Err; 142 break; 143 case dxbc::PartType::Unknown: 144 break; 145 } 146 } 147 return Error::success(); 148 } 149 150 Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) { 151 DXContainer Container(Object); 152 if (Error Err = Container.parseHeader()) 153 return std::move(Err); 154 if (Error Err = Container.parsePartOffsets()) 155 return std::move(Err); 156 return Container; 157 } 158 159 void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) { 160 StringRef Buffer = Container.Data.getBuffer(); 161 const char *Current = Buffer.data() + Offset; 162 // Offsets are validated during parsing, so all offsets in the container are 163 // valid and contain enough readable data to read a header. 164 cantFail(readStruct(Buffer, Current, IteratorState.Part)); 165 IteratorState.Data = 166 StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size); 167 IteratorState.Offset = Offset; 168 } 169