Skip to content

Commit dc1afc9

Browse files
committed
Fix fork() crash on Windows
On Windows, sometimes fork() could crash with message likes: fork() ViewOrDie(170000) failed with win32 error 487 This is due to a bug in our file descriptor inheritance. We have cursors which are shared between processes. They let us track the file positions of read() and write() operations. At startup they were being mmap()ed to memory addresses that were assigned by WIN32. That's bad because Windows likes to give us memory addresses beneath the program image in the first 4mb range that are likely to conflict with other assignments. That ended up causing problems because fork() needs to be able to assume that a map will be possible to resurrect at the same address. But for one reason or another, Windows libraries we don't control could sneak allocations into the memory space that overlap with these mappings. This change solves it by choosing a random memory address instead when mapping cursor objects.
1 parent 5edc081 commit dc1afc9

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

libc/intrin/fds.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
129129
if (IsWindows()) {
130130
const char *fdspec;
131131
if ((fdspec = getenv("_COSMO_FDS_V2"))) {
132+
char *smaddr = 0;
132133
unsetenv("_COSMO_FDS");
133134
unsetenv("_COSMO_FDS_V2");
134135
for (;;) {
@@ -171,8 +172,13 @@ textstartup void __init_fds(int argc, char **argv, char **envp) {
171172
if (shand) {
172173
struct Map *map;
173174
struct CursorShared *shared;
175+
if (!smaddr) {
176+
smaddr = __maps_randaddr();
177+
} else {
178+
smaddr += 65536;
179+
}
174180
if ((shared = MapViewOfFileEx(shand, kNtFileMapWrite, 0, 0,
175-
sizeof(struct CursorShared), 0))) {
181+
sizeof(struct CursorShared), smaddr))) {
176182
if ((f->cursor = _mapanon(sizeof(struct Cursor)))) {
177183
f->cursor->shared = shared;
178184
if ((map = __maps_alloc())) {

libc/intrin/printmapswin32.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/fmt/conv.h"
20+
#include "libc/intrin/describeflags.h"
21+
#include "libc/intrin/kprintf.h"
22+
#include "libc/macros.h"
23+
#include "libc/nt/enum/memflags.h"
24+
#include "libc/nt/memory.h"
25+
#include "libc/runtime/runtime.h"
26+
#include "libc/str/str.h"
27+
28+
static const struct DescribeFlags kNtMemState[] = {
29+
{kNtMemCommit, "Commit"}, //
30+
{kNtMemFree, "Free"}, //
31+
{kNtMemReserve, "Reserve"}, //
32+
};
33+
34+
const char *DescribeNtMemState(char buf[64], uint32_t x) {
35+
return _DescribeFlags(buf, 64, kNtMemState, ARRAYLEN(kNtMemState), "kNtMem",
36+
x);
37+
}
38+
39+
static const struct DescribeFlags kNtMemType[] = {
40+
{kNtMemImage, "Image"}, //
41+
{kNtMemMapped, "Mapped"}, //
42+
{kNtMemPrivate, "Private"}, //
43+
};
44+
45+
const char *DescribeNtMemType(char buf[64], uint32_t x) {
46+
return _DescribeFlags(buf, 64, kNtMemType, ARRAYLEN(kNtMemType), "kNtMem", x);
47+
}
48+
49+
void __print_maps_win32(void) {
50+
char *p, b[5][64];
51+
struct NtMemoryBasicInformation mi;
52+
kprintf("%-12s %-12s %10s %16s %16s %32s %32s\n", "Allocation", "BaseAddress",
53+
"RegionSize", "State", "Type", "AllocationProtect", "Protect");
54+
for (p = 0;; p = (char *)mi.BaseAddress + mi.RegionSize) {
55+
bzero(&mi, sizeof(mi));
56+
if (!VirtualQuery(p, &mi, sizeof(mi)))
57+
break;
58+
sizefmt(b[0], mi.RegionSize, 1024);
59+
kprintf("%.12lx %.12lx %10s %16s %16s %32s %32s\n", mi.AllocationBase,
60+
mi.BaseAddress, b[0], DescribeNtMemState(b[1], mi.State),
61+
DescribeNtMemType(b[2], mi.Type),
62+
_DescribeNtPageFlags(b[3], mi.AllocationProtect),
63+
_DescribeNtPageFlags(b[4], mi.Protect));
64+
}
65+
}

libc/proc/fork-nt.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ static dontinline textwindows ssize_t ForkIo2(
125125
static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
126126
bool ok;
127127
ok = ForkIo2(h, buf, n, (void *)WriteFile, "WriteFile", false) != -1;
128-
if (!ok) {
128+
if (!ok)
129129
STRACE("fork() failed in parent due to WriteAll(%ld, %p, %'zu) → %u", h,
130130
buf, n, GetLastError());
131-
__print_maps(0);
132-
}
133131
return ok;
134132
}
135133

libc/runtime/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ int ftrace_install(void) libcesque;
9595
int ftrace_enabled(int) libcesque;
9696
int strace_enabled(int) libcesque;
9797
void __print_maps(size_t) libcesque;
98+
void __print_maps_win32(void) libcesque;
9899
void __printargs(const char *) libcesque;
99100
/* builtin sh-like system/popen dsl */
100101
int _cocmd(int, char **, char **) libcesque;

0 commit comments

Comments
 (0)