1
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
# include "incls/_precompiled.incl"
|
|
26 |
# include "incls/_nativeLookup.cpp.incl"
|
|
27 |
|
|
28 |
|
|
29 |
static void mangle_name_on(outputStream* st, symbolOop name, int begin, int end) {
|
|
30 |
char* bytes = (char*)name->bytes() + begin;
|
|
31 |
char* end_bytes = (char*)name->bytes() + end;
|
|
32 |
while (bytes < end_bytes) {
|
|
33 |
jchar c;
|
|
34 |
bytes = UTF8::next(bytes, &c);
|
|
35 |
if (c <= 0x7f && isalnum(c)) {
|
|
36 |
st->put((char) c);
|
|
37 |
} else {
|
|
38 |
if (c == '_') st->print("_1");
|
|
39 |
else if (c == '/') st->print("_");
|
|
40 |
else if (c == ';') st->print("_2");
|
|
41 |
else if (c == '[') st->print("_3");
|
|
42 |
else st->print("_%.5x", c);
|
|
43 |
}
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
|
|
48 |
static void mangle_name_on(outputStream* st, symbolOop name) {
|
|
49 |
mangle_name_on(st, name, 0, name->utf8_length());
|
|
50 |
}
|
|
51 |
|
|
52 |
|
|
53 |
char* NativeLookup::pure_jni_name(methodHandle method) {
|
|
54 |
stringStream st;
|
|
55 |
// Prefix
|
|
56 |
st.print("Java_");
|
|
57 |
// Klass name
|
|
58 |
mangle_name_on(&st, method->klass_name());
|
|
59 |
st.print("_");
|
|
60 |
// Method name
|
|
61 |
mangle_name_on(&st, method->name());
|
|
62 |
return st.as_string();
|
|
63 |
}
|
|
64 |
|
|
65 |
|
|
66 |
char* NativeLookup::long_jni_name(methodHandle method) {
|
|
67 |
// Signature ignore the wrapping parenteses and the trailing return type
|
|
68 |
stringStream st;
|
|
69 |
symbolOop signature = method->signature();
|
|
70 |
st.print("__");
|
|
71 |
// find ')'
|
|
72 |
int end;
|
|
73 |
for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++);
|
|
74 |
// skip first '('
|
|
75 |
mangle_name_on(&st, signature, 1, end);
|
|
76 |
return st.as_string();
|
|
77 |
}
|
|
78 |
|
|
79 |
extern "C" {
|
|
80 |
void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls);
|
|
81 |
void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
|
|
82 |
}
|
|
83 |
|
|
84 |
static address lookup_special_native(char* jni_name) {
|
|
85 |
// NB: To ignore the jni prefix and jni postfix strstr is used matching.
|
|
86 |
if (!JDK_Version::is_gte_jdk14x_version()) {
|
|
87 |
// These functions only exist for compatibility with 1.3.1 and earlier
|
|
88 |
// Intercept ObjectOutputStream getPrimitiveFieldValues for faster serialization
|
|
89 |
if (strstr(jni_name, "Java_java_io_ObjectOutputStream_getPrimitiveFieldValues") != NULL) {
|
|
90 |
return CAST_FROM_FN_PTR(address, JVM_GetPrimitiveFieldValues);
|
|
91 |
}
|
|
92 |
// Intercept ObjectInputStream setPrimitiveFieldValues for faster serialization
|
|
93 |
if (strstr(jni_name, "Java_java_io_ObjectInputStream_setPrimitiveFieldValues") != NULL) {
|
|
94 |
return CAST_FROM_FN_PTR(address, JVM_SetPrimitiveFieldValues);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
if (strstr(jni_name, "Java_sun_misc_Unsafe_registerNatives") != NULL) {
|
|
98 |
return CAST_FROM_FN_PTR(address, JVM_RegisterUnsafeMethods);
|
|
99 |
}
|
|
100 |
if (strstr(jni_name, "Java_sun_misc_Perf_registerNatives") != NULL) {
|
|
101 |
return CAST_FROM_FN_PTR(address, JVM_RegisterPerfMethods);
|
|
102 |
}
|
|
103 |
|
|
104 |
return NULL;
|
|
105 |
}
|
|
106 |
|
|
107 |
address NativeLookup::lookup_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
|
|
108 |
address entry;
|
|
109 |
// Compute complete JNI name for style
|
|
110 |
stringStream st;
|
|
111 |
if (os_style) os::print_jni_name_prefix_on(&st, args_size);
|
|
112 |
st.print_raw(pure_name);
|
|
113 |
st.print_raw(long_name);
|
|
114 |
if (os_style) os::print_jni_name_suffix_on(&st, args_size);
|
|
115 |
char* jni_name = st.as_string();
|
|
116 |
|
|
117 |
// If the loader is null we have a system class, so we attempt a lookup in
|
|
118 |
// the native Java library. This takes care of any bootstrapping problems.
|
|
119 |
// Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find
|
|
120 |
// gets found the first time around - otherwise an infinite loop can occure. This is
|
|
121 |
// another VM/library dependency
|
|
122 |
Handle loader(THREAD,
|
|
123 |
instanceKlass::cast(method->method_holder())->class_loader());
|
|
124 |
if (loader.is_null()) {
|
|
125 |
entry = lookup_special_native(jni_name);
|
|
126 |
if (entry == NULL) {
|
|
127 |
entry = (address) hpi::dll_lookup(os::native_java_library(), jni_name);
|
|
128 |
}
|
|
129 |
if (entry != NULL) {
|
|
130 |
in_base_library = true;
|
|
131 |
return entry;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
// Otherwise call static method findNative in ClassLoader
|
|
136 |
KlassHandle klass (THREAD, SystemDictionary::classloader_klass());
|
|
137 |
Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
|
|
138 |
|
|
139 |
JavaValue result(T_LONG);
|
|
140 |
JavaCalls::call_static(&result,
|
|
141 |
klass,
|
|
142 |
vmSymbolHandles::findNative_name(),
|
|
143 |
vmSymbolHandles::classloader_string_long_signature(),
|
|
144 |
// Arguments
|
|
145 |
loader,
|
|
146 |
name_arg,
|
|
147 |
CHECK_NULL);
|
|
148 |
entry = (address) (intptr_t) result.get_jlong();
|
|
149 |
|
|
150 |
if (entry == NULL) {
|
|
151 |
// findNative didn't find it, if there are any agent libraries look in them
|
|
152 |
AgentLibrary* agent;
|
|
153 |
for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
|
|
154 |
entry = (address) hpi::dll_lookup(agent->os_lib(), jni_name);
|
|
155 |
if (entry != NULL) {
|
|
156 |
return entry;
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
return entry;
|
|
162 |
}
|
|
163 |
|
|
164 |
|
|
165 |
// Check all the formats of native implementation name to see if there is one
|
|
166 |
// for the specified method.
|
|
167 |
address NativeLookup::lookup_entry(methodHandle method, bool& in_base_library, TRAPS) {
|
|
168 |
address entry = NULL;
|
|
169 |
in_base_library = false;
|
|
170 |
// Compute pure name
|
|
171 |
char* pure_name = pure_jni_name(method);
|
|
172 |
|
|
173 |
// Compute argument size
|
|
174 |
int args_size = 1 // JNIEnv
|
|
175 |
+ (method->is_static() ? 1 : 0) // class for static methods
|
|
176 |
+ method->size_of_parameters(); // actual parameters
|
|
177 |
|
|
178 |
|
|
179 |
// 1) Try JNI short style
|
|
180 |
entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL);
|
|
181 |
if (entry != NULL) return entry;
|
|
182 |
|
|
183 |
// Compute long name
|
|
184 |
char* long_name = long_jni_name(method);
|
|
185 |
|
|
186 |
// 2) Try JNI long style
|
|
187 |
entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL);
|
|
188 |
if (entry != NULL) return entry;
|
|
189 |
|
|
190 |
// 3) Try JNI short style without os prefix/suffix
|
|
191 |
entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL);
|
|
192 |
if (entry != NULL) return entry;
|
|
193 |
|
|
194 |
// 4) Try JNI long style without os prefix/suffix
|
|
195 |
entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
|
|
196 |
|
|
197 |
return entry; // NULL indicates not found
|
|
198 |
}
|
|
199 |
|
|
200 |
// Check if there are any JVM TI prefixes which have been applied to the native method name.
|
|
201 |
// If any are found, remove them before attemping the look up of the
|
|
202 |
// native implementation again.
|
|
203 |
// See SetNativeMethodPrefix in the JVM TI Spec for more details.
|
|
204 |
address NativeLookup::lookup_entry_prefixed(methodHandle method, bool& in_base_library, TRAPS) {
|
|
205 |
ResourceMark rm(THREAD);
|
|
206 |
|
|
207 |
int prefix_count;
|
|
208 |
char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
|
|
209 |
char* in_name = method->name()->as_C_string();
|
|
210 |
char* wrapper_name = in_name;
|
|
211 |
// last applied prefix will be first -- go backwards
|
|
212 |
for (int i = prefix_count-1; i >= 0; i--) {
|
|
213 |
char* prefix = prefixes[i];
|
|
214 |
size_t prefix_len = strlen(prefix);
|
|
215 |
if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
|
|
216 |
// has this prefix remove it
|
|
217 |
wrapper_name += prefix_len;
|
|
218 |
}
|
|
219 |
}
|
|
220 |
if (wrapper_name != in_name) {
|
|
221 |
// we have a name for a wrapping method
|
|
222 |
int wrapper_name_len = (int)strlen(wrapper_name);
|
|
223 |
symbolHandle wrapper_symbol(THREAD, SymbolTable::probe(wrapper_name, wrapper_name_len));
|
|
224 |
if (!wrapper_symbol.is_null()) {
|
|
225 |
KlassHandle kh(method->method_holder());
|
|
226 |
methodOop wrapper_method = Klass::cast(kh())->lookup_method(wrapper_symbol(),
|
|
227 |
method->signature());
|
|
228 |
if (wrapper_method != NULL && !wrapper_method->is_native()) {
|
|
229 |
// we found a wrapper method, use its native entry
|
|
230 |
method->set_is_prefixed_native();
|
|
231 |
return lookup_entry(wrapper_method, in_base_library, THREAD);
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
return NULL;
|
|
236 |
}
|
|
237 |
|
|
238 |
address NativeLookup::lookup_base(methodHandle method, bool& in_base_library, TRAPS) {
|
|
239 |
address entry = NULL;
|
|
240 |
ResourceMark rm(THREAD);
|
|
241 |
|
|
242 |
entry = lookup_entry(method, in_base_library, THREAD);
|
|
243 |
if (entry != NULL) return entry;
|
|
244 |
|
|
245 |
// standard native method resolution has failed. Check if there are any
|
|
246 |
// JVM TI prefixes which have been applied to the native method name.
|
|
247 |
entry = lookup_entry_prefixed(method, in_base_library, THREAD);
|
|
248 |
if (entry != NULL) return entry;
|
|
249 |
|
|
250 |
// Native function not found, throw UnsatisfiedLinkError
|
|
251 |
THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(),
|
|
252 |
method->name_and_sig_as_C_string());
|
|
253 |
}
|
|
254 |
|
|
255 |
|
|
256 |
address NativeLookup::lookup(methodHandle method, bool& in_base_library, TRAPS) {
|
|
257 |
if (!method->has_native_function()) {
|
|
258 |
address entry = lookup_base(method, in_base_library, CHECK_NULL);
|
|
259 |
method->set_native_function(entry,
|
|
260 |
methodOopDesc::native_bind_event_is_interesting);
|
|
261 |
// -verbose:jni printing
|
|
262 |
if (PrintJNIResolving) {
|
|
263 |
ResourceMark rm(THREAD);
|
|
264 |
tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]",
|
|
265 |
Klass::cast(method->method_holder())->external_name(),
|
|
266 |
method->name()->as_C_string());
|
|
267 |
}
|
|
268 |
}
|
|
269 |
return method->native_function();
|
|
270 |
}
|
|
271 |
|
|
272 |
address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
|
|
273 |
EXCEPTION_MARK;
|
|
274 |
bool in_base_library = true; // SharedRuntime inits some math methods.
|
|
275 |
symbolHandle c_name = oopFactory::new_symbol_handle(class_name, CATCH);
|
|
276 |
symbolHandle m_name = oopFactory::new_symbol_handle(method_name, CATCH);
|
|
277 |
symbolHandle s_name = oopFactory::new_symbol_handle(signature, CATCH);
|
|
278 |
|
|
279 |
// Find the class
|
|
280 |
klassOop k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
|
|
281 |
instanceKlassHandle klass (THREAD, k);
|
|
282 |
|
|
283 |
// Find method and invoke standard lookup
|
|
284 |
methodHandle method (THREAD,
|
|
285 |
klass->uncached_lookup_method(m_name(), s_name()));
|
|
286 |
address result = lookup(method, in_base_library, CATCH);
|
|
287 |
assert(in_base_library, "must be in basic library");
|
|
288 |
guarantee(result != NULL, "must be non NULL");
|
|
289 |
return result;
|
|
290 |
}
|