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.get());
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 =
81       "debug_abbrev:\n"
82       "  - Table:\n"
83       "      - Code:            0x00000001\n"
84       "        Tag:             DW_TAG_compile_unit\n"
85       "        Children:        DW_CHILDREN_yes\n"
86       "        Attributes:\n"
87       "          - Attribute:       DW_AT_language\n"
88       "            Form:            DW_FORM_data2\n"
89       "      - Code:            0x00000002\n"
90       "        Tag:             DW_TAG_base_type\n"
91       "        Children:        DW_CHILDREN_no\n"
92       "        Attributes:\n"
93       "          - Attribute:       DW_AT_encoding\n"
94       "            Form:            DW_FORM_data1\n"
95       "          - Attribute:       DW_AT_byte_size\n"
96       "            Form:            DW_FORM_data1\n"
97       "debug_info:\n"
98       "  - Version:         4\n"
99       "    AddrSize:        8\n"
100       "    Entries:\n"
101       "      - AbbrCode:        0x00000001\n"
102       "        Values:\n"
103       "          - Value:           0x000000000000000C\n"
104       // 0x0000000e:
105       "      - AbbrCode:        0x00000002\n"
106       "        Values:\n"
107       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
108       "          - Value:           0x0000000000000004\n"
109       // 0x00000011:
110       "      - AbbrCode:        0x00000002\n"
111       "        Values:\n"
112       "          - Value:           0x0000000000000007\n" // DW_ATE_unsigned
113       "          - Value:           0x0000000000000008\n"
114       // 0x00000014:
115       "      - AbbrCode:        0x00000002\n"
116       "        Values:\n"
117       "          - Value:           0x0000000000000005\n" // DW_ATE_signed
118       "          - Value:           0x0000000000000008\n"
119       // 0x00000017:
120       "      - AbbrCode:        0x00000002\n"
121       "        Values:\n"
122       "          - Value:           0x0000000000000008\n" // DW_ATE_unsigned_char
123       "          - Value:           0x0000000000000001\n"
124       // 0x0000001a:
125       "      - AbbrCode:        0x00000002\n"
126       "        Values:\n"
127       "          - Value:           0x0000000000000006\n" // DW_ATE_signed_char
128       "          - Value:           0x0000000000000001\n"
129       // 0x0000001d:
130       "      - AbbrCode:        0x00000002\n"
131       "        Values:\n"
132       "          - Value:           0x000000000000000b\n" // DW_ATE_numeric_string
133       "          - Value:           0x0000000000000001\n"
134       "      - AbbrCode:        0x00000000\n";
135   uint8_t offs_uint32_t = 0x0000000e;
136   uint8_t offs_uint64_t = 0x00000011;
137   uint8_t offs_sint64_t = 0x00000014;
138   uint8_t offs_uchar = 0x00000017;
139   uint8_t offs_schar = 0x0000001a;
140 
141   DWARFExpressionTester t(yamldata, "i386-unknown-linux");
142   ASSERT_TRUE((bool)t.GetDwarfUnit());
143 
144   // Constant is given as little-endian.
145   bool is_signed = true;
146   bool not_signed = false;
147 
148   //
149   // Positive tests.
150   //
151 
152   // Truncate to default unspecified (pointer-sized) type.
153   EXPECT_THAT_EXPECTED(
154       t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, //
155               DW_OP_convert, 0x00}),
156       llvm::HasValue(GetScalar(32, 0x44332211, not_signed)));
157   // Truncate to 32 bits.
158   EXPECT_THAT_EXPECTED(t.Eval({DW_OP_const8u, //
159                                0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,//
160                                DW_OP_convert, offs_uint32_t}),
161                        llvm::HasValue(GetScalar(32, 0x44332211, not_signed)));
162 
163   // Leave as is.
164   EXPECT_THAT_EXPECTED(
165       t.Eval({DW_OP_const8u, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, //
166               DW_OP_convert, offs_uint64_t}),
167       llvm::HasValue(GetScalar(64, 0x8877665544332211, not_signed)));
168 
169   // Sign-extend to 64 bits.
170   EXPECT_THAT_EXPECTED(
171       t.Eval({DW_OP_const4s, 0xcc, 0xdd, 0xee, 0xff, //
172               DW_OP_convert, offs_sint64_t}),
173       llvm::HasValue(GetScalar(64, 0xffffffffffeeddcc, is_signed)));
174 
175   // Truncate to 8 bits.
176   EXPECT_THAT_EXPECTED(
177       t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, offs_uchar}),
178       llvm::HasValue(GetScalar(8, 'A', not_signed)));
179 
180   // Also truncate to 8 bits.
181   EXPECT_THAT_EXPECTED(
182       t.Eval({DW_OP_const4s, 'A', 'B', 'C', 'D', DW_OP_convert, offs_schar}),
183       llvm::HasValue(GetScalar(8, 'A', is_signed)));
184 
185   //
186   // Errors.
187   //
188 
189   // No Module.
190   EXPECT_THAT_ERROR(Evaluate({DW_OP_const1s, 'X', DW_OP_convert, 0x00}, nullptr,
191                              t.GetDwarfUnit().get())
192                         .takeError(),
193                     llvm::Failed());
194 
195   // No DIE.
196   EXPECT_THAT_ERROR(
197       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x01}).takeError(),
198       llvm::Failed());
199 
200   // Unsupported.
201   EXPECT_THAT_ERROR(
202       t.Eval({DW_OP_const1s, 'X', DW_OP_convert, 0x1d}).takeError(),
203       llvm::Failed());
204 }
205 
206 TEST(DWARFExpression, DW_OP_stack_value) {
207   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_stack_value}), llvm::Failed());
208 }
209 
210 TEST(DWARFExpression, DW_OP_piece) {
211   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_const2u, 0x11, 0x22, DW_OP_piece, 2,
212                                  DW_OP_const2u, 0x33, 0x44, DW_OP_piece, 2}),
213                        llvm::HasValue(GetScalar(32, 0x44332211, true)));
214   EXPECT_THAT_EXPECTED(
215       Evaluate({DW_OP_piece, 1, DW_OP_const1u, 0xff, DW_OP_piece, 1}),
216       // Note that the "00" should really be "undef", but we can't
217       // represent that yet.
218       llvm::HasValue(GetScalar(16, 0xff00, true)));
219 }
220 
221 TEST(DWARFExpression, DW_OP_implicit_value) {
222   unsigned char bytes = 4;
223 
224   EXPECT_THAT_EXPECTED(
225       Evaluate({DW_OP_implicit_value, bytes, 0x11, 0x22, 0x33, 0x44}),
226       llvm::HasValue(GetScalar(8 * bytes, 0x44332211, true)));
227 }
228 
229 TEST(DWARFExpression, DW_OP_unknown) {
230   EXPECT_THAT_EXPECTED(
231       Evaluate({0xff}),
232       llvm::FailedWithMessage(
233           "Unhandled opcode DW_OP_unknown_ff in DWARFExpression"));
234 }
235