Skip to content

Commit 995ad2a

Browse files
riptlripatel-fd
authored andcommitted
Unmap MAP_SHARED stacks on fork()
1 parent ef4d188 commit 995ad2a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/disco/topo/fd_topo_run.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ typedef struct {
147147
uint gid;
148148
int * done_futex;
149149
volatile int copied;
150+
void * stack_lo;
151+
void * stack_hi;
150152
} fd_topo_run_thread_args_t;
151153

152154
static void *
@@ -155,6 +157,11 @@ run_tile_thread_main( void * _args ) {
155157
FD_COMPILER_MFENCE();
156158
((fd_topo_run_thread_args_t *)_args)->copied = 1;
157159

160+
/* Prevent fork() from smashing the stack */
161+
if( FD_UNLIKELY( madvise( args.stack_lo, (ulong)args.stack_hi - (ulong)args.stack_lo, MADV_DONTFORK ) ) ) {
162+
FD_LOG_ERR(( "madvise(stack,MADV_DONTFORK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
163+
}
164+
158165
fd_topo_run_tile( args.topo, args.tile, 0, 1, 1, args.uid, args.gid, -1, NULL, NULL, &args.tile_run );
159166
if( FD_UNLIKELY( args.done_futex ) ) {
160167
for(;;) {
@@ -327,6 +334,8 @@ run_tile_thread( fd_topo_t * topo,
327334
.gid = gid,
328335
.done_futex = done_futex,
329336
.copied = 0,
337+
.stack_lo = stack,
338+
.stack_hi = (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ
330339
};
331340

332341
pthread_t pthread;

src/util/tile/fd_tile_threads.cxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ fd_tile_private_manager( void * _args ) {
297297
if( FD_LIKELY( stack ) ) { /* User provided stack */
298298
fd_tile_private_stack0 = (ulong)stack;
299299
fd_tile_private_stack1 = (ulong)stack + stack_sz;
300+
301+
/* Prevent another fork() from smashing the stack */
302+
if( FD_UNLIKELY( madvise( stack, FD_TILE_PRIVATE_STACK_SZ, MADV_DONTFORK ) ) ) {
303+
FD_LOG_ERR(( "madvise(stack,MADV_DONTFORK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
304+
}
305+
300306
} else { /* Pthread provided stack */
301307
fd_log_private_stack_discover( stack_sz, &fd_tile_private_stack0, &fd_tile_private_stack1 ); /* logs details */
302308
if( FD_UNLIKELY( !fd_tile_private_stack0 ) )

0 commit comments

Comments
 (0)