src/share/vm/prims/nativeLookup.cpp

Wed, 08 Apr 2009 10:56:49 -0700

author
jrose
date
Wed, 08 Apr 2009 10:56:49 -0700
changeset 1145
e5b0439ef4ae
parent 435
a61af66fc99e
child 1577
4ce7240d622c
permissions
-rw-r--r--

6655638: dynamic languages need method handles
Summary: initial implementation, with known omissions (x86/64, sparc, compiler optim., c-oops, C++ interp.)
Reviewed-by: kvn, twisti, never

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

mercurial