Skip to content

CI: ASan, UBSan and TSan over the host and CPU texture tests, as a ratchet - #412

Closed
v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:ci-sanitizers
Closed

v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:ci-sanitizers

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge order: this now builds on #443, a one-line test fix. On today's main the asan target found a real read past an array in t_hid.c (from ceaf6d6): the rotation passed to dm_motion_set_switch was one float, and three are read. Following #313's rule, that fix is its own PR; this one sits on it.

Update: rebased on main 84ca44b. #296's Pro Controller motion tests build through $(CLANG) too. With #443 underneath, run, asan and tsan-threads pass.

Update: the first CI runs failed on known races reported under other names. GitHub's runner (Xcode 26.6) inlined au_release_pending into au_stop_at and thr_runnable into dm_yield_to_core_peers, so the suppressions for those functions didn't match. Locally, Xcode 27.1 reported them under their own names. The TSan builds now use -fno-inline, so every function keeps its own frame on any toolchain. TSan judges the order of accesses, not their timing, so this doesn't hide anything. CI is green. #421 moves CI to Xcode 27.1, the project's toolchain.

Part of #313 ("Code quality checks in CI").

Problem

Data races and undefined behaviour in the host show up as rare hangs or wrong results in some game, and are found by hand (like the CPU completion race you found in #158/#213). None of the host tests ran under a sanitizer.

Change

  • dm/sanitizers/:
    • sanitizers.mk: the flags and options, and a stamp that rebuilds when they or the lists change;
    • ignorelist.txt (ASan, UBSan) and tsan.supp (TSan): a ratchet. Every entry names its finding (file:line) and why it's listed. The job fails only on a new finding, and an entry goes with its fix.
  • dm/host/tests/Makefile:
    • clang → $(CLANG);
    • make asan: every test in run under ASan and UBSan, built into build/asan;
    • make tsan-threads: the 12 tests that start threads, under TSan, built into build/tsan.
      The plain build is unchanged.
  • dm/gfx/tex/tests/Makefile: make asan for t_tex.
  • CI: a sanitizers job of its own in tests.yml (macos-26, 20 min timeout, logs uploaded on failure). It runs the three targets in about a minute and a half after checkout.
  • Test fixtures:
    • t_altstack.c counts ASan's live bytes (ASan's zone statistics don't go down on free);
    • test_memory_profile.py takes the build folder from DM_TEST_BUILD (registered in docs/env_switches.tsv).
  • Docs: docs/development.md (Continuous integration: what runs, how to run it locally, how an entry comes and goes), 06-dev-loop.md §6.

Not in this PR: dm/gfx/tests, whose Makefile #307 restructures. Its CPU-only tests get the same targets in a follow-up on top of #307, taking their list from #307's run list.

The lists

Kind Where Why listed
ASan hle_ent_*, hle_force_* HLE tables in __DATA,swhle / swforce not a finding: walked as arrays, redzones would break the walk
UBSan pointer-overflow h_ro_LoadModule / Unload t_nro.c:48 → hle_ro.c:75, :89 test fixture: init/fini offsets below the module base
UBSan alignment cmd_ok / cmd_size / cmd_id hle_audren.c:200-203 host bug: #409
TSan null_sink, au_release_pending hle_audio.c aout.pos not a finding: ordered by AudioOutputUnitStop, which TSan can't see (CoreAudio isn't instrumented)
TSan au_create hle_audio.c:210 vs :284 host bug: #410 (about one t_audio run in two)
TSan t_condvar.c t_condvar.c:53 test fixture: _exit without joining
TSan thr_runnable hle_thread.c:293 vs :130 host bug: #411

No host code is changed here. The three bugs have their own issues, each with a repro and a possible fix.

Checked (Apple silicon, -j8)

Command Result Time
make -C dm/host/tests GAME=010015100B514000 run OK 28 s
… asan (clean build) rc 0, no reports 32.5 s
… tsan-threads (clean build) rc 0 25.3 s
make -C dm/gfx/tex/tests asan all passed: 5334/5334 1 s
tool tests (348), test_docs, lint ratchet OK, 0 new

Removal check: with each entry removed in turn, its target fails with the expected report. For example, without thr_runnable: data race hle_thread.c:293 in thread_main; without the HLE globals: global-buffer-overflow in t_fs_commit_list.

