diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp --- a/bolt/lib/Profile/DataAggregator.cpp +++ b/bolt/lib/Profile/DataAggregator.cpp @@ -327,15 +327,22 @@ return true; Expected FD = sys::fs::openNativeFileForRead(FileName); - if (!FD) + if (!FD) { + consumeError(FD.takeError()); return false; + } char Buf[7] = {0, 0, 0, 0, 0, 0, 0}; auto Close = make_scope_exit([&] { sys::fs::closeFile(*FD); }); Expected BytesRead = sys::fs::readNativeFileSlice( *FD, makeMutableArrayRef(Buf, sizeof(Buf)), 0); - if (!BytesRead || *BytesRead != 7) + if (!BytesRead) { + consumeError(BytesRead.takeError()); + return false; + } + + if (*BytesRead != 7) return false; if (strncmp(Buf, "PERFILE", 7) == 0) diff --git a/bolt/test/unreadable-profile.test b/bolt/test/unreadable-profile.test new file mode 100644 --- /dev/null +++ b/bolt/test/unreadable-profile.test @@ -0,0 +1,13 @@ +REQUIRES: system-linux + +RUN: touch %t.profile && chmod 000 %t.profile +RUN: %clang %S/Inputs/hello.c -o %t +RUN: not llvm-bolt %t -o %t.bolt --data %t.profile 2>&1 \ +RUN: | FileCheck %s --check-prefix CHECK-NOPERM +RUN: not llvm-bolt %t -o %t.bolt --data %t.fake.profile 2>&1 \ +RUN: | FileCheck %s --check-prefix CHECK-FAKE + +## Check that llvm-bolt gracefully handles errors accessing profile data. + +CHECK-NOPERM: Permission denied +CHECK-FAKE: No such file or directory