Be incredibly lenient when parsing ELFs #434
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proof of concept PR to demonstrate an excessive solution that could be used to sidestep problems like the one mentioned in getsentry/sentry-dart#591, where only a portion of an ELF is malformed but the rest of it can be perfectly parsed. The ELF parser has been changed so that malformed ELFs could still be partially parsed out instead of eagerly failing at the first error.
The PR basically copy-pastes nearly everything from goblin's ELF parser, and replaces all short-circuited errors with a macro that returns the ELF object with everything successfully parsed up until the failure. A good second iteration on this would be to merely skip unparseable pieces but not short-circuit on the first failure, allowing us to, for example parse out dynamic symbols even if regular symbol parsing fails.
An additional field as been added to all object types to indicate whether it's malformed or not, in order to provide some feedback in the event that an ELF is malformed and cannot be fully parsed. Ideally this is properly used in other objects.
An ideal alternative to this would probably be to extend goblin's repertoire of
lazy_parse
helpers, following the idea mentioned in m4b/goblin#254 (comment) instead. Following that path would allow us to properly lazily parse the file instead of just copying line-for-line whatElf::parse()
is doing.