-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsimplify_expression.py
More file actions
134 lines (125 loc) · 4.01 KB
/
simplify_expression.py
File metadata and controls
134 lines (125 loc) · 4.01 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
import logging
import time
from argparse import ArgumentParser
from pathlib import Path
from msynth import Simplifier
from miasm.expression.expression import Expr, ExprId, ExprInt
logger = logging.getLogger("msynth")
def setup_logging() -> None:
"""Setup logger"""
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
logger.setLevel(logging.INFO)
console_handler.setFormatter(
logging.Formatter("%(name)-18s - %(levelname)-8s: %(message)s")
)
logger.addHandler(console_handler)
def mba(size: int) -> Expr:
"""Generate exemplary MBA expression (for testing/debug purposes)"""
v0 = ExprId("v0", size)
v1 = ExprId("v1", size)
v2 = ExprId("v2", size)
return (
(
(~v0 | v2)
- ~(
(~((((ExprInt(0x1, size) + v2) - ExprInt(0x1, size)) | ~v0) - ~v0) & v2)
+ (v2 + (v2 & ~v2))
)
)
+ (
(
(v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
+ (v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (v0 ^ (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (
(
ExprInt(0x1, size)
+ ~(
(
(v1 + (~v1 & v2))
| ((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2))
)
- (v1 & (v1 + (~v1 & v2)))
)
)
+ ((-(-v0)) + (((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2)) & ~v2))
)
+ (
(~v0 | v2)
- ~(
(~((((ExprInt(0x1, size) + v2) - ExprInt(0x1, size)) | ~v0) - ~v0) & v2)
+ (v2 + (v2 & ~v2))
)
)
+ (
(
(v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
+ (v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (v0 ^ (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (
(
ExprInt(0x1, size)
+ ~(
(
(v1 + (~v1 & v2))
| ((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2))
)
- (v1 & (v1 + (~v1 & v2)))
)
)
+ ((-(-v0)) + (((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2)) & ~v2))
)
+ (
(~v0 | v2)
- ~(
(~((((ExprInt(0x1, size) + v2) - ExprInt(0x1, size)) | ~v0) - ~v0) & v2)
+ (v2 + (v2 & ~v2))
)
)
+ (
(
(v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
+ (v0 & (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (v0 ^ (((v0 & v1) + (v0 & v1)) + (v0 ^ v1)))
)
+ (
(
ExprInt(0x1, size)
+ ~(
(
(v1 + (~v1 & v2))
| ((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2))
)
- (v1 & (v1 + (~v1 & v2)))
)
)
+ ((-(-v0)) + (((v1 + (~v1 & v2)) + (~(v1 + (~v1 & v2)) & v2)) & ~v2))
)
)
def main(oracle_path: Path, expr: Expr) -> None:
start_time = time.time()
# init simplification engine
logger.debug("Initializing simplification engine")
simplifier = Simplifier(oracle_path)
logger.info("Simplifying expression")
# simplify expression
simplified = simplifier.simplify(expr)
print(f"initial: {expr}")
print(f"simplified: {simplified}")
logger.info(f"Done in {round(time.time() - start_time, 2)} seconds")
if __name__ == "__main__":
parser = ArgumentParser(
description="Simplify expressions (especially MBAs) using a precomputed Oracle"
)
parser.add_argument(
"oracle_path", type=Path, help="Path to (serialized) precomputed oracle"
)
args = parser.parse_args()
setup_logging()
main(args.oracle_path, mba(32))