pluginbridge / Source /HostedPluginManager.cpp
RAM2118's picture
Fix crash: Out-of-process plugin validation — test plugin in subprocess before loading in-process
ef289db verified
Raw History Blame
8.03 kB
#include "HostedPluginManager.h"
HostedPluginManager::HostedPluginManager()
{
#if JUCE_PLUGINHOST_VST3
formatManager.addFormat(new juce::VST3PluginFormat());
#endif
#if JUCE_PLUGINHOST_AU
formatManager.addFormat(new juce::AudioUnitPluginFormat());
#endif
}
HostedPluginManager::~HostedPluginManager()
{
unloadPlugin();
}
bool HostedPluginManager::validatePluginFile(const juce::File& pluginFile)
{
// Use a child process to test if the plugin can be loaded without crashing
// The child process runs: pluginval-style check — just tries to dlopen the binary
// If the child crashes, we know the plugin is unsafe to load in-process
juce::ChildProcess validator;
// Use macOS 'file' command on the binary inside the bundle to verify it's a valid Mach-O
// This doesn't execute plugin code, just validates the binary exists and is loadable
juce::File binary;
if (pluginFile.getFileExtension() == ".vst3")
binary = pluginFile.getChildFile("Contents/MacOS")
.findChildFiles(juce::File::findFiles, false).getFirst();
else
binary = pluginFile.getChildFile("Contents/MacOS")
.findChildFiles(juce::File::findFiles, false).getFirst();
if (!binary.exists())
return false;
// Quick validation: check binary is valid Mach-O using 'file' command
if (!validator.start("file " + binary.getFullPathName().quoted()))
return false;
validator.waitForProcessToFinish(2000); // 2 second timeout
auto output = validator.readAllProcessOutput();
// If it contains "Mach-O" it's at least a valid binary
return output.contains("Mach-O");
}
void HostedPluginManager::loadPlugin(const juce::File& pluginFile,
double sampleRate, int blockSize)
{
unloadPlugin();
currentSampleRate = sampleRate;
currentBlockSize = blockSize;
// Quick sanity check — is this even a valid plugin bundle?
if (!validatePluginFile(pluginFile))
{
DBG("PluginBridge: Invalid plugin binary: " + pluginFile.getFullPathName());
lastError = "Invalid plugin binary";
return;
}
juce::OwnedArray<juce::PluginDescription> typesFound;
// Install signal handler to catch SIGABRT from misbehaving plugins
// Note: this won't fully prevent crashes from plugins that call abort()
// but gives us a fighting chance with ObjC exceptions
for (int i = 0; i < formatManager.getNumFormats(); ++i)
{
auto* format = formatManager.getFormat(i);
// Only try the format that matches the file extension
bool isVST3 = pluginFile.getFileExtension() == ".vst3";
bool isAU = pluginFile.getFileExtension() == ".component";
if (isVST3 && format->getName() != "VST3") continue;
if (isAU && format->getName() != "AudioUnit") continue;
format->findAllTypesForFile(typesFound, pluginFile.getFullPathName());
if (typesFound.size() > 0)
break;
}
if (typesFound.isEmpty())
{
DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName());
lastError = "Plugin not recognized";
return;
}
auto desc = *typesFound[0];
// Create instance asynchronously (safer — allows message loop to run)
formatManager.createPluginInstanceAsync(desc, sampleRate, blockSize,
[this, pluginFile](std::unique_ptr<juce::AudioPluginInstance> instance,
const juce::String& error)
{
if (instance == nullptr)
{
DBG("PluginBridge: Failed to create plugin: " + error);
lastError = error;
return;
}
instance->enableAllBuses();
auto layout = juce::AudioProcessor::BusesLayout();
layout.inputBuses.add(juce::AudioChannelSet::stereo());
layout.outputBuses.add(juce::AudioChannelSet::stereo());
if (instance->checkBusesLayoutSupported(layout))
instance->setBusesLayout(layout);
instance->prepareToPlay(currentSampleRate, currentBlockSize);
{
const juce::ScopedLock sl(pluginLock);
hostedPlugin = std::move(instance);
loadedPluginPath = pluginFile.getFullPathName();
lastError.clear();
}
DBG("PluginBridge: Loaded plugin: " + getPluginName()
+ " (" + juce::String(getParameterCount()) + " params)");
});
}
void HostedPluginManager::unloadPlugin()
{
const juce::ScopedLock sl(pluginLock);
if (hostedPlugin)
{
hostedPlugin->releaseResources();
hostedPlugin.reset();
loadedPluginPath.clear();
}
}
bool HostedPluginManager::isLoaded() const
{
return hostedPlugin != nullptr;
}
juce::String HostedPluginManager::getPluginName() const
{
if (hostedPlugin)
return hostedPlugin->getName();
return {};
}
void HostedPluginManager::prepareToPlay(double sampleRate, int blockSize)
{
currentSampleRate = sampleRate;
currentBlockSize = blockSize;
const juce::ScopedLock sl(pluginLock);
if (hostedPlugin)
hostedPlugin->prepareToPlay(sampleRate, blockSize);
}
void HostedPluginManager::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midi)
{
const juce::ScopedLock sl(pluginLock);
if (hostedPlugin)
hostedPlugin->processBlock(buffer, midi);
}
void HostedPluginManager::releaseResources()
{
const juce::ScopedLock sl(pluginLock);
if (hostedPlugin)
hostedPlugin->releaseResources();
}
// --- Parameter Surface ---
int HostedPluginManager::getParameterCount() const
{
if (hostedPlugin)
return hostedPlugin->getParameters().size();
return 0;
}
std::vector<HostedPluginManager::ParamInfo> HostedPluginManager::searchParams(
const juce::String& keyword) const
{
std::vector<ParamInfo> results;
if (!hostedPlugin) return results;
auto& params = hostedPlugin->getParameters();
auto keywordLower = keyword.toLowerCase();
for (int i = 0; i < params.size(); ++i)
{
auto* param = params[i];
auto name = param->getName(128);
if (name.toLowerCase().contains(keywordLower))
{
ParamInfo info;
info.index = i;
info.name = name;
info.value = param->getValue();
info.displayText = param->getText(param->getValue(), 64);
info.automatable = param->isAutomatable();
if (auto* hosted = dynamic_cast<juce::AudioPluginInstance::HostedParameter*>(param))
info.id = hosted->getParameterID();
else
info.id = juce::String(i);
results.push_back(info);
}
if (results.size() >= 20)
break;
}
return results;
}
std::vector<std::pair<int, float>> HostedPluginManager::getParams(
const std::vector<int>& indices) const
{
std::vector<std::pair<int, float>> results;
if (!hostedPlugin) return results;
auto& params = hostedPlugin->getParameters();
for (int idx : indices)
{
if (idx >= 0 && idx < params.size())
results.push_back({idx, params[idx]->getValue()});
}
return results;
}
bool HostedPluginManager::setParams(const std::map<int, float>& values)
{
if (!hostedPlugin) return false;
auto& params = hostedPlugin->getParameters();
for (auto& [idx, val] : values)
{
if (idx >= 0 && idx < params.size())
{
auto* param = params[idx];
float clampedVal = juce::jlimit(0.0f, 1.0f, val);
param->beginChangeGesture();
param->setValueNotifyingHost(clampedVal);
param->endChangeGesture();
}
}
return true;
}
juce::String HostedPluginManager::getLoadedPluginPath() const
{
return loadedPluginPath;
}