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