[KERNEL32]: Cleanup of virtual memory functions.

[KERNEL32]: VirtualAllocEx should build a SEH frame.
[KERNEL32]: VirtualAllocEx should make sure the address given is not lower than the system's allocation granularity.

svn path=/trunk/; revision=52899
This commit is contained in:
Alex Ionescu 2011-07-26 14:33:22 +00:00
parent 0b545fdc07
commit 6d971807f7

View file

@ -1,19 +1,19 @@
/* /*
* PROJECT: ReactOS Win32 Base API * PROJECT: ReactOS Win32 Base API
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL - See COPYING in the top level directory
* FILE: dll/win32/kernel32/mem/virtual.c * FILE: dll/win32/kernel32/client/virtmem.c
* PURPOSE: Handles virtual memory APIs * PURPOSE: Handles virtual memory APIs
* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
*/ */
/* INCLUDES ******************************************************************/ /* INCLUDES *******************************************************************/
#include <k32.h> #include <k32.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS ******************************************************************/
/* /*
* @implemented * @implemented
@ -28,13 +28,33 @@ VirtualAllocEx(IN HANDLE hProcess,
{ {
NTSTATUS Status; NTSTATUS Status;
/* Make sure the address is within the granularity of the system (64K) */
if ((lpAddress) &&
(lpAddress < (PVOID)BaseStaticServerData->SysInfo.AllocationGranularity))
{
/* Fail the call */
SetLastError(ERROR_INVALID_PARAMETER);
return NULL;
}
/* Handle any possible exceptions */
_SEH2_TRY
{
/* Allocate the memory */ /* Allocate the memory */
Status = NtAllocateVirtualMemory(hProcess, Status = NtAllocateVirtualMemory(hProcess,
(PVOID *)&lpAddress, &lpAddress,
0, 0,
&dwSize, &dwSize,
flAllocationType, flAllocationType,
flProtect); flProtect);
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
Status = _SEH2_GetExceptionCode();
}
_SEH2_END;
/* Check for status */
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
/* We failed */ /* We failed */
@ -76,12 +96,13 @@ VirtualFreeEx(IN HANDLE hProcess,
{ {
NTSTATUS Status; NTSTATUS Status;
if (dwSize == 0 || !(dwFreeType & MEM_RELEASE)) /* Validate size and flags */
if (!(dwSize) || !(dwFreeType & MEM_RELEASE))
{ {
/* Free the memory */ /* Free the memory */
Status = NtFreeVirtualMemory(hProcess, Status = NtFreeVirtualMemory(hProcess,
(PVOID *)&lpAddress, &lpAddress,
(PULONG)&dwSize, &dwSize,
dwFreeType); dwFreeType);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
@ -94,6 +115,7 @@ VirtualFreeEx(IN HANDLE hProcess,
return TRUE; return TRUE;
} }
/* Invalid combo */
BaseSetLastNTError(STATUS_INVALID_PARAMETER); BaseSetLastNTError(STATUS_INVALID_PARAMETER);
return FALSE; return FALSE;
} }
@ -270,14 +292,12 @@ VirtualUnlock(IN LPVOID lpAddress,
*/ */
UINT UINT
WINAPI WINAPI
GetWriteWatch( GetWriteWatch(IN DWORD dwFlags,
DWORD dwFlags, IN PVOID lpBaseAddress,
PVOID lpBaseAddress, IN SIZE_T dwRegionSize,
SIZE_T dwRegionSize, IN PVOID *lpAddresses,
PVOID *lpAddresses, OUT PULONG_PTR lpdwCount,
PULONG_PTR lpdwCount, OUT PULONG lpdwGranularity)
PULONG lpdwGranularity
)
{ {
NTSTATUS Status; NTSTATUS Status;
@ -288,7 +308,6 @@ GetWriteWatch(
lpAddresses, lpAddresses,
lpdwCount, lpdwCount,
lpdwGranularity); lpdwGranularity);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -303,17 +322,14 @@ GetWriteWatch(
*/ */
UINT UINT
WINAPI WINAPI
ResetWriteWatch( ResetWriteWatch(IN LPVOID lpBaseAddress,
LPVOID lpBaseAddress, IN SIZE_T dwRegionSize)
SIZE_T dwRegionSize
)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = NtResetWriteWatch(NtCurrentProcess(), Status = NtResetWriteWatch(NtCurrentProcess(),
lpBaseAddress, lpBaseAddress,
dwRegionSize); dwRegionSize);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -328,18 +344,13 @@ ResetWriteWatch(
*/ */
BOOL BOOL
WINAPI WINAPI
AllocateUserPhysicalPages( AllocateUserPhysicalPages(IN HANDLE hProcess,
HANDLE hProcess, IN PULONG_PTR NumberOfPages,
PULONG_PTR NumberOfPages, OUT PULONG_PTR UserPfnArray)
PULONG_PTR UserPfnArray
)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = NtAllocateUserPhysicalPages(hProcess, Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray);
NumberOfPages,
UserPfnArray);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -354,18 +365,13 @@ AllocateUserPhysicalPages(
*/ */
BOOL BOOL
WINAPI WINAPI
FreeUserPhysicalPages( FreeUserPhysicalPages(IN HANDLE hProcess,
HANDLE hProcess, IN PULONG_PTR NumberOfPages,
PULONG_PTR NumberOfPages, IN PULONG_PTR PageArray)
PULONG_PTR PageArray
)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = NtFreeUserPhysicalPages(hProcess, Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray);
NumberOfPages,
PageArray);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -380,18 +386,13 @@ FreeUserPhysicalPages(
*/ */
BOOL BOOL
WINAPI WINAPI
MapUserPhysicalPages( MapUserPhysicalPages(IN PVOID VirtualAddress,
PVOID VirtualAddress, IN ULONG_PTR NumberOfPages,
ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
PULONG_PTR PageArray OPTIONAL
)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = NtMapUserPhysicalPages(VirtualAddress, Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray);
NumberOfPages,
PageArray);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);
@ -406,18 +407,15 @@ MapUserPhysicalPages(
*/ */
BOOL BOOL
WINAPI WINAPI
MapUserPhysicalPagesScatter( MapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses,
PVOID *VirtualAddresses, IN ULONG_PTR NumberOfPages,
ULONG_PTR NumberOfPages, OUT PULONG_PTR PageArray OPTIONAL)
PULONG_PTR PageArray OPTIONAL
)
{ {
NTSTATUS Status; NTSTATUS Status;
Status = NtMapUserPhysicalPagesScatter(VirtualAddresses, Status = NtMapUserPhysicalPagesScatter(VirtualAddresses,
NumberOfPages, NumberOfPages,
PageArray); PageArray);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
BaseSetLastNTError(Status); BaseSetLastNTError(Status);