Skip to content

Commit

Permalink
PHO and PHO reverse, some TAC builders
Browse files Browse the repository at this point in the history
  • Loading branch information
kokifish committed Jan 12, 2025
1 parent c8de389 commit 940923f
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 188 deletions.
4 changes: 4 additions & 0 deletions examples/dis_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
panda_re.trans_NAC_to_TAC(method_id=FUNC_IDX)
panda_re._code_lifting_algorithms(FUNC_IDX)

# nac_total = panda_re.get_insts_total()
# for idx in range(panda_re.method_len()):
# panda_re.split_native_code_block(idx)
# print(f">> [{idx}/{panda_re.method_len()}] CF built {panda_re.dis_file.methods[idx]._debug_vstr()}")
# panda_re.trans_NAC_to_TAC(method_id=idx)
# tac_total = panda_re.get_insts_total()
# todo_tac = panda_re.get_tac_unknown_count()
# print(f"todo_tac {todo_tac}/tac {tac_total} {todo_tac/tac_total:.4f} / nac {nac_total} {todo_tac/nac_total:.4f}")
69 changes: 57 additions & 12 deletions ohre/abcre/dis/AsmArg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, arg_type: AsmTypes = AsmTypes.UNKNOWN,
# name: e.g. for v0, type is VAR, name is v0(stored without truncating the prefix v)
self.name: str = name
# value: may be set in the subsequent analysis
self.value = value
self.value = value # if type is ARRAY, value is AsmArg list
self.ref_base = ref_base # AsmArg
self.paras_len: Union[int, None] = paras_len # for method object, store paras len here

Expand Down Expand Up @@ -53,6 +53,9 @@ def __hash__(self):
def __repr__(self):
return f"Arg({self._debug_str()})"

def set_ref(self, ref_ed_arg):
self.ref_base = ref_ed_arg

@classmethod
def build_arg(cls, s: str): # return VAR v0 v1... or ARG a0 a1...
assert isinstance(s, str) and len(s) > 0
Expand All @@ -65,7 +68,15 @@ def build_arg(cls, s: str): # return VAR v0 v1... or ARG a0 a1...
Log.error(f"build_arg failed: s={s}")

@classmethod
def build_arr(cls, args: List, name: str = ""):
def build_acc(cls): # return AsmArg(AsmTypes.ACC)
return cls.ACC()

@classmethod
def ACC(cls): # return AsmArg(AsmTypes.ACC)
return AsmArg(AsmTypes.ACC)

@classmethod
def build_arr(cls, args: List, name: str = ""): # element of args should be AsmArg
return AsmArg(AsmTypes.ARRAY, name=name, value=list(args))

@classmethod
Expand All @@ -89,28 +100,62 @@ def is_acc(self) -> bool:
return True
return False

def get_all_args_recursively(self, include_self: bool = True) -> List:
out = list()
if (include_self):
out.append(self)
if (isinstance(self.ref_base, AsmArg)):
out.append(self.ref_base)
if (self.value is not None and isinstance(self.value, Iterable)): # if type is ARRAY
for v in self.value:
if (isinstance(v, AsmArg)):
out.append(v)
return out

def _common_error_check(self):
if (self.type == AsmTypes.FIELD):
if (self.ref_base is None or len(self.name) == 0):
Log.error(f"[ArgCC] A filed without ref_base or name len==0: name {self.name} len {len(self.name)}")
if (self.type == AsmTypes.MODULE):
if (len(self.name) == 0):
Log.error(f"[ArgCC] A module without name: len {len(self.name)}")
if (self.type == AsmTypes.METHOD):
if (len(self.name) == 0):
Log.error(f"[ArgCC] A method without name: len {len(self.name)}")
if (self.type == AsmTypes.LABEL):
if (len(self.name) == 0):
Log.error(f"[ArgCC] A label without name: len {len(self.name)}")

