[lld] b327088 - [ELF] addDependentLibrary: fix a use-after-free bug in archiveName

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 26 14:52:11 PDT 2022


Author: Fangrui Song
Date: 2022-07-26T14:52:07-07:00
New Revision: b3270888cb2f61e9441c59cc81dbeea2bee07584

URL: https://github.com/llvm/llvm-project/commit/b3270888cb2f61e9441c59cc81dbeea2bee07584
DIFF: https://github.com/llvm/llvm-project/commit/b3270888cb2f61e9441c59cc81dbeea2bee07584.diff

LOG: [ELF] addDependentLibrary: fix a use-after-free bug in archiveName

Added: 
    

Modified: 
    lld/ELF/InputFiles.cpp
    lld/test/ELF/print-archive-stats.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index c0076a3722fe..927dc272b532 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -460,9 +460,9 @@ static void addDependentLibrary(StringRef specifier, const InputFile *f) {
   if (!config->dependentLibraries)
     return;
   if (Optional<std::string> s = searchLibraryBaseName(specifier))
-    driver->addFile(*s, /*withLOption=*/true);
+    driver->addFile(saver().save(*s), /*withLOption=*/true);
   else if (Optional<std::string> s = findFromSearchPaths(specifier))
-    driver->addFile(*s, /*withLOption=*/true);
+    driver->addFile(saver().save(*s), /*withLOption=*/true);
   else if (fs::exists(specifier))
     driver->addFile(specifier, /*withLOption=*/false);
   else

diff  --git a/lld/test/ELF/print-archive-stats.s b/lld/test/ELF/print-archive-stats.s
index 61e571e352a5..2dd236f8e0a1 100644
--- a/lld/test/ELF/print-archive-stats.s
+++ b/lld/test/ELF/print-archive-stats.s
@@ -6,19 +6,21 @@
 # RUN: echo '.global foo; foo:' | llvm-mc -filetype=obj -triple=x86_64 - -o 1.o
 # RUN: echo '.global bar; bar:' | llvm-mc -filetype=obj -triple=x86_64 - -o 2.o
 # RUN: echo '.global baz; baz:' | llvm-mc -filetype=obj -triple=x86_64 - -o 3.o
-# RUN: rm -f weak.a && llvm-ar rc weak.a weak.o
-# RUN: rm -f 1.a && llvm-ar rc 1.a 1.o 2.o 3.o
+# RUN: llvm-ar rc weak.a weak.o
+# RUN: llvm-ar rc 1.a 1.o 2.o 3.o
+# RUN: llvm-ar rc lib2.a
 
-# RUN: ld.lld a.o %t/weak.a 1.a --print-archive-stats=a.txt -o /dev/null
+# RUN: ld.lld a.o %t/weak.a 1.a -L. --print-archive-stats=a.txt -o /dev/null
 # RUN: FileCheck --input-file=a.txt -DT=%t %s --match-full-lines --strict-whitespace
 
 ## Fetches 0 member from %t/weak.a and 2 members from %t1.a
 #      CHECK:members	extracted	archive
 # CHECK-NEXT:1	0	[[T]]/weak.a
 # CHECK-NEXT:3	2	1.a
+# CHECK-NEXT:0	0	{{.*}}lib2.a
 
 ## - means stdout.
-# RUN: ld.lld a.o %t/weak.a 1.a --print-archive-stats=- -o /dev/null | 
diff  a.txt -
+# RUN: ld.lld a.o %t/weak.a 1.a -L. --print-archive-stats=- -o /dev/null | 
diff  a.txt -
 
 ## The second 1.a has 0 fetched member.
 # RUN: ld.lld a.o %t/weak.a -L. -l:1.a -l:1.a --print-archive-stats=- -o /dev/null | \
@@ -27,8 +29,9 @@
 # CHECK2-NEXT: 1	0	{{.*}}weak.a
 # CHECK2-NEXT: 3	2	{{.*}}1.a
 # CHECK2-NEXT: 3	0	{{.*}}1.a
+# CHECK2-NEXT: 0	0	{{.*}}lib2.a
 
-# RUN: not ld.lld -shared a.o --print-archive-stats=/ -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
+# RUN: not ld.lld -shared a.o -L. --print-archive-stats=/ -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
 # ERR: error: --print-archive-stats=: cannot open /: {{.*}}
 
 #--- a.s
@@ -38,3 +41,6 @@ _start:
   call foo
   call bar
   call weak
+
+.section ".deplibs","MS", at llvm_dependent_libraries,1
+.asciz "2"


        


More information about the llvm-commits mailing list