[KERNEL32]: Enable SxS support in CreateRemoteThread, and other misc. cleanups.

svn path=/trunk/; revision=56656
This commit is contained in:
Alex Ionescu 2012-05-24 19:20:33 +00:00
parent 13ae0280e5
commit 4d61ae25fc
2 changed files with 83 additions and 73 deletions

View file

@ -417,7 +417,7 @@
@ stdcall RtlAcquireSRWLockExclusive(ptr) @ stdcall RtlAcquireSRWLockExclusive(ptr)
@ stdcall RtlAcquireSRWLockShared(ptr) @ stdcall RtlAcquireSRWLockShared(ptr)
@ stdcall RtlActivateActivationContext(long ptr ptr) @ stdcall RtlActivateActivationContext(long ptr ptr)
;@ stdcall RtlActivateActivationContextEx @ stdcall RtlActivateActivationContextEx(long ptr ptr ptr)
@ fastcall RtlActivateActivationContextUnsafeFast(ptr ptr) @ fastcall RtlActivateActivationContextUnsafeFast(ptr ptr)
@ stdcall RtlAddAccessAllowedAce(ptr long long ptr) @ stdcall RtlAddAccessAllowedAce(ptr long long ptr)
@ stdcall RtlAddAccessAllowedAceEx(ptr long long long ptr) @ stdcall RtlAddAccessAllowedAceEx(ptr long long long ptr)

View file

