|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# CodeHawk Binary Analyzer |
| 3 | +# Author: Henny Sipma |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2024 Aarno Labs LLC |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in all |
| 17 | +# copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +# SOFTWARE. |
| 26 | +# ------------------------------------------------------------------------------ |
| 27 | +"""List of application function addresses and cfg characteristics.""" |
| 28 | + |
| 29 | +import xml.etree.ElementTree as ET |
| 30 | + |
| 31 | +from typing import Dict, List, Optional |
| 32 | + |
| 33 | + |
| 34 | +class FnCfgInfo: |
| 35 | + |
| 36 | + def __init__(self, xnode: ET.Element) -> None: |
| 37 | + self.xnode = xnode |
| 38 | + |
| 39 | + @property |
| 40 | + def faddr(self) -> str: |
| 41 | + return self.xnode.get("va", "0") |
| 42 | + |
| 43 | + @property |
| 44 | + def faddr_i(self) -> int: |
| 45 | + return int(self.xnode.get("va", "0"), 16) |
| 46 | + |
| 47 | + @property |
| 48 | + def basic_blocks(self) -> int: |
| 49 | + return int(self.xnode.get("bc", "0")) |
| 50 | + |
| 51 | + @property |
| 52 | + def instructions(self) -> int: |
| 53 | + return int(self.xnode.get("ic", "0")) |
| 54 | + |
| 55 | + @property |
| 56 | + def loops(self) -> int: |
| 57 | + return int(self.xnode.get("lc", "0")) |
| 58 | + |
| 59 | + @property |
| 60 | + def max_loopdepth(self) -> int: |
| 61 | + return int(self.xnode.get("ld", "0")) |
| 62 | + |
| 63 | + @property |
| 64 | + def has_error(self) -> bool: |
| 65 | + return self.xnode.get("tr", "ok") == "x" |
| 66 | + |
| 67 | + @property |
| 68 | + def name(self) -> Optional[str]: |
| 69 | + return self.xnode.get("name") |
| 70 | + |
| 71 | + def __str__(self) -> str: |
| 72 | + return ( |
| 73 | + ("bc:" + str(self.basic_blocks)).ljust(10) |
| 74 | + + ("; ic: " + str(self.instructions)).ljust(14) |
| 75 | + + ("" if self.loops == 0 else ("; lc: " + str(self.loops)))) |
| 76 | + |
| 77 | + |
| 78 | +class AppCfgInfo: |
| 79 | + |
| 80 | + def __init__(self, xnode: Optional[ET.Element]) -> None: |
| 81 | + self.xnode = xnode |
| 82 | + self._function_cfg_infos: Optional[Dict[str, FnCfgInfo]] = None |
| 83 | + |
| 84 | + @property |
| 85 | + def function_cfg_infos(self) -> Dict[str, FnCfgInfo]: |
| 86 | + if self._function_cfg_infos is None: |
| 87 | + self._function_cfg_infos = {} |
| 88 | + self._initialize_functions() |
| 89 | + return self._function_cfg_infos |
| 90 | + |
| 91 | + @property |
| 92 | + def cfg_infos(self) -> List[FnCfgInfo]: |
| 93 | + return sorted( |
| 94 | + self.function_cfg_infos.values(), |
| 95 | + key = lambda c: c.faddr_i) |
| 96 | + |
| 97 | + def _initialize_functions(self) -> None: |
| 98 | + self._function_cfg_infos = {} |
| 99 | + if self.xnode is not None: |
| 100 | + for xf in self.xnode.findall("fn"): |
| 101 | + optva = xf.get("va") |
| 102 | + if optva is not None: |
| 103 | + self._function_cfg_infos[optva] = FnCfgInfo(xf) |
0 commit comments