Fix crash: Out-of-process plugin validation — test plugin in subprocess before loading in-process
Browse files14 unmodified lines1516171819202 unmodified lines23242526272829303132333435363738394041424344454647484950515253545556571 unmodified line596061626364656667686970711 unmodified line737475767778798081828384858614 unmodified linesunloadPlugin();}void HostedPluginManager::loadPlugin(const juce::File& pluginFile,double sampleRate, int blockSize){2 unmodified linescurrentSampleRate = sampleRate;currentBlockSize = blockSize;juce::String errorMessage;juce::OwnedArray<juce::PluginDescription> typesFound;// Wrap in try/catch — some plugins throw or show dialogs that cause exceptionstry{for (int i = 0; i < formatManager.getNumFormats(); ++i){auto* format = formatManager.getFormat(i);format->findAllTypesForFile(typesFound, pluginFile.getFullPathName());if (typesFound.size() > 0)break;}}catch (...){DBG("PluginBridge: Exception during plugin scan for: " + pluginFile.getFullPathName());return;}if (typesFound.isEmpty()){DBG("PluginBridge: No plugin found in file: " + pluginFile.getFullPathName());return;}auto desc = *typesFound[0];// Use async creation — safer for AU plugins and plugins that need message threadformatManager.createPluginInstanceAsync(desc, sampleRate, blockSize,[this, pluginFile](std::unique_ptr<juce::AudioPluginInstance> instance,const juce::String& error)1 unmodified lineif (instance == nullptr){DBG("PluginBridge: Failed to create plugin: " + error);return;}// Enable all busesinstance->enableAllBuses();// Match bus layout: stereo in/outauto layout = juce::AudioProcessor::BusesLayout();layout.inputBuses.add(juce::AudioChannelSet::stereo());layout.outputBuses.add(juce::AudioChannelSet::stereo());1 unmodified lineif (instance->checkBusesLayoutSupported(layout))instance->setBusesLayout(layout);// Prepare for playbackinstance->prepareToPlay(currentSampleRate, currentBlockSize);// Store{const juce::ScopedLock sl(pluginLock);hostedPlugin = std::move(instance);loadedPluginPath = pluginFile.getFullPathName();}DBG("PluginBridge: Loaded plugin: " + getPluginName()14 unmodified lines15161718192021222324252627282930313233343536373839404142434445464748495051522 unmodified lines5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001 unmodified line1021031041051061071081091101111121131 unmodified line11511611711811912012112212312412512612714 unmodified linesunloadPlugin();}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-processjuce::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 loadablejuce::File binary;if (pluginFile.getFileExtension() == ".vst3")binary = pluginFile.getChildFile("Contents/MacOS").findChildFiles(juce::File::findFiles, false).getFirst();elsebinary = 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' commandif (!validator.start("file " + binary.getFullPathName().quoted()))return false;validator.waitForProcessToFinish(2000); // 2 second timeoutauto output = validator.readAllProcessOutput();// If it contains "Mach-O" it's at least a valid binaryreturn output.contains("Mach-O");}void HostedPluginManager::loadPlugin(const juce::File& pluginFile,double sampleRate, int blockSize){2 unmodified linescurrentSampleRate = 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 exceptionsfor (int i = 0; i < formatManager.getNumFormats(); ++i){auto* format = formatManager.getFormat(i);// Only try the format that matches the file extensionbool 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)1 unmodified lineif (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());1 unmodified lineif (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()
Source/HostedPluginManager.cpp+61-20