annotate src/share/vm/prims/nativeLookup.cpp @ 13526:f5dd157e3889

8186439: [MVT] ClassFileParser should ignore JVM_ACC_VALUE for class file version < 53.1 Reviewed-by: dsimms
author thartmann
date Mon, 21 Aug 2017 12:26:21 +0200
parents ffee6483b81b
children
rev   line source
duke@0 1 /*
coleenp@12742 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/javaClasses.hpp"
stefank@1879 27 #include "classfile/systemDictionary.hpp"
stefank@1879 28 #include "classfile/vmSymbols.hpp"
stefank@1879 29 #include "memory/oopFactory.hpp"
stefank@1879 30 #include "memory/resourceArea.hpp"
stefank@1879 31 #include "memory/universe.inline.hpp"
stefank@1879 32 #include "oops/instanceKlass.hpp"
coleenp@3602 33 #include "oops/method.hpp"
stefank@1879 34 #include "oops/oop.inline.hpp"
coleenp@2062 35 #include "oops/symbol.hpp"
stefank@1879 36 #include "prims/jvm_misc.hpp"
stefank@1879 37 #include "prims/nativeLookup.hpp"
mikael@10425 38 #include "prims/unsafe.hpp"
stefank@1879 39 #include "runtime/arguments.hpp"
stefank@1879 40 #include "runtime/handles.inline.hpp"
stefank@1879 41 #include "runtime/javaCalls.hpp"
stefank@1879 42 #include "runtime/sharedRuntime.hpp"
stefank@1879 43 #include "runtime/signature.hpp"
jprovino@4107 44 #include "utilities/macros.hpp"
alanb@10420 45 #if INCLUDE_TRACE
alanb@10420 46 #include "trace/traceMacros.hpp"
alanb@10420 47 #endif
duke@0 48
coleenp@2062 49 static void mangle_name_on(outputStream* st, Symbol* name, int begin, int end) {
duke@0 50 char* bytes = (char*)name->bytes() + begin;
duke@0 51 char* end_bytes = (char*)name->bytes() + end;
duke@0 52 while (bytes < end_bytes) {
duke@0 53 jchar c;
duke@0 54 bytes = UTF8::next(bytes, &c);
duke@0 55 if (c <= 0x7f && isalnum(c)) {
duke@0 56 st->put((char) c);
duke@0 57 } else {
duke@0 58 if (c == '_') st->print("_1");
duke@0 59 else if (c == '/') st->print("_");
duke@0 60 else if (c == ';') st->print("_2");
duke@0 61 else if (c == '[') st->print("_3");
duke@0 62 else st->print("_%.5x", c);
duke@0 63 }
duke@0 64 }
duke@0 65 }
duke@0 66
duke@0 67
coleenp@2062 68 static void mangle_name_on(outputStream* st, Symbol* name) {
duke@0 69 mangle_name_on(st, name, 0, name->utf8_length());
duke@0 70 }
duke@0 71
duke@0 72
coleenp@9248 73 char* NativeLookup::pure_jni_name(const methodHandle& method) {
duke@0 74 stringStream st;
duke@0 75 // Prefix
duke@0 76 st.print("Java_");
duke@0 77 // Klass name
duke@0 78 mangle_name_on(&st, method->klass_name());
duke@0 79 st.print("_");
duke@0 80 // Method name
duke@0 81 mangle_name_on(&st, method->name());
duke@0 82 return st.as_string();
duke@0 83 }
duke@0 84
duke@0 85
coleenp@9248 86 char* NativeLookup::critical_jni_name(const methodHandle& method) {
never@3065 87 stringStream st;
never@3065 88 // Prefix
never@3065 89 st.print("JavaCritical_");
never@3065 90 // Klass name
never@3065 91 mangle_name_on(&st, method->klass_name());
never@3065 92 st.print("_");
never@3065 93 // Method name
never@3065 94 mangle_name_on(&st, method->name());
never@3065 95 return st.as_string();
never@3065 96 }
never@3065 97
never@3065 98
coleenp@9248 99 char* NativeLookup::long_jni_name(const methodHandle& method) {
duke@0 100 // Signature ignore the wrapping parenteses and the trailing return type
duke@0 101 stringStream st;
coleenp@2062 102 Symbol* signature = method->signature();
duke@0 103 st.print("__");
duke@0 104 // find ')'
duke@0 105 int end;
duke@0 106 for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++);
duke@0 107 // skip first '('
duke@0 108 mangle_name_on(&st, signature, 1, end);
duke@0 109 return st.as_string();
duke@0 110 }
duke@0 111
duke@0 112 extern "C" {
jrose@710 113 void JNICALL JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass unsafecls);
duke@0 114 void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
mgerdin@3184 115 void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass);
twisti@9111 116 #if INCLUDE_JVMCI
twisti@9111 117 jobject JNICALL JVM_GetJVMCIRuntime(JNIEnv *env, jclass c);
twisti@9111 118 void JNICALL JVM_RegisterJVMCINatives(JNIEnv *env, jclass compilerToVMClass);
twisti@9111 119 #endif
duke@0 120 }
duke@0 121
jrose@2203 122 #define CC (char*) /* cast a literal from (const char*) */
jrose@2203 123 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
jrose@2203 124
jrose@2203 125 static JNINativeMethod lookup_special_native_methods[] = {
psandoz@9769 126 { CC"Java_jdk_internal_misc_Unsafe_registerNatives", NULL, FN_PTR(JVM_RegisterJDKInternalMiscUnsafeMethods) },
jrose@2204 127 { CC"Java_java_lang_invoke_MethodHandleNatives_registerNatives", NULL, FN_PTR(JVM_RegisterMethodHandleMethods) },
chegar@9703 128 { CC"Java_jdk_internal_perf_Perf_registerNatives", NULL, FN_PTR(JVM_RegisterPerfMethods) },
mgerdin@3184 129 { CC"Java_sun_hotspot_WhiteBox_registerNatives", NULL, FN_PTR(JVM_RegisterWhiteBoxMethods) },
twisti@9111 130 #if INCLUDE_JVMCI
twisti@9111 131 { CC"Java_jdk_vm_ci_runtime_JVMCI_initializeRuntime", NULL, FN_PTR(JVM_GetJVMCIRuntime) },
twisti@9111 132 { CC"Java_jdk_vm_ci_hotspot_CompilerToVM_registerNatives", NULL, FN_PTR(JVM_RegisterJVMCINatives) },
twisti@9111 133 #endif
alanb@10420 134 #if INCLUDE_TRACE
alanb@10420 135 { CC"Java_jdk_jfr_internal_JVM_registerNatives", NULL, TRACE_REGISTER_NATIVES },
alanb@10420 136 #endif
jrose@2203 137 };
jrose@2203 138
duke@0 139 static address lookup_special_native(char* jni_name) {
jrose@2203 140 int count = sizeof(lookup_special_native_methods) / sizeof(JNINativeMethod);
mikael@5403 141 for (int i = 0; i < count; i++) {
jrose@2203 142 // NB: To ignore the jni prefix and jni postfix strstr is used matching.
jrose@2203 143 if (strstr(jni_name, lookup_special_native_methods[i].name) != NULL) {
jrose@2203 144 return CAST_FROM_FN_PTR(address, lookup_special_native_methods[i].fnPtr);
duke@0 145 }
duke@0 146 }
duke@0 147 return NULL;
duke@0 148 }
duke@0 149
coleenp@9248 150 address NativeLookup::lookup_style(const methodHandle& method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
duke@0 151 address entry;
duke@0 152 // Compute complete JNI name for style
duke@0 153 stringStream st;
duke@0 154 if (os_style) os::print_jni_name_prefix_on(&st, args_size);
duke@0 155 st.print_raw(pure_name);
duke@0 156 st.print_raw(long_name);
duke@0 157 if (os_style) os::print_jni_name_suffix_on(&st, args_size);
duke@0 158 char* jni_name = st.as_string();
duke@0 159
duke@0 160 // If the loader is null we have a system class, so we attempt a lookup in
duke@0 161 // the native Java library. This takes care of any bootstrapping problems.
duke@0 162 // Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find
duke@0 163 // gets found the first time around - otherwise an infinite loop can occure. This is
duke@0 164 // another VM/library dependency
coleenp@3816 165 Handle loader(THREAD, method->method_holder()->class_loader());
duke@0 166 if (loader.is_null()) {
duke@0 167 entry = lookup_special_native(jni_name);
duke@0 168 if (entry == NULL) {
ikrylov@1887 169 entry = (address) os::dll_lookup(os::native_java_library(), jni_name);
duke@0 170 }
duke@0 171 if (entry != NULL) {
duke@0 172 in_base_library = true;
duke@0 173 return entry;
duke@0 174 }
duke@0 175 }
duke@0 176
duke@0 177 // Otherwise call static method findNative in ClassLoader
coleenp@12742 178 Klass* klass = SystemDictionary::ClassLoader_klass();
duke@0 179 Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
duke@0 180
duke@0 181 JavaValue result(T_LONG);
duke@0 182 JavaCalls::call_static(&result,
duke@0 183 klass,
coleenp@2062 184 vmSymbols::findNative_name(),
coleenp@2062 185 vmSymbols::classloader_string_long_signature(),
duke@0 186 // Arguments
duke@0 187 loader,
duke@0 188 name_arg,
duke@0 189 CHECK_NULL);
duke@0 190 entry = (address) (intptr_t) result.get_jlong();
duke@0 191
duke@0 192 if (entry == NULL) {
duke@0 193 // findNative didn't find it, if there are any agent libraries look in them
duke@0 194 AgentLibrary* agent;
duke@0 195 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
ikrylov@1887 196 entry = (address) os::dll_lookup(agent->os_lib(), jni_name);
duke@0 197 if (entry != NULL) {
duke@0 198 return entry;
duke@0 199 }
duke@0 200 }
duke@0 201 }
duke@0 202
duke@0 203 return entry;
duke@0 204 }
duke@0 205
duke@0 206
coleenp@9248 207 address NativeLookup::lookup_critical_style(const methodHandle& method, char* pure_name, const char* long_name, int args_size, bool os_style) {
never@3065 208 if (!method->has_native_function()) {
never@3065 209 return NULL;
never@3065 210 }
never@3065 211
never@3065 212 address current_entry = method->native_function();
never@3065 213
never@3065 214 char dll_name[JVM_MAXPATHLEN];
never@3065 215 int offset;
never@3065 216 if (os::dll_address_to_library_name(current_entry, dll_name, sizeof(dll_name), &offset)) {
never@3065 217 char ebuf[32];
never@3065 218 void* dll = os::dll_load(dll_name, ebuf, sizeof(ebuf));
never@3065 219 if (dll != NULL) {
never@3065 220 // Compute complete JNI name for style
never@3065 221 stringStream st;
never@3065 222 if (os_style) os::print_jni_name_prefix_on(&st, args_size);
never@3065 223 st.print_raw(pure_name);
never@3065 224 st.print_raw(long_name);
never@3065 225 if (os_style) os::print_jni_name_suffix_on(&st, args_size);
never@3065 226 char* jni_name = st.as_string();
never@3065 227 return (address)os::dll_lookup(dll, jni_name);
never@3065 228 }
never@3065 229 }
never@3065 230
never@3065 231 return NULL;
never@3065 232 }
never@3065 233
never@3065 234
duke@0 235 // Check all the formats of native implementation name to see if there is one
duke@0 236 // for the specified method.
coleenp@9248 237 address NativeLookup::lookup_entry(const methodHandle& method, bool& in_base_library, TRAPS) {
duke@0 238 address entry = NULL;
duke@0 239 in_base_library = false;
duke@0 240 // Compute pure name
duke@0 241 char* pure_name = pure_jni_name(method);
duke@0 242
duke@0 243 // Compute argument size
duke@0 244 int args_size = 1 // JNIEnv
duke@0 245 + (method->is_static() ? 1 : 0) // class for static methods
duke@0 246 + method->size_of_parameters(); // actual parameters
duke@0 247
duke@0 248
duke@0 249 // 1) Try JNI short style
duke@0 250 entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL);
duke@0 251 if (entry != NULL) return entry;
duke@0 252
duke@0 253 // Compute long name
duke@0 254 char* long_name = long_jni_name(method);
duke@0 255
duke@0 256 // 2) Try JNI long style
duke@0 257 entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL);
duke@0 258 if (entry != NULL) return entry;
duke@0 259
duke@0 260 // 3) Try JNI short style without os prefix/suffix
duke@0 261 entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL);
duke@0 262 if (entry != NULL) return entry;
duke@0 263
duke@0 264 // 4) Try JNI long style without os prefix/suffix
duke@0 265 entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
duke@0 266
duke@0 267 return entry; // NULL indicates not found
duke@0 268 }
duke@0 269
never@3065 270 // Check all the formats of native implementation name to see if there is one
never@3065 271 // for the specified method.
coleenp@9248 272 address NativeLookup::lookup_critical_entry(const methodHandle& method) {
never@3065 273 if (!CriticalJNINatives) return NULL;
never@3065 274
never@3065 275 if (method->is_synchronized() ||
never@3065 276 !method->is_static()) {
never@3065 277 // Only static non-synchronized methods are allowed
never@3065 278 return NULL;
never@3065 279 }
never@3065 280
never@3065 281 ResourceMark rm;
never@3065 282 address entry = NULL;
never@3065 283
never@3065 284 Symbol* signature = method->signature();
never@3065 285 for (int end = 0; end < signature->utf8_length(); end++) {
never@3065 286 if (signature->byte_at(end) == 'L') {
never@3065 287 // Don't allow object types
never@3065 288 return NULL;
never@3065 289 }
never@3065 290 }
never@3065 291
never@3065 292 // Compute critical name
never@3065 293 char* critical_name = critical_jni_name(method);
never@3065 294
never@3065 295 // Compute argument size
never@3065 296 int args_size = 1 // JNIEnv
never@3065 297 + (method->is_static() ? 1 : 0) // class for static methods
never@3065 298 + method->size_of_parameters(); // actual parameters
never@3065 299
never@3065 300
never@3065 301 // 1) Try JNI short style
never@3065 302 entry = lookup_critical_style(method, critical_name, "", args_size, true);
never@3065 303 if (entry != NULL) return entry;
never@3065 304
never@3065 305 // Compute long name
never@3065 306 char* long_name = long_jni_name(method);
never@3065 307
never@3065 308 // 2) Try JNI long style
never@3065 309 entry = lookup_critical_style(method, critical_name, long_name, args_size, true);
never@3065 310 if (entry != NULL) return entry;
never@3065 311
never@3065 312 // 3) Try JNI short style without os prefix/suffix
never@3065 313 entry = lookup_critical_style(method, critical_name, "", args_size, false);
never@3065 314 if (entry != NULL) return entry;
never@3065 315
never@3065 316 // 4) Try JNI long style without os prefix/suffix
never@3065 317 entry = lookup_critical_style(method, critical_name, long_name, args_size, false);
never@3065 318
never@3065 319 return entry; // NULL indicates not found
never@3065 320 }
never@3065 321
duke@0 322 // Check if there are any JVM TI prefixes which have been applied to the native method name.
duke@0 323 // If any are found, remove them before attemping the look up of the
duke@0 324 // native implementation again.
duke@0 325 // See SetNativeMethodPrefix in the JVM TI Spec for more details.
coleenp@9248 326 address NativeLookup::lookup_entry_prefixed(const methodHandle& method, bool& in_base_library, TRAPS) {
jprovino@3730 327 #if INCLUDE_JVMTI
duke@0 328 ResourceMark rm(THREAD);
duke@0 329
duke@0 330 int prefix_count;
duke@0 331 char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
duke@0 332 char* in_name = method->name()->as_C_string();
duke@0 333 char* wrapper_name = in_name;
duke@0 334 // last applied prefix will be first -- go backwards
duke@0 335 for (int i = prefix_count-1; i >= 0; i--) {
duke@0 336 char* prefix = prefixes[i];
duke@0 337 size_t prefix_len = strlen(prefix);
duke@0 338 if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
duke@0 339 // has this prefix remove it
duke@0 340 wrapper_name += prefix_len;
duke@0 341 }
duke@0 342 }
duke@0 343 if (wrapper_name != in_name) {
duke@0 344 // we have a name for a wrapping method
duke@0 345 int wrapper_name_len = (int)strlen(wrapper_name);
coleenp@2062 346 TempNewSymbol wrapper_symbol = SymbolTable::probe(wrapper_name, wrapper_name_len);
coleenp@2062 347 if (wrapper_symbol != NULL) {
coleenp@12742 348 Klass* k = method->method_holder();
coleenp@12742 349 Method* wrapper_method = k->lookup_method(wrapper_symbol, method->signature());
duke@0 350 if (wrapper_method != NULL && !wrapper_method->is_native()) {
duke@0 351 // we found a wrapper method, use its native entry
duke@0 352 method->set_is_prefixed_native();
duke@0 353 return lookup_entry(wrapper_method, in_base_library, THREAD);
duke@0 354 }
duke@0 355 }
duke@0 356 }
jprovino@3730 357 #endif // INCLUDE_JVMTI
duke@0 358 return NULL;
duke@0 359 }
duke@0 360
coleenp@9248 361 address NativeLookup::lookup_base(const methodHandle& method, bool& in_base_library, TRAPS) {
duke@0 362 address entry = NULL;
duke@0 363 ResourceMark rm(THREAD);
duke@0 364
duke@0 365 entry = lookup_entry(method, in_base_library, THREAD);
duke@0 366 if (entry != NULL) return entry;
duke@0 367
duke@0 368 // standard native method resolution has failed. Check if there are any
duke@0 369 // JVM TI prefixes which have been applied to the native method name.
duke@0 370 entry = lookup_entry_prefixed(method, in_base_library, THREAD);
duke@0 371 if (entry != NULL) return entry;
duke@0 372
duke@0 373 // Native function not found, throw UnsatisfiedLinkError
duke@0 374 THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(),
duke@0 375 method->name_and_sig_as_C_string());
duke@0 376 }
duke@0 377
duke@0 378
coleenp@9248 379 address NativeLookup::lookup(const methodHandle& method, bool& in_base_library, TRAPS) {
duke@0 380 if (!method->has_native_function()) {
twisti@4673 381 address entry = lookup_base(method, in_base_library, CHECK_NULL);
duke@0 382 method->set_native_function(entry,
coleenp@3602 383 Method::native_bind_event_is_interesting);
duke@0 384 // -verbose:jni printing
duke@0 385 if (PrintJNIResolving) {
duke@0 386 ResourceMark rm(THREAD);
duke@0 387 tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]",
coleenp@3816 388 method->method_holder()->external_name(),
duke@0 389 method->name()->as_C_string());
duke@0 390 }
duke@0 391 }
duke@0 392 return method->native_function();
duke@0 393 }
duke@0 394
duke@0 395 address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
duke@0 396 EXCEPTION_MARK;
duke@0 397 bool in_base_library = true; // SharedRuntime inits some math methods.
coleenp@2062 398 TempNewSymbol c_name = SymbolTable::new_symbol(class_name, CATCH);
coleenp@2062 399 TempNewSymbol m_name = SymbolTable::new_symbol(method_name, CATCH);
coleenp@2062 400 TempNewSymbol s_name = SymbolTable::new_symbol(signature, CATCH);
duke@0 401
duke@0 402 // Find the class
coleenp@3602 403 Klass* k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
coleenp@12742 404 InstanceKlass* klass = InstanceKlass::cast(k);
duke@0 405
duke@0 406 // Find method and invoke standard lookup
duke@0 407 methodHandle method (THREAD,
lfoltan@7759 408 klass->uncached_lookup_method(m_name, s_name, Klass::find_overpass));
duke@0 409 address result = lookup(method, in_base_library, CATCH);
duke@0 410 assert(in_base_library, "must be in basic library");
duke@0 411 guarantee(result != NULL, "must be non NULL");
duke@0 412 return result;
duke@0 413 }