Wed, 12 Oct 2011 21:00:13 -0700
7092712: JSR 292: unloaded invokedynamic call sites can lead to a crash with signature types not on BCP
Reviewed-by: jrose, never
1 /*
2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "ci/ciCallProfile.hpp"
27 #include "ci/ciExceptionHandler.hpp"
28 #include "ci/ciInstanceKlass.hpp"
29 #include "ci/ciMethod.hpp"
30 #include "ci/ciMethodBlocks.hpp"
31 #include "ci/ciMethodData.hpp"
32 #include "ci/ciMethodKlass.hpp"
33 #include "ci/ciStreams.hpp"
34 #include "ci/ciSymbol.hpp"
35 #include "ci/ciUtilities.hpp"
36 #include "classfile/systemDictionary.hpp"
37 #include "compiler/abstractCompiler.hpp"
38 #include "compiler/compilerOracle.hpp"
39 #include "compiler/methodLiveness.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "interpreter/linkResolver.hpp"
42 #include "interpreter/oopMapCache.hpp"
43 #include "memory/allocation.inline.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "oops/generateOopMap.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "prims/nativeLookup.hpp"
48 #include "runtime/deoptimization.hpp"
49 #include "utilities/bitMap.inline.hpp"
50 #include "utilities/xmlstream.hpp"
51 #ifdef COMPILER2
52 #include "ci/bcEscapeAnalyzer.hpp"
53 #include "ci/ciTypeFlow.hpp"
54 #include "oops/methodOop.hpp"
55 #endif
56 #ifdef SHARK
57 #include "ci/ciTypeFlow.hpp"
58 #include "oops/methodOop.hpp"
59 #endif
61 // ciMethod
62 //
63 // This class represents a methodOop in the HotSpot virtual
64 // machine.
67 // ------------------------------------------------------------------
68 // ciMethod::ciMethod
69 //
70 // Loaded method.
71 ciMethod::ciMethod(methodHandle h_m) : ciObject(h_m) {
72 assert(h_m() != NULL, "no null method");
74 // These fields are always filled in in loaded methods.
75 _flags = ciFlags(h_m()->access_flags());
77 // Easy to compute, so fill them in now.
78 _max_stack = h_m()->max_stack();
79 _max_locals = h_m()->max_locals();
80 _code_size = h_m()->code_size();
81 _intrinsic_id = h_m()->intrinsic_id();
82 _handler_count = h_m()->exception_table()->length() / 4;
83 _uses_monitors = h_m()->access_flags().has_monitor_bytecodes();
84 _balanced_monitors = !_uses_monitors || h_m()->access_flags().is_monitor_matching();
85 _is_c1_compilable = !h_m()->is_not_c1_compilable();
86 _is_c2_compilable = !h_m()->is_not_c2_compilable();
87 // Lazy fields, filled in on demand. Require allocation.
88 _code = NULL;
89 _exception_handlers = NULL;
90 _liveness = NULL;
91 _method_blocks = NULL;
92 #if defined(COMPILER2) || defined(SHARK)
93 _flow = NULL;
94 _bcea = NULL;
95 #endif // COMPILER2 || SHARK
97 ciEnv *env = CURRENT_ENV;
98 if (env->jvmti_can_hotswap_or_post_breakpoint() && can_be_compiled()) {
99 // 6328518 check hotswap conditions under the right lock.
100 MutexLocker locker(Compile_lock);
101 if (Dependencies::check_evol_method(h_m()) != NULL) {
102 _is_c1_compilable = false;
103 _is_c2_compilable = false;
104 }
105 } else {
106 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
107 }
109 if (instanceKlass::cast(h_m()->method_holder())->is_linked()) {
110 _can_be_statically_bound = h_m()->can_be_statically_bound();
111 } else {
112 // Have to use a conservative value in this case.
113 _can_be_statically_bound = false;
114 }
116 // Adjust the definition of this condition to be more useful:
117 // %%% take these conditions into account in vtable generation
118 if (!_can_be_statically_bound && h_m()->is_private())
119 _can_be_statically_bound = true;
120 if (_can_be_statically_bound && h_m()->is_abstract())
121 _can_be_statically_bound = false;
123 // generating _signature may allow GC and therefore move m.
124 // These fields are always filled in.
125 _name = env->get_symbol(h_m()->name());
126 _holder = env->get_object(h_m()->method_holder())->as_instance_klass();
127 ciSymbol* sig_symbol = env->get_symbol(h_m()->signature());
128 constantPoolHandle cpool = h_m()->constants();
129 _signature = new (env->arena()) ciSignature(_holder, cpool, sig_symbol);
130 _method_data = NULL;
131 // Take a snapshot of these values, so they will be commensurate with the MDO.
132 if (ProfileInterpreter || TieredCompilation) {
133 int invcnt = h_m()->interpreter_invocation_count();
134 // if the value overflowed report it as max int
135 _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
136 _interpreter_throwout_count = h_m()->interpreter_throwout_count();
137 } else {
138 _interpreter_invocation_count = 0;
139 _interpreter_throwout_count = 0;
140 }
141 if (_interpreter_invocation_count == 0)
142 _interpreter_invocation_count = 1;
143 }
146 // ------------------------------------------------------------------
147 // ciMethod::ciMethod
148 //
149 // Unloaded method.
150 ciMethod::ciMethod(ciInstanceKlass* holder,
151 ciSymbol* name,
152 ciSymbol* signature,
153 ciInstanceKlass* accessor) :
154 ciObject(ciMethodKlass::make()),
155 _name( name),
156 _holder( holder),
157 _intrinsic_id( vmIntrinsics::_none),
158 _liveness( NULL),
159 _can_be_statically_bound(false),
160 _method_blocks( NULL),
161 _method_data( NULL)
162 #if defined(COMPILER2) || defined(SHARK)
163 ,
164 _flow( NULL),
165 _bcea( NULL)
166 #endif // COMPILER2 || SHARK
167 {
168 // Usually holder and accessor are the same type but in some cases
169 // the holder has the wrong class loader (e.g. invokedynamic call
170 // sites) so we pass the accessor.
171 _signature = new (CURRENT_ENV->arena()) ciSignature(accessor, constantPoolHandle(), signature);
172 }
175 // ------------------------------------------------------------------
176 // ciMethod::load_code
177 //
178 // Load the bytecodes and exception handler table for this method.
179 void ciMethod::load_code() {
180 VM_ENTRY_MARK;
181 assert(is_loaded(), "only loaded methods have code");
183 methodOop me = get_methodOop();
184 Arena* arena = CURRENT_THREAD_ENV->arena();
186 // Load the bytecodes.
187 _code = (address)arena->Amalloc(code_size());
188 memcpy(_code, me->code_base(), code_size());
190 // Revert any breakpoint bytecodes in ci's copy
191 if (me->number_of_breakpoints() > 0) {
192 BreakpointInfo* bp = instanceKlass::cast(me->method_holder())->breakpoints();
193 for (; bp != NULL; bp = bp->next()) {
194 if (bp->match(me)) {
195 code_at_put(bp->bci(), bp->orig_bytecode());
196 }
197 }
198 }
200 // And load the exception table.
201 typeArrayOop exc_table = me->exception_table();
203 // Allocate one extra spot in our list of exceptions. This
204 // last entry will be used to represent the possibility that
205 // an exception escapes the method. See ciExceptionHandlerStream
206 // for details.
207 _exception_handlers =
208 (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
209 * (_handler_count + 1));
210 if (_handler_count > 0) {
211 for (int i=0; i<_handler_count; i++) {
212 int base = i*4;
213 _exception_handlers[i] = new (arena) ciExceptionHandler(
214 holder(),
215 /* start */ exc_table->int_at(base),
216 /* limit */ exc_table->int_at(base+1),
217 /* goto pc */ exc_table->int_at(base+2),
218 /* cp index */ exc_table->int_at(base+3));
219 }
220 }
222 // Put an entry at the end of our list to represent the possibility
223 // of exceptional exit.
224 _exception_handlers[_handler_count] =
225 new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
227 if (CIPrintMethodCodes) {
228 print_codes();
229 }
230 }
233 // ------------------------------------------------------------------
234 // ciMethod::has_linenumber_table
235 //
236 // length unknown until decompression
237 bool ciMethod::has_linenumber_table() const {
238 check_is_loaded();
239 VM_ENTRY_MARK;
240 return get_methodOop()->has_linenumber_table();
241 }
244 // ------------------------------------------------------------------
245 // ciMethod::compressed_linenumber_table
246 u_char* ciMethod::compressed_linenumber_table() const {
247 check_is_loaded();
248 VM_ENTRY_MARK;
249 return get_methodOop()->compressed_linenumber_table();
250 }
253 // ------------------------------------------------------------------
254 // ciMethod::line_number_from_bci
255 int ciMethod::line_number_from_bci(int bci) const {
256 check_is_loaded();
257 VM_ENTRY_MARK;
258 return get_methodOop()->line_number_from_bci(bci);
259 }
262 // ------------------------------------------------------------------
263 // ciMethod::vtable_index
264 //
265 // Get the position of this method's entry in the vtable, if any.
266 int ciMethod::vtable_index() {
267 check_is_loaded();
268 assert(holder()->is_linked(), "must be linked");
269 VM_ENTRY_MARK;
270 return get_methodOop()->vtable_index();
271 }
274 #ifdef SHARK
275 // ------------------------------------------------------------------
276 // ciMethod::itable_index
277 //
278 // Get the position of this method's entry in the itable, if any.
279 int ciMethod::itable_index() {
280 check_is_loaded();
281 assert(holder()->is_linked(), "must be linked");
282 VM_ENTRY_MARK;
283 return klassItable::compute_itable_index(get_methodOop());
284 }
285 #endif // SHARK
288 // ------------------------------------------------------------------
289 // ciMethod::native_entry
290 //
291 // Get the address of this method's native code, if any.
292 address ciMethod::native_entry() {
293 check_is_loaded();
294 assert(flags().is_native(), "must be native method");
295 VM_ENTRY_MARK;
296 methodOop method = get_methodOop();
297 address entry = method->native_function();
298 assert(entry != NULL, "must be valid entry point");
299 return entry;
300 }
303 // ------------------------------------------------------------------
304 // ciMethod::interpreter_entry
305 //
306 // Get the entry point for running this method in the interpreter.
307 address ciMethod::interpreter_entry() {
308 check_is_loaded();
309 VM_ENTRY_MARK;
310 methodHandle mh(THREAD, get_methodOop());
311 return Interpreter::entry_for_method(mh);
312 }
315 // ------------------------------------------------------------------
316 // ciMethod::uses_balanced_monitors
317 //
318 // Does this method use monitors in a strict stack-disciplined manner?
319 bool ciMethod::has_balanced_monitors() {
320 check_is_loaded();
321 if (_balanced_monitors) return true;
323 // Analyze the method to see if monitors are used properly.
324 VM_ENTRY_MARK;
325 methodHandle method(THREAD, get_methodOop());
326 assert(method->has_monitor_bytecodes(), "should have checked this");
328 // Check to see if a previous compilation computed the
329 // monitor-matching analysis.
330 if (method->guaranteed_monitor_matching()) {
331 _balanced_monitors = true;
332 return true;
333 }
335 {
336 EXCEPTION_MARK;
337 ResourceMark rm(THREAD);
338 GeneratePairingInfo gpi(method);
339 gpi.compute_map(CATCH);
340 if (!gpi.monitor_safe()) {
341 return false;
342 }
343 method->set_guaranteed_monitor_matching();
344 _balanced_monitors = true;
345 }
346 return true;
347 }
350 // ------------------------------------------------------------------
351 // ciMethod::get_flow_analysis
352 ciTypeFlow* ciMethod::get_flow_analysis() {
353 #if defined(COMPILER2) || defined(SHARK)
354 if (_flow == NULL) {
355 ciEnv* env = CURRENT_ENV;
356 _flow = new (env->arena()) ciTypeFlow(env, this);
357 _flow->do_flow();
358 }
359 return _flow;
360 #else // COMPILER2 || SHARK
361 ShouldNotReachHere();
362 return NULL;
363 #endif // COMPILER2 || SHARK
364 }
367 // ------------------------------------------------------------------
368 // ciMethod::get_osr_flow_analysis
369 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
370 #if defined(COMPILER2) || defined(SHARK)
371 // OSR entry points are always place after a call bytecode of some sort
372 assert(osr_bci >= 0, "must supply valid OSR entry point");
373 ciEnv* env = CURRENT_ENV;
374 ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
375 flow->do_flow();
376 return flow;
377 #else // COMPILER2 || SHARK
378 ShouldNotReachHere();
379 return NULL;
380 #endif // COMPILER2 || SHARK
381 }
383 // ------------------------------------------------------------------
384 // ciMethod::raw_liveness_at_bci
385 //
386 // Which local variables are live at a specific bci?
387 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) {
388 check_is_loaded();
389 if (_liveness == NULL) {
390 // Create the liveness analyzer.
391 Arena* arena = CURRENT_ENV->arena();
392 _liveness = new (arena) MethodLiveness(arena, this);
393 _liveness->compute_liveness();
394 }
395 return _liveness->get_liveness_at(bci);
396 }
398 // ------------------------------------------------------------------
399 // ciMethod::liveness_at_bci
400 //
401 // Which local variables are live at a specific bci? When debugging
402 // will return true for all locals in some cases to improve debug
403 // information.
404 MethodLivenessResult ciMethod::liveness_at_bci(int bci) {
405 MethodLivenessResult result = raw_liveness_at_bci(bci);
406 if (CURRENT_ENV->jvmti_can_access_local_variables() || DeoptimizeALot || CompileTheWorld) {
407 // Keep all locals live for the user's edification and amusement.
408 result.at_put_range(0, result.size(), true);
409 }
410 return result;
411 }
413 // ciMethod::live_local_oops_at_bci
414 //
415 // find all the live oops in the locals array for a particular bci
416 // Compute what the interpreter believes by using the interpreter
417 // oopmap generator. This is used as a double check during osr to
418 // guard against conservative result from MethodLiveness making us
419 // think a dead oop is live. MethodLiveness is conservative in the
420 // sense that it may consider locals to be live which cannot be live,
421 // like in the case where a local could contain an oop or a primitive
422 // along different paths. In that case the local must be dead when
423 // those paths merge. Since the interpreter's viewpoint is used when
424 // gc'ing an interpreter frame we need to use its viewpoint during
425 // OSR when loading the locals.
427 BitMap ciMethod::live_local_oops_at_bci(int bci) {
428 VM_ENTRY_MARK;
429 InterpreterOopMap mask;
430 OopMapCache::compute_one_oop_map(get_methodOop(), bci, &mask);
431 int mask_size = max_locals();
432 BitMap result(mask_size);
433 result.clear();
434 int i;
435 for (i = 0; i < mask_size ; i++ ) {
436 if (mask.is_oop(i)) result.set_bit(i);
437 }
438 return result;
439 }
442 #ifdef COMPILER1
443 // ------------------------------------------------------------------
444 // ciMethod::bci_block_start
445 //
446 // Marks all bcis where a new basic block starts
447 const BitMap ciMethod::bci_block_start() {
448 check_is_loaded();
449 if (_liveness == NULL) {
450 // Create the liveness analyzer.
451 Arena* arena = CURRENT_ENV->arena();
452 _liveness = new (arena) MethodLiveness(arena, this);
453 _liveness->compute_liveness();
454 }
456 return _liveness->get_bci_block_start();
457 }
458 #endif // COMPILER1
461 // ------------------------------------------------------------------
462 // ciMethod::call_profile_at_bci
463 //
464 // Get the ciCallProfile for the invocation of this method.
465 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
466 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
467 ResourceMark rm;
468 ciCallProfile result;
469 if (method_data() != NULL && method_data()->is_mature()) {
470 ciProfileData* data = method_data()->bci_to_data(bci);
471 if (data != NULL && data->is_CounterData()) {
472 // Every profiled call site has a counter.
473 int count = data->as_CounterData()->count();
475 if (!data->is_ReceiverTypeData()) {
476 result._receiver_count[0] = 0; // that's a definite zero
477 } else { // ReceiverTypeData is a subclass of CounterData
478 ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
479 // In addition, virtual call sites have receiver type information
480 int receivers_count_total = 0;
481 int morphism = 0;
482 // Precompute morphism for the possible fixup
483 for (uint i = 0; i < call->row_limit(); i++) {
484 ciKlass* receiver = call->receiver(i);
485 if (receiver == NULL) continue;
486 morphism++;
487 }
488 int epsilon = 0;
489 if (TieredCompilation && ProfileInterpreter) {
490 // Interpreter and C1 treat final and special invokes differently.
491 // C1 will record a type, whereas the interpreter will just
492 // increment the count. Detect this case.
493 if (morphism == 1 && count > 0) {
494 epsilon = count;
495 count = 0;
496 }
497 }
498 for (uint i = 0; i < call->row_limit(); i++) {
499 ciKlass* receiver = call->receiver(i);
500 if (receiver == NULL) continue;
501 int rcount = call->receiver_count(i) + epsilon;
502 if (rcount == 0) rcount = 1; // Should be valid value
503 receivers_count_total += rcount;
504 // Add the receiver to result data.
505 result.add_receiver(receiver, rcount);
506 // If we extend profiling to record methods,
507 // we will set result._method also.
508 }
509 // Determine call site's morphism.
510 // The call site count is 0 with known morphism (onlt 1 or 2 receivers)
511 // or < 0 in the case of a type check failured for checkcast, aastore, instanceof.
512 // The call site count is > 0 in the case of a polymorphic virtual call.
513 if (morphism > 0 && morphism == result._limit) {
514 // The morphism <= MorphismLimit.
515 if ((morphism < ciCallProfile::MorphismLimit) ||
516 (morphism == ciCallProfile::MorphismLimit && count == 0)) {
517 #ifdef ASSERT
518 if (count > 0) {
519 this->print_short_name(tty);
520 tty->print_cr(" @ bci:%d", bci);
521 this->print_codes();
522 assert(false, "this call site should not be polymorphic");
523 }
524 #endif
525 result._morphism = morphism;
526 }
527 }
528 // Make the count consistent if this is a call profile. If count is
529 // zero or less, presume that this is a typecheck profile and
530 // do nothing. Otherwise, increase count to be the sum of all
531 // receiver's counts.
532 if (count >= 0) {
533 count += receivers_count_total;
534 }
535 }
536 result._count = count;
537 }
538 }
539 return result;
540 }
542 // ------------------------------------------------------------------
543 // Add new receiver and sort data by receiver's profile count.
544 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
545 // Add new receiver and sort data by receiver's counts when we have space
546 // for it otherwise replace the less called receiver (less called receiver
547 // is placed to the last array element which is not used).
548 // First array's element contains most called receiver.
549 int i = _limit;
550 for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
551 _receiver[i] = _receiver[i-1];
552 _receiver_count[i] = _receiver_count[i-1];
553 }
554 _receiver[i] = receiver;
555 _receiver_count[i] = receiver_count;
556 if (_limit < MorphismLimit) _limit++;
557 }
559 // ------------------------------------------------------------------
560 // ciMethod::find_monomorphic_target
561 //
562 // Given a certain calling environment, find the monomorphic target
563 // for the call. Return NULL if the call is not monomorphic in
564 // its calling environment, or if there are only abstract methods.
565 // The returned method is never abstract.
566 // Note: If caller uses a non-null result, it must inform dependencies
567 // via assert_unique_concrete_method or assert_leaf_type.
568 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
569 ciInstanceKlass* callee_holder,
570 ciInstanceKlass* actual_recv) {
571 check_is_loaded();
573 if (actual_recv->is_interface()) {
574 // %%% We cannot trust interface types, yet. See bug 6312651.
575 return NULL;
576 }
578 ciMethod* root_m = resolve_invoke(caller, actual_recv);
579 if (root_m == NULL) {
580 // Something went wrong looking up the actual receiver method.
581 return NULL;
582 }
583 assert(!root_m->is_abstract(), "resolve_invoke promise");
585 // Make certain quick checks even if UseCHA is false.
587 // Is it private or final?
588 if (root_m->can_be_statically_bound()) {
589 return root_m;
590 }
592 if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
593 // Easy case. There is no other place to put a method, so don't bother
594 // to go through the VM_ENTRY_MARK and all the rest.
595 return root_m;
596 }
598 // Array methods (clone, hashCode, etc.) are always statically bound.
599 // If we were to see an array type here, we'd return root_m.
600 // However, this method processes only ciInstanceKlasses. (See 4962591.)
601 // The inline_native_clone intrinsic narrows Object to T[] properly,
602 // so there is no need to do the same job here.
604 if (!UseCHA) return NULL;
606 VM_ENTRY_MARK;
608 methodHandle target;
609 {
610 MutexLocker locker(Compile_lock);
611 klassOop context = actual_recv->get_klassOop();
612 target = Dependencies::find_unique_concrete_method(context,
613 root_m->get_methodOop());
614 // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
615 }
617 #ifndef PRODUCT
618 if (TraceDependencies && target() != NULL && target() != root_m->get_methodOop()) {
619 tty->print("found a non-root unique target method");
620 tty->print_cr(" context = %s", instanceKlass::cast(actual_recv->get_klassOop())->external_name());
621 tty->print(" method = ");
622 target->print_short_name(tty);
623 tty->cr();
624 }
625 #endif //PRODUCT
627 if (target() == NULL) {
628 return NULL;
629 }
630 if (target() == root_m->get_methodOop()) {
631 return root_m;
632 }
633 if (!root_m->is_public() &&
634 !root_m->is_protected()) {
635 // If we are going to reason about inheritance, it's easiest
636 // if the method in question is public, protected, or private.
637 // If the answer is not root_m, it is conservatively correct
638 // to return NULL, even if the CHA encountered irrelevant
639 // methods in other packages.
640 // %%% TO DO: Work out logic for package-private methods
641 // with the same name but different vtable indexes.
642 return NULL;
643 }
644 return CURRENT_THREAD_ENV->get_object(target())->as_method();
645 }
647 // ------------------------------------------------------------------
648 // ciMethod::resolve_invoke
649 //
650 // Given a known receiver klass, find the target for the call.
651 // Return NULL if the call has no target or the target is abstract.
652 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver) {
653 check_is_loaded();
654 VM_ENTRY_MARK;
656 KlassHandle caller_klass (THREAD, caller->get_klassOop());
657 KlassHandle h_recv (THREAD, exact_receiver->get_klassOop());
658 KlassHandle h_resolved (THREAD, holder()->get_klassOop());
659 Symbol* h_name = name()->get_symbol();
660 Symbol* h_signature = signature()->get_symbol();
662 methodHandle m;
663 // Only do exact lookup if receiver klass has been linked. Otherwise,
664 // the vtable has not been setup, and the LinkResolver will fail.
665 if (h_recv->oop_is_javaArray()
666 ||
667 instanceKlass::cast(h_recv())->is_linked() && !exact_receiver->is_interface()) {
668 if (holder()->is_interface()) {
669 m = LinkResolver::resolve_interface_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
670 } else {
671 m = LinkResolver::resolve_virtual_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
672 }
673 }
675 if (m.is_null()) {
676 // Return NULL only if there was a problem with lookup (uninitialized class, etc.)
677 return NULL;
678 }
680 ciMethod* result = this;
681 if (m() != get_methodOop()) {
682 result = CURRENT_THREAD_ENV->get_object(m())->as_method();
683 }
685 // Don't return abstract methods because they aren't
686 // optimizable or interesting.
687 if (result->is_abstract()) {
688 return NULL;
689 } else {
690 return result;
691 }
692 }
694 // ------------------------------------------------------------------
695 // ciMethod::resolve_vtable_index
696 //
697 // Given a known receiver klass, find the vtable index for the call.
698 // Return methodOopDesc::invalid_vtable_index if the vtable_index is unknown.
699 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
700 check_is_loaded();
702 int vtable_index = methodOopDesc::invalid_vtable_index;
703 // Only do lookup if receiver klass has been linked. Otherwise,
704 // the vtable has not been setup, and the LinkResolver will fail.
705 if (!receiver->is_interface()
706 && (!receiver->is_instance_klass() ||
707 receiver->as_instance_klass()->is_linked())) {
708 VM_ENTRY_MARK;
710 KlassHandle caller_klass (THREAD, caller->get_klassOop());
711 KlassHandle h_recv (THREAD, receiver->get_klassOop());
712 Symbol* h_name = name()->get_symbol();
713 Symbol* h_signature = signature()->get_symbol();
715 vtable_index = LinkResolver::resolve_virtual_vtable_index(h_recv, h_recv, h_name, h_signature, caller_klass);
716 if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
717 // A statically bound method. Return "no such index".
718 vtable_index = methodOopDesc::invalid_vtable_index;
719 }
720 }
722 return vtable_index;
723 }
725 // ------------------------------------------------------------------
726 // ciMethod::interpreter_call_site_count
727 int ciMethod::interpreter_call_site_count(int bci) {
728 if (method_data() != NULL) {
729 ResourceMark rm;
730 ciProfileData* data = method_data()->bci_to_data(bci);
731 if (data != NULL && data->is_CounterData()) {
732 return scale_count(data->as_CounterData()->count());
733 }
734 }
735 return -1; // unknown
736 }
738 // ------------------------------------------------------------------
739 // Adjust a CounterData count to be commensurate with
740 // interpreter_invocation_count. If the MDO exists for
741 // only 25% of the time the method exists, then the
742 // counts in the MDO should be scaled by 4X, so that
743 // they can be usefully and stably compared against the
744 // invocation counts in methods.
745 int ciMethod::scale_count(int count, float prof_factor) {
746 if (count > 0 && method_data() != NULL) {
747 int counter_life;
748 int method_life = interpreter_invocation_count();
749 if (TieredCompilation) {
750 // In tiered the MDO's life is measured directly, so just use the snapshotted counters
751 counter_life = MAX2(method_data()->invocation_count(), method_data()->backedge_count());
752 } else {
753 int current_mileage = method_data()->current_mileage();
754 int creation_mileage = method_data()->creation_mileage();
755 counter_life = current_mileage - creation_mileage;
756 }
758 // counter_life due to backedge_counter could be > method_life
759 if (counter_life > method_life)
760 counter_life = method_life;
761 if (0 < counter_life && counter_life <= method_life) {
762 count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
763 count = (count > 0) ? count : 1;
764 }
765 }
766 return count;
767 }
769 // ------------------------------------------------------------------
770 // invokedynamic support
772 // ------------------------------------------------------------------
773 // ciMethod::is_method_handle_invoke
774 //
775 // Return true if the method is an instance of one of the two
776 // signature-polymorphic MethodHandle methods, invokeExact or invokeGeneric.
777 bool ciMethod::is_method_handle_invoke() const {
778 if (!is_loaded()) {
779 bool flag = (holder()->name() == ciSymbol::java_lang_invoke_MethodHandle() &&
780 methodOopDesc::is_method_handle_invoke_name(name()->sid()));
781 return flag;
782 }
783 VM_ENTRY_MARK;
784 return get_methodOop()->is_method_handle_invoke();
785 }
787 // ------------------------------------------------------------------
788 // ciMethod::is_method_handle_adapter
789 //
790 // Return true if the method is a generated MethodHandle adapter.
791 // These are built by MethodHandleCompiler.
792 bool ciMethod::is_method_handle_adapter() const {
793 if (!is_loaded()) return false;
794 VM_ENTRY_MARK;
795 return get_methodOop()->is_method_handle_adapter();
796 }
798 ciInstance* ciMethod::method_handle_type() {
799 check_is_loaded();
800 VM_ENTRY_MARK;
801 oop mtype = get_methodOop()->method_handle_type();
802 return CURRENT_THREAD_ENV->get_object(mtype)->as_instance();
803 }
806 // ------------------------------------------------------------------
807 // ciMethod::ensure_method_data
808 //
809 // Generate new methodDataOop objects at compile time.
810 // Return true if allocation was successful or no MDO is required.
811 bool ciMethod::ensure_method_data(methodHandle h_m) {
812 EXCEPTION_CONTEXT;
813 if (is_native() || is_abstract() || h_m()->is_accessor()) return true;
814 if (h_m()->method_data() == NULL) {
815 methodOopDesc::build_interpreter_method_data(h_m, THREAD);
816 if (HAS_PENDING_EXCEPTION) {
817 CLEAR_PENDING_EXCEPTION;
818 }
819 }
820 if (h_m()->method_data() != NULL) {
821 _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
822 _method_data->load_data();
823 return true;
824 } else {
825 _method_data = CURRENT_ENV->get_empty_methodData();
826 return false;
827 }
828 }
830 // public, retroactive version
831 bool ciMethod::ensure_method_data() {
832 bool result = true;
833 if (_method_data == NULL || _method_data->is_empty()) {
834 GUARDED_VM_ENTRY({
835 result = ensure_method_data(get_methodOop());
836 });
837 }
838 return result;
839 }
842 // ------------------------------------------------------------------
843 // ciMethod::method_data
844 //
845 ciMethodData* ciMethod::method_data() {
846 if (_method_data != NULL) {
847 return _method_data;
848 }
849 VM_ENTRY_MARK;
850 ciEnv* env = CURRENT_ENV;
851 Thread* my_thread = JavaThread::current();
852 methodHandle h_m(my_thread, get_methodOop());
854 if (h_m()->method_data() != NULL) {
855 _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
856 _method_data->load_data();
857 } else {
858 _method_data = CURRENT_ENV->get_empty_methodData();
859 }
860 return _method_data;
862 }
864 // ------------------------------------------------------------------
865 // ciMethod::method_data_or_null
866 // Returns a pointer to ciMethodData if MDO exists on the VM side,
867 // NULL otherwise.
868 ciMethodData* ciMethod::method_data_or_null() {
869 ciMethodData *md = method_data();
870 if (md->is_empty()) return NULL;
871 return md;
872 }
874 // ------------------------------------------------------------------
875 // ciMethod::will_link
876 //
877 // Will this method link in a specific calling context?
878 bool ciMethod::will_link(ciKlass* accessing_klass,
879 ciKlass* declared_method_holder,
880 Bytecodes::Code bc) {
881 if (!is_loaded()) {
882 // Method lookup failed.
883 return false;
884 }
886 // The link checks have been front-loaded into the get_method
887 // call. This method (ciMethod::will_link()) will be removed
888 // in the future.
890 return true;
891 }
893 // ------------------------------------------------------------------
894 // ciMethod::should_exclude
895 //
896 // Should this method be excluded from compilation?
897 bool ciMethod::should_exclude() {
898 check_is_loaded();
899 VM_ENTRY_MARK;
900 methodHandle mh(THREAD, get_methodOop());
901 bool ignore;
902 return CompilerOracle::should_exclude(mh, ignore);
903 }
905 // ------------------------------------------------------------------
906 // ciMethod::should_inline
907 //
908 // Should this method be inlined during compilation?
909 bool ciMethod::should_inline() {
910 check_is_loaded();
911 VM_ENTRY_MARK;
912 methodHandle mh(THREAD, get_methodOop());
913 return CompilerOracle::should_inline(mh);
914 }
916 // ------------------------------------------------------------------
917 // ciMethod::should_not_inline
918 //
919 // Should this method be disallowed from inlining during compilation?
920 bool ciMethod::should_not_inline() {
921 check_is_loaded();
922 VM_ENTRY_MARK;
923 methodHandle mh(THREAD, get_methodOop());
924 return CompilerOracle::should_not_inline(mh);
925 }
927 // ------------------------------------------------------------------
928 // ciMethod::should_print_assembly
929 //
930 // Should the compiler print the generated code for this method?
931 bool ciMethod::should_print_assembly() {
932 check_is_loaded();
933 VM_ENTRY_MARK;
934 methodHandle mh(THREAD, get_methodOop());
935 return CompilerOracle::should_print(mh);
936 }
938 // ------------------------------------------------------------------
939 // ciMethod::break_at_execute
940 //
941 // Should the compiler insert a breakpoint into the generated code
942 // method?
943 bool ciMethod::break_at_execute() {
944 check_is_loaded();
945 VM_ENTRY_MARK;
946 methodHandle mh(THREAD, get_methodOop());
947 return CompilerOracle::should_break_at(mh);
948 }
950 // ------------------------------------------------------------------
951 // ciMethod::has_option
952 //
953 bool ciMethod::has_option(const char* option) {
954 check_is_loaded();
955 VM_ENTRY_MARK;
956 methodHandle mh(THREAD, get_methodOop());
957 return CompilerOracle::has_option_string(mh, option);
958 }
960 // ------------------------------------------------------------------
961 // ciMethod::can_be_compiled
962 //
963 // Have previous compilations of this method succeeded?
964 bool ciMethod::can_be_compiled() {
965 check_is_loaded();
966 ciEnv* env = CURRENT_ENV;
967 if (is_c1_compile(env->comp_level())) {
968 return _is_c1_compilable;
969 }
970 return _is_c2_compilable;
971 }
973 // ------------------------------------------------------------------
974 // ciMethod::set_not_compilable
975 //
976 // Tell the VM that this method cannot be compiled at all.
977 void ciMethod::set_not_compilable() {
978 check_is_loaded();
979 VM_ENTRY_MARK;
980 ciEnv* env = CURRENT_ENV;
981 if (is_c1_compile(env->comp_level())) {
982 _is_c1_compilable = false;
983 } else {
984 _is_c2_compilable = false;
985 }
986 get_methodOop()->set_not_compilable(env->comp_level());
987 }
989 // ------------------------------------------------------------------
990 // ciMethod::can_be_osr_compiled
991 //
992 // Have previous compilations of this method succeeded?
993 //
994 // Implementation note: the VM does not currently keep track
995 // of failed OSR compilations per bci. The entry_bci parameter
996 // is currently unused.
997 bool ciMethod::can_be_osr_compiled(int entry_bci) {
998 check_is_loaded();
999 VM_ENTRY_MARK;
1000 ciEnv* env = CURRENT_ENV;
1001 return !get_methodOop()->is_not_osr_compilable(env->comp_level());
1002 }
1004 // ------------------------------------------------------------------
1005 // ciMethod::has_compiled_code
1006 bool ciMethod::has_compiled_code() {
1007 VM_ENTRY_MARK;
1008 return get_methodOop()->code() != NULL;
1009 }
1011 int ciMethod::comp_level() {
1012 check_is_loaded();
1013 VM_ENTRY_MARK;
1014 nmethod* nm = get_methodOop()->code();
1015 if (nm != NULL) return nm->comp_level();
1016 return 0;
1017 }
1019 int ciMethod::highest_osr_comp_level() {
1020 check_is_loaded();
1021 VM_ENTRY_MARK;
1022 return get_methodOop()->highest_osr_comp_level();
1023 }
1025 // ------------------------------------------------------------------
1026 // ciMethod::code_size_for_inlining
1027 //
1028 // Code size for inlining decisions.
1029 //
1030 // Don't fully count method handle adapters against inlining budgets:
1031 // the metric we use here is the number of call sites in the adapter
1032 // as they are probably the instructions which generate some code.
1033 int ciMethod::code_size_for_inlining() {
1034 check_is_loaded();
1036 // Method handle adapters
1037 if (is_method_handle_adapter()) {
1038 // Count call sites
1039 int call_site_count = 0;
1040 ciBytecodeStream iter(this);
1041 while (iter.next() != ciBytecodeStream::EOBC()) {
1042 if (Bytecodes::is_invoke(iter.cur_bc())) {
1043 call_site_count++;
1044 }
1045 }
1046 return call_site_count;
1047 }
1049 // Normal method
1050 return code_size();
1051 }
1053 // ------------------------------------------------------------------
1054 // ciMethod::instructions_size
1055 //
1056 // This is a rough metric for "fat" methods, compared before inlining
1057 // with InlineSmallCode. The CodeBlob::code_size accessor includes
1058 // junk like exception handler, stubs, and constant table, which are
1059 // not highly relevant to an inlined method. So we use the more
1060 // specific accessor nmethod::insts_size.
1061 int ciMethod::instructions_size(int comp_level) {
1062 GUARDED_VM_ENTRY(
1063 nmethod* code = get_methodOop()->code();
1064 if (code != NULL && (comp_level == CompLevel_any || comp_level == code->comp_level())) {
1065 return code->insts_end() - code->verified_entry_point();
1066 }
1067 return 0;
1068 )
1069 }
1071 // ------------------------------------------------------------------
1072 // ciMethod::log_nmethod_identity
1073 void ciMethod::log_nmethod_identity(xmlStream* log) {
1074 GUARDED_VM_ENTRY(
1075 nmethod* code = get_methodOop()->code();
1076 if (code != NULL) {
1077 code->log_identity(log);
1078 }
1079 )
1080 }
1082 // ------------------------------------------------------------------
1083 // ciMethod::is_not_reached
1084 bool ciMethod::is_not_reached(int bci) {
1085 check_is_loaded();
1086 VM_ENTRY_MARK;
1087 return Interpreter::is_not_reached(
1088 methodHandle(THREAD, get_methodOop()), bci);
1089 }
1091 // ------------------------------------------------------------------
1092 // ciMethod::was_never_executed
1093 bool ciMethod::was_executed_more_than(int times) {
1094 VM_ENTRY_MARK;
1095 return get_methodOop()->was_executed_more_than(times);
1096 }
1098 // ------------------------------------------------------------------
1099 // ciMethod::has_unloaded_classes_in_signature
1100 bool ciMethod::has_unloaded_classes_in_signature() {
1101 VM_ENTRY_MARK;
1102 {
1103 EXCEPTION_MARK;
1104 methodHandle m(THREAD, get_methodOop());
1105 bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
1106 if( HAS_PENDING_EXCEPTION ) {
1107 CLEAR_PENDING_EXCEPTION;
1108 return true; // Declare that we may have unloaded classes
1109 }
1110 return has_unloaded;
1111 }
1112 }
1114 // ------------------------------------------------------------------
1115 // ciMethod::is_klass_loaded
1116 bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1117 VM_ENTRY_MARK;
1118 return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
1119 }
1121 // ------------------------------------------------------------------
1122 // ciMethod::check_call
1123 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1124 VM_ENTRY_MARK;
1125 {
1126 EXCEPTION_MARK;
1127 HandleMark hm(THREAD);
1128 constantPoolHandle pool (THREAD, get_methodOop()->constants());
1129 methodHandle spec_method;
1130 KlassHandle spec_klass;
1131 LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
1132 if (HAS_PENDING_EXCEPTION) {
1133 CLEAR_PENDING_EXCEPTION;
1134 return false;
1135 } else {
1136 return (spec_method->is_static() == is_static);
1137 }
1138 }
1139 return false;
1140 }
1142 // ------------------------------------------------------------------
1143 // ciMethod::print_codes
1144 //
1145 // Print the bytecodes for this method.
1146 void ciMethod::print_codes_on(outputStream* st) {
1147 check_is_loaded();
1148 GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
1149 }
1152 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1153 check_is_loaded(); \
1154 VM_ENTRY_MARK; \
1155 return get_methodOop()->flag_accessor(); \
1156 }
1158 bool ciMethod::is_empty_method() const { FETCH_FLAG_FROM_VM(is_empty_method); }
1159 bool ciMethod::is_vanilla_constructor() const { FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
1160 bool ciMethod::has_loops () const { FETCH_FLAG_FROM_VM(has_loops); }
1161 bool ciMethod::has_jsrs () const { FETCH_FLAG_FROM_VM(has_jsrs); }
1162 bool ciMethod::is_accessor () const { FETCH_FLAG_FROM_VM(is_accessor); }
1163 bool ciMethod::is_initializer () const { FETCH_FLAG_FROM_VM(is_initializer); }
1165 BCEscapeAnalyzer *ciMethod::get_bcea() {
1166 #ifdef COMPILER2
1167 if (_bcea == NULL) {
1168 _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
1169 }
1170 return _bcea;
1171 #else // COMPILER2
1172 ShouldNotReachHere();
1173 return NULL;
1174 #endif // COMPILER2
1175 }
1177 ciMethodBlocks *ciMethod::get_method_blocks() {
1178 Arena *arena = CURRENT_ENV->arena();
1179 if (_method_blocks == NULL) {
1180 _method_blocks = new (arena) ciMethodBlocks(arena, this);
1181 }
1182 return _method_blocks;
1183 }
1185 #undef FETCH_FLAG_FROM_VM
1188 // ------------------------------------------------------------------
1189 // ciMethod::print_codes
1190 //
1191 // Print a range of the bytecodes for this method.
1192 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1193 check_is_loaded();
1194 GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1195 }
1197 // ------------------------------------------------------------------
1198 // ciMethod::print_name
1199 //
1200 // Print the name of this method, including signature and some flags.
1201 void ciMethod::print_name(outputStream* st) {
1202 check_is_loaded();
1203 GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1204 }
1206 // ------------------------------------------------------------------
1207 // ciMethod::print_short_name
1208 //
1209 // Print the name of this method, without signature.
1210 void ciMethod::print_short_name(outputStream* st) {
1211 check_is_loaded();
1212 GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1213 }
1215 // ------------------------------------------------------------------
1216 // ciMethod::print_impl
1217 //
1218 // Implementation of the print method.
1219 void ciMethod::print_impl(outputStream* st) {
1220 ciObject::print_impl(st);
1221 st->print(" name=");
1222 name()->print_symbol_on(st);
1223 st->print(" holder=");
1224 holder()->print_name_on(st);
1225 st->print(" signature=");
1226 signature()->as_symbol()->print_symbol_on(st);
1227 if (is_loaded()) {
1228 st->print(" loaded=true flags=");
1229 flags().print_member_flags(st);
1230 } else {
1231 st->print(" loaded=false");
1232 }
1233 }