mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-25 18:48:51 -04:00
Evil HD (SMAA Ultra, 16x filtering, full textures, ambient occlusion with bounce light, sunlight shafts, foliage drawn 4x as far, the remastered texture pack's loader and the launcher's download of it), FSR 3.1 frame generation for every GPU, Intel XeSS, FSR 4 model choice, ReShade presets, Auto (this PC), HUD placement, the options menu in 13 languages, a second input for every control, hold to toggle, DualSense speaker and haptics, NFC from the player's own dumps, the above-60 fps game fixes, the Debug tab's flight and Inventory fixes, the launcher's full settings, and over 250 fixes.
184 lines
8.3 KiB
Python
184 lines
8.3 KiB
Python
"""(Triforce) The discovery's walks in spawned processes where there is no fork (Windows), with the kit's result.
|
|
|
|
a64_lift.Lifter.discover walks every entry in address order (~20 s of one core for OoT on a 16-thread PC, and more on a busy one,
|
|
while the other cores wait), then retries the jump tables it missed, finds the PLT stubs and the setjmp owners. Here its first
|
|
loop's walks are made by processes, the entries cut into parts of consecutive addresses (at unwind-range starts), each part walked
|
|
in order by a lifter of its own; the kit's discover then runs as it is, its first loop given those bodies (the rest, from the
|
|
retry on, walks as always, in this process).
|
|
|
|
What one function's walk finds depends on the others in one way: a jump table whose address is not in the window before its BR
|
|
is looked for along the code's predecessors (a64_lift reaching_constants, preds), which include the tables found so far in the
|
|
same unwind range. So the parts agree with the kit's single walk unless two parts meet in a range where that search ran: each
|
|
part reports the ranges it found tables in and the ranges where the search ran, and when one range has both from two parts, or
|
|
the search from two, the parts are thrown away and the kit's discover walks everything, as before (conflicts).
|
|
tests/test_discover.py checks the merge and the fallbacks on a made-up image.
|
|
|
|
The workers rebuild the image from <img_dir>/image.bin (what image.write wrote, after its relocations) and the entries, hooks
|
|
and unwind starts the lifter has; they are spawned processes, which run nx2/lift.py's module level again (nx2/insn.py's
|
|
classify, nx2/lift_fixes.py's jump tables, nx2/walk.py): a worker whose walk is not this process's (identities) stops, and the
|
|
kit's discover walks all. HOOKSHOT_LIFT_JOBS=1, a fork (Linux, macOS: unchanged there), another image layout, a Lifter subclass
|
|
or fewer than MIN_ENTRIES entries: the kit's discover, in this process.
|
|
"""
|
|
import bisect
|
|
import collections
|
|
import multiprocessing
|
|
import os
|
|
from concurrent.futures import ProcessPoolExecutor
|
|
|
|
import a64_lift
|
|
import nx_image
|
|
|
|
PARTS_PER_JOB = 3 # (parts are uneven: more parts than processes even them out)
|
|
MIN_ENTRIES = 4000 # (fewer: the processes' start costs more than the walks)
|
|
|
|
_view = None # (a worker's lifter)
|
|
|
|
|
|
def identities():
|
|
"""Where the callables the walk goes through come from, as (module, qualified name): a worker compares its own with the
|
|
lift's before it walks (as nx2/emit_pool.py's workers do for the emit)"""
|
|
L = a64_lift.Lifter
|
|
return [("__main__" if f.__module__ == "__mp_main__" else f.__module__, f.__qualname__)
|
|
for f in (a64_lift.classify, L.walk, L.jump_table, L.function_constant, L.reaching_constants, L.preds, L.add_jt_edges,
|
|
L.hoisted_constant, L.adr_targets)]
|
|
|
|
|
|
def parts_of(L, order, n):
|
|
"""order (sorted entries) cut into about n runs of consecutive entries, each cut where the unwind range changes"""
|
|
starts = L.eh_starts
|
|
|
|
def ext(e):
|
|
return bisect.bisect_right(starts, e)
|
|
cuts = [0]
|
|
for k in range(1, n):
|
|
i = max(cuts[-1] + 1, len(order) * k // n)
|
|
while i < len(order) and ext(order[i]) == ext(order[i - 1]):
|
|
i += 1
|
|
if i >= len(order):
|
|
break
|
|
cuts.append(i)
|
|
cuts.append(len(order))
|
|
return [order[a:b] for a, b in zip(cuts, cuts[1:]) if b > a]
|
|
|
|
|
|
def _init(path, base, end, code, entries, hooks, eh_starts, theirs):
|
|
global _view
|
|
import image
|
|
try:
|
|
import emit_pool
|
|
emit_pool._no_throttle() # (as the emit's processes: Windows slows a background window's processes to almost nothing)
|
|
emit_pool._leave_with_parent() # (and one whose lift was stopped from outside ends by itself)
|
|
except ImportError:
|
|
pass
|
|
nx_image.MAIN_BASE = base
|
|
img = object.__new__(image.EdenImage)
|
|
with open(path, "rb") as f:
|
|
img.mem = bytearray(f.read())
|
|
if len(img.mem) != end - base:
|
|
raise RuntimeError("%s is %d bytes, the lift's image %d" % (path, len(img.mem), end - base))
|
|
img.end, img.code = end, code
|
|
img.lookups()
|
|
img.entries, img.hooks, img.names, img.eh_starts = set(entries), dict(hooks), {}, eh_starts
|
|
if identities() != theirs:
|
|
raise RuntimeError("this process walks with %s, the lift's with %s" % (identities(), theirs))
|
|
_view = a64_lift.Lifter(img)
|
|
|
|
|
|
def walk_part(L, es):
|
|
"""The kit's first loop over es with L's state made fresh: (funcs, jt, jt_missing, stats, ranges with tables found,
|
|
ranges whose predecessors were read). The predecessors (a64_lift preds, with the tables found so far in the range) are
|
|
the only state of one walk that another walk's changes: reaching_constants reads them. (Tracked at preds, not at
|
|
reaching_constants, which nx2/lift_fixes.py replaces on the instance for a while and then deletes)"""
|
|
L.funcs, L.jt, L.jt_missing, L.stats = {}, {}, {}, collections.Counter()
|
|
L.__dict__.pop("_preds", None)
|
|
searched = set()
|
|
base = type(L).preds
|
|
|
|
def preds(lo, hi):
|
|
searched.add(lo)
|
|
return base(L, lo, hi)
|
|
L.preds = preds
|
|
try:
|
|
for e in es:
|
|
if e in L.hooks:
|
|
continue
|
|
L.funcs[e] = L.walk(e)
|
|
finally:
|
|
del L.preds
|
|
found = {L.function_extent(pc)[0] for pc in L.jt}
|
|
return L.funcs, L.jt, L.jt_missing, L.stats, found, searched
|
|
|
|
|
|
def _walk(es):
|
|
return walk_part(_view, es)
|
|
|
|
|
|
def conflicts(results):
|
|
"""The unwind ranges where two parts could have seen each other's tables: a range searched by one part (reaching_constants)
|
|
with tables found or searched by another"""
|
|
by = collections.defaultdict(set)
|
|
searched = set()
|
|
for i, r in enumerate(results):
|
|
for x in r[4] | r[5]:
|
|
by[x].add(i)
|
|
searched |= r[5]
|
|
return sorted(x for x in searched if len(by[x]) > 1)
|
|
|
|
|
|
def merge(L, results):
|
|
"""The parts' walks as the kit's first loop leaves them: their bodies by entry (for discover's first loop), and L's jt,
|
|
jt_missing and stats"""
|
|
bodies = {}
|
|
for funcs, jt, missing, stats, _, _ in results:
|
|
bodies.update(funcs)
|
|
L.jt.update(jt)
|
|
L.jt_missing.update(missing)
|
|
L.stats.update(stats)
|
|
return bodies
|
|
|
|
|
|
def discover(L, img_dir, jobs, max_insns=0, run=None):
|
|
"""L.discover(max_insns), its first loop's walks made by `jobs` spawned processes when they can be (the module's note).
|
|
run(fn, parts): how the parts are walked (the processes; a test's stand-in). Returns the processes used (1: none)"""
|
|
import chunks
|
|
import image
|
|
img = L.img
|
|
if run is None and (jobs <= 1 or chunks.FORK or type(L) is not a64_lift.Lifter
|
|
or type(img) is not image.EdenImage or len(L.entries) < MIN_ENTRIES):
|
|
L.discover(max_insns=max_insns)
|
|
return 1
|
|
order = sorted(L.entries)
|
|
parts = parts_of(L, order, max(1, jobs) * PARTS_PER_JOB)
|
|
try:
|
|
if run is not None:
|
|
results = run(walk_part, parts)
|
|
else:
|
|
path = os.path.join(img_dir, "image.bin")
|
|
args = (path, nx_image.MAIN_BASE, img.end, img.code, L.entries - set(L.hooks), L.hooks, L.eh_starts, identities())
|
|
with ProcessPoolExecutor(max_workers=jobs, mp_context=multiprocessing.get_context("spawn"),
|
|
initializer=_init, initargs=args) as pool:
|
|
results = list(pool.map(_walk, parts))
|
|
except Exception as e: # (BrokenProcessPool, a worker that stopped or could not start, anything raised in a walk)
|
|
print("discovery: the walks' processes stopped (%s): walked here" % str(e).strip().splitlines()[-1:], flush=True)
|
|
L.discover(max_insns=max_insns)
|
|
return 1
|
|
shared = conflicts(results)
|
|
if shared:
|
|
print("discovery: %d unwind ranges searched for jump tables by two parts (first at %#x): walked here, in order"
|
|
% (len(shared), shared[0]), flush=True)
|
|
L.discover(max_insns=max_insns)
|
|
return 1
|
|
bodies = merge(L, results)
|
|
walk = L.walk
|
|
|
|
def given(e): # (the kit's first loop takes each body walked already; any later walk is a real one)
|
|
b = bodies.pop(e, None)
|
|
return b if b is not None else walk(e)
|
|
L.walk = given
|
|
try:
|
|
L.discover(max_insns=max_insns)
|
|
finally:
|
|
del L.walk
|
|
print("discovery: %d parts walked in %d processes" % (len(parts), jobs), flush=True)
|
|
return jobs
|