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 TEST(MangledTest, BoolConversionOperator) { 93 { 94 ConstString MangledName("_ZN1a1b1cIiiiEEvm"); 95 Mangled TheMangled(MangledName); 96 EXPECT_EQ(true, bool(TheMangled)); 97 EXPECT_EQ(false, !TheMangled); 98 } 99 { 100 ConstString UnmangledName("puts"); 101 Mangled TheMangled(UnmangledName); 102 EXPECT_EQ(true, bool(TheMangled)); 103 EXPECT_EQ(false, !TheMangled); 104 } 105 { 106 Mangled TheMangled{}; 107 EXPECT_EQ(false, bool(TheMangled)); 108 EXPECT_EQ(true, !TheMangled); 109 } 110 } 111 112 TEST(MangledTest, NameIndexes_FindFunctionSymbols) { 113 SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab> 114 subsystems; 115 116 auto ExpectedFile = TestFile::fromYaml(R"( 117 --- !ELF 118 FileHeader: 119 Class: ELFCLASS64 120 Data: ELFDATA2LSB 121 Type: ET_EXEC 122 Machine: EM_X86_64 123 Sections: 124 - Name: .text 125 Type: SHT_PROGBITS 126 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 127 AddressAlign: 0x0000000000000010 128 Size: 0x20 129 - Name: .anothertext 130 Type: SHT_PROGBITS 131 Flags: [ SHF_ALLOC, SHF_EXECINSTR ] 132 Address: 0x0000000000000010 133 AddressAlign: 0x0000000000000010 134 Size: 0x40 135 - Name: .data 136 Type: SHT_PROGBITS 137 Flags: [ SHF_WRITE, SHF_ALLOC ] 138 Address: 0x00000000000000A8 139 AddressAlign: 0x0000000000000004 140 Content: '01000000' 141 Symbols: 142 - Name: somedata 143 Type: STT_OBJECT 144 Section: .anothertext 145 Value: 0x0000000000000045 146 Binding: STB_GLOBAL 147 - Name: main 148 Type: STT_FUNC 149 Section: .anothertext 150 Value: 0x0000000000000010 151 Size: 0x000000000000003F 152 Binding: STB_GLOBAL 153 - Name: _Z3foov 154 Type: STT_FUNC 155 Section: .text 156 Size: 0x000000000000000D 157 Binding: STB_GLOBAL 158 - Name: puts@GLIBC_2.5 159 Type: STT_FUNC 160 Section: .text 161 Size: 0x000000000000000D 162 Binding: STB_GLOBAL 163 - Name: puts@GLIBC_2.6 164 Type: STT_FUNC 165 Section: .text 166 Size: 0x000000000000000D 167 Binding: STB_GLOBAL 168 - Name: _Z5annotv@VERSION3 169 Type: STT_FUNC 170 Section: .text 171 Size: 0x000000000000000D 172 Binding: STB_GLOBAL 173 - Name: _ZN1AC2Ev 174 Type: STT_FUNC 175 Section: .text 176 Size: 0x000000000000000D 177 Binding: STB_GLOBAL 178 - Name: _ZN1AD2Ev 179 Type: STT_FUNC 180 Section: .text 181 Size: 0x000000000000000D 182 Binding: STB_GLOBAL 183 - Name: _ZN1A3barEv 184 Type: STT_FUNC 185 Section: .text 186 Size: 0x000000000000000D 187 Binding: STB_GLOBAL 188 - Name: _ZGVZN4llvm4dbgsEvE7thestrm 189 Type: STT_FUNC 190 Section: .text 191 Size: 0x000000000000000D 192 Binding: STB_GLOBAL 193 - Name: _ZZN4llvm4dbgsEvE7thestrm 194 Type: STT_FUNC 195 Section: .text 196 Size: 0x000000000000000D 197 Binding: STB_GLOBAL 198 - Name: _ZTVN5clang4DeclE 199 Type: STT_FUNC 200 Section: .text 201 Size: 0x000000000000000D 202 Binding: STB_GLOBAL 203 - Name: -[ObjCfoo] 204 Type: STT_FUNC 205 Section: .text 206 Size: 0x000000000000000D 207 Binding: STB_GLOBAL 208 - Name: +[B ObjCbar(WithCategory)] 209 Type: STT_FUNC 210 Section: .text 211 Size: 0x000000000000000D 212 Binding: STB_GLOBAL 213 - Name: _Z12undemangableEvx42 214 Type: STT_FUNC 215 Section: .text 216 Size: 0x000000000000000D 217 Binding: STB_GLOBAL 218 ... 219 )"); 220 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded()); 221 222 auto M = std::make_shared<Module>(ExpectedFile->moduleSpec()); 223 224 auto Count = [M](const char *Name, FunctionNameType Type) -> int { 225 SymbolContextList SymList; 226 M->FindFunctionSymbols(ConstString(Name), Type, SymList); 227 return SymList.GetSize(); 228 }; 229 230 // Unmangled 231 EXPECT_EQ(1, Count("main", eFunctionNameTypeFull)); 232 EXPECT_EQ(1, Count("main", eFunctionNameTypeBase)); 233 EXPECT_EQ(0, Count("main", eFunctionNameTypeMethod)); 234 235 // Itanium mangled 236 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeFull)); 237 EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeBase)); 238 EXPECT_EQ(1, Count("foo", eFunctionNameTypeBase)); 239 EXPECT_EQ(0, Count("foo", eFunctionNameTypeMethod)); 240 241 // Unmangled with linker annotation 242 EXPECT_EQ(1, Count("puts@GLIBC_2.5", eFunctionNameTypeFull)); 243 EXPECT_EQ(1, Count("puts@GLIBC_2.6", eFunctionNameTypeFull)); 244 EXPECT_EQ(2, Count("puts", eFunctionNameTypeFull)); 245 EXPECT_EQ(2, Count("puts", eFunctionNameTypeBase)); 246 EXPECT_EQ(0, Count("puts", eFunctionNameTypeMethod)); 247 248 // Itanium mangled with linker annotation 249 EXPECT_EQ(1, Count("_Z5annotv@VERSION3", eFunctionNameTypeFull)); 250 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeFull)); 251 EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeBase)); 252 EXPECT_EQ(0, Count("annot", eFunctionNameTypeBase)); 253 EXPECT_EQ(0, Count("annot", eFunctionNameTypeMethod)); 254 255 // Itanium mangled ctor A::A() 256 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeFull)); 257 EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeBase)); 258 EXPECT_EQ(1, Count("A", eFunctionNameTypeMethod)); 259 EXPECT_EQ(0, Count("A", eFunctionNameTypeBase)); 260 261 // Itanium mangled dtor A::~A() 262 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeFull)); 263 EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeBase)); 264 EXPECT_EQ(1, Count("~A", eFunctionNameTypeMethod)); 265 EXPECT_EQ(0, Count("~A", eFunctionNameTypeBase)); 266 267 // Itanium mangled method A::bar() 268 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeFull)); 269 EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeBase)); 270 EXPECT_EQ(1, Count("bar", eFunctionNameTypeMethod)); 271 EXPECT_EQ(0, Count("bar", eFunctionNameTypeBase)); 272 273 // Itanium mangled names that are explicitly excluded from parsing 274 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull)); 275 EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase)); 276 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod)); 277 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase)); 278 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull)); 279 EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase)); 280 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod)); 281 EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase)); 282 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeFull)); 283 EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeBase)); 284 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeMethod)); 285 EXPECT_EQ(0, Count("Decl", eFunctionNameTypeBase)); 286 287 // ObjC mangled static 288 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeFull)); 289 EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeBase)); 290 EXPECT_EQ(0, Count("ObjCfoo", eFunctionNameTypeMethod)); 291 292 // ObjC mangled method with category 293 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeFull)); 294 EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeBase)); 295 EXPECT_EQ(0, Count("ObjCbar", eFunctionNameTypeMethod)); 296 297 // Invalid things: unable to decode but still possible to find by full name 298 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeFull)); 299 EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeBase)); 300 EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod)); 301 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase)); 302 EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod)); 303 } 304