Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Mangled.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Mangled.cpp -------------------------------------------------------===//
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
#include "lldb/Core/Mangled.h"
10
11
#include "lldb/Core/DataFileCache.h"
12
#include "lldb/Core/RichManglingContext.h"
13
#include "lldb/Target/Language.h"
14
#include "lldb/Utility/ConstString.h"
15
#include "lldb/Utility/DataEncoder.h"
16
#include "lldb/Utility/Log.h"
17
#include "lldb/Utility/Logging.h"
18
#include "lldb/Utility/RegularExpression.h"
19
#include "lldb/Utility/Stream.h"
20
#include "lldb/lldb-enumerations.h"
21
22
#include "llvm/ADT/StringRef.h"
23
#include "llvm/Demangle/Demangle.h"
24
#include "llvm/Support/Compiler.h"
25
26
#include <mutex>
27
#include <string>
28
#include <utility>
29
30
#include <cstdlib>
31
#include <cstring>
32
using namespace lldb_private;
33
34
121k
static inline bool cstring_is_mangled(llvm::StringRef s) {
35
121k
  return Mangled::GetManglingScheme(s) != Mangled::eManglingSchemeNone;
36
121k
}
37
38
#pragma mark Mangled
39
40
100M
Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
41
100M
  if (name.empty())
42
40
    return Mangled::eManglingSchemeNone;
43
44
100M
  if (name.startswith("?"))
45
0
    return Mangled::eManglingSchemeMSVC;
46
47
100M
  if (name.startswith("_R"))
48
11.0k
    return Mangled::eManglingSchemeRustV0;
49
50
100M
  if (name.startswith("_D"))
51
100k
    return Mangled::eManglingSchemeD;
52
53
99.9M
  if (name.startswith("_Z"))
54
29.3M
    return Mangled::eManglingSchemeItanium;
55
56
  // ___Z is a clang extension of block invocations
57
70.5M
  if (name.startswith("___Z"))
58
3.60M
    return Mangled::eManglingSchemeItanium;
59
60
66.9M
  return Mangled::eManglingSchemeNone;
61
70.5M
}
62
63
143k
Mangled::Mangled(ConstString s) : m_mangled(), m_demangled() {
64
143k
  if (s)
65
119k
    SetValue(s);
66
143k
}
67
68
2.65k
Mangled::Mangled(llvm::StringRef name) {
69
2.65k
  if (!name.empty())
70
1.56k
    SetValue(ConstString(name));
71
2.65k
}
72
73
// Convert to bool operator. This allows code to check any Mangled objects
74
// to see if they contain anything valid using code such as:
75
//
76
//  Mangled mangled(...);
77
//  if (mangled)
78
//  { ...
79
13.7M
Mangled::operator bool() const { return m_mangled || 
m_demangled13.7M
; }
80
81
// Clear the mangled and demangled values.
82
304k
void Mangled::Clear() {
83
304k
  m_mangled.Clear();
84
304k
  m_demangled.Clear();
85
304k
}
86
87
// Compare the string values.
88
0
int Mangled::Compare(const Mangled &a, const Mangled &b) {
89
0
  return ConstString::Compare(a.GetName(ePreferMangled),
90
0
                              b.GetName(ePreferMangled));
91
0
}
92
93
// Set the string value in this objects. If "mangled" is true, then the mangled
94
// named is set with the new value in "s", else the demangled name is set.
95
155M
void Mangled::SetValue(ConstString s, bool mangled) {
96
155M
  if (s) {
97
155M
    if (mangled) {
98
81.3M
      m_demangled.Clear();
99
81.3M
      m_mangled = s;
100
81.3M
    } else {
101
74.1M
      m_demangled = s;
102
74.1M
      m_mangled.Clear();
103
74.1M
    }
104
155M
  } else {
105
0
    m_demangled.Clear();
106
0
    m_mangled.Clear();
107
0
  }
108
155M
}
109
110
121k
void Mangled::SetValue(ConstString name) {
111
121k
  if (name) {
112
121k
    if (cstring_is_mangled(name.GetStringRef())) {
113
575
      m_demangled.Clear();
114
575
      m_mangled = name;
115
120k
    } else {
116
120k
      m_demangled = name;
117
120k
      m_mangled.Clear();
118
120k
    }
119
121k
  } else {
120
0
    m_demangled.Clear();
121
0
    m_mangled.Clear();
122
0
  }
123
121k
}
124
125
// Local helpers for different demangling implementations.
126
0
static char *GetMSVCDemangledStr(const char *M) {
127
0
  char *demangled_cstr = llvm::microsoftDemangle(
128
0
      M, nullptr, nullptr, nullptr, nullptr,
129
0
      llvm::MSDemangleFlags(
130
0
          llvm::MSDF_NoAccessSpecifier | llvm::MSDF_NoCallingConvention |
131
0
          llvm::MSDF_NoMemberType | llvm::MSDF_NoVariableType));
132
133
0
  if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
134
0
    if (demangled_cstr && demangled_cstr[0])
135
0
      LLDB_LOGF(log, "demangled msvc: %s -> \"%s\"", M, demangled_cstr);
136
0
    else
137
0
      LLDB_LOGF(log, "demangled msvc: %s -> error", M);
138
0
  }
