From 072c5cd6eee7af9671e6c4fe583e77bc3bcbdd1d Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Tue, 14 Apr 2020 21:25:41 -0400 Subject: Add IL support to linear view --- binaryninjaapi.h | 10 +++++++++ binaryninjacore.h | 17 ++++++++++++++++ function.cpp | 18 +++++++++++++++++ linearviewobject.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++ python/function.py | 15 ++++++++++++++ python/lineardisassembly.py | 42 ++++++++++++++++++++++++++++++++++++++ ui/linearview.h | 9 ++++++++- 7 files changed, 159 insertions(+), 1 deletion(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 9d85f616..c91b13c4 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2962,6 +2962,7 @@ __attribute__ ((format (printf, 1, 2))) void RemoveUserCodeReference(Architecture* fromArch, uint64_t fromAddr, uint64_t toAddr); Ref GetLowLevelIL() const; + Ref GetLowLevelILIfAvailable() const; size_t GetLowLevelILForInstruction(Architecture* arch, uint64_t addr); std::vector GetLowLevelILExitsForInstruction(Architecture* arch, uint64_t addr); RegisterValue GetRegisterValueAtInstruction(Architecture* arch, uint64_t addr, uint32_t reg); @@ -2976,6 +2977,7 @@ __attribute__ ((format (printf, 1, 2))) std::vector GetConstantsReferencedByInstruction(Architecture* arch, uint64_t addr); Ref GetLiftedIL() const; + Ref GetLiftedILIfAvailable() const; size_t GetLiftedILForInstruction(Architecture* arch, uint64_t addr); std::set GetLiftedILFlagUsesForDefinition(size_t i, uint32_t flag); std::set GetLiftedILFlagDefinitionsForUse(size_t i, uint32_t flag); @@ -2983,6 +2985,7 @@ __attribute__ ((format (printf, 1, 2))) std::set GetFlagsWrittenByLiftedILInstruction(size_t i); Ref GetMediumLevelIL() const; + Ref GetMediumLevelILIfAvailable() const; Ref GetType() const; Confidence> GetReturnType() const; @@ -4990,6 +4993,13 @@ __attribute__ ((format (printf, 1, 2))) Ref GetChildForOrderingIndex(uint64_t idx); static Ref CreateDisassembly(BinaryView* view, DisassemblySettings* settings); + static Ref CreateLiftedIL(BinaryView* view, DisassemblySettings* settings); + static Ref CreateLowLevelIL(BinaryView* view, DisassemblySettings* settings); + static Ref CreateLowLevelILSSAForm(BinaryView* view, DisassemblySettings* settings); + static Ref CreateMediumLevelIL(BinaryView* view, DisassemblySettings* settings); + static Ref CreateMediumLevelILSSAForm(BinaryView* view, DisassemblySettings* settings); + static Ref CreateMappedMediumLevelIL(BinaryView* view, DisassemblySettings* settings); + static Ref CreateMappedMediumLevelILSSAForm(BinaryView* view, DisassemblySettings* settings); }; class LinearViewCursor: public CoreRefCountObject Function::GetLowLevelIL() const } +Ref Function::GetLowLevelILIfAvailable() const +{ + return new LowLevelILFunction(BNGetFunctionLowLevelILIfAvailable(m_object)); +} + + size_t Function::GetLowLevelILForInstruction(Architecture* arch, uint64_t addr) { return BNGetLowLevelILForInstruction(m_object, arch->GetObject(), addr); @@ -467,6 +473,12 @@ Ref Function::GetLiftedIL() const } +Ref Function::GetLiftedILIfAvailable() const +{ + return new LowLevelILFunction(BNGetFunctionLiftedILIfAvailable(m_object)); +} + + size_t Function::GetLiftedILForInstruction(Architecture* arch, uint64_t addr) { return BNGetLiftedILForInstruction(m_object, arch->GetObject(), addr); @@ -527,6 +539,12 @@ Ref Function::GetMediumLevelIL() const } +Ref Function::GetMediumLevelILIfAvailable() const +{ + return new MediumLevelILFunction(BNGetFunctionMediumLevelILIfAvailable(m_object)); +} + + Ref Function::GetType() const { return new Type(BNGetFunctionType(m_object)); diff --git a/linearviewobject.cpp b/linearviewobject.cpp index 3cd8f360..19c7e4b3 100644 --- a/linearviewobject.cpp +++ b/linearviewobject.cpp @@ -204,3 +204,52 @@ Ref LinearViewObject::CreateDisassembly(BinaryView* view, Disa return new LinearViewObject(BNCreateLinearViewDisassembly(view->GetObject(), settings ? settings->GetObject() : nullptr)); } + + +Ref LinearViewObject::CreateLiftedIL(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewLiftedIL(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateLowLevelIL(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewLowLevelIL(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateLowLevelILSSAForm(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewLowLevelILSSAForm(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateMediumLevelIL(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewMediumLevelIL(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateMediumLevelILSSAForm(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewMediumLevelILSSAForm(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateMappedMediumLevelIL(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewMappedMediumLevelIL(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} + + +Ref LinearViewObject::CreateMappedMediumLevelILSSAForm(BinaryView* view, DisassemblySettings* settings) +{ + return new LinearViewObject(BNCreateLinearViewMappedMediumLevelILSSAForm(view->GetObject(), + settings ? settings->GetObject() : nullptr)); +} diff --git a/python/function.py b/python/function.py index bdd04590..7cee467c 100644 --- a/python/function.py +++ b/python/function.py @@ -1214,11 +1214,21 @@ class Function(object): """returns LowLevelILFunction used to represent Function low level IL (read-only)""" return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLowLevelIL(self.handle), self) + @property + def llil_if_available(self): + """returns LowLevelILFunction used to represent Function low level IL, or None if not loaded (read-only)""" + return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLowLevelILIfAvailable(self.handle), self) + @property def lifted_il(self): """returns LowLevelILFunction used to represent lifted IL (read-only)""" return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLiftedIL(self.handle), self) + @property + def lifted_il_if_available(self): + """returns LowLevelILFunction used to represent lifted IL, or None if not loaded (read-only)""" + return binaryninja.lowlevelil.LowLevelILFunction(self.arch, core.BNGetFunctionLiftedILIfAvailable(self.handle), self) + @property def medium_level_il(self): """Deprecated property provided for compatibility. Use mlil instead.""" @@ -1229,6 +1239,11 @@ class Function(object): """Function medium level IL (read-only)""" return binaryninja.mediumlevelil.MediumLevelILFunction(self.arch, core.BNGetFunctionMediumLevelIL(self.handle), self) + @property + def mlil_if_available(self): + """Function medium level IL, or None if not loaded (read-only)""" + return binaryninja.mediumlevelil.MediumLevelILFunction(self.arch, core.BNGetFunctionMediumLevelILIfAvailable(self.handle), self) + @property def function_type(self): """Function type object, can be set with either a string representing the function prototype (`str(function)` shows examples) or a :py:class:`Type` object""" diff --git a/python/lineardisassembly.py b/python/lineardisassembly.py index a23c829f..ec213a19 100644 --- a/python/lineardisassembly.py +++ b/python/lineardisassembly.py @@ -258,6 +258,48 @@ class LinearViewObject(object): settings = settings.handle return LinearViewObject(core.BNCreateLinearViewDisassembly(view.handle, settings)) + @classmethod + def lifted_il(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewLiftedIL(view.handle, settings)) + + @classmethod + def llil(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewLowLevelIL(view.handle, settings)) + + @classmethod + def llil_ssa_form(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewLowLevelILSSAForm(view.handle, settings)) + + @classmethod + def mlil(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewMediumLevelIL(view.handle, settings)) + + @classmethod + def mlil_ssa_form(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewMediumLevelILSSAForm(view.handle, settings)) + + @classmethod + def mmlil(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewMappedMediumLevelIL(view.handle, settings)) + + @classmethod + def mmlil_ssa_form(cls, view, settings = None): + if settings is not None: + settings = settings.handle + return LinearViewObject(core.BNCreateLinearViewMappedMediumLevelILSSAForm(view.handle, settings)) + class LinearViewCursor(object): def __init__(self, root_object, handle = None): diff --git a/ui/linearview.h b/ui/linearview.h index b4928785..0c76fe83 100644 --- a/ui/linearview.h +++ b/ui/linearview.h @@ -117,7 +117,9 @@ class BINARYNINJAUIAPI LinearView: public QAbstractScrollArea, public View, publ uint64_t m_navByRefTarget; bool m_navByRef = false; - DisassemblySettingsRef m_settings; + SettingsRef m_settings; + DisassemblySettingsRef m_options; + BNFunctionGraphType m_type; BinaryNinja::Ref m_topPosition, m_bottomPosition; std::vector m_lines; @@ -128,6 +130,8 @@ class BINARYNINJAUIAPI LinearView: public QAbstractScrollArea, public View, publ ContextMenuManager m_contextMenuManager; QPointer m_commentDialog; + std::map m_analysisRequestors; + void adjustSize(int width, int height); void setTopToAddress(uint64_t addr); @@ -171,6 +175,8 @@ class BINARYNINJAUIAPI LinearView: public QAbstractScrollArea, public View, publ BinaryNinja::LinearViewCursor* newCursor); uint64_t getOrderingIndexForLine(const LinearViewLine& line); + void updateAnalysisRequestorsForCache(); + private Q_SLOTS: void viewInHexEditor(); void viewInGraph(); @@ -280,6 +286,7 @@ public: virtual HighlightTokenState getHighlightTokenState() override { return m_highlight; } void toggleOption(BNDisassemblyOption option); + void setViewType(BNFunctionGraphType type); virtual bool goToReference(FunctionRef func, uint64_t source, uint64_t target) override; QFont getFont() override { return m_render.getFont(); } -- cgit v1.3.1