forked from static-analysis-engineering/CodeHawk-Binary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTICodeTransformer.py
More file actions
151 lines (133 loc) · 6.22 KB
/
ASTICodeTransformer.py
File metadata and controls
151 lines (133 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# ------------------------------------------------------------------------------
# CodeHawk Binary Analyzer
# Author: Henny Sipma
# ------------------------------------------------------------------------------
# The MIT License (MIT)
#
# Copyright (c) 2022-2025 Aarno Labs LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ------------------------------------------------------------------------------
"""Transforms from low-level to high-level code by removing instructions.
Assignment instructions are removed based on whether their left-hand side
is used later, which is, in turn, based on the def-use-high of the left-hand
side.
"""
from typing import cast, List, TYPE_CHECKING
from chb.ast.ASTIdentityTransformer import ASTIdentityTransformer
import chb.ast.ASTNode as AST
from chb.util.loggingutil import chklogger
if TYPE_CHECKING:
from chb.astinterface.ASTInterface import ASTInterface
from chb.astinterface.ASTIProvenance import ASTIProvenance
class ASTICodeTransformer(ASTIdentityTransformer):
def __init__(
self,
astinterface: "ASTInterface") -> None:
self._astinterface = astinterface
@property
def astinterface(self) -> "ASTInterface":
return self._astinterface
@property
def provenance(self) -> "ASTIProvenance":
return self.astinterface.astiprovenance
def transform_stmt(self, stmt: AST.ASTStmt) -> AST.ASTStmt:
return stmt.transform(self)
def transform_loop_stmt(self, stmt: AST.ASTLoop) -> AST.ASTStmt:
newbody = stmt.body.transform(self)
return self.astinterface.mk_loop(
newbody, mergeaddr=stmt.breakaddr, continueaddr=stmt.continueaddr,
optlocationid=stmt.locationid)
def transform_block_stmt(self, stmt: AST.ASTBlock) -> AST.ASTStmt:
newstmts: List[AST.ASTStmt] = []
for s in stmt.stmts:
newstmt = s.transform(self)
# prune empty blocks that may have been created by the pruning
# of redundant if statements.
# StmtLabels may be intermixed with statements, hence the check
# for is_stmt_label.
if (
not newstmt.is_stmt_label
and newstmt.is_ast_block
and len((cast(AST.ASTBlock, newstmt)).stmts) == 0):
continue
newstmts.append(newstmt)
return self.astinterface.mk_block(
newstmts,
labels=stmt.labels,
optlocationid=stmt.locationid)
def transform_instruction_sequence_stmt(
self, stmt: AST.ASTInstrSequence) -> AST.ASTStmt:
instrs: List[AST.ASTInstruction] = []
for instr in stmt.instructions:
if instr.is_ast_assign:
instr = cast(AST.ASTAssign, instr)
if (
self.astinterface.has_ssa_value(str(instr.lhs))
and not self.provenance.has_expose_instruction(instr.instrid)):
chklogger.logger.info(
"Remove [%s]: has ssa value", str(instr))
elif self.provenance.has_active_lval_defuse_high(instr.lhs.lvalid):
chklogger.logger.info(
"Transform [%s]: active lval_defuse_high: %s",
str(instr),
self.provenance.active_lval_defuse_high(instr.lhs.lvalid))
instrs.append(instr)
elif self.provenance.has_lval_store(instr.lhs.lvalid):
chklogger.logger.info(
"Transform [%s]: lval_store", str(instr))
instrs.append(instr)
elif self.provenance.has_expose_instruction(instr.instrid):
chklogger.logger.info(
"Transform [%s]: expose instruction", str(instr))
instrs.append(instr)
elif instr.lhs.lhost.is_global:
chklogger.logger.info(
"Transform [%s]: global lhs", str(instr))
instrs.append(instr)
else:
chklogger.logger.info("Transform [%s]: remove", str(instr))
else:
chklogger.logger.info(
"Transform [%s]: include by default", str(instr))
instrs.append(instr)
return self.astinterface.mk_instr_sequence(
instrs,
labels=stmt.labels,
optlocationid=stmt.locationid)
def transform_branch_stmt(self, stmt: AST.ASTBranch) -> AST.ASTStmt:
newif = stmt.ifstmt.transform(self)
newelse = stmt.elsestmt.transform(self)
if newif.is_empty() and newelse.is_empty():
return self.astinterface.mk_block([])
return self.astinterface.mk_branch(
stmt.condition,
newif,
newelse,
stmt.target_address,
mergeaddr=stmt.merge_address,
optlocationid=stmt.locationid,
predicated=stmt.predicated)
def transform_switch_stmt(self, stmt: AST.ASTSwitchStmt) -> AST.ASTStmt:
newcases = stmt.cases.transform(self)
return self.astinterface.mk_switch_stmt(
stmt.switchexpr,
newcases,
stmt.merge_address,
optlocationid=stmt.locationid)