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/Raw/TpiStream.h"
11
12 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
13 #include "llvm/DebugInfo/CodeView/CodeView.h"
14 #include "llvm/DebugInfo/CodeView/StreamReader.h"
15 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
16 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
17 #include "llvm/DebugInfo/PDB/Raw/Hash.h"
18 #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
19 #include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
20 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
21 #include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
22 #include "llvm/DebugInfo/PDB/Raw/RawError.h"
23 #include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
24
25 #include "llvm/Support/Endian.h"
26
27 using namespace llvm;
28 using namespace llvm::codeview;
29 using namespace llvm::support;
30 using namespace llvm::pdb;
31
32 namespace {
33 const uint32_t MinHashBuckets = 0x1000;
34 const uint32_t MaxHashBuckets = 0x40000;
35 }
36
37 // This corresponds to `HDR` in PDB/dbi/tpi.h.
38 struct TpiStream::HeaderInfo {
39 struct EmbeddedBuf {
40 little32_t Off;
41 ulittle32_t Length;
42 };
43
44 ulittle32_t Version;
45 ulittle32_t HeaderSize;
46 ulittle32_t TypeIndexBegin;
47 ulittle32_t TypeIndexEnd;
48 ulittle32_t TypeRecordBytes;
49
50 // The following members correspond to `TpiHash` in PDB/dbi/tpi.h.
51 ulittle16_t HashStreamIndex;
52 ulittle16_t HashAuxStreamIndex;
53 ulittle32_t HashKeySize;
54 ulittle32_t NumHashBuckets;
55
56 EmbeddedBuf HashValueBuffer;
57 EmbeddedBuf IndexOffsetBuffer;
58 EmbeddedBuf HashAdjBuffer;
59 };
60
TpiStream(const PDBFile & File,std::unique_ptr<MappedBlockStream> Stream)61 TpiStream::TpiStream(const PDBFile &File,
62 std::unique_ptr<MappedBlockStream> Stream)
63 : Pdb(File), Stream(std::move(Stream)) {}
64
~TpiStream()65 TpiStream::~TpiStream() {}
66
67 // Corresponds to `fUDTAnon`.
isAnonymous(T & Rec)68 template <typename T> static bool isAnonymous(T &Rec) {
69 StringRef Name = Rec.getName();
70 return Name == "<unnamed-tag>" || Name == "__unnamed" ||
71 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
72 }
73
74 // Computes a hash for a given TPI record.
75 template <typename T>
getTpiHash(T & Rec,const CVRecord<TypeLeafKind> & RawRec)76 static uint32_t getTpiHash(T &Rec, const CVRecord<TypeLeafKind> &RawRec) {
77 auto Opts = static_cast<uint16_t>(Rec.getOptions());
78
79 bool ForwardRef =
80 Opts & static_cast<uint16_t>(ClassOptions::ForwardReference);
81 bool Scoped = Opts & static_cast<uint16_t>(ClassOptions::Scoped);
82 bool UniqueName = Opts & static_cast<uint16_t>(ClassOptions::HasUniqueName);
83 bool IsAnon = UniqueName && isAnonymous(Rec);
84
85 if (!ForwardRef && !Scoped && !IsAnon)
86 return hashStringV1(Rec.getName());
87 if (!ForwardRef && UniqueName && !IsAnon)
88 return hashStringV1(Rec.getUniqueName());
89 return hashBufferV8(RawRec.RawData);
90 }
91
92 namespace {
93 class TpiHashVerifier : public TypeVisitorCallbacks {
94 public:
TpiHashVerifier(FixedStreamArray<support::ulittle32_t> & HashValues,uint32_t NumHashBuckets)95 TpiHashVerifier(FixedStreamArray<support::ulittle32_t> &HashValues,
96 uint32_t NumHashBuckets)
97 : HashValues(HashValues), NumHashBuckets(NumHashBuckets) {}
98
visitUdtSourceLine(UdtSourceLineRecord & Rec)99 Error visitUdtSourceLine(UdtSourceLineRecord &Rec) override {
100 return verifySourceLine(Rec);
101 }
102
visitUdtModSourceLine(UdtModSourceLineRecord & Rec)103 Error visitUdtModSourceLine(UdtModSourceLineRecord &Rec) override {
104 return verifySourceLine(Rec);
105 }
106
visitClass(ClassRecord & Rec)107 Error visitClass(ClassRecord &Rec) override { return verify(Rec); }
visitEnum(EnumRecord & Rec)108 Error visitEnum(EnumRecord &Rec) override { return verify(Rec); }
visitUnion(UnionRecord & Rec)109 Error visitUnion(UnionRecord &Rec) override { return verify(Rec); }
110
visitTypeBegin(const CVRecord<TypeLeafKind> & Rec)111 Error visitTypeBegin(const CVRecord<TypeLeafKind> &Rec) override {
112 ++Index;
113 RawRecord = &Rec;
114 return Error::success();
115 }
116
117 private:
verify(T & Rec)118 template <typename T> Error verify(T &Rec) {
119 uint32_t Hash = getTpiHash(Rec, *RawRecord);
120 if (Hash % NumHashBuckets != HashValues[Index])
121 return errorInvalidHash();
122 return Error::success();
123 }
124
verifySourceLine(T & Rec)125 template <typename T> Error verifySourceLine(T &Rec) {
126 char Buf[4];
127 support::endian::write32le(Buf, Rec.getUDT().getIndex());
128 uint32_t Hash = hashStringV1(StringRef(Buf, 4));
129 if (Hash % NumHashBuckets != HashValues[Index])
130 return errorInvalidHash();
131 return Error::success();
132 }
133
errorInvalidHash()134 Error errorInvalidHash() {
135 return make_error<RawError>(
136 raw_error_code::invalid_tpi_hash,
137 "Type index is 0x" + utohexstr(TypeIndex::FirstNonSimpleIndex + Index));
138 }
139
140 FixedStreamArray<support::ulittle32_t> HashValues;
141 const CVRecord<TypeLeafKind> *RawRecord;
142 uint32_t NumHashBuckets;
143 uint32_t Index = -1;
144 };
145 }
146
147 // Verifies that a given type record matches with a given hash value.
148 // Currently we only verify SRC_LINE records.
verifyHashValues()149 Error TpiStream::verifyHashValues() {
150 TpiHashVerifier Verifier(HashValues, Header->NumHashBuckets);
151 CVTypeVisitor Visitor(Verifier);
152 return Visitor.visitTypeStream(TypeRecords);
153 }
154
reload()155 Error TpiStream::reload() {
156 StreamReader Reader(*Stream);
157
158 if (Reader.bytesRemaining() < sizeof(HeaderInfo))
159 return make_error<RawError>(raw_error_code::corrupt_file,
160 "TPI Stream does not contain a header.");
161
162 if (Reader.readObject(Header))
163 return make_error<RawError>(raw_error_code::corrupt_file,
164 "TPI Stream does not contain a header.");
165
166 if (Header->Version != PdbTpiV80)
167 return make_error<RawError>(raw_error_code::corrupt_file,
168 "Unsupported TPI Version.");
169
170 if (Header->HeaderSize != sizeof(HeaderInfo))
171 return make_error<RawError>(raw_error_code::corrupt_file,
172 "Corrupt TPI Header size.");
173
174 if (Header->HashKeySize != sizeof(ulittle32_t))
175 return make_error<RawError>(raw_error_code::corrupt_file,
176 "TPI Stream expected 4 byte hash key size.");
177
178 if (Header->NumHashBuckets < MinHashBuckets ||
179 Header->NumHashBuckets > MaxHashBuckets)
180 return make_error<RawError>(raw_error_code::corrupt_file,
181 "TPI Stream Invalid number of hash buckets.");
182
183 // The actual type records themselves come from this stream
184 if (auto EC = Reader.readArray(TypeRecords, Header->TypeRecordBytes))
185 return EC;
186
187 // Hash indices, hash values, etc come from the hash stream.
188 if (Header->HashStreamIndex >= Pdb.getNumStreams())
189 return make_error<RawError>(raw_error_code::corrupt_file,
190 "Invalid TPI hash stream index.");
191
192 auto HS =
193 MappedBlockStream::createIndexedStream(Header->HashStreamIndex, Pdb);
194 if (!HS)
195 return HS.takeError();
196 StreamReader HSR(**HS);
197
198 uint32_t NumHashValues = Header->HashValueBuffer.Length / sizeof(ulittle32_t);
199 if (NumHashValues != NumTypeRecords())
200 return make_error<RawError>(
201 raw_error_code::corrupt_file,
202 "TPI hash count does not match with the number of type records.");
203 HSR.setOffset(Header->HashValueBuffer.Off);
204 if (auto EC = HSR.readArray(HashValues, NumHashValues))
205 return EC;
206
207 HSR.setOffset(Header->IndexOffsetBuffer.Off);
208 uint32_t NumTypeIndexOffsets =
209 Header->IndexOffsetBuffer.Length / sizeof(TypeIndexOffset);
210 if (auto EC = HSR.readArray(TypeIndexOffsets, NumTypeIndexOffsets))
211 return EC;
212
213 HSR.setOffset(Header->HashAdjBuffer.Off);
214 uint32_t NumHashAdjustments =
215 Header->HashAdjBuffer.Length / sizeof(TypeIndexOffset);
216 if (auto EC = HSR.readArray(HashAdjustments, NumHashAdjustments))
217 return EC;
218
219 HashStream = std::move(*HS);
220
221 // TPI hash table is a parallel array for the type records.
222 // Verify that the hash values match with type records.
223 if (auto EC = verifyHashValues())
224 return EC;
225
226 return Error::success();
227 }
228
getTpiVersion() const229 PdbRaw_TpiVer TpiStream::getTpiVersion() const {
230 uint32_t Value = Header->Version;
231 return static_cast<PdbRaw_TpiVer>(Value);
232 }
233
TypeIndexBegin() const234 uint32_t TpiStream::TypeIndexBegin() const { return Header->TypeIndexBegin; }
235
TypeIndexEnd() const236 uint32_t TpiStream::TypeIndexEnd() const { return Header->TypeIndexEnd; }
237
NumTypeRecords() const238 uint32_t TpiStream::NumTypeRecords() const {
239 return TypeIndexEnd() - TypeIndexBegin();
240 }
241
getTypeHashStreamIndex() const242 uint16_t TpiStream::getTypeHashStreamIndex() const {
243 return Header->HashStreamIndex;
244 }
245
getTypeHashStreamAuxIndex() const246 uint16_t TpiStream::getTypeHashStreamAuxIndex() const {
247 return Header->HashAuxStreamIndex;
248 }
249
NumHashBuckets() const250 uint32_t TpiStream::NumHashBuckets() const { return Header->NumHashBuckets; }
getHashKeySize() const251 uint32_t TpiStream::getHashKeySize() const { return Header->HashKeySize; }
252
253 FixedStreamArray<support::ulittle32_t>
getHashValues() const254 TpiStream::getHashValues() const {
255 return HashValues;
256 }
257
258 FixedStreamArray<TypeIndexOffset>
getTypeIndexOffsets() const259 TpiStream::getTypeIndexOffsets() const {
260 return TypeIndexOffsets;
261 }
262
263 FixedStreamArray<TypeIndexOffset>
getHashAdjustments() const264 TpiStream::getHashAdjustments() const {
265 return HashAdjustments;
266 }
267
268 iterator_range<CVTypeArray::Iterator>
types(bool * HadError) const269 TpiStream::types(bool *HadError) const {
270 return llvm::make_range(TypeRecords.begin(HadError), TypeRecords.end());
271 }
272
commit()273 Error TpiStream::commit() { return Error::success(); }
274