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 13 using namespace llvm; 14 using namespace llvm::object; 15 16 static Error parseFailed(const Twine &Msg) { 17 return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed); 18 } 19 20 template <typename T> 21 static Error readStruct(StringRef Buffer, const char *Src, T &Struct) { 22 // Don't read before the beginning or past the end of the file 23 if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end()) 24 return parseFailed("Reading structure out of file bounds"); 25 26 memcpy(&Struct, Src, sizeof(T)); 27 // DXContainer is always little endian 28 if (sys::IsBigEndianHost) 29 Struct.swapBytes(); 30 return Error::success(); 31 } 32 33 template <typename T> 34 static Error readInteger(StringRef Buffer, const char *Src, T &Val) { 35 static_assert(std::is_integral<T>::value, 36 "Cannot call readInteger on non-integral type."); 37 assert(reinterpret_cast<uintptr_t>(Src) % alignof(T) == 0 && 38 "Unaligned read of value from buffer!"); 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("Reading structure out of file bounds"); 42 43 Val = *reinterpret_cast<const T *>(Src); 44 // DXContainer is always little endian 45 if (sys::IsBigEndianHost) 46 sys::swapByteOrder(Val); 47 return Error::success(); 48 } 49 50 DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {} 51 52 Error DXContainer::parseHeader() { 53 return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header); 54 } 55 56 Error DXContainer::parsePartOffsets() { 57 const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header); 58 for (uint32_t Part = 0; Part < Header.PartCount; ++Part) { 59 uint32_t PartOffset; 60 if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset)) 61 return Err; 62 Current += sizeof(uint32_t); 63 if (PartOffset + sizeof(dxbc::PartHeader) > Data.getBufferSize()) 64 return parseFailed("Part offset points beyond boundary of the file"); 65 PartOffsets.push_back(PartOffset); 66 } 67 return Error::success(); 68 } 69 70 Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) { 71 DXContainer Container(Object); 72 if (Error Err = Container.parseHeader()) 73 return std::move(Err); 74 if (Error Err = Container.parsePartOffsets()) 75 return std::move(Err); 76 return Container; 77 } 78 79 void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) { 80 StringRef Buffer = Container.Data.getBuffer(); 81 const char *Current = Buffer.data() + Offset; 82 // Offsets are validated during parsing, so all offsets in the container are 83 // valid and contain enough readable data to read a header. 84 cantFail(readStruct(Buffer, Current, IteratorState.Part)); 85 IteratorState.Data = 86 StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size); 87 } 88