[BOLT] Handle access errors while reading profile

When the user does not have permissions to access the profile, consume
the error contained in Expected<> to avoid dumping stack to the user.

Differential Revision: https://reviews.llvm.org/D139480
This commit is contained in:
Maksim Panchenko 2022-12-06 15:49:35 -08:00
parent f6b1d88527
commit 0f915826cc
2 changed files with 22 additions and 2 deletions

View File

@ -327,15 +327,22 @@ bool DataAggregator::checkPerfDataMagic(StringRef FileName) {
return true;
Expected<sys::fs::file_t> 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<size_t> 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)

View File

@ -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