The forced-phone case of t_motion_hid (ceaf6d6) passed (float[]){0} as the
rotation, one float, while motion_set reads three (motion_map_apply in
motion_fusion.h), so the test read two floats past the array on the stack.
It passed by chance; AddressSanitizer reports a stack-buffer-overflow at
motion_fusion.h:45 (found by the asan target of seedds#412). The rotation is now
(float[]){0, 0, 0}, which is what the case means: no rotation.

Checked: make -C dm/host/tests run passes (hid players: 119 checks), and
with seedds#412's asan target the report is gone.
…tchet

Roadmap item seedds#313. The host tests ran under sanitizers only in a few
hand-written targets (tsan, memory-tsan, the 16k memory test). Now:

- make -C dm/host/tests GAME=<id> asan runs every test of run under
  AddressSanitizer and UndefinedBehaviorSanitizer, built into build/asan;
- make -C dm/host/tests GAME=<id> tsan-threads runs the twelve tests
  that start threads under ThreadSanitizer, built into build/tsan;
- make -C dm/gfx/tex/tests asan runs t_tex under ASan and UBSan.

The rules are the plain ones, compiled with SAN (the extra flags) into a
folder of their own, so the plain build is unchanged. The flags, options
and lists are in dm/sanitizers/: ignorelist.txt (ASan, UBSan) and
tsan.supp (TSan). Each entry names the finding and why it is not fixed
here, so CI fails only on a new one; a fix removes its entry. Today's:

- the HLE tables are walked as arrays of their sections (not a finding);
- t_nro gives init and fini offsets below the module base (test fixture);
- hle_audren.c reads a command header misaligned while it tries a wrong
  layout on an SDK 4/6 list (host, open);
- aout.pos handed across AudioOutputUnitStop, which TSan cannot see
  (CoreAudio is not instrumented);
- au_create writes a->au after the output is in g_aos while the watchdog
  reads it (host, open);
- thread_main sets host_work without G after the thread is in g_all,
  read by thr_runnable (host, open);
- t_condvar exits without joining its threads (test).

The tests keep stderr in sanitizer builds (the reports go there),
t_altstack counts ASan's live bytes (its zone statistics do not go down on
free), and test_memory_profile.py takes the build folder from
DM_TEST_BUILD. CI runs the three targets in a job of its own, so their
time shows. docs/development.md says how to run them and how to add or
remove an entry.

Checked on an M-series Mac: plain run, tsan and memory-tsan pass; asan
(32 s), tsan-threads (25 s) and the texture asan (1 s) pass with the
lists, and fail with an entry removed (thr_runnable, the HLE table
globals, the h_ro functions, the audren alignment entries). Tool tests
(348, 17 skipped), test_docs and the lint ratchet (0 new) pass.
seedds added a commit that referenced this pull request Oct 27, 2026
Integrate the sanitizer targets, HID fixture correction and pinned Xcode
27.1 CI toolchain from #412, #443 and #421. Select the matching runner for
the new sanitizer job as well as the four existing jobs.

Decode audio command headers without alignment assumptions and validate
their extents before consuming them. Finish AudioUnit construction before
publishing an output to lifecycle and watchdog readers. Publish pthread
handles under the scheduler lock and serialize startup, priority and QoS
fields with their readers. Remove all three real-bug suppressions.

Add unaligned/truncated header coverage, an output-publication assertion
and priority/QoS registration checks, including QoS-enabled TSan CI cases.
Document the mechanisms and retain only fixture/framework exceptions.

Plain host, ASan/UBSan, TSan, 40 QoS startup stress runs, the complete Metal
graphics suite, 525 Python tests (20 skipped), lint, all 178 iOS source
compiles and the unsigned synthetic iOS app build passed. Gameplay
regression results are reported with the PR follow-up.

Fixes #409
Fixes #410
Fixes #411

Co-authored-by: v0lumehi <[email protected]>
@seedds

seedds commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Applied directly to main in ce99604, with v0lumehi credited as co-author. This includes the sanitizer infrastructure from this PR, #443's HID fixture fix, and #421's supported-toolchain selection. The new sanitizer job also uses the pinned Xcode 27.1 runner.

The three initial host findings are fixed at their causes, and their suppressions are absent:

All three original reports reproduced without their suppressions before the fixes. The remaining exceptions cover the documented fixture/framework cases, not these host bugs. No game-specific rule, runtime retry, timeout or feature cut was added.

Validation passed: plain host tests; host ASan/UBSan; all host TSan targets; 40 additional QoS/startup runs across both preemption modes; 5,334 texture checks under ASan/UBSan; the complete local Metal graphics suite; 525 Python tests (20 skipped); the lint ratchet (0 new findings); all 178 iOS source compiles; and the unsigned synthetic iOS app build. No device signing or installation was performed.

