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
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/bits/weaken.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
|
#include "libc/calls/internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/calls/ntspawn.h"
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
#include "libc/calls/strace.internal.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
|
#include "libc/fmt/itoa.h"
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#include "libc/intrin/kprintf.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
|
#include "libc/macros.internal.h"
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#include "libc/mem/alloca.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/nexgen32e/nt2sysv.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
|
#include "libc/nt/enum/filemapflags.h"
|
|
|
|
|
#include "libc/nt/enum/pageflags.h"
|
2020-12-01 11:43:40 +00:00
|
|
|
|
#include "libc/nt/enum/startf.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"
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#include "libc/nt/struct/ntexceptionpointers.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"
|
|
|
|
|
#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"
|
|
|
|
|
|
2022-03-23 04:04:08 +00:00
|
|
|
|
STATIC_YOINK("_check_sigchld");
|
|
|
|
|
|
2022-03-20 15:01:14 +00:00
|
|
|
|
extern int __pid;
|
|
|
|
|
extern unsigned long long __kbirth;
|
|
|
|
|
extern unsigned char __data_start[]; /* αpε */
|
|
|
|
|
extern unsigned char __data_end[]; /* αpε */
|
|
|
|
|
extern unsigned char __bss_start[]; /* αpε */
|
|
|
|
|
extern unsigned char __bss_end[]; /* αpε */
|
|
|
|
|
|
|
|
|
|
static textwindows 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
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 15:01:14 +00:00
|
|
|
|
static inline textwindows ssize_t ForkIo(int64_t h, char *p, size_t n,
|
|
|
|
|
bool32 (*f)()) {
|
2020-11-13 09:27:49 +00:00
|
|
|
|
size_t i;
|
2020-11-18 16:26:03 +00:00
|
|
|
|
uint32_t x;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
for (i = 0; i < n; i += x) {
|
2021-09-04 20:20:47 +00:00
|
|
|
|
if (!f(h, p + i, n - i, &x, NULL)) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
return -1;
|
2021-09-04 20:20:47 +00:00
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
|
|
|
|
bool32 (*fn)(), const char *sf) {
|
|
|
|
|
ssize_t rc = ForkIo(h, buf, n, fn);
|
|
|
|
|
STRACE("%s(%ld, %'zu) → %'zd% m", sf, h, n, rc);
|
|
|
|
|
return rc != -1;
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 15:01:14 +00:00
|
|
|
|
static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
|
|
|
|
|
return ForkIo2(h, buf, n, WriteFile, "WriteFile");
|
2020-11-18 16:26:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 15:01:14 +00:00
|
|
|
|
static textwindows dontinline bool ReadAll(int64_t h, void *buf, size_t n) {
|
|
|
|
|
return ForkIo2(h, buf, n, ReadFile, "ReadFile");
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 15:01:14 +00:00
|
|
|
|
static textwindows int OnForkCrash(struct NtExceptionPointers *ep) {
|
|
|
|
|
kprintf("error: fork() child crashed!%n"
|
|
|
|
|
"\tExceptionCode = %#x%n"
|
|
|
|
|
"\tRip = %x%n"
|
|
|
|
|
"\tRax = %.16x R8 = %.16x%n"
|
|
|
|
|
"\tRbx = %.16x R9 = %.16x%n"
|
|
|
|
|
"\tRcx = %.16x R10 = %.16x%n"
|
|
|
|
|
"\tRdx = %.16x R11 = %.16x%n"
|
|
|
|
|
"\tRdi = %.16x R12 = %.16x%n"
|
|
|
|
|
"\tRsi = %.16x R13 = %.16x%n"
|
|
|
|
|
"\tRbp = %.16x R14 = %.16x%n"
|
|
|
|
|
"\tRsp = %.16x R15 = %.16x%n",
|
|
|
|
|
ep->ExceptionRecord->ExceptionCode,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rip : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rax : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R8 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rbx : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R9 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rcx : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R10 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rdx : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R11 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rdi : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R12 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rsi : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R13 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rbp : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R14 : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->Rsp : -1,
|
|
|
|
|
ep->ContextRecord ? ep->ContextRecord->R15 : -1);
|
|
|
|
|
ExitProcess(73);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textwindows void WinMainForked(void) {
|
2020-11-13 09:27:49 +00:00
|
|
|
|
jmp_buf jb;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
char *addr, *shad;
|
2021-01-25 21:08:05 +00:00
|
|
|
|
struct DirectMap dm;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
uint64_t size, upsize;
|
2021-02-03 06:17:53 +00:00
|
|
|
|
int64_t reader, writer;
|
2021-09-04 20:20:47 +00:00
|
|
|
|
struct MemoryInterval *maps;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
char16_t fvar[21 + 1 + 21 + 1];
|
|
|
|
|
int64_t oncrash, savetsc, savebir;
|
|
|
|
|
uint32_t i, varlen, oldprot, savepid;
|
|
|
|
|
long mapcount, mapcapacity, specialz;
|
|
|
|
|
extern uint64_t ts asm("kStartTsc");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* check to see if the process was actually forked
|
|
|
|
|
* this variable should have the pipe handle numba
|
|
|
|
|
*/
|
|
|
|
|
varlen = GetEnvironmentVariable(u"_FORK", fvar, ARRAYLEN(fvar));
|
|
|
|
|
if (!varlen || varlen >= ARRAYLEN(fvar)) return;
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
STRACE("WinMainForked()");
|
2021-01-25 21:08:05 +00:00
|
|
|
|
SetEnvironmentVariable(u"_FORK", NULL);
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#ifdef SYSDEBUG
|
|
|
|
|
oncrash = AddVectoredExceptionHandler(1, NT2SYSV(OnForkCrash));
|
|
|
|
|
#endif
|
|
|
|
|
ParseInt(ParseInt(fvar, &reader), &writer);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* read the cpu state from the parent process
|
|
|
|
|
*/
|
2021-02-03 06:17:53 +00:00
|
|
|
|
ReadAll(reader, jb, sizeof(jb));
|
2022-03-20 15:01:14 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* read the list of mappings from the parent process
|
|
|
|
|
* this is stored in a special secretive memory map!
|
|
|
|
|
* read ExtendMemoryIntervals for further details :|
|
|
|
|
|
*/
|
|
|
|
|
maps = (void *)kMemtrackStart;
|
2021-09-04 20:20:47 +00:00
|
|
|
|
ReadAll(reader, &mapcount, sizeof(_mmi.i));
|
2022-03-20 15:01:14 +00:00
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
#if 0
|
|
|
|
|
ReadAll(reader, shad, (mapcount * sizeof(_mmi.p[0])) >> 3);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* read the heap mappings from the parent process
|
|
|
|
|
* we can avoid copying via pipe for shared maps!
|
|
|
|
|
*/
|
2021-09-04 20:20:47 +00:00
|
|
|
|
for (i = 0; i < mapcount; ++i) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
addr = (char *)((uint64_t)maps[i].x << 16);
|
|
|
|
|
size = maps[i].size;
|
2021-09-04 20:20:47 +00:00
|
|
|
|
if (maps[i].flags & MAP_PRIVATE) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
STRACE("fork() child %p %'zu copying private map", addr, size);
|
|
|
|
|
if (!CloseHandle(maps[i].h)) {
|
|
|
|
|
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);
|
2021-02-03 06:17:53 +00:00
|
|
|
|
ReadAll(reader, addr, size);
|
2020-11-13 09:27:49 +00:00
|
|
|
|
} else {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
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);
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* read the .data and .bss program image sections
|
|
|
|
|
*/
|
|
|
|
|
savepid = __pid;
|
|
|
|
|
savebir = __kbirth;
|
|
|
|
|
savetsc = ts;
|
|
|
|
|
STRACE("fork() child reading %'zu bytes of .data to %p",
|
|
|
|
|
__data_end - __data_start, __data_start);
|
|
|
|
|
ReadAll(reader, __data_start, __data_end - __data_start);
|
|
|
|
|
STRACE("fork() child reading %'zu bytes of .bss to %p",
|
|
|
|
|
__bss_end - __bss_start, __bss_start);
|
|
|
|
|
ReadAll(reader, __bss_start, __bss_end - __bss_start);
|
|
|
|
|
__pid = savepid;
|
|
|
|
|
__kbirth = savebir;
|
|
|
|
|
ts = savetsc;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* apply fixups and reapply memory protections
|
|
|
|
|
*/
|
|
|
|
|
STRACE("fork() child applying fixups to _mmi.p");
|
|
|
|
|
_mmi.p = maps;
|
|
|
|
|
_mmi.n = specialz / sizeof(_mmi.p[0]);
|
2021-09-04 20:20:47 +00:00
|
|
|
|
for (i = 0; i < mapcount; ++i) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
if ((maps[i].flags & MAP_PRIVATE) && (~maps[i].prot & PROT_WRITE)) {
|
|
|
|
|
VirtualProtect((void *)((uint64_t)maps[i].x << 16),
|
|
|
|
|
ROUNDUP(maps[i].size, FRAMESIZE),
|
|
|
|
|
__prot2nt(maps[i].prot, 0), &oldprot);
|
|
|
|
|
}
|
2021-09-04 20:20:47 +00:00
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* clean up and restore old processor state
|
|
|
|
|
*/
|
|
|
|
|
STRACE("fork() child almost done!");
|
2021-02-03 06:17:53 +00:00
|
|
|
|
CloseHandle(reader);
|
|
|
|
|
CloseHandle(writer);
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#ifdef SYSDEBUG
|
|
|
|
|
RemoveVectoredExceptionHandler(oncrash);
|
|
|
|
|
#endif
|
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
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
STRACE("fork() child it's time for the big jump (>'.')>");
|
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) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
bool ok;
|
2020-11-13 09:27:49 +00:00
|
|
|
|
jmp_buf jb;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
char **args, **args2;
|
2020-11-13 09:27:49 +00:00
|
|
|
|
int64_t reader, writer;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
int i, n, rc, pid, untrackpid;
|
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;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
if ((pid = untrackpid = __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;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
args = __argv;
|
|
|
|
|
#ifdef SYSDEBUG
|
|
|
|
|
/*
|
|
|
|
|
* If --strace was passed to this program, then propagate it the
|
|
|
|
|
* forked process since the flag was removed by __intercept_flag
|
|
|
|
|
*/
|
|
|
|
|
if (__strace > 0) {
|
|
|
|
|
for (n = 0; args[n];) ++n;
|
|
|
|
|
args2 = alloca((n + 2) * sizeof(char *));
|
|
|
|
|
for (i = 0; i < n; ++i) args2[i] = args[i];
|
|
|
|
|
args2[i++] = "--strace";
|
|
|
|
|
args2[i] = 0;
|
|
|
|
|
args = args2;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (ntspawn(program_executable_name, args, 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);
|
2022-03-20 15:01:14 +00:00
|
|
|
|
ok = WriteAll(writer, jb, sizeof(jb)) &&
|
|
|
|
|
WriteAll(writer, &_mmi.i, sizeof(_mmi.i)) &&
|
|
|
|
|
WriteAll(writer, &_mmi.n, sizeof(_mmi.n)) &&
|
|
|
|
|
WriteAll(writer, _mmi.p, _mmi.i * sizeof(_mmi.p[0]));
|
|
|
|
|
#if 0
|
|
|
|
|
if (IsAsan() && ok) {
|
|
|
|
|
ok = WriteAll(writer, (char *)(((intptr_t)_mmi.p >> 3) + 0x7fff8000),
|
|
|
|
|
(_mmi.i * sizeof(_mmi.p[0])) >> 3);
|
2021-01-25 21:08:05 +00:00
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
#endif
|
|
|
|
|
for (i = 0; i < _mmi.i && ok; ++i) {
|
2020-11-13 09:27:49 +00:00
|
|
|
|
if (_mmi.p[i].flags & MAP_PRIVATE) {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
ok = WriteAll(writer, (void *)((uint64_t)_mmi.p[i].x << 16),
|
|
|
|
|
_mmi.p[i].size);
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
if (ok) {
|
|
|
|
|
STRACE("fork() parent writing %'zu bytes of .data",
|
|
|
|
|
__data_end - __data_start);
|
|
|
|
|
ok = WriteAll(writer, __data_start, __data_end - __data_start);
|
|
|
|
|
}
|
|
|
|
|
if (ok) {
|
|
|
|
|
STRACE("fork() parent writing %'zu bytes of .bss",
|
|
|
|
|
__bss_end - __bss_start);
|
|
|
|
|
ok = WriteAll(writer, __bss_start, __bss_end - __bss_start);
|
|
|
|
|
}
|
|
|
|
|
ok = ok & !!CloseHandle(writer);
|
|
|
|
|
if (ok) {
|
|
|
|
|
if (!weaken(__sighandrvas) ||
|
|
|
|
|
weaken(__sighandrvas)[SIGCHLD] != SIG_IGN) {
|
|
|
|
|
g_fds.p[pid].kind = kFdProcess;
|
|
|
|
|
g_fds.p[pid].handle = procinfo.hProcess;
|
|
|
|
|
g_fds.p[pid].flags = O_CLOEXEC;
|
|
|
|
|
untrackpid = -1;
|
2022-03-23 02:51:27 +00:00
|
|
|
|
rc = pid;
|
2022-03-20 15:01:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
/*
|
|
|
|
|
* XXX: Ignoring SIGCHLD should track the process information.
|
|
|
|
|
* What we need to do instead, is periodically check if a
|
|
|
|
|
* process has exited and remove it automatically via i/o
|
|
|
|
|
* functions like poll() so it doesn't get zombdied.
|
|
|
|
|
*/
|
|
|
|
|
STRACE("fork() parent closing process handle b/c SIGCHLD=SIG_IGN");
|
2022-03-23 02:51:27 +00:00
|
|
|
|
rc = GetProcessId(procinfo.hProcess);
|
2022-03-20 15:01:14 +00:00
|
|
|
|
CloseHandle(procinfo.hProcess);
|
|
|
|
|
}
|
|
|
|
|
STRACE("fork() parent everything looks good");
|
|
|
|
|
} else {
|
|
|
|
|
STRACE("fork() parent ~~failed~~ because writing failed");
|
|
|
|
|
rc = __winerr();
|
|
|
|
|
TerminateProcess(procinfo.hProcess, 127);
|
|
|
|
|
CloseHandle(procinfo.hProcess);
|
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
|
} else {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
STRACE("fork() parent ~~failed~~ because ntspawn failed");
|
|
|
|
|
CloseHandle(writer);
|
2020-11-13 09:27:49 +00:00
|
|
|
|
rc = -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
STRACE("fork() parent ~~failed~~ because CreatePipe() failed %m");
|
2020-11-28 20:01:51 +00:00
|
|
|
|
rc = __winerr();
|
2022-03-20 15:01:14 +00:00
|
|
|
|
CloseHandle(writer);
|
2020-11-13 09:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-03-20 15:01:14 +00:00
|
|
|
|
STRACE("fork() child welcome back <('.'<)");
|
2020-11-13 09:27:49 +00:00
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
2022-03-20 15:01:14 +00:00
|
|
|
|
if (untrackpid != -1) {
|
|
|
|
|
__releasefd(untrackpid);
|
2021-02-03 14:22:51 +00:00
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|