1 //===-- DWARFExpressionTest.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/Expression/DWARFExpression.h" 10 #include "../../source/Plugins/SymbolFile/DWARF/DWARFUnit.h" 11 #include "../../source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" 12 #include "TestingSupport/SubsystemRAII.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/Section.h" 15 #include "lldb/Core/Value.h" 16 #include "lldb/Core/dwarf.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Utility/StreamString.h" 19 #include "llvm/ADT/StringExtras.h" 20 #include "llvm/ObjectYAML/DWARFEmitter.h" 21 #include "llvm/Testing/Support/Error.h" 22 #include "gtest/gtest.h" 23 24 using namespace lldb_private; 25 26 static llvm::Expected<Scalar> Evaluate(llvm::ArrayRef<uint8_t> expr, 27 lldb::ModuleSP module_sp = {}, 28 DWARFUnit *unit = nullptr) { 29 DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle, 30 /*addr_size*/ 4); 31 Value result; 32 Status status; 33 if (!DWARFExpression::Evaluate( 34 /*exe_ctx*/ nullptr, /*reg_ctx*/ nullptr, module_sp, extractor, unit, 35 lldb::eRegisterKindLLDB, 36 /*initial_value_ptr*/ nullptr, 37 /*object_address_ptr*/ nullptr, result, &status)) 38 return status.ToError(); 39 40 switch (result.GetValueType()) { 41 case Value::eValueTypeScalar: 42 return result.GetScalar(); 43 case Value::eValueTypeHostAddress: { 44 // Convert small buffers to scalars to simplify the tests. 45 DataBufferHeap &buf = result.GetBuffer(); 46 if (buf.GetByteSize() <= 8) { 47 uint64_t val = 0; 48 memcpy(&val, buf.GetBytes(), buf.GetByteSize()); 49 return Scalar(llvm::APInt(buf.GetByteSize()*8, val, false)); 50 } 51 } 52 LLVM_FALLTHROUGH; 53 default: 54 return status.ToError(); 55 } 56 } 57 58 /// A mock module holding an object file parsed from YAML. 59 class YAMLModule : public lldb_private::Module { 60 public: 61 YAMLModule(ArchSpec &arch) : Module(FileSpec("test"), arch) {} 62 void SetObjectFile(lldb::ObjectFileSP obj_file) { m_objfile_sp = obj_file; } 63 ObjectFile *GetObjectFile() override { return m_objfile_sp.get(); } 64 }; 65 66 /// A mock object file that can be parsed from YAML. 67 class YAMLObjectFile : public lldb_private::ObjectFile { 68 const lldb::ModuleSP m_module_sp; 69 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &m_section_map; 70 /// Because there is only one DataExtractor in the ObjectFile 71 /// interface, all sections are copied into a contiguous buffer. 72 std::vector<char> m_buffer; 73 74 public: 75 YAMLObjectFile(const lldb::ModuleSP &module_sp, 76 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> &map) 77 : ObjectFile(module_sp, &module_sp->GetFileSpec(), /*file_offset*/ 0, 78 /*length*/ 0, /*data_sp*/ nullptr, /*data_offset*/ 0), 79 m_module_sp(module_sp), m_section_map(map) {} 80 81 /// Callback for initializing the module's list of sections. 82 void CreateSections(SectionList &unified_section_list) override { 83 lldb::offset_t total_bytes = 0; 84 for (auto &entry : m_section_map) 85 total_bytes += entry.getValue()->getBufferSize(); 86 m_buffer.reserve(total_bytes); 87 m_data = 88 DataExtractor(m_buffer.data(), total_bytes, lldb::eByteOrderLittle, 4); 89 90 lldb::user_id_t sect_id = 1; 91 for (auto &entry : m_section_map) { 92 llvm::StringRef name = entry.getKey(); 93 lldb::SectionType sect_type = 94 llvm::StringSwitch<lldb::SectionType>(name) 95 .Case("debug_info", lldb::eSectionTypeDWARFDebugInfo) 96 .Case("debug_abbrev", lldb::eSectionTypeDWARFDebugAbbrev); 97 auto &membuf = entry.getValue(); 98 lldb::addr_t file_vm_addr = 0; 99 lldb::addr_t vm_size = 0; 100 lldb::offset_t file_offset = m_buffer.size(); 101 lldb::offset_t file_size = membuf->getBufferSize(); 102 m_buffer.resize(file_offset + file_size); 103 memcpy(m_buffer.data() + file_offset, membuf->getBufferStart(), 104 file_size); 105 uint32_t log2align = 0; 106 uint32_t flags = 0; 107 auto section_sp = std::make_shared<lldb_private::Section>( 108 m_module_sp, this, sect_id++, ConstString(name), sect_type, 109 file_vm_addr, vm_size, file_offset, file_size, log2align, flags); 110 unified_section_list.AddSection(section_sp); 111 } 112 } 113 114 /// \{ 115 /// Stub methods that aren't needed here. 116 ConstString GetPluginName() override { return ConstString("YAMLObjectFile"); } 117 uint32_t GetPluginVersion() override { return 0; } 118 void Dump(Stream *s) override {} 119 uint32_t GetAddressByteSize() const override { return 8; } 120 uint32_t GetDependentModules(FileSpecList &file_list) override { return 0; } 121 bool IsExecutable() const override { return 0; } 122 ArchSpec GetArchitecture() override { return {}; } 123 Symtab *GetSymtab() override { return nullptr; } 124 bool IsStripped() override { return false; } 125 UUID GetUUID() override { return {}; } 126 lldb::ByteOrder GetByteOrder() const override { 127 return lldb::eByteOrderLittle; 128 } 129 bool ParseHeader() override { return false; } 130 Type CalculateType() override { return {}; } 131 Strata CalculateStrata() override { return {}; } 132 /// \} 133 }; 134 135 /// Helper class that can construct a module from YAML and evaluate 136 /// DWARF expressions on it. 137 class YAMLModuleTester { 138 SubsystemRAII<FileSystem> subsystems; 139 llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> m_sections_map; 140 lldb::ModuleSP m_module_sp; 141 lldb::ObjectFileSP m_objfile_sp; 142 DWARFUnitSP m_dwarf_unit; 143 std::unique_ptr<SymbolFileDWARF> m_symfile_dwarf; 144 145 public: 146 /// Parse the debug info sections from the YAML description. 147 YAMLModuleTester(llvm::StringRef yaml_data, llvm::StringRef triple) { 148 auto sections_map = llvm::DWARFYAML::EmitDebugSections(yaml_data, true); 149 if (!sections_map) 150 return; 151 m_sections_map = std::move(*sections_map); 152 ArchSpec arch(triple); 153 m_module_sp = std::make_shared<YAMLModule>(arch); 154 m_objfile_sp = std::make_shared<YAMLObjectFile>(m_module_sp, m_sections_map); 155 static_cast<YAMLModule *>(m_module_sp.get())->SetObjectFile(m_objfile_sp); 156 157 lldb::user_id_t uid = 0; 158 llvm::StringRef raw_debug_info = m_sections_map["debug_info"]->getBuffer(); 159 lldb_private::DataExtractor debug_info( 160 raw_debug_info.data(), raw_debug_info.size(), 161 m_objfile_sp->GetByteOrder(), m_objfile_sp->GetAddressByteSize()); 162 lldb::offset_t offset_ptr = 0; 163 m_symfile_dwarf = std::make_unique<SymbolFileDWARF>(m_objfile_sp, nullptr); 164 llvm::Expected<DWARFUnitSP> dwarf_unit = DWARFUnit::extract( 165 *m_symfile_dwarf, uid, 166 *static_cast<lldb_private::DWARFDataExtractor *>(&debug_info), 167 DIERef::DebugInfo, &offset_ptr); 168 if (dwarf_unit) 169 m_dwarf_unit = dwarf_unit.get(); 170 } 171 DWARFUnitSP GetDwarfUnit() { return m_dwarf_unit; } 172 173 // Evaluate a raw DWARF expression. 174 llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) { 175 return ::Evaluate(expr, m_module_sp, m_dwarf_unit.get()); 176 } 177 }; 178 179 /// Unfortunately Scalar's operator==() is really picky. 180 static Scalar GetScalar(unsigned bits, uint64_t value, bool sign) { 181 Scalar scalar; 182 auto type = Scalar::GetBestTypeForBitSize(bits, sign); 183 switch (type) { 184 case Scalar::e_sint: 185 scalar = Scalar((int)value); 186 break; 187 case Scalar::e_slong: 188 scalar = Scalar((long)value); 189 break; 190 case Scalar::e_slonglong: 191 scalar = Scalar((long long)value); 192 break; 193 case Scalar::e_uint: 194 scalar = Scalar((unsigned int)value); 195 break; 196 case Scalar::e_ulong: 197 scalar = Scalar((unsigned long)value); 198 break; 199 case Scalar::e_ulonglong: 200 scalar = Scalar((unsigned long long)value); 201 break; 202 default: 203 llvm_unreachable("not implemented"); 204 } 205 scalar.TruncOrExtendTo(type, bits); 206 if (sign) 207 scalar.MakeSigned(); 208 else 209 scalar.MakeUnsigned(); 210 return scalar; 211 } 212 213 TEST(DWARFExpression, DW_OP_pick) { 214 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 0}), 215 llvm::HasValue(0)); 216 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 1}), 217 llvm::HasValue(1)); 218 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 2}), 219 llvm::Failed()); 220 } 221 222 TEST(DWARFExpression, DW_OP_convert) { 223 /// Auxiliary debug info. 224 const char *yamldata = 225 "debug_abbrev:\n" 226 " - Code: 0x00000001\n" 227 " Tag: DW_TAG_compile_unit\n" 228 " Children: DW_CHILDREN_yes\n" 229 " Attributes:\n" 230 " - Attribute: DW_AT_language\n" 231 " Form: DW_FORM_data2\n" 232 " - Code: 0x00000002\n" 233 " Tag: DW_TAG_base_type\n" 234 " Children: DW_CHILDREN_no\n" 235 " Attributes:\n" 236 " - Attribute: DW_AT_encoding\n" 237 " Form: DW_FORM_data1\n" 238 " - Attribute: DW_AT_byte_size\n" 239 " Form: DW_FORM_data1\n" 240 "debug_info:\n" 241 " - Length:\n" 242 " TotalLength: 0\n" 243 " Version: 4\n" 244 " AbbrOffset: 0\n" 245 " AddrSize: 8\n" 246 " Entries:\n" 247 " - AbbrCode: 0x00000001\n" 248 " Values:\n" 249 " - Value: 0x000000000000000C\n" 250 // 0x0000000e: 251 " - AbbrCode: 0x00000002\n" 252 " Values:\n" 253 " - Value: 0x0000000000000007\n" // DW_ATE_unsigned 254 " - Value: 0x0000000000000004\n" 255 // 0x00000011: 256 " - AbbrCode: 0x00000002\n" 257 " Values:\n" 258 " - Value: 0x0000000000000007\n" // DW_ATE_unsigned 259 " - Value: 0x0000000000000008\n" 260 // 0x00000014: 261 " - AbbrCode: 0x00000002\n" 262 " Values:\n" 263 " - Value: 0x0000000000000005\n" // DW_ATE_signed 264 " - Value: 0x0000000000000008\n" 265 // 0x00000017: 266 " - AbbrCode: 0x00000002\n" 267 " Values:\n" 268 " - Value: 0x0000000000000008\n" // DW_ATE_unsigned_char 269 " - Value: 0x0000000000000001\n" 270 // 0x0000001a: 271 " - AbbrCode: 0x00000002\n" 272 " Values:\n" 273 " - Value: 0x0000000000000006\n" // DW_ATE_signed_char 274 " - Value: 0x0000000000000001\n" 275 // 0x0000001d: 276 " - AbbrCode: 0x00000002\n" 277 " Values:\n" 278 " - Value: 0x000000000000000b\n" // DW_ATE_numeric_string 279 " - Value: 0x0000000000000001\n" 280 "" 281 " - AbbrCode: 0x00000000\n" 282 " Values: []\n"; 283 uint8_t offs_uint32_t = 0x0000000e; 284 uint8_t offs_uint64_t = 0x00000011; 285 uint8_t offs_sint64_t = 0x00000014; 286 uint8_t offs_uchar = 0x00000017; 287 uint8_t offs_schar = 0x0000001a; 288 289 YAMLModuleTester t(yamldata, "i386-unknown-linux"); 290 ASSERT_TRUE((bool)t.GetDwarfUnit()); 291 292 // Constant is given as little-endian. 293 bool is_signed = true; 294 bool not_signed = false; 295 296 // 297 // Positive tests. 298 // 299 300 // Truncate to default unspecified (pointer-sized) type. 301 EXPECT_THAT_EXPECTED( 302 t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, // 303 DW_OP_convert, 0x00}), 304 llvm::HasValue(GetScalar(32, 0x44332211, not_signed))); 305 // Truncate to 32 bits. 306 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const8u, // 307 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,// 308 DW_OP_convert, offs_uint32_t}), 309 llvm::HasValue(GetScalar(32, 0x44332211, not_signed))); 310 311 // Leave as is. 312 EXPECT_THAT_EXPECTED( 313 t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, // 314 DW_OP_convert, offs_uint64_t}), 315 llvm::HasValue(GetScalar(64, 0x8877665544332211, not_signed))); 316 317 // Sign-extend to 64 bits. 318 EXPECT_THAT_EXPECTED( 319 t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, // 320 DW_OP_convert, offs_sint64_t}), 321 llvm::HasValue(GetScalar(64, 0xffffffffffeeddcc, is_signed))); 322 323 // Truncate to 8 bits. 324 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', 0xee, 0xff, // 325 DW_OP_convert, offs_uchar}), 326 llvm::HasValue(GetScalar(8, 'A', not_signed))); 327 328 // Also truncate to 8 bits. 329 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', 0xee, 0xff, // 330 DW_OP_convert, offs_schar}), 331 llvm::HasValue(GetScalar(8, 'A', is_signed))); 332 333 // 334 // Errors. 335 // 336 337 // No Module. 338 EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr, 339 t.GetDwarfUnit().get()) 340 .takeError(), 341 llvm::Failed()); 342 343 // No DIE. 344 EXPECT_THAT_ERROR( 345 t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x01}).takeError(), 346 llvm::Failed()); 347 348 // Unsupported. 349 EXPECT_THAT_ERROR( 350 t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x1d}).takeError(), 351 llvm::Failed()); 352 } 353 354 TEST(DWARFExpression, DW_OP_piece) { 355 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x11, 0x22, DW_OP_piece, 2, 356 DW_OP_const2u, 0x33, 0x44, DW_OP_piece, 2}), 357 llvm::HasValue(GetScalar(32, 0x44332211, true))); 358 EXPECT_THAT_EXPECTED( 359 Evaluate({DW_OP_piece, 1, DW_OP_const1u, 0xff, DW_OP_piece, 1}), 360 // Note that the "00" should really be "undef", but we can't 361 // represent that yet. 362 llvm::HasValue(GetScalar(16, 0xff00, true))); 363 } 364