| 1 |
/* |
| 2 |
* Copyright (c) 2025 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2025 Valve Corporation |
| 4 |
* Copyright (c) 2025 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "dynamic_library_wrapper.h" |
| 28 |
|
| 29 |
#include "test_defines.h" |
| 30 |
#include "env_var_wrapper.h" |
| 31 |
|
| 32 |
#include <assert.h> |
| 33 |
#include <string.h> |
| 34 |
#include <stdio.h> |
| 35 |
|
| 36 |
#include <iostream> |
| 37 |
|
| 38 |
namespace detail { |
| 39 |
|
| 40 |
#if defined(_WIN32) |
| 41 |
typedef HMODULE test_platform_dl_handle; |
| 42 |
|
| 43 |
std::string error_code_to_string(DWORD error_code) { |
| 44 |
LPSTR lpMsgBuf{}; |
| 45 |
size_t MsgBufSize = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 46 |
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpMsgBuf, 0, nullptr); |
| 47 |
std::string code_str(lpMsgBuf, MsgBufSize); |
| 48 |
LocalFree(lpMsgBuf); |
| 49 |
return code_str; |
| 50 |
} |
| 51 |
std::wstring wide_error_code_to_string(DWORD error_code) { |
| 52 |
LPWSTR lpwMsgBuf{}; |
| 53 |
size_t MsgBufSize = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 54 |
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), lpwMsgBuf, 0, nullptr); |
| 55 |
std::wstring code_str(lpwMsgBuf, MsgBufSize); |
| 56 |
LocalFree(lpwMsgBuf); |
| 57 |
return code_str; |
| 58 |
} |
| 59 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 60 |
#include <dlfcn.h> |
| 61 |
|
| 62 |
typedef void* test_platform_dl_handle; |
| 63 |
#endif |
| 64 |
} // namespace detail |
| 65 |
|
| 66 |
LibraryWrapper::LibraryWrapper() noexcept {} |
| 67 |
LibraryWrapper::LibraryWrapper(std::filesystem::path const& lib_path) noexcept : lib_path(lib_path.lexically_normal()) { |
| 68 |
#if defined(_WIN32) |
| 69 |
auto wide_path = lib_path.native(); // Get a std::wstring first |
| 70 |
// Try loading the library the original way first. |
| 71 |
lib_handle = LoadLibraryW(wide_path.c_str()); |
| 72 |
DWORD last_error = GetLastError(); |
| 73 |
// If that failed because of path limitations, prepend a special sequence to opt into longer path support |
| 74 |
if (lib_handle == nullptr && last_error == ERROR_FILENAME_EXCED_RANGE) { |
| 75 |
// "\\?\" is a special prefix for filenames that tells winapi to ignore path limits, so we can workaround path limitations |
| 76 |
// without relying on system level changes. |
| 77 |
wide_path = L"\\\\?\\" + wide_path; |
| 78 |
last_error = 0; |
| 79 |
lib_handle = LoadLibraryW(wide_path.c_str()); |
| 80 |
last_error = GetLastError(); |
| 81 |
} |
| 82 |
// If that failed, then try loading it with broader search folders. |
| 83 |
if (lib_handle == nullptr && last_error == ERROR_MOD_NOT_FOUND) { |
| 84 |
last_error = 0; |
| 85 |
lib_handle = |
| 86 |
LoadLibraryExW(wide_path.c_str(), nullptr, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR); |
| 87 |
last_error = GetLastError(); |
| 88 |
} |
| 89 |
// If neither of the above worked, print an error message and abort |
| 90 |
if (lib_handle == nullptr) { |
| 91 |
auto error_msg = detail::wide_error_code_to_string(last_error); |
| 92 |
std::wcerr << L"Unable to open library: " << wide_path << L" due to: " << error_msg << L"\n"; |
| 93 |
abort(); |
| 94 |
} |
| 95 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 96 |
lib_handle = dlopen(lib_path.string().c_str(), RTLD_LAZY | RTLD_LOCAL); |
| 97 |
if (lib_handle == nullptr) { |
| 98 |
std::wcerr << "Unable to open library: " << lib_path << " due to: " << dlerror() << "\n"; |
| 99 |
abort(); |
| 100 |
} |
| 101 |
#else |
| 102 |
#error "Unhandled platform in dynamic_library_wrapper.cpp!" |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
LibraryWrapper::~LibraryWrapper() noexcept { close_library(); } |
| 107 |
|
| 108 |
LibraryWrapper::LibraryWrapper(LibraryWrapper&& wrapper) noexcept : lib_handle(wrapper.lib_handle), lib_path(wrapper.lib_path) { |
| 109 |
wrapper.lib_handle = nullptr; |
| 110 |
} |
| 111 |
LibraryWrapper& LibraryWrapper::operator=(LibraryWrapper&& wrapper) noexcept { |
| 112 |
if (this != &wrapper) { |
| 113 |
lib_handle = wrapper.lib_handle; |
| 114 |
lib_path = wrapper.lib_path; |
| 115 |
wrapper.lib_handle = nullptr; |
| 116 |
} |
| 117 |
return *this; |
| 118 |
} |
| 119 |
|
| 120 |
void LibraryWrapper::close_library() noexcept { |
| 121 |
auto loader_disable_dynamic_library_unloading_env_var = get_env_var("VK_LOADER_DISABLE_DYNAMIC_LIBRARY_UNLOADING", false); |
| 122 |
if (loader_disable_dynamic_library_unloading_env_var.empty() || loader_disable_dynamic_library_unloading_env_var != "1") { |
| 123 |
if (lib_handle != nullptr) { |
| 124 |
#if defined(_WIN32) |
| 125 |
FreeLibrary(lib_handle); |
| 126 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 127 |
dlclose(lib_handle); |
| 128 |
#else |
| 129 |
#error "Unhandled platform in dynamic_library_wrapper.cpp!" |
| 130 |
#endif |
| 131 |
lib_handle = nullptr; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
FromVoidStarFunc LibraryWrapper::get_symbol(const char* symbol_name) const { |
| 137 |
if (symbol_name == nullptr) { |
| 138 |
std::cerr << "LibraryWrapper::get_symbol called with a null symbol_name!\n"; |
| 139 |
abort(); |
| 140 |
} |
| 141 |
if (lib_handle == nullptr) { |
| 142 |
std::cerr << "LibraryWrapper::get_symbol called when lib_handle is nullptr!\n"; |
| 143 |
abort(); |
| 144 |
} |
| 145 |
|
| 146 |
#if defined(_WIN32) |
| 147 |
void* symbol = reinterpret_cast<void*>(GetProcAddress(lib_handle, symbol_name)); |
| 148 |
if (symbol == nullptr) { |
| 149 |
auto last_error = detail::error_code_to_string(GetLastError()); |
| 150 |
std::cerr << "Unable to get symbol " << symbol_name << " in library " << lib_path.string() << " with to error code " |
| 151 |
<< last_error << "\n"; |
| 152 |
abort(); |
| 153 |
} |
| 154 |
#elif TESTING_COMMON_UNIX_PLATFORMS |
| 155 |
void* symbol = dlsym(lib_handle, symbol_name); |
| 156 |
if (symbol == nullptr) { |
| 157 |
std::cerr << "Unable to get symbol " << symbol_name << " in library " << lib_path.string() << " due to " << dlerror() |
| 158 |
<< "\n"; |
| 159 |
abort(); |
| 160 |
} |
| 161 |
#else |
| 162 |
#error "Unhandled platform in dynamic_library_wrapper.cpp!" |
| 163 |
#endif |
| 164 |
return FromVoidStarFunc(symbol); |
| 165 |
} |