public inbox for gdb-testers@sourceware.org
help / color / mirror / Atom feed
From: sergiodj+buildbot@redhat.com
To: gdb-testers@sourceware.org
Subject: [binutils-gdb] [amd64] Invalid return address after displaced stepping
Date: Wed, 12 Aug 2015 20:32:00 -0000	[thread overview]
Message-ID: <4dafcdeb1341c4dd1a4641373bc17aab3ef2e788@gdb-build> (raw)

*** TEST RESULTS FOR COMMIT 4dafcdeb1341c4dd1a4641373bc17aab3ef2e788 ***

Author: Joel Brobecker <brobecker@adacore.com>
Branch: master
Commit: 4dafcdeb1341c4dd1a4641373bc17aab3ef2e788

[amd64] Invalid return address after displaced stepping

Making all-stop run on top of non-stop caused a small regression
in behavior. This was observed on x86_64-linux. The attached testcase
is in C whereas the investigation was done with an Ada program,
but it's the same scenario, and using a C testcase allows wider testing.
Basically: I am debugging a single-threaded program, and currently
stopped inside a function provided by a shared-library, at a line
calling a subprogram provided by a second shared library, and trying
to "next" over that function call.

Before we changed the default all-stop behavior, we had:

    7             Impl_Initialize;  -- Stop here and try "next" over this line
    (gdb) n
    8             return 5;  <<-- OK

But now, "next" just stops much earlier:

    (gdb) n
    0x00007ffff7bd8560 in impl.initialize@plt () from /[...]/lib/libpck.so

What happens is that next stops at a call instruction, which calls
the function's PLT, and GDB fails to notice that the inferior stepped
into a subroutine, and so decides that we're done. We can see another
symptom of the same issue by looking at the backtrace at the point
GDB stopped:

    (gdb) bt
    #0  0x00007ffff7bd8560 in impl.initialize@plt ()
       from /[...]/lib/libpck.so
    #1  0x00000000f7bd86f9 in ?? ()
    #2  0x00007fffffffdf50 in ?? ()
    #3  0x0000000000401893 in a () at /[...]/a.adb:7
    Backtrace stopped: frame did not save the PC

With a functioning GDB, the backtrace looks like the following instead:

    #0  0x00007ffff7bd8560 in impl.initialize@plt ()
       from /[...]/lib/libpck.so
    #1  0x00007ffff7bd86f9 in sub () at /[...]/pck.adb:7
    #2  0x0000000000401893 in a () at /[...]/a.adb:7

Note how, for frame #1, the address looks quite similar, except
for the high-order bits not being set:

    #1  0x00007ffff7bd86f9 in sub () at /[...]/pck.adb:7   <<<--  OK
    #1  0x00000000f7bd86f9 in ?? ()                        <<<--  WRONG
              ^^^^
              ||||
              Wrong

Investigating this further led me to displaced stepping.
As we are "next"-ing from a location where a breakpoint is inserted,
we need to step out of it, and since we're on non-stop mode, we need
to do it using displaced stepping. And looking at
amd64-tdep.c:amd64_displaced_step_fixup, I found the code that handles
the return address:

    regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
    retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
    retaddr = (retaddr - insn_offset) & 0xffffffffUL;

The mask used to compute retaddr looks wrong to me, keeping only
4 bytes instead of 8, and explains why the high order bits of
the backtrace are unset. What happens is that, after the displaced
stepping has completed, GDB restores that return address at the location
where the program expects it.  But because the top half bits of
the address have been masked out, the return address is now invalid.
The incorrect behavior of the "next" command and the backtrace at
that location are the first symptoms of that.  Another symptom is
that this actually alters the behavior of the program, where a "cont"
from there soon leads to a SEGV when the inferior tries to jump back
to that incorrect return address:

    (gdb) c
    Continuing.

    Program received signal SIGSEGV, Segmentation fault.
    0x00000000f7bd86f9 in ?? ()
    ^^^^^^^^^^^^^^^^^^

This patch fixes the issue by using a mask that seems more appropriate
for this architecture.

gdb/ChangeLog:

        * amd64-tdep.c (amd64_displaced_step_fixup): Fix the mask used to
        compute RETADDR.

gdb/testsuite/ChangeLog:

        * gdb.base/dso2dso-dso2.c, gdb.base/dso2dso-dso2.h,
        gdb.base/dso2dso-dso1.c, gdb.base/dso2dso-dso1.h, gdb.base/dso2dso.c,
        gdb.base/dso2dso.exp: New files.

Tested on x86_64-linux, no regression.


             reply	other threads:[~2015-08-12 20:32 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-12 20:32 sergiodj+buildbot [this message]
2015-08-12 20:32 ` Failures on Debian-s390x-native-gdbserver-m64, branch master sergiodj+buildbot
2015-08-12 20:37 ` Failures on Fedora-x86_64-cxx-build-m64, " sergiodj+buildbot
2015-08-12 20:44 ` Failures on Debian-s390x-m64, " sergiodj+buildbot
2015-08-12 20:44 ` Failures on AIX-POWER7-plain, " sergiodj+buildbot
2015-08-12 20:49 ` Failures on Fedora-x86_64-native-extended-gdbserver-m64, " sergiodj+buildbot
2015-08-12 20:49 ` Failures on Fedora-x86_64-native-gdbserver-m64, " sergiodj+buildbot
2015-08-12 20:50 ` Failures on Fedora-x86_64-native-gdbserver-m32, " sergiodj+buildbot
2015-08-12 20:57 ` Failures on Debian-s390x-native-extended-gdbserver-m64, " sergiodj+buildbot
2015-08-13 14:19 ` Failures on Debian-x86_64-m64, " sergiodj+buildbot
2015-08-13 14:47 ` Failures on Debian-x86_64-native-gdbserver-m64, " sergiodj+buildbot
2015-08-13 14:59 ` Failures on Fedora-ppc64be-cc-with-index, " sergiodj+buildbot
2015-08-13 15:19 ` Failures on Fedora-ppc64be-m64, " sergiodj+buildbot
2015-08-13 15:35 ` Failures on Fedora-ppc64be-native-gdbserver-m64, " sergiodj+buildbot
2015-08-13 15:54 ` Failures on Fedora-ppc64be-native-extended-gdbserver-m64, " sergiodj+buildbot
2015-08-14  3:07 ` Failures on Fedora-ppc64le-native-extended-gdbserver-m64, " sergiodj+buildbot
2015-08-14  3:41 ` Failures on Fedora-ppc64le-native-gdbserver-m64, " sergiodj+buildbot
2015-08-14  4:00 ` Failures on Fedora-ppc64le-m64, " sergiodj+buildbot
2015-08-14 12:37 ` Failures on Debian-i686-native-gdbserver, " sergiodj+buildbot
2015-08-14 13:39 ` Failures on Debian-i686-native-extended-gdbserver, " sergiodj+buildbot
2015-08-22  2:24 ` Failures on Debian-MIPS-m64, " sergiodj+buildbot

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=4dafcdeb1341c4dd1a4641373bc17aab3ef2e788@gdb-build \
    --to=sergiodj+buildbot@redhat.com \
    --cc=gdb-testers@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).