1 //===- TpiStream.cpp - PDB Type Info (TPI) Stream 2 Access ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/DebugInfo/PDB/Native/TpiStream.h" 11 #include "llvm/ADT/iterator_range.h" 12 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 13 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 14 #include "llvm/DebugInfo/CodeView/TypeRecord.h" 15 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" 16 #include "llvm/DebugInfo/MSF/MappedBlockStream.h" 17 #include "llvm/DebugInfo/PDB/Native/PDBFile.h" 18 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h" 19 #include "llvm/DebugInfo/PDB/Native/RawConstants.h" 20 #include "llvm/DebugInfo/PDB/Native/RawError.h" 21 #include "llvm/DebugInfo/PDB/Native/RawTypes.h" 22 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 23 #include "llvm/Support/BinaryStreamReader.h" 24 #include "llvm/Support/Endian.h" 25 #include "llvm/Support/Error.h" 26 #include <algorithm> 27 #include <cstdint> 28 #include <vector> 29 30 using namespace llvm; 31 using namespace llvm::codeview; 32 using namespace llvm::support; 33 using namespace llvm::msf; 34 using namespace llvm::pdb; 35 36 TpiStream::TpiStream(const PDBFile &File, 37 std::unique_ptr<MappedBlockStream> Stream) 38 : Pdb(File), Stream(std::move(Stream)) {} 39 40 TpiStream::~TpiStream() = default; 41 42 // Verifies that a given type record matches with a given hash value. 43 // Currently we only verify SRC_LINE records. 44 Error TpiStream::verifyHashValues() { 45 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets); 46 TypeDeserializer Deserializer; 47 48 TypeVisitorCallbackPipeline Pipeline; 49 Pipeline.addCallbackToPipeline(Deserializer); 50 Pipeline.addCallbackToPipeline(Verifier); 51 52 CVTypeVisitor Visitor(Pipeline); 53 return Visitor.visitTypeStream(TypeRecords); 54 } 55 56 Error TpiStream::reload() { 57 BinaryStreamReader Reader(*Stream); 58 59 if (Reader.bytesRemaining() < sizeof(TpiStreamHeader)) 60 return make_error<RawError>(raw_error_code::corrupt_file, 61 "TPI Stream does not contain a header."); 62 63 if (Reader.readObject(Header)) 64 return make_error<RawError>(raw_error_code::corrupt_file, 65 "TPI Stream does not contain a header."); 66 67 if (Header->Version != PdbTpiV80) 68 return make_error<RawError>(raw_error_code::corrupt_file, 69 "Unsupported TPI Version."); 70 71 if (Header->HeaderSize != sizeof(TpiStreamHeader)) 72 return make_error<RawError>(raw_error_code::corrupt_file, 73 "Corrupt TPI Header size."); 74 75 if (Header->HashKeySize != sizeof(ulittle32_t)) 76 return make_error<RawError>(raw_error_code::corrupt_file, 77 "TPI Stream expected 4 byte hash key size."); 78 79 if (Header->NumHashBuckets < MinTpiHashBuckets || 80 Header->NumHashBuckets > MaxTpiHashBuckets) 81 return make_error<RawError>(raw_error_code::corrupt_file, 82 "TPI Stream Invalid number of hash buckets."); 83 84 // The actual type records themselves come from this stream 85 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes)) 86 return EC; 87 88 // Hash indices, hash values, etc come from the hash stream. 89 if (Header->HashStreamIndex != kInvalidStreamIndex) { 90 if (Header->HashStreamIndex >= Pdb.getNumStreams()) 91 return make_error<RawError>(raw_error_code::corrupt_file, 92 "Invalid TPI hash stream index."); 93 94 auto HS = MappedBlockStream::createIndexedStream( 95 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), Header->HashStreamIndex); 96 BinaryStreamReader HSR(*HS); 97 98 uint32_t NumHashValues = 99 Header->HashValueBuffer.Length / sizeof(ulittle32_t); 100 if (NumHashValues != NumTypeRecords()) 101 return make_error<RawError>( 102 raw_error_code::corrupt_file, 103 "TPI hash count does not match with the number of type records."); 104 HSR.setOffset(Header->HashValueBuffer.Off); 105 if (auto EC = HSR.readArray(HashValues, NumHashValues)) 106 return EC; 107 std::vector<ulittle32_t> HashValueList; 108 for (auto I : HashValues) 109 HashValueList.push_back(I); 110 111 HSR.setOffset(Header->IndexOffsetBuffer.Off); 112 uint32_t NumTypeIndexOffsets = 113 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset); 114 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets)) 115 return EC; 116 117 if (Header->HashAdjBuffer.Length > 0) { 118 HSR.setOffset(Header->HashAdjBuffer.Off); 119 if (auto EC = HashAdjusters.load(HSR)) 120 return EC; 121 } 122 123 HashStream = std::move(HS); 124 125 // TPI hash table is a parallel array for the type records. 126 // Verify that the hash values match with type records. 127 if (auto EC = verifyHashValues()) 128 return EC; 129 } 130 131 return Error::success(); 132 } 133 134 PdbRaw_TpiVer TpiStream::getTpiVersion() const { 135 uint32_t Value = Header->Version; 136 return static_cast<PdbRaw_TpiVer>(Value); 137 } 138 139 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; } 140 141 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; } 142 143 uint32_t TpiStream::NumTypeRecords() const { 144 return TypeIndexEnd() - TypeIndexBegin(); 145 } 146 147 uint16_t TpiStream::getTypeHashStreamIndex() const { 148 return Header->HashStreamIndex; 149 } 150 151 uint16_t TpiStream::getTypeHashStreamAuxIndex() const { 152 return Header->HashAuxStreamIndex; 153 } 154 155 uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; } 156 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; } 157 158 FixedStreamArray<support::ulittle32_t> TpiStream::getHashValues() const { 159 return HashValues; 160 } 161 162 FixedStreamArray<TypeIndexOffset> TpiStream::getTypeIndexOffsets() const { 163 return TypeIndexOffsets; 164 } 165 166 HashTable &TpiStream::getHashAdjusters() { return HashAdjusters; } 167 168 CVTypeRange TpiStream::types(bool *HadError) const { 169 return make_range(TypeRecords.begin(HadError), TypeRecords.end()); 170 } 171 172 Error TpiStream::commit() { return Error::success(); } 173