From: Fabian Maurer Subject: [PATCH v6 3/3] sxs: Implement SxsLookupClrGuid and add tests Message-Id: <20180905210254.27707-3-dark.shadow4@web.de> Date: Wed, 5 Sep 2018 23:02:54 +0200 In-Reply-To: <20180905210254.27707-1-dark.shadow4@web.de> References: <20180905210254.27707-1-dark.shadow4@web.de> Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=43270 Signed-off-by: Fabian Maurer --- dlls/sxs/sxs.c | 148 ++++++++++++++++++ dlls/sxs/sxs.spec | 1 + dlls/sxs/tests/Makefile.in | 8 +- dlls/sxs/tests/comtest_dll.manifest | 16 ++ dlls/sxs/tests/comtest_exe.manifest | 11 ++ dlls/sxs/tests/interfaces.idl | 33 ++++ dlls/sxs/tests/resource.rc | 27 ++++ dlls/sxs/tests/sxs.c | 223 ++++++++++++++++++++++++++++ 8 files changed, 465 insertions(+), 2 deletions(-) create mode 100644 dlls/sxs/tests/comtest_dll.manifest create mode 100644 dlls/sxs/tests/comtest_exe.manifest create mode 100644 dlls/sxs/tests/interfaces.idl create mode 100644 dlls/sxs/tests/resource.rc create mode 100644 dlls/sxs/tests/sxs.c diff --git a/dlls/sxs/sxs.c b/dlls/sxs/sxs.c index 06e6672dbf..7eec772de1 100644 --- a/dlls/sxs/sxs.c +++ b/dlls/sxs/sxs.c @@ -23,6 +23,10 @@ #include "windef.h" #include "winbase.h" +#include "wine/heap.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(sxs); /*********************************************************************** * DllMain (SXS.@) @@ -40,3 +44,147 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) } return TRUE; } + +typedef struct _SXS_GUID_INFORMATION_CLR +{ + DWORD cbSize; + DWORD dwFlags; + PCWSTR pcwszRuntimeVersion; + PCWSTR pcwszTypeName; + PCWSTR pcwszAssemblyIdentity; +} SXS_GUID_INFORMATION_CLR, *PSXS_GUID_INFORMATION_CLR; + +#define SXS_GUID_INFORMATION_CLR_FLAG_IS_SURROGATE 0x1 +#define SXS_GUID_INFORMATION_CLR_FLAG_IS_CLASS 0x2 + +#define SXS_LOOKUP_CLR_GUID_USE_ACTCTX 0x00000001 +#define SXS_LOOKUP_CLR_GUID_FIND_SURROGATE 0x00010000 +#define SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS 0x00020000 + +struct comclassredirect_data +{ + ULONG size; + BYTE res; + BYTE miscmask; + BYTE res1[2]; + DWORD model; + GUID clsid; + GUID alias; + GUID clsid2; + GUID tlbid; + ULONG name_len; + ULONG name_offset; + ULONG progid_len; + ULONG progid_offset; + ULONG clrdata_len; + ULONG clrdata_offset; + DWORD miscstatus; + DWORD miscstatuscontent; + DWORD miscstatusthumbnail; + DWORD miscstatusicon; + DWORD miscstatusdocprint; +}; + +struct clrclass_data +{ + ULONG size; + DWORD res[2]; + ULONG module_len; + ULONG module_offset; + ULONG name_len; + ULONG name_offset; + ULONG version_len; + ULONG version_offset; + DWORD res2[2]; +}; + +BOOL WINAPI SxsLookupClrGuid(DWORD flags, GUID *clsid, HANDLE actctx, void *buffer, SIZE_T buffer_len, SIZE_T *buffer_len_required) +{ + ACTCTX_SECTION_KEYED_DATA guid_info = { sizeof(ACTCTX_SECTION_KEYED_DATA) }; + ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION *assembly_info; + SIZE_T bytes_assembly_info; + struct comclassredirect_data *redirect_data; + struct clrclass_data *class_data; + int len_version = 0, len_name, len_identity; + const void *ptr_name, *ptr_version, *ptr_identity; + SXS_GUID_INFORMATION_CLR *ret = buffer; + char *ret_strings; + + TRACE("(%x, %s, %p, %p, %08lx, %p): stub\n", flags, wine_dbgstr_guid(clsid), actctx, buffer, buffer_len, buffer_len_required); + + if (flags & (~SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS)) + FIXME("Ignored flags: %x\n", flags & (~SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS)); + + if (!FindActCtxSectionGuid(FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX, 0, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION, clsid, &guid_info)) + { + SetLastError(ERROR_NOT_FOUND); + return FALSE; + } + + QueryActCtxW(0, guid_info.hActCtx, &guid_info.ulAssemblyRosterIndex, + AssemblyDetailedInformationInActivationContext, NULL, 0, &bytes_assembly_info); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + ERR("QueryActCtxW failed: %d!\n", GetLastError()); + ReleaseActCtx(guid_info.hActCtx); + return FALSE; + } + assembly_info = heap_alloc(bytes_assembly_info); + if(!QueryActCtxW(0, guid_info.hActCtx, &guid_info.ulAssemblyRosterIndex, + AssemblyDetailedInformationInActivationContext, assembly_info, bytes_assembly_info, &bytes_assembly_info)) + { + ERR("QueryActCtxW failed: %d!\n", GetLastError()); + heap_free(assembly_info); + ReleaseActCtx(guid_info.hActCtx); + return FALSE; + } + + redirect_data = guid_info.lpData; + class_data = (void *)((char*)redirect_data + redirect_data->clrdata_offset); + + ptr_identity = assembly_info->lpAssemblyEncodedAssemblyIdentity; + ptr_name = (char *)class_data + class_data->name_offset; + ptr_version = (char *)class_data + class_data->version_offset; + + len_identity = assembly_info->ulEncodedAssemblyIdentityLength + sizeof(WCHAR); + len_name = class_data->name_len + sizeof(WCHAR); + if (class_data->version_len > 0) + len_version = class_data->version_len + sizeof(WCHAR); + + *buffer_len_required = sizeof(SXS_GUID_INFORMATION_CLR) + len_identity + len_version + len_name; + if (!buffer || buffer_len < *buffer_len_required) + { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + heap_free(assembly_info); + ReleaseActCtx(guid_info.hActCtx); + return FALSE; + } + + ret->cbSize = sizeof(SXS_GUID_INFORMATION_CLR); + ret->dwFlags = SXS_GUID_INFORMATION_CLR_FLAG_IS_CLASS; + + /* Copy strings into buffer */ + ret_strings = (char *)ret + sizeof(SXS_GUID_INFORMATION_CLR); + + memcpy(ret_strings, ptr_identity, len_identity); + ret->pcwszAssemblyIdentity = (WCHAR *)ret_strings; + ret_strings += len_identity; + + memcpy(ret_strings, ptr_name, len_name); + ret->pcwszTypeName = (WCHAR *)ret_strings; + ret_strings += len_name; + + if (len_version > 0) + { + memcpy(ret_strings, ptr_version, len_version); + ret->pcwszRuntimeVersion = (WCHAR *)ret_strings; + } + else + ret->pcwszRuntimeVersion = NULL; + + SetLastError(0); + + ReleaseActCtx(guid_info.hActCtx); + heap_free(assembly_info); + return TRUE; +} diff --git a/dlls/sxs/sxs.spec b/dlls/sxs/sxs.spec index 2a27313427..138d68f7d7 100644 --- a/dlls/sxs/sxs.spec +++ b/dlls/sxs/sxs.spec @@ -1,2 +1,3 @@ @ stdcall CreateAssemblyCache(ptr long) @ stdcall CreateAssemblyNameObject(ptr wstr long ptr) +@ stdcall SxsLookupClrGuid(long ptr ptr ptr long ptr) diff --git a/dlls/sxs/tests/Makefile.in b/dlls/sxs/tests/Makefile.in index 34609a14ae..46b5485d29 100644 --- a/dlls/sxs/tests/Makefile.in +++ b/dlls/sxs/tests/Makefile.in @@ -1,6 +1,10 @@ TESTDLL = sxs.dll -IMPORTS = sxs +IMPORTS = sxs shlwapi C_SRCS = \ cache.c \ - name.c + name.c \ + sxs.c + +RC_SRCS = resource.rc +IDL_SRCS = interfaces.idl \ No newline at end of file diff --git a/dlls/sxs/tests/comtest_dll.manifest b/dlls/sxs/tests/comtest_dll.manifest new file mode 100644 index 0000000000..ef6924de9e --- /dev/null +++ b/dlls/sxs/tests/comtest_dll.manifest @@ -0,0 +1,16 @@ + + + + + + + + diff --git a/dlls/sxs/tests/comtest_exe.manifest b/dlls/sxs/tests/comtest_exe.manifest new file mode 100644 index 0000000000..bc9ce4c045 --- /dev/null +++ b/dlls/sxs/tests/comtest_exe.manifest @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/dlls/sxs/tests/interfaces.idl b/dlls/sxs/tests/interfaces.idl new file mode 100644 index 0000000000..9e64988197 --- /dev/null +++ b/dlls/sxs/tests/interfaces.idl @@ -0,0 +1,33 @@ +/* + * Copyright 2018 Fabian Maurer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "unknwn.idl" + +[ + object, + uuid(1dbc4491-080d-45c5-a15d-1e3c4610bdd9), + local +] +interface ITest : IUnknown { + HRESULT Func([in, out] int *i); +}; + +[ + uuid(2e106e50-e7a4-4489-8538-83643f100fdc), +] +coclass Test { interface ITest; }; diff --git a/dlls/sxs/tests/resource.rc b/dlls/sxs/tests/resource.rc new file mode 100644 index 0000000000..dd2c25bf37 --- /dev/null +++ b/dlls/sxs/tests/resource.rc @@ -0,0 +1,27 @@ +/* + * Resources for sxs test suite. + * + * Copyright 2018 Fabian Maurer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "windef.h" + +/* @makedep: comtest_exe.manifest */ +comtest_exe.manifest RCDATA comtest_exe.manifest + +/* @makedep: comtest_dll.manifest */ +comtest_dll.manifest RCDATA comtest_dll.manifest diff --git a/dlls/sxs/tests/sxs.c b/dlls/sxs/tests/sxs.c new file mode 100644 index 0000000000..d92bfb65c1 --- /dev/null +++ b/dlls/sxs/tests/sxs.c @@ -0,0 +1,223 @@ +/* + * Copyright 2018 Fabian Maurer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include + +#define COBJMACROS + +#include +#include +#include +#include "shlwapi.h" + +#include "wine/test.h" +#include "wine/heap.h" + +#include "initguid.h" +#include "interfaces.h" + +BOOL (WINAPI *pSxsLookupClrGuid)(DWORD flags, GUID *clsid, HANDLE actctx, void *buffer, SIZE_T buffer_len, SIZE_T *buffer_len_required); + +#define SXS_LOOKUP_CLR_GUID_USE_ACTCTX 0x00000001 +#define SXS_LOOKUP_CLR_GUID_FIND_SURROGATE 0x00010000 +#define SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS 0x00020000 +#define SXS_LOOKUP_CLR_GUID_FIND_ANY (SXS_LOOKUP_CLR_GUID_FIND_SURROGATE | SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS) + +#define SXS_GUID_INFORMATION_CLR_FLAG_IS_SURROGATE 0x00000001 +#define SXS_GUID_INFORMATION_CLR_FLAG_IS_CLASS 0x00000002 + +typedef struct _SXS_GUID_INFORMATION_CLR +{ + DWORD cbSize; + DWORD dwFlags; + PCWSTR pcwszRuntimeVersion; + PCWSTR pcwszTypeName; + PCWSTR pcwszAssemblyIdentity; +} SXS_GUID_INFORMATION_CLR; + +static BOOL write_resource_file(const char *path_tmp, const char *name_res, const char *name_file, char *path_file) +{ + HRSRC rsrc; + void *rsrc_data; + DWORD rsrc_size; + BOOL ret; + HANDLE hfile; + + rsrc = FindResourceA(GetModuleHandleA(NULL), name_res, (LPCSTR)RT_RCDATA); + if (!rsrc) return FALSE; + + rsrc_data = LockResource(LoadResource(GetModuleHandleA(NULL), rsrc)); + if (!rsrc_data) return FALSE; + + rsrc_size = SizeofResource(GetModuleHandleA(NULL), rsrc); + if (!rsrc_size) return FALSE; + + strcpy(path_file, path_tmp); + PathAppendA(path_file, name_file); + hfile = CreateFileA(path_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + if (hfile == INVALID_HANDLE_VALUE) return FALSE; + + ret = WriteFile(hfile, rsrc_data, rsrc_size, &rsrc_size, NULL); + + CloseHandle(hfile); + return ret; +} + +static void run_test(void) +{ + SIZE_T buffer_size; + BOOL ret; + SXS_GUID_INFORMATION_CLR *info; + WCHAR expected_type_name[] = {'D','L','L','.','T','e','s','t',0}; + WCHAR expected_runtime_version[] = {'v','4','.','0','.','0','.','0',0}; + WCHAR expected_assembly_identity[] = {'c','o','m','t','e','s','t',',','t','y','p','e','=','"','w','i','n','3','2','"',',','v','e','r','s','i','o','n','=','"','1','.','0','.','0','.','0','"',0}; + + ret = pSxsLookupClrGuid(SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS, (GUID*)&CLSID_Test, NULL, NULL, 0, &buffer_size); + ok(ret == FALSE, "Got %d\n", ret); + ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Got %d\n", GetLastError()); + + info = heap_alloc(buffer_size); + ret = pSxsLookupClrGuid(SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS, (GUID*)&CLSID_Test, NULL, info, buffer_size, &buffer_size); + ok(ret == TRUE, "Got %d\n", ret); + ok(GetLastError() == 0, "Got %d\n", GetLastError()); + + ok(info->dwFlags == SXS_GUID_INFORMATION_CLR_FLAG_IS_CLASS, "Got %d\n", info->dwFlags); + ok(lstrcmpW(info->pcwszTypeName, expected_type_name) == 0, "Got %s\n", wine_dbgstr_w(info->pcwszTypeName)); + ok(lstrcmpW(info->pcwszAssemblyIdentity, expected_assembly_identity) == 0, "Got %s\n", wine_dbgstr_w(info->pcwszAssemblyIdentity)); + ok(lstrcmpW(info->pcwszRuntimeVersion, expected_runtime_version) == 0, "Got %s\n", wine_dbgstr_w(info->pcwszRuntimeVersion)); + + heap_free(info); +} + +static void prepare_and_run_test(void) +{ + char path_tmp[MAX_PATH] = {0}; + char path_manifest_dll[MAX_PATH] = {0}; + char path_manifest_exe[MAX_PATH] = {0}; + BOOL success; + ACTCTXA context = {0}; + ULONG_PTR cookie; + HANDLE handle_context = 0; + + GetTempPathA(MAX_PATH, path_tmp); + + if (!write_resource_file(path_tmp, "comtest_exe.manifest", "exe.manifest", path_manifest_exe)) + { + ok(0, "Failed to create file for testing\n"); + goto cleanup; + } + + if (!write_resource_file(path_tmp, "comtest_dll.manifest", "comtest.manifest", path_manifest_dll)) + { + ok(0, "Failed to create file for testing\n"); + goto cleanup; + } + + context.cbSize = sizeof(ACTCTXA); + context.lpSource = path_manifest_exe; + context.lpAssemblyDirectory = path_tmp; + context.dwFlags = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID; + + handle_context = CreateActCtxA(&context); + ok(handle_context != NULL && handle_context != INVALID_HANDLE_VALUE, "CreateActCtxA failed: %d\n", GetLastError()); + + if (handle_context == NULL || handle_context == INVALID_HANDLE_VALUE) + { + ok(0, "Failed to create activation context\n"); + goto cleanup; + } + + success = ActivateActCtx(handle_context, &cookie); + ok(success, "ActivateActCtx failed: %d\n", GetLastError()); + + run_test(); + +cleanup: + if (handle_context != NULL && handle_context != INVALID_HANDLE_VALUE) + { + success = DeactivateActCtx(0, cookie); + ok(success, "DeactivateActCtx failed: %d\n", GetLastError()); + ReleaseActCtx(handle_context); + } + if (*path_manifest_exe) + { + success = DeleteFileA(path_manifest_exe); + ok(success, "DeleteFileA failed: %d\n", GetLastError()); + } + if(*path_manifest_dll) + { + success = DeleteFileA(path_manifest_dll); + ok(success, "DeleteFileA failed: %d\n", GetLastError()); + } +} + +static void run_child_process(void) +{ + char cmdline[MAX_PATH]; + char exe[MAX_PATH]; + char **argv; + PROCESS_INFORMATION pi; + STARTUPINFOA si = { 0 }; + BOOL ret; + + winetest_get_mainargs(&argv); + + if (strstr(argv[0], ".exe")) + sprintf(exe, "%s", argv[0]); + else + sprintf(exe, "%s.exe", argv[0]); + sprintf(cmdline, "\"%s\" %s %s", argv[0], argv[1], "subtest"); + + si.cb = sizeof(si); + ret = CreateProcessA(exe, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); + ok(ret, "Could not create process: %u\n", GetLastError()); + + winetest_wait_child_process(pi.hProcess); + + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); +} + +static void test_SxsLookupClrGuid(void) +{ + SIZE_T buffer_size; + BOOL ret; + + ret = pSxsLookupClrGuid(SXS_LOOKUP_CLR_GUID_FIND_CLR_CLASS, (GUID*)&CLSID_Test, NULL, NULL, 0, &buffer_size); + ok(ret == FALSE, "Expected FALSE, got %d\n", ret); + ok(GetLastError() == ERROR_NOT_FOUND, "Expected ERROR_NOT_FOUND, got %d\n", GetLastError()); + + run_child_process(); +} + +START_TEST(sxs) +{ + int argc; + char **argv; + + pSxsLookupClrGuid = (void *)GetProcAddress(LoadLibraryA("sxs.dll"), "SxsLookupClrGuid"); + + argc = winetest_get_mainargs(&argv); + if (argc > 2) + { + prepare_and_run_test(); + return; + } + + test_SxsLookupClrGuid(); +} -- 2.18.0