[0/7] Fix race in DWARF reader

Message ID 20240217-dwarf-race-relocate-v1-0-d3d2d908c1e8@tromey.com
Headers
Series Fix race in DWARF reader |

Message

Tom Tromey Feb. 18, 2024, 1:10 a.m. UTC
  The background DWARF reader turns out to have a few races.  This
series fixes one that occurs when the indexer runs at the same time as
DWARF relocation.

Most of the series is just cleanup / preparation.

The main patch affects MIPS16.  I can't test this -- I tried on a MIPS
machine in the GCC compile farm, but unfortunately the relevant
gdb.arch test says that the processor doesn't support MIPS16.  It's
possible this code is simply dead; I do not know.

Regression tested on x86-64 Fedora 38.

---
Tom Tromey (7):
      Compare section index in lookup_minimal_symbol_by_pc_section
      Remove unnecessary null check in lookup_minimal_symbol_by_pc_section
      Hoist a call to frob_address
      Add unrelocated overload of lookup_minimal_symbol_by_pc_section
      Fix race in background DWARF indexer
      Use bound_minimal_symbol more in lookup_minimal_symbol_by_pc_section
      Fix address comparison in lookup_minimal_symbol_by_pc_section

 gdb/arch-utils.c          |   5 +-
 gdb/arch-utils.h          |   3 +-
 gdb/dwarf2/frame.c        |  13 +-
 gdb/dwarf2/read.c         |  12 +-
 gdb/gdbarch-gen.h         |   4 +-
 gdb/gdbarch.c             |   6 +-
 gdb/gdbarch_components.py |   4 +-
 gdb/minsyms.c             | 449 +++++++++++++++++++++++++---------------------
 gdb/minsyms.h             |   7 +
 gdb/mips-tdep.c           |  49 +++++
 gdb/mips-tdep.h           |   2 +
 11 files changed, 330 insertions(+), 224 deletions(-)
---
base-commit: 989aa9b8e8e340ba65f386cbfd239009a3aba68f
change-id: 20240217-dwarf-race-relocate-287780f3ac82

Best regards,
  

Comments

Tom Tromey April 3, 2024, 3:16 p.m. UTC | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> The background DWARF reader turns out to have a few races.  This
Tom> series fixes one that occurs when the indexer runs at the same time as
Tom> DWARF relocation.

Tom> Most of the series is just cleanup / preparation.

Tom> The main patch affects MIPS16.  I can't test this -- I tried on a MIPS
Tom> machine in the GCC compile farm, but unfortunately the relevant
Tom> gdb.arch test says that the processor doesn't support MIPS16.  It's
Tom> possible this code is simply dead; I do not know.

Elsewhere I mentioned that I had a different idea for this series.

It seems to me that most (or maybe even all) the calls to
dwarf2_per_objfile::adjust aren't really needed.  Many of them only
affect lookup tables, where the adjustment isn't needed.  This includes
all calls made by the indexer.

Some of the calls (like the one in read_attribute_value) even seem to be
wrong.

So, I wrote a short series to remove these.  Unfortunately, though, it's
hard to know for sure if the result is correct, given that I don't know
how to test MIPS16.

I could probably test some simple things ("break") by debugging gdb
while examining (but not running) a MIPS16 program.  I'm not sure if
that's really sufficient though.

I'd appreciate some insight if you have any.

thanks,
Tom
  
John Baldwin April 9, 2024, 6:16 p.m. UTC | #2
On 4/3/24 11:16 AM, Tom Tromey wrote:
>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
> 
> Tom> The background DWARF reader turns out to have a few races.  This
> Tom> series fixes one that occurs when the indexer runs at the same time as
> Tom> DWARF relocation.
> 
> Tom> Most of the series is just cleanup / preparation.
> 
> Tom> The main patch affects MIPS16.  I can't test this -- I tried on a MIPS
> Tom> machine in the GCC compile farm, but unfortunately the relevant
> Tom> gdb.arch test says that the processor doesn't support MIPS16.  It's
> Tom> possible this code is simply dead; I do not know.
> 
> Elsewhere I mentioned that I had a different idea for this series.
> 
> It seems to me that most (or maybe even all) the calls to
> dwarf2_per_objfile::adjust aren't really needed.  Many of them only
> affect lookup tables, where the adjustment isn't needed.  This includes
> all calls made by the indexer.
> 
> Some of the calls (like the one in read_attribute_value) even seem to be
> wrong.
> 
> So, I wrote a short series to remove these.  Unfortunately, though, it's
> hard to know for sure if the result is correct, given that I don't know
> how to test MIPS16.
> 
> I could probably test some simple things ("break") by debugging gdb
> while examining (but not running) a MIPS16 program.  I'm not sure if
> that's really sufficient though.
> 
> I'd appreciate some insight if you have any.

