Skip to content

[LLDB] SBMemoryRegionInfoListExtensions re-uses reference and then mutates it. #144439

@Jlalond

Description

@Jlalond
Contributor

In SBMemoryRegionInfoListExtensions, the __iter__ function always yields the same SBMemoryRegionInfo reference, but each loop the content of the info will be updated.

Using the example that I wrote in the docs:

        readable_regions = []
        for region in exe_ctx.GetProcess().GetMemoryRegions():
            if region.IsReadable():
                readable_regions.append(region)

We end up with a list all to the same region

Image

This is because in the iter:

    def __iter__(self):
      '''Iterate over all the memory regions in a lldb.SBMemoryRegionInfoList object.'''
      import lldb
      size = self.GetSize()
      region = lldb.SBMemoryRegionInfo()
      for i in range(size):
        self.GetMemoryRegionAtIndex(i, region)
        yield region

Region is a reference and if you assign it to anything, that new variable will be mutated in the next loop.

I filed this as an issue to communicate the bug because I assume a user has encountered this without realizing (because it's not very apparent).

Activity

Jlalond

Jlalond commented on Jun 16, 2025

@Jlalond
ContributorAuthor

@satyajanga could you fix this?

llvmbot

llvmbot commented on Jun 16, 2025

@llvmbot
Member

@llvm/issue-subscribers-lldb

Author: Jacob Lalonde (Jlalond)

In [SBMemoryRegionInfoListExtensions](https://github.com/llvm/llvm-project/blob/main/lldb/bindings/interface/SBMemoryRegionInfoListExtensions.i#L8), the `__iter__` function always yields the same `SBMemoryRegionInfo` reference, but each loop the content of the info will be updated.

Using the example that I wrote in the docs:

        readable_regions = []
        for region in exe_ctx.GetProcess().GetMemoryRegions():
            if region.IsReadable():
                readable_regions.append(region)

We end up with a list all to the same region

Image

This is because in the iter:

    def __iter__(self):
      '''Iterate over all the memory regions in a lldb.SBMemoryRegionInfoList object.'''
      import lldb
      size = self.GetSize()
      region = lldb.SBMemoryRegionInfo()
      for i in range(size):
        self.GetMemoryRegionAtIndex(i, region)
        yield region

Region is a reference, and we will in the contents every loop, but if you assign anything to this reference, it will then be mutated next loop.

I filed this as an issue to communicate the bug because I assume a user has encountered this without realizing (because it's not very apparent).

Jlalond

Jlalond commented on Jun 16, 2025

@Jlalond
ContributorAuthor
zyn-li

zyn-li commented on Jun 16, 2025

@zyn-li
Contributor

@Jlalond I can fix this, assign to me

Jlalond

Jlalond commented on Jun 16, 2025

@Jlalond
ContributorAuthor

@Jlalond I can fix this, assign to me

Sounds good, as @aperez mentioned to me offline, we should also update the test to assert about the elements after we've iterated

https://github.com/llvm/llvm-project/blob/main/lldb/test/API/python_api/find_in_memory/TestFindInMemory.py#L165

added theissue type on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @Jlalond@llvmbot@zyn-li

    Issue actions

      [LLDB] SBMemoryRegionInfoListExtensions re-uses reference and then mutates it. · Issue #144439 · llvm/llvm-project