[PATCH] D139480: [BOLT] Handle access errors while reading profile

Maksim Panchenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 7 16:38:00 PST 2022


maksfb updated this revision to Diff 481108.
maksfb added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D139480/new/

https://reviews.llvm.org/D139480

Files:
  bolt/lib/Profile/DataAggregator.cpp
  bolt/test/unreadable-profile.test


Index: bolt/test/unreadable-profile.test
===================================================================
--- /dev/null
+++ 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
Index: bolt/lib/Profile/DataAggregator.cpp
===================================================================
--- bolt/lib/Profile/DataAggregator.cpp
+++ bolt/lib/Profile/DataAggregator.cpp
@@ -327,15 +327,22 @@
     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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139480.481108.patch
Type: text/x-patch
Size: 1496 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221208/6e1377cd/attachment.bin>


More information about the llvm-commits mailing list