summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVedant Kumar <vsk@apple.com>2019-09-03 22:23:14 +0000
committerVedant Kumar <vsk@apple.com>2019-09-03 22:23:14 +0000
commit95fb23ab37e5e348788bb34623ebdc1e583e1ec8 (patch)
treeff6f0bd6c6a2813e32036607cd579ea1bc6c9783
parenta6fcadd0f037df5e5bdc8d44a1769bf4bc9d22cd (diff)
downloadbcm5719-llvm-95fb23ab37e5e348788bb34623ebdc1e583e1ec8.tar.gz
bcm5719-llvm-95fb23ab37e5e348788bb34623ebdc1e583e1ec8.zip
[InstrProf] Tighten a check for malformed data records in raw profiles
The check needs to validate a counter offset before performing pointer arithmetic with the (potentially corrupt) offset. Found by UBSan's pointer overflow check. rdar://54843625 Differential Revision: https://reviews.llvm.org/D66979 llvm-svn: 370826
-rw-r--r--llvm/include/llvm/ProfileData/InstrProfReader.h10
-rw-r--r--llvm/lib/ProfileData/InstrProfReader.cpp14
-rw-r--r--llvm/test/tools/llvm-profdata/Inputs/malformed-ptr-to-counter-array.profrawbin0 -> 563560 bytes
-rw-r--r--llvm/test/tools/llvm-profdata/malformed-ptr-to-counter-array.test5
4 files changed, 23 insertions, 6 deletions
diff --git a/llvm/include/llvm/ProfileData/InstrProfReader.h b/llvm/include/llvm/ProfileData/InstrProfReader.h
index 73751faab88..3eb84a835f9 100644
--- a/llvm/include/llvm/ProfileData/InstrProfReader.h
+++ b/llvm/include/llvm/ProfileData/InstrProfReader.h
@@ -268,8 +268,14 @@ private:
return (const char *)ValueDataStart;
}
- const uint64_t *getCounter(IntPtrT CounterPtr) const {
- ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
+ /// Get the offset of \p CounterPtr from the start of the counters section of
+ /// the profile. The offset has units of "number of counters", i.e. increasing
+ /// the offset by 1 corresponds to an increase in the *byte offset* by 8.
+ ptrdiff_t getCounterOffset(IntPtrT CounterPtr) const {
+ return (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
+ }
+
+ const uint64_t *getCounter(ptrdiff_t Offset) const {
return CountersStart + Offset;
}
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index b97601ce172..5fb1d9486c1 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -413,13 +413,19 @@ Error RawInstrProfReader<IntPtrT>::readRawCounts(
if (NumCounters == 0)
return error(instrprof_error::malformed);
- auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
+ ptrdiff_t MaxNumCounters = NamesStartAsCounter - CountersStart;
- // Check bounds.
- if (RawCounts.data() < CountersStart ||
- RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
+ // Check bounds. Note that the counter pointer embedded in the data record
+ // may itself be corrupt.
+ if (NumCounters > MaxNumCounters)
return error(instrprof_error::malformed);
+ ptrdiff_t CounterOffset = getCounterOffset(CounterPtr);
+ if (CounterOffset < 0 || CounterOffset > MaxNumCounters ||
+ (CounterOffset + NumCounters) > MaxNumCounters)
+ return error(instrprof_error::malformed);
+
+ auto RawCounts = makeArrayRef(getCounter(CounterOffset), NumCounters);
if (ShouldSwapBytes) {
Record.Counts.clear();
diff --git a/llvm/test/tools/llvm-profdata/Inputs/malformed-ptr-to-counter-array.profraw b/llvm/test/tools/llvm-profdata/Inputs/malformed-ptr-to-counter-array.profraw
new file mode 100644
index 00000000000..72fddd76fc6
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/Inputs/malformed-ptr-to-counter-array.profraw
Binary files differ
diff --git a/llvm/test/tools/llvm-profdata/malformed-ptr-to-counter-array.test b/llvm/test/tools/llvm-profdata/malformed-ptr-to-counter-array.test
new file mode 100644
index 00000000000..24a68e35fad
--- /dev/null
+++ b/llvm/test/tools/llvm-profdata/malformed-ptr-to-counter-array.test
@@ -0,0 +1,5 @@
+REQUIRES: zlib
+
+RUN: not llvm-profdata merge -o /dev/null %p/Inputs/malformed-ptr-to-counter-array.profraw 2>&1 | FileCheck %s
+
+CHECK: Malformed instrumentation profile data
OpenPOWER on IntegriCloud