forked from angr/angr-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rename_functions.py
129 lines (100 loc) · 5.11 KB
/
test_rename_functions.py
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
# pylint:disable=missing-class-docstring,wrong-import-order
from __future__ import annotations
import os
import sys
import threading
import unittest
import angr
from angr.analyses.decompiler.structured_codegen.c import CFunction, CFunctionCall
from common import start_main_window_and_event_loop, test_location
from PySide6.QtCore import Qt
from PySide6.QtTest import QTest
from angrmanagement.logic.threads import gui_thread_schedule
from angrmanagement.ui.dialogs.rename_label import RenameLabel
from angrmanagement.ui.dialogs.rename_node import RenameNode
from angrmanagement.ui.views import CodeView, DisassemblyView
class TestRenameFunctions(unittest.TestCase):
def setUp(self):
self.event = threading.Event()
_, self.main = start_main_window_and_event_loop(self.event)
def tearDown(self) -> None:
self.event.set()
del self.main
def _test_rename_a_function_in_disasm_and_pseudocode_views(self):
main = self.main
func = main.workspace.main_instance.project.kb.functions["main"]
disasm_view = main.workspace._get_or_create_view("disassembly", DisassemblyView)
pseudocode_view = main.workspace._get_or_create_view("pseudocode", CodeView)
# find the node for function
for _, item in pseudocode_view.codegen.map_pos_to_node.items():
if isinstance(item.obj, CFunction):
func_node = item.obj
break
else:
self.fail("The CFunction instance is not found.")
self.assertEqual(func_node.name, "main")
# rename the function in the disassembly view
rlabel = RenameLabel(disasm_view, func.addr, parent=None)
rlabel._name_box.setText("")
QTest.keyClicks(rlabel._name_box, "asdf")
QTest.mouseClick(rlabel._ok_button, Qt.MouseButton.LeftButton)
self.assertEqual(func.name, "asdf")
self.assertEqual(func_node.name, "main")
# rename the function in the pseudocode view
rnode = RenameNode(code_view=pseudocode_view, node=func_node)
rnode._name_box.setText("")
QTest.keyClicks(rnode._name_box, "fdsa")
QTest.mouseClick(rnode._ok_button, Qt.MouseButton.LeftButton)
self.assertEqual(func.name, "fdsa")
def test_rename_a_function_in_disasm_and_pseudocode_views(self):
main = self.main
binpath = os.path.join(test_location, "x86_64", "fauxware")
main.workspace.main_instance.project.am_obj = angr.Project(binpath, auto_load_libs=False)
main.workspace.main_instance.project.am_event()
main.workspace.job_manager.join_all_jobs()
func = main.workspace.main_instance.project.kb.functions["main"]
self.assertIsNotNone(func)
# decompile the function
disasm_view = main.workspace._get_or_create_view("disassembly", DisassemblyView)
disasm_view.display_disasm_graph()
gui_thread_schedule(disasm_view.display_function, args=(func,))
disasm_view.decompile_current_function()
main.workspace.job_manager.join_all_jobs()
# run the jobless method in the GUI thread
gui_thread_schedule(self._test_rename_a_function_in_disasm_and_pseudocode_views)
def _test_rename_a_callee_in_pseudocode_view(self):
main = self.main
func = main.workspace.main_instance.project.kb.functions["authenticate"]
_ = main.workspace._get_or_create_view("disassembly", DisassemblyView)
pseudocode_view = main.workspace._get_or_create_view("pseudocode", CodeView)
# find the node for function
for _, item in pseudocode_view.codegen.map_pos_to_node.items():
if isinstance(item.obj, CFunctionCall) and item.obj.callee_func.name == "authenticate":
func_node = item.obj
break
else:
self.fail("The CFunction instance is not found.")
# rename the function in the pseudocode view
rnode = RenameNode(code_view=pseudocode_view, node=func_node)
rnode._name_box.setText("")
QTest.keyClicks(rnode._name_box, "authenticate_1337")
QTest.mouseClick(rnode._ok_button, Qt.MouseButton.LeftButton)
self.assertEqual(func.name, "authenticate_1337")
def test_rename_a_callee_in_pseudocode_view(self):
main = self.main
binpath = os.path.join(test_location, "x86_64", "fauxware")
main.workspace.main_instance.project.am_obj = angr.Project(binpath, auto_load_libs=False)
main.workspace.main_instance.project.am_event()
main.workspace.job_manager.join_all_jobs()
func = main.workspace.main_instance.project.kb.functions["main"]
self.assertIsNotNone(func)
# decompile the function
disasm_view = main.workspace._get_or_create_view("disassembly", DisassemblyView)
disasm_view.display_disasm_graph()
gui_thread_schedule(disasm_view.display_function, args=(func,))
disasm_view.decompile_current_function()
main.workspace.job_manager.join_all_jobs()
# run the jobless method in the GUI thread
gui_thread_schedule(self._test_rename_a_callee_in_pseudocode_view)
if __name__ == "__main__":
unittest.main(argv=sys.argv)