feature: Add logic for reading loaded library's version

getLoadedLibVersion is intended for reading version of a library that was
already loaded into the process prior to this call (i.e. new instance of
the library willnot be loaded).
Note: Currently, only windows version of this functionality is implemented.
Related-To: GSD-10248

Signed-off-by: Chodor, Jaroslaw <jaroslaw.chodor@intel.com>
This commit is contained in:
Chodor, Jaroslaw
2024-12-19 16:56:46 +00:00
committed by Compute-Runtime-Automation
parent db3f268a97
commit dd3d5c6460
11 changed files with 477 additions and 6 deletions

View File

@@ -31,6 +31,10 @@ const std::string OsLibrary::createFullSystemPath(const std::string &name) {
return name;
}
bool getLoadedLibVersion(const std::string &libName, const std::string &regexVersionPattern, std::string &outVersion, std::string &errReason) {
return false;
}
namespace Linux {
OsLibrary::OsLibrary(const OsLibraryCreateProperties &properties) {

View File

@@ -51,4 +51,7 @@ class OsLibrary {
virtual bool isLoaded() = 0;
virtual std::string getFullPath() = 0;
};
bool getLoadedLibVersion(const std::string &libName, const std::string &regexVersionPattern, std::string &outVersion, std::string &errReason);
} // namespace NEO

View File

@@ -7,6 +7,13 @@
#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) {
@@ -109,4 +116,82 @@ std::string OsLibrary::getFullPath() {
return std::string(dllPath);
}
} // namespace Windows
bool getLoadedLibVersion(const std::string &libName, const std::string &regexVersionPattern, 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 = sdata;
return true;
}
}
errReason = "Could not find version info for " + std::string(libName) + " that would satisfy expected pattern\n";
return false;
}
} // namespace NEO

View File

@@ -180,5 +180,22 @@ BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) {
SIZE_T virtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength) {
return VirtualQuery(lpAddress, lpBuffer, dwLength);
}
BOOL getModuleHandleExW(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule) {
return ::GetModuleHandleExW(dwFlags, lpModuleName, phModule);
}
DWORD getModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize) {
return ::GetModuleFileNameW(hModule, lpFilename, nSize);
}
DWORD getFileVersionInfoSizeW(LPCWSTR lptstrFilename, LPDWORD lpdwHandle) {
return ::GetFileVersionInfoSizeW(lptstrFilename, lpdwHandle);
}
BOOL getFileVersionInfoW(LPCWSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData) {
return ::GetFileVersionInfoW(lptstrFilename, dwHandle, dwLen, lpData);
}
BOOL verQueryValueW(LPCVOID pBlock, LPCWSTR lpSubBlock, LPVOID *lpBuffer, PUINT puLen) {
return ::VerQueryValueW(pBlock, lpSubBlock, lpBuffer, puLen);
}
} // namespace SysCalls
} // namespace NEO

View File

@@ -59,6 +59,13 @@ CONFIGRET cmGetDeviceInterfaceList(LPGUID interfaceClassGuid, DEVINSTID_W pDevic
LPVOID heapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
BOOL heapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
SIZE_T virtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);
BOOL getModuleHandleExW(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule);
DWORD getModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
DWORD getFileVersionInfoSizeW(LPCWSTR lptstrFilename, LPDWORD lpdwHandle);
BOOL getFileVersionInfoW(LPCWSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData);
BOOL verQueryValueW(LPCVOID pBlock, LPCWSTR lpSubBlock, LPVOID *lplpBuffer, PUINT puLen);
DWORD getLastError();
} // namespace SysCalls
} // namespace NEO