139
140
0
  return demangled_cstr;
141
0
}
142
143
2.50M
static char *GetItaniumDemangledStr(const char *M) {
144
2.50M
  char *demangled_cstr = nullptr;
145
146
2.50M
  llvm::ItaniumPartialDemangler ipd;
147
2.50M
  bool err = ipd.partialDemangle(M);
148
2.50M
  if (!err) {
149
    // Default buffer and size (will realloc in case it's too small).
150
2.50M
    size_t demangled_size = 80;
151
2.50M
    demangled_cstr = static_cast<char *>(std::malloc(demangled_size));
152
2.50M
    demangled_cstr = ipd.finishDemangle(demangled_cstr, &demangled_size);
153
154
2.50M
    assert(demangled_cstr &&
155
2.50M
           "finishDemangle must always succeed if partialDemangle did");
156
0
    assert(demangled_cstr[demangled_size - 1] == '\0' &&
157
2.50M
           "Expected demangled_size to return length including trailing null");
158
2.50M
  }
159
160
2.50M
  if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
161
0
    if (demangled_cstr)
162
0
      LLDB_LOGF(log, "demangled itanium: %s -> \"%s\"", M, demangled_cstr);
163
0
    else
164
0
      LLDB_LOGF(log, "demangled itanium: %s -> error: failed to demangle", M);
165
0
  }
166
167
2.50M
  return demangled_cstr;
168
2.50M
}
169
170
5.75k
static char *GetRustV0DemangledStr(const char *M) {
171
5.75k
  char *demangled_cstr = llvm::rustDemangle(M, nullptr, nullptr, nullptr);
172
173
5.75k
  if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
174
0
    if (demangled_cstr && demangled_cstr[0])
175
0
      LLDB_LOG(log, "demangled rustv0: {0} -> \"{1}\"", M, demangled_cstr);
176
0
    else
177
0
      LLDB_LOG(log, "demangled rustv0: {0} -> error: failed to demangle", M);
178
0
  }
179
180
5.75k
  return demangled_cstr;
181
5.75k
}
182
183
72.0k
static char *GetDLangDemangledStr(const char *M) {
184
72.0k
  char *demangled_cstr = llvm::dlangDemangle(M);
185
186
72.0k
  if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
187
0
    if (demangled_cstr && demangled_cstr[0])
188
0
      LLDB_LOG(log, "demangled dlang: {0} -> \"{1}\"", M, demangled_cstr);
189
0
    else
190
0
      LLDB_LOG(log, "demangled dlang: {0} -> error: failed to demangle", M);
191
0
  }
192
193
72.0k
  return demangled_cstr;
