From 3920850b705ce06945c823219b0b1221cb0ce694 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 21 Mar 2014 20:42:28 +0000 Subject: [PATCH] InstrProf: Actually detect bad headers llvm-svn: 204510 --- include/llvm/ProfileData/InstrProf.h | 1 + lib/ProfileData/InstrProf.cpp | 2 ++ lib/ProfileData/InstrProfReader.cpp | 12 +++++------- .../tools/llvm-profdata/raw-magic-but-no-header.test | 6 ++++++ 4 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 test/tools/llvm-profdata/raw-magic-but-no-header.test diff --git a/include/llvm/ProfileData/InstrProf.h b/include/llvm/ProfileData/InstrProf.h index e2291ef2a9b..d8f3ca6b9d8 100644 --- a/include/llvm/ProfileData/InstrProf.h +++ b/include/llvm/ProfileData/InstrProf.h @@ -27,6 +27,7 @@ struct instrprof_error { success = 0, eof, bad_magic, + bad_header, unsupported_version, too_large, truncated, diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp index ecabdd75bdf..850f61354e3 100644 --- a/lib/ProfileData/InstrProf.cpp +++ b/lib/ProfileData/InstrProf.cpp @@ -29,6 +29,8 @@ class InstrProfErrorCategoryType : public error_category { return "End of File"; case instrprof_error::bad_magic: return "Invalid file format (bad magic)"; + case instrprof_error::bad_header: + return "Invalid header"; case instrprof_error::unsupported_version: return "Unsupported format version"; case instrprof_error::too_large: diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index c563355599f..d2e5fbd9b9a 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -43,8 +43,7 @@ error_code InstrProfReader::create(std::string Path, if (Buffer->getBufferSize() < sizeof(uint64_t)) { Result.reset(new TextInstrProfReader(Buffer)); - Result->readHeader(); - return instrprof_error::success; + return Result->readHeader(); } uint64_t Magic = *(uint64_t *)Buffer->getBufferStart(); @@ -53,8 +52,7 @@ error_code InstrProfReader::create(std::string Path, Result.reset(new RawInstrProfReader(Buffer)); else Result.reset(new TextInstrProfReader(Buffer)); - Result->readHeader(); - return instrprof_error::success; + return Result->readHeader(); } void InstrProfIterator::Increment() { @@ -113,13 +111,13 @@ RawInstrProfReader::RawInstrProfReader(std::unique_ptr &DataBuffer error_code RawInstrProfReader::readHeader() { if (DataBuffer->getBufferSize() < sizeof(RawHeader)) - return error(instrprof_error::malformed); + return error(instrprof_error::bad_header); const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart(); if (Header->Magic == getRawMagic()) ShouldSwapBytes = false; else { if (sys::SwapByteOrder(Header->Magic) != getRawMagic()) - return error(instrprof_error::malformed); + return error(instrprof_error::bad_magic); ShouldSwapBytes = true; } @@ -142,7 +140,7 @@ error_code RawInstrProfReader::readHeader(const RawHeader &Header) { size_t FileSize = NamesOffset + sizeof(char) * NamesSize; if (FileSize != DataBuffer->getBufferSize()) - return error(instrprof_error::malformed); + return error(instrprof_error::bad_header); Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset); DataEnd = Data + DataSize; diff --git a/test/tools/llvm-profdata/raw-magic-but-no-header.test b/test/tools/llvm-profdata/raw-magic-but-no-header.test new file mode 100644 index 00000000000..e899ece2755 --- /dev/null +++ b/test/tools/llvm-profdata/raw-magic-but-no-header.test @@ -0,0 +1,6 @@ +RUN: printf "warforpl" > %t +RUN: not llvm-profdata show %t 2>&1 | FileCheck %s +RUN: printf "lprofraw" > %t +RUN: not llvm-profdata show %t 2>&1 | FileCheck %s + +CHECK: error: {{.+}}: Invalid header