GeezerVison is showing github.com — Add a hook to allow BinaryView subclasses to run code after snapshot … · Vector35/binaryninja-api@ddd7aa4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: binaryninjaapi.h +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5773,13 +5773,23 @@ namespace BinaryNinja { | 5773 | 5773 | void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc); | 5774 | 5774 | void PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> sym, uint64_t reloc); | 5775 | 5775 |
| | 5776 | + /*! OnAfterSnapshotDataApplied is called when loading a view from a database, after snapshot data has been applied to it. | | 5777 | + | | 5778 | + \note This method **may** be overridden by custom BinaryViews. | | 5779 | + | | 5780 | + \warning This method **must not** be called directly. | | 5781 | + | | 5782 | + */ | | 5783 | + virtual void OnAfterSnapshotDataApplied() {} | | 5784 | + | 5776 | 5785 | public: | 5777 | 5786 | void NotifyDataWritten(uint64_t offset, size_t len); | 5778 | 5787 | void NotifyDataInserted(uint64_t offset, size_t len); | 5779 | 5788 | void NotifyDataRemoved(uint64_t offset, uint64_t len); | 5780 | 5789 |
| 5781 | 5790 | private: | 5782 | 5791 | static bool InitCallback(void* ctxt); | | 5792 | + static void OnAfterSnapshotDataAppliedCallback(void* ctxt); | 5783 | 5793 | static void FreeCallback(void* ctxt); | 5784 | 5794 | static size_t ReadCallback(void* ctxt, void* dest, uint64_t offset, size_t len); | 5785 | 5795 | static size_t WriteCallback(void* ctxt, uint64_t offset, const void* src, size_t len); | |
Collapse file Copy file name to clipboardExpand all lines: binaryninjacore.h +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 37 | 37 | // Current ABI version for linking to the core. This is incremented any time | 38 | 38 | // there are changes to the API that affect linking, including new functions, | 39 | 39 | // new types, or modifications to existing functions or types. | 40 | | -#define BN_CURRENT_CORE_ABI_VERSION 162 | | 40 | +#define BN_CURRENT_CORE_ABI_VERSION 163 | 41 | 41 |
| 42 | 42 | // Minimum ABI version that is supported for loading of plugins. Plugins that | 43 | 43 | // are linked to an ABI version less than this will not be able to load and | 44 | 44 | // will require rebuilding. The minimum version is increased when there are | 45 | 45 | // incompatible changes that break binary compatibility, such as changes to | 46 | 46 | // existing types or functions. | 47 | | -#define BN_MINIMUM_CORE_ABI_VERSION 161 | | 47 | +#define BN_MINIMUM_CORE_ABI_VERSION 163 | 48 | 48 |
| 49 | 49 | #ifdef __GNUC__ | 50 | 50 | #ifdef BINARYNINJACORE_LIBRARY | @@ -1847,6 +1847,7 @@ extern "C" | 1847 | 1847 | bool (*isRelocatable)(void* ctxt); | 1848 | 1848 | size_t (*getAddressSize)(void* ctxt); | 1849 | 1849 | bool (*save)(void* ctxt, BNFileAccessor* accessor); | | 1850 | + void (*onAfterSnapshotDataApplied)(void* ctxt); | 1850 | 1851 | } BNCustomBinaryView; | 1851 | 1852 |
| 1852 | 1853 | typedef struct BNCustomBinaryViewType | |
Collapse file Copy file name to clipboardExpand all lines: binaryview.cpp +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1365,6 +1365,7 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryVi | 1365 | 1365 | view.isRelocatable = IsRelocatableCallback; | 1366 | 1366 | view.getAddressSize = GetAddressSizeCallback; | 1367 | 1367 | view.save = SaveCallback; | | 1368 | + view.onAfterSnapshotDataApplied = OnAfterSnapshotDataAppliedCallback; | 1368 | 1369 | m_file = file; | 1369 | 1370 | AddRefForRegistration(); | 1370 | 1371 | m_object = BNCreateCustomBinaryView( | @@ -1388,6 +1389,13 @@ bool BinaryView::InitCallback(void* ctxt) | 1388 | 1389 | } | 1389 | 1390 |
| 1390 | 1391 |
| | 1392 | +void BinaryView::OnAfterSnapshotDataAppliedCallback(void* ctxt) | | 1393 | +{ | | 1394 | + CallbackRef<BinaryView> view(ctxt); | | 1395 | + view->OnAfterSnapshotDataApplied(); | | 1396 | +} | | 1397 | + | | 1398 | + | 1391 | 1399 | void BinaryView::FreeCallback(void* ctxt) | 1392 | 1400 | { | 1393 | 1401 | BinaryView* view = (BinaryView*)ctxt; | |
Collapse file Copy file name to clipboardExpand all lines: python/binaryview.py +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3173,6 +3173,7 @@ def __init__( | 3173 | 3173 | self._cb.isRelocatable = self._cb.isRelocatable.__class__(self._is_relocatable) | 3174 | 3174 | self._cb.getAddressSize = self._cb.getAddressSize.__class__(self._get_address_size) | 3175 | 3175 | self._cb.save = self._cb.save.__class__(self._save) | | 3176 | + self._cb.onAfterSnapshotDataApplied = self._cb.onAfterSnapshotDataApplied.__class__(self._on_after_snapshot_data_applied) | 3176 | 3177 | if file_metadata is None: | 3177 | 3178 | raise Exception("Attempting to create a BinaryView with FileMetadata which is None") | 3178 | 3179 | self._file = file_metadata | @@ -4482,6 +4483,15 @@ def _save(self, ctxt, file_accessor): | 4482 | 4483 | log_error_for_exception("Unhandled Python exception in BinaryView._save") | 4483 | 4484 | return False | 4484 | 4485 |
| | 4486 | + def _on_after_snapshot_data_applied(self, ctxt): | | 4487 | + try: | | 4488 | + self.perform_on_after_snapshot_data_applied() | | 4489 | + except: | | 4490 | + log_error_for_exception("Unhandled Python exception in BinaryView._on_after_snapshot_data_applied") | | 4491 | + | | 4492 | + def perform_on_after_snapshot_data_applied(self) -> None: | | 4493 | + pass | | 4494 | + | 4485 | 4495 | def init(self) -> bool: | 4486 | 4496 | return True | 4487 | 4497 |
| |
Collapse file Copy file name to clipboardExpand all lines: rust/src/custom_binary_view.rs +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -460,6 +460,7 @@ pub unsafe trait CustomBinaryView: 'static + BinaryViewBase + Sync + Sized { | 460 | 460 |
| 461 | 461 | fn new(handle: &BinaryView, args: &Self::Args) -> Result<Self>; | 462 | 462 | fn init(&mut self, args: Self::Args) -> Result<()>; | | 463 | + fn on_after_snapshot_data_applied(&mut self) {} | 463 | 464 | } | 464 | 465 |
| 465 | 466 | /// Represents a partially initialized custom `BinaryView` that should be returned to the core | @@ -600,6 +601,18 @@ impl<'a, T: CustomBinaryViewType> CustomViewBuilder<'a, T> { | 600 | 601 | }) | 601 | 602 | } | 602 | 603 |
| | 604 | + extern "C" fn cb_on_after_snapshot_data_applied<V>(ctxt: *mut c_void) | | 605 | + where | | 606 | + V: CustomBinaryView, | | 607 | + { | | 608 | + ffi_wrap!("BinaryViewBase::onAfterSnapshotDataApplied", unsafe { | | 609 | + let context = &mut *(ctxt as *mut CustomViewContext<V>); | | 610 | + if let CustomViewContextState::Initialized { view } = &mut context.state { | | 611 | + view.on_after_snapshot_data_applied(); | | 612 | + } | | 613 | + }) | | 614 | + } | | 615 | + | 603 | 616 | extern "C" fn cb_free_object<V>(ctxt: *mut c_void) | 604 | 617 | where | 605 | 618 | V: CustomBinaryView, | @@ -890,6 +903,7 @@ impl<'a, T: CustomBinaryViewType> CustomViewBuilder<'a, T> { | 890 | 903 | isRelocatable: Some(cb_relocatable::<V>), | 891 | 904 | getAddressSize: Some(cb_address_size::<V>), | 892 | 905 | save: Some(cb_save::<V>), | | 906 | + onAfterSnapshotDataApplied: Some(cb_on_after_snapshot_data_applied::<V>), | 893 | 907 | }; | 894 | 908 |
| 895 | 909 | let view_name = view_name.to_cstr(); | |
|