Skip to content

[lldb] Fix SBMemoryRegionInfoListExtensions iter to yield unique refe… #144815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zyn-li
Copy link

@zyn-li zyn-li commented Jun 19, 2025

Fixes #144439.

The __iter__ method in SBMemoryRegionInfoListExtensions reused the same SBMemoryRegionInfo object reference across all iterations, only updating its content in each loop. This caused all yielded objects to be references to the same instance.

Moved the object creation inside the loop to ensure each iteration yields a unique instance:

Testing

Updated the existing test in **TestFindInMemory.py** to:

  1. Iterator vs Index Access Comparison: Collects regions via iteration and compares their data with regions accessed by index to ensure they match
  2. Data Integrity Verification: Confirms that different regions have different base addresses (would fail before the fix since all collected regions had the same data)

The test validates both that the iterator works correctly and that each yielded object contains the correct data for its respective memory region.

@zyn-li zyn-li requested a review from JDevlieghere as a code owner June 19, 2025 00:27
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the lldb label Jun 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-lldb

Author: Zyn (zyn-li)

Changes

The __iter__ method in SBMemoryRegionInfoListExtensions reused the same SBMemoryRegionInfo object reference across all iterations, only updating its content in each loop. This caused all yielded objects to be references to the same instance.

Moved the object creation inside the loop to ensure each iteration yields a unique instance:

Testing

Updated the existing test in **TestFindInMemory.py** to:

  1. Iterator vs Index Access Comparison: Collects regions via iteration and compares their data with regions accessed by index to ensure they match
  2. Data Integrity Verification: Confirms that different regions have different base addresses (would fail before the fix since all collected regions had the same data)

The test validates both that the iterator works correctly and that each yielded object contains the correct data for its respective memory region.


Full diff: https://github.com/llvm/llvm-project/pull/144815.diff

2 Files Affected:

  • (modified) lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i (+1-1)
  • (modified) lldb/test/API/python_api/find_in_memory/TestFindInMemory.py (+21-3)
diff --git a/lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i b/lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i
index 29c0179c0ffe3..f565f45880119 100644
--- a/lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i
+++ b/lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i
@@ -9,8 +9,8 @@
       '''Iterate over all the memory regions in a lldb.SBMemoryRegionInfoList object.'''
       import lldb
       size = self.GetSize()
-      region = lldb.SBMemoryRegionInfo()
       for i in range(size):
+        region = lldb.SBMemoryRegionInfo()
         self.GetMemoryRegionAtIndex(i, region)
         yield region
     %}
diff --git a/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py b/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
index 1ef37d2ec9898..4eabbb6e46774 100644
--- a/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
+++ b/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
@@ -154,14 +154,32 @@ def test_find_in_memory_unaligned(self):
         self.assertEqual(addr, lldb.LLDB_INVALID_ADDRESS)
 
     def test_memory_info_list_iterable(self):
-        """Make sure the SBMemoryRegionInfoList is iterable"""
+        """Make sure the SBMemoryRegionInfoList is iterable and each yielded object is unique"""
         self.assertTrue(self.process, PROCESS_IS_VALID)
         self.assertState(self.process.GetState(), lldb.eStateStopped, PROCESS_STOPPED)
 
         info_list = self.process.GetMemoryRegions()
         self.assertTrue(info_list.GetSize() > 0)
+
+        collected_info = []
         try:
-            for info in info_list:
-                pass
+            for region in info_list:
+                collected_info.append(region)
         except Exception:
             self.fail("SBMemoryRegionInfoList is not iterable")
+        
+        self.assertTrue(len(collected_info) >= 2, "Need at least 2 items")
+        self.assertEqual(len(collected_info), info_list.GetSize(), "Should have collected all items")
+
+        for i in range(len(collected_info)):
+            region = lldb.SBMemoryRegionInfo()
+            info_list.GetMemoryRegionAtIndex(i, region)
+            
+            self.assertEqual(collected_info[i].GetRegionBase(), region.GetRegionBase(),
+                           f"items {i}: iterator data should match index access data")
+            self.assertEqual(collected_info[i].GetRegionEnd(), region.GetRegionEnd(),
+                           f"items {i}: iterator data should match index access data")
+            
+        if len(collected_info) >= 2:
+            self.assertNotEqual(collected_info[0].GetRegionBase(), collected_info[1].GetRegionBase(),
+                              "Different items should have different base addresses")

@zyn-li
Copy link
Author

zyn-li commented Jun 19, 2025

Could you please review this. @dmpots @Jlalond @aperez @royitaqi

Copy link
Contributor

@Jlalond Jlalond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits, I think we should expand the scope of the fix and add an equality operator for the SBMemoryRegionInfo. Cleans up your test and will save a lot of developer time because this is a very popular SB API

except Exception:
self.fail("SBMemoryRegionInfoList is not iterable")

self.assertTrue(len(collected_info) >= 2, "Need at least 2 items")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 2? We know the number of emmeory regions from info_list, compare to the underlying collection

region = lldb.SBMemoryRegionInfo()
info_list.GetMemoryRegionAtIndex(i, region)

self.assertEqual(collected_info[i].GetRegionBase(), region.GetRegionBase(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should just add an equality method to SBMemoryRegionInfo and call that.

Copy link

⚠️ Python code formatter, darker found issues in your code. ⚠️

You can test this locally with the following command:
darker --check --diff -r HEAD~1...HEAD lldb/test/API/python_api/find_in_memory/TestFindInMemory.py
View the diff from darker here.
--- TestFindInMemory.py	2025-06-18 23:49:12.000000 +0000
+++ TestFindInMemory.py	2025-06-19 00:51:36.148357 +0000
@@ -165,21 +165,32 @@
         try:
             for region in info_list:
                 collected_info.append(region)
         except Exception:
             self.fail("SBMemoryRegionInfoList is not iterable")
-        
+
         self.assertTrue(len(collected_info) >= 2, "Need at least 2 items")
-        self.assertEqual(len(collected_info), info_list.GetSize(), "Should have collected all items")
+        self.assertEqual(
+            len(collected_info), info_list.GetSize(), "Should have collected all items"
+        )
 
         for i in range(len(collected_info)):
             region = lldb.SBMemoryRegionInfo()
             info_list.GetMemoryRegionAtIndex(i, region)
-            
-            self.assertEqual(collected_info[i].GetRegionBase(), region.GetRegionBase(),
-                           f"items {i}: iterator data should match index access data")
-            self.assertEqual(collected_info[i].GetRegionEnd(), region.GetRegionEnd(),
-                           f"items {i}: iterator data should match index access data")
-            
+
+            self.assertEqual(
+                collected_info[i].GetRegionBase(),
+                region.GetRegionBase(),
+                f"items {i}: iterator data should match index access data",
+            )
+            self.assertEqual(
+                collected_info[i].GetRegionEnd(),
+                region.GetRegionEnd(),
+                f"items {i}: iterator data should match index access data",
+            )
+
         if len(collected_info) >= 2:
-            self.assertNotEqual(collected_info[0].GetRegionBase(), collected_info[1].GetRegionBase(),
-                              "Different items should have different base addresses")
+            self.assertNotEqual(
+                collected_info[0].GetRegionBase(),
+                collected_info[1].GetRegionBase(),
+                "Different items should have different base addresses",
+            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LLDB] SBMemoryRegionInfoListExtensions re-uses reference and then mutates it.
3 participants