From 71f58228a704c98faed57f0ba7fe404424cb6a3e Mon Sep 17 00:00:00 2001 From: norcal88 <43097213+norcal88@users.noreply.github.com> Date: Mon, 28 Sep 2026 11:19:43 -0700 Subject: [PATCH 1/4] Fix Pluto buddy use-after-free on device set removal (#2721) --- sdrgui/mainwindow.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index cf1321c93..dbf2202c0 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -805,9 +805,7 @@ void RemoveDeviceSetFSM::removeUI() QObject::disconnect(m_deviceUISet->m_deviceAPI, nullptr, deviceGUI, nullptr); delete deviceGUI; m_deviceUISet->m_deviceAPI->resetSamplingDeviceId(); - if (!m_deviceMIMOEngine) { - m_deviceUISet->m_deviceAPI->clearBuddiesLists(); // clear old API buddies lists - } + // Buddy lists are cleared in removeDeviceSet(), after the sample source/sink is deleted (#2721). } void RemoveDeviceSetFSM::stopEngine() @@ -848,11 +846,13 @@ void RemoveDeviceSetFSM::removeDeviceSet() if (m_deviceSourceEngine) { delete deviceAPI->getSampleSource(); + deviceAPI->clearBuddiesLists(); // after the delete, so closeDevice() still saw its buddies (#2721) delete m_deviceSourceEngine; } else if (m_deviceSinkEngine) { delete deviceAPI->getSampleSink(); + deviceAPI->clearBuddiesLists(); // after the delete, so closeDevice() still saw its buddies (#2721) delete m_deviceSinkEngine; } else From 0586dcf20237f00f77622a9589353f5424f77861 Mon Sep 17 00:00:00 2001 From: norcal88 <43097213+norcal88@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:30:49 -0700 Subject: [PATCH 2/4] PlutoSDROutput::start: check m_deviceParams before dereferencing it PlutoSDRInput::start() guards both m_deviceShared.m_deviceParams and m_deviceShared.m_deviceParams->getBox(); PlutoSDROutput::start() only guards getBox() and dereferences m_deviceParams to get there. m_deviceParams is null when the device never opened (no route to the iio host, radio rebooting, or -- on macOS -- the Local Network privacy grant denied, which is silent), and closeDevice() also sets it to 0. Pressing Start on such a device set then dereferences null and crashes the application. Mirror the Input side's check. --- plugins/samplesink/plutosdroutput/plutosdroutput.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp index 7ebb8af76..96ee392d0 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp +++ b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp @@ -108,6 +108,11 @@ bool PlutoSDROutput::start() return true; } + if (!m_deviceShared.m_deviceParams) + { + qCritical("PlutoSDROutput::start: device not open"); + return false; + } if (!m_deviceShared.m_deviceParams->getBox()) { qCritical("PlutoSDROutput::start: device not open"); From 840cd65e1f001c2a9e95ae9c68a075e31932e0af Mon Sep 17 00:00:00 2001 From: norcal88 <43097213+norcal88@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:43:54 -0700 Subject: [PATCH 3/4] PlutoSDR: check the buddy's thread before resuming it in applySettings Both PlutoSDRInput::applySettings() and PlutoSDROutput::applySettings() suspend their buddies' threads and resume them later: if (buddySharedPtr && buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); // m_thread may be null The suspend half tests buddySharedPtr->m_thread and sets m_threadWasRunning from it, but m_threadWasRunning is sticky state on the buddy's shared struct: between suspend and resume the buddy's thread can be destroyed -- its device was stopped, or a preset was loaded into it, both of which null m_thread -- while m_threadWasRunning stays true. startWork() is then called on a null pointer. Seen on macOS with the Satellite Tracker, which loads a preset into the Rx and the Tx device set of the same Pluto at AOS: the Tx applySettings is between suspend and resume when the Rx side is rebuilt. PlutoSDROutput::applySettings + 7284 PlutoSDROutput::handleMessage DeviceSampleSink::handleInputMessages MessageQueue::push PlutoSDROutput::deserialize DeviceAPI::loadSamplingDeviceSettings DeviceUISet::loadDeviceSetSettings Disassembly confirms the faulting load is m_thread's vtable read with x0 = 0. Test m_thread, as the suspend half does. --- plugins/samplesink/plutosdroutput/plutosdroutput.cpp | 6 +++++- plugins/samplesource/plutosdrinput/plutosdrinput.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp index 96ee392d0..3bdc14a0a 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp +++ b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp @@ -615,7 +615,11 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const { DevicePlutoSDRShared *buddySharedPtr = (DevicePlutoSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr && buddySharedPtr->m_threadWasRunning) { + // m_threadWasRunning is sticky state on the buddy's shared struct, but the + // buddy's thread can be gone by the time we resume (its device was stopped, + // or a preset was loaded into it), which nulls m_thread. The matching + // suspend block above already tests m_thread; test it here too. + if (buddySharedPtr && buddySharedPtr->m_threadWasRunning && buddySharedPtr->m_thread) { buddySharedPtr->m_thread->startWork(); } } diff --git a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp index 3b04c2653..380c1efe2 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp +++ b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp @@ -677,7 +677,11 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q { DevicePlutoSDRShared *buddySharedPtr = (DevicePlutoSDRShared *) (*itSink)->getBuddySharedPtr(); - if (buddySharedPtr && buddySharedPtr->m_threadWasRunning) { + // m_threadWasRunning is sticky state on the buddy's shared struct, but the + // buddy's thread can be gone by the time we resume (its device was stopped, + // or a preset was loaded into it), which nulls m_thread. The matching + // suspend block above already tests m_thread; test it here too. + if (buddySharedPtr && buddySharedPtr->m_threadWasRunning && buddySharedPtr->m_thread) { buddySharedPtr->m_thread->startWork(); } } From 0f8dbc2574e08afc381dac20d7d59af901bd086b Mon Sep 17 00:00:00 2001 From: norcal88 <43097213+norcal88@users.noreply.github.com> Date: Wed, 30 Sep 2026 07:41:37 -0700 Subject: [PATCH 4/4] PlutoSDR: serialize sample thread lifecycle across buddies stop() tore the sample thread down with stopWork() + delete and only then cleared m_deviceShared.m_thread. The buddy's applySettings() suspends and resumes that thread through m_thread from another thread, without holding this device's mutex, so it could restart the thread between stopWork() and delete (QThread: Destroyed while thread is still running -> qFatal/abort), or call through the pointer after the delete. The device's own applySettings() had the same exposure on its own thread pointer, since it does not take m_mutex either. Seen when the Satellite Tracker loads presets into the Rx and Tx device sets of one Pluto at AOS: abort in PlutoSDRInputThread::~PlutoSDRInputThread <- PlutoSDRInput::stop <- DSPDeviceSourceEngine::gotoIdle. Add DevicePlutoSDRShared::m_threadsMutex (recursive, shared by all PlutoSDR device sets) and take it in start() around startWork()/publish, in stop() and handleError() around unpublish + teardown (unpublishing first), in suspendBuddies()/resumeBuddies(), and for the whole suspend -> apply -> resume sequence in applySettings(). Also wait() before deleting the thread and check the own-thread pointer before resuming it. Co-Authored-By: Claude Fable 5.1 --- devices/plutosdr/deviceplutosdrshared.cpp | 1 + devices/plutosdr/deviceplutosdrshared.h | 11 +++++++ .../plutosdroutput/plutosdroutput.cpp | 33 ++++++++++++++----- .../plutosdrinput/plutosdrinput.cpp | 29 ++++++++++++---- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrshared.cpp b/devices/plutosdr/deviceplutosdrshared.cpp index 6f82146b9..8b08800f2 100644 --- a/devices/plutosdr/deviceplutosdrshared.cpp +++ b/devices/plutosdr/deviceplutosdrshared.cpp @@ -22,3 +22,4 @@ MESSAGE_CLASS_DEFINITION(DevicePlutoSDRShared::MsgCrossReportToBuddy, Message) const unsigned int DevicePlutoSDRShared::m_sampleFifoMinRate = 48000; +QRecursiveMutex DevicePlutoSDRShared::m_threadsMutex; diff --git a/devices/plutosdr/deviceplutosdrshared.h b/devices/plutosdr/deviceplutosdrshared.h index a1f0d229c..389194e9f 100644 --- a/devices/plutosdr/deviceplutosdrshared.h +++ b/devices/plutosdr/deviceplutosdrshared.h @@ -22,6 +22,8 @@ #include +#include + #include "util/message.h" #include "export.h" @@ -94,6 +96,15 @@ public: static const unsigned int m_sampleFifoMinRate; + /** + * Serializes every access to m_thread across all PlutoSDR device sets. + * A buddy suspends and resumes this device's sample thread through m_thread, from another + * thread and without holding this device's own mutex. Without a common lock it can restart + * a thread that stop() is in the middle of tearing down (QThread: Destroyed while thread is + * still running) or call through a pointer that has just been deleted. + */ + static QRecursiveMutex m_threadsMutex; + DevicePlutoSDRShared() : m_deviceParams(0), m_thread(0), diff --git a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp index 3bdc14a0a..f60e8bba0 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp +++ b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp @@ -126,8 +126,11 @@ bool PlutoSDROutput::start() qDebug("PlutoSDROutput::start: thread created"); m_plutoSDROutputThread->setLog2Interpolation(m_settings.m_log2Interp); - m_plutoSDROutputThread->startWork(); - m_deviceShared.m_thread = m_plutoSDROutputThread; + { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_plutoSDROutputThread->startWork(); + m_deviceShared.m_thread = m_plutoSDROutputThread; + } m_running = true; mutexLocker.unlock(); @@ -146,14 +149,20 @@ void PlutoSDROutput::stop() m_running = false; - if (m_plutoSDROutputThread != 0) + // Unpublish the thread before tearing it down, and keep the shared lock through + // stop/wait/delete. applySettings() holds the same lock across its suspend -> + // apply -> resume transaction, so teardown must not interleave with that sequence. + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_deviceShared.m_thread = nullptr; + m_deviceShared.m_threadWasRunning = false; + + if (m_plutoSDROutputThread) { m_plutoSDROutputThread->stopWork(); + m_plutoSDROutputThread->wait(); delete m_plutoSDROutputThread; - m_plutoSDROutputThread = 0; + m_plutoSDROutputThread = nullptr; } - - m_deviceShared.m_thread = 0; } void PlutoSDROutput::handleError(int errorCode) @@ -161,6 +170,9 @@ void PlutoSDROutput::handleError(int errorCode) QMutexLocker mutexLocker(&m_mutex); m_running = false; + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_deviceShared.m_thread = nullptr; + m_deviceShared.m_threadWasRunning = false; if (m_plutoSDROutputThread) { if (m_plutoSDROutputThread->isRunning()) @@ -171,7 +183,6 @@ void PlutoSDROutput::handleError(int errorCode) delete m_plutoSDROutputThread; m_plutoSDROutputThread = nullptr; } - m_deviceShared.m_thread = 0; const QString errorMessage = tr("Radio needs a restart: %1 (%2)") @@ -400,6 +411,7 @@ void PlutoSDROutput::closeDevice() void PlutoSDROutput::suspendBuddies() { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); // suspend Rx buddy's thread for (unsigned int i = 0; i < m_deviceAPI->getSourceBuddies().size(); i++) @@ -415,6 +427,7 @@ void PlutoSDROutput::suspendBuddies() void PlutoSDROutput::resumeBuddies() { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); // resume Rx buddy's thread for (unsigned int i = 0; i < m_deviceAPI->getSourceBuddies().size(); i++) @@ -438,6 +451,10 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const qDebug().noquote() << "PlutoSDROutput::applySettings: force:" << force << settings.getDebugString(settingsKeys, force); + // Held for the whole suspend -> apply -> resume sequence below (own thread and buddies'), + // so that a concurrent stop()/start() of either device set cannot interleave with it. + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + bool forwardChangeOwnDSP = false; bool forwardChangeOtherDSP = false; bool ownThreadWasRunning = false; @@ -625,7 +642,7 @@ bool PlutoSDROutput::applySettings(const PlutoSDROutputSettings& settings, const } } - if (ownThreadWasRunning) { + if (ownThreadWasRunning && m_plutoSDROutputThread) { m_plutoSDROutputThread->startWork(); } diff --git a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp index 380c1efe2..9bfee3305 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp +++ b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp @@ -127,8 +127,11 @@ bool PlutoSDRInput::start() m_plutoSDRInputThread->setLog2Decimation(m_settings.m_log2Decim); m_plutoSDRInputThread->setIQOrder(m_settings.m_iqOrder); - m_plutoSDRInputThread->startWork(); - m_deviceShared.m_thread = m_plutoSDRInputThread; + { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_plutoSDRInputThread->startWork(); + m_deviceShared.m_thread = m_plutoSDRInputThread; + } m_running = true; mutexLocker.unlock(); @@ -147,14 +150,20 @@ void PlutoSDRInput::stop() m_running = false; + // Unpublish the thread before tearing it down, and keep the shared lock through + // stop/wait/delete. applySettings() holds the same lock across its suspend -> + // apply -> resume transaction, so teardown must not interleave with that sequence. + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_deviceShared.m_thread = nullptr; + m_deviceShared.m_threadWasRunning = false; + if (m_plutoSDRInputThread) { m_plutoSDRInputThread->stopWork(); + m_plutoSDRInputThread->wait(); delete m_plutoSDRInputThread; m_plutoSDRInputThread = nullptr; } - - m_deviceShared.m_thread = nullptr; } void PlutoSDRInput::handleError(int errorCode) @@ -162,6 +171,9 @@ void PlutoSDRInput::handleError(int errorCode) QMutexLocker mutexLocker(&m_mutex); m_running = false; + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + m_deviceShared.m_thread = nullptr; + m_deviceShared.m_threadWasRunning = false; if (m_plutoSDRInputThread) { if (m_plutoSDRInputThread->isRunning()) @@ -172,7 +184,6 @@ void PlutoSDRInput::handleError(int errorCode) delete m_plutoSDRInputThread; m_plutoSDRInputThread = nullptr; } - m_deviceShared.m_thread = nullptr; if (m_deviceAPI->getSinkBuddies().size() == 0) { @@ -419,6 +430,7 @@ void PlutoSDRInput::closeDevice() void PlutoSDRInput::suspendBuddies() { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); // suspend Tx buddy's thread for (unsigned int i = 0; i < m_deviceAPI->getSinkBuddies().size(); i++) @@ -434,6 +446,7 @@ void PlutoSDRInput::suspendBuddies() void PlutoSDRInput::resumeBuddies() { + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); // resume Tx buddy's thread for (unsigned int i = 0; i < m_deviceAPI->getSinkBuddies().size(); i++) @@ -455,6 +468,10 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q return false; } + // Held for the whole suspend -> apply -> resume sequence below (own thread and buddies'), + // so that a concurrent stop()/start() of either device set cannot interleave with it. + QMutexLocker threadsLocker(&DevicePlutoSDRShared::m_threadsMutex); + bool forwardChangeOwnDSP = false; bool forwardChangeOtherDSP = false; bool ownThreadWasRunning = false; @@ -687,7 +704,7 @@ bool PlutoSDRInput::applySettings(const PlutoSDRInputSettings& settings, const Q } } - if (ownThreadWasRunning) { + if (ownThreadWasRunning && m_plutoSDRInputThread) { m_plutoSDRInputThread->startWork(); }