1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/oops/method.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,2033 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "classfile/metadataOnStackMark.hpp" 1.30 +#include "classfile/systemDictionary.hpp" 1.31 +#include "code/debugInfoRec.hpp" 1.32 +#include "gc_interface/collectedHeap.inline.hpp" 1.33 +#include "interpreter/bytecodeStream.hpp" 1.34 +#include "interpreter/bytecodeTracer.hpp" 1.35 +#include "interpreter/bytecodes.hpp" 1.36 +#include "interpreter/interpreter.hpp" 1.37 +#include "interpreter/oopMapCache.hpp" 1.38 +#include "memory/gcLocker.hpp" 1.39 +#include "memory/generation.hpp" 1.40 +#include "memory/heapInspection.hpp" 1.41 +#include "memory/metadataFactory.hpp" 1.42 +#include "memory/oopFactory.hpp" 1.43 +#include "oops/constMethod.hpp" 1.44 +#include "oops/methodData.hpp" 1.45 +#include "oops/method.hpp" 1.46 +#include "oops/oop.inline.hpp" 1.47 +#include "oops/symbol.hpp" 1.48 +#include "prims/jvmtiExport.hpp" 1.49 +#include "prims/methodHandles.hpp" 1.50 +#include "prims/nativeLookup.hpp" 1.51 +#include "runtime/arguments.hpp" 1.52 +#include "runtime/compilationPolicy.hpp" 1.53 +#include "runtime/frame.inline.hpp" 1.54 +#include "runtime/handles.inline.hpp" 1.55 +#include "runtime/relocator.hpp" 1.56 +#include "runtime/sharedRuntime.hpp" 1.57 +#include "runtime/signature.hpp" 1.58 +#include "utilities/quickSort.hpp" 1.59 +#include "utilities/xmlstream.hpp" 1.60 + 1.61 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.62 + 1.63 +// Implementation of Method 1.64 + 1.65 +Method* Method::allocate(ClassLoaderData* loader_data, 1.66 + int byte_code_size, 1.67 + AccessFlags access_flags, 1.68 + InlineTableSizes* sizes, 1.69 + ConstMethod::MethodType method_type, 1.70 + TRAPS) { 1.71 + assert(!access_flags.is_native() || byte_code_size == 0, 1.72 + "native methods should not contain byte codes"); 1.73 + ConstMethod* cm = ConstMethod::allocate(loader_data, 1.74 + byte_code_size, 1.75 + sizes, 1.76 + method_type, 1.77 + CHECK_NULL); 1.78 + 1.79 + int size = Method::size(access_flags.is_native()); 1.80 + 1.81 + return new (loader_data, size, false, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags, size); 1.82 +} 1.83 + 1.84 +Method::Method(ConstMethod* xconst, AccessFlags access_flags, int size) { 1.85 + No_Safepoint_Verifier no_safepoint; 1.86 + set_constMethod(xconst); 1.87 + set_access_flags(access_flags); 1.88 + set_method_size(size); 1.89 +#ifdef CC_INTERP 1.90 + set_result_index(T_VOID); 1.91 +#endif 1.92 + set_intrinsic_id(vmIntrinsics::_none); 1.93 + set_jfr_towrite(false); 1.94 + set_force_inline(false); 1.95 + set_hidden(false); 1.96 + set_dont_inline(false); 1.97 + set_method_data(NULL); 1.98 + set_method_counters(NULL); 1.99 + set_vtable_index(Method::garbage_vtable_index); 1.100 + 1.101 + // Fix and bury in Method* 1.102 + set_interpreter_entry(NULL); // sets i2i entry and from_int 1.103 + set_adapter_entry(NULL); 1.104 + clear_code(); // from_c/from_i get set to c2i/i2i 1.105 + 1.106 + if (access_flags.is_native()) { 1.107 + clear_native_function(); 1.108 + set_signature_handler(NULL); 1.109 + } 1.110 + 1.111 + NOT_PRODUCT(set_compiled_invocation_count(0);) 1.112 +} 1.113 + 1.114 +// Release Method*. The nmethod will be gone when we get here because 1.115 +// we've walked the code cache. 1.116 +void Method::deallocate_contents(ClassLoaderData* loader_data) { 1.117 + MetadataFactory::free_metadata(loader_data, constMethod()); 1.118 + set_constMethod(NULL); 1.119 + MetadataFactory::free_metadata(loader_data, method_data()); 1.120 + set_method_data(NULL); 1.121 + MetadataFactory::free_metadata(loader_data, method_counters()); 1.122 + set_method_counters(NULL); 1.123 + // The nmethod will be gone when we get here. 1.124 + if (code() != NULL) _code = NULL; 1.125 +} 1.126 + 1.127 +address Method::get_i2c_entry() { 1.128 + assert(_adapter != NULL, "must have"); 1.129 + return _adapter->get_i2c_entry(); 1.130 +} 1.131 + 1.132 +address Method::get_c2i_entry() { 1.133 + assert(_adapter != NULL, "must have"); 1.134 + return _adapter->get_c2i_entry(); 1.135 +} 1.136 + 1.137 +address Method::get_c2i_unverified_entry() { 1.138 + assert(_adapter != NULL, "must have"); 1.139 + return _adapter->get_c2i_unverified_entry(); 1.140 +} 1.141 + 1.142 +char* Method::name_and_sig_as_C_string() const { 1.143 + return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature()); 1.144 +} 1.145 + 1.146 +char* Method::name_and_sig_as_C_string(char* buf, int size) const { 1.147 + return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size); 1.148 +} 1.149 + 1.150 +char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) { 1.151 + const char* klass_name = klass->external_name(); 1.152 + int klass_name_len = (int)strlen(klass_name); 1.153 + int method_name_len = method_name->utf8_length(); 1.154 + int len = klass_name_len + 1 + method_name_len + signature->utf8_length(); 1.155 + char* dest = NEW_RESOURCE_ARRAY(char, len + 1); 1.156 + strcpy(dest, klass_name); 1.157 + dest[klass_name_len] = '.'; 1.158 + strcpy(&dest[klass_name_len + 1], method_name->as_C_string()); 1.159 + strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string()); 1.160 + dest[len] = 0; 1.161 + return dest; 1.162 +} 1.163 + 1.164 +char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) { 1.165 + Symbol* klass_name = klass->name(); 1.166 + klass_name->as_klass_external_name(buf, size); 1.167 + int len = (int)strlen(buf); 1.168 + 1.169 + if (len < size - 1) { 1.170 + buf[len++] = '.'; 1.171 + 1.172 + method_name->as_C_string(&(buf[len]), size - len); 1.173 + len = (int)strlen(buf); 1.174 + 1.175 + signature->as_C_string(&(buf[len]), size - len); 1.176 + } 1.177 + 1.178 + return buf; 1.179 +} 1.180 + 1.181 +int Method::fast_exception_handler_bci_for(methodHandle mh, KlassHandle ex_klass, int throw_bci, TRAPS) { 1.182 + // exception table holds quadruple entries of the form (beg_bci, end_bci, handler_bci, klass_index) 1.183 + // access exception table 1.184 + ExceptionTable table(mh()); 1.185 + int length = table.length(); 1.186 + // iterate through all entries sequentially 1.187 + constantPoolHandle pool(THREAD, mh->constants()); 1.188 + for (int i = 0; i < length; i ++) { 1.189 + //reacquire the table in case a GC happened 1.190 + ExceptionTable table(mh()); 1.191 + int beg_bci = table.start_pc(i); 1.192 + int end_bci = table.end_pc(i); 1.193 + assert(beg_bci <= end_bci, "inconsistent exception table"); 1.194 + if (beg_bci <= throw_bci && throw_bci < end_bci) { 1.195 + // exception handler bci range covers throw_bci => investigate further 1.196 + int handler_bci = table.handler_pc(i); 1.197 + int klass_index = table.catch_type_index(i); 1.198 + if (klass_index == 0) { 1.199 + return handler_bci; 1.200 + } else if (ex_klass.is_null()) { 1.201 + return handler_bci; 1.202 + } else { 1.203 + // we know the exception class => get the constraint class 1.204 + // this may require loading of the constraint class; if verification 1.205 + // fails or some other exception occurs, return handler_bci 1.206 + Klass* k = pool->klass_at(klass_index, CHECK_(handler_bci)); 1.207 + KlassHandle klass = KlassHandle(THREAD, k); 1.208 + assert(klass.not_null(), "klass not loaded"); 1.209 + if (ex_klass->is_subtype_of(klass())) { 1.210 + return handler_bci; 1.211 + } 1.212 + } 1.213 + } 1.214 + } 1.215 + 1.216 + return -1; 1.217 +} 1.218 + 1.219 +void Method::mask_for(int bci, InterpreterOopMap* mask) { 1.220 + 1.221 + Thread* myThread = Thread::current(); 1.222 + methodHandle h_this(myThread, this); 1.223 +#ifdef ASSERT 1.224 + bool has_capability = myThread->is_VM_thread() || 1.225 + myThread->is_ConcurrentGC_thread() || 1.226 + myThread->is_GC_task_thread(); 1.227 + 1.228 + if (!has_capability) { 1.229 + if (!VerifyStack && !VerifyLastFrame) { 1.230 + // verify stack calls this outside VM thread 1.231 + warning("oopmap should only be accessed by the " 1.232 + "VM, GC task or CMS threads (or during debugging)"); 1.233 + InterpreterOopMap local_mask; 1.234 + method_holder()->mask_for(h_this, bci, &local_mask); 1.235 + local_mask.print(); 1.236 + } 1.237 + } 1.238 +#endif 1.239 + method_holder()->mask_for(h_this, bci, mask); 1.240 + return; 1.241 +} 1.242 + 1.243 + 1.244 +int Method::bci_from(address bcp) const { 1.245 +#ifdef ASSERT 1.246 + { ResourceMark rm; 1.247 + assert(is_native() && bcp == code_base() || contains(bcp) || is_error_reported(), 1.248 + err_msg("bcp doesn't belong to this method: bcp: " INTPTR_FORMAT ", method: %s", bcp, name_and_sig_as_C_string())); 1.249 + } 1.250 +#endif 1.251 + return bcp - code_base(); 1.252 +} 1.253 + 1.254 + 1.255 +// Return (int)bcx if it appears to be a valid BCI. 1.256 +// Return bci_from((address)bcx) if it appears to be a valid BCP. 1.257 +// Return -1 otherwise. 1.258 +// Used by profiling code, when invalid data is a possibility. 1.259 +// The caller is responsible for validating the Method* itself. 1.260 +int Method::validate_bci_from_bcx(intptr_t bcx) const { 1.261 + // keep bci as -1 if not a valid bci 1.262 + int bci = -1; 1.263 + if (bcx == 0 || (address)bcx == code_base()) { 1.264 + // code_size() may return 0 and we allow 0 here 1.265 + // the method may be native 1.266 + bci = 0; 1.267 + } else if (frame::is_bci(bcx)) { 1.268 + if (bcx < code_size()) { 1.269 + bci = (int)bcx; 1.270 + } 1.271 + } else if (contains((address)bcx)) { 1.272 + bci = (address)bcx - code_base(); 1.273 + } 1.274 + // Assert that if we have dodged any asserts, bci is negative. 1.275 + assert(bci == -1 || bci == bci_from(bcp_from(bci)), "sane bci if >=0"); 1.276 + return bci; 1.277 +} 1.278 + 1.279 +address Method::bcp_from(int bci) const { 1.280 + assert((is_native() && bci == 0) || (!is_native() && 0 <= bci && bci < code_size()), err_msg("illegal bci: %d", bci)); 1.281 + address bcp = code_base() + bci; 1.282 + assert(is_native() && bcp == code_base() || contains(bcp), "bcp doesn't belong to this method"); 1.283 + return bcp; 1.284 +} 1.285 + 1.286 + 1.287 +int Method::size(bool is_native) { 1.288 + // If native, then include pointers for native_function and signature_handler 1.289 + int extra_bytes = (is_native) ? 2*sizeof(address*) : 0; 1.290 + int extra_words = align_size_up(extra_bytes, BytesPerWord) / BytesPerWord; 1.291 + return align_object_size(header_size() + extra_words); 1.292 +} 1.293 + 1.294 + 1.295 +Symbol* Method::klass_name() const { 1.296 + Klass* k = method_holder(); 1.297 + assert(k->is_klass(), "must be klass"); 1.298 + InstanceKlass* ik = (InstanceKlass*) k; 1.299 + return ik->name(); 1.300 +} 1.301 + 1.302 + 1.303 +// Attempt to return method oop to original state. Clear any pointers 1.304 +// (to objects outside the shared spaces). We won't be able to predict 1.305 +// where they should point in a new JVM. Further initialize some 1.306 +// entries now in order allow them to be write protected later. 1.307 + 1.308 +void Method::remove_unshareable_info() { 1.309 + unlink_method(); 1.310 +} 1.311 + 1.312 + 1.313 +bool Method::was_executed_more_than(int n) { 1.314 + // Invocation counter is reset when the Method* is compiled. 1.315 + // If the method has compiled code we therefore assume it has 1.316 + // be excuted more than n times. 1.317 + if (is_accessor() || is_empty_method() || (code() != NULL)) { 1.318 + // interpreter doesn't bump invocation counter of trivial methods 1.319 + // compiler does not bump invocation counter of compiled methods 1.320 + return true; 1.321 + } 1.322 + else if ((method_counters() != NULL && 1.323 + method_counters()->invocation_counter()->carry()) || 1.324 + (method_data() != NULL && 1.325 + method_data()->invocation_counter()->carry())) { 1.326 + // The carry bit is set when the counter overflows and causes 1.327 + // a compilation to occur. We don't know how many times 1.328 + // the counter has been reset, so we simply assume it has 1.329 + // been executed more than n times. 1.330 + return true; 1.331 + } else { 1.332 + return invocation_count() > n; 1.333 + } 1.334 +} 1.335 + 1.336 +#ifndef PRODUCT 1.337 +void Method::print_invocation_count() { 1.338 + if (is_static()) tty->print("static "); 1.339 + if (is_final()) tty->print("final "); 1.340 + if (is_synchronized()) tty->print("synchronized "); 1.341 + if (is_native()) tty->print("native "); 1.342 + method_holder()->name()->print_symbol_on(tty); 1.343 + tty->print("."); 1.344 + name()->print_symbol_on(tty); 1.345 + signature()->print_symbol_on(tty); 1.346 + 1.347 + if (WizardMode) { 1.348 + // dump the size of the byte codes 1.349 + tty->print(" {%d}", code_size()); 1.350 + } 1.351 + tty->cr(); 1.352 + 1.353 + tty->print_cr (" interpreter_invocation_count: %8d ", interpreter_invocation_count()); 1.354 + tty->print_cr (" invocation_counter: %8d ", invocation_count()); 1.355 + tty->print_cr (" backedge_counter: %8d ", backedge_count()); 1.356 + if (CountCompiledCalls) { 1.357 + tty->print_cr (" compiled_invocation_count: %8d ", compiled_invocation_count()); 1.358 + } 1.359 + 1.360 +} 1.361 +#endif 1.362 + 1.363 +// Build a MethodData* object to hold information about this method 1.364 +// collected in the interpreter. 1.365 +void Method::build_interpreter_method_data(methodHandle method, TRAPS) { 1.366 + // Do not profile method if current thread holds the pending list lock, 1.367 + // which avoids deadlock for acquiring the MethodData_lock. 1.368 + if (InstanceRefKlass::owns_pending_list_lock((JavaThread*)THREAD)) { 1.369 + return; 1.370 + } 1.371 + 1.372 + // Grab a lock here to prevent multiple 1.373 + // MethodData*s from being created. 1.374 + MutexLocker ml(MethodData_lock, THREAD); 1.375 + if (method->method_data() == NULL) { 1.376 + ClassLoaderData* loader_data = method->method_holder()->class_loader_data(); 1.377 + MethodData* method_data = MethodData::allocate(loader_data, method, CHECK); 1.378 + method->set_method_data(method_data); 1.379 + if (PrintMethodData && (Verbose || WizardMode)) { 1.380 + ResourceMark rm(THREAD); 1.381 + tty->print("build_interpreter_method_data for "); 1.382 + method->print_name(tty); 1.383 + tty->cr(); 1.384 + // At the end of the run, the MDO, full of data, will be dumped. 1.385 + } 1.386 + } 1.387 +} 1.388 + 1.389 +MethodCounters* Method::build_method_counters(Method* m, TRAPS) { 1.390 + methodHandle mh(m); 1.391 + ClassLoaderData* loader_data = mh->method_holder()->class_loader_data(); 1.392 + MethodCounters* counters = MethodCounters::allocate(loader_data, CHECK_NULL); 1.393 + if (mh->method_counters() == NULL) { 1.394 + mh->set_method_counters(counters); 1.395 + } else { 1.396 + MetadataFactory::free_metadata(loader_data, counters); 1.397 + } 1.398 + return mh->method_counters(); 1.399 +} 1.400 + 1.401 +void Method::cleanup_inline_caches() { 1.402 + // The current system doesn't use inline caches in the interpreter 1.403 + // => nothing to do (keep this method around for future use) 1.404 +} 1.405 + 1.406 + 1.407 +int Method::extra_stack_words() { 1.408 + // not an inline function, to avoid a header dependency on Interpreter 1.409 + return extra_stack_entries() * Interpreter::stackElementSize; 1.410 +} 1.411 + 1.412 + 1.413 +void Method::compute_size_of_parameters(Thread *thread) { 1.414 + ArgumentSizeComputer asc(signature()); 1.415 + set_size_of_parameters(asc.size() + (is_static() ? 0 : 1)); 1.416 +} 1.417 + 1.418 +#ifdef CC_INTERP 1.419 +void Method::set_result_index(BasicType type) { 1.420 + _result_index = Interpreter::BasicType_as_index(type); 1.421 +} 1.422 +#endif 1.423 + 1.424 +BasicType Method::result_type() const { 1.425 + ResultTypeFinder rtf(signature()); 1.426 + return rtf.type(); 1.427 +} 1.428 + 1.429 + 1.430 +bool Method::is_empty_method() const { 1.431 + return code_size() == 1 1.432 + && *code_base() == Bytecodes::_return; 1.433 +} 1.434 + 1.435 + 1.436 +bool Method::is_vanilla_constructor() const { 1.437 + // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method 1.438 + // which only calls the superclass vanilla constructor and possibly does stores of 1.439 + // zero constants to local fields: 1.440 + // 1.441 + // aload_0 1.442 + // invokespecial 1.443 + // indexbyte1 1.444 + // indexbyte2 1.445 + // 1.446 + // followed by an (optional) sequence of: 1.447 + // 1.448 + // aload_0 1.449 + // aconst_null / iconst_0 / fconst_0 / dconst_0 1.450 + // putfield 1.451 + // indexbyte1 1.452 + // indexbyte2 1.453 + // 1.454 + // followed by: 1.455 + // 1.456 + // return 1.457 + 1.458 + assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors"); 1.459 + assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors"); 1.460 + int size = code_size(); 1.461 + // Check if size match 1.462 + if (size == 0 || size % 5 != 0) return false; 1.463 + address cb = code_base(); 1.464 + int last = size - 1; 1.465 + if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) { 1.466 + // Does not call superclass default constructor 1.467 + return false; 1.468 + } 1.469 + // Check optional sequence 1.470 + for (int i = 4; i < last; i += 5) { 1.471 + if (cb[i] != Bytecodes::_aload_0) return false; 1.472 + if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false; 1.473 + if (cb[i+2] != Bytecodes::_putfield) return false; 1.474 + } 1.475 + return true; 1.476 +} 1.477 + 1.478 + 1.479 +bool Method::compute_has_loops_flag() { 1.480 + BytecodeStream bcs(this); 1.481 + Bytecodes::Code bc; 1.482 + 1.483 + while ((bc = bcs.next()) >= 0) { 1.484 + switch( bc ) { 1.485 + case Bytecodes::_ifeq: 1.486 + case Bytecodes::_ifnull: 1.487 + case Bytecodes::_iflt: 1.488 + case Bytecodes::_ifle: 1.489 + case Bytecodes::_ifne: 1.490 + case Bytecodes::_ifnonnull: 1.491 + case Bytecodes::_ifgt: 1.492 + case Bytecodes::_ifge: 1.493 + case Bytecodes::_if_icmpeq: 1.494 + case Bytecodes::_if_icmpne: 1.495 + case Bytecodes::_if_icmplt: 1.496 + case Bytecodes::_if_icmpgt: 1.497 + case Bytecodes::_if_icmple: 1.498 + case Bytecodes::_if_icmpge: 1.499 + case Bytecodes::_if_acmpeq: 1.500 + case Bytecodes::_if_acmpne: 1.501 + case Bytecodes::_goto: 1.502 + case Bytecodes::_jsr: 1.503 + if( bcs.dest() < bcs.next_bci() ) _access_flags.set_has_loops(); 1.504 + break; 1.505 + 1.506 + case Bytecodes::_goto_w: 1.507 + case Bytecodes::_jsr_w: 1.508 + if( bcs.dest_w() < bcs.next_bci() ) _access_flags.set_has_loops(); 1.509 + break; 1.510 + } 1.511 + } 1.512 + _access_flags.set_loops_flag_init(); 1.513 + return _access_flags.has_loops(); 1.514 +} 1.515 + 1.516 +bool Method::is_final_method(AccessFlags class_access_flags) const { 1.517 + // or "does_not_require_vtable_entry" 1.518 + // default method or overpass can occur, is not final (reuses vtable entry) 1.519 + // private methods get vtable entries for backward class compatibility. 1.520 + if (is_overpass() || is_default_method()) return false; 1.521 + return is_final() || class_access_flags.is_final(); 1.522 +} 1.523 + 1.524 +bool Method::is_final_method() const { 1.525 + return is_final_method(method_holder()->access_flags()); 1.526 +} 1.527 + 1.528 +bool Method::is_default_method() const { 1.529 + if (method_holder() != NULL && 1.530 + method_holder()->is_interface() && 1.531 + !is_abstract()) { 1.532 + return true; 1.533 + } else { 1.534 + return false; 1.535 + } 1.536 +} 1.537 + 1.538 +bool Method::can_be_statically_bound(AccessFlags class_access_flags) const { 1.539 + if (is_final_method(class_access_flags)) return true; 1.540 +#ifdef ASSERT 1.541 + ResourceMark rm; 1.542 + bool is_nonv = (vtable_index() == nonvirtual_vtable_index); 1.543 + if (class_access_flags.is_interface()) { 1.544 + assert(is_nonv == is_static(), err_msg("is_nonv=%s", name_and_sig_as_C_string())); 1.545 + } 1.546 +#endif 1.547 + assert(valid_vtable_index() || valid_itable_index(), "method must be linked before we ask this question"); 1.548 + return vtable_index() == nonvirtual_vtable_index; 1.549 +} 1.550 + 1.551 +bool Method::can_be_statically_bound() const { 1.552 + return can_be_statically_bound(method_holder()->access_flags()); 1.553 +} 1.554 + 1.555 +bool Method::is_accessor() const { 1.556 + if (code_size() != 5) return false; 1.557 + if (size_of_parameters() != 1) return false; 1.558 + if (java_code_at(0) != Bytecodes::_aload_0 ) return false; 1.559 + if (java_code_at(1) != Bytecodes::_getfield) return false; 1.560 + if (java_code_at(4) != Bytecodes::_areturn && 1.561 + java_code_at(4) != Bytecodes::_ireturn ) return false; 1.562 + return true; 1.563 +} 1.564 + 1.565 + 1.566 +bool Method::is_initializer() const { 1.567 + return name() == vmSymbols::object_initializer_name() || is_static_initializer(); 1.568 +} 1.569 + 1.570 +bool Method::has_valid_initializer_flags() const { 1.571 + return (is_static() || 1.572 + method_holder()->major_version() < 51); 1.573 +} 1.574 + 1.575 +bool Method::is_static_initializer() const { 1.576 + // For classfiles version 51 or greater, ensure that the clinit method is 1.577 + // static. Non-static methods with the name "<clinit>" are not static 1.578 + // initializers. (older classfiles exempted for backward compatibility) 1.579 + return name() == vmSymbols::class_initializer_name() && 1.580 + has_valid_initializer_flags(); 1.581 +} 1.582 + 1.583 + 1.584 +objArrayHandle Method::resolved_checked_exceptions_impl(Method* this_oop, TRAPS) { 1.585 + int length = this_oop->checked_exceptions_length(); 1.586 + if (length == 0) { // common case 1.587 + return objArrayHandle(THREAD, Universe::the_empty_class_klass_array()); 1.588 + } else { 1.589 + methodHandle h_this(THREAD, this_oop); 1.590 + objArrayOop m_oop = oopFactory::new_objArray(SystemDictionary::Class_klass(), length, CHECK_(objArrayHandle())); 1.591 + objArrayHandle mirrors (THREAD, m_oop); 1.592 + for (int i = 0; i < length; i++) { 1.593 + CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe 1.594 + Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle())); 1.595 + assert(k->is_subclass_of(SystemDictionary::Throwable_klass()), "invalid exception class"); 1.596 + mirrors->obj_at_put(i, k->java_mirror()); 1.597 + } 1.598 + return mirrors; 1.599 + } 1.600 +}; 1.601 + 1.602 + 1.603 +int Method::line_number_from_bci(int bci) const { 1.604 + if (bci == SynchronizationEntryBCI) bci = 0; 1.605 + assert(bci == 0 || 0 <= bci && bci < code_size(), "illegal bci"); 1.606 + int best_bci = 0; 1.607 + int best_line = -1; 1.608 + 1.609 + if (has_linenumber_table()) { 1.610 + // The line numbers are a short array of 2-tuples [start_pc, line_number]. 1.611 + // Not necessarily sorted and not necessarily one-to-one. 1.612 + CompressedLineNumberReadStream stream(compressed_linenumber_table()); 1.613 + while (stream.read_pair()) { 1.614 + if (stream.bci() == bci) { 1.615 + // perfect match 1.616 + return stream.line(); 1.617 + } else { 1.618 + // update best_bci/line 1.619 + if (stream.bci() < bci && stream.bci() >= best_bci) { 1.620 + best_bci = stream.bci(); 1.621 + best_line = stream.line(); 1.622 + } 1.623 + } 1.624 + } 1.625 + } 1.626 + return best_line; 1.627 +} 1.628 + 1.629 + 1.630 +bool Method::is_klass_loaded_by_klass_index(int klass_index) const { 1.631 + if( constants()->tag_at(klass_index).is_unresolved_klass() ) { 1.632 + Thread *thread = Thread::current(); 1.633 + Symbol* klass_name = constants()->klass_name_at(klass_index); 1.634 + Handle loader(thread, method_holder()->class_loader()); 1.635 + Handle prot (thread, method_holder()->protection_domain()); 1.636 + return SystemDictionary::find(klass_name, loader, prot, thread) != NULL; 1.637 + } else { 1.638 + return true; 1.639 + } 1.640 +} 1.641 + 1.642 + 1.643 +bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const { 1.644 + int klass_index = constants()->klass_ref_index_at(refinfo_index); 1.645 + if (must_be_resolved) { 1.646 + // Make sure klass is resolved in constantpool. 1.647 + if (constants()->tag_at(klass_index).is_unresolved_klass()) return false; 1.648 + } 1.649 + return is_klass_loaded_by_klass_index(klass_index); 1.650 +} 1.651 + 1.652 + 1.653 +void Method::set_native_function(address function, bool post_event_flag) { 1.654 + assert(function != NULL, "use clear_native_function to unregister natives"); 1.655 + assert(!is_method_handle_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), ""); 1.656 + address* native_function = native_function_addr(); 1.657 + 1.658 + // We can see racers trying to place the same native function into place. Once 1.659 + // is plenty. 1.660 + address current = *native_function; 1.661 + if (current == function) return; 1.662 + if (post_event_flag && JvmtiExport::should_post_native_method_bind() && 1.663 + function != NULL) { 1.664 + // native_method_throw_unsatisfied_link_error_entry() should only 1.665 + // be passed when post_event_flag is false. 1.666 + assert(function != 1.667 + SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1.668 + "post_event_flag mis-match"); 1.669 + 1.670 + // post the bind event, and possible change the bind function 1.671 + JvmtiExport::post_native_method_bind(this, &function); 1.672 + } 1.673 + *native_function = function; 1.674 + // This function can be called more than once. We must make sure that we always 1.675 + // use the latest registered method -> check if a stub already has been generated. 1.676 + // If so, we have to make it not_entrant. 1.677 + nmethod* nm = code(); // Put it into local variable to guard against concurrent updates 1.678 + if (nm != NULL) { 1.679 + nm->make_not_entrant(); 1.680 + } 1.681 +} 1.682 + 1.683 + 1.684 +bool Method::has_native_function() const { 1.685 + if (is_method_handle_intrinsic()) 1.686 + return false; // special-cased in SharedRuntime::generate_native_wrapper 1.687 + address func = native_function(); 1.688 + return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry()); 1.689 +} 1.690 + 1.691 + 1.692 +void Method::clear_native_function() { 1.693 + // Note: is_method_handle_intrinsic() is allowed here. 1.694 + set_native_function( 1.695 + SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1.696 + !native_bind_event_is_interesting); 1.697 + clear_code(); 1.698 +} 1.699 + 1.700 +address Method::critical_native_function() { 1.701 + methodHandle mh(this); 1.702 + return NativeLookup::lookup_critical_entry(mh); 1.703 +} 1.704 + 1.705 + 1.706 +void Method::set_signature_handler(address handler) { 1.707 + address* signature_handler = signature_handler_addr(); 1.708 + *signature_handler = handler; 1.709 +} 1.710 + 1.711 + 1.712 +void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) { 1.713 + if (PrintCompilation && report) { 1.714 + ttyLocker ttyl; 1.715 + tty->print("made not %scompilable on ", is_osr ? "OSR " : ""); 1.716 + if (comp_level == CompLevel_all) { 1.717 + tty->print("all levels "); 1.718 + } else { 1.719 + tty->print("levels "); 1.720 + for (int i = (int)CompLevel_none; i <= comp_level; i++) { 1.721 + tty->print("%d ", i); 1.722 + } 1.723 + } 1.724 + this->print_short_name(tty); 1.725 + int size = this->code_size(); 1.726 + if (size > 0) { 1.727 + tty->print(" (%d bytes)", size); 1.728 + } 1.729 + if (reason != NULL) { 1.730 + tty->print(" %s", reason); 1.731 + } 1.732 + tty->cr(); 1.733 + } 1.734 + if ((TraceDeoptimization || LogCompilation) && (xtty != NULL)) { 1.735 + ttyLocker ttyl; 1.736 + xtty->begin_elem("make_not_%scompilable thread='" UINTX_FORMAT "'", 1.737 + is_osr ? "osr_" : "", os::current_thread_id()); 1.738 + if (reason != NULL) { 1.739 + xtty->print(" reason=\'%s\'", reason); 1.740 + } 1.741 + xtty->method(this); 1.742 + xtty->stamp(); 1.743 + xtty->end_elem(); 1.744 + } 1.745 +} 1.746 + 1.747 +bool Method::is_always_compilable() const { 1.748 + // Generated adapters must be compiled 1.749 + if (is_method_handle_intrinsic() && is_synthetic()) { 1.750 + assert(!is_not_c1_compilable(), "sanity check"); 1.751 + assert(!is_not_c2_compilable(), "sanity check"); 1.752 + return true; 1.753 + } 1.754 + 1.755 + return false; 1.756 +} 1.757 + 1.758 +bool Method::is_not_compilable(int comp_level) const { 1.759 + if (number_of_breakpoints() > 0) 1.760 + return true; 1.761 + if (is_always_compilable()) 1.762 + return false; 1.763 + if (comp_level == CompLevel_any) 1.764 + return is_not_c1_compilable() || is_not_c2_compilable(); 1.765 + if (is_c1_compile(comp_level)) 1.766 + return is_not_c1_compilable(); 1.767 + if (is_c2_compile(comp_level)) 1.768 + return is_not_c2_compilable(); 1.769 + return false; 1.770 +} 1.771 + 1.772 +// call this when compiler finds that this method is not compilable 1.773 +void Method::set_not_compilable(int comp_level, bool report, const char* reason) { 1.774 + if (is_always_compilable()) { 1.775 + // Don't mark a method which should be always compilable 1.776 + return; 1.777 + } 1.778 + print_made_not_compilable(comp_level, /*is_osr*/ false, report, reason); 1.779 + if (comp_level == CompLevel_all) { 1.780 + set_not_c1_compilable(); 1.781 + set_not_c2_compilable(); 1.782 + } else { 1.783 + if (is_c1_compile(comp_level)) 1.784 + set_not_c1_compilable(); 1.785 + if (is_c2_compile(comp_level)) 1.786 + set_not_c2_compilable(); 1.787 + } 1.788 + CompilationPolicy::policy()->disable_compilation(this); 1.789 + assert(!CompilationPolicy::can_be_compiled(this, comp_level), "sanity check"); 1.790 +} 1.791 + 1.792 +bool Method::is_not_osr_compilable(int comp_level) const { 1.793 + if (is_not_compilable(comp_level)) 1.794 + return true; 1.795 + if (comp_level == CompLevel_any) 1.796 + return is_not_c1_osr_compilable() || is_not_c2_osr_compilable(); 1.797 + if (is_c1_compile(comp_level)) 1.798 + return is_not_c1_osr_compilable(); 1.799 + if (is_c2_compile(comp_level)) 1.800 + return is_not_c2_osr_compilable(); 1.801 + return false; 1.802 +} 1.803 + 1.804 +void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) { 1.805 + print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason); 1.806 + if (comp_level == CompLevel_all) { 1.807 + set_not_c1_osr_compilable(); 1.808 + set_not_c2_osr_compilable(); 1.809 + } else { 1.810 + if (is_c1_compile(comp_level)) 1.811 + set_not_c1_osr_compilable(); 1.812 + if (is_c2_compile(comp_level)) 1.813 + set_not_c2_osr_compilable(); 1.814 + } 1.815 + CompilationPolicy::policy()->disable_compilation(this); 1.816 + assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check"); 1.817 +} 1.818 + 1.819 +// Revert to using the interpreter and clear out the nmethod 1.820 +void Method::clear_code() { 1.821 + 1.822 + // this may be NULL if c2i adapters have not been made yet 1.823 + // Only should happen at allocate time. 1.824 + if (_adapter == NULL) { 1.825 + _from_compiled_entry = NULL; 1.826 + } else { 1.827 + _from_compiled_entry = _adapter->get_c2i_entry(); 1.828 + } 1.829 + OrderAccess::storestore(); 1.830 + _from_interpreted_entry = _i2i_entry; 1.831 + OrderAccess::storestore(); 1.832 + _code = NULL; 1.833 +} 1.834 + 1.835 +// Called by class data sharing to remove any entry points (which are not shared) 1.836 +void Method::unlink_method() { 1.837 + _code = NULL; 1.838 + _i2i_entry = NULL; 1.839 + _from_interpreted_entry = NULL; 1.840 + if (is_native()) { 1.841 + *native_function_addr() = NULL; 1.842 + set_signature_handler(NULL); 1.843 + } 1.844 + NOT_PRODUCT(set_compiled_invocation_count(0);) 1.845 + _adapter = NULL; 1.846 + _from_compiled_entry = NULL; 1.847 + 1.848 + // In case of DumpSharedSpaces, _method_data should always be NULL. 1.849 + // 1.850 + // During runtime (!DumpSharedSpaces), when we are cleaning a 1.851 + // shared class that failed to load, this->link_method() may 1.852 + // have already been called (before an exception happened), so 1.853 + // this->_method_data may not be NULL. 1.854 + assert(!DumpSharedSpaces || _method_data == NULL, "unexpected method data?"); 1.855 + 1.856 + set_method_data(NULL); 1.857 + set_method_counters(NULL); 1.858 +} 1.859 + 1.860 +// Called when the method_holder is getting linked. Setup entrypoints so the method 1.861 +// is ready to be called from interpreter, compiler, and vtables. 1.862 +void Method::link_method(methodHandle h_method, TRAPS) { 1.863 + // If the code cache is full, we may reenter this function for the 1.864 + // leftover methods that weren't linked. 1.865 + if (_i2i_entry != NULL) return; 1.866 + 1.867 + assert(_adapter == NULL, "init'd to NULL" ); 1.868 + assert( _code == NULL, "nothing compiled yet" ); 1.869 + 1.870 + // Setup interpreter entrypoint 1.871 + assert(this == h_method(), "wrong h_method()" ); 1.872 + address entry = Interpreter::entry_for_method(h_method); 1.873 + assert(entry != NULL, "interpreter entry must be non-null"); 1.874 + // Sets both _i2i_entry and _from_interpreted_entry 1.875 + set_interpreter_entry(entry); 1.876 + 1.877 + // Don't overwrite already registered native entries. 1.878 + if (is_native() && !has_native_function()) { 1.879 + set_native_function( 1.880 + SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), 1.881 + !native_bind_event_is_interesting); 1.882 + } 1.883 + 1.884 + // Setup compiler entrypoint. This is made eagerly, so we do not need 1.885 + // special handling of vtables. An alternative is to make adapters more 1.886 + // lazily by calling make_adapter() from from_compiled_entry() for the 1.887 + // normal calls. For vtable calls life gets more complicated. When a 1.888 + // call-site goes mega-morphic we need adapters in all methods which can be 1.889 + // called from the vtable. We need adapters on such methods that get loaded 1.890 + // later. Ditto for mega-morphic itable calls. If this proves to be a 1.891 + // problem we'll make these lazily later. 1.892 + (void) make_adapters(h_method, CHECK); 1.893 + 1.894 + // ONLY USE the h_method now as make_adapter may have blocked 1.895 + 1.896 +} 1.897 + 1.898 +address Method::make_adapters(methodHandle mh, TRAPS) { 1.899 + // Adapters for compiled code are made eagerly here. They are fairly 1.900 + // small (generally < 100 bytes) and quick to make (and cached and shared) 1.901 + // so making them eagerly shouldn't be too expensive. 1.902 + AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh); 1.903 + if (adapter == NULL ) { 1.904 + THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "out of space in CodeCache for adapters"); 1.905 + } 1.906 + 1.907 + mh->set_adapter_entry(adapter); 1.908 + mh->_from_compiled_entry = adapter->get_c2i_entry(); 1.909 + return adapter->get_c2i_entry(); 1.910 +} 1.911 + 1.912 +void Method::restore_unshareable_info(TRAPS) { 1.913 + // Since restore_unshareable_info can be called more than once for a method, don't 1.914 + // redo any work. If this field is restored, there is nothing to do. 1.915 + if (_from_compiled_entry == NULL) { 1.916 + // restore method's vtable by calling a virtual function 1.917 + restore_vtable(); 1.918 + 1.919 + methodHandle mh(THREAD, this); 1.920 + link_method(mh, CHECK); 1.921 + } 1.922 +} 1.923 + 1.924 + 1.925 +// The verified_code_entry() must be called when a invoke is resolved 1.926 +// on this method. 1.927 + 1.928 +// It returns the compiled code entry point, after asserting not null. 1.929 +// This function is called after potential safepoints so that nmethod 1.930 +// or adapter that it points to is still live and valid. 1.931 +// This function must not hit a safepoint! 1.932 +address Method::verified_code_entry() { 1.933 + debug_only(No_Safepoint_Verifier nsv;) 1.934 + assert(_from_compiled_entry != NULL, "must be set"); 1.935 + return _from_compiled_entry; 1.936 +} 1.937 + 1.938 +// Check that if an nmethod ref exists, it has a backlink to this or no backlink at all 1.939 +// (could be racing a deopt). 1.940 +// Not inline to avoid circular ref. 1.941 +bool Method::check_code() const { 1.942 + // cached in a register or local. There's a race on the value of the field. 1.943 + nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code); 1.944 + return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method()); 1.945 +} 1.946 + 1.947 +// Install compiled code. Instantly it can execute. 1.948 +void Method::set_code(methodHandle mh, nmethod *code) { 1.949 + assert( code, "use clear_code to remove code" ); 1.950 + assert( mh->check_code(), "" ); 1.951 + 1.952 + guarantee(mh->adapter() != NULL, "Adapter blob must already exist!"); 1.953 + 1.954 + // These writes must happen in this order, because the interpreter will 1.955 + // directly jump to from_interpreted_entry which jumps to an i2c adapter 1.956 + // which jumps to _from_compiled_entry. 1.957 + mh->_code = code; // Assign before allowing compiled code to exec 1.958 + 1.959 + int comp_level = code->comp_level(); 1.960 + // In theory there could be a race here. In practice it is unlikely 1.961 + // and not worth worrying about. 1.962 + if (comp_level > mh->highest_comp_level()) { 1.963 + mh->set_highest_comp_level(comp_level); 1.964 + } 1.965 + 1.966 + OrderAccess::storestore(); 1.967 +#ifdef SHARK 1.968 + mh->_from_interpreted_entry = code->insts_begin(); 1.969 +#else //!SHARK 1.970 + mh->_from_compiled_entry = code->verified_entry_point(); 1.971 + OrderAccess::storestore(); 1.972 + // Instantly compiled code can execute. 1.973 + if (!mh->is_method_handle_intrinsic()) 1.974 + mh->_from_interpreted_entry = mh->get_i2c_entry(); 1.975 +#endif //!SHARK 1.976 +} 1.977 + 1.978 + 1.979 +bool Method::is_overridden_in(Klass* k) const { 1.980 + InstanceKlass* ik = InstanceKlass::cast(k); 1.981 + 1.982 + if (ik->is_interface()) return false; 1.983 + 1.984 + // If method is an interface, we skip it - except if it 1.985 + // is a miranda method 1.986 + if (method_holder()->is_interface()) { 1.987 + // Check that method is not a miranda method 1.988 + if (ik->lookup_method(name(), signature()) == NULL) { 1.989 + // No implementation exist - so miranda method 1.990 + return false; 1.991 + } 1.992 + return true; 1.993 + } 1.994 + 1.995 + assert(ik->is_subclass_of(method_holder()), "should be subklass"); 1.996 + assert(ik->vtable() != NULL, "vtable should exist"); 1.997 + if (!has_vtable_index()) { 1.998 + return false; 1.999 + } else { 1.1000 + Method* vt_m = ik->method_at_vtable(vtable_index()); 1.1001 + return vt_m != this; 1.1002 + } 1.1003 +} 1.1004 + 1.1005 + 1.1006 +// give advice about whether this Method* should be cached or not 1.1007 +bool Method::should_not_be_cached() const { 1.1008 + if (is_old()) { 1.1009 + // This method has been redefined. It is either EMCP or obsolete 1.1010 + // and we don't want to cache it because that would pin the method 1.1011 + // down and prevent it from being collectible if and when it 1.1012 + // finishes executing. 1.1013 + return true; 1.1014 + } 1.1015 + 1.1016 + // caching this method should be just fine 1.1017 + return false; 1.1018 +} 1.1019 + 1.1020 + 1.1021 +/** 1.1022 + * Returns true if this is one of the specially treated methods for 1.1023 + * security related stack walks (like Reflection.getCallerClass). 1.1024 + */ 1.1025 +bool Method::is_ignored_by_security_stack_walk() const { 1.1026 + const bool use_new_reflection = JDK_Version::is_gte_jdk14x_version() && UseNewReflection; 1.1027 + 1.1028 + if (intrinsic_id() == vmIntrinsics::_invoke) { 1.1029 + // This is Method.invoke() -- ignore it 1.1030 + return true; 1.1031 + } 1.1032 + if (use_new_reflection && 1.1033 + method_holder()->is_subclass_of(SystemDictionary::reflect_MethodAccessorImpl_klass())) { 1.1034 + // This is an auxilary frame -- ignore it 1.1035 + return true; 1.1036 + } 1.1037 + if (is_method_handle_intrinsic() || is_compiled_lambda_form()) { 1.1038 + // This is an internal adapter frame for method handles -- ignore it 1.1039 + return true; 1.1040 + } 1.1041 + return false; 1.1042 +} 1.1043 + 1.1044 + 1.1045 +// Constant pool structure for invoke methods: 1.1046 +enum { 1.1047 + _imcp_invoke_name = 1, // utf8: 'invokeExact', etc. 1.1048 + _imcp_invoke_signature, // utf8: (variable Symbol*) 1.1049 + _imcp_limit 1.1050 +}; 1.1051 + 1.1052 +// Test if this method is an MH adapter frame generated by Java code. 1.1053 +// Cf. java/lang/invoke/InvokerBytecodeGenerator 1.1054 +bool Method::is_compiled_lambda_form() const { 1.1055 + return intrinsic_id() == vmIntrinsics::_compiledLambdaForm; 1.1056 +} 1.1057 + 1.1058 +// Test if this method is an internal MH primitive method. 1.1059 +bool Method::is_method_handle_intrinsic() const { 1.1060 + vmIntrinsics::ID iid = intrinsic_id(); 1.1061 + return (MethodHandles::is_signature_polymorphic(iid) && 1.1062 + MethodHandles::is_signature_polymorphic_intrinsic(iid)); 1.1063 +} 1.1064 + 1.1065 +bool Method::has_member_arg() const { 1.1066 + vmIntrinsics::ID iid = intrinsic_id(); 1.1067 + return (MethodHandles::is_signature_polymorphic(iid) && 1.1068 + MethodHandles::has_member_arg(iid)); 1.1069 +} 1.1070 + 1.1071 +// Make an instance of a signature-polymorphic internal MH primitive. 1.1072 +methodHandle Method::make_method_handle_intrinsic(vmIntrinsics::ID iid, 1.1073 + Symbol* signature, 1.1074 + TRAPS) { 1.1075 + ResourceMark rm; 1.1076 + methodHandle empty; 1.1077 + 1.1078 + KlassHandle holder = SystemDictionary::MethodHandle_klass(); 1.1079 + Symbol* name = MethodHandles::signature_polymorphic_intrinsic_name(iid); 1.1080 + assert(iid == MethodHandles::signature_polymorphic_name_id(name), ""); 1.1081 + if (TraceMethodHandles) { 1.1082 + tty->print_cr("make_method_handle_intrinsic MH.%s%s", name->as_C_string(), signature->as_C_string()); 1.1083 + } 1.1084 + 1.1085 + // invariant: cp->symbol_at_put is preceded by a refcount increment (more usually a lookup) 1.1086 + name->increment_refcount(); 1.1087 + signature->increment_refcount(); 1.1088 + 1.1089 + int cp_length = _imcp_limit; 1.1090 + ClassLoaderData* loader_data = holder->class_loader_data(); 1.1091 + constantPoolHandle cp; 1.1092 + { 1.1093 + ConstantPool* cp_oop = ConstantPool::allocate(loader_data, cp_length, CHECK_(empty)); 1.1094 + cp = constantPoolHandle(THREAD, cp_oop); 1.1095 + } 1.1096 + cp->set_pool_holder(InstanceKlass::cast(holder())); 1.1097 + cp->symbol_at_put(_imcp_invoke_name, name); 1.1098 + cp->symbol_at_put(_imcp_invoke_signature, signature); 1.1099 + cp->set_has_preresolution(); 1.1100 + 1.1101 + // decide on access bits: public or not? 1.1102 + int flags_bits = (JVM_ACC_NATIVE | JVM_ACC_SYNTHETIC | JVM_ACC_FINAL); 1.1103 + bool must_be_static = MethodHandles::is_signature_polymorphic_static(iid); 1.1104 + if (must_be_static) flags_bits |= JVM_ACC_STATIC; 1.1105 + assert((flags_bits & JVM_ACC_PUBLIC) == 0, "do not expose these methods"); 1.1106 + 1.1107 + methodHandle m; 1.1108 + { 1.1109 + InlineTableSizes sizes; 1.1110 + Method* m_oop = Method::allocate(loader_data, 0, 1.1111 + accessFlags_from(flags_bits), &sizes, 1.1112 + ConstMethod::NORMAL, CHECK_(empty)); 1.1113 + m = methodHandle(THREAD, m_oop); 1.1114 + } 1.1115 + m->set_constants(cp()); 1.1116 + m->set_name_index(_imcp_invoke_name); 1.1117 + m->set_signature_index(_imcp_invoke_signature); 1.1118 + assert(MethodHandles::is_signature_polymorphic_name(m->name()), ""); 1.1119 + assert(m->signature() == signature, ""); 1.1120 +#ifdef CC_INTERP 1.1121 + ResultTypeFinder rtf(signature); 1.1122 + m->set_result_index(rtf.type()); 1.1123 +#endif 1.1124 + m->compute_size_of_parameters(THREAD); 1.1125 + m->init_intrinsic_id(); 1.1126 + assert(m->is_method_handle_intrinsic(), ""); 1.1127 +#ifdef ASSERT 1.1128 + if (!MethodHandles::is_signature_polymorphic(m->intrinsic_id())) m->print(); 1.1129 + assert(MethodHandles::is_signature_polymorphic(m->intrinsic_id()), "must be an invoker"); 1.1130 + assert(m->intrinsic_id() == iid, "correctly predicted iid"); 1.1131 +#endif //ASSERT 1.1132 + 1.1133 + // Finally, set up its entry points. 1.1134 + assert(m->can_be_statically_bound(), ""); 1.1135 + m->set_vtable_index(Method::nonvirtual_vtable_index); 1.1136 + m->link_method(m, CHECK_(empty)); 1.1137 + 1.1138 + if (TraceMethodHandles && (Verbose || WizardMode)) 1.1139 + m->print_on(tty); 1.1140 + 1.1141 + return m; 1.1142 +} 1.1143 + 1.1144 +Klass* Method::check_non_bcp_klass(Klass* klass) { 1.1145 + if (klass != NULL && klass->class_loader() != NULL) { 1.1146 + if (klass->oop_is_objArray()) 1.1147 + klass = ObjArrayKlass::cast(klass)->bottom_klass(); 1.1148 + return klass; 1.1149 + } 1.1150 + return NULL; 1.1151 +} 1.1152 + 1.1153 + 1.1154 +methodHandle Method::clone_with_new_data(methodHandle m, u_char* new_code, int new_code_length, 1.1155 + u_char* new_compressed_linenumber_table, int new_compressed_linenumber_size, TRAPS) { 1.1156 + // Code below does not work for native methods - they should never get rewritten anyway 1.1157 + assert(!m->is_native(), "cannot rewrite native methods"); 1.1158 + // Allocate new Method* 1.1159 + AccessFlags flags = m->access_flags(); 1.1160 + 1.1161 + ConstMethod* cm = m->constMethod(); 1.1162 + int checked_exceptions_len = cm->checked_exceptions_length(); 1.1163 + int localvariable_len = cm->localvariable_table_length(); 1.1164 + int exception_table_len = cm->exception_table_length(); 1.1165 + int method_parameters_len = cm->method_parameters_length(); 1.1166 + int method_annotations_len = cm->method_annotations_length(); 1.1167 + int parameter_annotations_len = cm->parameter_annotations_length(); 1.1168 + int type_annotations_len = cm->type_annotations_length(); 1.1169 + int default_annotations_len = cm->default_annotations_length(); 1.1170 + 1.1171 + InlineTableSizes sizes( 1.1172 + localvariable_len, 1.1173 + new_compressed_linenumber_size, 1.1174 + exception_table_len, 1.1175 + checked_exceptions_len, 1.1176 + method_parameters_len, 1.1177 + cm->generic_signature_index(), 1.1178 + method_annotations_len, 1.1179 + parameter_annotations_len, 1.1180 + type_annotations_len, 1.1181 + default_annotations_len, 1.1182 + 0); 1.1183 + 1.1184 + ClassLoaderData* loader_data = m->method_holder()->class_loader_data(); 1.1185 + Method* newm_oop = Method::allocate(loader_data, 1.1186 + new_code_length, 1.1187 + flags, 1.1188 + &sizes, 1.1189 + m->method_type(), 1.1190 + CHECK_(methodHandle())); 1.1191 + methodHandle newm (THREAD, newm_oop); 1.1192 + int new_method_size = newm->method_size(); 1.1193 + 1.1194 + // Create a shallow copy of Method part, but be careful to preserve the new ConstMethod* 1.1195 + ConstMethod* newcm = newm->constMethod(); 1.1196 + int new_const_method_size = newm->constMethod()->size(); 1.1197 + 1.1198 + memcpy(newm(), m(), sizeof(Method)); 1.1199 + 1.1200 + // Create shallow copy of ConstMethod. 1.1201 + memcpy(newcm, m->constMethod(), sizeof(ConstMethod)); 1.1202 + 1.1203 + // Reset correct method/const method, method size, and parameter info 1.1204 + newm->set_constMethod(newcm); 1.1205 + newm->constMethod()->set_code_size(new_code_length); 1.1206 + newm->constMethod()->set_constMethod_size(new_const_method_size); 1.1207 + newm->set_method_size(new_method_size); 1.1208 + assert(newm->code_size() == new_code_length, "check"); 1.1209 + assert(newm->method_parameters_length() == method_parameters_len, "check"); 1.1210 + assert(newm->checked_exceptions_length() == checked_exceptions_len, "check"); 1.1211 + assert(newm->exception_table_length() == exception_table_len, "check"); 1.1212 + assert(newm->localvariable_table_length() == localvariable_len, "check"); 1.1213 + // Copy new byte codes 1.1214 + memcpy(newm->code_base(), new_code, new_code_length); 1.1215 + // Copy line number table 1.1216 + if (new_compressed_linenumber_size > 0) { 1.1217 + memcpy(newm->compressed_linenumber_table(), 1.1218 + new_compressed_linenumber_table, 1.1219 + new_compressed_linenumber_size); 1.1220 + } 1.1221 + // Copy method_parameters 1.1222 + if (method_parameters_len > 0) { 1.1223 + memcpy(newm->method_parameters_start(), 1.1224 + m->method_parameters_start(), 1.1225 + method_parameters_len * sizeof(MethodParametersElement)); 1.1226 + } 1.1227 + // Copy checked_exceptions 1.1228 + if (checked_exceptions_len > 0) { 1.1229 + memcpy(newm->checked_exceptions_start(), 1.1230 + m->checked_exceptions_start(), 1.1231 + checked_exceptions_len * sizeof(CheckedExceptionElement)); 1.1232 + } 1.1233 + // Copy exception table 1.1234 + if (exception_table_len > 0) { 1.1235 + memcpy(newm->exception_table_start(), 1.1236 + m->exception_table_start(), 1.1237 + exception_table_len * sizeof(ExceptionTableElement)); 1.1238 + } 1.1239 + // Copy local variable number table 1.1240 + if (localvariable_len > 0) { 1.1241 + memcpy(newm->localvariable_table_start(), 1.1242 + m->localvariable_table_start(), 1.1243 + localvariable_len * sizeof(LocalVariableTableElement)); 1.1244 + } 1.1245 + // Copy stackmap table 1.1246 + if (m->has_stackmap_table()) { 1.1247 + int code_attribute_length = m->stackmap_data()->length(); 1.1248 + Array<u1>* stackmap_data = 1.1249 + MetadataFactory::new_array<u1>(loader_data, code_attribute_length, 0, CHECK_NULL); 1.1250 + memcpy((void*)stackmap_data->adr_at(0), 1.1251 + (void*)m->stackmap_data()->adr_at(0), code_attribute_length); 1.1252 + newm->set_stackmap_data(stackmap_data); 1.1253 + } 1.1254 + 1.1255 + // copy annotations over to new method 1.1256 + newcm->copy_annotations_from(cm); 1.1257 + return newm; 1.1258 +} 1.1259 + 1.1260 +vmSymbols::SID Method::klass_id_for_intrinsics(Klass* holder) { 1.1261 + // if loader is not the default loader (i.e., != NULL), we can't know the intrinsics 1.1262 + // because we are not loading from core libraries 1.1263 + // exception: the AES intrinsics come from lib/ext/sunjce_provider.jar 1.1264 + // which does not use the class default class loader so we check for its loader here 1.1265 + InstanceKlass* ik = InstanceKlass::cast(holder); 1.1266 + if ((ik->class_loader() != NULL) && !SystemDictionary::is_ext_class_loader(ik->class_loader())) { 1.1267 + return vmSymbols::NO_SID; // regardless of name, no intrinsics here 1.1268 + } 1.1269 + 1.1270 + // see if the klass name is well-known: 1.1271 + Symbol* klass_name = ik->name(); 1.1272 + return vmSymbols::find_sid(klass_name); 1.1273 +} 1.1274 + 1.1275 +void Method::init_intrinsic_id() { 1.1276 + assert(_intrinsic_id == vmIntrinsics::_none, "do this just once"); 1.1277 + const uintptr_t max_id_uint = right_n_bits((int)(sizeof(_intrinsic_id) * BitsPerByte)); 1.1278 + assert((uintptr_t)vmIntrinsics::ID_LIMIT <= max_id_uint, "else fix size"); 1.1279 + assert(intrinsic_id_size_in_bytes() == sizeof(_intrinsic_id), ""); 1.1280 + 1.1281 + // the klass name is well-known: 1.1282 + vmSymbols::SID klass_id = klass_id_for_intrinsics(method_holder()); 1.1283 + assert(klass_id != vmSymbols::NO_SID, "caller responsibility"); 1.1284 + 1.1285 + // ditto for method and signature: 1.1286 + vmSymbols::SID name_id = vmSymbols::find_sid(name()); 1.1287 + if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) 1.1288 + && name_id == vmSymbols::NO_SID) 1.1289 + return; 1.1290 + vmSymbols::SID sig_id = vmSymbols::find_sid(signature()); 1.1291 + if (klass_id != vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle) 1.1292 + && sig_id == vmSymbols::NO_SID) return; 1.1293 + jshort flags = access_flags().as_short(); 1.1294 + 1.1295 + vmIntrinsics::ID id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); 1.1296 + if (id != vmIntrinsics::_none) { 1.1297 + set_intrinsic_id(id); 1.1298 + return; 1.1299 + } 1.1300 + 1.1301 + // A few slightly irregular cases: 1.1302 + switch (klass_id) { 1.1303 + case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_StrictMath): 1.1304 + // Second chance: check in regular Math. 1.1305 + switch (name_id) { 1.1306 + case vmSymbols::VM_SYMBOL_ENUM_NAME(min_name): 1.1307 + case vmSymbols::VM_SYMBOL_ENUM_NAME(max_name): 1.1308 + case vmSymbols::VM_SYMBOL_ENUM_NAME(sqrt_name): 1.1309 + // pretend it is the corresponding method in the non-strict class: 1.1310 + klass_id = vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_Math); 1.1311 + id = vmIntrinsics::find_id(klass_id, name_id, sig_id, flags); 1.1312 + break; 1.1313 + } 1.1314 + break; 1.1315 + 1.1316 + // Signature-polymorphic methods: MethodHandle.invoke*, InvokeDynamic.*. 1.1317 + case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_MethodHandle): 1.1318 + if (!is_native()) break; 1.1319 + id = MethodHandles::signature_polymorphic_name_id(method_holder(), name()); 1.1320 + if (is_static() != MethodHandles::is_signature_polymorphic_static(id)) 1.1321 + id = vmIntrinsics::_none; 1.1322 + break; 1.1323 + } 1.1324 + 1.1325 + if (id != vmIntrinsics::_none) { 1.1326 + // Set up its iid. It is an alias method. 1.1327 + set_intrinsic_id(id); 1.1328 + return; 1.1329 + } 1.1330 +} 1.1331 + 1.1332 +// These two methods are static since a GC may move the Method 1.1333 +bool Method::load_signature_classes(methodHandle m, TRAPS) { 1.1334 + if (THREAD->is_Compiler_thread()) { 1.1335 + // There is nothing useful this routine can do from within the Compile thread. 1.1336 + // Hopefully, the signature contains only well-known classes. 1.1337 + // We could scan for this and return true/false, but the caller won't care. 1.1338 + return false; 1.1339 + } 1.1340 + bool sig_is_loaded = true; 1.1341 + Handle class_loader(THREAD, m->method_holder()->class_loader()); 1.1342 + Handle protection_domain(THREAD, m->method_holder()->protection_domain()); 1.1343 + ResourceMark rm(THREAD); 1.1344 + Symbol* signature = m->signature(); 1.1345 + for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { 1.1346 + if (ss.is_object()) { 1.1347 + Symbol* sym = ss.as_symbol(CHECK_(false)); 1.1348 + Symbol* name = sym; 1.1349 + Klass* klass = SystemDictionary::resolve_or_null(name, class_loader, 1.1350 + protection_domain, THREAD); 1.1351 + // We are loading classes eagerly. If a ClassNotFoundException or 1.1352 + // a LinkageError was generated, be sure to ignore it. 1.1353 + if (HAS_PENDING_EXCEPTION) { 1.1354 + if (PENDING_EXCEPTION->is_a(SystemDictionary::ClassNotFoundException_klass()) || 1.1355 + PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { 1.1356 + CLEAR_PENDING_EXCEPTION; 1.1357 + } else { 1.1358 + return false; 1.1359 + } 1.1360 + } 1.1361 + if( klass == NULL) { sig_is_loaded = false; } 1.1362 + } 1.1363 + } 1.1364 + return sig_is_loaded; 1.1365 +} 1.1366 + 1.1367 +bool Method::has_unloaded_classes_in_signature(methodHandle m, TRAPS) { 1.1368 + Handle class_loader(THREAD, m->method_holder()->class_loader()); 1.1369 + Handle protection_domain(THREAD, m->method_holder()->protection_domain()); 1.1370 + ResourceMark rm(THREAD); 1.1371 + Symbol* signature = m->signature(); 1.1372 + for(SignatureStream ss(signature); !ss.is_done(); ss.next()) { 1.1373 + if (ss.type() == T_OBJECT) { 1.1374 + Symbol* name = ss.as_symbol_or_null(); 1.1375 + if (name == NULL) return true; 1.1376 + Klass* klass = SystemDictionary::find(name, class_loader, protection_domain, THREAD); 1.1377 + if (klass == NULL) return true; 1.1378 + } 1.1379 + } 1.1380 + return false; 1.1381 +} 1.1382 + 1.1383 +// Exposed so field engineers can debug VM 1.1384 +void Method::print_short_name(outputStream* st) { 1.1385 + ResourceMark rm; 1.1386 +#ifdef PRODUCT 1.1387 + st->print(" %s::", method_holder()->external_name()); 1.1388 +#else 1.1389 + st->print(" %s::", method_holder()->internal_name()); 1.1390 +#endif 1.1391 + name()->print_symbol_on(st); 1.1392 + if (WizardMode) signature()->print_symbol_on(st); 1.1393 + else if (MethodHandles::is_signature_polymorphic(intrinsic_id())) 1.1394 + MethodHandles::print_as_basic_type_signature_on(st, signature(), true); 1.1395 +} 1.1396 + 1.1397 +// Comparer for sorting an object array containing 1.1398 +// Method*s. 1.1399 +static int method_comparator(Method* a, Method* b) { 1.1400 + return a->name()->fast_compare(b->name()); 1.1401 +} 1.1402 + 1.1403 +// This is only done during class loading, so it is OK to assume method_idnum matches the methods() array 1.1404 +// default_methods also uses this without the ordering for fast find_method 1.1405 +void Method::sort_methods(Array<Method*>* methods, bool idempotent, bool set_idnums) { 1.1406 + int length = methods->length(); 1.1407 + if (length > 1) { 1.1408 + { 1.1409 + No_Safepoint_Verifier nsv; 1.1410 + QuickSort::sort<Method*>(methods->data(), length, method_comparator, idempotent); 1.1411 + } 1.1412 + // Reset method ordering 1.1413 + if (set_idnums) { 1.1414 + for (int i = 0; i < length; i++) { 1.1415 + Method* m = methods->at(i); 1.1416 + m->set_method_idnum(i); 1.1417 + } 1.1418 + } 1.1419 + } 1.1420 +} 1.1421 + 1.1422 +//----------------------------------------------------------------------------------- 1.1423 +// Non-product code unless JVM/TI needs it 1.1424 + 1.1425 +#if !defined(PRODUCT) || INCLUDE_JVMTI 1.1426 +class SignatureTypePrinter : public SignatureTypeNames { 1.1427 + private: 1.1428 + outputStream* _st; 1.1429 + bool _use_separator; 1.1430 + 1.1431 + void type_name(const char* name) { 1.1432 + if (_use_separator) _st->print(", "); 1.1433 + _st->print("%s", name); 1.1434 + _use_separator = true; 1.1435 + } 1.1436 + 1.1437 + public: 1.1438 + SignatureTypePrinter(Symbol* signature, outputStream* st) : SignatureTypeNames(signature) { 1.1439 + _st = st; 1.1440 + _use_separator = false; 1.1441 + } 1.1442 + 1.1443 + void print_parameters() { _use_separator = false; iterate_parameters(); } 1.1444 + void print_returntype() { _use_separator = false; iterate_returntype(); } 1.1445 +}; 1.1446 + 1.1447 + 1.1448 +void Method::print_name(outputStream* st) { 1.1449 + Thread *thread = Thread::current(); 1.1450 + ResourceMark rm(thread); 1.1451 + SignatureTypePrinter sig(signature(), st); 1.1452 + st->print("%s ", is_static() ? "static" : "virtual"); 1.1453 + sig.print_returntype(); 1.1454 + st->print(" %s.", method_holder()->internal_name()); 1.1455 + name()->print_symbol_on(st); 1.1456 + st->print("("); 1.1457 + sig.print_parameters(); 1.1458 + st->print(")"); 1.1459 +} 1.1460 +#endif // !PRODUCT || INCLUDE_JVMTI 1.1461 + 1.1462 + 1.1463 +//----------------------------------------------------------------------------------- 1.1464 +// Non-product code 1.1465 + 1.1466 +#ifndef PRODUCT 1.1467 +void Method::print_codes_on(outputStream* st) const { 1.1468 + print_codes_on(0, code_size(), st); 1.1469 +} 1.1470 + 1.1471 +void Method::print_codes_on(int from, int to, outputStream* st) const { 1.1472 + Thread *thread = Thread::current(); 1.1473 + ResourceMark rm(thread); 1.1474 + methodHandle mh (thread, (Method*)this); 1.1475 + BytecodeStream s(mh); 1.1476 + s.set_interval(from, to); 1.1477 + BytecodeTracer::set_closure(BytecodeTracer::std_closure()); 1.1478 + while (s.next() >= 0) BytecodeTracer::trace(mh, s.bcp(), st); 1.1479 +} 1.1480 +#endif // not PRODUCT 1.1481 + 1.1482 + 1.1483 +// Simple compression of line number tables. We use a regular compressed stream, except that we compress deltas 1.1484 +// between (bci,line) pairs since they are smaller. If (bci delta, line delta) fits in (5-bit unsigned, 3-bit unsigned) 1.1485 +// we save it as one byte, otherwise we write a 0xFF escape character and use regular compression. 0x0 is used 1.1486 +// as end-of-stream terminator. 1.1487 + 1.1488 +void CompressedLineNumberWriteStream::write_pair_regular(int bci_delta, int line_delta) { 1.1489 + // bci and line number does not compress into single byte. 1.1490 + // Write out escape character and use regular compression for bci and line number. 1.1491 + write_byte((jubyte)0xFF); 1.1492 + write_signed_int(bci_delta); 1.1493 + write_signed_int(line_delta); 1.1494 +} 1.1495 + 1.1496 +// See comment in method.hpp which explains why this exists. 1.1497 +#if defined(_M_AMD64) && _MSC_VER >= 1400 1.1498 +#pragma optimize("", off) 1.1499 +void CompressedLineNumberWriteStream::write_pair(int bci, int line) { 1.1500 + write_pair_inline(bci, line); 1.1501 +} 1.1502 +#pragma optimize("", on) 1.1503 +#endif 1.1504 + 1.1505 +CompressedLineNumberReadStream::CompressedLineNumberReadStream(u_char* buffer) : CompressedReadStream(buffer) { 1.1506 + _bci = 0; 1.1507 + _line = 0; 1.1508 +}; 1.1509 + 1.1510 + 1.1511 +bool CompressedLineNumberReadStream::read_pair() { 1.1512 + jubyte next = read_byte(); 1.1513 + // Check for terminator 1.1514 + if (next == 0) return false; 1.1515 + if (next == 0xFF) { 1.1516 + // Escape character, regular compression used 1.1517 + _bci += read_signed_int(); 1.1518 + _line += read_signed_int(); 1.1519 + } else { 1.1520 + // Single byte compression used 1.1521 + _bci += next >> 3; 1.1522 + _line += next & 0x7; 1.1523 + } 1.1524 + return true; 1.1525 +} 1.1526 + 1.1527 + 1.1528 +Bytecodes::Code Method::orig_bytecode_at(int bci) const { 1.1529 + BreakpointInfo* bp = method_holder()->breakpoints(); 1.1530 + for (; bp != NULL; bp = bp->next()) { 1.1531 + if (bp->match(this, bci)) { 1.1532 + return bp->orig_bytecode(); 1.1533 + } 1.1534 + } 1.1535 + { 1.1536 + ResourceMark rm; 1.1537 + fatal(err_msg("no original bytecode found in %s at bci %d", name_and_sig_as_C_string(), bci)); 1.1538 + } 1.1539 + return Bytecodes::_shouldnotreachhere; 1.1540 +} 1.1541 + 1.1542 +void Method::set_orig_bytecode_at(int bci, Bytecodes::Code code) { 1.1543 + assert(code != Bytecodes::_breakpoint, "cannot patch breakpoints this way"); 1.1544 + BreakpointInfo* bp = method_holder()->breakpoints(); 1.1545 + for (; bp != NULL; bp = bp->next()) { 1.1546 + if (bp->match(this, bci)) { 1.1547 + bp->set_orig_bytecode(code); 1.1548 + // and continue, in case there is more than one 1.1549 + } 1.1550 + } 1.1551 +} 1.1552 + 1.1553 +void Method::set_breakpoint(int bci) { 1.1554 + InstanceKlass* ik = method_holder(); 1.1555 + BreakpointInfo *bp = new BreakpointInfo(this, bci); 1.1556 + bp->set_next(ik->breakpoints()); 1.1557 + ik->set_breakpoints(bp); 1.1558 + // do this last: 1.1559 + bp->set(this); 1.1560 +} 1.1561 + 1.1562 +static void clear_matches(Method* m, int bci) { 1.1563 + InstanceKlass* ik = m->method_holder(); 1.1564 + BreakpointInfo* prev_bp = NULL; 1.1565 + BreakpointInfo* next_bp; 1.1566 + for (BreakpointInfo* bp = ik->breakpoints(); bp != NULL; bp = next_bp) { 1.1567 + next_bp = bp->next(); 1.1568 + // bci value of -1 is used to delete all breakpoints in method m (ex: clear_all_breakpoint). 1.1569 + if (bci >= 0 ? bp->match(m, bci) : bp->match(m)) { 1.1570 + // do this first: 1.1571 + bp->clear(m); 1.1572 + // unhook it 1.1573 + if (prev_bp != NULL) 1.1574 + prev_bp->set_next(next_bp); 1.1575 + else 1.1576 + ik->set_breakpoints(next_bp); 1.1577 + delete bp; 1.1578 + // When class is redefined JVMTI sets breakpoint in all versions of EMCP methods 1.1579 + // at same location. So we have multiple matching (method_index and bci) 1.1580 + // BreakpointInfo nodes in BreakpointInfo list. We should just delete one 1.1581 + // breakpoint for clear_breakpoint request and keep all other method versions 1.1582 + // BreakpointInfo for future clear_breakpoint request. 1.1583 + // bcivalue of -1 is used to clear all breakpoints (see clear_all_breakpoints) 1.1584 + // which is being called when class is unloaded. We delete all the Breakpoint 1.1585 + // information for all versions of method. We may not correctly restore the original 1.1586 + // bytecode in all method versions, but that is ok. Because the class is being unloaded 1.1587 + // so these methods won't be used anymore. 1.1588 + if (bci >= 0) { 1.1589 + break; 1.1590 + } 1.1591 + } else { 1.1592 + // This one is a keeper. 1.1593 + prev_bp = bp; 1.1594 + } 1.1595 + } 1.1596 +} 1.1597 + 1.1598 +void Method::clear_breakpoint(int bci) { 1.1599 + assert(bci >= 0, ""); 1.1600 + clear_matches(this, bci); 1.1601 +} 1.1602 + 1.1603 +void Method::clear_all_breakpoints() { 1.1604 + clear_matches(this, -1); 1.1605 +} 1.1606 + 1.1607 + 1.1608 +int Method::invocation_count() { 1.1609 + MethodCounters *mcs = method_counters(); 1.1610 + if (TieredCompilation) { 1.1611 + MethodData* const mdo = method_data(); 1.1612 + if (((mcs != NULL) ? mcs->invocation_counter()->carry() : false) || 1.1613 + ((mdo != NULL) ? mdo->invocation_counter()->carry() : false)) { 1.1614 + return InvocationCounter::count_limit; 1.1615 + } else { 1.1616 + return ((mcs != NULL) ? mcs->invocation_counter()->count() : 0) + 1.1617 + ((mdo != NULL) ? mdo->invocation_counter()->count() : 0); 1.1618 + } 1.1619 + } else { 1.1620 + return (mcs == NULL) ? 0 : mcs->invocation_counter()->count(); 1.1621 + } 1.1622 +} 1.1623 + 1.1624 +int Method::backedge_count() { 1.1625 + MethodCounters *mcs = method_counters(); 1.1626 + if (TieredCompilation) { 1.1627 + MethodData* const mdo = method_data(); 1.1628 + if (((mcs != NULL) ? mcs->backedge_counter()->carry() : false) || 1.1629 + ((mdo != NULL) ? mdo->backedge_counter()->carry() : false)) { 1.1630 + return InvocationCounter::count_limit; 1.1631 + } else { 1.1632 + return ((mcs != NULL) ? mcs->backedge_counter()->count() : 0) + 1.1633 + ((mdo != NULL) ? mdo->backedge_counter()->count() : 0); 1.1634 + } 1.1635 + } else { 1.1636 + return (mcs == NULL) ? 0 : mcs->backedge_counter()->count(); 1.1637 + } 1.1638 +} 1.1639 + 1.1640 +int Method::highest_comp_level() const { 1.1641 + const MethodData* mdo = method_data(); 1.1642 + if (mdo != NULL) { 1.1643 + return mdo->highest_comp_level(); 1.1644 + } else { 1.1645 + return CompLevel_none; 1.1646 + } 1.1647 +} 1.1648 + 1.1649 +int Method::highest_osr_comp_level() const { 1.1650 + const MethodData* mdo = method_data(); 1.1651 + if (mdo != NULL) { 1.1652 + return mdo->highest_osr_comp_level(); 1.1653 + } else { 1.1654 + return CompLevel_none; 1.1655 + } 1.1656 +} 1.1657 + 1.1658 +void Method::set_highest_comp_level(int level) { 1.1659 + MethodData* mdo = method_data(); 1.1660 + if (mdo != NULL) { 1.1661 + mdo->set_highest_comp_level(level); 1.1662 + } 1.1663 +} 1.1664 + 1.1665 +void Method::set_highest_osr_comp_level(int level) { 1.1666 + MethodData* mdo = method_data(); 1.1667 + if (mdo != NULL) { 1.1668 + mdo->set_highest_osr_comp_level(level); 1.1669 + } 1.1670 +} 1.1671 + 1.1672 +BreakpointInfo::BreakpointInfo(Method* m, int bci) { 1.1673 + _bci = bci; 1.1674 + _name_index = m->name_index(); 1.1675 + _signature_index = m->signature_index(); 1.1676 + _orig_bytecode = (Bytecodes::Code) *m->bcp_from(_bci); 1.1677 + if (_orig_bytecode == Bytecodes::_breakpoint) 1.1678 + _orig_bytecode = m->orig_bytecode_at(_bci); 1.1679 + _next = NULL; 1.1680 +} 1.1681 + 1.1682 +void BreakpointInfo::set(Method* method) { 1.1683 +#ifdef ASSERT 1.1684 + { 1.1685 + Bytecodes::Code code = (Bytecodes::Code) *method->bcp_from(_bci); 1.1686 + if (code == Bytecodes::_breakpoint) 1.1687 + code = method->orig_bytecode_at(_bci); 1.1688 + assert(orig_bytecode() == code, "original bytecode must be the same"); 1.1689 + } 1.1690 +#endif 1.1691 + Thread *thread = Thread::current(); 1.1692 + *method->bcp_from(_bci) = Bytecodes::_breakpoint; 1.1693 + method->incr_number_of_breakpoints(thread); 1.1694 + SystemDictionary::notice_modification(); 1.1695 + { 1.1696 + // Deoptimize all dependents on this method 1.1697 + HandleMark hm(thread); 1.1698 + methodHandle mh(thread, method); 1.1699 + Universe::flush_dependents_on_method(mh); 1.1700 + } 1.1701 +} 1.1702 + 1.1703 +void BreakpointInfo::clear(Method* method) { 1.1704 + *method->bcp_from(_bci) = orig_bytecode(); 1.1705 + assert(method->number_of_breakpoints() > 0, "must not go negative"); 1.1706 + method->decr_number_of_breakpoints(Thread::current()); 1.1707 +} 1.1708 + 1.1709 +// jmethodID handling 1.1710 + 1.1711 +// This is a block allocating object, sort of like JNIHandleBlock, only a 1.1712 +// lot simpler. There aren't many of these, they aren't long, they are rarely 1.1713 +// deleted and so we can do some suboptimal things. 1.1714 +// It's allocated on the CHeap because once we allocate a jmethodID, we can 1.1715 +// never get rid of it. 1.1716 +// It would be nice to be able to parameterize the number of methods for 1.1717 +// the null_class_loader but then we'd have to turn this and ClassLoaderData 1.1718 +// into templates. 1.1719 + 1.1720 +// I feel like this brain dead class should exist somewhere in the STL 1.1721 + 1.1722 +class JNIMethodBlock : public CHeapObj<mtClass> { 1.1723 + enum { number_of_methods = 8 }; 1.1724 + 1.1725 + Method* _methods[number_of_methods]; 1.1726 + int _top; 1.1727 + JNIMethodBlock* _next; 1.1728 + public: 1.1729 + static Method* const _free_method; 1.1730 + 1.1731 + JNIMethodBlock() : _next(NULL), _top(0) { 1.1732 + for (int i = 0; i< number_of_methods; i++) _methods[i] = _free_method; 1.1733 + } 1.1734 + 1.1735 + Method** add_method(Method* m) { 1.1736 + if (_top < number_of_methods) { 1.1737 + // top points to the next free entry. 1.1738 + int i = _top; 1.1739 + _methods[i] = m; 1.1740 + _top++; 1.1741 + return &_methods[i]; 1.1742 + } else if (_top == number_of_methods) { 1.1743 + // if the next free entry ran off the block see if there's a free entry 1.1744 + for (int i = 0; i< number_of_methods; i++) { 1.1745 + if (_methods[i] == _free_method) { 1.1746 + _methods[i] = m; 1.1747 + return &_methods[i]; 1.1748 + } 1.1749 + } 1.1750 + // Only check each block once for frees. They're very unlikely. 1.1751 + // Increment top past the end of the block. 1.1752 + _top++; 1.1753 + } 1.1754 + // need to allocate a next block. 1.1755 + if (_next == NULL) { 1.1756 + _next = new JNIMethodBlock(); 1.1757 + } 1.1758 + return _next->add_method(m); 1.1759 + } 1.1760 + 1.1761 + bool contains(Method** m) { 1.1762 + for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { 1.1763 + for (int i = 0; i< number_of_methods; i++) { 1.1764 + if (&(b->_methods[i]) == m) { 1.1765 + return true; 1.1766 + } 1.1767 + } 1.1768 + } 1.1769 + return false; // not found 1.1770 + } 1.1771 + 1.1772 + // Doesn't really destroy it, just marks it as free so it can be reused. 1.1773 + void destroy_method(Method** m) { 1.1774 +#ifdef ASSERT 1.1775 + assert(contains(m), "should be a methodID"); 1.1776 +#endif // ASSERT 1.1777 + *m = _free_method; 1.1778 + } 1.1779 + 1.1780 + // During class unloading the methods are cleared, which is different 1.1781 + // than freed. 1.1782 + void clear_all_methods() { 1.1783 + for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { 1.1784 + for (int i = 0; i< number_of_methods; i++) { 1.1785 + _methods[i] = NULL; 1.1786 + } 1.1787 + } 1.1788 + } 1.1789 +#ifndef PRODUCT 1.1790 + int count_methods() { 1.1791 + // count all allocated methods 1.1792 + int count = 0; 1.1793 + for (JNIMethodBlock* b = this; b != NULL; b = b->_next) { 1.1794 + for (int i = 0; i< number_of_methods; i++) { 1.1795 + if (_methods[i] != _free_method) count++; 1.1796 + } 1.1797 + } 1.1798 + return count; 1.1799 + } 1.1800 +#endif // PRODUCT 1.1801 +}; 1.1802 + 1.1803 +// Something that can't be mistaken for an address or a markOop 1.1804 +Method* const JNIMethodBlock::_free_method = (Method*)55; 1.1805 + 1.1806 +// Add a method id to the jmethod_ids 1.1807 +jmethodID Method::make_jmethod_id(ClassLoaderData* loader_data, Method* m) { 1.1808 + ClassLoaderData* cld = loader_data; 1.1809 + 1.1810 + if (!SafepointSynchronize::is_at_safepoint()) { 1.1811 + // Have to add jmethod_ids() to class loader data thread-safely. 1.1812 + // Also have to add the method to the list safely, which the cld lock 1.1813 + // protects as well. 1.1814 + MutexLockerEx ml(cld->metaspace_lock(), Mutex::_no_safepoint_check_flag); 1.1815 + if (cld->jmethod_ids() == NULL) { 1.1816 + cld->set_jmethod_ids(new JNIMethodBlock()); 1.1817 + } 1.1818 + // jmethodID is a pointer to Method* 1.1819 + return (jmethodID)cld->jmethod_ids()->add_method(m); 1.1820 + } else { 1.1821 + // At safepoint, we are single threaded and can set this. 1.1822 + if (cld->jmethod_ids() == NULL) { 1.1823 + cld->set_jmethod_ids(new JNIMethodBlock()); 1.1824 + } 1.1825 + // jmethodID is a pointer to Method* 1.1826 + return (jmethodID)cld->jmethod_ids()->add_method(m); 1.1827 + } 1.1828 +} 1.1829 + 1.1830 +// Mark a jmethodID as free. This is called when there is a data race in 1.1831 +// InstanceKlass while creating the jmethodID cache. 1.1832 +void Method::destroy_jmethod_id(ClassLoaderData* loader_data, jmethodID m) { 1.1833 + ClassLoaderData* cld = loader_data; 1.1834 + Method** ptr = (Method**)m; 1.1835 + assert(cld->jmethod_ids() != NULL, "should have method handles"); 1.1836 + cld->jmethod_ids()->destroy_method(ptr); 1.1837 +} 1.1838 + 1.1839 +void Method::change_method_associated_with_jmethod_id(jmethodID jmid, Method* new_method) { 1.1840 + // Can't assert the method_holder is the same because the new method has the 1.1841 + // scratch method holder. 1.1842 + assert(resolve_jmethod_id(jmid)->method_holder()->class_loader() 1.1843 + == new_method->method_holder()->class_loader(), 1.1844 + "changing to a different class loader"); 1.1845 + // Just change the method in place, jmethodID pointer doesn't change. 1.1846 + *((Method**)jmid) = new_method; 1.1847 +} 1.1848 + 1.1849 +bool Method::is_method_id(jmethodID mid) { 1.1850 + Method* m = resolve_jmethod_id(mid); 1.1851 + assert(m != NULL, "should be called with non-null method"); 1.1852 + InstanceKlass* ik = m->method_holder(); 1.1853 + ClassLoaderData* cld = ik->class_loader_data(); 1.1854 + if (cld->jmethod_ids() == NULL) return false; 1.1855 + return (cld->jmethod_ids()->contains((Method**)mid)); 1.1856 +} 1.1857 + 1.1858 +Method* Method::checked_resolve_jmethod_id(jmethodID mid) { 1.1859 + if (mid == NULL) return NULL; 1.1860 + Method* o = resolve_jmethod_id(mid); 1.1861 + if (o == NULL || o == JNIMethodBlock::_free_method || !((Metadata*)o)->is_method()) { 1.1862 + return NULL; 1.1863 + } 1.1864 + return o; 1.1865 +}; 1.1866 + 1.1867 +void Method::set_on_stack(const bool value) { 1.1868 + // Set both the method itself and its constant pool. The constant pool 1.1869 + // on stack means some method referring to it is also on the stack. 1.1870 + _access_flags.set_on_stack(value); 1.1871 + constants()->set_on_stack(value); 1.1872 + if (value) MetadataOnStackMark::record(this); 1.1873 +} 1.1874 + 1.1875 +// Called when the class loader is unloaded to make all methods weak. 1.1876 +void Method::clear_jmethod_ids(ClassLoaderData* loader_data) { 1.1877 + loader_data->jmethod_ids()->clear_all_methods(); 1.1878 +} 1.1879 + 1.1880 +bool Method::has_method_vptr(const void* ptr) { 1.1881 + Method m; 1.1882 + // This assumes that the vtbl pointer is the first word of a C++ object. 1.1883 + // This assumption is also in universe.cpp patch_klass_vtble 1.1884 + void* vtbl2 = dereference_vptr((const void*)&m); 1.1885 + void* this_vtbl = dereference_vptr(ptr); 1.1886 + return vtbl2 == this_vtbl; 1.1887 +} 1.1888 + 1.1889 +// Check that this pointer is valid by checking that the vtbl pointer matches 1.1890 +bool Method::is_valid_method() const { 1.1891 + if (this == NULL) { 1.1892 + return false; 1.1893 + } else if (!is_metaspace_object()) { 1.1894 + return false; 1.1895 + } else { 1.1896 + return has_method_vptr((const void*)this); 1.1897 + } 1.1898 +} 1.1899 + 1.1900 +#ifndef PRODUCT 1.1901 +void Method::print_jmethod_ids(ClassLoaderData* loader_data, outputStream* out) { 1.1902 + out->print_cr("jni_method_id count = %d", loader_data->jmethod_ids()->count_methods()); 1.1903 +} 1.1904 +#endif // PRODUCT 1.1905 + 1.1906 + 1.1907 +// Printing 1.1908 + 1.1909 +#ifndef PRODUCT 1.1910 + 1.1911 +void Method::print_on(outputStream* st) const { 1.1912 + ResourceMark rm; 1.1913 + assert(is_method(), "must be method"); 1.1914 + st->print_cr("%s", internal_name()); 1.1915 + // get the effect of PrintOopAddress, always, for methods: 1.1916 + st->print_cr(" - this oop: "INTPTR_FORMAT, (intptr_t)this); 1.1917 + st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr(); 1.1918 + st->print (" - constants: "INTPTR_FORMAT" ", (address)constants()); 1.1919 + constants()->print_value_on(st); st->cr(); 1.1920 + st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr(); 1.1921 + st->print (" - name: "); name()->print_value_on(st); st->cr(); 1.1922 + st->print (" - signature: "); signature()->print_value_on(st); st->cr(); 1.1923 + st->print_cr(" - max stack: %d", max_stack()); 1.1924 + st->print_cr(" - max locals: %d", max_locals()); 1.1925 + st->print_cr(" - size of params: %d", size_of_parameters()); 1.1926 + st->print_cr(" - method size: %d", method_size()); 1.1927 + if (intrinsic_id() != vmIntrinsics::_none) 1.1928 + st->print_cr(" - intrinsic id: %d %s", intrinsic_id(), vmIntrinsics::name_at(intrinsic_id())); 1.1929 + if (highest_comp_level() != CompLevel_none) 1.1930 + st->print_cr(" - highest level: %d", highest_comp_level()); 1.1931 + st->print_cr(" - vtable index: %d", _vtable_index); 1.1932 + st->print_cr(" - i2i entry: " INTPTR_FORMAT, interpreter_entry()); 1.1933 + st->print( " - adapters: "); 1.1934 + AdapterHandlerEntry* a = ((Method*)this)->adapter(); 1.1935 + if (a == NULL) 1.1936 + st->print_cr(INTPTR_FORMAT, a); 1.1937 + else 1.1938 + a->print_adapter_on(st); 1.1939 + st->print_cr(" - compiled entry " INTPTR_FORMAT, from_compiled_entry()); 1.1940 + st->print_cr(" - code size: %d", code_size()); 1.1941 + if (code_size() != 0) { 1.1942 + st->print_cr(" - code start: " INTPTR_FORMAT, code_base()); 1.1943 + st->print_cr(" - code end (excl): " INTPTR_FORMAT, code_base() + code_size()); 1.1944 + } 1.1945 + if (method_data() != NULL) { 1.1946 + st->print_cr(" - method data: " INTPTR_FORMAT, (address)method_data()); 1.1947 + } 1.1948 + st->print_cr(" - checked ex length: %d", checked_exceptions_length()); 1.1949 + if (checked_exceptions_length() > 0) { 1.1950 + CheckedExceptionElement* table = checked_exceptions_start(); 1.1951 + st->print_cr(" - checked ex start: " INTPTR_FORMAT, table); 1.1952 + if (Verbose) { 1.1953 + for (int i = 0; i < checked_exceptions_length(); i++) { 1.1954 + st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index)); 1.1955 + } 1.1956 + } 1.1957 + } 1.1958 + if (has_linenumber_table()) { 1.1959 + u_char* table = compressed_linenumber_table(); 1.1960 + st->print_cr(" - linenumber start: " INTPTR_FORMAT, table); 1.1961 + if (Verbose) { 1.1962 + CompressedLineNumberReadStream stream(table); 1.1963 + while (stream.read_pair()) { 1.1964 + st->print_cr(" - line %d: %d", stream.line(), stream.bci()); 1.1965 + } 1.1966 + } 1.1967 + } 1.1968 + st->print_cr(" - localvar length: %d", localvariable_table_length()); 1.1969 + if (localvariable_table_length() > 0) { 1.1970 + LocalVariableTableElement* table = localvariable_table_start(); 1.1971 + st->print_cr(" - localvar start: " INTPTR_FORMAT, table); 1.1972 + if (Verbose) { 1.1973 + for (int i = 0; i < localvariable_table_length(); i++) { 1.1974 + int bci = table[i].start_bci; 1.1975 + int len = table[i].length; 1.1976 + const char* name = constants()->printable_name_at(table[i].name_cp_index); 1.1977 + const char* desc = constants()->printable_name_at(table[i].descriptor_cp_index); 1.1978 + int slot = table[i].slot; 1.1979 + st->print_cr(" - %s %s bci=%d len=%d slot=%d", desc, name, bci, len, slot); 1.1980 + } 1.1981 + } 1.1982 + } 1.1983 + if (code() != NULL) { 1.1984 + st->print (" - compiled code: "); 1.1985 + code()->print_value_on(st); 1.1986 + } 1.1987 + if (is_native()) { 1.1988 + st->print_cr(" - native function: " INTPTR_FORMAT, native_function()); 1.1989 + st->print_cr(" - signature handler: " INTPTR_FORMAT, signature_handler()); 1.1990 + } 1.1991 +} 1.1992 + 1.1993 +#endif //PRODUCT 1.1994 + 1.1995 +void Method::print_value_on(outputStream* st) const { 1.1996 + assert(is_method(), "must be method"); 1.1997 + st->print("%s", internal_name()); 1.1998 + print_address_on(st); 1.1999 + st->print(" "); 1.2000 + name()->print_value_on(st); 1.2001 + st->print(" "); 1.2002 + signature()->print_value_on(st); 1.2003 + st->print(" in "); 1.2004 + method_holder()->print_value_on(st); 1.2005 + if (WizardMode) st->print("#%d", _vtable_index); 1.2006 + if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals()); 1.2007 + if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code()); 1.2008 +} 1.2009 + 1.2010 +#if INCLUDE_SERVICES 1.2011 +// Size Statistics 1.2012 +void Method::collect_statistics(KlassSizeStats *sz) const { 1.2013 + int mysize = sz->count(this); 1.2014 + sz->_method_bytes += mysize; 1.2015 + sz->_method_all_bytes += mysize; 1.2016 + sz->_rw_bytes += mysize; 1.2017 + 1.2018 + if (constMethod()) { 1.2019 + constMethod()->collect_statistics(sz); 1.2020 + } 1.2021 + if (method_data()) { 1.2022 + method_data()->collect_statistics(sz); 1.2023 + } 1.2024 +} 1.2025 +#endif // INCLUDE_SERVICES 1.2026 + 1.2027 +// Verification 1.2028 + 1.2029 +void Method::verify_on(outputStream* st) { 1.2030 + guarantee(is_method(), "object must be method"); 1.2031 + guarantee(constants()->is_constantPool(), "should be constant pool"); 1.2032 + guarantee(constMethod()->is_constMethod(), "should be ConstMethod*"); 1.2033 + MethodData* md = method_data(); 1.2034 + guarantee(md == NULL || 1.2035 + md->is_methodData(), "should be method data"); 1.2036 +}