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