...

Text file src/pkg/runtime/cgo/gcc_linux_amd64.c

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	#include <pthread.h>
     6	#include <errno.h>
     7	#include <string.h> // strerror
     8	#include <signal.h>
     9	#include <stdlib.h>
    10	#include "libcgo.h"
    11	#include "libcgo_unix.h"
    12	
    13	static void* threadentry(void*);
    14	static void (*setg_gcc)(void*);
    15	
    16	// This will be set in gcc_android.c for android-specific customization.
    17	void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
    18	
    19	void
    20	x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    21	{
    22		pthread_attr_t *attr;
    23		size_t size;
    24	
    25		/* The memory sanitizer distributed with versions of clang
    26		   before 3.8 has a bug: if you call mmap before malloc, mmap
    27		   may return an address that is later overwritten by the msan
    28		   library.  Avoid this problem by forcing a call to malloc
    29		   here, before we ever call malloc.
    30	
    31		   This is only required for the memory sanitizer, so it's
    32		   unfortunate that we always run it.  It should be possible
    33		   to remove this when we no longer care about versions of
    34		   clang before 3.8.  The test for this is
    35		   misc/cgo/testsanitizers.
    36	
    37		   GCC works hard to eliminate a seemingly unnecessary call to
    38		   malloc, so we actually use the memory we allocate.  */
    39	
    40		setg_gcc = setg;
    41		attr = (pthread_attr_t*)malloc(sizeof *attr);
    42		if (attr == NULL) {
    43			fatalf("malloc failed: %s", strerror(errno));
    44		}
    45		pthread_attr_init(attr);
    46		pthread_attr_getstacksize(attr, &size);
    47		g->stacklo = (uintptr)&size - size + 4096;
    48		pthread_attr_destroy(attr);
    49		free(attr);
    50	
    51		if (x_cgo_inittls) {
    52			x_cgo_inittls(tlsg, tlsbase);
    53		}
    54	}
    55	
    56	
    57	void
    58	_cgo_sys_thread_start(ThreadStart *ts)
    59	{
    60		pthread_attr_t attr;
    61		sigset_t ign, oset;
    62		pthread_t p;
    63		size_t size;
    64		int err;
    65	
    66		sigfillset(&ign);
    67		pthread_sigmask(SIG_SETMASK, &ign, &oset);
    68	
    69		pthread_attr_init(&attr);
    70		pthread_attr_getstacksize(&attr, &size);
    71		// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    72		ts->g->stackhi = size;
    73		err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    74	
    75		pthread_sigmask(SIG_SETMASK, &oset, nil);
    76	
    77		if (err != 0) {
    78			fatalf("pthread_create failed: %s", strerror(err));
    79		}
    80	}
    81	
    82	static void*
    83	threadentry(void *v)
    84	{
    85		ThreadStart ts;
    86	
    87		ts = *(ThreadStart*)v;
    88		_cgo_tsan_acquire();
    89		free(v);
    90		_cgo_tsan_release();
    91	
    92		/*
    93		 * Set specific keys.
    94		 */
    95		setg_gcc((void*)ts.g);
    96	
    97		crosscall_amd64(ts.fn);
    98		return nil;
    99	}

View as plain text