Files
NX2navi/tests/audio_params_check.py
chrg6 7e7fed639e Refuse or leave out the audio values the game sends that the renderer cannot use, instead of ending the game on them.
The audio renderer checked the game's RequestUpdate with about 80 rt_fatal and need() sites in audren.c,
audio_mixer.c, audio_effects.c and audio_reverb.c, so a section size, a splitter or mix record or an effect setting
out of range (a reverb3d whose delays add up past 0.4 s, a limiter attack outside 0 to 1, a mix buffer index past its
mix, a filter coefficient that is not finite) ended the process. The tested route never reached one; dungeons and
bosses were never run. Now an update whose sections do not match the renderer is refused whole with 0x5299 before
anything of it is applied: the SDK's RequestUpdateAudioRenderer hands that result back to the game, which ignores it
and sends its next update, where any other result makes the SDK abort. A record with an id the renderer does not have
is left out of an update that is otherwise applied, a splitter destination id it does not have is stored as -1, which
both render loops skip, and an effect or sink whose values cannot be used is passed through or skipped in the render
pass, with every index checked before its first write. audio_bad logs the first of each kind per renderer with the
game frame and counts the rest. rt_fatal stays for host allocations, OpenAudioRenderer's parameters and the checks
the new ones turn into invariants. Two inputs that did not end the process before change too: a mix in use with a
negative buffer count is refused (it laid the mixes after it out before buffer 0), and a limiter attack or release
that is not a number passes the sound through instead of turning the mix into NaN. NX2NAVI_AUDIO_STRICT=1
(audio_strict, default 0) makes audio_bad end the process as before, for bug reports.

Not reproduced before the change in the game; crafted updates reproduced the fatals. The new
tests/audio_params_check sends 27 refused updates, 10 records and 38 effects and sinks the renderer cannot use
through renderer_handler and runs the strict switch; 15 deliberate breakages of the change each made it fail.
audio_mixer_check, cfg_rows_check, check_content, a compile of every runtime file with no new -Wall -Wextra warnings
in the changed ones and the stability pass's test matrix (the two baseline environment failures only) passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:13:02 -05:00

60 lines
3.1 KiB
Python

"""Build and run tests/audio_params_check.c: runtime/audren.c and runtime/audio_mixer.c with the rest of the runtime
stubbed and the real effect DSPs (runtime/audio_effects.c, runtime/audio_reverb.c) linked. It sends RequestUpdates the
renderer cannot use: refused ones must answer 0x5299 and change nothing, records, effects and sinks are left out of
updates that are otherwise applied, each kind is logged once, and with NX2NAVI_AUDIO_STRICT=1 such a value ends the
process (two runs of their own, which must exit 2 with "FATAL: audio:"). No game files and no audio device.
Windows: python tests\\audio_params_check.py [--toolchain C:/msys64/ucrt64] [-v]
Linux: python tests/audio_params_check.py (clang and SDL3 development files via pkg-config)
"""
import argparse
import os
import shlex
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS = Path(r'C:\msys64\ucrt64')
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument('--toolchain', type=Path, default=MSYS if os.name == 'nt' and MSYS.exists() else None)
ap.add_argument('-v', action='store_true', help='print the log lines of the checks')
a = ap.parse_args()
work = ROOT / 'build' / 'test' / 'audio_params'
work.mkdir(parents=True, exist_ok=True)
env = dict(os.environ)
if a.toolchain:
env['PATH'] = str(a.toolchain / 'bin') + os.pathsep + env.get('PATH', '')
cc = os.environ.get('CC') or str(a.toolchain / 'bin' / 'clang.exe')
sdl = ['-L', str(a.toolchain / 'lib'), '-lSDL3']
else:
cc = os.environ.get('CC') or shutil.which('clang') or 'cc'
sdl = shlex.split(subprocess.check_output(['pkg-config', '--cflags', '--libs', 'sdl3'], text=True))
exe = work / ('audio_params_check.exe' if os.name == 'nt' else 'audio_params_check')
# three translation units: both effect files have a static gain10; the default build's target (x86-64-v3)
subprocess.check_call([cc, '-std=gnu11', '-O2', '-fno-strict-aliasing', '-march=x86-64-v3', '-Wall',
'-Wno-unused-function', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'audio_params_check.c'), str(ROOT / 'runtime' / 'audio_effects.c'),
str(ROOT / 'runtime' / 'audio_reverb.c'), '-o', str(exe), *sdl, '-lm'], env=env)
fails = 0
p = subprocess.run([str(exe)] + (['-v'] if a.v else []), env=env, timeout=120, capture_output=True, text=True)
sys.stdout.write(p.stdout + p.stderr)
if p.returncode != 0:
fails += 1
for case in ('strict-effect', 'strict-update'):
p = subprocess.run([str(exe), case], env=env, timeout=60, capture_output=True, text=True)
text = p.stdout + p.stderr
ok = p.returncode == 2 and 'FATAL: audio:' in text and 'NX2NAVI_AUDIO_STRICT=1' in text
print(f'audio_params_check {case}: {"ok" if ok else "FAIL"} (exit {p.returncode}: {text.strip()[-200:]})')
fails += not ok
print('audio_params_check.py:', 'FAIL' if fails else 'ok')
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())