|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# CodeHawk Binary Analyzer |
| 3 | +# Author: Henny Sipma |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2025 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 | + |
| 28 | +from typing import Dict, List, Optional, TYPE_CHECKING |
| 29 | + |
| 30 | +import chb.util.fileutil as UF |
| 31 | + |
| 32 | +from chb.util.DotGraph import DotGraph |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + from chb.app.Function import Function |
| 36 | + from chb.app.Instruction import Instruction |
| 37 | + from chb.astinterface.ASTInterface import ASTInterface |
| 38 | + |
| 39 | + |
| 40 | +class DotRdefPath: |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + graphname: str, |
| 45 | + fn: "Function", |
| 46 | + astree: "ASTInterface", |
| 47 | + path: List[str], |
| 48 | + nodeprefix: str = "", |
| 49 | + replacements: Dict[str, str] = {}, |
| 50 | + rdefinstrs: List[str] = [], |
| 51 | + subgraph: bool = False) -> None: |
| 52 | + |
| 53 | + self._fn = fn |
| 54 | + self._graphname = graphname |
| 55 | + self._astree = astree |
| 56 | + self._path = path |
| 57 | + self._nodeprefix = nodeprefix |
| 58 | + self._subgraph = subgraph |
| 59 | + self._replacements = replacements |
| 60 | + self._rdefinstrs = rdefinstrs |
| 61 | + self._dotgraph = DotGraph(graphname, subgraph=self.subgraph) |
| 62 | + |
| 63 | + @property |
| 64 | + def function(self) -> "Function": |
| 65 | + return self._fn |
| 66 | + |
| 67 | + @property |
| 68 | + def graphname(self) -> str: |
| 69 | + return self._graphname |
| 70 | + |
| 71 | + @property |
| 72 | + def astree(self) -> "ASTInterface": |
| 73 | + return self._astree |
| 74 | + |
| 75 | + @property |
| 76 | + def path(self) -> List[str]: |
| 77 | + return self._path |
| 78 | + |
| 79 | + @property |
| 80 | + def nodeprefix(self) -> str: |
| 81 | + return self._nodeprefix |
| 82 | + |
| 83 | + @property |
| 84 | + def subgraph(self) -> bool: |
| 85 | + return self._subgraph |
| 86 | + |
| 87 | + def pathindex(self, baddr: str) -> int: |
| 88 | + for (i, n) in enumerate(self.path): |
| 89 | + if n == baddr: |
| 90 | + return i |
| 91 | + raise UF.CHBError("Address " + baddr + " not found in path") |
| 92 | + |
| 93 | + def build(self) -> DotGraph: |
| 94 | + for n in self.path: |
| 95 | + self.add_node(n) |
| 96 | + |
| 97 | + for i in range(len(self.path) - 1): |
| 98 | + self.add_edge(self.path[i], self.path[i+1]) |
| 99 | + |
| 100 | + if self.init_is_exposed(): |
| 101 | + (fvar, _) = self.astree.get_formal_locindices(0) |
| 102 | + btype = fvar.bctyp |
| 103 | + self._dotgraph.add_node( |
| 104 | + self.nodeprefix + "init", |
| 105 | + labeltxt="{ init | " + str(btype) + " " + fvar.vname + "}", |
| 106 | + shaded=True, |
| 107 | + color="orange", |
| 108 | + recordformat=True) |
| 109 | + self._dotgraph.add_edge( |
| 110 | + self.nodeprefix + "init", self.nodeprefix + self.path[0]) |
| 111 | + |
| 112 | + return self._dotgraph |
| 113 | + |
| 114 | + def init_is_exposed(self) -> bool: |
| 115 | + result = True |
| 116 | + for p in self.path: |
| 117 | + instrs = self.rdef_instructions(p) |
| 118 | + if any(not instr.has_control_flow() for instr in instrs): |
| 119 | + result = False |
| 120 | + return result |
| 121 | + |
| 122 | + def is_exposed(self, n: str) -> bool: |
| 123 | + index = self.pathindex(n) |
| 124 | + for i in range(index + 1, len(self.path)): |
| 125 | + node = self.path[i] |
| 126 | + instrs = self.rdef_instructions(node) |
| 127 | + if any(not instr.has_control_flow() for instr in instrs): |
| 128 | + return False |
| 129 | + return True |
| 130 | + |
| 131 | + def replace_text(self, txt: str) -> str: |
| 132 | + result = txt |
| 133 | + for src in sorted(self._replacements, key=lambda x: len(x), reverse=True): |
| 134 | + result = result.replace(src, self._replacements[src]) |
| 135 | + return result |
| 136 | + |
| 137 | + def get_branch_instruction(self, n: str) -> Optional["Instruction"]: |
| 138 | + src = self.function.cfg.blocks[n] |
| 139 | + instraddr = src.lastaddr |
| 140 | + return self.function.instruction(instraddr) |
| 141 | + |
| 142 | + def rdef_instructions(self, n: str) -> List["Instruction"]: |
| 143 | + block = self.function.blocks[n] |
| 144 | + lastaddr = block.lastaddr |
| 145 | + baddr = int(n, 16) |
| 146 | + xaddr = int(lastaddr, 16) |
| 147 | + result: List["Instruction"] = [] |
| 148 | + for i in self._rdefinstrs: |
| 149 | + if i == "init": |
| 150 | + continue |
| 151 | + ix = int(i, 16) |
| 152 | + if ix >= baddr and ix <= xaddr: |
| 153 | + instr = block.instructions[i] |
| 154 | + result.append(instr) |
| 155 | + return result |
| 156 | + |
| 157 | + def add_node(self, n: str) -> None: |
| 158 | + nodename = self.nodeprefix + n |
| 159 | + rdefinstrs = self.rdef_instructions(n) |
| 160 | + blocktxt = n |
| 161 | + color: Optional[str] = None |
| 162 | + fillcolor: Optional[str] = None |
| 163 | + if len(rdefinstrs) > 0: |
| 164 | + conditions: List[str] = [] |
| 165 | + pinstrs: List[str] = [] |
| 166 | + for instr in rdefinstrs: |
| 167 | + (hlinstrs, _) = instr.ast_prov(self.astree) |
| 168 | + pinstrs.extend(str(hlinstr) for hlinstr in hlinstrs) |
| 169 | + if instr.has_control_flow(): |
| 170 | + (cc, _) = instr.ast_cc_condition_prov(self.astree) |
| 171 | + conditions.append(str(cc)) |
| 172 | + if self.is_exposed(n): |
| 173 | + if any(instr.has_control_flow() for instr in rdefinstrs): |
| 174 | + fillcolor = "yellow" |
| 175 | + else: |
| 176 | + fillcolor = "orange" |
| 177 | + if len(conditions) > 0: |
| 178 | + blocktxt = ( |
| 179 | + "{" + n + "|" + ("if " + "\\n".join(conditions)) |
| 180 | + + "|" + "\\n".join(pinstrs) + "}") |
| 181 | + else: |
| 182 | + blocktxt = ("{" + n + "|" + "\\n".join(pinstrs) + "}") |
| 183 | + self._dotgraph.add_node( |
| 184 | + str(nodename), |
| 185 | + labeltxt=blocktxt, |
| 186 | + shaded=True, |
| 187 | + color=color, |
| 188 | + fillcolor=fillcolor, |
| 189 | + recordformat=True) |
| 190 | + |
| 191 | + def add_edge(self, n1: str, n2: str) -> None: |
| 192 | + nodename1 = self.nodeprefix + n1 |
| 193 | + nodename2 = self.nodeprefix + n2 |
| 194 | + srcblock = self.function.block(n1) |
| 195 | + labeltxt: Optional[str] = None |
| 196 | + if len(self.function.cfg.edges[n1]) == 2: |
| 197 | + tgtedges = self.function.cfg.edges[n1] |
| 198 | + branchinstr = self.get_branch_instruction(n1) |
| 199 | + if branchinstr and branchinstr.is_branch_instruction: |
| 200 | + ftconds = branchinstr.ft_conditions |
| 201 | + if len(ftconds) == 2: |
| 202 | + if n2 == tgtedges[0]: |
| 203 | + astcond = branchinstr.ast_condition_prov( |
| 204 | + self.astree, reverse=True) |
| 205 | + else: |
| 206 | + astcond = branchinstr.ast_condition_prov( |
| 207 | + self.astree, reverse=False) |
| 208 | + labeltxt = str(astcond[0]) |
| 209 | + self._dotgraph.add_edge(nodename1, nodename2, labeltxt=labeltxt) |
0 commit comments