https://github.com/MrEven132 updated 
https://github.com/llvm/llvm-project/pull/216291

>From 11c8f853236c342514635295a9cfe2b11e87e19c Mon Sep 17 00:00:00 2001
From: MrEven132 <[email protected]>
Date: Fri, 14 Aug 2026 18:09:32 +0800
Subject: [PATCH 1/3] fix lldb breg adress width

---
 lldb/source/Expression/DWARFExpression.cpp    |  6 ++--
 .../Expression/DWARFExpressionTest.cpp        | 29 +++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/lldb/source/Expression/DWARFExpression.cpp 
b/lldb/source/Expression/DWARFExpression.cpp
index 2dbf61a14eac4..f4c4f519b09c7 100644
--- a/lldb/source/Expression/DWARFExpression.cpp
+++ b/lldb/source/Expression/DWARFExpression.cpp
@@ -1901,7 +1901,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
         return err;
 
       int64_t breg_offset = op->getRawOperand(0);
-      tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
+      tmp.GetScalar() = to_generic(tmp.GetScalar().ULongLong());
+      tmp.GetScalar() += to_generic(breg_offset);
       tmp.ClearContext();
       stack.push_back(tmp);
       stack.back().SetValueType(Value::ValueType::LoadAddress);
@@ -1913,7 +1914,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
         return err;
 
       int64_t breg_offset = op->getRawOperand(1);
-      tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
+      tmp.GetScalar() = to_generic(tmp.GetScalar().ULongLong());
+      tmp.GetScalar() += to_generic(breg_offset);
       tmp.ClearContext();
       stack.push_back(tmp);
       stack.back().SetValueType(Value::ValueType::LoadAddress);
diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp 
b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index e8bca7208c8d5..2d5890570d8eb 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -1344,6 +1344,35 @@ TEST_F(DWARFExpressionMockProcessTest, DW_OP_bregx) {
                        ExpectLoadAddress(0x2010));
 }
 
+TEST_F(DWARFExpressionMockProcessTest, DW_OP_breg_address_size) {
+  TestContext ctx;
+  ASSERT_TRUE(
+      CreateTestContext(&ctx, "i386-pc-linux", RegisterValue(uint32_t{0x2a})));
+  ExecutionContext exe_ctx(ctx.process_sp);
+
+  // Address arithmetic wraps at the target address size. In particular,
+  // 0xffffffff + 1 is zero on this 32-bit target.
+  EXPECT_THAT_EXPECTED(
+      Evaluate({DW_OP_breg0, 0x7f, DW_OP_const4u, 0xff, 0xff, 0xff, 0xff,
+                DW_OP_plus, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value},
+               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get()),
+      ExpectScalar(32, 0x29, false));
+}
+
+TEST_F(DWARFExpressionMockProcessTest, DW_OP_bregx_address_size) {
+  TestContext ctx;
+  ASSERT_TRUE(CreateTestContext(&ctx, "i386-pc-linux",
+                                RegisterValue(uint64_t{0x10000002a})));
+  ExecutionContext exe_ctx(ctx.process_sp);
+
+  // The register backend may expose a value wider than the target address.
+  EXPECT_THAT_EXPECTED(
+      Evaluate({DW_OP_bregx, 0x40, 0x7f, DW_OP_const4u, 0xff, 0xff, 0xff, 0xff,
+                DW_OP_plus, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value},
+               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get()),
+      ExpectScalar(32, 0x29, false));
+}
+
 TEST_F(DWARFExpressionMockProcessTest, DW_OP_deref) {
   EXPECT_THAT_EXPECTED(Evaluate({DW_OP_lit0, DW_OP_deref}), llvm::Failed());
 

>From 5d728630caceaddb755a92d8aacef089017596b9 Mon Sep 17 00:00:00 2001
From: MrEven132 <[email protected]>
Date: Fri, 14 Aug 2026 20:42:34 +0800
Subject: [PATCH 2/3] check the result's bit width

---
 lldb/unittests/Expression/DWARFExpressionTest.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp 
b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index 2d5890570d8eb..ab8d9431dabf5 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -1352,11 +1352,12 @@ TEST_F(DWARFExpressionMockProcessTest, 
DW_OP_breg_address_size) {
 
   // Address arithmetic wraps at the target address size. In particular,
   // 0xffffffff + 1 is zero on this 32-bit target.
-  EXPECT_THAT_EXPECTED(
+  auto result =
       Evaluate({DW_OP_breg0, 0x7f, DW_OP_const4u, 0xff, 0xff, 0xff, 0xff,
                 DW_OP_plus, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value},
-               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get()),
-      ExpectScalar(32, 0x29, false));
+               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get());
+  ASSERT_THAT_EXPECTED(result, ExpectScalar(32, 0x29, false));
+  EXPECT_EQ(result->GetScalar().GetAPSInt().getBitWidth(), 32u);
 }
 
 TEST_F(DWARFExpressionMockProcessTest, DW_OP_bregx_address_size) {

>From a61f9c24f7aff64ec622fa1d64d40eb4b3c297d0 Mon Sep 17 00:00:00 2001
From: MrEven132 <[email protected]>
Date: Fri, 14 Aug 2026 21:12:05 +0800
Subject: [PATCH 3/3] add second check for result's bit width

---
 lldb/unittests/Expression/DWARFExpressionTest.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lldb/unittests/Expression/DWARFExpressionTest.cpp 
b/lldb/unittests/Expression/DWARFExpressionTest.cpp
index ab8d9431dabf5..c2752e16672f7 100644
--- a/lldb/unittests/Expression/DWARFExpressionTest.cpp
+++ b/lldb/unittests/Expression/DWARFExpressionTest.cpp
@@ -1367,11 +1367,12 @@ TEST_F(DWARFExpressionMockProcessTest, 
DW_OP_bregx_address_size) {
   ExecutionContext exe_ctx(ctx.process_sp);
 
   // The register backend may expose a value wider than the target address.
-  EXPECT_THAT_EXPECTED(
+  auto result =
       Evaluate({DW_OP_bregx, 0x40, 0x7f, DW_OP_const4u, 0xff, 0xff, 0xff, 0xff,
                 DW_OP_plus, DW_OP_lit1, DW_OP_plus, DW_OP_stack_value},
-               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get()),
-      ExpectScalar(32, 0x29, false));
+               {}, {}, &exe_ctx, ctx.reg_ctx_sp.get());
+  ASSERT_THAT_EXPECTED(result, ExpectScalar(32, 0x29, false));
+  EXPECT_EQ(result->GetScalar().GetAPSInt().getBitWidth(), 32u);
 }
 
 TEST_F(DWARFExpressionMockProcessTest, DW_OP_deref) {

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to