Fix NtAllocateVirtualMemory Parameters

Robert Shearman rob at codeweavers.com
Sun Oct 10 08:03:20 CDT 2004


Hi,

As documented [1] and as found by experimentation, our declaration of 
NtAllocateVirtualMemory is currently wrong.
This patch fixes this to be more compatible with the Windows (and 
ReactOS) version.
I have only compile tested this patch.

Rob

[1] 
http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Memory%20Management/Virtual%20Memory/NtAllocateVirtualMemory.html

Changelog:
Fix NtAllocateVirtualMemory declaration and fix users of the function.
-------------- next part --------------
Index: wine/include/winternl.h
===================================================================
RCS file: /home/wine/wine/include/winternl.h,v
retrieving revision 1.94
diff -u -p -r1.94 winternl.h
--- wine/include/winternl.h	10 Sep 2004 21:13:55 -0000	1.94
+++ wine/include/winternl.h	10 Oct 2004 12:47:17 -0000
@@ -1268,7 +1268,7 @@ NTSTATUS  WINAPI NtAccessCheck(PSECURITY
 NTSTATUS  WINAPI NtAdjustGroupsToken(HANDLE,BOOLEAN,PTOKEN_GROUPS,ULONG,PTOKEN_GROUPS,PULONG);
 NTSTATUS  WINAPI NtAdjustPrivilegesToken(HANDLE,BOOLEAN,PTOKEN_PRIVILEGES,DWORD,PTOKEN_PRIVILEGES,PDWORD);
 NTSTATUS  WINAPI NtAlertThread(HANDLE ThreadHandle);
-NTSTATUS  WINAPI NtAllocateVirtualMemory(HANDLE,PVOID*,PVOID,ULONG*,ULONG,ULONG);
+NTSTATUS  WINAPI NtAllocateVirtualMemory(HANDLE,PVOID*,ULONG,ULONG*,ULONG,ULONG);
 NTSTATUS  WINAPI NtCancelIoFile(HANDLE,PIO_STATUS_BLOCK);
 NTSTATUS  WINAPI NtCancelTimer(HANDLE, BOOLEAN*);
 NTSTATUS  WINAPI NtClearEvent(HANDLE);
Index: wine/dlls/kernel/process.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/process.c,v
retrieving revision 1.76
diff -u -p -r1.76 process.c
--- wine/dlls/kernel/process.c	15 Sep 2004 18:02:50 -0000	1.76
+++ wine/dlls/kernel/process.c	10 Oct 2004 12:47:18 -0000
@@ -379,6 +379,7 @@ static BOOL build_initial_environment( c
     size *= sizeof(WCHAR);
 
     /* Now allocate the environment */
+    ptr = NULL;
     if (NtAllocateVirtualMemory(NtCurrentProcess(), &ptr, 0, &size,
                                 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) != STATUS_SUCCESS)
         return FALSE;
@@ -719,7 +720,8 @@ static RTL_USER_PROCESS_PARAMETERS *init
     RTL_USER_PROCESS_PARAMETERS *params;
 
     size = info_size;
-    if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &size,
+    ptr = NULL;
+    if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &size,
                                  MEM_COMMIT, PAGE_READWRITE ) != STATUS_SUCCESS)
         return NULL;
 
@@ -748,7 +750,8 @@ static RTL_USER_PROCESS_PARAMETERS *init
     /* environment needs to be a separate memory block */
     env_size = info_size - params->Size;
     if (!env_size) env_size = 1;
-    if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &env_size,
+    ptr = NULL;
+    if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
                                  MEM_COMMIT, PAGE_READWRITE ) != STATUS_SUCCESS)
         return NULL;
     memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