The game report names candidate 96a49ed; I verified that its production host, graphics, game-profile generator and rewriter sources are byte-identical to the final pushed commit. The later rebase retained upstream test/harness repairs.

The final rebase includes main's memory-test repair (f1bb192); its equivalent observation-gate fixture adapters were retained, and the repaired tests pass.

Main CI: all five test/build jobs passed, and lint passed, on the exact pushed commit.

The five-game selection covers the SDK 6 parser (Fire Emblem: Three Houses), Wonder's renderer path, Xenoblade 2's threaded engine, and Kirby/Odyssey's scheduler and service paths. Fire Emblem has no committed smoke route, so its harness result covers the shader/interface checks only; SDK 6 parsing is exercised by the host tests. Other games are unchecked. Full route comparisons used matching temporary launch wrappers that unset DM_WINDOW, with identical copies of existing pipeline archives for both revisions; every prebuild reran archive coverage validation. Earlier windowed checks had display-link stalls and missing screenshots on the unchanged baseline as well; those measurements are not presented as a clean regression result. No production window/presentation behavior was changed. Real CoreAudio was enabled with output muted for both baseline and candidate game runs.

The full five-game comparison passed. I also inspected a candidate screenshot from each of the four routes.

Complete regression report

regress.py check (full): PASS

Baseline df694e3 df694e3, candidate 96a49ed 96a49edb8609; Apple M6, macOS 27.0.1, Xcode 27.1; 3329.7 s

game status shaders (base → cand) newly failing newly building changed MSL interface (new / known) boot fps p50 frame p99 ms peak MB prepare s new RED
Fire Emblem Three Houses 010055D009F78000 PASS 1372/1372 → 1372/1372 0 0 0 0 / 0 – – – – – –
Super Mario Bros Wonder 010015100B514000 PASS 16268/16268 → 16268/16268 0 0 0 0 / 0 ok 60.0 → 59.1 17.09 → 18.89 2676 → 2627 None → None 0
Xenoblade Chronicles 2 0100E95004038000 PASS 15572/15572 → 15572/15572 0 0 0 0 / 0 ok 30.1 → 30.1 33.38 → 33.41 2503 → 2527 None → None 0
Kirby and the Forgotten Land 01004D300C5AE000 PASS 27297/27297 → 27297/27297 0 0 0 0 / 0 ok 30.0 → 30.0 33.7 → 33.83 5138 → 5163 None → None 0
SUPER MARIO ODYSSEY 0100000000010000 PASS 27984/27984 → 27984/27984 0 0 0 0 / 0 ok 60.0 → 60.0 18.82 → 19.35 4306 → 4308 None → None 0

Fire Emblem Three Houses 010055D009F78000: PASS

  • time: chain 0.5 s

Super Mario Bros Wonder 010015100B514000: PASS

  • screenshots (s: dHash bits / RGB diff / baseline noise): 50: 1/0.5/0, 58: 0/0.71/0
  • baseline: exit None, first frame 1.8 s, max frame 3793, reached True, prepare None s, prebuild 23.9 s (0 built, 0 failed), fps 60.0, p50/p99 16.66/17.09 ms, peak 2676 MB
  • candidate: exit None, first frame 0.8 s, max frame 3837, reached True, prepare None s, prebuild 18.1 s (0 built, 0 failed), fps 59.1, p50/p99 16.67/18.89 ms, peak 2627 MB
  • time: chain 7 s, smoke 143.4 s

Xenoblade Chronicles 2 0100E95004038000: PASS

  • screenshots (s: dHash bits / RGB diff / baseline noise): 46: 0/0.15/0, 58: 0/0.18/0
  • baseline: exit None, first frame 0.3 s, max frame 1931, reached True, prepare None s, prebuild 14.7 s (0 built, 0 failed), fps 30.1, p50/p99 33.23/33.38 ms, peak 2503 MB
  • candidate: exit None, first frame 0.3 s, max frame 1937, reached True, prepare None s, prebuild 14.7 s (0 built, 0 failed), fps 30.1, p50/p99 33.23/33.41 ms, peak 2527 MB
  • time: chain 7.4 s, smoke 162.8 s

