From 272bc3ef9f5b6e801342f429ad0f4394a4916c4c Mon Sep 17 00:00:00 2001 From: Mark Jansen Date: Wed, 14 Jun 2017 15:25:04 +0000 Subject: [PATCH] [APITEST] Move RedirectIat into it's own header so that it can be used from multiple tests. svn path=/trunk/; revision=75041 --- rostests/apitests/apphelp/layerapi.c | 74 +----------------- rostests/apitests/appshim/dispmode.c | 84 ++++++--------------- rostests/apitests/include/apitest_iathook.h | 64 ++++++++++++++++ 3 files changed, 90 insertions(+), 132 deletions(-) create mode 100644 rostests/apitests/include/apitest_iathook.h diff --git a/rostests/apitests/apphelp/layerapi.c b/rostests/apitests/apphelp/layerapi.c index 34700fad7da..616910a3dc5 100644 --- a/rostests/apitests/apphelp/layerapi.c +++ b/rostests/apitests/apphelp/layerapi.c @@ -30,7 +30,7 @@ #include #include "wine/test.h" - +#include "apitest_iathook.h" #include "apphelp_apitest.h" #define GPLK_USER 1 @@ -612,73 +612,6 @@ UINT WINAPI mGetDriveTypeW(LPCWSTR target) return uRet; } - -static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName) -{ - ULONG Size; - PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = RtlImageDirectoryEntryToData((HMODULE)DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &Size); - while (ImportDescriptor->Name && ImportDescriptor->OriginalFirstThunk) - { - PCHAR Name = (PCHAR)(DllBase + ImportDescriptor->Name); - if (!lstrcmpiA(Name, DllName)) - { - return ImportDescriptor; - } - ImportDescriptor++; - } - return NULL; -} - -static BOOL RedirectIat(PCSTR TargetDllName, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR* OriginalFunction) -{ - PBYTE DllBase = (PBYTE)GetModuleHandleA(TargetDllName); - if (DllBase) - { - PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = FindImportDescriptor(DllBase, DllName); - if (ImportDescriptor) - { - // On loaded images, OriginalFirstThunk points to the name / ordinal of the function - PIMAGE_THUNK_DATA OriginalThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->OriginalFirstThunk); - // FirstThunk points to the resolved address. - PIMAGE_THUNK_DATA FirstThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->FirstThunk); - while (OriginalThunk->u1.AddressOfData && FirstThunk->u1.Function) - { - if (!IMAGE_SNAP_BY_ORDINAL32(OriginalThunk->u1.AddressOfData)) - { - PIMAGE_IMPORT_BY_NAME ImportName = (PIMAGE_IMPORT_BY_NAME)(DllBase + OriginalThunk->u1.AddressOfData); - if (!lstrcmpiA((PCSTR)ImportName->Name, FunctionName)) - { - DWORD dwOld; - VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), PAGE_EXECUTE_READWRITE, &dwOld); - *OriginalFunction = FirstThunk->u1.Function; - FirstThunk->u1.Function = NewFunction; - VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), dwOld, &dwOld); - return TRUE; - } - } - OriginalThunk++; - FirstThunk++; - } - skip("Unable to find the Import '%s' from '%s' in %s'\n", FunctionName, DllName, TargetDllName); - } - else - { - skip("Unable to find the ImportDescriptor for '%s' in '%s'\n", DllName, TargetDllName); - } - } - else - { - skip("Unable to find the loaded module '%s'\n", TargetDllName); - } - return FALSE; -} - -static BOOL RestoreIat(PCSTR target, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction) -{ - ULONG_PTR old = 0; - return RedirectIat(target, DllName, FunctionName, OriginalFunction, &old); -} - static BOOL wrapSdbSetPermLayerKeys2(LPCSTR dir, LPCSTR name, PCSTR szLayers, BOOL bMachine) { char szPath[MAX_PATH]; @@ -759,7 +692,8 @@ static void test_Sign_Media(void) ok(ret, "DefineDosDeviceA error: %d\n", GetLastError()); if(ret) { - ret = RedirectIat("apphelp.dll", "kernel32.dll", "GetDriveTypeW", (ULONG_PTR)mGetDriveTypeW, (ULONG_PTR*)&pGetDriveTypeW); + ret = RedirectIat(GetModuleHandleA("apphelp.dll"), "kernel32.dll", "GetDriveTypeW", + (ULONG_PTR)mGetDriveTypeW, (ULONG_PTR*)&pGetDriveTypeW); if (g_WinVersion < WINVER_WIN8) ok(ret, "Expected redirect_iat to succeed\n"); if(ret) @@ -862,7 +796,7 @@ static void test_Sign_Media(void) ok(wrapSdbSetPermLayerKeys2(drive, "sub\\test.exe", "", 0), "Expected wrapSdbSetPermLayerKeys2 to succeed\n"); } - ret = RestoreIat("apphelp.dll", "kernel32.dll", "GetDriveTypeW", (ULONG_PTR)pGetDriveTypeW); + ret = RestoreIat(GetModuleHandleA("apphelp.dll"), "kernel32.dll", "GetDriveTypeW", (ULONG_PTR)pGetDriveTypeW); ok(ret, "Expected restore_iat to succeed\n"); ok(delete_file(subdir, "test.bbb"), "delete_file error: %d\n", GetLastError()); diff --git a/rostests/apitests/appshim/dispmode.c b/rostests/apitests/appshim/dispmode.c index d904e92a769..3fa966066a5 100644 --- a/rostests/apitests/appshim/dispmode.c +++ b/rostests/apitests/appshim/dispmode.c @@ -27,6 +27,7 @@ #include #include #include "wine/test.h" +#include "apitest_iathook.h" static DWORD g_Version; #define WINVER_ANY 0 @@ -311,58 +312,6 @@ static void post_theme_no(void) } } -static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName) -{ - ULONG Size; - PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = RtlImageDirectoryEntryToData((HMODULE)DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &Size); - while (ImportDescriptor->Name && ImportDescriptor->OriginalFirstThunk) - { - PCHAR Name = (PCHAR)(DllBase + ImportDescriptor->Name); - if (!lstrcmpiA(Name, DllName)) - { - return ImportDescriptor; - } - ImportDescriptor++; - } - return NULL; -} - -static BOOL RedirectIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR* OriginalFunction) -{ - PBYTE DllBase = (PBYTE)TargetDll; - PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = FindImportDescriptor(DllBase, DllName); - if (ImportDescriptor) - { - // On loaded images, OriginalFirstThunk points to the name / ordinal of the function - PIMAGE_THUNK_DATA OriginalThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->OriginalFirstThunk); - // FirstThunk points to the resolved address. - PIMAGE_THUNK_DATA FirstThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->FirstThunk); - while (OriginalThunk->u1.AddressOfData && FirstThunk->u1.Function) - { - if (!IMAGE_SNAP_BY_ORDINAL32(OriginalThunk->u1.AddressOfData)) - { - PIMAGE_IMPORT_BY_NAME ImportName = (PIMAGE_IMPORT_BY_NAME)(DllBase + OriginalThunk->u1.AddressOfData); - if (!lstrcmpiA((PCSTR)ImportName->Name, FunctionName)) - { - DWORD dwOld; - VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), PAGE_EXECUTE_READWRITE, &dwOld); - *OriginalFunction = FirstThunk->u1.Function; - FirstThunk->u1.Function = NewFunction; - VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), dwOld, &dwOld); - return TRUE; - } - } - OriginalThunk++; - FirstThunk++; - } - skip("Unable to find the Import %s!%s\n", DllName, FunctionName); - } - else - { - skip("Unable to find the ImportDescriptor for %s\n", DllName); - } - return FALSE; -} static BOOL hook_disp(HMODULE dll) { @@ -370,11 +319,21 @@ static BOOL hook_disp(HMODULE dll) RedirectIat(dll, "user32.dll", "EnumDisplaySettingsA", (ULONG_PTR)mEnumDisplaySettingsA, (ULONG_PTR*)&pEnumDisplaySettingsA); } +static VOID unhook_disp(HMODULE dll) +{ + RestoreIat(dll, "user32.dll", "ChangeDisplaySettingsA", (ULONG_PTR)pChangeDisplaySettingsA); + RestoreIat(dll, "user32.dll", "EnumDisplaySettingsA", (ULONG_PTR)pEnumDisplaySettingsA); +} + static BOOL hook_theme(HMODULE dll) { return RedirectIat(dll, "uxtheme.dll", "SetThemeAppProperties", (ULONG_PTR)mSetThemeAppProperties, (ULONG_PTR*)&pSetThemeAppProperties); } +static VOID unhook_theme(HMODULE dll) +{ + RestoreIat(dll, "uxtheme.dll", "SetThemeAppProperties", (ULONG_PTR)pSetThemeAppProperties); +} static void test_one(LPCSTR shim, DWORD dwReason, void(*pre)(), void(*post)(), void(*second)(void)) { @@ -424,24 +383,25 @@ static struct test_info DWORD winver; DWORD reason; BOOL(*hook)(HMODULE); + void(*unhook)(HMODULE); void(*pre)(void); void(*post)(void); void(*second)(void); } tests[] = { /* Success */ - { "Force8BitColor", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, pre_8bit, post_8bit, post_8bit_no }, - { "Force8BitColor", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, pre_8bit, post_8bit, post_8bit_no }, - { "Force640x480", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, pre_640, post_640, post_640_no }, - { "Force640x480", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, pre_640, post_640, post_640_no }, - { "DisableThemes", L"\\acgenral.dll", WINVER_ANY, 1, hook_theme, pre_theme, post_theme, post_theme_no }, - { "DisableThemes", L"\\acgenral.dll", _WIN32_WINNT_VISTA, 100, hook_theme, pre_theme, post_theme, post_theme_no }, + { "Force8BitColor", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, unhook_disp, pre_8bit, post_8bit, post_8bit_no }, + { "Force8BitColor", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, unhook_disp,pre_8bit, post_8bit, post_8bit_no }, + { "Force640x480", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, unhook_disp, pre_640, post_640, post_640_no }, + { "Force640x480", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, unhook_disp, pre_640, post_640, post_640_no }, + { "DisableThemes", L"\\acgenral.dll", WINVER_ANY, 1, hook_theme, unhook_theme, pre_theme, post_theme, post_theme_no }, + { "DisableThemes", L"\\acgenral.dll", _WIN32_WINNT_VISTA, 100, hook_theme, unhook_theme, pre_theme, post_theme, post_theme_no }, /* No need to change anything */ - { "Force8BitColor", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, pre_8bit_2, post_8bit_2, post_8bit_2_no }, - { "Force8BitColor", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, pre_8bit_2, post_8bit_2, post_8bit_2_no }, - { "Force640x480", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, pre_640_2, post_640_2, post_640_2_no }, - { "Force640x480", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, pre_640_2, post_640_2, post_640_2_no }, + { "Force8BitColor", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, unhook_disp, pre_8bit_2, post_8bit_2, post_8bit_2_no }, + { "Force8BitColor", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, unhook_disp, pre_8bit_2, post_8bit_2, post_8bit_2_no }, + { "Force640x480", L"\\aclayers.dll", WINVER_ANY, 1, hook_disp, unhook_disp, pre_640_2, post_640_2, post_640_2_no }, + { "Force640x480", L"\\aclayers.dll", _WIN32_WINNT_VISTA, 100, hook_disp, unhook_disp, pre_640_2, post_640_2, post_640_2_no }, }; diff --git a/rostests/apitests/include/apitest_iathook.h b/rostests/apitests/include/apitest_iathook.h new file mode 100644 index 00000000000..9a0ae77e034 --- /dev/null +++ b/rostests/apitests/include/apitest_iathook.h @@ -0,0 +1,64 @@ +#ifndef _APITEST_IATHOOK_H +#define _APITEST_IATHOOK_H + +static PIMAGE_IMPORT_DESCRIPTOR FindImportDescriptor(PBYTE DllBase, PCSTR DllName) +{ + ULONG Size; + PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = RtlImageDirectoryEntryToData((HMODULE)DllBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &Size); + while (ImportDescriptor->Name && ImportDescriptor->OriginalFirstThunk) + { + PCHAR Name = (PCHAR)(DllBase + ImportDescriptor->Name); + if (!lstrcmpiA(Name, DllName)) + { + return ImportDescriptor; + } + ImportDescriptor++; + } + return NULL; +} + +static BOOL RedirectIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR NewFunction, ULONG_PTR* OriginalFunction) +{ + PBYTE DllBase = (PBYTE)TargetDll; + PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor = FindImportDescriptor(DllBase, DllName); + if (ImportDescriptor) + { + // On loaded images, OriginalFirstThunk points to the name / ordinal of the function + PIMAGE_THUNK_DATA OriginalThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->OriginalFirstThunk); + // FirstThunk points to the resolved address. + PIMAGE_THUNK_DATA FirstThunk = (PIMAGE_THUNK_DATA)(DllBase + ImportDescriptor->FirstThunk); + while (OriginalThunk->u1.AddressOfData && FirstThunk->u1.Function) + { + if (!IMAGE_SNAP_BY_ORDINAL32(OriginalThunk->u1.AddressOfData)) + { + PIMAGE_IMPORT_BY_NAME ImportName = (PIMAGE_IMPORT_BY_NAME)(DllBase + OriginalThunk->u1.AddressOfData); + if (!lstrcmpiA((PCSTR)ImportName->Name, FunctionName)) + { + DWORD dwOld; + VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), PAGE_EXECUTE_READWRITE, &dwOld); + *OriginalFunction = FirstThunk->u1.Function; + FirstThunk->u1.Function = NewFunction; + VirtualProtect(&FirstThunk->u1.Function, sizeof(ULONG_PTR), dwOld, &dwOld); + return TRUE; + } + } + OriginalThunk++; + FirstThunk++; + } + skip("Unable to find the Import %s!%s\n", DllName, FunctionName); + } + else + { + skip("Unable to find the ImportDescriptor for %s\n", DllName); + } + return FALSE; +} + +static BOOL RestoreIat(HMODULE TargetDll, PCSTR DllName, PCSTR FunctionName, ULONG_PTR OriginalFunction) +{ + ULONG_PTR old = 0; + return RedirectIat(TargetDll, DllName, FunctionName, OriginalFunction, &old); +} + + #endif // _APITEST_IATHOOK_H + \ No newline at end of file -- 2.17.1