311 lines
No EOL
11 KiB
Python
311 lines
No EOL
11 KiB
Python
"""Report JP executable documentation and Ghidra analysis coverage.
|
|
|
|
Run with /ja/CRUSADER.EXE active in Ghidra, using Script Manager or the live
|
|
Ghidra MCP read-only script endpoint. The script does not modify the program.
|
|
|
|
The primary progress proxy is exact function-entry addresses listed in
|
|
JP-address tables in docs/jp-remorse-decompilation-progress.md. This measures
|
|
address tracking, not semantic completeness; naming, comments, and byte
|
|
coverage are reported separately.
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
|
|
from ghidra.program.model.address import AddressSet
|
|
|
|
|
|
DOCUMENT_NAME = "jp-remorse-decompilation-progress.md"
|
|
GENERATED_NAME_PREFIXES = ("FUN_", "thunk_FUN_", "LAB_", "SUB_")
|
|
REPORT_LIST_LIMIT = 20
|
|
ADDRESS_BUCKET_SIZE = 0x10000
|
|
|
|
|
|
def find_progress_document():
|
|
candidate_paths = []
|
|
configured_root = os.environ.get("CRUSADER_DECOMP_ROOT")
|
|
if configured_root:
|
|
candidate_paths.append(
|
|
os.path.join(configured_root, "docs", DOCUMENT_NAME)
|
|
)
|
|
|
|
possible_roots = []
|
|
script_path = globals().get("__file__")
|
|
if script_path:
|
|
possible_roots.append(os.path.dirname(os.path.abspath(script_path)))
|
|
possible_roots.append(os.getcwd())
|
|
|
|
for possible_root in possible_roots:
|
|
current_path = os.path.abspath(possible_root)
|
|
for unused_level in range(8):
|
|
candidate_paths.append(
|
|
os.path.join(current_path, "docs", DOCUMENT_NAME)
|
|
)
|
|
parent_path = os.path.dirname(current_path)
|
|
if parent_path == current_path:
|
|
break
|
|
current_path = parent_path
|
|
|
|
candidate_paths.append(
|
|
os.path.join(
|
|
r"K:\ghidra\Crusader_Decomp", "docs", DOCUMENT_NAME
|
|
)
|
|
)
|
|
|
|
checked_paths = set()
|
|
for candidate_path in candidate_paths:
|
|
normalized_path = os.path.abspath(candidate_path)
|
|
if normalized_path in checked_paths:
|
|
continue
|
|
checked_paths.add(normalized_path)
|
|
if os.path.isfile(normalized_path):
|
|
return normalized_path
|
|
|
|
raise RuntimeError(
|
|
"Could not locate docs/" + DOCUMENT_NAME
|
|
+ ". Set CRUSADER_DECOMP_ROOT to the repository root."
|
|
)
|
|
|
|
|
|
def collect_progress_addresses(document_text):
|
|
addresses = set()
|
|
in_address_table = False
|
|
|
|
for line in document_text.splitlines():
|
|
stripped_line = line.strip()
|
|
if re.match(r"^\|\s*JP address\s*\|", stripped_line):
|
|
in_address_table = True
|
|
continue
|
|
if not stripped_line.startswith("|"):
|
|
in_address_table = False
|
|
continue
|
|
if not in_address_table:
|
|
continue
|
|
|
|
cells = stripped_line.split("|")
|
|
if len(cells) < 3:
|
|
continue
|
|
first_cell = cells[1]
|
|
for address_part in first_cell.split(","):
|
|
token = address_part.replace("`", "").strip()
|
|
match = re.fullmatch(r"(?:0[xX])?([0-9a-fA-F]{8})", token)
|
|
if match:
|
|
addresses.add(match.group(1).upper())
|
|
|
|
return addresses
|
|
|
|
|
|
def is_generated_function_name(name):
|
|
return name.startswith(GENERATED_NAME_PREFIXES)
|
|
|
|
|
|
def format_percent(numerator, denominator):
|
|
if denominator == 0:
|
|
return "n/a"
|
|
return "{:.1f}%".format(100.0 * numerator / denominator)
|
|
|
|
|
|
def build_report(program):
|
|
progress_path = find_progress_document()
|
|
with open(progress_path, "r", encoding="utf-8") as progress_file:
|
|
progress_text = progress_file.read()
|
|
progress_addresses = collect_progress_addresses(progress_text)
|
|
|
|
memory = program.getMemory()
|
|
executable_addresses = AddressSet()
|
|
executable_bytes = 0
|
|
executable_block_count = 0
|
|
for memory_block in memory.getBlocks():
|
|
if memory_block.isExecute():
|
|
executable_addresses.add(
|
|
memory_block.getStart(), memory_block.getEnd()
|
|
)
|
|
executable_bytes += int(memory_block.getSize())
|
|
executable_block_count += 1
|
|
|
|
function_manager = program.getFunctionManager()
|
|
all_function_count = int(function_manager.getFunctionCount())
|
|
functions_by_entry = {}
|
|
function_body_union = AddressSet()
|
|
function_body_bytes_sum = 0
|
|
generated_name_count = 0
|
|
function_comment_count = 0
|
|
thunk_count = 0
|
|
|
|
function_iterator = function_manager.getFunctions(True)
|
|
while function_iterator.hasNext():
|
|
function = function_iterator.next()
|
|
entry = function.getEntryPoint()
|
|
memory_block = memory.getBlock(entry)
|
|
if memory_block is None or not memory_block.isExecute():
|
|
continue
|
|
|
|
entry_text = str(entry).upper()
|
|
functions_by_entry[entry_text] = function
|
|
clipped_body = function.getBody().intersect(executable_addresses)
|
|
function_body_bytes_sum += int(clipped_body.getNumAddresses())
|
|
function_body_union.add(clipped_body)
|
|
|
|
if is_generated_function_name(function.getName()):
|
|
generated_name_count += 1
|
|
if function.getComment() and str(function.getComment()).strip():
|
|
function_comment_count += 1
|
|
if function.isThunk():
|
|
thunk_count += 1
|
|
|
|
function_count = len(functions_by_entry)
|
|
function_body_unique_bytes = int(function_body_union.getNumAddresses())
|
|
tracked_entries = set(functions_by_entry).intersection(progress_addresses)
|
|
untracked_functions = [
|
|
function
|
|
for entry_text, function in functions_by_entry.items()
|
|
if entry_text not in tracked_entries
|
|
]
|
|
untracked_functions.sort(key=lambda function: function.getEntryPoint().getOffset())
|
|
|
|
tracked_generated_count = sum(
|
|
1
|
|
for entry_text in tracked_entries
|
|
if is_generated_function_name(functions_by_entry[entry_text].getName())
|
|
)
|
|
tracked_named_count = len(tracked_entries) - tracked_generated_count
|
|
untracked_named_count = sum(
|
|
1
|
|
for function in untracked_functions
|
|
if not is_generated_function_name(function.getName())
|
|
)
|
|
unmatched_document_addresses = sorted(
|
|
progress_addresses.difference(functions_by_entry)
|
|
)
|
|
|
|
instruction_count = 0
|
|
instruction_bytes = 0
|
|
instruction_iterator = program.getListing().getInstructions(
|
|
executable_addresses, True
|
|
)
|
|
while instruction_iterator.hasNext():
|
|
instruction = instruction_iterator.next()
|
|
instruction_count += 1
|
|
instruction_bytes += int(instruction.getLength())
|
|
|
|
untracked_by_bucket = {}
|
|
for function in untracked_functions:
|
|
offset = int(function.getEntryPoint().getOffset())
|
|
bucket_start = (offset // ADDRESS_BUCKET_SIZE) * ADDRESS_BUCKET_SIZE
|
|
untracked_by_bucket[bucket_start] = (
|
|
untracked_by_bucket.get(bucket_start, 0) + 1
|
|
)
|
|
|
|
report_lines = [
|
|
"# JP Executable Documentation Coverage",
|
|
"",
|
|
"- Target: `{}` (`{}`), image base `{}`".format(
|
|
program.getName(), program.getDomainFile().getPathname(),
|
|
program.getImageBase()
|
|
),
|
|
"- Progress source: `{}`".format(progress_path),
|
|
"- Metric: exact function-entry addresses in `JP address` tables; this is address tracking, not a semantic-completeness score.",
|
|
"",
|
|
"## Documentation and Names",
|
|
"",
|
|
"- Ghidra functions across all address spaces: **{}**".format(
|
|
all_function_count
|
|
),
|
|
"- Executable functions: **{}**".format(function_count),
|
|
"- Ledger-tracked function entries: **{} / {} ({})**".format(
|
|
len(tracked_entries), function_count,
|
|
format_percent(len(tracked_entries), function_count)
|
|
),
|
|
"- Not yet listed in the primary progress tables: **{} ({})**".format(
|
|
len(untracked_functions),
|
|
format_percent(len(untracked_functions), function_count)
|
|
),
|
|
"- Tracked entries with descriptive Ghidra names: **{}**".format(
|
|
tracked_named_count
|
|
),
|
|
"- Tracked entries still using generated names: **{}**".format(
|
|
tracked_generated_count
|
|
),
|
|
"- Descriptive Ghidra names outside the progress tables: **{}**".format(
|
|
untracked_named_count
|
|
),
|
|
"- Functions with a Ghidra function comment: **{} / {} ({})**".format(
|
|
function_comment_count, function_count,
|
|
format_percent(function_comment_count, function_count)
|
|
),
|
|
"- Thunk functions: **{}**".format(thunk_count),
|
|
"- Progress-table addresses that are not executable function entries: **{}**".format(
|
|
len(unmatched_document_addresses)
|
|
),
|
|
"",
|
|
"## Executable Byte Coverage",
|
|
"",
|
|
"- Executable memory blocks: **{}**".format(executable_block_count),
|
|
"- Executable memory bytes: **{}**".format(executable_bytes),
|
|
"- Bytes covered by at least one defined function body: **{} / {} ({})**".format(
|
|
function_body_unique_bytes, executable_bytes,
|
|
format_percent(function_body_unique_bytes, executable_bytes)
|
|
),
|
|
"- Sum of function-body byte sizes (overlaps can count more than once): **{}**".format(
|
|
function_body_bytes_sum
|
|
),
|
|
"- Defined instruction bytes: **{} / {} ({})** across **{} instructions**".format(
|
|
instruction_bytes, executable_bytes,
|
|
format_percent(instruction_bytes, executable_bytes),
|
|
instruction_count
|
|
),
|
|
"- Executable bytes outside defined instructions: **{}** (may include data, padding, or unanalyzed code)".format(
|
|
executable_bytes - instruction_bytes
|
|
),
|
|
"",
|
|
"## Untracked Functions by 64 KiB Address Bucket",
|
|
"",
|
|
]
|
|
|
|
if untracked_by_bucket:
|
|
for bucket_start in sorted(untracked_by_bucket):
|
|
bucket_end = bucket_start + ADDRESS_BUCKET_SIZE - 1
|
|
report_lines.append(
|
|
"- `{:08X}-{:08X}`: {} untracked functions".format(
|
|
bucket_start, bucket_end,
|
|
untracked_by_bucket[bucket_start]
|
|
)
|
|
)
|
|
else:
|
|
report_lines.append("- None")
|
|
|
|
report_lines.extend(["", "## Next Untracked Entries", ""])
|
|
for function in untracked_functions[:REPORT_LIST_LIMIT]:
|
|
report_lines.append(
|
|
"- `{}` `{}`".format(
|
|
function.getEntryPoint(), function.getName()
|
|
)
|
|
)
|
|
if not untracked_functions:
|
|
report_lines.append("- None")
|
|
elif len(untracked_functions) > REPORT_LIST_LIMIT:
|
|
report_lines.append(
|
|
"- Showing first {} of {} untracked functions.".format(
|
|
REPORT_LIST_LIMIT, len(untracked_functions)
|
|
)
|
|
)
|
|
|
|
if unmatched_document_addresses:
|
|
report_lines.extend(["", "## Unmatched Table Addresses", ""])
|
|
for address_text in unmatched_document_addresses[:REPORT_LIST_LIMIT]:
|
|
report_lines.append("- `{}`".format(address_text))
|
|
if len(unmatched_document_addresses) > REPORT_LIST_LIMIT:
|
|
report_lines.append(
|
|
"- Showing first {} of {} unmatched addresses.".format(
|
|
REPORT_LIST_LIMIT, len(unmatched_document_addresses)
|
|
)
|
|
)
|
|
|
|
report_lines.extend([
|
|
"",
|
|
"Generated from the active Ghidra program. Ghidra project state and executable bytes were not modified.",
|
|
])
|
|
return "\n".join(report_lines)
|
|
|
|
|
|
print(build_report(currentProgram)) |