summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/DebugInfo/PDB/Raw/TpiHashing.cpp
blob: 6c3ddb3d57af7b1242787ae3e375c2701814e00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//===- TpiHashing.cpp -----------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/PDB/Raw/TpiHashing.h"

#include "llvm/DebugInfo/PDB/Raw/Hash.h"
#include "llvm/DebugInfo/PDB/Raw/RawError.h"

using namespace llvm;
using namespace llvm::codeview;
using namespace llvm::pdb;

// Corresponds to `fUDTAnon`.
template <typename T> static bool isAnonymous(T &Rec) {
  StringRef Name = Rec.getName();
  return Name == "<unnamed-tag>" || Name == "__unnamed" ||
         Name.endswith("::<unnamed-tag>") || Name.endswith("::__unnamed");
}

// Computes a hash for a given TPI record.
template <typename T>
static uint32_t getTpiHash(T &Rec, ArrayRef<uint8_t> FullRecord) {
  auto Opts = static_cast<uint16_t>(Rec.getOptions());

  bool ForwardRef =
      Opts & static_cast<uint16_t>(ClassOptions::ForwardReference);
  bool Scoped = Opts & static_cast<uint16_t>(ClassOptions::Scoped);
  bool UniqueName = Opts & static_cast<uint16_t>(ClassOptions::HasUniqueName);
  bool IsAnon = UniqueName && isAnonymous(Rec);

  if (!ForwardRef && !Scoped && !IsAnon)
    return hashStringV1(Rec.getName());
  if (!ForwardRef && UniqueName && !IsAnon)
    return hashStringV1(Rec.getUniqueName());
  return hashBufferV8(FullRecord);
}

template <typename T> static uint32_t getSourceLineHash(T &Rec) {
  char Buf[4];
  support::endian::write32le(Buf, Rec.getUDT().getIndex());
  return hashStringV1(StringRef(Buf, 4));
}

void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
                                          UdtSourceLineRecord &Rec) {
  CVR.Hash = getSourceLineHash(Rec);
}

void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR,
                                          UdtModSourceLineRecord &Rec) {
  CVR.Hash = getSourceLineHash(Rec);
}

void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, ClassRecord &Rec) {
  CVR.Hash = getTpiHash(Rec, CVR.data());
}

void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, EnumRecord &Rec) {
  CVR.Hash = getTpiHash(Rec, CVR.data());
}

void TpiHashUpdater::visitKnownRecordImpl(CVType &CVR, UnionRecord &Rec) {
  CVR.Hash = getTpiHash(Rec, CVR.data());
}

Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UdtSourceLineRecord &Rec) {
  return verifySourceLine(Rec.getUDT());
}

Error TpiHashVerifier::visitKnownRecord(CVType &CVR,
                                        UdtModSourceLineRecord &Rec) {
  return verifySourceLine(Rec.getUDT());
}

Error TpiHashVerifier::visitKnownRecord(CVType &CVR, ClassRecord &Rec) {
  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
    return errorInvalidHash();
  return Error::success();
}
Error TpiHashVerifier::visitKnownRecord(CVType &CVR, EnumRecord &Rec) {
  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
    return errorInvalidHash();
  return Error::success();
}
Error TpiHashVerifier::visitKnownRecord(CVType &CVR, UnionRecord &Rec) {
  if (getTpiHash(Rec, CVR.data()) % NumHashBuckets != HashValues[Index])
    return errorInvalidHash();
  return Error::success();
}

Error TpiHashVerifier::verifySourceLine(codeview::TypeIndex TI) {
  char Buf[4];
  support::endian::write32le(Buf, TI.getIndex());
  uint32_t Hash = hashStringV1(StringRef(Buf, 4));
  if (Hash % NumHashBuckets != HashValues[Index])
    return errorInvalidHash();
  return Error::success();
}

Error TpiHashVerifier::visitTypeBegin(CVType &Rec) {
  ++Index;
  RawRecord = Rec;
  return Error::success();
}
OpenPOWER on IntegriCloud