[llvm] r204510 - InstrProf: Actually detect bad headers

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Mar 21 13:42:28 PDT 2014


Author: dexonsmith
Date: Fri Mar 21 15:42:28 2014
New Revision: 204510

URL: http://llvm.org/viewvc/llvm-project?rev=204510&view=rev
Log:
InstrProf: Actually detect bad headers

<rdar://problem/15950346>

Added:
    llvm/trunk/test/tools/llvm-profdata/raw-magic-but-no-header.test
Modified:
    llvm/trunk/include/llvm/ProfileData/InstrProf.h
    llvm/trunk/lib/ProfileData/InstrProf.cpp
    llvm/trunk/lib/ProfileData/InstrProfReader.cpp

Modified: llvm/trunk/include/llvm/ProfileData/InstrProf.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProf.h?rev=204510&r1=204509&r2=204510&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/InstrProf.h (original)
+++ llvm/trunk/include/llvm/ProfileData/InstrProf.h Fri Mar 21 15:42:28 2014
@@ -27,6 +27,7 @@ struct instrprof_error {
     success = 0,
     eof,
     bad_magic,
+    bad_header,
     unsupported_version,
     too_large,
     truncated,

Modified: llvm/trunk/lib/ProfileData/InstrProf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/InstrProf.cpp?rev=204510&r1=204509&r2=204510&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/InstrProf.cpp (original)
+++ llvm/trunk/lib/ProfileData/InstrProf.cpp Fri Mar 21 15:42:28 2014
@@ -29,6 +29,8 @@ class InstrProfErrorCategoryType : publi
       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:

Modified: llvm/trunk/lib/ProfileData/InstrProfReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/InstrProfReader.cpp?rev=204510&r1=204509&r2=204510&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/InstrProfReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/InstrProfReader.cpp Fri Mar 21 15:42:28 2014
@@ -43,8 +43,7 @@ error_code InstrProfReader::create(std::
 
   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::
     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(s
 
 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::readHeade
   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;

Added: llvm/trunk/test/tools/llvm-profdata/raw-magic-but-no-header.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/raw-magic-but-no-header.test?rev=204510&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/raw-magic-but-no-header.test (added)
+++ llvm/trunk/test/tools/llvm-profdata/raw-magic-but-no-header.test Fri Mar 21 15:42:28 2014
@@ -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





More information about the llvm-commits mailing list