This is an archive of the discontinued LLVM Phabricator instance.

[Object] [COFF] Improve error messages
ClosedPublic

Authored by mstorsjo on Feb 28 2022, 1:36 PM.

Details

Summary

This aids debugging when working with possibly broken files,
instead of just flat out erroring out without telling what's wrong.

Not sure if it's relevant with tests for this?

Diff Detail

Event Timeline

mstorsjo created this revision.Feb 28 2022, 1:36 PM
mstorsjo requested review of this revision.Feb 28 2022, 1:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 28 2022, 1:36 PM
rnk accepted this revision.Feb 28 2022, 3:29 PM

lgtm

If there are existing tests, it would be nice to check for additional context, but I don't think it makes sense to add coverage for every corner case here.

This revision is now accepted and ready to land.Feb 28 2022, 3:29 PM
mstorsjo updated this revision to Diff 412007.Mar 1 2022, 1:28 AM

Update a test that checks an error message.

Thanks for the improvement. Whilst I get what @rnk's saying about testing, I've learnt from past experience that if an error message is not tested, there's a non-trivial chance that the message is garbled in some way, e.g. due to incorrect format strings, or using wrong parameters. I'd personally prefer seeing a test for each case that has the additional new context (including, where possible, using the actual values passed in, rather than regexes), at least if there's already testing for the error path.

llvm/lib/Object/COFFObjectFile.cpp
488

Here and below, do you want the "0x" prefix?

490

No need for else after return.

516

No need for else after return.

llvm/test/tools/llvm-readobj/COFF/tls-directory.test
107

For the filename parameter, I'd consider passing in the filename as a FileCheck variable, although I acknowledge it didn't before, so am not too fussed by this.

For the RVA parameter, if you don't want to hard-code in the value, I'd use a FileCheck numeric variable to ensure that the value is actually an expected number. A possible style is in the inline edit, but you may want a tighter restriction etc.

Thanks for the improvement. Whilst I get what @rnk's saying about testing, I've learnt from past experience that if an error message is not tested, there's a non-trivial chance that the message is garbled in some way, e.g. due to incorrect format strings, or using wrong parameters. I'd personally prefer seeing a test for each case that has the additional new context (including, where possible, using the actual values passed in, rather than regexes), at least if there's already testing for the error path.

While I agree that it would be ideal to have tests for every single case, that’s way out of scope for what I meant to spend effort on here. If that’s required, I’ll just drop this patch - that’s not what I set out to do.

I’m not changing any error handling part, I’m just adding unique strings to all textless cases of object_error::parse_failed in the file, so that if such a message gets printed, it can be mapped back to the code unambiguously. And we happen to have one test that triggers one of the changed messages, so we actually do get the formatting tested.

Some of the errors might even be impossible to trigger, if it’s just an internal consistency check that is guarded by an outer condition too. I’m not planning on investigating if it’s possible to trigger them all, and how to (many of them might require handcrafting broken binaries), I’m just adding messages.

llvm/lib/Object/COFFObjectFile.cpp
488

Sure, that’s probably better.

490

Ok, sure.

llvm/test/tools/llvm-readobj/COFF/tls-directory.test
107

Ok, I guess we can add the literal address, as it’s probably constant.

While I agree that it would be ideal to have tests for every single case, that’s way out of scope for what I meant to spend effort on here. If that’s required, I’ll just drop this patch - that’s not what I set out to do.

I’m not changing any error handling part, I’m just adding unique strings to all textless cases of object_error::parse_failed in the file, so that if such a message gets printed, it can be mapped back to the code unambiguously. And we happen to have one test that triggers one of the changed messages, so we actually do get the formatting tested.

Some of the errors might even be impossible to trigger, if it’s just an internal consistency check that is guarded by an outer condition too. I’m not planning on investigating if it’s possible to trigger them all, and how to (many of them might require handcrafting broken binaries), I’m just adding messages.

Just to be clear, I'm not suggesting adding new tests where they don't exist, only enhancing existing testing to be a little stricter (if there is such testing). If such testing doesn't exist, then what you've done is fine.

llvm/test/tools/llvm-readobj/COFF/tls-directory.test
107

I'm mostly interested in getting rid of the naked wildcard regex, since it could hide dodgy content.

mstorsjo updated this revision to Diff 412182.Mar 1 2022, 11:28 AM

Added an 0x prefix to the RVAs, removed the else statement after a return, checking explicitly for the literal RVA and file name in the test.

Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2022, 12:33 PM
This revision was landed with ongoing or failed builds.Mar 2 2022, 12:45 AM
This revision was automatically updated to reflect the committed changes.