mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 01:49:58 -04:00
kern_signal_system_event read the table entry and the state byte without the kernel lock, then kern_event_signal took the lock and ended the process when the kept writable handle was no longer an event. A host thread (the vsync thread signals every bound vsync event 60 times a second) racing the guest's DestroySystemEvent, which zeroes the byte and then closes both handles under the kernel lock, hit that fatal; a probe built on kernel.c reached it after 437 destroy and bind rounds. A stale entry whose byte reads nonzero again ended the process at once, and since h_new hands a closed number to the next new object, a stale entry could also signal or clear an unrelated event. Each Event now gets a serial number, and kern_bind_system_event records the serial of the Event its writable handle names. The new system_event_set reads the entry again under the kernel lock and sets or clears the event only while the handle still names that same Event; a stale entry is skipped and logged once. kern_clear_system_event goes through it as well. The lock order is G before g_se_lock; the binder never holds both. The full-table path of kern_bind_system_event took any entry whose object mem_readable did not know, which includes the live events host services keep in their own memory (ov_movie2.c Video.in_ev/out_ev, net_offline.c HostTask.event). The service's next signal from its host thread then found no entry and tried to bind there, which is fatal. Entries now record whether the object lies in guest memory, and only guest entries that were destroyed or unmapped give their slot. tests/system_event_check.py builds kernel.c with stubs and checks a destroyed event with its byte nonzero again, a closed number taken by an unrelated event, two seconds of a host thread signalling and clearing against a guest thread destroying and binding again, a full table with the movie's two host-memory events, and the "table full" fatal with every entry live. Against the previous kernel.c it ends with "kern_event_signal: 0x1001 is not a writable event handle". sync_wake_check and thread_end_check pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.7 KiB
Python
63 lines
2.7 KiB
Python
"""Build and run tests/system_event_check.c: the system-event table of runtime/kernel.c (kern_bind_system_event,
|
|
kern_signal_system_event, kern_clear_system_event) against destroyed events, reused handle numbers, a host thread racing
|
|
a destroy and bind, and a full table holding events in host memory; with the platform layer linked and the rest of the
|
|
runtime stubbed. No game files: the module layout the runtime includes is a stand-in written to the test's build folder.
|
|
|
|
Usage: system_event_check.py
|
|
"""
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
|
|
|
|
|
def compiler():
|
|
if os.environ.get('CC'):
|
|
return os.environ['CC']
|
|
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
|
return str(MSYS_BIN / 'clang.exe')
|
|
return shutil.which('clang') or 'clang'
|
|
|
|
LAYOUT = '''// A stand-in for the generated layout.h (tests/system_event_check.py).
|
|
#pragma once
|
|
#define MOD_MAIN_BASE 0x7000000000ull
|
|
#define MOD_MAIN_SIZE 0x4ea0000ull
|
|
'''
|
|
|
|
|
|
def main():
|
|
work = ROOT / 'build' / 'test' / 'system_event'
|
|
work.mkdir(parents=True, exist_ok=True)
|
|
(work / 'layout.h').write_text(LAYOUT)
|
|
win = os.name == 'nt'
|
|
exe = work / ('system_event_check.exe' if win else 'system_event_check')
|
|
env = dict(os.environ)
|
|
if win and MSYS_BIN.exists():
|
|
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
|
rt = ROOT / 'runtime'
|
|
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
|
|
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
|
|
else ['-lpthread']
|
|
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
|
|
'-march=x86-64-v3', '-I', str(rt), '-I', str(work),
|
|
str(ROOT / 'tests' / 'system_event_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
|
|
env=env)
|
|
rc = subprocess.run([str(exe)], env=env, timeout=300).returncode
|
|
# Every entry live: the next bind must end the process with "table full" (rt_fatal's stub exits 3).
|
|
p = subprocess.run([str(exe), 'full'], env=env, timeout=300, capture_output=True, text=True)
|
|
sys.stdout.write(p.stdout)
|
|
if p.returncode != 3 or 'table full' not in p.stdout:
|
|
print(f'FAIL: a bind with every entry live must end with "table full" (exit {p.returncode})')
|
|
rc = rc or 1
|
|
else:
|
|
print('system_event_check full: ok (table full with every entry live)')
|
|
return rc
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|