-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhopper_export_metadata.py
More file actions
333 lines (284 loc) · 10.8 KB
/
hopper_export_metadata.py
File metadata and controls
333 lines (284 loc) · 10.8 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# hopper_export_metadata.py — run inside Hopper's script engine
# Exports full document metadata (segments, symbols, procedures, CFG, pseudocode) to JSON.
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
Document: Any # provided by Hopper's script engine at runtime
VERSION = "1.5.2"
# ---------------------------------------------------------------------------
# Utility helpers (inlined — no external module dependencies)
# ---------------------------------------------------------------------------
def to_hex(value: object) -> str | None:
try:
return f"0x{int(value):x}"
except (TypeError, ValueError):
return None
def write_json(path: str | Path, payload: Any) -> None:
Path(path).write_text(f"{json.dumps(payload, indent=2)}\n", encoding="utf-8")
def default_output_path(
executable_path: str | None,
suffix: str,
fallback_name: str,
) -> Path:
if executable_path:
return Path(f"{executable_path}{suffix}")
return Path.home() / fallback_name
def ensure_document_ready(document: Any) -> None:
"""Check that background analysis has finished.
NOTE: waitForBackgroundProcessToEnd() dispatches to the main thread.
If analysis is still running, log a warning instead of blocking
(which would deadlock the Python thread against the main thread GIL).
"""
try:
if document.backgroundProcessActive():
document.log(
"[hopper_export_metadata] Warning: background analysis still active — "
"export may be incomplete. Wait for analysis to finish, then re-run."
)
except AttributeError:
return
def iter_document_procedures(document: Any) -> list[tuple[Any, Any]]:
"""Yield (segment, procedure) pairs for all procedures in the document."""
results: list[tuple[Any, Any]] = []
for segment in document.getSegmentsList():
try:
count = int(segment.getProcedureCount())
except Exception:
continue
for index in range(count):
try:
procedure = segment.getProcedureAtIndex(index)
except Exception:
continue
if procedure is not None:
results.append((segment, procedure))
return results
def procedure_name(segment: Any, procedure: Any) -> str:
"""Get the name of a procedure via segment name lookup at its entry point."""
try:
entry = procedure.getEntryPoint()
name = segment.getNameAtAddress(entry)
if name:
return str(name)
except Exception:
pass
return ""
def tag_names(owner: Any) -> list[str]:
try:
tags = owner.getTagList()
except Exception:
return []
names: list[str] = []
for tag in tags:
try:
names.append(str(tag.getName()))
except Exception:
continue
return names
def safe_name(value: object, fallback: str = "") -> str:
try:
return fallback if value is None else str(value)
except Exception:
return fallback
# ---------------------------------------------------------------------------
# Metadata collection
# ---------------------------------------------------------------------------
def basic_block_details(procedure: Any) -> list[dict[str, Any]]:
basic_blocks: list[dict[str, Any]] = []
try:
count = int(procedure.getBasicBlockCount())
except Exception:
return basic_blocks
for index in range(count):
try:
basic_block = procedure.getBasicBlock(index)
except Exception:
continue
successors: list[str] = []
try:
successor_count = int(basic_block.getSuccessorCount())
except Exception:
successor_count = 0
for successor_index in range(successor_count):
try:
successor_address = basic_block.getSuccessorAddressAtIndex(successor_index)
except Exception:
continue
successor_text = to_hex(successor_address)
if successor_text:
successors.append(successor_text)
basic_blocks.append(
{
"start": to_hex(basic_block.getStartingAddress()),
"end": to_hex(basic_block.getEndingAddress()),
"successors": successors,
"tags": tag_names(basic_block),
},
)
return basic_blocks
def local_variable_details(procedure: Any) -> list[dict[str, Any]]:
details: list[dict[str, Any]] = []
try:
variables = procedure.getLocalVariableList()
except Exception:
return details
for variable in variables:
try:
details.append(
{
"name": safe_name(variable.name(), ""),
"displacement": int(variable.displacement()),
},
)
except Exception:
continue
return details
def collect_metadata(document: Any) -> dict[str, Any]:
result: dict[str, Any] = {
"tool": "hopper_export_metadata.py",
"version": VERSION,
"document": {},
"segments": [],
"symbols": [],
"procedures": [],
"entry_points": [],
}
warnings: list[str] = []
try:
result["document"]["name"] = safe_name(document.getDocumentName(), "")
except Exception:
result["document"]["name"] = ""
try:
result["document"]["database_path"] = safe_name(document.getDatabaseFilePath(), "")
except Exception:
result["document"]["database_path"] = ""
try:
result["document"]["file_path"] = safe_name(document.getExecutableFilePath(), "")
except Exception:
result["document"]["file_path"] = ""
try:
result["document"]["entry_point"] = to_hex(document.getEntryPoint())
except Exception:
result["document"]["entry_point"] = None
try:
result["document"]["is_64_bit"] = bool(document.is64Bits())
except Exception:
result["document"]["is_64_bit"] = None
try:
for segment in document.getSegmentsList():
segment_info = {
"name": safe_name(segment.getName(), ""),
"start": to_hex(segment.getStartingAddress()),
"length": int(segment.getLength()),
"file_offset": int(segment.getFileOffset()),
"sections": [],
"procedure_count": int(segment.getProcedureCount()),
"string_count": int(segment.getStringCount()),
}
try:
section_count = int(segment.getSectionCount())
except Exception:
section_count = 0
for index in range(section_count):
try:
section = segment.getSection(index)
except Exception:
continue
segment_info["sections"].append(
{
"name": safe_name(section.getName(), ""),
"start": to_hex(section.getStartingAddress()),
"length": int(section.getLength()),
"flags": int(section.getFlags()),
},
)
result["segments"].append(segment_info)
try:
for address in segment.getNamedAddresses():
result["symbols"].append(
{
"segment": segment_info["name"],
"address": to_hex(address),
"name": safe_name(segment.getNameAtAddress(address), ""),
},
)
except Exception:
continue
try:
strings = []
for text, address in segment.getStringsList():
strings.append({"address": to_hex(address), "value": safe_name(text, "")})
if strings:
segment_info["strings"] = strings
except Exception:
continue
except Exception as error:
warnings.append(f"Segment parsing failed: {error}")
try:
for seg, proc in iter_document_procedures(document):
try:
entry = to_hex(proc.getEntryPoint())
except Exception:
entry = None
try:
start = to_hex(proc.getStartingAddress())
except Exception:
start = entry
try:
end = to_hex(proc.getEndingAddress())
except Exception:
end = None
try:
signature = safe_name(proc.signatureString(), "")
except Exception:
signature = ""
try:
heap_size = int(proc.getHeapSize())
except Exception:
heap_size = 0
basic_blocks = basic_block_details(proc)
local_variables = local_variable_details(proc)
procedure_info: dict[str, Any] = {
"name": procedure_name(seg, proc),
"entry_point": entry,
"start": start,
"end": end,
"signature": signature,
"heap_size": heap_size,
"local_variables": local_variables,
"tags": tag_names(proc),
"basic_block_count": len(basic_blocks),
"basic_blocks": basic_blocks,
}
# NOTE: proc.decompile() is intentionally skipped in batch export.
# It dispatches to the main thread via _dispatch_sync_f_slow, which
# deadlocks when the main thread is waiting on the Python GIL.
# Use the MCP server's decompile_procedure() for individual procedures.
result["procedures"].append(procedure_info)
except Exception as error:
warnings.append(f"Procedure parsing failed: {error}")
result["entry_points"] = [
result["document"]["entry_point"],
] if result["document"].get("entry_point") else []
if warnings:
result["warnings"] = warnings
return result
# ---------------------------------------------------------------------------
# Script entry — runs immediately inside Hopper
# ---------------------------------------------------------------------------
doc = Document.getCurrentDocument()
if doc is None:
raise RuntimeError("No active Hopper document.")
ensure_document_ready(doc)
result = collect_metadata(doc)
output_path = default_output_path(
result["document"].get("file_path"),
".hopper_export.json",
"hopper_export.json",
)
write_json(output_path, result)
doc.log(f"[hopper_export_metadata] Export complete: {output_path}")
if result.get("warnings"):
for w in result["warnings"]:
doc.log(f"[hopper_export_metadata] Warning: {w}")