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/CodeView/TypeDeserializer.h" 13 #include "llvm/DebugInfo/PDB/Native/Hash.h" 14 #include "llvm/DebugInfo/PDB/Native/RawError.h" 15 16 using namespace llvm; 17 using namespace llvm::codeview; 18 using namespace llvm::pdb; 19 20 // Corresponds to `fUDTAnon`. 21 template <typename T> static bool isAnonymous(T &Rec) { 22 StringRef Name = Rec.getName(); 23 return Name == "<unnamed-tag>" || Name == "__unnamed" || 24 Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed"); 25 } 26 27 // Computes a hash for a given TPI record. 28 template <typename T> 29 static uint32_t getTpiHash(T &Rec, ArrayRef<uint8_t> FullRecord) { 30 auto Opts = static_cast<uint16_t>(Rec.getOptions()); 31 32 bool ForwardRef = 33 Opts & static_cast<uint16_t>(ClassOptions::ForwardReference); 34 bool Scoped = Opts & static_cast<uint16_t>(ClassOptions::Scoped); 35 bool UniqueName = Opts & static_cast<uint16_t>(ClassOptions::HasUniqueName); 36 bool IsAnon = UniqueName && isAnonymous(Rec); 37 38 if (!ForwardRef && !Scoped && !IsAnon) 39 return hashStringV1(Rec.getName()); 40 if (!ForwardRef && UniqueName && !IsAnon) 41 return hashStringV1(Rec.getUniqueName()); 42 return hashBufferV8(FullRecord); 43 } 44 45 template <typename T> static uint32_t getSourceLineHash(T &Rec) { 46 char Buf[4]; 47 support::endian::write32le(Buf, Rec.getUDT().getIndex()); 48 return hashStringV1(StringRef(Buf, 4)); 49 } 50 51 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, 52 UdtSourceLineRecord &Rec) { 53 CVR.Hash = getSourceLineHash(Rec); 54 } 55 56 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, 57 UdtModSourceLineRecord &Rec) { 58 CVR.Hash = getSourceLineHash(Rec); 59 } 60 61 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, ClassRecord &Rec) { 62 CVR.Hash = getTpiHash(Rec, CVR.data()); 63 } 64 65 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, EnumRecord &Rec) { 66 CVR.Hash = getTpiHash(Rec, CVR.data()); 67 } 68 69 void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, UnionRecord &Rec) { 70 CVR.Hash = getTpiHash(Rec, CVR.data()); 71 } 72 73 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UdtSourceLineRecord &Rec) { 74 return verifySourceLine(Rec.getUDT()); 75 } 76 77 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, 78 UdtModSourceLineRecord &Rec) { 79 return verifySourceLine(Rec.getUDT()); 80 } 81 82 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, ClassRecord &Rec) { 83 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index]) 84 return errorInvalidHash(); 85 return Error::success(); 86 } 87 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, EnumRecord &Rec) { 88 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index]) 89 return errorInvalidHash(); 90 return Error::success(); 91 } 92 Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UnionRecord &Rec) { 93 if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index]) 94 return errorInvalidHash(); 95 return Error::success(); 96 } 97 98 Error TpiHashVerifier::verifySourceLine(codeview::TypeIndex TI) { 99 char Buf[4]; 100 support::endian::write32le(Buf, TI.getIndex()); 101 uint32_t Hash = hashStringV1(StringRef(Buf, 4)); 102 if (Hash % NumHashBuckets != HashValues[Index]) 103 return errorInvalidHash(); 104 return Error::success(); 105 } 106 107 Error TpiHashVerifier::visitTypeBegin(CVType &Rec) { 108 ++Index; 109 RawRecord = Rec; 110 return Error::success(); 111 } 112