...

Text file src/runtime/cgo/gcc_linux_arm64.c

Documentation: runtime/cgo

     1// Copyright 2015 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>
     8#include <signal.h>
     9#include <stdlib.h>
    10#include "libcgo.h"
    11#include "libcgo_unix.h"
    12
    13static void *threadentry(void*);
    14
    15void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
    16static void (*setg_gcc)(void*);
    17
    18void
    19_cgo_sys_thread_start(ThreadStart *ts)
    20{
    21	pthread_attr_t attr;
    22	sigset_t ign, oset;
    23	pthread_t p;
    24	size_t size;
    25	int err;
    26
    27	sigfillset(&ign);
    28	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    29
    30	pthread_attr_init(&attr);
    31	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    32	pthread_attr_getstacksize(&attr, &size);
    33	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    34	ts->g->stackhi = size;
    35	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    36
    37	pthread_sigmask(SIG_SETMASK, &oset, nil);
    38
    39	if (err != 0) {
    40		fatalf("pthread_create failed: %s", strerror(err));
    41	}
    42}
    43
    44extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    45static void*
    46threadentry(void *v)
    47{
    48	ThreadStart ts;
    49
    50	ts = *(ThreadStart*)v;
    51	free(v);
    52
    53	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    54	return nil;
    55}
    56
    57void
    58x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    59{
    60	uintptr *pbounds;
    61
    62	/* The memory sanitizer distributed with versions of clang
    63	   before 3.8 has a bug: if you call mmap before malloc, mmap
    64	   may return an address that is later overwritten by the msan
    65	   library.  Avoid this problem by forcing a call to malloc
    66	   here, before we ever call malloc.
    67
    68	   This is only required for the memory sanitizer, so it's
    69	   unfortunate that we always run it.  It should be possible
    70	   to remove this when we no longer care about versions of
    71	   clang before 3.8.  The test for this is
    72	   misc/cgo/testsanitizers.
    73
    74	   GCC works hard to eliminate a seemingly unnecessary call to
    75	   malloc, so we actually use the memory we allocate.  */
    76
    77	setg_gcc = setg;
    78	pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
    79	if (pbounds == NULL) {
    80		fatalf("malloc failed: %s", strerror(errno));
    81	}
    82	_cgo_set_stacklo(g, pbounds);
    83	free(pbounds);
    84
    85	if (x_cgo_inittls) {
    86		x_cgo_inittls(tlsg, tlsbase);
    87	}
    88}

View as plain text