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

@ -1296,7 +1296,7 @@ void __asan_map_shadow(uintptr_t p, size_t n) {
if (sm.addr == MAP_FAILED ||
weaken(TrackMemoryInterval)(
m, a, a + i - 1, sm.maphandle, PROT_READ | PROT_WRITE,
MAP_PRIVATE | *weaken(MAP_ANONYMOUS) | MAP_FIXED) == -1) {
MAP_PRIVATE | *weaken(MAP_ANONYMOUS) | MAP_FIXED, 0, size) == -1) {
kprintf("error: could not map asan shadow memory%n");
__asan_die()();
__asan_unreachable();

View file

@ -0,0 +1,69 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/thunk/msabi.h"
extern typeof(CreateFile) *const __imp_CreateFileW __msabi;
static const char *DescribeDisposition(int x) {
switch (x) {
case kNtCreateNew:
return "kNtCreateNew";
case kNtCreateAlways:
return "kNtCreateAlways";
case kNtOpenExisting:
return "kNtOpenExisting";
case kNtOpenAlways:
return "kNtOpenAlways";
case kNtTruncateExisting:
return "kNtTruncateExisting";
default:
return "wut";
}
}
/**
* Opens file on the New Technology.
*
* @return handle, or -1 on failure
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
* @see MapViewOfFileExNuma()
*/
int64_t CreateFile(const char16_t *lpFileName, uint32_t dwDesiredAccess,
uint32_t dwShareMode,
struct NtSecurityAttributes *opt_lpSecurityAttributes,
int dwCreationDisposition, uint32_t dwFlagsAndAttributes,
int64_t opt_hTemplateFile) {
int64_t hHandle;
hHandle = __imp_CreateFileW(lpFileName, dwDesiredAccess, dwShareMode,
opt_lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, opt_hTemplateFile);
if (hHandle == -1) __winerr();
STRACE("CreateFile(%#hs, %s, %s, %p, %s, %s, %ld) → %ld% m", lpFileName,
DescribeNtFileAccessFlags(dwDesiredAccess),
DescribeNtFileShareFlags(dwShareMode), opt_lpSecurityAttributes,
DescribeDisposition(dwCreationDisposition),
DescribeNtFileFlagsAndAttributes(dwFlagsAndAttributes),
opt_hTemplateFile, hHandle);
return hHandle;
}

View file

@ -0,0 +1,51 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/memory.h"
#include "libc/nt/struct/securityattributes.h"
extern typeof(CreateFileMappingNuma) *const
__imp_CreateFileMappingNumaW __msabi;
/**
* Creates file mapping object on the New Technology.
*
* @param opt_hFile may be -1 for MAP_ANONYMOUS behavior
* @return handle, or 0 on failure
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
* @see MapViewOfFileExNuma()
*/
int64_t CreateFileMappingNuma(
int64_t opt_hFile,
const struct NtSecurityAttributes *opt_lpFileMappingAttributes,
uint32_t flProtect, uint32_t dwMaximumSizeHigh, uint32_t dwMaximumSizeLow,
const char16_t *opt_lpName, uint32_t nndDesiredNumaNode) {
int64_t hHandle;
hHandle = __imp_CreateFileMappingNumaW(
opt_hFile, opt_lpFileMappingAttributes, flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, opt_lpName, nndDesiredNumaNode);
if (!hHandle) __winerr();
STRACE("CreateFileMappingNuma(%ld, %s, max:%'zu, name:%#hs) → %ld% m",
opt_hFile, DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
return hHandle;
}

View file

@ -0,0 +1,59 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/kprintf.h"
const char *DescribeFlags(char *p, size_t n, struct DescribeFlags *d, size_t m,
const char *prefix, unsigned x) {
bool t;
char b[21];
size_t i, j, k;
for (t = i = j = 0; j < m; ++j) {
if ((x & d[j].flag) == d[j].flag) {
x &= ~d[j].flag;
if (t) {
if (i + 1 < n) p[i++] = '|';
} else {
t = true;
}
for (k = 0; prefix && prefix[k]; ++k) {
if (i + 1 < n) p[i++] = prefix[k];
}
for (k = 0; d[j].name[k]; ++k) {
if (i + 1 < n) p[i++] = d[j].name[k];
}
}
}
if (x || !t) {
if (t && i + 1 < n) p[i++] = '|';
if (i + 1 < n) p[i++] = '0';
if (x) {
if (i + 1 < n) p[i++] = 'x';
k = 0;
do {
if (i + 1 < n) b[k++] = "0123456789abcdef"[x % 16];
} while ((x /= 16));
while (k--) {
if (i + 1 < n) p[i++] = b[k];
}
}
}
if (i < n) p[i] = 0;
return p;
}

View file

@ -0,0 +1,21 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEFLAGS_INTERNAL_H_
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEFLAGS_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct thatispacked DescribeFlags {
unsigned flag;
const char *name;
};
const char *DescribeFlags(char *, size_t, struct DescribeFlags *, size_t,
const char *, unsigned);
const char *DescribeNtPageFlags(uint32_t);
const char *DescribeNtFileMapFlags(uint32_t);
const char *DescribeNtFileFlagsAndAttributes(uint32_t);
const char *DescribeNtFileShareFlags(uint32_t);
const char *DescribeNtFileAccessFlags(uint32_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEFLAGS_INTERNAL_H_ */

View file

@ -0,0 +1,70 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/filesharemode.h"
static const struct DescribeFlags kFileAccessflags[] = {
{kNtFileAllAccess, "FileAllAccess"}, // order matters
{kNtFileGenericRead, "FileGenericRead"}, // order matters
{kNtFileGenericWrite, "FileGenericWrite"}, // order matters
{kNtFileGenericExecute, "FileGenericExecute"}, // order matters
{kNtGenericRead, "GenericRead"}, //
{kNtGenericWrite, "GenericWrite"}, //
{kNtGenericExecute, "GenericExecute"}, //
{kNtGenericAll, "GenericAll"}, //
{kNtDelete, "Delete"}, //
{kNtReadControl, "ReadControl"}, //
{kNtWriteDac, "WriteDac"}, //
{kNtWriteOwner, "WriteOwner"}, //
{kNtSynchronize, "Synchronize"}, //
{kNtStandardRightsRequired, "StandardRightsRequired"}, //
{kNtAccessSystemSecurity, "AccessSystemSecurity"}, //
{kNtMaximumAllowed, "MaximumAllowed"}, //
{kNtFileReadData, "FileReadData"}, //
{kNtFileListDirectory, "FileListDirectory"}, //
{kNtFileWriteData, "FileWriteData"}, //
{kNtFileAddFile, "FileAddFile"}, //
{kNtFileAppendData, "FileAppendData"}, //
{kNtFileAddSubdirectory, "FileAddSubdirectory"}, //
{kNtFileCreatePipeInstance, "FileCreatePipeInstance"}, //
{kNtFileReadEa, "FileReadEa"}, //
{kNtFileWriteEa, "FileWriteEa"}, //
{kNtFileExecute, "FileExecute"}, //
{kNtFileTraverse, "FileTraverse"}, //
{kNtFileDeleteChild, "FileDeleteChild"}, //
{kNtFileReadAttributes, "FileReadAttributes"}, //
{kNtFileWriteAttributes, "FileWriteAttributes"}, //
{kNtTokenAssignPrimary, "TokenAssignPrimary"}, //
{kNtTokenDuplicate, "TokenDuplicate"}, //
{kNtTokenImpersonate, "TokenImpersonate"}, //
{kNtTokenQuery, "TokenQuery"}, //
{kNtTokenQuerySource, "TokenQuerySource"}, //
{kNtTokenAdjustPrivileges, "TokenAdjustPrivileges"}, //
{kNtTokenAdjustGroups, "TokenAdjustGroups"}, //
{kNtTokenAdjustDefault, "TokenAdjustDefault"}, //
{kNtTokenAdjustSessionid, "TokenAdjustSessionid"}, //
};
const char *DescribeNtFileAccessFlags(uint32_t x) {
static char ntfileaccessflags[256];
return DescribeFlags(ntfileaccessflags, sizeof(ntfileaccessflags),
kFileAccessflags, ARRAYLEN(kFileAccessflags), "kNt", x);
}

View file

@ -0,0 +1,56 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/fileflagandattributes.h"
static const struct DescribeFlags kFileFlags[] = {
{kNtFileAttributeReadonly, "AttributeReadonly"}, //
{kNtFileAttributeHidden, "AttributeHidden"}, //
{kNtFileAttributeSystem, "AttributeSystem"}, //
{kNtFileAttributeVolumelabel, "AttributeVolumelabel"}, //
{kNtFileAttributeDirectory, "AttributeDirectory"}, //
{kNtFileAttributeArchive, "AttributeArchive"}, //
{kNtFileAttributeDevice, "AttributeDevice"}, //
{kNtFileAttributeNormal, "AttributeNormal"}, //
{kNtFileAttributeTemporary, "AttributeTemporary"}, //
{kNtFileAttributeSparseFile, "AttributeSparseFile"}, //
{kNtFileAttributeReparsePoint, "AttributeReparsePoint"}, //
{kNtFileAttributeCompressed, "AttributeCompressed"}, //
{kNtFileAttributeOffline, "AttributeOffline"}, //
{kNtFileAttributeNotContentIndexed, "AttributeNotContentIndexed"}, //
{kNtFileAttributeEncrypted, "AttributeEncrypted"}, //
{kNtFileFlagWriteThrough, "FlagWriteThrough"}, //
{kNtFileFlagOverlapped, "FlagOverlapped"}, //
{kNtFileFlagNoBuffering, "FlagNoBuffering"}, //
{kNtFileFlagRandomAccess, "FlagRandomAccess"}, //
{kNtFileFlagSequentialScan, "FlagSequentialScan"}, //
{kNtFileFlagDeleteOnClose, "FlagDeleteOnClose"}, //
{kNtFileFlagBackupSemantics, "FlagBackupSemantics"}, //
{kNtFileFlagPosixSemantics, "FlagPosixSemantics"}, //
{kNtFileFlagOpenReparsePoint, "FlagOpenReparsePoint"}, //
{kNtFileFlagOpenNoRecall, "FlagOpenNoRecall"}, //
{kNtFileFlagFirstPipeInstance, "FlagFirstPipeInstance"}, //
};
const char *DescribeNtFileFlagsAndAttributes(uint32_t x) {
static char ntfileflags[256];
return DescribeFlags(ntfileflags, sizeof(ntfileflags), kFileFlags,
ARRAYLEN(kFileFlags), "kNtFile", x);
}

View file

@ -0,0 +1,37 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/filemapflags.h"
static const struct DescribeFlags kFileMapFlags[] = {
{kNtFileMapCopy, "Copy"}, //
{kNtFileMapWrite, "Write"}, //
{kNtFileMapRead, "Read"}, //
{kNtFileMapExecute, "Execute"}, //
{kNtFileMapReserve, "Reserve"}, //
{kNtFileMapTargetsInvalid, "TargetsInvalid"}, //
{kNtFileMapLargePages, "LargePages"}, //
};
const char *DescribeNtFileMapFlags(uint32_t x) {
static char filemapflags[64];
return DescribeFlags(filemapflags, sizeof(filemapflags), kFileMapFlags,
ARRAYLEN(kFileMapFlags), "kNtFileMap", x);
}

View file

@ -0,0 +1,34 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/filesharemode.h"
static const struct DescribeFlags kFileShareflags[] = {
{kNtFileShareRead, "Read"}, //
{kNtFileShareWrite, "Write"}, //
{kNtFileShareDelete, "Delete"}, //
};
const char *DescribeNtFileShareFlags(uint32_t x) {
static char ntfileshareflags[64];
return DescribeFlags(ntfileshareflags, sizeof(ntfileshareflags),
kFileShareflags, ARRAYLEN(kFileShareflags),
"kNtFileShare", x);
}

View file

@ -0,0 +1,48 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/describeflags.internal.h"
#include "libc/macros.internal.h"
#include "libc/nt/enum/pageflags.h"
static const struct DescribeFlags kPageFlags[] = {
{kNtPageNoaccess, "PageNoaccess"}, //
{kNtPageReadonly, "PageReadonly"}, //
{kNtPageReadwrite, "PageReadwrite"}, //
{kNtPageWritecopy, "PageWritecopy"}, //
{kNtPageExecute, "PageExecute"}, //
{kNtPageExecuteRead, "PageExecuteRead"}, //
{kNtPageExecuteReadwrite, "PageExecuteReadwrite"}, //
{kNtPageExecuteWritecopy, "PageExecuteWritecopy"}, //
{kNtPageGuard, "PageGuard"}, //
{kNtPageNocache, "PageNocache"}, //
{kNtPageWritecombine, "PageWritecombine"}, //
{kNtSecReserve, "SecReserve"}, //
{kNtSecCommit, "SecCommit"}, //
{kNtSecImageNoExecute, "SecImageNoExecute"}, // order matters
{kNtSecImage, "SecImage"}, //
{kNtSecLargePages, "SecLargePages"}, //
{kNtSecNocache, "SecNocache"}, //
{kNtSecWritecombine, "SecWritecombine"}, //
};
const char *DescribeNtPageFlags(uint32_t x) {
static char pageflags[64];
return DescribeFlags(pageflags, sizeof(pageflags), kPageFlags,
ARRAYLEN(kPageFlags), "kNt", x);
}

View file

@ -20,12 +20,17 @@
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/nexgen32e/vendor.internal.h"
#include "libc/nt/console.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/runtime/internal.h"
#include "libc/sysv/consts/nr.h"
uint32_t __winmainpid;
const char kConsoleHandles[2] = {kNtStdInputHandle, kNtStdOutputHandle};
/**
* Terminates process, ignoring destructors and atexit() handlers.
*
@ -38,7 +43,13 @@
* @noreturn
*/
privileged noinstrument noasan noubsan wontreturn void _Exit(int exitcode) {
int i;
STRACE("_Exit(%d)", exitcode);
if (SupportsWindows() && GetCurrentProcessId() == __winmainpid) {
for (i = 0; i < 2; ++i) {
SetConsoleMode(GetStdHandle(kConsoleHandles[i]), __ntconsolemode[i]);
}
}
if ((!IsWindows() && !IsMetal()) || (IsMetal() && IsGenuineCosmo())) {
asm volatile("syscall"
: /* no outputs */

View file

@ -57,11 +57,23 @@ o/$(MODE)/libc/intrin/asan.o: \
-finline \
-finline-functions
o/$(MODE)/libc/intrin/kprintf.greg.o: \
OVERRIDE_CFLAGS += \
-fpie \
-ffreestanding \
$(NO_MAGIC)
o/$(MODE)/libc/intrin/createfile.greg.o \
o/$(MODE)/libc/intrin/describeflags.greg.o \
o/$(MODE)/libc/intrin/mapviewoffileexnuma.greg.o \
o/$(MODE)/libc/intrin/createfilemappingnuma.greg.o \
o/$(MODE)/libc/intrin/kstarttsc.o \
o/$(MODE)/libc/intrin/nomultics.o \
o/$(MODE)/libc/intrin/ntconsolemode.o: \
OVERRIDE_CFLAGS += \
-fno-sanitize=all
-Os \
-ffreestanding \
$(NO_MAGIC)
o/$(MODE)/libc/intrin/asan.o \
o/$(MODE)/libc/intrin/ubsan.o: \
@ -86,13 +98,6 @@ o/$(MODE)/libc/intrin/memmove.o: \
OVERRIDE_CFLAGS += \
-fpie
o/$(MODE)/libc/intrin/kprintf.greg.o: \
OVERRIDE_CFLAGS += \
-fpie \
-fwrapv \
-fno-sanitize=all \
-fschedule-insns2
LIBC_INTRIN_LIBS = $(foreach x,$(LIBC_INTRIN_ARTIFACTS),$($(x)))
LIBC_INTRIN_HDRS = $(foreach x,$(LIBC_INTRIN_ARTIFACTS),$($(x)_HDRS))
LIBC_INTRIN_SRCS = $(foreach x,$(LIBC_INTRIN_ARTIFACTS),$($(x)_SRCS))

View file

@ -54,16 +54,16 @@ struct Timestamps {
extern int __pid;
extern bool __replmode;
extern bool __nomultics;
static volatile unsigned long long kbirth;
volatile unsigned long long __kbirth;
privileged static struct Timestamps kenter(void) {
struct Timestamps ts;
ts.start = rdtsc();
ts.birth = kbirth;
ts.birth = __kbirth;
if (!ts.birth) {
ts.birth = kStartTsc;
if (!ts.birth) ts.birth = 1;
cmpxchg(&kbirth, 0, ts.birth);
cmpxchg(&__kbirth, 0, ts.birth);
}
return ts;
}
@ -74,7 +74,7 @@ privileged static void kleave(struct Timestamps ts) {
elapse = unsignedsubtract(finish, ts.start);
adjust = ts.birth + elapse;
if (!adjust) adjust = 1;
cmpxchg(&kbirth, ts.birth, adjust); /* ignore overlapping time intervals */
cmpxchg(&__kbirth, ts.birth, adjust); /* ignore overlapping time intervals */
}
privileged static inline char *kadvance(char *p, char *e, long n) {
@ -465,7 +465,8 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
}
goto EmitChar;
case 'm':
if (!(x = errno) && sign == ' ') {
if (!(x = errno) && sign == ' ' &&
(!IsWindows() || !__imp_GetLastError())) {
break;
} else if (weaken(strerror_r) &&
!weaken(strerror_r)(x, z, sizeof(z))) {
@ -572,7 +573,7 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, va_list va,
goto EmitChar;
}
} else if (type < -1) {
if ((t = *s++ & 255)) {
if ((t = *s++ & 255) || prec) {
t = kCp437[t];
}
} else if (type < 0) {

View file

@ -0,0 +1,52 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/memory.h"
extern typeof(MapViewOfFileExNuma) *const __imp_MapViewOfFileExNuma __msabi;
/**
* Maps view of file mapping into memory on the New Technology.
*
* @param hFileMappingObject was returned by CreateFileMapping()
* @param dwDesiredAccess has kNtFileMap... flags
* @param opt_lpDesiredBaseAddress may be NULL to let o/s choose
* @return base address, or NULL on failure
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
*/
void *MapViewOfFileExNuma(int64_t hFileMappingObject, uint32_t dwDesiredAccess,
uint32_t dwFileOffsetHigh, uint32_t dwFileOffsetLow,
size_t dwNumberOfBytesToMap,
void *opt_lpDesiredBaseAddress,
uint32_t nndDesiredNumaNode) {
void *pStartingAddress;
pStartingAddress = __imp_MapViewOfFileExNuma(
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, nndDesiredNumaNode);
if (!pStartingAddress) __winerr();
STRACE("MapViewOfFileExNuma(%ld, %s, off:%'ld, size:%'zu, %p) → %p% m",
hFileMappingObject, DescribeNtFileMapFlags(dwDesiredAccess),
(uint64_t)dwFileOffsetHigh << 32 | dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress, pStartingAddress);
return pStartingAddress;
}

View file

@ -0,0 +1,38 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/sysv/consts/prot.h"
privileged uint32_t __prot2nt(int prot, int flags) {
switch (prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) {
case PROT_READ:
return kNtPageReadonly;
case PROT_WRITE:
case PROT_READ | PROT_WRITE:
return kNtPageReadwrite;
case PROT_READ | PROT_EXEC:
return kNtPageExecuteRead;
case PROT_WRITE | PROT_EXEC:
case PROT_READ | PROT_WRITE | PROT_EXEC:
return kNtPageExecuteReadwrite;
default:
return kNtPageNoaccess;
}
}

View file

@ -25,8 +25,6 @@
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
const char kConsoleHandles[2] = {kNtStdInputHandle, kNtStdOutputHandle};
/**
* Exits process faster.
*
@ -40,11 +38,6 @@ wontreturn void quick_exit(int exitcode) {
if (weaken(fflush)) {
weaken(fflush)(0);
}
if (SupportsWindows() && __ntconsolemode[0]) {
for (i = 0; i < 2; ++i) {
SetConsoleMode(GetStdHandle(kConsoleHandles[i]), __ntconsolemode[i]);
}
}
for (p = __fini_array_end; p > __fini_array_start;) {
((void (*)(void))(*--p))();
}

View file

@ -0,0 +1,39 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/memory.h"
extern typeof(VirtualProtect) *const __imp_VirtualProtect __msabi;
/**
* Protects memory on the New Technology.
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
*/
bool32 VirtualProtect(void *lpAddress, uint64_t dwSize, uint32_t flNewProtect,
uint32_t *lpflOldProtect) {
bool32 bOk;
bOk = __imp_VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);
if (!bOk) __winerr();
STRACE("VirtualProtect(%p, %'zu, %s, [%s]) → %hhhd% m", lpAddress, dwSize,
DescribeNtPageFlags(flNewProtect),
DescribeNtPageFlags(*lpflOldProtect), bOk);
return bOk;
}

46
libc/intrin/winerr.greg.c Normal file
View file

@ -0,0 +1,46 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#define ShouldUseMsabiAttribute() 1
#include "libc/bits/weaken.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/nt/errors.h"
#include "libc/nt/runtime.h"
#include "libc/sock/internal.h"
#include "libc/sysv/errfuns.h"
/**
* Return path for failed Win32 API calls.
*
* @return -1 w/ few exceptions
* @note this is a code-size saving device
*/
privileged int64_t __winerr(void) {
errno_t e;
if (IsWindows()) {
e = __imp_GetLastError();
if (weaken(__dos2errno)) {
e = weaken(__dos2errno)(e);
}
} else {
e = ENOSYS;
}
errno = e;
return -1;
}