Author: ion Date: Tue Jul 26 14:33:22 2011 New Revision: 52899
URL: http://svn.reactos.org/svn/reactos?rev=52899&view=rev Log: [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.
Modified: trunk/reactos/dll/win32/kernel32/client/virtmem.c
Modified: trunk/reactos/dll/win32/kernel32/client/virtmem.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/client/v... ============================================================================== --- trunk/reactos/dll/win32/kernel32/client/virtmem.c [iso-8859-1] (original) +++ trunk/reactos/dll/win32/kernel32/client/virtmem.c [iso-8859-1] Tue Jul 26 14:33:22 2011 @@ -1,19 +1,19 @@ /* * PROJECT: ReactOS Win32 Base API * 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 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) */
-/* INCLUDES ******************************************************************/ +/* INCLUDES *******************************************************************/
#include <k32.h>
#define NDEBUG #include <debug.h>
-/* FUNCTIONS *****************************************************************/ +/* FUNCTIONS ******************************************************************/
/* * @implemented @@ -27,14 +27,34 @@ IN DWORD flProtect) { NTSTATUS Status; - - /* Allocate the memory */ - Status = NtAllocateVirtualMemory(hProcess, - (PVOID *)&lpAddress, - 0, - &dwSize, - flAllocationType, - flProtect); + + /* 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 */ + Status = NtAllocateVirtualMemory(hProcess, + &lpAddress, + 0, + &dwSize, + flAllocationType, + flProtect); + } + _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) + { + Status = _SEH2_GetExceptionCode(); + } + _SEH2_END; + + /* Check for status */ if (!NT_SUCCESS(Status)) { /* We failed */ @@ -76,12 +96,13 @@ { NTSTATUS Status;
- if (dwSize == 0 || !(dwFreeType & MEM_RELEASE)) + /* Validate size and flags */ + if (!(dwSize) || !(dwFreeType & MEM_RELEASE)) { /* Free the memory */ Status = NtFreeVirtualMemory(hProcess, - (PVOID *)&lpAddress, - (PULONG)&dwSize, + &lpAddress, + &dwSize, dwFreeType); if (!NT_SUCCESS(Status)) { @@ -94,6 +115,7 @@ return TRUE; }
+ /* Invalid combo */ BaseSetLastNTError(STATUS_INVALID_PARAMETER); return FALSE; } @@ -270,14 +292,12 @@ */ UINT WINAPI -GetWriteWatch( - DWORD dwFlags, - PVOID lpBaseAddress, - SIZE_T dwRegionSize, - PVOID *lpAddresses, - PULONG_PTR lpdwCount, - PULONG lpdwGranularity - ) +GetWriteWatch(IN DWORD dwFlags, + IN PVOID lpBaseAddress, + IN SIZE_T dwRegionSize, + IN PVOID *lpAddresses, + OUT PULONG_PTR lpdwCount, + OUT PULONG lpdwGranularity) { NTSTATUS Status;
@@ -288,7 +308,6 @@ lpAddresses, lpdwCount, lpdwGranularity); - if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); @@ -303,17 +322,14 @@ */ UINT WINAPI -ResetWriteWatch( - LPVOID lpBaseAddress, - SIZE_T dwRegionSize - ) +ResetWriteWatch(IN LPVOID lpBaseAddress, + IN SIZE_T dwRegionSize) { NTSTATUS Status;
Status = NtResetWriteWatch(NtCurrentProcess(), lpBaseAddress, dwRegionSize); - if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status); @@ -328,96 +344,78 @@ */ BOOL WINAPI -AllocateUserPhysicalPages( - HANDLE hProcess, - PULONG_PTR NumberOfPages, - PULONG_PTR UserPfnArray - ) -{ - NTSTATUS Status; - - Status = NtAllocateUserPhysicalPages(hProcess, - NumberOfPages, - UserPfnArray); - - if (!NT_SUCCESS(Status)) - { - BaseSetLastNTError(Status); - return FALSE; - } - - return TRUE; -} - -/* - * @implemented - */ -BOOL -WINAPI -FreeUserPhysicalPages( - HANDLE hProcess, - PULONG_PTR NumberOfPages, - PULONG_PTR PageArray - ) -{ - NTSTATUS Status; - - Status = NtFreeUserPhysicalPages(hProcess, - NumberOfPages, - PageArray); - - if (!NT_SUCCESS(Status)) - { - BaseSetLastNTError(Status); - return FALSE; - } - - return TRUE; -} - -/* - * @implemented - */ -BOOL -WINAPI -MapUserPhysicalPages( - PVOID VirtualAddress, - ULONG_PTR NumberOfPages, - PULONG_PTR PageArray OPTIONAL - ) -{ - NTSTATUS Status; - - Status = NtMapUserPhysicalPages(VirtualAddress, - NumberOfPages, - PageArray); - - if (!NT_SUCCESS(Status)) - { - BaseSetLastNTError(Status); - return FALSE; - } - - return TRUE; -} - -/* - * @implemented - */ -BOOL -WINAPI -MapUserPhysicalPagesScatter( - PVOID *VirtualAddresses, - ULONG_PTR NumberOfPages, - PULONG_PTR PageArray OPTIONAL - ) +AllocateUserPhysicalPages(IN HANDLE hProcess, + IN PULONG_PTR NumberOfPages, + OUT PULONG_PTR UserPfnArray) +{ + NTSTATUS Status; + + Status = NtAllocateUserPhysicalPages(hProcess, NumberOfPages, UserPfnArray); + if (!NT_SUCCESS(Status)) + { + BaseSetLastNTError(Status); + return FALSE; + } + + return TRUE; +} + +/* + * @implemented + */ +BOOL +WINAPI +FreeUserPhysicalPages(IN HANDLE hProcess, + IN PULONG_PTR NumberOfPages, + IN PULONG_PTR PageArray) +{ + NTSTATUS Status; + + Status = NtFreeUserPhysicalPages(hProcess, NumberOfPages, PageArray); + if (!NT_SUCCESS(Status)) + { + BaseSetLastNTError(Status); + return FALSE; + } + + return TRUE; +} + +/* + * @implemented + */ +BOOL +WINAPI +MapUserPhysicalPages(IN PVOID VirtualAddress, + IN ULONG_PTR NumberOfPages, + OUT PULONG_PTR PageArray OPTIONAL) +{ + NTSTATUS Status; + + Status = NtMapUserPhysicalPages(VirtualAddress, NumberOfPages, PageArray); + if (!NT_SUCCESS(Status)) + { + BaseSetLastNTError(Status); + return FALSE; + } + + return TRUE; +} + +/* + * @implemented + */ +BOOL +WINAPI +MapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses, + IN ULONG_PTR NumberOfPages, + OUT PULONG_PTR PageArray OPTIONAL) { NTSTATUS Status;
Status = NtMapUserPhysicalPagesScatter(VirtualAddresses, NumberOfPages, PageArray); - if (!NT_SUCCESS(Status)) { BaseSetLastNTError(Status);