Skip to content

Commit

Permalink
Add more alias for Function artifacts (#80)
Browse files Browse the repository at this point in the history
* Add more alias for Function artifacts

* More tests
  • Loading branch information
mahaloz authored Jun 3, 2024
1 parent 01d6f37 commit 4c05caf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.6.2"
__version__ = "1.7.0"

import logging
logging.getLogger("libbs").addHandler(logging.NullHandler())
Expand Down
21 changes: 21 additions & 0 deletions libbs/artifacts/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ def __getstate__(self):
} if self.stack_vars else None

state = super().__getstate__()
# give alias for name and type for convenience
state["name"] = self.name
state["type"] = self.type
state["header"] = header
state["stack_vars"] = stack_vars
return state
Expand All @@ -247,6 +250,13 @@ def __setstate__(self, state):
header = None
self.header = header

# alias for name overrides header if it exists
if "name" in state:
self.name = state.pop("name")
# alias for type overrides header if it exists
if "type" in state:
self.type = state.pop("type")

stack_vars_dat = state.pop("stack_vars", {})
if stack_vars_dat:
stack_vars = {}
Expand Down Expand Up @@ -380,6 +390,17 @@ def name(self, value):
self.header = FunctionHeader(name=None, addr=self.addr)
self.header.name = value

@property
def type(self):
return self.header.type if self.header else None

@type.setter
def type(self, value):
# create a header if one does not exist for this function
if not self.header:
self.header = FunctionHeader(name=None, addr=self.addr)
self.header.type = value

@property
def args(self):
return self.header.args if self.header else {}
4 changes: 4 additions & 0 deletions tests/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def generate_test_funcs(func_addr):
func2 = Function(addr=func_addr, size=0x150, header=fh2, stack_vars=stack_vars2)
return func1, func2


class TestArtifacts(unittest.TestCase):
def test_func_diffing(self):
# setup top
Expand Down Expand Up @@ -141,12 +142,15 @@ def test_serialization(self):
0: StructMember(offset=0, name="m0", type_="int", size=4),
4: StructMember(offset=4, name="m4", type_="long", size=8)
})
# TODO: add comments, enums, patches, and global vars to the test
for fmt, load_func in native_load_funcs.items():
serialized_func = func.dumps(fmt=fmt)
loaded_func_dict = load_func(serialized_func)

assert loaded_func_dict["addr"] == func.addr
assert loaded_func_dict["size"] == func.size
assert loaded_func_dict["name"] == func.name
assert loaded_func_dict["type"] == func.type
assert loaded_func_dict["header"]["name"] == func.header.name
assert loaded_func_dict["header"]["type"] == func.header.type
assert loaded_func_dict["header"]["args"]["0x0"]["name"] == func.header.args[0].name
Expand Down

0 comments on commit 4c05caf

Please sign in to comment.