Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals

- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-14 14:55:05 +09:00
co-authored by Claude Opus 5.5
parent 5bc5a01087
commit 4d91f80bae
106 changed files with 7009 additions and 181 deletions
+17 -7
View File
@@ -27,6 +27,7 @@ A = ap.parse_args()
IB = 0x100000000
ARENA = 0x0; ARENA_SZ = 0x1000000 # 低 16 MB 可读写随机区 | Low 16 MB readable/writable random area
TSD = 0x2000000
TPA = TSD + 0x8000 # linux-bionic:模拟 Bionic 线程指针区,[TPA + TP_SLOT_OFF] = TSD | linux-bionic: emulated Bionic thread pointer area, [TPA + TP_SLOT_OFF] = TSD
SP0 = 0x800000
DIR, MOD = A.dir, A.mod
hdr = nsoimg.header(DIR, MOD) # 表头:段边界与写死进指令的线程槽号 | Header: segment boundaries and the thread slot numbers hard-coded into instructions
@@ -80,12 +81,13 @@ def plain_block(a):
"""普通改写块(stp; 键页间接读槽; 替换指令; 键页间接写槽; ...)→ 替换指令偏移。 | Ordinary rewrite block (stp; indirect slot read via key page; replaced instruction; indirect slot write via key page; ...) → offset of the replaced instruction."""
s = btarget(pat, a); w0 = struct.unpack_from('<I', pat, s)[0]
if w0 & 0xFFC003E0 != 0xA90003E0: return None # stp xS, xT, [sp, #off]
ins = list(md.disasm(bytes(pat[s:s + 36]), s))
if len(ins) < 8 or ins[1].mnemonic != 'mrs' or 'tpidrro_el0' not in ins[1].op_str:
return None
ins = list(md.disasm(bytes(pat[s:s + 40]), s))
sb = nsoimg.slot_base(ins, 1, hdr)
if not sb: return None
T, d = sb; ins = ins[:1] + ins[d:] # 基址前缀折成一条,下标与 apple 相同 | Fold the base prefix into one so indices match apple
if len(ins) < 8: return None
if ins[2].mnemonic != 'adrp' or ins[2].operands[1].imm != KEY_PAGE_OFF: return None
if ins[3].mnemonic != 'ldr' or ins[3].operands[1].mem.base != ins[2].operands[0].reg: return None
T = ins[1].operands[0].reg
if ins[4].mnemonic != 'add' or ins[4].operands[0].reg != T or ins[4].operands[1].reg != T: return None
if ins[5].mnemonic != 'ldr' or ins[5].operands[1].mem.base != T: return None # ldr xS,[xT]:读槽 | ldr xS,[xT]: read the slot
if ins[7].mnemonic != 'str' or ins[7].operands[1].mem.base != T: return None # str xS,[xT]:写回槽——只有普通块有,条件块没有(与旧版口径相同) | str xS,[xT]: write back to the slot - only ordinary blocks have it, conditional blocks do not (same convention as the old version)
@@ -102,9 +104,14 @@ def cond_target(w, pc):
return None
win_br = {}
for a0, hi0 in winend.items():
gate = btarget(pat, a0); pre = list(md.disasm(bytes(pat[gate:gate + 24]), gate))
assert [x.mnemonic for x in pre] == ['stp', 'mrs', 'adrp', 'ldr', 'add', 'ldr'], f'窗口块前导不是预期形状 {a0:#x}'
copy0 = gate + 24; lst = []
# 前导:apple = stp; mrs; adrp; ldr; add; ldr。android 的槽块基址多一条(mrs tpidr_el0; ldr [tp,#off]),窗口引用 sp 时先 sub sp / stp / str / add U——
# 所以按「adrp 键页; ldr; add; ldr 读槽」这一串定位,副本紧跟其后
# Prologue: apple = stp; mrs; adrp; ldr; add; ldr. android's slot base takes one more instruction (mrs tpidr_el0; ldr [tp,#off]), and a window that references sp first does sub sp / stp / str / add U --
# so locate by the "adrp key page; ldr; add; ldr slot read" run; the copy follows right after it
gate = btarget(pat, a0); pre = list(md.disasm(bytes(pat[gate:gate + 48]), gate)); mn = [x.mnemonic for x in pre]
k = next((i for i in range(len(mn) - 3) if mn[i:i + 4] == ['adrp', 'ldr', 'add', 'ldr'] and pre[i].operands[1].imm == KEY_PAGE_OFF), None)
assert mn[0] in ('stp', 'sub') and k is not None and k <= 6, f'窗口块前导不是预期形状 {a0:#x}'
copy0 = pre[k + 3].address + 4; lst = []
for c in range(a0, hi0, 4):
wo = struct.unpack_from('<I', orig, c)[0]; to = cond_target(wo, c)
if to is None: continue
@@ -170,6 +177,9 @@ def run(u, st, start, patched, init):
u.reg_write(UC_ARM64_REG_SP, SP0); u.reg_write(UC_ARM64_REG_NZCV, init['nzcv'])
u.reg_write(UC_ARM64_REG_TPIDR_EL0, init['tls'])
if not patched: u.reg_write(UC_ARM64_REG_TPIDRRO_EL0, init['tlr'])
if patched and hdr['ABI'] != 'apple': # 改写映像不读 tpidrro;真 tpidr_el0 是宿主线程指针 | The rewritten image does not read tpidrro; the real tpidr_el0 is the host thread pointer
u.reg_write(UC_ARM64_REG_TPIDRRO_EL0, 0)
u.reg_write(UC_ARM64_REG_TPIDR_EL0, TPA); u.mem_write(TPA + hdr['TP_SLOT_OFF'], struct.pack('<Q', TSD))
if patched:
for key, idx in KEY_IDX.items():
u.mem_write(IB + KEY_PAGE_OFF + 8 * idx, struct.pack('<Q', KEY_SLOT[key] * 8))
+9 -2
View File
@@ -34,6 +34,7 @@ IB = 0x100000000
ARENA, ARENA_SZ = 0x10000000, 0x1000000 # 随机堆:一半的 8 字节字是指回堆内的指针 | Random heap: half of the 8-byte words are pointers back into the heap
STK, STK_SZ = 0x20000000, 0x100000; SP0 = STK + STK_SZ - 0x10000 # sp 以上留 64 KB 给栈传参 | 64 KB kept above sp for stack-passed arguments
TSD = 0x30000000
TPA = TSD + 0x8000 # linux-bionic:模拟 Bionic 线程指针区,[TPA + TP_SLOT_OFF] = TSD | linux-bionic: emulated Bionic thread pointer area, [TPA + TP_SLOT_OFF] = TSD
TRAP = 0x300000000; RET = 0x400000000; GATE = 0x500000000
hdr = nsoimg.header(A.dir, A.mod)
PO, TE = hdr['PATCH_OFF'], hdr['TEXT_END']
@@ -73,8 +74,11 @@ def btarget(b, o):
return o + 4 * imm
def plain_subst(a):
s = btarget(pat, a)
ins = list(md.disasm(bytes(pat[s:s + 24]), s))
if len(ins) < 5 or ins[0].mnemonic != 'stp' or ins[1].mnemonic != 'mrs' or 'tpidrro_el0' not in ins[1].op_str: return None
ins = list(md.disasm(bytes(pat[s:s + 28]), s))
sb = nsoimg.slot_base(ins, 1, hdr)
if not ins or ins[0].mnemonic != 'stp' or not sb: return None
ins = ins[:1] + ins[sb[1]:] # 基址前缀折成一条 | Fold the base prefix into one
if len(ins) < 5: return None
if ins[2].mnemonic != 'adrp' or ins[2].operands[1].imm != KEY_PAGE_OFF: return None
if ins[3].mnemonic != 'ldr' or ins[3].operands[1].mem.base != ins[2].operands[0].reg: return None
if ins[4].mnemonic != 'add' or ins[4].operands[0].reg != ins[1].operands[0].reg or ins[4].operands[1].reg != ins[1].operands[0].reg: return None
@@ -142,6 +146,9 @@ class Emu:
u.mem_write(IB + KEY_PAGE_OFF + 8 * idx, struct.pack('<Q', KEY_SLOT[key] * 8))
for k, v in ((K18, init['g'][18]), (KTLS, init['tls']), (KTLR, init['tlr']), (KGATE, GATE)): u.mem_write(TSD + 8 * k, struct.pack('<Q', v))
u.reg_write(UC_ARM64_REG_X18, 0xDEADBEEFDEADBEEF); u.reg_write(UC_ARM64_REG_TPIDR_EL0, 0)
if hdr['ABI'] != 'apple':
u.reg_write(UC_ARM64_REG_TPIDRRO_EL0, 0)
u.reg_write(UC_ARM64_REG_TPIDR_EL0, TPA); u.mem_write(TPA + hdr['TP_SLOT_OFF'], struct.pack('<Q', TSD))
else:
u.reg_write(UC_ARM64_REG_TPIDRRO_EL0, init['tlr']); u.reg_write(UC_ARM64_REG_TPIDR_EL0, init['tls'])
try: u.emu_start(IB + fn, 0, count=budget)
+22 -1
View File
@@ -21,9 +21,30 @@ def header(out_dir, mod):
h = {}
for line in open(f'{out_dir}/{mod}.fixups.tsv'):
if not line.startswith('#'): break
k, v = line[1:].split(); h[k] = int(v, 0) # 0x 开头为偏移,十进制为槽号 | A 0x prefix means an offset, decimal means a slot number
k, v = line[1:].split()
h[k] = v if k == 'ABI' else int(v, 0) # 0x 开头为偏移,十进制为槽号;ABI 为名字 | A 0x prefix means an offset, decimal means a slot number; ABI is a name
h.setdefault('ABI', 'apple')
return h
def slot_base(ins, i, hdr):
"""ins[i:] 是否为槽块基址前缀(patch_nso.emit_slot_base)→ (基址寄存器, 指令条数),否则 None。
apple:mrs xT, tpidrro_el0;linux-bionic:mrs xT, tpidr_el0 / ldr xT, [xT, #TP_SLOT_OFF]。
Whether ins[i:] is a slot block base prefix (patch_nso.emit_slot_base) → (base register, instruction count), else None.
apple: mrs xT, tpidrro_el0; linux-bionic: mrs xT, tpidr_el0 / ldr xT, [xT, #TP_SLOT_OFF]."""
if i >= len(ins) or ins[i].mnemonic != 'mrs': return None
T = ins[i].operands[0].reg
if hdr['ABI'] == 'apple':
return (T, 1) if ins[i].op_str.endswith('tpidrro_el0') else None
if not ins[i].op_str.endswith('tpidr_el0') or i + 1 >= len(ins): return None
l = ins[i + 1]
if l.mnemonic != 'ldr' or l.operands[0].reg != T or l.operands[1].mem.base != T or l.operands[1].mem.disp != hdr['TP_SLOT_OFF']: return None
return (T, 2)
def slot_base_words(hdr, reg):
"""槽块基址前缀的机器字(与 patch_nso.emit_slot_base 同)。 | Machine words of the slot block base prefix (same as patch_nso.emit_slot_base)."""
if hdr['ABI'] == 'apple': return [0xD53BD060 | reg]
return [0xD53BD040 | reg, 0xF9400000 | (hdr['TP_SLOT_OFF'] // 8 << 10) | (reg << 5) | reg]
def mod0(img):
m0 = struct.unpack_from('<I', img, 4)[0]
assert img[m0:m0 + 4] == b'MOD0'
+58 -21
View File
@@ -226,7 +226,13 @@ nwin_ref = sum(1 for o in ind if o in inwin)
# ④ 补丁区干净
# ④ Patch area is clean
P = llvm_dis(pat[PO:], PO)
left = sorted(o for o, t in P.items() if RX18.search(t) or sysk(t) not in (None, 'tpidrro_r'))
# 槽块基址前缀(nsoimg.slot_base_words)所在偏移:apple 为 mrs tpidrro,linux-bionic 为 mrs tpidr_el0 + ldr [tp,#TP_SLOT_OFF],只在这些位置放行
# Offsets of slot block base prefixes (nsoimg.slot_base_words): apple = mrs tpidrro, linux-bionic = mrs tpidr_el0 + ldr [tp,#TP_SLOT_OFF]; allowed only at these positions
def is_base(o):
bw = nsoimg.slot_base_words(h, W(pat, o) & 31)
return o + 4 * len(bw) <= len(pat) and all(W(pat, o + 4 * i) == x for i, x in enumerate(bw))
BASE_K = 'tpidrro_r' if h['ABI'] == 'apple' else 'tpidr_r'
left = sorted(o for o, t in P.items() if RX18.search(t) or (sysk(t) is not None and not (sysk(t) == BASE_K and is_base(o))))
if left: bad(f'补丁区残留 x18/系统寄存器/svc:{len(left)} 例 {[(hex(x), P[x]) for x in left[:5]]}')
# ⑤ 宿主门
@@ -235,9 +241,11 @@ KEY = {k[4:-4]: v for k, v in h.items() if k.startswith('KEY_') and k.endswith('
from capstone import Cs, CS_ARCH_ARM64, CS_MODE_ARM
_md = Cs(CS_ARCH_ARM64, CS_MODE_ARM); _md.detail = True
def keyseq(at):
q = list(_md.disasm(pat[at:at + 20], at))
if len(q) < 5 or q[0].mnemonic != 'mrs' or 'tpidrro_el0' not in q[0].op_str \
or q[1].mnemonic != 'adrp' or q[1].operands[1].imm != h['KEY_PAGE_OFF'] \
q = list(_md.disasm(pat[at:at + 24], at))
sb = nsoimg.slot_base(q, 0, h)
if not sb: return None
q = q[sb[1] - 1:] # 基址前缀折成一条 | Fold the base prefix into one
if len(q) < 5 or q[1].mnemonic != 'adrp' or q[1].operands[1].imm != h['KEY_PAGE_OFF'] \
or q[2].mnemonic != 'ldr' or q[2].operands[1].mem.base != q[1].operands[0].reg \
or q[3].mnemonic != 'add' or q[3].operands[0].reg != q[0].operands[0].reg \
or q[3].operands[1].reg != q[0].operands[0].reg:
@@ -245,21 +253,31 @@ def keyseq(at):
d = q[2].operands[1].mem.disp
return (q[3].address + 4, d // 8) if d % 8 == 0 and 0 <= d // 8 < 8 else None
def paramseq(at, r):
q = list(_md.disasm(pat[at:at + 40], at))
q = list(_md.disasm(pat[at:at + 44], at))
sb = nsoimg.slot_base(q, 1, h)
if not sb: return None
q = q[:2] + q[1 + sb[1]:] # 基址前缀折成一条(留 mrs)| Fold the base prefix into one (keep the mrs)
tmp = 17 if r == 16 else 16
rn = lambda ins, i: ins.reg_name(ins.operands[i].reg)
if len(q) < 8 or q[0].mnemonic not in ('stur', 'str') or rn(q[0], 0) != f'x{tmp}' \
or q[0].operands[1].mem.base != capstone.arm64_const.ARM64_REG_SP or q[0].operands[1].mem.disp not in (-16, -24) \
if h['ABI'] != 'apple':
# android 没有红区:stp xtmp,xzr,[sp,#-16]! … ldp xtmp,xzr,[sp],#16 | android has no red zone: stp xtmp,xzr,[sp,#-16]! ... ldp xtmp,xzr,[sp],#16
ok_save = len(q) >= 8 and q[0].mnemonic == 'stp' and rn(q[0], 0) == f'x{tmp}' and rn(q[0], 1) == 'xzr' and q[0].writeback \
and q[0].operands[2].mem.base == capstone.arm64_const.ARM64_REG_SP and q[0].operands[2].mem.disp == -16
ok_rest = len(q) >= 8 and q[6].mnemonic == 'ldp' and rn(q[6], 0) == f'x{tmp}' and rn(q[6], 1) == 'xzr' and q[6].writeback \
and q[6].operands[2].mem.base == capstone.arm64_const.ARM64_REG_SP and q[6].operands[-1].imm == 16
else:
ok_save = len(q) >= 8 and q[0].mnemonic in ('stur', 'str') and rn(q[0], 0) == f'x{tmp}' \
and q[0].operands[1].mem.base == capstone.arm64_const.ARM64_REG_SP and q[0].operands[1].mem.disp in (-16, -24)
ok_rest = len(q) >= 8 and q[6].mnemonic in ('ldur', 'ldr') and rn(q[6], 0) == f'x{tmp}' \
and q[6].operands[1].mem.base == capstone.arm64_const.ARM64_REG_SP and q[6].operands[1].mem.disp == q[0].operands[1].mem.disp
if not ok_save or not ok_rest \
or q[1].mnemonic != 'mrs' or rn(q[1], 0) != f'x{r}' \
or 'tpidrro_el0' not in q[1].op_str or q[2].mnemonic != 'adrp' \
or q[2].mnemonic != 'adrp' \
or q[2].operands[1].imm != h['KEY_PAGE_OFF'] or q[3].mnemonic != 'ldr' \
or q[3].operands[1].mem.base != q[2].operands[0].reg or q[4].mnemonic != 'add' \
or q[4].operands[0].reg != q[1].operands[0].reg or q[4].operands[1].reg != q[1].operands[0].reg \
or q[5].mnemonic != 'ldr' or q[5].operands[0].reg != q[1].operands[0].reg \
or q[5].operands[1].mem.base != q[4].operands[0].reg or q[6].mnemonic not in ('ldur', 'ldr') \
or rn(q[6], 0) != f'x{tmp}' \
or q[6].operands[1].mem.base != capstone.arm64_const.ARM64_REG_SP \
or q[6].operands[1].mem.disp != q[0].operands[1].mem.disp or q[7].mnemonic != 'b':
or q[5].operands[1].mem.base != q[4].operands[0].reg or q[7].mnemonic != 'b':
return None
d = q[3].operands[1].mem.disp
return (q[6].address + 4, d // 8)
@@ -342,10 +360,10 @@ if set(KEY) != {'K18', 'KTLS', 'KTLR', 'KGATE', 'KRES0', 'KRES1', 'KASPECT', 'KA
kv = {v: k for k, v in KEY.items()}; nk = {}
for o in range(PO, len(pat) - 4, 4):
w = W(pat, o)
if w & 0xFFFFFFE0 != 0xD53BD060: continue # mrs xN, tpidrro_el0
if w & 0xFFFFFFE0 != nsoimg.slot_base_words(h, 0)[0]: continue # mrs xN, tpidrro_el0(apple)/ tpidr_el0(linux-bionic)
n = w & 31; ks = keyseq(o); k = kv.get(ks[1]) if ks else None
if not ks or k is None:
bad(f'补丁区 {o:#x} 读 tpidrro 后不是键页间接槽序列'); continue
bad(f'补丁区 {o:#x} 读线程寄存器后不是键页间接槽序列'); continue
nk[k] = nk.get(k, 0) + 1
expected = {}
for kind in sites.values():
@@ -456,6 +474,19 @@ def _mem_sp(ins, load):
if op.mem.disp < 0: return op.mem.disp
if load and ins.mnemonic == 'ldp' and op.mem.disp == 0: return -16
return None
def _sp_mem(ins, cur):
"""android:块内 sp 相对入口的偏移 cur 逐条累计(stp/ldp 前后变址写回、sub/add sp,sp,#imm),返回 (本条访问地址相对入口 sp 的偏移或 None, 新 cur)。
android: accumulate the block's sp offset cur relative to entry instruction by instruction (pre/post-index writeback of stp/ldp, sub/add sp,sp,#imm); returns (this access's address offset relative to entry sp or None, new cur)."""
ops = ins.operands
if ins.mnemonic in ('sub', 'add') and len(ops) == 3 and ops[0].type == capstone.arm64_const.ARM64_OP_REG and ins.reg_name(ops[0].reg) == 'sp' \
and ins.reg_name(ops[1].reg) == 'sp' and ops[2].type == capstone.arm64_const.ARM64_OP_IMM:
sh = ops[2].shift.value if ops[2].shift.type else 0
return None, cur + (ops[2].imm << sh) * (-1 if ins.mnemonic == 'sub' else 1)
mem = next((op for op in ops if op.type == capstone.arm64_const.ARM64_OP_MEM), None)
if mem is None or ins.reg_name(mem.mem.base) != 'sp': return None, cur
if ins.writeback and getattr(ins, 'post_index', False): return cur, cur + ops[-1].imm
if ins.writeback: return cur + mem.mem.disp, cur + mem.mem.disp
return cur + mem.mem.disp, cur
def _block(start):
out = []
for ins in _md.disasm(pat[start:start + 256], start):
@@ -479,22 +510,28 @@ for a, allowed in static_entries:
allowed.update(('x0', 'x1'))
saved = {}
restored = {}
_cur = 0; _abs = []
for ins in block:
d, _cur = _sp_mem(ins, _cur); _abs.append(d)
def _msp(i, ins, load):
if h['ABI'] == 'apple': return _mem_sp(ins, load)
return _abs[i] if ins.mnemonic.startswith('ld' if load else 'st') else None
for i, ins in enumerate(block):
d = _mem_sp(ins, False)
d = _msp(i, ins, False)
if d is not None:
regs = [op.reg for op in ins.operands[:2] if op.type == capstone.arm64_const.ARM64_OP_REG]
for reg in regs:
for j, reg in enumerate(regs):
name = ins.reg_name(reg); name = 'x29' if name == 'fp' else 'x30' if name == 'lr' else re.sub(r'^[wx]', 'x', name)
saved.setdefault(name, []).append((d, i))
d = _mem_sp(ins, True)
saved.setdefault(name, []).append((d + 8 * j if h['ABI'] != 'apple' else d, i)) # android 按真实地址:成对的第二个寄存器在 +8 | android uses the real address: the second register of a pair is at +8
d = _msp(i, ins, True)
if d is not None:
regs = [op.reg for op in ins.operands[:2] if op.type == capstone.arm64_const.ARM64_OP_REG]
for reg in regs:
for j, reg in enumerate(regs):
name = ins.reg_name(reg); name = 'x29' if name == 'fp' else 'x30' if name == 'lr' else re.sub(r'^[wx]', 'x', name)
restored.setdefault(name, []).append((d, i))
restored.setdefault(name, []).append((d + 8 * j if h['ABI'] != 'apple' else d, i)) # android 按真实地址:成对的第二个寄存器在 +8 | android uses the real address: the second register of a pair is at +8
writes = set().union(*(_writes(ins) for ins in block)) if block else set()
for reg in sorted(writes - allowed):
wi = [i for i, ins in enumerate(block) if reg in _writes(ins) and not (i > 0 and _mem_sp(ins, True) is not None)]
wi = [i for i, ins in enumerate(block) if reg in _writes(ins) and not (i > 0 and _msp(i, ins, True) is not None)]
dead = any(ins.mnemonic in ('br', 'blr') and ins.operands and ins.reg_name(ins.operands[0].reg) in (reg, reg[1:]) for ins in block)
ok = dead or any(sd == rd and si < min(wi, default=10**9) and ri > max(wi, default=-1)
for sd, si in saved.get(reg, []) for rd, ri in restored.get(reg, []))