[RFC] (windows) GDB/MI crash when using "-list-thread-groups --available"

Message ID 1525978704-70543-1-git-send-email-brobecker@adacore.com
State New, archived
Headers

Commit Message

Joel Brobecker May 10, 2018, 6:58 p.m. UTC
  Hello,

On Windows, using the "-list-thread-groups --available" GDB/MI command
before an inferior is being debugged:

    % gdb -q -i=mi
    =thread-group-added,id="i1"
    =cmd-param-changed,param="auto-load safe-path",value="/"
    (gdb)
    -list-thread-groups --available
    Segmentation fault

Ooops!

The SEGV happens because the -list-thread-groups --available command
triggers a windows_nat_target::xfer_partial call for a TARGET_OBJECT_OSDATA
object.  Until a program is being debugged, the target_ops layer that
gets the call is the Windows "native" layer. Except for a couple of
specific objects (TARGET_OBJECT_MEMORY and TARGET_OBJECT_LIBRARIES),
this layer's xfer_partial method delegates the xfer of other objects
to the target beneath:

    default:
      return beneath->xfer_partial (object, annex,
                                    readbuf, writebuf, offset, len,
                                    xfered_len);

Unfortunately, there is no "beneath layer" in this case, so
beneath is NULL and dereferencing it leads to the SEGV.

This patch fixes the issue by checking beneath before trying
to delegate the request. But I am wondering whether this is
the right place to fix this issue, or whether we should expect
BENEATH to never be NULL. Ideas?

Also, The testcase I am proposing fails on the -list-thread-groups test
when run on GNU/Linux because, on that platform, the command returns
more output than the expect buffer can handle, resulting in an UNRESOLVED
status. How does one usually handle this? The only why I can think of
is a loop of gdb_test_multiple... Other ideas?

gdb/ChangeLog:

        * windows-nat.c (windows_nat_target::xfer_partial): Return
        TARGET_XFER_E_IO if we need to delegate to the target beneath
        but BENEATH is NULL.

gdb/testsuite/ChangeLog:

        * gdb.mi/mi-list-thread-groups-no-inferior.exp: New testcase.

Thanks!
  

Comments

Simon Marchi May 10, 2018, 9:02 p.m. UTC | #1
On 2018-05-10 14:58, Joel Brobecker wrote:
> Hello,
> 
> On Windows, using the "-list-thread-groups --available" GDB/MI command
> before an inferior is being debugged:
> 
>     % gdb -q -i=mi
>     =thread-group-added,id="i1"
>     =cmd-param-changed,param="auto-load safe-path",value="/"
>     (gdb)
>     -list-thread-groups --available
>     Segmentation fault
> 
> Ooops!
> 
> The SEGV happens because the -list-thread-groups --available command
> triggers a windows_nat_target::xfer_partial call for a 
> TARGET_OBJECT_OSDATA
> object.  Until a program is being debugged, the target_ops layer that
> gets the call is the Windows "native" layer. Except for a couple of
> specific objects (TARGET_OBJECT_MEMORY and TARGET_OBJECT_LIBRARIES),
> this layer's xfer_partial method delegates the xfer of other objects
> to the target beneath:
> 
>     default:
>       return beneath->xfer_partial (object, annex,
>                                     readbuf, writebuf, offset, len,
>                                     xfered_len);
> 
> Unfortunately, there is no "beneath layer" in this case, so
> beneath is NULL and dereferencing it leads to the SEGV.
> 
> This patch fixes the issue by checking beneath before trying
> to delegate the request. But I am wondering whether this is
> the right place to fix this issue, or whether we should expect
> BENEATH to never be NULL. Ideas?

So when an inferior is started, "-list-thread-groups --available" works 
correctly on Windows?  What is the target beneath then, which provides 
the list of available processes on Windows?

Looking at how it works on Linux, it's the process stratum, 
inf_ptrace_target, that answers this request.  On Windows, shouldn't 
windows_nat_target answer this request?  After all, it's the 
responsibility of windows_nat_target to communicate with the Windows OS 
to debug processes natively on it.