Kirby and the Forgotten Land 01004D300C5AE000: PASS

  • screenshots (s: dHash bits / RGB diff / baseline noise): 55: 0/0.08/0, 68: 0/0.28/0, 95: 3/0.59/0
  • baseline: exit None, first frame 2.8 s, max frame 2927, reached True, prepare None s, prebuild 815.0 s (21409 built, 0 failed), fps 30.0, p50/p99 33.33/33.7 ms, peak 5138 MB
  • candidate: exit None, first frame 2.8 s, max frame 2931, reached True, prepare None s, prebuild 736.7 s (21409 built, 0 failed), fps 30.0, p50/p99 33.33/33.83 ms, peak 5163 MB
  • time: chain 900.9 s, smoke 1753 s

SUPER MARIO ODYSSEY 0100000000010000: PASS

  • screenshots (s: dHash bits / RGB diff / baseline noise): 70: 1/0.44/0, 95: 4/0.89/0
  • baseline: exit None, first frame 3.1 s, max frame 6395, reached True, prepare None s, prebuild 53.6 s (0 built, 0 failed), fps 60.0, p50/p99 16.66/18.82 ms, peak 4306 MB
  • candidate: exit None, first frame 2.6 s, max frame 6433, reached True, prepare None s, prebuild 44.9 s (0 built, 0 failed), fps 60.0, p50/p99 16.67/19.35 ms, peak 4308 MB
  • time: chain 34.5 s, smoke 319.7 s

Thanks for the sanitizer infrastructure, reproductions and separate HID fix. Closing this PR because the work has been applied on main with the real-bug suppressions removed.

@seedds seedds closed this Oct 27, 2026
v0lumehi added a commit to v0lumehi/nxnative that referenced this pull request Oct 28, 2026
…coverage per PR (seedds#313)

The roadmap (seedds#313) asks which code the tests reach and which lines of a
change they run. A coverage job now answers that in its job summary,
with no external service and no threshold: it reports, it never fails.

C: `make coverage` in dm/host/tests and dm/gfx/tex/tests builds the same
test rules again through seedds#412's SAN hook with clang's source-based
coverage, into build/cov and cov/ (dm/sanitizers/coverage.mk), so the
plain build stays as it is. Each test process writes its raw profile in
continuous mode (%c), so the children that t_fpslock, t_preempt and
others fork and end with _exit count too (without it hle_fpslock.c read
0%). llvm-profdata merges, llvm-cov exports JSON without the tests,
third-party code and generated headers. The coverage run uses make -i:
it measures, the plain run is the test.

Python: coverage.py over dm/tools/tests, measuring dm/tools,
dm/gfx/tools, dm/games and scripts, subprocesses included (the tests
run many tools with sys.executable); configured in pyproject.toml,
pinned in .github/requirements/coverage.txt.

dm/tools/coverage_report.py writes the Markdown: totals per area, the
15 largest files and, given a base, the changed C, Objective-C and
Python lines the tests run and those they do not, counting only lines
the data knows. llvm-cov lines are rebuilt from the export's segments
as `llvm-cov show` marks them (checked line for line on five host
files), not from its per-function summaries, which count shared lines
twice. In CI the pull request's merge commit gives HEAD^1 as the base,
so fetch-depth 2 is enough. The raw reports are uploaded on every run.

Checked on an M-series Mac: make coverage in both folders (host 29 s,
textures 1 s), the tool tests under coverage (96 s); host 56.5% of
6,487 lines, CPU textures 86.3% of 233, Python tools 36.2% of 11,763;
the reporter against 659c69d~1 (seedds#351: 186 of 203 changed lines run);
unittest discover over dm/tools/tests (372 tests, OK), test_docs, and
lint_python.py (0 new).
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
Integrate the sanitizer targets, HID fixture correction and pinned Xcode
27.1 CI toolchain from seedds#412, seedds#443 and seedds#421. Select the matching runner for
the new sanitizer job as well as the four existing jobs.

Decode audio command headers without alignment assumptions and validate
their extents before consuming them. Finish AudioUnit construction before
publishing an output to lifecycle and watchdog readers. Publish pthread
handles under the scheduler lock and serialize startup, priority and QoS
fields with their readers. Remove all three real-bug suppressions.

Add unaligned/truncated header coverage, an output-publication assertion
and priority/QoS registration checks, including QoS-enabled TSan CI cases.
Document the mechanisms and retain only fixture/framework exceptions.

Plain host, ASan/UBSan, TSan, 40 QoS startup stress runs, the complete Metal
graphics suite, 525 Python tests (20 skipped), lint, all 178 iOS source
compiles and the unsigned synthetic iOS app build passed. Gameplay
regression results are reported with the PR follow-up.

Fixes seedds#409
Fixes seedds#410
Fixes seedds#411

Co-authored-by: v0lumehi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants