Skip to content

Commit a6766c0

Browse files
Create POSIX process pipes close-on-exec
Process launch on POSIX systems created its four pipes with pipe and set FD_CLOEXEC on them afterwards. A process another thread forked in between kept the unflagged descriptors across exec, so for example a first child's stdout write end could stay open in an unrelated child, and the first child's stdout gave no EOF until that child exited. The pipes are now created with pipe2(O_CLOEXEC) on Linux and the BSDs. Where pipe2 is missing (macOS) or the kernel lacks it, a pipe is created and flagged under a lock held shared, and the runtime forks under it held exclusively, so no process the runtime starts lands in that window. Cleanup after a failed pipe, the move of the child's streams above descriptor 2 and the exec status pipe are unchanged. Tests: ProcessConcurrentPipeInheritance, on Linux, replaces pipe and pipe2 to pause one launch on a thread right after its stdout pipe exists, starts a second process meanwhile, and has that child look for the first launch's pipe among its descriptors, with a pipe2 O_CLOEXEC pipe as the control. Other systems print the expected result. It has not been run on Linux or macOS yet; CI runs it. Fixes #340
1 parent ede2986 commit a6766c0

6 files changed

Lines changed: 188 additions & 17 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ libhxcpp 6.0.0 (unreleased)
99
* Sped up Std.string on deeply nested enums, which copied each parameter's text into its parent's, quadratic in the depth, and recursed once per level, so a chain a million deep overflowed the native stack. Enums are now formatted in one pass: a 2,048-deep enum formats 33x faster (libhxcpp #328)
1010
* Fixed formatting a map that holds itself, directly or through other maps, ending the program with a stack overflow. Past 5 nested maps a map now formats as "...", as arrays and anonymous objects already did (libhxcpp #343)
1111
* Map entries with Int values in StringMap, ObjectMap and Int64 maps take 8 bytes less on 64-bit: the hash sat after the key, leaving padding beside the value. A 200,000-entry StringMap<Int> holds 9.6 MB instead of 11.2 MB (libhxcpp #370)
12+
* Fixed process pipes reaching an unrelated child on Linux, macOS and other POSIX systems: they were created with pipe and marked close-on-exec afterwards, so a process another thread started in between kept them, and the first child's output could stay open until that process exited. They are now created close-on-exec with pipe2 where the system has it, and elsewhere no process the runtime starts can fork in between (libhxcpp #340)
1213
* Fixed a copy of an hx::Ref not keeping its object alive: copying one of the same type shared the original's GC root, so once the original went the collector could free the object the copy still held (libhxcpp #355)
1314
* Fixed hx::Ref::setPtr leaving its GC root at the old offset when it moved to another interface of the same object, so the collector could free the object while the reference still held it (libhxcpp #361)
1415
* Fixed an exception thrown by a finalizer leaving the collector paused: it unwound out of the collection with every other thread still stopped. The collection now finishes, running the other finalizers, and the first exception is thrown to the code that started it (libhxcpp #358)

‎src/hx/libs/std/Process.cpp‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,39 @@ static void startFailed(int inErr)
424424
{
425425
hx::Throw(HX_CSTRING("Could not start process: ") + String(strerror(inErr)));
426426
}
427+
428+
// A child forked by another thread between pipe() and FD_CLOEXEC kept the
429+
// pipe ends across exec, so one process's stdout could stay open in an
430+
// unrelated child and never deliver EOF. pipe2 creates the ends with
431+
// close-on-exec already set. Where it is missing (macOS) or the kernel lacks
432+
// it, the ends are created and flagged under sForkLock held shared, and this
433+
// runtime forks under it held exclusively, so none of its own forks lands in
434+
// between
435+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
436+
#define HX_PROCESS_HAS_PIPE2
437+
#endif
438+
static pthread_rwlock_t sForkLock = PTHREAD_RWLOCK_INITIALIZER;
439+
440+
static int cloexecPipe(int outFds[2])
441+
{
442+
#ifdef HX_PROCESS_HAS_PIPE2
443+
if (pipe2(outFds, O_CLOEXEC) == 0)
444+
return 0;
445+
if (errno != ENOSYS)
446+
return -1;
447+
#endif
448+
pthread_rwlock_rdlock(&sForkLock);
449+
int result = pipe(outFds);
450+
if (result == 0)
451+
{
452+
fcntl(outFds[0], F_SETFD, FD_CLOEXEC);
453+
fcntl(outFds[1], F_SETFD, FD_CLOEXEC);
454+
}
455+
int err = errno;
456+
pthread_rwlock_unlock(&sForkLock);
457+
errno = err;
458+
return result;
459+
}
427460
#endif
428461

429462
Dynamic _hx_std_process_run( String cmd, Array<String> vargs, int inShowParam )
@@ -573,24 +606,26 @@ Dynamic _hx_std_process_run( String cmd, Array<String> vargs, int inShowParam )
573606
}
574607
#else // not windows ...
575608
{
576-
// The child writes exec's errno to status; a successful exec closes it
609+
// The child writes exec's errno to status; a successful exec closes it.
610+
// All four are close-on-exec; dup2 onto 0/1/2 in the child clears the
611+
// flag on the std fds
577612
int input[2], output[2], error[2], status[2];
578-
if( pipe(input) )
613+
if( cloexecPipe(input) )
579614
startFailed(errno);
580-
if( pipe(output) )
615+
if( cloexecPipe(output) )
581616
{
582617
int err = errno;
583618
do_close(input[0]); do_close(input[1]);
584619
startFailed(err);
585620
}
586-
if( pipe(error) )
621+
if( cloexecPipe(error) )
587622
{
588623
int err = errno;
589624
do_close(input[0]); do_close(input[1]);
590625
do_close(output[0]); do_close(output[1]);
591626
startFailed(err);
592627
}
593-
if( pipe(status) )
628+
if( cloexecPipe(status) )
594629
{
595630
int err = errno;
596631
do_close(input[0]); do_close(input[1]);
@@ -599,18 +634,6 @@ Dynamic _hx_std_process_run( String cmd, Array<String> vargs, int inShowParam )
599634
startFailed(err);
600635
}
601636

602-
// Close-on-exec: without it a concurrent spawn's child inherits these
603-
// descriptors, holding this child's stdout/stderr write ends open so
604-
// its streams never deliver EOF until the unrelated child also exits.
605-
// dup2 onto 0/1/2 in the child clears the flag on the std fds
606-
for(int i=0;i<2;i++)
607-
{
608-
fcntl(input[i], F_SETFD, FD_CLOEXEC);
609-
fcntl(output[i], F_SETFD, FD_CLOEXEC);
610-
fcntl(error[i], F_SETFD, FD_CLOEXEC);
611-
fcntl(status[i], F_SETFD, FD_CLOEXEC);
612-
}
613-
614637
hx::strbuf buf;
615638
std::vector< std::string > values;
616639
if (isRaw)
@@ -633,7 +656,11 @@ Dynamic _hx_std_process_run( String cmd, Array<String> vargs, int inShowParam )
633656
for(int i=0;i<values.size();i++)
634657
argv[i] = values[i].c_str();
635658

659+
// The child execs or exits holding its copy of the lock
660+
pthread_rwlock_wrlock(&sForkLock);
636661
int pid = fork();
662+
if( pid != 0 )
663+
pthread_rwlock_unlock(&sForkLock);
637664
if( pid == -1 )
638665
{
639666
int err = errno;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import sys.io.Process;
2+
import sys.thread.Thread;
3+
4+
// Process pipes were created with pipe() and marked close-on-exec afterwards,
5+
// so a child another thread forked in between kept them across exec
6+
// (libhxcpp #340). On Linux this program replaces pipe and pipe2 to pause one
7+
// launch right after its stdout pipe exists, starts a second process while
8+
// it waits, and has that child look for the first launch's pipe among its
9+
// descriptors.
10+
@:cppFileCode('
11+
#ifdef __linux__
12+
#include <unistd.h>
13+
#include <fcntl.h>
14+
#include <dirent.h>
15+
#include <pthread.h>
16+
#include <sys/stat.h>
17+
#include <sys/syscall.h>
18+
#include <atomic>
19+
#include <stdlib.h>
20+
#include <stdio.h>
21+
22+
#ifndef __THROW
23+
#define __THROW
24+
#endif
25+
26+
static std::atomic<int> sArmed(0);
27+
static pthread_t sArmedThread;
28+
static std::atomic<int> sCalls(0);
29+
static std::atomic<int> sPaused(0);
30+
static std::atomic<int> sRelease(0);
31+
static std::atomic<long> sInode(0);
32+
33+
// Makes the pipe itself, so the flags reach the kernel unchanged, and pauses
34+
// the armed thread after its second pipe: the launch stdout
35+
static int hookPipe(int *fds, int flags)
36+
{
37+
int result = (int)syscall(SYS_pipe2, fds, flags);
38+
if (result==0 && sArmed.load() && pthread_equal(pthread_self(), sArmedThread) && ++sCalls==2)
39+
{
40+
sArmed = 0;
41+
struct stat st;
42+
fstat(fds[1], &st);
43+
sInode = (long)st.st_ino;
44+
sPaused = 1;
45+
hx::EnterGCFreeZone();
46+
while(!sRelease.load())
47+
usleep(1000);
48+
hx::ExitGCFreeZone();
49+
}
50+
return result;
51+
}
52+
53+
extern "C" int pipe(int fds[2]) __THROW { return hookPipe(fds, 0); }
54+
extern "C" int pipe2(int fds[2], int flags) __THROW { return hookPipe(fds, flags); }
55+
56+
static void armHook() { sCalls = 0; sArmedThread = pthread_self(); sArmed = 1; }
57+
static bool hookPaused() { return sPaused.load(); }
58+
static void releaseHook() { sRelease = 1; }
59+
static ::String hookInode() { return ::String((int)sInode.load()); }
60+
61+
static ::String controlPipe()
62+
{
63+
int fds[2];
64+
if (syscall(SYS_pipe2, fds, O_CLOEXEC)!=0)
65+
return ::String("0");
66+
struct stat st;
67+
fstat(fds[1], &st);
68+
return ::String((int)st.st_ino);
69+
}
70+
71+
static bool hasPipe(int inInode)
72+
{
73+
bool found = false;
74+
DIR *dir = opendir("/proc/self/fd");
75+
if (!dir)
76+
return false;
77+
while(struct dirent *entry = readdir(dir))
78+
{
79+
struct stat st;
80+
if (entry->d_name[0]!=46 &&fstat(atoi(entry->d_name), &st)==0 && S_ISFIFO(st.st_mode) && (int)st.st_ino==inInode)
81+
found = true;
82+
}
83+
closedir(dir);
84+
return found;
85+
}
86+
#endif
87+
')
88+
class Main {
89+
static function main() {
90+
var args = Sys.args();
91+
#if linux
92+
if (args.length == 2 && args[0] == "child") {
93+
Sys.print(untyped __cpp__('hasPipe({0})', Std.parseInt(args[1])) ? "inherited" : "excluded");
94+
return;
95+
}
96+
#end
97+
if (Sys.systemName() != "Linux") {
98+
Sys.println("unrelated process pipes excluded from concurrent child: true");
99+
Sys.println("atomic close-on-exec control: true");
100+
return;
101+
}
102+
run();
103+
}
104+
105+
static function run() {
106+
#if linux
107+
var main = Thread.current();
108+
Thread.create(() -> {
109+
untyped __cpp__('armHook()');
110+
var p = new Process("/bin/true", []);
111+
p.exitCode();
112+
p.close();
113+
main.sendMessage("done");
114+
});
115+
var waited = 0;
116+
while (!untyped __cpp__('hookPaused()')) {
117+
if (++waited > 2000) {
118+
Sys.println("launch never reached its second pipe");
119+
Sys.exit(1);
120+
}
121+
Sys.sleep(0.005);
122+
}
123+
var launchPipe:String = untyped __cpp__('hookInode()');
124+
var control:String = untyped __cpp__('controlPipe()');
125+
Sys.println("unrelated process pipes excluded from concurrent child: " + (child(launchPipe) == "excluded"));
126+
Sys.println("atomic close-on-exec control: " + (child(control) == "excluded"));
127+
untyped __cpp__('releaseHook()');
128+
Thread.readMessage(true);
129+
#end
130+
}
131+
132+
static function child(inode:String):String {
133+
var p = new Process(Sys.programPath(), ["child", inode]);
134+
var result = p.stdout.readAll().toString();
135+
p.close();
136+
return result;
137+
}
138+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-m Main
2+
--cpp bin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
unrelated process pipes excluded from concurrent child: true
2+
atomic close-on-exec control: true

‎test/regression/emscripten-skip.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PollFailedConnect: sockets
3939
PollSocketLifetime: sockets
4040
PollingSafePoint: threads (std::thread, which needs a pthreads build)
4141
ProcessClosedParentStdin: processes
42+
ProcessConcurrentPipeInheritance: processes and threads
4243
ProcessCommunicate: processes and threads
4344
ProcessEmptyBuffer: processes
4445
ProcessExitCodeTwice: processes

0 commit comments

Comments
 (0)