Make improvements

- We now serialize the file descriptor table when spawning / executing
  processes on Windows. This means you can now inherit more stuff than
  just standard i/o. It's needed by bash, which duplicates the console
  to file descriptor #255. We also now do a better job serializing the
  environment variables, so you're less likely to encounter E2BIG when
  using your bash shell. We also no longer coerce environ to uppercase

- execve() on Windows now remotely controls its parent process to make
  them spawn a replacement for itself. Then it'll be able to terminate
  immediately once the spawn succeeds, without having to linger around
  for the lifetime as a shell process for proxying the exit code. When
  process worker thread running in the parent sees the child die, it's
  given a handle to the new child, to replace it in the process table.

- execve() and posix_spawn() on Windows will now provide CreateProcess
  an explicit handle list. This allows us to remove handle locks which
  enables better fork/spawn concurrency, with seriously correct thread
  safety. Other codebases like Go use the same technique. On the other
  hand fork() still favors the conventional WIN32 inheritence approach
  which can be a little bit messy, but is *controlled* by guaranteeing
  perfectly clean slates at both the spawning and execution boundaries

- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
  there's no reason to use that and it's only supported by FreeBSD. By
  using the system word size, signal mask manipulation on Windows goes
  very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
  Windows to take advantage of signal masking, now that it's much more
  pleasant to use.

- All the overlapped i/o code on Windows has been rewritten for pretty
  good signal and cancelation safety. We're now able to ensure overlap
  data structures are cleaned up so long as you don't longjmp() out of
  out of a signal handler that interrupted an i/o operation. Latencies
  are also improved thanks to the removal of lots of "busy wait" code.
  Waits should be optimal for everything except poll(), which shall be
  the last and final demon we slay in the win32 i/o horror show.

- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
  as RUSAGE_SELF, thanks to aggregation in the process manager thread.
This commit is contained in:
Justine Tunney 2023-10-08 05:36:18 -07:00
parent af7cb3c82f
commit 791f79fcb3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
382 changed files with 4008 additions and 4511 deletions

View file

@ -2,30 +2,30 @@
#define COSMOPOLITAN_LIBC_NT_ENUM_FILEINFOBYHANDLECLASS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define kNtFileBasicInfo 0 /* struct NtFileBasicInformation */
#define kNtFileStandardInfo 1 /* struct NtFileStandardInformation */
#define kNtFileNameInfo 2 /* struct NtFileNameInformation */
#define kNtFileStreamInfo 7 /* struct NtFileStreamInformation */
#define kNtFileCompressionInfo 8 /* struct NtFileCompressionInfo */
#define kNtFileAttributeTagInfo 9 /* struct NtFileAttributeTagInformation */
#define kNtFileIdBothDirectoryInfo 10
#define kNtFileBasicInfo 0 /* struct NtFileBasicInformation */
#define kNtFileStandardInfo 1 /* struct NtFileStandardInformation */
#define kNtFileNameInfo 2 /* struct NtFileNameInformation */
#define kNtFileStreamInfo 7 /* struct NtFileStreamInformation */
#define kNtFileCompressionInfo 8 /* struct NtFileCompressionInfo */
#define kNtFileAttributeTagInfo 9 /* struct NtFileAttributeTagInformation */
#define kNtFileIdBothDirectoryInfo 10
#define kNtFileIdBothDirectoryRestartInfo 11
#define kNtFileRemoteProtocolInfo 13
#define kNtFileFullDirectoryInfo 14 /* NtFileFullDirectoryInformation */
#define kNtFileFullDirectoryRestartInfo 15
#define kNtFileStorageInfo 16 /* win8+ */
#define kNtFileAlignmentInfo 17 /* win8+ */
#define kNtFileIdInfo 18 /* win8+ */
#define kNtFileIdExtdDirectoryInfo 19 /* win8+ */
#define kNtFileRemoteProtocolInfo 13
#define kNtFileFullDirectoryInfo 14 /* NtFileFullDirectoryInformation */
#define kNtFileFullDirectoryRestartInfo 15
#define kNtFileStorageInfo 16 /* win8+ */
#define kNtFileAlignmentInfo 17 /* win8+ */
#define kNtFileIdInfo 18 /* win8+ */
#define kNtFileIdExtdDirectoryInfo 19 /* win8+ */
#define kNtFileIdExtdDirectoryRestartInfo 20 /* win8+ */
/* #define kNtFileRenameInfo 4 */
/* #define kNtFileDispositionInfo 5 */
/* #define kNtFileAllocationInfo 6 */
/* #define kNtFileEndOfFileInfo 7 */
/* #define kNtFileIoPriorityHintInfo 13 */
/* #define kNtFileDispositionInfoEx 22 /\* win10+ *\/ */
/* #define kNtFileRenameInfoEx 23 /\* win10+ *\/ */
#define kNtFileRenameInfo 4
#define kNtFileDispositionInfo 5
#define kNtFileAllocationInfo 6
#define kNtFileEndOfFileInfo 7
#define kNtFileIoPriorityHintInfo 13
#define kNtFileDispositionInfoEx 22 /* win10+ */
#define kNtFileRenameInfoEx 23 /* win10+ */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_NT_ENUM_FILEINFOBYHANDLECLASS_H_ */

