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/TypeSystem/Clang/TypeSystemClang.h"
11 #include "TestingSupport/Symbol/YAMLModuleTester.h"
12 #include "lldb/Core/Value.h"
13 #include "lldb/Core/dwarf.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Utility/StreamString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Testing/Support/Error.h"
18 #include "gtest/gtest.h"
19 
20 using namespace lldb_private;
21 
22 static llvm::Expected<Scalar> Evaluate(llvm::ArrayRef<uint8_t> expr,
23                                        lldb::ModuleSP module_sp = {},
24                                        DWARFUnit *unit = nullptr) {
25   DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle,
26                           /*addr_size*/ 4);
27   Value result;
28   Status status;
29   if (!DWARFExpression::Evaluate(
30           /*exe_ctx*/ nullptr, /*reg_ctx*/ nullptr, module_sp, extractor, unit,
31           lldb::eRegisterKindLLDB,
32           /*initial_value_ptr*/ nullptr,
33           /*object_address_ptr*/ nullptr, result, &status))
34     return status.ToError();
35 
36   switch (result.GetValueType()) {
37   case Value::eValueTypeScalar:
38     return result.GetScalar();
39   case Value::eValueTypeHostAddress: {
40     // Convert small buffers to scalars to simplify the tests.
41     DataBufferHeap &buf = result.GetBuffer();
42     if (buf.GetByteSize() <= 8) {
43       uint64_t val = 0;
44       memcpy(&val, buf.GetBytes(), buf.GetByteSize());
45       return Scalar(llvm::APInt(buf.GetByteSize()*8, val, false));
46     }
47   }
48     LLVM_FALLTHROUGH;
49   default:
50     return status.ToError();
51   }
52 }
53 
54 class DWARFExpressionTester : public YAMLModuleTester {
55 public:
56   using YAMLModuleTester::YAMLModuleTester;
57   llvm::Expected<Scalar> Eval(llvm::ArrayRef<uint8_t> expr) {
58     return ::Evaluate(expr, m_module_sp, m_dwarf_unit);
59   }
60 };
61 
62 /// Unfortunately Scalar's operator==() is really picky.
63 static Scalar GetScalar(unsigned bits, uint64_t value, bool sign) {
64   Scalar scalar(value);
65   scalar.TruncOrExtendTo(bits, sign);
66   return scalar;
67 }
68 
69 TEST(DWARFExpression, DW_OP_pick) {
70   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 0}),
71                        llvm::HasValue(0));
72   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 1}),
73                        llvm::HasValue(1));
74   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit1, DW_OP_lit0, DW_OP_pick, 2}),
75                        llvm::Failed());
76 }
77 
78 TEST(DWARFExpression, DW_OP_convert) {
79   /// Auxiliary debug info.
80   const char *yamldata = R"(
81 --- !ELF
82 FileHeader:
83   Class:   ELFCLASS64
84   Data:    ELFDATA2LSB
85   Type:    ET_EXEC
86   Machine: EM_386
87 DWARF:
88   debug_abbrev:
89     - Table:
90         - Code:            0x00000001
91           Tag:             DW_TAG_compile_unit
92           Children:        DW_CHILDREN_yes
93           Attributes:
94             - Attribute:       DW_AT_language
95               Form:            DW_FORM_data2
96         - Code:            0x00000002
97           Tag:             DW_TAG_base_type
98           Children:        DW_CHILDREN_no
99           Attributes:
100             - Attribute:       DW_AT_encoding
101               Form:            DW_FORM_data1
102             - Attribute:       DW_AT_byte_size
103               Form:            DW_FORM_data1
104   debug_info:
105     - Version:         4
106       AddrSize:        8
107       Entries:
108         - AbbrCode:        0x00000001
109           Values:
110             - Value:           0x000000000000000C
111         # 0x0000000e:
112         - AbbrCode:        0x00000002
113           Values:
114             - Value:           0x0000000000000007 # DW_ATE_unsigned
115             - Value:           0x0000000000000004
116         # 0x00000011:
117         - AbbrCode:        0x00000002
118           Values:
119             - Value:           0x0000000000000007 # DW_ATE_unsigned
120             - Value:           0x0000000000000008
121         # 0x00000014:
122         - AbbrCode:        0x00000002
123           Values:
124             - Value:           0x0000000000000005 # DW_ATE_signed
125             - Value:           0x0000000000000008
126         # 0x00000017:
127         - AbbrCode:        0x00000002
128           Values:
129             - Value:           0x0000000000000008 # DW_ATE_unsigned_char
130             - Value:           0x0000000000000001
131         # 0x0000001a:
132         - AbbrCode:        0x00000002
133           Values:
134             - Value:           0x0000000000000006 # DW_ATE_signed_char
135             - Value:           0x0000000000000001
136         # 0x0000001d:
137         - AbbrCode:        0x00000002
138           Values:
139             - Value:           0x000000000000000b # DW_ATE_numeric_string
140             - Value:           0x0000000000000001
141         - AbbrCode:        0x00000000
142 )";
143   uint8_t offs_uint32_t = 0x0000000e;
144   uint8_t offs_uint64_t = 0x00000011;
145   uint8_t offs_sint64_t = 0x00000014;
146   uint8_t offs_uchar = 0x00000017;
147   uint8_t offs_schar = 0x0000001a;
148 
149   DWARFExpressionTester t(yamldata);
150   ASSERT_TRUE((bool)t.GetDwarfUnit());
151 
152   // Constant is given as little-endian.
153   bool is_signed = true;
154   bool not_signed = false;
155 
156   //
157   // Positive tests.
158   //
159 
160   // Truncate to default unspecified (pointer-sized) type.
161   EXPECT_THAT_EXPECTED(
162       t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, //
163               DW_OP_convert, 0x00}),
164       llvm::HasValue(GetScalar(32, 0x44332211, not_signed)));
165   // Truncate to 32 bits.
166   EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const8u, //
167                                0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,//
168                                DW_OP_convert, offs_uint32_t}),
169                        llvm::HasValue(GetScalar(32, 0x44332211, not_signed)));
170 
171   // Leave as is.
172   EXPECT_THAT_EXPECTED(
173       t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, //
174               DW_OP_convert, offs_uint64_t}),
175       llvm::HasValue(GetScalar(64, 0x8877665544332211, not_signed)));
176 
177   // Sign-extend to 64 bits.
178   EXPECT_THAT_EXPECTED(
179       t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, //
180               DW_OP_convert, offs_sint64_t}),
181       llvm::HasValue(GetScalar(64, 0xffffffffffeeddcc, is_signed)));
182 
183   // Truncate to 8 bits.
184   EXPECT_THAT_EXPECTED(
185       t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, offs_uchar}),
186       llvm::HasValue(GetScalar(8, 'A', not_signed)));
187 
188   // Also truncate to 8 bits.
189   EXPECT_THAT_EXPECTED(
190       t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, offs_schar}),
191       llvm::HasValue(GetScalar(8, 'A', is_signed)));
192 
193   //
194   // Errors.
195   //
196 
197   // No Module.
198   EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr,
199                              t.GetDwarfUnit())
200                         .takeError(),
201                     llvm::Failed());
202 
203   // No DIE.
204   EXPECT_THAT_ERROR(
205       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x01}).takeError(),
206       llvm::Failed());
207 
208   // Unsupported.
209   EXPECT_THAT_ERROR(
210       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x1d}).takeError(),
211       llvm::Failed());
212 }
213 
214 TEST(DWARFExpression, DW_OP_stack_value) {
215   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_stack_value}), llvm::Failed());
216 }
217 
218 TEST(DWARFExpression, DW_OP_piece) {
219   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x11, 0x22, DW_OP_piece, 2,
220                                  DW_OP_const2u, 0x33, 0x44, DW_OP_piece, 2}),
221                        llvm::HasValue(GetScalar(32, 0x44332211, true)));
222   EXPECT_THAT_EXPECTED(
223       Evaluate({DW_OP_piece, 1, DW_OP_const1u, 0xff, DW_OP_piece, 1}),
224       // Note that the "00" should really be "undef", but we can't
225       // represent that yet.
226       llvm::HasValue(GetScalar(16, 0xff00, true)));
227 }
228 
229 TEST(DWARFExpression, DW_OP_implicit_value) {
230   unsigned char bytes = 4;
231 
232   EXPECT_THAT_EXPECTED(
233       Evaluate({DW_OP_implicit_value, bytes, 0x11, 0x22, 0x33, 0x44}),
234       llvm::HasValue(GetScalar(8 * bytes, 0x44332211, true)));
235 }
236 
237 TEST(DWARFExpression, DW_OP_unknown) {
238   EXPECT_THAT_EXPECTED(
239       Evaluate({0xff}),
240       llvm::FailedWithMessage(
241           "Unhandled opcode DW_OP_unknown_ff in DWARFExpression"));
242 }
243