Bug 30615 - search_memory() method on the gdb.Inferior objects disregards the particular inferior and always searches the memory of the current inferior instead
Summary: search_memory() method on the gdb.Inferior objects disregards the particular ...
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 14.1
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-04 22:29 UTC by Disconnect3d
Modified: 2023-07-14 19:43 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2023-07-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Disconnect3d 2023-07-04 22:29:18 UTC
The search_memory() method on the gdb.Inferior objects disregards the particular inferior and always searches the memory of the current inferior instead.

The error is here:

```
static PyObject *
infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) {
...
      found = target_search_memory (start_addr, length,
				    buffer, pattern_size,
				    &found_addr);
```

(Can also be seen on https://github.com/bminor/binutils-gdb/blob/a89e364b45a93acd20f48abd787ef5cb7c07f683/gdb/python/py-inferior.c#L659).


Instead of calling target_search_memory(...args) the GDB should call self->...<some field referencing inferior>...->top_target()->search_memory(...args)


This can be triggered from Python API via `gdb.selected_inferior().search_memory(...)` but I haven't created a reproducer that would tell if this is really a problem or not. However, the fact that there is a "selected inferior" suggests that there could  be.


Thanks to my friend Arusekk who spotted this bug.
Comment 1 Disconnect3d 2023-07-05 12:02:57 UTC
Relevant documentation: https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-Connections-and-Programs.html
Comment 2 Tom Tromey 2023-07-06 12:56:06 UTC
Python bug.
Comment 3 Tom Tromey 2023-07-06 17:26:44 UTC
I think this affects reading and writing memory as well.
Comment 4 Tom Tromey 2023-07-06 17:34:21 UTC
I have a patch.
Comment 6 Sourceware Commits 2023-07-14 19:43:36 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=75ec09829780f8d00d8515e07101a29272a22303

commit 75ec09829780f8d00d8515e07101a29272a22303
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Jul 6 11:33:47 2023 -0600

    Use correct inferior in Inferior.read_memory et al
    
    A user noticed that Inferior.read_memory and a few other Python APIs
    will always use the currently selected inferior, not the one passed to
    the call.
    
    This patch fixes the bug by arranging to switch to the inferior.  I
    found this same issue in several APIs, so this fixes them all.
    
    I also added a few missing calls to INFPY_REQUIRE_VALID to these
    methods.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615
    Approved-By: Pedro Alves <pedro@palves.net>
Comment 7 Tom Tromey 2023-07-14 19:43:57 UTC
Fixed.