Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lldb/source/Expression/DWARFExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,11 @@ static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
return addr_data.GetAddress(&addr_data_offset);
}

static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack,
ExecutionContext *exe_ctx,
lldb::ModuleSP module_sp,
Process *process, Target *target,
uint8_t size) {
static llvm::Error Evaluate_DW_OP_deref_size(
DWARFExpression::Stack &stack, ExecutionContext *exe_ctx,
lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size,
size_t size_addr_bytes,
LocationDescriptionKind &dwarf4_location_description_kind) {
if (stack.empty())
return llvm::createStringError(
"expression stack empty for DW_OP_deref_size");
Expand All @@ -892,6 +892,25 @@ static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack,
return llvm::createStringError(
"Invalid address size for DW_OP_deref_size: %d\n", size);

// Deref a register or implicit location and truncate the value to `size`
// bytes. See the corresponding comment in DW_OP_deref for more details on
// why we deref these locations this way.
if (dwarf4_location_description_kind == Register ||
dwarf4_location_description_kind == Implicit) {
// Reset context to default values.
dwarf4_location_description_kind = Memory;
stack.back().ClearContext();

// Truncate the value on top of the stack to *size* bytes then
// extend to the size of an address (e.g. generic type).
Scalar scalar = stack.back().GetScalar();
scalar.TruncOrExtendTo(size * 8, /*sign=*/false);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be interesting to also add the DW_OP_LLVM_offset operator here just to be able to handle bigger registers than the size of the global address space.

For the DW_OP_LLVM_bit_offset variant, we might want to add that one later considering that DW_OP_bit_piece was also never implemented.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets wait on DW_OP_LLVM_offset for now until we need it.

scalar.TruncOrExtendTo(size_addr_bytes * 8,
/*sign=*/false);
stack.back().GetScalar() = scalar;
return llvm::Error::success();
}

Value::ValueType value_type = stack.back().GetValueType();
switch (value_type) {
case Value::ValueType::HostAddress: {
Expand Down Expand Up @@ -1142,8 +1161,9 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
// target machine.
case DW_OP_deref: {
size_t size = opcodes.GetAddressByteSize();
if (llvm::Error err = Evaluate_DW_OP_deref_size(stack, exe_ctx, module_sp,
process, target, size))
if (llvm::Error err = Evaluate_DW_OP_deref_size(
stack, exe_ctx, module_sp, process, target, size, size,
dwarf4_location_description_kind))
return err;
} break;

Expand All @@ -1161,8 +1181,9 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
// expression stack.
case DW_OP_deref_size: {
size_t size = opcodes.GetU8(&offset);
if (llvm::Error err = Evaluate_DW_OP_deref_size(stack, exe_ctx, module_sp,
process, target, size))
if (llvm::Error err = Evaluate_DW_OP_deref_size(
stack, exe_ctx, module_sp, process, target, size,
opcodes.GetAddressByteSize(), dwarf4_location_description_kind))
return err;
} break;

Expand Down
104 changes: 104 additions & 0 deletions lldb/unittests/Expression/DWARFExpressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,3 +1217,107 @@ TEST_F(DWARFExpressionMockProcessTestWithAArch, DW_op_deref_no_ptr_fixing) {
llvm::Expected<Value> result_deref = evaluate_expr(expr_deref);
EXPECT_THAT_EXPECTED(result_deref, ExpectLoadAddress(expected_value));
}

TEST_F(DWARFExpressionMockProcessTest, deref_register) {
TestContext test_ctx;
constexpr uint32_t reg_r0 = 0x504;
MockMemory::Map memory = {
{{0x004, 4}, {0x1, 0x2, 0x3, 0x4}},
{{0x504, 4}, {0xa, 0xb, 0xc, 0xd}},
{{0x505, 4}, {0x5, 0x6, 0x7, 0x8}},
};
ASSERT_TRUE(CreateTestContext(&test_ctx, "i386-pc-linux",
RegisterValue(reg_r0), memory, memory));

ExecutionContext exe_ctx(test_ctx.process_sp);
MockDwarfDelegate delegate = MockDwarfDelegate::Dwarf5();
auto Eval = [&](llvm::ArrayRef<uint8_t> expr_data) {
ExecutionContext exe_ctx(test_ctx.process_sp);
return Evaluate(expr_data, {}, &delegate, &exe_ctx,
test_ctx.reg_ctx_sp.get());
};

// Reads from the register r0.
// Sets the context to RegisterInfo so we know this is a register location.
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0}),
ExpectScalar(reg_r0, Value::ContextType::RegisterInfo));

// Reads from the location(register r0).
// Clears the context so we know this is a value not a location.
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref}),
ExpectLoadAddress(reg_r0, Value::ContextType::Invalid));

