Skip to content

Commit

Permalink
Various fixes to align with the BinSync backend refactor (#25)
Browse files Browse the repository at this point in the history
* Fixes for BinSync

* Fix IDA interface and remove bad decs from installer

* All apis should always return and take Lifted objects

* Correct some bugs in IDA with the new lifting standards for API

* Make some stuff more quiet

* dont forget to lower angr

* bump

* correct version
  • Loading branch information
mahaloz authored Jan 20, 2024
1 parent 572248f commit 88af476
Show file tree
Hide file tree
Showing 16 changed files with 490 additions and 471 deletions.
8 changes: 4 additions & 4 deletions examples/change_watcher_plugin/bs_change_watcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def create_plugin(*args, **kwargs):
"""
This is the entry point that all decompilers will call in various ways. To remain agnostic,
always pass the args and kwargs to the ui_init_args and ui_init_kwargs of DecompilerInterface, inited
always pass the args and kwargs to the gui_init_args and gui_init_kwargs of DecompilerInterface, inited
through the discover api.
"""

Expand All @@ -18,8 +18,8 @@ def create_plugin(*args, **kwargs):
deci = DecompilerInterface.discover(
plugin_name="ArtifactChangeWatcher",
init_plugin=True,
ui_init_args=args,
ui_init_kwargs=kwargs
gui_init_args=args,
gui_init_kwargs=kwargs
)
# create a function to print a string in the decompiler console
decompiler_printer = lambda *x, **y: deci.print(f"Changed {x}{y}")
Expand All @@ -29,7 +29,7 @@ def create_plugin(*args, **kwargs):
}

# register a menu to open when you right click on the psuedocode view
deci.register_ctx_menu_item(
deci.gui_register_ctx_menu(
"StartArtifactChangeWatcher",
"Start watching artifact changes",
lambda: deci.start_artifact_watchers(),
Expand Down
9 changes: 8 additions & 1 deletion libbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
__version__ = "0.18.0"
__version__ = "0.19.0"

import logging
logging.getLogger("libbs").addHandler(logging.NullHandler())
from libbs.logger import Loggers
loggers = Loggers()
del Loggers
del logging
16 changes: 14 additions & 2 deletions libbs/api/artifact_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,36 @@ def __len__(self):
return len(self._artifact_lister())

def __getitem__(self, item):
"""
Takes a lifted identifier as input and returns a lifted artifact
"""
if isinstance(item, int):
item = self._deci.art_lifter.lower_addr(item)

art = self._artifact_getter(item)
if art is None:
raise KeyError

return self._deci.art_lifter.lift(art)

def __setitem__(self, key, value):
"""
Both key and value must be lifted artifacts
"""
if not isinstance(value, self._artifact_class):
raise ValueError(f"Attempting to set a value of type {type(value)} to a dict of type {self._artifact_class}")

if hasattr(value, "addr") and value.addr is None:
value.addr = key
if isinstance(key, int):
key = self._deci.art_lifter.lower_addr(key)

art = self._deci.art_lifter.lower(value)
if not self._artifact_setter(art) and self._error_on_duplicate:
raise ValueError(f"Set value {value} is already present at key {key}")

def __contains__(self, item):
if isinstance(item, int):
item = self._deci.art_lifter.lower_addr(item)

data = self._artifact_getter(item)
return data is not None

Expand Down
9 changes: 5 additions & 4 deletions libbs/api/artifact_lifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def lift_type(self, type_str: str) -> str:

def lift_addr(self, addr: int) -> int:
if addr < self.deci.binary_base_addr:
self.deci.warning(f"Lifting an address that appears already lifted: {addr}...")
self.deci.debug(f"Lifting an address that appears already lifted: {addr}...")
return addr
else:
return addr - self.deci.binary_base_addr
Expand All @@ -47,7 +47,7 @@ def lower_type(self, type_str: str) -> str:

def lower_addr(self, addr: int) -> int:
if addr >= self.deci.binary_base_addr:
self.deci.warning(f"Lowering an address that appears already lowered: {addr}...")
self.deci.debug(f"Lowering an address that appears already lowered: {addr}...")
return addr
else:
return addr + self.deci.binary_base_addr
Expand All @@ -60,7 +60,7 @@ def lower_stack_offset(self, offset: int, func_addr: int) -> int:
#

def _lift_or_lower_artifact(self, artifact, mode):
target_attrs = ("type", "offset", "addr")
target_attrs = ("type", "offset", "addr", "func_addr")
if mode not in ("lower", "lift"):
return None

Expand All @@ -82,7 +82,8 @@ def _lift_or_lower_artifact(self, artifact, mode):
lifting_func = getattr(self, f"{mode}_stack_offset")
setattr(lifted_art, attr, lifting_func(curr_val, lifted_art.addr))
else:
lifting_func = getattr(self, f"{mode}_{attr}")
attr_func_name = attr if attr != "func_addr" else "addr"
lifting_func = getattr(self, f"{mode}_{attr_func_name}")
setattr(lifted_art, attr, lifting_func(curr_val))

# recursively correct nested artifacts
Expand Down
Loading

0 comments on commit 88af476

Please sign in to comment.