LLVM  4.0.0
TpiHashing.cpp
Go to the documentation of this file.
1 //===- TpiHashing.cpp -----------------------------------------------------===//
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 
11 
14 
15 using namespace llvm;
16 using namespace llvm::codeview;
17 using namespace llvm::pdb;
18 
19 // Corresponds to `fUDTAnon`.
20 template <typename T> static bool isAnonymous(T &Rec) {
21  StringRef Name = Rec.getName();
22  return Name == "<unnamed-tag>" || Name == "__unnamed" ||
23  Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
24 }
25 
26 // Computes a hash for a given TPI record.
27 template <typename T>
28 static uint32_t getTpiHash(T &Rec, ArrayRef<uint8_t> FullRecord) {
29  auto Opts = static_cast<uint16_t>(Rec.getOptions());
30 
31  bool ForwardRef =
32  Opts & static_cast<uint16_t>(ClassOptions::ForwardReference);
33  bool Scoped = Opts & static_cast<uint16_t>(ClassOptions::Scoped);
34  bool UniqueName = Opts & static_cast<uint16_t>(ClassOptions::HasUniqueName);
35  bool IsAnon = UniqueName && isAnonymous(Rec);
36 
37  if (!ForwardRef && !Scoped && !IsAnon)
38  return hashStringV1(Rec.getName());
39  if (!ForwardRef && UniqueName && !IsAnon)
40  return hashStringV1(Rec.getUniqueName());
41  return hashBufferV8(FullRecord);
42 }
43 
44 template <typename T> static uint32_t getSourceLineHash(T &Rec) {
45  char Buf[4];
46  support::endian::write32le(Buf, Rec.getUDT().getIndex());
47  return hashStringV1(StringRef(Buf, 4));
48 }
49 
50 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
51  UdtSourceLineRecord &Rec) {
52  CVR.Hash = getSourceLineHash(Rec);
53 }
54 
55 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
57  CVR.Hash = getSourceLineHash(Rec);
58 }
59 
60 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, ClassRecord &Rec) {
61  CVR.Hash = getTpiHash(Rec, CVR.data());
62 }
63 
64 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, EnumRecord &Rec) {
65  CVR.Hash = getTpiHash(Rec, CVR.data());
66 }
67 
68 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, UnionRecord &Rec) {
69  CVR.Hash = getTpiHash(Rec, CVR.data());
70 }
71 
73  return verifySourceLine(Rec.getUDT());
74 }
75 
78  return verifySourceLine(Rec.getUDT());
79 }
80 
82  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
83  return errorInvalidHash();
84  return Error::success();
85 }
87  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
88  return errorInvalidHash();
89  return Error::success();
90 }
92  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
93  return errorInvalidHash();
94  return Error::success();
95 }
96 
97 Error TpiHashVerifier::verifySourceLine(codeview::TypeIndex TI) {
98  char Buf[4];
100  uint32_t Hash = hashStringV1(StringRef(Buf, 4));
101  if (Hash % NumHashBuckets != HashValues[Index])
102  return errorInvalidHash();
103  return Error::success();
104 }
105 
106 Error TpiHashVerifier::visitTypeBegin(CVType &Rec) {
107  ++Index;
108  RawRecord = Rec;
109  return Error::success();
110 }
uint32_t hashBufferV8(ArrayRef< uint8_t > Data)
Definition: Hash.cpp:81
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:276
ArrayRef< uint8_t > data() const
Definition: CVRecord.h:34
void write32le(void *P, uint32_t V)
Definition: Endian.h:339
static bool isAnonymous(T &Rec)
Definition: TpiHashing.cpp:20
static Error visitKnownRecord(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks)
A 32-bit type reference.
Definition: TypeIndex.h:89
uint32_t getIndex() const
Definition: TypeIndex.h:103
uint32_t hashStringV1(StringRef Str)
Definition: Hash.cpp:21
Optional< uint32_t > Hash
Definition: CVRecord.h:46
static ErrorSuccess success()
Create a success value.
static uint32_t getTpiHash(T &Rec, ArrayRef< uint8_t > FullRecord)
Definition: TpiHashing.cpp:28
static uint32_t getSourceLineHash(T &Rec)
Definition: TpiHashing.cpp:44
Lightweight error class with error context and mandatory checking.
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47