SDL_CloseAudioDevice heap-use-after-free crash for specific audio capture devices
We have found that in our software, specific audio capture devices causes double-free (and use-after-free) crash in SDL_CloseAudioDevice function. The same thing can be reproduced in testaudio program (See video below)
(Video with audio)
https://github.com/libsdl-org/SDL/assets/29149078/a6f49adc-e354-43e8-aa23-6b7c54b22fa8
How to reproduce (Linux):
- Install EasyEffects
- Start audio capture using "Easy Effects Source" device in
testaudio.cprogram - Close audio stream
We have found out that this can also happen on MacOS on specific devices, according to sent crash reports on our side (SDL_CloseAudioDeviceREAL).
Output from this device (testaudioinfo):
INFO: Built-in audio drivers:
INFO: 0: pulseaudio
INFO: 1: alsa
INFO: 2: jack
INFO: 3: pipewire
INFO: 4: dsp
INFO: 5: disk
INFO: 6: dummy
INFO: Select a driver with the SDL_AUDIO_DRIVER environment variable.
INFO: Using audio driver: pulseaudio
INFO: Found 3 capture devices:
INFO: 0: Easy Effects Source <----------------- Problematic device
INFO: Sample Rate: 48000
INFO: Channels: 2
INFO: SDL_AudioFormat: 8120
INFO: Buffer Size: 1024 frames
INFO: 1: NCX Analog Stereo
INFO: Sample Rate: 48000
INFO: Channels: 2
INFO: SDL_AudioFormat: 8010
INFO: Buffer Size: 1024 frames
INFO: 2: Loopback Analog Stereo
INFO: Sample Rate: 48000
INFO: Channels: 2
INFO: SDL_AudioFormat: 8020
INFO: Buffer Size: 1024 frames
Tested on main branch, newest commit (bf64fecf)
Additional info from sanitizers (-fsanitize=address,undefined) shows up another use-after-free error:
As you can see, DestroyPhysicalAudioDevice performs SDL_free(device), then accessing device->thread_alive a line below.
Tested on (95a9271d)
The thread_alive issue you mentioned is now fixed, I assume the other crash is still present, though.
Yes, can confirm.
The latest fixes this bug for me (which went from 100% reproducible with EasyEffects to not reproducible at all), but it's a dramatic change, so please let me know if other problems pop up.
Just tested it, works perfectly! Thanks!