Index: wine/dlls/kernel/virtual.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/virtual.c,v
retrieving revision 1.3
diff -u -p -r1.3 virtual.c
--- wine/dlls/kernel/virtual.c	21 May 2004 20:58:44 -0000	1.3
+++ wine/dlls/kernel/virtual.c	10 Oct 2004 12:47:18 -0000
@@ -86,10 +86,10 @@ LPVOID WINAPI VirtualAllocEx(
               DWORD type,      /* [in] Type of allocation */
               DWORD protect )  /* [in] Type of access protection */
 {
-    LPVOID ret;
+    LPVOID ret = addr;
     NTSTATUS status;
 
-    if ((status = NtAllocateVirtualMemory( hProcess, &ret, addr, &size, type, protect )))
+    if ((status = NtAllocateVirtualMemory( hProcess, &ret, 0, &size, type, protect )))
     {
         SetLastError( RtlNtStatusToDosError(status) );
         ret = NULL;
Index: wine/dlls/ntdll/env.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/env.c,v
retrieving revision 1.13
diff -u -p -r1.13 env.c
--- wine/dlls/ntdll/env.c	8 Sep 2004 01:25:05 -0000	1.13
+++ wine/dlls/ntdll/env.c	10 Oct 2004 12:47:18 -0000
@@ -65,10 +65,14 @@ NTSTATUS WINAPI RtlCreateEnvironment(BOO
     else 
     {
         ULONG       size = 1;
-        nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &size, 
+        PVOID       addr = NULL;
+        nts = NtAllocateVirtualMemory(NtCurrentProcess(), &addr, 0, &size, 
                                       MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
         if (nts == STATUS_SUCCESS)
+        {
+            *env = addr;
             memset(*env, 0, size);
+        }
     }
 
     return nts;
@@ -446,7 +450,8 @@ NTSTATUS WINAPI RtlCreateProcessParamete
             + RuntimeInfo->MaximumLength);
 
     total_size = size;
-    if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, NULL, &total_size,
+    ptr = NULL;
+    if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &total_size,
                                            MEM_COMMIT, PAGE_READWRITE )) == STATUS_SUCCESS)
     {
         RTL_USER_PROCESS_PARAMETERS *params = ptr;
Index: wine/dlls/ntdll/virtual.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/virtual.c,v
retrieving revision 1.40
diff -u -p -r1.40 virtual.c
--- wine/dlls/ntdll/virtual.c	22 Sep 2004 04:03:10 -0000	1.40
+++ wine/dlls/ntdll/virtual.c	10 Oct 2004 12:47:19 -0000
@@ -1153,7 +1153,7 @@ void VIRTUAL_UseLargeAddressSpace(void)
  *             NtAllocateVirtualMemory   (NTDLL.@)
  *             ZwAllocateVirtualMemory   (NTDLL.@)
  */
-NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, PVOID addr,
+NTSTATUS WINAPI NtAllocateVirtualMemory( HANDLE process, PVOID *ret, ULONG zero_bits,
                                          ULONG *size_ptr, ULONG type, ULONG protect )
 {
     void *base;
@@ -1162,7 +1162,7 @@ NTSTATUS WINAPI NtAllocateVirtualMemory(
     NTSTATUS status = STATUS_SUCCESS;
     struct file_view *view;
 
-    TRACE("%p %p %08lx %lx %08lx\n", process, addr, size, type, protect );
+    TRACE("%p %p %08lx %lx %08lx\n", process, *ret, size, type, protect );
 
     if (!size) return STATUS_INVALID_PARAMETER;
 
@@ -1176,13 +1176,13 @@ NTSTATUS WINAPI NtAllocateVirtualMemory(
 
     if (size > 0x7fc00000) return STATUS_WORKING_SET_LIMIT_RANGE; /* 2Gb - 4Mb */
 
-    if (addr)
+    if (*ret)
     {
         if (type & MEM_RESERVE) /* Round down to 64k boundary */
-            base = ROUND_ADDR( addr, granularity_mask );
+            base = ROUND_ADDR( *ret, granularity_mask );
         else
-            base = ROUND_ADDR( addr, page_mask );
-        size = (((UINT_PTR)addr + size + page_mask) & ~page_mask) - (UINT_PTR)base;
+            base = ROUND_ADDR( *ret, page_mask );
+        size = (((UINT_PTR)*ret + size + page_mask) & ~page_mask) - (UINT_PTR)base;
 
         /* disallow low 64k, wrap-around and kernel space */
         if (((char *)base <= (char *)granularity_mask) ||
@@ -1201,6 +1201,9 @@ NTSTATUS WINAPI NtAllocateVirtualMemory(
         WARN("MEM_TOP_DOWN ignored\n");
         type &= ~MEM_TOP_DOWN;
     }
+
+    if (zero_bits)
+        WARN("zero_bits %lu ignored\n", zero_bits);
 
     /* Compute the alloc type flags */
 
Index: wine/dlls/ntdll/thread.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/thread.c,v
retrieving revision 1.22
diff -u -p -r1.22 thread.c
--- wine/dlls/ntdll/thread.c	21 Sep 2004 00:23:50 -0000	1.22
+++ wine/dlls/ntdll/thread.c	10 Oct 2004 12:47:19 -0000
@@ -142,7 +142,8 @@ void thread_init(void)
     server_init_thread( thread_info.pid, thread_info.tid, NULL );
 
     /* create a memory view for the TEB */
-    NtAllocateVirtualMemory( GetCurrentProcess(), &addr, teb, &size,
+    addr = teb;
+    NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size,
                              MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
 
     /* create the process heap */
@@ -179,7 +180,8 @@ static void start_thread( struct wine_pt
 
     /* allocate a memory view for the stack */
     size = info->stack_size;
-    NtAllocateVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, info->stack_base,
+    teb->DeallocationStack = info->stack_base;
+    NtAllocateVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, 0,
                              &size, MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
     /* limit is lower than base since the stack grows down */
     teb->Tib.StackBase  = (char *)info->stack_base + info->stack_size;
@@ -263,7 +265,8 @@ NTSTATUS WINAPI RtlCreateUserThread( HAN
     teb->wait_fd[1]  = -1;
     teb->htask16     = NtCurrentTeb()->htask16;
 
-    NtAllocateVirtualMemory( GetCurrentProcess(), &info->pthread_info.teb_base, teb, &size,
+    info->pthread_info.teb_base = teb;
+    NtAllocateVirtualMemory( GetCurrentProcess(), &info->pthread_info.teb_base, 0, &size,
                              MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
     info->pthread_info.teb_size = size;
     info->pthread_info.teb_sel  = teb->teb_sel;
Index: wine/dlls/ntdll/loader.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/loader.c,v
retrieving revision 1.77
diff -u -p -r1.77 loader.c
--- wine/dlls/ntdll/loader.c	15 Sep 2004 18:02:49 -0000	1.77
+++ wine/dlls/ntdll/loader.c	10 Oct 2004 12:47:20 -0000
@@ -1139,7 +1139,8 @@ static void load_builtin_callback( void 
         return;
     }
     wm->ldr.Flags |= LDR_WINE_INTERNAL;
-    NtAllocateVirtualMemory( GetCurrentProcess(), &addr, module, &nt->OptionalHeader.SizeOfImage,
+    addr = module;
+    NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &nt->OptionalHeader.SizeOfImage,
                              MEM_SYSTEM | MEM_IMAGE, PAGE_EXECUTE_WRITECOPY );
 
     /* fixup imports */
Index: wine/dlls/ntdll/relay.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/relay.c,v
retrieving revision 1.15
diff -u -p -r1.15 relay.c
--- wine/dlls/ntdll/relay.c	13 Aug 2004 23:53:44 -0000	1.15
+++ wine/dlls/ntdll/relay.c	10 Oct 2004 12:47:20 -0000
@@ -841,7 +841,8 @@ void SNOOP_SetupDLL(HMODULE hmod)
     if (p > (*dll)->name && !strcasecmp( p, ".dll" )) *p = 0;
 
     size = exports->NumberOfFunctions * sizeof(SNOOP_FUN);
-    NtAllocateVirtualMemory(GetCurrentProcess(), &addr, NULL, &size,
+    addr = NULL;
+    NtAllocateVirtualMemory(GetCurrentProcess(), &addr, 0, &size,
                             MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
     if (!addr) {
         RtlFreeHeap(GetProcessHeap(),0,*dll);
@@ -1006,9 +1007,9 @@ void WINAPI SNOOP_DoEntry( CONTEXT86 *co
 	}
 	if (!*rets) {
                 SIZE_T size = 4096;
-                VOID* addr;
+                VOID* addr = NULL;
 
-                NtAllocateVirtualMemory(GetCurrentProcess(), &addr, NULL, &size, 
+                NtAllocateVirtualMemory(GetCurrentProcess(), &addr, 0, &size, 
                                         MEM_COMMIT | MEM_RESERVE,
                                         PAGE_EXECUTE_READWRITE);
                 if (!addr) return;
Index: wine/dlls/ntdll/heap.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/heap.c,v
retrieving revision 1.28
diff -u -p -r1.28 heap.c
--- wine/dlls/ntdll/heap.c	15 Jun 2004 00:47:01 -0000	1.28
+++ wine/dlls/ntdll/heap.c	10 Oct 2004 12:47:20 -0000
@@ -343,12 +343,12 @@ static inline BOOL HEAP_Commit( SUBHEAP 
     if (size > subheap->size) size = subheap->size;
     if (size <= subheap->commitSize) return TRUE;
     size -= subheap->commitSize;
-    if (NtAllocateVirtualMemory( GetCurrentProcess(), &ptr, (char *)subheap + subheap->commitSize,
+    ptr = (char *)subheap + subheap->commitSize;
+    if (NtAllocateVirtualMemory( GetCurrentProcess(), &ptr, 0,
                                  &size, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
     {
-        WARN("Could not commit %08lx bytes at %08lx for heap %08lx\n",
-                 size, (DWORD)((char *)subheap + subheap->commitSize),
-                 (DWORD)subheap->heap );
+        WARN("Could not commit %08lx bytes at %p for heap %p\n",
+                 size, ptr, subheap->heap );
         return FALSE;
     }
     subheap->commitSize += size;
@@ -530,7 +530,7 @@ static BOOL HEAP_InitSubHeap( HEAP *heap
 
     if (flags & HEAP_SHARED)
         commitSize = totalSize;  /* always commit everything in a shared heap */
-    if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, address,
+    if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, 0,
                                  &commitSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE))
     {
         WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
@@ -619,7 +619,7 @@ static SUBHEAP *HEAP_CreateSubHeap( HEAP
     if (!address)
     {
         /* allocate the memory block */
-        if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, NULL, &totalSize,
+        if (NtAllocateVirtualMemory( GetCurrentProcess(), &address, 0, &totalSize,
                                      MEM_RESERVE, PAGE_EXECUTE_READWRITE ))
         {
             WARN("Could not allocate %08lx bytes\n", totalSize );


More information about the wine-patches mailing list