aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/javaClasses.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "memory/oopFactory.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "memory/universe.inline.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "oops/symbol.hpp" aoqi@0: #include "prims/jvm_misc.hpp" aoqi@0: #include "prims/nativeLookup.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: #ifdef TARGET_OS_FAMILY_linux aoqi@0: # include "os_linux.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_solaris aoqi@0: # include "os_solaris.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_windows aoqi@0: # include "os_windows.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_aix aoqi@0: # include "os_aix.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_OS_FAMILY_bsd aoqi@0: # include "os_bsd.inline.hpp" aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: static void mangle_name_on(outputStream* st, Symbol* name, int begin, int end) { aoqi@0: char* bytes = (char*)name->bytes() + begin; aoqi@0: char* end_bytes = (char*)name->bytes() + end; aoqi@0: while (bytes < end_bytes) { aoqi@0: jchar c; aoqi@0: bytes = UTF8::next(bytes, &c); aoqi@0: if (c <= 0x7f && isalnum(c)) { aoqi@0: st->put((char) c); aoqi@0: } else { aoqi@0: if (c == '_') st->print("_1"); aoqi@0: else if (c == '/') st->print("_"); aoqi@0: else if (c == ';') st->print("_2"); aoqi@0: else if (c == '[') st->print("_3"); aoqi@0: else st->print("_%.5x", c); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static void mangle_name_on(outputStream* st, Symbol* name) { aoqi@0: mangle_name_on(st, name, 0, name->utf8_length()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: char* NativeLookup::pure_jni_name(methodHandle method) { aoqi@0: stringStream st; aoqi@0: // Prefix aoqi@0: st.print("Java_"); aoqi@0: // Klass name aoqi@0: mangle_name_on(&st, method->klass_name()); aoqi@0: st.print("_"); aoqi@0: // Method name aoqi@0: mangle_name_on(&st, method->name()); aoqi@0: return st.as_string(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: char* NativeLookup::critical_jni_name(methodHandle method) { aoqi@0: stringStream st; aoqi@0: // Prefix aoqi@0: st.print("JavaCritical_"); aoqi@0: // Klass name aoqi@0: mangle_name_on(&st, method->klass_name()); aoqi@0: st.print("_"); aoqi@0: // Method name aoqi@0: mangle_name_on(&st, method->name()); aoqi@0: return st.as_string(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: char* NativeLookup::long_jni_name(methodHandle method) { aoqi@0: // Signature ignore the wrapping parenteses and the trailing return type aoqi@0: stringStream st; aoqi@0: Symbol* signature = method->signature(); aoqi@0: st.print("__"); aoqi@0: // find ')' aoqi@0: int end; aoqi@0: for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++); aoqi@0: // skip first '(' aoqi@0: mangle_name_on(&st, signature, 1, end); aoqi@0: return st.as_string(); aoqi@0: } aoqi@0: aoqi@0: extern "C" { aoqi@0: void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls); aoqi@0: void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls); aoqi@0: void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass); aoqi@0: void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass); aoqi@0: } aoqi@0: aoqi@0: #define CC (char*) /* cast a literal from (const char*) */ aoqi@0: #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) aoqi@0: aoqi@0: static JNINativeMethod lookup_special_native_methods[] = { aoqi@0: { CC"Java_sun_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterUnsafeMethods) }, aoqi@0: { CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) }, aoqi@0: { CC"Java_sun_misc_Perf_registerNatives", NULL, FN_PTR(JVM_RegisterPerfMethods) }, aoqi@0: { CC"Java_sun_hotspot_WhiteBox_registerNatives", NULL, FN_PTR(JVM_RegisterWhiteBoxMethods) }, aoqi@0: }; aoqi@0: aoqi@0: static address lookup_special_native(char* jni_name) { aoqi@0: int count = sizeof(lookup_special_native_methods) / sizeof(JNINativeMethod); aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: // NB: To ignore the jni prefix and jni postfix strstr is used matching. aoqi@0: if (strstr(jni_name, lookup_special_native_methods[i].name) != NULL) { aoqi@0: return CAST_FROM_FN_PTR(address, lookup_special_native_methods[i].fnPtr); aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: address NativeLookup::lookup_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) { aoqi@0: address entry; aoqi@0: // Compute complete JNI name for style aoqi@0: stringStream st; aoqi@0: if (os_style) os::print_jni_name_prefix_on(&st, args_size); aoqi@0: st.print_raw(pure_name); aoqi@0: st.print_raw(long_name); aoqi@0: if (os_style) os::print_jni_name_suffix_on(&st, args_size); aoqi@0: char* jni_name = st.as_string(); aoqi@0: aoqi@0: // If the loader is null we have a system class, so we attempt a lookup in aoqi@0: // the native Java library. This takes care of any bootstrapping problems. aoqi@0: // Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find aoqi@0: // gets found the first time around - otherwise an infinite loop can occure. This is aoqi@0: // another VM/library dependency aoqi@0: Handle loader(THREAD, method->method_holder()->class_loader()); aoqi@0: if (loader.is_null()) { aoqi@0: entry = lookup_special_native(jni_name); aoqi@0: if (entry == NULL) { aoqi@0: entry = (address) os::dll_lookup(os::native_java_library(), jni_name); aoqi@0: } aoqi@0: if (entry != NULL) { aoqi@0: in_base_library = true; aoqi@0: return entry; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Otherwise call static method findNative in ClassLoader aoqi@0: KlassHandle klass (THREAD, SystemDictionary::ClassLoader_klass()); aoqi@0: Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL); aoqi@0: aoqi@0: JavaValue result(T_LONG); aoqi@0: JavaCalls::call_static(&result, aoqi@0: klass, aoqi@0: vmSymbols::findNative_name(), aoqi@0: vmSymbols::classloader_string_long_signature(), aoqi@0: // Arguments aoqi@0: loader, aoqi@0: name_arg, aoqi@0: CHECK_NULL); aoqi@0: entry = (address) (intptr_t) result.get_jlong(); aoqi@0: aoqi@0: if (entry == NULL) { aoqi@0: // findNative didn't find it, if there are any agent libraries look in them aoqi@0: AgentLibrary* agent; aoqi@0: for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) { aoqi@0: entry = (address) os::dll_lookup(agent->os_lib(), jni_name); aoqi@0: if (entry != NULL) { aoqi@0: return entry; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return entry; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address NativeLookup::lookup_critical_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style) { aoqi@0: if (!method->has_native_function()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: address current_entry = method->native_function(); aoqi@0: aoqi@0: char dll_name[JVM_MAXPATHLEN]; aoqi@0: int offset; aoqi@0: if (os::dll_address_to_library_name(current_entry, dll_name, sizeof(dll_name), &offset)) { aoqi@0: char ebuf[32]; aoqi@0: void* dll = os::dll_load(dll_name, ebuf, sizeof(ebuf)); aoqi@0: if (dll != NULL) { aoqi@0: // Compute complete JNI name for style aoqi@0: stringStream st; aoqi@0: if (os_style) os::print_jni_name_prefix_on(&st, args_size); aoqi@0: st.print_raw(pure_name); aoqi@0: st.print_raw(long_name); aoqi@0: if (os_style) os::print_jni_name_suffix_on(&st, args_size); aoqi@0: char* jni_name = st.as_string(); aoqi@0: return (address)os::dll_lookup(dll, jni_name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Check all the formats of native implementation name to see if there is one aoqi@0: // for the specified method. aoqi@0: address NativeLookup::lookup_entry(methodHandle method, bool& in_base_library, TRAPS) { aoqi@0: address entry = NULL; aoqi@0: in_base_library = false; aoqi@0: // Compute pure name aoqi@0: char* pure_name = pure_jni_name(method); aoqi@0: aoqi@0: // Compute argument size aoqi@0: int args_size = 1 // JNIEnv aoqi@0: + (method->is_static() ? 1 : 0) // class for static methods aoqi@0: + method->size_of_parameters(); // actual parameters aoqi@0: aoqi@0: aoqi@0: // 1) Try JNI short style aoqi@0: entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // Compute long name aoqi@0: char* long_name = long_jni_name(method); aoqi@0: aoqi@0: // 2) Try JNI long style aoqi@0: entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // 3) Try JNI short style without os prefix/suffix aoqi@0: entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // 4) Try JNI long style without os prefix/suffix aoqi@0: entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL); aoqi@0: aoqi@0: return entry; // NULL indicates not found aoqi@0: } aoqi@0: aoqi@0: // Check all the formats of native implementation name to see if there is one aoqi@0: // for the specified method. aoqi@0: address NativeLookup::lookup_critical_entry(methodHandle method) { aoqi@0: if (!CriticalJNINatives) return NULL; aoqi@0: aoqi@0: if (method->is_synchronized() || aoqi@0: !method->is_static()) { aoqi@0: // Only static non-synchronized methods are allowed aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: address entry = NULL; aoqi@0: aoqi@0: Symbol* signature = method->signature(); aoqi@0: for (int end = 0; end < signature->utf8_length(); end++) { aoqi@0: if (signature->byte_at(end) == 'L') { aoqi@0: // Don't allow object types aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Compute critical name aoqi@0: char* critical_name = critical_jni_name(method); aoqi@0: aoqi@0: // Compute argument size aoqi@0: int args_size = 1 // JNIEnv aoqi@0: + (method->is_static() ? 1 : 0) // class for static methods aoqi@0: + method->size_of_parameters(); // actual parameters aoqi@0: aoqi@0: aoqi@0: // 1) Try JNI short style aoqi@0: entry = lookup_critical_style(method, critical_name, "", args_size, true); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // Compute long name aoqi@0: char* long_name = long_jni_name(method); aoqi@0: aoqi@0: // 2) Try JNI long style aoqi@0: entry = lookup_critical_style(method, critical_name, long_name, args_size, true); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // 3) Try JNI short style without os prefix/suffix aoqi@0: entry = lookup_critical_style(method, critical_name, "", args_size, false); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // 4) Try JNI long style without os prefix/suffix aoqi@0: entry = lookup_critical_style(method, critical_name, long_name, args_size, false); aoqi@0: aoqi@0: return entry; // NULL indicates not found aoqi@0: } aoqi@0: aoqi@0: // Check if there are any JVM TI prefixes which have been applied to the native method name. aoqi@0: // If any are found, remove them before attemping the look up of the aoqi@0: // native implementation again. aoqi@0: // See SetNativeMethodPrefix in the JVM TI Spec for more details. aoqi@0: address NativeLookup::lookup_entry_prefixed(methodHandle method, bool& in_base_library, TRAPS) { aoqi@0: #if INCLUDE_JVMTI aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: int prefix_count; aoqi@0: char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count); aoqi@0: char* in_name = method->name()->as_C_string(); aoqi@0: char* wrapper_name = in_name; aoqi@0: // last applied prefix will be first -- go backwards aoqi@0: for (int i = prefix_count-1; i >= 0; i--) { aoqi@0: char* prefix = prefixes[i]; aoqi@0: size_t prefix_len = strlen(prefix); aoqi@0: if (strncmp(prefix, wrapper_name, prefix_len) == 0) { aoqi@0: // has this prefix remove it aoqi@0: wrapper_name += prefix_len; aoqi@0: } aoqi@0: } aoqi@0: if (wrapper_name != in_name) { aoqi@0: // we have a name for a wrapping method aoqi@0: int wrapper_name_len = (int)strlen(wrapper_name); aoqi@0: TempNewSymbol wrapper_symbol = SymbolTable::probe(wrapper_name, wrapper_name_len); aoqi@0: if (wrapper_symbol != NULL) { aoqi@0: KlassHandle kh(method->method_holder()); aoqi@0: Method* wrapper_method = kh()->lookup_method(wrapper_symbol, aoqi@0: method->signature()); aoqi@0: if (wrapper_method != NULL && !wrapper_method->is_native()) { aoqi@0: // we found a wrapper method, use its native entry aoqi@0: method->set_is_prefixed_native(); aoqi@0: return lookup_entry(wrapper_method, in_base_library, THREAD); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif // INCLUDE_JVMTI aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: address NativeLookup::lookup_base(methodHandle method, bool& in_base_library, TRAPS) { aoqi@0: address entry = NULL; aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: entry = lookup_entry(method, in_base_library, THREAD); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // standard native method resolution has failed. Check if there are any aoqi@0: // JVM TI prefixes which have been applied to the native method name. aoqi@0: entry = lookup_entry_prefixed(method, in_base_library, THREAD); aoqi@0: if (entry != NULL) return entry; aoqi@0: aoqi@0: // Native function not found, throw UnsatisfiedLinkError aoqi@0: THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(), aoqi@0: method->name_and_sig_as_C_string()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address NativeLookup::lookup(methodHandle method, bool& in_base_library, TRAPS) { aoqi@0: if (!method->has_native_function()) { aoqi@0: address entry = lookup_base(method, in_base_library, CHECK_NULL); aoqi@0: method->set_native_function(entry, aoqi@0: Method::native_bind_event_is_interesting); aoqi@0: // -verbose:jni printing aoqi@0: if (PrintJNIResolving) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]", aoqi@0: method->method_holder()->external_name(), aoqi@0: method->name()->as_C_string()); aoqi@0: } aoqi@0: } aoqi@0: return method->native_function(); aoqi@0: } aoqi@0: aoqi@0: address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) { aoqi@0: EXCEPTION_MARK; aoqi@0: bool in_base_library = true; // SharedRuntime inits some math methods. aoqi@0: TempNewSymbol c_name = SymbolTable::new_symbol(class_name, CATCH); aoqi@0: TempNewSymbol m_name = SymbolTable::new_symbol(method_name, CATCH); aoqi@0: TempNewSymbol s_name = SymbolTable::new_symbol(signature, CATCH); aoqi@0: aoqi@0: // Find the class aoqi@0: Klass* k = SystemDictionary::resolve_or_fail(c_name, true, CATCH); aoqi@0: instanceKlassHandle klass (THREAD, k); aoqi@0: aoqi@0: // Find method and invoke standard lookup aoqi@0: methodHandle method (THREAD, aoqi@0: klass->uncached_lookup_method(m_name, s_name, Klass::normal)); aoqi@0: address result = lookup(method, in_base_library, CATCH); aoqi@0: assert(in_base_library, "must be in basic library"); aoqi@0: guarantee(result != NULL, "must be non NULL"); aoqi@0: return result; aoqi@0: }