This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Encapsulate target_fileio file descriptors


On 03/18/2015 01:20 PM, Gary Benson wrote:
> Hi all,
> 
> Various target_fileio_* functions use integer file descriptors to
> refer to open files, which can cause problems if the target stack
> changes between the file being opened and other file operations being
> performed on that file.  This commit makes the target_fileio_*
> functions that use file descriptors use an opaque target_fileio_t
> handle instead that encapsulates both the file descriptor itself and
> the target_ops vector that should be used to access it.

Thanks Gary.

I think this deserves an expanded explanation, for posterity.

Making the handle be something other than an integer itself
doesn't actually fix things -- e.g., if the patch only added
the target_fileio_t as a wrapper struct _without_ the target_ops
pointer.

The main issue is this :

/* Close FD on the target.  Return 0, or -1 if an error occurs
   (and set *TARGET_ERRNO).  */
int
target_fileio_close (int fd, int *target_errno)
{
  struct target_ops *t;

  for (t = default_fileio_target (); t != NULL; t = t->beneath)
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    {
      if (t->to_fileio_close != NULL)
	{
	  int ret = t->to_fileio_close (t, fd, target_errno);

	  if (targetdebug)
	    fprintf_unfiltered (gdb_stdlog,
				"target_fileio_close (%d) = %d (%d)\n",
				fd, ret, ret != -1 ? 0 : *target_errno);
	  return ret;
	}
    }

  *target_errno = FILEIO_ENOSYS;
  return -1;
}



Say that the file is originally opened with the remote target.  Then,
something goes wrong while the file is open, and the target
disconnects/closes, which pops the remote target off the stack.
Then, when later something tries to close the original
fileio file (e.g., target_fileio_close_cleanup, from
target_fileio_read_alloc_1), default_fileio_target is called to
find the target to apply the target method to.  This does:

static struct target_ops *
default_fileio_target (void)
{
  /* If we're already connected to something that can perform
     file I/O, use it. Otherwise, try using the native target.  */
  if (current_target.to_stratum >= process_stratum)
    return current_target.beneath;
  else
    return find_default_run_target ("file I/O");
}

Since the remote target is no longer in the target stack, the current
target won't have process_stratum stratum, and we'll reach
find_default_run_target.  And that, unless "set auto-connect-native-target"
is "off" (which is "on" by default), will return a pointer to
the native target.

The end result is that although the file descriptor only made
sense for the remote target, we call "t->to_fileio_close" in the
native target, which ends up in:

/* Close FD on the target.  Return 0, or -1 if an error occurs
   (and set *TARGET_ERRNO).  */
static int
inf_child_fileio_close (struct target_ops *self, int fd, int *target_errno)
{
  int ret;

  ret = close (fd);
        ^^^^^^^^^^

  if (ret == -1)
    *target_errno = inf_child_errno_to_fileio_error (errno);

  return ret;
}

And obviously, if FD happens to be a file descriptor number that
happens to be open in GDB as well, we'll close it by mistake.
That's the sort of random issue that wouldn't be very fun to
track down!

The fix for this is to always operate on the file
descriptor using the target that originally created it
in the first place.

> 
> Built and regtested on RHEL 6.6 x86_64.
> 
> Ok to commit?

Note that this solution of embedding the target_ops pointer in
the handle itself means that the targets themselves need to
be prepared to be called when they're already closed.

It works today, though I suspect that it may cause problems in
the future.  When we get to multi-target, and target_ops instances
become objects instantiated on the heap, we'll have the problem
that the target_ops pointer in the handle becomes invalid
as soon as the target_ops object is closed/destroyed/released
(unless the target object is refcounted, which isn't clear
it should).

A table-based solution, where target.c maintains (something
conceptually like) a table of "target file descriptor + target_ops"
elements, and passes an index into that table as file handle to
target_fileio_ callers, would make it possible to invalidate the
file handles directly in the table when a target is closed, so
that e.g., target_fileio_close (etc.) could detect that the file
handle is now invalid, and return error with errno=EBADF _without_
calling through the now invalid target_ops pointer.

We can cross that bridge when we get to it, but I thought I
should point it it out.

Speaking of EBADF, I noticed that remote_hostio_send_command
fails with ENOSYS if the remote connection is closed, which
seems wrong to me.

> gdb/ChangeLog:
> 
> 	* target.h (target_fileio_t): New typedef.

"target_fileio_t" doesn't really suggest to me that this is a
descriptor or a handle.  How about "target_fhandle_t" or "target_fildes_t"?

