Skip to content

Commit f369157

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 f369157

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,14 +1974,13 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
19741974
piece_byte_size,
19751975
(uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
19761976
}
1977-
// Create curr_piece with bit_size. By default Scalar
1978-
// grows to the nearest host integer type.
1979-
llvm::APInt fail_value(1, 0, false);
1980-
llvm::APInt ap_int = scalar.UInt128(fail_value);
1981-
assert(ap_int.getBitWidth() >= bit_size);
1982-
llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
1983-
ap_int.getNumWords()};
1984-
curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
1977+
1978+
// We have seen a case where we have expression like:
1979+
// DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1980+
// here we are assuming the compiler was trying to zero
1981+
// extend the value that we should append to the buffer.
1982+
scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1983+
curr_piece.GetScalar() = scalar;
19851984
} break;
19861985
}
19871986

lldb/unittests/Expression/DWARFExpressionTest.cpp

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

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

0 commit comments

Comments
 (0)