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 #define WIN32_LEAN_AND_MEAN
6 #include <windows.h>
7 #include <process.h>
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 
13 #include "libcgo.h"
14 #include "libcgo_windows.h"
15 
16 // Ensure there's one symbol marked __declspec(dllexport).
17 // If there are no exported symbols, the unfortunate behavior of
18 // the binutils linker is to also strip the relocations table,
19 // resulting in non-PIE binary. The other option is the
20 // --export-all-symbols flag, but we don't need to export all symbols
21 // and this may overflow the export table (#40795).
22 // See https://sourceware.org/bugzilla/show_bug.cgi?id=19011
23 __declspec(dllexport) int _cgo_dummy_export;
24 
25 static volatile LONG runtime_init_once_gate = 0;
26 static volatile LONG runtime_init_once_done = 0;
27 
28 static CRITICAL_SECTION runtime_init_cs;
29 
30 static HANDLE runtime_init_wait;
31 static int runtime_init_done;
32 
33 uintptr_t x_cgo_pthread_key_created;
34 void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t);
35 
36 // Pre-initialize the runtime synchronization objects
37 void
_cgo_preinit_init()38 _cgo_preinit_init() {
39 	 runtime_init_wait = CreateEvent(NULL, TRUE, FALSE, NULL);
40 	 if (runtime_init_wait == NULL) {
41 		fprintf(stderr, "runtime: failed to create runtime initialization wait event.\n");
42 		abort();
43 	 }
44 
45 	 InitializeCriticalSection(&runtime_init_cs);
46 }
47 
48 // Make sure that the preinit sequence has run.
49 void
_cgo_maybe_run_preinit()50 _cgo_maybe_run_preinit() {
51 	 if (!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
52 			if (InterlockedIncrement(&runtime_init_once_gate) == 1) {
53 				 _cgo_preinit_init();
54 				 InterlockedIncrement(&runtime_init_once_done);
55 			} else {
56 				 // Decrement to avoid overflow.
57 				 InterlockedDecrement(&runtime_init_once_gate);
58 				 while(!InterlockedExchangeAdd(&runtime_init_once_done, 0)) {
59 						Sleep(0);
60 				 }
61 			}
62 	 }
63 }
64 
65 void
x_cgo_sys_thread_create(void (* func)(void *),void * arg)66 x_cgo_sys_thread_create(void (*func)(void*), void* arg) {
67 	_cgo_beginthread(func, arg);
68 }
69 
70 int
_cgo_is_runtime_initialized()71 _cgo_is_runtime_initialized() {
72 	 EnterCriticalSection(&runtime_init_cs);
73 	 int status = runtime_init_done;
74 	 LeaveCriticalSection(&runtime_init_cs);
75 	 return status;
76 }
77 
78 uintptr_t
_cgo_wait_runtime_init_done(void)79 _cgo_wait_runtime_init_done(void) {
80 	void (*pfn)(struct context_arg*);
81 
82 	 _cgo_maybe_run_preinit();
83 	while (!_cgo_is_runtime_initialized()) {
84 			WaitForSingleObject(runtime_init_wait, INFINITE);
85 	}
86 	pfn = _cgo_get_context_function();
87 	if (pfn != nil) {
88 		struct context_arg arg;
89 
90 		arg.Context = 0;
91 		(*pfn)(&arg);
92 		return arg.Context;
93 	}
94 	return 0;
95 }
96 
97 // Should not be used since x_cgo_pthread_key_created will always be zero.
x_cgo_bindm(void * dummy)98 void x_cgo_bindm(void* dummy) {
99 	fprintf(stderr, "unexpected cgo_bindm on Windows\n");
100 	abort();
101 }
102 
103 void
x_cgo_notify_runtime_init_done(void * dummy)104 x_cgo_notify_runtime_init_done(void* dummy) {
105 	 _cgo_maybe_run_preinit();
106 
107 	 EnterCriticalSection(&runtime_init_cs);
108 	runtime_init_done = 1;
109 	 LeaveCriticalSection(&runtime_init_cs);
110 
111 	 if (!SetEvent(runtime_init_wait)) {
112 		fprintf(stderr, "runtime: failed to signal runtime initialization complete.\n");
113 		abort();
114 	}
115 }
116 
117 // The context function, used when tracing back C calls into Go.
118 static void (*cgo_context_function)(struct context_arg*);
119 
120 // Sets the context function to call to record the traceback context
121 // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
x_cgo_set_context_function(void (* context)(struct context_arg *))122 void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
123 	EnterCriticalSection(&runtime_init_cs);
124 	cgo_context_function = context;
125 	LeaveCriticalSection(&runtime_init_cs);
126 }
127 
128 // Gets the context function.
_cgo_get_context_function(void)129 void (*(_cgo_get_context_function(void)))(struct context_arg*) {
130 	void (*ret)(struct context_arg*);
131 
132 	EnterCriticalSection(&runtime_init_cs);
133 	ret = cgo_context_function;
134 	LeaveCriticalSection(&runtime_init_cs);
135 	return ret;
136 }
137 
_cgo_beginthread(void (* func)(void *),void * arg)138 void _cgo_beginthread(void (*func)(void*), void* arg) {
139 	int tries;
140 	uintptr_t thandle;
141 
142 	for (tries = 0; tries < 20; tries++) {
143 		thandle = _beginthread(func, 0, arg);
144 		if (thandle == -1 && errno == EACCES) {
145 			// "Insufficient resources", try again in a bit.
146 			//
147 			// Note that the first Sleep(0) is a yield.
148 			Sleep(tries); // milliseconds
149 			continue;
150 		} else if (thandle == -1) {
151 			break;
152 		}
153 		return; // Success!
154 	}
155 
156 	fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);
157 	abort();
158 }
159