public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping
@ 2023-09-29  8:15 Luis Machado
  2023-09-29 17:59 ` Simon Marchi
  2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
  0 siblings, 2 replies; 9+ messages in thread
From: Luis Machado @ 2023-09-29  8:15 UTC (permalink / raw)
  To: gdb-patches

Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
is available whenever we have a match between the provided address argument and
the buffer address.

That is fine, but the report in PR30872 shows this assertion triggering when
it really shouldn't. After some investigation, here's what I found out.

The 32-bit Arm architecture is the only one that calls
gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
32-bit Arm needs to figure out the thumb state of the original instruction
that we displaced-stepped through the displaced-step buffer.

Before the assertion was put in place by commit
1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
getting nullptr back, which meant we were not doing a displaced-stepping
operation.

Now, with the assertion in place, this is running into issues.

It looks like displaced_step_buffers::copy_insn_closure_by_addr is
being used to return a couple different answers depending on the
state we're in:

1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
is supposed to return a valid closure for us, so we can determine the
thumb mode.

2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
should return nullptr to signal that there isn't any displaced-step buffers
in use, because we don't have a valid closure (but we should always have
this).

Since the displaced-step buffers are always allocated, but not always used,
that means the buffers will always contain data. In particular, the buffer
addr field cannot be used to determine if the buffer is active or not.

For instance, we cannot set the buffer addr field to 0x0, as that can be a
valid PC in some cases.

My understanding is that the current_thread field should be a good candidate
to signal that a particular displaced-step buffer is active or not. If it is
nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
it is an active buffer in use by a particular thread.

The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
function so we only attempt to return a closure if the buffer has an assigned
current_thread and if the buffer address matches the address argument.

Alternatively, I think we could use a function to answer the question of
whether we're actively displaced-stepping (so we have an active buffer) or
not.

I've also added a testcase that exercises the problem, restricted to 32-bit
Arm, as that is the only architecture that faces this problem.

Regression-tested on Ubuntu 20.04. OK?

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
---
 gdb/displaced-stepping.c                      |  3 +-
 .../gdb.arch/arm-displaced-step-closure.c     |  5 +++
 .../gdb.arch/arm-displaced-step-closure.exp   | 41 +++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
 create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp

diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index bc59ef01478..41c3c999d1e 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
 {
   for (const displaced_step_buffer &buffer : m_buffers)
     {
-      if (addr == buffer.addr)
+      /* Make sure we have active buffers to compare to.  */
+      if (buffer.current_thread != nullptr && addr == buffer.addr)
       {
 	/* The closure information should always be available. */
 	gdb_assert (buffer.copy_insn_closure.get () != nullptr);
diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
new file mode 100644
index 00000000000..085e682be50
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
@@ -0,0 +1,5 @@
+int main (int argc, char **argv)
+
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
new file mode 100644
index 00000000000..ddac04ebe76
--- /dev/null
+++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
@@ -0,0 +1,41 @@
+# Copyright 2023 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/>.
+#
+# This file is part of the gdb testsuite.
+#
+# Test a displaced stepping closure management bug, where a closure lookup
+# by address returns a match even if no displaced stepping is currently
+# taking place.
+
+require is_aarch32_target
+
+standard_testfile
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+# We have a breakpoint at the current pc (from stopping at main).  Step over
+# the breakpoint.
+gdb_test "stepi" ".*" "step-over breakpoint"
+
+# Now attempt to disassemble the entry point function, where the displaced
+# stepping buffer is.  With the bug, gdb will crash when we attempt to list
+# the PC that was used to displaced-step the previous instruction.
+gdb_test "disassemble _start" ".*End of assembler dump\." \
+	 "disassemble through displaced-step buffer"
-- 
2.25.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping
  2023-09-29  8:15 [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping Luis Machado
@ 2023-09-29 17:59 ` Simon Marchi
  2023-09-30  8:14   ` Luis Machado
  2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
  1 sibling, 1 reply; 9+ messages in thread
From: Simon Marchi @ 2023-09-29 17:59 UTC (permalink / raw)
  To: Luis Machado, gdb-patches

On 9/29/23 04:15, Luis Machado via Gdb-patches wrote:
> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
> is available whenever we have a match between the provided address argument and
> the buffer address.
> 
> That is fine, but the report in PR30872 shows this assertion triggering when
> it really shouldn't. After some investigation, here's what I found out.
> 
> The 32-bit Arm architecture is the only one that calls
> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
> 32-bit Arm needs to figure out the thumb state of the original instruction
> that we displaced-stepped through the displaced-step buffer.
> 
> Before the assertion was put in place by commit
> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
> getting nullptr back, which meant we were not doing a displaced-stepping
> operation.
> 
> Now, with the assertion in place, this is running into issues.
> 
> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
> being used to return a couple different answers depending on the
> state we're in:
> 
> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
> is supposed to return a valid closure for us, so we can determine the
> thumb mode.
> 
> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
> should return nullptr to signal that there isn't any displaced-step buffers
> in use, because we don't have a valid closure (but we should always have
> this).
> 
> Since the displaced-step buffers are always allocated, but not always used,
> that means the buffers will always contain data. In particular, the buffer
> addr field cannot be used to determine if the buffer is active or not.
> 
> For instance, we cannot set the buffer addr field to 0x0, as that can be a
> valid PC in some cases.
> 
> My understanding is that the current_thread field should be a good candidate
> to signal that a particular displaced-step buffer is active or not. If it is
> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
> it is an active buffer in use by a particular thread.
> 
> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
> function so we only attempt to return a closure if the buffer has an assigned
> current_thread and if the buffer address matches the address argument.
> 
> Alternatively, I think we could use a function to answer the question of
> whether we're actively displaced-stepping (so we have an active buffer) or
> not.
> 
> I've also added a testcase that exercises the problem, restricted to 32-bit
> Arm, as that is the only architecture that faces this problem.
> 
> Regression-tested on Ubuntu 20.04. OK?
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
> ---
>  gdb/displaced-stepping.c                      |  3 +-
>  .../gdb.arch/arm-displaced-step-closure.c     |  5 +++
>  .../gdb.arch/arm-displaced-step-closure.exp   | 41 +++++++++++++++++++
>  3 files changed, 48 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
>  create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
> 
> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
> index bc59ef01478..41c3c999d1e 100644
> --- a/gdb/displaced-stepping.c
> +++ b/gdb/displaced-stepping.c
> @@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
>  {
>    for (const displaced_step_buffer &buffer : m_buffers)
>      {
> -      if (addr == buffer.addr)
> +      /* Make sure we have active buffers to compare to.  */
> +      if (buffer.current_thread != nullptr && addr == buffer.addr)
>        {
>  	/* The closure information should always be available. */
>  	gdb_assert (buffer.copy_insn_closure.get () != nullptr);

This code change looks correct to me.  We indeed only have a
copy_insn_closure when the buffer is being used by a thread.

> diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
> new file mode 100644
> index 00000000000..085e682be50
> --- /dev/null
> +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
> @@ -0,0 +1,5 @@
> +int main (int argc, char **argv)
> +
> +{
> +  return 0;
> +}

Missing license, and apply GNU formatting.

> diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
> new file mode 100644
> index 00000000000..ddac04ebe76
> --- /dev/null
> +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
> @@ -0,0 +1,41 @@
> +# Copyright 2023 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/>.
> +#
> +# This file is part of the gdb testsuite.
> +#
> +# Test a displaced stepping closure management bug, where a closure lookup
> +# by address returns a match even if no displaced stepping is currently
> +# taking place.
> +
> +require is_aarch32_target
> +
> +standard_testfile
> +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
> +    return -1
> +}
> +
> +if ![runto_main] {
> +    return -1
> +}
> +
> +# We have a breakpoint at the current pc (from stopping at main).  Step over
> +# the breakpoint.
> +gdb_test "stepi" ".*" "step-over breakpoint"
> +
> +# Now attempt to disassemble the entry point function, where the displaced
> +# stepping buffer is.  With the bug, gdb will crash when we attempt to list
> +# the PC that was used to displaced-step the previous instruction.
> +gdb_test "disassemble _start" ".*End of assembler dump\." \
> +	 "disassemble through displaced-step buffer"

I don't see anything inherently ARM-specific with the test.  Can we have
it as a general test?

Simon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping
  2023-09-29 17:59 ` Simon Marchi
