reactos/dll/win32/kernel32/wine/actctx.c

199 lines
6.3 KiB
C
Raw Normal View History

/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: dll/win32/kernel32/wine/actctx.c
* PURPOSE: Activation contexts
* PROGRAMMERS: Jacek Caban for CodeWeavers
* Eric Pouech
* Jon Griffiths
* Dmitry Chapyshev (dmitry@reactos.org)
* Samuel Serapi<EFBFBD>n
*/
/* Partly synched with Wine 1.7.17 */
#include <k32.h>
[KERNEL32]: While working on the CMAKE branch, Amine and myself discovered a rather serious issue in kernel32 (and perhaps other libraries as well). Unlike rbuild, CMake does not allow you to export non-existant DLL functions (try it: add "poopyhead" in kernel32's exports under RBuild, and will it export "poopyhead", God knowing what that will actually link to). As an additional feature on top of the "allow non-existing functions to be exported" "feature", because rbuild generates and links STDCALL function names without the proper decoration (vs. enforcing decoration at linking, but only removing it at export-time), this allows the definition (as an export) of a STDCALL function that is completely different from the actual function itself. For example, the 5-parameter Foo function is normally Foo@20, while the 3-parameter Foo function woudl be Foo@12. Linking one against the other would fail (say, 2 parameters were added to Foo in a newer version). However, under RBUILD, both of these would appear as "Foo", and the linker/compiler would happilly connect the caller of Foo@3 (that has pushed 3 parameters) to the receiving side of Foo@5 (that is about to pop 5 parameters). Even -if- decorations WERE to be applied, Foo@12 would STILL succeed, because of the first feature, which would enable the export of Foo@12 even though no such function exist. In a further, bizare, twist of fate, the behavior of this RBUILD "feature", when the target function is not found, is to link the exported DLL TO ITSELF. Therefore, one can see how, previously to this patch, kernel32.dll would import a dozen functions from itself (all the non-existing functions). To really seal the deal, the behavior of exported functions used by kernel32, but that are actually forwarded to another DLL deserves a special mention. GetLastError, for example, merely forwards to RtlGetLastWin32Error, so it is normal behavior to use a #define in the C code so that all internal calls to the function are routed correctly. This did not happen, so instead, kernel32 tried importing/linking/exporting GetLastError, but this symbol is not found in the binary, because it is only a forwarder. This caused kernel32 to import from itself (the behavior when an exported symbol is not found). When importing from itself, the loader would now find the _forwarded_ for GetLastError, and correctly link with ntdll. What should be a one-liner of assembly (inline TEB access) thus became a triple-call indirection (GetLastError@0->StubLastError@0->__impGetLastError@0->__impRtlGetLastWin32Error->RtlGetLastWin32Error. While analyzing these issues, we also realized a strange macro SetLastErrorByStatus that manually tried to perform what there already exists a function for: RtlSetLastNtStatusFromWin32Error. And, in an exciting coda, we also realized that our Server 2003 Kernel32 exports more than a dozen Windows 95 APIs, through an auto-stub generation mechanism within winebuild, that gets linked as an object behind the scenes. [KERNEL32]: Removed all Win95 exports, cleaned up exports. [KERNEL32]: Fixed up set/get error macros by making them inline and/or calling the correct ntdll function. [KERNEL32]: Removed bizare calls to Wine-internal/specific APIs from our core Win32 DLL. [KERNEL32]: Wrote stubs for all functions which should be exported, and set the correct number of parameters for them. [KERNEL32]: Kernel32 is smaller, loads faster, does not export Windows 95 functions, does not export non-existing functions, and does not import from itself anymore. Note: This is one of the many failings of RBUILD the CMAKE system has helped us discover. I believe these issues are serious enough to warrant an immediate sync with trunk, but rest assured, there are many more completely broken, infinitely-regressing things that we discovered while switching to CMAKE. svn path=/trunk/; revision=48475
2010-08-07 05:02:58 +00:00
#define NDEBUG
#include <debug.h>
DEBUG_CHANNEL(actctx);
#define ACTCTX_FAKE_HANDLE ((HANDLE) 0xf00baa)
/***********************************************************************
* CreateActCtxA (KERNEL32.@)
*
* Create an activation context.
*/
HANDLE WINAPI CreateActCtxA(PCACTCTXA pActCtx)
{
ACTCTXW actw;
SIZE_T len;
HANDLE ret = INVALID_HANDLE_VALUE;
LPWSTR src = NULL, assdir = NULL, resname = NULL, appname = NULL;
TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
if (!pActCtx || pActCtx->cbSize != sizeof(*pActCtx))
{
SetLastError(ERROR_INVALID_PARAMETER);
return INVALID_HANDLE_VALUE;
}
actw.cbSize = sizeof(actw);
actw.dwFlags = pActCtx->dwFlags;
if (pActCtx->lpSource)
{
len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, NULL, 0);
src = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!src) return INVALID_HANDLE_VALUE;
MultiByteToWideChar(CP_ACP, 0, pActCtx->lpSource, -1, src, len);
}
actw.lpSource = src;
if (actw.dwFlags & ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID)
actw.wProcessorArchitecture = pActCtx->wProcessorArchitecture;
if (actw.dwFlags & ACTCTX_FLAG_LANGID_VALID)
actw.wLangId = pActCtx->wLangId;
if (actw.dwFlags & ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID)
{
len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, NULL, 0);
assdir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!assdir) goto done;
MultiByteToWideChar(CP_ACP, 0, pActCtx->lpAssemblyDirectory, -1, assdir, len);
actw.lpAssemblyDirectory = assdir;
}
if (actw.dwFlags & ACTCTX_FLAG_RESOURCE_NAME_VALID)
{
if ((ULONG_PTR)pActCtx->lpResourceName >> 16)
{
len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, NULL, 0);
resname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!resname) goto done;
MultiByteToWideChar(CP_ACP, 0, pActCtx->lpResourceName, -1, resname, len);
actw.lpResourceName = resname;
}
else actw.lpResourceName = (LPCWSTR)pActCtx->lpResourceName;
}
if (actw.dwFlags & ACTCTX_FLAG_APPLICATION_NAME_VALID)
{
len = MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, NULL, 0);
appname = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!appname) goto done;
MultiByteToWideChar(CP_ACP, 0, pActCtx->lpApplicationName, -1, appname, len);
actw.lpApplicationName = appname;
}
if (actw.dwFlags & ACTCTX_FLAG_HMODULE_VALID)
actw.hModule = pActCtx->hModule;
ret = CreateActCtxW(&actw);
done:
HeapFree(GetProcessHeap(), 0, src);
HeapFree(GetProcessHeap(), 0, assdir);
HeapFree(GetProcessHeap(), 0, resname);
HeapFree(GetProcessHeap(), 0, appname);
return ret;
}
/***********************************************************************
* CreateActCtxW (KERNEL32.@)
*
* Create an activation context.
*/
HANDLE WINAPI CreateActCtxW(PCACTCTXW pActCtx)
{
NTSTATUS status;
HANDLE hActCtx;
TRACE("%p %08x\n", pActCtx, pActCtx ? pActCtx->dwFlags : 0);
if ((status = RtlCreateActivationContext(0, (PVOID)pActCtx, 0, NULL, NULL, &hActCtx)))
{
SetLastError(RtlNtStatusToDosError(status));
return INVALID_HANDLE_VALUE;
}
return hActCtx;
}
/***********************************************************************
* FindActCtxSectionStringA (KERNEL32.@)
*
* Find information about a string in an activation context.
*/
BOOL WINAPI FindActCtxSectionStringA(DWORD dwFlags, const GUID* lpExtGuid,
ULONG ulId, LPCSTR lpSearchStr,
PACTCTX_SECTION_KEYED_DATA pInfo)
{
LPWSTR search_str;
DWORD len;
BOOL ret;
TRACE("%08x %s %u %s %p\n", dwFlags, debugstr_guid(lpExtGuid),
ulId, debugstr_a(lpSearchStr), pInfo);
if (!lpSearchStr || !pInfo)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
len = MultiByteToWideChar(CP_ACP, 0, lpSearchStr, -1, NULL, 0);
search_str = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, lpSearchStr, -1, search_str, len);
ret = FindActCtxSectionStringW(dwFlags, lpExtGuid, ulId, search_str, pInfo);
HeapFree(GetProcessHeap(), 0, search_str);
return ret;
}
/***********************************************************************
* FindActCtxSectionStringW (KERNEL32.@)
*
* Find information about a string in an activation context.
*/
BOOL WINAPI FindActCtxSectionStringW(DWORD dwFlags, const GUID* lpExtGuid,
ULONG ulId, LPCWSTR lpSearchStr,
PACTCTX_SECTION_KEYED_DATA pInfo)
{
UNICODE_STRING us;
NTSTATUS status;
if (!pInfo)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
RtlInitUnicodeString(&us, lpSearchStr);
if ((status = RtlFindActivationContextSectionString(dwFlags, lpExtGuid, ulId, &us, pInfo)))
{
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
return TRUE;
}
/***********************************************************************
* FindActCtxSectionGuid (KERNEL32.@)
*
* Find information about a GUID in an activation context.
*/
BOOL WINAPI FindActCtxSectionGuid(DWORD dwFlags, const GUID* lpExtGuid,
ULONG ulId, const GUID* lpSearchGuid,
PACTCTX_SECTION_KEYED_DATA pInfo)
{
NTSTATUS status;
if ((status = RtlFindActivationContextSectionGuid(dwFlags, lpExtGuid, ulId, lpSearchGuid, pInfo)))
{
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
return TRUE;
}
/* EOF */