Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/CodeGen/AsmPrinter/DIEHash.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/CodeGen/DIEHash.cpp - Dwarf Hashing Framework ----------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains support for DWARF4 hashing of DIEs.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "DIEHash.h"
14
#include "ByteStreamer.h"
15
#include "DwarfCompileUnit.h"
16
#include "DwarfDebug.h"
17
#include "llvm/ADT/ArrayRef.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/BinaryFormat/Dwarf.h"
20
#include "llvm/CodeGen/AsmPrinter.h"
21
#include "llvm/Support/Debug.h"
22
#include "llvm/Support/raw_ostream.h"
23
24
using namespace llvm;
25
26
#define DEBUG_TYPE "dwarfdebug"
27
28
/// Grabs the string in whichever attribute is passed in and returns
29
/// a reference to it.
30
0
static StringRef getDIEStringAttr(const DIE &Die, uint16_t Attr) {
31
  // Iterate through all the attributes until we find the one we're
32
  // looking for, if we can't find it return an empty string.
33
0
  for (const auto &V : Die.values())
34
0
    if (V.getAttribute() == Attr)
35
0
      return V.getDIEString().getString();
36
37
0
  return StringRef("");
38
0
}
39
40
/// Adds the string in \p Str to the hash. This also hashes
41
/// a trailing NULL with the string.
42
0
void DIEHash::addString(StringRef Str) {
43
0
  LLVM_DEBUG(dbgs() << "Adding string " << Str << " to hash.\n");
44
0
  Hash.update(Str);
45
0
  Hash.update(ArrayRef((uint8_t)'\0'));
46
0
}
47
48
// FIXME: The LEB128 routines are copied and only slightly modified out of
49
// LEB128.h.
50
51
/// Adds the unsigned in \p Value to the hash encoded as a ULEB128.
52
0
void DIEHash::addULEB128(uint64_t Value) {
53
0
  LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
54
0
  do {
55
0
    uint8_t Byte = Value & 0x7f;
56
0
    Value >>= 7;
57
0
    if (Value != 0)
58
0
      Byte |= 0x80; // Mark this byte to show that more bytes will follow.
59
0
    Hash.update(Byte);
60
0
  } while (Value != 0);
61
0
}
62
63
0
void DIEHash::addSLEB128(int64_t Value) {
64
0
  LLVM_DEBUG(dbgs() << "Adding ULEB128 " << Value << " to hash.\n");
65
0
  bool More;
66
0
  do {
67
0
    uint8_t Byte = Value & 0x7f;
68
0
    Value >>= 7;
69
0
    More = !((((Value == 0) && ((Byte & 0x40) == 0)) ||
70
0
              ((Value == -1) && ((Byte & 0x40) != 0))));
71
0
    if (More)
72
0
      Byte |= 0x80; // Mark this byte to show that more bytes will follow.
73
0
    Hash.update(Byte);
74
0
  } while (More);
75
0
}
76
77
/// Including \p Parent adds the context of Parent to the hash..
78
0
void DIEHash::addParentContext(const DIE &Parent) {
79
80
0
  LLVM_DEBUG(dbgs() << "Adding parent context to hash...\n");
81
82
  // [7.27.2] For each surrounding type or namespace beginning with the
83
  // outermost such construct...
84
0
  SmallVector<const DIE *, 1> Parents;
85
0
  const DIE *Cur = &Parent;
86
0
  while (Cur->getParent()) {
87
0
    Parents.push_back(Cur);
88
0
    Cur = Cur->getParent();
89
0
  }
90
0
  assert(Cur->getTag() == dwarf::DW_TAG_compile_unit ||
91
0
         Cur->getTag() == dwarf::DW_TAG_type_unit);
92
93
  // Reverse iterate over our list to go from the outermost construct to the
94
  // innermost.
95
0
  for (const DIE *Die : llvm::reverse(Parents)) {
96
    // ... Append the letter "C" to the sequence...
97
0
    addULEB128('C');
98
99
    // ... Followed by the DWARF tag of the construct...
100
0
    addULEB128(Die->getTag());
101
102
    // ... Then the name, taken from the DW_AT_name attribute.
103
0
    StringRef Name = getDIEStringAttr(*Die, dwarf::DW_AT_name);
104
0
    LLVM_DEBUG(dbgs() << "... adding context: " << Name << "\n");
105
0
    if (!Name.empty())
106
0
      addString(Name);
107
0
  }
108
0
}
109
110
// Collect all of the attributes for a particular DIE in single structure.
111
0
void DIEHash::collectAttributes(const DIE &Die, DIEAttrs &Attrs) {
112
113
0
  for (const auto &V : Die.values()) {
114
0
    LLVM_DEBUG(dbgs() << "Attribute: "
115
0
                      << dwarf::AttributeString(V.getAttribute())
116
0
                      << " added.\n");
117
0
    switch (V.getAttribute()) {
118
0
#define HANDLE_DIE_HASH_ATTR(NAME)                                             \
119
0
  case dwarf::NAME:                                                            \
120
0
    Attrs.NAME = V;                                                            \
121
0
    break;
122
0
#include "DIEHashAttributes.def"
123
0
    default:
124
0
      break;
125
0
    }
126
0
  }
127
0
}
128
129
void DIEHash::hashShallowTypeReference(dwarf::Attribute Attribute,
130
0
                                       const DIE &Entry, StringRef Name) {
131
  // append the letter 'N'
132
0
  addULEB128('N');
133
134
  // the DWARF attribute code (DW_AT_type or DW_AT_friend),
135
0
  addULEB128(Attribute);
136
137
  // the context of the tag,
138
0
  if (const DIE *Parent = Entry.getParent())
139
0
    addParentContext(*Parent);
140
141
  // the letter 'E',
142
0
  addULEB128('E');
143
144
  // and the name of the type.
145
0
  addString(Name);
146
147
  // Currently DW_TAG_friends are not used by Clang, but if they do become so,
148
  // here's the relevant spec text to implement:
149
  //
150
  // For DW_TAG_friend, if the referenced entry is the DW_TAG_subprogram,
151
  // the context is omitted and the name to be used is the ABI-specific name
152
  // of the subprogram (e.g., the mangled linker name).
153
0
}
154
155
void DIEHash::hashRepeatedTypeReference(dwarf::Attribute Attribute,
156
0
                                        unsigned DieNumber) {
157
  // a) If T is in the list of [previously hashed types], use the letter
158
  // 'R' as the marker
159
0
  addULEB128('R');
160
161
0
  addULEB128(Attribute);
162
163
  // and use the unsigned LEB128 encoding of [the index of T in the
164
  // list] as the attribute value;
165
0
  addULEB128(DieNumber);
166
0
}
167
168
void DIEHash::hashDIEEntry(dwarf::Attribute Attribute, dwarf::Tag Tag,
169
0
                           const DIE &Entry) {
170
0
  assert(Tag != dwarf::DW_TAG_friend && "No current LLVM clients emit friend "
171
0
                                        "tags. Add support here when there's "
172
0
                                        "a use case");
173
  // Step 5
174
  // If the tag in Step 3 is one of [the below tags]
175
0
  if ((Tag == dwarf::DW_TAG_pointer_type ||
176
0
       Tag == dwarf::DW_TAG_reference_type ||
177
0
       Tag == dwarf::DW_TAG_rvalue_reference_type ||
178
0
       Tag == dwarf::DW_TAG_ptr_to_member_type) &&
179
      // and the referenced type (via the [below attributes])
180
      // FIXME: This seems overly restrictive, and causes hash mismatches
181
      // there's a decl/def difference in the containing type of a
182
      // ptr_to_member_type, but it's what DWARF says, for some reason.
183
0
      Attribute == dwarf::DW_AT_type) {
184
    // ... has a DW_AT_name attribute,
185
0
    StringRef Name = getDIEStringAttr(Entry, dwarf::DW_AT_name);
186
0
    if (!Name.empty()) {
187
0
      hashShallowTypeReference(Attribute, Entry, Name);
188
0
      return;
189
0
    }
190
0
  }
191
192
0
  unsigned &DieNumber = Numbering[&Entry];
193
0
  if (DieNumber) {
194
0
    hashRepeatedTypeReference(Attribute, DieNumber);
195
0
    return;
196
0
  }
197
198
  // otherwise, b) use the letter 'T' as the marker, ...
199
0
  addULEB128('T');
200
201
0
  addULEB128(Attribute);
202
203
  // ... process the type T recursively by performing Steps 2 through 7, and
204
  // use the result as the attribute value.
205
0
  DieNumber = Numbering.size();
206
0
  computeHash(Entry);
207
0
}
208
209
0
void DIEHash::hashRawTypeReference(const DIE &Entry) {
210
0
  unsigned &DieNumber = Numbering[&Entry];
211
0
  if (DieNumber) {
212
0
    addULEB128('R');
213
0
    addULEB128(DieNumber);
214
0
    return;
215
0
  }
216
0
  DieNumber = Numbering.size();
217
0
  addULEB128('T');
218
0
  computeHash(Entry);
219
0
}
220
221
// Hash all of the values in a block like set of values. This assumes that
222
// all of the data is going to be added as integers.
223
0
void DIEHash::hashBlockData(const DIE::const_value_range &Values) {
224
0
  for (const auto &V : Values)
225
0
    if (V.getType() == DIEValue::isBaseTypeRef) {
226
0
      const DIE &C =
227
0
          *CU->ExprRefedBaseTypes[V.getDIEBaseTypeRef().getIndex()].Die;
228
0
      StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
229
0
      assert(!Name.empty() &&
230
0
             "Base types referenced from DW_OP_convert should have a name");
231
0
      hashNestedType(C, Name);
232
0
    } else
233
0
      Hash.update((uint64_t)V.getDIEInteger().getValue());
234
0
}
235
236
// Hash the contents of a loclistptr class.
237
0
void DIEHash::hashLocList(const DIELocList &LocList) {
238
0
  HashingByteStreamer Streamer(*this);
239
0
  DwarfDebug &DD = *AP->getDwarfDebug();
240
0
  const DebugLocStream &Locs = DD.getDebugLocs();
241
0
  const DebugLocStream::List &List = Locs.getList(LocList.getValue());
242
0
  for (const DebugLocStream::Entry &Entry : Locs.getEntries(List))
243
0
    DD.emitDebugLocEntry(Streamer, Entry, List.CU);
244
0
}
245
246
// Hash an individual attribute \param Attr based on the type of attribute and
247
// the form.
248
0
void DIEHash::hashAttribute(const DIEValue &Value, dwarf::Tag Tag) {
249
0
  dwarf::Attribute Attribute = Value.getAttribute();
250
251
  // Other attribute values use the letter 'A' as the marker, and the value
252
  // consists of the form code (encoded as an unsigned LEB128 value) followed by
253
  // the encoding of the value according to the form code. To ensure
254
  // reproducibility of the signature, the set of forms used in the signature
255
  // computation is limited to the following: DW_FORM_sdata, DW_FORM_flag,
256
  // DW_FORM_string, and DW_FORM_block.
257
258
0
  switch (Value.getType()) {
259
0
  case DIEValue::isNone:
260
0
    llvm_unreachable("Expected valid DIEValue");
261
262
    // 7.27 Step 3
263
    // ... An attribute that refers to another type entry T is processed as
264
    // follows:
265
0
  case DIEValue::isEntry:
266
0
    hashDIEEntry(Attribute, Tag, Value.getDIEEntry().getEntry());
267
0
    break;
268
0
  case DIEValue::isInteger: {
269
0
    addULEB128('A');
270
0
    addULEB128(Attribute);
271
0
    switch (Value.getForm()) {
272
0
    case dwarf::DW_FORM_data1:
273
0
    case dwarf::DW_FORM_data2:
274
0
    case dwarf::DW_FORM_data4:
275
0
    case dwarf::DW_FORM_data8:
276
0
    case dwarf::DW_FORM_udata:
277
0
    case dwarf::DW_FORM_sdata:
278
0
      addULEB128(dwarf::DW_FORM_sdata);
279
0
      addSLEB128((int64_t)Value.getDIEInteger().getValue());
280
0
      break;
281
    // DW_FORM_flag_present is just flag with a value of one. We still give it a
282
    // value so just use the value.
283
0
    case dwarf::DW_FORM_flag_present:
284
0
    case dwarf::DW_FORM_flag:
285
0
      addULEB128(dwarf::DW_FORM_flag);
286
0
      addULEB128((int64_t)Value.getDIEInteger().getValue());
287
0
      break;
288
0
    default:
289
0
      llvm_unreachable("Unknown integer form!");
290
0
    }
291
0
    break;
292
0
  }
293
0
  case DIEValue::isString:
294
0
    addULEB128('A');
295
0
    addULEB128(Attribute);
296
0
    addULEB128(dwarf::DW_FORM_string);
297
0
    addString(Value.getDIEString().getString());
298
0
    break;
299
0
  case DIEValue::isInlineString:
300
0
    addULEB128('A');
301
0
    addULEB128(Attribute);
302
0
    addULEB128(dwarf::DW_FORM_string);
303
0
    addString(Value.getDIEInlineString().getString());
304
0
    break;
305
0
  case DIEValue::isBlock:
306
0
  case DIEValue::isLoc:
307
0
  case DIEValue::isLocList:
308
0
    addULEB128('A');
309
0
    addULEB128(Attribute);
310
0
    addULEB128(dwarf::DW_FORM_block);
311
0
    if (Value.getType() == DIEValue::isBlock) {
312
0
      addULEB128(Value.getDIEBlock().computeSize(AP->getDwarfFormParams()));
313
0
      hashBlockData(Value.getDIEBlock().values());
314
0
    } else if (Value.getType() == DIEValue::isLoc) {
315
0
      addULEB128(Value.getDIELoc().computeSize(AP->getDwarfFormParams()));
316
0
      hashBlockData(Value.getDIELoc().values());
317
0
    } else {
318
      // We could add the block length, but that would take
319
      // a bit of work and not add a lot of uniqueness
320
      // to the hash in some way we could test.
321
0
      hashLocList(Value.getDIELocList());
322
0
    }
323
0
    break;
324
    // FIXME: It's uncertain whether or not we should handle this at the moment.
325
0
  case DIEValue::isExpr:
326
0
  case DIEValue::isLabel:
327
0
  case DIEValue::isBaseTypeRef:
328
0
  case DIEValue::isDelta:
329
0
  case DIEValue::isAddrOffset:
330
0
    llvm_unreachable("Add support for additional value types.");
331
0
  }
332
0
}
333
334
// Go through the attributes from \param Attrs in the order specified in 7.27.4
335
// and hash them.
336
0
void DIEHash::hashAttributes(const DIEAttrs &Attrs, dwarf::Tag Tag) {
337
0
#define HANDLE_DIE_HASH_ATTR(NAME)                                             \
338
0
  {                                                                            \
339
0
    if (Attrs.NAME)                                                           \
340
0
      hashAttribute(Attrs.NAME, Tag);                                         \
341
0
  }
342
0
#include "DIEHashAttributes.def"
343
  // FIXME: Add the extended attributes.
344
0
}
345
346
// Add all of the attributes for \param Die to the hash.
347
0
void DIEHash::addAttributes(const DIE &Die) {
348
0
  DIEAttrs Attrs = {};
349
0
  collectAttributes(Die, Attrs);
350
0
  hashAttributes(Attrs, Die.getTag());
351
0
}
352
353
0
void DIEHash::hashNestedType(const DIE &Die, StringRef Name) {
354
  // 7.27 Step 7
355
  // ... append the letter 'S',
356
0
  addULEB128('S');
357
358
  // the tag of C,
359
0
  addULEB128(Die.getTag());
360
361
  // and the name.
362
0
  addString(Name);
363
0
}
364
365
// Compute the hash of a DIE. This is based on the type signature computation
366
// given in section 7.27 of the DWARF4 standard. It is the md5 hash of a
367
// flattened description of the DIE.
368
0
void DIEHash::computeHash(const DIE &Die) {
369
  // Append the letter 'D', followed by the DWARF tag of the DIE.
370
0
  addULEB128('D');
371
0
  addULEB128(Die.getTag());
372
373
  // Add each of the attributes of the DIE.
374
0
  addAttributes(Die);
375
376
  // Then hash each of the children of the DIE.
377
0
  for (const auto &C : Die.children()) {
378
    // 7.27 Step 7
379
    // If C is a nested type entry or a member function entry, ...
380
0
    if (isType(C.getTag()) || (C.getTag() == dwarf::DW_TAG_subprogram && isType(C.getParent()->getTag()))) {
381
0
      StringRef Name = getDIEStringAttr(C, dwarf::DW_AT_name);
382
      // ... and has a DW_AT_name attribute
383
0
      if (!Name.empty()) {
384
0
        hashNestedType(C, Name);
385
0
        continue;
386
0
      }
387
0
    }
388
0
    computeHash(C);
389
0
  }
390
391
  // Following the last (or if there are no children), append a zero byte.
392
0
  Hash.update(ArrayRef((uint8_t)'\0'));
393
0
}
394
395
/// This is based on the type signature computation given in section 7.27 of the
396
/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
397
/// with the inclusion of the full CU and all top level CU entities.
398
// TODO: Initialize the type chain at 0 instead of 1 for CU signatures.
399
0
uint64_t DIEHash::computeCUSignature(StringRef DWOName, const DIE &Die) {
400
0
  Numbering.clear();
401
0
  Numbering[&Die] = 1;
402
403
0
  if (!DWOName.empty())
404
0
    Hash.update(DWOName);
405
  // Hash the DIE.
406
0
  computeHash(Die);
407
408
  // Now return the result.
409
0
  MD5::MD5Result Result;
410
0
  Hash.final(Result);
411
412
  // ... take the least significant 8 bytes and return those. Our MD5
413
  // implementation always returns its results in little endian, so we actually
414
  // need the "high" word.
415
0
  return Result.high();
416
0
}
417
418
/// This is based on the type signature computation given in section 7.27 of the
419
/// DWARF4 standard. It is an md5 hash of the flattened description of the DIE
420
/// with the inclusion of additional forms not specifically called out in the
421
/// standard.
422
0
uint64_t DIEHash::computeTypeSignature(const DIE &Die) {
423
0
  Numbering.clear();
424
0
  Numbering[&Die] = 1;
425
426
0
  if (const DIE *Parent = Die.getParent())
427
0
    addParentContext(*Parent);
428
429
  // Hash the DIE.
430
0
  computeHash(Die);
431
432
  // Now return the result.
433
0
  MD5::MD5Result Result;
434
0
  Hash.final(Result);
435
436
  // ... take the least significant 8 bytes and return those. Our MD5
437
  // implementation always returns its results in little endian, so we actually
438
  // need the "high" word.
439
0
  return Result.high();
440
0
}