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