annotate src/helper.cpp @ 41:7f5cf0fed473 errorcode

Merge from default branch
author Chris Cannam
date Wed, 29 Aug 2018 17:40:36 +0100
parents 40c6936c2fc9
children 49946b02414e
rev   line source
Chris@2 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /**
Chris@28 4 * [Vamp] Plugin Load Checker
Chris@0 5 *
Chris@4 6 * This program accepts the name of a descriptor symbol as its only
Chris@1 7 * command-line argument. It then reads a list of plugin library paths
Chris@1 8 * from stdin, one per line. For each path read, it attempts to load
Chris@4 9 * that library and retrieve the named descriptor symbol, printing a
Chris@1 10 * line to stdout reporting whether this was successful or not and
Chris@1 11 * then flushing stdout. The output line format is described
Chris@1 12 * below. The program exits with code 0 if all libraries were loaded
Chris@1 13 * successfully and non-zero otherwise.
Chris@0 14 *
Chris@0 15 * Note that library paths must be ready to pass to dlopen() or
Chris@0 16 * equivalent; this usually means they should be absolute paths.
Chris@0 17 *
Chris@0 18 * Output line for successful load of library libname.so:
Chris@0 19 * SUCCESS|/path/to/libname.so|
Chris@0 20 *
Chris@0 21 * Output line for failed load of library libname.so:
Chris@40 22 * FAILURE|/path/to/libname.so|Error message [failureCode]
Chris@40 23 *
Chris@40 24 * or:
Chris@40 25 * FAILURE|/path/to/libname.so|[failureCode]
Chris@40 26 *
Chris@40 27 * where the error message is an optional system-level message, such
Chris@40 28 * as may be returned from strerror or similar (which should be in the
Chris@40 29 * native language for the system ready to show the user), and the
Chris@40 30 * failureCode in square brackets is a mandatory number corresponding
Chris@40 31 * to one of the PluginCandidates::FailureCode values (requiring
Chris@40 32 * conversion to a translated string by the client).
Chris@0 33 *
Chris@28 34 * Although this program was written for use with Vamp audio analysis
Chris@28 35 * plugins, it also works with other plugin formats. The program has
Chris@28 36 * some hardcoded knowledge of Vamp, LADSPA, and DSSI plugins, but it
Chris@28 37 * can be used with any plugins that involve loading DLLs and looking
Chris@28 38 * up descriptor functions from them.
Chris@28 39 *
Chris@2 40 * Sometimes plugins will crash completely on load, bringing down this
Chris@2 41 * program with them. If the program exits before all listed plugins
Chris@2 42 * have been checked, this means that the plugin following the last
Chris@2 43 * reported one has crashed. Typically the caller may want to run it
Chris@2 44 * again, omitting that plugin.
Chris@0 45 */
Chris@0 46
Chris@5 47 /*
Chris@28 48 Copyright (c) 2016-2017 Queen Mary, University of London
Chris@5 49
Chris@5 50 Permission is hereby granted, free of charge, to any person
Chris@5 51 obtaining a copy of this software and associated documentation
Chris@5 52 files (the "Software"), to deal in the Software without
Chris@5 53 restriction, including without limitation the rights to use, copy,
Chris@5 54 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@5 55 of the Software, and to permit persons to whom the Software is
Chris@5 56 furnished to do so, subject to the following conditions:
Chris@5 57
Chris@5 58 The above copyright notice and this permission notice shall be
Chris@5 59 included in all copies or substantial portions of the Software.
Chris@5 60
Chris@5 61 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@5 62 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@5 63 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@5 64 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
Chris@5 65 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@5 66 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@5 67 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@5 68
Chris@5 69 Except as contained in this notice, the names of the Centre for
Chris@5 70 Digital Music and Queen Mary, University of London shall not be
Chris@5 71 used in advertising or otherwise to promote the sale, use or other
Chris@5 72 dealings in this Software without prior written authorization.
Chris@5 73 */
Chris@5 74
Chris@27 75 #include "../version.h"
Chris@27 76
Chris@40 77 #include "../checker/checkcode.h"
Chris@40 78
Chris@28 79 static const char programName[] = "vamp-plugin-load-checker";
Chris@28 80
Chris@0 81 #ifdef _WIN32
Chris@0 82 #include <windows.h>
Chris@0 83 #include <process.h>
Chris@38 84 #endif
Chris@38 85
Chris@10 86 #include <string>
Chris@38 87 #include <iostream>
Chris@38 88
Chris@38 89 #ifdef _WIN32
Chris@38 90 #ifndef UNICODE
Chris@38 91 #error "This must be compiled with UNICODE defined"
Chris@38 92 #endif
Chris@40 93
Chris@18 94 static std::string lastLibraryName = "";
Chris@40 95
Chris@10 96 static HMODULE LoadLibraryUTF8(std::string name) {
Chris@18 97 lastLibraryName = name;
Chris@10 98 int n = name.size();
Chris@16 99 int wn = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), n, 0, 0);
Chris@16 100 wchar_t *wname = new wchar_t[wn+1];
Chris@16 101 wn = MultiByteToWideChar(CP_UTF8, 0, name.c_str(), n, wname, wn);
Chris@16 102 wname[wn] = L'\0';
Chris@10 103 HMODULE h = LoadLibraryW(wname);
Chris@10 104 delete[] wname;
Chris@10 105 return h;
Chris@10 106 }
Chris@40 107
Chris@10 108 static std::string GetErrorText() {
Chris@38 109 DWORD err = GetLastError();
Chris@10 110 wchar_t *buffer;
Chris@38 111 FormatMessageW(
Chris@10 112 FORMAT_MESSAGE_ALLOCATE_BUFFER |
Chris@10 113 FORMAT_MESSAGE_FROM_SYSTEM |
Chris@10 114 FORMAT_MESSAGE_IGNORE_INSERTS,
Chris@10 115 NULL,
Chris@10 116 err,
Chris@40 117 MAKELANGID(LANG_USER_DEFAULT, SUBLANG_USER_DEFAULT),
Chris@38 118 (LPWSTR) &buffer,
Chris@10 119 0, NULL );
Chris@16 120 int wn = wcslen(buffer);
Chris@16 121 int n = WideCharToMultiByte(CP_UTF8, 0, buffer, wn, 0, 0, 0, 0);
Chris@17 122 if (n < 0) {
Chris@17 123 LocalFree(&buffer);
Chris@17 124 return "Unable to convert error string (internal error)";
Chris@17 125 }
Chris@16 126 char *text = new char[n+1];
Chris@17 127 (void)WideCharToMultiByte(CP_UTF8, 0, buffer, wn, text, n, 0, 0);
Chris@16 128 text[n] = '\0';
Chris@10 129 std::string s(text);
Chris@10 130 LocalFree(&buffer);
Chris@10 131 delete[] text;
Chris@10 132 for (int i = s.size(); i > 0; ) {
Chris@10 133 --i;
Chris@10 134 if (s[i] == '\n' || s[i] == '\r') {
Chris@10 135 s.erase(i, 1);
Chris@10 136 }
Chris@10 137 }
Chris@18 138 std::size_t pos = s.find("%1");
Chris@18 139 if (pos != std::string::npos && lastLibraryName != "") {
Chris@18 140 s.replace(pos, 2, lastLibraryName);
Chris@18 141 }
Chris@10 142 return s;
Chris@10 143 }
Chris@40 144
Chris@10 145 #define DLOPEN(a,b) LoadLibraryUTF8(a)
Chris@10 146 #define DLSYM(a,b) (void *)GetProcAddress((HINSTANCE)(a),(b).c_str())
Chris@0 147 #define DLCLOSE(a) (!FreeLibrary((HINSTANCE)(a)))
Chris@10 148 #define DLERROR() (GetErrorText())
Chris@40 149
Chris@0 150 #else
Chris@40 151
Chris@0 152 #include <dlfcn.h>
Chris@0 153 #define DLOPEN(a,b) dlopen((a).c_str(),(b))
Chris@0 154 #define DLSYM(a,b) dlsym((a),(b).c_str())
Chris@0 155 #define DLCLOSE(a) dlclose((a))
Chris@0 156 #define DLERROR() dlerror()
Chris@40 157
Chris@0 158 #endif
Chris@0 159
Chris@12 160 //#include <unistd.h>
Chris@6 161
Chris@0 162 using namespace std;
Chris@0 163
Chris@0 164 string error()
Chris@0 165 {
Chris@40 166 return DLERROR();
Chris@0 167 }
Chris@0 168
Chris@40 169 struct Result {
Chris@40 170 PluginCheckCode code;
Chris@40 171 string message;
Chris@40 172 };
Chris@40 173
Chris@40 174 Result checkLADSPAStyleDescriptorFn(void *f)
Chris@23 175 {
Chris@23 176 typedef const void *(*DFn)(unsigned long);
Chris@23 177 DFn fn = DFn(f);
Chris@23 178 unsigned long index = 0;
Chris@23 179 while (fn(index)) ++index;
Chris@40 180 if (index == 0) return { PluginCheckCode::FAIL_NO_PLUGINS, "" };
Chris@40 181 return { PluginCheckCode::SUCCESS, "" };
Chris@23 182 }
Chris@23 183
Chris@40 184 Result checkVampDescriptorFn(void *f)
Chris@23 185 {
Chris@23 186 typedef const void *(*DFn)(unsigned int, unsigned int);
Chris@23 187 DFn fn = DFn(f);
Chris@23 188 unsigned int index = 0;
Chris@25 189 while (fn(2, index)) ++index;
Chris@40 190 if (index == 0) return { PluginCheckCode::FAIL_NO_PLUGINS, "" };
Chris@40 191 return { PluginCheckCode::SUCCESS, "" };
Chris@23 192 }
Chris@23 193
Chris@40 194 Result check(string soname, string descriptor)
Chris@0 195 {
Chris@0 196 void *handle = DLOPEN(soname, RTLD_NOW | RTLD_LOCAL);
Chris@0 197 if (!handle) {
Chris@40 198 PluginCheckCode code = PluginCheckCode::FAIL_NOT_LOADABLE;
Chris@40 199 string message = error();
Chris@40 200 #ifdef _WIN32
Chris@40 201 DWORD err = GetLastError();
Chris@40 202 if (err == ERROR_BAD_EXE_FORMAT) {
Chris@40 203 code = PluginCheckCode::FAIL_WRONG_ARCHITECTURE;
Chris@40 204 } else if (err == ERROR_MOD_NOT_FOUND) {
Chris@40 205 code = PluginCheckCode::FAIL_DEPENDENCY_MISSING;
Chris@40 206 }
Chris@40 207 #endif
Chris@40 208 return { code, message };
Chris@0 209 }
Chris@0 210
Chris@40 211 Result result { PluginCheckCode::SUCCESS, "" };
Chris@38 212
Chris@0 213 void *fn = DLSYM(handle, descriptor);
Chris@0 214 if (!fn) {
Chris@40 215 result = { PluginCheckCode::FAIL_DESCRIPTOR_MISSING, error() };
Chris@38 216 } else if (descriptor == "ladspa_descriptor") {
Chris@40 217 result = checkLADSPAStyleDescriptorFn(fn);
Chris@23 218 } else if (descriptor == "dssi_descriptor") {
Chris@40 219 result = checkLADSPAStyleDescriptorFn(fn);
Chris@23 220 } else if (descriptor == "vampGetPluginDescriptor") {
Chris@40 221 result = checkVampDescriptorFn(fn);
Chris@23 222 } else {
Chris@23 223 cerr << "Note: no descriptor logic known for descriptor function \""
Chris@23 224 << descriptor << "\"; not actually calling it" << endl;
Chris@23 225 }
Chris@38 226
Chris@38 227 DLCLOSE(handle);
Chris@23 228
Chris@40 229 return result;
Chris@0 230 }
Chris@0 231
Chris@0 232 int main(int argc, char **argv)
Chris@0 233 {
Chris@0 234 bool allGood = true;
Chris@0 235 string soname;
Chris@0 236
Chris@27 237 bool showUsage = false;
Chris@27 238
Chris@27 239 if (argc > 1) {
Chris@27 240 string opt = argv[1];
Chris@27 241 if (opt == "-?" || opt == "-h" || opt == "--help") {
Chris@27 242 showUsage = true;
Chris@27 243 } else if (opt == "-v" || opt == "--version") {
Chris@40 244 cout << CHECKER_COMPATIBILITY_VERSION << endl;
Chris@27 245 return 0;
Chris@27 246 }
Chris@27 247 }
Chris@27 248
Chris@27 249 if (argc != 2 || showUsage) {
Chris@27 250 cerr << endl;
Chris@28 251 cerr << programName << ": Test shared library objects for plugins to be" << endl;
Chris@27 252 cerr << "loaded via descriptor functions." << endl;
Chris@28 253 cerr << "\n Usage: " << programName << " <descriptorname>\n"
Chris@11 254 "\nwhere descriptorname is the name of a plugin descriptor symbol to be sought\n"
Chris@11 255 "in each library (e.g. vampGetPluginDescriptor for Vamp plugins). The list of\n"
Chris@11 256 "candidate plugin library filenames is read from stdin.\n" << endl;
Chris@11 257 return 2;
Chris@1 258 }
Chris@1 259
Chris@1 260 string descriptor = argv[1];
Chris@1 261
Chris@32 262 #ifdef _WIN32
Chris@32 263 // Avoid showing the error-handler dialog for missing DLLs,
Chris@32 264 // failing quietly instead. It's permissible for this program
Chris@32 265 // to simply fail when a DLL can't be loaded -- showing the
Chris@32 266 // error dialog wouldn't change this anyway, it would just
Chris@32 267 // block the program until the user clicked it away and then
Chris@32 268 // fail anyway.
Chris@32 269 SetErrorMode(SEM_FAILCRITICALERRORS);
Chris@32 270 #endif
Chris@32 271
Chris@0 272 while (getline(cin, soname)) {
Chris@40 273 Result result = check(soname, descriptor);
Chris@40 274 if (result.code == PluginCheckCode::SUCCESS) {
Chris@40 275 cout << "SUCCESS|" << soname << "|" << endl;
Chris@40 276 } else {
Chris@40 277 if (result.message == "") {
Chris@40 278 cout << "FAILURE|" << soname
Chris@40 279 << "|[" << int(result.code) << "]" << endl;
Chris@40 280 } else {
Chris@40 281 cout << "FAILURE|" << soname
Chris@40 282 << "|" << result.message << " ["
Chris@40 283 << int(result.code) << "]" << endl;
Chris@40 284 }
Chris@11 285 allGood = false;
Chris@11 286 }
Chris@0 287 }
Chris@11 288
Chris@0 289 return allGood ? 0 : 1;
Chris@0 290 }