...
Text file
src/runtime/cgo/gcc_linux_arm64.c
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_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
43extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
44static void*
45threadentry(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
56void
57x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
58{
59 uintptr *pbounds;
60
61 /* The memory sanitizer distributed with versions of clang
62 before 3.8 has a bug: if you call mmap before malloc, mmap
63 may return an address that is later overwritten by the msan
64 library. Avoid this problem by forcing a call to malloc
65 here, before we ever call malloc.
66
67 This is only required for the memory sanitizer, so it's
68 unfortunate that we always run it. It should be possible
69 to remove this when we no longer care about versions of
70 clang before 3.8. The test for this is
71 misc/cgo/testsanitizers.
72
73 GCC works hard to eliminate a seemingly unnecessary call to
74 malloc, so we actually use the memory we allocate. */
75
76 setg_gcc = setg;
77 pbounds = (uintptr*)malloc(2 * sizeof(uintptr));
78 if (pbounds == NULL) {
79 fatalf("malloc failed: %s", strerror(errno));
80 }
81 _cgo_set_stacklo(g, pbounds);
82 free(pbounds);
83
84 if (x_cgo_inittls) {
85 x_cgo_inittls(tlsg, tlsbase);
86 }
87}
View as plain text