mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-09 12:48:31 +00:00
Fix XNU / FreeBSD / OpenBSD / RHEL5 / NT bugs
For the first time ever, all tests in this codebase now pass, when run automatically on macos, freebsd, openbsd, rhel5, rhel7, alpine and windows via the network using the runit and runitd build tools - Fix vfork exec path etc. - Add XNU opendir() support - Add OpenBSD opendir() support - Add Linux history to syscalls.sh - Use copy_file_range on FreeBSD 13+ - Fix system calls with 7+ arguments - Fix Windows with greater than 16 FDs - Fix RUNIT.COM and RUNITD.COM flakiness - Fix OpenBSD munmap() when files are mapped - Fix long double so it's actually long on Windows - Fix OpenBSD truncate() and ftruncate() thunk typo - Let Windows fcntl() be used on socket files descriptors - Fix Windows fstat() which had an accidental printf statement - Fix RHEL5 CLOCK_MONOTONIC by not aliasing to CLOCK_MONOTONIC_RAW This is wonderful. I never could have dreamed it would be possible to get it working so well on so many platforms with tiny binaries. Fixes #31 Fixes #25 Fixes #14
This commit is contained in:
parent
c20dad3534
commit
45b72485ad
1032 changed files with 6083 additions and 2348 deletions
|
@ -34,6 +34,8 @@ struct DirectMap __mmap(void *addr, size_t size, unsigned prot, unsigned flags,
|
|||
return (struct DirectMap){mmap$sysv(addr, size, prot, flags, fd, off),
|
||||
kNtInvalidHandleValue};
|
||||
} else {
|
||||
return __mmap$nt(addr, size, prot, flags, fd, off);
|
||||
return __mmap$nt(addr, size, prot,
|
||||
fd != -1 ? g_fds.p[fd].handle : kNtInvalidHandleValue,
|
||||
off);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ struct DirectMap {
|
|||
};
|
||||
|
||||
struct DirectMap __mmap(void *, size_t, unsigned, unsigned, int, int64_t);
|
||||
struct DirectMap __mmap$nt(void *, size_t, unsigned, unsigned, int, int64_t);
|
||||
struct DirectMap __mmap$nt(void *, size_t, unsigned, int64_t, int64_t);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -17,33 +17,33 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/nt/enum/filemapflags.h"
|
||||
#include "libc/nt/enum/pageflags.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/runtime/directmap.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
textwindows struct DirectMap __mmap$nt(void *addr, size_t size, unsigned prot,
|
||||
unsigned flags, int fd, int64_t off) {
|
||||
int64_t handle;
|
||||
struct DirectMap res;
|
||||
if (fd != -1) {
|
||||
handle = g_fds.p[fd].handle;
|
||||
} else {
|
||||
handle = kNtInvalidHandleValue;
|
||||
}
|
||||
if ((res.maphandle = CreateFileMappingNuma(
|
||||
handle, NULL, kNtPageExecuteReadwrite, size >> 32, size, NULL,
|
||||
kNtNumaNoPreferredNode))) {
|
||||
if (!(res.addr = MapViewOfFileExNuma(res.maphandle, fprot2nt(prot, flags),
|
||||
off >> 32, off, size, addr,
|
||||
kNtNumaNoPreferredNode))) {
|
||||
CloseHandle(res.maphandle);
|
||||
res.maphandle = kNtInvalidHandleValue;
|
||||
res.addr = (void *)(intptr_t)__winerr();
|
||||
int64_t handle, int64_t off) {
|
||||
struct DirectMap dm;
|
||||
if ((dm.maphandle = CreateFileMappingNuma(
|
||||
handle, &kNtIsInheritable,
|
||||
(prot & PROT_WRITE) ? kNtPageExecuteReadwrite : kNtPageExecuteRead,
|
||||
size >> 32, size, NULL, kNtNumaNoPreferredNode))) {
|
||||
if (!(dm.addr = MapViewOfFileExNuma(
|
||||
dm.maphandle,
|
||||
(prot & PROT_WRITE)
|
||||
? kNtFileMapWrite | kNtFileMapExecute | kNtFileMapRead
|
||||
: kNtFileMapExecute | kNtFileMapRead,
|
||||
off >> 32, off, size, addr, kNtNumaNoPreferredNode))) {
|
||||
CloseHandle(dm.maphandle);
|
||||
dm.maphandle = kNtInvalidHandleValue;
|
||||
dm.addr = (void *)(intptr_t)__winerr();
|
||||
}
|
||||
} else {
|
||||
res.maphandle = kNtInvalidHandleValue;
|
||||
res.addr = (void *)(intptr_t)__winerr();
|
||||
dm.maphandle = kNtInvalidHandleValue;
|
||||
dm.addr = (void *)(intptr_t)__winerr();
|
||||
}
|
||||
return res;
|
||||
return dm;
|
||||
}
|
||||
|
|
30
libc/runtime/exit2.S
Normal file
30
libc/runtime/exit2.S
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
||||
│vi: set et ft=asm ts=8 tw=8 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/macros.h"
|
||||
.privileged
|
||||
.source __FILE__
|
||||
|
||||
/ Terminates process, ignoring destructors and atexit() handlers.
|
||||
/
|
||||
/ @param edi is exit code ∈ [0,256)
|
||||
/ @asyncsignalsafe
|
||||
/ @vforksafe
|
||||
/ @noreturn
|
||||
_exit: jmp _Exit
|
||||
.endfn _exit,globl,protected
|
|
@ -25,8 +25,9 @@
|
|||
/ Terminates process, ignoring destructors and atexit() handlers.
|
||||
/
|
||||
/ @param edi is exit code ∈ [0,256)
|
||||
/ @note _exit() is same thing
|
||||
/ @asyncsignalsafe
|
||||
/ @vforksafe
|
||||
/ @noreturn
|
||||
_Exit: push %rbp
|
||||
mov %rsp,%rbp
|
||||
#if SupportsWindows()
|
||||
|
@ -38,7 +39,7 @@ _Exit: push %rbp
|
|||
jmp 4b
|
||||
0: int3 # @see setjmp() in WinMain()
|
||||
#endif
|
||||
1: mov __NR_exit(%rip),%eax
|
||||
1: mov __NR_exit_group(%rip),%eax
|
||||
syscall
|
||||
cli
|
||||
lidt 3f
|
||||
|
@ -46,4 +47,4 @@ _Exit: push %rbp
|
|||
jmp 2b
|
||||
3: .quad 0
|
||||
.endfn _Exit,globl,protected
|
||||
.hidden __NR_exit
|
||||
.hidden __NR_exit_group
|
|
@ -66,6 +66,7 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
|
|||
if (!(!!(flags & MAP_ANONYMOUS) ^ (fd != -1))) return VIP(einval());
|
||||
if (!(!!(flags & MAP_PRIVATE) ^ !!(flags & MAP_SHARED))) return VIP(einval());
|
||||
if (fd == -1) size = ROUNDUP(size, FRAMESIZE);
|
||||
if (IsWindows() && fd == -1) prot |= PROT_WRITE;
|
||||
if (flags & MAP_FIXED) {
|
||||
if (UntrackMemoryIntervals(addr, size) == -1) {
|
||||
return MAP_FAILED;
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
/**
|
||||
* Synchronize memory mapping changes to disk.
|
||||
*
|
||||
* Without this, there's no guarantee memory is written back to disk.
|
||||
* Without this, there's no guarantee memory is written back to disk. In
|
||||
* practice, what that means is just Windows NT.
|
||||
*
|
||||
* @param flags needs MS_ASYNC or MS_SYNC and can have MS_INVALIDATE
|
||||
* @return 0 on success or -1 w/ errno
|
||||
|
|
|
@ -30,19 +30,20 @@
|
|||
/**
|
||||
* Releases memory pages.
|
||||
*
|
||||
* This function may be used to punch holes in existing mappings, but your
|
||||
* mileage may vary on Windows.
|
||||
*
|
||||
* @param addr is a pointer within any memory mapped region the process
|
||||
* has permission to control, such as address ranges returned by
|
||||
* mmap(), the program image itself, etc.
|
||||
* @param size is the amount of memory to unmap, which needn't be a
|
||||
* multiple of FRAMESIZE, and may be a subset of that which was
|
||||
* mapped previously, and may punch holes in existing mappings,
|
||||
* but your mileage may vary on windows
|
||||
* @param size is the amount of memory to unmap, which needs to be a
|
||||
* multiple of FRAMESIZE for anonymous mappings, because windows
|
||||
* and for files size needs to be perfect to the byte bc openbsd
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
*/
|
||||
int munmap(void *addr, size_t size) {
|
||||
int rc;
|
||||
if (!ALIGNED(addr) || !CANONICAL(addr) || !size) return einval();
|
||||
size = ROUNDUP(size, FRAMESIZE);
|
||||
if (UntrackMemoryIntervals(addr, size) == -1) return -1;
|
||||
if (IsWindows()) return 0;
|
||||
return munmap$sysv(addr, size);
|
||||
|
|
|
@ -30,13 +30,13 @@
|
|||
|
||||
static privileged void __print$nt(const void *data, size_t len) {
|
||||
int64_t hand;
|
||||
char xmm[256];
|
||||
uint32_t wrote;
|
||||
char xmm[256] forcealign(16);
|
||||
savexmm(&xmm[128]);
|
||||
savexmm(xmm + 128);
|
||||
hand = __imp_GetStdHandle(kNtStdErrorHandle);
|
||||
__imp_WriteFile(hand, data, len, &wrote, NULL);
|
||||
__imp_FlushFileBuffers(hand);
|
||||
loadxmm(&xmm[128]);
|
||||
loadxmm(xmm + 128);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -44,7 +44,7 @@ void __stack_chk_fail(void) {
|
|||
: "rcx", "r11", "cc", "memory");
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax)
|
||||
: "0"(__NR_exit), "D"(pushpop(23))
|
||||
: "0"(__NR_exit_group), "D"(pushpop(23))
|
||||
: "rcx", "r11", "cc", "memory");
|
||||
}
|
||||
asm volatile("rep outsb"
|
||||
|
|
|
@ -36,6 +36,7 @@
|
|||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/teb.h"
|
||||
#include "libc/runtime/directmap.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/memtrack.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
@ -50,52 +51,34 @@
|
|||
#define ADDRESS 0x77700000 /*0000*/
|
||||
|
||||
struct WinArgs {
|
||||
char *argv[512];
|
||||
char *envp[512];
|
||||
long auxv[1][2];
|
||||
char *argv[4096];
|
||||
char *envp[4096];
|
||||
char argblock[ARG_MAX];
|
||||
char envblock[ENV_MAX];
|
||||
char envblock[ARG_MAX];
|
||||
};
|
||||
|
||||
static struct CmdExe {
|
||||
bool result;
|
||||
struct OldNtConsole {
|
||||
uint32_t codepage;
|
||||
uint32_t mode;
|
||||
int64_t handle;
|
||||
} oldin, oldout;
|
||||
} g_cmdexe;
|
||||
|
||||
static textwindows void RestoreCmdExe(void) {
|
||||
if (g_cmdexe.oldin.handle) {
|
||||
SetConsoleCP(g_cmdexe.oldin.codepage);
|
||||
SetConsoleMode(g_cmdexe.oldin.handle, g_cmdexe.oldin.mode);
|
||||
}
|
||||
if (g_cmdexe.oldout.handle) {
|
||||
SetConsoleOutputCP(g_cmdexe.oldout.codepage);
|
||||
SetConsoleMode(g_cmdexe.oldout.handle, g_cmdexe.oldout.mode);
|
||||
}
|
||||
}
|
||||
|
||||
static textwindows void SetTrueColor(void) {
|
||||
SetEnvironmentVariable(u"TERM", u"xterm-truecolor");
|
||||
}
|
||||
|
||||
static textwindows void MakeLongDoubleLongAgain(void) {
|
||||
int x87cw = 0x037f; /* let's hope win32 won't undo this */
|
||||
asm volatile("fldcw\t%0" : /* no outputs */ : "m"(x87cw));
|
||||
}
|
||||
|
||||
static textwindows void NormalizeCmdExe(void) {
|
||||
uint32_t mode;
|
||||
int64_t handle, hstdin, hstdout, hstderr;
|
||||
if ((int)weakaddr("v_ntsubsystem") == kNtImageSubsystemWindowsCui &&
|
||||
NtGetVersion() >= kNtVersionWindows10) {
|
||||
atexit(RestoreCmdExe);
|
||||
hstdin = GetStdHandle(pushpop(kNtStdInputHandle));
|
||||
hstdout = GetStdHandle(pushpop(kNtStdOutputHandle));
|
||||
hstderr = GetStdHandle(pushpop(kNtStdErrorHandle));
|
||||
if (GetFileType((handle = hstdin)) == kNtFileTypeChar) {
|
||||
SetTrueColor();
|
||||
g_cmdexe.oldin.handle = handle;
|
||||
g_cmdexe.oldin.codepage = GetConsoleCP();
|
||||
SetConsoleCP(kNtCpUtf8);
|
||||
GetConsoleMode(handle, &g_cmdexe.oldin.mode);
|
||||
SetConsoleMode(handle, g_cmdexe.oldin.mode | kNtEnableProcessedInput |
|
||||
GetConsoleMode(handle, &mode);
|
||||
SetConsoleMode(handle, mode | kNtEnableProcessedInput |
|
||||
kNtEnableEchoInput | kNtEnableLineInput |
|
||||
kNtEnableWindowInput |
|
||||
kNtEnableVirtualTerminalInput);
|
||||
|
@ -103,11 +86,9 @@ static textwindows void NormalizeCmdExe(void) {
|
|||
if (GetFileType((handle = hstdout)) == kNtFileTypeChar ||
|
||||
GetFileType((handle = hstderr)) == kNtFileTypeChar) {
|
||||
SetTrueColor();
|
||||
g_cmdexe.oldout.handle = handle;
|
||||
g_cmdexe.oldout.codepage = GetConsoleOutputCP();
|
||||
SetConsoleOutputCP(kNtCpUtf8);
|
||||
GetConsoleMode(handle, &g_cmdexe.oldout.mode);
|
||||
SetConsoleMode(handle, g_cmdexe.oldout.mode | kNtEnableProcessedOutput |
|
||||
GetConsoleMode(handle, &mode);
|
||||
SetConsoleMode(handle, mode | kNtEnableProcessedOutput |
|
||||
kNtEnableWrapAtEolOutput |
|
||||
(NtGetVersion() >= kNtVersionWindows10
|
||||
? kNtEnableVirtualTerminalProcessing
|
||||
|
@ -116,44 +97,38 @@ static textwindows void NormalizeCmdExe(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static textwindows char *AllocateMemory(void *addr, size_t size, int64_t *h) {
|
||||
return MapViewOfFileExNuma(
|
||||
(*h = CreateFileMappingNuma(-1, NULL, pushpop(kNtPageExecuteReadwrite), 0,
|
||||
size, NULL, kNtNumaNoPreferredNode)),
|
||||
kNtFileMapRead | kNtFileMapWrite, 0, 0, size, addr,
|
||||
kNtNumaNoPreferredNode);
|
||||
}
|
||||
|
||||
static textwindows wontreturn void WinMainNew(void) {
|
||||
int64_t h;
|
||||
size_t size;
|
||||
int i, count;
|
||||
uint64_t data;
|
||||
long auxv[1][2];
|
||||
struct WinArgs *wa;
|
||||
const char16_t *env16;
|
||||
NormalizeCmdExe();
|
||||
*(/*unconst*/ int *)&__hostos = WINDOWS;
|
||||
size = ROUNDUP(STACKSIZE + sizeof(struct WinArgs), FRAMESIZE);
|
||||
data = (intptr_t)AllocateMemory((char *)ADDRESS, size, &_mmi.p[0].h);
|
||||
_mmi.p[0].x = data >> 16;
|
||||
_mmi.p[0].y = (data >> 16) + ((size >> 16) - 1);
|
||||
_mmi.p[0].prot = PROT_READ | PROT_WRITE;
|
||||
_mmi.p[0].h = __mmap$nt((char *)ADDRESS, size,
|
||||
PROT_READ | PROT_WRITE | PROT_EXEC, -1, 0)
|
||||
.maphandle;
|
||||
_mmi.p[0].x = ADDRESS >> 16;
|
||||
_mmi.p[0].y = (ADDRESS >> 16) + ((size >> 16) - 1);
|
||||
_mmi.p[0].prot = PROT_READ | PROT_WRITE | PROT_EXEC;
|
||||
_mmi.p[0].flags = MAP_PRIVATE | MAP_ANONYMOUS;
|
||||
_mmi.i = pushpop(1L);
|
||||
wa = (struct WinArgs *)(data + size - sizeof(struct WinArgs));
|
||||
count = GetDosArgv(GetCommandLine(), wa->argblock, ARG_MAX, wa->argv, 512);
|
||||
wa = (struct WinArgs *)(ADDRESS + size - sizeof(struct WinArgs));
|
||||
count = GetDosArgv(GetCommandLine(), wa->argblock, ARG_MAX, wa->argv, 4096);
|
||||
for (i = 0; wa->argv[0][i]; ++i) {
|
||||
if (wa->argv[0][i] == '\\') {
|
||||
wa->argv[0][i] = '/';
|
||||
}
|
||||
}
|
||||
env16 = GetEnvironmentStrings();
|
||||
GetDosEnviron(env16, wa->envblock, ENV_MAX, wa->envp, 512);
|
||||
GetDosEnviron(env16, wa->envblock, ARG_MAX, wa->envp, 4096);
|
||||
FreeEnvironmentStrings(env16);
|
||||
wa->auxv[0][0] = pushpop(0L);
|
||||
wa->auxv[0][1] = pushpop(0L);
|
||||
_jmpstack((char *)data + STACKSIZE, _executive, count, wa->argv, wa->envp,
|
||||
wa->auxv);
|
||||
auxv[0][0] = pushpop(0L);
|
||||
auxv[0][1] = pushpop(0L);
|
||||
_jmpstack((char *)ADDRESS + STACKSIZE, _executive, count, wa->argv, wa->envp,
|
||||
auxv);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -182,7 +157,9 @@ static textwindows wontreturn void WinMainNew(void) {
|
|||
* the downloads folder. Since we don't even use dynamic linking,
|
||||
* we've cargo culted some API calls, that may harden against it.
|
||||
*
|
||||
* 6. Finally, we need fork. Microsoft designed Windows to prevent us
|
||||
* 6. Reconfigure x87 FPU so long double is actually long (80 bits).
|
||||
*
|
||||
* 7. Finally, we need fork. Microsoft designed Windows to prevent us
|
||||
* from having fork() so we pass pipe handles in an environment
|
||||
* variable literally copy all the memory.
|
||||
*
|
||||
|
@ -190,6 +167,7 @@ static textwindows wontreturn void WinMainNew(void) {
|
|||
*/
|
||||
textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
|
||||
const char *lpCmdLine, int nCmdShow) {
|
||||
MakeLongDoubleLongAgain();
|
||||
if (weaken(winsockinit)) weaken(winsockinit)();
|
||||
if (weaken(WinMainForked)) weaken(WinMainForked)();
|
||||
WinMainNew();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue