diff --git a/dm/PRODUCT b/dm/PRODUCT index 3d0e755..69849f8 100644 --- a/dm/PRODUCT +++ b/dm/PRODUCT @@ -1 +1 @@ -3.3.94012-r40 +3.3.94012-r41 diff --git a/dm/tools/patch_nso.py b/dm/tools/patch_nso.py index ef8fb80..4de8277 100644 --- a/dm/tools/patch_nso.py +++ b/dm/tools/patch_nso.py @@ -483,7 +483,7 @@ def site_window(seg): if m in ('cbz', 'cbnz', 'tbz', 'tbnz') or m.startswith('b.'): tgt = x.operands[-1].imm if lo <= tgt < hi: newt = copy0 + (tgt - lo) - else: ex_targets.append(tgt); newt = exits + 16 * (len(ex_targets) - 1) + else: ex_targets.append(tgt); newt = exits + 16 * len(ex_targets) # 出口桩第 0 个留给顺序落出(见下),窗口外的目标从第 1 个起 | Exit stub 0 is reserved for the sequential fall-out (see below); targets outside the window start from stub 1 pc = copy0 + 4 * k; off = (newt - pc) >> 2 if m in ('tbz', 'tbnz'): w = (w & 0xFFF8001F) | ((off & 0x3FFF) << 5) else: w = (w & 0xFF00001F) | ((off & 0x7FFFF) << 5) @@ -492,8 +492,15 @@ def site_window(seg): assert m not in ('adr', 'adrp', 'b', 'bl', 'br', 'blr', 'ret') and not (m == 'ldr' and '[' not in x.op_str), hex(x.address) words.append(w) for w in words: emit(w) - ex_targets.append(hi) - for t in ex_targets: + # 出口桩的次序:副本最后一条指令执行完是「顺序落出」,落进紧跟在副本后面的第 0 个出口桩——所以第 0 个必须是回 hi 的落出口,窗口外的跳转目标排在其后。 + # r40 及以前落出口排在最后:窗口里只要有跳到窗口外的条件跳转(比较交换的「比较不等」出口),写成功后就落进第一个外部出口 = 当成比较失败处理 + # (一次操作做两遍 / 无锁表被多弹)。暗黑有 6 个这样的窗口(main 0x1848eb8 / 0x1848f28 / 0x1848f84,sdk 0x2ea0fc / 0x2ea4cc / 0x2ea644), + # 只有跳回窗口内的 cbnz 的窗口不受影响。等价验证原先随机抽不到「比较相等」那一向,所以一直是绿的(equiv.py 已补窗口分支双向强制)。 + # Order of the exit stubs: when the last instruction of the copy has executed, control "falls out sequentially" into exit stub 0, which immediately follows the copy -- so stub 0 must be the fall-out exit back to hi, and jump targets outside the window come after it. + # Up to r40 the fall-out exit was placed last: whenever a window contained a conditional jump to outside the window (the "compare not equal" exit of a compare-and-swap), a successful write fell into the first external exit = handled as a failed compare + # (one operation done twice / a lock-free list popped more than once). The game has 6 such windows (main 0x1848eb8 / 0x1848f28 / 0x1848f84, sdk 0x2ea0fc / 0x2ea4cc / 0x2ea644); + # windows whose only branch is a cbnz back into the window are not affected. The equivalence check used to never draw the "compare equal" direction at random, so it stayed green (equiv.py now forces both directions of window branches). + for t in [hi] + ex_targets: emit(STRX(S, T)); emit(LDP(S, T, 31, -16)); emit(B(here(), t)); emit(NOP) return start, lo, hi diff --git a/dm/verify/equiv.py b/dm/verify/equiv.py index 0ad83a4..62b9431 100644 --- a/dm/verify/equiv.py +++ b/dm/verify/equiv.py @@ -90,6 +90,29 @@ def plain_block(a): 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) return ins[6].address +# 独占窗口内的条件分支(状态 cbz/cbnz 已被 deexcl 改成 nop,不在此列):随机状态几乎抽不到「比较相等」那一向(2^-32), +# 所以每条分支两个方向各强制走一遍——两份映像同一条分支同时改成 nop(不跳)/ 无条件 b(必跳),其余照常比。 +# 由来:r40 及以前改写器把窗口副本的「顺序落出」出口排在外部出口之后,比较交换窗口写成功后落进「比较不等」的出口;随机试验全绿(暗黑 6 个窗口:main 0x1848eb8 / 0x1848f28 / 0x1848f84,sdk 0x2ea0fc / 0x2ea4cc / 0x2ea644)。 +# Conditional branches inside an exclusive window (the status cbz/cbnz has already been turned into nop by deexcl and is not included): a random state almost never draws the "compare equal" direction (2^-32), +# so each branch is forced through both directions once -- in both images the same branch is changed to nop (not taken) / an unconditional b (taken) at the same time, and the rest is compared as usual. +# Background: up to r40 the rewriter placed the "sequential fall-out" exit of the window copy after the external exits, so a compare-and-swap window fell into the "compare not equal" exit after a successful write; the random trials were all green (6 windows in the game: main 0x1848eb8 / 0x1848f28 / 0x1848f84, sdk 0x2ea0fc / 0x2ea4cc / 0x2ea644). +def cond_target(w, pc): + if w & 0xFF000010 == 0x54000000 or w & 0x7E000000 == 0x34000000: imm = (w >> 5) & 0x7FFFF; imm -= (imm & 0x40000) << 1; return pc + 4 * imm + if w & 0x7E000000 == 0x36000000: imm = (w >> 5) & 0x3FFF; imm -= (imm & 0x2000) << 1; return pc + 4 * imm + 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 = [] + for c in range(a0, hi0, 4): + wo = struct.unpack_from('> 2) & 0x3FFFFFF) mut_sites = [] if A.mutate: cand = [a for a in sites if a not in winend and a not in brinfo and plain_block(a)] @@ -216,6 +239,18 @@ def work(job): n += 1; eff += not both if not same: diffs.append((kind, fl)) if mutated and diffs: break + if not mutated: + for c, wo, to, cp, wp, tp_ in win_br.get(a, ()): + for mode in ('不跳', '必跳'): + UO.mem_write(IB + c, struct.pack('