mirror of
https://github.com/sp00nznet/xboxrecomp.git
synced 2026-12-04 09:38:16 +00:00
d7774d5088
Rebuilt Halo with the kernel fixes and it held: exit=3, still reaching render_cameras.c:458, and PsCreateSystemThreadEx now genuinely spawns a worker instead of running it inline -- the <stdlib.h> malloc truncation fix taking effect. Then wrote the 23 bridge wrappers for the ordinals Halo's thunk table binds but the bridge did not route. Every one had a working xbox_* behind it, so this looked like transcription. It crashed the title EARLIER than the stubs it replaced, twice, with two different faults, and both times presented as "0 assert sites" -- the trap run.sh warns about. Bisected (11 -> 5 -> 3 -> 1) to a memory-model mismatch, not to argument arithmetic: xbox_IoCreateDevice HeapAllocs from GetProcessHeap() and writes that NATIVE pointer through its out-parameter. The bridge hands it the native address of a 4-BYTE GUEST slot, so a 64-bit pointer goes into 4 bytes -- clobbering the adjacent guest dword and leaving the title a truncated pointer it dereferences. The crash was a write to 0x90909090. xbox_ExFreePool calls HeapFree(GetProcessHeap(), P) on guest pool memory that was never on the host heap. These functions were written for a native caller: host pointers, host allocations. The bridge is a different world -- guest VAs, memory in the mapped guest space. XBOX_TO_NATIVE converts an address; it cannot convert an allocator. So the wrappers are left in place and NOT routed, with the reasoning at the call site, and tools.kernel_audit.coverage no longer calls category A "mechanical". Routing one now requires checking which side owns the allocation and whether any out-pointer must carry a guest VA. The Xc* crypto group and RtlTimeToTimeFields only read/write bytes at a caller-supplied address and should be fine; anything that allocates, frees, or returns a pointer is not. Halo is back at its best known state, verified: exit=3, milestones held, render_cameras.c:458 reached. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
203 lines
8.6 KiB
Python
203 lines
8.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Kernel coverage for one title: what it imports vs what the bridge routes.
|
|
|
|
py -3 -m tools.kernel_audit.coverage <analysis.json>
|
|
|
|
Answers the question you actually have when standing up a new title -- "how much
|
|
kernel work is left?" -- and, more usefully, splits the remainder by how much
|
|
work each piece is:
|
|
|
|
A. xbox_* exists, needs a bridge_ wrapper NOT mechanical, see below
|
|
B. data export needs a value, not a function
|
|
C. no implementation at all real work
|
|
|
|
The headline "N imports missing" number on its own is misleading in both
|
|
directions. An XBE imports whatever its statically-linked XDK references, so
|
|
much of the list is never called; and an unrouted ordinal is not necessarily
|
|
fatal, because kernel_bridge.c's argument-size table lets the generic stub clean
|
|
the right number of bytes off the simulated stack and return 0. A missing size entry is the dangerous case: the stub then pops
|
|
nothing and leaks the arguments.
|
|
|
|
Category A is not as cheap as it looks. An xbox_* existing does NOT mean a
|
|
wrapper around it is safe. Those functions were written for a native caller,
|
|
where pointers are host pointers and allocations come from the host heap; the
|
|
bridge is a different world where pointers are guest VAs and memory lives in the
|
|
mapped guest space. XBOX_TO_NATIVE converts an address, not an allocator.
|
|
|
|
Two that bit, on Halo 2276, each crashing it earlier than the stub it replaced:
|
|
|
|
IoCreateDevice HeapAllocs from GetProcessHeap() and writes that native
|
|
pointer through an out-parameter -- into a 4-byte guest slot.
|
|
ExFreePool calls HeapFree(GetProcessHeap(), P) on guest pool memory that
|
|
was never on the host heap.
|
|
|
|
Before routing one, check which side owns the allocation and whether any
|
|
out-pointer has to carry a guest VA. Functions that only read or write bytes at
|
|
a caller-supplied address are fine; functions that allocate, free, or return a
|
|
pointer are not.
|
|
|
|
Data exports are called out separately because they are a different mechanism:
|
|
the thunk holds a pointer to a variable, not a function, so routing one to a
|
|
bridge wrapper produces a call through a data address.
|
|
"""
|
|
|
|
import argparse
|
|
import glob
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
BRIDGE_C = os.path.join(ROOT, "src", "kernel", "kernel_bridge.c")
|
|
|
|
# Kernel exports whose thunk holds a variable rather than a function.
|
|
# Routing these to a bridge wrapper would call through a data address.
|
|
DATA_EXPORTS = {
|
|
"ExEventObjectType", "ExMutantObjectType", "ExSemaphoreObjectType",
|
|
"ExTimerObjectType", "IoCompletionObjectType", "IoDeviceObjectType",
|
|
"IoFileObjectType", "PsThreadObjectType", "ObDirectoryObjectType",
|
|
"ObSymbolicLinkObjectType",
|
|
"HalDiskCachePartitionCount", "HalDiskModelNumber", "HalDiskSerialNumber",
|
|
"HalBootSMCVideoMode", "IdexChannelObject",
|
|
"KeTickCount", "KeTimeIncrement", "KeInterruptTime", "KeSystemTime",
|
|
"LaunchDataPage", "XboxHardwareInfo", "XboxKrnlVersion", "XboxHDKey",
|
|
"XboxSignatureKey", "XboxLANKey", "XboxAlternateSignatureKeys",
|
|
"XePublicKeyData", "XeImageFileName",
|
|
# NOT here: XcKeyTable (347) looks like a table but is a function --
|
|
# VOID XcKeyTable(ULONG CipherSelect, PUCHAR KeyTable, PUCHAR Key) --
|
|
# and kernel_bridge.c already sizes it as 3 args.
|
|
}
|
|
|
|
|
|
def bridge_routes():
|
|
src = open(BRIDGE_C, encoding="utf-8", errors="replace").read()
|
|
return {int(o) for o in re.findall(
|
|
r"case\s+(\d+):\s*return\s+bridge_[A-Za-z_]", src)}
|
|
|
|
|
|
def bridge_arg_sizes():
|
|
src = open(BRIDGE_C, encoding="utf-8", errors="replace").read()
|
|
return {int(o) for o in re.findall(
|
|
r"case\s+(\d+):\s*return\s+\d+;\s*/\*", src)}
|
|
|
|
|
|
def thunk_routes():
|
|
"""Ordinals resolved as data exports, from kernel_data_va_for_ordinal.
|
|
|
|
Data exports are not functions: their thunk holds a pointer into emulated
|
|
kernel data, so there is nothing to wrap and no stack arguments to size.
|
|
Counting them as gaps overstates the work, which is why they are separated
|
|
out here.
|
|
|
|
Read from kernel_bridge.c's kernel_data_va_for_ordinal specifically, not
|
|
from kernel_thunks.c. kernel_thunks.c has its own ordinal switch for a
|
|
different purpose, and matching against it happened to give the right answer
|
|
for one title and the wrong one for the next.
|
|
"""
|
|
src = open(BRIDGE_C, encoding="utf-8", errors="replace").read()
|
|
m = re.search(r"kernel_data_va_for_ordinal.*?\n\}", src, re.S)
|
|
if not m:
|
|
return set()
|
|
return {int(o) for o in re.findall(
|
|
r"case\s+(\d+):\s*return\s+XBOX_KERNEL_DATA_BASE", m.group(0))}
|
|
|
|
|
|
def kernel_impls():
|
|
"""{name: file} for every xbox_<Name>() defined outside the bridge."""
|
|
out = {}
|
|
for path in glob.glob(os.path.join(ROOT, "src", "kernel", "*.c")):
|
|
if "kernel_bridge" in path:
|
|
continue
|
|
src = open(path, encoding="utf-8", errors="replace").read()
|
|
for name in re.findall(r"\bxbox_([A-Za-z_][A-Za-z0-9_]*)\s*\(", src):
|
|
out.setdefault(name, os.path.basename(path))
|
|
return out
|
|
|
|
|
|
def classify(imports):
|
|
routed, sizes, impls = bridge_routes(), bridge_arg_sizes(), kernel_impls()
|
|
thunks = thunk_routes()
|
|
covered, wrap, data, todo = [], [], [], []
|
|
for imp in sorted(imports, key=lambda i: i["ordinal"]):
|
|
o, n = imp["ordinal"], imp["name"]
|
|
if o in routed or (n in DATA_EXPORTS and o in thunks):
|
|
covered.append(imp)
|
|
elif n in DATA_EXPORTS:
|
|
data.append(imp)
|
|
elif n in impls:
|
|
wrap.append((imp, impls[n]))
|
|
else:
|
|
todo.append(imp)
|
|
return covered, wrap, data, todo, sizes
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser(prog="tools.kernel_audit.coverage",
|
|
description=__doc__,
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
ap.add_argument("analysis", help="analysis.json from tools.xbe_parser")
|
|
ap.add_argument("--list", action="store_true",
|
|
help="also list the ordinals already routed")
|
|
args = ap.parse_args(argv)
|
|
|
|
data_in = json.load(open(args.analysis))
|
|
imports = data_in["kernel_imports"]
|
|
title = data_in.get("title", os.path.basename(args.analysis))
|
|
covered, wrap, data, todo, sizes = classify(imports)
|
|
n = len(imports)
|
|
|
|
print("%s -- %d kernel imports" % (title, n))
|
|
print("=" * 66)
|
|
print(" handled (bridge or thunk) %4d (%.1f%%)" % (len(covered), 100.0 * len(covered) / n))
|
|
print(" xbox_* exists, needs a wrap %4d (check its memory model first)" % len(wrap))
|
|
print(" data exports %4d need a value, not a function" % len(data))
|
|
print(" no implementation %4d real work" % len(todo))
|
|
print()
|
|
|
|
_routed, _thunks = bridge_routes(), thunk_routes()
|
|
unsafe = [i for i in imports
|
|
if i["ordinal"] not in _routed and i["ordinal"] not in sizes
|
|
and not (i["name"] in DATA_EXPORTS and i["ordinal"] in _thunks)]
|
|
print(" of the %d unhandled, %d have no argument-size entry either --" %
|
|
(n - len(covered), len(unsafe)))
|
|
# stdcall_args_for_ordinal defaults to 0, so a missing entry makes the stub
|
|
# pop nothing and LEAK the caller's arguments -- 4 bytes per argument, every
|
|
# call. That accumulates rather than failing immediately, which is why it
|
|
# shows up far from the cause. (The repo has been here before: a 2-argument
|
|
# CRT handler leaking 0x24 bytes a call turned a 6-iteration init loop into
|
|
# 21,938 allocations and exhausted the heap.)
|
|
print(" so the stub pops nothing and leaks 4 bytes per argument per call,")
|
|
print(" which accumulates until something far away runs out of stack:")
|
|
for i in unsafe:
|
|
print(" %3d %s" % (i["ordinal"], i["name"]))
|
|
if not unsafe:
|
|
print(" (none -- every unrouted import can be stubbed safely)")
|
|
print()
|
|
|
|
if wrap:
|
|
print("-- A. needs a bridge_ wrapper around an existing xbox_* --")
|
|
for i, where in wrap:
|
|
print(" %3d %-36s %s" % (i["ordinal"], i["name"], where))
|
|
print()
|
|
if data:
|
|
print("-- B. data exports --")
|
|
for i in data:
|
|
print(" %3d %s" % (i["ordinal"], i["name"]))
|
|
print()
|
|
if todo:
|
|
print("-- C. no implementation --")
|
|
for i in todo:
|
|
print(" %3d %s" % (i["ordinal"], i["name"]))
|
|
print()
|
|
if args.list:
|
|
print("-- routed --")
|
|
for i in covered:
|
|
print(" %3d %s" % (i["ordinal"], i["name"]))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|