Get Redbean fork() working on the New Technology

Now that we have understandable system call tracing on Windows, this
change rewrites many of the polyfill internals for that platform, to
help things get closer to tip top shape. Support for complex forking
scenarios had been in a regressed state for quite some time. Now, it
works! Subsequent changes should be able to address the performance.
This commit is contained in:
Justine Tunney 2022-03-20 08:01:14 -07:00
parent efedef6e65
commit 0cb6b6ff4b
84 changed files with 1340 additions and 338 deletions

View file

@ -20,38 +20,38 @@
* };
* };
*/
#define kNtGenericRead 0x80000000u
#define kNtGenericWrite 0x40000000u
#define kNtGenericExecute 0x20000000u
#define kNtGenericAll 0x10000000u
#define kNtDelete 0x00010000u
#define kNtReadControl 0x00020000u
#define kNtWriteDac 0x00040000u
#define kNtWriteOwner 0x00080000u
#define kNtSynchronize 0x00100000u
#define kNtGenericRead 0x80000000u
#define kNtGenericWrite 0x40000000u
#define kNtGenericExecute 0x20000000u
#define kNtGenericAll 0x10000000u
#define kNtDelete 0x00010000u
#define kNtReadControl 0x00020000u
#define kNtWriteDac 0x00040000u
#define kNtWriteOwner 0x00080000u
#define kNtSynchronize 0x00100000u
#define kNtStandardRightsRequired 0x000F0000u
#define kNtStandardRightsRead kNtReadControl
#define kNtStandardRightsWrite kNtReadControl
#define kNtStandardRightsExecute kNtReadControl
#define kNtStandardRightsAll 0x001F0000u
#define kNtSpecificRightsAll 0x0000FFFFu
#define kNtAccessSystemSecurity 0x01000000u
#define kNtMaximumAllowed 0x02000000u
#define kNtFileReadData 0x0001u
#define kNtFileListDirectory 0x0001u
#define kNtFileWriteData 0x0002u
#define kNtFileAddFile 0x0002u
#define kNtFileAppendData 0x0004u
#define kNtFileAddSubdirectory 0x0004u
#define kNtStandardRightsRead kNtReadControl
#define kNtStandardRightsWrite kNtReadControl
#define kNtStandardRightsExecute kNtReadControl
#define kNtStandardRightsAll 0x001F0000u
#define kNtSpecificRightsAll 0x0000FFFFu
#define kNtAccessSystemSecurity 0x01000000u
#define kNtMaximumAllowed 0x02000000u
#define kNtFileReadData 0x0001u
#define kNtFileListDirectory 0x0001u
#define kNtFileWriteData 0x0002u
#define kNtFileAddFile 0x0002u
#define kNtFileAppendData 0x0004u
#define kNtFileAddSubdirectory 0x0004u
#define kNtFileCreatePipeInstance 0x0004u
#define kNtFileReadEa 0x0008u
#define kNtFileWriteEa 0x0010u
#define kNtFileExecute 0x0020u
#define kNtFileTraverse 0x0020u
#define kNtFileDeleteChild 0x0040u
#define kNtFileReadAttributes 0x0080u
#define kNtFileWriteAttributes 0x0100u
#define kNtFileAllAccess (kNtStandardRightsRequired | kNtSynchronize | 0x1FFu)
#define kNtFileReadEa 0x0008u
#define kNtFileWriteEa 0x0010u
#define kNtFileExecute 0x0020u
#define kNtFileTraverse 0x0020u
#define kNtFileDeleteChild 0x0040u
#define kNtFileReadAttributes 0x0080u
#define kNtFileWriteAttributes 0x0100u
#define kNtFileAllAccess (kNtStandardRightsRequired | kNtSynchronize | 0x1FFu)
#define kNtFileGenericRead \
(kNtStandardRightsRead | kNtFileReadData | kNtFileReadAttributes | \
kNtFileReadEa | kNtSynchronize)
@ -61,21 +61,21 @@
#define kNtFileGenericExecute \
(kNtStandardRightsExecute | kNtFileReadAttributes | kNtFileExecute | \
kNtSynchronize)
#define kNtTokenAssignPrimary 0x0001u
#define kNtTokenDuplicate 0x0002u
#define kNtTokenImpersonate 0x0004u
#define kNtTokenQuery 0x0008u
#define kNtTokenQuerySource 0x0010u
#define kNtTokenAssignPrimary 0x0001u
#define kNtTokenDuplicate 0x0002u
#define kNtTokenImpersonate 0x0004u
#define kNtTokenQuery 0x0008u
#define kNtTokenQuerySource 0x0010u
#define kNtTokenAdjustPrivileges 0x0020u
#define kNtTokenAdjustGroups 0x0040u
#define kNtTokenAdjustDefault 0x0080u
#define kNtTokenAdjustSessionid 0x0100u
#define kNtTokenAdjustGroups 0x0040u
#define kNtTokenAdjustDefault 0x0080u
#define kNtTokenAdjustSessionid 0x0100u
#define kNtTokenAllAccessP \
(kNtStandardRightsRequired | kNtTokenAssignPrimary | kNtTokenDuplicate | \
kNtTokenImpersonate | kNtTokenQuery | kNtTokenQuerySource | \
kNtTokenAdjustPrivileges | kNtTokenAdjustGroups | kNtTokenAdjustDefault)
#define kNtTokenAllAccess kNtTokenAllAccessP | kNtTokenAdjustSessionid
#define kNtTokenRead kNtStandardRightsRead | kNtTokenQuery
#define kNtTokenRead kNtStandardRightsRead | kNtTokenQuery
#define kNtTokenWrite \
(kNtStandardRightsWrite | kNtTokenAdjustPrivileges | kNtTokenAdjustGroups | \
kNtTokenAdjustDefault)
@ -83,6 +83,6 @@
#define kNtTokenTrustConstraintMask \
(kNtStandardRightsRead | kNtTokenQuery | kNtTokenQuerySource)
#define kNtTokenAccessPseudoHandleWin8 kNtTokenQuery | kNtTokenQuerySource
#define kNtTokenAccessPseudoHandle kNtTokenAccessPseudoHandleWin8
#define kNtTokenAccessPseudoHandle kNtTokenAccessPseudoHandleWin8
#endif /* COSMOPOLITAN_LIBC_NT_ENUM_ACCESSMASK_H_ */

