Skip to content

Commit 9b37c3b

Browse files
authored
Merge pull request #1376 from elicn/periodic
Periodic maintenance PR
2 parents b5e8b53 + f4a9f85 commit 9b37c3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1981
-1380
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: https://editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*.py]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 4
10+
indent_style = space
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true

qiling/arch/arch.py

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

6-
from abc import abstractmethod
6+
from abc import ABC, abstractmethod
7+
from typing import ClassVar, Optional
78

89
from unicorn import Uc
910
from unicorn.unicorn import UcContext
@@ -12,16 +13,20 @@
1213

1314
from qiling import Qiling
1415
from qiling.const import QL_ARCH, QL_ENDIAN
16+
17+
from .models import QL_CPU
1518
from .register import QlRegisterManager
1619
from .utils import QlArchUtils
1720

1821

19-
class QlArch:
20-
type: QL_ARCH
21-
bits: int
22+
class QlArch(ABC):
23+
type: ClassVar[QL_ARCH]
24+
bits: ClassVar[int]
2225

23-
def __init__(self, ql: Qiling):
26+
def __init__(self, ql: Qiling, *, cputype: Optional[QL_CPU] = None):
2427
self.ql = ql
28+
29+
self.cpu = cputype
2530
self.utils = QlArchUtils(ql)
2631

2732
@property

qiling/arch/arm.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
from functools import cached_property, lru_cache
7+
from typing import Optional
78

89
from unicorn import Uc, UC_ARCH_ARM, UC_MODE_ARM, UC_MODE_THUMB, UC_MODE_BIG_ENDIAN
910
from capstone import Cs, CS_ARCH_ARM, CS_MODE_ARM, CS_MODE_THUMB, CS_MODE_BIG_ENDIAN
@@ -12,6 +13,7 @@
1213
from qiling import Qiling
1314
from qiling.arch.arch import QlArch
1415
from qiling.arch import arm_const
16+
from qiling.arch.models import ARM_CPU_MODEL
1517
from qiling.arch.register import QlRegisterManager
1618
from qiling.const import QL_ARCH, QL_ENDIAN
1719

@@ -20,8 +22,8 @@ class QlArchARM(QlArch):
2022
type = QL_ARCH.ARM
2123
bits = 32
2224

23-
def __init__(self, ql: Qiling, endian: QL_ENDIAN, thumb: bool):
24-
super().__init__(ql)
25+
def __init__(self, ql: Qiling, *, cputype: Optional[ARM_CPU_MODEL], endian: QL_ENDIAN, thumb: bool):
26+
super().__init__(ql, cputype=cputype)
2527

2628
self._init_endian = endian
2729
self._init_thumb = thumb
@@ -32,13 +34,18 @@ def __init__(self, ql: Qiling, endian: QL_ENDIAN, thumb: bool):
3234
def uc(self) -> Uc:
3335
mode = UC_MODE_ARM
3436

35-
if self._init_endian == QL_ENDIAN.EB:
37+
if self._init_endian is QL_ENDIAN.EB:
3638
mode += UC_MODE_BIG_ENDIAN
3739

3840
if self._init_thumb:
3941
mode += UC_MODE_THUMB
4042

41-
return Uc(UC_ARCH_ARM, mode)
43+
obj = Uc(UC_ARCH_ARM, mode)
44+
45+
if self.cpu is not None:
46+
obj.ctl_set_cpu_model(self.cpu.value)
47+
48+
return obj
4249

4350
@cached_property
4451
def regs(self) -> QlRegisterManager:
@@ -82,7 +89,7 @@ def disassembler(self) -> Cs:
8289

8390
mode = CS_MODE_ARM
8491

85-
if self.endian == QL_ENDIAN.EB:
92+
if self.endian is QL_ENDIAN.EB:
8693
mode += CS_MODE_BIG_ENDIAN
8794

8895
if self.is_thumb:
@@ -102,7 +109,7 @@ def assembler(self) -> Ks:
102109

