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