@ -153,13 +153,13 @@ CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
*/ */
HANDLE HANDLE
WINAPI WINAPI
CreateRemoteThread(HANDLE hProcess, CreateRemoteThread(IN HANDLE hProcess,
LPSECURITY_ATTRIBUTES lpThreadAttributes, IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
DWORD dwStackSize, IN DWORD dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress, IN LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter, IN LPVOID lpParameter,
DWORD dwCreationFlags, IN DWORD dwCreationFlags,
LPDWORD lpThreadId) OUT LPDWORD lpThreadId)
{ {
NTSTATUS Status; NTSTATUS Status;
INITIAL_TEB InitialTeb; INITIAL_TEB InitialTeb;
@ -169,7 +169,12 @@ CreateRemoteThread(HANDLE hProcess,
POBJECT_ATTRIBUTES ObjectAttributes; POBJECT_ATTRIBUTES ObjectAttributes;
HANDLE hThread; HANDLE hThread;
ULONG Dummy; ULONG Dummy;
PTEB Teb;
THREAD_BASIC_INFORMATION ThreadBasicInfo;
PVOID ActivationContextStack = NULL;
ACTIVATION_CONTEXT_BASIC_INFORMATION ActCtxInfo;
ULONG_PTR Cookie;
ULONG ReturnLength;
DPRINT("CreateRemoteThread: hProcess: %ld dwStackSize: %ld lpStartAddress" DPRINT("CreateRemoteThread: hProcess: %ld dwStackSize: %ld lpStartAddress"
": %p lpParameter: %lx, dwCreationFlags: %lx\n", hProcess, ": %p lpParameter: %lx, dwCreationFlags: %lx\n", hProcess,
dwStackSize, lpStartAddress, lpParameter, dwCreationFlags); dwStackSize, lpStartAddress, lpParameter, dwCreationFlags);
@ -182,10 +187,10 @@ CreateRemoteThread(HANDLE hProcess,
/* Create the Stack */ /* Create the Stack */
Status = BaseCreateStack(hProcess, Status = BaseCreateStack(hProcess,
dwStackSize, dwStackSize,
dwCreationFlags & STACK_SIZE_PARAM_IS_A_RESERVATION ? dwCreationFlags & STACK_SIZE_PARAM_IS_A_RESERVATION ?
dwStackSize : 0, dwStackSize : 0,
&InitialTeb); &InitialTeb);
if(!NT_SUCCESS(Status)) if(!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -194,15 +199,15 @@ CreateRemoteThread(HANDLE hProcess,
/* Create Initial Context */ /* Create Initial Context */
BaseInitializeContext(&Context, BaseInitializeContext(&Context,
lpParameter, lpParameter,
lpStartAddress, lpStartAddress,
InitialTeb.StackBase, InitialTeb.StackBase,
1); 1);
/* initialize the attributes for the thread object */ /* initialize the attributes for the thread object */
ObjectAttributes = BaseFormatObjectAttributes(&LocalObjectAttributes, ObjectAttributes = BaseFormatObjectAttributes(&LocalObjectAttributes,
lpThreadAttributes, lpThreadAttributes,
NULL); NULL);
/* Create the Kernel Thread Object */ /* Create the Kernel Thread Object */
Status = NtCreateThread(&hThread, Status = NtCreateThread(&hThread,
@ -213,8 +218,9 @@ CreateRemoteThread(HANDLE hProcess,
&Context, &Context,
&InitialTeb, &InitialTeb,
TRUE); TRUE);
if(!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Fail the kernel create */
BaseFreeThreadStack(hProcess, &InitialTeb); BaseFreeThreadStack(hProcess, &InitialTeb);
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
return NULL; return NULL;
@ -223,71 +229,82 @@ CreateRemoteThread(HANDLE hProcess,
/* Are we in the same process? */ /* Are we in the same process? */
if (hProcess == NtCurrentProcess()) if (hProcess == NtCurrentProcess())
{ {
PTEB Teb;
PVOID ActivationContextStack = NULL;
THREAD_BASIC_INFORMATION ThreadBasicInfo;
#ifndef SXS_SUPPORT_FIXME
ACTIVATION_CONTEXT_BASIC_INFORMATION ActivationCtxInfo;
ULONG_PTR Cookie;
#endif
ULONG retLen;
/* Get the TEB */ /* Get the TEB */
Status = NtQueryInformationThread(hThread, Status = NtQueryInformationThread(hThread,
ThreadBasicInformation, ThreadBasicInformation,
&ThreadBasicInfo, &ThreadBasicInfo,
sizeof(ThreadBasicInfo), sizeof(ThreadBasicInfo),
&retLen); &ReturnLength);
if (NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* Allocate the Activation Context Stack */ /* Fail */
Status = RtlAllocateActivationContextStack(&ActivationContextStack); DbgPrint("SXS: %s - Failing thread create because "
"NtQueryInformationThread() failed with status %08lx\n",
__FUNCTION__, Status);
while (TRUE);
} }
if (NT_SUCCESS(Status)) /* Allocate the Activation Context Stack */
Status = RtlAllocateActivationContextStack(&ActivationContextStack);
if (!NT_SUCCESS(Status))
{ {
Teb = ThreadBasicInfo.TebBaseAddress; /* Fail */
DbgPrint("SXS: %s - Failing thread create because "
"RtlAllocateActivationContextStack() failed with status %08lx\n",
__FUNCTION__, Status);
while (TRUE);
}
/* Save it */ /* Save it */
Teb->ActivationContextStackPointer = ActivationContextStack; Teb = ThreadBasicInfo.TebBaseAddress;
#ifndef SXS_SUPPORT_FIXME Teb->ActivationContextStackPointer = ActivationContextStack;
/* Query the Context */
Status = RtlQueryInformationActivationContext(1, /* Query the Context */
0, // WARNING!!! THIS IS USING THE WIN32 FLAG BECAUSE REACTOS CONTINUES TO BE A POS!!! ///
NULL, Status = RtlQueryInformationActivationContext(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX,
ActivationContextBasicInformation, NULL,
&ActivationCtxInfo, 0,
sizeof(ActivationCtxInfo), ActivationContextBasicInformation,
&retLen); &ActCtxInfo,
if (NT_SUCCESS(Status)) sizeof(ActCtxInfo),
&ReturnLength);
if (!NT_SUCCESS(Status))
{
/* Fail */
DbgPrint("SXS: %s - Failing thread create because "
"RtlQueryInformationActivationContext() failed with status %08lx\n",
__FUNCTION__, Status);
while (TRUE);
}
/* Does it need to be activated? */
if ((ActCtxInfo.hActCtx) && !(ActCtxInfo.dwFlags & 1))
{
/* Activate it */
Status = RtlActivateActivationContextEx(RTL_ACTIVATE_ACTIVATION_CONTEXT_EX_FLAG_RELEASE_ON_STACK_DEALLOCATION,
Teb,
ActCtxInfo.hActCtx,
&Cookie);
if (!NT_SUCCESS(Status))
{ {
/* Does it need to be activated? */ /* Fail */
if (!ActivationCtxInfo.hActCtx) DbgPrint("SXS: %s - Failing thread create because "
{ "RtlActivateActivationContextEx() failed with status %08lx\n",
/* Activate it */ __FUNCTION__, Status);
Status = RtlActivateActivationContext(1, while (TRUE);
ActivationCtxInfo.hActCtx,
&Cookie);
if (!NT_SUCCESS(Status))
DPRINT1("RtlActivateActivationContext failed %x\n", Status);
}
} }
else
DPRINT1("RtlQueryInformationActivationContext failed %x\n", Status);
#endif
} }
else
DPRINT1("RtlAllocateActivationContextStack failed %x\n", Status);
} }
/* Notify CSR */ /* Notify CSR */
if (!BaseRunningInServerProcess) if (!BaseRunningInServerProcess)
{ {
/* Notify CSR */
Status = BasepNotifyCsrOfThread(hThread, &ClientId); Status = BasepNotifyCsrOfThread(hThread, &ClientId);
ASSERT(NT_SUCCESS(Status));
} }
else else
{ {
DPRINT("Server thread in Server. Handle: %lx\n", hProcess);
if (hProcess != NtCurrentProcess()) if (hProcess != NtCurrentProcess())
{ {
PCSR_CREATE_REMOTE_THREAD CsrCreateRemoteThread; PCSR_CREATE_REMOTE_THREAD CsrCreateRemoteThread;
@ -300,23 +317,16 @@ CreateRemoteThread(HANDLE hProcess,
{ {
/* Call it instead of going through LPC */ /* Call it instead of going through LPC */
Status = CsrCreateRemoteThread(hThread, &ClientId); Status = CsrCreateRemoteThread(hThread, &ClientId);
ASSERT(NT_SUCCESS(Status));
} }
} }
} }
if (!NT_SUCCESS(Status))
{
ASSERT(FALSE);
}
/* Success */ /* Success */
if(lpThreadId) *lpThreadId = HandleToUlong(ClientId.UniqueThread); if (lpThreadId) *lpThreadId = HandleToUlong(ClientId.UniqueThread);
/* Resume it if asked */ /* Resume it if asked */
if (!(dwCreationFlags & CREATE_SUSPENDED)) if (!(dwCreationFlags & CREATE_SUSPENDED)) NtResumeThread(hThread, &Dummy);
{
NtResumeThread(hThread, &Dummy);
}
/* Return handle to thread */ /* Return handle to thread */
return hThread; return hThread;