From 660ebacbe6daa3ab44e4417d7b99ef24e6de9c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gardou?= Date: Mon, 16 Feb 2015 21:12:51 +0000 Subject: [PATCH] [KERNEL32] Revert r64525 - Always allocate a guard page at the bottom of the stack. svn path=/trunk/; revision=66323 --- reactos/dll/win32/kernel32/client/utils.c | 43 ++++++++++++++++------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/reactos/dll/win32/kernel32/client/utils.c b/reactos/dll/win32/kernel32/client/utils.c index 80f9bab2eac..31134552815 100644 --- a/reactos/dll/win32/kernel32/client/utils.c +++ b/reactos/dll/win32/kernel32/client/utils.c @@ -358,6 +358,7 @@ BaseCreateStack(HANDLE hProcess, NTSTATUS Status; PIMAGE_NT_HEADERS Headers; ULONG_PTR Stack; + BOOLEAN UseGuard; ULONG PageSize, Dummy, AllocationGranularity; SIZE_T StackReserveHeader, StackCommitHeader, GuardPageSize, GuaranteedStackCommit; DPRINT("BaseCreateStack (hProcess: %p, Max: %lx, Current: %lx)\n", @@ -425,6 +426,18 @@ BaseCreateStack(HANDLE hProcess, /* Update the Stack Position */ Stack += StackReserve - StackCommit; + /* Check if we will need a guard page */ + if (StackReserve > StackCommit) + { + Stack -= PageSize; + StackCommit += PageSize; + UseGuard = TRUE; + } + else + { + UseGuard = FALSE; + } + /* Allocate memory for the stack */ Status = NtAllocateVirtualMemory(hProcess, (PVOID*)&Stack, @@ -444,21 +457,25 @@ BaseCreateStack(HANDLE hProcess, InitialTeb->StackLimit = (PVOID)Stack; /* Create a guard page */ - GuardPageSize = PageSize; - Status = NtProtectVirtualMemory(hProcess, - (PVOID*)&Stack, - &GuardPageSize, - PAGE_GUARD | PAGE_READWRITE, - &Dummy); - if (!NT_SUCCESS(Status)) + if (UseGuard) { - DPRINT1("Failure to set guard page\n"); - return Status; - } + /* Set the guard page */ + GuardPageSize = PAGE_SIZE; + Status = NtProtectVirtualMemory(hProcess, + (PVOID*)&Stack, + &GuardPageSize, + PAGE_GUARD | PAGE_READWRITE, + &Dummy); + if (!NT_SUCCESS(Status)) + { + DPRINT1("Failure to set guard page\n"); + return Status; + } - /* Update the Stack Limit keeping in mind the Guard Page */ - InitialTeb->StackLimit = (PVOID)((ULONG_PTR)InitialTeb->StackLimit + - GuardPageSize); + /* Update the Stack Limit keeping in mind the Guard Page */ + InitialTeb->StackLimit = (PVOID)((ULONG_PTR)InitialTeb->StackLimit + + GuardPageSize); + } /* We are done! */ return STATUS_SUCCESS;