[Lldb-commits] [lldb] Zero extend APInt when piece size is bigger than the bitwidth (PR #150149)
satyanarayana reddy janga via lldb-commits
lldb-commits at lists.llvm.org
Tue Jul 22 22:05:30 PDT 2025
https://github.com/satyajanga updated https://github.com/llvm/llvm-project/pull/150149
>From 766882f582904685fa5c216bbe88d1a0588c97aa Mon Sep 17 00:00:00 2001
From: satya janga <satyajanga at fb.com>
Date: Tue, 22 Jul 2025 17:57:30 -0700
Subject: [PATCH] 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
---
lldb/source/Expression/DWARFExpression.cpp | 7 ++++++-
.../Expression/DWARFExpressionTest.cpp | 20 +++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/lldb/source/Expression/DWARFExpression.cpp b/lldb/source/Expression/DWARFExpression.cpp
index 52891fcefd68b..a709574e96f98 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -1978,7 +1978,12 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
// grows to the nearest host integer type.
llvm::APInt fail_value(1, 0, false);
llvm::APInt ap_int = scalar.UInt128(fail_value);
- assert(ap_int.getBitWidth() >= bit_size);
+ // We have seen a case where we have expression like:
+ // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
+ // here we are assuming the compiler was trying to zero
+ // extend the value that we should append to the buffer.
+ if (ap_int.getBitWidth() < bit_size)
+ ap_int.zext(bit_size);
llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
ap_int.getNumWords()};
curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index fdc9bfae1876c..b2bafca5dd25b 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -358,6 +358,26 @@ TEST(DWARFExpression, DW_OP_piece) {
llvm::HasValue(GetScalar(16, 0xff00, true)));
}
+TEST(DWARFExpression, DW_OP_piece_host_address) {
+ llvm::ArrayRef<uint8_t> expr = {DW_OP_lit2, DW_OP_stack_value, DW_OP_piece,
+ 40};
+ DataExtractor extractor(expr.data(), expr.size(), lldb::eByteOrderLittle, 4);
+
+ // This tests if ap_int is extended to the right width.
+ // expect 40*8 = 320 bits size.
+ llvm::Expected<Value> result =
+ DWARFExpression::Evaluate(nullptr, nullptr, nullptr, extractor, nullptr,
+ lldb::eRegisterKindLLDB, nullptr, nullptr);
+ ASSERT_THAT_EXPECTED(result, llvm::Succeeded());
+ ASSERT_EQ(result->GetValueType(), Value::ValueType::HostAddress);
+ ASSERT_EQ(result->GetBuffer().GetByteSize(), 40);
+ const uint8_t *data = result->GetBuffer().GetBytes();
+ ASSERT_EQ(data[0], 2);
+ for (int i = 1; i < 40; i++) {
+ ASSERT_EQ(data[i], 0);
+ }
+}
+
TEST(DWARFExpression, DW_OP_implicit_value) {
unsigned char bytes = 4;
More information about the lldb-commits
mailing list