Skip to content

Commit 0d26efd

Browse files
authored
Merge pull request #2003 from elicn/dev
Misc. Python binding re-arrangements
2 parents 9427f0a + 386e0ed commit 0d26efd

File tree

8 files changed

+172
-68
lines changed

8 files changed

+172
-68
lines changed

bindings/python/sample_all.sh

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ echo "=========================="
2727
python3 ./shellcode.py
2828
echo "=========================="
2929
python3 ./sample_ctl.py
30+
echo "=========================="
31+
python3 ./sample_network_auditing.py

bindings/python/unicorn/unicorn.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import sys
1+
import sys as _sys
22

3-
if sys.version_info[0] == 2:
3+
from .unicorn_const import (
4+
UC_VERSION_MAJOR as __MAJOR,
5+
UC_VERSION_MINOR as __MINOR,
6+
UC_VERSION_PATCH as __PATCH
7+
)
8+
9+
__version__ = "%u.%u.%u" % (__MAJOR, __MINOR, __PATCH)
10+
11+
if _sys.version_info.major == 2:
412
from .unicorn_py2 import *
513
else:
614
from .unicorn_py3 import *
7-
8-
__version__ = "%u.%u.%u" % (uc.UC_VERSION_MAJOR, uc.UC_VERSION_MINOR, uc.UC_VERSION_PATCH)
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
# from .arm import UcRegCP
2-
# from .arm64 import UcRegCP64
3-
# from .intel import UcRegFPR, UcRegMMR, UcRegMSR
4-
# from .types import UcReg128, UcReg256, UcReg512, UcReg, UcLargeReg, UcTupledReg

bindings/python/unicorn/unicorn_py3/arch/arm.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# AArch32 classes and structures.
2-
#
1+
"""AArch32 classes and structures.
2+
"""
33
# @author elicn
44

55
from typing import Any, Tuple
66

77
import ctypes
88

9-
from .. import Uc
10-
from .. import arm_const as const
9+
# traditional unicorn imports
10+
from unicorn import arm_const as const
1111

12+
# newly introduced unicorn imports
13+
from ..unicorn import Uc
1214
from .types import UcTupledReg, UcReg128
1315

1416
ARMCPReg = Tuple[int, int, int, int, int, int, int, int]
1517

16-
def _structure_repr(self):
17-
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_))
18-
1918

2019
class UcRegCP(UcTupledReg[ARMCPReg]):
2120
"""ARM coprocessors registers for instructions MRC, MCR, MRRC, MCRR
@@ -36,7 +35,6 @@ class UcRegCP(UcTupledReg[ARMCPReg]):
3635
def value(self) -> int:
3736
return self.val
3837

39-
__repr__ = _structure_repr
4038

4139
class UcAArch32(Uc):
4240
"""Unicorn subclass for ARM architecture.
@@ -83,3 +81,5 @@ def reg_write(self, reg_id: int, value) -> None:
8381

8482
else:
8583
self._reg_write(reg_id, reg_cls, value)
84+
85+
__all__ = ['UcRegCP', 'UcAArch32']

bindings/python/unicorn/unicorn_py3/arch/arm64.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# AArch64 classes and structures.
2-
#
1+
"""AArch64 classes and structures.
2+
"""
33
# @author elicn
44

55
from typing import Any, Callable, NamedTuple, Tuple
66

77
import ctypes
88

9-
from .. import Uc, UcError
10-
from .. import arm64_const as const
9+
# traditional unicorn imports
10+
from unicorn import arm64_const as const
11+
from unicorn.unicorn_const import UC_ERR_ARG, UC_HOOK_INSN
1112

12-
from ..unicorn import uccallback
13-
from unicorn import UC_ERR_ARG, UC_HOOK_INSN
13+
# newly introduced unicorn imports
14+
from ..unicorn import Uc, UcError, uccallback
1415
from .types import uc_engine, UcTupledReg, UcReg128
1516

1617
ARM64CPReg = Tuple[int, int, int, int, int, int]
@@ -124,3 +125,5 @@ def reg_write(self, reg_id: int, value) -> None:
124125

125126
else:
126127
self._reg_write(reg_id, reg_cls, value)
128+
129+
__all__ = ['UcRegCP64', 'UcAArch64']

