Skip to content

ASIO driver use-after-free when a device fails the capability check #3868

Open

Description

@ann0see

Summary

Use-after-free on the ASIO SDK's global theAsioDriver pointer when a device
fails the capability check.
On current main, a device that fails
CheckDeviceCapabilities() during ASIO re-init can leave a dangling global
pointer that a later re-init dereferences. Symptoms are inconsistent — hang,
crash, or silent corruption — which matches the reports in #872 and #305.

Context

Evidence (mcfnord, on Windows — issue #3779 comment 5224437188)

One honest caveat up front: this measurement was done with a synthetic test
driver
that deliberately fails the capability check, not with ASIO4ALL on real
hardware. With that said:

  • This is a use-after-free, not a leak: ASIOExit() is effectively
    removeCurrentDriver() + theAsioDriver = 0. The current failure branch
    only does the first of those.
  • Outcome is driver-dependent: ASIO4ALL's driver object is static in its
    DLL and survives the COM release (→ hang / lockout); Focusrite USB
    ASIO's
    object is heap-allocated, so the next call faults (0xC0000005).
  • With the synthetic driver, the stock build (4d142945) made seven ASIO
    calls on the freed object
    ; with ASIOExit(), zero.

How to reproduce

The reliable repro we have is with a synthetic driver that fails
CheckDeviceCapabilities() on demand. On real Windows hardware with ASIO4ALL
this is our best guess at hitting the same path — it is unverified:

  1. Select a device that fails the capability check (e.g. one that does not
    support the required sample rate) while a valid device is also available.
  2. Trigger re-init (e.g. change the audio device in the client settings).
  3. Re-select the previous working device.

The failure branch is hit at CSound::LoadAndInitializeDriver()
(src/sound/asio/sound.cpp) — CheckDeviceCapabilities() fails, the driver is
released via asioDrivers->removeCurrentDriver(), and the global
theAsioDriver pointer is left dangling. A subsequent re-init dereferences it.

Proposed fix

In CSound::LoadAndInitializeDriver() (src/sound/asio/sound.cpp), the
CheckDeviceCapabilities() failure branch should call ASIOExit() instead
of asioDrivers->removeCurrentDriver(), so the global pointer is nulled. The
ASIOInit()-failed branch is left alone — from reading the ASIO SDK it resets
the global pointer on that path, though I haven't instrumented it to be sure.

A ready patch exists in fork PR ann0see/jamulus#286 (one-line change + comment),
reproducing the 7-vs-0 result. This issue is opened to track the defect so the
fix is not lost if the fork PR is never reopened upstream.

Requested actions

  • Review whether the failure branch should use ASIOExit().
  • If agreed, apply the fix on main (a small, low-risk change).

⚠️ AI-generated issue — please verify

  • This text is AI generated, may be wrong, and may contain inaccuracies.
  • The hardware verification described below was performed by a human with an AI agent
    (mcfnord, on Windows).

Activity

  1. added
    bugSomething isn't working
    WindowsWindows runtime issue
    AIAI generated or potentially AI generated
    on Aug 8, 2026
  2. ann0see commented on Aug 8, 2026

    @ann0see
    MemberAuthor

    Opened to track the capability check potential bug.

  3. ann0see commented on Aug 8, 2026

    @ann0see
    MemberAuthor

    ⚠️ AI-generated — may not be correct. Please verify before acting.

    Very brief diff idea (for the fix in this issue)

    In CSound::LoadAndInitializeDriver() (src/sound/asio/sound.cpp), the
    CheckDeviceCapabilities() failure branch:

    -        // driver cannot be used, clean up
    -        asioDrivers->removeCurrentDriver();
    +        // driver cannot be used, clean up with ASIOExit() (also nulls theAsioDriver)
    +        ASIOExit();

    The idea is that ASIOExit() closes the session and nulls the SDK global
    theAsioDriver, preventing the use-after-free. This is just an idea and may
    not be correct
    — e.g. the exact return/error path may differ on current
    main, or ASIOExit() may have side effects the branch should avoid.

  4. changed the title [-]ASIO driver use-after-free on failed device capability check (ASIO4ALL lockout / process won't exit)[/-] [+]ASIO driver use-after-free when a device fails the capability check[/+] on Aug 8, 2026
  5. mcfnord commented on Aug 12, 2026

    @mcfnord
    Contributor

    🤖 AI: The ASIOExit() idea splits into an in-tree half that checks out and an SDK half that cannot be checked from this repo. The routine loads the selected driver by name, then reaches the only ASIOInit() call anywhere in this file, and the path directly beneath it returns early with a bare removeCurrentDriver() when that initialization fails — correct, because nothing was initialized at that point. The capability check that runs immediately afterwards, backed by the channel-requirement logic it delegates to, is reached only once ASE_OK has come back, and yet its failure branch releases the driver through that same bare removeCurrentDriver(). The normal teardown routine handling the identical situation does it the other way, calling ASIOExit() before it releases the driver, while the driver enumeration inside the constructor never initializes one at all. Since that ASIOInit() is the file's only one, the capability-failure branch is the single place an initialized driver is released without ASIOExit() — the branch to compare against the ASIO4ALL red-cross lockout described in #872 and the inconsistent crash-or-hang behaviour recorded in #305. The same re-init sequence already carries a second and independent hardening candidate in the stray-bufferSwitch() and ASIOMutex-scope proposal filed as #3869, which touches the callback side rather than the release side. The SDK-internal half — that ASIOExit() nulls the global — is not verifiable from here, since the ASIO SDK is not vendored in this repo.

  6. mcfnord commented on Aug 14, 2026

    @mcfnord
    Contributor

    🤖 AI: The sound.cpp hunk from ann0see/jamulus#286 has not reached main. That PR closed on 2026-08-08 with "I HOPE that everything from this PR is now captured upstream (issue #3868)"; the client.cpp parts did land via PR #3851, the ASIO one did not.

    At 5146a072, ASIOExit() occurs once in the ASIO backend, inside UnloadCurrentDriver(). The capability-failure branch still ends in a bare removeCurrentDriver(), so the dangling theAsioDriver this issue describes, measured on real hardware, is still reachable on current main. A search of this repository returns no pull request mentioning ASIOExit, open or closed.

    Is that hunk still intended for merge?

  7. ann0see commented on Aug 14, 2026

    @ann0see
    MemberAuthor

    I'd like to not look at this ATM since we've fixed the hang (which is the most important one).
    Feel free to take it over after the PR freeze.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AIAI generated or potentially AI generatedWindowsWindows runtime issuebugSomething isn't working

    Type

    No type

    Projects

    • Status
      Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions