Skip to content

Commit 1aa8842

Browse files
committed
MemoryManager should work now and move check_and_eval to misc.py
1 parent da79d89 commit 1aa8842

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

qiling/debugger/qdb/branch_predictor/branch_predictor_x86.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from .branch_predictor import *
1111
from ..arch import ArchX86
12+
from ..misc import check_and_eval
1213

1314
class BranchPredictorX86(BranchPredictor, ArchX86):
1415
"""
@@ -98,12 +99,6 @@ def predict(self):
9899

99100
if prophecy.going:
100101
takeaway_list = ["ptr", "dword", "[", "]"]
101-
class AST_checker(ast.NodeVisitor):
102-
def generic_visit(self, node):
103-
if type(node) in (ast.Module, ast.Expr, ast.BinOp, ast.Constant, ast.Add, ast.Mult, ast.Sub):
104-
ast.NodeVisitor.generic_visit(self, node)
105-
else:
106-
raise ParseError("malform or invalid ast node")
107102

108103
if len(line.op_str.split()) > 1:
109104
new_line = line.op_str.replace(":", "+")
@@ -119,12 +114,8 @@ def generic_visit(self, node):
119114
if each_reg in new_line:
120115
new_line = re.sub(each_reg, hex(self.read_reg(each_reg)), new_line)
121116

122-
checker = AST_checker()
123-
ast_tree = ast.parse(new_line)
124117

125-
checker.visit(ast_tree)
126-
127-
prophecy.where = eval(new_line)
118+
prophecy.where = check_and_eval(new_line)
128119

129120
elif line.op_str in self.ql.reg.register_mapping:
130121
prophecy.where = self.ql.reg.read(line.op_str)

qiling/debugger/qdb/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def try_read_pointer(self, address: int) -> Optional[bytes]:
7474
try to read pointer size of data from ql.mem
7575
"""
7676

77-
return self.try_read(address, self.pointersize)
77+
return self.try_read(address, self.arch_insn_size)
7878

7979
def read_string(self, address: int) -> Optional[str]:
8080
"""

qiling/debugger/qdb/memory.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from .context import Context
99
from .arch import ArchCORTEX_M, ArchARM, ArchMIPS, ArchX86
10-
import re, ast, math
10+
from .misc import check_and_eval
11+
import re, math
1112

1213

1314

@@ -122,7 +123,12 @@ def parse(self, line: str):
122123
if (regs_dict := getattr(self, "regs_need_swapped", None)):
123124
for each in rest:
124125
for reg in regs_dict:
125-
line.append(regs_dict[each] if each in regs_dict else each)
126+
if each in regs_dict:
127+
line.append(regs_dict[each])
128+
else:
129+
line.append(each)
130+
else:
131+
line = rest
126132

127133
# for simple calculation with register and address
128134

@@ -133,20 +139,13 @@ def parse(self, line: str):
133139
if reg in line:
134140
line = re.sub(f"\{reg}", hex(self.ql.reg.read(each_reg)), line)
135141

136-
class AST_checker(ast.NodeVisitor):
137-
def generic_visit(self, node):
138-
if type(node) in (ast.Module, ast.Expr, ast.BinOp, ast.Constant, ast.Add, ast.Mult, ast.Sub):
139-
ast.NodeVisitor.generic_visit(self, node)
140-
else:
141-
raise ParseError("malform or invalid ast node")
142142

143143
ft, sz, ct = fmt
144144

145-
checker = AST_checker()
146-
ast_tree = ast.parse(line)
147-
checker.visit(ast_tree)
148-
149-
addr = eval(line)
145+
try:
146+
addr = check_and_eval(line)
147+
except:
148+
return "something went wrong ..."
150149

151150
if ft == "i":
152151
output = self.handle_i(addr, ct)

qiling/debugger/qdb/misc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55

66
from typing import Callable, Optional
77

8+
import ast
9+
10+
def check_and_eval(line: str):
11+
"""
12+
This function will valid all type of nodes and evaluate it if nothing went wrong
13+
"""
14+
15+
class AST_checker(ast.NodeVisitor):
16+
def generic_visit(self, node):
17+
if type(node) in (ast.Module, ast.Expr, ast.BinOp, ast.Constant, ast.Add, ast.Mult, ast.Sub):
18+
ast.NodeVisitor.generic_visit(self, node)
19+
else:
20+
raise ParseError("malform or invalid ast node")
21+
22+
checker = AST_checker()
23+
ast_tree = ast.parse(line)
24+
checker.visit(ast_tree)
25+
26+
return eval(line)
27+
828

929
class Breakpoint:
1030
"""

qiling/debugger/qdb/qdb.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,8 @@ def do_examine(self, line: str) -> None:
322322
e.g. x/4wx 0x41414141 , print 4 word size begin from address 0x41414141 in hex
323323
"""
324324

325-
# try:
326325
if type(err_msg := self.mm.parse(line)) is str:
327326
qdb_print(QDB_MSG.ERROR, err_msg)
328-
# except:
329-
# qdb_print(QDB_MSG.ERROR, "something went wrong ...")
330327

331328
def do_start(self, *args) -> None:
332329
"""

qiling/debugger/qdb/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
)
2727

2828
from .const import color, QDB_MSG
29+
2930

3031

3132
def qdb_print(msgtype: QDB_MSG, msg: str) -> None:

0 commit comments

Comments
 (0)