-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathrun_all_images_multiprocess.py
executable file
·197 lines (159 loc) · 4.22 KB
/
run_all_images_multiprocess.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import multiprocessing as mp
import signal
import logging
import io
import progressbar
import argparse
import pathlib
from typing import (
Tuple,
List,
BinaryIO
)
from DyldExtractor.macho.macho_context import MachOContext
from DyldExtractor.dyld.dyld_context import DyldContext
from DyldExtractor.extraction_context import ExtractionContext
from DyldExtractor.converter import (
linkedit_optimizer,
stub_fixer,
objc_fixer,
slide_info,
macho_offset
)
class _DyldExtractorArgs(argparse.Namespace):
dyld_path: pathlib.Path
jobs: int
pass
class _DummyProgressBar(object):
def update(*args, **kwargs):
pass
def _imageRunner(dyldPath: str, imageIndex: int) -> None:
level = logging.DEBUG
loggingStream = io.StringIO()
# setup logging
logger = logging.getLogger(f"Worker: {imageIndex}")
handler = logging.StreamHandler(loggingStream)
formatter = logging.Formatter(
fmt="{asctime}:{msecs:03.0f} [{levelname:^9}] {filename}:{lineno:d} : {message}", # noqa
datefmt="%H:%M:%S",
style="{",
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
# process the image
with open(dyldPath, "rb") as f:
dyldCtx = DyldContext(f)
subCacheFiles: List[BinaryIO] = []
try:
# add sub caches if there are any
subCacheFiles = dyldCtx.addSubCaches(dyldPath)
machoOffset, context = dyldCtx.convertAddr(
dyldCtx.images[imageIndex].address
)
machoCtx = MachOContext(context.fileObject, machoOffset, True)
# Add sub caches if necessary
if dyldCtx.hasSubCaches():
mappings = dyldCtx.mappings
mainFileMap = next(
(mapping[0] for mapping in mappings if mapping[1] == context)
)
machoCtx.addSubfiles(
mainFileMap,
((m, ctx.makeCopy(copyMode=True)) for m, ctx in mappings)
)
pass
extractionCtx = ExtractionContext(
dyldCtx,
machoCtx,
_DummyProgressBar(),
logger
)
slide_info.processSlideInfo(extractionCtx)
linkedit_optimizer.optimizeLinkedit(extractionCtx)
stub_fixer.fixStubs(extractionCtx)
objc_fixer.fixObjC(extractionCtx)
macho_offset.optimizeOffsets(extractionCtx)
except Exception as e:
logger.exception(e)
pass
finally:
for file in subCacheFiles:
file.close()
pass
pass
pass
# cleanup
handler.close()
return loggingStream.getvalue()
def _workerInitializer():
# ignore KeyboardInterrupt in workers
signal.signal(signal.SIGINT, signal.SIG_IGN)
pass
if "__main__" == __name__:
# Get arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"dyld_path",
type=pathlib.Path,
help="A path to the target DYLD cache."
)
parser.add_argument(
"-j", "--jobs", type=int, default=mp.cpu_count(),
help="Number of jobs to run simultaneously." # noqa
)
args = parser.parse_args(namespace=_DyldExtractorArgs)
# create a list of images
images: List[str] = []
with open(args.dyld_path, "rb") as f:
dyldCtx = DyldContext(f)
for index, image in enumerate(dyldCtx.images):
imagePath = dyldCtx.readString(image.pathFileOffset)[0:-1]
imagePath = imagePath.decode("utf-8")
imageName = imagePath.split("/")[-1]
images.append(imageName)
pass
summary = ""
with mp.Pool(args.jobs, initializer=_workerInitializer) as pool:
# create jobs for each image
jobs: List[Tuple[str, mp.pool.AsyncResult]] = []
for index, imageName in enumerate(images):
jobs.append(
(imageName, pool.apply_async(_imageRunner, (args.dyld_path, index)))
)
pass
total = len(jobs)
jobsComplete = 0
# setup progress bar
statusBar = progressbar.ProgressBar(
max_value=total,
redirect_stdout=True
)
# wait for all the jobs to complete
while True:
if len(jobs) == 0:
break
for i in reversed(range(len(jobs))):
imageName, job = jobs[i]
if job.ready():
jobs.pop(i)
# update the status
jobsComplete += 1
statusBar.update(jobsComplete)
print(f"processed: {imageName}")
# print the result if any
result = job.get()
if len(result):
result = f"----- {imageName} -----\n{result}--------------------\n"
summary += result
print(result)
pass
# close the pool and cleanup
pool.close()
pool.join()
print("\n----- Summary -----\n")
print(summary)
statusBar.update(jobsComplete)
statusBar.finish()
pass
pass