14070aa01SChris Bieneman //===- DXContainer.cpp - DXContainer object file implementation -----------===//
24070aa01SChris Bieneman //
34070aa01SChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44070aa01SChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
54070aa01SChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64070aa01SChris Bieneman //
74070aa01SChris Bieneman //===----------------------------------------------------------------------===//
84070aa01SChris Bieneman 
94070aa01SChris Bieneman #include "llvm/Object/DXContainer.h"
104070aa01SChris Bieneman #include "llvm/BinaryFormat/DXContainer.h"
114070aa01SChris Bieneman #include "llvm/Object/Error.h"
124070aa01SChris Bieneman 
134070aa01SChris Bieneman using namespace llvm;
144070aa01SChris Bieneman using namespace llvm::object;
154070aa01SChris Bieneman 
parseFailed(const Twine & Msg)164070aa01SChris Bieneman static Error parseFailed(const Twine &Msg) {
174070aa01SChris Bieneman   return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed);
184070aa01SChris Bieneman }
194070aa01SChris Bieneman 
204070aa01SChris Bieneman template <typename T>
readStruct(StringRef Buffer,const char * Src,T & Struct)219e3919daSChris Bieneman static Error readStruct(StringRef Buffer, const char *Src, T &Struct) {
224070aa01SChris Bieneman   // Don't read before the beginning or past the end of the file
239e3919daSChris Bieneman   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
244070aa01SChris Bieneman     return parseFailed("Reading structure out of file bounds");
254070aa01SChris Bieneman 
269e3919daSChris Bieneman   memcpy(&Struct, Src, sizeof(T));
2747258ffcSChris Bieneman   // DXContainer is always little endian
284070aa01SChris Bieneman   if (sys::IsBigEndianHost)
299e3919daSChris Bieneman     Struct.swapBytes();
309e3919daSChris Bieneman   return Error::success();
319e3919daSChris Bieneman }
329e3919daSChris Bieneman 
339e3919daSChris Bieneman template <typename T>
readInteger(StringRef Buffer,const char * Src,T & Val)349e3919daSChris Bieneman static Error readInteger(StringRef Buffer, const char *Src, T &Val) {
359e3919daSChris Bieneman   static_assert(std::is_integral<T>::value,
369e3919daSChris Bieneman                 "Cannot call readInteger on non-integral type.");
379e3919daSChris Bieneman   assert(reinterpret_cast<uintptr_t>(Src) % alignof(T) == 0 &&
389e3919daSChris Bieneman          "Unaligned read of value from buffer!");
399e3919daSChris Bieneman   // Don't read before the beginning or past the end of the file
409e3919daSChris Bieneman   if (Src < Buffer.begin() || Src + sizeof(T) > Buffer.end())
419e3919daSChris Bieneman     return parseFailed("Reading structure out of file bounds");
429e3919daSChris Bieneman 
439e3919daSChris Bieneman   Val = *reinterpret_cast<const T *>(Src);
449e3919daSChris Bieneman   // DXContainer is always little endian
459e3919daSChris Bieneman   if (sys::IsBigEndianHost)
469e3919daSChris Bieneman     sys::swapByteOrder(Val);
474070aa01SChris Bieneman   return Error::success();
484070aa01SChris Bieneman }
494070aa01SChris Bieneman 
DXContainer(MemoryBufferRef O)504070aa01SChris Bieneman DXContainer::DXContainer(MemoryBufferRef O) : Data(O) {}
514070aa01SChris Bieneman 
parseHeader()524070aa01SChris Bieneman Error DXContainer::parseHeader() {
534070aa01SChris Bieneman   return readStruct(Data.getBuffer(), Data.getBuffer().data(), Header);
544070aa01SChris Bieneman }
554070aa01SChris Bieneman 
parseDXILHeader(uint32_t Offset)56*21c94523SChris Bieneman Error DXContainer::parseDXILHeader(uint32_t Offset) {
57*21c94523SChris Bieneman   if (DXIL)
58*21c94523SChris Bieneman     return parseFailed("More than one DXIL part is present in the file");
59*21c94523SChris Bieneman   const char *Current = Data.getBuffer().data() + Offset;
60*21c94523SChris Bieneman   dxbc::ProgramHeader Header;
61*21c94523SChris Bieneman   if (Error Err = readStruct(Data.getBuffer(), Current, Header))
62*21c94523SChris Bieneman     return Err;
63*21c94523SChris Bieneman   Current += offsetof(dxbc::ProgramHeader, Bitcode) + Header.Bitcode.Offset;
64*21c94523SChris Bieneman   DXIL.emplace(std::make_pair(Header, Current));
65*21c94523SChris Bieneman   return Error::success();
66*21c94523SChris Bieneman }
67*21c94523SChris Bieneman 
parsePartOffsets()689e3919daSChris Bieneman Error DXContainer::parsePartOffsets() {
699e3919daSChris Bieneman   const char *Current = Data.getBuffer().data() + sizeof(dxbc::Header);
709e3919daSChris Bieneman   for (uint32_t Part = 0; Part < Header.PartCount; ++Part) {
719e3919daSChris Bieneman     uint32_t PartOffset;
729e3919daSChris Bieneman     if (Error Err = readInteger(Data.getBuffer(), Current, PartOffset))
739e3919daSChris Bieneman       return Err;
749e3919daSChris Bieneman     Current += sizeof(uint32_t);
750498415fSChris Bieneman     // We need to ensure that each part offset leaves enough space for a part
760498415fSChris Bieneman     // header. To prevent overflow, we subtract the part header size from the
770498415fSChris Bieneman     // buffer size, rather than adding to the offset. Since the file header is
780498415fSChris Bieneman     // larger than the part header we can't reach this code unless the buffer
790498415fSChris Bieneman     // is larger than the part header, so this can't underflow.
800498415fSChris Bieneman     if (PartOffset > Data.getBufferSize() - sizeof(dxbc::PartHeader))
819e3919daSChris Bieneman       return parseFailed("Part offset points beyond boundary of the file");
829e3919daSChris Bieneman     PartOffsets.push_back(PartOffset);
83*21c94523SChris Bieneman 
84*21c94523SChris Bieneman     // If this isn't a dxil part stop here...
85*21c94523SChris Bieneman     if (Data.getBuffer().substr(PartOffset, 4) != "DXIL")
86*21c94523SChris Bieneman       continue;
87*21c94523SChris Bieneman     if (Error Err = parseDXILHeader(PartOffset + sizeof(dxbc::PartHeader)))
88*21c94523SChris Bieneman       return Err;
899e3919daSChris Bieneman   }
909e3919daSChris Bieneman   return Error::success();
919e3919daSChris Bieneman }
929e3919daSChris Bieneman 
create(MemoryBufferRef Object)934070aa01SChris Bieneman Expected<DXContainer> DXContainer::create(MemoryBufferRef Object) {
944070aa01SChris Bieneman   DXContainer Container(Object);
954070aa01SChris Bieneman   if (Error Err = Container.parseHeader())
96b26e44e6SChris Bieneman     return std::move(Err);
979e3919daSChris Bieneman   if (Error Err = Container.parsePartOffsets())
989e3919daSChris Bieneman     return std::move(Err);
994070aa01SChris Bieneman   return Container;
1004070aa01SChris Bieneman }
1019e3919daSChris Bieneman 
updateIteratorImpl(const uint32_t Offset)1029e3919daSChris Bieneman void DXContainer::PartIterator::updateIteratorImpl(const uint32_t Offset) {
1039e3919daSChris Bieneman   StringRef Buffer = Container.Data.getBuffer();
1049e3919daSChris Bieneman   const char *Current = Buffer.data() + Offset;
1059e3919daSChris Bieneman   // Offsets are validated during parsing, so all offsets in the container are
1069e3919daSChris Bieneman   // valid and contain enough readable data to read a header.
1079e3919daSChris Bieneman   cantFail(readStruct(Buffer, Current, IteratorState.Part));
1089e3919daSChris Bieneman   IteratorState.Data =
1099e3919daSChris Bieneman       StringRef(Current + sizeof(dxbc::PartHeader), IteratorState.Part.Size);
110352c395fSChris Bieneman   IteratorState.Offset = Offset;
1119e3919daSChris Bieneman }
112