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