Skip to content

Eli suggestions #1

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

Open
wants to merge 3 commits into
base: armdev
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
67 changes: 14 additions & 53 deletions chb/app/AppAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""Access point for most analysis results."""

from abc import ABC, abstractmethod
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Generic, Type, TypeVar

from chb.api.InterfaceDictionary import InterfaceDictionary

Expand All @@ -55,27 +55,24 @@
import chb.util.fileutil as UF


class AppAccess(ABC):
HeaderTy = TypeVar('HeaderTy', PEHeader, ELFHeader)
class AppAccess(ABC, Generic[HeaderTy]):

def __init__(
self,
path: str,
filename: str,
deps: List[str] = [],
fileformat: str = "elf",
arch: str = "x86") -> None:
fileformat: Type[HeaderTy] = ELFHeader) -> None:
"""Initializes access to analysis results."""
self._path = path
self._filename = filename
self._deps = deps # list of summary jars registered as dependencies
self._fileformat = fileformat # currently supported: elf, pe
self._arch = arch # currently supported: arm, mips, x86
self._header_ty = fileformat # currently supported: elf, pe

self._userdata: Optional[UserData] = None

# file-format specific
self._peheader: Optional[PEHeader] = None
self._elfheader: Optional[ELFHeader] = None
self._header: Optional[HeaderTy] = None

# functions
self._appresultdata: Optional[AppResultData] = None
Expand Down Expand Up @@ -106,34 +103,17 @@ def dependencies(self) -> Sequence[str]:
return self._deps

# Architecture and file format ---------------------------------------------

@property
def architecture(self) -> str:
return self._arch

@property
def fileformat(self) -> str:
return self._fileformat

@property
def arm(self) -> bool:
return self.architecture == "arm"

@property
def mips(self) -> bool:
return self.architecture == "mips"

@property
def x86(self) -> bool:
return self.architecture == "x86"
return self._header_ty.fmt_name()

@property
def elf(self) -> bool:
return self.fileformat == "elf"
return self._header_ty == ELFHeader

@property
def pe(self) -> bool:
return self.fileformat in ["pe", "pe32"]
return self._header_ty == PEHeader

# Dictionaries ------------------------------------------------------------

Expand All @@ -152,31 +132,12 @@ def interfacedictionary(self) -> InterfaceDictionary:
return self._interfacedictionary

# File format --------------------------------------------------------------

@property
def peheader(self) -> PEHeader:
if self.pe:
if self._peheader is None:
x = UF.get_pe_header_xnode(self.path, self.filename)
self._peheader = PEHeader(
self.path, self.filename, x, self.dependencies)
return self._peheader
else:
raise UF.CHBError("File with file format "
+ self.fileformat
+ " does not have a PE header")

@property
def elfheader(self) -> ELFHeader:
if self.elf:
if self._elfheader is None:
x = UF.get_elf_header_xnode(self.path, self.filename)
self._elfheader = ELFHeader(self.path, self.filename, x)
return self._elfheader
else:
raise UF.CHBError("File with file format "
+ self.fileformat
+ " does not have an ELF header")
def header(self) -> HeaderTy:
if self._header is None:
x = self._fileformat.get_xnode(self.path, self.filename)
self._header = self._header_ty(self.path, self.filename, x, self.dependencies)
return self._header

# Systeminfo ---------------------------------------------------------------

Expand Down
10 changes: 5 additions & 5 deletions chb/arm/ARMAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@
# SOFTWARE.
# ------------------------------------------------------------------------------

from chb.elfformat.ELFHeader import ELFHeader
from typing import Dict, List, Mapping, Optional

from chb.app.AppAccess import AppAccess
from chb.app.AppAccess import AppAccess, HeaderTy

from chb.arm.ARMDictionary import ARMDictionary
from chb.arm.ARMFunction import ARMFunction

import chb.util.fileutil as UF


class ARMAccess(AppAccess):
class ARMAccess(AppAccess[HeaderTy]):

def __init__(
self,
path: str,
filename: str,
deps: List[str] = [],
fileformat: str = "elf",
arch: str = "arm") -> None:
AppAccess.__init__(self, path, filename, deps, fileformat, arch)
fileformat: HeaderTy = ELFHeader) -> None:
AppAccess.__init__(self, path, filename, deps, fileformat)
self._armd: Optional[ARMDictionary] = None
self._functions: Dict[str, ARMFunction] = {}

Expand Down
6 changes: 3 additions & 3 deletions chb/cmdline/commandutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ def get_app(path: str, xfile: str, xinfo: XI.XInfo) -> AppAccess:
arch = xinfo.architecture
format = xinfo.format
if arch == "x86":
return X86Access(path, xfile, fileformat=format, arch=arch)
return X86Access(path, xfile, fileformat=format)
elif arch == "mips":
return MIPSAccess(path, xfile, fileformat=format, arch=arch)
return MIPSAccess(path, xfile, fileformat=format)
elif arch == "arm":
return ARMAccess(path, xfile, fileformat=format, arch=arch)
return ARMAccess(path, xfile, fileformat=format)
else:
raise UF.CHBError("Archicture " + arch + " not yet supported")

Expand Down
4 changes: 2 additions & 2 deletions chb/cmdline/fileformatcmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def pedatacmd(args: argparse.Namespace) -> NoReturn:
exit(1)

app = UC.get_app(path, xfile, xinfo)
peheader = app.peheader
peheader = app.header
if headeronly:
print(peheader)
exit(0)
Expand Down Expand Up @@ -118,7 +118,7 @@ def elfdatacmd(args: argparse.Namespace) -> NoReturn:
app = UC.get_app(path, xfile, xinfo)
# app = AP.AppAccess(
# path, xfile, fileformat=xinfo.format, arch=xinfo.architecture)
elfheader = app.elfheader
elfheader = app.header

try:
print(str(elfheader))
Expand Down
14 changes: 12 additions & 2 deletions chb/elfformat/ELFHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# ------------------------------------------------------------------------------
import xml.etree.ElementTree as ET

from typing import Any, Callable, Dict, List, Optional, Tuple
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple

from chb.elfformat.ELFProgramHeader import ELFProgramHeader
from chb.elfformat.ELFSectionHeader import ELFSectionHeader
Expand Down Expand Up @@ -105,11 +105,21 @@ def get_value(d: Dict[str, str], v: str) -> str:

class ELFHeader:

@staticmethod
def fmt_name() -> str:
return "elf"

@staticmethod
def get_xnode(path: str, filename: str) -> ET.Element:
return UF.get_elf_header_xnode(path, filename)

def __init__(
self,
pathname: str,
filename: str,
xnode: ET.Element) -> None:
xnode: ET.Element,
# ignored, used for compatibility with PEHeader
deps: Sequence[str] = []) -> None:
self._pathname = pathname
self._filename = filename
self.xnode = xnode
Expand Down
10 changes: 5 additions & 5 deletions chb/mips/MIPSAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@
# SOFTWARE.
# ------------------------------------------------------------------------------

from chb.elfformat.ELFHeader import ELFHeader
from typing import Callable, cast, Dict, List, Mapping, Optional, Sequence, Tuple

from chb.app.AppAccess import AppAccess
from chb.app.AppAccess import AppAccess, HeaderTy

from chb.mips.MIPSDictionary import MIPSDictionary
from chb.mips.MIPSFunction import MIPSFunction

import chb.util.fileutil as UF


class MIPSAccess(AppAccess):
class MIPSAccess(AppAccess[HeaderTy]):

def __init__(
self,
path: str,
filename: str,
deps: List[str] = [],
fileformat: str = "elf",
arch: str = "mips") -> None:
AppAccess.__init__(self, path, filename, deps, fileformat, arch)
fileformat: HeaderTy = ELFHeader) -> None:
AppAccess.__init__(self, path, filename, deps, fileformat)
self._mipsd: Optional[MIPSDictionary] = None
self._functions: Dict[str, MIPSFunction] = {}

Expand Down
8 changes: 4 additions & 4 deletions chb/mips/simulation/MIPSimulationState.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ def __init__(self,
self.registers: Dict[str, SV.SimValue] = {} # register name -> SimValue
self.registers['zero'] = SV.SimDoubleWordValue(0)
self.stackmem = MIPSimStackMemory(self)
self.globalmem = MIPSimGlobalMemory(self, self.app.elfheader)
self.globalmem = MIPSimGlobalMemory(self, self.app.header)
self.basemem: Dict[str, MIPSimBaseMemory] = {}

# static library (optional)
self.libapp = libapp
if self.libapp is not None:
self.libstubs: Dict[int, Tuple[str, Optional[MIPSimStub]]] = {}
self.libglobalmem = MIPSimGlobalMemory(self, self.libapp.elfheader)
self.libglobalmem = MIPSimGlobalMemory(self, self.libapp.header)
# function-name -> function address in static lib
self.static_lib: Dict[str, str] = {}
libimgbase = self.libapp.elfheader.image_base
libimgbase = self.libapp.header.image_base
self.libimagebase = SSV.SimGlobalAddress(SV.SimDoubleWordValue(
int(libimgbase, 16)))

Expand All @@ -214,7 +214,7 @@ def __init__(self,
# target executable for dynamic loading (optional)
self.xapp = xapp
if self.xapp is not None:
self.xglobalmem = MIPSimGlobalMemory(self, self.xapp.elfheader)
self.xglobalmem = MIPSimGlobalMemory(self, self.xapp.header)

# log
self.fnlog: Dict[str, List[str]] = {} # iaddr -> msg list2
Expand Down
8 changes: 8 additions & 0 deletions chb/peformat/PEHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ def subsystem(self) -> str:
class PEHeader:
"""Main entry point to access the raw data in the executable."""

@staticmethod
def fmt_name() -> str:
return "pe32"

@staticmethod
def get_xnode(path: str, filename: str) -> ET.Element:
return UF.get_pe_header_xnode(path, filename)

def __init__(
self,
pathname: str,
Expand Down
10 changes: 5 additions & 5 deletions chb/x86/X86Access.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
# SOFTWARE.
# ------------------------------------------------------------------------------

from chb.elfformat.ELFHeader import ELFHeader
from typing import Dict, List, Mapping, Optional, Sequence

from chb.app.AppAccess import AppAccess
from chb.app.AppAccess import AppAccess, HeaderTy

import chb.util.fileutil as UF

Expand All @@ -38,16 +39,15 @@
from chb.x86.X86Instruction import X86Instruction


class X86Access(AppAccess):
class X86Access(AppAccess[HeaderTy]):

def __init__(
self,
path: str,
filename: str,
deps: List[str] = [],
fileformat: str = "elf",
arch: str = "x86") -> None:
AppAccess.__init__(self, path, filename, deps, fileformat, arch)
fileformat: HeaderTy = ELFHeader) -> None:
AppAccess.__init__(self, path, filename, deps, fileformat)
self._x86d: Optional[X86Dictionary] = None
self._functions: Dict[str, X86Function] = {}

Expand Down
Loading