-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathnative.cpp
103 lines (87 loc) · 3.09 KB
/
native.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
// Copyright (C) 2018 Cornell University
#include "native.h"
#include "class.h"
#include "jni.h"
#include "stack_trace.h"
#include <cstdlib>
#include <dlfcn.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <pthread.h>
#define GC_THREADS
#include <gc.h>
#undef GC_THREADS
static constexpr bool kDebug = false;
// This serves as a cache for native method lookups,
// as well as a way to register methods through JNI.
static std::unordered_map<std::string, void *> native_map;
// Builds a unique identifier for native methods.
// E.g., java.lang.Object#wait(J)V.
//
// This intentionally builds on the format used in the
// JNI method registerNatives();
static std::string BuildJavaNativeFuncKey(jclass cls, // e.g., java.lang.Object
const char *name, // e.g., wait
const char *signature // e.g., (J)V
) {
std::string key;
key += GetJavaClassInfo(cls)->name;
key += '#';
key += name;
key += signature;
return key;
}
void RegisterJavaNativeFunc(jclass cls, // e.g., java.lang.Object
const char *name, // e.g., wait
const char *signature, // e.g., (J)V
void *func) {
if (kDebug)
printf("[runtime] registering native method %s%s\n", name, signature);
auto key = BuildJavaNativeFuncKey(cls, name, signature);
decltype(native_map)::iterator it;
bool success;
tie(it, success) = native_map.emplace(key, func);
if (!success) {
// The native method was already linked; replace it.
it->second = func;
}
}
extern "C" void *
GetJavaNativeFunc(jclass cls, // e.g., java.lang.Object
const char *name, // e.g., wait
const char *signature, // e.g., (J)V
const char *short_symbol, // e.g., Java_java_lang_Object_wait
const char *long_symbol // e.g., Java_java_lang_Object_wait__J
) {
// Check cache.
auto key = BuildJavaNativeFuncKey(cls, name, signature);
auto it = native_map.find(key);
if (it != native_map.end()) {
if (kDebug)
printf("[runtime] found cached native method %s\n", key.c_str());
return it->second;
}
// Search for symbol by short name first, then long name.
for (const char *symbol : {short_symbol, long_symbol}) {
if (void *func = dlsym(RTLD_DEFAULT, symbol)) {
if (kDebug)
printf("[runtime] found native method symbol %s\n", symbol);
native_map.emplace(key, func);
return func;
}
}
fprintf(stderr,
"- - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n"
"Link error.\n"
"The following Java native method is unlinked:\n"
" %s\n"
"Aborting for now.\n"
"- - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n",
short_symbol);
DumpStackTrace();
abort();
}
extern "C" void* __GC_malloc(size_t size) {
return GC_malloc(size);
}