1 //===-- Mangled.cpp ---------------------------------------------*- C++ -*-===//
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 #if defined(_WIN32)
12 #include "lldb/Host/windows/windows.h"
13 
14 #include <dbghelp.h>
15 #pragma comment(lib, "dbghelp.lib")
16 #endif
17 
18 #include "lldb/Core/RichManglingContext.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Logging.h"
22 #include "lldb/Utility/RegularExpression.h"
23 #include "lldb/Utility/Stream.h"
24 #include "lldb/Utility/Timer.h"
25 #include "lldb/lldb-enumerations.h"
26 
27 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
28 #include "Plugins/Language/ObjC/ObjCLanguage.h"
29 
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Demangle/Demangle.h"
32 #include "llvm/Support/Compiler.h"
33 
34 #include <mutex>
35 #include <string>
36 #include <utility>
37 
38 #include <stdlib.h>
39 #include <string.h>
40 using namespace lldb_private;
41 
42 #if defined(_MSC_VER)
43 static DWORD safeUndecorateName(const char *Mangled, char *Demangled,
44                                 DWORD DemangledLength) {
45   static std::mutex M;
46   std::lock_guard<std::mutex> Lock(M);
47   return ::UnDecorateSymbolName(
48       Mangled, Demangled, DemangledLength,
49       UNDNAME_NO_ACCESS_SPECIFIERS |       // Strip public, private, protected
50                                            // keywords
51           UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall,
52                                            // etc keywords
53           UNDNAME_NO_THROW_SIGNATURES |    // Strip throw() specifications
54           UNDNAME_NO_MEMBER_TYPE |         // Strip virtual, static, etc
55                                            // specifiers
56           UNDNAME_NO_MS_KEYWORDS           // Strip all MS extension keywords
57       );
58 }
59 #endif
60 
61 static inline Mangled::ManglingScheme cstring_mangling_scheme(const char *s) {
62   if (s) {
63     if (s[0] == '?')
64       return Mangled::eManglingSchemeMSVC;
65     if (s[0] == '_' && s[1] == 'Z')
66       return Mangled::eManglingSchemeItanium;
67   }
68   return Mangled::eManglingSchemeNone;
69 }
70 
71 static inline bool cstring_is_mangled(const char *s) {
72   return cstring_mangling_scheme(s) != Mangled::eManglingSchemeNone;
73 }
74 
75 static ConstString
76 get_demangled_name_without_arguments(ConstString mangled,
77                                      ConstString demangled) {
78   // This pair is <mangled name, demangled name without function arguments>
79   static std::pair<ConstString, ConstString>
80       g_most_recent_mangled_to_name_sans_args;
81 
82   // Need to have the mangled & demangled names we're currently examining as
83   // statics so we can return a const ref to them at the end of the func if we
84   // don't have anything better.
85   static ConstString g_last_mangled;
86   static ConstString g_last_demangled;
87 
88   if (mangled && g_most_recent_mangled_to_name_sans_args.first == mangled) {
89     return g_most_recent_mangled_to_name_sans_args.second;
90   }
91 
92   g_last_demangled = demangled;
93   g_last_mangled = mangled;
94 
95   const char *mangled_name_cstr = mangled.GetCString();
96 
97   if (demangled && mangled_name_cstr && mangled_name_cstr[0]) {
98     if (mangled_name_cstr[0] == '_' && mangled_name_cstr[1] == 'Z' &&
99         (mangled_name_cstr[2] != 'T' && // avoid virtual table, VTT structure,
100                                         // typeinfo structure, and typeinfo
101                                         // mangled_name
102          mangled_name_cstr[2] != 'G' && // avoid guard variables
103          mangled_name_cstr[2] != 'Z')) // named local entities (if we eventually
104                                        // handle eSymbolTypeData, we will want
105                                        // this back)
106     {
107       CPlusPlusLanguage::MethodName cxx_method(demangled);
108       if (!cxx_method.GetBasename().empty()) {
109         std::string shortname;
110         if (!cxx_method.GetContext().empty())
111           shortname = cxx_method.GetContext().str() + "::";
112         shortname += cxx_method.GetBasename().str();
113         ConstString result(shortname.c_str());
114         g_most_recent_mangled_to_name_sans_args.first = mangled;
115         g_most_recent_mangled_to_name_sans_args.second = result;
116         return g_most_recent_mangled_to_name_sans_args.second;
117       }
118     }
119   }
120 
121   if (demangled)
122     return g_last_demangled;
123   return g_last_mangled;
124 }
125 
126 #pragma mark Mangled
127 //----------------------------------------------------------------------
128 // Default constructor
129 //----------------------------------------------------------------------
130 Mangled::Mangled() : m_mangled(), m_demangled() {}
131 
132 //----------------------------------------------------------------------
133 // Constructor with an optional string and a boolean indicating if it is the
134 // mangled version.
135 //----------------------------------------------------------------------
136 Mangled::Mangled(ConstString s, bool mangled)
137     : m_mangled(), m_demangled() {
138   if (s)
139     SetValue(s, mangled);
140 }
141 
142 Mangled::Mangled(llvm::StringRef name, bool is_mangled) {
143   if (!name.empty())
144     SetValue(ConstString(name), is_mangled);
145 }
146 
147 Mangled::Mangled(ConstString s) : m_mangled(), m_demangled() {
148   if (s)
149     SetValue(s);
150 }
151 
152 Mangled::Mangled(llvm::StringRef name) {
153   if (!name.empty())
154     SetValue(ConstString(name));
155 }
156 
157 //----------------------------------------------------------------------
158 // Destructor
159 //----------------------------------------------------------------------
160 Mangled::~Mangled() {}
161 
162 //----------------------------------------------------------------------
163 // Convert to pointer operator. This allows code to check any Mangled objects
164 // to see if they contain anything valid using code such as:
165 //
166 //  Mangled mangled(...);
167 //  if (mangled)
168 //  { ...
169 //----------------------------------------------------------------------
170 Mangled::operator void *() const {
171   return (m_mangled) ? const_cast<Mangled *>(this) : NULL;
172 }
173 
174 //----------------------------------------------------------------------
175 // Logical NOT operator. This allows code to check any Mangled objects to see
176 // if they are invalid using code such as:
177 //
178 //  Mangled mangled(...);
179 //  if (!file_spec)
180 //  { ...
181 //----------------------------------------------------------------------
182 bool Mangled::operator!() const { return !m_mangled; }
183 
184 //----------------------------------------------------------------------
185 // Clear the mangled and demangled values.
186 //----------------------------------------------------------------------
187 void Mangled::Clear() {
188   m_mangled.Clear();
189   m_demangled.Clear();
190 }
191 
192 //----------------------------------------------------------------------
193 // Compare the string values.
194 //----------------------------------------------------------------------
195 int Mangled::Compare(const Mangled &a, const Mangled &b) {
196   return ConstString::Compare(
197       a.GetName(lldb::eLanguageTypeUnknown, ePreferMangled),
198       b.GetName(lldb::eLanguageTypeUnknown, ePreferMangled));
199 }
200 
201 //----------------------------------------------------------------------
202 // Set the string value in this objects. If "mangled" is true, then the mangled
203 // named is set with the new value in "s", else the demangled name is set.
204 //----------------------------------------------------------------------
205 void Mangled::SetValue(ConstString s, bool mangled) {
206   if (s) {
207     if (mangled) {
208       m_demangled.Clear();
209       m_mangled = s;
210     } else {
211       m_demangled = s;
212       m_mangled.Clear();
213     }
214   } else {
215     m_demangled.Clear();
216     m_mangled.Clear();
217   }
218 }
219 
220 void Mangled::SetValue(ConstString name) {
221   if (name) {
222     if (cstring_is_mangled(name.GetCString())) {
223       m_demangled.Clear();
224       m_mangled = name;
225     } else {
226       m_demangled = name;
227       m_mangled.Clear();
228     }
229   } else {
230     m_demangled.Clear();
231     m_mangled.Clear();
232   }
233 }
234 
235 //----------------------------------------------------------------------
236 // Local helpers for different demangling implementations.
237 //----------------------------------------------------------------------
238 static char *GetMSVCDemangledStr(const char *M) {
239 #if defined(_MSC_VER)
240   const size_t demangled_length = 2048;
241   char *demangled_cstr = static_cast<char *>(::malloc(demangled_length));
242   ::ZeroMemory(demangled_cstr, demangled_length);
243   DWORD result = safeUndecorateName(M, demangled_cstr, demangled_length);
244 
245   if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
246     if (demangled_cstr && demangled_cstr[0])
247       log->Printf("demangled msvc: %s -> \"%s\"", M, demangled_cstr);
248     else
249       log->Printf("demangled msvc: %s -> error: 0x%lu", M, result);
250   }
251 
252   if (result != 0) {
253     return demangled_cstr;
254   } else {
255     ::free(demangled_cstr);
256     return nullptr;
257   }
258 #else
259   return nullptr;
260 #endif
261 }
262 
263 static char *GetItaniumDemangledStr(const char *M) {
264   char *demangled_cstr = nullptr;
265 
266   llvm::ItaniumPartialDemangler ipd;
267   bool err = ipd.partialDemangle(M);
268   if (!err) {
269     // Default buffer and size (will realloc in case it's too small).
270     size_t demangled_size = 80;
271     demangled_cstr = static_cast<char *>(std::malloc(demangled_size));
272     demangled_cstr = ipd.finishDemangle(demangled_cstr, &demangled_size);
273 
274     assert(demangled_cstr &&
275            "finishDemangle must always succeed if partialDemangle did");
276     assert(demangled_cstr[demangled_size - 1] == '\0' &&
277            "Expected demangled_size to return length including trailing null");
278   }
279 
280   if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
281     if (demangled_cstr)
282       log->Printf("demangled itanium: %s -> \"%s\"", M, demangled_cstr);
283     else
284       log->Printf("demangled itanium: %s -> error: failed to demangle", M);
285   }
286 
287   return demangled_cstr;
288 }
289 
290 //----------------------------------------------------------------------
291 // Explicit demangling for scheduled requests during batch processing. This
292 // makes use of ItaniumPartialDemangler's rich demangle info
293 //----------------------------------------------------------------------
294 bool Mangled::DemangleWithRichManglingInfo(
295     RichManglingContext &context, SkipMangledNameFn *skip_mangled_name) {
296   // We need to generate and cache the demangled name.
297   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
298   Timer scoped_timer(func_cat,
299                      "Mangled::DemangleWithRichNameIndexInfo (m_mangled = %s)",
300                      m_mangled.GetCString());
301 
302   // Others are not meant to arrive here. ObjC names or C's main() for example
303   // have their names stored in m_demangled, while m_mangled is empty.
304   assert(m_mangled);
305 
306   // Check whether or not we are interested in this name at all.
307   ManglingScheme scheme = cstring_mangling_scheme(m_mangled.GetCString());
308   if (skip_mangled_name && skip_mangled_name(m_mangled.GetStringRef(), scheme))
309     return false;
310 
311   switch (scheme) {
312   case eManglingSchemeNone:
313     // The current mangled_name_filter would allow llvm_unreachable here.
314     return false;
315 
316   case eManglingSchemeItanium:
317     // We want the rich mangling info here, so we don't care whether or not
318     // there is a demangled string in the pool already.
319     if (context.FromItaniumName(m_mangled)) {
320       // If we got an info, we have a name. Copy to string pool and connect the
321       // counterparts to accelerate later access in GetDemangledName().
322       context.ParseFullName();
323       m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(),
324                                                   m_mangled);
325       return true;
326     } else {
327       m_demangled.SetCString("");
328       return false;
329     }
330 
331   case eManglingSchemeMSVC: {
332     // We have no rich mangling for MSVC-mangled names yet, so first try to
333     // demangle it if necessary.
334     if (!m_demangled && !m_mangled.GetMangledCounterpart(m_demangled)) {
335       if (char *d = GetMSVCDemangledStr(m_mangled.GetCString())) {
336         // If we got an info, we have a name. Copy to string pool and connect
337         // the counterparts to accelerate later access in GetDemangledName().
338         m_demangled.SetStringWithMangledCounterpart(llvm::StringRef(d),
339                                                     m_mangled);
340         ::free(d);
341       } else {
342         m_demangled.SetCString("");
343       }
344     }
345 
346     if (m_demangled.IsEmpty()) {
347       // Cannot demangle it, so don't try parsing.
348       return false;
349     } else {
350       // Demangled successfully, we can try and parse it with
351       // CPlusPlusLanguage::MethodName.
352       return context.FromCxxMethodName(m_demangled);
353     }
354   }
355   }
356   llvm_unreachable("Fully covered switch above!");
357 }
358 
359 //----------------------------------------------------------------------
360 // Generate the demangled name on demand using this accessor. Code in this
361 // class will need to use this accessor if it wishes to decode the demangled
362 // name. The result is cached and will be kept until a new string value is
363 // supplied to this object, or until the end of the object's lifetime.
364 //----------------------------------------------------------------------
365 ConstString
366 Mangled::GetDemangledName(lldb::LanguageType language) const {
367   // Check to make sure we have a valid mangled name and that we haven't
368   // already decoded our mangled name.
369   if (m_mangled && m_demangled.IsNull()) {
370     // We need to generate and cache the demangled name.
371     static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
372     Timer scoped_timer(func_cat, "Mangled::GetDemangledName (m_mangled = %s)",
373                        m_mangled.GetCString());
374 
375     // Don't bother running anything that isn't mangled
376     const char *mangled_name = m_mangled.GetCString();
377     ManglingScheme mangling_scheme{cstring_mangling_scheme(mangled_name)};
378     if (mangling_scheme != eManglingSchemeNone &&
379         !m_mangled.GetMangledCounterpart(m_demangled)) {
380       // We didn't already mangle this name, demangle it and if all goes well
381       // add it to our map.
382       char *demangled_name = nullptr;
383       switch (mangling_scheme) {
384       case eManglingSchemeMSVC:
385         demangled_name = GetMSVCDemangledStr(mangled_name);
386         break;
387       case eManglingSchemeItanium: {
388         demangled_name = GetItaniumDemangledStr(mangled_name);
389         break;
390       }
391       case eManglingSchemeNone:
392         llvm_unreachable("eManglingSchemeNone was handled already");
393       }
394       if (demangled_name) {
395         m_demangled.SetStringWithMangledCounterpart(
396             llvm::StringRef(demangled_name), m_mangled);
397         free(demangled_name);
398       }
399     }
400     if (m_demangled.IsNull()) {
401       // Set the demangled string to the empty string to indicate we tried to
402       // parse it once and failed.
403       m_demangled.SetCString("");
404     }
405   }
406 
407   return m_demangled;
408 }
409 
410 ConstString
411 Mangled::GetDisplayDemangledName(lldb::LanguageType language) const {
412   return GetDemangledName(language);
413 }
414 
415 bool Mangled::NameMatches(const RegularExpression &regex,
416                           lldb::LanguageType language) const {
417   if (m_mangled && regex.Execute(m_mangled.AsCString()))
418     return true;
419 
420   ConstString demangled = GetDemangledName(language);
421   return demangled && regex.Execute(demangled.AsCString());
422 }
423 
424 //----------------------------------------------------------------------
425 // Get the demangled name if there is one, else return the mangled name.
426 //----------------------------------------------------------------------
427 ConstString Mangled::GetName(lldb::LanguageType language,
428                              Mangled::NamePreference preference) const {
429   if (preference == ePreferMangled && m_mangled)
430     return m_mangled;
431 
432   ConstString demangled = GetDemangledName(language);
433 
434   if (preference == ePreferDemangledWithoutArguments) {
435     return get_demangled_name_without_arguments(m_mangled, demangled);
436   }
437   if (preference == ePreferDemangled) {
438     // Call the accessor to make sure we get a demangled name in case it hasn't
439     // been demangled yet...
440     if (demangled)
441       return demangled;
442     return m_mangled;
443   }
444   return demangled;
445 }
446 
447 //----------------------------------------------------------------------
448 // Dump a Mangled object to stream "s". We don't force our demangled name to be
449 // computed currently (we don't use the accessor).
450 //----------------------------------------------------------------------
451 void Mangled::Dump(Stream *s) const {
452   if (m_mangled) {
453     *s << ", mangled = " << m_mangled;
454   }
455   if (m_demangled) {
456     const char *demangled = m_demangled.AsCString();
457     s->Printf(", demangled = %s", demangled[0] ? demangled : "<error>");
458   }
459 }
460 
461 //----------------------------------------------------------------------
462 // Dumps a debug version of this string with extra object and state information
463 // to stream "s".
464 //----------------------------------------------------------------------
465 void Mangled::DumpDebug(Stream *s) const {
466   s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2),
467             static_cast<const void *>(this));
468   m_mangled.DumpDebug(s);
469   s->Printf(", demangled = ");
470   m_demangled.DumpDebug(s);
471 }
472 
473 //----------------------------------------------------------------------
474 // Return the size in byte that this object takes in memory. The size includes
475 // the size of the objects it owns, and not the strings that it references
476 // because they are shared strings.
477 //----------------------------------------------------------------------
478 size_t Mangled::MemorySize() const {
479   return m_mangled.MemorySize() + m_demangled.MemorySize();
480 }
481 
482 //----------------------------------------------------------------------
483 // We "guess" the language because we can't determine a symbol's language from
484 // it's name.  For example, a Pascal symbol can be mangled using the C++
485 // Itanium scheme, and defined in a compilation unit within the same module as
486 // other C++ units.  In addition, different targets could have different ways
487 // of mangling names from a given language, likewise the compilation units
488 // within those targets.
489 //----------------------------------------------------------------------
490 lldb::LanguageType Mangled::GuessLanguage() const {
491   ConstString mangled = GetMangledName();
492   if (mangled) {
493     const char *mangled_name = mangled.GetCString();
494     if (CPlusPlusLanguage::IsCPPMangledName(mangled_name))
495       return lldb::eLanguageTypeC_plus_plus;
496     else if (ObjCLanguage::IsPossibleObjCMethodName(mangled_name))
497       return lldb::eLanguageTypeObjC;
498   } else {
499     // ObjC names aren't really mangled, so they won't necessarily be in the
500     // mangled name slot.
501     ConstString demangled_name = GetDemangledName(lldb::eLanguageTypeUnknown);
502     if (demangled_name
503         && ObjCLanguage::IsPossibleObjCMethodName(demangled_name.GetCString()))
504       return lldb::eLanguageTypeObjC;
505 
506   }
507   return lldb::eLanguageTypeUnknown;
508 }
509 
510 //----------------------------------------------------------------------
511 // Dump OBJ to the supplied stream S.
512 //----------------------------------------------------------------------
513 Stream &operator<<(Stream &s, const Mangled &obj) {
514   if (obj.GetMangledName())
515     s << "mangled = '" << obj.GetMangledName() << "'";
516 
517   ConstString demangled =
518       obj.GetDemangledName(lldb::eLanguageTypeUnknown);
519   if (demangled)
520     s << ", demangled = '" << demangled << '\'';
521   else
522     s << ", demangled = <error>";
523   return s;
524 }
525