Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_topo.h"
3 :
4 : #include "../metrics/fd_metrics.h"
5 : #include "../../util/tile/fd_tile_private.h"
6 :
7 : #include <unistd.h>
8 : #include <signal.h>
9 : #include <errno.h>
10 : #include <pthread.h>
11 : #include <sys/syscall.h>
12 : #include <linux/futex.h>
13 : #include <sys/resource.h>
14 : #include <sys/prctl.h>
15 : #include <sys/stat.h>
16 : #include <sys/mman.h>
17 : #include <net/if.h>
18 :
19 : static void
20 : initialize_logging( char const * tile_name,
21 : ulong tile_kind_id,
22 0 : ulong tid ) {
23 0 : fd_log_cpu_set( NULL );
24 0 : fd_log_private_tid_set( tid );
25 0 : char thread_name[ 20 ];
26 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile_name, tile_kind_id ) );
27 0 : fd_log_thread_set( thread_name );
28 0 : fd_log_private_stack_discover( FD_TILE_PRIVATE_STACK_SZ,
29 0 : &fd_tile_private_stack0, &fd_tile_private_stack1 );
30 0 : FD_LOG_INFO(( "booting tile %s pid:%lu tid:%lu", thread_name, fd_log_group_id(), tid ));
31 :
32 : /* FD_LOG_* calls fd_log_wallclock_cstr, which calls localtime_r. In
33 : glibc, this ends up calling a function called tzset_internal. The
34 : first time tzset_internal is called by a process, it may (and
35 : almost always does) call __tzfile_read, which invokes the openat
36 : syscall, and possibly several others on the time zone file
37 : (typically /etc/localtime) or on a file in the time zone directory.
38 : This kind of behavior is tricky to sandbox, so the easiest thing to
39 : do is initialize it prior to the sandbox and hope whatever libc is
40 : used behaves like glibc. This only matters when both the logfile
41 : and stderr filters are strict enough so that the immediately prior
42 : FD_LOG_INFO call is a no-op, since otherwise that call would have
43 : taken care of it. */
44 0 : char wallclock[FD_LOG_WALLCLOCK_CSTR_BUF_SZ];
45 0 : fd_log_wallclock_cstr( 0L, wallclock );
46 0 : }
47 :
48 : static void
49 : check_wait_debugger( ulong pid,
50 : volatile int * wait,
51 0 : volatile int * debugger ) {
52 0 : if( FD_UNLIKELY( debugger ) ) {
53 0 : FD_LOG_WARNING(( "waiting for debugger to attach to tile pid:%lu", pid ));
54 0 : if( FD_UNLIKELY( -1==kill( getpid(), SIGSTOP ) ) )
55 0 : FD_LOG_ERR(( "kill(SIGSTOP) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
56 0 : *FD_VOLATILE( debugger ) = 1;
57 0 : }
58 :
59 0 : if( FD_UNLIKELY( wait ) ) {
60 0 : while( FD_LIKELY( !*FD_VOLATILE( wait ) ) ) FD_SPIN_PAUSE();
61 0 : }
62 0 : }
63 :
64 : void
65 : fd_topo_run_tile( fd_topo_t * topo,
66 : fd_topo_tile_t * tile,
67 : int sandbox,
68 : int keep_controlling_terminal,
69 : int dumpable,
70 : uint uid,
71 : uint gid,
72 : int allow_fd,
73 : volatile int * wait,
74 : volatile int * debugger,
75 0 : fd_topo_run_tile_t * tile_run ) {
76 0 : char thread_name[ 20 ];
77 0 : FD_TEST( fd_cstr_printf_check( thread_name, sizeof( thread_name ), NULL, "%s:%lu", tile->name, tile->kind_id ) );
78 0 : if( FD_UNLIKELY( prctl( PR_SET_NAME, thread_name, 0, 0, 0 ) ) ) FD_LOG_ERR(( "prctl(PR_SET_NAME) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
79 :
80 0 : ulong pid = fd_sandbox_getpid(); /* Need to read /proc again.. we got a new PID from clone */
81 0 : ulong tid = fd_sandbox_gettid(); /* Need to read /proc again.. we got a new TID from clone */
82 :
83 0 : check_wait_debugger( pid, wait, debugger );
84 0 : initialize_logging( tile->name, tile->kind_id, tid );
85 :
86 : /* preload shared memory before sandboxing, so it is already mapped */
87 0 : fd_topo_join_tile_workspaces( topo, tile );
88 :
89 0 : if( FD_UNLIKELY( tile_run->privileged_init ) )
90 0 : tile_run->privileged_init( topo, tile );
91 :
92 0 : ulong allow_fds_offset = 0UL;
93 0 : int allow_fds[ 256 ] = { 0 };
94 0 : if( FD_LIKELY( -1!=allow_fd ) ) {
95 0 : allow_fds_offset = 1UL;
96 0 : allow_fds[ 0 ] = allow_fd;
97 0 : }
98 0 : ulong allow_fds_cnt = 0UL;
99 0 : if( FD_LIKELY( tile_run->populate_allowed_fds ) ) {
100 0 : allow_fds_cnt = tile_run->populate_allowed_fds( topo,
101 0 : tile,
102 0 : (sizeof(allow_fds)/sizeof(allow_fds[ 0 ]))-allow_fds_offset,
103 0 : allow_fds+allow_fds_offset );
104 0 : }
105 :
106 :
107 0 : struct sock_filter seccomp_filter[ 256UL ];
108 0 : ulong seccomp_filter_cnt = 0UL;
109 0 : if( FD_LIKELY( tile_run->populate_allowed_seccomp ) ) {
110 0 : seccomp_filter_cnt = tile_run->populate_allowed_seccomp( topo,
111 0 : tile,
112 0 : sizeof(seccomp_filter)/sizeof(seccomp_filter[ 0 ]),
113 0 : seccomp_filter );
114 0 : }
115 :
116 0 : ulong rlimit_file_cnt = tile_run->rlimit_file_cnt;
117 0 : if( tile_run->rlimit_file_cnt_fn ) {
118 0 : rlimit_file_cnt = tile_run->rlimit_file_cnt_fn( topo, tile );
119 0 : }
120 :
121 0 : if( FD_LIKELY( sandbox ) ) {
122 0 : fd_sandbox_enter( uid,
123 0 : gid,
124 0 : tile_run->keep_host_networking,
125 0 : tile_run->allow_connect,
126 0 : tile_run->allow_renameat,
127 0 : keep_controlling_terminal,
128 0 : dumpable,
129 0 : rlimit_file_cnt,
130 0 : tile_run->rlimit_address_space,
131 0 : tile_run->rlimit_data,
132 0 : allow_fds_cnt+allow_fds_offset,
133 0 : allow_fds,
134 0 : seccomp_filter_cnt,
135 0 : seccomp_filter );
136 0 : } else {
137 0 : fd_sandbox_switch_uid_gid( uid, gid );
138 0 : }
139 :
140 : /* Now we are sandboxed, join all the tango IPC objects in the workspaces */
141 0 : fd_topo_fill_tile( topo, tile );
142 :
143 0 : FD_TEST( tile->metrics );
144 0 : fd_metrics_register( tile->metrics );
145 :
146 0 : FD_MGAUGE_SET( TILE, PID, pid );
147 0 : FD_MGAUGE_SET( TILE, TID, tid );
148 :
149 0 : if( FD_UNLIKELY( tile_run->unprivileged_init ) )
150 0 : tile_run->unprivileged_init( topo, tile );
151 :
152 0 : tile_run->run( topo, tile );
153 0 : if( FD_UNLIKELY( !tile->allow_shutdown ) ) FD_LOG_ERR(( "tile %s:%lu run loop returned", tile->name, tile->kind_id ));
154 :
155 0 : FD_MGAUGE_SET( TILE, STATUS, 2UL );
156 0 : }
157 :
158 : typedef struct {
159 : fd_topo_t * topo;
160 : fd_topo_tile_t * tile;
161 : fd_topo_run_tile_t tile_run;
162 : uint uid;
163 : uint gid;
164 : volatile int copied;
165 : void * stack_lo;
166 : void * stack_hi;
167 : } fd_topo_run_thread_args_t;
168 :
169 : static void *
170 0 : run_tile_thread_main( void * _args ) {
171 0 : fd_topo_run_thread_args_t args = *(fd_topo_run_thread_args_t *)_args;
172 0 : FD_COMPILER_MFENCE();
173 0 : ((fd_topo_run_thread_args_t *)_args)->copied = 1;
174 0 : FD_COMPILER_MFENCE();
175 :
176 : /* Prevent fork() from smashing the stack */
177 0 : if( FD_UNLIKELY( madvise( args.stack_lo, (ulong)args.stack_hi - (ulong)args.stack_lo, MADV_DONTFORK ) ) ) {
178 0 : FD_LOG_ERR(( "madvise(stack,MADV_DONTFORK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
179 0 : }
180 :
181 0 : fd_topo_run_tile( args.topo, args.tile, 0, 1, 1, args.uid, args.gid, -1, NULL, NULL, &args.tile_run );
182 0 : FD_TEST( args.tile->allow_shutdown );
183 0 : return NULL;
184 0 : }
185 :
186 : /* fd_topo_tile_stack_join_anon is a variant of fd_topo_tile_stack_join
187 : that acquires private anonymous memory instead of shared pages.
188 :
189 : This is required for fork() to work, as the parent and child process
190 : would otherwise share a stack and corrupt each other. While fork()
191 : is banned in tile user code, some dynamic analysis tools (like MSan)
192 : unfortunately rely on it. */
193 :
194 : FD_FN_UNUSED static void *
195 0 : fd_topo_tile_stack_join_anon( void ) {
196 0 :
197 0 : ulong sz = 2*FD_TILE_PRIVATE_STACK_SZ;
198 0 : int prot = PROT_READ|PROT_WRITE;
199 0 : int flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK;
200 0 :
201 0 : uchar * stack = MAP_FAILED;
202 0 : #if !FD_HAS_ASAN && !FD_HAS_MSAN
203 0 : stack = mmap( NULL, sz, prot, flags|MAP_HUGETLB, -1, 0 );
204 0 : #endif
205 0 :
206 0 : if( stack==MAP_FAILED ) {
207 0 : stack = mmap( NULL, sz, prot, flags, -1, 0 );
208 0 : if( FD_UNLIKELY( stack==MAP_FAILED ) ) {
209 0 : FD_LOG_ERR(( "mmap() for stack failed (%i-%s)", errno, fd_io_strerror( errno ) ));
210 0 : }
211 0 : }
212 0 :
213 0 : /* Create the guard regions in the extra space */
214 0 : void * guard_lo = (void *)( stack - FD_SHMEM_NORMAL_PAGE_SZ );
215 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
216 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
217 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_lo, errno, fd_io_strerror( errno ) ));
218 0 :
219 0 : void * guard_hi = (void *)( stack + FD_TILE_PRIVATE_STACK_SZ );
220 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
221 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
222 0 : FD_LOG_ERR(( "mmap(%p) failed (%i-%s)", guard_hi, errno, fd_io_strerror( errno ) ));
223 0 :
224 0 : return stack;
225 0 : }
226 :
227 : void *
228 : fd_topo_tile_stack_join( char const * app_name,
229 : char const * tile_name,
230 0 : ulong tile_kind_id ) {
231 : #if FD_HAS_MSAN
232 : return fd_topo_tile_stack_join_anon();
233 : #endif
234 :
235 0 : char name[ PATH_MAX ];
236 0 : FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_stack_%s%lu", app_name, tile_name, tile_kind_id ) );
237 :
238 0 : uchar * stack = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, NULL, NULL, NULL );
239 0 : if( FD_UNLIKELY( !stack ) ) FD_LOG_ERR(( "fd_shmem_join failed" ));
240 :
241 : /* Make space for guard lo and guard hi */
242 0 : if( FD_UNLIKELY( fd_shmem_release( stack, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
243 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
244 0 : stack += FD_SHMEM_HUGE_PAGE_SZ;
245 0 : if( FD_UNLIKELY( fd_shmem_release( stack + FD_TILE_PRIVATE_STACK_SZ, FD_SHMEM_HUGE_PAGE_SZ, 1UL ) ) )
246 0 : FD_LOG_ERR(( "fd_shmem_release (%d-%s)", errno, fd_io_strerror( errno ) ));
247 :
248 : /* Create the guard regions in the extra space */
249 0 : void * guard_lo = (void *)(stack - FD_SHMEM_NORMAL_PAGE_SZ );
250 0 : if( FD_UNLIKELY( mmap( guard_lo, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
251 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_lo ) )
252 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
253 :
254 0 : void * guard_hi = (void *)(stack + FD_TILE_PRIVATE_STACK_SZ);
255 0 : if( FD_UNLIKELY( mmap( guard_hi, FD_SHMEM_NORMAL_PAGE_SZ, PROT_NONE,
256 0 : MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, (off_t)0 )!=guard_hi ) )
257 0 : FD_LOG_ERR(( "mmap failed (%i-%s)", errno, fd_io_strerror( errno ) ));
258 :
259 0 : return stack;
260 0 : }
261 :
262 : static inline void
263 : run_tile_thread( fd_topo_t * topo,
264 : fd_topo_tile_t * tile,
265 : fd_topo_run_tile_t tile_run,
266 : uint uid,
267 : uint gid,
268 : fd_cpuset_t const * floating_cpu_set,
269 : int floating_priority,
270 0 : fd_topo_run_thread_args_t * args ) {
271 : /* tpool will assign a thread later */
272 0 : if( FD_UNLIKELY( tile_run.for_tpool ) ) return;
273 0 : void * stack = fd_topo_tile_stack_join( topo->app_name, tile->name, tile->kind_id );
274 :
275 0 : pthread_attr_t attr[ 1 ];
276 0 : if( FD_UNLIKELY( pthread_attr_init( attr ) ) ) FD_LOG_ERR(( "pthread_attr_init() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
277 0 : if( FD_UNLIKELY( pthread_attr_setstack( attr, stack, FD_TILE_PRIVATE_STACK_SZ ) ) ) FD_LOG_ERR(( "pthread_attr_setstacksize() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
278 :
279 0 : FD_CPUSET_DECL( cpu_set );
280 0 : if( FD_LIKELY( tile->cpu_idx<65535UL ) ) {
281 : /* set the thread affinity before we clone the new process to ensure
282 : kernel first touch happens on the desired thread. */
283 0 : fd_cpuset_insert( cpu_set, tile->cpu_idx );
284 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
285 0 : } else {
286 0 : fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
287 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
288 0 : }
289 :
290 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
291 0 : if( FD_LIKELY( errno==EINVAL ) ) {
292 0 : FD_LOG_ERR(( "Unable to set the thread affinity for tile %s:%lu on cpu %lu. It is likely that the affinity "
293 0 : "you have specified for this tile in [layout.affinity] of your configuration file contains a "
294 0 : "CPU (%lu) which does not exist on this machine.",
295 0 : tile->name, tile->kind_id, tile->cpu_idx, tile->cpu_idx ));
296 0 : } else {
297 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
298 0 : }
299 0 : }
300 :
301 0 : *args = (fd_topo_run_thread_args_t) {
302 0 : .topo = topo,
303 0 : .tile = tile,
304 0 : .tile_run = tile_run,
305 0 : .uid = uid,
306 0 : .gid = gid,
307 0 : .copied = 0,
308 0 : .stack_lo = stack,
309 0 : .stack_hi = (uchar *)stack + FD_TILE_PRIVATE_STACK_SZ
310 0 : };
311 :
312 0 : pthread_t pthread;
313 0 : if( FD_UNLIKELY( pthread_create( &pthread, attr, run_tile_thread_main, args ) ) ) FD_LOG_ERR(( "pthread_create() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
314 0 : }
315 :
316 : void
317 : fd_topo_run_single_process( fd_topo_t * topo,
318 : int agave,
319 : uint uid,
320 : uint gid,
321 0 : fd_topo_run_tile_t (* tile_run )( fd_topo_tile_t const * tile ) ) {
322 0 : FD_LOG_NOTICE(( "running single threaded topology with %lu tiles and %lu GiB memory",
323 0 : topo->tile_cnt, fd_topo_mlock( topo ) / (1UL << 30) ));
324 :
325 : /* Save the current affinity, it will be restored after creating any child tiles */
326 0 : FD_CPUSET_DECL( floating_cpu_set );
327 0 : if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
328 0 : FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
329 :
330 0 : errno = 0;
331 0 : int save_priority = getpriority( PRIO_PROCESS, 0 );
332 0 : if( FD_UNLIKELY( -1==save_priority && errno ) ) FD_LOG_ERR(( "getpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
333 :
334 0 : fd_topo_run_thread_args_t args[ FD_TOPO_MAX_TILES ];
335 :
336 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
337 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
338 0 : if( !agave && tile->is_agave ) continue;
339 0 : if( agave==1 && !tile->is_agave ) continue;
340 :
341 0 : fd_topo_run_tile_t run_tile = tile_run( tile );
342 0 : run_tile_thread( topo, tile, run_tile, uid, gid, floating_cpu_set, save_priority, &args[ i ] );
343 0 : }
344 :
345 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
346 0 : fd_topo_tile_t * tile = &topo->tiles[ i ];
347 0 : if( !agave && tile->is_agave ) continue;
348 0 : if( agave==1 && !tile->is_agave ) continue;
349 :
350 0 : while( !FD_VOLATILE( args[ i ].copied ) ) FD_SPIN_PAUSE();
351 0 : }
352 :
353 0 : fd_sandbox_switch_uid_gid( uid, gid );
354 :
355 0 : if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
356 0 : if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
357 0 : FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
358 0 : }
|