Text file
src/runtime/cgo/gcc_darwin_arm64.c
1 // Copyright 2014 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 <limits.h>
6 #include <pthread.h>
7 #include <signal.h>
8 #include <string.h> /* for strerror */
9 #include <sys/param.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12
13 #include "libcgo.h"
14 #include "libcgo_unix.h"
15
16 #include <TargetConditionals.h>
17
18 #if TARGET_OS_IPHONE
19 #include <CoreFoundation/CFBundle.h>
20 #include <CoreFoundation/CFString.h>
21 #endif
22
23 static void *threadentry(void*);
24 static void (*setg_gcc)(void*);
25
26 void
27 _cgo_sys_thread_start(ThreadStart *ts)
28 {
29 pthread_attr_t attr;
30 sigset_t ign, oset;
31 pthread_t p;
32 size_t size;
33 int err;
34
35 //fprintf(stderr, "runtime/cgo: _cgo_sys_thread_start: fn=%p, g=%p\n", ts->fn, ts->g); // debug
36 sigfillset(&ign);
37 pthread_sigmask(SIG_SETMASK, &ign, &oset);
38
39 size = pthread_get_stacksize_np(pthread_self());
40 pthread_attr_init(&attr);
41 pthread_attr_setstacksize(&attr, size);
42 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
43 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
44 ts->g->stackhi = size;
45 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
46
47 pthread_sigmask(SIG_SETMASK, &oset, nil);
48
49 if (err != 0) {
50 fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
51 abort();
52 }
53 }
54
55 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
56 static void*
57 threadentry(void *v)
58 {
59 ThreadStart ts;
60
61 ts = *(ThreadStart*)v;
62 free(v);
63
64 #if TARGET_OS_IPHONE
65 darwin_arm_init_thread_exception_port();
66 #endif
67
68 crosscall1(ts.fn, setg_gcc, (void*)ts.g);
69 return nil;
70 }
71
72 #if TARGET_OS_IPHONE
73
74 // init_working_dir sets the current working directory to the app root.
75 // By default ios/arm64 processes start in "/".
76 static void
77 init_working_dir()
78 {
79 CFBundleRef bundle = CFBundleGetMainBundle();
80 if (bundle == NULL) {
81 fprintf(stderr, "runtime/cgo: no main bundle\n");
82 return;
83 }
84 CFURLRef url_ref = CFBundleCopyResourceURL(bundle, CFSTR("Info"), CFSTR("plist"), NULL);
85 if (url_ref == NULL) {
86 // No Info.plist found. It can happen on Corellium virtual devices.
87 return;
88 }
89 CFStringRef url_str_ref = CFURLGetString(url_ref);
90 char buf[MAXPATHLEN];
91 Boolean res = CFStringGetCString(url_str_ref, buf, sizeof(buf), kCFStringEncodingUTF8);
92 CFRelease(url_ref);
93 if (!res) {
94 fprintf(stderr, "runtime/cgo: cannot get URL string\n");
95 return;
96 }
97
98 // url is of the form "file:///path/to/Info.plist".
99 // strip it down to the working directory "/path/to".
100 int url_len = strlen(buf);
101 if (url_len < sizeof("file://")+sizeof("/Info.plist")) {
102 fprintf(stderr, "runtime/cgo: bad URL: %s\n", buf);
103 return;
104 }
105 buf[url_len-sizeof("/Info.plist")+1] = 0;
106 char *dir = &buf[0] + sizeof("file://")-1;
107
108 if (chdir(dir) != 0) {
109 fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", dir);
110 }
111
112 // The test harness in go_ios_exec passes the relative working directory
113 // in the GoExecWrapperWorkingDirectory property of the app bundle.
114 CFStringRef wd_ref = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("GoExecWrapperWorkingDirectory"));
115 if (wd_ref != NULL) {
116 if (!CFStringGetCString(wd_ref, buf, sizeof(buf), kCFStringEncodingUTF8)) {
117 fprintf(stderr, "runtime/cgo: cannot get GoExecWrapperWorkingDirectory string\n");
118 return;
119 }
120 if (chdir(buf) != 0) {
121 fprintf(stderr, "runtime/cgo: chdir(%s) failed\n", buf);
122 }
123 }
124 }
125
126 #endif // TARGET_OS_IPHONE
127
128 void
129 x_cgo_init(G *g, void (*setg)(void*))
130 {
131 //fprintf(stderr, "x_cgo_init = %p\n", &x_cgo_init); // aid debugging in presence of ASLR
132 setg_gcc = setg;
133 _cgo_set_stacklo(g, NULL);
134
135 #if TARGET_OS_IPHONE
136 darwin_arm_init_mach_exception_handler();
137 darwin_arm_init_thread_exception_port();
138 init_working_dir();
139 #endif
140 }
141
View as plain text