Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add information about the current bitstream in flen ('bs' and 'offset_b') #1051

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions miasm/arch/msp430/arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def encode(self):
class bs_cond_off_s(bs_cond):

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if v['a_s'] == 0b00:
return None
elif v['a_s'] == 0b01:
Expand Down Expand Up @@ -501,7 +501,7 @@ def decode(self, v):
class bs_cond_off_d(bs_cond_off_s):

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if v['a_d'] == 0:
return None
elif v['a_d'] == 1:
Expand Down
20 changes: 10 additions & 10 deletions miasm/arch/x86/arch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,7 @@ class bs_cond_scale(bs_cond):
ll = 2

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
return sib_cond(cls, mode, v)

def encode(self):
Expand All @@ -2690,15 +2690,15 @@ class bs_cond_index(bs_cond_scale):
ll = 3

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
return sib_cond(cls, mode, v)


class bs_cond_disp(bs_cond):
# cond must return field len

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if admode_prefix((mode, v['opmode'], v['admode'])) == 16:
if v['mod'] == 0b00:
if v['rm'] == 0b110:
Expand Down Expand Up @@ -2780,7 +2780,7 @@ def fromstring(self, text, loc_db, parser_result=None):
return start, stop

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if 'w8' not in v or v['w8'] == 1:
if 'se' in v and v['se'] == 1:
return 8
Expand Down Expand Up @@ -2868,7 +2868,7 @@ def getmaxlen(self):
return 64

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if 'w8' not in v or v['w8'] == 1:
if 'se' in v and v['se'] == 1:
return 8
Expand Down Expand Up @@ -2899,7 +2899,7 @@ def fromstring(self, text, loc_db, parser_result=None):
return start, stop

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
osize = v_opmode_info(mode, v['opmode'], v['rex_w'], 0)
if osize == 16:
return 16
Expand Down Expand Up @@ -2941,7 +2941,7 @@ class bs_s08(bs_rel_off):
parser = base_expr

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
return 8

def encode(self):
Expand Down Expand Up @@ -2974,14 +2974,14 @@ def decode(self, v):
class bs_rel_off08(bs_rel_off):

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
return 8


class bs_moff(bsi):

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
osize = v_opmode_info(mode, v['opmode'], v['rex_w'], 0)
if osize == 16:
return 16
Expand Down Expand Up @@ -3046,7 +3046,7 @@ def fromstring(self, text, loc_db, parser_result=None):
return start, stop

@classmethod
def flen(cls, mode, v):
def flen(cls, mode, v, bs, offset_b):
if mode == 64:
if v['admode']:
return 32
Expand Down
21 changes: 17 additions & 4 deletions miasm/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,10 @@ def __init__(self, strbits=None, l=None, cls=None,
# gen conditional field
if cls:
for b in cls:
if 'flen' in b.__dict__:
try:
flen = getattr(b, 'flen')
except AttributeError:
pass

self.strbits = strbits
self.l = l
Expand Down Expand Up @@ -514,7 +516,7 @@ def check_fbits(self, v):
return v & self.fmask == self.fbits

@classmethod
def flen(cls, v):
def flen(cls, mode, v, bs, offset_b):
raise NotImplementedError('not fully functional')


Expand Down Expand Up @@ -1085,7 +1087,12 @@ def guess_mnemo(cls, bs, attrib, pre_dis_info, offset):
(l, fmask, fbits, fname, flen), vals = branch

if flen is not None:
l = flen(attrib, fname_values)
try:
l = flen(attrib, fname_values, bs, offset_b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this new code, flen can raise IOError as the user can get bits from the bs.
You must have the same behavior as getbits below in case of IOError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it in the latest commit.

except NotImplementedError:
pass
except IOError:
continue
if l is not None:
try:
v = cls.getbits(bs, attrib, offset_b, l)
Expand Down Expand Up @@ -1214,7 +1221,13 @@ def dis(cls, bs_o, mode_o = None, offset=0):
total_l = 0
for i, f in enumerate(c.fields_order):
if f.flen is not None:
l = f.flen(mode, fname_values)
try:
l = f.flen(mode, fname_values, bs, offset_b)
serpilliere marked this conversation as resolved.
Show resolved Hide resolved
except NotImplementedError:
pass
except IOError:
bs_o.leave_atomic_mode()
raise
else:
l = f.l
if l is not None:
Expand Down