[PATCHv2,04/13] gdbserver: allows agent_mem_read to return an error code

Message ID 38df45adb7bfb18c47724d63f67bd35b5cd0923c.1674058360.git.aburgess@redhat.com
State New
Headers
Series Infcalls from B/P conditions in multi-threaded inferiors |

Commit Message

Andrew Burgess Jan. 18, 2023, 4:18 p.m. UTC
  Currently the gdbserver function agent_mem_read ignores any errors
from calling read_inferior_memory.  This means that if there is an
attempt to access invalid memory then this will appear to succeed.

In this I update agent_mem_read so that if read_inferior_memory fails,
agent_mem_read will return an error code.

However, non of the callers of agent_mem_read actually check the
return value, so this commit will have no effect on anything.  In the
next commit I will update the users of agent_mem_read to check for the
error code.

I've also updated the header comments on agent_mem_read to better
reflect what the function does, and its possible return values.
---
 gdbserver/tracepoint.cc | 11 ++++-------
 gdbserver/tracepoint.h  |  9 +++++++--
 2 files changed, 11 insertions(+), 9 deletions(-)
  

Comments

Terekhov, Mikhail via Gdb-patches Jan. 19, 2023, 9:59 a.m. UTC | #1
On Wednesday, January 18, 2023 5:18 PM, Andrew Burgess wrote:
> Currently the gdbserver function agent_mem_read ignores any errors
> from calling read_inferior_memory.  This means that if there is an
> attempt to access invalid memory then this will appear to succeed.
> 
> In this I update agent_mem_read so that if read_inferior_memory fails,

Did you mean "In this *patch* I update"?

> agent_mem_read will return an error code.
> 
> However, non of the callers of agent_mem_read actually check the

non -> none

Regards
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
  

Patch

diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 37a9a8c5b7c..60918fc0678 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -4914,8 +4914,7 @@  condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
   return (value ? 1 : 0);
 }
 
-/* Do memory copies for bytecodes.  */
-/* Do the recording of memory blocks for actions and bytecodes.  */
+/* See tracepoint.h.  */
 
 int
 agent_mem_read (struct eval_agent_expr_context *ctx,
@@ -4927,10 +4926,7 @@  agent_mem_read (struct eval_agent_expr_context *ctx,
 
   /* If a 'to' buffer is specified, use it.  */
   if (to != NULL)
-    {
-      read_inferior_memory (from, to, len);
-      return 0;
-    }
+    return read_inferior_memory (from, to, len);
 
   /* Otherwise, create a new memory block in the trace buffer.  */
   while (remaining > 0)
@@ -4951,7 +4947,8 @@  agent_mem_read (struct eval_agent_expr_context *ctx,
       memcpy (mspace, &blocklen, sizeof (blocklen));
       mspace += sizeof (blocklen);
       /* Record the memory block proper.  */
-      read_inferior_memory (from, mspace, blocklen);
+      if (read_inferior_memory (from, mspace, blocklen) != 0)
+	return 1;
       trace_debug ("%d bytes recorded", blocklen);
       remaining -= blocklen;
       from += blocklen;
diff --git a/gdbserver/tracepoint.h b/gdbserver/tracepoint.h
index a30f540d130..e424dc9dfb8 100644
--- a/gdbserver/tracepoint.h
+++ b/gdbserver/tracepoint.h
@@ -161,8 +161,13 @@  void gdb_agent_about_to_close (int pid);
 struct traceframe;
 struct eval_agent_expr_context;
 
-/* Do memory copies for bytecodes.  */
-/* Do the recording of memory blocks for actions and bytecodes.  */
+/* When TO is not NULL, do memory copies for bytecodes, read LEN bytes
+   starting at address FROM, and place the result in the buffer TO.
+   Return 0 on success, otherwise a non-zero error code.
+
+   When TO is NULL, do the recording of memory blocks for actions and
+   bytecodes into a new traceframe block.  Return 0 on success, otherwise,
+   return 1 if there is an error.  */
 
 int agent_mem_read (struct eval_agent_expr_context *ctx,
 		    unsigned char *to, CORE_ADDR from,