I haven't seen anyone active with submitting MIPS patches in several years.
I no longer make use of MIPS myself (and we've removed it from FreeBSD
entirely, though I know it's still present in Linux distros).  Even when I
was working with MIPS I never tested microMIPS / MIPS16.

OTOH, I think MIPS16 is similar to Thumb on ARM, and it might even be
using a similar trick from reading your series (setting the LSB to enter
"compressed decoding mode" vs "regular decoding mode").  I think ARM uses
special mapping symbols to mark Thumb vs non-Thumb code though instead of
depending on the LSB?  That is, I wonder why Thumb doesn't trip over this
issue the way MIPS16 does?
  
Luis Machado April 9, 2024, 10:11 p.m. UTC | #3
On 4/9/24 19:16, John Baldwin wrote:
> On 4/3/24 11:16 AM, Tom Tromey wrote:
>>>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:
>>
>> Tom> The background DWARF reader turns out to have a few races.  This
>> Tom> series fixes one that occurs when the indexer runs at the same time as
>> Tom> DWARF relocation.
>>
>> Tom> Most of the series is just cleanup / preparation.
>>
>> Tom> The main patch affects MIPS16.  I can't test this -- I tried on a MIPS
>> Tom> machine in the GCC compile farm, but unfortunately the relevant
>> Tom> gdb.arch test says that the processor doesn't support MIPS16.  It's
>> Tom> possible this code is simply dead; I do not know.
>>
>> Elsewhere I mentioned that I had a different idea for this series.
>>
>> It seems to me that most (or maybe even all) the calls to
>> dwarf2_per_objfile::adjust aren't really needed.  Many of them only
>> affect lookup tables, where the adjustment isn't needed.  This includes
>> all calls made by the indexer.
>>
>> Some of the calls (like the one in read_attribute_value) even seem to be
>> wrong.
>>
>> So, I wrote a short series to remove these.  Unfortunately, though, it's
>> hard to know for sure if the result is correct, given that I don't know
>> how to test MIPS16.
>>
>> I could probably test some simple things ("break") by debugging gdb
>> while examining (but not running) a MIPS16 program.  I'm not sure if
>> that's really sufficient though.
>>
>> I'd appreciate some insight if you have any.
> 
> I haven't seen anyone active with submitting MIPS patches in several years.
> I no longer make use of MIPS myself (and we've removed it from FreeBSD
> entirely, though I know it's still present in Linux distros).  Even when I
> was working with MIPS I never tested microMIPS / MIPS16.
> 
> OTOH, I think MIPS16 is similar to Thumb on ARM, and it might even be
> using a similar trick from reading your series (setting the LSB to enter
> "compressed decoding mode" vs "regular decoding mode").  I think ARM uses
> special mapping symbols to mark Thumb vs non-Thumb code though instead of
> depending on the LSB?  That is, I wonder why Thumb doesn't trip over this
> issue the way MIPS16 does?
> 

cc-ing Maciej, who might have a better idea on MIPS bits.
  
Maciej W. Rozycki April 10, 2024, 12:02 a.m. UTC | #4
On Tue, 9 Apr 2024, Luis Machado wrote:

> >> Tom> The main patch affects MIPS16.  I can't test this -- I tried on a MIPS
> >> Tom> machine in the GCC compile farm, but unfortunately the relevant
> >> Tom> gdb.arch test says that the processor doesn't support MIPS16.  It's
> >> Tom> possible this code is simply dead; I do not know.
> >>
> >> Elsewhere I mentioned that I had a different idea for this series.
> >>
> >> It seems to me that most (or maybe even all) the calls to
> >> dwarf2_per_objfile::adjust aren't really needed.  Many of them only
> >> affect lookup tables, where the adjustment isn't needed.  This includes
> >> all calls made by the indexer.
> >>
> >> Some of the calls (like the one in read_attribute_value) even seem to be
> >> wrong.
> >>
> >> So, I wrote a short series to remove these.  Unfortunately, though, it's
> >> hard to know for sure if the result is correct, given that I don't know
> >> how to test MIPS16.
> >>
> >> I could probably test some simple things ("break") by debugging gdb
> >> while examining (but not running) a MIPS16 program.  I'm not sure if
> >> that's really sufficient though.
> >>
> >> I'd appreciate some insight if you have any.
> > 
> > I haven't seen anyone active with submitting MIPS patches in several years.
> > I no longer make use of MIPS myself (and we've removed it from FreeBSD
> > entirely, though I know it's still present in Linux distros).  Even when I
> > was working with MIPS I never tested microMIPS / MIPS16.

 Support for the MIPS target in Linux is certainly far from being dead and 
I believe new MIPS hardware continues being made.  Also a substantial MIPS 
patch for GDB for R6 ISA support is being pinged for review right now.

> > OTOH, I think MIPS16 is similar to Thumb on ARM, and it might even be
> > using a similar trick from reading your series (setting the LSB to enter
> > "compressed decoding mode" vs "regular decoding mode").  I think ARM uses
> > special mapping symbols to mark Thumb vs non-Thumb code though instead of
> > depending on the LSB?  That is, I wonder why Thumb doesn't trip over this
> > issue the way MIPS16 does?
> > 
> 
> cc-ing Maciej, who might have a better idea on MIPS bits.

 Thanks, Luis!

 I can certainly run MIPS16 GDB verification right away with actual 
hardware:

macro@malta(1)~$ uname -a
Linux malta 5.18.0-rc2-00254-gfb649bda6f56-dirty #2 Sat Nov 12 20:14:53 GMT 2022 mips unknown unknown GNU/Linux
macro@malta(2)~$ grep mips16 /proc/cpuinfo
ASEs implemented	: mips16 dsp dsp2
macro@malta(3)~$ 

however to understand the impact I'd have to go through the code changes, 
which I can't guarantee any specific timeframe for.

 Indeed at the machine level it is the LSB of the PC that tells compressed 
and regular code apart: you just flip the bit as required either by using 
special instructions with direct calls/jumps or explicitly with indirect 
ones; it's also correctly set according to the execution mode in effect in 
PC values recorded by hardware, such as the return address for function 
calls or the exception PC for kernel traps.

 Then whether compressed code uses the MIPS16 instruction encoding or the 
microMIPS instruction encoding it is the property of the implementation.

 Offhand I recall for compressed functions DWARF line information has the 
LSB of the PC set according to compressed vs regular encoding, however 
other DWARF records or the static ELF symbol table do not.  Compressed 
function symbols have appropriate flags set in `st_other' to tell them 
apart from regular function symbols.  BFD uses that information to set the 
LSB appropriately in relocation processing where applicable.

 To call a compressed function by hand or for function pointer comparison 
(e.g. against a datum stored in a program's variable or in a CPU register) 
in expression evaluation GDB has to recreate the LSB from information 
available and apply it to symbol values obtained from the symbol table, 
and it's a bit messy due to how things happened in the past.  Conversely, 
in certain contexts the LSB has to be removed instead, such as `x /i $pc'.  
I made all this at least work at one point, not without shortcomings (e.g. 
broken hex instruction dumps in `disassemble /r' output), which is what we 
have now.

 Later on Yao Qi came up with a better proposal building on a generalised 
property of some psABIs where a function pointer is not the function's 
address: <https://sourceware.org/ml/gdb-patches/2016-10/msg00430.html>.  
The proposal got stuck on an issue with the PPC64 target which got never 
resolved due to the shortage of time and higher priority tasks combined: 
<https://sourceware.org/ml/gdb-patches/2017-10/msg00096.html>.

 Maybe someone can pick it up from there?  I could do the necessary MIPS 
bits then myself, I certainly find it important enough to preempt other 
stuff I might otherwise want doing instead.

  Maciej
  
Tom Tromey April 16, 2024, 5:05 p.m. UTC | #5
>>>>> "Maciej" == Maciej W Rozycki <macro@orcam.me.uk> writes:

Maciej>  Support for the MIPS target in Linux is certainly far from being dead and 
Maciej> I believe new MIPS hardware continues being made.  Also a substantial MIPS 
Maciej> patch for GDB for R6 ISA support is being pinged for review right now.

Thanks.  In terms of dead-ness I was really referring just to MIPS16.

Anyway, I'm sending a different & much simpler variant of this fix.
Any testing you could provide on that would be appreciated.

Tom