@@ -204,13 +204,15 @@ TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
204
204
return success ();
205
205
}
206
206
if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1 )
207
- return error (instrprof_error::malformed);
207
+ return error (instrprof_error::malformed,
208
+ " number of value kinds is invalid" );
208
209
Line++;
209
210
210
211
for (uint32_t VK = 0 ; VK < NumValueKinds; VK++) {
211
212
VP_READ_ADVANCE (ValueKind);
212
213
if (ValueKind > IPVK_Last)
213
- return error (instrprof_error::malformed);
214
+ return error (instrprof_error::malformed, " value kind is invalid" );
215
+ ;
214
216
VP_READ_ADVANCE (NumValueSites);
215
217
if (!NumValueSites)
216
218
continue ;
@@ -268,16 +270,18 @@ Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
268
270
if (Line.is_at_end ())
269
271
return error (instrprof_error::truncated);
270
272
if ((Line++)->getAsInteger (0 , Record.Hash ))
271
- return error (instrprof_error::malformed);
273
+ return error (instrprof_error::malformed,
274
+ " function hash is not a valid integer" );
272
275
273
276
// Read the number of counters.
274
277
uint64_t NumCounters;
275
278
if (Line.is_at_end ())
276
279
return error (instrprof_error::truncated);
277
280
if ((Line++)->getAsInteger (10 , NumCounters))
278
- return error (instrprof_error::malformed);
281
+ return error (instrprof_error::malformed,
282
+ " number of counters is not a valid integer" );
279
283
if (NumCounters == 0 )
280
- return error (instrprof_error::malformed);
284
+ return error (instrprof_error::malformed, " number of counters is zero " );
281
285
282
286
// Read each counter and fill our internal storage with the values.
283
287
Record.Clear ();
@@ -287,7 +291,7 @@ Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
287
291
return error (instrprof_error::truncated);
288
292
uint64_t Count;
289
293
if ((Line++)->getAsInteger (10 , Count))
290
- return error (instrprof_error::malformed);
294
+ return error (instrprof_error::malformed, " count is invalid " );
291
295
Record.Counts .push_back (Count);
292
296
}
293
297
@@ -332,10 +336,12 @@ Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
332
336
// If there isn't enough space for another header, this is probably just
333
337
// garbage at the end of the file.
334
338
if (CurrentPos + sizeof (RawInstrProf::Header) > End)
335
- return make_error<InstrProfError>(instrprof_error::malformed);
339
+ return make_error<InstrProfError>(instrprof_error::malformed,
340
+ " not enough space for another header" );
336
341
// The writer ensures each profile is padded to start at an aligned address.
337
342
if (reinterpret_cast <size_t >(CurrentPos) % alignof (uint64_t ))
338
- return make_error<InstrProfError>(instrprof_error::malformed);
343
+ return make_error<InstrProfError>(instrprof_error::malformed,
344
+ " insufficient padding" );
339
345
// The magic should have the same byte order as in the previous header.
340
346
uint64_t Magic = *reinterpret_cast <const uint64_t *>(CurrentPos);
341
347
if (Magic != swap (RawInstrProf::getMagic<IntPtrT>()))
@@ -426,21 +432,39 @@ template <class IntPtrT>
426
432
Error RawInstrProfReader<IntPtrT>::readRawCounts(
427
433
InstrProfRecord &Record) {
428
434
uint32_t NumCounters = swap (Data->NumCounters );
429
- IntPtrT CounterPtr = Data->CounterPtr ;
430
435
if (NumCounters == 0 )
431
- return error (instrprof_error::malformed);
436
+ return error (instrprof_error::malformed, " number of counters is zero " );
432
437
438
+ IntPtrT CounterPtr = Data->CounterPtr ;
433
439
auto *NamesStartAsCounter = reinterpret_cast <const uint64_t *>(NamesStart);
434
440
ptrdiff_t MaxNumCounters = NamesStartAsCounter - CountersStart;
435
441
436
442
// Check bounds. Note that the counter pointer embedded in the data record
437
443
// may itself be corrupt.
438
444
if (MaxNumCounters < 0 || NumCounters > (uint32_t )MaxNumCounters)
439
- return error (instrprof_error::malformed);
445
+ return error (instrprof_error::malformed,
446
+ " counter pointer is out of bounds" );
440
447
ptrdiff_t CounterOffset = getCounterOffset (CounterPtr);
441
- if (CounterOffset < 0 || CounterOffset > MaxNumCounters ||
442
- ((uint32_t )CounterOffset + NumCounters) > (uint32_t )MaxNumCounters)
443
- return error (instrprof_error::malformed);
448
+ if (CounterOffset < 0 )
449
+ return error (
450
+ instrprof_error::malformed,
451
+ (" counter offset(" + Twine (CounterOffset) + " )" + " is < 0" ).str ());
452
+
453
+ if (CounterOffset > MaxNumCounters)
454
+ return error (instrprof_error::malformed,
455
+ (" counter offset(" + Twine (CounterOffset) + " )" + " > " +
456
+ " max number of counters(" + Twine ((uint32_t )MaxNumCounters) +
457
+ " )" )
458
+ .str ());
459
+
460
+ if (((uint32_t )CounterOffset + NumCounters) > (uint32_t )MaxNumCounters)
461
+ return error (instrprof_error::malformed,
462
+ (" number of counters is out of bounds(counter offset(" +
463
+ Twine ((uint32_t )CounterOffset) + " ) + " +
464
+ " number of counters(" + Twine (NumCounters) + " ) > " +
465
+ " max number of counters(" + Twine ((uint32_t )MaxNumCounters) +
466
+ " ))" )
467
+ .str ());
444
468
445
469
auto RawCounts = makeArrayRef (getCounter (CounterOffset), NumCounters);
446
470
@@ -524,7 +548,9 @@ Error RawInstrProfReader<IntPtrT>::printBinaryIds(raw_ostream &OS) {
524
548
// Increment by binary id length data type size.
525
549
BI += sizeof (BinaryIdLen);
526
550
if (BI > (const uint8_t *)DataBuffer->getBufferEnd ())
527
- return make_error<InstrProfError>(instrprof_error::malformed);
551
+ return make_error<InstrProfError>(
552
+ instrprof_error::malformed,
553
+ " binary id that is read is bigger than buffer size" );
528
554
529
555
for (uint64_t I = 0 ; I < BinaryIdLen; I++)
530
556
OS << format (" %02x" , BI[I]);
@@ -624,7 +650,8 @@ Error InstrProfReaderIndex<HashTableImpl>::getRecords(
624
650
625
651
Data = (*Iter);
626
652
if (Data.empty ())
627
- return make_error<InstrProfError>(instrprof_error::malformed);
653
+ return make_error<InstrProfError>(instrprof_error::malformed,
654
+ " profile data is empty" );
628
655
629
656
return Error::success ();
630
657
}
@@ -638,7 +665,8 @@ Error InstrProfReaderIndex<HashTableImpl>::getRecords(
638
665
Data = *RecordIterator;
639
666
640
667
if (Data.empty ())
641
- return make_error<InstrProfError>(instrprof_error::malformed);
668
+ return make_error<InstrProfError>(instrprof_error::malformed,
669
+ " profile data is empty" );
642
670
643
671
return Error::success ();
644
672
}
@@ -669,7 +697,7 @@ class InstrProfReaderNullRemapper : public InstrProfReaderRemapper {
669
697
return Underlying.getRecords (FuncName, Data);
670
698
}
671
699
};
672
- }
700
+ } // namespace
673
701
674
702
// / A remapper that applies remappings based on a symbol remapping file.
675
703
template <typename HashTableImpl>
0 commit comments