Get life.com in MODE=tiny back down to 12kb

This commit is contained in:
Justine Tunney 2022-03-23 08:09:01 -07:00
parent 23b72eb617
commit b90fa996b4
14 changed files with 199 additions and 72 deletions

View file

@ -44,12 +44,12 @@ textwindows noasan struct DirectMap sys_mmap_nt(void *addr, size_t size,
* combination of flags, that'll cause Windows to actually do this!
*/
upsize = ROUNDUP(size, FRAMESIZE);
if ((dm.maphandle = CreateFileMappingNuma(
-1, &kNtIsInheritable, kNtPageExecuteReadwrite, upsize >> 32,
upsize, NULL, kNtNumaNoPreferredNode))) {
if ((dm.addr = MapViewOfFileExNuma(
dm.maphandle, kNtFileMapWrite | kNtFileMapExecute, 0, 0, upsize,
addr, kNtNumaNoPreferredNode))) {
if ((dm.maphandle =
CreateFileMapping(-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
upsize >> 32, upsize, NULL))) {
if ((dm.addr = MapViewOfFileEx(dm.maphandle,
kNtFileMapWrite | kNtFileMapExecute, 0, 0,
upsize, addr))) {
for (i = 0; i < size; i += got) {
got = 0;
op.Internal = 0;
@ -68,16 +68,15 @@ textwindows noasan struct DirectMap sys_mmap_nt(void *addr, size_t size,
CloseHandle(dm.maphandle);
}
} else {
if ((dm.maphandle = CreateFileMappingNuma(
if ((dm.maphandle = CreateFileMapping(
handle, &kNtIsInheritable,
(prot & PROT_WRITE) ? kNtPageExecuteReadwrite : kNtPageExecuteRead,
handle != -1 ? 0 : size >> 32, handle != -1 ? 0 : size, NULL,
kNtNumaNoPreferredNode))) {
if ((dm.addr = MapViewOfFileExNuma(
dm.maphandle,
(prot & PROT_WRITE) ? kNtFileMapWrite | kNtFileMapExecute
: kNtFileMapRead | kNtFileMapExecute,
off >> 32, off, size, addr, kNtNumaNoPreferredNode))) {
handle != -1 ? 0 : size >> 32, handle != -1 ? 0 : size, NULL))) {
if ((dm.addr = MapViewOfFileEx(dm.maphandle,
(prot & PROT_WRITE)
? kNtFileMapWrite | kNtFileMapExecute
: kNtFileMapRead | kNtFileMapExecute,
off >> 32, off, size, addr))) {
return dm;
}
CloseHandle(dm.maphandle);

View file

@ -75,15 +75,13 @@ textwindows int ntspawn(
block = NULL;
if (__mkntpath(prog, prog16) == -1) return -1;
blocksize = ROUNDUP(sizeof(*block), FRAMESIZE);
if ((handle = CreateFileMappingNuma(
if ((handle = CreateFileMapping(
-1,
&(struct NtSecurityAttributes){sizeof(struct NtSecurityAttributes),
NULL, false},
pushpop(kNtPageReadwrite), 0, blocksize, NULL,
kNtNumaNoPreferredNode)) &&
(block =
MapViewOfFileExNuma(handle, kNtFileMapRead | kNtFileMapWrite, 0, 0,
blocksize, NULL, kNtNumaNoPreferredNode))) {
pushpop(kNtPageReadwrite), 0, blocksize, NULL)) &&
(block = MapViewOfFileEx(handle, kNtFileMapRead | kNtFileMapWrite, 0, 0,
blocksize, NULL))) {
if (mkntcmdline(block->cmdline, prog, argv) != -1 &&
mkntenvblock(block->envvars, envp, extravar) != -1) {
if (CreateProcess(prog16, block->cmdline, opt_lpProcessAttributes,

View file

@ -47,13 +47,12 @@ static const char *DescribeDisposition(int x) {
*
* @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) {
textwindows 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,

View file

@ -0,0 +1,50 @@
/*-*- 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(CreateFileMapping) *const __imp_CreateFileMappingW __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 MapViewOfFileEx()
*/
textwindows int64_t CreateFileMapping(
int64_t opt_hFile,
const struct NtSecurityAttributes *opt_lpFileMappingAttributes,
uint32_t flProtect, uint32_t dwMaximumSizeHigh, uint32_t dwMaximumSizeLow,
const char16_t *opt_lpName) {
int64_t hHandle;
hHandle = __imp_CreateFileMappingW(opt_hFile, opt_lpFileMappingAttributes,
flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, opt_lpName);
if (!hHandle) __winerr();
STRACE("CreateFileMapping(%ld, %s, max:%'zu, name:%#hs) → %ld% m", opt_hFile,
DescribeNtPageFlags(flProtect),
(uint64_t)dwMaximumSizeHigh << 32 | dwMaximumSizeLow, opt_lpName,
hHandle);
return hHandle;
}

View file

@ -18,6 +18,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/memory.h"
#include "libc/nt/struct/securityattributes.h"
@ -33,7 +34,7 @@ extern typeof(CreateFileMappingNuma) *const
* @note this wrapper takes care of ABI, STRACE(), and __winerr()
* @see MapViewOfFileExNuma()
*/
int64_t CreateFileMappingNuma(
textwindows int64_t CreateFileMappingNuma(
int64_t opt_hFile,
const struct NtSecurityAttributes *opt_lpFileMappingAttributes,
uint32_t flProtect, uint32_t dwMaximumSizeHigh, uint32_t dwMaximumSizeLow,

View file

@ -0,0 +1,53 @@
/*-*- 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(MapViewOfFileEx) *const __imp_MapViewOfFileEx __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()
*/
textwindows void *MapViewOfFileEx(int64_t hFileMappingObject,
uint32_t dwDesiredAccess,
uint32_t dwFileOffsetHigh,
uint32_t dwFileOffsetLow,
size_t dwNumberOfBytesToMap,
void *opt_lpDesiredBaseAddress) {
void *pStartingAddress;
pStartingAddress = __imp_MapViewOfFileEx(
hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow,
dwNumberOfBytesToMap, opt_lpDesiredBaseAddress);
if (!pStartingAddress) __winerr();
STRACE("MapViewOfFileEx(%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

@ -19,6 +19,7 @@
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/memory.h"
@ -34,11 +35,13 @@ extern typeof(MapViewOfFileExNuma) *const __imp_MapViewOfFileExNuma __msabi;
* @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) {
textwindows 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,

View file

@ -2,11 +2,11 @@
.imp kernel32,__imp_CreateFileMappingW,CreateFileMappingW,0
.text.windows
CreateFileMapping:
__CreateFileMapping:
push %rbp
mov %rsp,%rbp
.profilable
mov __imp_CreateFileMappingW(%rip),%rax
jmp __sysv2nt8
.endfn CreateFileMapping,globl
jmp __sysv2nt6
.endfn __CreateFileMapping,globl
.previous

View file

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

View file

@ -580,10 +580,11 @@ imp 'CreateFiber' CreateFiber kernel32 0 # KernelBase
imp 'CreateFiberEx' CreateFiberEx kernel32 0 # KernelBase
imp 'CreateFileA' CreateFileA kernel32 0 7 # KernelBase
imp '__CreateFile' CreateFileW kernel32 0 7 # KernelBase
imp '__CreateFileMapping' CreateFileMappingW kernel32 0 6 # KernelBase
imp '__CreateFileMappingNuma' CreateFileMappingNumaW kernel32 0 7 # Kernelbase
imp '__MapViewOfFileEx' MapViewOfFileEx kernel32 0 6 # 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
imp 'CreateFileTransacted' CreateFileTransactedW kernel32 202
imp 'CreateFileTransactedA' CreateFileTransactedA kernel32 201
@ -3339,7 +3340,6 @@ imp 'MapUserPhysicalPagesScatter' MapUserPhysicalPagesScatter kernel32 986
imp 'MapViewOfFile' MapViewOfFile kernel32 0 # KernelBase
imp 'MapViewOfFile3' MapViewOfFile3 KernelBase 1003
imp 'MapViewOfFile3FromApp' MapViewOfFile3FromApp KernelBase 1004
imp 'MapViewOfFileEx' MapViewOfFileEx kernel32 0 # KernelBase
imp 'MapViewOfFileFromApp' MapViewOfFileFromApp kernel32 0 # KernelBase
imp 'MapViewOfFileNuma2' MapViewOfFileNuma2 KernelBase 1008
imp 'MapVirtualKeyA' MapVirtualKeyA user32 2153

View file

@ -36,18 +36,26 @@ COSMOPOLITAN_C_START_
void *LocalFree(void *hMem);
int64_t CreateFileMapping(
int64_t opt_hFile,
const struct NtSecurityAttributes *opt_lpFileMappingAttributes,
uint32_t flProtect, uint32_t dwMaximumSizeHigh, uint32_t dwMaximumSizeLow,
const char16_t *opt_lpName);
int64_t CreateFileMappingNuma(
int64_t opt_hFile /* -1ul is MAP_ANONYMOUS */,
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);
void *MapViewOfFileExNuma(
int64_t hFileMappingObject, /* @see CreateFileMapping() */
uint32_t dwDesiredAccess, uint32_t dwFileOffsetHigh, /* high order bits */
uint32_t dwFileOffsetLow, /* low order bits */
size_t dwNumberOfBytesToMap, void *opt_lpDesiredBaseAddress,
uint32_t nndDesiredNumaNode);
void *MapViewOfFileEx(int64_t hFileMappingObject, uint32_t dwDesiredAccess,
uint32_t dwFileOffsetHigh, uint32_t dwFileOffsetLow,
size_t dwNumberOfBytesToMap,
void *opt_lpDesiredBaseAddress);
void *MapViewOfFileExNuma(int64_t hFileMappingObject, uint32_t dwDesiredAccess,
uint32_t dwFileOffsetHigh, uint32_t dwFileOffsetLow,
size_t dwNumberOfBytesToMap,
void *opt_lpDesiredBaseAddress,
uint32_t nndDesiredNumaNode);
bool32 UnmapViewOfFile(const void *lpBaseAddress);
bool32 FlushViewOfFile(const void *lpBaseAddress,

View file

@ -33,9 +33,14 @@
* @noreturn
*/
wontreturn void exit(int exitcode) {
const uintptr_t *p;
STRACE("exit(%d)", exitcode);
if (weaken(__cxa_finalize)) {
weaken(__cxa_finalize)(NULL);
}
quick_exit(exitcode);
for (p = __fini_array_end; p > __fini_array_start;) {
((void (*)(void))(*--p))();
}
__restorewintty();
_Exit(exitcode);
}

View file

@ -160,19 +160,16 @@ textwindows void WinMainForked(void) {
ReadAll(reader, &mapcount, sizeof(_mmi.i));
ReadAll(reader, &mapcapacity, sizeof(_mmi.n));
specialz = ROUNDUP(mapcapacity * sizeof(_mmi.p[0]), kMemtrackGran);
MapViewOfFileExNuma(CreateFileMappingNuma(
-1, &kNtIsInheritable, kNtPageReadwrite,
specialz >> 32, specialz, 0, kNtNumaNoPreferredNode),
kNtFileMapWrite, 0, 0, specialz, maps,
kNtNumaNoPreferredNode);
MapViewOfFileEx(CreateFileMapping(-1, &kNtIsInheritable, kNtPageReadwrite,
specialz >> 32, specialz, 0),
kNtFileMapWrite, 0, 0, specialz, maps);
ReadAll(reader, maps, mapcount * sizeof(_mmi.p[0]));
if (IsAsan()) {
shad = (char *)(((intptr_t)maps >> 3) + 0x7fff8000);
size = ROUNDUP(specialz >> 3, FRAMESIZE);
MapViewOfFileExNuma(
CreateFileMappingNuma(-1, &kNtIsInheritable, kNtPageReadwrite,
size >> 32, size, 0, kNtNumaNoPreferredNode),
kNtFileMapWrite, 0, 0, size, maps, kNtNumaNoPreferredNode);
MapViewOfFileEx(CreateFileMapping(-1, &kNtIsInheritable, kNtPageReadwrite,
size >> 32, size, 0),
kNtFileMapWrite, 0, 0, size, maps);
#if 0
ReadAll(reader, shad, (mapcount * sizeof(_mmi.p[0])) >> 3);
#endif
@ -191,21 +188,20 @@ textwindows void WinMainForked(void) {
STRACE("fork() child CloseHandle(%ld) ~~~FAILED~~~ %m", maps[i].h);
}
upsize = ROUNDUP(size, FRAMESIZE);
maps[i].h = CreateFileMappingNuma(-1, &kNtIsInheritable,
kNtPageExecuteReadwrite, upsize >> 32,
upsize, NULL, kNtNumaNoPreferredNode);
MapViewOfFileExNuma(maps[i].h, kNtFileMapWrite | kNtFileMapExecute, 0, 0,
upsize, addr, kNtNumaNoPreferredNode);
maps[i].h =
CreateFileMapping(-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
upsize >> 32, upsize, NULL);
MapViewOfFileEx(maps[i].h, kNtFileMapWrite | kNtFileMapExecute, 0, 0,
upsize, addr);
ReadAll(reader, addr, size);
} else {
STRACE("fork() child %p %'zu mapping shared hand:%ld offset:%'lu", addr,
size, maps[i].h, maps[i].offset);
MapViewOfFileExNuma(maps[i].h,
(maps[i].prot & PROT_WRITE)
? kNtFileMapWrite | kNtFileMapExecute
: kNtFileMapRead | kNtFileMapExecute,
maps[i].offset >> 32, maps[i].offset, size, addr,
kNtNumaNoPreferredNode);
MapViewOfFileEx(maps[i].h,
(maps[i].prot & PROT_WRITE)
? kNtFileMapWrite | kNtFileMapExecute
: kNtFileMapRead | kNtFileMapExecute,
maps[i].offset >> 32, maps[i].offset, size, addr);
}
}

View file

@ -50,6 +50,13 @@
#include "libc/str/tpenc.h"
#include "libc/str/utf16.h"
#if IsTiny()
extern typeof(CreateFileMapping) *const __imp_CreateFileMappingW __msabi;
extern typeof(MapViewOfFileEx) *const __imp_MapViewOfFileEx __msabi;
#define CreateFileMapping __imp_CreateFileMappingW
#define MapViewOfFileEx __imp_MapViewOfFileEx
#endif
#define AT_EXECFN 31L
#define MAP_ANONYMOUS 32
#define MAP_PRIVATE 2
@ -121,7 +128,6 @@ static noasan textwindows wontreturn noinstrument void WinMainNew(
STRACE("SetConsoleCP(kNtCpUtf8) → %hhhd", rc);
rc = SetConsoleOutputCP(kNtCpUtf8);
STRACE("SetConsoleOutputCP(kNtCpUtf8) → %hhhd", rc);
SetEnvironmentVariable(u"TERM", u"xterm-truecolor");
for (i = 0; i < 2; ++i) {
hand = GetStdHandle(kConsoleHandles[i]);
rc = GetConsoleMode(hand, __ntconsolemode + i);
@ -139,12 +145,11 @@ static noasan textwindows wontreturn noinstrument void WinMainNew(
allocaddr = stackaddr - argsize;
STRACE("WinMainNew() mapping %'zu byte arg block + stack at %p", allocsize,
allocaddr);
MapViewOfFileExNuma(
(_mmi.p[0].h = CreateFileMappingNuma(
-1, &kNtIsInheritable, kNtPageExecuteReadwrite, allocsize >> 32,
allocsize, NULL, kNtNumaNoPreferredNode)),
kNtFileMapWrite | kNtFileMapExecute, 0, 0, allocsize, (void *)allocaddr,
kNtNumaNoPreferredNode);
MapViewOfFileEx(
(_mmi.p[0].h =
CreateFileMapping(-1, &kNtIsInheritable, kNtPageExecuteReadwrite,
allocsize >> 32, allocsize, NULL)),
kNtFileMapWrite | kNtFileMapExecute, 0, 0, allocsize, (void *)allocaddr);
_mmi.p[0].x = allocaddr >> 16;
_mmi.p[0].y = (allocaddr >> 16) + ((allocsize >> 16) - 1);
_mmi.p[0].prot = PROT_READ | PROT_WRITE | PROT_EXEC;