// Reads from the location(register r0) and adds the value to the host buffer.
// The evaluator should implicitly convert it to a memory location when
// added to a composite value and should add the contents of memory[r0]
// to the host buffer.
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref, DW_OP_piece, 4}),
ExpectHostAddress({0xa, 0xb, 0xc, 0xd}));

// Reads from the location(register r0) and truncates the value to one byte.
// Clears the context so we know this is a value not a location.
EXPECT_THAT_EXPECTED(
Eval({DW_OP_reg0, DW_OP_deref_size, 1}),
ExpectLoadAddress(reg_r0 & 0xff, Value::ContextType::Invalid));

// Reads from the location(register r0) and truncates to one byte then adds
// the value to the host buffer. The evaluator should implicitly convert it to
// a memory location when added to a composite value and should add the
// contents of memory[r0 & 0xff] to the host buffer.
EXPECT_THAT_EXPECTED(Eval({DW_OP_reg0, DW_OP_deref_size, 1, DW_OP_piece, 4}),
ExpectHostAddress({0x1, 0x2, 0x3, 0x4}));

// Reads from the register r0 + 1.
EXPECT_THAT_EXPECTED(
Eval({DW_OP_breg0, 1}),
ExpectLoadAddress(reg_r0 + 1, Value::ContextType::Invalid));

// Reads from address r0 + 1, which contains the bytes [5,6,7,8].
EXPECT_THAT_EXPECTED(
Eval({DW_OP_breg0, 1, DW_OP_deref}),
ExpectLoadAddress(0x08070605, Value::ContextType::Invalid));
}

TEST_F(DWARFExpressionMockProcessTest, deref_implicit_value) {
TestContext test_ctx;
MockMemory::Map memory = {
{{0x4, 1}, {0x1}},
{{0x4, 4}, {0x1, 0x2, 0x3, 0x4}},
};
ASSERT_TRUE(CreateTestContext(&test_ctx, "i386-pc-linux", {}, memory));

ExecutionContext exe_ctx(test_ctx.process_sp);
MockDwarfDelegate delegate = MockDwarfDelegate::Dwarf5();
auto Eval = [&](llvm::ArrayRef<uint8_t> expr_data) {
ExecutionContext exe_ctx(test_ctx.process_sp);
return Evaluate(expr_data, {}, &delegate, &exe_ctx,
test_ctx.reg_ctx_sp.get());
};

// Creates an implicit location with a value of 4.
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_stack_value}),
ExpectScalar(0x4));

// Creates an implicit location with a value of 4. The deref reads the value
// out of the location and implicitly converts it to a load address.
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_stack_value, DW_OP_deref}),
ExpectLoadAddress(0x4));

// Creates an implicit location with a value of 0x504 (uleb128(0x504) =
// 0xa84). The deref reads the low byte out of the location and implicitly
// converts it to a load address.
EXPECT_THAT_EXPECTED(
Eval({DW_OP_constu, 0x84, 0xa, DW_OP_stack_value, DW_OP_deref_size, 1}),
ExpectLoadAddress(0x4));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right or I am not understanding how the uleb128 output is represented here.

Shouldn't dereferencing of the stack value be the value itself as a load address (aka value of 0x84)? Also what does 0xa byte represents here?

I would expect for the expression to look something more like this:
DW_OP_constu 0x84 DW_OP_stack_value DW_OP_deref_size 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are passing raw bytes in here so the ULEB128 value for the DW_OP_constu bytes are [0x84, 0x0a]

Copy link
Contributor Author

@dmpots dmpots Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZaricZoran As Greg mentioned this is the ULEB encoded value and ULEB(0x504) == [0x84, 0x0a] (in little endian byte order). So when we take the lower byte the result should be 0x4.


// The tests below are similar to the ones above, but there is no implicit
// location created by a stack_value operation. They are provided here as a
// reference to contrast with the above tests.
EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4}), ExpectLoadAddress(0x4));

EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_deref}),
ExpectLoadAddress(0x04030201));

EXPECT_THAT_EXPECTED(Eval({DW_OP_lit4, DW_OP_deref_size, 1}),
ExpectLoadAddress(0x01));
}
Loading