| 1 |
/* |
| 2 |
* Copyright (c) 2021 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2021 Valve Corporation |
| 4 |
* Copyright (c) 2021 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 |
* Author: Charles Giessen <[email protected]> |
| 26 |
*/ |
| 27 |
|
| 28 |
// This needs to be defined first, or else we'll get redefinitions on NTSTATUS values |
| 29 |
#if defined(_WIN32) |
| 30 |
#define UMDF_USING_NTSTATUS |
| 31 |
#include <ntstatus.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <iostream> |
| 35 |
|
| 36 |
#include <assert.h> |
| 37 |
|
| 38 |
#include <windows.h> |
| 39 |
#include <debugapi.h> |
| 40 |
|
| 41 |
#include "shim.h" |
| 42 |
|
| 43 |
#include "detours.h" |
| 44 |
|
| 45 |
#include "util/dynamic_library_wrapper.h" |
| 46 |
#include "util/wide_char_handling.h" |
| 47 |
|
| 48 |
static PlatformShim platform_shim; |
| 49 |
|
| 50 |
extern "C" { |
| 51 |
|
| 52 |
static LibraryWrapper gdi32_dll; |
| 53 |
|
| 54 |
using PFN_GetSidSubAuthority = PDWORD(__stdcall *)(PSID pSid, DWORD nSubAuthority); |
| 55 |
static PFN_GetSidSubAuthority fpGetSidSubAuthority = GetSidSubAuthority; |
| 56 |
|
| 57 |
PDWORD __stdcall ShimGetSidSubAuthority(PSID, DWORD) { return &platform_shim.elevation_level; } |
| 58 |
|
| 59 |
static PFN_LoaderEnumAdapters2 fpEnumAdapters2 = nullptr; |
| 60 |
static PFN_LoaderQueryAdapterInfo fpQueryAdapterInfo = nullptr; |
| 61 |
|
| 62 |
NTSTATUS APIENTRY ShimEnumAdapters2(LoaderEnumAdapters2 *adapters) { |
| 63 |
if (adapters == nullptr) { |
| 64 |
return STATUS_INVALID_PARAMETER; |
| 65 |
} |
| 66 |
if (platform_shim.d3dkmt_adapters.size() == 0) { |
| 67 |
if (adapters->adapters != nullptr) adapters->adapter_count = 0; |
| 68 |
return STATUS_SUCCESS; |
| 69 |
} |
| 70 |
if (adapters->adapters != nullptr) { |
| 71 |
for (size_t i = 0; i < platform_shim.d3dkmt_adapters.size(); i++) { |
| 72 |
adapters->adapters[i].handle = platform_shim.d3dkmt_adapters[i].hAdapter; |
| 73 |
adapters->adapters[i].luid = platform_shim.d3dkmt_adapters[i].adapter_luid; |
| 74 |
} |
| 75 |
adapters->adapter_count = static_cast<ULONG>(platform_shim.d3dkmt_adapters.size()); |
| 76 |
} else { |
| 77 |
adapters->adapter_count = static_cast<ULONG>(platform_shim.d3dkmt_adapters.size()); |
| 78 |
} |
| 79 |
return STATUS_SUCCESS; |
| 80 |
} |
| 81 |
NTSTATUS APIENTRY ShimQueryAdapterInfo(const LoaderQueryAdapterInfo *query_info) { |
| 82 |
if (query_info == nullptr || query_info->private_data == nullptr) { |
| 83 |
return STATUS_INVALID_PARAMETER; |
| 84 |
} |
| 85 |
auto handle = query_info->handle; |
| 86 |
auto it = std::find_if(platform_shim.d3dkmt_adapters.begin(), platform_shim.d3dkmt_adapters.end(), |
| 87 |
[handle](D3DKMT_Adapter const &adapter) { return handle == adapter.hAdapter; }); |
| 88 |
if (it == platform_shim.d3dkmt_adapters.end()) { |
| 89 |
return STATUS_INVALID_PARAMETER; |
| 90 |
} |
| 91 |
auto &adapter = *it; |
| 92 |
auto *reg_info = reinterpret_cast<LoaderQueryRegistryInfo *>(query_info->private_data); |
| 93 |
|
| 94 |
std::vector<std::wstring> *paths = nullptr; |
| 95 |
if (wcsstr(reg_info->value_name, L"DriverName") != nullptr) { // looking for drivers |
| 96 |
paths = &adapter.driver_paths; |
| 97 |
} else if (wcsstr(reg_info->value_name, L"ImplicitLayers") != nullptr) { // looking for implicit layers |
| 98 |
paths = &adapter.implicit_layer_paths; |
| 99 |
} else if (wcsstr(reg_info->value_name, L"ExplicitLayers") != nullptr) { // looking for explicit layers |
| 100 |
paths = &adapter.explicit_layer_paths; |
| 101 |
} |
| 102 |
|
| 103 |
reg_info->status = LOADER_QUERY_REGISTRY_STATUS_SUCCESS; |
| 104 |
if (reg_info->output_value_size == 0) { |
| 105 |
// final null terminator size |
| 106 |
ULONG size = 2; |
| 107 |
|
| 108 |
// size is in bytes, so multiply path size + 1 (for null terminator) by size of wchar (basically, 2). |
| 109 |
for (auto const &path : *paths) size += static_cast<ULONG>((path.length() + 1) * sizeof(wchar_t)); |
| 110 |
reg_info->output_value_size = size; |
| 111 |
if (size != 2) { |
| 112 |
// only want to write data if there is path data to write |
| 113 |
reg_info->status = LOADER_QUERY_REGISTRY_STATUS_BUFFER_OVERFLOW; |
| 114 |
} |
| 115 |
} else if (reg_info->output_value_size > 2) { |
| 116 |
size_t index = 0; |
| 117 |
for (auto const &path : *paths) { |
| 118 |
for (auto w : path) { |
| 119 |
reg_info->output_string[index++] = w; |
| 120 |
} |
| 121 |
reg_info->output_string[index++] = L'\0'; |
| 122 |
} |
| 123 |
// make sure there is a null terminator |
| 124 |
reg_info->output_string[index++] = L'\0'; |
| 125 |
|
| 126 |
reg_info->status = LOADER_QUERY_REGISTRY_STATUS_SUCCESS; |
| 127 |
} |
| 128 |
|
| 129 |
return STATUS_SUCCESS; |
| 130 |
} |
| 131 |
|
| 132 |
// clang-format off |
| 133 |
static CONFIGRET(WINAPI *REAL_CM_Get_Device_ID_List_SizeW)(PULONG pulLen, PCWSTR pszFilter, ULONG ulFlags) = CM_Get_Device_ID_List_SizeW; |
| 134 |
static CONFIGRET(WINAPI *REAL_CM_Get_Device_ID_ListW)(PCWSTR pszFilter, PZZWSTR Buffer, ULONG BufferLen, ULONG ulFlags) = CM_Get_Device_ID_ListW; |
| 135 |
static CONFIGRET(WINAPI *REAL_CM_Locate_DevNodeW)(PDEVINST pdnDevInst, DEVINSTID_W pDeviceID, ULONG ulFlags) = CM_Locate_DevNodeW; |
| 136 |
static CONFIGRET(WINAPI *REAL_CM_Get_DevNode_Status)(PULONG pulStatus, PULONG pulProblemNumber, DEVINST dnDevInst, ULONG ulFlags) = CM_Get_DevNode_Status; |
| 137 |
static CONFIGRET(WINAPI *REAL_CM_Get_Device_IDW)(DEVINST dnDevInst, PWSTR Buffer, ULONG BufferLen, ULONG ulFlags) = CM_Get_Device_IDW; |
| 138 |
static CONFIGRET(WINAPI *REAL_CM_Get_Child)(PDEVINST pdnDevInst, DEVINST dnDevInst, ULONG ulFlags) = CM_Get_Child; |
| 139 |
static CONFIGRET(WINAPI *REAL_CM_Get_DevNode_Registry_PropertyW)(DEVINST dnDevInst, ULONG ulProperty, PULONG pulRegDataType, PVOID Buffer, PULONG pulLength, ULONG ulFlags) = CM_Get_DevNode_Registry_PropertyW; |
| 140 |
static CONFIGRET(WINAPI *REAL_CM_Get_Sibling)(PDEVINST pdnDevInst, DEVINST dnDevInst, ULONG ulFlags) = CM_Get_Sibling; |
| 141 |
// clang-format on |
| 142 |
|
| 143 |
CONFIGRET WINAPI SHIM_CM_Get_Device_ID_List_SizeW(PULONG pulLen, [[maybe_unused]] PCWSTR pszFilter, |
| 144 |
[[maybe_unused]] ULONG ulFlags) { |
| 145 |
if (pulLen == nullptr) { |
| 146 |
return CR_INVALID_POINTER; |
| 147 |
} |
| 148 |
*pulLen = static_cast<ULONG>(platform_shim.CM_device_ID_list.size()); |
| 149 |
return CR_SUCCESS; |
| 150 |
} |
| 151 |
CONFIGRET WINAPI SHIM_CM_Get_Device_ID_ListW([[maybe_unused]] PCWSTR pszFilter, PZZWSTR Buffer, ULONG BufferLen, |
| 152 |
[[maybe_unused]] ULONG ulFlags) { |
| 153 |
if (Buffer != NULL) { |
| 154 |
if (BufferLen < platform_shim.CM_device_ID_list.size()) return CR_BUFFER_SMALL; |
| 155 |
for (size_t i = 0; i < BufferLen; i++) { |
| 156 |
Buffer[i] = platform_shim.CM_device_ID_list[i]; |
| 157 |
} |
| 158 |
} |
| 159 |
return CR_SUCCESS; |
| 160 |
} |
| 161 |
// TODO |
| 162 |
CONFIGRET WINAPI SHIM_CM_Locate_DevNodeW(PDEVINST, DEVINSTID_W, ULONG) { return CR_FAILURE; } |
| 163 |
// TODO |
| 164 |
CONFIGRET WINAPI SHIM_CM_Get_DevNode_Status(PULONG, PULONG, DEVINST, ULONG) { return CR_FAILURE; } |
| 165 |
// TODO |
| 166 |
CONFIGRET WINAPI SHIM_CM_Get_Device_IDW(DEVINST, PWSTR, ULONG, ULONG) { return CR_FAILURE; } |
| 167 |
// TODO |
| 168 |
CONFIGRET WINAPI SHIM_CM_Get_Child(PDEVINST, DEVINST, ULONG) { return CR_FAILURE; } |
| 169 |
// TODO |
| 170 |
CONFIGRET WINAPI SHIM_CM_Get_DevNode_Registry_PropertyW(DEVINST, ULONG, PULONG, PVOID, PULONG, ULONG) { return CR_FAILURE; } |
| 171 |
// TODO |
| 172 |
CONFIGRET WINAPI SHIM_CM_Get_Sibling(PDEVINST, DEVINST, ULONG) { return CR_FAILURE; } |
| 173 |
|
| 174 |
static LibraryWrapper dxgi_module; |
| 175 |
typedef HRESULT(APIENTRY *PFN_CreateDXGIFactory1)(REFIID riid, void **ppFactory); |
| 176 |
|
| 177 |
PFN_CreateDXGIFactory1 RealCreateDXGIFactory1; |
| 178 |
|
| 179 |
HRESULT __stdcall ShimGetDesc1(IDXGIAdapter1 *pAdapter, |
| 180 |
/* [annotation][out] */ |
| 181 |
_Out_ DXGI_ADAPTER_DESC1 *pDesc) { |
| 182 |
if (pAdapter == nullptr || pDesc == nullptr) return DXGI_ERROR_INVALID_CALL; |
| 183 |
|
| 184 |
for (const auto &[index, adapter] : platform_shim.dxgi_adapters) { |
| 185 |
if (&adapter.adapter_instance == pAdapter) { |
| 186 |
*pDesc = adapter.desc1; |
| 187 |
return S_OK; |
| 188 |
} |
| 189 |
} |
| 190 |
return DXGI_ERROR_INVALID_CALL; |
| 191 |
} |
| 192 |
ULONG __stdcall ShimIDXGIFactory1Release(IDXGIFactory1 *) { return S_OK; } |
| 193 |
ULONG __stdcall ShimIDXGIFactory6Release(IDXGIFactory6 *) { return S_OK; } |
| 194 |
ULONG __stdcall ShimRelease(IDXGIAdapter1 *) { return S_OK; } |
| 195 |
|
| 196 |
IDXGIAdapter1 *setup_and_get_IDXGIAdapter1(DXGIAdapter &adapter) { |
| 197 |
adapter.adapter_vtbl_instance.GetDesc1 = ShimGetDesc1; |
| 198 |
adapter.adapter_vtbl_instance.Release = ShimRelease; |
| 199 |
adapter.adapter_instance.lpVtbl = &adapter.adapter_vtbl_instance; |
| 200 |
return &adapter.adapter_instance; |
| 201 |
} |
| 202 |
|
| 203 |
HRESULT __stdcall ShimEnumAdapters1_1([[maybe_unused]] IDXGIFactory1 *This, |
| 204 |
/* [in] */ UINT Adapter, |
| 205 |
/* [annotation][out] */ |
| 206 |
_COM_Outptr_ IDXGIAdapter1 **ppAdapter) { |
| 207 |
if (Adapter >= platform_shim.dxgi_adapters.size()) { |
| 208 |
return DXGI_ERROR_INVALID_CALL; |
| 209 |
} |
| 210 |
if (ppAdapter != nullptr) { |
| 211 |
*ppAdapter = setup_and_get_IDXGIAdapter1(platform_shim.dxgi_adapters.at(Adapter)); |
| 212 |
} |
| 213 |
return S_OK; |
| 214 |
} |
| 215 |
|
| 216 |
HRESULT __stdcall ShimEnumAdapters1_6([[maybe_unused]] IDXGIFactory6 *This, |
| 217 |
/* [in] */ UINT Adapter, |
| 218 |
/* [annotation][out] */ |
| 219 |
_COM_Outptr_ IDXGIAdapter1 **ppAdapter) { |
| 220 |
if (Adapter >= platform_shim.dxgi_adapters.size()) { |
| 221 |
return DXGI_ERROR_INVALID_CALL; |
| 222 |
} |
| 223 |
if (ppAdapter != nullptr) { |
| 224 |
*ppAdapter = setup_and_get_IDXGIAdapter1(platform_shim.dxgi_adapters.at(Adapter)); |
| 225 |
} |
| 226 |
return S_OK; |
| 227 |
} |
| 228 |
|
| 229 |
HRESULT __stdcall ShimEnumAdapterByGpuPreference([[maybe_unused]] IDXGIFactory6 *This, _In_ UINT Adapter, |
| 230 |
[[maybe_unused]] _In_ DXGI_GPU_PREFERENCE GpuPreference, |
| 231 |
[[maybe_unused]] _In_ REFIID riid, _COM_Outptr_ void **ppvAdapter) { |
| 232 |
if (Adapter >= platform_shim.dxgi_adapters.size()) { |
| 233 |
return DXGI_ERROR_NOT_FOUND; |
| 234 |
} |
| 235 |
// loader always uses DXGI_GPU_PREFERENCE_UNSPECIFIED |
| 236 |
// Update the shim if this isn't the case |
| 237 |
assert(GpuPreference == DXGI_GPU_PREFERENCE::DXGI_GPU_PREFERENCE_UNSPECIFIED && |
| 238 |
"Test shim assumes the GpuPreference is unspecified."); |
| 239 |
if (ppvAdapter != nullptr) { |
| 240 |
*ppvAdapter = setup_and_get_IDXGIAdapter1(platform_shim.dxgi_adapters.at(Adapter)); |
| 241 |
} |
| 242 |
return S_OK; |
| 243 |
} |
| 244 |
|
| 245 |
static IDXGIFactory1 *get_IDXGIFactory1() { |
| 246 |
static IDXGIFactory1Vtbl vtbl{}; |
| 247 |
vtbl.EnumAdapters1 = ShimEnumAdapters1_1; |
| 248 |
vtbl.Release = ShimIDXGIFactory1Release; |
| 249 |
static IDXGIFactory1 factory{}; |
| 250 |
factory.lpVtbl = &vtbl; |
| 251 |
return &factory; |
| 252 |
} |
| 253 |
|
| 254 |
static IDXGIFactory6 *get_IDXGIFactory6() { |
| 255 |
static IDXGIFactory6Vtbl vtbl{}; |
| 256 |
vtbl.EnumAdapters1 = ShimEnumAdapters1_6; |
| 257 |
vtbl.EnumAdapterByGpuPreference = ShimEnumAdapterByGpuPreference; |
| 258 |
vtbl.Release = ShimIDXGIFactory6Release; |
| 259 |
static IDXGIFactory6 factory{}; |
| 260 |
factory.lpVtbl = &vtbl; |
| 261 |
return &factory; |
| 262 |
} |
| 263 |
|
| 264 |
HRESULT __stdcall ShimCreateDXGIFactory1(REFIID riid, void **ppFactory) { |
| 265 |
if (riid == IID_IDXGIFactory1) { |
| 266 |
auto *factory = get_IDXGIFactory1(); |
| 267 |
*ppFactory = factory; |
| 268 |
return S_OK; |
| 269 |
} |
| 270 |
if (riid == IID_IDXGIFactory6) { |
| 271 |
auto *factory = get_IDXGIFactory6(); |
| 272 |
*ppFactory = factory; |
| 273 |
return S_OK; |
| 274 |
} |
| 275 |
assert(false && "new riid, update shim code to handle"); |
| 276 |
return S_FALSE; |
| 277 |
} |
| 278 |
|
| 279 |
// Windows Registry shims |
| 280 |
using PFN_RegOpenKeyExA = LSTATUS(__stdcall *)(HKEY hKey, LPCSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult); |
| 281 |
static PFN_RegOpenKeyExA fpRegOpenKeyExA = RegOpenKeyExA; |
| 282 |
using PFN_RegQueryValueExA = LSTATUS(__stdcall *)(HKEY hKey, LPCSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, |
| 283 |
LPDWORD lpcbData); |
| 284 |
static PFN_RegQueryValueExA fpRegQueryValueExA = RegQueryValueExA; |
| 285 |
using PFN_RegEnumValueA = LSTATUS(__stdcall *)(HKEY hKey, DWORD dwIndex, LPSTR lpValueName, LPDWORD lpcchValueName, |
| 286 |
LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData); |
| 287 |
static PFN_RegEnumValueA fpRegEnumValueA = RegEnumValueA; |
| 288 |
|
| 289 |
using PFN_RegCloseKey = LSTATUS(__stdcall *)(HKEY hKey); |
| 290 |
static PFN_RegCloseKey fpRegCloseKey = RegCloseKey; |
| 291 |
|
| 292 |
LSTATUS __stdcall ShimRegOpenKeyExA(HKEY hKey, LPCSTR lpSubKey, [[maybe_unused]] DWORD ulOptions, |
| 293 |
[[maybe_unused]] REGSAM samDesired, PHKEY phkResult) { |
| 294 |
if (HKEY_LOCAL_MACHINE != hKey && HKEY_CURRENT_USER != hKey) return ERROR_BADKEY; |
| 295 |
std::string hive = ""; |
| 296 |
if (HKEY_LOCAL_MACHINE == hKey) |
| 297 |
hive = "HKEY_LOCAL_MACHINE"; |
| 298 |
else if (HKEY_CURRENT_USER == hKey) |
| 299 |
hive = "HKEY_CURRENT_USER"; |
| 300 |
if (hive == "") return ERROR_ACCESS_DENIED; |
| 301 |
|
| 302 |
platform_shim.created_keys.emplace_back(platform_shim.created_key_count++, hive + "\\" + lpSubKey); |
| 303 |
*phkResult = platform_shim.created_keys.back().get(); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
const std::string *get_path_of_created_key(HKEY hKey) { |
| 307 |
for (const auto &key : platform_shim.created_keys) { |
| 308 |
if (key.key == hKey) { |
| 309 |
return &key.path; |
| 310 |
} |
| 311 |
} |
| 312 |
return nullptr; |
| 313 |
} |
| 314 |
std::vector<RegistryEntry> *get_registry_vector(std::string const &path) { |
| 315 |
if (path == "HKEY_LOCAL_MACHINE\\" VK_DRIVERS_INFO_REGISTRY_LOC) return &platform_shim.hkey_local_machine_drivers; |
| 316 |
if (path == "HKEY_LOCAL_MACHINE\\" VK_ELAYERS_INFO_REGISTRY_LOC) return &platform_shim.hkey_local_machine_explicit_layers; |
| 317 |
if (path == "HKEY_LOCAL_MACHINE\\" VK_ILAYERS_INFO_REGISTRY_LOC) return &platform_shim.hkey_local_machine_implicit_layers; |
| 318 |
if (path == "HKEY_CURRENT_USER\\" VK_ELAYERS_INFO_REGISTRY_LOC) return &platform_shim.hkey_current_user_explicit_layers; |
| 319 |
if (path == "HKEY_CURRENT_USER\\" VK_ILAYERS_INFO_REGISTRY_LOC) return &platform_shim.hkey_current_user_implicit_layers; |
| 320 |
if (path == "HKEY_LOCAL_MACHINE\\" VK_SETTINGS_INFO_REGISTRY_LOC) return &platform_shim.hkey_local_machine_settings; |
| 321 |
if (path == "HKEY_CURRENT_USER\\" VK_SETTINGS_INFO_REGISTRY_LOC) return &platform_shim.hkey_current_user_settings; |
| 322 |
return nullptr; |
| 323 |
} |
| 324 |
LSTATUS __stdcall ShimRegQueryValueExA(HKEY, LPCSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD) { |
| 325 |
// TODO: |
| 326 |
return ERROR_SUCCESS; |
| 327 |
} |
| 328 |
LSTATUS __stdcall ShimRegEnumValueA(HKEY hKey, DWORD dwIndex, LPSTR lpValueName, LPDWORD lpcchValueName, |
| 329 |
[[maybe_unused]] LPDWORD lpReserved, [[maybe_unused]] LPDWORD lpType, LPBYTE lpData, |
| 330 |
LPDWORD lpcbData) { |
| 331 |
const std::string *path = get_path_of_created_key(hKey); |
| 332 |
if (path == nullptr) return ERROR_NO_MORE_ITEMS; |
| 333 |
|
| 334 |
const auto *location_ptr = get_registry_vector(*path); |
| 335 |
if (location_ptr == nullptr) return ERROR_NO_MORE_ITEMS; |
| 336 |
const auto &location = *location_ptr; |
| 337 |
if (dwIndex >= location.size()) return ERROR_NO_MORE_ITEMS; |
| 338 |
|
| 339 |
std::string name = narrow(location[dwIndex].name); |
| 340 |
if (*lpcchValueName < name.size()) return ERROR_NO_MORE_ITEMS; |
| 341 |
for (size_t i = 0; i < name.size(); i++) { |
| 342 |
lpValueName[i] = name[i]; |
| 343 |
} |
| 344 |
lpValueName[name.size()] = '\0'; |
| 345 |
*lpcchValueName = static_cast<DWORD>(name.size() + 1); |
| 346 |
if (*lpcbData < sizeof(DWORD)) return ERROR_NO_MORE_ITEMS; |
| 347 |
DWORD *lpcbData_dword = reinterpret_cast<DWORD *>(lpData); |
| 348 |
*lpcbData_dword = location[dwIndex].value; |
| 349 |
*lpcbData = sizeof(DWORD); |
| 350 |
return ERROR_SUCCESS; |
| 351 |
} |
| 352 |
LSTATUS __stdcall ShimRegCloseKey(HKEY hKey) { |
| 353 |
for (size_t i = 0; i < platform_shim.created_keys.size(); i++) { |
| 354 |
if (platform_shim.created_keys[i].get() == hKey) { |
| 355 |
platform_shim.created_keys.erase(platform_shim.created_keys.begin() + i); |
| 356 |
return ERROR_SUCCESS; |
| 357 |
} |
| 358 |
} |
| 359 |
// means that RegCloseKey was called with an invalid key value (one that doesn't exist or has already been closed) |
| 360 |
exit(-1); |
| 361 |
} |
| 362 |
|
| 363 |
// Windows app package shims |
| 364 |
using PFN_GetPackagesByPackageFamily = LONG(WINAPI *)(PCWSTR, UINT32 *, PWSTR *, UINT32 *, WCHAR *); |
| 365 |
static PFN_GetPackagesByPackageFamily fpGetPackagesByPackageFamily = GetPackagesByPackageFamily; |
| 366 |
using PFN_GetPackagePathByFullName = LONG(WINAPI *)(PCWSTR, UINT32 *, PWSTR); |
| 367 |
static PFN_GetPackagePathByFullName fpGetPackagePathByFullName = GetPackagePathByFullName; |
| 368 |
|
| 369 |
static constexpr wchar_t package_full_name[] = L"ThisIsARandomStringSinceTheNameDoesn'tMatter"; |
| 370 |
LONG WINAPI ShimGetPackagesByPackageFamily(_In_ PCWSTR packageFamilyName, _Inout_ UINT32 *count, |
| 371 |
_Out_writes_opt_(*count) PWSTR *packageFullNames, _Inout_ UINT32 *bufferLength, |
| 372 |
_Out_writes_opt_(*bufferLength) WCHAR *buffer) { |
| 373 |
if (!packageFamilyName || !count || !bufferLength) return ERROR_INVALID_PARAMETER; |
| 374 |
if (!platform_shim.app_package_path.empty() && wcscmp(packageFamilyName, L"Microsoft.D3DMappingLayers_8wekyb3d8bbwe") == 0) { |
| 375 |
if (*count > 0 && !packageFullNames) return ERROR_INVALID_PARAMETER; |
| 376 |
if (*bufferLength > 0 && !buffer) return ERROR_INVALID_PARAMETER; |
| 377 |
if (*count > 1) return ERROR_INVALID_PARAMETER; |
| 378 |
bool too_small = *count < 1 || *bufferLength < ARRAYSIZE(package_full_name); |
| 379 |
*count = 1; |
| 380 |
*bufferLength = ARRAYSIZE(package_full_name); |
| 381 |
if (too_small) return ERROR_INSUFFICIENT_BUFFER; |
| 382 |
|
| 383 |
for (size_t i = 0; i < sizeof(package_full_name) / sizeof(wchar_t); i++) { |
| 384 |
if (i >= *bufferLength) { |
| 385 |
break; |
| 386 |
} |
| 387 |
buffer[i] = package_full_name[i]; |
| 388 |
} |
| 389 |
*packageFullNames = buffer; |
| 390 |
return 0; |
| 391 |
} |
| 392 |
*count = 0; |
| 393 |
*bufferLength = 0; |
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
LONG WINAPI ShimGetPackagePathByFullName(_In_ PCWSTR packageFullName, _Inout_ UINT32 *pathLength, |
| 398 |
_Out_writes_opt_(*pathLength) PWSTR path) { |
| 399 |
if (!packageFullName || !pathLength) return ERROR_INVALID_PARAMETER; |
| 400 |
if (*pathLength > 0 && !path) return ERROR_INVALID_PARAMETER; |
| 401 |
if (wcscmp(packageFullName, package_full_name) != 0) { |
| 402 |
*pathLength = 0; |
| 403 |
return 0; |
| 404 |
} |
| 405 |
if (*pathLength < platform_shim.app_package_path.size() + 1) { |
| 406 |
*pathLength = static_cast<UINT32>(platform_shim.app_package_path.size() + 1); |
| 407 |
return ERROR_INSUFFICIENT_BUFFER; |
| 408 |
} |
| 409 |
for (size_t i = 0; i < platform_shim.app_package_path.length(); i++) { |
| 410 |
if (i >= *pathLength) { |
| 411 |
break; |
| 412 |
} |
| 413 |
path[i] = platform_shim.app_package_path.c_str()[i]; |
| 414 |
} |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
using PFN_OutputDebugStringA = void(__stdcall *)(LPCSTR lpOutputString); |
| 419 |
static PFN_OutputDebugStringA fp_OutputDebugStringA = OutputDebugStringA; |
| 420 |
|
| 421 |
void __stdcall intercept_OutputDebugStringA(LPCSTR lpOutputString) { |
| 422 |
if (lpOutputString != nullptr) { |
| 423 |
platform_shim.fputs_stderr_log += lpOutputString; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// Initialization |
| 428 |
void WINAPI DetourFunctions() { |
| 429 |
if (!gdi32_dll) { |
| 430 |
gdi32_dll = LibraryWrapper("gdi32.dll"); |
| 431 |
fpEnumAdapters2 = gdi32_dll.get_symbol("D3DKMTEnumAdapters2"); |
| 432 |
if (fpEnumAdapters2 == nullptr) { |
| 433 |
std::cerr << "Failed to load D3DKMTEnumAdapters2\n"; |
| 434 |
return; |
| 435 |
} |
| 436 |
fpQueryAdapterInfo = gdi32_dll.get_symbol("D3DKMTQueryAdapterInfo"); |
| 437 |
if (fpQueryAdapterInfo == nullptr) { |
| 438 |
std::cerr << "Failed to load D3DKMTQueryAdapterInfo\n"; |
| 439 |
return; |
| 440 |
} |
| 441 |
} |
| 442 |
if (!dxgi_module) { |
| 443 |
TCHAR systemPath[MAX_PATH] = ""; |
| 444 |
GetSystemDirectory(systemPath, MAX_PATH); |
| 445 |
StringCchCat(systemPath, MAX_PATH, TEXT("\\dxgi.dll")); |
| 446 |
dxgi_module = LibraryWrapper(systemPath); |
| 447 |
RealCreateDXGIFactory1 = dxgi_module.get_symbol("CreateDXGIFactory1"); |
| 448 |
if (RealCreateDXGIFactory1 == nullptr) { |
| 449 |
std::cerr << "Failed to load CreateDXGIFactory1\n"; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
DetourRestoreAfterWith(); |
| 454 |
|
| 455 |
DetourTransactionBegin(); |
| 456 |
DetourUpdateThread(GetCurrentThread()); |
| 457 |
DetourAttach(&(PVOID &)fpGetSidSubAuthority, (PVOID)ShimGetSidSubAuthority); |
| 458 |
DetourAttach(&(PVOID &)fpEnumAdapters2, (PVOID)ShimEnumAdapters2); |
| 459 |
DetourAttach(&(PVOID &)fpQueryAdapterInfo, (PVOID)ShimQueryAdapterInfo); |
| 460 |
DetourAttach(&(PVOID &)REAL_CM_Get_Device_ID_List_SizeW, (PVOID)SHIM_CM_Get_Device_ID_List_SizeW); |
| 461 |
DetourAttach(&(PVOID &)REAL_CM_Get_Device_ID_ListW, (PVOID)SHIM_CM_Get_Device_ID_ListW); |
| 462 |
DetourAttach(&(PVOID &)REAL_CM_Get_Device_ID_ListW, (PVOID)SHIM_CM_Get_Device_ID_ListW); |
| 463 |
DetourAttach(&(PVOID &)REAL_CM_Locate_DevNodeW, (PVOID)SHIM_CM_Locate_DevNodeW); |
| 464 |
DetourAttach(&(PVOID &)REAL_CM_Get_DevNode_Status, (PVOID)SHIM_CM_Get_DevNode_Status); |
| 465 |
DetourAttach(&(PVOID &)REAL_CM_Get_Device_IDW, (PVOID)SHIM_CM_Get_Device_IDW); |
| 466 |
DetourAttach(&(PVOID &)REAL_CM_Get_Child, (PVOID)SHIM_CM_Get_Child); |
| 467 |
DetourAttach(&(PVOID &)REAL_CM_Get_DevNode_Registry_PropertyW, (PVOID)SHIM_CM_Get_DevNode_Registry_PropertyW); |
| 468 |
DetourAttach(&(PVOID &)REAL_CM_Get_Sibling, (PVOID)SHIM_CM_Get_Sibling); |
| 469 |
DetourAttach(&(PVOID &)RealCreateDXGIFactory1, (PVOID)ShimCreateDXGIFactory1); |
| 470 |
DetourAttach(&(PVOID &)fpRegOpenKeyExA, (PVOID)ShimRegOpenKeyExA); |
| 471 |
DetourAttach(&(PVOID &)fpRegQueryValueExA, (PVOID)ShimRegQueryValueExA); |
| 472 |
DetourAttach(&(PVOID &)fpRegEnumValueA, (PVOID)ShimRegEnumValueA); |
| 473 |
DetourAttach(&(PVOID &)fpRegCloseKey, (PVOID)ShimRegCloseKey); |
| 474 |
DetourAttach(&(PVOID &)fpGetPackagesByPackageFamily, (PVOID)ShimGetPackagesByPackageFamily); |
| 475 |
DetourAttach(&(PVOID &)fpGetPackagePathByFullName, (PVOID)ShimGetPackagePathByFullName); |
| 476 |
DetourAttach(&(PVOID &)fp_OutputDebugStringA, (PVOID)intercept_OutputDebugStringA); |
| 477 |
LONG error = DetourTransactionCommit(); |
| 478 |
|
| 479 |
if (error != NO_ERROR) { |
| 480 |
std::cerr << "simple" << DETOURS_STRINGIFY(DETOURS_BITS) << ".dll:" |
| 481 |
<< " Error detouring function(): " << error << "\n"; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
void DetachFunctions() { |
| 486 |
DetourTransactionBegin(); |
| 487 |
DetourUpdateThread(GetCurrentThread()); |
| 488 |
DetourDetach(&(PVOID &)fpGetSidSubAuthority, (PVOID)ShimGetSidSubAuthority); |
| 489 |
DetourDetach(&(PVOID &)fpEnumAdapters2, (PVOID)ShimEnumAdapters2); |
| 490 |
DetourDetach(&(PVOID &)fpQueryAdapterInfo, (PVOID)ShimQueryAdapterInfo); |
| 491 |
DetourDetach(&(PVOID &)REAL_CM_Get_Device_ID_List_SizeW, (PVOID)SHIM_CM_Get_Device_ID_List_SizeW); |
| 492 |
DetourDetach(&(PVOID &)REAL_CM_Get_Device_ID_ListW, (PVOID)SHIM_CM_Get_Device_ID_ListW); |
| 493 |
DetourDetach(&(PVOID &)REAL_CM_Locate_DevNodeW, (PVOID)SHIM_CM_Locate_DevNodeW); |
| 494 |
DetourDetach(&(PVOID &)REAL_CM_Get_DevNode_Status, (PVOID)SHIM_CM_Get_DevNode_Status); |
| 495 |
DetourDetach(&(PVOID &)REAL_CM_Get_Device_IDW, (PVOID)SHIM_CM_Get_Device_IDW); |
| 496 |
DetourDetach(&(PVOID &)REAL_CM_Get_Child, (PVOID)SHIM_CM_Get_Child); |
| 497 |
DetourDetach(&(PVOID &)REAL_CM_Get_DevNode_Registry_PropertyW, (PVOID)SHIM_CM_Get_DevNode_Registry_PropertyW); |
| 498 |
DetourDetach(&(PVOID &)REAL_CM_Get_Sibling, (PVOID)SHIM_CM_Get_Sibling); |
| 499 |
DetourDetach(&(PVOID &)RealCreateDXGIFactory1, (PVOID)ShimCreateDXGIFactory1); |
| 500 |
DetourDetach(&(PVOID &)fpRegOpenKeyExA, (PVOID)ShimRegOpenKeyExA); |
| 501 |
DetourDetach(&(PVOID &)fpRegQueryValueExA, (PVOID)ShimRegQueryValueExA); |
| 502 |
DetourDetach(&(PVOID &)fpRegEnumValueA, (PVOID)ShimRegEnumValueA); |
| 503 |
DetourDetach(&(PVOID &)fpRegCloseKey, (PVOID)ShimRegCloseKey); |
| 504 |
DetourDetach(&(PVOID &)fpGetPackagesByPackageFamily, (PVOID)ShimGetPackagesByPackageFamily); |
| 505 |
DetourDetach(&(PVOID &)fpGetPackagePathByFullName, (PVOID)ShimGetPackagePathByFullName); |
| 506 |
DetourDetach(&(PVOID &)fp_OutputDebugStringA, (PVOID)intercept_OutputDebugStringA); |
| 507 |
DetourTransactionCommit(); |
| 508 |
} |
| 509 |
|
| 510 |
BOOL WINAPI DllMain([[maybe_unused]] HINSTANCE hinst, DWORD dwReason, [[maybe_unused]] LPVOID reserved) { |
| 511 |
if (DetourIsHelperProcess()) { |
| 512 |
return TRUE; |
| 513 |
} |
| 514 |
|
| 515 |
if (dwReason == DLL_PROCESS_ATTACH) { |
| 516 |
DetourFunctions(); |
| 517 |
} else if (dwReason == DLL_PROCESS_DETACH) { |
| 518 |
DetachFunctions(); |
| 519 |
} |
| 520 |
return TRUE; |
| 521 |
} |
| 522 |
FRAMEWORK_EXPORT PlatformShim *get_platform_shim() { return &platform_shim; } |
| 523 |
} |