[5/5] Use correct inferior in Inferior.read_memory et al

Message ID 20230707-py-inf-fixes-30615-v1-5-7792ab559530@adacore.com
State New
Headers
Series Fix some Python Inferior methods |

Commit Message

Tom Tromey July 7, 2023, 3:08 p.m. UTC
  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 found out that setting current_inferior isn't enough when
reading memory -- one must also set inferior_ptid.  This seems very
confusing to me, especially considering that current_inferior must be
set properly anyway in order to access the target stack.

Finally, I also added a few missing calls to INFPY_REQUIRE_VALID to
these methods.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615
---
 gdb/python/py-inferior.c                 | 36 ++++++++++++++++++++++++++++----
 gdb/testsuite/gdb.python/py-inferior.exp | 25 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 4 deletions(-)
  

Comments

Pedro Alves July 7, 2023, 6:26 p.m. UTC | #1
On 2023-07-07 16:08, Tom Tromey via Gdb-patches wrote:
> 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 found out that setting current_inferior isn't enough when
> reading memory -- one must also set inferior_ptid.  This seems very
> confusing to me, especially considering that current_inferior must be
> set properly anyway in order to access the target stack.

That is because we must be able to remove breakpoints from fork children
with "set detach-on-fork off", in which case there's no inferior to represent
the child at all.  (removing breakpoints == writing to memory to replace breakpoint
insn with original instruction).


> Finally, I also added a few missing calls to INFPY_REQUIRE_VALID to
> these methods.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615
> ---
>  gdb/python/py-inferior.c                 | 36 ++++++++++++++++++++++++++++----
>  gdb/testsuite/gdb.python/py-inferior.exp | 25 ++++++++++++++++++++++
>  2 files changed, 57 insertions(+), 4 deletions(-)
> 
> diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
> index af8bd8855a3..7fb422f621e 100644
> --- a/gdb/python/py-inferior.c
> +++ b/gdb/python/py-inferior.c
> @@ -30,6 +30,7 @@
>  #include "gdbsupport/gdb_signals.h"
>  #include "py-event.h"
>  #include "py-stopevent.h"
> +#include "progspace-and-thread.h"
>  #include <unordered_map>
>  
>  using thread_map_t
> @@ -528,11 +529,14 @@ gdbpy_inferiors (PyObject *unused, PyObject *unused2)
>  static PyObject *
>  infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
>  {
> +  inferior_object *inf = (inferior_object *) self;
>    CORE_ADDR addr, length;
>    gdb::unique_xmalloc_ptr<gdb_byte> buffer;
>    PyObject *addr_obj, *length_obj;
>    static const char *keywords[] = { "address", "length", NULL };
>  
> +  INFPY_REQUIRE_VALID (inf);
> +
>    if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
>  					&addr_obj, &length_obj))
>      return NULL;
> @@ -543,6 +547,12 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
>  
>    try
>      {
> +      scoped_restore_current_inferior restore_inferior;
> +      scoped_restore_current_pspace_and_thread restore_thread;
> +
> +      thread_info *thr = any_thread_of_inferior (inf->inferior);
> +      switch_to_thread_no_regs (thr);
> +

I think this is going to break any Python unwinder that needs to read memory, because switching
threads invalidates the frame chain.  See how proc-service.c:ps_xfer_memory avoids switching
threads, switching only inferior_ptid.  I think this needs to do the same.

Pedro Alves
  
Pedro Alves July 7, 2023, 6:27 p.m. UTC | #2
On 2023-07-07 19:26, Pedro Alves wrote:
> On 2023-07-07 16:08, Tom Tromey via Gdb-patches wrote:
>> 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 found out that setting current_inferior isn't enough when
>> reading memory -- one must also set inferior_ptid.  This seems very
>> confusing to me, especially considering that current_inferior must be
>> set properly anyway in order to access the target stack.
> 

> That is because we must be able to remove breakpoints from fork children
> with "set detach-on-fork off", in which case there's no inferior to represent

Err, I meant "set detach-on-fork on", of course.

Pedro Alves
  

Patch

diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index af8bd8855a3..7fb422f621e 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -30,6 +30,7 @@ 
 #include "gdbsupport/gdb_signals.h"
 #include "py-event.h"
 #include "py-stopevent.h"
+#include "progspace-and-thread.h"
 #include <unordered_map>
 
 using thread_map_t
@@ -528,11 +529,14 @@  gdbpy_inferiors (PyObject *unused, PyObject *unused2)
 static PyObject *
 infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
 {
+  inferior_object *inf = (inferior_object *) self;
   CORE_ADDR addr, length;
   gdb::unique_xmalloc_ptr<gdb_byte> buffer;
   PyObject *addr_obj, *length_obj;
   static const char *keywords[] = { "address", "length", NULL };
 
+  INFPY_REQUIRE_VALID (inf);
+
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords,
 					&addr_obj, &length_obj))
     return NULL;
