1 //===-- MangledTest.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 "Plugins/ObjectFile/ELF/ObjectFileELF.h" 10 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h" 11 #include "TestingSupport/SubsystemRAII.h" 12 #include "TestingSupport/TestUtilities.h" 13 14 #include "lldb/Core/Mangled.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Host/FileSystem.h" 18 #include "lldb/Host/HostInfo.h" 19 #include "lldb/Symbol/SymbolContext.h" 20 21 #include "llvm/Support/FileUtilities.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/Program.h" 24 #include "llvm/Testing/Support/Error.h" 25 26 #include "gtest/gtest.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 TEST(MangledTest, ResultForValidName) { 32 ConstString MangledName("_ZN1a1b1cIiiiEEvm"); 33 Mangled TheMangled(MangledName); 34 ConstString TheDemangled = TheMangled.GetDemangledName(); 35 36 ConstString ExpectedResult("void a::b::c<int, int, int>(unsigned long)"); 37 EXPECT_STREQ(ExpectedResult.GetCString(), TheDemangled.GetCString()); 38 } 39 40 TEST(MangledTest, ResultForBlockInvocation) { 41 ConstString MangledName("___Z1fU13block_pointerFviE_block_invoke"); 42 Mangled TheMangled(MangledName); 43 ConstString TheDemangled = TheMangled.GetDemangledName(); 44 45 ConstString ExpectedResult( 46 "invocation function for block in f(void (int) block_pointer)"); 47 EXPECT_STREQ(ExpectedResult.GetCString(), TheDemangled.GetCString()); 48 } 49 50 TEST(MangledTest, EmptyForInvalidName) { 51 ConstString MangledName("_ZN1a1b1cmxktpEEvm"); 52 Mangled TheMangled(MangledName); 53 ConstString TheDemangled = TheMangled.GetDemangledName(); 54 55 EXPECT_STREQ("", TheDemangled.GetCString()); 56 } 57 58 TEST(MangledTest, ResultForValidRustV0Name) { 59 ConstString mangled_name("_RNvC1a4main"); 60 Mangled the_mangled(mangled_name); 61 ConstString the_demangled = the_mangled.GetDemangledName(); 62 63 ConstString expected_result("a::main"); 64 EXPECT_STREQ(expected_result.GetCString(), the_demangled.GetCString()); 65 } 66 67 TEST(MangledTest, EmptyForInvalidRustV0Name) { 68 ConstString mangled_name("_RRR"); 69 Mangled the_mangled(mangled_name); 70 ConstString the_demangled = the_mangled.GetDemangledName(); 71 72 EXPECT_STREQ("", the_demangled.GetCString()); 73 } 74 75 TEST(MangledTest, ResultForValidDLangName) { 76 ConstString mangled_name("_Dmain"); 77 Mangled the_mangled(mangled_name); 78 ConstString the_demangled = the_mangled.GetDemangledName(); 79 80 ConstString expected_result("D main"); 81 EXPECT_STREQ(expected_result.GetCString(), the_demangled.GetCString()); 82 } 83 84 TEST(MangledTest, EmptyForInvalidDLangName) { 85 ConstString mangled_name("_DDD"); 86 Mangled the_mangled(mangled_name); 87 ConstString the_demangled = the_mangled.GetDemangledName(); 88 89 EXPECT_STREQ("", the_demangled.GetCString()); 90 } 91 92 93 TEST(MangledTest, NameIndexes_FindFunctionSymbols) { 94 SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab> 95 subsystems; 96 97 auto ExpectedFile = TestFile::fromYaml(R"( 98 --- !ELF 99 FileHeader: 100 Class: ELFCLASS64 101 Data: ELFDATA2LSB 102 Type: ET_EXEC 103 Machine: EM_X86_64 104 Sections: 105 - Name: .text 106 Type: SHT_PROGBITS 107 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 108 AddressAlign: 0x0000000000000010 109 Size: 0x20 110 - Name: .anothertext 111 Type: SHT_PROGBITS 112 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 113 Address: 0x0000000000000010 114 AddressAlign: 0x0000000000000010 115 Size: 0x40 116 - Name: .data 117 Type: SHT_PROGBITS 118 Flags: [ SHF_WRITE, SHF_ALLOC ] 119 Address: 0x00000000000000A8 120 AddressAlign: 0x0000000000000004 121 Content: '01000000' 122 Symbols: 123 - Name: somedata 124 Type: STT_OBJECT 125 Section: .anothertext 126 Value: 0x0000000000000045 127 Binding: STB_GLOBAL 128 - Name: main 129 Type: STT_FUNC 130 Section: .anothertext 131 Value: 0x0000000000000010 132 Size: 0x000000000000003F 133 Binding: STB_GLOBAL 134 - Name: _Z3foov 135 Type: STT_FUNC 136 Section: .text 137 Size: 0x000000000000000D 138 Binding: STB_GLOBAL 139 - Name: puts@GLIBC_2.5 140 Type: STT_FUNC 141 Section: .text 142 Size: 0x000000000000000D 143 Binding: STB_GLOBAL 144 - Name: puts@GLIBC_2.6 145 Type: STT_FUNC 146 Section: .text 147 Size: 0x000000000000000D 148 Binding: STB_GLOBAL 149 - Name: _Z5annotv@VERSION3 150 Type: STT_FUNC 151 Section: .text 152 Size: 0x000000000000000D 153 Binding: STB_GLOBAL 154 - Name: _ZN1AC2Ev 155 Type: STT_FUNC 156 Section: .text 157 Size: 0x000000000000000D 158 Binding: STB_GLOBAL 159 - Name: _ZN1AD2Ev 160 Type: STT_FUNC 161 Section: .text 162 Size: 0x000000000000000D 163 Binding: STB_GLOBAL 164 - Name: _ZN1A3barEv 165 Type: STT_FUNC 166 Section: .text 167 Size: 0x000000000000000D 168 Binding: STB_GLOBAL 169 - Name: _ZGVZN4llvm4dbgsEvE7thestrm 170 Type: STT_FUNC 171 Section: .text 172 Size: 0x000000000000000D 173 Binding: STB_GLOBAL 174 - Name: _ZZN4llvm4dbgsEvE7thestrm 175 Type: STT_FUNC 176 Section: .text 177 Size: 0x000000000000000D 178 Binding: STB_GLOBAL 179 - Name: _ZTVN5clang4DeclE 180 Type: STT_FUNC 181 Section: .text 182 Size: 0x000000000000000D 183 Binding: STB_GLOBAL 184 - Name: -[ObjCfoo] 185 Type: STT_FUNC 186 Section: .text 187 Size: 0x000000000000000D 188 Binding: STB_GLOBAL 189 - Name: +[B ObjCbar(WithCategory)] 190 Type: STT_FUNC 191 Section: .text 192 Size: 0x000000000000000D 193 Binding: STB_GLOBAL 194 - Name: _Z12undemangableEvx42 195 Type: STT_FUNC 196 Section: .text 197 Size: 0x000000000000000D 198 Binding: STB_GLOBAL 199 ... 200 )"); 201 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded()); 202 203 auto M = std::make_shared<Module>(ExpectedFile->moduleSpec()); 204 205 auto Count = [M](const char *Name, FunctionNameType Type) -> int { 206 SymbolContextList SymList; 207 M->FindFunctionSymbols(ConstString(Name), Type, SymList); 208 return SymList.GetSize(); 209 }; 210 211 // Unmangled 212 EXPECT_EQ(1, Count("main", eFunctionNameTypeFull)); 213 EXPECT_EQ(1, Count("main", eFunctionNameTypeBase)); 214 EXPECT_EQ(0, Count("main", eFunctionNameTypeMethod)); 215 216 // Itanium mangled 217 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeFull)); 218 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeBase)); 219 EXPECT_EQ(1, Count("foo", eFunctionNameTypeBase)); 220 EXPECT_EQ(0, Count("foo", eFunctionNameTypeMethod)); 221 222 // Unmangled with linker annotation 223 EXPECT_EQ(1, Count("puts@GLIBC_2.5", eFunctionNameTypeFull)); 224 EXPECT_EQ(1, Count("puts@GLIBC_2.6", eFunctionNameTypeFull)); 225 EXPECT_EQ(2, Count("puts", eFunctionNameTypeFull)); 226 EXPECT_EQ(2, Count("puts", eFunctionNameTypeBase)); 227 EXPECT_EQ(0, Count("puts", eFunctionNameTypeMethod)); 228 229 // Itanium mangled with linker annotation 230 EXPECT_EQ(1, Count("_Z5annotv@VERSION3", eFunctionNameTypeFull)); 231 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeFull)); 232 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeBase)); 233 EXPECT_EQ(0, Count("annot", eFunctionNameTypeBase)); 234 EXPECT_EQ(0, Count("annot", eFunctionNameTypeMethod)); 235 236 // Itanium mangled ctor A::A() 237 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeFull)); 238 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeBase)); 239 EXPECT_EQ(1, Count("A", eFunctionNameTypeMethod)); 240 EXPECT_EQ(0, Count("A", eFunctionNameTypeBase)); 241 242 // Itanium mangled dtor A::~A() 243 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeFull)); 244 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeBase)); 245 EXPECT_EQ(1, Count("~A", eFunctionNameTypeMethod)); 246 EXPECT_EQ(0, Count("~A", eFunctionNameTypeBase)); 247 248 // Itanium mangled method A::bar() 249 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeFull)); 250 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeBase)); 251 EXPECT_EQ(1, Count("bar", eFunctionNameTypeMethod)); 252 EXPECT_EQ(0, Count("bar", eFunctionNameTypeBase)); 253 254 // Itanium mangled names that are explicitly excluded from parsing 255 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull)); 256 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase)); 257 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod)); 258 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase)); 259 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull)); 260 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase)); 261 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod)); 262 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase)); 263 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeFull)); 264 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeBase)); 265 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeMethod)); 266 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeBase)); 267 268 // ObjC mangled static 269 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeFull)); 270 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeBase)); 271 EXPECT_EQ(0, Count("ObjCfoo", eFunctionNameTypeMethod)); 272 273 // ObjC mangled method with category 274 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeFull)); 275 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeBase)); 276 EXPECT_EQ(0, Count("ObjCbar", eFunctionNameTypeMethod)); 277 278 // Invalid things: unable to decode but still possible to find by full name 279 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeFull)); 280 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeBase)); 281 EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod)); 282 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase)); 283 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod)); 284 } 285