-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
235 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from ohre.abcre.dis.NACBlock import NACBlock | ||
from ohre.abcre.dis.NACBlocks import NACBlocks | ||
from ohre.abcre.dis.NACTYPE import NACTYPE | ||
from ohre.misc import Log, utils | ||
|
||
|
||
class ControlFlow(): | ||
def split_native_code_block(blocks: NACBlocks) -> NACBlocks: | ||
assert len(blocks) == 1 | ||
nac_block = blocks.nac_blocks[0] | ||
delimited_id: list = list() | ||
for i in range(len(nac_block)): | ||
nac = nac_block.nacs[i] | ||
if (nac.type == NACTYPE.LABEL): | ||
delimited_id.append(i) | ||
elif (nac.type == NACTYPE.COND_JMP or nac.type == NACTYPE.UNCN_JMP or nac.type == NACTYPE.RETURN): | ||
if (i + 1 < len(nac_block)): | ||
delimited_id.append(i + 1) | ||
delimited_id = sorted(list(set(delimited_id))) | ||
if (len(nac_block) not in delimited_id): | ||
delimited_id.append(len(nac_block)) | ||
debug_out = "" | ||
for idx in delimited_id: | ||
if (idx < len(nac_block)): | ||
debug_out += f"{idx}-{nac_block.nacs[idx]}; " | ||
else: | ||
debug_out += f"{idx} nac_block len {len(nac_block)}" | ||
Log.info(f"[ControlFlow] delimited id-nac {debug_out}", False) | ||
|
||
final_nac_blocks: list = list() | ||
idx_start = 0 | ||
for i in range(len(delimited_id)): | ||
idx_end = delimited_id[i] | ||
final_nac_blocks.append(nac_block.get_slice_block(idx_start, idx_end)) | ||
idx_start = idx_end | ||
return NACBlocks(final_nac_blocks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
import copy | ||
from typing import Any, Dict, Iterable, List, Tuple | ||
|
||
from ohre.abcre.dis.NAC import NAC | ||
from ohre.abcre.dis.NACTYPE import NACTYPE | ||
import copy | ||
|
||
|
||
class NACBLOCK_LV: | ||
NATIVE = 0 | ||
LEVEL1 = 1 | ||
LEVEL2 = 2 | ||
|
||
|
||
class NACBlock(): | ||
def __init__(self, insts: List[List[str]], level=NACBLOCK_LV.NATIVE): | ||
assert len(insts) > 0 | ||
class NACBlock(): # asm instruction(NAC) cantained | ||
def __init__(self, in_l: List[List[str]] | List[NAC]): | ||
assert len(in_l) >= 0 | ||
self.nacs: List[NAC] = list() | ||
self.level = level | ||
for inst in insts: | ||
assert len(inst) > 0 | ||
self.nacs.append(NAC(inst)) | ||
if (isinstance(in_l[0], NAC)): # NAC in list | ||
self.nacs = copy.deepcopy(in_l) | ||
else: # maybe list in list # anyway, try init NAC using element in list | ||
for inst in in_l: | ||
assert len(inst) > 0 | ||
self.nacs.append(NAC(inst)) | ||
|
||
def get_slice_block(self, idx_start: int, idx_end: int): | ||
return NACBlock(copy.deepcopy(self.nacs[idx_start: idx_end])) | ||
|
||
def __str__(self): | ||
return self.debug_short() | ||
|
||
def __len__(self): | ||
return len(self.nacs) | ||
|
||
def debug_short(self): | ||
out = f"NACBlock: nacs {len(self.nacs)} lv {self.level}" | ||
out = f"NACBlock: nacs {len(self.nacs)}" | ||
return out | ||
|
||
def debug_deep(self): | ||
out = f"NACBlock: nacs {len(self.nacs)} lv {self.level}\n" | ||
out = f"NACBlock: nacs {len(self.nacs)}\n" | ||
for i in range(len(self.nacs)): | ||
out += f"{i}\t{self.nacs[i].debug_deep()}\n" | ||
return out | ||
if (self.nacs[i].type == NACTYPE.LABEL): | ||
out += f"{i} {self.nacs[i].debug_deep()}\n" | ||
else: | ||
out += f"{i}\t{self.nacs[i].debug_deep()}\n" | ||
return out.strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.