This is an archive of the discontinued LLVM Phabricator instance.

Avoid using unspecified ordering in MetadataLoader::MetadataLoaderImpl::parseOneMetadata.
ClosedPublic

Authored by krasin on Jan 26 2017, 4:07 PM.

Details

Summary

MetadataLoader::MetadataLoaderImpl::parseOneMetadata uses
the following construct in a number of places:

MetadataList.assignValue(<...>, NextMetadataNo++);

There, NextMetadataNo gets incremented, and since the order
of arguments evaluation is not specified, that can happen
before or after other arguments are evaluated.

In a few cases the other arguments indirectly use NextMetadataNo.
For instance, it's

MetadataList.assignValue(
    GET_OR_DISTINCT(DIModule,
                    (Context, getMDOrNull(Record[1]),
                     getMDString(Record[2]), getMDString(Record[3]),
                     getMDString(Record[4]), getMDString(Record[5]))),
    NextMetadataNo++);

getMDOrNull calls getMD that uses NextMetadataNo:

MetadataList.getMetadataFwdRef(NextMetadataNo);

Therefore, the order of evaluation becomes important. That caused
a very subtle LLD crash that only happens if compiled with GCC or
if LLD is built with LTO. In the case if LLD is compiled with Clang
and regular linking mode, everything worked as intended.

This change extracts incrementing of NextMetadataNo outside of
the arguments list to guarantee the correct order of evaluation.

For the record, this has taken 3 days to track to the origin. It all
started with a ThinLTO bot in Chrome not being able to link a target
if debug info is enabled.

Event Timeline

krasin created this revision.Jan 26 2017, 4:07 PM
krasin edited the summary of this revision. (Show Details)Jan 26 2017, 4:08 PM
pcc edited edge metadata.Jan 26 2017, 4:25 PM

Thanks for helping to track this down!

This looks correct to me, but I'll also ask Mehdi to take a look.

mehdi_amini edited edge metadata.Jan 26 2017, 4:37 PM

Oh I should have thought about it when Peter mentioned that it broke only when built with GCC, this is not the first time I'm bitten by the change in argument evaluation order. Sorry you have to spend 3 days debugging this :(

mehdi_amini accepted this revision.Jan 26 2017, 5:29 PM

LGTM.

@pcc: we should get this in 4.0 I think.

This revision is now accepted and ready to land.Jan 26 2017, 5:29 PM
pcc added a comment.Jan 26 2017, 5:32 PM

Yes, approved for 4.0.

krasin closed this revision.Jan 27 2017, 8:05 AM