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