Skip to content

Commit d775c3d

Browse files
committed
clean up NONEOS and MCU group
1 parent e2e6c1d commit d775c3d

File tree

6 files changed

+80
-53
lines changed

6 files changed

+80
-53
lines changed

qiling/const.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class QL_INTERCEPT(IntEnum):
5757
QL_ARCH_32BIT = (QL_ARCH.ARM, QL_ARCH.ARM_THUMB, QL_ARCH.MIPS, QL_ARCH.X86, QL_ARCH.CORTEX_M)
5858
QL_ARCH_64BIT = (QL_ARCH.ARM64, QL_ARCH.X8664)
5959

60-
QL_OS_NONPID = (QL_OS.DOS, QL_OS.UEFI)
61-
QL_ARCH_HARDWARE = (QL_ARCH.CORTEX_M,)
62-
QL_ARCH_NONEOS = (QL_ARCH.EVM,)
63-
QL_OS_POSIX = (QL_OS.LINUX, QL_OS.FREEBSD, QL_OS.MACOS, QL_OS.QNX)
64-
QL_OS_ALL = QL_OS_POSIX + QL_OS_NONPID + (QL_OS.WINDOWS,)
60+
QL_OS_NONPID = (QL_OS.DOS, QL_OS.UEFI)
61+
QL_ARCH_BAREMETAL = (QL_ARCH.CORTEX_M,)
62+
QL_ARCH_INTERPRETER = (QL_ARCH.EVM,)
63+
QL_OS_POSIX = (QL_OS.LINUX, QL_OS.FREEBSD, QL_OS.MACOS, QL_OS.QNX)
64+
QL_OS_ALL = QL_OS_POSIX + QL_OS_NONPID + (QL_OS.WINDOWS,)
6565

6666
QL_HOOK_BLOCK = 0b0001
6767
QL_CALL_BLOCK = 0b0010

qiling/core.py

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .hw.hw import QlHwManager
2020
from .loader.loader import QlLoader
2121

