Rewriter r41: fix the exit order of exclusive-window copies; equivalence check forces both branch directions

In rewritten compare-and-swap windows the sequential fall-out exit was placed after the external exits, so a successful write fell into the "compare not equal" exit and was handled as a failed compare (an operation done twice, or a lock-free list popped more than once). Stub 0 is now the fall-out exit. equiv.py now forces each conditional branch inside such a window through both directions, since random states practically never take the "equal" side.

The build output generation changes from r40 to r41; shells built for r40 are refused by the packaging tool. Use the v1.0.3 shell.
This commit is contained in:
jhaizhou
2026-10-13 13:47:28 +08:00
parent 30dc58383c
commit b5c910ef98
3 changed files with 47 additions and 5 deletions
+1 -1
View File
@@ -1 +1 @@
3.3.94012-r40
3.3.94012-r41
+10 -3
View File
@@ -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
+36 -1
View File
@@ -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('<I', orig, c)[0]; to = cond_target(wo, c)
if to is None: continue
cp = copy0 + (c - a0); wp = struct.unpack_from('<I', pat, cp)[0]; tp_ = cond_target(wp, cp)
assert tp_ is not None, f'窗口副本里找不到对应的条件分支 {c:#x}'
lst.append((c, wo, to, cp, wp, tp_))
if lst: win_br[a0] = lst
def enc_b(pc, tgt): return 0x14000000 | (((tgt - pc) >> 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('<I', 0xD503201F if mode == '不跳' else enc_b(c, to)))
UP.mem_write(IB + cp, struct.pack('<I', 0xD503201F if mode == '不跳' else enc_b(cp, tp_)))
UO.ctl_remove_cache(IB + c, IB + c + 4); UP.ctl_remove_cache(IB + cp, IB + cp + 4)
for kind in (0, 1, 0, 1):
same, both, fl = trial(UO, SO, UP, SP, a, init_state(rng, kind))
n += 1; eff += not both
if not same: diffs.append((f'窗口分支 {c:#x} 强制{mode}', fl))
UO.mem_write(IB + c, struct.pack('<I', wo)); UP.mem_write(IB + cp, struct.pack('<I', wp))
UO.ctl_remove_cache(IB + c, IB + c + 4); UP.ctl_remove_cache(IB + cp, IB + cp + 4)
out.append((a, n, eff, diffs))
return out
@@ -253,5 +288,5 @@ if __name__ == '__main__':
ok = not bad and not zero and not miss # 变异漏抓 = 判据分不出坏改写,判红(早先漏抓只打印不判) | A missed mutation = the criterion cannot tell a bad rewrite apart, so fail (earlier a miss was only printed, not judged)
print(f'EQUIV {MOD} {"OK" if ok else "FAIL"} 站点={len(res)} 试验={T} 有效试验={E} 双异常={T - E} '
f'有效≥1站点={len(res) - len(zero)} 有效≥{A.min_eff}站点={len(res) - len(low)} 零有效={len(zero)} 分歧站点={len(bad)} '
f'门站点(结构核对)={ngate} 已登记剪口(结构核对)={ncut} 观测门(结构核对)={nobs} 变异={len(mut_sites)}({mut_by}) 抓到={caught} 漏抓={miss} seed={A.seed}', flush=True)
f'窗口={len(winend)} 其中含条件分支={len(win_br)}({sum(len(v) for v in win_br.values())} 条×2 向强制) 门站点(结构核对)={ngate} 已登记剪口(结构核对)={ncut} 观测门(结构核对)={nobs} 变异={len(mut_sites)}({mut_by}) 抓到={caught} 漏抓={miss} seed={A.seed}', flush=True)
sys.exit(0 if ok else 1)