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 10 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h" 11 12 #include "llvm/DebugInfo/PDB/Native/Hash.h" 13 #include "llvm/DebugInfo/PDB/Native/RawError.h" 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, 56 UdtModSourceLineRecord &Rec) { 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 72 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UdtSourceLineRecord &Rec) { 73 return verifySourceLine(Rec.getUDT()); 74 } 75 76 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, 77 UdtModSourceLineRecord &Rec) { 78 return verifySourceLine(Rec.getUDT()); 79 } 80 81 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, ClassRecord &Rec) { 82 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index]) 83 return errorInvalidHash(); 84 return Error::success(); 85 } 86 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, EnumRecord &Rec) { 87 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index]) 88 return errorInvalidHash(); 89 return Error::success(); 90 } 91 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UnionRecord &Rec) { 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]; 99 support::endian::write32le(Buf, TI.getIndex()); 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 } 111