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