This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profgen] Fix an out-of-range error during unwinding
ClosedPublic

Authored by wlei on Sep 22 2021, 10:25 AM.

Details

Summary

It happened that the LBR entry target can be the first address of text section which causes an out-of-range crash. So here add a boundary check.

Diff Detail

Event Timeline

wlei created this revision.Sep 22 2021, 10:25 AM
wlei requested review of this revision.Sep 22 2021, 10:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 22 2021, 10:25 AM
wlei edited the summary of this revision. (Show Details)Sep 22 2021, 10:30 AM
wlei added reviewers: hoy, wenlei.
wlei updated this revision to Diff 374288.Sep 22 2021, 10:35 AM

fix typo

wenlei added inline comments.Sep 22 2021, 10:46 AM
llvm/tools/llvm-profgen/PerfReader.cpp
63–75

Why do we run into such cases? Is the profile corrupted? If that's the case, it may make sense to emit a warning about corrupted profile.

As for the check itself, how about fold it into advance/backward, and let them return a boolean for caller to check? Index is implementation details of InstructionPoint and would be good to avoid using it from outside.

wenlei added inline comments.Sep 22 2021, 10:51 AM
llvm/tools/llvm-profgen/PerfReader.cpp
63–75

Actually Index==0 is still valid, the problem is we cannot "backward" from index 0. However we still want to "recordRangeCount" if this index 0 is the end of the linear range. But the fix would skip "recordRangeCount" in end of range has index 0, which looks incorrect. Or am I missing something?

wlei updated this revision to Diff 374397.Sep 22 2021, 4:40 PM

Updating D110271: [llvm-profgen] Fix an out-of-range error during unwinding

wlei added inline comments.Sep 22 2021, 4:49 PM
llvm/tools/llvm-profgen/PerfReader.cpp
63–75

Good point, Index 0 is missed then. The while loop here did compare the IP and next IP to see if they're in the same inline context or if we reach the end(target). I think the mixed logic is a little confusing. so I separate the logic letting the while loop only for inline check and after the loop we know it's the end and record the remaining range.

BTW, some refactors is done for the address being used in switchToFrame, we can use any of the address in the range, so just use the End.

wenlei added inline comments.Sep 22 2021, 5:08 PM
llvm/tools/llvm-profgen/PerfReader.cpp
81

This is a change beyond refactoring? Before, we use last address of old frame as frame address, but now it's going to be the first address of old frame.

State.switchToFrame(LeafAddr);
->
State.switchToFrame(End);

Or do did you actually meant State.switchToFrame(PrevIP);?

wlei added inline comments.Sep 22 2021, 5:17 PM
llvm/tools/llvm-profgen/PerfReader.cpp
81

It won't affect the result. I think any address in [PrevIP, End] is good for the leaf frame because we will trim the location info of the last frame. Using End is because End is also used out of the loop(just for readability).

hoy accepted this revision.Sep 22 2021, 5:18 PM

lgtm, thanks for the fix.

This revision is now accepted and ready to land.Sep 22 2021, 5:18 PM
wenlei accepted this revision.Sep 22 2021, 5:21 PM

lgtm, thanks.

llvm/tools/llvm-profgen/PerfReader.cpp
81

Ok, so it changes what's used in the frame trie, but as long as we use a consistent address to represent frame, changes there won't affect output, correct?

wlei added inline comments.Sep 22 2021, 5:43 PM
llvm/tools/llvm-profgen/PerfReader.cpp
81

Sorry, my fault, it should be the last address of the old frame, otherwise if the last address is a call, it will be recorded in the wrong callsite. Thanks for correcting me!

wlei added inline comments.Sep 22 2021, 6:18 PM
llvm/tools/llvm-profgen/PerfReader.cpp
81

OK, I double checked the call address won't use the address from linear unwinder, it use the LBR address. So changing to End should be fine. Anyway I choose use the last address which align to the program execution logic.

wlei updated this revision to Diff 374411.Sep 22 2021, 6:20 PM

Updating D110271: [llvm-profgen] Fix an out-of-range error during unwinding

This revision was landed with ongoing or failed builds.Sep 22 2021, 6:34 PM
This revision was automatically updated to reflect the committed changes.