103110
mode = KS_MODE_ARM
104111

105-
if self.endian == QL_ENDIAN.EB:
112+
if self.endian is QL_ENDIAN.EB:
106113
mode += KS_MODE_BIG_ENDIAN
107114

108115
if self.is_thumb:

qiling/arch/arm64.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#
55

66
from functools import cached_property
7+
from typing import Optional
78

89
from unicorn import Uc, UC_ARCH_ARM64, UC_MODE_ARM
910
from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM
1011
from keystone import Ks, KS_ARCH_ARM64, KS_MODE_ARM
1112

13+
from qiling import Qiling
1214
from qiling.arch.arch import QlArch
1315
from qiling.arch import arm64_const
16+
from qiling.arch.models import ARM64_CPU_MODEL
1417
from qiling.arch.register import QlRegisterManager
1518
from qiling.const import QL_ARCH, QL_ENDIAN
1619

@@ -19,9 +22,17 @@ class QlArchARM64(QlArch):
1922
type = QL_ARCH.ARM64
2023
bits = 64
2124

25+
def __init__(self, ql: Qiling, *, cputype: Optional[ARM64_CPU_MODEL] = None):
26+
super().__init__(ql, cputype=cputype)
27+
2228
@cached_property
2329
def uc(self) -> Uc:
24-
return Uc(UC_ARCH_ARM64, UC_MODE_ARM)
30+
obj = Uc(UC_ARCH_ARM64, UC_MODE_ARM)
31+
32+
if self.cpu is not None:
33+
obj.ctl_set_cpu_model(self.cpu.value)
34+
35+
return obj
2536

2637
@cached_property
2738
def regs(self) -> QlRegisterManager:

qiling/arch/cortex_m.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from functools import cached_property
77
from contextlib import ContextDecorator
8+
from typing import Optional
89

910
from unicorn import UC_ARCH_ARM, UC_MODE_ARM, UC_MODE_MCLASS, UC_MODE_THUMB
1011
from capstone import Cs, CS_ARCH_ARM, CS_MODE_ARM, CS_MODE_MCLASS, CS_MODE_THUMB
@@ -13,6 +14,7 @@
1314
from qiling import Qiling
1415
from qiling.arch.arm import QlArchARM
1516
from qiling.arch import cortex_m_const
17+
from qiling.arch.models import ARM_CPU_MODEL
1618
from qiling.arch.register import QlRegisterManager
1719
from qiling.arch.cortex_m_const import IRQ, EXC_RETURN, CONTROL, EXCP
1820
from qiling.const import QL_ARCH, QL_ENDIAN, QL_VERBOSE
@@ -66,12 +68,14 @@ class QlArchCORTEX_M(QlArchARM):
6668
type = QL_ARCH.CORTEX_M
6769
bits = 32
6870

69-
def __init__(self, ql: Qiling):
70-
super().__init__(ql, endian=QL_ENDIAN.EL, thumb=True)
71+
def __init__(self, ql: Qiling, *, cputype: Optional[ARM_CPU_MODEL] = None):
72+
super().__init__(ql, cputype=cputype, endian=QL_ENDIAN.EL, thumb=True)
7173

7274
@cached_property
7375
def uc(self):
74-
return MultiTaskUnicorn(UC_ARCH_ARM, UC_MODE_ARM + UC_MODE_MCLASS + UC_MODE_THUMB, 10)
76+
cpu = self.cpu and self.cpu.value
77+
78+
return MultiTaskUnicorn(UC_ARCH_ARM, UC_MODE_ARM + UC_MODE_MCLASS + UC_MODE_THUMB, cpu, 10)
7579

7680
@cached_property
7781
def regs(self) -> QlRegisterManager:
@@ -97,18 +101,18 @@ def is_thumb(self) -> bool:
97101
def endian(self) -> QL_ENDIAN:
98102
return QL_ENDIAN.EL
99103