9
libc/nt/enum/heap.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_NT_ENUM_HEAP_H_
#define COSMOPOLITAN_LIBC_NT_ENUM_HEAP_H_
#define kNtHeapNoSerialize 1
#define kNtHeapGenerateExceptions 4
#define kNtHeapZeroMemory 8
#define kNtHeapReallocInPlaceOnly 16
#endif /* COSMOPOLITAN_LIBC_NT_ENUM_HEAP_H_ */

View file

@ -2,25 +2,25 @@
#define COSMOPOLITAN_LIBC_NT_ENUM_PAGEFLAGS_H_
/* Pick One */
#define kNtPageNoaccess 0x01
#define kNtPageReadonly 0x02
#define kNtPageReadwrite 0x04
#define kNtPageWritecopy 0x08
#define kNtPageExecute 0x10
#define kNtPageExecuteRead 0x20
#define kNtPageExecuteReadwrite 0x40
#define kNtPageExecuteWritecopy 0x80
#define kNtPageGuard 0x100
#define kNtPageNocache 0x200
#define kNtPageWritecombine 0x400
#define kNtPageNoaccess 0x001
#define kNtPageReadonly 0x002
#define kNtPageReadwrite 0x004
#define kNtPageWritecopy 0x008
#define kNtPageExecute 0x010
#define kNtPageExecuteRead 0x020
#define kNtPageExecuteReadwrite 0x040
#define kNtPageExecuteWritecopy 0x080
#define kNtPageGuard 0x100
#define kNtPageNocache 0x200
#define kNtPageWritecombine 0x400
/* These may be OR'd */
#define kNtSecReserve 0x4000000
#define kNtSecCommit 0x8000000 /* ←default */
#define kNtSecImage 0x1000000
#define kNtSecReserve 0x04000000
#define kNtSecCommit 0x08000000 /* default */
#define kNtSecImageNoExecute 0x11000000
#define kNtSecLargePages 0x80000000
#define kNtSecNocache 0x10000000
#define kNtSecWritecombine 0x40000000
#define kNtSecImage 0x01000000
#define kNtSecNocache 0x10000000
#define kNtSecLargePages 0x80000000
#define kNtSecWritecombine 0x40000000
#endif /* COSMOPOLITAN_LIBC_NT_ENUM_PAGEFLAGS_H_ */

