binaryninja-api icon indicating copy to clipboard operation
binaryninja-api copied to clipboard

Type Parser system should allow the platform to specify arguments

Open CouleeApps opened this issue 1 year ago • 4 comments

What is the feature you'd like to have? When the clang type parser determines its list of arguments, currently it is only specified by the private internal code, leaving no ability for plugins to include other arguments for clang to enable them to have types parsed correctly. Indeed the only way to actually get the correct triple is by naming your platform exactly the same as what clang expects (or have a hard-coded patch in the core, not exactly accessible).

Is your feature request related to a problem? #4817 and related

Are any alternative solutions acceptable? Could keep hard coding new platforms forever, but that doesn't seem very extensible to me.

Proposed solution Add an additional callback to Platform for changing the arguments. I'm thinking some sort of back-and-forth, where the Parser gives the Platform all of the arguments it plans to use, and the Platform can modify them however it desires and send them back. That way platforms can have full control over what goes into Clang. Signature something like this:

std::vector<std::string> Platform::AdjustTypeParserArguments(Ref<TypeParser> parser, const std::vector<std::string>& existingArguments); // TBD: Pass source too?

And this would be called within the type parser code, right before the compiler invocation is created, roughly at clang_svcs.cpp:1246

CouleeApps avatar Jan 06 '24 18:01 CouleeApps

Dev builds >= 4.1.4923 have a setting analysis.types.parserDefaultArgs which allows users to specify custom arguments used for all type parser operations. Unfortunately for now, this setting is only applied globally and there is not an easy way for it to apply to a specific BinaryView or Project. With a UIContextNotification plugin, this can be worked around, though. Leaving this issue open as it tracks a proper solution, as detailed above.

CouleeApps avatar Mar 05 '24 02:03 CouleeApps

Further investigation suggests the proper solution to this is blocked by Platforms not actually having a callback system. There are a couple solutions to that:

  • Block on #1977 introducing this system
  • Write this system, but only for this one function
  • Use a less powerful solution (platform can specify static string args?)

CouleeApps avatar Mar 12 '24 23:03 CouleeApps

Sounds like the plan for this for now is going to be Block on #1977 since it sounds like progress is being made on that which will enable this soon.

CouleeApps avatar Mar 18 '24 19:03 CouleeApps

An additional solution sketch: Extend clang/lib/Basic/Targets.cpp!AllocateTarget to support a binja-driven custom target and implement the various binja types in that target. It won't completely fix the issue though, since many targets specify unusual base datatypes, like 16-bit int or 32-bit long. or all sorts of extra behavior. See clang::TargetInfo for the full extent of options.

CouleeApps avatar Apr 16 '24 18:04 CouleeApps

This has been implemented as of >= 4.1.5408-dev. Platforms can now implement Platform::AdjustTypeParserInput (or Platform.adjust_type_parser_input in Python) and make adjustments to both the parser arguments and the source files/contents. A sample usage was added to the Windows platforms which fixes the clang target to -pc-windows-msvc if it would have been -unknown-windows-unknown

CouleeApps avatar Jun 04 '24 04:06 CouleeApps