1 /*
   2  * Copyright (c) 2000, 2016, 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 "oops/oop.inline.hpp"
  27 #include "oops/symbol.hpp"
  28 #include "prims/jvmtiRedefineClassesTrace.hpp"
  29 #include "prims/methodComparator.hpp"
  30 #include "runtime/handles.inline.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 BytecodeStream *MethodComparator::_s_old;
  34 BytecodeStream *MethodComparator::_s_new;
  35 ConstantPool* MethodComparator::_old_cp;
  36 ConstantPool* MethodComparator::_new_cp;
  37 BciMap *MethodComparator::_bci_map;
  38 GrowableArray<int> *MethodComparator::_fwd_jmps;
  39 
  40 bool MethodComparator::methods_EMCP(Method* old_method, Method* new_method) {
  41   if (old_method->code_size() != new_method->code_size())
  42     return false;
  43   if (check_stack_and_locals_size(old_method, new_method) != 0) {
  44     // RC_TRACE macro has an embedded ResourceMark
  45     RC_TRACE(0x00800000, ("Methods %s non-comparable with diagnosis %d",
  46       old_method->name()->as_C_string(),
  47       check_stack_and_locals_size(old_method, new_method)));
  48     return false;
  49   }
  50 
  51   _old_cp = old_method->constants();
  52   _new_cp = new_method->constants();
  53   BytecodeStream s_old(old_method);
  54   BytecodeStream s_new(new_method);
  55   _s_old = &s_old;
  56   _s_new = &s_new;
  57   Bytecodes::Code c_old, c_new;
  58 
  59   while ((c_old = s_old.next()) >= 0) {
  60     if ((c_new = s_new.next()) < 0 || c_old != c_new)
  61       return false;
  62 
  63     if (! args_same(c_old, c_new))
  64       return false;
  65   }
  66   return true;
  67 }
  68 
  69 bool MethodComparator::args_same(Bytecodes::Code c_old, Bytecodes::Code c_new) {
  70   // BytecodeStream returns the correct standard Java bytecodes for various "fast"
  71   // bytecode versions, so we don't have to bother about them here..
  72   switch (c_old) {
  73   case Bytecodes::_new            : // fall through
  74   case Bytecodes::_anewarray      : // fall through
  75   case Bytecodes::_multianewarray : // fall through
  76   case Bytecodes::_checkcast      : // fall through
  77   case Bytecodes::_instanceof     : {
  78     u2 cpi_old = _s_old->get_index_u2();
  79     u2 cpi_new = _s_new->get_index_u2();
  80     if ((_old_cp->klass_at_noresolve(cpi_old) != _new_cp->klass_at_noresolve(cpi_new)))
  81         return false;
  82     if (c_old == Bytecodes::_multianewarray &&
  83         *(jbyte*)(_s_old->bcp() + 3) != *(jbyte*)(_s_new->bcp() + 3))
  84       return false;
  85     break;
  86   }
  87 
  88   case Bytecodes::_getstatic       : // fall through
  89   case Bytecodes::_putstatic       : // fall through
  90   case Bytecodes::_getfield        : // fall through
  91   case Bytecodes::_putfield        : // fall through
  92   case Bytecodes::_invokevirtual   : // fall through
  93   case Bytecodes::_invokespecial   : // fall through
  94   case Bytecodes::_invokestatic    : // fall through
  95   case Bytecodes::_invokeinterface : {
  96     int cpci_old = _s_old->get_index_u2_cpcache();
  97     int cpci_new = _s_new->get_index_u2_cpcache();
  98     // Check if the names of classes, field/method names and signatures at these indexes
  99     // are the same. Indices which are really into constantpool cache (rather than constant
 100     // pool itself) are accepted by the constantpool query routines below.
 101     if ((_old_cp->klass_ref_at_noresolve(cpci_old) != _new_cp->klass_ref_at_noresolve(cpci_new)) ||
 102         (_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
 103         (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
 104       return false;
 105     break;
 106   }
 107   case Bytecodes::_invokedynamic: {
 108     int cpci_old = _s_old->get_index_u4();
 109     int cpci_new = _s_new->get_index_u4();
 110 
 111     // Check if the names of classes, field/method names and signatures at these indexes
 112     // are the same. Indices which are really into constantpool cache (rather than constant
 113     // pool itself) are accepted by the constantpool query routines below.
 114     if ((_old_cp->name_ref_at(cpci_old) != _new_cp->name_ref_at(cpci_new)) ||
 115         (_old_cp->signature_ref_at(cpci_old) != _new_cp->signature_ref_at(cpci_new)))
 116       return false;
 117 
 118     // Translate object indexes to constant pool cache indexes.
 119     cpci_old = _old_cp->invokedynamic_cp_cache_index(cpci_old);
 120     cpci_new = _new_cp->invokedynamic_cp_cache_index(cpci_new);
 121 
 122     int cpi_old = _old_cp->cache()->entry_at(cpci_old)->constant_pool_index();
 123     int cpi_new = _new_cp->cache()->entry_at(cpci_new)->constant_pool_index();
 124     int bsm_old = _old_cp->invoke_dynamic_bootstrap_method_ref_index_at(cpi_old);
 125     int bsm_new = _new_cp->invoke_dynamic_bootstrap_method_ref_index_at(cpi_new);
 126     if (!pool_constants_same(bsm_old, bsm_new))
 127       return false;
 128     int cnt_old = _old_cp->invoke_dynamic_argument_count_at(cpi_old);
 129     int cnt_new = _new_cp->invoke_dynamic_argument_count_at(cpi_new);
 130     if (cnt_old != cnt_new)
 131       return false;
 132     for (int arg_i = 0; arg_i < cnt_old; arg_i++) {
 133       int idx_old = _old_cp->invoke_dynamic_argument_index_at(cpi_old, arg_i);
 134       int idx_new = _new_cp->invoke_dynamic_argument_index_at(cpi_new, arg_i);
 135       if (!pool_constants_same(idx_old, idx_new))
 136         return false;
 137     }
 138     break;
 139   }
 140 
 141   case Bytecodes::_ldc   : // fall through
 142   case Bytecodes::_ldc_w : {
 143     Bytecode_loadconstant ldc_old(_s_old->method(), _s_old->bci());
 144     Bytecode_loadconstant ldc_new(_s_new->method(), _s_new->bci());
 145     int cpi_old = ldc_old.pool_index();
 146     int cpi_new = ldc_new.pool_index();
 147     if (!pool_constants_same(cpi_old, cpi_new))
 148       return false;
 149     break;
 150   }
 151 
 152   case Bytecodes::_ldc2_w : {
 153     u2 cpi_old = _s_old->get_index_u2();
 154     u2 cpi_new = _s_new->get_index_u2();
 155     constantTag tag_old = _old_cp->tag_at(cpi_old);
 156     constantTag tag_new = _new_cp->tag_at(cpi_new);
 157     if (tag_old.value() != tag_new.value())
 158       return false;
 159     if (tag_old.is_long()) {
 160       if (_old_cp->long_at(cpi_old) != _new_cp->long_at(cpi_new))
 161         return false;
 162     } else {
 163       // Use jlong_cast to compare the bits rather than numerical values.
 164       // This makes a difference for NaN constants.
 165       if (jlong_cast(_old_cp->double_at(cpi_old)) != jlong_cast(_new_cp->double_at(cpi_new)))
 166         return false;
 167     }
 168     break;
 169   }
 170 
 171   case Bytecodes::_bipush :
 172     if (_s_old->bcp()[1] != _s_new->bcp()[1])
 173       return false;
 174     break;
 175 
 176   case Bytecodes::_sipush    :
 177     if (_s_old->get_index_u2() != _s_new->get_index_u2())
 178       return false;
 179     break;
 180 
 181   case Bytecodes::_aload  : // fall through
 182   case Bytecodes::_astore : // fall through
 183   case Bytecodes::_dload  : // fall through
 184   case Bytecodes::_dstore : // fall through
 185   case Bytecodes::_fload  : // fall through
 186   case Bytecodes::_fstore : // fall through
 187   case Bytecodes::_iload  : // fall through
 188   case Bytecodes::_istore : // fall through
 189   case Bytecodes::_lload  : // fall through
 190   case Bytecodes::_lstore : // fall through
 191   case Bytecodes::_ret    :
 192     if (_s_old->is_wide() != _s_new->is_wide())
 193       return false;
 194     if (_s_old->get_index() != _s_new->get_index())
 195       return false;
 196     break;
 197 
 198   case Bytecodes::_goto      : // fall through
 199   case Bytecodes::_if_acmpeq : // fall through
 200   case Bytecodes::_if_acmpne : // fall through
 201   case Bytecodes::_if_icmpeq : // fall through
 202   case Bytecodes::_if_icmpne : // fall through
 203   case Bytecodes::_if_icmplt : // fall through
 204   case Bytecodes::_if_icmpge : // fall through
 205   case Bytecodes::_if_icmpgt : // fall through
 206   case Bytecodes::_if_icmple : // fall through
 207   case Bytecodes::_ifeq      : // fall through
 208   case Bytecodes::_ifne      : // fall through
 209   case Bytecodes::_iflt      : // fall through
 210   case Bytecodes::_ifge      : // fall through
 211   case Bytecodes::_ifgt      : // fall through
 212   case Bytecodes::_ifle      : // fall through
 213   case Bytecodes::_ifnonnull : // fall through
 214   case Bytecodes::_ifnull    : // fall through
 215   case Bytecodes::_jsr       : {
 216     int old_ofs = _s_old->bytecode().get_offset_s2(c_old);
 217     int new_ofs = _s_new->bytecode().get_offset_s2(c_new);
 218     if (old_ofs != new_ofs)
 219       return false;
 220     break;
 221   }
 222 
 223   case Bytecodes::_iinc :
 224     if (_s_old->is_wide() != _s_new->is_wide())
 225       return false;
 226     if (! _s_old->is_wide()) {
 227       // We could use get_index_u1 and get_constant_u1, but it's simpler to grab both bytes at once:
 228       if (Bytes::get_Java_u2(_s_old->bcp() + 1) != Bytes::get_Java_u2(_s_new->bcp() + 1))
 229         return false;
 230     } else {
 231       // We could use get_index_u2 and get_constant_u2, but it's simpler to grab all four bytes at once:
 232       if (Bytes::get_Java_u4(_s_old->bcp() + 1) != Bytes::get_Java_u4(_s_new->bcp() + 1))
 233         return false;
 234     }
 235     break;
 236 
 237   case Bytecodes::_goto_w : // fall through
 238   case Bytecodes::_jsr_w  : {
 239     int old_ofs = _s_old->bytecode().get_offset_s4(c_old);
 240     int new_ofs = _s_new->bytecode().get_offset_s4(c_new);
 241     if (old_ofs != new_ofs)
 242       return false;
 243     break;
 244   }
 245 
 246   case Bytecodes::_lookupswitch : // fall through
 247   case Bytecodes::_tableswitch  : {
 248     int len_old = _s_old->instruction_size();
 249     int len_new = _s_new->instruction_size();
 250     if (len_old != len_new)
 251       return false;
 252     if (memcmp(_s_old->bcp(), _s_new->bcp(), len_old) != 0)
 253       return false;
 254     break;
 255   }
 256   }
 257 
 258   return true;
 259 }
 260 
 261 bool MethodComparator::pool_constants_same(int cpi_old, int cpi_new) {
 262   constantTag tag_old = _old_cp->tag_at(cpi_old);
 263   constantTag tag_new = _new_cp->tag_at(cpi_new);
 264   if (tag_old.is_int() || tag_old.is_float()) {
 265     if (tag_old.value() != tag_new.value())
 266       return false;
 267     if (tag_old.is_int()) {
 268       if (_old_cp->int_at(cpi_old) != _new_cp->int_at(cpi_new))
 269         return false;
 270     } else {
 271       // Use jint_cast to compare the bits rather than numerical values.
 272       // This makes a difference for NaN constants.
 273       if (jint_cast(_old_cp->float_at(cpi_old)) != jint_cast(_new_cp->float_at(cpi_new)))
 274         return false;
 275     }
 276   } else if (tag_old.is_string() && tag_new.is_string()) {
 277     if (strcmp(_old_cp->string_at_noresolve(cpi_old),
 278                _new_cp->string_at_noresolve(cpi_new)) != 0)
 279       return false;
 280     if (_old_cp->is_pseudo_string_at(cpi_old) || _new_cp->is_pseudo_string_at(cpi_new))
 281       return (_old_cp->is_pseudo_string_at(cpi_old) == _new_cp->is_pseudo_string_at(cpi_new));
 282   } else if (tag_old.is_klass() || tag_old.is_unresolved_klass()) {
 283     // tag_old should be klass - 4881222
 284     if (! (tag_new.is_unresolved_klass() || tag_new.is_klass()))
 285       return false;
 286     if (_old_cp->klass_at_noresolve(cpi_old) !=
 287         _new_cp->klass_at_noresolve(cpi_new))
 288       return false;
 289   } else if (tag_old.is_method_type() && tag_new.is_method_type()) {
 290     int mti_old = _old_cp->method_type_index_at(cpi_old);
 291     int mti_new = _new_cp->method_type_index_at(cpi_new);
 292     if ((_old_cp->symbol_at(mti_old) != _new_cp->symbol_at(mti_new)))
 293       return false;
 294   } else if (tag_old.is_method_handle() && tag_new.is_method_handle()) {
 295     if (_old_cp->method_handle_ref_kind_at(cpi_old) !=
 296         _new_cp->method_handle_ref_kind_at(cpi_new))
 297       return false;
 298     int mhi_old = _old_cp->method_handle_index_at(cpi_old);
 299     int mhi_new = _new_cp->method_handle_index_at(cpi_new);
 300     if ((_old_cp->uncached_klass_ref_at_noresolve(mhi_old) != _new_cp->uncached_klass_ref_at_noresolve(mhi_new)) ||
 301         (_old_cp->uncached_name_ref_at(mhi_old) != _new_cp->uncached_name_ref_at(mhi_new)) ||
 302         (_old_cp->uncached_signature_ref_at(mhi_old) != _new_cp->uncached_signature_ref_at(mhi_new)))
 303       return false;
 304   } else {
 305     return false;  // unknown tag
 306   }
 307   return true;
 308 }
 309 
 310 
 311 int MethodComparator::check_stack_and_locals_size(Method* old_method, Method* new_method) {
 312   if (old_method->max_stack() != new_method->max_stack()) {
 313     return 1;
 314   } else if (old_method->max_locals() != new_method->max_locals()) {
 315     return 2;
 316   } else if (old_method->size_of_parameters() != new_method->size_of_parameters()) {
 317     return 3;
 318   } else return 0;
 319 }