This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][profile] Make corrupted-profile.c more robuset
ClosedPublic

Authored by leonardchan on Sep 22 2021, 11:53 AM.

Details

Summary

This test specifically checks that profiles are not mergeable if there's a change in the CounterPtr in the profile header. The test manually changes CounterPtr by explicitly calling memset on some offset into the profile file. This test would fail if binary IDs were emitted because the offset calculation does not take into account the binary ID sizes.

This patch updates the test to use types provided in profile/InstrProfData.inc to make it more resistant to profile layout changes.

Diff Detail

Event Timeline

leonardchan created this revision.Sep 22 2021, 11:53 AM
leonardchan requested review of this revision.Sep 22 2021, 11:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 22 2021, 11:53 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript

does not having the build id change the header size?

does not having the build id change the header size?

No, the header will still contain the section for the build ID size. It's just that the value will be zero.

does not having the build id change the header size?

No, the header will still contain the section for the build ID size. It's just that the value will be zero.

I'm not understanding the description then. Why would the offset calculation be wrong if we're not changing the size of anything?

This test seems quite fragile, I'd prefer modifying it instead to compute the offset. It shouldn't be that difficult. You should be able to #include "profile/InstrProfData.inc" and then cast Buf to __llvm_profile_header and use it to compute the counter offset similarly to what's being done in https://github.com/llvm/llvm-project/blob/6e60bb6883178cf14e6fd47a6789495636e4322f/compiler-rt/lib/profile/InstrProfilingMerge.c#L95.

I'm not understanding the description then. Why would the offset calculation be wrong if we're not changing the size of anything?

Perhaps I should clarify in the description then. So after the header, we will also emit the binary IDs, then emit stuff like the data, names, and counters. If binary IDs are emitted, then there will be some extra data in between the header and the counters, which will shift the CounterPtr offset. Right now, with binary IDs, the calculation should be something like header_size + binary_ids_size + counter_offset. The calculation now is 11 * 8 + 2 * 8 only takes into account the header_size (11*8) and counter_offset (2*8) but not the binary_ids_size. If we don't emit binary ids though (via -Wl,--build-id=none), then binary_ids_size will be zero, making the calculation just header_size + counter_offset which matches what we currently have. The header will still contain the uint64_t referring to the binary ids size. It's just that its value will be zero. The alternative solution is to just explicitly calculate binary_ids_size which is what is suggested below.

This test seems quite fragile, I'd prefer modifying it instead to compute the offset. It shouldn't be that difficult. You should be able to #include "profile/InstrProfData.inc" and then cast Buf to __llvm_profile_header and use it to compute the counter offset similarly to what's being done in https://github.com/llvm/llvm-project/blob/6e60bb6883178cf14e6fd47a6789495636e4322f/compiler-rt/lib/profile/InstrProfilingMerge.c#L95.

If this seems more straightforward, then we can do that. I was just a bit lazy to update the test to account for this.

leonardchan retitled this revision from [compiler-rt][profile] Do not emit binary IDs for corrupted-profile.c test to [compiler-rt][profile] Account for binary IDs size in test.
leonardchan edited the summary of this revision. (Show Details)

I'd prefer including profile/InstrProfData.inc and using types rather than just hardcoding the offsets. In the past, some fields have already moved around (for example between version 6 and 7 binary IDs size has moved). If this happens again, this test would break. Using types should be less error prone and more readable.

I'd prefer including profile/InstrProfData.inc and using types rather than just hardcoding the offsets. In the past, some fields have already moved around (for example between version 6 and 7 binary IDs size has moved). If this happens again, this test would break. Using types should be less error prone and more readable.

that sgtm

leonardchan retitled this revision from [compiler-rt][profile] Account for binary IDs size in test to [compiler-rt][profile] Make corrupted-profile.c more robuset.
leonardchan edited the summary of this revision. (Show Details)
phosek accepted this revision.Sep 23 2021, 4:21 PM

LGTM thanks!

This revision is now accepted and ready to land.Sep 23 2021, 4:21 PM
This revision was landed with ongoing or failed builds.Sep 23 2021, 5:18 PM
This revision was automatically updated to reflect the committed changes.