diff --git a/llvm/tools/llvm-profgen/PerfReader.h b/llvm/tools/llvm-profgen/PerfReader.h --- a/llvm/tools/llvm-profgen/PerfReader.h +++ b/llvm/tools/llvm-profgen/PerfReader.h @@ -373,6 +373,7 @@ BranchSample BranchCounter; void recordRangeCount(uint64_t Start, uint64_t End, uint64_t Repeat) { + assert(Start <= End && "Invalid instruction range"); RangeCounter[{Start, End}] += Repeat; } void recordBranchCount(uint64_t Source, uint64_t Target, uint64_t Repeat) { diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -422,8 +422,15 @@ Token.split(Addresses, "/"); uint64_t Src; uint64_t Dst; - Addresses[0].substr(2).getAsInteger(16, Src); - Addresses[1].substr(2).getAsInteger(16, Dst); + + // Stop at broken LBR records. + if (Addresses[0].substr(2).getAsInteger(16, Src) || + Addresses[1].substr(2).getAsInteger(16, Dst)) { + WithColor::warning() << "Invalid address in LBR record at line " + << TraceIt.getLineNumber() << ": " + << TraceIt.getCurrentLine() << "\n"; + break; + } bool SrcIsInternal = Binary->addressIsCode(Src); bool DstIsInternal = Binary->addressIsCode(Dst); @@ -635,11 +642,8 @@ // If this not the first LBR, update the range count between TO of current // LBR and FROM of next LBR. uint64_t StartOffset = TargetOffset; - if (EndOffeset != 0) { - assert(StartOffset <= EndOffeset && - "Bogus range should be filtered ealier!"); + if (EndOffeset != 0) Counter.recordRangeCount(StartOffset, EndOffeset, Repeat); - } EndOffeset = SourceOffset; } } diff --git a/llvm/tools/llvm-profgen/ProfileGenerator.cpp b/llvm/tools/llvm-profgen/ProfileGenerator.cpp --- a/llvm/tools/llvm-profgen/ProfileGenerator.cpp +++ b/llvm/tools/llvm-profgen/ProfileGenerator.cpp @@ -183,6 +183,7 @@ for (auto Item : Ranges) { uint64_t Begin = Item.first.first; uint64_t End = Item.first.second; + assert(Begin <= End && "Invalid instruction range"); uint64_t Count = Item.second; if (Boundaries.find(Begin) == Boundaries.end()) Boundaries[Begin] = BoundaryPoint(); @@ -194,7 +195,7 @@ } uint64_t BeginAddress = UINT64_MAX; - int Count = 0; + uint64_t Count = 0; for (auto Item : Boundaries) { uint64_t Address = Item.first; BoundaryPoint &Point = Item.second; @@ -208,6 +209,7 @@ assert((BeginAddress != UINT64_MAX) && "First boundary point cannot be 'end' point"); DisjointRanges[{BeginAddress, Address}] = Count; + assert(Count >= Point.EndCount && "Mismatched live ranges"); Count -= Point.EndCount; BeginAddress = Address + 1; }