Author: mjansen Date: Wed Jun 14 15:25:04 2017 New Revision: 75041 URL: http://svn.reactos.org/svn/reactos?rev=75041&view=rev Log: [APITEST] Move RedirectIat into it's own header so that it can be used from multiple tests.
Added: trunk/rostests/apitests/include/apitest_iathook.h (with props) Modified: trunk/rostests/apitests/apphelp/layerapi.c trunk/rostests/apitests/appshim/dispmode.c Modified: trunk/rostests/apitests/apphelp/layerapi.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/apphelp/layerapi.c?rev=75041&r1=75040&r2=75041&view=diff ============================================================================== --- trunk/rostests/apitests/apphelp/layerapi.c [iso-8859-1] (original) +++ trunk/rostests/apitests/apphelp/layerapi.c [iso-8859-1] Wed Jun 14 15:25:04 2017 @@ -30,7 +30,7 @@ #include <stdio.h> #include "wine/test.h" - +#include "apitest_iathook.h" #include "apphelp_apitest.h" #define GPLK_USER 1 @@ -612,73 +612,6 @@ 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 @@ 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 @@ 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()); Modified: trunk/rostests/apitests/appshim/dispmode.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/appshim/dispmode.c?rev=75041&r1=75040&r2=75041&view=diff ============================================================================== --- trunk/rostests/apitests/appshim/dispmode.c [iso-8859-1] (original) +++ trunk/rostests/apitests/appshim/dispmode.c [iso-8859-1] Wed Jun 14 15:25:04 2017 @@ -27,6 +27,7 @@ #include <stdio.h> #include <strsafe.h> #include "wine/test.h" +#include "apitest_iathook.h" static DWORD g_Version; #define WINVER_ANY 0 @@ -311,58 +312,6 @@ } } -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 @@ 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 @@ 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 }, }; Added: trunk/rostests/apitests/include/apitest_iathook.h URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/include/apitest_iathook.h?rev=75041 ============================================================================== --- trunk/rostests/apitests/include/apitest_iathook.h (added) +++ trunk/rostests/apitests/include/apitest_iathook.h [iso-8859-1] Wed Jun 14 15:25:04 2017 @@ -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 + Propchange: trunk/rostests/apitests/include/apitest_iathook.h ------------------------------------------------------------------------------ svn:eol-style = native