From 5e14916fa6ab8dbafd06f4d3b7b8f9eb3abb767d Mon Sep 17 00:00:00 2001 From: Yashwant Singh Date: Mon, 6 Apr 2026 09:37:05 +0530 Subject: [PATCH] Early exit llvm-bolt when coming across empty data files (#176859) perf2bolt generates empty fdata files for small binaries and right now BOLT does this check while parsing by calling `((!hasBranchData() && !hasMemData()))`. Instead, early exit as soon as the buffer finishes reading the data file and exit with error message. --- bolt/lib/Profile/DataReader.cpp | 12 +++++++++--- bolt/test/empty-fdata-file.test | 12 ++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 bolt/test/empty-fdata-file.test diff --git a/bolt/lib/Profile/DataReader.cpp b/bolt/lib/Profile/DataReader.cpp index 2e0c094b371b..ef4a65bb03b9 100644 --- a/bolt/lib/Profile/DataReader.cpp +++ b/bolt/lib/Profile/DataReader.cpp @@ -315,11 +315,17 @@ std::error_code DataReader::parseInput() { } FileBuf = std::move(MB.get()); ParsingBuf = FileBuf->getBuffer(); + + if (ParsingBuf.empty()) { + Diag << "WARNING: empty profile data file: " << Filename << "\n"; + return make_error_code(llvm::errc::io_error); + } + if (std::error_code EC = parse()) return EC; - if (!ParsingBuf.empty()) - Diag << "WARNING: invalid profile data detected at line " << Line - << ". Possibly corrupted profile.\n"; + + Diag << "WARNING: invalid profile data detected at line " << Line + << ". Possibly corrupted profile.\n"; buildLTONameMaps(); diff --git a/bolt/test/empty-fdata-file.test b/bolt/test/empty-fdata-file.test new file mode 100644 index 000000000000..b7e5f63c9fc9 --- /dev/null +++ b/bolt/test/empty-fdata-file.test @@ -0,0 +1,12 @@ +## Check that llvm-bolt detects bad profile data and aborts + +## This test uses the clang driver without target flags and will only succeed +## on Linux systems where the host triple matches the target. +REQUIRES: system-linux + +RUN: %clang %cflags %S/Inputs/hello.c -o %t +RUN: touch %t.empty.fdata +RUN: not llvm-bolt %t -o %t.bolt --data %t.empty.fdata 2>&1 | FileCheck %s + +CHECK: WARNING: empty profile data file +CHECK-NEXT: BOLT-ERROR: {{.*}}Input/output error