@@ -543,6 +547,12 @@  infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
 
   try
     {
+      scoped_restore_current_inferior restore_inferior;
+      scoped_restore_current_pspace_and_thread restore_thread;
+
+      thread_info *thr = any_thread_of_inferior (inf->inferior);
+      switch_to_thread_no_regs (thr);
+
       buffer.reset ((gdb_byte *) xmalloc (length));
 
       read_memory (addr, buffer.get (), length);
@@ -565,6 +575,7 @@  infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw)
 static PyObject *
 infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
 {
+  inferior_object *inf = (inferior_object *) self;
   struct gdb_exception except;
   Py_ssize_t buf_len;
   const gdb_byte *buffer;
@@ -573,6 +584,8 @@  infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
   static const char *keywords[] = { "address", "buffer", "length", NULL };
   Py_buffer pybuf;
 
+  INFPY_REQUIRE_VALID (inf);
+
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords,
 					&addr_obj, &pybuf, &length_obj))
     return NULL;
@@ -591,6 +604,12 @@  infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
 
   try
     {
+      scoped_restore_current_inferior restore_inferior;
+      scoped_restore_current_pspace_and_thread restore_thread;
+
+      thread_info *thr = any_thread_of_inferior (inf->inferior);
+      switch_to_thread_no_regs (thr);
+
       write_memory_with_notification (addr, buffer, length);
     }
   catch (gdb_exception &ex)
@@ -604,7 +623,7 @@  infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
 }
 
 /* Implementation of
-   gdb.search_memory (address, length, pattern).  ADDRESS is the
+   Inferior.search_memory (address, length, pattern).  ADDRESS is the
    address to start the search.  LENGTH specifies the scope of the
    search from ADDRESS.  PATTERN is the pattern to search for (and
    must be a Python object supporting the buffer protocol).
@@ -614,6 +633,7 @@  infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw)
 static PyObject *
 infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
 {
+  inferior_object *inf = (inferior_object *) self;
   struct gdb_exception except;
   CORE_ADDR start_addr, length;
   static const char *keywords[] = { "address", "length", "pattern", NULL };
@@ -624,6 +644,8 @@  infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
   int found = 0;
   Py_buffer pybuf;
 
+  INFPY_REQUIRE_VALID (inf);
+
   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords,
 					&start_addr_obj, &length_obj,
 					&pybuf))
@@ -656,6 +678,12 @@  infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw)
 
   try
     {
+      scoped_restore_current_inferior restore_inferior;
+      scoped_restore_current_pspace_and_thread restore_thread;
+
+      thread_info *thr = any_thread_of_inferior (inf->inferior);
+      switch_to_thread_no_regs (thr);
+
       found = target_search_memory (start_addr, length,
 				    buffer, pattern_size,
 				    &found_addr);
@@ -912,10 +940,10 @@  infpy_get_main_name (PyObject *self, void *closure)
       /* This is unfortunate but the implementation of main_name can
 	 reach into memory.  */
       scoped_restore_current_inferior restore_inferior;
-      set_current_inferior (inf->inferior);
+      scoped_restore_current_pspace_and_thread restore_thread;
 
-      scoped_restore_current_program_space restore_current_progspace;
-      set_current_program_space (inf->inferior->pspace);
+      thread_info *thr = any_thread_of_inferior (inf->inferior);
+      switch_to_thread_no_regs (thr);
 
       name = main_name ();
     }
diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp
index 41e26878031..9b0643d63e2 100644
--- a/gdb/testsuite/gdb.python/py-inferior.exp
+++ b/gdb/testsuite/gdb.python/py-inferior.exp
@@ -90,6 +90,7 @@  gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \
   "read str address" 0
 gdb_py_test_silent_cmd "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \
   "read str contents" 1
+gdb_test "python print(astr\[0\])" .*
 gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0
 gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0
 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \
@@ -97,6 +98,10 @@  gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \
 gdb_test "print str" " = \"hallo, testsuite\"" \
   "ensure str was changed in the inferior"
 
+# Add a new inferior here, so we can test that operations work on the
+# correct inferior.
+set num [add_inferior]
+
 # Test memory search.
 
 set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*}
@@ -114,6 +119,9 @@  with_test_prefix "string" {
     gdb_test_no_output "py start_addr = search_buf.address"
     gdb_test_no_output "py length = search_buf.type.sizeof"
 
+    # Switch to the new inferior before testing.
+    gdb_test "inferior $num" ".*" "switch to inferior $num"
+
     gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \
 	"${one_pattern_found}" "find string pattern"
 
@@ -127,6 +135,23 @@  with_test_prefix "string" {
 	"${one_pattern_found}" "pattern found at end of range"
 }
 
+# While still in the new inferior, test reading and writing memory
+# again.
+gdb_py_test_silent_cmd "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \
+    "read str while other inferior selected" 1
+gdb_test "python print(astr\[1\])" "b'a'" \
+    "print a character from the string"
+gdb_py_test_silent_cmd "python astr\[1\] = b'X'" "change str again" 0
+gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \
+    "write str while other inferior selected" 1
+
+gdb_test "inferior 1" ".*" "switch back to inferior 1"
+
+gdb_test "print str" " = \"hXllo, testsuite\"" \
+    "ensure str was changed while other inferior selected"
+
+gdb_test_no_output "remove-inferiors $num" "remove-inferiors $num"
+
 # Import struct to pack the following patterns.
 gdb_test_no_output "py from struct import *"