diff --git a/libs/RVL_SDK/src/wud/WUD.c b/libs/RVL_SDK/src/wud/WUD.c index 5c87ef9a..4f261021 100644 --- a/libs/RVL_SDK/src/wud/WUD.c +++ b/libs/RVL_SDK/src/wud/WUD.c @@ -3030,14 +3030,18 @@ void _WUDStartSyncDevice(BD_ADDR bd_addr, u8* bd_name) { void _WUDDeleteStoredDevice() { WUDCB* p_wcb = &_wcb; + BOOL enabled; + u8 deleteState; WUD_DEBUGPrint("_WUDDeleteStoreDevice()\n"); WUDiDeleteAllLinkKeys(); do { - ; - } while (p_wcb->deleteState != 0); + enabled = OSDisableInterrupts(); + deleteState = p_wcb->deleteState; + OSRestoreInterrupts(enabled); + } while (deleteState != WUD_STATE_DELETE_START); WUD_DEBUGPrint("dev number = %d\n", WUDiGetDevNumber()); } diff --git a/tools/decomp-assist/verify_wud_delete_wait.py b/tools/decomp-assist/verify_wud_delete_wait.py new file mode 100644 index 00000000..88ff9c02 --- /dev/null +++ b/tools/decomp-assist/verify_wud_delete_wait.py @@ -0,0 +1,284 @@ +#!/usr/bin/env python3 +"""Exercise the WUD deletion wait with real source and a host interrupt model. + +This is a bounded regression test, not Wii hardware or full Bluetooth emulation. +The alarm dispatches only when interrupts are restored, never inside a protected +read. The tested deletion/cleanup functions are extracted verbatim from WUD.c. +""" + +import argparse +from pathlib import Path +import re +import subprocess +import tempfile + + +FUNCTIONS = ( + "DeleteFlushCallback", "WUDiCleanUp", "WUDiDeleteAllComplete", + "DeleteAllHandler", "WUDStartClearDevice", "WUDiDeleteAllLinkKeys", + "WUDIsBusy", "_WUDDeleteStoredDevice", +) + + +def function(source, name): + match = re.search(r"^(?:static )?\w+ " + name + r"\([^\n]*\) \{", source, re.M) + assert match, name + start = match.start() + depth = 1 + at = match.end() + while depth: + depth += (source[at] == "{") - (source[at] == "}") + at += 1 + return source[start:at] + + +PRELUDE = r''' +#include +#include +#include +#include + +typedef int BOOL; +typedef unsigned char u8; +typedef unsigned int u32; +typedef int s32; +typedef int SCStatus; +typedef int WUDDeleteState; +typedef void (*WUDClearDeviceCallback)(s32); +typedef char SCBtDeviceInfoArray[4]; +typedef char SCBtCmpDevInfoArray[4]; +typedef struct { + u8 syncState, deleteState, stackState, initState; + u32 libStatus; + int alarm; + WUDClearDeviceCallback clearDevCB; +} WUDCB; +enum { FALSE, TRUE }; +enum { WUD_STATE_DELETE_START = 0, WUD_STATE_DELETE_DISALLOW_INCOMING = 1, + WUD_STATE_DELETE_DISCONNECT_ALL = 2, WUD_STATE_DELETE_CLEANUP_DATABASE = 3, + WUD_STATE_DELETE_CLEANUP_SETTING = 5, WUD_STATE_DELETE_6 = 6, + WUD_STATE_DELETE_7 = 7, WUD_STATE_DELETE_DONE = 8 }; +enum { WUD_RESULT_DELETE_BUSY = -1, WUD_RESULT_DELETE_WAITING, + WUD_RESULT_DELETE_COMPLETE }; +enum { WUD_STATE_SYNC_START = 0, WUD_STATE_STACK_INITIALIZED = 4, + WUD_STATE_INIT_BLUETOOTH_ENABLED = 5, WUD_LIB_STATUS_3 = 3 }; +enum { SC_STATUS_OK, SC_STATUS_BUSY, SC_STATUS_FATAL }; +#define DEBUGPrint(...) ((void)0) +#define OSMillisecondsToTicks(ms) (ms) + +static WUDCB _wcb; +static SCBtDeviceInfoArray _scArray; +static SCBtCmpDevInfoArray _spArray; +static int interrupts, in_handler, tick_count, alarm_count, wait_print_count; +static int status_delay, link_delay, flush_delay, flush_status; +static int set_standard, set_simple, set_calls, flush_calls; +static int callback_count, last_callback, callback_starts; +static void (*pending_flush)(SCStatus); +static void DeleteAllHandler(void); +BOOL WUDStartClearDevice(void); +BOOL WUDIsBusy(void); + +static BOOL OSDisableInterrupts(void) { + BOOL previous = interrupts; + interrupts = FALSE; + return previous; +} +static void OSRestoreInterrupts(BOOL enabled) { + assert(!interrupts); + interrupts = enabled; + if (!enabled || in_handler || !_wcb.alarm) return; + assert(++tick_count < 100); + in_handler = TRUE; + interrupts = FALSE; + if (pending_flush && flush_delay-- == 0) { + void (*callback)(SCStatus) = pending_flush; + pending_flush = NULL; + callback(flush_status); + } + DeleteAllHandler(); + interrupts = enabled; + in_handler = FALSE; +} +static void OSCreateAlarm(int *alarm) { *alarm = 0; } +static int OSGetTime(void) { return 0; } +static void DeleteAllHandler0(void) { assert(0); } +static void OSSetPeriodicAlarm(int *alarm, int start, int period, void (*handler)(void)) { + assert(!interrupts && start == 0 && period == 20 && handler == DeleteAllHandler0); + *alarm = 1; + ++alarm_count; +} +static void OSCancelAlarm(int *alarm) { *alarm = 0; } +static void WUDSetVisibility(BOOL visible, BOOL connectable) { + assert(!visible && connectable); +} +static BOOL WUDGetConnectable(void) { return TRUE; } +static int WUDiGetDevNumber(void) { return 0; } +static void WUD_DEBUGPrint(const char *format, ...) { + if (!strcmp(format, "dev number = %d\n")) ++wait_print_count; +} +static WUDDeleteState WUDiDisallowIncoming(void) { return WUD_STATE_DELETE_DISCONNECT_ALL; } +static WUDDeleteState WUDiTerminateDevice(void) { return WUD_STATE_DELETE_CLEANUP_DATABASE; } +static WUDDeleteState WUDiDeleteDevice(void) { + return link_delay-- > 0 ? WUD_STATE_DELETE_CLEANUP_DATABASE : WUD_STATE_DELETE_CLEANUP_SETTING; +} +static SCStatus SCCheckStatus(void) { return status_delay-- > 0 ? SC_STATUS_BUSY : SC_STATUS_OK; } +static BOOL SCSetBtDeviceInfoArray(SCBtDeviceInfoArray *data) { + assert(!memcmp(*data, "\0\0\0", sizeof(*data))); + ++set_calls; + return set_standard; +} +static BOOL SCSetBtCmpDevInfoArray(SCBtCmpDevInfoArray *data) { + assert(!memcmp(*data, "\0\0\0", sizeof(*data))); + ++set_calls; + return set_simple; +} +static void SCFlushAsync(void (*callback)(SCStatus)) { + assert(_wcb.deleteState == WUD_STATE_DELETE_6); + pending_flush = callback; + ++flush_calls; +} +''' + +TESTS = r''' +static void clear_callback(s32 result) { + ++callback_count; + last_callback = result; + if (callback_starts && result == WUD_RESULT_DELETE_WAITING) + assert(WUDStartClearDevice()); +} +static void reset(void) { + memset(&_wcb, 0, sizeof(_wcb)); + memset(_scArray, 0xff, sizeof(_scArray)); + memset(_spArray, 0xff, sizeof(_spArray)); + _wcb.libStatus = WUD_LIB_STATUS_3; + _wcb.stackState = WUD_STATE_STACK_INITIALIZED; + _wcb.initState = WUD_STATE_INIT_BLUETOOTH_ENABLED; + interrupts = TRUE; + in_handler = tick_count = alarm_count = wait_print_count = 0; + status_delay = link_delay = flush_delay = 2; + set_standard = set_simple = TRUE; + set_calls = flush_calls = callback_count = callback_starts = 0; + last_callback = 99; + flush_status = SC_STATUS_OK; + pending_flush = NULL; +} +static void completed(void) { + assert(_wcb.deleteState == WUD_STATE_DELETE_START && !_wcb.alarm); + assert(interrupts && !in_handler && wait_print_count == 1); +} +int main(void) { + reset(); _wcb.libStatus = 0; + _WUDDeleteStoredDevice(); completed(); assert(!alarm_count && !flush_calls); + puts("PASS: uninitialized library declines deletion and returns idle"); + + reset(); _wcb.syncState = 1; + _WUDDeleteStoredDevice(); completed(); assert(!alarm_count && !flush_calls); + puts("PASS: unrelated busy state declines deletion and returns idle"); + + reset(); _wcb.clearDevCB = clear_callback; + _WUDDeleteStoredDevice(); completed(); + assert(callback_count == 1 && last_callback == WUD_RESULT_DELETE_WAITING && !alarm_count); + puts("PASS: callback owns request and may leave deletion idle"); + + reset(); _wcb.clearDevCB = clear_callback; interrupts = FALSE; + _WUDDeleteStoredDevice(); + assert(!interrupts && !alarm_count && wait_print_count == 1); + puts("PASS: idle call preserves already-disabled interrupt state"); + + reset(); + _WUDDeleteStoredDevice(); completed(); + assert(tick_count > 6 && alarm_count == 1 && set_calls == 2 && flush_calls == 1); + puts("PASS: delayed link drain, busy settings and asynchronous flush complete"); + + reset(); flush_status = SC_STATUS_FATAL; + _WUDDeleteStoredDevice(); completed(); assert(flush_calls == 1); + puts("PASS: failed asynchronous flush still reaches existing completion path"); + + reset(); set_standard = set_simple = FALSE; + _WUDDeleteStoredDevice(); completed(); assert(set_calls == 2 && !flush_calls); + puts("PASS: both settings writes fail without waiting for a flush callback"); + + reset(); set_standard = FALSE; + _WUDDeleteStoredDevice(); completed(); assert(set_calls == 2 && flush_calls == 1); + reset(); set_simple = FALSE; + _WUDDeleteStoredDevice(); completed(); assert(set_calls == 2 && flush_calls == 1); + puts("PASS: either successful settings write retains existing flush behavior"); + + reset(); _wcb.clearDevCB = clear_callback; callback_starts = TRUE; + _WUDDeleteStoredDevice(); completed(); + assert(callback_count == 2 && last_callback == WUD_RESULT_DELETE_COMPLETE && alarm_count == 1); + puts("PASS: request callback starts deletion and completion callback fires"); + + reset(); _wcb.clearDevCB = clear_callback; + _wcb.deleteState = WUD_STATE_DELETE_CLEANUP_DATABASE; _wcb.alarm = 1; + _WUDDeleteStoredDevice(); completed(); + assert(callback_count == 2 && last_callback == WUD_RESULT_DELETE_COMPLETE && !alarm_count); + puts("PASS: already-active deletion reports busy then waits for its completion"); + + reset(); DeleteFlushCallback(SC_STATUS_FATAL); + assert(_wcb.deleteState == WUD_STATE_DELETE_START); + puts("PASS: late flush callback leaves idle state unchanged"); + return 0; +} +''' + + +def verify_object(path): + from capstone import Cs, CS_ARCH_PPC, CS_MODE_32, CS_MODE_BIG_ENDIAN + from elftools.elf.elffile import ELFFile + + with path.open("rb") as stream: + elf = ELFFile(stream) + symbols = elf.get_section_by_name(".symtab") + symbol = symbols.get_symbol_by_name("_WUDDeleteStoredDevice")[0] + start, size, section = symbol["st_value"], symbol["st_size"], symbol["st_shndx"] + code = elf.get_section(section).data()[start:start + size] + calls = {} + for relocations in elf.iter_sections(): + if relocations["sh_type"] != "SHT_RELA" or relocations["sh_info"] != section: + continue + for relocation in relocations.iter_relocations(): + if relocation["r_info_type"] == 10: + calls[relocation["r_offset"]] = symbols.get_symbol(relocation["r_info_sym"]).name + decoder = Cs(CS_ARCH_PPC, CS_MODE_32 | CS_MODE_BIG_ENDIAN) + instructions = list(decoder.disasm(code, start)) + found = [] + for at in range(len(instructions) - 4): + disable, load, restore, compare, branch = instructions[at:at + 5] + if [i.mnemonic for i in instructions[at:at + 5]] != ["bl", "lbz", "bl", "cmpwi", "bne"]: + continue + if calls.get(disable.address) != "OSDisableInterrupts" or calls.get(restore.address) != "OSRestoreInterrupts": + continue + register, operand = load.op_str.split(", ") + assert register != "r3", "The load must preserve the saved interrupt state in r3" + assert operand.startswith("0xd(") and compare.op_str == register + ", 0" + assert int(branch.op_str, 0) == disable.address, "Each iteration must reload the shared field" + found.append(disable.address) + assert len(found) == 1, "Expected one interrupt-protected deleteState polling loop" + print("PASS: MWCC wait reloads deleteState and restores interrupts on every iteration at " + hex(found[0])) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--source", type=Path, default=Path("libs/RVL_SDK/src/wud/WUD.c")) + parser.add_argument("--object", type=Path, help="Also inspect the compiled 43U WUD object") + args = parser.parse_args() + source = args.source.read_text() + # The source functions' declarations retain their actual storage classes. + declarations = "\n".join(function(source, name).split("{")[0] + ";" for name in FUNCTIONS) + code = PRELUDE + declarations + "\n" + "\n\n".join(function(source, name) for name in FUNCTIONS) + TESTS + with tempfile.TemporaryDirectory(prefix="wud-delete-wait-") as directory: + path = Path(directory) + (path / "test.c").write_text(code) + subprocess.run(["cc", "-std=c99", "-O2", "-Wall", "-Wextra", "-Werror", + "-Wno-unused-parameter", str(path / "test.c"), "-o", str(path / "test")], check=True) + try: + subprocess.run([str(path / "test")], check=True, timeout=5) + except subprocess.TimeoutExpired: + raise SystemExit("FAIL: deletion wait did not finish within five seconds") + if args.object: + verify_object(args.object) + + +if __name__ == "__main__": + main() diff --git a/tools/decomp-assist/wud-deletion-wait-audit.md b/tools/decomp-assist/wud-deletion-wait-audit.md new file mode 100644 index 00000000..8492941d --- /dev/null +++ b/tools/decomp-assist/wud-deletion-wait-audit.md @@ -0,0 +1,129 @@ +# WUD stored-device deletion wait + +Original validation base: `f18082e88b680072eaeafbd8f6fe11c6198b9200`. +Current leaf base after mechanical rebase: `f762dfef` (PRs #1233–#1235). +Leaf: `agent/fix/wud-deletion-wait`. + +## Defect and correction + +The `_WUDDeleteStoredDevice` routine added in PR #1232 is absent from the 43U +retail object and final ELF. Its empty loop reads the nonvolatile +`WUDCB::deleteState` field. MWCC hoists the load to `.text+0x581c`; the compare at +`0x5820` and backward branch at `0x5824` repeatedly test the same register. +If deletion is still pending on the first read, later completion cannot release +the caller. + +The field genuinely crosses execution contexts: + +- `WUDiDeleteAllLinkKeys` asks the registered clear-device callback to act, or + calls `WUDStartClearDevice` when there is no callback +- `WUDStartClearDevice` checks library/busy state, sets deletion state 1 with + interrupts disabled, and installs `DeleteAllHandler0` as a 20 ms periodic alarm +- The alarm runs `DeleteAllHandler` through the WUD handler fiber. It advances + states 1, 2, 3, and 5; state 3 can wait for links, and state 5 can wait for SC +- `WUDiCleanUp` advances to 6 before asynchronous settings flush, or directly to + 8 when neither settings write succeeds. `DeleteFlushCallback` advances to 8 + for either flush success or failure, unless already idle +- The next alarm's `WUDiDeleteAllComplete` cancels the alarm and restores state 0 + +The correction snapshots `deleteState` between `OSDisableInterrupts` and +`OSRestoreInterrupts` on every iteration, using the established WUD access +pattern. The saved interrupt state is restored before testing the snapshot. +No declaration-wide qualifier, use-site cast, fake state, dummy operation, +assembly, compiler flag, or matching classification is introduced. + +The resulting MWCC loop is: + +```text +0x581c bl OSDisableInterrupts +0x5820 lbz r28,0xd(r30) +0x5824 bl OSRestoreInterrupts +0x5828 cmpwi r28,0 +0x582c bne 0x581c +``` + +The load preserves the saved interrupt state in r3 for `OSRestoreInterrupts`. +An interrupt can advance the state after restoration; the next iteration then +reloads it. This remains a blocking helper: pending work still requires the +normal interrupt/callback machinery to run. No timeout, cancellation, or error +reporting contract is invented. + +## Original snapshot validation + +The unmodified `f18082e8` base was fully rebuilt in this leaf for 43U before editing. +Its report, WUD object, all source-object hashes, ELF and DOL were preserved. +The candidate passed a full build, report generation and `build/43U/ok` using +the recovered tools and `../wii-toolchain-venv`. The measurements below compare +that original candidate against its `f18082e8` snapshot, not against later main +revisions. + +- Every report entry and global measure is byte-for-byte unchanged. WUD remains + 66/66 retail functions, 18,852/18,852 code bytes, and 12,016/12,016 data bytes; + matching and linkage are all 100% +- All 66 retail functions have identical raw instruction bytes and resolved + relocation identities against the original object: 4,713 instructions, + zero differences +- All 92 other compiled source functions retain their baseline raw instruction + bytes and resolved relocation identities. Only the non-retail wait changes, + from 126 to 128 instructions +- Only WUD.o changes among 1,027 source objects. Every allocated WUD data section + and its relocations are identical to the baseline +- The existing source-versus-retail data extent differences are unchanged: + `.data`, `.sbss` and `.sdata2` omit trailing alignment zeroes; `.sdata` retains + the non-retail `start\n` string. Shared extents and data relocation referents + match the retail object, and final linked bytes are unchanged +- Pool audit: 107/107 strings, no divergence. Direct-call audit: 511/511 calls + preserve destinations, no skips or errors. Literal-reference audit: 66 + functions, no candidates, skips or errors. DeleteAllHandler's jump table: + 9/9 entries match +- Every ELF section header and all section bytes except `.strtab` are identical + to the baseline. The only `.strtab` changes are non-allocated compiler-generated + `@NNNN` label names. Raw ctxdiff reports such label-name differences, including + ones already present before this fix; the raw-byte/resolved-relocation audit + verifies their actual instruction and referent equality +- DOL bytes are identical to the baseline; SHA1 remains + `26116613f624061ba99c8d1a299aaa6efa85670d` +- `git diff --check` passes + +`verify_wud_delete_wait.py` extracts the actual wait, request, busy-state, +cleanup, flush-callback and completion functions into a bounded host harness. +It models alarm delivery only after interrupt restoration, with delayed link +drain and settings completion. It covers normal asynchronous completion, SC +busy retries, failed flush, both settings writes failing, either write failing, +library rejection, unrelated busy state, callback-owned requests, an already +active deletion, idle calls with interrupts disabled, and late flush callbacks. +The candidate passes; the baseline times out on the pending-deletion case. +This is not Wii hardware or complete Bluetooth emulation. + +Re-run the host and MWCC loop checks from this leaf: + +```sh +../wii-toolchain-venv/bin/python tools/decomp-assist/verify_wud_delete_wait.py \ + --object build/43U/src/libs/RVL_SDK/src/wud/WUD.o +``` + +Original-snapshot evidence is under `build/wud-deletion-review/`: baseline/candidate objects, +reports and build logs, object-hash lists, disassemblies, pool/call/literal/jump +audits, positive and negative host-test results, `validate_objects.py`, and +`validation.json`. The baseline files and validator still refer to `f18082e8`; +they must not be mistaken for a fresh `f762dfef` comparison. + +## Rebase and independent verification + +The parent independently forced the original candidate's build and reran the +host/compiled-loop checks and object audit successfully. PRs #1233–#1235 then +advanced main to `f762dfef`. These commits do not change `WUD.c` or +`WUDInternal.h`; the parent mechanically rebased this leaf without changing the +correction or regression test. + +The parent is rebuilding the rebased candidate and will compare it with a fresh +`f762dfef` main report and linked bytes before publication. That verification +was still in progress when this documentation-only update was committed; the +original global report equality is not a claim that unrelated incoming work +left global progress unchanged. No competing worker build was started. + +The worker performed no push, PR, merge, upload, or upstream action. + +ORIGINAL-SNAPSHOT GATE PASS: pending-state reload corrected; retail code, data, +linkage, progress report and DOL preserved. Rebased acceptance belongs to the +parent's fresh verification.