[Lldb-commits] [lldb] [lldb] Avoid returning a stale AddressOf (PR #212915)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Jul 29 18:54:43 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Igor Kudrin (igorkudrin)
<details>
<summary>Changes</summary>
The 'ValueObject::AddressOf()' method assumes that the address of a value object cannot change, so when it is calculated once, it does not need to be updated afterwards. However, this is not the case if the 'ValueObject' is a dependent object obtained by calling 'Dereference()' of another 'ValueObject'. If the latter object is changed, the dependent value object should return a new address from the 'AddressOf()' method to reflect the change.
---
Full diff: https://github.com/llvm/llvm-project/pull/212915.diff
4 Files Affected:
- (modified) lldb/source/ValueObject/ValueObject.cpp (+4-3)
- (added) lldb/test/API/python_api/value/change_ptr/Makefile (+3)
- (added) lldb/test/API/python_api/value/change_ptr/TestChangePtr.py (+22)
- (added) lldb/test/API/python_api/value/change_ptr/main.c (+7)
``````````diff
diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
index e2bfa02500f2c..b36a020d262c4 100644
--- a/lldb/source/ValueObject/ValueObject.cpp
+++ b/lldb/source/ValueObject/ValueObject.cpp
@@ -2913,9 +2913,6 @@ ValueObjectSP ValueObject::Dereference(Status &error) {
}
ValueObjectSP ValueObject::AddressOf(Status &error) {
- if (m_addr_of_valobj_sp)
- return m_addr_of_valobj_sp;
-
auto [addr, address_type] = GetAddressOf(/*scalar_is_load_address=*/false);
error.Clear();
if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
@@ -2929,6 +2926,10 @@ ValueObjectSP ValueObject::AddressOf(Status &error) {
case eAddressTypeFile:
case eAddressTypeLoad: {
+ if (m_addr_of_valobj_sp &&
+ m_addr_of_valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS) == addr)
+ return m_addr_of_valobj_sp;
+ m_addr_of_valobj_sp.reset();
CompilerType compiler_type = GetCompilerType();
if (compiler_type) {
std::string name(1, '&');
diff --git a/lldb/test/API/python_api/value/change_ptr/Makefile b/lldb/test/API/python_api/value/change_ptr/Makefile
new file mode 100644
index 0000000000000..10495940055b6
--- /dev/null
+++ b/lldb/test/API/python_api/value/change_ptr/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules
diff --git a/lldb/test/API/python_api/value/change_ptr/TestChangePtr.py b/lldb/test/API/python_api/value/change_ptr/TestChangePtr.py
new file mode 100644
index 0000000000000..378332568e097
--- /dev/null
+++ b/lldb/test/API/python_api/value/change_ptr/TestChangePtr.py
@@ -0,0 +1,22 @@
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class ChangePtrTest(TestBase):
+
+ def test(self):
+ self.build()
+
+ _, _, thread, _ = lldbutil.run_to_source_breakpoint(
+ self, "// break here", lldb.SBFileSpec("main.c")
+ )
+ frame = thread.GetFrameAtIndex(0)
+ p = frame.FindVariable("p")
+ deref = p.Dereference()
+ self.assertEqual(deref.GetValueAsUnsigned(), 5)
+ self.assertEqual(deref.AddressOf().GetValueAsUnsigned(), p.GetValueAsUnsigned())
+ thread.StepOver()
+ self.assertEqual(deref.GetValueAsUnsigned(), 7)
+ self.assertEqual(deref.AddressOf().GetValueAsUnsigned(), p.GetValueAsUnsigned())
diff --git a/lldb/test/API/python_api/value/change_ptr/main.c b/lldb/test/API/python_api/value/change_ptr/main.c
new file mode 100644
index 0000000000000..15f223ae2577a
--- /dev/null
+++ b/lldb/test/API/python_api/value/change_ptr/main.c
@@ -0,0 +1,7 @@
+int main() {
+ int a = 5;
+ int b = 7;
+ int *p = &a;
+ p = &b; // break here
+ return 0;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/212915
More information about the lldb-commits
mailing list