Skip to content

Commit

Permalink
Fix misaligned struct modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mahaloz committed Oct 1, 2024
1 parent 644c7bb commit 37c232d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libbs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.0.1"
__version__ = "2.0.2"


import logging
Expand Down
4 changes: 3 additions & 1 deletion libbs/artifacts/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class StructMember(Artifact):
"""
Describes a struct member that corresponds to a struct.
Offset is the byte offset of the member from the start of the struct.
"""

__slots__ = Artifact.__slots__ + (
Expand Down Expand Up @@ -41,7 +42,8 @@ def __str__(self):

class Struct(Artifact):
"""
Describes a struct
Describes a struct.
All members are stored by their byte offset from the start of the struct.
"""

__slots__ = Artifact.__slots__ + (
Expand Down
17 changes: 13 additions & 4 deletions libbs/decompilers/ida/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,12 +1010,13 @@ def bs_struct_from_tif(tif):
udt_data = ida_typeinf.udt_type_data_t()
if tif.get_udt_details(udt_data):
for udt_memb in udt_data:
offset = udt_memb.offset
# TODO: warning if offset is not a multiple of 8 (a bit offset), we are in trouble
byte_offset = udt_memb.offset // 8
m_name = udt_memb.name
m_type = udt_memb.type
type_name = m_type.get_type_name() or str(m_type)
m_size = m_type.get_size()
members[offset] = StructMember(name=m_name, type_=type_name, size=m_size, offset=offset)
members[byte_offset] = StructMember(name=m_name, type_=type_name, size=m_size, offset=byte_offset)

return Struct(name=name, size=size, members=members)

Expand Down Expand Up @@ -1192,8 +1193,16 @@ def set_ida_struct_member_types(bs_struct: Struct):

data_changed = False
for udt_memb in udt_data:
offset = udt_memb.offset
bs_member = bs_struct.members.get(offset, None)
if udt_memb.offset % 8 != 0:
_l.warning(
f"Struct member %s of struct %s is not byte aligned! This is currently unsupported.",
udt_memb.name,
bs_struct.name
)
continue

byte_offset = udt_memb.offset // 8
bs_member = bs_struct.members.get(byte_offset, None)
if bs_member is None:
continue

Expand Down

0 comments on commit 37c232d

Please sign in to comment.