100-
def is_handler_mode(self):
104+
def is_handler_mode(self) -> bool:
101105
return self.regs.ipsr > 1
102106

103-
def using_psp(self):
107+
def using_psp(self) -> bool:
104108
return not self.is_handler_mode() and (self.regs.control & CONTROL.SPSEL) > 0
105109

106-
def init_context(self):
110+
def init_context(self) -> None:
107111
self.regs.lr = 0xffffffff
108112
self.regs.msp = self.ql.mem.read_ptr(0x0)
109113
self.regs.pc = self.ql.mem.read_ptr(0x4)
110114

111-
def unicorn_exception_handler(self, ql, intno):
115+
def unicorn_exception_handler(self, ql: Qiling, intno: int):
112116
forward_mapper = {
113117
EXCP.UDEF : IRQ.HARD_FAULT, # undefined instruction
114118
EXCP.SWI : IRQ.SVCALL, # software interrupt
@@ -139,8 +143,9 @@ def unicorn_exception_handler(self, ql, intno):
139143
except IndexError:
140144
raise QlErrorNotImplemented(f'Unhandled interrupt number ({intno})')
141145

142-
def interrupt_handler(self, ql, intno):
146+
def interrupt_handler(self, ql: Qiling, intno: int):
143147
basepri = self.regs.basepri & 0xf0
148+
144149
if basepri and basepri <= ql.hw.nvic.get_priority(intno):
145150
return
146151

qiling/arch/evm/evm.py

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

55

@@ -11,6 +11,7 @@
1111
from qiling.arch.evm.vm.message import Message
1212
from qiling.const import *
1313

14+
1415
class QlArchEVM(QlArch):
1516
type = QL_ARCH.EVM
1617
bits = 1
@@ -39,7 +40,19 @@ def stack_write(self, offset, data):
3940

4041
@property
4142
def uc(self):
42-
return None
43+
raise AttributeError
44+
45+
@property
46+
def regs(self):
47+
raise AttributeError
48+
49+
@property
50+
def disassembler(self):
51+
raise AttributeError
52+
53+
@property
54+
def assembler(self):
55+
raise AttributeError
4356

4457
@property
4558
def endian(self) -> QL_ENDIAN:
@@ -49,6 +62,7 @@ def endian(self) -> QL_ENDIAN:
4962
def __evm_run(self, code: Message):
5063
return self.arch.run(code)
5164

65+
5266
def monkeypatch_core_methods(ql):
5367
"""Monkeypatch core methods for evm
5468
"""

qiling/arch/mips.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55

66
from functools import cached_property
7+
from typing import Optional
78

89
from unicorn import Uc, UC_ARCH_MIPS, UC_MODE_MIPS32, UC_MODE_BIG_ENDIAN, UC_MODE_LITTLE_ENDIAN
910
from capstone import Cs, CS_ARCH_MIPS, CS_MODE_MIPS32, CS_MODE_BIG_ENDIAN, CS_MODE_LITTLE_ENDIAN
@@ -12,6 +13,7 @@
1213
from qiling import Qiling
1314
from qiling.arch.arch import QlArch
1415
from qiling.arch import mips_const
16+
from qiling.arch.models import MIPS_CPU_MODEL
1517
from qiling.arch.register import QlRegisterManager
1618
from qiling.const import QL_ARCH, QL_ENDIAN
1719

@@ -20,8 +22,8 @@ class QlArchMIPS(QlArch):
2022
type = QL_ARCH.MIPS
2123
bits = 32
2224

23-
def __init__(self, ql: Qiling, endian: QL_ENDIAN):
24-
super().__init__(ql)
25+
def __init__(self, ql: Qiling, *, cputype: Optional[MIPS_CPU_MODEL], endian: QL_ENDIAN):
26+
super().__init__(ql, cputype=cputype)
2527

2628
self._init_endian = endian
2729

0 commit comments

Comments
 (0)