Coverage Report

Created: 2019-07-24 05:18

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