llvm-profdata: Avoid undefined behaviour when reading raw profiles
authorJustin Bogner <mail@justinbogner.com>
Fri, 12 Sep 2014 21:22:55 +0000 (21:22 +0000)
committerJustin Bogner <mail@justinbogner.com>
Fri, 12 Sep 2014 21:22:55 +0000 (21:22 +0000)
The raw profiles that are generated in compiler-rt always add padding
so that each profile is aligned, so we can simply treat files that
don't have this property as malformed.

Caught by Alexey's new ubsan bot. Thanks!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217708 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ProfileData/InstrProfReader.cpp
test/tools/llvm-profdata/raw-two-profiles.test

index 5c1993766aacedf4ebb48821057c98e737d9443f..e333473f6f8d228b9face989d6f0888949852b9d 100644 (file)
@@ -190,6 +190,9 @@ RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
   // garbage at the end of the file.
   if (CurrentPos + sizeof(RawHeader) > End)
     return instrprof_error::malformed;
+  // The writer ensures each profile is padded to start at an aligned address.
+  if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
+    return instrprof_error::malformed;
   // The magic should have the same byte order as in the previous header.
   uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
   if (Magic != swap(getRawMagic<IntPtrT>()))
index 3260836ba6667fb705591b1140b7e18744dd8f38..be78793215ed8a7d2d51651c470f8662a6e05d6f 100644 (file)
@@ -39,11 +39,9 @@ RUN: printf '\0\0\0\0\0' >> %t-foo-padded.profraw
 RUN: cat %t-bar.profraw > %t-bar-padded.profraw
 RUN: printf '\0\0\0\0\0' >> %t-bar-padded.profraw
 
-RUN: cat %t-foo.profraw %t-bar.profraw > %t-nopad.profraw
 RUN: cat %t-foo-padded.profraw %t-bar.profraw > %t-pad-between.profraw
 RUN: cat %t-foo-padded.profraw %t-bar-padded.profraw > %t-pad.profraw
 
-RUN: llvm-profdata show %t-nopad.profraw -all-functions -counts | FileCheck %s
 RUN: llvm-profdata show %t-pad-between.profraw -all-functions -counts | FileCheck %s
 RUN: llvm-profdata show %t-pad.profraw -all-functions -counts | FileCheck %s