|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# CodeHawk Binary Analyzer |
| 3 | +# Author: Henny Sipma |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2021-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 | +"""Collects stmt and instruction provenance for pir inspector.""" |
| 28 | + |
| 29 | +from typing import Dict, List, TYPE_CHECKING |
| 30 | + |
| 31 | +import chb.ast.ASTNode as AST |
| 32 | +from chb.ast.ASTNOPVisitor import ASTNOPVisitor |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + from chb.ast.ASTDeserializer import ASTFunctionDeserialization |
| 36 | + |
| 37 | + |
| 38 | +class ASTProvenanceCollector(ASTNOPVisitor): |
| 39 | + |
| 40 | + def __init__(self, dfn: "ASTFunctionDeserialization") -> None: |
| 41 | + self._dfn = dfn |
| 42 | + self._hl_ll_instr_mapping: Dict[int, List[int]] = {} |
| 43 | + |
| 44 | + @property |
| 45 | + def dfn(self) -> "ASTFunctionDeserialization": |
| 46 | + return self._dfn |
| 47 | + |
| 48 | + @property |
| 49 | + def provenance(self) -> Dict[int, List[AST.ASTInstruction]]: |
| 50 | + result: Dict[int, List[AST.ASTInstruction]] = {} |
| 51 | + for hlinstrid in self._hl_ll_instr_mapping: |
| 52 | + llinstrids = self._hl_ll_instr_mapping[hlinstrid] |
| 53 | + result[hlinstrid] = [self.dfn.get_instruction(i) for i in llinstrids] |
| 54 | + return result |
| 55 | + |
| 56 | + def instruction_provenance( |
| 57 | + self, stmt: AST.ASTStmt) -> Dict[int, List[AST.ASTInstruction]]: |
| 58 | + stmt.accept(self) |
| 59 | + return self.provenance |
| 60 | + |
| 61 | + def visit_loop_stmt(self, stmt: AST.ASTLoop) -> None: |
| 62 | + stmt.body.accept(self) |
| 63 | + |
| 64 | + def visit_block_stmt(self, stmt: AST.ASTBlock) -> None: |
| 65 | + for s in stmt.stmts: |
| 66 | + s.accept(self) |
| 67 | + |
| 68 | + def visit_instruction_sequence_stmt(self, stmt: AST.ASTInstrSequence) -> None: |
| 69 | + for i in stmt.instructions: |
| 70 | + if i.instrid in self.dfn.astree.provenance.instruction_mapping: |
| 71 | + self._hl_ll_instr_mapping[i.instrid] = ( |
| 72 | + self.dfn.astree.provenance.instruction_mapping[i.instrid]) |
| 73 | + |
| 74 | + def visit_branch_stmt(self, stmt: AST.ASTBranch) -> None: |
| 75 | + stmt.ifstmt.accept(self) |
| 76 | + stmt.elsestmt.accept(self) |
0 commit comments