From 38ac7e92d8a489cc24c8bb877db2468cb80b55e2 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Fri, 25 Apr 2014 02:45:33 +0000 Subject: [PATCH] ProfileData: Treat missing function counts as malformed git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207172 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ProfileData/InstrProfReader.cpp | 8 ++++++-- test/tools/llvm-profdata/Inputs/no-counts.profdata | 3 +++ test/tools/llvm-profdata/errors.test | 3 +++ tools/llvm-profdata/llvm-profdata.cpp | 1 + 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/tools/llvm-profdata/Inputs/no-counts.profdata diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index 4f110a57122..12c3c8256be 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -101,6 +101,8 @@ error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { return error(instrprof_error::truncated); if ((Line++)->getAsInteger(10, NumCounters)) return error(instrprof_error::malformed); + if (NumCounters == 0) + return error(instrprof_error::malformed); // Read each counter and fill our internal storage with the values. Counts.clear(); @@ -210,8 +212,10 @@ RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { // Get the raw data. StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize)); - auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), - swap(Data->NumCounters)); + uint32_t NumCounters = swap(Data->NumCounters); + if (NumCounters == 0) + return error(instrprof_error::malformed); + auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr), NumCounters); // Check bounds. auto *NamesStartAsCounter = reinterpret_cast(NamesStart); diff --git a/test/tools/llvm-profdata/Inputs/no-counts.profdata b/test/tools/llvm-profdata/Inputs/no-counts.profdata new file mode 100644 index 00000000000..5c1fa15c086 --- /dev/null +++ b/test/tools/llvm-profdata/Inputs/no-counts.profdata @@ -0,0 +1,3 @@ +no_counts +0 +0 diff --git a/test/tools/llvm-profdata/errors.test b/test/tools/llvm-profdata/errors.test index 393e2d16856..28262efe063 100644 --- a/test/tools/llvm-profdata/errors.test +++ b/test/tools/llvm-profdata/errors.test @@ -11,3 +11,6 @@ INVALID-COUNT-LATER: error: {{.*}}invalid-count-later.profdata: Malformed profil RUN: not llvm-profdata show %p/Inputs/bad-hash.profdata 2>&1 | FileCheck %s --check-prefix=BAD-HASH RUN: not llvm-profdata merge %p/Inputs/bad-hash.profdata %p/Inputs/bad-hash.profdata -o %t.out 2>&1 | FileCheck %s --check-prefix=BAD-HASH BAD-HASH: error: {{.*}}bad-hash.profdata: Malformed profile data + +RUN: not llvm-profdata show %p/Inputs/no-counts.profdata 2>&1 | FileCheck %s --check-prefix=NO-COUNTS +NO-COUNTS: error: {{.*}}no-counts.profdata: Malformed profile data diff --git a/tools/llvm-profdata/llvm-profdata.cpp b/tools/llvm-profdata/llvm-profdata.cpp index 0bd8e834f45..3e58bc205d0 100644 --- a/tools/llvm-profdata/llvm-profdata.cpp +++ b/tools/llvm-profdata/llvm-profdata.cpp @@ -111,6 +111,7 @@ int show_main(int argc, const char *argv[]) { Func.Name.find(ShowFunction) != Func.Name.npos); ++TotalFunctions; + assert(Func.Counts.size() > 0 && "function missing entry counter"); if (Func.Counts[0] > MaxFunctionCount) MaxFunctionCount = Func.Counts[0];