Skip to content

Commit fe01642

Browse files
committed
Add missing lock to fork() on Windows
1 parent e939659 commit fe01642

19 files changed

+90
-96
lines changed

libc/intrin/msync-nt.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
#include "libc/calls/syscall-nt.internal.h"
2020
#include "libc/intrin/maps.h"
2121
#include "libc/nt/memory.h"
22-
#include "libc/nt/runtime.h"
2322
#include "libc/runtime/runtime.h"
2423
#include "libc/stdio/sysparam.h"
25-
#include "libc/sysv/consts/auxv.h"
2624
#include "libc/sysv/consts/map.h"
2725
#include "libc/sysv/errfuns.h"
2826

@@ -36,28 +34,17 @@ textwindows int sys_msync_nt(char *addr, size_t size, int flags) {
3634

3735
int rc = 0;
3836
__maps_lock();
39-
struct Map *next, *map;
37+
struct Map *map;
4038
if (!(map = __maps_floor(addr)))
4139
map = __maps_first();
42-
for (; map && map->addr <= addr + size; map = next) {
43-
next = __maps_next(map);
44-
if (!__maps_isalloc(map))
45-
continue;
40+
for (; map && map->addr <= addr + size; map = __maps_next(map)) {
4641
if (map->flags & MAP_ANONYMOUS)
47-
continue;
48-
if (MAX(addr, map->addr) >= MIN(addr + size, map->addr + map->size))
42+
continue; // msync() is about coherency between file and memory
43+
char *beg = MAX(addr, map->addr);
44+
char *end = MIN(addr + size, map->addr + map->size);
45+
if (beg >= end)
4946
continue; // didn't overlap mapping
50-
51-
// get true size of win32 allocation
52-
size_t allocsize = map->size;
53-
while (next && !__maps_isalloc(next) &&
54-
next->addr + allocsize == next->addr) {
55-
allocsize += next->size;
56-
next = __maps_next(next);
57-
}
58-
59-
// perform the flush
60-
if (!FlushViewOfFile(map->addr, allocsize))
47+
if (!FlushViewOfFile(beg, end - beg))
6148
rc = -1;
6249
// TODO(jart): FlushFileBuffers too on g_fds handle if MS_SYNC?
6350
}

libc/intrin/msync.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,19 @@ int msync(void *addr, size_t size, int flags) {
6868
} else {
6969
sysflags = MS_ASYNC;
7070
}
71-
if (flags & MS_INVALIDATE) {
71+
if (flags & MS_INVALIDATE)
7272
sysflags |= MS_INVALIDATE;
73-
}
7473

7574
// FreeBSD's manual says "The flags argument was both MS_ASYNC and
7675
// MS_INVALIDATE. Only one of these flags is allowed." which makes
7776
// following the POSIX recommendation somewhat difficult.
78-
if (IsFreebsd()) {
79-
if (sysflags == (MS_ASYNC | MS_INVALIDATE)) {
77+
if (IsFreebsd())
78+
if (sysflags == (MS_ASYNC | MS_INVALIDATE))
8079
sysflags = MS_INVALIDATE;
81-
}
82-
}
8380

8481
// FreeBSD specifies MS_SYNC as 0 so we shift the Cosmo constants
85-
if (IsFreebsd()) {
82+
if (IsFreebsd())
8683
sysflags >>= 1;
87-
}
8884

8985
BEGIN_CANCELATION_POINT;
9086
if (!IsWindows()) {

libc/log/gdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "libc/calls/calls.h"
44
#include "libc/calls/struct/rusage.h"
55
#include "libc/dce.h"
6-
#include "libc/proc/proc.internal.h"
6+
#include "libc/proc/proc.h"
77
#include "libc/sysv/consts/nr.h"
88
#include "libc/sysv/consts/w.h"
99
COSMOPOLITAN_C_START_

libc/proc/fork-nt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "libc/nt/thread.h"
4545
#include "libc/nt/thunk/msabi.h"
4646
#include "libc/nt/winsock.h"
47-
#include "libc/proc/proc.internal.h"
47+
#include "libc/proc/proc.h"
4848
#include "libc/runtime/internal.h"
4949
#include "libc/runtime/symbols.internal.h"
5050
#include "libc/sysv/consts/map.h"

libc/proc/fork.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "libc/nt/runtime.h"
3838
#include "libc/nt/thread.h"
3939
#include "libc/nt/thunk/msabi.h"
40-
#include "libc/proc/proc.internal.h"
40+
#include "libc/proc/proc.h"
4141
#include "libc/runtime/internal.h"
4242
#include "libc/runtime/runtime.h"
4343
#include "libc/runtime/syslib.internal.h"
@@ -119,6 +119,9 @@ static void fork_prepare(void) {
119119
_weaken(__localtime_lock)();
120120
if (_weaken(__dlopen_lock))
121121
_weaken(__dlopen_lock)();
122+
if (IsWindows())
123+
if (_weaken(__proc_lock))
124+
_weaken(__proc_lock)();
122125
if (_weaken(cosmo_stack_lock))
123126
_weaken(cosmo_stack_lock)();
124127
__cxa_lock();
@@ -151,6 +154,9 @@ static void fork_parent(void) {
151154
__cxa_unlock();
152155
if (_weaken(cosmo_stack_unlock))
153156
_weaken(cosmo_stack_unlock)();
157+
if (IsWindows())
158+
if (_weaken(__proc_unlock))
159+
_weaken(__proc_unlock)();
154160
if (_weaken(__dlopen_unlock))
155161
_weaken(__dlopen_unlock)();
156162
if (_weaken(__localtime_unlock))

libc/proc/getpriority-nt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "libc/nt/errors.h"
2323
#include "libc/nt/process.h"
2424
#include "libc/nt/runtime.h"
25-
#include "libc/proc/proc.internal.h"
25+
#include "libc/proc/proc.h"
2626
#include "libc/sysv/consts/prio.h"
2727
#include "libc/sysv/errfuns.h"
2828

libc/proc/getrusage-nt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "libc/nt/struct/iocounters.h"
3030
#include "libc/nt/struct/processmemorycounters.h"
3131
#include "libc/nt/thread.h"
32-
#include "libc/proc/proc.internal.h"
32+
#include "libc/proc/proc.h"
3333
#include "libc/str/str.h"
3434
#include "libc/sysv/consts/rusage.h"
3535
#include "libc/sysv/errfuns.h"

libc/proc/handle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "libc/calls/calls.h"
2020
#include "libc/intrin/weaken.h"
2121
#include "libc/nt/runtime.h"
22-
#include "libc/proc/proc.internal.h"
22+
#include "libc/proc/proc.h"
2323

2424
// retrieves handle of process
2525
// supports only current process and processes we created

libc/proc/kill-nt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "libc/nt/memory.h"
3434
#include "libc/nt/process.h"
3535
#include "libc/nt/runtime.h"
36-
#include "libc/proc/proc.internal.h"
36+
#include "libc/proc/proc.h"
3737
#include "libc/sysv/consts/sig.h"
3838
#include "libc/sysv/errfuns.h"
3939
#ifdef __x86_64__

libc/proc/posix_spawn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include "libc/proc/ntspawn.h"
5959
#include "libc/proc/posix_spawn.h"
6060
#include "libc/proc/posix_spawn.internal.h"
61-
#include "libc/proc/proc.internal.h"
61+
#include "libc/proc/proc.h"
6262
#include "libc/runtime/runtime.h"
6363
#include "libc/sock/sock.h"
6464
#include "libc/stdio/stdio.h"

0 commit comments

Comments
 (0)