1 /*
  2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "ci/ciConstant.hpp"
 27 #include "ci/ciField.hpp"
 28 #include "ci/ciKlass.hpp"
 29 #include "ci/ciObjArrayKlass.hpp"
 30 #include "ci/ciStreams.hpp"
 31 #include "ci/ciSymbols.hpp"
 32 #include "ci/ciUtilities.inline.hpp"
 33 #include "runtime/handles.inline.hpp"
 34 
 35 // ciExceptionHandlerStream
 36 //
 37 // Walk over some selected set of a methods exception handlers.
 38 
 39 // ------------------------------------------------------------------
 40 // ciExceptionHandlerStream::count
 41 //
 42 // How many exception handlers are there in this stream?
 43 //
 44 // Implementation note: Compiler2 needs this functionality, so I had
 45 int ciExceptionHandlerStream::count() {
 46   int save_pos = _pos;
 47   int save_end = _end;
 48 
 49   int count = 0;
 50 
 51   _pos = -1;
 52   _end = _method->_handler_count;
 53 
 54 
 55   next();
 56   while (!is_done()) {
 57     count++;
 58     next();
 59   }
 60 
 61   _pos = save_pos;
 62   _end = save_end;
 63 
 64   return count;
 65 }
 66 
 67 int ciExceptionHandlerStream::count_remaining() {
 68   int save_pos = _pos;
 69   int save_end = _end;
 70 
 71   int count = 0;
 72 
 73   while (!is_done()) {
 74     count++;
 75     next();
 76   }
 77 
 78   _pos = save_pos;
 79   _end = save_end;
 80 
 81   return count;
 82 }
 83 
 84 // ciBytecodeStream
 85 //
 86 // The class is used to iterate over the bytecodes of a method.
 87 // It hides the details of constant pool structure/access by
 88 // providing accessors for constant pool items.
 89 
 90 // ------------------------------------------------------------------
 91 // ciBytecodeStream::next_wide_or_table
 92 //
 93 // Special handling for switch ops
 94 Bytecodes::Code ciBytecodeStream::next_wide_or_table(Bytecodes::Code bc) {
 95   switch (bc) {                // Check for special bytecode handling
 96   case Bytecodes::_wide:
 97     // Special handling for the wide bytcode
 98     // Get following bytecode; do not return wide
 99     assert(Bytecodes::Code(_pc[0]) == Bytecodes::_wide, "");
100     bc = Bytecodes::java_code(_raw_bc = (Bytecodes::Code)_pc[1]);
101     assert(Bytecodes::wide_length_for(bc) > 2, "must make progress");
102     _pc += Bytecodes::wide_length_for(bc);
103     _was_wide = _pc;              // Flag last wide bytecode found
104     assert(is_wide(), "accessor works right");
105     break;
106 
107   case Bytecodes::_lookupswitch:
108     _pc++;                      // Skip wide bytecode
109     _pc += (_start-_pc)&3;      // Word align
110     _table_base = (jint*)_pc;   // Capture for later usage
111                                 // table_base[0] is default far_dest
112     // Table has 2 lead elements (default, length), then pairs of u4 values.
113     // So load table length, and compute address at end of table
114     _pc = (address)&_table_base[2+ 2*Bytes::get_Java_u4((address)&_table_base[1])];
115     break;
116 
117   case Bytecodes::_tableswitch: {
118     _pc++;                      // Skip wide bytecode
119     _pc += (_start-_pc)&3;      // Word align
120     _table_base = (jint*)_pc;   // Capture for later usage
121                                 // table_base[0] is default far_dest
122     int lo = Bytes::get_Java_u4((address)&_table_base[1]);// Low bound
123     int hi = Bytes::get_Java_u4((address)&_table_base[2]);// High bound
124     int len = hi - lo + 1;      // Dense table size
125     _pc = (address)&_table_base[3+len]; // Skip past table
126     break;
127   }
128 
129   default:
130     fatal("unhandled bytecode");
131   }
132   return bc;
133 }
134 
135 // ------------------------------------------------------------------
136 // ciBytecodeStream::reset_to_bci
137 void ciBytecodeStream::reset_to_bci( int bci ) {
138   _bc_start=_was_wide=0;
139   _pc = _start+bci;
140 }
141 
142 // ------------------------------------------------------------------
143 // ciBytecodeStream::force_bci
144 void ciBytecodeStream::force_bci(int bci) {
145   if (bci < 0) {
146     reset_to_bci(0);
147     _bc_start = _start + bci;
148     _bc = EOBC();
149   } else {
150     reset_to_bci(bci);
151     next();
152   }
153 }
154 
155 
156 // ------------------------------------------------------------------
157 // Constant pool access
158 // ------------------------------------------------------------------
159 
160 // ------------------------------------------------------------------
161 // ciBytecodeStream::get_klass_index
162 //
163 // If this bytecodes references a klass, return the index of the
164 // referenced klass.
165 int ciBytecodeStream::get_klass_index() const {
166   switch(cur_bc()) {
167   case Bytecodes::_ldc:
168     return get_index_u1();
169   case Bytecodes::_ldc_w:
170   case Bytecodes::_ldc2_w:
171   case Bytecodes::_checkcast:
172   case Bytecodes::_instanceof:
173   case Bytecodes::_anewarray:
174   case Bytecodes::_multianewarray:
175   case Bytecodes::_new:
176   case Bytecodes::_newarray:
177     return get_index_u2();
178   default:
179     ShouldNotReachHere();
180     return 0;
181   }
182 }
183 
184 // ------------------------------------------------------------------
185 // ciBytecodeStream::get_klass
186 //
187 // If this bytecode is a new, newarray, multianewarray, instanceof,
188 // or checkcast, get the referenced klass.
189 ciKlass* ciBytecodeStream::get_klass(bool& will_link) {
190   VM_ENTRY_MARK;
191   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
192   return CURRENT_ENV->get_klass_by_index(cpool, get_klass_index(), will_link, _holder);
193 }
194 
195 // ciBytecodeStream::get_klass
196 //
197 // If this bytecode is a new, newarray, multianewarray, instanceof,
198 // or checkcast, get the referenced klass. Retuns an unloaded ciKlass
199 // if the referenced klass is not accessible.
200 ciKlass* ciBytecodeStream::get_klass() {
201   bool will_link;
202   ciKlass* klass = get_klass(will_link);
203   if (!will_link && klass->is_loaded()) { // klass not accessible
204     VM_ENTRY_MARK;
205     klass = CURRENT_ENV->get_unloaded_klass(_holder, klass->name());
206   }
207   return klass;
208 }
209 
210 // ------------------------------------------------------------------
211 // ciBytecodeStream::get_constant_raw_index
212 //
213 // If this bytecode is one of the ldc variants, get the index of the
214 // referenced constant.
215 int ciBytecodeStream::get_constant_raw_index() const {
216   // work-alike for Bytecode_loadconstant::raw_index()
217   switch (cur_bc()) {
218   case Bytecodes::_ldc:
219     return get_index_u1();
220   case Bytecodes::_ldc_w:
221   case Bytecodes::_ldc2_w:
222     return get_index_u2();
223   default:
224     ShouldNotReachHere();
225     return 0;
226   }
227 }
228 
229 // ------------------------------------------------------------------
230 // ciBytecodeStream::get_constant_pool_index
231 // Decode any reference index into a regular pool index.
232 int ciBytecodeStream::get_constant_pool_index() const {
233   // work-alike for Bytecode_loadconstant::pool_index()
234   int index = get_constant_raw_index();
235   if (has_cache_index()) {
236     VM_ENTRY_MARK;
237     constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
238     return cpool->object_to_cp_index(index);
239   }
240   return index;
241 }
242 
243 // ------------------------------------------------------------------
244 // ciBytecodeStream::get_constant
245 //
246 // If this bytecode is one of the ldc variants, get the referenced
247 // constant.
248 ciConstant ciBytecodeStream::get_constant() {
249   VM_ENTRY_MARK;
250   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
251   int pool_index = get_constant_raw_index();
252   int cache_index = -1;
253   if (has_cache_index()) {
254     cache_index = pool_index;
255     pool_index = cpool->object_to_cp_index(cache_index);
256   } else if (cpool->tag_at(pool_index).is_dynamic_constant() ||
257              cpool->tag_at(pool_index).is_dynamic_constant_in_error()) {
258     // Condy with primitive type is not quickened, so the index into resolved reference cache should be reconstructed.
259     assert(is_java_primitive(cpool->basic_type_for_constant_at(pool_index)), "not quickened");
260     cache_index = cpool->cp_to_object_index(pool_index);
261   }
262   return CURRENT_ENV->get_constant_by_index(cpool, pool_index, cache_index, _holder);
263 }
264 
265 // ------------------------------------------------------------------
266 // ciBytecodeStream::get_constant_pool_tag
267 //
268 // If this bytecode is one of the ldc variants, get the referenced
269 // constant.
270 constantTag ciBytecodeStream::get_constant_pool_tag(int index) const {
271   VM_ENTRY_MARK;
272   return _method->get_Method()->constants()->constant_tag_at(index);
273 }
274 
275 // ------------------------------------------------------------------
276 // ciBytecodeStream::get_raw_pool_tag
277 //
278 constantTag ciBytecodeStream::get_raw_pool_tag_at(int index) const {
279   VM_ENTRY_MARK;
280   return _method->get_Method()->constants()->tag_at(index);
281 }
282 
283 // ------------------------------------------------------------------
284 // ciBytecodeStream::get_basic_type_for_constant_at
285 //
286 BasicType ciBytecodeStream::get_basic_type_for_constant_at(int index) const {
287   VM_ENTRY_MARK;
288   return _method->get_Method()->constants()->basic_type_for_constant_at(index);
289 }
290 
291 // ------------------------------------------------------------------
292 // ciBytecodeStream::get_field_index
293 //
294 // If this is a field access bytecode, get the constant pool
295 // index of the referenced field.
296 int ciBytecodeStream::get_field_index() {
297   assert(cur_bc() == Bytecodes::_getfield ||
298          cur_bc() == Bytecodes::_putfield ||
299          cur_bc() == Bytecodes::_getstatic ||
300          cur_bc() == Bytecodes::_putstatic, "wrong bc");
301   return get_index_u2_cpcache();
302 }
303 
304 
305 // ------------------------------------------------------------------
306 // ciBytecodeStream::get_field
307 //
308 // If this bytecode is one of get_field, get_static, put_field,
309 // or put_static, get the referenced field.
310 ciField* ciBytecodeStream::get_field(bool& will_link) {
311   ciField* f = CURRENT_ENV->get_field_by_index(_holder, get_field_index());
312   will_link = f->will_link(_method, _bc);
313   return f;
314 }
315 
316 
317 // ------------------------------------------------------------------
318 // ciBytecodeStream::get_declared_field_holder
319 //
320 // Get the declared holder of the currently referenced field.
321 //
322 // Usage note: the holder() of a ciField class returns the canonical
323 // holder of the field, rather than the holder declared in the
324 // bytecodes.
325 //
326 // There is no "will_link" result passed back.  The user is responsible
327 // for checking linkability when retrieving the associated field.
328 ciInstanceKlass* ciBytecodeStream::get_declared_field_holder() {
329   VM_ENTRY_MARK;
330   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
331   int holder_index = get_field_holder_index();
332   bool ignore;
333   return CURRENT_ENV->get_klass_by_index(cpool, holder_index, ignore, _holder)
334       ->as_instance_klass();
335 }
336 
337 // ------------------------------------------------------------------
338 // ciBytecodeStream::get_field_holder_index
339 //
340 // Get the constant pool index of the declared holder of the field
341 // referenced by the current bytecode.  Used for generating
342 // deoptimization information.
343 int ciBytecodeStream::get_field_holder_index() {
344   GUARDED_VM_ENTRY(
345     ConstantPool* cpool = _holder->get_instanceKlass()->constants();
346     return cpool->klass_ref_index_at(get_field_index());
347   )
348 }
349 
350 // ------------------------------------------------------------------
351 // ciBytecodeStream::get_method_index
352 //
353 // If this is a method invocation bytecode, get the constant pool
354 // index of the invoked method.
355 int ciBytecodeStream::get_method_index() {
356   assert(Bytecodes::is_invoke(cur_bc()), "invalid bytecode: %s", Bytecodes::name(cur_bc()));
357   if (has_index_u4())
358     return get_index_u4();  // invokedynamic
359   return get_index_u2_cpcache();
360 }
361 
362 // ------------------------------------------------------------------
363 // ciBytecodeStream::get_method
364 //
365 // If this is a method invocation bytecode, get the invoked method.
366 // Additionally return the declared signature to get more concrete
367 // type information if required (Cf. invokedynamic and invokehandle).
368 ciMethod* ciBytecodeStream::get_method(bool& will_link, ciSignature* *declared_signature_result) {
369   VM_ENTRY_MARK;
370   ciEnv* env = CURRENT_ENV;
371   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
372   ciMethod* m = env->get_method_by_index(cpool, get_method_index(), cur_bc(), _holder);
373   will_link = m->is_loaded();
374 
375   // Use the signature stored in the CP cache to create a signature
376   // with correct types (in respect to class loaders).
377   //
378   // In classic Java (before Java 7) there is never the slightest
379   // difference between the signature at the call site and that of the
380   // method.  Such a difference would have been a type error in the
381   // JVM.
382   //
383   // Now there are a few circumstances where the signature of a call
384   // site (which controls the outgoing stacked arguments) can differ
385   // from the signature of the method (which controls the receipt of
386   // those arguments at the method entry point).
387   //
388   // A. The signatures can differ if the callee is a static method and
389   // the caller thinks it is calling a non-static method (VH.get).
390   // This requires the method signature to have an explicit leading
391   // argument for the implicit 'this', not present at the call site.
392   //
393   // B. The call site can have less specific parameter types than the
394   // method, allowing loosely-typed code to handle strongly-typed
395   // methods.  This happens with linkToStatic and related linker
396   // commands.  Obviously the loosely-typed code has to ensure that
397   // the strongly typed method's invariants are respected, and this is
398   // done by issuing dynamic casts.
399   //
400   // C. The call site can have more specific parameter types than the
401   // method, allowing loosely-typed methods to handle strongly-typed
402   // requests.
403   //
404   // D. There are corresponding effects with return values, such as
405   // boolean method returning an int to an int-receiving call site,
406   // even though the method thought it returned just a boolean.
407   //
408   // E. The calling sequence at a particular call site may add an
409   // "appendix" argument not mentioned in the call site signature.  It
410   // is expected by the method signature, though, and this adds to the
411   // method's arity, even after 'this' parameter effects (A) are
412   // discounted.  Appendixes are used by invokehandle and
413   // invokedynamic instructions.
414   //
415   // F. A linker method (linkToStatic, etc.) can also take an extra
416   // argument, a MemberName which routes the call to a concrete
417   // strongly-typed method.  In this case the linker method may also
418   // differ in any of the ways A-D.  The eventual method will ignore
419   // the presence of the extra argument.
420   //
421   // None of these changes to calling sequences requires an argument
422   // to be moved or reformatted in any way.  This works because all
423   // references look alike to the JVM, as do all primitives (except
424   // float/long/double).  Another required property of the JVM is
425   // that, if a trailing argument is added or dropped, the placement
426   // of other arguments does not change.  This allows cases E and F to
427   // work smoothly, against without any moving or reformatting,
428   // despite the arity change.
429   //
430   if (has_local_signature()) {
431     Symbol* local_signature = cpool->symbol_at(get_method_signature_index(cpool));
432     ciSymbol* sig_sym  = env->get_symbol(local_signature);
433     ciKlass* pool_holder = env->get_klass(cpool->pool_holder());
434     ciSignature* call_site_sig = new (env->arena()) ciSignature(pool_holder, cpool, sig_sym);
435     // Examples of how the call site signature can differ from the method's own signature:
436     //
437     //  meth = static jboolean java.lang.invoke.VarHandleGuards.guard_LII_Z(jobject, jobject, jint, jint, jobject)
438     //  msig = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;IILjava/lang/invoke/VarHandle$AccessDescriptor;)Z
439     //  call = (Ljava/util/concurrent/locks/AbstractQueuedSynchronizer;II)Z
440     //
441     //  meth = static jobject java.lang.invoke.LambdaForm$MH/0x0000000800066840.linkToTargetMethod(jobject, jobject)
442     //  msig = (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
443     //  call = (Ljava/lang/String;)Ljava/util/function/Predicate;
444     //
445     (*declared_signature_result) = call_site_sig;
446 
447   } else {
448     // We can just use the method's own signature.  It may differ from the call site, but not by much.
449     //
450     // Examples of how the call site signature can differ from the method's signature:
451     //
452     // meth = static final native jint java.lang.invoke.MethodHandle.linkToStatic(jobject, jobject, jint, jint, jobject)
453     // msig = (Ljava/lang/Object;Ljava/lang/Object;IILjava/lang/invoke/MemberName;)I
454     // call = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;IILjava/lang/invoke/MemberName;)Z
455     //
456     // meth = final native jint java.lang.invoke.MethodHandle.invokeBasic(jobject, jobject, jint, jint)
457     // msig = (Ljava/lang/Object;Ljava/lang/Object;II)I
458     // call = (Ljava/lang/invoke/VarHandle;Ljava/lang/Object;II)Z
459     //
460     (*declared_signature_result) = m->signature();
461   }
462   return m;
463 }
464 
465 // ------------------------------------------------------------------
466 // ciBytecodeStream::has_appendix
467 //
468 // Returns true if there is an appendix argument stored in the
469 // constant pool cache at the current bci.
470 bool ciBytecodeStream::has_appendix() {
471   VM_ENTRY_MARK;
472   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
473   return ConstantPool::has_appendix_at_if_loaded(cpool, get_method_index());
474 }
475 
476 // ------------------------------------------------------------------
477 // ciBytecodeStream::get_appendix
478 //
479 // Return the appendix argument stored in the constant pool cache at
480 // the current bci.
481 ciObject* ciBytecodeStream::get_appendix() {
482   VM_ENTRY_MARK;
483   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
484   oop appendix_oop = ConstantPool::appendix_at_if_loaded(cpool, get_method_index());
485   return CURRENT_ENV->get_object(appendix_oop);
486 }
487 
488 // ------------------------------------------------------------------
489 // ciBytecodeStream::has_local_signature
490 //
491 // Returns true if the method stored in the constant
492 // pool cache at the current bci has a local signature.
493 bool ciBytecodeStream::has_local_signature() {
494   GUARDED_VM_ENTRY(
495     constantPoolHandle cpool(Thread::current(), _method->get_Method()->constants());
496     return ConstantPool::has_local_signature_at_if_loaded(cpool, get_method_index());
497   )
498 }
499 
500 // ------------------------------------------------------------------
501 // ciBytecodeStream::get_declared_method_holder
502 //
503 // Get the declared holder of the currently referenced method.
504 //
505 // Usage note: the holder() of a ciMethod class returns the canonical
506 // holder of the method, rather than the holder declared in the
507 // bytecodes.
508 //
509 // There is no "will_link" result passed back.  The user is responsible
510 // for checking linkability when retrieving the associated method.
511 ciKlass* ciBytecodeStream::get_declared_method_holder() {
512   VM_ENTRY_MARK;
513   constantPoolHandle cpool(THREAD, _method->get_Method()->constants());
514   bool ignore;
515   // report as MethodHandle for invokedynamic, which is syntactically classless
516   if (cur_bc() == Bytecodes::_invokedynamic) {
517     return CURRENT_ENV->MethodHandle_klass();
518   }
519   return CURRENT_ENV->get_klass_by_index(cpool, get_method_holder_index(), ignore, _holder);
520 }
521 
522 // ------------------------------------------------------------------
523 // ciBytecodeStream::get_method_holder_index
524 //
525 // Get the constant pool index of the declared holder of the method
526 // referenced by the current bytecode.  Used for generating
527 // deoptimization information.
528 int ciBytecodeStream::get_method_holder_index() {
529   ConstantPool* cpool = _method->get_Method()->constants();
530   return cpool->klass_ref_index_at(get_method_index());
531 }
532 
533 // ------------------------------------------------------------------
534 // ciBytecodeStream::get_method_signature_index
535 //
536 // Get the constant pool index of the signature of the method
537 // referenced by the current bytecode.  Used for generating
538 // deoptimization information.
539 int ciBytecodeStream::get_method_signature_index(const constantPoolHandle& cpool) {
540   GUARDED_VM_ENTRY(
541     const int method_index = get_method_index();
542     const int name_and_type_index = cpool->name_and_type_ref_index_at(method_index);
543     return cpool->signature_ref_index_at(name_and_type_index);
544   )
545 }
546