> 	(target_fileio_open): Changed signature, updated comment.

No past tense: "change", "update", throughout.

> 	(target_fileio_pread): Likewise.
> 	(target_fileio_pwrite): Likewise.
> 	(target_fileio_close): Likewise.
> 	* target.c (target_fileio_handle): New structure.
> 	(target_fileio_open): Updated to return a target_fileio_t
> 	instead of an integer file descriptor.  Updated targetdebug
> 	message to include the target shortname.  Updated comment.
> 	(target_fileio_pread): Take a target_fileio_t handle as the
> 	first argument instead of an integer file descriptor.  Updated
> 	targetdebug message to include the target shortname.  Updated
> 	comment.
> 	(target_fileio_pwrite): Likewise.
> 	(target_fileio_close): Likewise.
> 	(target_fileio_unlink): Updated comment.
> 	(target_fileio_readlink): Likewise.
> 	(target_fileio_close_cleanup): Take a target_fileio_t handle
> 	as the first argument instead of a pointer to an integer file
> 	descriptor.  Updated caller.
> 	(target_fileio_read_alloc_1): Use a target_fileio_t handle
> 	instead of an integer file descriptor.
> ---
>  gdb/ChangeLog |   25 +++++++++
>  gdb/target.c  |  164 ++++++++++++++++++++++++++------------------------------
>  gdb/target.h  |   28 ++++++----
>  3 files changed, 118 insertions(+), 99 deletions(-)
> 
> diff --git a/gdb/target.c b/gdb/target.c
> index af94f48..7e04679 100644
> --- a/gdb/target.c
> +++ b/gdb/target.c
> @@ -2684,10 +2684,20 @@ default_fileio_target (void)
>      return find_default_run_target ("file I/O");
>  }
>  
> -/* Open FILENAME on the target, using FLAGS and MODE.  Return a
> -   target file descriptor, or -1 if an error occurs (and set
> -   *TARGET_ERRNO).  */
> -int
> +/* File handle for target file operations.  */
> +
> +struct target_fileio_handle
> +{
> +  /* The target on which this file is open.  */
> +  struct target_ops *t;
> +
> +  /* The file's descriptor on the target.  */
> +  int fd;
> +};
> +
> +/* See target.h.  */
> +
> +target_fileio_t
>  target_fileio_open (const char *filename, int flags, int mode,
>  		    int *target_errno)
>  {
> @@ -2698,107 +2708,86 @@ target_fileio_open (const char *filename, int flags, int mode,
>        if (t->to_fileio_open != NULL)
>  	{
>  	  int fd = t->to_fileio_open (t, filename, flags, mode, target_errno);
> +	  target_fileio_t fh = NULL;
>  
>  	  if (targetdebug)
>  	    fprintf_unfiltered (gdb_stdlog,
> -				"target_fileio_open (%s,0x%x,0%o) = %d (%d)\n",
> -				filename, flags, mode,
> +				"target_fileio_open (%s,0x%x,0%o)"
> +				" = %s:%d (%d)\n",
> +				filename, flags, mode, t->to_shortname,
>  				fd, fd != -1 ? 0 : *target_errno);
> -	  return fd;
> +	  if (fd >= 0)
> +	    {
> +	      fh = XCNEW (struct target_fileio_handle);
> +	      fh->t = t;
> +	      fh->fd = fd;
> +	    }
> +
> +	  return fh;
>  	}
>      }
>  
>    *target_errno = FILEIO_ENOSYS;
> -  return -1;
> +  return NULL;
>  }
>  
> -/* Write up to LEN bytes from WRITE_BUF to FD on the target.
> -   Return the number of bytes written, or -1 if an error occurs
> -   (and set *TARGET_ERRNO).  */
> +/* See target.h.  */
> +
>  int
> -target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
> -		      ULONGEST offset, int *target_errno)
> +target_fileio_pwrite (target_fileio_t fh, const gdb_byte *write_buf,
> +		      int len, ULONGEST offset, int *target_errno)
>  {
> -  struct target_ops *t;
> +  int ret = fh->t->to_fileio_pwrite (fh->t, fh->fd, write_buf, len,
> +				     offset, target_errno);
>  
> -  for (t = default_fileio_target (); t != NULL; t = t->beneath)
> -    {
> -      if (t->to_fileio_pwrite != NULL)
> -	{
> -	  int ret = t->to_fileio_pwrite (t, fd, write_buf, len, offset,
> -					 target_errno);
> -
> -	  if (targetdebug)
> -	    fprintf_unfiltered (gdb_stdlog,
> -				"target_fileio_pwrite (%d,...,%d,%s) "
> -				"= %d (%d)\n",
> -				fd, len, pulongest (offset),
> -				ret, ret != -1 ? 0 : *target_errno);
> -	  return ret;
> -	}
> -    }
> -
> -  *target_errno = FILEIO_ENOSYS;
> -  return -1;
> +  if (targetdebug)
> +    fprintf_unfiltered (gdb_stdlog,
> +			"target_fileio_pwrite (%s:%d,...,%d,%s)"
> +			" = %d (%d)\n",
> +			fh->t->to_shortname, fh->fd, len,
> +			pulongest (offset), ret,
> +			ret != -1 ? 0 : *target_errno);
> +  return ret;
>  }
>  
> -/* Read up to LEN bytes FD on the target into READ_BUF.
> -   Return the number of bytes read, or -1 if an error occurs
> -   (and set *TARGET_ERRNO).  */
> +/* See target.h.  */
> +
>  int
> -target_fileio_pread (int fd, gdb_byte *read_buf, int len,
> +target_fileio_pread (target_fileio_t fh, gdb_byte *read_buf, int len,
>  		     ULONGEST offset, int *target_errno)
>  {
> -  struct target_ops *t;
> +  int ret = fh->t->to_fileio_pread (fh->t, fh->fd, read_buf, len,
> +				    offset, target_errno);
>  
> -  for (t = default_fileio_target (); t != NULL; t = t->beneath)
> -    {
> -      if (t->to_fileio_pread != NULL)
> -	{
> -	  int ret = t->to_fileio_pread (t, fd, read_buf, len, offset,
> -					target_errno);
> -
> -	  if (targetdebug)
> -	    fprintf_unfiltered (gdb_stdlog,
> -				"target_fileio_pread (%d,...,%d,%s) "
> -				"= %d (%d)\n",
> -				fd, len, pulongest (offset),
> -				ret, ret != -1 ? 0 : *target_errno);
> -	  return ret;
> -	}
> -    }
> -
> -  *target_errno = FILEIO_ENOSYS;
> -  return -1;
> +  if (targetdebug)
> +    fprintf_unfiltered (gdb_stdlog,
> +			"target_fileio_pread (%s:%d,...,%d,%s)"
> +			" = %d (%d)\n",
> +			fh->t->to_shortname, fh->fd, len,
> +			pulongest (offset), ret,
> +			ret != -1 ? 0 : *target_errno);
> +  return ret;
>  }
>  
> -/* Close FD on the target.  Return 0, or -1 if an error occurs
> -   (and set *TARGET_ERRNO).  */
> +/* See target.h.  */
> +
>  int
> -target_fileio_close (int fd, int *target_errno)
> +target_fileio_close (target_fileio_t fh, int *target_errno)
>  {
> -  struct target_ops *t;
> +  int ret = fh->t->to_fileio_close (fh->t, fh->fd, target_errno);
>  
> -  for (t = default_fileio_target (); t != NULL; t = t->beneath)
> -    {
> -      if (t->to_fileio_close != NULL)
> -	{
> -	  int ret = t->to_fileio_close (t, fd, target_errno);
> -
> -	  if (targetdebug)
> -	    fprintf_unfiltered (gdb_stdlog,
> -				"target_fileio_close (%d) = %d (%d)\n",
> -				fd, ret, ret != -1 ? 0 : *target_errno);
> -	  return ret;
> -	}
> -    }
> +  if (targetdebug)
> +    fprintf_unfiltered (gdb_stdlog,
> +			"target_fileio_close (%s:%d) = %d (%d)\n",
> +			fh->t->to_shortname, fh->fd, ret,
> +			ret != -1 ? 0 : *target_errno);
> +  xfree (fh);
>  
> -  *target_errno = FILEIO_ENOSYS;
> -  return -1;
> +  return ret;
>  }
>  
> -/* Unlink FILENAME on the target.  Return 0, or -1 if an error
> -   occurs (and set *TARGET_ERRNO).  */
> +/* See target.h.  */
> +
>  int
>  target_fileio_unlink (const char *filename, int *target_errno)
>  {
> @@ -2822,9 +2811,8 @@ target_fileio_unlink (const char *filename, int *target_errno)
>    return -1;
>  }
>  
> -/* Read value of symbolic link FILENAME on the target.  Return a
> -   null-terminated string allocated via xmalloc, or NULL if an error
> -   occurs (and set *TARGET_ERRNO).  */
> +/* See target.h.  */
> +
>  char *
>  target_fileio_readlink (const char *filename, int *target_errno)
>  {
> @@ -2852,10 +2840,10 @@ target_fileio_readlink (const char *filename, int *target_errno)
>  static void
>  target_fileio_close_cleanup (void *opaque)
>  {
> -  int fd = *(int *) opaque;
> +  target_fileio_t fh = (target_fileio_t) opaque;
>    int target_errno;
>  
> -  target_fileio_close (fd, &target_errno);
> +  target_fileio_close (fh, &target_errno);
>  }
>  
>  /* Read target file FILENAME.  Store the result in *BUF_P and
> @@ -2872,14 +2860,14 @@ target_fileio_read_alloc_1 (const char *filename,
>    size_t buf_alloc, buf_pos;
>    gdb_byte *buf;
>    LONGEST n;
> -  int fd;
> +  target_fileio_t fh;
>    int target_errno;
>  
> -  fd = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
> -  if (fd == -1)
> +  fh = target_fileio_open (filename, FILEIO_O_RDONLY, 0700, &target_errno);
> +  if (fh == NULL)
>      return -1;
>  
> -  close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
> +  close_cleanup = make_cleanup (target_fileio_close_cleanup, fh);
>  
>    /* Start by reading up to 4K at a time.  The target will throttle
>       this number down if necessary.  */
> @@ -2888,7 +2876,7 @@ target_fileio_read_alloc_1 (const char *filename,
>    buf_pos = 0;
>    while (1)
>      {
> -      n = target_fileio_pread (fd, &buf[buf_pos],
> +      n = target_fileio_pread (fh, &buf[buf_pos],
>  			       buf_alloc - buf_pos - padding, buf_pos,
>  			       &target_errno);
>        if (n < 0)
> diff --git a/gdb/target.h b/gdb/target.h
> index c95e1a4..1edee84 100644
> --- a/gdb/target.h
> +++ b/gdb/target.h
> @@ -1915,27 +1915,33 @@ extern int target_search_memory (CORE_ADDR start_addr,
>  
>  /* Target file operations.  */
>  
> -/* Open FILENAME on the target, using FLAGS and MODE.  Return a
> -   target file descriptor, or -1 if an error occurs (and set
> -   *TARGET_ERRNO).  */
> -extern int target_fileio_open (const char *filename, int flags, int mode,
> -			       int *target_errno);
> +/* File handle for target file operations.  */
> +typedef struct target_fileio_handle *target_fileio_t;
>  
> -/* Write up to LEN bytes from WRITE_BUF to FD on the target.
> +/* Open FILENAME on the target, using FLAGS and MODE.  Return a target
> +   file handle, or NULL if an error occurs (and set *TARGET_ERRNO).  */
> +extern target_fileio_t target_fileio_open (const char *filename,
> +					   int flags, int mode,
> +					   int *target_errno);
> +
> +/* Write up to LEN bytes from WRITE_BUF to FH on the target.
>     Return the number of bytes written, or -1 if an error occurs
>     (and set *TARGET_ERRNO).  */
> -extern int target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
> +extern int target_fileio_pwrite (target_fileio_t fh,
> +				 const gdb_byte *write_buf, int len,
>  				 ULONGEST offset, int *target_errno);
>  
> -/* Read up to LEN bytes FD on the target into READ_BUF.
> +/* Read up to LEN bytes from FH on the target into READ_BUF.
>     Return the number of bytes read, or -1 if an error occurs
>     (and set *TARGET_ERRNO).  */
> -extern int target_fileio_pread (int fd, gdb_byte *read_buf, int len,
> +extern int target_fileio_pread (target_fileio_t fh,
> +				gdb_byte *read_buf, int len,
>  				ULONGEST offset, int *target_errno);
>  
> -/* Close FD on the target.  Return 0, or -1 if an error occurs
> +/* Close FH on the target.  Return 0, or -1 if an error occurs
>     (and set *TARGET_ERRNO).  */
> -extern int target_fileio_close (int fd, int *target_errno);
> +extern int target_fileio_close (target_fileio_t fh,
> +				int *target_errno);
>  
>  /* Unlink FILENAME on the target.  Return 0, or -1 if an error
>     occurs (and set *TARGET_ERRNO).  */
> 


Thanks,
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]