src/share/vm/prims/nativeLookup.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
parent 8716
619700f41f8e
child 9931
fd44df5e3bc3
child 10006
d0f692037e7b
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

     1 /*
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "classfile/javaClasses.hpp"
    27 #include "classfile/systemDictionary.hpp"
    28 #include "classfile/vmSymbols.hpp"
    29 #include "memory/oopFactory.hpp"
    30 #include "memory/resourceArea.hpp"
    31 #include "memory/universe.inline.hpp"
    32 #include "oops/instanceKlass.hpp"
    33 #include "oops/method.hpp"
    34 #include "oops/oop.inline.hpp"
    35 #include "oops/symbol.hpp"
    36 #include "prims/jvm_misc.hpp"
    37 #include "prims/nativeLookup.hpp"
    38 #include "runtime/arguments.hpp"
    39 #include "runtime/handles.inline.hpp"
    40 #include "runtime/javaCalls.hpp"
    41 #include "runtime/sharedRuntime.hpp"
    42 #include "runtime/signature.hpp"
    43 #include "utilities/macros.hpp"
    44 #if INCLUDE_JFR
    45 #include "jfr/jfr.hpp"
    46 #endif
    47 #ifdef TARGET_OS_FAMILY_linux
    48 # include "os_linux.inline.hpp"
    49 #endif
    50 #ifdef TARGET_OS_FAMILY_solaris
    51 # include "os_solaris.inline.hpp"
    52 #endif
    53 #ifdef TARGET_OS_FAMILY_windows
    54 # include "os_windows.inline.hpp"
    55 #endif
    56 #ifdef TARGET_OS_FAMILY_aix
    57 # include "os_aix.inline.hpp"
    58 #endif
    59 #ifdef TARGET_OS_FAMILY_bsd
    60 # include "os_bsd.inline.hpp"
    61 #endif
    64 static void mangle_name_on(outputStream* st, Symbol* name, int begin, int end) {
    65   char* bytes = (char*)name->bytes() + begin;
    66   char* end_bytes = (char*)name->bytes() + end;
    67   while (bytes < end_bytes) {
    68     jchar c;
    69     bytes = UTF8::next(bytes, &c);
    70     if (c <= 0x7f && isalnum(c)) {
    71       st->put((char) c);
    72     } else {
    73            if (c == '_') st->print("_1");
    74       else if (c == '/') st->print("_");
    75       else if (c == ';') st->print("_2");
    76       else if (c == '[') st->print("_3");
    77       else               st->print("_%.5x", c);
    78     }
    79   }
    80 }
    83 static void mangle_name_on(outputStream* st, Symbol* name) {
    84   mangle_name_on(st, name, 0, name->utf8_length());
    85 }
    88 char* NativeLookup::pure_jni_name(methodHandle method) {
    89   stringStream st;
    90   // Prefix
    91   st.print("Java_");
    92   // Klass name
    93   mangle_name_on(&st, method->klass_name());
    94   st.print("_");
    95   // Method name
    96   mangle_name_on(&st, method->name());
    97   return st.as_string();
    98 }
   101 char* NativeLookup::critical_jni_name(methodHandle method) {
   102   stringStream st;
   103   // Prefix
   104   st.print("JavaCritical_");
   105   // Klass name
   106   mangle_name_on(&st, method->klass_name());
   107   st.print("_");
   108   // Method name
   109   mangle_name_on(&st, method->name());
   110   return st.as_string();
   111 }
   114 char* NativeLookup::long_jni_name(methodHandle method) {
   115   // Signature ignore the wrapping parenteses and the trailing return type
   116   stringStream st;
   117   Symbol* signature = method->signature();
   118   st.print("__");
   119   // find ')'
   120   int end;
   121   for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++);
   122   // skip first '('
   123   mangle_name_on(&st, signature, 1, end);
   124   return st.as_string();
   125 }
   127 extern "C" {
   128   void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls);
   129   void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls);
   130   void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
   131   void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass);
   132 }
   134 #define CC (char*)  /* cast a literal from (const char*) */
   135 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
   137 static JNINativeMethod lookup_special_native_methods[] = {
   138   { CC"Java_sun_misc_Unsafe_registerNatives",                      NULL, FN_PTR(JVM_RegisterUnsafeMethods)       },
   139   { CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) },
   140   { CC"Java_sun_misc_Perf_registerNatives",                        NULL, FN_PTR(JVM_RegisterPerfMethods)         },
   141   { CC"Java_sun_hotspot_WhiteBox_registerNatives",                 NULL, FN_PTR(JVM_RegisterWhiteBoxMethods)     },
   142 #if INCLUDE_JFR
   143   { CC"Java_jdk_jfr_internal_JVM_registerNatives",                 NULL, FN_PTR(jfr_register_natives)            },
   144 #endif
   145 };
   147 static address lookup_special_native(char* jni_name) {
   148   int count = sizeof(lookup_special_native_methods) / sizeof(JNINativeMethod);
   149   for (int i = 0; i < count; i++) {
   150     // NB: To ignore the jni prefix and jni postfix strstr is used matching.
   151     if (strstr(jni_name, lookup_special_native_methods[i].name) != NULL) {
   152       return CAST_FROM_FN_PTR(address, lookup_special_native_methods[i].fnPtr);
   153     }
   154   }
   155   return NULL;
   156 }
   158 address NativeLookup::lookup_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
   159   address entry;
   160   // Compute complete JNI name for style
   161   stringStream st;
   162   if (os_style) os::print_jni_name_prefix_on(&st, args_size);
   163   st.print_raw(pure_name);
   164   st.print_raw(long_name);
   165   if (os_style) os::print_jni_name_suffix_on(&st, args_size);
   166   char* jni_name = st.as_string();
   168   // If the loader is null we have a system class, so we attempt a lookup in
   169   // the native Java library. This takes care of any bootstrapping problems.
   170   // Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find
   171   // gets found the first time around - otherwise an infinite loop can occure. This is
   172   // another VM/library dependency
   173   Handle loader(THREAD, method->method_holder()->class_loader());
   174   if (loader.is_null()) {
   175     entry = lookup_special_native(jni_name);
   176     if (entry == NULL) {
   177        entry = (address) os::dll_lookup(os::native_java_library(), jni_name);
   178     }
   179     if (entry != NULL) {
   180       in_base_library = true;
   181       return entry;
   182     }
   183   }
   185   // Otherwise call static method findNative in ClassLoader
   186   KlassHandle   klass (THREAD, SystemDictionary::ClassLoader_klass());
   187   Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
   189   JavaValue result(T_LONG);
   190   JavaCalls::call_static(&result,
   191                          klass,
   192                          vmSymbols::findNative_name(),
   193                          vmSymbols::classloader_string_long_signature(),
   194                          // Arguments
   195                          loader,
   196                          name_arg,
   197                          CHECK_NULL);
   198   entry = (address) (intptr_t) result.get_jlong();
   200   if (entry == NULL) {
   201     // findNative didn't find it, if there are any agent libraries look in them
   202     AgentLibrary* agent;
   203     for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
   204       entry = (address) os::dll_lookup(agent->os_lib(), jni_name);
   205       if (entry != NULL) {
   206         return entry;
   207       }
   208     }
   209   }
   211   return entry;
   212 }
   215 address NativeLookup::lookup_critical_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style) {
   216   if (!method->has_native_function()) {
   217     return NULL;
   218   }
   220   address current_entry = method->native_function();
   222   char dll_name[JVM_MAXPATHLEN];
   223   int offset;
   224   if (os::dll_address_to_library_name(current_entry, dll_name, sizeof(dll_name), &offset)) {
   225     char ebuf[32];
   226     void* dll = os::dll_load(dll_name, ebuf, sizeof(ebuf));
   227     if (dll != NULL) {
   228       // Compute complete JNI name for style
   229       stringStream st;
   230       if (os_style) os::print_jni_name_prefix_on(&st, args_size);
   231       st.print_raw(pure_name);
   232       st.print_raw(long_name);
   233       if (os_style) os::print_jni_name_suffix_on(&st, args_size);
   234       char* jni_name = st.as_string();
   235       return (address)os::dll_lookup(dll, jni_name);
   236     }
   237   }
   239   return NULL;
   240 }
   243 // Check all the formats of native implementation name to see if there is one
   244 // for the specified method.
   245 address NativeLookup::lookup_entry(methodHandle method, bool& in_base_library, TRAPS) {
   246   address entry = NULL;
   247   in_base_library = false;
   248   // Compute pure name
   249   char* pure_name = pure_jni_name(method);
   251   // Compute argument size
   252   int args_size = 1                             // JNIEnv
   253                 + (method->is_static() ? 1 : 0) // class for static methods
   254                 + method->size_of_parameters(); // actual parameters
   257   // 1) Try JNI short style
   258   entry = lookup_style(method, pure_name, "",        args_size, true,  in_base_library, CHECK_NULL);
   259   if (entry != NULL) return entry;
   261   // Compute long name
   262   char* long_name = long_jni_name(method);
   264   // 2) Try JNI long style
   265   entry = lookup_style(method, pure_name, long_name, args_size, true,  in_base_library, CHECK_NULL);
   266   if (entry != NULL) return entry;
   268   // 3) Try JNI short style without os prefix/suffix
   269   entry = lookup_style(method, pure_name, "",        args_size, false, in_base_library, CHECK_NULL);
   270   if (entry != NULL) return entry;
   272   // 4) Try JNI long style without os prefix/suffix
   273   entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
   275   return entry; // NULL indicates not found
   276 }
   278 // Check all the formats of native implementation name to see if there is one
   279 // for the specified method.
   280 address NativeLookup::lookup_critical_entry(methodHandle method) {
   281   if (!CriticalJNINatives) return NULL;
   283   if (method->is_synchronized() ||
   284       !method->is_static()) {
   285     // Only static non-synchronized methods are allowed
   286     return NULL;
   287   }
   289   ResourceMark rm;
   290   address entry = NULL;
   292   Symbol* signature = method->signature();
   293   for (int end = 0; end < signature->utf8_length(); end++) {
   294     if (signature->byte_at(end) == 'L') {
   295       // Don't allow object types
   296       return NULL;
   297     }
   298   }
   300   // Compute critical name
   301   char* critical_name = critical_jni_name(method);
   303   // Compute argument size
   304   int args_size = 1                             // JNIEnv
   305                 + (method->is_static() ? 1 : 0) // class for static methods
   306                 + method->size_of_parameters(); // actual parameters
   309   // 1) Try JNI short style
   310   entry = lookup_critical_style(method, critical_name, "",        args_size, true);
   311   if (entry != NULL) return entry;
   313   // Compute long name
   314   char* long_name = long_jni_name(method);
   316   // 2) Try JNI long style
   317   entry = lookup_critical_style(method, critical_name, long_name, args_size, true);
   318   if (entry != NULL) return entry;
   320   // 3) Try JNI short style without os prefix/suffix
   321   entry = lookup_critical_style(method, critical_name, "",        args_size, false);
   322   if (entry != NULL) return entry;
   324   // 4) Try JNI long style without os prefix/suffix
   325   entry = lookup_critical_style(method, critical_name, long_name, args_size, false);
   327   return entry; // NULL indicates not found
   328 }
   330 // Check if there are any JVM TI prefixes which have been applied to the native method name.
   331 // If any are found, remove them before attemping the look up of the
   332 // native implementation again.
   333 // See SetNativeMethodPrefix in the JVM TI Spec for more details.
   334 address NativeLookup::lookup_entry_prefixed(methodHandle method, bool& in_base_library, TRAPS) {
   335 #if INCLUDE_JVMTI
   336   ResourceMark rm(THREAD);
   338   int prefix_count;
   339   char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
   340   char* in_name = method->name()->as_C_string();
   341   char* wrapper_name = in_name;
   342   // last applied prefix will be first -- go backwards
   343   for (int i = prefix_count-1; i >= 0; i--) {
   344     char* prefix = prefixes[i];
   345     size_t prefix_len = strlen(prefix);
   346     if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
   347       // has this prefix remove it
   348       wrapper_name += prefix_len;
   349     }
   350   }
   351   if (wrapper_name != in_name) {
   352     // we have a name for a wrapping method
   353     int wrapper_name_len = (int)strlen(wrapper_name);
   354     TempNewSymbol wrapper_symbol = SymbolTable::probe(wrapper_name, wrapper_name_len);
   355     if (wrapper_symbol != NULL) {
   356       KlassHandle kh(method->method_holder());
   357       Method* wrapper_method = kh()->lookup_method(wrapper_symbol,
   358                                                                   method->signature());
   359       if (wrapper_method != NULL && !wrapper_method->is_native()) {
   360         // we found a wrapper method, use its native entry
   361         method->set_is_prefixed_native();
   362         return lookup_entry(wrapper_method, in_base_library, THREAD);
   363       }
   364     }
   365   }
   366 #endif // INCLUDE_JVMTI
   367   return NULL;
   368 }
   370 address NativeLookup::lookup_base(methodHandle method, bool& in_base_library, TRAPS) {
   371   address entry = NULL;
   372   ResourceMark rm(THREAD);
   374   entry = lookup_entry(method, in_base_library, THREAD);
   375   if (entry != NULL) return entry;
   377   // standard native method resolution has failed.  Check if there are any
   378   // JVM TI prefixes which have been applied to the native method name.
   379   entry = lookup_entry_prefixed(method, in_base_library, THREAD);
   380   if (entry != NULL) return entry;
   382   // Native function not found, throw UnsatisfiedLinkError
   383   THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(),
   384               method->name_and_sig_as_C_string());
   385 }
   388 address NativeLookup::lookup(methodHandle method, bool& in_base_library, TRAPS) {
   389   if (!method->has_native_function()) {
   390     address entry = lookup_base(method, in_base_library, CHECK_NULL);
   391     method->set_native_function(entry,
   392       Method::native_bind_event_is_interesting);
   393     // -verbose:jni printing
   394     if (PrintJNIResolving) {
   395       ResourceMark rm(THREAD);
   396       tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]",
   397         method->method_holder()->external_name(),
   398         method->name()->as_C_string());
   399     }
   400   }
   401   return method->native_function();
   402 }
   404 address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
   405   EXCEPTION_MARK;
   406   bool in_base_library = true;  // SharedRuntime inits some math methods.
   407   TempNewSymbol c_name = SymbolTable::new_symbol(class_name,  CATCH);
   408   TempNewSymbol m_name = SymbolTable::new_symbol(method_name, CATCH);
   409   TempNewSymbol s_name = SymbolTable::new_symbol(signature,   CATCH);
   411   // Find the class
   412   Klass* k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
   413   instanceKlassHandle klass (THREAD, k);
   415   // Find method and invoke standard lookup
   416   methodHandle method (THREAD,
   417                        klass->uncached_lookup_method(m_name, s_name, Klass::find_overpass));
   418   address result = lookup(method, in_base_library, CATCH);
   419   assert(in_base_library, "must be in basic library");
   420   guarantee(result != NULL, "must be non NULL");
   421   return result;
   422 }

mercurial