Issue 238440
Summary [PGO] llvm-profdata merge aborts on text profiles with reserved/extreme internal counters
Labels new issue
Assignees
Reporter cactusBalll
    **Generated by Fuzzer.** While fuzzing PGO text profiles, `llvm-profdata merge` aborts with an
assertion when a text profile contains an internal counter value greater than
`getInstrMaxCountValue()` (`UINT64_MAX - 2`). `TextInstrProfReader` accepts every `uint64_t`
counter verbatim, and `InstrProfSummaryBuilder::addInternalCount()` later asserts instead of
returning a malformed-profile error.

## Environment

- `llvm-profdata` `24.0.0git`, master commit: `c3ea7c1fc5e9b5ef9c637fb9522a44c21d413a6d`
- build config `+assertions,+asan,+ubsan`, `x86_64-unknown-linux-gnu`

## Minimal reproduction

The input is a syntactically valid text profile with two counters for `foo`; the second
(non-entry) counter is set to `UINT64_MAX`:

```text
# IR level Instrumentation Flag
:ir
foo
# Func Hash:
1
# Num Counters:
2
# Counter Values:
0
18446744073709551615
```

```bash
cat > minimal.proftext <<'EOF'
# IR level Instrumentation Flag
:ir
foo
# Func Hash:
1
# Num Counters:
2
# Counter Values:
0
18446744073709551615
EOF

llvm-profdata merge -o minimal.profdata minimal.proftext
```

Output:

```text
llvm-profdata: llvm/lib/ProfileData/ProfileSummaryBuilder.cpp:246:
void llvm::InstrProfSummaryBuilder::addInternalCount(uint64_t):
Assertion `Count <= getInstrMaxCountValue() &&
"Count value should be less than the max count value."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: llvm-profdata merge -o minimal.profdata minimal.proftext
...
#12 llvm::InstrProfSummaryBuilder::addInternalCount(unsigned long)
#13 llvm::InstrProfSummaryBuilder::addRecord(llvm::InstrProfRecord const&)
#14 llvm::InstrProfRecordWriterTrait::EmitData(...)
#15 llvm::OnDiskChainedHashTableGenerator<...>::Emit(...)
#16 llvm::InstrProfWriter::writeImpl(...)
...
```

Boundary behaviour:

- internal counter `18446744073709551613` (`UINT64_MAX - 2`) → merge succeeds;
- internal counter `18446744073709551614` (`UINT64_MAX - 1`) → aborts with the same assertion;
- internal counter `18446744073709551615` (`UINT64_MAX`) → aborts when it is at index `> 0`.

At index `0`, `UINT64_MAX` / `UINT64_MAX - 1` are treated as the reserved pseudo-hot /
pseudo-warm values and are skipped by the summary builder; that is why the assertion only
triggers for non-entry counters.

## Root cause

`llvm/lib/ProfileData/InstrProfReader.cpp`, `TextInstrProfReader::readNextRecord()`
(lines 438-445), accepts any 64-bit counter:

```cpp
for (uint64_t I = 0; I < NumCounters; ++I) {
  if (Line.is_at_end())
    return error(instrprof_error::truncated);
  uint64_t Count;
  if ((Line++)->getAsInteger(10, Count))
    return error(instrprof_error::malformed, "count is invalid");
  Record.Counts.push_back(Count);
}
```

`llvm/lib/ProfileData/ProfileSummaryBuilder.cpp` (lines 245-246) then asserts:

```cpp
void InstrProfSummaryBuilder::addInternalCount(uint64_t Count) {
  assert(Count <= getInstrMaxCountValue() &&
         "Count value should be less than the max count value.");
```

`llvm/include/llvm/ProfileData/InstrProf.h` reserves the top values:

```cpp
inline uint64_t getInstrMaxCountValue() {
  return std::numeric_limits<uint64_t>::max() - 2;
}
```

`getCountPseudoKind()` only treats `UINT64_MAX` / `UINT64_MAX - 1` specially for `Counts[0]`;
there is no equivalent validation for internal counters.

## Relation to existing issues

- **#65416** asked for `llvm-profdata` to report unreasonable block counts;
- **PR #69513** only added a warning for *raw* profile counters greater than `2^56`;
- the text-profile reader still has no counter-range validation, and `UINT64_MAX - 1` /
  `UINT64_MAX` internal counters still reach the summary-builder assertion.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to