]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
Thread-safety improvements for bfd_check_format_matches
authorTom Tromey <tom@tromey.com>
Tue, 13 Feb 2024 01:06:56 +0000 (18:06 -0700)
committerTom Tromey <tom@tromey.com>
Tue, 16 Apr 2024 20:01:43 +0000 (14:01 -0600)
commitbacc61fd3e6fd61a59fb59bcc657be17a381520d
tree62c1fb63d1e326c4a8f921b7a5a16819626920ef
parent6732c57eeea61d72ad1046fae1dd0d00920150e1
Thread-safety improvements for bfd_check_format_matches

A gdb bug found that bfd_check_format_matches has some data races when
called from multiple threads.

In particular, it changes the BFD error handler, which is a global.
It also has a local static variable ("in_check_format") that is used
for recursion detection.  And, finally, it may emit warnings to the
per-xvec warning array, which is a global.

This patch removes all the races here.

The first part of patch is to change _bfd_error_handler to directly
handle the needs of bfd_check_format_matches.  This way, the error
handler does not need to be changed.

This change lets us use the new per-thread global
(error_handler_messages, replacing error_handler_bfd) to also remove
the need for in_check_format -- a single variable suffices.

Finally, the global per-xvec array is replaced with a new type that
holds the error messages.  The outermost such type is stack-allocated
in bfd_check_format_matches.

I tested this using the binutils test suite.  I also built gdb with
thread sanitizer and ran the test case that was noted as failing.
Finally, Alan sent me the test file that caused the addition of the
xvec warning code in the first place, and I confirmed that "nm-new"
has the same behavior on this file both before and after this patch.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31264
Co-Authored-By: Alan Modra <amodra@gmail.com>
bfd/bfd.c
bfd/format.c
bfd/libbfd.h
bfd/targets.c