about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2025-04-16 21:12:02 +0800
committerGitHub <noreply@github.com>2025-04-16 15:12:02 +0200
commit46a91e0ad92e3d8de6e6fc04b10dcc453a8d69cf (patch)
tree16781a99461e655aa9c3fc8a27cf05128daebb85 /src
parentb26a20e709fb8673fcab6930a879ff86d181055e (diff)
downloadbox64-46a91e0ad92e3d8de6e6fc04b10dcc453a8d69cf.tar.gz
box64-46a91e0ad92e3d8de6e6fc04b10dcc453a8d69cf.zip
[WOW64] More tweaks to CMake PE build (#2541)
Diffstat (limited to 'src')
-rw-r--r--src/os/os_wine.c58
1 files changed, 45 insertions, 13 deletions
diff --git a/src/os/os_wine.c b/src/os/os_wine.c
index 06a8317b..e2cdba2e 100644
--- a/src/os/os_wine.c
+++ b/src/os/os_wine.c
@@ -1,10 +1,14 @@
 #include <windows.h>
+#include <ntstatus.h>
+#include <winternl.h>
 
 #include "os.h"
 
+#define HandleToULong(h) ((ULONG)(ULONG_PTR)(h))
+
 int GetTID(void)
 {
-    return GetCurrentThreadId();
+    return HandleToULong(((HANDLE*)NtCurrentTeb())[9]);
 }
 
 int SchedYield(void)
@@ -81,30 +85,57 @@ static uint32_t prot_unix_to_win32(uint32_t unx)
     return 0;
 }
 
+#define NtCurrentProcess() ((HANDLE) ~(ULONG_PTR)0)
+
+NTSTATUS WINAPI NtProtectVirtualMemory(HANDLE, PVOID*, SIZE_T*, ULONG, ULONG*);
+NTSTATUS WINAPI NtAllocateVirtualMemory(HANDLE, PVOID*, ULONG_PTR, SIZE_T*, ULONG, ULONG);
+PVOID WINAPI RtlReAllocateHeap(HANDLE, ULONG, PVOID, SIZE_T);
+NTSTATUS WINAPI NtFreeVirtualMemory(HANDLE, PVOID*, SIZE_T*, ULONG);
+
 int mprotect(void* addr, size_t len, int prot)
 {
+    NTSTATUS ntstatus;
     ULONG old_prot;
-    if (VirtualProtect(&addr, len, prot_unix_to_win32(prot), &old_prot))
-        return 0;
-    return -1;
+    SIZE_T allocsize = len;
+    ntstatus = NtProtectVirtualMemory(NtCurrentProcess(), &addr, &allocsize, prot_unix_to_win32(prot), &old_prot);
+    if (ntstatus != STATUS_SUCCESS) {
+        return -1;
+    }
+    return 0;
 }
 
 void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
 {
+    NTSTATUS ntstatus;
+    SIZE_T sz = length;
+    ULONG_PTR limit;
+    void* ret = NULL;
+
+    if (addr != NULL) {
+        return MAP_FAILED;
+    }
     if (fd && fd != -1) {
         return MAP_FAILED;
     }
     if (offset) {
         return MAP_FAILED;
     }
-    return VirtualAlloc(addr, length, MEM_COMMIT | MEM_RESERVE, prot_unix_to_win32(prot));
+
+    if (flags & MAP_32BIT)
+        limit = default_zero_bits32;
+    else
+        limit = 0;
+
+    ntstatus = NtAllocateVirtualMemory(NtCurrentProcess(), &ret, limit, &sz, MEM_COMMIT | MEM_RESERVE, prot_unix_to_win32(prot));
+    return ret;
 }
 
 int munmap(void* addr, size_t length)
 {
-    if (VirtualFree(addr, length, MEM_RELEASE))
-        return 0;
-    return -1;
+    int ret = 0;
+    if (NtFreeVirtualMemory(NtCurrentProcess(), &addr, &length, MEM_RELEASE))
+        ret = -1;
+    return ret;
 }
 
 void* InternalMmap(void* addr, unsigned long length, int prot, int flags, int fd, ssize_t offset)
@@ -120,26 +151,27 @@ int InternalMunmap(void* addr, unsigned long length)
 void* WinMalloc(size_t size)
 {
     void* ret;
-    ret = HeapAlloc(GetProcessHeap(), 0, size);
+    ret = RtlAllocateHeap(GetProcessHeap(), 0, size);
     return ret;
 }
 
 void* WinRealloc(void* ptr, size_t size)
 {
     void* ret;
-    if (!ptr) return WinMalloc(size);
-    ret = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, size);
+    if (!ptr)
+        return WinMalloc(size);
+    ret = RtlReAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, ptr, size);
     return ret;
 }
 
 void* WinCalloc(size_t nmemb, size_t size)
 {
     void* ret;
-    ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nmemb * size);
+    ret = RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, nmemb * size);
     return ret;
 }
 
 void WinFree(void* ptr)
 {
-    HeapFree(GetProcessHeap(), 0, ptr);
+    RtlFreeHeap(GetProcessHeap(), 0, ptr);
 }
\ No newline at end of file