Skip to content

Commit 9cd4fb3

Browse files
committed
Opportunistic PEP8 and linter-friendly tweaks
1 parent 95a1651 commit 9cd4fb3

File tree

7 files changed

+76
-71
lines changed

7 files changed

+76
-71
lines changed

qiling/arch/cortex_m.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import cached_property
77
from contextlib import ContextDecorator
88

9-
from unicorn import Uc, UcError, UC_ARCH_ARM, UC_MODE_ARM, UC_MODE_MCLASS, UC_MODE_THUMB, UC_ERR_OK
9+
from unicorn import UC_ARCH_ARM, UC_MODE_ARM, UC_MODE_MCLASS, UC_MODE_THUMB
1010
from capstone import Cs, CS_ARCH_ARM, CS_MODE_ARM, CS_MODE_MCLASS, CS_MODE_THUMB
1111
from keystone import Ks, KS_ARCH_ARM, KS_MODE_ARM, KS_MODE_THUMB
1212

qiling/const.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python3
2-
#
2+
#
33
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44
#
55

66
from enum import Enum, Flag, IntEnum
7-
from typing import Mapping, Type, TypeVar
7+
from typing import Final, Mapping, Type, TypeVar
8+
89

910
class QL_ENDIAN(IntEnum):
1011
EL = 1
1112
EB = 2
1213

14+
1315
class QL_ARCH(IntEnum):
1416
X86 = 101
1517
X8664 = 102
@@ -23,6 +25,7 @@ class QL_ARCH(IntEnum):
2325
RISCV64 = 111
2426
PPC = 112
2527

28+
2629
class QL_OS(IntEnum):
2730
LINUX = 201
2831
FREEBSD = 202
@@ -35,44 +38,53 @@ class QL_OS(IntEnum):
3538
MCU = 209
3639
BLOB = 210
3740

41+
3842
class QL_VERBOSE(IntEnum):
39-
DISABLED = -1 # turn off all the output
40-
OFF = 0 # output only warnings
41-
DEFAULT = 1 # output warnings and Qiling execute process information
42-
DEBUG = 4 # output all logs above and debug information, include syscall information
43-
DISASM = 10 # output all assembly instructions during Qiling execution
44-
DUMP = 20 # output any log Qiling can, include instructions and registers
43+
DISABLED = -1 # turn off all the output
44+
OFF = 0 # output only warnings
45+
DEFAULT = 1 # output warnings and Qiling execute process information
46+
DEBUG = 4 # output all logs above and debug information, include syscall information
47+
DISASM = 10 # output all assembly instructions during Qiling execution
48+
DUMP = 20 # output any log Qiling can, include instructions and registers
49+
4550

4651
class QL_DEBUGGER(IntEnum):
4752
GDB = 1
4853
IDAPRO = 2
4954
QDB = 3
5055

56+
5157
class QL_INTERCEPT(IntEnum):
5258
CALL = 1
5359
ENTER = 2
5460
EXIT = 3
5561

62+
5663
class QL_STOP(Flag):
5764
NONE = 0
5865
STACK_POINTER = (1 << 0)
59-
EXIT_TRAP = (1 << 1)
66+
EXIT_TRAP = (1 << 1)
67+
68+
69+
QL_ARCH_INTERPRETER: Final = (QL_ARCH.EVM,)
6070

61-
QL_ARCH_INTERPRETER = (QL_ARCH.EVM,)
71+
QL_OS_POSIX: Final = (QL_OS.LINUX, QL_OS.FREEBSD, QL_OS.MACOS, QL_OS.QNX)
72+
QL_OS_BAREMETAL: Final = (QL_OS.MCU,)
6273

63-
QL_OS_POSIX = (QL_OS.LINUX, QL_OS.FREEBSD, QL_OS.MACOS, QL_OS.QNX)
64-
QL_OS_BAREMETAL = (QL_OS.MCU,)
6574

6675
QL_HOOK_BLOCK = 0b0001
6776
QL_CALL_BLOCK = 0b0010
6877

6978
T = TypeVar('T', bound=Enum)
79+
80+
7081
def __casefold_enum(e: Type[T]) -> Mapping[str, T]:
7182
'''Create a casefolded mapping of an enum to allow case-insensitive lookup.
7283
'''
7384

7485
return dict((k.casefold(), v) for k, v in e._member_map_.items())
7586

87+
7688
debugger_map = __casefold_enum(QL_DEBUGGER)
7789
arch_map = __casefold_enum(QL_ARCH)
7890
os_map = __casefold_enum(QL_OS)

qiling/debugger/gdb/utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from typing import Optional
77

88
from qiling import Qiling
9-
from qiling.const import QL_ARCH
109

1110
# this code is partially based on uDbg
1211
# @see: https://github.com/iGio90/uDdbg
1312

1413
PROMPT = r'gdb>'
1514

