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 "Plugins/Platform/Linux/PlatformLinux.h" 11 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 12 #include "TestingSupport/Symbol/YAMLModuleTester.h" 13 #include "lldb/Core/Value.h" 14 #include "lldb/Core/Debugger.h" 15 #include "lldb/Core/dwarf.h" 16 #include "lldb/Host/HostInfo.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Utility/Reproducer.h" 19 #include "lldb/Utility/StreamString.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/Testing/Support/Error.h" 22 #include "gtest/gtest.h" 23 24 using namespace lldb_private; 25 using namespace lldb_private::dwarf; 26 27 static llvm::Expected<Scalar> Evaluate(llvm::ArrayRef<uint8_t> expr, 28 lldb::ModuleSP module_sp = {}, 29 DWARFUnit *unit = nullptr, 30 ExecutionContext *exe_ctx = nullptr) { 31 DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle, 32 /*addr_size*/ 4); 33 Value result; 34 Status status; 35 if (!DWARFExpression::Evaluate(exe_ctx, /*reg_ctx*/ nullptr, module_sp, 36 extractor, unit, lldb::eRegisterKindLLDB, 37 /*initial_value_ptr*/ nullptr, 38 /*object_address_ptr*/ nullptr, result, 39 &status)) 40 return status.ToError(); 41 42 switch (result.GetValueType()) { 43 case Value::ValueType::Scalar: 44 return result.GetScalar(); 45 case Value::ValueType::LoadAddress: 46 return LLDB_INVALID_ADDRESS; 47 case Value::ValueType::HostAddress: { 48 // Convert small buffers to scalars to simplify the tests. 49 DataBufferHeap &buf = result.GetBuffer(); 50 if (buf.GetByteSize() <= 8) { 51 uint64_t val = 0; 52 memcpy(&val, buf.GetBytes(), buf.GetByteSize()); 53 return Scalar(llvm::APInt(buf.GetByteSize()*8, val, false)); 54 } 55 } 56 LLVM_FALLTHROUGH; 57 default: 58 return status.ToError(); 59 } 60 } 61 62 class DWARFExpressionTester : public YAMLModuleTester { 63 public: 64 using YAMLModuleTester::YAMLModuleTester; 65 llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) { 66 return ::Evaluate(expr, m_module_sp, m_dwarf_unit); 67 } 68 }; 69 70 /// Unfortunately Scalar's operator==() is really picky. 71 static Scalar GetScalar(unsigned bits, uint64_t value, bool sign) { 72 Scalar scalar(value); 73 scalar.TruncOrExtendTo(bits, sign); 74 return scalar; 75 } 76 77 /// This is needed for the tests that use a mock process. 78 class DWARFExpressionMockProcessTest : public ::testing::Test { 79 public: 80 void SetUp() override { 81 llvm::cantFail(repro::Reproducer::Initialize(repro::ReproducerMode::Off, {})); 82 FileSystem::Initialize(); 83 HostInfo::Initialize(); 84 platform_linux::PlatformLinux::Initialize(); 85 } 86 void TearDown() override { 87 platform_linux::PlatformLinux::Terminate(); 88 HostInfo::Terminate(); 89 FileSystem::Terminate(); 90 repro::Reproducer::Terminate(); 91 } 92 }; 93 94 TEST(DWARFExpression, DW_OP_pick) { 95 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 0}), 96 llvm::HasValue(0)); 97 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 1}), 98 llvm::HasValue(1)); 99 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 2}), 100 llvm::Failed()); 101 } 102 103 TEST(DWARFExpression, DW_OP_const) { 104 // Extend to address size. 105 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const1u, 0x88}), llvm::HasValue(0x88)); 106 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const1s, 0x88}), 107 llvm::HasValue(0xffffff88)); 108 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x47, 0x88}), 109 llvm::HasValue(0x8847)); 110 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2s, 0x47, 0x88}), 111 llvm::HasValue(0xffff8847)); 112 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const4u, 0x44, 0x42, 0x47, 0x88}), 113 llvm::HasValue(0x88474244)); 114 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const4s, 0x44, 0x42, 0x47, 0x88}), 115 llvm::HasValue(0x88474244)); 116 117 // Truncate to address size. 118 EXPECT_THAT_EXPECTED( 119 Evaluate({DW_OP_const8u, 0x00, 0x11, 0x22, 0x33, 0x44, 0x42, 0x47, 0x88}), 120 llvm::HasValue(0x33221100)); 121 EXPECT_THAT_EXPECTED( 122 Evaluate({DW_OP_const8s, 0x00, 0x11, 0x22, 0x33, 0x44, 0x42, 0x47, 0x88}), 123 llvm::HasValue(0x33221100)); 124 125 // Don't truncate to address size for compatibility with clang (pr48087). 126 EXPECT_THAT_EXPECTED( 127 Evaluate({DW_OP_constu, 0x81, 0x82, 0x84, 0x88, 0x90, 0xa0, 0x40}), 128 llvm::HasValue(0x01010101010101)); 129 EXPECT_THAT_EXPECTED( 130 Evaluate({DW_OP_consts, 0x81, 0x82, 0x84, 0x88, 0x90, 0xa0, 0x40}), 131 llvm::HasValue(0xffff010101010101)); 132 } 133 134 TEST(DWARFExpression, DW_OP_convert) { 135 /// Auxiliary debug info. 136 const char *yamldata = R"( 137 --- !ELF 138 FileHeader: 139 Class: ELFCLASS64 140 Data: ELFDATA2LSB 141 Type: ET_EXEC 142 Machine: EM_386 143 DWARF: 144 debug_abbrev: 145 - Table: 146 - Code: 0x00000001 147 Tag: DW_TAG_compile_unit 148 Children: DW_CHILDREN_yes 149 Attributes: 150 - Attribute: DW_AT_language 151 Form: DW_FORM_data2 152 - Code: 0x00000002 153 Tag: DW_TAG_base_type 154 Children: DW_CHILDREN_no 155 Attributes: 156 - Attribute: DW_AT_encoding 157 Form: DW_FORM_data1 158 - Attribute: DW_AT_byte_size 159 Form: DW_FORM_data1 160 debug_info: 161 - Version: 4 162 AddrSize: 8 163 Entries: 164 - AbbrCode: 0x00000001 165 Values: 166 - Value: 0x000000000000000C 167 # 0x0000000e: 168 - AbbrCode: 0x00000002 169 Values: 170 - Value: 0x0000000000000007 # DW_ATE_unsigned 171 - Value: 0x0000000000000004 172 # 0x00000011: 173 - AbbrCode: 0x00000002 174 Values: 175 - Value: 0x0000000000000007 # DW_ATE_unsigned 176 - Value: 0x0000000000000008 177 # 0x00000014: 178 - AbbrCode: 0x00000002 179 Values: 180 - Value: 0x0000000000000005 # DW_ATE_signed 181 - Value: 0x0000000000000008 182 # 0x00000017: 183 - AbbrCode: 0x00000002 184 Values: 185 - Value: 0x0000000000000008 # DW_ATE_unsigned_char 186 - Value: 0x0000000000000001 187 # 0x0000001a: 188 - AbbrCode: 0x00000002 189 Values: 190 - Value: 0x0000000000000006 # DW_ATE_signed_char 191 - Value: 0x0000000000000001 192 # 0x0000001d: 193 - AbbrCode: 0x00000002 194 Values: 195 - Value: 0x000000000000000b # DW_ATE_numeric_string 196 - Value: 0x0000000000000001 197 - AbbrCode: 0x00000000 198 )"; 199 uint8_t offs_uint32_t = 0x0000000e; 200 uint8_t offs_uint64_t = 0x00000011; 201 uint8_t offs_sint64_t = 0x00000014; 202 uint8_t offs_uchar = 0x00000017; 203 uint8_t offs_schar = 0x0000001a; 204 205 DWARFExpressionTester t(yamldata); 206 ASSERT_TRUE((bool)t.GetDwarfUnit()); 207 208 // Constant is given as little-endian. 209 bool is_signed = true; 210 bool not_signed = false; 211 212 // 213 // Positive tests. 214 // 215 216 // Leave as is. 217 EXPECT_THAT_EXPECTED( 218 t.Eval({DW_OP_const4u, 0x11, 0x22, 0x33, 0x44, // 219 DW_OP_convert, offs_uint32_t, DW_OP_stack_value}), 220 llvm::HasValue(GetScalar(64, 0x44332211, not_signed))); 221 222 // Zero-extend to 64 bits. 223 EXPECT_THAT_EXPECTED( 224 t.Eval({DW_OP_const4u, 0x11, 0x22, 0x33, 0x44, // 225 DW_OP_convert, offs_uint64_t, DW_OP_stack_value}), 226 llvm::HasValue(GetScalar(64, 0x44332211, not_signed))); 227 228 // Sign-extend to 64 bits. 229 EXPECT_THAT_EXPECTED( 230 t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, // 231 DW_OP_convert, offs_sint64_t, DW_OP_stack_value}), 232 llvm::HasValue(GetScalar(64, 0xffffffffffeeddcc, is_signed))); 233 234 // Sign-extend, then truncate. 235 EXPECT_THAT_EXPECTED( 236 t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, // 237 DW_OP_convert, offs_sint64_t, // 238 DW_OP_convert, offs_uint32_t, DW_OP_stack_value}), 239 llvm::HasValue(GetScalar(32, 0xffeeddcc, not_signed))); 240 241 // Truncate to default unspecified (pointer-sized) type. 242 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, // 243 DW_OP_convert, offs_sint64_t, // 244 DW_OP_convert, 0x00, DW_OP_stack_value}), 245 llvm::HasValue(GetScalar(32, 0xffeeddcc, not_signed))); 246 247 // Truncate to 8 bits. 248 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, 249 offs_uchar, DW_OP_stack_value}), 250 llvm::HasValue(GetScalar(8, 'A', not_signed))); 251 252 // Also truncate to 8 bits. 253 EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, 254 offs_schar, DW_OP_stack_value}), 255 llvm::HasValue(GetScalar(8, 'A', is_signed))); 256 257 // 258 // Errors. 259 // 260 261 // No Module. 262 EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr, 263 t.GetDwarfUnit()) 264 .takeError(), 265 llvm::Failed()); 266 267 // No DIE. 268 EXPECT_THAT_ERROR( 269 t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x01}).takeError(), 270 llvm::Failed()); 271 272 // Unsupported. 273 EXPECT_THAT_ERROR( 274 t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x1d}).takeError(), 275 llvm::Failed()); 276 } 277 278 TEST(DWARFExpression, DW_OP_stack_value) { 279 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_stack_value}), llvm::Failed()); 280 } 281 282 TEST(DWARFExpression, DW_OP_piece) { 283 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x11, 0x22, DW_OP_piece, 2, 284 DW_OP_const2u, 0x33, 0x44, DW_OP_piece, 2}), 285 llvm::HasValue(GetScalar(32, 0x44332211, true))); 286 EXPECT_THAT_EXPECTED( 287 Evaluate({DW_OP_piece, 1, DW_OP_const1u, 0xff, DW_OP_piece, 1}), 288 // Note that the "00" should really be "undef", but we can't 289 // represent that yet. 290 llvm::HasValue(GetScalar(16, 0xff00, true))); 291 } 292 293 TEST(DWARFExpression, DW_OP_implicit_value) { 294 unsigned char bytes = 4; 295 296 EXPECT_THAT_EXPECTED( 297 Evaluate({DW_OP_implicit_value, bytes, 0x11, 0x22, 0x33, 0x44}), 298 llvm::HasValue(GetScalar(8 * bytes, 0x44332211, true))); 299 } 300 301 TEST(DWARFExpression, DW_OP_unknown) { 302 EXPECT_THAT_EXPECTED( 303 Evaluate({0xff}), 304 llvm::FailedWithMessage( 305 "Unhandled opcode DW_OP_unknown_ff in DWARFExpression")); 306 } 307 308 TEST_F(DWARFExpressionMockProcessTest, DW_OP_deref) { 309 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit0, DW_OP_deref}), llvm::Failed()); 310 311 struct MockProcess : Process { 312 using Process::Process; 313 llvm::StringRef GetPluginName() override { return "mock process"; } 314 bool CanDebug(lldb::TargetSP target, 315 bool plugin_specified_by_name) override { 316 return false; 317 }; 318 Status DoDestroy() override { return {}; } 319 void RefreshStateAfterStop() override {} 320 bool DoUpdateThreadList(ThreadList &old_thread_list, 321 ThreadList &new_thread_list) override { 322 return false; 323 }; 324 size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, 325 Status &error) override { 326 for (size_t i = 0; i < size; ++i) 327 ((char *)buf)[i] = (vm_addr + i) & 0xff; 328 error.Clear(); 329 return size; 330 } 331 }; 332 333 // Set up a mock process. 334 ArchSpec arch("i386-pc-linux"); 335 Platform::SetHostPlatform( 336 platform_linux::PlatformLinux::CreateInstance(true, &arch)); 337 lldb::DebuggerSP debugger_sp = Debugger::CreateInstance(); 338 ASSERT_TRUE(debugger_sp); 339 lldb::TargetSP target_sp; 340 lldb::PlatformSP platform_sp; 341 debugger_sp->GetTargetList().CreateTarget( 342 *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); 343 ASSERT_TRUE(target_sp); 344 ASSERT_TRUE(target_sp->GetArchitecture().IsValid()); 345 ASSERT_TRUE(platform_sp); 346 lldb::ListenerSP listener_sp(Listener::MakeListener("dummy")); 347 lldb::ProcessSP process_sp = 348 std::make_shared<MockProcess>(target_sp, listener_sp); 349 ASSERT_TRUE(process_sp); 350 351 ExecutionContext exe_ctx(process_sp); 352 // Implicit location: *0x4. 353 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4, DW_OP_deref, DW_OP_stack_value}, 354 {}, {}, &exe_ctx), 355 llvm::HasValue(GetScalar(32, 0x07060504, false))); 356 // Memory location: *(*0x4). 357 // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses. 358 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4, DW_OP_deref}, {}, {}, &exe_ctx), 359 llvm::HasValue(Scalar(LLDB_INVALID_ADDRESS))); 360 // Memory location: *0x4. 361 // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses. 362 EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4}, {}, {}, &exe_ctx), 363 llvm::HasValue(Scalar(4))); 364 // Implicit location: *0x4. 365 // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses. 366 EXPECT_THAT_EXPECTED( 367 Evaluate({DW_OP_lit4, DW_OP_deref, DW_OP_stack_value}, {}, {}, &exe_ctx), 368 llvm::HasValue(GetScalar(32, 0x07060504, false))); 369 } 370