diff options
author | Vedant Kumar <vsk@apple.com> | 2019-09-03 22:23:14 +0000 |
---|---|---|
committer | Vedant Kumar <vsk@apple.com> | 2019-09-03 22:23:14 +0000 |
commit | 95fb23ab37e5e348788bb34623ebdc1e583e1ec8 (patch) | |
tree | ff6f0bd6c6a2813e32036607cd579ea1bc6c9783 /llvm/lib/ProfileData | |
parent | a6fcadd0f037df5e5bdc8d44a1769bf4bc9d22cd (diff) | |
download | bcm5719-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
Diffstat (limited to 'llvm/lib/ProfileData')
-rw-r--r-- | llvm/lib/ProfileData/InstrProfReader.cpp | 14 |
1 files changed, 10 insertions, 4 deletions
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(); |