Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Doug Evans <dje@google.com>
To: gdb-patches@sourceware.org
Subject: [patch] Fix 64x32 handling in gdbserver/linux-x86-low.c:ps_get_thread_area
Date: Sat, 25 May 2013 01:25:00 -0000	[thread overview]
Message-ID: <yjt24ndru9yf.fsf@ruffy2.mtv.corp.google.com> (raw)

Hi.

I found this while testing gdbserver with 64x32
(64-bit gdb/gdbserver with the testcase compiled for 32-bit).
tls.exp has a number of bizarre failures.

E.g.,
Cannot find thread-local storage for Thread 16598, executable file /g5/local/ctools/gdb/trunk/build/obj64/gdb/testsuite/gdb.threads/tls:
and
gdbserver: PID mismatch!  Expected 16598, got 16596

I found the cause to be the storing of a 32 bit address in a 64 bit value,
leaving the high bits uninitialized.
One can see the bug with this patch.

diff thread-db.c
--- thread-db.c~ 2013-02-19 14:45:14.000000000 -0800
+++ thread-db.c 2013-05-24 17:20:14.450500768 -0700
@@ -40,6 +40,8 @@
 #include <dlfcn.h>
 #endif
 
+#include <stdio.h>
+#include <stdarg.h>
 #include <stdint.h>
 #include <limits.h>
 #include <ctype.h>
@@ -267,6 +269,22 @@
   return 1;
 }
 
+void
+plogf (const char *msg, ...)
+{
+  static FILE *f;
+  va_list args;
+  if (f == NULL)
+    {
+      f = fopen ("/tmp/gdbserver.log", "a");
+      fprintf (f, "... new run ...\n");
+    }
+  va_start (args, msg);
+  vfprintf (f, msg, args);
+  va_end (args);
+  fflush (f);
+}
+
 static int
 find_one_thread (ptid_t ptid)
 {
@@ -283,12 +301,18 @@
   if (lwp->thread_known)
     return 1;
 
+  memset (&th, -1, sizeof (th));
+
   /* Get information about this thread.  */
   err = thread_db->td_ta_map_lwp2thr_p (thread_db->thread_agent, lwpid, &th);
   if (err != TD_OK)
     error ("Cannot get thread handle for LWP %d: %s",
 	   lwpid, thread_db_err_str (err));
 
+  plogf ("lwpid %d\n", lwpid);
+  plogf ("th_ta_p %p\n", th.th_ta_p);
+  plogf ("th_unique %p\n", th.th_unique);
+
   err = thread_db->td_thr_get_info_p (&th, &ti);
   if (err != TD_OK)
     error ("Cannot get thread info for LWP %d: %s",
---

Testing tls.exp in 32-bit mode with 64-bit gdb/gdbserver, one sees this
in /tmp/gdbserver.log:

... new run ...
lwpid 16596
th_ta_p 0x17ce020
th_unique (nil)
lwpid 16598
th_ta_p 0x17ce020
th_unique 0xfffffffff7cb9b40
lwpid 16598
th_ta_p 0x17ce020
th_unique 0xfffffffff7cb9b40
...

I need to do a bit more testing, but this seems pretty obvious.
The bug doesn't exist in gdb, it already does the needed extension.

2013-05-24  Doug Evans  <dje@google.com>

	* linux-x86-low.c (ps_get_thread_area): Properly extend address to
	64 bits in 64-cross-32 environment.

Index: gdbserver/linux-x86-low.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbserver/linux-x86-low.c,v
retrieving revision 1.45
diff -u -p -r1.45 linux-x86-low.c
--- gdbserver/linux-x86-low.c	23 May 2013 17:17:50 -0000	1.45
+++ gdbserver/linux-x86-low.c	25 May 2013 01:06:49 -0000
@@ -196,7 +196,8 @@ ps_get_thread_area (const struct ps_proc
 		(void *) (intptr_t) idx, (unsigned long) &desc) < 0)
       return PS_ERR;
 
-    *(int *)base = desc[1];
+    /* Ensure we properly extend the value to 64-bits for x86_64.  */
+    *base = (void*) (uintptr_t) desc[1];
     return PS_OK;
   }
 }


             reply	other threads:[~2013-05-25  1:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-25  1:25 Doug Evans [this message]
2013-05-25 15:04 ` Pedro Alves
2013-06-05 21:28   ` Doug Evans
2013-06-06  5:20     ` Joel Brobecker
2013-06-06 18:00       ` Doug Evans

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yjt24ndru9yf.fsf@ruffy2.mtv.corp.google.com \
    --to=dje@google.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox