1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* Copyright (C) 2019-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/windows/os_library_win.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/os_interface/windows/sys_calls.h"
#include <iomanip>
#include <regex>
#include <sstream>
namespace NEO {
OsLibrary *OsLibrary::load(const OsLibraryCreateProperties &properties) {
Windows::OsLibrary *ptr = new Windows::OsLibrary(properties);
if (!ptr->isLoaded()) {
delete ptr;
return nullptr;
}
return ptr;
}
const std::string OsLibrary::createFullSystemPath(const std::string &name) {
CHAR buff[MAX_PATH];
UINT ret = 0;
ret = Windows::OsLibrary::getSystemDirectoryA(buff, MAX_PATH);
buff[ret] = '\\';
buff[ret + 1] = 0;
strncat_s(&buff[0], sizeof(buff), name.c_str(), _TRUNCATE);
return std::string(buff);
}
namespace Windows {
decltype(&GetModuleHandleA) OsLibrary::getModuleHandleA = GetModuleHandleA;
decltype(&LoadLibraryExA) OsLibrary::loadLibraryExA = LoadLibraryExA;
decltype(&GetModuleFileNameA) OsLibrary::getModuleFileNameA = GetModuleFileNameA;
decltype(&GetSystemDirectoryA) OsLibrary::getSystemDirectoryA = GetSystemDirectoryA;
decltype(&FreeLibrary) OsLibrary::freeLibrary = FreeLibrary;
extern "C" IMAGE_DOS_HEADER __ImageBase; // NOLINT(readability-identifier-naming)
__inline HINSTANCE getModuleHINSTANCE() { return (HINSTANCE)&__ImageBase; }
void OsLibrary::getLastErrorString(std::string *errorValue) {
DWORD errorID = GetLastError();
if (errorID && errorValue != nullptr) {
LPSTR tempErrorMessage = nullptr;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&tempErrorMessage, 0, NULL);
errorValue->assign(tempErrorMessage);
LocalFree(tempErrorMessage);
}
}
HMODULE OsLibrary::loadDependency(const std::string &dependencyFileName) const {
char dllPath[MAX_PATH];
DWORD length = getModuleFileNameA(getModuleHINSTANCE(), dllPath, MAX_PATH);
for (DWORD idx = length; idx > 0; idx--) {
if (dllPath[idx - 1] == '\\') {
dllPath[idx] = '\0';
break;
}
}
strcat_s(dllPath, MAX_PATH, dependencyFileName.c_str());
return loadLibraryExA(dllPath, NULL, 0);
}
OsLibrary::OsLibrary(const OsLibraryCreateProperties &properties) {
if (properties.libraryName.empty()) {
this->handle = getModuleHandleA(nullptr);
this->selfOpen = true;
} else {
if (properties.performSelfLoad) {
this->handle = getModuleHandleA(properties.libraryName.c_str());
this->selfOpen = true;
} else {
this->handle = loadDependency(properties.libraryName);
if (this->handle == nullptr) {
this->handle = loadLibraryExA(properties.libraryName.c_str(), NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
}
}
if ((this->handle == nullptr) && (properties.errorValue != nullptr)) {
getLastErrorString(properties.errorValue);
}
}
}
OsLibrary::~OsLibrary() {
if (!this->selfOpen && this->handle) {
freeLibrary(this->handle);
this->handle = nullptr;
}
}
bool OsLibrary::isLoaded() {
return this->handle != nullptr;
}
void *OsLibrary::getProcAddress(const std::string &procName) {
return reinterpret_cast<void *>(::GetProcAddress(this->handle, procName.c_str()));
}
std::string OsLibrary::getFullPath() {
char dllPath[MAX_PATH];
getModuleFileNameA(getModuleHINSTANCE(), dllPath, MAX_PATH);
return std::string(dllPath);
}
} // namespace Windows
bool getLoadedLibVersion(const std::string &libName, const std::string ®exVersionPattern, std::string &outVersion, std::string &errReason) {
HMODULE mod = NULL;
auto ret = SysCalls::getModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, std::wstring(libName.begin(), libName.end()).c_str(), &mod);
if (0 == ret) {
errReason = "Failed to read info of " + libName + " - GetModuleHandleExA failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
wchar_t path[MAX_PATH];
DWORD length = SysCalls::getModuleFileNameW(mod, path, MAX_PATH);
if (0 == length) {
errReason = "Failed to read info of " + libName + " - GetModuleFileName failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
std::wstring trimmedPath = {path, length};
DWORD infoVersioSize = SysCalls::getFileVersionInfoSizeW(trimmedPath.c_str(), nullptr);
if (0 == infoVersioSize) {
errReason = "Failed to read info of " + libName + " - GetFileVersionInfoSize failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
std::vector<char> fileInformationBackingStorage;
fileInformationBackingStorage.resize(infoVersioSize);
ret = SysCalls::getFileVersionInfoW(path, 0, static_cast<DWORD>(fileInformationBackingStorage.size()), fileInformationBackingStorage.data());
if (0 == ret) {
errReason = "Failed to read info of " + libName + " - GetFileVersionInfo failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
struct LangCodePage {
WORD lang;
WORD codePage;
};
LangCodePage *translateInfo = nullptr;
unsigned int langCodePagesSize = 0;
ret = SysCalls::verQueryValueW(fileInformationBackingStorage.data(), L"\\VarFileInfo\\Translation", reinterpret_cast<LPVOID *>(&translateInfo), &langCodePagesSize);
if (0 == ret) {
errReason = "Failed to read info of " + libName + " - VerQueryValue(\\VarFileInfo\\Translation) failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
auto truncateWstringToString = [](const std::wstring &ws) {
std::string ret;
std::transform(ws.begin(), ws.end(), std::back_inserter(ret), [](wchar_t wc) { return static_cast<char>(wc); });
return ret;
};
size_t langCodePagesCount = (langCodePagesSize / sizeof(LangCodePage));
std::regex versionPattern{regexVersionPattern};
for (size_t j = 0; j < langCodePagesCount; ++j) {
std::wstringstream subBlockPath;
subBlockPath << L"\\StringFileInfo\\";
subBlockPath << std::setw(4) << std::setfill(L'0') << std::hex << translateInfo[j].lang;
subBlockPath << std::setw(4) << std::setfill(L'0') << std::hex << translateInfo[j].codePage;
subBlockPath << L"\\ProductVersion";
wchar_t *data;
unsigned int len = 0;
ret = SysCalls::verQueryValueW(fileInformationBackingStorage.data(), subBlockPath.str().c_str(), (LPVOID *)&data, &len);
if (0 == ret) {
errReason = "Failed to read info of " + libName + " - VerQueryValue(" + truncateWstringToString(subBlockPath.str()) + ") failed, GetLastError=" + std::to_string(SysCalls::getLastError());
return false;
}
auto sdata = truncateWstringToString(data);
if (std::regex_search(sdata, versionPattern)) {
outVersion = std::move(sdata);
return true;
}
}
errReason = "Could not find version info for " + std::string(libName) + " that would satisfy expected pattern\n";
return false;
}
} // namespace NEO
|