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 
Evaluate(llvm::ArrayRef<uint8_t> expr,lldb::ModuleSP module_sp={},DWARFUnit * unit=nullptr,ExecutionContext * exe_ctx=nullptr)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;
Eval(llvm::ArrayRef<uint8_t> expr)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.
GetScalar(unsigned bits,uint64_t value,bool sign)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:
SetUp()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   }
TearDown()86   void TearDown() override {
87     platform_linux::PlatformLinux::Terminate();
88     HostInfo::Terminate();
89     FileSystem::Terminate();
90     repro::Reproducer::Terminate();
91   }
92 };
93 
TEST(DWARFExpression,DW_OP_pick)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 
TEST(DWARFExpression,DW_OP_const)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 
TEST(DWARFExpression,DW_OP_skip)134 TEST(DWARFExpression, DW_OP_skip) {
135   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const1u, 0x42, DW_OP_skip, 0x02, 0x00,
136                                  DW_OP_const1u, 0xff}),
137                        llvm::HasValue(0x42));
138 }
139 
TEST(DWARFExpression,DW_OP_bra)140 TEST(DWARFExpression, DW_OP_bra) {
141   EXPECT_THAT_EXPECTED(
142       // clang-format off
143       Evaluate({
144         DW_OP_const1u, 0x42,     // push 0x42
145         DW_OP_const1u, 0x1,      // push 0x1
146         DW_OP_bra, 0x02, 0x00,   // if 0x1 > 0, then skip 0x0002 opcodes
147         DW_OP_const1u, 0xff,     // push 0xff
148       }),
149       // clang-format on
150       llvm::HasValue(0x42));
151 }
152 
TEST(DWARFExpression,DW_OP_convert)153 TEST(DWARFExpression, DW_OP_convert) {
154   /// Auxiliary debug info.
155   const char *yamldata = R"(
156 --- !ELF
157 FileHeader:
158   Class:   ELFCLASS64
159   Data:    ELFDATA2LSB
160   Type:    ET_EXEC
161   Machine: EM_386
162 DWARF:
163   debug_abbrev:
164     - Table:
165         - Code:            0x00000001
166           Tag:             DW_TAG_compile_unit
167           Children:        DW_CHILDREN_yes
168           Attributes:
169             - Attribute:       DW_AT_language
170               Form:            DW_FORM_data2
171         - Code:            0x00000002
172           Tag:             DW_TAG_base_type
173           Children:        DW_CHILDREN_no
174           Attributes:
175             - Attribute:       DW_AT_encoding
176               Form:            DW_FORM_data1
177             - Attribute:       DW_AT_byte_size
178               Form:            DW_FORM_data1
179   debug_info:
180     - Version:         4
181       AddrSize:        8
182       Entries:
183         - AbbrCode:        0x00000001
184           Values:
185             - Value:           0x000000000000000C
186         # 0x0000000e:
187         - AbbrCode:        0x00000002
188           Values:
189             - Value:           0x0000000000000007 # DW_ATE_unsigned
190             - Value:           0x0000000000000004
191         # 0x00000011:
192         - AbbrCode:        0x00000002
193           Values:
194             - Value:           0x0000000000000007 # DW_ATE_unsigned
195             - Value:           0x0000000000000008
196         # 0x00000014:
197         - AbbrCode:        0x00000002
198           Values:
199             - Value:           0x0000000000000005 # DW_ATE_signed
200             - Value:           0x0000000000000008
201         # 0x00000017:
202         - AbbrCode:        0x00000002
203           Values:
204             - Value:           0x0000000000000008 # DW_ATE_unsigned_char
205             - Value:           0x0000000000000001
206         # 0x0000001a:
207         - AbbrCode:        0x00000002
208           Values:
209             - Value:           0x0000000000000006 # DW_ATE_signed_char
210             - Value:           0x0000000000000001
211         # 0x0000001d:
212         - AbbrCode:        0x00000002
213           Values:
214             - Value:           0x000000000000000b # DW_ATE_numeric_string
215             - Value:           0x0000000000000001
216         - AbbrCode:        0x00000000
217 )";
218   uint8_t offs_uint32_t = 0x0000000e;
219   uint8_t offs_uint64_t = 0x00000011;
220   uint8_t offs_sint64_t = 0x00000014;
221   uint8_t offs_uchar = 0x00000017;
222   uint8_t offs_schar = 0x0000001a;
223 
224   DWARFExpressionTester t(yamldata);
225   ASSERT_TRUE((bool)t.GetDwarfUnit());
226 
227   // Constant is given as little-endian.
228   bool is_signed = true;
229   bool not_signed = false;
230 
231   //
232   // Positive tests.
233   //
234 
235   // Leave as is.
236   EXPECT_THAT_EXPECTED(
237       t.Eval({DW_OP_const4u, 0x11, 0x22, 0x33, 0x44, //
238               DW_OP_convert, offs_uint32_t, DW_OP_stack_value}),
239       llvm::HasValue(GetScalar(64, 0x44332211, not_signed)));
240 
241   // Zero-extend to 64 bits.
242   EXPECT_THAT_EXPECTED(
243       t.Eval({DW_OP_const4u, 0x11, 0x22, 0x33, 0x44, //
244               DW_OP_convert, offs_uint64_t, DW_OP_stack_value}),
245       llvm::HasValue(GetScalar(64, 0x44332211, not_signed)));
246 
247   // Sign-extend to 64 bits.
248   EXPECT_THAT_EXPECTED(
249       t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, //
250               DW_OP_convert, offs_sint64_t, DW_OP_stack_value}),
251       llvm::HasValue(GetScalar(64, 0xffffffffffeeddcc, is_signed)));
252 
253   // Sign-extend, then truncate.
254   EXPECT_THAT_EXPECTED(
255       t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, //
256               DW_OP_convert, offs_sint64_t,          //
257               DW_OP_convert, offs_uint32_t, DW_OP_stack_value}),
258       llvm::HasValue(GetScalar(32, 0xffeeddcc, not_signed)));
259 
260   // Truncate to default unspecified (pointer-sized) type.
261   EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, //
262                                DW_OP_convert, offs_sint64_t,          //
263                                DW_OP_convert, 0x00, DW_OP_stack_value}),
264                        llvm::HasValue(GetScalar(32, 0xffeeddcc, not_signed)));
265 
266   // Truncate to 8 bits.
267   EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert,
268                                offs_uchar, DW_OP_stack_value}),
269                        llvm::HasValue(GetScalar(8, 'A', not_signed)));
270 
271   // Also truncate to 8 bits.
272   EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert,
273                                offs_schar, DW_OP_stack_value}),
274                        llvm::HasValue(GetScalar(8, 'A', is_signed)));
275 
276   //
277   // Errors.
278   //
279 
280   // No Module.
281   EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr,
282                              t.GetDwarfUnit())
283                         .takeError(),
284                     llvm::Failed());
285 
286   // No DIE.
287   EXPECT_THAT_ERROR(
288       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x01}).takeError(),
289       llvm::Failed());
290 
291   // Unsupported.
292   EXPECT_THAT_ERROR(
293       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x1d}).takeError(),
294       llvm::Failed());
295 }
296 
TEST(DWARFExpression,DW_OP_stack_value)297 TEST(DWARFExpression, DW_OP_stack_value) {
298   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_stack_value}), llvm::Failed());
299 }
300 
TEST(DWARFExpression,DW_OP_piece)301 TEST(DWARFExpression, DW_OP_piece) {
302   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x11, 0x22, DW_OP_piece, 2,
303                                  DW_OP_const2u, 0x33, 0x44, DW_OP_piece, 2}),
304                        llvm::HasValue(GetScalar(32, 0x44332211, true)));
305   EXPECT_THAT_EXPECTED(
306       Evaluate({DW_OP_piece, 1, DW_OP_const1u, 0xff, DW_OP_piece, 1}),
307       // Note that the "00" should really be "undef", but we can't
308       // represent that yet.
309       llvm::HasValue(GetScalar(16, 0xff00, true)));
310 }
311 
TEST(DWARFExpression,DW_OP_implicit_value)312 TEST(DWARFExpression, DW_OP_implicit_value) {
313   unsigned char bytes = 4;
314 
315   EXPECT_THAT_EXPECTED(
316       Evaluate({DW_OP_implicit_value, bytes, 0x11, 0x22, 0x33, 0x44}),
317       llvm::HasValue(GetScalar(8 * bytes, 0x44332211, true)));
318 }
319 
TEST(DWARFExpression,DW_OP_unknown)320 TEST(DWARFExpression, DW_OP_unknown) {
321   EXPECT_THAT_EXPECTED(
322       Evaluate({0xff}),
323       llvm::FailedWithMessage(
324           "Unhandled opcode DW_OP_unknown_ff in DWARFExpression"));
325 }
326 
TEST_F(DWARFExpressionMockProcessTest,DW_OP_deref)327 TEST_F(DWARFExpressionMockProcessTest, DW_OP_deref) {
328   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit0, DW_OP_deref}), llvm::Failed());
329 
330   struct MockProcess : Process {
331     using Process::Process;
332     llvm::StringRef GetPluginName() override { return "mock process"; }
333     bool CanDebug(lldb::TargetSP target,
334                   bool plugin_specified_by_name) override {
335       return false;
336     };
337     Status DoDestroy() override { return {}; }
338     void RefreshStateAfterStop() override {}
339     bool DoUpdateThreadList(ThreadList &old_thread_list,
340                             ThreadList &new_thread_list) override {
341       return false;
342     };
343     size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
344                         Status &error) override {
345       for (size_t i = 0; i < size; ++i)
346         ((char *)buf)[i] = (vm_addr + i) & 0xff;
347       error.Clear();
348       return size;
349     }
350   };
351 
352   // Set up a mock process.
353   ArchSpec arch("i386-pc-linux");
354   Platform::SetHostPlatform(
355       platform_linux::PlatformLinux::CreateInstance(true, &arch));
356   lldb::DebuggerSP debugger_sp = Debugger::CreateInstance();
357   ASSERT_TRUE(debugger_sp);
358   lldb::TargetSP target_sp;
359   lldb::PlatformSP platform_sp;
360   debugger_sp->GetTargetList().CreateTarget(
361       *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp);
362   ASSERT_TRUE(target_sp);
363   ASSERT_TRUE(target_sp->GetArchitecture().IsValid());
364   ASSERT_TRUE(platform_sp);
365   lldb::ListenerSP listener_sp(Listener::MakeListener("dummy"));
366   lldb::ProcessSP process_sp =
367       std::make_shared<MockProcess>(target_sp, listener_sp);
368   ASSERT_TRUE(process_sp);
369 
370   ExecutionContext exe_ctx(process_sp);
371   // Implicit location: *0x4.
372   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4, DW_OP_deref, DW_OP_stack_value},
373                                 {}, {}, &exe_ctx),
374                        llvm::HasValue(GetScalar(32, 0x07060504, false)));
375   // Memory location: *(*0x4).
376   // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses.
377   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4, DW_OP_deref}, {}, {}, &exe_ctx),
378                        llvm::HasValue(Scalar(LLDB_INVALID_ADDRESS)));
379   // Memory location: *0x4.
380   // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses.
381   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit4}, {}, {}, &exe_ctx),
382                        llvm::HasValue(Scalar(4)));
383   // Implicit location: *0x4.
384   // Evaluate returns LLDB_INVALID_ADDRESS for all load addresses.
385   EXPECT_THAT_EXPECTED(
386       Evaluate({DW_OP_lit4, DW_OP_deref, DW_OP_stack_value}, {}, {}, &exe_ctx),
387       llvm::HasValue(GetScalar(32, 0x07060504, false)));
388 }
389