[llvm] 08ff4dc - [LTO] onfig::addSaveTemps: clear ResolutionFile upon an error

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 2 17:49:11 PST 2020


Author: Fangrui Song
Date: 2020-03-02T17:49:04-08:00
New Revision: 08ff4dc9ad962d0e2db8550deed31d0377e8d2e1

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

LOG: [LTO] onfig::addSaveTemps: clear ResolutionFile upon an error

Otherwise ld.lld -save-temps will crash when writing to ResolutionFile.

llvm-lto2 -save-temps does not crash because it exits immediately.

Reviewed By: evgeny777

Differential Revision: https://reviews.llvm.org/D75426

Added: 
    lld/test/ELF/lto/resolution-err.ll

Modified: 
    llvm/lib/LTO/LTOBackend.cpp

Removed: 
    


################################################################################
diff  --git a/lld/test/ELF/lto/resolution-err.ll b/lld/test/ELF/lto/resolution-err.ll
new file mode 100644
index 000000000000..00cdd94059ac
--- /dev/null
+++ b/lld/test/ELF/lto/resolution-err.ll
@@ -0,0 +1,16 @@
+; UNSUPPORTED: system-windows
+; REQUIRES: shell
+; RUN: llvm-as %s -o %t.bc
+; RUN: touch %t.resolution.txt
+; RUN: chmod -w %t.resolution.txt
+; RUN: not ld.lld -save-temps %t.bc -o %t 2>&1 | FileCheck %s
+; RUN: rm -f %t.resolution.txt
+
+; CHECK: error: {{[Pp]}}ermission denied{{$}}
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @_start() {
+  ret void
+}

diff  --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp
index ec57744cf480..b749909d7871 100644
--- a/llvm/lib/LTO/LTOBackend.cpp
+++ b/llvm/lib/LTO/LTOBackend.cpp
@@ -61,8 +61,10 @@ Error Config::addSaveTemps(std::string OutputFileName,
   std::error_code EC;
   ResolutionFile = std::make_unique<raw_fd_ostream>(
       OutputFileName + "resolution.txt", EC, sys::fs::OpenFlags::OF_Text);
-  if (EC)
+  if (EC) {
+    ResolutionFile.reset();
     return errorCodeToError(EC);
+  }
 
   auto setHook = [&](std::string PathSuffix, ModuleHookFn &Hook) {
     // Keep track of the hook provided by the linker, which also needs to run.


        


More information about the llvm-commits mailing list