194
72.0k
}
195
196
// Explicit demangling for scheduled requests during batch processing. This
197
// makes use of ItaniumPartialDemangler's rich demangle info
198
bool Mangled::DemangleWithRichManglingInfo(
199
42.4M
    RichManglingContext &context, SkipMangledNameFn *skip_mangled_name) {
200
  // Others are not meant to arrive here. ObjC names or C's main() for example
201
  // have their names stored in m_demangled, while m_mangled is empty.
202
42.4M
  assert(m_mangled);
203
204
  // Check whether or not we are interested in this name at all.
205
0
  ManglingScheme scheme = GetManglingScheme(m_mangled.GetStringRef());
206
42.4M
  if (skip_mangled_name && skip_mangled_name(m_mangled.GetStringRef(), scheme))
207
20.7M
    return false;
208
209
21.7M
  switch (scheme) {
210
0
  case eManglingSchemeNone:
211
    // The current mangled_name_filter would allow llvm_unreachable here.
212
0
    return false;
213
214
21.6M
  case eManglingSchemeItanium:
215
    // We want the rich mangling info here, so we don't care whether or not
216
    // there is a demangled string in the pool already.
217
21.6M
    if (context.FromItaniumName(m_mangled)) {
218
      // If we got an info, we have a name. Copy to string pool and connect the
219
      // counterparts to accelerate later access in GetDemangledName().
220
21.6M
      context.ParseFullName();
221
21.6M
      m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(),
222
21.6M
                                                  m_mangled);
223
21.6M
      return true;
224
21.6M
    } else {
225
5.15k
      m_demangled.SetCString("");
226
5.15k
      return false;
227
5.15k
    }
228
229
0
  case eManglingSchemeMSVC: {
230
    // We have no rich mangling for MSVC-mangled names yet, so first try to
231
    // demangle it if necessary.
232
0
    if (!m_demangled && !m_mangled.GetMangledCounterpart(m_demangled)) {
233
0
      if (char *d = GetMSVCDemangledStr(m_mangled.GetCString())) {
234
        // If we got an info, we have a name. Copy to string pool and connect
235
        // the counterparts to accelerate later access in GetDemangledName().
236
0
        m_demangled.SetStringWithMangledCounterpart(llvm::StringRef(d),
237
0
                                                    m_mangled);
238
0
        ::free(d);
239
0
      } else {
240
0
        m_demangled.SetCString("");
241
0
      }
242
0
    }
243
244
0
    if (m_demangled.IsEmpty()) {
245
      // Cannot demangle it, so don't try parsing.
246
0
      return false;
247
0
    } else {
248
      // Demangled successfully, we can try and parse it with
249
      // CPlusPlusLanguage::MethodName.
250
0
      return context.FromCxxMethodName(m_demangled);
251
0
    }
252
0
  }
253
254
5.32k
  case eManglingSchemeRustV0:
255
33.7k
  case eManglingSchemeD:
256
    // Rich demangling scheme is not supported
257
33.7k
    return false;
258
21.7M
  }
259
0
  llvm_unreachable("Fully covered switch above!");
260
0
}
261
262
// Generate the demangled name on demand using this accessor. Code in this
263
// class will need to use this accessor if it wishes to decode the demangled
264
// name. The result is cached and will be kept until a new string value is
265
// supplied to this object, or until the end of the object's lifetime.
266
162M
ConstString Mangled::GetDemangledName() const {
267
  // Check to make sure we have a valid mangled name and that we haven't
268
  // already decoded our mangled name.
269
162M
  if (m_mangled && 
m_demangled.IsNull()84.0M
) {
270
    // Don't bother running anything that isn't mangled
271
57.2M
    const char *mangled_name = m_mangled.GetCString();
272
57.2M
    ManglingScheme mangling_scheme =
273
57.2M
        GetManglingScheme(m_mangled.GetStringRef());
274
57.2M
    if (mangling_scheme != eManglingSchemeNone &&
275
57.2M
        
!m_mangled.GetMangledCounterpart(m_demangled)9.26M
) {
276
      // We didn't already mangle this name, demangle it and if all goes well
277
      // add it to our map.
278
2.58M
      char *demangled_name = nullptr;
279
2.58M
      switch (mangling_scheme) {
280
0
      case eManglingSchemeMSVC:
281
0
        demangled_name = GetMSVCDemangledStr(mangled_name);
282
0
        break;
283
2.50M
      case eManglingSchemeItanium: {
284
2.50M
        demangled_name = GetItaniumDemangledStr(mangled_name);
285
2.50M
        break;
286
0
      }
287
5.75k
      case eManglingSchemeRustV0:
288
5.75k
        demangled_name = GetRustV0DemangledStr(mangled_name);
289
5.75k
        break;
290
72.0k
      case eManglingSchemeD:
291
72.0k
        demangled_name = GetDLangDemangledStr(mangled_name);
292
72.0k
        break;
293
0
      case eManglingSchemeNone:
294
0
        llvm_unreachable("eManglingSchemeNone was handled already");
295
2.58M
      }
296
2.58M
      if (demangled_name) {
297
2.50M
        m_demangled.SetStringWithMangledCounterpart(
298
2.50M
            llvm::StringRef(demangled_name), m_mangled);
299
2.50M
        free(demangled_name);
300
2.50M
      }
301
2.58M
    }
302
57.2M
    if (m_demangled.IsNull()) {
303
      // Set the demangled string to the empty string to indicate we tried to
304
      // parse it once and failed.
305
48.0M
      m_demangled.SetCString("");
306
48.0M
    }
307
57.2M
  }
308
309
162M
  return m_demangled;
310
162M
}
311
312
64
ConstString Mangled::GetDisplayDemangledName() const {
313
64
  return GetDemangledName();
314
64
}
315
316
0
bool Mangled::NameMatches(const RegularExpression &regex) const {
317
0
  if (m_mangled && regex.Execute(m_mangled.GetStringRef()))
318
0
    return true;
319
320
0
  ConstString demangled = GetDemangledName();
321
0
  return demangled && regex.Execute(demangled.GetStringRef());
322
0
}
323
324
// Get the demangled name if there is one, else return the mangled name.
325
43.7M
ConstString Mangled::GetName(Mangled::NamePreference preference) const {
326
43.7M
  if (preference == ePreferMangled && 
m_mangled40.9M
)
327
27.6M
    return m_mangled;
328
329
  // Call the accessor to make sure we get a demangled name in case it hasn't
330
  // been demangled yet...
331
16.0M
  ConstString demangled = GetDemangledName();
332
333
16.0M
  if (preference == ePreferDemangledWithoutArguments) {
334
28.7k
    if (Language *lang = Language::FindPlugin(GuessLanguage())) {
335
14.6k
      return lang->GetDemangledFunctionNameWithoutArguments(*this);
336
14.6k
    }
337
28.7k
  }
338
16.0M
  if (preference == ePreferDemangled) {
339
2.74M
    if (demangled)
340
1.98M
      return demangled;
341
757k
    return m_mangled;
342
2.74M
  }
343
13.2M
  return demangled;
344
16.0M
}
345
346
// Dump a Mangled object to stream "s". We don't force our demangled name to be
347
// computed currently (we don't use the accessor).
348
21
void Mangled::Dump(Stream *s) const {
349
21
  if (m_mangled) {
350
3
    *s << ", mangled = " << m_mangled;
351
3
  }
352
21
  if (m_demangled) {
353
21
    const char *demangled = m_demangled.AsCString();
354
21
    s->Printf(", demangled = %s", demangled[0] ? demangled : 
"<error>"0
);
355
21
  }
356
21
}
357
358
// Dumps a debug version of this string with extra object and state information
359
// to stream "s".
360
0
void Mangled::DumpDebug(Stream *s) const {
361
0
  s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2),