def _debug_str(self):
self._common_error_check()
out = ""
if (len(self.name)):
out += f"{self.name}"
if (self.type == AsmTypes.FIELD):
if (self.ref_base is not None):
out += f"{self.ref_base}[{self.name}]"
else:
out = f"{AsmTypes.get_code_name(self.type)}"
if (self.ref_base is not None):
out += f"{self.ref_base}->"
out += f"{self.name}"
if (len(self.name) == 0):
out += f"{AsmTypes.get_code_name(self.type)}"
if (self.value is not None):
out += f"({self.value})"
if (self.ref_base is not None):
out += f"//ref:{self.ref_base}"
if (self.paras_len is not None):
out += f"(paras_len={self.paras_len})"
return out

def _debug_vstr(self):
out = f"{AsmTypes.get_code_name(self.type)}"
if (len(self.name) > 0):
out += f"-{self.name}"
self._common_error_check()
out = ""
if (self.type == AsmTypes.FIELD):
if (self.ref_base is not None):
out += f"{self.ref_base}[{AsmTypes.get_code_name(self.type)}-{self.name}]"
else:
if (self.ref_base is not None):
out += f"{self.ref_base}->"
out += f"{AsmTypes.get_code_name(self.type)}-{self.name}"
if (self.value is not None):
out += f"({self.value})"
if (self.ref_base is not None):
out += f"//ref:{self.ref_base}"
if (self.paras_len is not None):
out += f"(paras_len={self.paras_len})"
return out
8 changes: 4 additions & 4 deletions ohre/abcre/dis/AsmMethod.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import Any, Dict, Iterable, List, Tuple, Union

from ohre.abcre.dis.enum.AsmTypes import AsmTypes
from ohre.abcre.dis.AsmArg import AsmArg
from ohre.abcre.dis.enum.CODE_LV import CODE_LV
from ohre.abcre.dis.AsmRecord import AsmRecord
from ohre.abcre.dis.CodeBlock import CodeBlock
from ohre.abcre.dis.CodeBlocks import CodeBlocks
from ohre.abcre.dis.ControlFlow import ControlFlow
from ohre.misc import Log, utils
from ohre.abcre.dis.DebugBase import DebugBase
from ohre.abcre.dis.enum.AsmTypes import AsmTypes
from ohre.abcre.dis.enum.CODE_LV import CODE_LV
from ohre.abcre.dis.TAC import TAC
from ohre.misc import Log, utils


def is_label_line(s: str): # single str in a single line endswith ":", maybe label?
Expand Down Expand Up @@ -242,7 +242,7 @@ def split_native_code_block(self):
def get_insts_total(self):
return self.code_blocks.get_insts_total()

def get_args(self, start_pos: int = 0):
def get_args(self, start_pos: int = 0) -> List[AsmArg]:
ret: List[AsmArg] = list()
for i in range(start_pos, len(self.args)):
ty, name = self.args[i]
Expand Down
7 changes: 3 additions & 4 deletions ohre/abcre/dis/CodeBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from ohre.abcre.dis.AsmArg import AsmArg
from ohre.abcre.dis.DebugBase import DebugBase
from ohre.abcre.dis.NAC import NAC
from ohre.abcre.dis.enum.NACTYPE import NACTYPE
from ohre.abcre.dis.TAC import TAC
from ohre.abcre.dis.enum.TACTYPE import TACTYPE
from ohre.abcre.dis.NAC import NAC
from ohre.abcre.dis.TAC import TAC


class CodeBlock(DebugBase): # asm instruction(NAC) cantained
Expand All @@ -26,7 +26,7 @@ def __init__(self, in_l: Union[List[List[str]], List[NAC], List[TAC]], next_cb_l
self.next_cb_list = set()
else:
self.next_cb_list = next_cb_list

self.use_vars: set[AsmArg] = None

def get_slice_block(self, idx_start: int, idx_end: int):
Expand Down Expand Up @@ -69,7 +69,6 @@ def replace_insts(self, tac_l: List[TAC]):
def get_insts_len(self) -> int:
return len(self.insts)


def __len__(self) -> int:
return len(self.insts)

Expand Down
Loading

0 comments on commit 940923f

Please sign in to comment.