[PATCH v2] gold: ensure file_counts_lock is initialized before using

Nick Gasson nick.gasson@arm.com
Mon Nov 2 04:02:05 GMT 2020


Since upgrading to binutils 2.35 I've been experiencing random memory
corruption related crashes with ld.gold --threads. It's caused by
multiple threads concurrently pushing elements onto the shared
std::vector in File_read::record_file_read(). This vector is supposed to
be protected by file_counts_lock, but that is initialized lazily and
might be NULL when File_read::open() is called, in which case
Hold_optional_lock silently skips locking it.

Fix by calling the initialize() method before attempting to acquire the
lock, the same as other places that use file_counts_lock.

       PR 26827
       * fileread.cc (File_read::open): Ensure file_counts_lock is
       initialized.
       * testsuite/Makefile.am (check_PROGRAMS): Add a test that passes
       -Wl,--threads.
---
Add a test (basic_threads_test) that crashes with current gold.
Link to PR.

 gold/fileread.cc           | 1 +
 gold/testsuite/Makefile.am | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/gold/fileread.cc b/gold/fileread.cc
index f5ca719360d6..0b5228e2afd4 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -212,6 +212,7 @@ File_read::open(const Task* task, const std::string& name)
       gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
 		 this->name_.c_str());
       this->token_.add_writer(task);
+      file_counts_initialize_lock.initialize();
       Hold_optional_lock hl(file_counts_lock);
       record_file_read(this->name_);
     }
diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index 768caad276ac..3bfba9feb64a 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -508,6 +508,12 @@ basic_pie_test.o: basic_test.cc
 basic_pie_test: basic_pie_test.o gcctestdir/ld
 	$(CXXLINK) -pie basic_pie_test.o
 
+if THREADS
+check_PROGRAMS += basic_threads_test
+basic_threads_test: basic_test.o gcctestdir/ld
+	$(CXXLINK) -Wl,--threads basic_test.o
+endif
+
 check_PROGRAMS += constructor_test
 constructor_test_SOURCES = constructor_test.cc
 constructor_test_DEPENDENCIES = gcctestdir/ld
-- 
2.28.0



More information about the Binutils mailing list