From 824444e97a5a8f75ca913cfa676587721204487f Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Fri, 12 Sep 2014 21:22:55 +0000 Subject: [PATCH] llvm-profdata: Avoid undefined behaviour when reading raw profiles 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 | 3 +++ test/tools/llvm-profdata/raw-two-profiles.test | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index 5c1993766aa..e333473f6f8 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -190,6 +190,9 @@ RawInstrProfReader::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(CurrentPos) % alignOf()) + return instrprof_error::malformed; // The magic should have the same byte order as in the previous header. uint64_t Magic = *reinterpret_cast(CurrentPos); if (Magic != swap(getRawMagic())) diff --git a/test/tools/llvm-profdata/raw-two-profiles.test b/test/tools/llvm-profdata/raw-two-profiles.test index 3260836ba66..be78793215e 100644 --- a/test/tools/llvm-profdata/raw-two-profiles.test +++ b/test/tools/llvm-profdata/raw-two-profiles.test @@ -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