Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash due to concurrent access in MidiController #13980

Open
wants to merge 5 commits into
base: 2.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/controllers/bulk/bulkcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ void BulkController::send(const QList<int>& data, unsigned int length) {

void BulkController::sendBytes(const QByteArray& data) {
VERIFY_OR_DEBUG_ASSERT(!m_pMapping ||
m_pMapping->getDeviceDirection() &
LegacyControllerMapping::DeviceDirection::Outgoing) {
(m_pMapping->getDeviceDirection() &
LegacyControllerMapping::DeviceDirection::Outgoing)) {
qDebug() << "The mapping for the bulk device" << getName()
<< "doesn't require sending data. Ignoring sending request.";
return;
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/controllermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ void ControllerManager::slotShutdown() {
}

void ControllerManager::updateControllerList() {
static bool already_called = false;
VERIFY_OR_DEBUG_ASSERT(!already_called) {
qWarning() << "skipping ControllerManager::updateControllerList() because is already called";
return;
}
already_called = true;
// NOTE: Currently this function is only called on startup. If hotplug is added, changes to the
// controller list must be synchronized with dlgprefcontrollers to avoid dangling connections
// and possible crashes.
Expand Down
81 changes: 61 additions & 20 deletions src/controllers/midi/midicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QJSValue>
#include <algorithm>
#include <atomic>

#include "control/controlobject.h"
#include "controllers/defs_controllers.h"
Expand Down Expand Up @@ -37,7 +38,11 @@ MidiController::MidiController(const QString& deviceName)

void MidiController::slotBeforeEngineShutdown() {
Controller::slotBeforeEngineShutdown();
m_pMapping->removeInputHandlerMappings();
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return;
}
pMapping->removeInputHandlerMappings();
}

MidiController::~MidiController() {
Expand All @@ -55,14 +60,25 @@ QString MidiController::mappingExtension() {
}

void MidiController::setMapping(std::shared_ptr<LegacyControllerMapping> pMapping) {
m_pMapping = downcastAndTakeOwnership<LegacyMidiControllerMapping>(std::move(pMapping));
std::shared_ptr<LegacyMidiControllerMapping> pMidiMapping =
downcastAndTakeOwnership<LegacyMidiControllerMapping>(
std::move(pMapping));
#ifdef __cpp_lib_atomic_shared_ptr
// Used Spinlock
m_pMapping.store(pMidiMapping, std::memory_order_relaxed);
#else
// Uses Mutex
std::atomic_store_explicit(&m_pMapping, pMidiMapping, std::memory_order_relaxed);
#endif
}

std::shared_ptr<LegacyControllerMapping> MidiController::cloneMapping() {
if (!m_pMapping) {
// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return nullptr;
}
return m_pMapping->clone();
return pMapping->clone();
}

int MidiController::close() {
Expand Down Expand Up @@ -92,16 +108,14 @@ bool MidiController::applyMapping() {
}

void MidiController::createOutputHandlers() {
if (!m_pMapping) {
return;
}

if (m_pMapping->getOutputMappings().isEmpty()) {
// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping || pMapping->getOutputMappings().isEmpty()) {
return;
}

QStringList failures;
for (const auto& mapping : m_pMapping->getOutputMappings()) {
for (const auto& mapping : pMapping->getOutputMappings()) {
QString group = mapping.controlKey.group;
QString key = mapping.controlKey.item;

Expand Down Expand Up @@ -223,7 +237,9 @@ void MidiController::clearTemporaryInputMappings() {
}

void MidiController::commitTemporaryInputMappings() {
if (!m_pMapping) {
// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return;
}

Expand All @@ -232,15 +248,15 @@ void MidiController::commitTemporaryInputMappings() {
// m_temporaryInputMappings from m_mapping's input mappings.
for (auto it = m_temporaryInputMappings.constBegin();
it != m_temporaryInputMappings.constEnd(); ++it) {
m_pMapping->removeInputMapping(it.key());
pMapping->removeInputMapping(it.key());
}

// Now, we can just use add all mappings from m_temporaryInputMappings
// since we removed the duplicates in the original set.
for (auto it = m_temporaryInputMappings.constBegin();
it != m_temporaryInputMappings.constEnd();
++it) {
m_pMapping->addInputMapping(it.key(), it.value());
pMapping->addInputMapping(it.key(), it.value());
}
m_temporaryInputMappings.clear();
}
Expand Down Expand Up @@ -286,8 +302,13 @@ void MidiController::receivedShortMessage(unsigned char status,
}
}

auto it = m_pMapping->getInputMappings().constFind(mappingKey.key);
for (; it != m_pMapping->getInputMappings().constEnd() && it.key() == mappingKey.key; ++it) {
// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return;
}
auto it = pMapping->getInputMappings().constFind(mappingKey.key);
for (; it != pMapping->getInputMappings().constEnd() && it.key() == mappingKey.key; ++it) {
processInputMapping(it.value(), status, control, value, timestamp);
}
}
Expand Down Expand Up @@ -584,8 +605,13 @@ void MidiController::receive(const QByteArray& data, mixxx::Duration timestamp)
}
}

// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return;
}
const auto [inputMappingsBegin, inputMappingsEnd] =
m_pMapping->getInputMappings().equal_range(mappingKey.key);
pMapping->getInputMappings().equal_range(mappingKey.key);
std::for_each(inputMappingsBegin, inputMappingsEnd, [&](const auto& inputMapping) {
processInputMapping(inputMapping, data, timestamp);
});
Expand All @@ -612,6 +638,11 @@ QJSValue MidiController::makeInputHandler(int status, int midino, const QJSValue
VERIFY_OR_DEBUG_ASSERT(pJsEngine) {
return QJSValue();
}
// Function becomes temporary shared owner
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return QJSValue();
}

if (!scriptCode.isCallable()) {
auto error = kMakeInputHandlerError;
Expand All @@ -635,8 +666,8 @@ QJSValue MidiController::makeInputHandler(int status, int midino, const QJSValue

const auto midiKey = MidiKey(status, midino);

auto it = m_pMapping->getInputMappings().constFind(midiKey.key);
if (it != m_pMapping->getInputMappings().constEnd() &&
auto it = pMapping->getInputMappings().constFind(midiKey.key);
if (it != pMapping->getInputMappings().constEnd() &&
it.value().options.testFlag(MidiOption::Script) &&
std::holds_alternative<ConfigKey>(it.value().control)) {
qCWarning(m_logBase) << QStringLiteral(
Expand All @@ -652,6 +683,16 @@ QJSValue MidiController::makeInputHandler(int status, int midino, const QJSValue
MidiOption::Script,
std::make_shared<QJSValue>(scriptCode));

m_pMapping->addInputMapping(inputMapping.key.key, inputMapping);
return pJsEngine->newQObject(new MidiInputHandleJSProxy(m_pMapping, inputMapping));
pMapping->addInputMapping(inputMapping.key.key, inputMapping);
return pJsEngine->newQObject(new MidiInputHandleJSProxy(pMapping, inputMapping));
}

std::shared_ptr<LegacyMidiControllerMapping> MidiController::getSharedMapping() const {
#ifdef __cpp_lib_atomic_shared_ptr
// Used Spinlock
return m_pMapping.load(std::memory_order_relaxed);
#else
// Uses Mutex
return std::atomic_load_explicit(&m_pMapping, std::memory_order_relaxed);
#endif
}
12 changes: 10 additions & 2 deletions src/controllers/midi/midicontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ class MidiController : public Controller {
virtual std::shared_ptr<LegacyControllerMapping> cloneMapping() override;

bool isMappable() const override {
if (!m_pMapping) {
std::shared_ptr<LegacyMidiControllerMapping> pMapping = getSharedMapping();
if (!pMapping) {
return false;
}
return m_pMapping->isMappable();
return pMapping->isMappable();
}

bool matchMapping(const MappingInfo& mapping) override;
Expand Down Expand Up @@ -103,10 +104,17 @@ class MidiController : public Controller {
void createOutputHandlers();
void updateAllOutputs();
void destroyOutputHandlers();
std::shared_ptr<LegacyMidiControllerMapping> getSharedMapping() const;

QHash<uint16_t, MidiInputMapping> m_temporaryInputMappings;
QList<MidiOutputHandler*> m_outputs;

#ifdef __cpp_lib_atomic_shared_ptr
std::atomic<std::shared_ptr<LegacyMidiControllerMapping>> m_pMapping;
#else
std::shared_ptr<LegacyMidiControllerMapping> m_pMapping;
#endif

SoftTakeoverCtrl m_st;
QList<QPair<MidiInputMapping, unsigned char>> m_fourteen_bit_queued_mappings;

Expand Down
Loading