@ 2023-09-30  8:14   ` Luis Machado
  2023-09-30 19:48     ` Simon Marchi
  0 siblings, 1 reply; 9+ messages in thread
From: Luis Machado @ 2023-09-30  8:14 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 9/29/23 18:59, Simon Marchi wrote:
> On 9/29/23 04:15, Luis Machado via Gdb-patches wrote:
>> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
>> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
>> is available whenever we have a match between the provided address argument and
>> the buffer address.
>>
>> That is fine, but the report in PR30872 shows this assertion triggering when
>> it really shouldn't. After some investigation, here's what I found out.
>>
>> The 32-bit Arm architecture is the only one that calls
>> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
>> 32-bit Arm needs to figure out the thumb state of the original instruction
>> that we displaced-stepped through the displaced-step buffer.
>>
>> Before the assertion was put in place by commit
>> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
>> getting nullptr back, which meant we were not doing a displaced-stepping
>> operation.
>>
>> Now, with the assertion in place, this is running into issues.
>>
>> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
>> being used to return a couple different answers depending on the
>> state we're in:
>>
>> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
>> is supposed to return a valid closure for us, so we can determine the
>> thumb mode.
>>
>> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
>> should return nullptr to signal that there isn't any displaced-step buffers
>> in use, because we don't have a valid closure (but we should always have
>> this).
>>
>> Since the displaced-step buffers are always allocated, but not always used,
>> that means the buffers will always contain data. In particular, the buffer
>> addr field cannot be used to determine if the buffer is active or not.
>>
>> For instance, we cannot set the buffer addr field to 0x0, as that can be a
>> valid PC in some cases.
>>
>> My understanding is that the current_thread field should be a good candidate
>> to signal that a particular displaced-step buffer is active or not. If it is
>> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
>> it is an active buffer in use by a particular thread.
>>
>> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
>> function so we only attempt to return a closure if the buffer has an assigned
>> current_thread and if the buffer address matches the address argument.
>>
>> Alternatively, I think we could use a function to answer the question of
>> whether we're actively displaced-stepping (so we have an active buffer) or
>> not.
>>
>> I've also added a testcase that exercises the problem, restricted to 32-bit
>> Arm, as that is the only architecture that faces this problem.
>>
>> Regression-tested on Ubuntu 20.04. OK?
>>
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
>> ---
>>  gdb/displaced-stepping.c                      |  3 +-
>>  .../gdb.arch/arm-displaced-step-closure.c     |  5 +++
>>  .../gdb.arch/arm-displaced-step-closure.exp   | 41 +++++++++++++++++++
>>  3 files changed, 48 insertions(+), 1 deletion(-)
>>  create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
>>  create mode 100644 gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
>>
>> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
>> index bc59ef01478..41c3c999d1e 100644
>> --- a/gdb/displaced-stepping.c
>> +++ b/gdb/displaced-stepping.c
>> @@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
>>  {
>>    for (const displaced_step_buffer &buffer : m_buffers)
>>      {
>> -      if (addr == buffer.addr)
>> +      /* Make sure we have active buffers to compare to.  */
>> +      if (buffer.current_thread != nullptr && addr == buffer.addr)
>>        {
>>  	/* The closure information should always be available. */
>>  	gdb_assert (buffer.copy_insn_closure.get () != nullptr);
> 
> This code change looks correct to me.  We indeed only have a
> copy_insn_closure when the buffer is being used by a thread.
> 
>> diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
>> new file mode 100644
>> index 00000000000..085e682be50
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.c
>> @@ -0,0 +1,5 @@
>> +int main (int argc, char **argv)
>> +
>> +{
>> +  return 0;
>> +}
> 
> Missing license, and apply GNU formatting.
> 

Oops. Fixed locally.

>> diff --git a/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
>> new file mode 100644
>> index 00000000000..ddac04ebe76
>> --- /dev/null
>> +++ b/gdb/testsuite/gdb.arch/arm-displaced-step-closure.exp
>> @@ -0,0 +1,41 @@
>> +# Copyright 2023 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/>.
>> +#
>> +# This file is part of the gdb testsuite.
>> +#
>> +# Test a displaced stepping closure management bug, where a closure lookup
>> +# by address returns a match even if no displaced stepping is currently
>> +# taking place.
>> +
>> +require is_aarch32_target
>> +
>> +standard_testfile
>> +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
>> +    return -1
>> +}
>> +
>> +if ![runto_main] {
>> +    return -1
>> +}
>> +
>> +# We have a breakpoint at the current pc (from stopping at main).  Step over
>> +# the breakpoint.
>> +gdb_test "stepi" ".*" "step-over breakpoint"
>> +
>> +# Now attempt to disassemble the entry point function, where the displaced
>> +# stepping buffer is.  With the bug, gdb will crash when we attempt to list
>> +# the PC that was used to displaced-step the previous instruction.
>> +gdb_test "disassemble _start" ".*End of assembler dump\." \
>> +	 "disassemble through displaced-step buffer"
> 
> I don't see anything inherently ARM-specific with the test.  Can we have
> it as a general test?

We could. Though I don't think it would exercise anything useful until some other
target decides it needs to call gdbarch_displaced_step_copy_insn_closure_by_addr directly
(like Arm does).

For instance, we use disassemble in the testcase to cause the crash because Arm needs to
figure out the thumb state, and we check the situation of the displaced-stepping closure
while at it.

I'm fine either way though.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping
  2023-09-30  8:14   ` Luis Machado
@ 2023-09-30 19:48     ` Simon Marchi
  0 siblings, 0 replies; 9+ messages in thread
From: Simon Marchi @ 2023-09-30 19:48 UTC (permalink / raw)
  To: Luis Machado, gdb-patches


>> I don't see anything inherently ARM-specific with the test.  Can we have
>> it as a general test?
> 
> We could. Though I don't think it would exercise anything useful until some other
> target decides it needs to call gdbarch_displaced_step_copy_insn_closure_by_addr directly
> (like Arm does).
> 
> For instance, we use disassemble in the testcase to cause the crash because Arm needs to
> figure out the thumb state, and we check the situation of the displaced-stepping closure
> while at it.
> 
> I'm fine either way though.

My view is that we lose nothing if we run it for other arches.
And sometimes tests find bugs completely unrelated to what they were
initially written to exercise, so there is a small chance that this test
can be useful for another arch.

Simon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH,v2] Only allow closure lookup by address if there are threads displaced-stepping
  2023-09-29  8:15 [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping Luis Machado
  2023-09-29 17:59 ` Simon Marchi
@ 2023-10-02  6:56 ` Luis Machado
  2023-10-09 14:07   ` [PING] [PATCH, v2] " Luis Machado
                     ` (2 more replies)
  1 sibling, 3 replies; 9+ messages in thread
From: Luis Machado @ 2023-10-02  6:56 UTC (permalink / raw)
  To: gdb-patches, simon.marchi

Updated on v2:

- Added missing license to test file.
- Formatting fixes.
- Made the testcase always run.

Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
is available whenever we have a match between the provided address argument and
the buffer address.

That is fine, but the report in PR30872 shows this assertion triggering when
it really shouldn't. After some investigation, here's what I found out.

The 32-bit Arm architecture is the only one that calls
gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
32-bit Arm needs to figure out the thumb state of the original instruction
that we displaced-stepped through the displaced-step buffer.

Before the assertion was put in place by commit
1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
getting nullptr back, which meant we were not doing a displaced-stepping
operation.

Now, with the assertion in place, this is running into issues.

It looks like displaced_step_buffers::copy_insn_closure_by_addr is
being used to return a couple different answers depending on the
state we're in:

1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
is supposed to return a valid closure for us, so we can determine the
thumb mode.

2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
should return nullptr to signal that there isn't any displaced-step buffers
in use, because we don't have a valid closure (but we should always have
this).

Since the displaced-step buffers are always allocated, but not always used,
that means the buffers will always contain data. In particular, the buffer
addr field cannot be used to determine if the buffer is active or not.

For instance, we cannot set the buffer addr field to 0x0, as that can be a
valid PC in some cases.

My understanding is that the current_thread field should be a good candidate
to signal that a particular displaced-step buffer is active or not. If it is
nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
it is an active buffer in use by a particular thread.

The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
function so we only attempt to return a closure if the buffer has an assigned
current_thread and if the buffer address matches the address argument.

Alternatively, I think we could use a function to answer the question of
whether we're actively displaced-stepping (so we have an active buffer) or
not.

I've also added a testcase that exercises the problem. It should reproduce
reliably on Arm, as that is the only architecture that faces this problem
at the moment.

Regression-tested on Ubuntu 20.04. OK?

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
---
 gdb/displaced-stepping.c                      |  3 +-
 .../gdb.base/displaced-step-closure.c         | 21 ++++++++++
 .../gdb.base/displaced-step-closure.exp       | 39 +++++++++++++++++++
 3 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.c
 create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.exp

diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
index bc59ef01478..41c3c999d1e 100644
--- a/gdb/displaced-stepping.c
+++ b/gdb/displaced-stepping.c
@@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
 {
   for (const displaced_step_buffer &buffer : m_buffers)
     {
-      if (addr == buffer.addr)
+      /* Make sure we have active buffers to compare to.  */
+      if (buffer.current_thread != nullptr && addr == buffer.addr)
       {
 	/* The closure information should always be available. */
 	gdb_assert (buffer.copy_insn_closure.get () != nullptr);
diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.c b/gdb/testsuite/gdb.base/displaced-step-closure.c
new file mode 100644
index 00000000000..8540538e915
--- /dev/null
+++ b/gdb/testsuite/gdb.base/displaced-step-closure.c
@@ -0,0 +1,21 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2023 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/>.  */
+
+int main (int argc, char **argv)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.exp b/gdb/testsuite/gdb.base/displaced-step-closure.exp
new file mode 100644
index 00000000000..3389cd4f0de
--- /dev/null
+++ b/gdb/testsuite/gdb.base/displaced-step-closure.exp
@@ -0,0 +1,39 @@
+# Copyright 2023 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/>.
+#
+# This file is part of the gdb testsuite.
+#
+# Test a displaced stepping closure management bug, where a closure lookup
+# by address returns a match even if no displaced stepping is currently
+# taking place.
+
+standard_testfile
+if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+# We have a breakpoint at the current pc (from stopping at main).  Step over
+# the breakpoint.
+gdb_test "stepi" ".*" "step-over breakpoint"
+
+# Now attempt to disassemble the entry point function, where the displaced
+# stepping buffer is.  With the bug, gdb will crash when we attempt to list
+# the PC that was used to displaced-step the previous instruction.
+gdb_test "disassemble _start" ".*End of assembler dump\." \
+	 "disassemble through displaced-step buffer"
-- 
2.25.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PING] [PATCH, v2] Only allow closure lookup by address if there are threads displaced-stepping
  2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
@ 2023-10-09 14:07   ` Luis Machado
  2023-10-13 17:28   ` [PING][PATCH, " Luis Machado
  2023-10-14 20:46   ` [PATCH,v2] " Simon Marchi
  2 siblings, 0 replies; 9+ messages in thread
From: Luis Machado @ 2023-10-09 14:07 UTC (permalink / raw)
  To: gdb-patches, simon.marchi

On 10/2/23 07:56, Luis Machado via Gdb-patches wrote:
> Updated on v2:
> 
> - Added missing license to test file.
> - Formatting fixes.
> - Made the testcase always run.
> 
> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
> is available whenever we have a match between the provided address argument and
> the buffer address.
> 
> That is fine, but the report in PR30872 shows this assertion triggering when
> it really shouldn't. After some investigation, here's what I found out.
> 
> The 32-bit Arm architecture is the only one that calls
> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
> 32-bit Arm needs to figure out the thumb state of the original instruction
> that we displaced-stepped through the displaced-step buffer.
> 
> Before the assertion was put in place by commit
> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
> getting nullptr back, which meant we were not doing a displaced-stepping
> operation.
> 
> Now, with the assertion in place, this is running into issues.
> 
> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
> being used to return a couple different answers depending on the
> state we're in:
> 
> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
> is supposed to return a valid closure for us, so we can determine the
> thumb mode.
> 
> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
> should return nullptr to signal that there isn't any displaced-step buffers
> in use, because we don't have a valid closure (but we should always have
> this).
> 
> Since the displaced-step buffers are always allocated, but not always used,
> that means the buffers will always contain data. In particular, the buffer
> addr field cannot be used to determine if the buffer is active or not.
> 
> For instance, we cannot set the buffer addr field to 0x0, as that can be a
> valid PC in some cases.
> 
> My understanding is that the current_thread field should be a good candidate
> to signal that a particular displaced-step buffer is active or not. If it is
> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
> it is an active buffer in use by a particular thread.
> 
> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
> function so we only attempt to return a closure if the buffer has an assigned
> current_thread and if the buffer address matches the address argument.
> 
> Alternatively, I think we could use a function to answer the question of
> whether we're actively displaced-stepping (so we have an active buffer) or
> not.
> 
> I've also added a testcase that exercises the problem. It should reproduce
> reliably on Arm, as that is the only architecture that faces this problem
> at the moment.
> 
> Regression-tested on Ubuntu 20.04. OK?
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
> ---
>  gdb/displaced-stepping.c                      |  3 +-
>  .../gdb.base/displaced-step-closure.c         | 21 ++++++++++
>  .../gdb.base/displaced-step-closure.exp       | 39 +++++++++++++++++++
>  3 files changed, 62 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.c
>  create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.exp
> 
> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
> index bc59ef01478..41c3c999d1e 100644
> --- a/gdb/displaced-stepping.c
> +++ b/gdb/displaced-stepping.c
> @@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
>  {
>    for (const displaced_step_buffer &buffer : m_buffers)
>      {
> -      if (addr == buffer.addr)
> +      /* Make sure we have active buffers to compare to.  */
> +      if (buffer.current_thread != nullptr && addr == buffer.addr)
>        {
>  	/* The closure information should always be available. */
>  	gdb_assert (buffer.copy_insn_closure.get () != nullptr);
> diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.c b/gdb/testsuite/gdb.base/displaced-step-closure.c
> new file mode 100644
> index 00000000000..8540538e915
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/displaced-step-closure.c
> @@ -0,0 +1,21 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2023 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/>.  */
> +
> +int main (int argc, char **argv)
> +{
> +  return 0;
> +}
> diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.exp b/gdb/testsuite/gdb.base/displaced-step-closure.exp
> new file mode 100644
> index 00000000000..3389cd4f0de
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/displaced-step-closure.exp
> @@ -0,0 +1,39 @@
> +# Copyright 2023 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/>.
> +#
> +# This file is part of the gdb testsuite.
> +#
> +# Test a displaced stepping closure management bug, where a closure lookup
> +# by address returns a match even if no displaced stepping is currently
> +# taking place.
> +
> +standard_testfile
> +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
> +    return -1
> +}
> +
> +if ![runto_main] {
> +    return -1
> +}
> +
> +# We have a breakpoint at the current pc (from stopping at main).  Step over
> +# the breakpoint.
> +gdb_test "stepi" ".*" "step-over breakpoint"
> +
> +# Now attempt to disassemble the entry point function, where the displaced
> +# stepping buffer is.  With the bug, gdb will crash when we attempt to list
> +# the PC that was used to displaced-step the previous instruction.
> +gdb_test "disassemble _start" ".*End of assembler dump\." \
> +	 "disassemble through displaced-step buffer"


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PING][PATCH, v2] Only allow closure lookup by address if there are threads displaced-stepping
  2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
  2023-10-09 14:07   ` [PING] [PATCH, v2] " Luis Machado
@ 2023-10-13 17:28   ` Luis Machado
  2023-10-14 20:46   ` [PATCH,v2] " Simon Marchi
  2 siblings, 0 replies; 9+ messages in thread
From: Luis Machado @ 2023-10-13 17:28 UTC (permalink / raw)
  To: gdb-patches, simon.marchi

On 10/2/23 07:56, Luis Machado via Gdb-patches wrote:
> Updated on v2:
> 
> - Added missing license to test file.
> - Formatting fixes.
> - Made the testcase always run.
> 
> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
> is available whenever we have a match between the provided address argument and
> the buffer address.
> 
> That is fine, but the report in PR30872 shows this assertion triggering when
> it really shouldn't. After some investigation, here's what I found out.
> 
> The 32-bit Arm architecture is the only one that calls
> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
> 32-bit Arm needs to figure out the thumb state of the original instruction
> that we displaced-stepped through the displaced-step buffer.
> 
> Before the assertion was put in place by commit
> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
> getting nullptr back, which meant we were not doing a displaced-stepping
> operation.
> 
> Now, with the assertion in place, this is running into issues.
> 
> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
> being used to return a couple different answers depending on the
> state we're in:
> 
> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
> is supposed to return a valid closure for us, so we can determine the
> thumb mode.
> 
> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
> should return nullptr to signal that there isn't any displaced-step buffers
> in use, because we don't have a valid closure (but we should always have
> this).
> 
> Since the displaced-step buffers are always allocated, but not always used,
> that means the buffers will always contain data. In particular, the buffer
> addr field cannot be used to determine if the buffer is active or not.
> 
> For instance, we cannot set the buffer addr field to 0x0, as that can be a
> valid PC in some cases.
> 
> My understanding is that the current_thread field should be a good candidate
> to signal that a particular displaced-step buffer is active or not. If it is
> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
> it is an active buffer in use by a particular thread.
> 
> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
> function so we only attempt to return a closure if the buffer has an assigned
> current_thread and if the buffer address matches the address argument.
> 
> Alternatively, I think we could use a function to answer the question of
> whether we're actively displaced-stepping (so we have an active buffer) or
> not.
> 
> I've also added a testcase that exercises the problem. It should reproduce
> reliably on Arm, as that is the only architecture that faces this problem
> at the moment.
> 
> Regression-tested on Ubuntu 20.04. OK?
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
> ---
>  gdb/displaced-stepping.c                      |  3 +-
>  .../gdb.base/displaced-step-closure.c         | 21 ++++++++++
>  .../gdb.base/displaced-step-closure.exp       | 39 +++++++++++++++++++
>  3 files changed, 62 insertions(+), 1 deletion(-)
>  create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.c
>  create mode 100644 gdb/testsuite/gdb.base/displaced-step-closure.exp
> 
> diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c
> index bc59ef01478..41c3c999d1e 100644
> --- a/gdb/displaced-stepping.c
> +++ b/gdb/displaced-stepping.c
> @@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
>  {
>    for (const displaced_step_buffer &buffer : m_buffers)
>      {
> -      if (addr == buffer.addr)
> +      /* Make sure we have active buffers to compare to.  */
> +      if (buffer.current_thread != nullptr && addr == buffer.addr)
>        {
>  	/* The closure information should always be available. */
>  	gdb_assert (buffer.copy_insn_closure.get () != nullptr);
> diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.c b/gdb/testsuite/gdb.base/displaced-step-closure.c
> new file mode 100644
> index 00000000000..8540538e915
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/displaced-step-closure.c
> @@ -0,0 +1,21 @@
> +/* This testcase is part of GDB, the GNU debugger.
> +
> +   Copyright 2023 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/>.  */
> +
> +int main (int argc, char **argv)
> +{
> +  return 0;
> +}
> diff --git a/gdb/testsuite/gdb.base/displaced-step-closure.exp b/gdb/testsuite/gdb.base/displaced-step-closure.exp
> new file mode 100644
> index 00000000000..3389cd4f0de
> --- /dev/null
> +++ b/gdb/testsuite/gdb.base/displaced-step-closure.exp
> @@ -0,0 +1,39 @@
> +# Copyright 2023 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/>.
> +#
> +# This file is part of the gdb testsuite.
> +#
> +# Test a displaced stepping closure management bug, where a closure lookup
> +# by address returns a match even if no displaced stepping is currently
> +# taking place.
> +
> +standard_testfile
> +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
> +    return -1
> +}
> +
> +if ![runto_main] {
> +    return -1
> +}
> +
> +# We have a breakpoint at the current pc (from stopping at main).  Step over
> +# the breakpoint.
> +gdb_test "stepi" ".*" "step-over breakpoint"
> +
> +# Now attempt to disassemble the entry point function, where the displaced
> +# stepping buffer is.  With the bug, gdb will crash when we attempt to list
> +# the PC that was used to displaced-step the previous instruction.
> +gdb_test "disassemble _start" ".*End of assembler dump\." \
> +	 "disassemble through displaced-step buffer"


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH,v2] Only allow closure lookup by address if there are threads displaced-stepping
  2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
  2023-10-09 14:07   ` [PING] [PATCH, v2] " Luis Machado
  2023-10-13 17:28   ` [PING][PATCH, " Luis Machado
@ 2023-10-14 20:46   ` Simon Marchi
  2023-10-16 11:00     ` Luis Machado
  2 siblings, 1 reply; 9+ messages in thread
From: Simon Marchi @ 2023-10-14 20:46 UTC (permalink / raw)
  To: Luis Machado, gdb-patches

On 10/2/23 02:56, Luis Machado wrote:
> Updated on v2:
> 
> - Added missing license to test file.
> - Formatting fixes.
> - Made the testcase always run.
> 
> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
> is available whenever we have a match between the provided address argument and
> the buffer address.
> 
> That is fine, but the report in PR30872 shows this assertion triggering when
> it really shouldn't. After some investigation, here's what I found out.
> 
> The 32-bit Arm architecture is the only one that calls
> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
> 32-bit Arm needs to figure out the thumb state of the original instruction
> that we displaced-stepped through the displaced-step buffer.
> 
> Before the assertion was put in place by commit
> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
> getting nullptr back, which meant we were not doing a displaced-stepping
> operation.
> 
> Now, with the assertion in place, this is running into issues.
> 
> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
> being used to return a couple different answers depending on the
> state we're in:
> 
> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
> is supposed to return a valid closure for us, so we can determine the
> thumb mode.
> 
> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
> should return nullptr to signal that there isn't any displaced-step buffers
> in use, because we don't have a valid closure (but we should always have
> this).
> 
> Since the displaced-step buffers are always allocated, but not always used,
> that means the buffers will always contain data. In particular, the buffer
> addr field cannot be used to determine if the buffer is active or not.
> 
> For instance, we cannot set the buffer addr field to 0x0, as that can be a
> valid PC in some cases.
> 
> My understanding is that the current_thread field should be a good candidate
> to signal that a particular displaced-step buffer is active or not. If it is
> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
> it is an active buffer in use by a particular thread.
> 
> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
> function so we only attempt to return a closure if the buffer has an assigned
> current_thread and if the buffer address matches the address argument.
> 
> Alternatively, I think we could use a function to answer the question of
> whether we're actively displaced-stepping (so we have an active buffer) or
> not.
> 
> I've also added a testcase that exercises the problem. It should reproduce
> reliably on Arm, as that is the only architecture that faces this problem
> at the moment.
> 
> Regression-tested on Ubuntu 20.04. OK?
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Thanks,

Simon

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH,v2] Only allow closure lookup by address if there are threads displaced-stepping
  2023-10-14 20:46   ` [PATCH,v2] " Simon Marchi
@ 2023-10-16 11:00     ` Luis Machado
  0 siblings, 0 replies; 9+ messages in thread
From: Luis Machado @ 2023-10-16 11:00 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

On 10/14/23 21:46, Simon Marchi wrote:
> On 10/2/23 02:56, Luis Machado wrote:
>> Updated on v2:
>>
>> - Added missing license to test file.
>> - Formatting fixes.
>> - Made the testcase always run.
>>
>> Since commit 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, we have an assertion in
>> displaced_step_buffers::copy_insn_closure_by_addr that makes sure a closure
>> is available whenever we have a match between the provided address argument and
>> the buffer address.
>>
>> That is fine, but the report in PR30872 shows this assertion triggering when
>> it really shouldn't. After some investigation, here's what I found out.
>>
>> The 32-bit Arm architecture is the only one that calls
>> gdbarch_displaced_step_copy_insn_closure_by_addr directly, and that's because
>> 32-bit Arm needs to figure out the thumb state of the original instruction
>> that we displaced-stepped through the displaced-step buffer.
>>
>> Before the assertion was put in place by commit
>> 1e5ccb9c5ff4fd8ade4a8694676f99f4abf2d679, there was the possibility of
>> getting nullptr back, which meant we were not doing a displaced-stepping
>> operation.
>>
>> Now, with the assertion in place, this is running into issues.
>>
>> It looks like displaced_step_buffers::copy_insn_closure_by_addr is
>> being used to return a couple different answers depending on the
>> state we're in:
>>
>> 1 - If we are actively displaced-stepping, then copy_insn_closure_by_addr
>> is supposed to return a valid closure for us, so we can determine the
>> thumb mode.
>>
>> 2 - If we are not actively displaced-stepping, then copy_insn_closure_by_addr
>> should return nullptr to signal that there isn't any displaced-step buffers
>> in use, because we don't have a valid closure (but we should always have
>> this).
>>
>> Since the displaced-step buffers are always allocated, but not always used,
>> that means the buffers will always contain data. In particular, the buffer
>> addr field cannot be used to determine if the buffer is active or not.
>>
>> For instance, we cannot set the buffer addr field to 0x0, as that can be a
>> valid PC in some cases.
>>
>> My understanding is that the current_thread field should be a good candidate
>> to signal that a particular displaced-step buffer is active or not. If it is
>> nullptr, we have no threads using that buffer to displaced-step.  Otherwise,
>> it is an active buffer in use by a particular thread.
>>
>> The following fix modifies the displaced_step_buffers::copy_insn_closure_by_addr
>> function so we only attempt to return a closure if the buffer has an assigned
>> current_thread and if the buffer address matches the address argument.
>>
>> Alternatively, I think we could use a function to answer the question of
>> whether we're actively displaced-stepping (so we have an active buffer) or
>> not.
>>
>> I've also added a testcase that exercises the problem. It should reproduce
>> reliably on Arm, as that is the only architecture that faces this problem
>> at the moment.
>>
>> Regression-tested on Ubuntu 20.04. OK?
>>
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30872
> 
> Approved-By: Simon Marchi <simon.marchi@efficios.com>
> 
> Thanks,
> 
> Simon

Thanks Simon. Pushed now to both master and gdb-14-branch.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-10-16 11:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-29  8:15 [PATCH] [gdb/arm] Only allow closure lookup by address if there are threads displaced-stepping Luis Machado
2023-09-29 17:59 ` Simon Marchi
2023-09-30  8:14   ` Luis Machado
2023-09-30 19:48     ` Simon Marchi
2023-10-02  6:56 ` [PATCH,v2] " Luis Machado
2023-10-09 14:07   ` [PING] [PATCH, v2] " Luis Machado
2023-10-13 17:28   ` [PING][PATCH, " Luis Machado
2023-10-14 20:46   ` [PATCH,v2] " Simon Marchi
2023-10-16 11:00     ` Luis Machado

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).