15+
1616
class QlGdbUtils:
1717
def __init__(self, ql: Qiling, entry_point: int, exit_point: int):
1818
self.ql = ql
@@ -32,7 +32,6 @@ def __entry_point_hook(ql: Qiling):
3232
# that hook will be used to set up the breakpoint handling hook
3333
ep_hret = ql.hook_address(__entry_point_hook, entry_point)
3434

35-
3635
def dbg_hook(self, ql: Qiling, address: int, size: int):
3736
if getattr(ql.arch, 'is_thumb', False):
3837
address |= 1
@@ -53,18 +52,15 @@ def dbg_hook(self, ql: Qiling, address: int, size: int):
5352
# ql.log.debug(f'{PROMPT} emulation entrypoint at {self.entry_point:#x}')
5453
# ql.log.debug(f'{PROMPT} emulation exitpoint at {self.exit_point:#x}')
5554

56-
5755
def bp_insert(self, addr: int):
5856
if addr not in self.bp_list:
5957
self.bp_list.append(addr)
6058
self.ql.log.info(f'{PROMPT} breakpoint added at {addr:#x}')
6159

62-
6360
def bp_remove(self, addr: int):
6461
self.bp_list.remove(addr)
6562
self.ql.log.info(f'{PROMPT} breakpoint removed from {addr:#x}')
6663

67-
6864
def resume_emu(self, address: Optional[int] = None, steps: int = 0):
6965
if address is None:
7066
address = self.ql.arch.regs.arch_pc

qiling/debugger/qdb/qdb.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44
#
55

6-
from typing import Callable, Optional, Mapping, Tuple, Union
7-
86
import cmd
97

8+
from typing import Optional, Tuple, Union
9+
1010
from qiling import Qiling
11-
from qiling.const import QL_OS, QL_ARCH, QL_VERBOSE
11+
from qiling.const import QL_OS, QL_ARCH
1212
from qiling.debugger import QlDebugger
1313

1414
from .utils import setup_context_render, setup_branch_predictor, setup_address_marker, SnapshotManager, run_qdb_script
@@ -18,6 +18,7 @@
1818

1919
from .utils import QDB_MSG, qdb_print
2020

21+
2122
class QlQdb(cmd.Cmd, QlDebugger):
2223
"""
2324
The built-in debugger of Qiling Framework
@@ -342,7 +343,7 @@ def do_examine(self, line: str) -> None:
342343

343344
if type(err_msg := self.mm.parse(line)) is str:
344345
qdb_print(QDB_MSG.ERROR, err_msg)
345-
346+
346347

347348
def do_set(self, line: str) -> None:
348349
"""