362
0
            static_cast<const void *>(this));
363
0
  m_mangled.DumpDebug(s);
364
0
  s->Printf(", demangled = ");
365
0
  m_demangled.DumpDebug(s);
366
0
}
367
368
// Return the size in byte that this object takes in memory. The size includes
369
// the size of the objects it owns, and not the strings that it references
370
// because they are shared strings.
371
0
size_t Mangled::MemorySize() const {
372
0
  return m_mangled.MemorySize() + m_demangled.MemorySize();
373
0
}
374
375
// We "guess" the language because we can't determine a symbol's language from
376
// it's name.  For example, a Pascal symbol can be mangled using the C++
377
// Itanium scheme, and defined in a compilation unit within the same module as
378
// other C++ units.  In addition, different targets could have different ways
379
// of mangling names from a given language, likewise the compilation units
380
// within those targets.
381
159k
lldb::LanguageType Mangled::GuessLanguage() const {
382
159k
  lldb::LanguageType result = lldb::eLanguageTypeUnknown;
383
  // Ask each language plugin to check if the mangled name belongs to it.
384
779k
  Language::ForEach([this, &result](Language *l) {
385
779k
    if (l->SymbolNameFitsToLanguage(*this)) {
386
41.1k
      result = l->GetLanguageType();
387
41.1k
      return false;
388
41.1k
    }
389
738k
    return true;
390
779k
  });
391
159k
  return result;
392
159k
}
393
394
// Dump OBJ to the supplied stream S.
395
0
Stream &operator<<(Stream &s, const Mangled &obj) {
396
0
  if (obj.GetMangledName())
397
0
    s << "mangled = '" << obj.GetMangledName() << "'";
398
399
0
  ConstString demangled = obj.GetDemangledName();
400
0
  if (demangled)
401
0
    s << ", demangled = '" << demangled << '\'';
402
0
  else
403
0
    s << ", demangled = <error>";
404
0
  return s;
405
0
}
406
407
// When encoding Mangled objects we can get away with encoding as little
408
// information as is required. The enumeration below helps us to efficiently
409
// encode Mangled objects.
410
enum MangledEncoding {
411
  /// If the Mangled object has neither a mangled name or demangled name we can
412
  /// encode the object with one zero byte using the Empty enumeration.
413
  Empty = 0u,
414
  /// If the Mangled object has only a demangled name and no mangled named, we
415
  /// can encode only the demangled name.
416
  DemangledOnly = 1u,
417
  /// If the mangle name can calculate the demangled name (it is the
418
  /// mangled/demangled counterpart), then we only need to encode the mangled
419
  /// name as the demangled name can be recomputed.
420
  MangledOnly = 2u,
421
  /// If we have a Mangled object with two different names that are not related
422
  /// then we need to save both strings. This can happen if we have a name that
423
  /// isn't a true mangled name, but we want to be able to lookup a symbol by
424
  /// name and type in the symbol table. We do this for Objective C symbols like
425
  /// "OBJC_CLASS_$_NSValue" where the mangled named will be set to
426
  /// "OBJC_CLASS_$_NSValue" and the demangled name will be manually set to
427
  /// "NSValue". If we tried to demangled the name "OBJC_CLASS_$_NSValue" it
428
  /// would fail, but in these cases we want these unrelated names to be
429
  /// preserved.
430
  MangledAndDemangled = 3u
431
};
432
433
bool Mangled::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
434
38
                     const StringTableReader &strtab) {
435
38
  m_mangled.Clear();
436
38
  m_demangled.Clear();
437
38
  MangledEncoding encoding = (MangledEncoding)data.GetU8(offset_ptr);
438
38
  switch (encoding) {
439
2
    case Empty:
440
2
      return true;
441
442
30
    case DemangledOnly:
443
30
      m_demangled.SetString(strtab.Get(data.GetU32(offset_ptr)));
444
30
      return true;
445
446
4
    case MangledOnly:
447
4
      m_mangled.SetString(strtab.Get(data.GetU32(offset_ptr)));
448
4
      return true;
449
450
2
    case MangledAndDemangled:
451
2
      m_mangled.SetString(strtab.Get(data.GetU32(offset_ptr)));
452
2
      m_demangled.SetString(strtab.Get(data.GetU32(offset_ptr)));
453
2
      return true;
454
38
  }
455
0
  return false;
456
38
}
457
/// The encoding format for the Mangled object is as follows:
458
///
459
/// uint8_t encoding;
460
/// char str1[]; (only if DemangledOnly, MangledOnly)
461
/// char str2[]; (only if MangledAndDemangled)
462
///
463
/// The strings are stored as NULL terminated UTF8 strings and str1 and str2
464
/// are only saved if we need them based on the encoding.
465
///
466
/// Some mangled names have a mangled name that can be demangled by the built
467
/// in demanglers. These kinds of mangled objects know when the mangled and
468
/// demangled names are the counterparts for each other. This is done because
469
/// demangling is very expensive and avoiding demangling the same name twice
470
/// saves us a lot of compute time. For these kinds of names we only need to
471
/// save the mangled name and have the encoding set to "MangledOnly".
472
///
473
/// If a mangled obejct has only a demangled name, then we save only that string
474
/// and have the encoding set to "DemangledOnly".
475
///
476
/// Some mangled objects have both mangled and demangled names, but the
477
/// demangled name can not be computed from the mangled name. This is often used
478
/// for runtime named, like Objective C runtime V2 and V3 names. Both these
479
/// names must be saved and the encoding is set to "MangledAndDemangled".
480
///
481
/// For a Mangled object with no names, we only need to set the encoding to
482
/// "Empty" and not store any string values.
483
83
void Mangled::Encode(DataEncoder &file, ConstStringTable &strtab) const {
484
83
  MangledEncoding encoding = Empty;
485
83
  if (m_mangled) {
486
18
    encoding = MangledOnly;
487
18
    if (m_demangled) {
488
      // We have both mangled and demangled names. If the demangled name is the
489
      // counterpart of the mangled name, then we only need to save the mangled
490
      // named. If they are different, we need to save both.
491
3
      ConstString s;
492
3
      if (!(m_mangled.GetMangledCounterpart(s) && 
s == m_demangled1
))
493
2
        encoding = MangledAndDemangled;
494
3
    }
495
65
  } else if (m_demangled) {
496
63
    encoding = DemangledOnly;
497
63
  }
498
83
  file.AppendU8(encoding);
499
83
  switch (encoding) {
500
2
    case Empty:
501
2
      break;
502
63
    case DemangledOnly:
503
63
      file.AppendU32(strtab.Add(m_demangled));
504
63
      break;
505
16
    case MangledOnly:
506
16
      file.AppendU32(strtab.Add(m_mangled));
507
16
      break;
508
2
    case MangledAndDemangled:
509
2
      file.AppendU32(strtab.Add(m_mangled));
510
2
      file.AppendU32(strtab.Add(m_demangled));
511
2
      break;
512
83
  }
513
83
}