diff --git a/runtime/ipc.c b/runtime/ipc.c index 079d41e3..f0ea3f6b 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -422,11 +422,12 @@ static void msg_parse(const uint32_t *m, int tipc, Reply *r) { } // One request on the TLR message buffer; the kernel result of SendSyncRequest, the service result in r->result. +// A kernel failure has no reply: clear it before the probe reads fields in a failure diagnostic. static uint32_t call(Cpu *c, uint32_t h, const Msg *q, Reply *r) { uint32_t *m = (uint32_t *)(uintptr_t)c->tpidrro; msg_build(m, q); uint32_t k = sv(c, 0x21, h, 0, 0, 0); - if (k) return k; + if (k) { memset(r, 0, sizeof *r); return k; } msg_parse(m, q->type >= T_TIPC_BASE, r); return 0; } diff --git a/tests/ipc_probe_check.py b/tests/ipc_probe_check.py new file mode 100644 index 00000000..e8bec8b5 --- /dev/null +++ b/tests/ipc_probe_check.py @@ -0,0 +1,102 @@ +"""The IPC probe must not use stale or indeterminate replies after a kernel error. + + python tests/ipc_probe_check.py --cc path/to/clang +""" +import argparse +from pathlib import Path +import shutil +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +HARNESS = r''' +#include +#include +#include +#include "ipc.h" +// The probe's expected failure diagnostics are kept off the test's stdout. +static int probe_printf(const char *format, ...) { (void)format; return 0; } +static int probe_putchar(int c) { return c; } +#define printf probe_printf +#define putchar probe_putchar +PRODUCTION +#undef printf +#undef putchar + +static uint32_t kernel_result, service_result; +void rt_svc(Cpu *c, uint32_t imm, uint64_t pc) { + (void)pc; + assert(imm == 0x21); + c->x[0] = kernel_result; + if (kernel_result) return; + uint32_t *m = (uint32_t *)(uintptr_t)c->tpidrro; + int tipc = (m[0] & 0xffff) >= T_TIPC_BASE; + memset(m, 0, IPC_MSG_BYTES); + m[1] = 1u << 31; + m[2] = service_result ? 0 : 1u << 5; + uint32_t *p = m + 3; + if (!service_result) *p++ = 0x1234; + if (tipc) *p = service_result; + else { + p = m + (((p - m) + 3) & ~3); + p[0] = CMIF_OUT_MAGIC; + p[2] = service_result; + } +} + +int main(void) { + Cpu c = {0}; + uint32_t tlr[IPC_MSG_BYTES / 4] = {0}; + c.tpidrro = (uintptr_t)tlr; + for (int tipc = 0; tipc <= 1; tipc++) { + failures = 0; + kernel_result = service_result = 0; + assert(get_service(&c, 1, "test:u", tipc) == 0x1234 && failures == 0); + kernel_result = IPC_R_BAD_HANDLE; + uint32_t h = get_service(&c, 1, "test:u", tipc); + if (h) fprintf(stderr, "%s kernel error returned stale service handle %#x\n", tipc ? "TIPC" : "CMIF", h); + assert(h == 0 && failures == 1); + } + Reply r; + kernel_result = service_result = 0; + assert(cmd(&c, 1, 0, NULL, 0, &r) == 0 && r.nmove == 1 && r.move[0] == 0x1234); + memset(&r, 0xa5, sizeof r); + kernel_result = IPC_R_BAD_HANDLE; + assert(cmd(&c, 1, 0, NULL, 0, &r) == IPC_R_BAD_HANDLE); + assert(r.result == 0 && r.ncopy == 0 && r.nmove == 0 && r.data == NULL); + for (unsigned i = 0; i < 8; i++) assert(r.copy[i] == 0 && r.move[i] == 0); + kernel_result = 0; + service_result = IPC_R_UNKNOWN_COMMAND; + assert(cmd(&c, 1, 0, NULL, 0, &r) == IPC_R_UNKNOWN_COMMAND && r.result == service_result); + assert(r.nmove == 0 && r.data != NULL); + puts("IPC probe: CMIF/TIPC success and kernel-error replies, reply reuse and service errors passed"); + return 0; +} +''' + + +def production(): + text = (ROOT / 'runtime/ipc.c').read_text(encoding='utf-8') + magic = '\n'.join(line for line in text.splitlines() if line.startswith(('#define CMIF_IN_MAGIC', '#define CMIF_OUT_MAGIC'))) + start = text.index('enum { T_CLOSE') + types = text[start:text.index('};', start) + 2] + helpers = text[text.index('#define PROBE_FRAMES'):text.index('// A TotK-like renderer:')] + return magic + '\n' + types + '\n' + helpers + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument('--cc', default=shutil.which('clang') or r'C:\msys64\ucrt64\bin\clang.exe') + args = parser.parse_args() + with tempfile.TemporaryDirectory(prefix='ipc-probe-') as temp: + work = Path(temp) + source = work / 'probe.c' + source.write_text(HARNESS.replace('PRODUCTION', production()), encoding='utf-8') + exe = work / 'probe.exe' + subprocess.run([args.cc, '-O2', '-std=gnu11', '-ftrivial-auto-var-init=pattern', + '-I', str(ROOT / 'runtime'), str(source), '-o', str(exe)], check=True) + subprocess.run([str(exe)], check=True) + + +if __name__ == '__main__': + main() diff --git a/tests/run_all.py b/tests/run_all.py index fc74a277..4242aa29 100644 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -69,6 +69,7 @@ TESTS = { 'cheats_vm_check': ('.venv', 'cc', ['--cc', ''], 'compiles runtime/cheats.c into a harness with clang'), 'romfs_shapes_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production overlay selection helpers'), 'frame_world_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production world-clock player walk'), + 'ipc_probe_check': ('.venv', 'cc', ['--cc', ''], 'compiles the IPC probe helpers with forced kernel failures'), 'sync_ahead_check': ('.venv', 'cc', ['--cc', ''], 'compiles the production sync-ahead wait selection'), 'patches_v110_check': ('.venv', 'cc', [], 'compiles synthetic patch contracts for comparison with Unicorn'), 'patches_v110_highfps_check': ('.venv', 'cc', [], 'compiles synthetic high-fps contracts for comparison with Unicorn'),