2020-11-13 09:27:49 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-11-13 09:27:49 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-11-13 09:27:49 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/assert.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/bits/weaken.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/calls/ntspawn.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/calls/sysdebug.internal.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/nexgen32e/nt2sysv.h"
|
2021-01-30 07:19:29 +00:00
|
|
|
#include "libc/nt/dll.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/enum/filemapflags.h"
|
2021-09-04 20:20:47 +00:00
|
|
|
#include "libc/nt/enum/memflags.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/enum/pageflags.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/nt/enum/startf.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/nt/enum/wt.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/ipc.h"
|
|
|
|
#include "libc/nt/memory.h"
|
|
|
|
#include "libc/nt/process.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
#include "libc/nt/signals.h"
|
|
|
|
#include "libc/nt/synchronization.h"
|
|
|
|
#include "libc/nt/thread.h"
|
2021-02-24 04:23:19 +00:00
|
|
|
#include "libc/runtime/directmap.internal.h"
|
2021-09-04 20:20:47 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/str/str.h"
|
2021-09-28 05:58:51 +00:00
|
|
|
#include "libc/sysv/consts/auxv.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/sysv/consts/prot.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2021-02-03 14:22:51 +00:00
|
|
|
static textwindows noasan char16_t *ParseInt(char16_t *p, int64_t *x) {
|
2021-02-03 06:17:53 +00:00
|
|
|
*x = 0;
|
|
|
|
while (*p == ' ') p++;
|
|
|
|
while ('0' <= *p && *p <= '9') {
|
|
|
|
*x *= 10;
|
|
|
|
*x += *p++ - '0';
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2021-02-03 06:17:53 +00:00
|
|
|
return p;
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:12:18 +00:00
|
|
|
static dontinline textwindows noasan bool ForkIo(int64_t h, void *buf, size_t n,
|
2021-02-03 14:22:51 +00:00
|
|
|
bool32 (*f)()) {
|
2020-11-13 09:27:49 +00:00
|
|
|
char *p;
|
|
|
|
size_t i;
|
2020-11-18 16:26:03 +00:00
|
|
|
uint32_t x;
|
|
|
|
for (p = buf, i = 0; i < n; i += x) {
|
2021-09-04 20:20:47 +00:00
|
|
|
if (!f(h, p + i, n - i, &x, NULL)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2021-09-04 20:20:47 +00:00
|
|
|
return true;
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:12:18 +00:00
|
|
|
static dontinline textwindows noasan void WriteAll(int64_t h, void *buf,
|
2021-02-03 14:22:51 +00:00
|
|
|
size_t n) {
|
2021-09-04 20:20:47 +00:00
|
|
|
if (!ForkIo(h, buf, n, WriteFile)) {
|
2021-09-28 05:58:51 +00:00
|
|
|
SYSDEBUG("fork() WriteFile(%zu) failed %d\n", n, GetLastError());
|
2021-09-04 20:20:47 +00:00
|
|
|
}
|
2020-11-18 16:26:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 23:12:18 +00:00
|
|
|
static textwindows dontinline noasan void ReadAll(int64_t h, void *buf,
|
2021-02-03 14:22:51 +00:00
|
|
|
size_t n) {
|
2021-09-04 20:20:47 +00:00
|
|
|
if (!ForkIo(h, buf, n, ReadFile)) {
|
2021-09-28 05:58:51 +00:00
|
|
|
SYSDEBUG("fork() ReadFile(%zu) failed %d\n", n, GetLastError());
|
2021-09-04 20:20:47 +00:00
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 05:58:51 +00:00
|
|
|
textwindows noasan noinstrument void WinMainForked(void) {
|
2020-11-13 09:27:49 +00:00
|
|
|
void *addr;
|
|
|
|
jmp_buf jb;
|
2021-09-04 20:20:47 +00:00
|
|
|
long mapcount;
|
2020-11-13 09:27:49 +00:00
|
|
|
uint64_t size;
|
2021-01-25 21:08:05 +00:00
|
|
|
uint32_t i, varlen;
|
|
|
|
struct DirectMap dm;
|
2021-02-03 06:17:53 +00:00
|
|
|
int64_t reader, writer;
|
2021-09-04 20:20:47 +00:00
|
|
|
struct MemoryInterval *maps;
|
2020-11-13 09:27:49 +00:00
|
|
|
char16_t var[21 + 1 + 21 + 1];
|
|
|
|
varlen = GetEnvironmentVariable(u"_FORK", var, ARRAYLEN(var));
|
2021-02-03 06:17:53 +00:00
|
|
|
if (!varlen) return;
|
|
|
|
if (varlen >= ARRAYLEN(var)) ExitProcess(123);
|
2021-01-25 21:08:05 +00:00
|
|
|
SetEnvironmentVariable(u"_FORK", NULL);
|
2021-02-03 06:17:53 +00:00
|
|
|
ParseInt(ParseInt(var, &reader), &writer);
|
|
|
|
ReadAll(reader, jb, sizeof(jb));
|
2021-09-04 20:20:47 +00:00
|
|
|
ReadAll(reader, &mapcount, sizeof(_mmi.i));
|
|
|
|
maps = GlobalAlloc(0, mapcount * sizeof(*_mmi.p));
|
|
|
|
ReadAll(reader, maps, mapcount * sizeof(*_mmi.p));
|
|
|
|
for (i = 0; i < mapcount; ++i) {
|
|
|
|
addr = (void *)((uint64_t)maps[i].x << 16);
|
|
|
|
size = ((uint64_t)(maps[i].y - maps[i].x) << 16) + FRAMESIZE;
|
|
|
|
if (maps[i].flags & MAP_PRIVATE) {
|
|
|
|
CloseHandle(maps[i].h);
|
|
|
|
maps[i].h =
|
|
|
|
sys_mmap_nt(addr, size, maps[i].prot, maps[i].flags, -1, 0).maphandle;
|
2021-02-03 06:17:53 +00:00
|
|
|
ReadAll(reader, addr, size);
|
2020-11-13 09:27:49 +00:00
|
|
|
} else {
|
2021-01-25 21:08:05 +00:00
|
|
|
MapViewOfFileExNuma(
|
2021-09-04 20:20:47 +00:00
|
|
|
maps[i].h,
|
|
|
|
(maps[i].prot & PROT_WRITE)
|
2021-01-25 21:08:05 +00:00
|
|
|
? kNtFileMapWrite | kNtFileMapExecute | kNtFileMapRead
|
|
|
|
: kNtFileMapExecute | kNtFileMapRead,
|
|
|
|
0, 0, size, addr, kNtNumaNoPreferredNode);
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 20:20:47 +00:00
|
|
|
ReadAll(reader, _etext, _end - _etext);
|
|
|
|
for (i = 0; i < mapcount; ++i) {
|
|
|
|
_mmi.p[i].h = maps[i].h;
|
|
|
|
}
|
2021-02-03 06:17:53 +00:00
|
|
|
CloseHandle(reader);
|
|
|
|
CloseHandle(writer);
|
2021-09-04 20:20:47 +00:00
|
|
|
GlobalFree(maps);
|
2021-02-04 03:35:29 +00:00
|
|
|
if (weaken(__wincrash_nt)) {
|
|
|
|
AddVectoredExceptionHandler(1, (void *)weaken(__wincrash_nt));
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
longjmp(jb, 1);
|
|
|
|
}
|
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
textwindows int sys_fork_nt(void) {
|
2020-11-13 09:27:49 +00:00
|
|
|
jmp_buf jb;
|
|
|
|
int64_t reader, writer;
|
2021-02-03 14:22:51 +00:00
|
|
|
int i, rc, pid, releaseme;
|
2021-02-03 06:17:53 +00:00
|
|
|
char *p, forkvar[6 + 21 + 1 + 21 + 1];
|
2020-12-01 11:43:40 +00:00
|
|
|
struct NtStartupInfo startinfo;
|
|
|
|
struct NtProcessInformation procinfo;
|
2021-02-03 14:22:51 +00:00
|
|
|
if ((pid = releaseme = __reservefd()) == -1) return -1;
|
2020-11-13 09:27:49 +00:00
|
|
|
if (!setjmp(jb)) {
|
|
|
|
if (CreatePipe(&reader, &writer, &kNtIsInheritable, 0)) {
|
2021-02-03 06:17:53 +00:00
|
|
|
p = stpcpy(forkvar, "_FORK=");
|
2021-09-28 05:58:51 +00:00
|
|
|
p += uint64toarray_radix10(reader, p), *p++ = ' ';
|
2020-11-13 09:27:49 +00:00
|
|
|
p += uint64toarray_radix10(writer, p);
|
2021-09-28 05:58:51 +00:00
|
|
|
bzero(&startinfo, sizeof(startinfo));
|
2020-12-01 11:43:40 +00:00
|
|
|
startinfo.cb = sizeof(struct NtStartupInfo);
|
|
|
|
startinfo.dwFlags = kNtStartfUsestdhandles;
|
|
|
|
startinfo.hStdInput = g_fds.p[0].handle;
|
|
|
|
startinfo.hStdOutput = g_fds.p[1].handle;
|
|
|
|
startinfo.hStdError = g_fds.p[2].handle;
|
2021-10-02 15:17:04 +00:00
|
|
|
if (ntspawn(program_executable_name, __argv, environ, forkvar,
|
2021-09-28 05:58:51 +00:00
|
|
|
&kNtIsInheritable, NULL, true, 0, NULL, &startinfo,
|
|
|
|
&procinfo) != -1) {
|
2020-11-13 09:27:49 +00:00
|
|
|
CloseHandle(reader);
|
2020-12-01 11:43:40 +00:00
|
|
|
CloseHandle(procinfo.hThread);
|
2021-01-28 23:49:15 +00:00
|
|
|
if (weaken(__sighandrvas) &&
|
|
|
|
weaken(__sighandrvas)[SIGCHLD] == SIG_IGN) {
|
2021-01-25 21:08:05 +00:00
|
|
|
CloseHandle(procinfo.hProcess);
|
|
|
|
} else {
|
|
|
|
g_fds.p[pid].kind = kFdProcess;
|
|
|
|
g_fds.p[pid].handle = procinfo.hProcess;
|
|
|
|
g_fds.p[pid].flags = O_CLOEXEC;
|
2021-02-03 14:22:51 +00:00
|
|
|
releaseme = -1;
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
WriteAll(writer, jb, sizeof(jb));
|
|
|
|
WriteAll(writer, &_mmi.i, sizeof(_mmi.i));
|
2021-09-04 20:20:47 +00:00
|
|
|
WriteAll(writer, _mmi.p, _mmi.i * sizeof(*_mmi.p));
|
2020-11-13 09:27:49 +00:00
|
|
|
for (i = 0; i < _mmi.i; ++i) {
|
|
|
|
if (_mmi.p[i].flags & MAP_PRIVATE) {
|
|
|
|
WriteAll(writer, (void *)((uint64_t)_mmi.p[i].x << 16),
|
|
|
|
((uint64_t)(_mmi.p[i].y - _mmi.p[i].x) << 16) + FRAMESIZE);
|
|
|
|
}
|
|
|
|
}
|
2021-09-04 20:20:47 +00:00
|
|
|
WriteAll(writer, _etext, _end - _etext);
|
2020-11-13 09:27:49 +00:00
|
|
|
CloseHandle(writer);
|
|
|
|
} else {
|
|
|
|
rc = -1;
|
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
rc = pid;
|
2020-11-13 09:27:49 +00:00
|
|
|
} else {
|
2020-11-28 20:01:51 +00:00
|
|
|
rc = __winerr();
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
2021-02-03 14:22:51 +00:00
|
|
|
if (releaseme != -1) {
|
|
|
|
__releasefd(releaseme);
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
return rc;
|
|
|
|
}
|