]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit - gdb/arm-tdep.c
gdb: select suitable thread for gdbarch_adjust_breakpoint_address
authorAndrew Burgess <aburgess@redhat.com>
Mon, 13 Jun 2022 13:34:01 +0000 (14:34 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 21 Jul 2022 14:19:41 +0000 (15:19 +0100)
commit602707187fa54572a0addd5049d135f20a5b592b
treecdf242f1acc48e13527d2fa3857366ba797cdc18
parent52abb4de08e2ccbfd4dfafd13c200ec7b4db97cc
gdb: select suitable thread for gdbarch_adjust_breakpoint_address

The three targets that implement gdbarch_adjust_breakpoint_address are
arm, frv, and mips.  In each of these targets the adjust breakpoint
address function does some combination of reading the symbol table, or
reading memory at the location the breakpoint could be placed.

The problem is that performing these actions requires that the current
inferior and program space be the one in which the breakpoint will be
placed, and this is not currently always the case.

Consider a GDB session with multiple inferiors.  One inferior might be
a native target while another could be a remote target of a completely
different architecture.  Alternatively, if we consider ARM and
AArch64, one native inferior might be AArch64, while a second native
inferior could be ARM.

In these cases it is possible, and valid, for a user to have one
inferior selected, and place a breakpoint in the other inferior by
placing a breakpoint on a particular symbol.

If this happens, then currently, when
gdbarch_adjust_breakpoint_address is called, the wrong inferior (and
program space) will be selected, and memory reads, and symbol look
ups, will not return the expected results, this could lead to
breakpoints being placed in the wrong location.

There are currently two places where gdbarch_adjust_breakpoint_address
is called:

  1. In infrun.c, in the function handle_step_into_function.  In this
  case, I believe that the correct inferior and program space will
  already be selected as this is called as part of the stop event
  handling, so I don't think we need to worry about this case, and

  2. In breakpoint.c, in the function adjust_breakpoint_address, which
  is itself called from code_breakpoint::add_location and
  watch_command_1.

  The watch_command_1 case I don't think we need to worry about, this
  is for when a local watch expression is created, which can only be
  in the currently selected inferior, so this case should be fine.

  The code_breakpoint::add_location case is the one that needs fixing,
  this is what allows a breakpoint to be created between inferiors.

To fix the code_breakpoint::add_location case, I propose that we pass
the "correct" program_space (i.e. the program space in which the
breakpoint will be created) to the adjust_breakpoint_address function.
Then in adjust_breakpoint_address we can make use of
switch_to_program_space_and_thread to switch program_space and
inferior before calling gdbarch_adjust_breakpoint_address.

I discovered this issue while working on a later patch in this
series.  This later patch will detect when we cast the result of
gdbarch_tdep to the wrong type.

With this later patch in place I ran gdb.multi/multi-arch.exp on an
AArch64 target.  In this situation, two inferiors are created, an
AArch64 inferior, and an ARM inferior.  The test selected the AArch64
inferior and tries to create a breakpoint in the ARM inferior.

As a result of this we end up in arm_adjust_breakpoint_address, which
calls arm_pc_is_thumb.  Before this commit the AArch64 inferior would
be current.  As a result, all of the checks in arm_pc_is_thumb would
fail (they rely on reading symbols from the current program space),
and so, at the end of arm_pc_is_thumb we would call
arm_frame_is_thumb.  However, remember, at this point the current
inferior is the AArch64 inferior, so the current frame is an AArch64
frame.

In arm_frame_is_thumb we call arm_psr_thumb_bit, which calls
gdbarch_tdep and casts the result to arm_gdbarch_tdep.  This is wrong,
the tdep field is of type aarch64_gdbarch_tdep.  After this we have
undefined behaviour.

With this patch in place, we will have switched to a thread in the ARM
program space before calling arm_adjust_breakpoint_address.  As a
result, we now succeed in looking up the required symbols in
arm_pc_is_thumb, and so we never call arm_frame_is_thumb.

However, in the worst case scenario, if we did end up calling
arm_frame_is_thumb, as the current inferior should now be the ARM
inferior, the current frame should be an ARM frame, so we still should
not hit undefined behaviour.

I have added an assert to arm_frame_is_thumb.
gdb/arm-tdep.c
gdb/breakpoint.c