22-
from .const import QL_ARCH_ENDIAN, QL_ENDIAN, QL_OS, QL_VERBOSE, QL_ARCH_NONEOS, QL_ARCH_HARDWARE
22+
from .const import QL_ARCH_ENDIAN, QL_ENDIAN, QL_OS, QL_VERBOSE, QL_ARCH_INTERPRETER, QL_ARCH_BAREMETAL
2323
from .exception import QlErrorFileNotFound, QlErrorArch, QlErrorOsType, QlErrorOutput
2424
from .utils import *
2525
from .core_struct import QlCoreStructs
@@ -68,7 +68,8 @@ def __init__(
6868
self._code = code
6969
self._ostype = ostype
7070
self._archtype = archtype
71-
self._noneos = None,
71+
self._interpreter = False,
72+
self._baremetal = False,
7273
self._archendian = None
7374
self._archbit = None
7475
self._pointersize = None
@@ -110,29 +111,29 @@ def __init__(
110111
"""
111112
Qiling Framework Core Engine
112113
"""
113-
##############
114-
# Shellcode? #
115-
##############
114+
###############
115+
# code_exec() #
116+
###############
116117

117118
if self._code or (self._archtype and type(self._archtype) == str):
118119
if (self._archtype and type(self._archtype) == str):
119120
self._archtype= arch_convert(self._archtype.lower())
120121

121-
if (self._ostype and type(self._ostype) == str):
122+
if self._ostype == None:
123+
self._ostype = arch_os_convert(self._archtype)
124+
else:
122125
self._ostype = ostype_convert(self._ostype.lower())
123126

124-
if self._archtype in QL_ARCH_NONEOS or self._ostype == None:
125-
if self._ostype == None:
126-
self._ostype = arch_os_convert(self._archtype)
127-
if self._code == None:
128-
self._code = self._archtype
129-
130-
127+
if self._code == None:
128+
self._code = "qiling"
131129
if self._argv is None:
132130
self._argv = ["qilingcode"]
133131
if self._rootfs is None:
134132
self._rootfs = "."
135-
133+
134+
self._interpreter = True if self._archtype in (QL_ARCH_INTERPRETER) else False
135+
self._baremetal = True if self._archtype in (QL_ARCH_BAREMETAL) else False
136+
136137
# file check
137138
if self._code is None:
138139
if not os.path.exists(str(self._argv[0])):
@@ -201,11 +202,11 @@ def __init__(
201202
# Components #
202203
##############
203204

204-
if self.archtype not in QL_ARCH_NONEOS:
205+
if not self._interpreter:
205206
self._mem = component_setup("os", "memory", self)
206207
self._reg = component_setup("arch", "register", self)
207208

208-
if self.archtype in QL_ARCH_HARDWARE:
209+
if self._baremetal:
209210
self._hw = component_setup("hw", "hw", self)
210211

211212
self._arch = arch_setup(self.archtype, self)
@@ -215,11 +216,11 @@ def __init__(
215216
QlCoreHooks.__init__(self, self.uc)
216217

217218
# Setup Outpt
218-
if self.archtype not in QL_ARCH_NONEOS:
219+
if not self._interpreter:
219220
self.arch.utils.setup_output()
220221

221-
if (self.archtype not in QL_ARCH_NONEOS):
222-
if (self.archtype not in QL_ARCH_HARDWARE):
222+
if not self._interpreter:
223+
if not self._baremetal:
223224
self._os = os_setup(self.archtype, self.ostype, self)
224225

225226
if stdin is not None:
@@ -234,8 +235,8 @@ def __init__(
234235
# Run the loader
235236
self.loader.run()
236237

237-
if (self.archtype not in QL_ARCH_NONEOS):
238-
if (self.archtype not in QL_ARCH_HARDWARE):
238+
if not self._interpreter:
239+
if not self._baremetal:
239240
# Add extra guard options when configured to do so
240241
self._init_stop_guard()
241242

@@ -545,7 +546,7 @@ def verbose(self):
545546
def verbose(self, v):
546547
self._verbose = v
547548
self.log.setLevel(ql_resolve_logger_level(self._verbose))
548-
if self.archtype not in QL_ARCH_NONEOS:
549+
if self.interpreter:
549550
self.arch.utils.setup_output()
550551

551552
@property
@@ -629,6 +630,31 @@ def uc(self) -> Uc:
629630
def uc(self, u):
630631
self._uc = u
631632

633+
@property
634+
def interpreter(self) -> bool:
635+
""" Raw uc instance.
636+
637+
Type: Ucgit
638+
"""
639+
return self._interpreter
640+
641+
@interpreter.setter
642+
def interpreter(self, i):
643+
self._interpreter = i
644+
645+
@property
646+
def baremetal(self) -> bool:
647+
""" Raw uc instance.
648+
649+
Type: Ucgit
650+
"""
651+
return self._baremetal
652+
653+
@baremetal.setter
654+
def baremetal(self, b):
655+
self._baremetal = b
656+
657+
632658
@property
633659
def stop_options(self) -> "QlStopOptions":
634660
""" The stop options configured:
@@ -710,21 +736,22 @@ def run(self, begin=None, end=None, timeout=0, count=0, code = None):
710736
if self._debugger != False and self._debugger != None:
711737
self._debugger = debugger_setup(self._debugger, self)
712738

713-
if self.archtype not in QL_ARCH_NONEOS and self.archtype not in QL_ARCH_HARDWARE:
714-
self.write_exit_trap()
715-
# patch binary
716-
self.__enable_bin_patch()
739+
if not self.interpreter:
740+
if not self.baremetal:
741+
self.write_exit_trap()
742+
# patch binary
743+
self.__enable_bin_patch()
717744

718-
# emulate the binary
719-
self.os.run()
745+
# emulate the binary
746+
self.os.run()
720747

721-
if self.archtype in QL_ARCH_NONEOS:
748+
if self.interpreter:
722749
if code == None:
723750
return self.arch.run(self._code)
724751
else:
725752
return self.arch.run(code)
726753

727-
if self.archtype in QL_ARCH_HARDWARE:
754+
if self.baremetal:
728755
self.__enable_bin_patch()
729756
if self.count <= 0:
730757
self.count = -1
@@ -872,7 +899,7 @@ def stop(self):
872899
if self.multithread:
873900
self.os.thread_management.stop()
874901

875-
elif self.archtype in QL_ARCH_HARDWARE:
902+
elif self.baremetal:
876903
self.arch.stop()
877904

878905
else:

qiling/core_hooks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .core_hooks_types import Hook, HookAddr, HookIntr, HookRet
1717
from .utils import catch_KeyboardInterrupt
18-
from .const import QL_HOOK_BLOCK, QL_ARCH_NONEOS
18+
from .const import QL_HOOK_BLOCK
1919
from .exception import QlErrorCoreHook
2020

2121
# Don't assume self is Qiling.
@@ -238,7 +238,7 @@ def ql_hook(self, hook_type: int, callback: Callable, user_data=None, begin=1, e
238238

239239

240240
def hook_code(self, callback, user_data=None, begin=1, end=0):
241-
if self.archtype in QL_ARCH_NONEOS:
241+
if self.interpreter:
242242
from .arch.evm.hooks import ql_evm_hooks
243243
return ql_evm_hooks(self, 'HOOK_CODE', callback, user_data, begin, end)
244244

@@ -281,7 +281,7 @@ def hook_mem_invalid(self, callback, user_data=None, begin=1, end=0):
281281
def hook_address(self, callback, address, user_data=None):
282282
hook = HookAddr(callback, address, user_data)
283283

284-
if self.archtype in QL_ARCH_NONEOS:
284+
if self.interpreter:
285285
from .arch.evm.hooks import evm_hook_address
286286
return evm_hook_address(self, 'HOOK_ADDR', hook, address)
287287

@@ -320,7 +320,7 @@ def hook_mem_fetch(self, callback, user_data=None, begin=1, end=0):
320320

321321

322322
def hook_insn(self, callback, arg1, user_data=None, begin=1, end=0):
323-
if self.archtype in QL_ARCH_NONEOS:
323+
if self.interpreter:
324324
from .arch.evm.hooks import evm_hook_insn
325325
return evm_hook_insn(self, 'HOOK_INSN', callback, arg1, user_data, begin, end)
326326

@@ -337,7 +337,7 @@ def hook_del(self, *args):
337337

338338
hook_type, h = args
339339

340-
if self.archtype in QL_ARCH_NONEOS:
340+
if self.interpreter:
341341
from .arch.evm.hooks import evm_hook_del
342342
return evm_hook_del(hook_type, h)
343343

qiling/debugger/gdb/gdb.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ def __init__(self, ql: Qiling, ip: str = '127.0.01', port: int = 9999):
5252
self.port = port
5353

5454

55-
if ql.archtype in QL_ARCH_HARDWARE:
56-
load_address = ql.loader.load_address
55+
if self.ql.baremetal:
56+
load_address = self.ql.loader.load_address
5757
exit_point = load_address + os.path.getsize(ql.path)
58-
elif ql.code:
59-
load_address = ql.os.entry_point
58+
elif self.ql.code:
59+
load_address = self.ql.os.entry_point
6060
exit_point = load_address + len(ql.code)
6161
else:
6262
load_address = ql.loader.load_address
6363
exit_point = load_address + os.path.getsize(ql.path)
6464

65-
if ql.archtype in QL_ARCH_HARDWARE:
66-
self.entry_point = ql.loader.entry_point
65+
if self.ql.baremetal:
66+
self.entry_point = self.ql.loader.entry_point
6767
elif self.ql.ostype in (QL_OS.LINUX, QL_OS.FREEBSD) and not self.ql.code:
6868
self.entry_point = self.ql.os.elf_entry
6969
else:
@@ -506,7 +506,7 @@ def handle_q(subcmd):
506506

507507

508508
elif subcmd.startswith('Xfer:threads:read::0,'):
509-
if self.ql.ostype in QL_OS_NONPID or self.ql.archtype in QL_ARCH_HARDWARE:
509+
if self.ql.ostype in QL_OS_NONPID or self.ql.baremetal:
510510
self.send("l")
511511
else:
512512
file_contents = ("<threads>\r\n<thread id=\""+ str(self.ql.os.pid) + "\" core=\"1\" name=\"" + self.ql.targetname + "\"/>\r\n</threads>")
@@ -621,7 +621,7 @@ def handle_v(subcmd):
621621
self.send("")
622622

623623
elif subcmd.startswith('File:open'):
624-
if self.ql.ostype == QL_OS.UEFI or self.ql.archtype in QL_ARCH_HARDWARE:
624+
if self.ql.ostype == QL_OS.UEFI or self.ql.baremetal:
625625
self.send("F-1")
626626
return
627627

qiling/debugger/gdb/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(self):
2626

2727
def initialize(self, ql, hook_address, exit_point=None, mappings=None):
2828
self.ql = ql
29-
if self.ql.archtype in QL_ARCH_HARDWARE:
29+
if self.ql.baremetal:
3030
self.current_address = self.entry_point
3131
else:
3232
self.current_address = self.entry_point = self.ql.os.entry_point

qiling/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from unicorn import UC_ERR_READ_UNMAPPED, UC_ERR_FETCH_UNMAPPED
1717

1818
from .exception import *
19-
from .const import QL_ARCH_HARDWARE, QL_ARCH_NONEOS, QL_VERBOSE, QL_ARCH, QL_ENDIAN, QL_OS, QL_DEBUGGER, QL_ARCH_1BIT, QL_ARCH_16BIT, QL_ARCH_32BIT, QL_ARCH_64BIT
19+
from .const import QL_VERBOSE, QL_ARCH, QL_ENDIAN, QL_OS, QL_DEBUGGER, QL_ARCH_1BIT, QL_ARCH_16BIT, QL_ARCH_32BIT, QL_ARCH_64BIT
2020
from .const import debugger_map, arch_map, os_map, arch_os_map, loader_map
2121

2222
FMT_STR = "%(levelname)s\t%(message)s"
@@ -432,7 +432,7 @@ def arch_setup(archtype, ql):
432432
else:
433433
arch_str = arch_convert_str(archtype)
434434

435-
if archtype in QL_ARCH_NONEOS:
435+
if ql.interpreter:
436436
return ql_get_module_function(f"qiling.arch.{arch_str.lower()}.{arch_str.lower()}", archmanager)(ql)
437437
else:
438438
return ql_get_module_function(f"qiling.arch.{arch_str.lower()}", archmanager)(ql)
@@ -458,7 +458,7 @@ def os_setup(archtype: QL_ARCH, ostype: QL_OS, ql):
458458

459459

460460
def profile_setup(ql):
461-
if ql.archtype in QL_ARCH_HARDWARE:
461+
if ql.baremetal:
462462
return ql_hw_profile_setup(ql)
463463

464464
_profile = "Default"

0 commit comments

Comments
 (0)