| Issue |
243922
|
| Summary |
[compiler-rt][profile] Padding handling causes Windows Arm64 profile corruption and cross-platform MC/DC merge crashes
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
marcpems
|
## Summary
While investigating PGO failures on Windows Arm64 with Microsoft link.exe - for Rust optimizations - two separate padding-handling defects in the compiler-rt profiling runtime came to light:
1. LLVM's Windows profiling runtime assumes the profile names immediately follow a one-byte section sentinel. In the observed Windows Arm64 binaries, Microsoft LINK places the names payload four bytes after that sentinel's address, leaving three zero padding bytes that the runtime incorrectly includes in the names data. So when producing prof files they end up empty for Arm64.
2. The runtime profile merger ignores `PaddingBytesAfterBitmapBytes` when locating the names and value-profile records. With a nonempty, padded bitmap and value profiling, repeated execution against a shared profile can crash in `lprofMergeValueProfData`.
The second defect is **not Windows-specific**: I reproduced it on native Windows and Linux, on both x64 and Arm64, using standalone C rather than Rust.
## 1. Windows profile names-section padding
### How the padding causes corruption
LLVM's Windows profiling runtime uses ordered COFF subsections to locate the names:
- `.lprfn$A`: the one-byte, zero-valued `NamesStart` sentinel.
- `.lprfn$M`: the compiler-generated, encoded names payload.
- `.lprfn$Z`: the `NamesEnd` sentinel.
The linker combines these into `.lprfn` in subsection-name order. That ordering does not guarantee that the payload immediately follows the one-byte start sentinel.
Nevertheless, in `compiler-rt/lib/profile/InstrProfilingPlatformWindows.c`, `__llvm_profile_begin_names()` returns:
```c
return &NamesStart + 1;
```
This assumes the names payload starts at offset 1 from `NamesStart`.
In the observed LLVM 23.1.4 Arm64 reproducer, the Microsoft link.exe map instead shows:
```text
Offset Contribution
0x00 NamesStart: one zero byte
0x01-0x03 Three linker-inserted zero padding bytes
0x04 Actual names payload in .lprfn$M
```
The map gives `.lprfn$A` a four-byte span and starts `.lprfn$M` at offset 4. However, the input runtime object's `.lprfn$A` contains only one byte, and both that subsection and the instrumented object's `.lprfn$M` specify `IMAGE_SCN_ALIGN_1BYTES`. The extra gap is therefore introduced during linking, rather than requested as four-byte alignment by these input sections.
The runtime starts copying at offset 1, not offset 4, so the raw profile's names region begins with three padding zeros rather than the encoded names. Reading that profile fails with `symbol name is empty`.
### Why this surfaced on Arm64
Although the trigger is Microsoft link's layout for these Arm64 inputs it's not limited to Arm64-only branch in LLVM's names-reading code:
| Configuration | Observed result |
|---|---|
| Windows Arm64, Microsoft link.exe| Names payload starts at `NamesStart + 4`; malformed profile. |
| Windows Arm64, LLD, using the same instrumented object and runtime library | Names payload starts at `NamesStart + 1`; profile is readable. |
| Ordinary Windows x64 control | Profile is readable; the natural Arm64 failure was not reproduced. |
| Windows x64 with the three-byte gap explicitly introduced below | Same malformed-profile failure. |
The Arm64 failure occurs with `/INCREMENTAL:NO` and was reproduced with Microsoft link.exe 14.44 and 14.51. It is not explained by incremental linking being enabled.
This establishes the layout difference and the incorrect runtime assumption, but I haven't got down to the internal Microsoft link behavious that produces the gap. To be clear, I am not drawing the conclusion that the Arm64 ABI requires all COFF subsections to be four-byte aligned, or that link.exe behaviour is incorrect.
I'd suggest the defect is in LLVM's Windows runtime handling of padding; Arm64 with Microsoft link.exe is the configuration that naturally exposed it in this investigation. The runtime fix should not be restricted to Arm64 specifically, but in my experiments it will only apply today for Arm64 symbols.
### Controlled regression reproducer
The following synthetic reproducer explicitly introduces three padding bytes, making the failure deterministic on both Windows x64 and Arm64:
```c
#pragma section(".lprfn$B", read)
__declspec(allocate(".lprfn$B")) const char Padding[3] = {0};
int main(void) { return 0; }
```
Save as `names-padding.c`. build as follows:
```powershell
clang -fprofile-instr-generate names-padding.c -o names-padding.exe '-Wl,/INCREMENTAL:NO'
$env:LLVM_PROFILE_FILE = 'names-padding.profraw'
.\names-padding.exe
llvm-profdata show --counts --function=main names-padding.profraw
```
**Expected:** a readable profile containing `main` with execution count 1.
**Actual:**
```text
malformed instrumentation profile data: symbol name is empty
```
## 2. Bitmap padding ignored during runtime merging
In `compiler-rt/lib/profile/InstrProfilingMerge.c`, the merger calculates:
```c
uintptr_t SrcNameStart = SrcBitmapStart + Header->NumBitmapBytes;
```
The serialized format also contains `Header->PaddingBytesAfterBitmapBytes`. Omitting it shifts the subsequent names/value-profile offsets, so the merger can interpret padding or names bytes as value-profile data. When using lld the padding is always zero, but with link.exe this value is set which highlights the issue on Arm64 platforms where link.exe tends to insert a 3 byte padding.
A reproducer using an actual compiler-generated MC/DC bitmap:
```c
#include <stdio.h>
int left(int x) { return x + 1; }
int right(int x) { return x + 2; }
int decide(int a, int b, int c) { return (a && b) || c; }
int main(int argc, char **argv) {
int (*volatile target)(int) = argc > 1 ? right : left;
int begin = argc > 1 ? 4 : 0;
int sum = 0;
for (int i = begin; i < begin + 4; ++i)
sum += target(i) + decide((i >> 2) & 1, (i >> 1) & 1, i & 1);
printf("%d\n", sum);
return 0;
}
```
Save as `mcdc-values.c`. On Linux, run in a fresh directory:
```sh
clang -O0 -fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc \
-mllvm -enable-value-profiling -no-pie mcdc-values.c -o mcdc-values
LLVM_PROFILE_FILE='shared-%m.profraw' ./mcdc-values
LLVM_PROFILE_FILE='shared-%m.profraw' ./mcdc-values second
```
`-no-pie` keeps indirect-call target addresses stable across executions, matching the existing Linux value-profile merge tests. It avoids conflating this defect with address-randomization limitations in value-profile merging.
**Expected:** both executions succeed and the shared profile contains the combined counters, bitmap, and indirect-call targets.
**Actual:** the second execution crashes. The observed raw profile uses format version 11, with one bitmap byte followed by seven padding bytes.
## Proposed corrections
- Skip leading zero padding between the Windows names sentinel and the names payload, bounded by the section end.
- Include `PaddingBytesAfterBitmapBytes` to honour the offset provided when calculating the names offset in the shared runtime merger.
## Related issue
Related: https://github.com/llvm/llvm-project/issues/39959
Not a dupe, but its related to profiling-runtime assumptions broken by Microsoft linker padding on Windows x64 with incremental linking. Adding a link for completeness
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs