-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
115 lines (91 loc) · 2.55 KB
/
demo.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
from abc import ABC
from dataclasses import dataclass
from typing import Any, Optional
import megastone as ms
import hyperstone as hs
from hyperstone import HyperEmu
class Entrypoint(hs.RunnerPlugin, ABC):
def __init__(self, entrypoint: int):
super().__init__()
self.entrypoint = entrypoint
def _run(self):
try:
self.emu.run_function(self.entrypoint)
except ms.MemFaultError as f:
hs.log.error(f)
@dataclass
class SegmentDecl:
name: str
address: int
size: int
perms: ms.AccessType = ms.AccessType.RWX
class SegmentPlugin(hs.Plugin):
def __init__(self, *segments: SegmentDecl):
super().__init__()
self.interact_queue += segments
def _handle_interact(self, *objs: SegmentDecl):
for seg in objs:
hs.log.info(f'Mapping segment {seg.name}: {seg}')
self.emu.mem.map(seg.address, seg.size, seg.name, seg.perms)
class SetupMemory(hs.Plugin):
def _handle_interact(self, *objs: Optional[Any]):
pass
SUPPORT_BASE = 0x08000000
SUPPORT_SIZE = 0x8000
STACK_BASE = 0x7e000000
STACK_SIZE = 0x8000
def __init__(self, *,
support_base: int = SUPPORT_BASE,
support_size: int = SUPPORT_SIZE,
stack_base: int = STACK_BASE,
stack_size: int = STACK_SIZE):
super().__init__()
self.support_segment = SegmentDecl(
'hyperstone_support',
support_base,
support_size,
)
self.stack_segment = SegmentDecl(
'stack',
stack_base,
stack_size,
)
def prepare(self, emu: HyperEmu):
segments = self.require(SegmentPlugin, emu)
segments.interact(self.support_segment, self.stack_segment)
super().prepare(emu)
class Settings(hs.Settings):
DEFAULT_SEGMENTS = SetupMemory()
SEGMENTS = SegmentPlugin(
SegmentDecl(
'test1',
0x40000000,
0x1000,
),
SegmentDecl(
'test2',
0x41000000,
0x1000,
ms.AccessType.RX
)
)
ENTRY = Entrypoint(0x40000000)
SIMPLE_SETTINGS = [
SetupMemory(),
SegmentPlugin(
SegmentDecl(
'test1',
0x40000000,
0x1000,
),
SegmentDecl(
'test2',
0x41000000,
0x1000,
ms.AccessType.RX
)
),
Entrypoint(0x40000000),
]
if __name__ == '__main__':
hs.start(ms.ARCH_ARM, SIMPLE_SETTINGS)