View file

@ -90,6 +90,9 @@ bool32 GetFileInformationByHandleEx(int64_t hFile,
bool32 GetFileInformationByHandle(
int64_t hFile, struct NtByHandleFileInformation *lpFileInformation);
bool32 SetFileInformationByHandle(int64_t hFile, int FileInformationClass,
const void *lpFileInformation,
uint32_t dwBufferSize);
uint32_t GetFileAttributes(const char16_t *lpFileName);
bool32 GetFileAttributesEx(
@ -137,9 +140,6 @@ bool32 CreateSymbolicLink(const char16_t *lpSymlinkFileName,
const char16_t *lpTargetPathName, uint32_t dwFlags)
paramsnonnull();
bool32 SetFilePointerEx(int64_t hFile, int64_t liDistanceToMove,
int64_t *optional_lpNewFilePointer, int dwMoveMethod);
bool32 SetEndOfFile(int64_t hFile);
bool32 SetFileValidData(int64_t hFile, int64_t ValidDataLength);

View file

@ -1,18 +1,18 @@
#include "libc/nt/codegen.h"
.imp kernel32,__imp_SetFilePointerEx,SetFilePointerEx
.imp kernel32,__imp_GetThreadDescription,GetThreadDescription
.text.windows
.ftrace1
SetFilePointerEx:
GetThreadDescription:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov __imp_SetFilePointerEx(%rip),%rax
mov __imp_GetThreadDescription(%rip),%rax
jmp __sysv2nt
#elif defined(__aarch64__)
mov x0,#0
ret
#endif
.endfn SetFilePointerEx,globl
.endfn GetThreadDescription,globl
.previous

View file

@ -0,0 +1,18 @@
#include "libc/nt/codegen.h"
.imp kernel32,__imp_SetFileInformationByHandle,SetFileInformationByHandle
.text.windows
.ftrace1
SetFileInformationByHandle:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov __imp_SetFileInformationByHandle(%rip),%rax
jmp __sysv2nt
#elif defined(__aarch64__)
mov x0,#0
ret
#endif
.endfn SetFileInformationByHandle,globl
.previous

View file

@ -0,0 +1,18 @@
#include "libc/nt/codegen.h"
.imp kernel32,__imp_SetThreadDescription,SetThreadDescription
.text.windows
.ftrace1
SetThreadDescription:
.ftrace2
#ifdef __x86_64__
push %rbp
mov %rsp,%rbp
mov __imp_SetThreadDescription(%rip),%rax
jmp __sysv2nt
#elif defined(__aarch64__)
mov x0,#0
ret
#endif
.endfn SetThreadDescription,globl
.previous

View file

@ -162,6 +162,7 @@ imp 'GetSystemTimes' GetSystemTimes kernel32 3
imp 'GetTempPath' GetTempPathW kernel32 2
imp 'GetTempPathA' GetTempPathA kernel32 2
imp 'GetThreadContext' GetThreadContext kernel32 2
imp 'GetThreadDescription' GetThreadDescription kernel32 2
imp 'GetThreadIOPendingFlag' GetThreadIOPendingFlag kernel32 2
imp 'GetThreadId' GetThreadId kernel32 1
imp 'GetThreadPriority' GetThreadPriority kernel32 1
@ -242,9 +243,9 @@ imp 'SetEndOfFile' SetEndOfFile kernel32 1
imp 'SetEnvironmentVariable' SetEnvironmentVariableW kernel32 2
imp 'SetErrorMode' SetErrorMode kernel32 1
imp 'SetEvent' SetEvent kernel32 1
imp 'SetFileInformationByHandle' SetFileInformationByHandle kernel32 4
imp 'SetFileAttributes' SetFileAttributesW kernel32 2
imp 'SetFileCompletionNotificationModes' SetFileCompletionNotificationModes kernel32 2
imp 'SetFilePointerEx' SetFilePointerEx kernel32 4
imp 'SetFileTime' SetFileTime kernel32 4
imp 'SetFileValidData' SetFileValidData kernel32 2
imp 'SetHandleCount' SetHandleCount kernel32 1
@ -259,6 +260,7 @@ imp 'SetProcessWorkingSetSizeEx' SetProcessWorkingSetSizeEx kernel32 4
imp 'SetStdHandle' SetStdHandle kernel32 2
imp 'SetThreadAffinityMask' SetThreadAffinityMask kernel32 2
imp 'SetThreadContext' SetThreadContext kernel32 2
imp 'SetThreadDescription' SetThreadDescription kernel32 2
imp 'SetThreadPriority' SetThreadPriority kernel32 2
imp 'SetThreadPriorityBoost' SetThreadPriorityBoost kernel32 2
imp 'SetUnhandledExceptionFilter' SetUnhandledExceptionFilter kernel32 1

View file

@ -6,19 +6,20 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define kNtProcThreadAttributeParentProcess 0x00020000
#define kNtProcThreadAttributeHandleList 0x00020002
void GetStartupInfo(struct NtStartupInfo *lpStartupInfo);
bool32 InitializeProcThreadAttributeList(
struct NtProcThreadAttributeList *opt_inout_lpAttributeList,
uint32_t dwAttributeCount, uint32_t reserved_dwFlags, size_t *inout_lpSize)
paramsnonnull((4));
uint32_t dwAttributeCount, uint32_t reserved_dwFlags, size_t *inout_lpSize);
bool32 UpdateProcThreadAttribute(
struct NtProcThreadAttributeList *inout_lpAttributeList, uint32_t dwFlags,
const uint32_t *Attribute, const void *lpValue, size_t cbSize,
void *reserved_lpPreviousValue, size_t *reserved_lpReturnSize)
paramsnonnull((1, 3, 4));
uint64_t Attribute, const void *lpValue, size_t cbSize,
void *reserved_lpPreviousValue, size_t *reserved_lpReturnSize);
void DeleteProcThreadAttributeList(
struct NtProcThreadAttributeList *inout_lpAttributeList) paramsnonnull();
struct NtProcThreadAttributeList *inout_lpAttributeList);
#if ShouldUseMsabiAttribute()
#include "libc/nt/thunk/startupinfo.inc"

View file

@ -66,6 +66,11 @@ uint32_t ResumeThread(int64_t hThread);
bool32 GetThreadContext(int64_t hThread, struct NtContext *in_out_lpContext);
bool32 SetThreadContext(int64_t hThread, const struct NtContext *lpContext);
void *SetThreadDescription(int64_t hThread,
const char16_t *lpThreadDescription);
void *GetThreadDescription(int64_t hThread,
char16_t *out_ppszThreadDescription);
#if ShouldUseMsabiAttribute()
#include "libc/nt/thunk/thread.inc"
#endif /* ShouldUseMsabiAttribute() */