bindings/python/unicorn/unicorn_py3/arch/intel.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# Intel architecture classes and structures.
2-
#
1+
"""Intel architecture classes and structures.
2+
"""
33
# @author elicn
44

5-
from typing import Any, Callable, Tuple
5+
from typing import Any, Callable, Sequence, Tuple
66

77
import ctypes
88

9-
from .. import Uc, UcError
10-
from .. import x86_const as const
11-
12-
from unicorn.unicorn_py3 import uccallback
9+
# traditional unicorn imports
10+
from unicorn import x86_const as const
1311
from unicorn.unicorn_const import UC_ERR_ARG, UC_HOOK_INSN
12+
13+
# newly introduced unicorn imports
14+
from ..unicorn import Uc, UcError, uccallback
1415
from .types import uc_engine, UcTupledReg, UcReg128, UcReg256, UcReg512
1516

1617
X86MMRReg = Tuple[int, int, int, int]
@@ -36,6 +37,9 @@ class UcRegMMR(UcTupledReg[X86MMRReg]):
3637

3738

3839
class UcRegMSR(UcTupledReg[X86MSRReg]):
40+
"""Intel Model Specific Register
41+
"""
42+
3943
_fields_ = (
4044
('rid', ctypes.c_uint32),
4145
('val', ctypes.c_uint64)
@@ -47,6 +51,9 @@ def value(self) -> int:
4751

4852

4953
class UcRegFPR(UcTupledReg[X86FPReg]):
54+
"""Intel Floating Point Register
55+
"""
56+
5057
_fields_ = (
5158
('mantissa', ctypes.c_uint64),
5259
('exponent', ctypes.c_uint16)
@@ -176,3 +183,11 @@ def msr_read(self, msr_id: int) -> int:
176183

177184
def msr_write(self, msr_id: int, value: int) -> None:
178185
self._reg_write(const.UC_X86_REG_MSR, UcRegMSR, (msr_id, value))
186+
187+
def reg_read_batch(self, reg_ids: Sequence[int]) -> Tuple:
188+
reg_types = [UcIntel.__select_reg_class(rid) or self._DEFAULT_REGTYPE for rid in reg_ids]
189+
190+
return self._reg_read_batch(reg_ids, reg_types)
191+
192+
193+
__all__ = ['UcRegMMR', 'UcRegMSR', 'UcRegFPR', 'UcIntel']

bindings/python/unicorn/unicorn_py3/arch/types.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Common types and structures.
2-
#
1+
"""Common types and structures.
2+
"""
33
# @author elicn
44

55
from abc import abstractmethod
@@ -15,9 +15,6 @@
1515

1616
VT = TypeVar('VT', bound=Tuple[int, ...])
1717

18-
def _structure_repr(self):
19-
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%s" % (k, getattr(self, k)) for (k, _) in self._fields_))
20-
2118

2219
class UcReg(ctypes.Structure):
2320
"""A base class for composite registers.
@@ -41,7 +38,6 @@ def from_value(cls, value):
4138

4239
pass
4340

44-
_repr_ = _structure_repr
4541

4642
class UcTupledReg(UcReg, Generic[VT]):
4743
"""A base class for registers whose values are represented as a set
@@ -85,12 +81,24 @@ def from_value(cls, value: int):
8581

8682

8783
class UcReg128(UcLargeReg):
84+
"""Large register holding a 128-bit value.
85+
"""
86+
8887
_fields_ = [('qwords', ctypes.c_uint64 * 2)]
8988

9089

9190
class UcReg256(UcLargeReg):
91+
"""Large register holding a 256-bit value.
92+
"""
93+
9294
_fields_ = [('qwords', ctypes.c_uint64 * 4)]
9395

9496

9597
class UcReg512(UcLargeReg):
98+
"""Large register holding a 512-bit value.
99+
"""
100+
96101
_fields_ = [('qwords', ctypes.c_uint64 * 8)]
102+
103+
104+
__all__ = ['uc_err', 'uc_engine', 'uc_context', 'uc_hook_h', 'UcReg', 'UcTupledReg', 'UcLargeReg', 'UcReg128', 'UcReg256', 'UcReg512']

0 commit comments

Comments
 (0)