This is an archive of the discontinued LLVM Phabricator instance.

[SamplePGO] Fixing a memory issue when creating profiles on-demand
ClosedPublic

Authored by hoy on Aug 16 2021, 9:23 AM.

Details

Summary

There is a on-dmeand creation of function profile during top-down processing in the sample loader when merging uninlined callees. During the profile creation, a stack string object is used to store a newly-created MD5 name, which is then used by reference as hash key in the profile map. This makes the hash key a dangling reference when later on the stack string object is deallocated.

The issue only happens with md5 profile use and was exposed by context split work for CS profile. I'm making a fix by storing newly created names in the reader.

Diff Detail

Event Timeline

hoy created this revision.Aug 16 2021, 9:23 AM
hoy requested review of this revision.Aug 16 2021, 9:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2021, 9:23 AM
wlei accepted this revision.Aug 16 2021, 10:36 AM

LGTM, thanks for the fix!

This revision is now accepted and ready to land.Aug 16 2021, 10:36 AM

Thanks for the fix. I guess since we didn't really use the key of the map, it was just a hidden bug not causing trouble in the past (no ICE)?

llvm/include/llvm/ProfileData/SampleProfReader.h
411

Can we assert this only happens for MD5?

Additionally, NameBuffer -> MD5NameBuffer to make that explicit?

hoy added a comment.Aug 16 2021, 11:55 AM

Thanks for the fix. I guess since we didn't really use the key of the map, it was just a hidden bug not causing trouble in the past (no ICE)?

We have a test for this currently, but since it is small it doesn't capture the use-after-free case.

llvm/include/llvm/ProfileData/SampleProfReader.h
411

Sounds good.

hoy updated this revision to Diff 366696.Aug 16 2021, 11:56 AM

Addressing Wenlei's comment.

hoy updated this revision to Diff 366699.Aug 16 2021, 11:57 AM

Updating D108142: [SamplePGO] Fixing a memory issue when creating profiles on-demand

wmi added inline comments.Aug 16 2021, 12:24 PM
llvm/include/llvm/ProfileData/SampleProf.h
107–110

Thanks for the improvement.

llvm/include/llvm/ProfileData/SampleProfReader.h
411

Nit:

if (It != Profiles.end())
  return &It->second;
...
516

std::unordered_set is enough?

hoy added inline comments.Aug 16 2021, 12:49 PM
llvm/include/llvm/ProfileData/SampleProfReader.h
411

This looks cleaner.

516

Oops, that's what I meant to use. Good catch. Thanks.

hoy updated this revision to Diff 366720.Aug 16 2021, 12:50 PM

Addresing Wei's comments.

wenlei accepted this revision.Aug 16 2021, 1:01 PM

lgtm, thanks.

wmi accepted this revision.Aug 16 2021, 2:24 PM

LGTM.