Skip to content

Commit 5d4a870

Browse files
author
Luis Machado
committed
Only allow closure lookup by address if there are threads displaced-stepping
Since commit 1e5ccb9, 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 1e5ccb9, 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 <[email protected]>
1 parent 4b2f71e commit 5d4a870

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

gdb/displaced-stepping.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ displaced_step_buffers::copy_insn_closure_by_addr (CORE_ADDR addr)
277277
{
278278
for (const displaced_step_buffer &buffer : m_buffers)
279279
{
280-
if (addr == buffer.addr)
280+
/* Make sure we have active buffers to compare to. */
281+
if (buffer.current_thread != nullptr && addr == buffer.addr)
281282
{
282283
/* The closure information should always be available. */
283284
gdb_assert (buffer.copy_insn_closure.get () != nullptr);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2023 Free Software Foundation, Inc.
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+
int main (int argc, char **argv)
19+
{
20+
return 0;
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2023 Free Software Foundation, Inc.
2+
#
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
#
16+
# This file is part of the gdb testsuite.
17+
#
18+
# Test a displaced stepping closure management bug, where a closure lookup
19+
# by address returns a match even if no displaced stepping is currently
20+
# taking place.
21+
22+
standard_testfile
23+
if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
24+
return -1
25+
}
26+
27+
if ![runto_main] {
28+
return -1
29+
}
30+
31+
# We have a breakpoint at the current pc (from stopping at main). Step over
32+
# the breakpoint.
33+
gdb_test "stepi" ".*" "step-over breakpoint"
34+
35+
# Now attempt to disassemble the entry point function, where the displaced
36+
# stepping buffer is. With the bug, gdb will crash when we attempt to list
37+
# the PC that was used to displaced-step the previous instruction.
38+
gdb_test "disassemble _start" ".*End of assembler dump\." \
39+
"disassemble through displaced-step buffer"

0 commit comments

Comments
 (0)