View file

@ -2,11 +2,11 @@
.imp kernel32,__imp_CreateFileMappingNumaW,CreateFileMappingNumaW,0
.text.windows
CreateFileMappingNuma:
__CreateFileMappingNuma:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateFileMappingNumaW(%rip),%rax
jmp __sysv2nt8
.endfn CreateFileMappingNuma,globl
.endfn __CreateFileMappingNuma,globl
.previous

View file

@ -2,11 +2,11 @@
.imp kernel32,__imp_CreateFileW,CreateFileW,0
.text.windows
CreateFile:
__CreateFile:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateFileW(%rip),%rax
jmp __sysv2nt8
.endfn CreateFile,globl
.endfn __CreateFile,globl
.previous

View file

@ -1,2 +1,14 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_GetProcessHeap,GetProcessHeap,0
.text.windows
GetProcessHeap:
push %rbp
mov %rsp,%rbp
.profilable
sub $32,%rsp
call *__imp_GetProcessHeap(%rip)
leave
ret
.endfn GetProcessHeap,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_GetProcessHeaps,GetProcessHeaps,0
.text.windows
GetProcessHeaps:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_GetProcessHeaps(%rip),%rax
jmp __sysv2nt
.endfn GetProcessHeaps,globl
.previous

View file

@ -0,0 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapAlloc,HeapAlloc,0
.text.windows
HeapAlloc:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_HeapAlloc(%rip),%rax
jmp __sysv2nt
.endfn HeapAlloc,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapCompact,HeapCompact,0
.text.windows
HeapCompact:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_HeapCompact(%rip),%rax
jmp __sysv2nt
.endfn HeapCompact,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapCreate,HeapCreate,0
.text.windows
HeapCreate:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_HeapCreate(%rip),%rax
jmp __sysv2nt
.endfn HeapCreate,globl
.previous

View file

@ -1,2 +1,15 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapDestroy,HeapDestroy,0
.text.windows
HeapDestroy:
push %rbp
mov %rsp,%rbp
.profilable
mov %rdi,%rcx
sub $32,%rsp
call *__imp_HeapDestroy(%rip)
leave
ret
.endfn HeapDestroy,globl
.previous

View file

@ -1,2 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapFree,HeapFree,847
.text.windows
HeapFree:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_HeapFree(%rip),%rax
jmp __sysv2nt
.endfn HeapFree,globl
.previous

View file

@ -0,0 +1,12 @@
.include "o/libc/nt/codegen.inc"
.imp kernel32,__imp_HeapReAlloc,HeapReAlloc,0
.text.windows
HeapReAlloc:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_HeapReAlloc(%rip),%rax
jmp __sysv2nt
.endfn HeapReAlloc,globl
.previous

View file

@ -2,11 +2,11 @@
.imp kernel32,__imp_MapViewOfFileExNuma,MapViewOfFileExNuma,0
.text.windows
MapViewOfFileExNuma:
__MapViewOfFileExNuma:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_MapViewOfFileExNuma(%rip),%rax
jmp __sysv2nt8
.endfn MapViewOfFileExNuma,globl
.endfn __MapViewOfFileExNuma,globl
.previous

