mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-02 17:28:30 +00:00
Make more improvements
This change includes many bug fixes, for the NT polyfills, strings, memory, boot, and math libraries which were discovered by adding more tools for recreational programming, such as PC emulation. Lemon has also been vendored because it works so well at parsing languages.
This commit is contained in:
parent
416fd86676
commit
23d333c090
201 changed files with 14558 additions and 3082 deletions
|
@ -18,6 +18,7 @@
|
|||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/nt/memory.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/runtime/directmap.h"
|
||||
|
@ -25,14 +26,21 @@
|
|||
static textwindows struct DirectMap DirectMapNt(void *addr, size_t size,
|
||||
unsigned prot, unsigned flags,
|
||||
int fd, int64_t off) {
|
||||
int64_t handle;
|
||||
struct DirectMap res;
|
||||
if ((res.maphandle = CreateFileMappingNuma(
|
||||
fd != -1 ? g_fds.p[fd].handle : kNtInvalidHandleValue,
|
||||
&kNtIsInheritable, prot2nt(prot, flags), size >> 32, size, NULL,
|
||||
kNtNumaNoPreferredNode))) {
|
||||
if (!(res.addr = MapViewOfFileExNuma(res.maphandle, fprot2nt(prot, flags),
|
||||
off >> 32, off, size, addr,
|
||||
kNtNumaNoPreferredNode))) {
|
||||
uint32_t protect, access;
|
||||
if (fd != -1) {
|
||||
handle = g_fds.p[fd].handle;
|
||||
} else {
|
||||
handle = kNtInvalidHandleValue;
|
||||
}
|
||||
protect = prot2nt(prot, flags);
|
||||
access = fprot2nt(prot, flags);
|
||||
if ((res.maphandle =
|
||||
CreateFileMappingNuma(handle, &kNtIsInheritable, protect, size >> 32,
|
||||
size, NULL, kNtNumaNoPreferredNode))) {
|
||||
if (!(res.addr = MapViewOfFileExNuma(res.maphandle, access, off >> 32, off,
|
||||
size, addr, kNtNumaNoPreferredNode))) {
|
||||
CloseHandle(res.maphandle);
|
||||
res.maphandle = kNtInvalidHandleValue;
|
||||
res.addr = (void *)(intptr_t)winerr();
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/notice.inc"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/dce.h"
|
||||
.text.startup
|
||||
.source __FILE__
|
||||
|
@ -40,12 +42,15 @@ _executive:
|
|||
mov %rdx,%r14
|
||||
mov %rcx,%r15
|
||||
call _spawn
|
||||
mov %r12,%rdi
|
||||
mov %r13,%rsi
|
||||
mov %r14,%rdx
|
||||
mov %r15,%rcx
|
||||
call _getstack
|
||||
mov %rax,%rdi
|
||||
.weak main
|
||||
call main
|
||||
mov $main,%esi
|
||||
mov %r12,%rdx
|
||||
mov %r13,%rcx
|
||||
mov %r14,%r8
|
||||
mov %r15,%r9
|
||||
call _setstack
|
||||
mov %eax,%edi
|
||||
call exit
|
||||
9: .endfn _executive,weak,hidden
|
||||
|
|
|
@ -34,9 +34,9 @@ int mapfileread(const char *filename, struct MappedFile *mf) {
|
|||
mf->addr = MAP_FAILED;
|
||||
if ((mf->fd = open(filename, O_RDONLY)) != -1 &&
|
||||
(mf->size = getfiledescriptorsize(mf->fd)) < INT_MAX &&
|
||||
(mf->addr = mf->size
|
||||
? mmap(NULL, mf->size, PROT_READ, MAP_SHARED, mf->fd, 0)
|
||||
: NULL) != MAP_FAILED) {
|
||||
(mf->addr = mf->size ? mmap(NULL, mf->size, PROT_READ,
|
||||
MAP_PRIVATE | MAP_POPULATE, mf->fd, 0)
|
||||
: NULL) != MAP_FAILED) {
|
||||
return 0;
|
||||
} else {
|
||||
unmapfile(mf);
|
||||
|
@ -50,7 +50,7 @@ int mapfileread(const char *filename, struct MappedFile *mf) {
|
|||
int unmapfile(struct MappedFile *mf) {
|
||||
int rc;
|
||||
rc = 0;
|
||||
if (mf->addr != MAP_FAILED) {
|
||||
if (mf->addr && mf->addr != MAP_FAILED) {
|
||||
rc |= munmap(mf->addr, mf->size);
|
||||
mf->addr = MAP_FAILED;
|
||||
}
|
||||
|
|
36
libc/runtime/getstack.c
Normal file
36
libc/runtime/getstack.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*-*- 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 │
|
||||
│ │
|
||||
│ This program is free software; you can redistribute it and/or modify │
|
||||
│ it under the terms of the GNU General Public License as published by │
|
||||
│ the Free Software Foundation; version 2 of the License. │
|
||||
│ │
|
||||
│ This program is distributed in the hope that it will be useful, but │
|
||||
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
|
||||
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
|
||||
│ General Public License for more details. │
|
||||
│ │
|
||||
│ You should have received a copy of the GNU General Public License │
|
||||
│ along with this program; if not, write to the Free Software │
|
||||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
|
||||
/**
|
||||
* Allocates deterministic stack for process.
|
||||
* @see _executive()
|
||||
*/
|
||||
void *_getstack(void) {
|
||||
char *p;
|
||||
p = mmap((char *)0x700000000000 - STACKSIZE, STACKSIZE,
|
||||
PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1,
|
||||
0);
|
||||
if (p == MAP_FAILED) abort();
|
||||
return p + STACKSIZE;
|
||||
}
|
|
@ -12,8 +12,8 @@ struct MemoryIntervals {
|
|||
struct MemoryInterval {
|
||||
int x;
|
||||
int y;
|
||||
} p[32];
|
||||
long h[32];
|
||||
} p[64];
|
||||
long h[64];
|
||||
};
|
||||
|
||||
extern struct MemoryIntervals _mmi;
|
||||
|
|
|
@ -44,6 +44,10 @@ struct MemoryIntervals _mmi;
|
|||
/**
|
||||
* Beseeches system for page-table entries.
|
||||
*
|
||||
* char *p = mmap(NULL, 65536, PROT_READ | PROT_WRITE,
|
||||
* MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
* munmap(p, 65536);
|
||||
*
|
||||
* @param addr optionally requests a particular virtual base address,
|
||||
* which needs to be 64kb aligned if passed (for NT compatibility)
|
||||
* @param size must be >0 and needn't be a multiple of FRAMESIZE
|
||||
|
@ -82,8 +86,6 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
|
|||
}
|
||||
addr = (void *)(intptr_t)((int64_t)x << 16);
|
||||
}
|
||||
assert((flags & MAP_FIXED) ||
|
||||
(!isheap(addr) && !isheap((char *)addr + size - 1)));
|
||||
dm = DirectMap(addr, size, prot, flags | MAP_FIXED, fd, off);
|
||||
if (dm.addr == MAP_FAILED || dm.addr != addr) {
|
||||
return MAP_FAILED;
|
||||
|
|
|
@ -25,16 +25,14 @@
|
|||
|
||||
textwindows int msync$nt(void *addr, size_t size, int flags) {
|
||||
int x, y, l, r, i;
|
||||
if (!FlushViewOfFile(addr, size)) return winerr();
|
||||
x = (intptr_t)addr >> 16;
|
||||
y = x + (ROUNDUP(size, 65536) >> 16) - 1;
|
||||
l = FindMemoryInterval(&_mmi, x);
|
||||
r = FindMemoryInterval(&_mmi, y);
|
||||
if (l && x <= _mmi.p[l - 1].y) --l;
|
||||
if (r && y <= _mmi.p[r - 1].y) --r;
|
||||
if (l < _mmi.i) {
|
||||
for (i = l; i <= r; --i) {
|
||||
FlushFileBuffers(_mmi.h[i - 1]);
|
||||
x = ROUNDDOWN((intptr_t)addr, FRAMESIZE) >> 16;
|
||||
y = ROUNDDOWN((intptr_t)addr + size - 1, FRAMESIZE) >> 16;
|
||||
for (i = FindMemoryInterval(&_mmi, x); i < _mmi.i; ++i) {
|
||||
if ((x >= _mmi.p[i].x && x <= _mmi.p[i].y) ||
|
||||
(y >= _mmi.p[i].x && y <= _mmi.p[i].y)) {
|
||||
FlushFileBuffers(_mmi.h[i]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
|
@ -44,6 +44,7 @@ void exit(int) noreturn;
|
|||
void quick_exit(int) noreturn;
|
||||
void _exit(int) libcesque noreturn;
|
||||
void _Exit(int) libcesque noreturn;
|
||||
long _setstack(void *, void *, ...);
|
||||
void abort(void) noreturn noinstrument;
|
||||
void panic(void) noreturn noinstrument privileged;
|
||||
void triplf(void) noreturn noinstrument privileged;
|
||||
|
|
|
@ -69,6 +69,8 @@ o/$(MODE)/libc/runtime/winmain.greg.o: \
|
|||
DEFAULT_CPPFLAGS += \
|
||||
-DSTACK_FRAME_UNLIMITED
|
||||
|
||||
o/$(MODE)/libc/runtime/main-gcc.asm: OVERRIDE_CFLAGS += -ffixed-r12 -ffixed-r13 -ffixed-r14 -ffixed-r15
|
||||
|
||||
LIBC_RUNTIME_LIBS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)))
|
||||
LIBC_RUNTIME_SRCS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)_SRCS))
|
||||
LIBC_RUNTIME_HDRS = $(foreach x,$(LIBC_RUNTIME_ARTIFACTS),$($(x)_HDRS))
|
||||
|
|
|
@ -17,30 +17,30 @@
|
|||
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
|
||||
│ 02110-1301 USA │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/sysv/consts/madv.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/macros.h"
|
||||
.text.startup
|
||||
.source __FILE__
|
||||
|
||||
/ Merges page table entries for pages with identical content.
|
||||
/ Switches stack.
|
||||
/
|
||||
/ This is a hint. It can help trim physical memory for things
|
||||
/ like extremely sparse data.
|
||||
/
|
||||
/ @param rdi is base address
|
||||
/ @param rsi is byte length
|
||||
mergepages:
|
||||
.leafprologue
|
||||
.profilable
|
||||
mov $-PAGESIZE,%rax
|
||||
and %rax,%rdi
|
||||
and %rax,%rsi
|
||||
mov __NR_madvise,%eax
|
||||
mov MADV_MERGEABLE,%edx
|
||||
test %edx,%edx
|
||||
jz 1f
|
||||
syscall
|
||||
1: .leafepilogue
|
||||
.endfn mergepages,globl
|
||||
/ @param rdi is new rsp, passed as malloc(size) + size
|
||||
/ @param rsi is function to call in new stack space
|
||||
/ @param rdx,rcx,r8,r9 get passed as args to rsi
|
||||
/ @return happens on original stack
|
||||
_setstack:
|
||||
push %rbp
|
||||
mov %rsp,%rbp
|
||||
push %rbx
|
||||
mov %rsp,%rbx
|
||||
mov %rdi,%rsp
|
||||
push 16(%rbx)
|
||||
push 8(%rbx)
|
||||
mov %rsi,%rax
|
||||
mov %rdx,%rdi
|
||||
mov %rcx,%rsi
|
||||
mov %r8,%rdx
|
||||
mov %r9,%rcx
|
||||
call *%rax
|
||||
mov %rbx,%rsp
|
||||
pop %rbx
|
||||
pop %rbp
|
||||
ret
|
||||
.endfn _setstack,globl
|
|
@ -19,15 +19,32 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/bits/pushpop.h"
|
||||
#include "libc/bits/weaken.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/nt/console.h"
|
||||
#include "libc/nt/enum/consolemodeflags.h"
|
||||
#include "libc/nt/enum/filetype.h"
|
||||
#include "libc/nt/enum/loadlibrarysearch.h"
|
||||
#include "libc/nt/files.h"
|
||||
#include "libc/nt/pedef.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/teb.h"
|
||||
#include "libc/runtime/getdosenviron.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/missioncritical.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
||||
static void LoadFasterAndPreventHijacking(void) {
|
||||
static struct CmdExe {
|
||||
bool result;
|
||||
struct OldNtConsole {
|
||||
unsigned codepage;
|
||||
unsigned mode;
|
||||
int64_t handle;
|
||||
} oldin, oldout;
|
||||
} g_cmdexe;
|
||||
|
||||
static textwindows void MitigateDriveByDownloads(void) {
|
||||
unsigned wrote;
|
||||
if (!SetDefaultDllDirectories(kNtLoadLibrarySearchSearchSystem32)) {
|
||||
WriteFile(GetStdHandle(kNtStdErrorHandle), "nodll\n", 6, &wrote, NULL);
|
||||
|
@ -35,6 +52,78 @@ static void LoadFasterAndPreventHijacking(void) {
|
|||
}
|
||||
}
|
||||
|
||||
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 NormalizeCmdExe(void) {
|
||||
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 |
|
||||
kNtEnableVirtualTerminalInput);
|
||||
}
|
||||
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 |
|
||||
kNtEnableVirtualTerminalProcessing);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function on Windows NT.
|
||||
*
|
||||
* The Cosmopolitan Runtime provides the following services, which aim
|
||||
* to bring Windows NT behavior closer in harmony with System Five:
|
||||
*
|
||||
* 1. We configure CMD.EXE for UTF-8 and enable ANSI colors on Win10.
|
||||
*
|
||||
* 2. Command line arguments are passed as a blob of UTF-16 text. We
|
||||
* chop them up into an char *argv[] UTF-8 data structure, in
|
||||
* accordance with the DOS conventions for argument quoting.
|
||||
*
|
||||
* 3. Environment variables are passed to us as a sorted UTF-16 double
|
||||
* NUL terminated list. We translate this to char ** using UTF-8.
|
||||
*
|
||||
* 4. NT likes to choose a stack address that's beneath the program
|
||||
* image. We want to be able to assume that stack addresses are
|
||||
* located at higher addresses than heap and program memory. So the
|
||||
* _executive() function will switch stacks appropriately.
|
||||
*
|
||||
* 5. Windows users are afraid of "drive-by downloads" where someone
|
||||
* might accidentally an evil DLL to their Downloads folder which
|
||||
* then overrides the behavior of a legitimate EXE being run from
|
||||
* the downloads folder. Since we don't even use dynamic linking,
|
||||
* we've cargo culted some API calls, that may harden against it.
|
||||
*
|
||||
*/
|
||||
textwindows int WinMain(void *hInstance, void *hPrevInstance,
|
||||
const char *lpCmdLine, int nCmdShow) {
|
||||
int i, count;
|
||||
|
@ -42,7 +131,8 @@ textwindows int WinMain(void *hInstance, void *hPrevInstance,
|
|||
char *argarray[512], *envarray[512];
|
||||
char argblock[ARG_MAX], envblock[ENV_MAX];
|
||||
long auxarray[][2] = {{pushpop(0L), pushpop(0L)}};
|
||||
LoadFasterAndPreventHijacking();
|
||||
MitigateDriveByDownloads();
|
||||
NormalizeCmdExe();
|
||||
*(/*unconst*/ int *)&hostos = WINDOWS;
|
||||
cmd16 = GetCommandLine();
|
||||
env16 = GetEnvironmentStrings();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue