src/share/vm/prims/nativeLookup.cpp

Tue, 06 Jan 2009 07:05:05 -0800

author
jmasa
date
Tue, 06 Jan 2009 07:05:05 -0800
changeset 952
e9be0e04635a
parent 435
a61af66fc99e
child 1145
e5b0439ef4ae
permissions
-rw-r--r--

6689653: JMapPerm fails with UseConcMarkSweepIncGC and compressed oops off
Summary: Added safe_object_iterate() for use by JMapPerm.
Reviewed-by: tonyp

duke@435 1 /*
duke@435 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_nativeLookup.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 static void mangle_name_on(outputStream* st, symbolOop name, int begin, int end) {
duke@435 30 char* bytes = (char*)name->bytes() + begin;
duke@435 31 char* end_bytes = (char*)name->bytes() + end;
duke@435 32 while (bytes < end_bytes) {
duke@435 33 jchar c;
duke@435 34 bytes = UTF8::next(bytes, &c);
duke@435 35 if (c <= 0x7f && isalnum(c)) {
duke@435 36 st->put((char) c);
duke@435 37 } else {
duke@435 38 if (c == '_') st->print("_1");
duke@435 39 else if (c == '/') st->print("_");
duke@435 40 else if (c == ';') st->print("_2");
duke@435 41 else if (c == '[') st->print("_3");
duke@435 42 else st->print("_%.5x", c);
duke@435 43 }
duke@435 44 }
duke@435 45 }
duke@435 46
duke@435 47
duke@435 48 static void mangle_name_on(outputStream* st, symbolOop name) {
duke@435 49 mangle_name_on(st, name, 0, name->utf8_length());
duke@435 50 }
duke@435 51
duke@435 52
duke@435 53 char* NativeLookup::pure_jni_name(methodHandle method) {
duke@435 54 stringStream st;
duke@435 55 // Prefix
duke@435 56 st.print("Java_");
duke@435 57 // Klass name
duke@435 58 mangle_name_on(&st, method->klass_name());
duke@435 59 st.print("_");
duke@435 60 // Method name
duke@435 61 mangle_name_on(&st, method->name());
duke@435 62 return st.as_string();
duke@435 63 }
duke@435 64
duke@435 65
duke@435 66 char* NativeLookup::long_jni_name(methodHandle method) {
duke@435 67 // Signature ignore the wrapping parenteses and the trailing return type
duke@435 68 stringStream st;
duke@435 69 symbolOop signature = method->signature();
duke@435 70 st.print("__");
duke@435 71 // find ')'
duke@435 72 int end;
duke@435 73 for (end = 0; end < signature->utf8_length() && signature->byte_at(end) != ')'; end++);
duke@435 74 // skip first '('
duke@435 75 mangle_name_on(&st, signature, 1, end);
duke@435 76 return st.as_string();
duke@435 77 }
duke@435 78
duke@435 79 extern "C" {
duke@435 80 void JNICALL JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls);
duke@435 81 void JNICALL JVM_RegisterPerfMethods(JNIEnv *env, jclass perfclass);
duke@435 82 }
duke@435 83
duke@435 84 static address lookup_special_native(char* jni_name) {
duke@435 85 // NB: To ignore the jni prefix and jni postfix strstr is used matching.
duke@435 86 if (!JDK_Version::is_gte_jdk14x_version()) {
duke@435 87 // These functions only exist for compatibility with 1.3.1 and earlier
duke@435 88 // Intercept ObjectOutputStream getPrimitiveFieldValues for faster serialization
duke@435 89 if (strstr(jni_name, "Java_java_io_ObjectOutputStream_getPrimitiveFieldValues") != NULL) {
duke@435 90 return CAST_FROM_FN_PTR(address, JVM_GetPrimitiveFieldValues);
duke@435 91 }
duke@435 92 // Intercept ObjectInputStream setPrimitiveFieldValues for faster serialization
duke@435 93 if (strstr(jni_name, "Java_java_io_ObjectInputStream_setPrimitiveFieldValues") != NULL) {
duke@435 94 return CAST_FROM_FN_PTR(address, JVM_SetPrimitiveFieldValues);
duke@435 95 }
duke@435 96 }
duke@435 97 if (strstr(jni_name, "Java_sun_misc_Unsafe_registerNatives") != NULL) {
duke@435 98 return CAST_FROM_FN_PTR(address, JVM_RegisterUnsafeMethods);
duke@435 99 }
duke@435 100 if (strstr(jni_name, "Java_sun_misc_Perf_registerNatives") != NULL) {
duke@435 101 return CAST_FROM_FN_PTR(address, JVM_RegisterPerfMethods);
duke@435 102 }
duke@435 103
duke@435 104 return NULL;
duke@435 105 }
duke@435 106
duke@435 107 address NativeLookup::lookup_style(methodHandle method, char* pure_name, const char* long_name, int args_size, bool os_style, bool& in_base_library, TRAPS) {
duke@435 108 address entry;
duke@435 109 // Compute complete JNI name for style
duke@435 110 stringStream st;
duke@435 111 if (os_style) os::print_jni_name_prefix_on(&st, args_size);
duke@435 112 st.print_raw(pure_name);
duke@435 113 st.print_raw(long_name);
duke@435 114 if (os_style) os::print_jni_name_suffix_on(&st, args_size);
duke@435 115 char* jni_name = st.as_string();
duke@435 116
duke@435 117 // If the loader is null we have a system class, so we attempt a lookup in
duke@435 118 // the native Java library. This takes care of any bootstrapping problems.
duke@435 119 // Note: It is critical for bootstrapping that Java_java_lang_ClassLoader_00024NativeLibrary_find
duke@435 120 // gets found the first time around - otherwise an infinite loop can occure. This is
duke@435 121 // another VM/library dependency
duke@435 122 Handle loader(THREAD,
duke@435 123 instanceKlass::cast(method->method_holder())->class_loader());
duke@435 124 if (loader.is_null()) {
duke@435 125 entry = lookup_special_native(jni_name);
duke@435 126 if (entry == NULL) {
duke@435 127 entry = (address) hpi::dll_lookup(os::native_java_library(), jni_name);
duke@435 128 }
duke@435 129 if (entry != NULL) {
duke@435 130 in_base_library = true;
duke@435 131 return entry;
duke@435 132 }
duke@435 133 }
duke@435 134
duke@435 135 // Otherwise call static method findNative in ClassLoader
duke@435 136 KlassHandle klass (THREAD, SystemDictionary::classloader_klass());
duke@435 137 Handle name_arg = java_lang_String::create_from_str(jni_name, CHECK_NULL);
duke@435 138
duke@435 139 JavaValue result(T_LONG);
duke@435 140 JavaCalls::call_static(&result,
duke@435 141 klass,
duke@435 142 vmSymbolHandles::findNative_name(),
duke@435 143 vmSymbolHandles::classloader_string_long_signature(),
duke@435 144 // Arguments
duke@435 145 loader,
duke@435 146 name_arg,
duke@435 147 CHECK_NULL);
duke@435 148 entry = (address) (intptr_t) result.get_jlong();
duke@435 149
duke@435 150 if (entry == NULL) {
duke@435 151 // findNative didn't find it, if there are any agent libraries look in them
duke@435 152 AgentLibrary* agent;
duke@435 153 for (agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
duke@435 154 entry = (address) hpi::dll_lookup(agent->os_lib(), jni_name);
duke@435 155 if (entry != NULL) {
duke@435 156 return entry;
duke@435 157 }
duke@435 158 }
duke@435 159 }
duke@435 160
duke@435 161 return entry;
duke@435 162 }
duke@435 163
duke@435 164
duke@435 165 // Check all the formats of native implementation name to see if there is one
duke@435 166 // for the specified method.
duke@435 167 address NativeLookup::lookup_entry(methodHandle method, bool& in_base_library, TRAPS) {
duke@435 168 address entry = NULL;
duke@435 169 in_base_library = false;
duke@435 170 // Compute pure name
duke@435 171 char* pure_name = pure_jni_name(method);
duke@435 172
duke@435 173 // Compute argument size
duke@435 174 int args_size = 1 // JNIEnv
duke@435 175 + (method->is_static() ? 1 : 0) // class for static methods
duke@435 176 + method->size_of_parameters(); // actual parameters
duke@435 177
duke@435 178
duke@435 179 // 1) Try JNI short style
duke@435 180 entry = lookup_style(method, pure_name, "", args_size, true, in_base_library, CHECK_NULL);
duke@435 181 if (entry != NULL) return entry;
duke@435 182
duke@435 183 // Compute long name
duke@435 184 char* long_name = long_jni_name(method);
duke@435 185
duke@435 186 // 2) Try JNI long style
duke@435 187 entry = lookup_style(method, pure_name, long_name, args_size, true, in_base_library, CHECK_NULL);
duke@435 188 if (entry != NULL) return entry;
duke@435 189
duke@435 190 // 3) Try JNI short style without os prefix/suffix
duke@435 191 entry = lookup_style(method, pure_name, "", args_size, false, in_base_library, CHECK_NULL);
duke@435 192 if (entry != NULL) return entry;
duke@435 193
duke@435 194 // 4) Try JNI long style without os prefix/suffix
duke@435 195 entry = lookup_style(method, pure_name, long_name, args_size, false, in_base_library, CHECK_NULL);
duke@435 196
duke@435 197 return entry; // NULL indicates not found
duke@435 198 }
duke@435 199
duke@435 200 // Check if there are any JVM TI prefixes which have been applied to the native method name.
duke@435 201 // If any are found, remove them before attemping the look up of the
duke@435 202 // native implementation again.
duke@435 203 // See SetNativeMethodPrefix in the JVM TI Spec for more details.
duke@435 204 address NativeLookup::lookup_entry_prefixed(methodHandle method, bool& in_base_library, TRAPS) {
duke@435 205 ResourceMark rm(THREAD);
duke@435 206
duke@435 207 int prefix_count;
duke@435 208 char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
duke@435 209 char* in_name = method->name()->as_C_string();
duke@435 210 char* wrapper_name = in_name;
duke@435 211 // last applied prefix will be first -- go backwards
duke@435 212 for (int i = prefix_count-1; i >= 0; i--) {
duke@435 213 char* prefix = prefixes[i];
duke@435 214 size_t prefix_len = strlen(prefix);
duke@435 215 if (strncmp(prefix, wrapper_name, prefix_len) == 0) {
duke@435 216 // has this prefix remove it
duke@435 217 wrapper_name += prefix_len;
duke@435 218 }
duke@435 219 }
duke@435 220 if (wrapper_name != in_name) {
duke@435 221 // we have a name for a wrapping method
duke@435 222 int wrapper_name_len = (int)strlen(wrapper_name);
duke@435 223 symbolHandle wrapper_symbol(THREAD, SymbolTable::probe(wrapper_name, wrapper_name_len));
duke@435 224 if (!wrapper_symbol.is_null()) {
duke@435 225 KlassHandle kh(method->method_holder());
duke@435 226 methodOop wrapper_method = Klass::cast(kh())->lookup_method(wrapper_symbol(),
duke@435 227 method->signature());
duke@435 228 if (wrapper_method != NULL && !wrapper_method->is_native()) {
duke@435 229 // we found a wrapper method, use its native entry
duke@435 230 method->set_is_prefixed_native();
duke@435 231 return lookup_entry(wrapper_method, in_base_library, THREAD);
duke@435 232 }
duke@435 233 }
duke@435 234 }
duke@435 235 return NULL;
duke@435 236 }
duke@435 237
duke@435 238 address NativeLookup::lookup_base(methodHandle method, bool& in_base_library, TRAPS) {
duke@435 239 address entry = NULL;
duke@435 240 ResourceMark rm(THREAD);
duke@435 241
duke@435 242 entry = lookup_entry(method, in_base_library, THREAD);
duke@435 243 if (entry != NULL) return entry;
duke@435 244
duke@435 245 // standard native method resolution has failed. Check if there are any
duke@435 246 // JVM TI prefixes which have been applied to the native method name.
duke@435 247 entry = lookup_entry_prefixed(method, in_base_library, THREAD);
duke@435 248 if (entry != NULL) return entry;
duke@435 249
duke@435 250 // Native function not found, throw UnsatisfiedLinkError
duke@435 251 THROW_MSG_0(vmSymbols::java_lang_UnsatisfiedLinkError(),
duke@435 252 method->name_and_sig_as_C_string());
duke@435 253 }
duke@435 254
duke@435 255
duke@435 256 address NativeLookup::lookup(methodHandle method, bool& in_base_library, TRAPS) {
duke@435 257 if (!method->has_native_function()) {
duke@435 258 address entry = lookup_base(method, in_base_library, CHECK_NULL);
duke@435 259 method->set_native_function(entry,
duke@435 260 methodOopDesc::native_bind_event_is_interesting);
duke@435 261 // -verbose:jni printing
duke@435 262 if (PrintJNIResolving) {
duke@435 263 ResourceMark rm(THREAD);
duke@435 264 tty->print_cr("[Dynamic-linking native method %s.%s ... JNI]",
duke@435 265 Klass::cast(method->method_holder())->external_name(),
duke@435 266 method->name()->as_C_string());
duke@435 267 }
duke@435 268 }
duke@435 269 return method->native_function();
duke@435 270 }
duke@435 271
duke@435 272 address NativeLookup::base_library_lookup(const char* class_name, const char* method_name, const char* signature) {
duke@435 273 EXCEPTION_MARK;
duke@435 274 bool in_base_library = true; // SharedRuntime inits some math methods.
duke@435 275 symbolHandle c_name = oopFactory::new_symbol_handle(class_name, CATCH);
duke@435 276 symbolHandle m_name = oopFactory::new_symbol_handle(method_name, CATCH);
duke@435 277 symbolHandle s_name = oopFactory::new_symbol_handle(signature, CATCH);
duke@435 278
duke@435 279 // Find the class
duke@435 280 klassOop k = SystemDictionary::resolve_or_fail(c_name, true, CATCH);
duke@435 281 instanceKlassHandle klass (THREAD, k);
duke@435 282
duke@435 283 // Find method and invoke standard lookup
duke@435 284 methodHandle method (THREAD,
duke@435 285 klass->uncached_lookup_method(m_name(), s_name()));
duke@435 286 address result = lookup(method, in_base_library, CATCH);
duke@435 287 assert(in_base_library, "must be in basic library");
duke@435 288 guarantee(result != NULL, "must be non NULL");
duke@435 289 return result;
duke@435 290 }

mercurial