- Properly create, grow and delete the kernel stack. Fixes potential memory leaks introduced in 20806. Thanks to Filip for verifying my math.

svn path=/trunk/; revision=20838
This commit is contained in:
Alex Ionescu
2006-01-13 21:56:51 +00:00
parent 0d0c70b9f0
commit 65ef48820e
5 changed files with 17 additions and 21 deletions
+3 -3
View File
@@ -853,9 +853,9 @@ KeInitializeThread(PKPROCESS Process,
Thread->Teb = Teb;
/* Set the Thread Stacks */
Thread->InitialStack = (PCHAR)KernelStack + KERNEL_STACK_SIZE;
Thread->StackBase = (PCHAR)KernelStack + KERNEL_STACK_SIZE;
Thread->StackLimit = (ULONG_PTR)KernelStack;
Thread->InitialStack = (PCHAR)KernelStack;
Thread->StackBase = (PCHAR)KernelStack;
Thread->StackLimit = (ULONG_PTR)KernelStack - KERNEL_STACK_SIZE;
Thread->KernelStackResident = TRUE;
/*
+4 -5
View File
@@ -63,7 +63,7 @@ PsInitializeIdleOrFirstThread(PEPROCESS Process,
BOOLEAN First)
{
PETHREAD Thread;
PVOID KernelStack;
ULONG_PTR KernelStack;
extern unsigned int init_stack;
Thread = ExAllocatePool(NonPagedPool, sizeof(ETHREAD));
@@ -71,11 +71,11 @@ PsInitializeIdleOrFirstThread(PEPROCESS Process,
Thread->ThreadsProcess = Process;
if (First)
{
KernelStack = (PVOID)init_stack;
KernelStack = init_stack;
}
else
{
KernelStack = MmCreateKernelStack(FALSE);
KernelStack = (ULONG_PTR)MmCreateKernelStack(FALSE) + KERNEL_STACK_SIZE;
}
KeInitializeThread(&Process->Pcb,
&Thread->Tcb,
@@ -84,8 +84,7 @@ PsInitializeIdleOrFirstThread(PEPROCESS Process,
NULL,
NULL,
NULL,
KernelStack);
Thread->Tcb.ApcQueueable = TRUE;
(PVOID)KernelStack);
InitializeListHead(&Thread->IrpList);
*ThreadPtr = Thread;
return STATUS_SUCCESS;
+1 -1
View File
@@ -194,7 +194,7 @@ PspDeleteThread(PVOID ObjectBody)
if(Thread->Tcb.Win32Thread != NULL) ExFreePool (Thread->Tcb.Win32Thread);
/* Release the Kernel Stack */
//MmDeleteKernelStack((PVOID)Thread->Tcb.StackLimit, FALSE);
MmDeleteKernelStack((PVOID)Thread->Tcb.StackLimit, Thread->Tcb.StackLimit);
/* Dereference the Process */
ObDereferenceObject(Process);
+4 -8
View File
@@ -116,7 +116,7 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
NTSTATUS Status;
HANDLE_TABLE_ENTRY CidEntry;
PVOID KernelStack;
ULONG_PTR KernelStack;
/* Reference the Process by handle or pointer, depending on what we got */
DPRINT("PspCreateThread: %x, %x, %x\n", ProcessHandle, TargetProcess, ThreadContext);
@@ -206,7 +206,7 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
/* Allocate Stack for non-GUI Thread */
DPRINT("Initialliazing Thread Stack\n");
KernelStack = MmCreateKernelStack(FALSE);
KernelStack = (ULONG_PTR)MmCreateKernelStack(FALSE) + KERNEL_STACK_SIZE;
/* Set the Process CID */
DPRINT("Initialliazing Thread PID and Parent Process\n");
@@ -236,7 +236,7 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
NULL,
ThreadContext,
TebBase,
KernelStack);
(PVOID)KernelStack);
} else {
@@ -254,7 +254,7 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
StartContext,
NULL,
NULL,
KernelStack);
(PVOID)KernelStack);
}
/*
@@ -269,10 +269,6 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
DPRINT("Running Thread Notify \n");
PspRunCreateThreadNotifyRoutines(Thread, TRUE);
/* FIXME: Use Lock */
DPRINT("Apcs Queueable: %d \n", Thread->Tcb.ApcQueueable);
Thread->Tcb.ApcQueueable = TRUE;
/* Suspend the Thread if we have to */
if (CreateSuspended) {
+5 -4
View File
@@ -71,7 +71,8 @@ NTSTATUS
NTAPI
PsConvertToGuiThread(VOID)
{
PVOID NewStack, OldStack;
ULONG_PTR NewStack;
PVOID OldStack;
PETHREAD Thread = PsGetCurrentThread();
PEPROCESS Process = PsGetCurrentProcess();
NTSTATUS Status;
@@ -102,7 +103,7 @@ PsConvertToGuiThread(VOID)
if (!Thread->Tcb.LargeStack)
{
/* We don't create one */
NewStack = MmCreateKernelStack(TRUE);
NewStack = (ULONG_PTR)MmCreateKernelStack(TRUE) + KERNEL_LARGE_STACK_SIZE;
if (!NewStack)
{
/* Panic in user-mode */
@@ -114,8 +115,8 @@ PsConvertToGuiThread(VOID)
KeEnterCriticalRegion();
/* Switch stacks */
OldStack = KeSwitchKernelStack((PVOID)((ULONG_PTR)NewStack + 0x3000),
NewStack);
OldStack = KeSwitchKernelStack((PVOID)NewStack,
(PVOID)(NewStack - KERNEL_STACK_SIZE));
/* Leave the critical region */
KeLeaveCriticalRegion();