This is an archive of the discontinued LLVM Phabricator instance.

[BOLT] Don't use section relocations when computing hash for data from other section
ClosedPublic

Authored by treapster on Mar 22 2023, 3:28 AM.

Details

Summary

When computing symbol hashes in BinarySection::hash, we try to find relocations in the section which reference the passed BinaryData. We do so by doing lower_bound on data begin offset and upper_bound on data end offset. Since offsets are relative to the current section, if it is a data from the previous section, we get underflow when computing offset and lower_bound returns Relocations.end(). If this data also ends where current section begins, upper_bound on zero offset will return some valid iterator if we have any relocations after the first byte. Then we'll try to iterate from lower_bound to upper_bound, since they're not equal, which in that case means we'll dereference Relocations.end(), increment it, and try to do so until we reach the second valid iterator. Of course we reach segfault earlier. In this patch we stop BOLT from searching relocations for symbols outside of the current section.

Diff Detail

Event Timeline

treapster created this revision.Mar 22 2023, 3:28 AM
Herald added a reviewer: Amir. · View Herald Transcript
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: ayermolo. · View Herald Transcript
treapster requested review of this revision.Mar 22 2023, 3:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 22 2023, 3:28 AM

Makes sense, but why is the BD object being assigned the wrong section?

From what I can tell, in BinaryContext.cpp, we call:

const uint64_t Hash = BD.getSection().hash(BD);

But somehow BD.getSection() is returning the wrong section?

treapster added a comment.EditedMar 23 2023, 11:17 AM

Makes sense, but why is the BD object being assigned the wrong section?

From what I can tell, in BinaryContext.cpp, we call:

const uint64_t Hash = BD.getSection().hash(BD);

But somehow BD.getSection() is returning the wrong section?

It is not assigned wrong section, but external BD may get passed to a recursive call hash(*RelBD, Cache) on line 58

This revision is now accepted and ready to land.Mar 23 2023, 1:50 PM