View file

@ -2,11 +2,11 @@
.imp kernel32,__imp_VirtualProtect,VirtualProtect,0
.text.windows
VirtualProtect:
__VirtualProtect:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_VirtualProtect(%rip),%rax
jmp __sysv2nt
.endfn VirtualProtect,globl
.endfn __VirtualProtect,globl
.previous

View file

@ -593,9 +593,10 @@ imp 'CreateEventEx' CreateEventExW kernel32 0 4 # KernelBase
imp 'CreateEvent' CreateEventW kernel32 0 4 # KernelBase
imp 'CreateFiber' CreateFiber kernel32 0 # KernelBase
imp 'CreateFiberEx' CreateFiberEx kernel32 0 # KernelBase
imp 'CreateFile' CreateFileW kernel32 0 7 # KernelBase
imp 'CreateFileA' CreateFileA kernel32 0 7 # KernelBase
imp 'CreateFileMappingNuma' CreateFileMappingNumaW kernel32 0 7 # Kernelbase
imp '__CreateFile' CreateFileW kernel32 0 7 # KernelBase
imp '__CreateFileMappingNuma' CreateFileMappingNumaW kernel32 0 7 # Kernelbase
imp '__MapViewOfFileExNuma' MapViewOfFileExNuma kernel32 0 7 # KernelBase
imp 'CreateFileMappingNumaA' CreateFileMappingNumaA kernel32 198 7
imp 'CreateFileMapping' CreateFileMappingW kernel32 0 7 # KernelBase
imp 'CreateFileMappingA' CreateFileMappingA kernel32 196 7
@ -2474,8 +2475,8 @@ imp 'GetProcessDefaultLayout' GetProcessDefaultLayout user32 1930
imp 'GetProcessDpiAwarenessInternal' GetProcessDpiAwarenessInternal user32 1931
imp 'GetProcessGroupAffinity' GetProcessGroupAffinity kernel32 0 # KernelBase
imp 'GetProcessHandleCount' GetProcessHandleCount kernel32 0 2 # KernelBase
imp 'GetProcessHeap' GetProcessHeap kernel32 0 # KernelBase
imp 'GetProcessHeaps' GetProcessHeaps kernel32 0 # KernelBase
imp 'GetProcessHeap' GetProcessHeap kernel32 0 0 # KernelBase
imp 'GetProcessHeaps' GetProcessHeaps kernel32 0 2 # KernelBase
imp 'GetProcessId' GetProcessId kernel32 0 1 # KernelBase
imp 'GetProcessIdOfThread' GetProcessIdOfThread kernel32 0 1 # KernelBase
imp 'GetProcessImageFileNameA' GetProcessImageFileNameA kernel32 676 3
@ -2817,10 +2818,12 @@ imp 'Heap32First' Heap32First kernel32 839
imp 'Heap32ListFirst' Heap32ListFirst kernel32 840
imp 'Heap32ListNext' Heap32ListNext kernel32 841
imp 'Heap32Next' Heap32Next kernel32 842
imp 'HeapCompact' HeapCompact kernel32 0 # KernelBase
imp 'HeapCreate' HeapCreate kernel32 0 # KernelBase
imp 'HeapDestroy' HeapDestroy kernel32 0 # KernelBase
imp 'HeapFree' HeapFree kernel32 847
imp 'HeapCompact' HeapCompact kernel32 0 2 # KernelBase
imp 'HeapAlloc' HeapAlloc kernel32 0 3
imp 'HeapReAlloc' HeapReAlloc kernel32 0 4
imp 'HeapCreate' HeapCreate kernel32 0 3 # KernelBase
imp 'HeapDestroy' HeapDestroy kernel32 0 1 # KernelBase
imp 'HeapFree' HeapFree kernel32 847 3
imp 'HeapLock' HeapLock kernel32 0 # KernelBase
imp 'HeapQueryInformation' HeapQueryInformation kernel32 0 # KernelBase
imp 'HeapSetInformation' HeapSetInformation kernel32 0 # KernelBase
@ -3380,7 +3383,6 @@ imp 'MapViewOfFile' MapViewOfFile kernel32 0 # KernelBase
imp 'MapViewOfFile3' MapViewOfFile3 KernelBase 1003
imp 'MapViewOfFile3FromApp' MapViewOfFile3FromApp KernelBase 1004
imp 'MapViewOfFileEx' MapViewOfFileEx kernel32 0 # KernelBase
imp 'MapViewOfFileExNuma' MapViewOfFileExNuma kernel32 0 7 # KernelBase
imp 'MapViewOfFileFromApp' MapViewOfFileFromApp kernel32 0 # KernelBase
imp 'MapViewOfFileNuma2' MapViewOfFileNuma2 KernelBase 1008
imp 'MapVirtualKeyA' MapVirtualKeyA user32 2153
@ -6879,7 +6881,7 @@ imp 'VirtualAllocFromApp' VirtualAllocFromApp KernelBase 1760
imp 'VirtualFree' VirtualFree kernel32 0 3 # KernelBase
imp 'VirtualFreeEx' VirtualFreeEx kernel32 0 # KernelBase
imp 'VirtualLock' VirtualLock kernel32 0 # KernelBase
imp 'VirtualProtect' VirtualProtect kernel32 0 4 # KernelBase
imp '__VirtualProtect' VirtualProtect kernel32 0 4 # KernelBase
imp 'VirtualProtectEx' VirtualProtectEx kernel32 0 # KernelBase
imp 'VirtualProtectFromApp' VirtualProtectFromApp KernelBase 1766
imp 'VirtualQuery' VirtualQuery kernel32 0 3 # KernelBase