qiling/os/linux/thread.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(self, ql: Qiling, start_address: int, exit_point: int, context = No
9191
@property
9292
def ql(self):
9393
return self._ql
94-
94+
9595
@ql.setter
9696
def ql(self, q):
9797
self._ql = q
@@ -107,23 +107,23 @@ def saved_context(self, ctx):
107107
@property
108108
def exit_point(self):
109109
return self._exit_point
110-
110+
111111
@exit_point.setter
112112
def exit_point(self, ep):
113113
self._exit_point = ep
114-
114+
115115
@property
116116
def start_address(self):
117117
return self._start_address
118-
118+
119119
@start_address.setter
120120
def start_address(self, sa):
121121
self._start_address = sa
122122

123123
@property
124124
def status(self):
125125
return self._status
126-
126+
127127
@status.setter
128128
def status(self, s):
129129
self._status = s
@@ -150,46 +150,46 @@ def id(self):
150150

151151
def __hash__(self):
152152
return self.id
153-
153+
154154
def __str__(self):
155155
return f"[Thread {self.id}]"
156156

157157
@property
158158
def set_child_tid_address(self):
159159
return self._set_child_tid_address
160-
160+
161161
@set_child_tid_address.setter
162162
def set_child_tid_address(self, addr):
163163
self._set_child_tid_address = addr
164-
164+
165165
@property
166166
def clear_child_tid_address(self):
167167
return self._clear_child_tid_address
168168

169169
@clear_child_tid_address.setter
170170
def clear_child_tid_address(self, addr):
171171
self._clear_child_tid_address = addr
172-
172+
173173
@property
174174
def robust_list_head_ptr(self):
175175
return self._robust_list_head_ptr
176-
176+
177177
@robust_list_head_ptr.setter
178178
def robust_list_head_ptr(self, p):
179179
self._robust_list_head_ptr = p
180-
180+
181181
@property
182182
def robust_list_head_len(self):
183183
return self._robust_list_head_len
184-
184+
185185
@robust_list_head_len.setter
186186
def robust_list_head_len(self, l):
187187
self._robust_list_head_len = l
188188

189189
@property
190190
def sched_cb(self) -> Callable:
191191
return self._sched_cb
192-
192+
193193
@sched_cb.setter
194194
def sched_cb(self, cb):
195195
self._sched_cb = cb
@@ -207,7 +207,7 @@ def _run(self):
207207
# Within both contexts, our program is single thread.
208208
#
209209
# The only fail safe: **Never give up control in Unicorn context.**
210-
#
210+
#
211211
# In Unicorn context, in other words, in Unicorn callbacks, we do:
212212
# - Implement non-blocking syscalls directly.
213213
# - Prepare sched_cb for non-unicorn context.
@@ -219,7 +219,7 @@ def _run(self):
219219
self.ql.arch.regs.arch_pc = self.start_address
220220
if not self._saved_context:
221221
self.save()
222-
222+
223223
while self.status != THREAD_STATUS_TERMINATED:
224224
# Rewrite our status and the current thread.
225225
self.status = THREAD_STATUS_RUNNING
@@ -235,7 +235,7 @@ def _run(self):
235235
# Run and log the run event
236236
start_address = getattr(self.ql.arch, 'effective_pc', self.ql.arch.regs.arch_pc) # For arm thumb.
237237
self.sched_cb = QlLinuxThread._default_sched_cb
238-
238+
239239
self.ql.log.debug(f"Scheduled from {hex(start_address)}.")
240240
try:
241241
# Known issue for timeout: https://github.com/unicorn-engine/unicorn/issues/1355
@@ -246,7 +246,7 @@ def _run(self):
246246
raise e
247247
self.ql.log.debug(f"Suspended at {hex(self.ql.arch.regs.arch_pc)}")
248248
self.save()
249-
249+
250250
# Note that this callback may be set by UC callbacks.
251251
# Some thought on this design:
252252
# 1. Never give up control during a UC callback.
@@ -275,16 +275,16 @@ def restore(self):
275275
@abstractmethod
276276
def set_thread_tls(self, tls_addr):
277277
pass
278-
278+
279279
@abstractmethod
280280
def clone(self):
281281
# This is a workaround to implement our thread based on gevent greenlet.
282282
# Core idea:
283283
# A gevent greenlet can't re-run if it has finished _run method but our framework requires threads to be resumed anytime. Therefore, a workaround is to
284284
# use multiple greenlets to represent a single qiling thread.
285-
#
285+
#
286286
# Of course we can make the greenlet run forever and wait for notifications to resume but that would make the design much more complicated.
287-
#
287+
#
288288
# Caveat:
289289
# Don't use thread id to identify the thread object.
290290
new_thread = self.ql.os.thread_class.spawn(self._ql, self._start_address, self._exit_point, self._saved_context, set_child_tid_addr = None, thread_id = self._thread_id)
@@ -435,7 +435,7 @@ def restore(self):
435435
self.restore_context()
436436
self.set_thread_tls(self.tls)
437437
self.ql.log.debug(f"Restored context: fs={hex(self.ql.arch.regs.fsbase)} tls={hex(self.tls)}")
438-
438+
439439
def clone(self):
440440
new_thread = super(QlLinuxX8664Thread, self).clone()
441441
new_thread.tls = self.tls
@@ -458,7 +458,7 @@ def set_thread_tls(self, tls_addr):
458458
def save(self):
459459
self.save_context()
460460
self.tls = self.ql.arch.regs.cp0_userlocal
461-
self.ql.log.debug(f"Saved context. cp0={hex(self.ql.arch.regs.cp0_userlocal)}")
461+
self.ql.log.debug(f"Saved context. cp0={hex(self.ql.arch.regs.cp0_userlocal)}")
462462

463463
def restore(self):
464464
self.restore_context()
@@ -492,7 +492,7 @@ def restore(self):
492492
self.restore_context()
493493
self.set_thread_tls(self.tls)
494494
self.ql.log.debug(f"Restored context. c13_c0_3={hex(self.ql.arch.regs.c13_c0_3)}")
495-
495+
496496
def clone(self):
497497
new_thread = super(QlLinuxARMThread, self).clone()
498498
new_thread.tls = self.tls
@@ -519,7 +519,7 @@ def restore(self):
519519
self.restore_context()
520520
self.set_thread_tls(self.tls)
521521
self.ql.log.debug(f"Restored context. tpidr_el0={hex(self.ql.arch.regs.tpidr_el0)}")
522-
522+
523523
def clone(self):
524524
new_thread = super(QlLinuxARM64Thread, self).clone()
525525
new_thread.tls = self.tls
@@ -545,7 +545,7 @@ def cur_thread(self, ct):
545545
@property
546546
def main_thread(self):
547547
return self._main_thread
548-
548+
549549
@main_thread.setter
550550
def main_thread(self, mt):
551551
self._main_thread = mt
@@ -570,7 +570,7 @@ def _clear_queued_msg(self):
570570
self.main_thread.log_file_fd.log(lvl, msg)
571571
except AttributeError:
572572
pass
573-
573+
574574
def _prepare_lib_patch(self):
575575
if self.ql.loader.elf_entry != self.ql.loader.entry_point:
576576
entry_address = self.ql.loader.elf_entry

0 commit comments

Comments
 (0)