-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathruntime_ccall.cpp
333 lines (309 loc) · 10.2 KB
/
runtime_ccall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// This file is a part of Julia. License is MIT: https://julialang.org/license
#include "llvm-version.h"
#include <map>
#include <string>
#include <llvm/ADT/StringMap.h>
#include <llvm/TargetParser/Host.h>
#include <llvm/Support/raw_ostream.h>
#include "julia.h"
#include "julia_internal.h"
#include "processor.h"
#include "julia_assert.h"
#ifndef _OS_WINDOWS_
#include <sys/mman.h>
#if defined(_OS_DARWIN_) && !defined(MAP_ANONYMOUS)
#define MAP_ANONYMOUS MAP_ANON
#endif
#endif
using namespace llvm;
// --- library symbol lookup ---
// map from user-specified lib names to handles
static std::map<std::string, void*> libMap;
static jl_mutex_t libmap_lock;
extern "C"
void *jl_get_library_(const char *f_lib, int throw_err)
{
if (f_lib == NULL)
return jl_RTLD_DEFAULT_handle;
if (f_lib == JL_EXE_LIBNAME)
return jl_exe_handle;
if (f_lib == JL_LIBJULIA_INTERNAL_DL_LIBNAME)
return jl_libjulia_internal_handle;
if (f_lib == JL_LIBJULIA_DL_LIBNAME)
return jl_libjulia_handle;
JL_LOCK(&libmap_lock);
// This is the only operation we do on the map, which doesn't invalidate
// any references or iterators.
void **map_slot = &libMap[f_lib];
void *hnd = *map_slot;
if (hnd == NULL) {
hnd = jl_load_dynamic_library(f_lib, JL_RTLD_DEFAULT, throw_err);
if (hnd != NULL)
*map_slot = hnd;
}
JL_UNLOCK(&libmap_lock);
return hnd;
}
extern "C" JL_DLLEXPORT
void *jl_load_and_lookup(const char *f_lib, const char *f_name, _Atomic(void*) *hnd)
{
void *handle = jl_atomic_load_acquire(hnd);
if (!handle)
jl_atomic_store_release(hnd, (handle = jl_get_library(f_lib)));
void * ptr;
jl_dlsym(handle, f_name, &ptr, 1);
return ptr;
}
// jl_load_and_lookup, but with library computed at run time on first call
extern "C" JL_DLLEXPORT
void *jl_lazy_load_and_lookup(jl_value_t *lib_val, const char *f_name)
{
void *lib_ptr;
if (jl_is_symbol(lib_val))
lib_ptr = jl_get_library(jl_symbol_name((jl_sym_t*)lib_val));
else if (jl_is_string(lib_val))
lib_ptr = jl_get_library(jl_string_data(lib_val));
else if (jl_libdl_dlopen_func != NULL) {
// Call `dlopen(lib_val)`; this is the correct path for the `LazyLibrary` case,
// but it also takes any other value, and so we define `dlopen(x::Any) = throw(TypeError(...))`.
lib_ptr = jl_unbox_voidpointer(jl_apply_generic(jl_libdl_dlopen_func, &lib_val, 1));
} else
jl_type_error("ccall", (jl_value_t*)jl_symbol_type, lib_val);
void *ptr;
jl_dlsym(lib_ptr, f_name, &ptr, 1);
return ptr;
}
// miscellany
extern "C" JL_DLLEXPORT
jl_value_t *jl_get_JIT(void)
{
const std::string& HostJITName = "ORCJIT";
return jl_pchar_to_string(HostJITName.data(), HostJITName.size());
}
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 256
#endif
// Form a file name from a pattern made by replacing tokens,
// similar to many of those provided by ssh_config TOKENS:
//
// %% A literal `%'.
// %p The process PID
// %d Local user's home directory.
// %i The local user ID.
// %L The local hostname.
// %l The local hostname, including the domain name.
// %u The local username.
std::string jl_format_filename(StringRef output_pattern)
{
std::string buf;
raw_string_ostream outfile(buf);
bool special = false;
char hostname[MAXHOSTNAMELEN + 1];
uv_passwd_t pwd;
bool got_pwd = false;
for (auto c : output_pattern) {
if (special) {
if (!got_pwd && (c == 'i' || c == 'd' || c == 'u')) {
int r = uv_os_get_passwd(&pwd);
if (r == 0)
got_pwd = true;
}
switch (c) {
case 'p':
outfile << uv_os_getpid();
break;
case 'd':
if (got_pwd)
outfile << pwd.homedir;
break;
case 'i':
if (got_pwd)
outfile << pwd.uid;
break;
case 'l':
case 'L':
if (gethostname(hostname, sizeof(hostname)) == 0) {
hostname[sizeof(hostname) - 1] = '\0'; /* Null terminate, just to be safe. */
outfile << hostname;
}
#ifndef _OS_WINDOWS_
if (c == 'l' && getdomainname(hostname, sizeof(hostname)) == 0) {
hostname[sizeof(hostname) - 1] = '\0'; /* Null terminate, just to be safe. */
outfile << hostname;
}
#endif
break;
case 'u':
if (got_pwd)
outfile << pwd.username;
break;
default:
outfile << c;
break;
}
special = false;
}
else if (c == '%') {
special = true;
}
else {
outfile << c;
}
}
if (got_pwd)
uv_os_free_passwd(&pwd);
return outfile.str();
}
extern "C" JL_DLLEXPORT char *jl_format_filename(const char *output_pattern)
{
return strdup(jl_format_filename(StringRef(output_pattern)).c_str());
}
static uv_mutex_t trampoline_lock; // for accesses to the cache and freelist
static void *trampoline_freelist;
static void *trampoline_alloc() JL_NOTSAFEPOINT // lock taken by caller
{
const int sz = 64; // oversized for most platforms. todo: use precise value?
if (!trampoline_freelist) {
int last_errno = errno;
#ifdef _OS_WINDOWS_
DWORD last_error = GetLastError();
void *mem = VirtualAlloc(NULL, jl_page_size,
MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (mem == NULL)
jl_throw(jl_memory_exception);
SetLastError(last_error);
#else
void *mem = mmap(0, jl_page_size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
errno = last_errno;
if (mem == MAP_FAILED)
jl_throw(jl_memory_exception);
#endif
errno = last_errno;
void *next = NULL;
assert(sz < jl_page_size);
for (size_t i = 0; i + sz <= jl_page_size; i += sz) {
void **curr = (void**)((char*)mem + i);
*curr = next;
next = (void*)curr;
}
trampoline_freelist = next;
}
void *tramp = trampoline_freelist;
trampoline_freelist = *(void**)tramp;
return tramp;
}
static void trampoline_free(void *tramp) JL_NOTSAFEPOINT // lock taken by caller
{
*(void**)tramp = trampoline_freelist;
trampoline_freelist = tramp;
}
static void trampoline_deleter(void **f) JL_NOTSAFEPOINT
{
void *tramp = f[0];
void *fobj = f[1];
void *cache = f[2];
void *nval = f[3];
f[0] = NULL;
f[2] = NULL;
f[3] = NULL;
uv_mutex_lock(&trampoline_lock);
if (tramp)
trampoline_free(tramp);
if (fobj && cache)
ptrhash_remove((htable_t*)cache, fobj);
if (nval)
free(nval);
uv_mutex_unlock(&trampoline_lock);
}
typedef void *(*init_trampoline_t)(void *tramp, void **nval) JL_NOTSAFEPOINT;
// Use of `cache` is not clobbered in JL_TRY
JL_GCC_IGNORE_START("-Wclobbered")
extern "C" JL_DLLEXPORT
jl_value_t *jl_get_cfunction_trampoline(
// dynamic inputs:
jl_value_t *fobj,
jl_datatype_t *result_type,
// call-site constants:
htable_t *cache, // weakref htable indexed by (fobj, vals)
jl_svec_t *fill,
init_trampoline_t init_trampoline,
jl_unionall_t *env,
jl_value_t **vals)
{
// lookup (fobj, vals) in cache
uv_mutex_lock(&trampoline_lock);
if (!cache->table)
htable_new(cache, 1);
if (fill != jl_emptysvec) {
htable_t **cache2 = (htable_t**)ptrhash_bp(cache, (void*)vals);
cache = *cache2;
if (cache == HT_NOTFOUND) {
cache = htable_new((htable_t*)malloc_s(sizeof(htable_t)), 1);
*cache2 = cache;
}
}
void *tramp = ptrhash_get(cache, (void*)fobj);
uv_mutex_unlock(&trampoline_lock);
if (tramp != HT_NOTFOUND) {
assert((jl_datatype_t*)jl_typeof(tramp) == result_type);
return (jl_value_t*)tramp;
}
// not found, allocate a new one
size_t n = jl_svec_len(fill);
void **nval = (void**)malloc_s(sizeof(void*) * (n + 1));
nval[0] = (void*)fobj;
jl_value_t *result;
JL_TRY {
for (size_t i = 0; i < n; i++) {
jl_value_t *sparam_val = jl_instantiate_type_in_env(jl_svecref(fill, i), env, vals);
if (sparam_val != (jl_value_t*)jl_any_type)
if (!jl_is_concrete_type(sparam_val) || !jl_is_immutable(sparam_val))
sparam_val = NULL;
nval[i + 1] = (void*)sparam_val;
}
int permanent =
(result_type == jl_voidpointer_type) ||
jl_is_concrete_type(fobj) ||
(((jl_datatype_t*)jl_typeof(fobj))->instance == fobj);
if (jl_is_unionall(fobj)) {
jl_value_t *uw = jl_unwrap_unionall(fobj);
if (jl_is_datatype(uw) && ((jl_datatype_t*)uw)->name->wrapper == fobj)
permanent = true;
}
if (permanent) {
result = jl_gc_permobj(sizeof(jl_taggedvalue_t) + jl_datatype_size(result_type), result_type);
memset(result, 0, jl_datatype_size(result_type));
}
else {
result = jl_new_struct_uninit(result_type);
}
if (result_type != jl_voidpointer_type) {
assert(jl_datatype_size(result_type) == sizeof(void*) * 4);
((void**)result)[1] = (void*)fobj;
}
if (!permanent) {
jl_task_t *ct = jl_current_task;
jl_gc_add_ptr_finalizer(ct->ptls, result, (void*)(uintptr_t)&trampoline_deleter);
((void**)result)[2] = (void*)cache;
((void**)result)[3] = (void*)nval;
}
}
JL_CATCH {
free(nval);
jl_rethrow();
}
uv_mutex_lock(&trampoline_lock);
tramp = trampoline_alloc();
((void**)result)[0] = tramp;
init_trampoline(tramp, nval);
ptrhash_put(cache, (void*)fobj, result);
uv_mutex_unlock(&trampoline_lock);
return result;
}
JL_GCC_IGNORE_STOP
void jl_init_runtime_ccall(void)
{
JL_MUTEX_INIT(&libmap_lock, "libmap_lock");
uv_mutex_init(&trampoline_lock);
}