View file

@ -70,6 +70,12 @@ bool32 PrefetchVirtualMemory(int64_t hProcess, const uint32_t *NumberOfEntries,
bool32 OfferVirtualMemory(void *inout_VirtualAddress, size_t Size,
int Priority);
int64_t GetProcessHeap(void);
void *HeapAlloc(int64_t hHeap, uint32_t dwFlags, size_t dwBytes) nodiscard;
bool32 HeapFree(int64_t hHeap, uint32_t dwFlags, void *opt_lpMem);
void *HeapReAlloc(int64_t hHeap, uint32_t dwFlags, void *lpMem,
size_t dwBytes) nodiscard;
void *GlobalAlloc(uint32_t uFlags, uint64_t dwBytes) nodiscard;
void *GlobalFree(void *hMem);

View file

@ -1,12 +1,6 @@
#define CreateFileMappingNuma(...) __imp_CreateFileMappingNumaW(__VA_ARGS__)
#define MapViewOfFileExNuma(...) __imp_MapViewOfFileExNuma(__VA_ARGS__)
#define FlushViewOfFile(...) __imp_FlushViewOfFile(__VA_ARGS__)
#define UnmapViewOfFile(...) __imp_UnmapViewOfFile(__VA_ARGS__)
#define FlushViewOfFile(...) __imp_FlushViewOfFile(__VA_ARGS__)
#define UnmapViewOfFile(...) __imp_UnmapViewOfFile(__VA_ARGS__)
extern typeof(LocalFree) *const __imp_LocalFree __msabi;
extern typeof(VirtualProtect) *const __imp_VirtualProtect __msabi;
extern typeof(UnmapViewOfFile) *const __imp_UnmapViewOfFile __msabi;
extern typeof(FlushViewOfFile) *const __imp_FlushViewOfFile __msabi;
extern typeof(MapViewOfFileExNuma) *const __imp_MapViewOfFileExNuma __msabi;
extern typeof(CreateFileMappingNuma) *const
__imp_CreateFileMappingNumaW __msabi;