Skip to content

Commit 0431e9a

Browse files
committed
Zero extend APInt when piece size is bigger than the bitwidth
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D78791142
1 parent 5753be4 commit 0431e9a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,12 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
19781978
// grows to the nearest host integer type.
19791979
llvm::APInt fail_value(1, 0, false);
19801980
llvm::APInt ap_int = scalar.UInt128(fail_value);
1981-
assert(ap_int.getBitWidth() >= bit_size);
1981+
// We have seen a case where we have expression like:
1982+
// DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1983+
// here we are assuming the compiler was trying to zero
1984+
// extend the value that we should append to the buffer.
1985+
if (ap_int.getBitWidth() < bit_size)
1986+
ap_int = ap_int.zext(bit_size);
19821987
llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
19831988
ap_int.getNumWords()};
19841989
curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));

lldb/unittests/Expression/DWARFExpressionTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,26 @@ TEST(DWARFExpression, DW_OP_piece) {
358358
llvm::HasValue(GetScalar(16, 0xff00, true)));
359359
}
360360

361+
TEST(DWARFExpression, DW_OP_piece_host_address) {
362+
llvm::ArrayRef<uint8_t> expr = {DW_OP_lit2, DW_OP_stack_value, DW_OP_piece,
363+
40};
364+
DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle, 4);
365+
366+
// This tests if ap_int is extended to the right width.
367+
// expect 40*8 = 320 bits size.
368+
llvm::Expected<Value> result =
369+
DWARFExpression::Evaluate(nullptr, nullptr, nullptr, extractor, nullptr,
370+
lldb::eRegisterKindLLDB, nullptr, nullptr);
371+
ASSERT_THAT_EXPECTED(result, llvm::Succeeded());
372+
ASSERT_EQ(result->GetValueType(), Value::ValueType::HostAddress);
373+
ASSERT_EQ(result->GetBuffer().GetByteSize(), 40);
374+
const uint8_t *data = result->GetBuffer().GetBytes();
375+
ASSERT_EQ(data[0], 2);
376+
for (int i = 1; i < 40; i++) {
377+
ASSERT_EQ(data[i], 0);
378+
}
379+
}
380+
361381
TEST(DWARFExpression, DW_OP_implicit_value) {
362382
unsigned char bytes = 4;
363383

0 commit comments

Comments
 (0)