1 /* 2 * Copyright (c) 1998, 2010, 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 "interpreter/bytecodes.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "interpreter/rewriter.hpp" 29 #include "memory/gcLocker.hpp" 30 #include "memory/oopFactory.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/generateOopMap.hpp" 33 #include "oops/objArrayOop.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "prims/methodComparator.hpp" 36 37 // Computes a CPC map (new_index -> original_index) for constant pool entries 38 // that are referred to by the interpreter at runtime via the constant pool cache. 39 // Also computes a CP map (original_index -> new_index). 40 // Marks entries in CP which require additional processing. 41 void Rewriter::compute_index_maps() { 42 const int length = _pool->length(); 43 init_cp_map(length); 44 jint tag_mask = 0; 45 for (int i = 0; i < length; i++) { 46 int tag = _pool->tag_at(i).value(); 47 tag_mask |= (1 << tag); 48 switch (tag) { 49 case JVM_CONSTANT_InterfaceMethodref: 50 case JVM_CONSTANT_Fieldref : // fall through 51 case JVM_CONSTANT_Methodref : // fall through 52 case JVM_CONSTANT_MethodHandle : // fall through 53 case JVM_CONSTANT_MethodType : // fall through 54 case JVM_CONSTANT_InvokeDynamic : // fall through 55 add_cp_cache_entry(i); 56 break; 57 } 58 } 59 60 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), 61 "all cp cache indexes fit in a u2"); 62 63 _have_invoke_dynamic = ((tag_mask & (1 << JVM_CONSTANT_InvokeDynamic)) != 0); 64 } 65 66 67 // Creates a constant pool cache given a CPC map 68 // This creates the constant pool cache initially in a state 69 // that is unsafe for concurrent GC processing but sets it to 70 // a safe mode before the constant pool cache is returned. 71 void Rewriter::make_constant_pool_cache(TRAPS) { 72 const int length = _cp_cache_map.length(); 73 constantPoolCacheOop cache = 74 oopFactory::new_constantPoolCache(length, methodOopDesc::IsUnsafeConc, CHECK); 75 cache->initialize(_cp_cache_map); 76 77 // Don't bother to the next pass if there is no JVM_CONSTANT_InvokeDynamic. 78 if (_have_invoke_dynamic) { 79 for (int i = 0; i < length; i++) { 80 int pool_index = cp_cache_entry_pool_index(i); 81 if (pool_index >= 0 && 82 _pool->tag_at(pool_index).is_invoke_dynamic()) { 83 int bsm_index = _pool->invoke_dynamic_bootstrap_method_ref_index_at(pool_index); 84 if (bsm_index != 0) { 85 assert(_pool->tag_at(bsm_index).is_method_handle(), "must be a MH constant"); 86 // There is a CP cache entry holding the BSM for these calls. 87 int bsm_cache_index = cp_entry_to_cp_cache(bsm_index); 88 cache->entry_at(i)->initialize_bootstrap_method_index_in_cache(bsm_cache_index); 89 } else { 90 // There is no CP cache entry holding the BSM for these calls. 91 // We will need to look for a class-global BSM, later. 92 guarantee(AllowTransitionalJSR292, ""); 93 } 94 } 95 } 96 } 97 98 _pool->set_cache(cache); 99 cache->set_constant_pool(_pool()); 100 } 101 102 103 104 // The new finalization semantics says that registration of 105 // finalizable objects must be performed on successful return from the 106 // Object.<init> constructor. We could implement this trivially if 107 // <init> were never rewritten but since JVMTI allows this to occur, a 108 // more complicated solution is required. A special return bytecode 109 // is used only by Object.<init> to signal the finalization 110 // registration point. Additionally local 0 must be preserved so it's 111 // available to pass to the registration function. For simplicty we 112 // require that local 0 is never overwritten so it's available as an 113 // argument for registration. 114 115 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { 116 RawBytecodeStream bcs(method); 117 while (!bcs.is_last_bytecode()) { 118 Bytecodes::Code opcode = bcs.raw_next(); 119 switch (opcode) { 120 case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; 121 122 case Bytecodes::_istore: 123 case Bytecodes::_lstore: 124 case Bytecodes::_fstore: 125 case Bytecodes::_dstore: 126 case Bytecodes::_astore: 127 if (bcs.get_index() != 0) continue; 128 129 // fall through 130 case Bytecodes::_istore_0: 131 case Bytecodes::_lstore_0: 132 case Bytecodes::_fstore_0: 133 case Bytecodes::_dstore_0: 134 case Bytecodes::_astore_0: 135 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), 136 "can't overwrite local 0 in Object.<init>"); 137 break; 138 } 139 } 140 } 141 142 143 // Rewrite a classfile-order CP index into a native-order CPC index. 144 void Rewriter::rewrite_member_reference(address bcp, int offset) { 145 address p = bcp + offset; 146 int cp_index = Bytes::get_Java_u2(p); 147 int cache_index = cp_entry_to_cp_cache(cp_index); 148 Bytes::put_native_u2(p, cache_index); 149 } 150 151 152 void Rewriter::rewrite_invokedynamic(address bcp, int offset) { 153 address p = bcp + offset; 154 assert(p[-1] == Bytecodes::_invokedynamic, ""); 155 int cp_index = Bytes::get_Java_u2(p); 156 int cpc = maybe_add_cp_cache_entry(cp_index); // add lazily 157 int cpc2 = add_secondary_cp_cache_entry(cpc); 158 159 // Replace the trailing four bytes with a CPC index for the dynamic 160 // call site. Unlike other CPC entries, there is one per bytecode, 161 // not just one per distinct CP entry. In other words, the 162 // CPC-to-CP relation is many-to-one for invokedynamic entries. 163 // This means we must use a larger index size than u2 to address 164 // all these entries. That is the main reason invokedynamic 165 // must have a five-byte instruction format. (Of course, other JVM 166 // implementations can use the bytes for other purposes.) 167 Bytes::put_native_u4(p, constantPoolCacheOopDesc::encode_secondary_index(cpc2)); 168 // Note: We use native_u4 format exclusively for 4-byte indexes. 169 } 170 171 172 // Rewrite some ldc bytecodes to _fast_aldc 173 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide) { 174 assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), ""); 175 address p = bcp + offset; 176 int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p); 177 constantTag tag = _pool->tag_at(cp_index).value(); 178 if (tag.is_method_handle() || tag.is_method_type()) { 179 int cache_index = cp_entry_to_cp_cache(cp_index); 180 if (is_wide) { 181 (*bcp) = Bytecodes::_fast_aldc_w; 182 assert(cache_index == (u2)cache_index, ""); 183 Bytes::put_native_u2(p, cache_index); 184 } else { 185 (*bcp) = Bytecodes::_fast_aldc; 186 assert(cache_index == (u1)cache_index, ""); 187 (*p) = (u1)cache_index; 188 } 189 } 190 } 191 192 193 // Rewrites a method given the index_map information 194 void Rewriter::scan_method(methodOop method) { 195 196 int nof_jsrs = 0; 197 bool has_monitor_bytecodes = false; 198 199 { 200 // We cannot tolerate a GC in this block, because we've 201 // cached the bytecodes in 'code_base'. If the methodOop 202 // moves, the bytecodes will also move. 203 No_Safepoint_Verifier nsv; 204 Bytecodes::Code c; 205 206 // Bytecodes and their length 207 const address code_base = method->code_base(); 208 const int code_length = method->code_size(); 209 210 int bc_length; 211 for (int bci = 0; bci < code_length; bci += bc_length) { 212 address bcp = code_base + bci; 213 int prefix_length = 0; 214 c = (Bytecodes::Code)(*bcp); 215 216 // Since we have the code, see if we can get the length 217 // directly. Some more complicated bytecodes will report 218 // a length of zero, meaning we need to make another method 219 // call to calculate the length. 220 bc_length = Bytecodes::length_for(c); 221 if (bc_length == 0) { 222 bc_length = Bytecodes::length_at(bcp); 223 224 // length_at will put us at the bytecode after the one modified 225 // by 'wide'. We don't currently examine any of the bytecodes 226 // modified by wide, but in case we do in the future... 227 if (c == Bytecodes::_wide) { 228 prefix_length = 1; 229 c = (Bytecodes::Code)bcp[1]; 230 } 231 } 232 233 assert(bc_length != 0, "impossible bytecode length"); 234 235 switch (c) { 236 case Bytecodes::_lookupswitch : { 237 #ifndef CC_INTERP 238 Bytecode_lookupswitch* bc = Bytecode_lookupswitch_at(bcp); 239 (*bcp) = ( 240 bc->number_of_pairs() < BinarySwitchThreshold 241 ? Bytecodes::_fast_linearswitch 242 : Bytecodes::_fast_binaryswitch 243 ); 244 #endif 245 break; 246 } 247 case Bytecodes::_getstatic : // fall through 248 case Bytecodes::_putstatic : // fall through 249 case Bytecodes::_getfield : // fall through 250 case Bytecodes::_putfield : // fall through 251 case Bytecodes::_invokevirtual : // fall through 252 case Bytecodes::_invokespecial : // fall through 253 case Bytecodes::_invokestatic : 254 case Bytecodes::_invokeinterface: 255 rewrite_member_reference(bcp, prefix_length+1); 256 break; 257 case Bytecodes::_invokedynamic: 258 rewrite_invokedynamic(bcp, prefix_length+1); 259 break; 260 case Bytecodes::_ldc: 261 maybe_rewrite_ldc(bcp, prefix_length+1, false); 262 break; 263 case Bytecodes::_ldc_w: 264 maybe_rewrite_ldc(bcp, prefix_length+1, true); 265 break; 266 case Bytecodes::_jsr : // fall through 267 case Bytecodes::_jsr_w : nof_jsrs++; break; 268 case Bytecodes::_monitorenter : // fall through 269 case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; 270 } 271 } 272 } 273 274 // Update access flags 275 if (has_monitor_bytecodes) { 276 method->set_has_monitor_bytecodes(); 277 } 278 279 // The present of a jsr bytecode implies that the method might potentially 280 // have to be rewritten, so we run the oopMapGenerator on the method 281 if (nof_jsrs > 0) { 282 method->set_has_jsrs(); 283 // Second pass will revisit this method. 284 assert(method->has_jsrs(), ""); 285 } 286 } 287 288 // After constant pool is created, revisit methods containing jsrs. 289 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) { 290 ResolveOopMapConflicts romc(method); 291 methodHandle original_method = method; 292 method = romc.do_potential_rewrite(CHECK_(methodHandle())); 293 if (method() != original_method()) { 294 // Insert invalid bytecode into original methodOop and set 295 // interpreter entrypoint, so that a executing this method 296 // will manifest itself in an easy recognizable form. 297 address bcp = original_method->bcp_from(0); 298 *bcp = (u1)Bytecodes::_shouldnotreachhere; 299 int kind = Interpreter::method_kind(original_method); 300 original_method->set_interpreter_kind(kind); 301 } 302 303 // Update monitor matching info. 304 if (romc.monitor_safe()) { 305 method->set_guaranteed_monitor_matching(); 306 } 307 308 return method; 309 } 310 311 312 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { 313 ResourceMark rm(THREAD); 314 Rewriter rw(klass, klass->constants(), klass->methods(), CHECK); 315 // (That's all, folks.) 316 } 317 318 319 void Rewriter::rewrite(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) { 320 ResourceMark rm(THREAD); 321 Rewriter rw(klass, cpool, methods, CHECK); 322 // (That's all, folks.) 323 } 324 325 326 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, objArrayHandle methods, TRAPS) 327 : _klass(klass), 328 _pool(cpool), 329 _methods(methods) 330 { 331 assert(_pool->cache() == NULL, "constant pool cache must not be set yet"); 332 333 // determine index maps for methodOop rewriting 334 compute_index_maps(); 335 336 if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) { 337 bool did_rewrite = false; 338 int i = _methods->length(); 339 while (i-- > 0) { 340 methodOop method = (methodOop)_methods->obj_at(i); 341 if (method->intrinsic_id() == vmIntrinsics::_Object_init) { 342 // rewrite the return bytecodes of Object.<init> to register the 343 // object for finalization if needed. 344 methodHandle m(THREAD, method); 345 rewrite_Object_init(m, CHECK); 346 did_rewrite = true; 347 break; 348 } 349 } 350 assert(did_rewrite, "must find Object::<init> to rewrite it"); 351 } 352 353 // rewrite methods, in two passes 354 int i, len = _methods->length(); 355 356 for (i = len; --i >= 0; ) { 357 methodOop method = (methodOop)_methods->obj_at(i); 358 scan_method(method); 359 } 360 361 // allocate constant pool cache, now that we've seen all the bytecodes 362 make_constant_pool_cache(CHECK); 363 364 for (i = len; --i >= 0; ) { 365 methodHandle m(THREAD, (methodOop)_methods->obj_at(i)); 366 367 if (m->has_jsrs()) { 368 m = rewrite_jsrs(m, CHECK); 369 // Method might have gotten rewritten. 370 _methods->obj_at_put(i, m()); 371 } 372 373 // Set up method entry points for compiler and interpreter. 374 m->link_method(m, CHECK); 375 376 #ifdef ASSERT 377 if (StressMethodComparator) { 378 static int nmc = 0; 379 for (int j = i; j >= 0 && j >= i-4; j--) { 380 if ((++nmc % 1000) == 0) tty->print_cr("Have run MethodComparator %d times...", nmc); 381 bool z = MethodComparator::methods_EMCP(m(), (methodOop)_methods->obj_at(j)); 382 if (j == i && !z) { 383 tty->print("MethodComparator FAIL: "); m->print(); m->print_codes(); 384 assert(z, "method must compare equal to itself"); 385 } 386 } 387 } 388 #endif //ASSERT 389 } 390 }