This is an archive of the discontinued LLVM Phabricator instance.

Gracefully handle empty .drectve sections
ClosedPublic

Authored by kastiglione on Apr 28 2017, 11:48 AM.

Details

Summary

Running llvm-readobj -coff-directives msvcrt.lib resulted in this error:

Invalid data was encountered while parsing the file

This happened because some of the object files in the archive have empty
.drectve sections. These empty sections result in a parse_failed error being
returned from COFFObjectFile::getSectionContents(), which in turn caused
llvm-readobj to stop. With this change, getSectionContents now returns
success, and like before the resulting array is empty.

Diff Detail

Repository
rL LLVM

Event Timeline

kastiglione created this revision.Apr 28 2017, 11:48 AM

Move size check before name check

smeenai accepted this revision.Apr 28 2017, 11:55 AM

Empty sections causing a parse failure strikes me as slightly odd, but I'm guessing there's a good rationale for it. @compnerd?

This revision is now accepted and ready to land.Apr 28 2017, 11:55 AM
ruiu added a subscriber: ruiu.Apr 28 2017, 12:13 PM

I feel like we should fix COFFObjectFile::getSectionContents to not fail on empty sections instead of avoid calling it on empty sections, as I naturally expect that getSectionContents returns an empty StringRef for an empty section.

In D32652#741088, @ruiu wrote:

I feel like we should fix COFFObjectFile::getSectionContents to not fail on empty sections instead of avoid calling it on empty sections, as I naturally expect that getSectionContents returns an empty StringRef for an empty section.

That was my intuition as well, but I wasn't sure if there was a reason for the existing behavior/callers that depend on that behavior.

majnemer requested changes to this revision.Apr 28 2017, 12:15 PM
majnemer added a subscriber: majnemer.

Testcase?

This revision now requires changes to proceed.Apr 28 2017, 12:15 PM
kastiglione edited edge metadata.

Added a test covering the desired behavior.

kastiglione edited the summary of this revision. (Show Details)May 1 2017, 10:17 PM
ruiu added inline comments.May 1 2017, 10:18 PM
test/Object/coff-empty-drectve.test
3 ↗(On Diff #97389)

You want to add {{$}} at end of this line to make sure that Directive(s): is not followed by any other characters.

4 ↗(On Diff #97389)

Remove trailing empty line.

Constrain FileCheck match with {{$}} and remove extra newline.

kastiglione marked 2 inline comments as done.May 1 2017, 10:31 PM
ruiu accepted this revision.May 1 2017, 10:37 PM

LGTM, but please wait until tomorrow to give other people a chance to take a look.

test/Object/Inputs/COFF/empty-drectve.yaml
1–14 ↗(On Diff #97391)

You can merge this file with coff-empty-drectve.test file.

test/Object/coff-empty-drectve.test
1 ↗(On Diff #97391)

To do that you want to change this to

yaml2obj %s | llvm-readobj ...
3 ↗(On Diff #97391)

And make this line a YAML comment.

please wait until tomorrow

I don't have commit access anyway :)

test/Object/Inputs/COFF/empty-drectve.yaml
1–14 ↗(On Diff #97391)

I was going to write the test this way originally, but the handful of tests I looked at in test/Object mostly all used a separate input file, it seemed like this was the convention. I'm happy to merge these two files.

ruiu added a comment.May 1 2017, 10:47 PM

I do not have a strong preference if that's a local convention here.

@ruiu would you be willing to commit this for me? I'm happy to change the test to be single file too.

This revision was automatically updated to reflect the committed changes.