Skip to content

Commit fae64ad

Browse files
authored
[lldb] Handle deref of register and implicit locations (#169419)
This commit modifies the dwarf expression evaluator in how we handle the deref operation for register and implicit locations on the stack. For a typical memory location a deref operation will read the value from memory. For register and implicit locations the deref operation will read the value from the register or its implicit location. In lldb we eagerly read register and implicit values and push them on the stack so the deref operation for these becomes a "no-op" that leaves the value on the stack and updates the tracked location kind. The motivation for this change is to handle `DW_OP_deref*` operations on location descriptions as described by the heterogenious debugging [extensions](https://rocm.docs.amd.com/projects/llvm-project/en/latest/LLVM/llvm/html/AMDGPUDwarfExtensionsForHeterogeneousDebugging.html#a-2-5-4-4-4-register-location-description-operations). Specifically, for register locations it states > These operations obtain a register location. To fetch the contents of > a register, it is necessary to use DW_OP_regval_type, use one of the > DW_OP_breg* register-based addressing operations, or use DW_OP_deref* on > a register location description. My understanding is that this is the intended behavior from dwarf5 as well and is not a change in behavior.
1 parent 3f2e3e6 commit fae64ad

File tree

2 files changed

+134
-9
lines changed

2 files changed

+134
-9
lines changed

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -879,11 +879,11 @@ static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
879879
return addr_data.GetAddress(&addr_data_offset);
880880
}
881881

882-
static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack,
883-
ExecutionContext *exe_ctx,
884-
lldb::ModuleSP module_sp,
885-
Process *process, Target *target,
886-
uint8_t size) {
882+
static llvm::Error Evaluate_DW_OP_deref_size(
883+
DWARFExpression::Stack &stack, ExecutionContext *exe_ctx,
884+
lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size,
885+
size_t size_addr_bytes,
886+
LocationDescriptionKind &dwarf4_location_description_kind) {
887887
if (stack.empty())
888888
return llvm::createStringError(
889889
"expression stack empty for DW_OP_deref_size");
@@ -892,6 +892,25 @@ static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack,
892892
return llvm::createStringError(
893893
"Invalid address size for DW_OP_deref_size: %d\n", size);
894894

895+
// Deref a register or implicit location and truncate the value to `size`
896+
// bytes. See the corresponding comment in DW_OP_deref for more details on
897+
// why we deref these locations this way.
898+
if (dwarf4_location_description_kind == Register ||
899+
dwarf4_location_description_kind == Implicit) {
900+
// Reset context to default values.
901+
dwarf4_location_description_kind = Memory;
902+
stack.back().ClearContext();
903+
904+
// Truncate the value on top of the stack to *size* bytes then
905+
// extend to the size of an address (e.g. generic type).
906+
Scalar scalar = stack.back().GetScalar();
907+
scalar.TruncOrExtendTo(size * 8, /*sign=*/false);
908+
scalar.TruncOrExtendTo(size_addr_bytes * 8,
909+
/*sign=*/false);
910+
stack.back().GetScalar() = scalar;
911+
return llvm::Error::success();
912+
}
913+
895914
Value::ValueType value_type = stack.back().GetValueType();
896915
switch (value_type) {
897916
case Value::ValueType::HostAddress: {
@@ -1142,8 +1161,9 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
11421161
// target machine.
11431162
case DW_OP_deref: {
11441163
size_t size = opcodes.GetAddressByteSize();
1145-
if (llvm::Error err = Evaluate_DW_OP_deref_size(stack, exe_ctx, module_sp,
1146-
process, target, size))
1164+
if (llvm::Error err = Evaluate_DW_OP_deref_size(
1165+
stack, exe_ctx, module_sp, process, target, size, size,
1166+
dwarf4_location_description_kind))
11471167
return err;
11481168
} break;
11491169

@@ -1161,8 +1181,9 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
11611181
// expression stack.
11621182
case DW_OP_deref_size: {
11631183
size_t size = opcodes.GetU8(&offset);
1164-
if (llvm::Error err = Evaluate_DW_OP_deref_size(stack, exe_ctx, module_sp,
1165-
process, target, size))
1184+
if (llvm::Error err = Evaluate_DW_OP_deref_size(
1185+
stack, exe_ctx, module_sp, process, target, size,
1186+
opcodes.GetAddressByteSize(), dwarf4_location_description_kind))
11661187
return err;
11671188
} break;
11681189

lldb/unittests/Expression/DWARFExpressionTest.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,3 +1217,107 @@ TEST_F(DWARFExpressionMockProcessTestWithAArch, DW_op_deref_no_ptr_fixing) {
12171217
llvm::Expected<Value> result_deref = evaluate_expr(expr_deref);
12181218
EXPECT_THAT_EXPECTED(result_deref, ExpectLoadAddress(expected_value));
12191219
}
1220+
1221+
TEST_F(DWARFExpressionMockProcessTest, deref_register) {
1222+
TestContext test_ctx;
1223+
constexpr uint32_t reg_r0 = 0x504;
1224+
MockMemory::Map memory = {
1225+
{{0x004, 4}, {0x1, 0x2, 0x3, 0x4}},
1226+
{{0x504, 4}, {0xa, 0xb, 0xc, 0xd}},
1227+
{{0x505, 4}, {0x5, 0x6, 0x7, 0x8}},
1228+
};
1229+
ASSERT_TRUE(CreateTestContext(&test_ctx, "i386-pc-linux",
1230+
RegisterValue(reg_r0), memory, memory));
1231+
1232+
ExecutionContext exe_ctx(test_ctx.process_sp);
1233+
MockDwarfDelegate delegate = MockDwarfDelegate::Dwarf5();
1234+
auto Eval = [&](llvm::ArrayRef<uint8_t> expr_data) {
1235+
ExecutionContext exe_ctx(test_ctx.process_sp);
1236+
return Evaluate(expr_data, {}, &delegate, &exe_ctx,
1237+
test_ctx.reg_ctx_sp.get());
1238+
};
1239+
1240+
// Reads from the register r0.
1241+
// Sets the context to RegisterInfo so we know this is a register location.
1242+
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0}),
1243+
ExpectScalar(reg_r0, Value::ContextType::RegisterInfo));
1244+
1245+
// Reads from the location(register r0).
1246+
// Clears the context so we know this is a value not a location.
1247+
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref}),
1248+
ExpectLoadAddress(reg_r0, Value::ContextType::Invalid));
1249+
1250+
// Reads from the location(register r0) and adds the value to the host buffer.
1251+
// The evaluator should implicitly convert it to a memory location when
1252+
// added to a composite value and should add the contents of memory[r0]
1253+
// to the host buffer.
1254+
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref, DW_OP_piece, 4}),
1255+
ExpectHostAddress({0xa, 0xb, 0xc, 0xd}));
1256+
1257+
// Reads from the location(register r0) and truncates the value to one byte.
1258+
// Clears the context so we know this is a value not a location.
1259+
EXPECT_THAT_EXPECTED(
1260+
Eval({DW_OP_reg0, DW_OP_deref_size, 1}),
1261+
ExpectLoadAddress(reg_r0 & 0xff, Value::ContextType::Invalid));
1262+
1263+
// Reads from the location(register r0) and truncates to one byte then adds
1264+
// the value to the host buffer. The evaluator should implicitly convert it to
1265+
// a memory location when added to a composite value and should add the
1266+
// contents of memory[r0 & 0xff] to the host buffer.
1267+
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref_size, 1, DW_OP_piece, 4}),
1268+
ExpectHostAddress({0x1, 0x2, 0x3, 0x4}));
1269+
1270+
// Reads from the register r0 + 1.
1271+
EXPECT_THAT_EXPECTED(
1272+
Eval({DW_OP_breg0, 1}),
1273+
ExpectLoadAddress(reg_r0 + 1, Value::ContextType::Invalid));
1274+
1275+
// Reads from address r0 + 1, which contains the bytes [5,6,7,8].
1276+
EXPECT_THAT_EXPECTED(
1277+
Eval({DW_OP_breg0, 1, DW_OP_deref}),
1278+
ExpectLoadAddress(0x08070605, Value::ContextType::Invalid));
1279+
}
1280+
1281+
TEST_F(DWARFExpressionMockProcessTest, deref_implicit_value) {
1282+
TestContext test_ctx;
1283+
MockMemory::Map memory = {
1284+
{{0x4, 1}, {0x1}},
1285+
{{0x4, 4}, {0x1, 0x2, 0x3, 0x4}},
1286+
};
1287+
ASSERT_TRUE(CreateTestContext(&test_ctx, "i386-pc-linux", {}, memory));
1288+
1289+
ExecutionContext exe_ctx(test_ctx.process_sp);
1290+
MockDwarfDelegate delegate = MockDwarfDelegate::Dwarf5();
1291+
auto Eval = [&](llvm::ArrayRef<uint8_t> expr_data) {
1292+
ExecutionContext exe_ctx(test_ctx.process_sp);
1293+
return Evaluate(expr_data, {}, &delegate, &exe_ctx,
1294+
test_ctx.reg_ctx_sp.get());
1295+
};
1296+
1297+
// Creates an implicit location with a value of 4.
1298+
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_stack_value}),
1299+
ExpectScalar(0x4));
1300+
1301+
// Creates an implicit location with a value of 4. The deref reads the value
1302+
// out of the location and implicitly converts it to a load address.
1303+
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_stack_value, DW_OP_deref}),
1304+
ExpectLoadAddress(0x4));
1305+
1306+
// Creates an implicit location with a value of 0x504 (uleb128(0x504) =
1307+
// 0xa84). The deref reads the low byte out of the location and implicitly
1308+
// converts it to a load address.
1309+
EXPECT_THAT_EXPECTED(
1310+
Eval({DW_OP_constu, 0x84, 0xa, DW_OP_stack_value, DW_OP_deref_size, 1}),
1311+
ExpectLoadAddress(0x4));
1312+
1313+
// The tests below are similar to the ones above, but there is no implicit
1314+
// location created by a stack_value operation. They are provided here as a
1315+
// reference to contrast with the above tests.
1316+
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4}), ExpectLoadAddress(0x4));
1317+
1318+
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_deref}),
1319+
ExpectLoadAddress(0x04030201));
1320+
1321+
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_deref_size, 1}),
1322+
ExpectLoadAddress(0x01));
1323+
}

0 commit comments

Comments
 (0)