fix(coreaudio): fix undefined behavior causing sanitizer crashes #1052
+205
−20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes undefined behavior in CoreAudio backend that causes crashes under AddressSanitizer and ThreadSanitizer. This was introduced in #943 when migrating from
coreaudio-rstoobjc2-core-audio.The Bug
In 10 locations across
macos/enumerate.rsandmacos/device.rs,data_sizevariables were declared immutable but passed to CoreAudio APIs that write to them:CoreAudio's size parameter is
inout- the API writes the actual data size back. CreatingNonNullfrom an immutable reference and then writing through it is undefined behavior.Why It Wasn't Caught Earlier
The old raw pointer code hid the UB with unsafe casts:
The new
objc2-core-audioAPI properly enforces mutability contracts viaNonNull, exposing the latent bug. Normal builds work fine because at the machine level the stack memory is writable, but sanitizers enforce Rust's safety contracts and crash.The Fix
Changed all affected variables to
mutand updatedNonNullcalls:Files changed:
src/host/coreaudio/macos/enumerate.rs: 2data_sizevariables, 4 API callssrc/host/coreaudio/macos/device.rs: 7data_sizevariables, 14 API callsReproducing the Bug
The existing tests (
test_play,test_record) trigger device enumeration, which hits the buggy code.Bonus: Sanitizer CI Workflow
This PR includes a new
.github/workflows/sanitizers.ymlthat would have caught this bug before release. It runs ASAN and TSAN on both macOS (CoreAudio) and Linux (ALSA) since UB can be platform-specific.Benefits:
Checklist
cargo check)Related
coreaudio-rs#943 (objc2-core-audio migration)