> Also, The testcase I am proposing fails on the -list-thread-groups test
> when run on GNU/Linux because, on that platform, the command returns
> more output than the expect buffer can handle, resulting in an 
> UNRESOLVED
> status. How does one usually handle this? The only why I can think of
> is a loop of gdb_test_multiple... Other ideas?

What's the difference between the new test case and 
gdb.mi/list-thread-groups-available.exp?  In that one too, 
-list-thread-groups --available is executed with no inferior started.  
It also uses mi_gdb_test though, so it probably hits the same 
limitation.

As a quick and dirty hack, is it possible to just increase temporarily 
the size of the buffer to something that will surely be large enough?  
Otherwise, using gdb_test_multiple or maybe gdb_expect to consume the 
output little by little sounds good.

Simon
  
Pedro Alves May 11, 2018, 4:45 p.m. UTC | #2
On 05/10/2018 10:02 PM, Simon Marchi wrote:

> 
> So when an inferior is started, "-list-thread-groups --available" works correctly on Windows?  What is the target beneath then, which provides the list of available processes on Windows?

After the inferior is started, the Windows target is pushed in the target
stack, so there will be a target beneath, either the exec target, or the
dummy target directly.  Either of those returns TARGET_XFER_E_IO for
this target object.  

The issue here is that before the inferior is started, the
Windows target is not pushed on the target stack.
See target_get_osdata.

> which provides the list of available processes on Windows?

I don't think the feature works at all on Windows.  It's probably
returning an empty list of processes.

> 
> Looking at how it works on Linux, it's the process stratum, inf_ptrace_target, that answers this request.  On Windows, shouldn't windows_nat_target answer this request?  After all, it's the responsibility of windows_nat_target to communicate with the Windows OS to debug processes natively on it.

Yeah, it's just that the Windows port doesn't really implement the
feature at all, AFAICT.  Ideally it'd be implemented.  Otherwise,
I guess handling the case of not having a target beneath
here is reasonable.

> 
>> Also, The testcase I am proposing fails on the -list-thread-groups test
>> when run on GNU/Linux because, on that platform, the command returns
>> more output than the expect buffer can handle, resulting in an UNRESOLVED
>> status. How does one usually handle this? The only why I can think of
>> is a loop of gdb_test_multiple... Other ideas?
> 

> What's the difference between the new test case and gdb.mi/list-thread-groups-available.exp?  In that one too, -list-thread-groups --available is executed with no inferior started.  It also uses mi_gdb_test though, so it probably hits the same limitation.

I've actually saw that testcase fail before because of this issue.  :-)
I probably saw it when I was running many test jobs in parallel (thus many
processes) or something like that.

> 
> As a quick and dirty hack, is it possible to just increase temporarily the size of the buffer to something that will surely be large enough?  Otherwise, using gdb_test_multiple or maybe gdb_expect to consume the output little by little sounds good.

Yeah, the best way to address this is to consume
output in chunks, with exp_continue.  That fixes it for good.
See for example:

commit 11859c310cd6b6fd892337a5ee1d36921e6d08d8
Author:     Andrew Burgess <andrew.burgess@embecosm.com>
AuthorDate: Mon Apr 9 00:18:34 2018 +0100

    gdb/testsuite: Handle targets with lots of registers


I'd prefer that over increasing buffer sizes.

Thanks,
Pedro Alves
  
Simon Marchi May 16, 2018, 3:46 p.m. UTC | #3
On 2018-05-11 12:45, Pedro Alves wrote:
> After the inferior is started, the Windows target is pushed in the 
> target
> stack, so there will be a target beneath, either the exec target, or 
> the
> dummy target directly.  Either of those returns TARGET_XFER_E_IO for
> this target object.
> 
> The issue here is that before the inferior is started, the
> Windows target is not pushed on the target stack.
> See target_get_osdata.

Ah ok, so the target is used without being pushed?  I didn't know it was 
possible.

>> which provides the list of available processes on Windows?
> 
> I don't think the feature works at all on Windows.  It's probably
> returning an empty list of processes.

But Joel reported that the test case fails due to the expect buffer 
being full, so the list must not be empty...  we'll need clarifications 
from him :)

>> What's the difference between the new test case and 
>> gdb.mi/list-thread-groups-available.exp?  In that one too, 
>> -list-thread-groups --available is executed with no inferior started.  
>> It also uses mi_gdb_test though, so it probably hits the same 
>> limitation.
> 
> I've actually saw that testcase fail before because of this issue.  :-)
> I probably saw it when I was running many test jobs in parallel (thus 
> many
> processes) or something like that.

It should be fairly easy to reproduce, start a few thousands "sleep 100 
&" processes and then run the test case.

>> As a quick and dirty hack, is it possible to just increase temporarily 
>> the size of the buffer to something that will surely be large enough?  
>> Otherwise, using gdb_test_multiple or maybe gdb_expect to consume the 
>> output little by little sounds good.
> 
> Yeah, the best way to address this is to consume
> output in chunks, with exp_continue.  That fixes it for good.
> See for example:
> 
> commit 11859c310cd6b6fd892337a5ee1d36921e6d08d8
> Author:     Andrew Burgess <andrew.burgess@embecosm.com>
> AuthorDate: Mon Apr 9 00:18:34 2018 +0100
> 
>     gdb/testsuite: Handle targets with lots of registers
> 
> 
> I'd prefer that over increasing buffer sizes.

Of course!

Simon
  
Pedro Alves May 16, 2018, 4:17 p.m. UTC | #4
On 05/16/2018 04:46 PM, Simon Marchi wrote:
> On 2018-05-11 12:45, Pedro Alves wrote:
>> After the inferior is started, the Windows target is pushed in the target
>> stack, so there will be a target beneath, either the exec target, or the
>> dummy target directly.  Either of those returns TARGET_XFER_E_IO for
>> this target object.
>>
>> The issue here is that before the inferior is started, the
>> Windows target is not pushed on the target stack.
>> See target_get_osdata.
> 
> Ah ok, so the target is used without being pushed?  I didn't know it was possible.

Yeah.  A few methods can be called like that.  target_ops::create_inferior
for instance, is called before the target is pushed, and the implementation
of that method is responsible for pushing itself on the stack on success.

> 
>>> which provides the list of available processes on Windows?
>>
>> I don't think the feature works at all on Windows.  It's probably
>> returning an empty list of processes.
> 
> But Joel reported that the test case fails due to the expect buffer being full, so the list must not be empty...  we'll need clarifications from him :)

The reported failure was on GNU/Linux, not Windows.  :-)

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/testsuite/gdb.mi/mi-list-thread-groups-no-inferior.exp b/gdb/testsuite/gdb.mi/mi-list-thread-groups-no-inferior.exp
new file mode 100644
index 0000000..bff6671
--- /dev/null
+++ b/gdb/testsuite/gdb.mi/mi-list-thread-groups-no-inferior.exp
@@ -0,0 +1,32 @@ 
+# Copyright 2018 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+gdb_exit
+if [mi_gdb_start] {
+    continue
+}
+
+mi_gdb_test "-list-thread-groups --available" \
+            ".*^(done|error),.*"
+
+# Verify that GDB is still alive.
+
+mi_gdb_test "-data-evaluate-expression 1" \
+            ".*\\^done,value=\"1\"" \
+            "check GDB is still alive"
+
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 16ebd17..0d3a6cc 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2966,6 +2966,13 @@  windows_nat_target::xfer_partial (enum target_object object,
 					    writebuf, offset, len, xfered_len);
 
     default:
+      if (beneath == NULL)
+	{
+	  /* This can happen when requesting the transfer of unsupported
+	     objects before a program has been started (and therefore
+	     with the current_target having no target beneath).  */
+	  return TARGET_XFER_E_IO;
+	}
       return beneath->xfer_partial (object, annex,
 				    readbuf, writebuf, offset, len,
 				    xfered_len);