github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/runtime/cgo/gcc_linux_arm64.c (about)

     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  
    13  static void *threadentry(void*);
    14  
    15  void (*x_cgo_inittls)(void **tlsg, void **tlsbase) __attribute__((common));
    16  static void (*setg_gcc)(void*);
    17  
    18  void
    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_getstacksize(&attr, &size);
    32  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    33  	ts->g->stackhi = size;
    34  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    35  
    36  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    37  
    38  	if (err != 0) {
    39  		fatalf("pthread_create failed: %s", strerror(err));
    40  	}
    41  }
    42  
    43  extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    44  static void*
    45  threadentry(void *v)
    46  {
    47  	ThreadStart ts;
    48  
    49  	ts = *(ThreadStart*)v;
    50  	free(v);
    51  
    52  	crosscall1(ts.fn, setg_gcc, (void*)ts.g);
    53  	return nil;
    54  }
    55  
    56  void
    57  x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
    58  {
    59  	pthread_attr_t *attr;
    60  	size_t size;
    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  	attr = (pthread_attr_t*)malloc(sizeof *attr);
    79  	if (attr == NULL) {
    80  		fatalf("malloc failed: %s", strerror(errno));
    81  	}
    82  	pthread_attr_init(attr);
    83  	pthread_attr_getstacksize(attr, &size);
    84  	g->stacklo = (uintptr)&size - size + 4096;
    85  	pthread_attr_destroy(attr);
    86  	free(attr);
    87  
    88  	if (x_cgo_inittls) {
    89  		x_cgo_inittls(tlsg, tlsbase);
    90  	}
    91  }