This is an archive of the discontinued LLVM Phabricator instance.

[BOLT][NFC] Report errors from createBinaryContext and RewriteInstance ctor
ClosedPublic

Authored by Amir on Feb 13 2022, 1:12 AM.

Details

Summary

Refactor createBinaryContext and RewriteInstance/MachORewriteInstance
constructors to report an error in a library and fuzzer-friendly way instead of
returning a nullptr or exiting.

Diff Detail

Event Timeline

Amir created this revision.Feb 13 2022, 1:12 AM
Amir requested review of this revision.Feb 13 2022, 1:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 13 2022, 1:12 AM

Instead of having Error as out parameter in constructors, what about refactoring these classes to put this complex constructor logic into a separate "initialize" member function?

Amir added a comment.EditedFeb 14 2022, 11:57 PM

Instead of having Error as out parameter in constructors, what about refactoring these classes to put this complex constructor logic into a separate "initialize" member function?

This approach is sensible and pretty similar, but having an Error as out parameter looks like a more idiomatic solution. It's outlined in the Programmers Manual, the chapter about error handling in fallible constructors: https://llvm.org/docs/ProgrammersManual.html#fallible-constructors

Although I do see the "initialize" method used by COFFObjectFile. To note, even in this class, initialize is then used (only) by create method which wraps both constructor and initialize method:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Object/COFFObjectFile.cpp#L711
So the constructor is always wrapped, similar to what I do in this diff. I don't have a strong preference for fallible constructor over the initialize method, but the latter seems more idiomatic and widespread in the codebase. If you can list one specific reason for initialize method, I'll adjust this diff.

rafauler accepted this revision.Feb 16 2022, 6:24 PM

Sounds good. There is one nit to fix.

bolt/include/bolt/Rewrite/RewriteInstance.h
48–52

Insert a newline after this line to mirror MachORewriteInstance.h

This revision is now accepted and ready to land.Feb 16 2022, 6:24 PM
Amir updated this revision to Diff 409478.Feb 16 2022, 8:41 PM

Added newline

Amir marked an inline comment as done.Feb 16 2022, 8:45 PM
Amir updated this revision to Diff 409490.Feb 16 2022, 10:11 PM

Rebased, fixed build issue with createStringError