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>
This commit is contained in:
chrg6
2026-11-13 19:21:56 -05:00
parent 93e9434869
commit 7e7fed639e
14 changed files with 1758 additions and 174 deletions

View File

@@ -162,6 +162,30 @@ typedef struct {
} ServerVoice;
enum { SV_STOPPED, SV_STARTED, SV_PAUSED, SV_STOPPING };
// A RequestUpdate this host refuses (audren.c update_check, audio_mixer.c audio_mixer_routing_check and
// audio_mixer_mix_layout_check): audio module 153, description 41, the update's sections do not match the renderer.
// The SDK's RequestUpdateAudioRenderer hands it back to its caller (as it does descriptions 42 and 43) and aborts on
// any other result; the game ignores it and sends its next update. Nothing of a refused update is applied.
#define AUDREN_R_INVALID_UPDATE 0x5299u
// Kinds of game value this host cannot use (audren.c audio_bad), one per site; each is logged once per renderer.
enum {
// the RequestUpdate is refused
AB_UPD_BUFFERS, AB_UPD_SMALL, AB_UPD_TOTAL, AB_UPD_BEHAVIOR, AB_UPD_POOLS, AB_UPD_CHANNELS, AB_UPD_VOICES,
AB_UPD_EFFECTS, AB_UPD_SINKS, AB_UPD_OVERRUN, AB_UPD_UNCONSUMED, AB_UPD_OUTPUT,
AB_SPL_HEADER, AB_SPL_MAGIC, AB_SPL_INFO_OVERRUN, AB_SPL_INFO, AB_SPL_PADDING, AB_DEST_OVERRUN, AB_DEST_MAGIC,
AB_SPL_SIZE, AB_MIX_DIRTY, AB_MIX_SIZE, AB_MIX_LAYOUT,
// one record of the update is left out, the rest is applied
AB_CHANNEL_ID, AB_VOICE_ID, AB_WAVE_INDEX, AB_VOICE_FILTER, AB_SPL_ID, AB_SPL_DEST_REF, AB_DEST_ID,
AB_DEST_FILTER, AB_MIX_ID,
// one effect or sink is passed through or skipped
AB_LIMITER_SIZE, AB_LIMITER_CHANNELS, AB_LIMITER_PARAMS, AB_LIMITER_INDEX, AB_DELAY_PARAMS, AB_REVERB_PARAMS,
AB_COMPRESSOR_PARAMS, AB_DSP_CHANNELS, AB_DSP_INDEX, AB_R3D_PARAMS, AB_R3D_CHANNELS, AB_R3D_INDEX,
AB_AUX_SIZE, AB_AUX_ADDRESS, AB_AUX_INDEX, AB_CAPTURE_SIZE, AB_CAPTURE_ADDRESS, AB_CAPTURE_INDEX,
AB_BUFMIX_COUNT, AB_BUFMIX_INDEX, AB_SINK_PARAMS, AB_SINK_ADDRESS, AB_SINK_INDEX,
AB_KINDS
};
typedef struct AudioRenderer {
AudrenParameter param;
PlatMutex lock;
@@ -179,22 +203,48 @@ typedef struct AudioRenderer {
AudrenEffectIn *effects;
AudrenChannelIn *channels;
void *mixer;
uint8_t strict; // NX2NAVI_AUDIO_STRICT (OpenAudioRenderer): audio_bad ends the process
uint8_t bad_seen[AB_KINDS]; // audio_bad: the kinds logged so far (atomic exchange; any thread)
uint64_t bad_count; // audio_bad: values left out and updates refused (atomic)
uint64_t refused, refused_run; // updates refused, and refused in a row (the thread that sends RequestUpdate)
} AudioRenderer;
// A value the game sent that this host cannot use: the caller refuses the update or leaves the one record, effect or
// sink out. Logs the first of each kind per renderer, with the game frame, and counts every one; with
// NX2NAVI_AUDIO_STRICT=1 it ends the process instead, as these values did before 0.10.2. Any thread; takes no lock but
// the log's.
void audio_bad(AudioRenderer *r, int kind, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
// audio_bad for a refused RequestUpdate, as an expression that gives AUDREN_R_INVALID_UPDATE.
#define AUDIO_REFUSE(r, kind, fmt, ...) \
(audio_bad((r), (kind), \
"RequestUpdate refused with %#x: " fmt "; the renderer goes on with the last update it took", \
AUDREN_R_INVALID_UPDATE, ##__VA_ARGS__), \
AUDREN_R_INVALID_UPDATE)
// IAudioRendererManager, served under "audren:u" (ipc.c's service table).
void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs);
long audren_renderers_live(void); // renderers whose render tick runs
// Mixer interface (audio_mixer.c). Every call runs with r->lock held except open, close and set_running, and the
// render thread's audio_mixer_pace_ns, audio_mixer_owed and audio_mixer_out_stats (audren.c render_thread and
// stats_report call them without it). Those three touch only atomics and state that only the render thread changes:
// device_check and channels_switch, which close and open the output stream, run on that thread too, and
// audio_mixer_close runs after renderer_close has joined it.
// Mixer interface (audio_mixer.c). Every call runs with r->lock held except open, close and set_running, the render
// thread's audio_mixer_pace_ns, audio_mixer_owed and audio_mixer_out_stats (audren.c render_thread and stats_report
// call them without it), and audio_mixer_routing_check. The render thread's three touch only atomics and state that
// only the render thread changes: device_check and channels_switch, which close and open the output stream, run on
// that thread too, and audio_mixer_close runs after renderer_close has joined it. audio_mixer_routing_check runs on the
// game's update thread before it takes the lock and reads only the renderer's fixed counts and the game's input.
void audio_mixer_open(AudioRenderer *r);
void audio_mixer_set_running(AudioRenderer *r, int running);
void audio_mixer_close(AudioRenderer *r);
// Before an update is applied, without the renderer lock: the framing of the splitter and mix sections in the layout
// of `revision`. 0, or AUDREN_R_INVALID_UPDATE (logged through audio_bad).
uint32_t audio_mixer_routing_check(AudioRenderer *r, uint32_t revision, const uint8_t *splitters,
uint32_t splitter_bytes, const uint8_t *mixes, uint32_t mix_bytes, int mix_count);
// Renderer lock held, before the update is applied: the mixes in use, as the update leaves them, fit the renderer's
// mix buffers (audio_mixer_render lays them out one after another). 0, or AUDREN_R_INVALID_UPDATE.
uint32_t audio_mixer_mix_layout_check(AudioRenderer *r, const uint8_t *mixes, uint32_t mix_bytes, int mix_count);
// After both checks passed. A record whose ids the renderer does not have is left out (audio_bad).
void audio_mixer_update_routing(AudioRenderer *r, const uint8_t *splitters, uint32_t splitter_bytes,
const uint8_t *mixes, uint32_t mix_bytes, int mix_count);
// An enabled effect whose parameters the DSP cannot use is passed through by audio_mixer_render (audio_bad).
void audio_mixer_update_effects(AudioRenderer *r);
void audio_mixer_effect_result(AudioRenderer *r, int id, void *output, unsigned size);
uint32_t audio_mixer_sink_written(AudioRenderer *r, int id);