Mon, 12 Dec 2016 12:53:38 +0100
8157181: Compilers accept modification of final fields outside initializer methods
Summary: Track initialized final field updates; disable constant folding if an update is detected.
Reviewed-by: vlivanov, dnsimon, forax, never, kvn, coleenp
duke@435 | 1 | /* |
coleenp@6307 | 2 | * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "interpreter/bytecodes.hpp" |
stefank@2314 | 27 | #include "interpreter/interpreter.hpp" |
stefank@2314 | 28 | #include "interpreter/rewriter.hpp" |
stefank@2314 | 29 | #include "memory/gcLocker.hpp" |
stefank@2314 | 30 | #include "memory/resourceArea.hpp" |
stefank@2314 | 31 | #include "oops/generateOopMap.hpp" |
twisti@3969 | 32 | #include "prims/methodHandles.hpp" |
duke@435 | 33 | |
jrose@1161 | 34 | // Computes a CPC map (new_index -> original_index) for constant pool entries |
duke@435 | 35 | // that are referred to by the interpreter at runtime via the constant pool cache. |
jrose@1161 | 36 | // Also computes a CP map (original_index -> new_index). |
jrose@1161 | 37 | // Marks entries in CP which require additional processing. |
jrose@1161 | 38 | void Rewriter::compute_index_maps() { |
jrose@1161 | 39 | const int length = _pool->length(); |
coleenp@4037 | 40 | init_maps(length); |
twisti@3969 | 41 | bool saw_mh_symbol = false; |
duke@435 | 42 | for (int i = 0; i < length; i++) { |
jrose@1161 | 43 | int tag = _pool->tag_at(i).value(); |
jrose@1161 | 44 | switch (tag) { |
jrose@1161 | 45 | case JVM_CONSTANT_InterfaceMethodref: |
duke@435 | 46 | case JVM_CONSTANT_Fieldref : // fall through |
duke@435 | 47 | case JVM_CONSTANT_Methodref : // fall through |
coleenp@4037 | 48 | add_cp_cache_entry(i); |
coleenp@4037 | 49 | break; |
coleenp@4037 | 50 | case JVM_CONSTANT_String: |
jrose@1957 | 51 | case JVM_CONSTANT_MethodHandle : // fall through |
jrose@1957 | 52 | case JVM_CONSTANT_MethodType : // fall through |
coleenp@4037 | 53 | add_resolved_references_entry(i); |
jrose@1161 | 54 | break; |
twisti@3969 | 55 | case JVM_CONSTANT_Utf8: |
twisti@3969 | 56 | if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle()) |
twisti@3969 | 57 | saw_mh_symbol = true; |
twisti@3969 | 58 | break; |
duke@435 | 59 | } |
duke@435 | 60 | } |
jrose@1161 | 61 | |
coleenp@4037 | 62 | // Record limits of resolved reference map for constant pool cache indices |
coleenp@4037 | 63 | record_map_limits(); |
coleenp@4037 | 64 | |
jrose@1161 | 65 | guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1), |
jrose@1161 | 66 | "all cp cache indexes fit in a u2"); |
jrose@2015 | 67 | |
twisti@3969 | 68 | if (saw_mh_symbol) |
twisti@3969 | 69 | _method_handle_invokers.initialize(length, (int)0); |
duke@435 | 70 | } |
duke@435 | 71 | |
coleenp@2945 | 72 | // Unrewrite the bytecodes if an error occurs. |
coleenp@6121 | 73 | void Rewriter::restore_bytecodes() { |
coleenp@2945 | 74 | int len = _methods->length(); |
coleenp@6121 | 75 | bool invokespecial_error = false; |
coleenp@2945 | 76 | |
coleenp@2945 | 77 | for (int i = len-1; i >= 0; i--) { |
coleenp@4037 | 78 | Method* method = _methods->at(i); |
coleenp@6121 | 79 | scan_method(method, true, &invokespecial_error); |
coleenp@6121 | 80 | assert(!invokespecial_error, "reversing should not get an invokespecial error"); |
coleenp@2945 | 81 | } |
coleenp@2945 | 82 | } |
duke@435 | 83 | |
jrose@1161 | 84 | // Creates a constant pool cache given a CPC map |
jrose@1161 | 85 | void Rewriter::make_constant_pool_cache(TRAPS) { |
coleenp@4037 | 86 | ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data(); |
coleenp@4037 | 87 | ConstantPoolCache* cache = |
coleenp@6081 | 88 | ConstantPoolCache::allocate(loader_data, _cp_cache_map, |
coleenp@6081 | 89 | _invokedynamic_cp_cache_map, |
coleenp@4712 | 90 | _invokedynamic_references_map, CHECK); |
coleenp@4037 | 91 | |
coleenp@4037 | 92 | // initialize object cache in constant pool |
coleenp@4037 | 93 | _pool->initialize_resolved_references(loader_data, _resolved_references_map, |
coleenp@4037 | 94 | _resolved_reference_limit, |
coleenp@4037 | 95 | CHECK); |
jrose@1161 | 96 | _pool->set_cache(cache); |
jrose@1161 | 97 | cache->set_constant_pool(_pool()); |
duke@435 | 98 | } |
duke@435 | 99 | |
duke@435 | 100 | |
duke@435 | 101 | |
duke@435 | 102 | // The new finalization semantics says that registration of |
duke@435 | 103 | // finalizable objects must be performed on successful return from the |
duke@435 | 104 | // Object.<init> constructor. We could implement this trivially if |
duke@435 | 105 | // <init> were never rewritten but since JVMTI allows this to occur, a |
duke@435 | 106 | // more complicated solution is required. A special return bytecode |
duke@435 | 107 | // is used only by Object.<init> to signal the finalization |
duke@435 | 108 | // registration point. Additionally local 0 must be preserved so it's |
duke@435 | 109 | // available to pass to the registration function. For simplicty we |
duke@435 | 110 | // require that local 0 is never overwritten so it's available as an |
duke@435 | 111 | // argument for registration. |
duke@435 | 112 | |
duke@435 | 113 | void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) { |
duke@435 | 114 | RawBytecodeStream bcs(method); |
duke@435 | 115 | while (!bcs.is_last_bytecode()) { |
duke@435 | 116 | Bytecodes::Code opcode = bcs.raw_next(); |
duke@435 | 117 | switch (opcode) { |
duke@435 | 118 | case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break; |
duke@435 | 119 | |
duke@435 | 120 | case Bytecodes::_istore: |
duke@435 | 121 | case Bytecodes::_lstore: |
duke@435 | 122 | case Bytecodes::_fstore: |
duke@435 | 123 | case Bytecodes::_dstore: |
duke@435 | 124 | case Bytecodes::_astore: |
duke@435 | 125 | if (bcs.get_index() != 0) continue; |
duke@435 | 126 | |
duke@435 | 127 | // fall through |
duke@435 | 128 | case Bytecodes::_istore_0: |
duke@435 | 129 | case Bytecodes::_lstore_0: |
duke@435 | 130 | case Bytecodes::_fstore_0: |
duke@435 | 131 | case Bytecodes::_dstore_0: |
duke@435 | 132 | case Bytecodes::_astore_0: |
duke@435 | 133 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), |
duke@435 | 134 | "can't overwrite local 0 in Object.<init>"); |
duke@435 | 135 | break; |
duke@435 | 136 | } |
duke@435 | 137 | } |
duke@435 | 138 | } |
duke@435 | 139 | |
duke@435 | 140 | |
jrose@1161 | 141 | // Rewrite a classfile-order CP index into a native-order CPC index. |
coleenp@2945 | 142 | void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) { |
jrose@1161 | 143 | address p = bcp + offset; |
coleenp@2945 | 144 | if (!reverse) { |
coleenp@2945 | 145 | int cp_index = Bytes::get_Java_u2(p); |
coleenp@2945 | 146 | int cache_index = cp_entry_to_cp_cache(cp_index); |
coleenp@2945 | 147 | Bytes::put_native_u2(p, cache_index); |
twisti@3969 | 148 | if (!_method_handle_invokers.is_empty()) |
coleenp@4037 | 149 | maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse); |
coleenp@2945 | 150 | } else { |
coleenp@2945 | 151 | int cache_index = Bytes::get_native_u2(p); |
coleenp@2945 | 152 | int pool_index = cp_cache_entry_pool_index(cache_index); |
coleenp@2945 | 153 | Bytes::put_Java_u2(p, pool_index); |
twisti@3969 | 154 | if (!_method_handle_invokers.is_empty()) |
coleenp@4037 | 155 | maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse); |
twisti@3969 | 156 | } |
twisti@3969 | 157 | } |
twisti@3969 | 158 | |
coleenp@6081 | 159 | // If the constant pool entry for invokespecial is InterfaceMethodref, |
coleenp@6081 | 160 | // we need to add a separate cpCache entry for its resolution, because it is |
coleenp@6081 | 161 | // different than the resolution for invokeinterface with InterfaceMethodref. |
coleenp@6081 | 162 | // These cannot share cpCache entries. It's unclear if all invokespecial to |
coleenp@6081 | 163 | // InterfaceMethodrefs would resolve to the same thing so a new cpCache entry |
coleenp@6081 | 164 | // is created for each one. This was added with lambda. |
coleenp@6121 | 165 | void Rewriter::rewrite_invokespecial(address bcp, int offset, bool reverse, bool* invokespecial_error) { |
coleenp@6081 | 166 | address p = bcp + offset; |
coleenp@6081 | 167 | if (!reverse) { |
coleenp@6081 | 168 | int cp_index = Bytes::get_Java_u2(p); |
coleenp@6121 | 169 | if (_pool->tag_at(cp_index).is_interface_method()) { |
coleenp@6081 | 170 | int cache_index = add_invokespecial_cp_cache_entry(cp_index); |
coleenp@6081 | 171 | if (cache_index != (int)(jushort) cache_index) { |
coleenp@6121 | 172 | *invokespecial_error = true; |
coleenp@6081 | 173 | } |
coleenp@6081 | 174 | Bytes::put_native_u2(p, cache_index); |
coleenp@6081 | 175 | } else { |
coleenp@6121 | 176 | rewrite_member_reference(bcp, offset, reverse); |
coleenp@6121 | 177 | } |
coleenp@6121 | 178 | } else { |
coleenp@6121 | 179 | rewrite_member_reference(bcp, offset, reverse); |
coleenp@6081 | 180 | } |
coleenp@6081 | 181 | } |
coleenp@6081 | 182 | |
twisti@3969 | 183 | |
twisti@3969 | 184 | // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.) |
coleenp@4037 | 185 | void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) { |
twisti@3969 | 186 | if (!reverse) { |
twisti@3969 | 187 | if ((*opc) == (u1)Bytecodes::_invokevirtual || |
twisti@3969 | 188 | // allow invokespecial as an alias, although it would be very odd: |
twisti@3969 | 189 | (*opc) == (u1)Bytecodes::_invokespecial) { |
twisti@3969 | 190 | assert(_pool->tag_at(cp_index).is_method(), "wrong index"); |
twisti@3969 | 191 | // Determine whether this is a signature-polymorphic method. |
twisti@3969 | 192 | if (cp_index >= _method_handle_invokers.length()) return; |
twisti@3969 | 193 | int status = _method_handle_invokers[cp_index]; |
twisti@3969 | 194 | assert(status >= -1 && status <= 1, "oob tri-state"); |
twisti@3969 | 195 | if (status == 0) { |
twisti@3969 | 196 | if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() && |
twisti@3969 | 197 | MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(), |
coleenp@4037 | 198 | _pool->name_ref_at(cp_index))) { |
coleenp@4037 | 199 | // we may need a resolved_refs entry for the appendix |
twisti@4133 | 200 | add_invokedynamic_resolved_references_entries(cp_index, cache_index); |
twisti@3969 | 201 | status = +1; |
coleenp@4037 | 202 | } else { |
twisti@3969 | 203 | status = -1; |
coleenp@4037 | 204 | } |
twisti@3969 | 205 | _method_handle_invokers[cp_index] = status; |
twisti@3969 | 206 | } |
twisti@3969 | 207 | // We use a special internal bytecode for such methods (if non-static). |
twisti@3969 | 208 | // The basic reason for this is that such methods need an extra "appendix" argument |
twisti@3969 | 209 | // to transmit the call site's intended call type. |
twisti@3969 | 210 | if (status > 0) { |
twisti@3969 | 211 | (*opc) = (u1)Bytecodes::_invokehandle; |
twisti@3969 | 212 | } |
twisti@3969 | 213 | } |
twisti@3969 | 214 | } else { |
twisti@3969 | 215 | // Do not need to look at cp_index. |
twisti@3969 | 216 | if ((*opc) == (u1)Bytecodes::_invokehandle) { |
twisti@3969 | 217 | (*opc) = (u1)Bytecodes::_invokevirtual; |
twisti@3969 | 218 | // Ignore corner case of original _invokespecial instruction. |
twisti@3969 | 219 | // This is safe because (a) the signature polymorphic method was final, and |
twisti@3969 | 220 | // (b) the implementation of MethodHandle will not call invokespecial on it. |
twisti@3969 | 221 | } |
coleenp@2945 | 222 | } |
jrose@1161 | 223 | } |
jrose@1161 | 224 | |
jrose@1161 | 225 | |
coleenp@2945 | 226 | void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) { |
jrose@1161 | 227 | address p = bcp + offset; |
coleenp@2945 | 228 | assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode"); |
coleenp@2945 | 229 | if (!reverse) { |
coleenp@2945 | 230 | int cp_index = Bytes::get_Java_u2(p); |
coleenp@4037 | 231 | int cache_index = add_invokedynamic_cp_cache_entry(cp_index); |
coleenp@6081 | 232 | int resolved_index = add_invokedynamic_resolved_references_entries(cp_index, cache_index); |
coleenp@2945 | 233 | // Replace the trailing four bytes with a CPC index for the dynamic |
coleenp@2945 | 234 | // call site. Unlike other CPC entries, there is one per bytecode, |
coleenp@2945 | 235 | // not just one per distinct CP entry. In other words, the |
coleenp@2945 | 236 | // CPC-to-CP relation is many-to-one for invokedynamic entries. |
coleenp@2945 | 237 | // This means we must use a larger index size than u2 to address |
coleenp@2945 | 238 | // all these entries. That is the main reason invokedynamic |
coleenp@2945 | 239 | // must have a five-byte instruction format. (Of course, other JVM |
coleenp@2945 | 240 | // implementations can use the bytes for other purposes.) |
coleenp@6081 | 241 | // Note: We use native_u4 format exclusively for 4-byte indexes. |
coleenp@4037 | 242 | Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index)); |
coleenp@6081 | 243 | // add the bcp in case we need to patch this bytecode if we also find a |
coleenp@6081 | 244 | // invokespecial/InterfaceMethodref in the bytecode stream |
coleenp@6081 | 245 | _patch_invokedynamic_bcps->push(p); |
coleenp@6081 | 246 | _patch_invokedynamic_refs->push(resolved_index); |
coleenp@2945 | 247 | } else { |
coleenp@4037 | 248 | int cache_index = ConstantPool::decode_invokedynamic_index( |
coleenp@2945 | 249 | Bytes::get_native_u4(p)); |
coleenp@6081 | 250 | // We will reverse the bytecode rewriting _after_ adjusting them. |
coleenp@6081 | 251 | // Adjust the cache index by offset to the invokedynamic entries in the |
coleenp@6081 | 252 | // cpCache plus the delta if the invokedynamic bytecodes were adjusted. |
coleenp@6307 | 253 | int adjustment = cp_cache_delta() + _first_iteration_cp_cache_limit; |
coleenp@6307 | 254 | int cp_index = invokedynamic_cp_cache_entry_pool_index(cache_index - adjustment); |
coleenp@4037 | 255 | assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index"); |
coleenp@2945 | 256 | // zero out 4 bytes |
coleenp@2945 | 257 | Bytes::put_Java_u4(p, 0); |
coleenp@4037 | 258 | Bytes::put_Java_u2(p, cp_index); |
coleenp@2945 | 259 | } |
jrose@1161 | 260 | } |
jrose@1161 | 261 | |
coleenp@6081 | 262 | void Rewriter::patch_invokedynamic_bytecodes() { |
coleenp@6081 | 263 | // If the end of the cp_cache is the same as after initializing with the |
coleenp@6081 | 264 | // cpool, nothing needs to be done. Invokedynamic bytecodes are at the |
coleenp@6081 | 265 | // correct offsets. ie. no invokespecials added |
coleenp@6081 | 266 | int delta = cp_cache_delta(); |
coleenp@6081 | 267 | if (delta > 0) { |
coleenp@6081 | 268 | int length = _patch_invokedynamic_bcps->length(); |
coleenp@6081 | 269 | assert(length == _patch_invokedynamic_refs->length(), |
coleenp@6081 | 270 | "lengths should match"); |
coleenp@6081 | 271 | for (int i = 0; i < length; i++) { |
coleenp@6081 | 272 | address p = _patch_invokedynamic_bcps->at(i); |
coleenp@6081 | 273 | int cache_index = ConstantPool::decode_invokedynamic_index( |
coleenp@6081 | 274 | Bytes::get_native_u4(p)); |
coleenp@6081 | 275 | Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index + delta)); |
coleenp@6081 | 276 | |
coleenp@6081 | 277 | // invokedynamic resolved references map also points to cp cache and must |
coleenp@6081 | 278 | // add delta to each. |
coleenp@6081 | 279 | int resolved_index = _patch_invokedynamic_refs->at(i); |
coleenp@6081 | 280 | for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) { |
coleenp@6081 | 281 | assert(_invokedynamic_references_map[resolved_index+entry] == cache_index, |
coleenp@6081 | 282 | "should be the same index"); |
coleenp@6081 | 283 | _invokedynamic_references_map.at_put(resolved_index+entry, |
coleenp@6081 | 284 | cache_index + delta); |
coleenp@6081 | 285 | } |
coleenp@6081 | 286 | } |
coleenp@6081 | 287 | } |
coleenp@6081 | 288 | } |
coleenp@6081 | 289 | |
jrose@1161 | 290 | |
jrose@1957 | 291 | // Rewrite some ldc bytecodes to _fast_aldc |
coleenp@2945 | 292 | void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide, |
coleenp@2945 | 293 | bool reverse) { |
coleenp@2945 | 294 | if (!reverse) { |
coleenp@2945 | 295 | assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode"); |
coleenp@2945 | 296 | address p = bcp + offset; |
coleenp@2945 | 297 | int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p); |
coleenp@2945 | 298 | constantTag tag = _pool->tag_at(cp_index).value(); |
coleenp@4643 | 299 | if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) { |
coleenp@4037 | 300 | int ref_index = cp_entry_to_resolved_references(cp_index); |
coleenp@2945 | 301 | if (is_wide) { |
coleenp@2945 | 302 | (*bcp) = Bytecodes::_fast_aldc_w; |
coleenp@4037 | 303 | assert(ref_index == (u2)ref_index, "index overflow"); |
coleenp@4037 | 304 | Bytes::put_native_u2(p, ref_index); |
coleenp@2945 | 305 | } else { |
coleenp@2945 | 306 | (*bcp) = Bytecodes::_fast_aldc; |
coleenp@4037 | 307 | assert(ref_index == (u1)ref_index, "index overflow"); |
coleenp@4037 | 308 | (*p) = (u1)ref_index; |
coleenp@2945 | 309 | } |
coleenp@2945 | 310 | } |
coleenp@2945 | 311 | } else { |
coleenp@2945 | 312 | Bytecodes::Code rewritten_bc = |
coleenp@2945 | 313 | (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc); |
coleenp@2945 | 314 | if ((*bcp) == rewritten_bc) { |
coleenp@2945 | 315 | address p = bcp + offset; |
coleenp@4037 | 316 | int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p); |
coleenp@4037 | 317 | int pool_index = resolved_references_entry_to_pool_index(ref_index); |
coleenp@2945 | 318 | if (is_wide) { |
coleenp@2945 | 319 | (*bcp) = Bytecodes::_ldc_w; |
coleenp@2945 | 320 | assert(pool_index == (u2)pool_index, "index overflow"); |
coleenp@2945 | 321 | Bytes::put_Java_u2(p, pool_index); |
coleenp@2945 | 322 | } else { |
coleenp@2945 | 323 | (*bcp) = Bytecodes::_ldc; |
coleenp@2945 | 324 | assert(pool_index == (u1)pool_index, "index overflow"); |
coleenp@2945 | 325 | (*p) = (u1)pool_index; |
coleenp@2945 | 326 | } |
jrose@1957 | 327 | } |
jrose@1957 | 328 | } |
jrose@1957 | 329 | } |
jrose@1957 | 330 | |
jrose@1957 | 331 | |
duke@435 | 332 | // Rewrites a method given the index_map information |
coleenp@6121 | 333 | void Rewriter::scan_method(Method* method, bool reverse, bool* invokespecial_error) { |
duke@435 | 334 | |
duke@435 | 335 | int nof_jsrs = 0; |
duke@435 | 336 | bool has_monitor_bytecodes = false; |
duke@435 | 337 | |
duke@435 | 338 | { |
duke@435 | 339 | // We cannot tolerate a GC in this block, because we've |
coleenp@4037 | 340 | // cached the bytecodes in 'code_base'. If the Method* |
duke@435 | 341 | // moves, the bytecodes will also move. |
duke@435 | 342 | No_Safepoint_Verifier nsv; |
duke@435 | 343 | Bytecodes::Code c; |
duke@435 | 344 | |
duke@435 | 345 | // Bytecodes and their length |
duke@435 | 346 | const address code_base = method->code_base(); |
duke@435 | 347 | const int code_length = method->code_size(); |
duke@435 | 348 | |
duke@435 | 349 | int bc_length; |
duke@435 | 350 | for (int bci = 0; bci < code_length; bci += bc_length) { |
duke@435 | 351 | address bcp = code_base + bci; |
jrose@1161 | 352 | int prefix_length = 0; |
duke@435 | 353 | c = (Bytecodes::Code)(*bcp); |
duke@435 | 354 | |
duke@435 | 355 | // Since we have the code, see if we can get the length |
duke@435 | 356 | // directly. Some more complicated bytecodes will report |
duke@435 | 357 | // a length of zero, meaning we need to make another method |
duke@435 | 358 | // call to calculate the length. |
duke@435 | 359 | bc_length = Bytecodes::length_for(c); |
duke@435 | 360 | if (bc_length == 0) { |
never@2462 | 361 | bc_length = Bytecodes::length_at(method, bcp); |
duke@435 | 362 | |
duke@435 | 363 | // length_at will put us at the bytecode after the one modified |
duke@435 | 364 | // by 'wide'. We don't currently examine any of the bytecodes |
duke@435 | 365 | // modified by wide, but in case we do in the future... |
duke@435 | 366 | if (c == Bytecodes::_wide) { |
jrose@1161 | 367 | prefix_length = 1; |
duke@435 | 368 | c = (Bytecodes::Code)bcp[1]; |
duke@435 | 369 | } |
duke@435 | 370 | } |
duke@435 | 371 | |
duke@435 | 372 | assert(bc_length != 0, "impossible bytecode length"); |
duke@435 | 373 | |
duke@435 | 374 | switch (c) { |
duke@435 | 375 | case Bytecodes::_lookupswitch : { |
duke@435 | 376 | #ifndef CC_INTERP |
never@2462 | 377 | Bytecode_lookupswitch bc(method, bcp); |
jrose@1920 | 378 | (*bcp) = ( |
never@2462 | 379 | bc.number_of_pairs() < BinarySwitchThreshold |
duke@435 | 380 | ? Bytecodes::_fast_linearswitch |
duke@435 | 381 | : Bytecodes::_fast_binaryswitch |
duke@435 | 382 | ); |
duke@435 | 383 | #endif |
duke@435 | 384 | break; |
duke@435 | 385 | } |
coleenp@2945 | 386 | case Bytecodes::_fast_linearswitch: |
coleenp@2945 | 387 | case Bytecodes::_fast_binaryswitch: { |
coleenp@2945 | 388 | #ifndef CC_INTERP |
coleenp@2945 | 389 | (*bcp) = Bytecodes::_lookupswitch; |
coleenp@2945 | 390 | #endif |
coleenp@2945 | 391 | break; |
coleenp@2945 | 392 | } |
coleenp@6081 | 393 | |
coleenp@6081 | 394 | case Bytecodes::_invokespecial : { |
coleenp@6121 | 395 | rewrite_invokespecial(bcp, prefix_length+1, reverse, invokespecial_error); |
coleenp@6081 | 396 | break; |
coleenp@6081 | 397 | } |
coleenp@6081 | 398 | |
zmajo@8664 | 399 | case Bytecodes::_putstatic : |
zmajo@8664 | 400 | case Bytecodes::_putfield : { |
zmajo@8664 | 401 | if (!reverse) { |
zmajo@8664 | 402 | // Check if any final field of the class given as parameter is modified |
zmajo@8664 | 403 | // outside of initializer methods of the class. Fields that are modified |
zmajo@8664 | 404 | // are marked with a flag. For marked fields, the compilers do not perform |
zmajo@8664 | 405 | // constant folding (as the field can be changed after initialization). |
zmajo@8664 | 406 | // |
zmajo@8664 | 407 | // The check is performed after verification and only if verification has |
zmajo@8664 | 408 | // succeeded. Therefore, the class is guaranteed to be well-formed. |
zmajo@8664 | 409 | InstanceKlass* klass = method->method_holder(); |
zmajo@8664 | 410 | u2 bc_index = Bytes::get_Java_u2(bcp + prefix_length + 1); |
zmajo@8664 | 411 | constantPoolHandle cp(method->constants()); |
zmajo@8664 | 412 | Symbol* field_name = cp->name_ref_at(bc_index); |
zmajo@8664 | 413 | Symbol* field_sig = cp->signature_ref_at(bc_index); |
zmajo@8664 | 414 | Symbol* ref_class_name = cp->klass_name_at(cp->klass_ref_index_at(bc_index)); |
zmajo@8664 | 415 | |
zmajo@8664 | 416 | if (klass->name() == ref_class_name) { |
zmajo@8664 | 417 | fieldDescriptor fd; |
zmajo@8664 | 418 | klass->find_field(field_name, field_sig, &fd); |
zmajo@8664 | 419 | if (fd.access_flags().is_final()) { |
zmajo@8664 | 420 | if (fd.access_flags().is_static()) { |
zmajo@8664 | 421 | assert(c == Bytecodes::_putstatic, "must be putstatic"); |
zmajo@8664 | 422 | if (!method->is_static_initializer()) { |
zmajo@8664 | 423 | fd.set_has_initialized_final_update(true); |
zmajo@8664 | 424 | } |
zmajo@8664 | 425 | } else { |
zmajo@8664 | 426 | assert(c == Bytecodes::_putfield, "must be putfield"); |
zmajo@8664 | 427 | if (!method->is_object_initializer()) { |
zmajo@8664 | 428 | fd.set_has_initialized_final_update(true); |
zmajo@8664 | 429 | } |
zmajo@8664 | 430 | } |
zmajo@8664 | 431 | } |
zmajo@8664 | 432 | } |
zmajo@8664 | 433 | } |
zmajo@8664 | 434 | } |
zmajo@8664 | 435 | // fall through |
duke@435 | 436 | case Bytecodes::_getstatic : // fall through |
duke@435 | 437 | case Bytecodes::_getfield : // fall through |
duke@435 | 438 | case Bytecodes::_invokevirtual : // fall through |
jrose@1161 | 439 | case Bytecodes::_invokestatic : |
jrose@1161 | 440 | case Bytecodes::_invokeinterface: |
twisti@3969 | 441 | case Bytecodes::_invokehandle : // if reverse=true |
coleenp@2945 | 442 | rewrite_member_reference(bcp, prefix_length+1, reverse); |
duke@435 | 443 | break; |
jrose@1161 | 444 | case Bytecodes::_invokedynamic: |
coleenp@2945 | 445 | rewrite_invokedynamic(bcp, prefix_length+1, reverse); |
jrose@1161 | 446 | break; |
jrose@1957 | 447 | case Bytecodes::_ldc: |
twisti@3969 | 448 | case Bytecodes::_fast_aldc: // if reverse=true |
coleenp@2945 | 449 | maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse); |
jrose@1957 | 450 | break; |
jrose@1957 | 451 | case Bytecodes::_ldc_w: |
twisti@3969 | 452 | case Bytecodes::_fast_aldc_w: // if reverse=true |
coleenp@2945 | 453 | maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse); |
jrose@1957 | 454 | break; |
duke@435 | 455 | case Bytecodes::_jsr : // fall through |
duke@435 | 456 | case Bytecodes::_jsr_w : nof_jsrs++; break; |
duke@435 | 457 | case Bytecodes::_monitorenter : // fall through |
duke@435 | 458 | case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break; |
duke@435 | 459 | } |
duke@435 | 460 | } |
duke@435 | 461 | } |
duke@435 | 462 | |
duke@435 | 463 | // Update access flags |
duke@435 | 464 | if (has_monitor_bytecodes) { |
duke@435 | 465 | method->set_has_monitor_bytecodes(); |
duke@435 | 466 | } |
duke@435 | 467 | |
duke@435 | 468 | // The present of a jsr bytecode implies that the method might potentially |
duke@435 | 469 | // have to be rewritten, so we run the oopMapGenerator on the method |
duke@435 | 470 | if (nof_jsrs > 0) { |
duke@435 | 471 | method->set_has_jsrs(); |
jrose@1161 | 472 | // Second pass will revisit this method. |
coleenp@2945 | 473 | assert(method->has_jsrs(), "didn't we just set this?"); |
jrose@1161 | 474 | } |
jrose@1161 | 475 | } |
duke@435 | 476 | |
jrose@1161 | 477 | // After constant pool is created, revisit methods containing jsrs. |
jrose@1161 | 478 | methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) { |
coleenp@2945 | 479 | ResourceMark rm(THREAD); |
jrose@1161 | 480 | ResolveOopMapConflicts romc(method); |
jrose@1161 | 481 | methodHandle original_method = method; |
jrose@1161 | 482 | method = romc.do_potential_rewrite(CHECK_(methodHandle())); |
jrose@1161 | 483 | // Update monitor matching info. |
jrose@1161 | 484 | if (romc.monitor_safe()) { |
jrose@1161 | 485 | method->set_guaranteed_monitor_matching(); |
jrose@1161 | 486 | } |
duke@435 | 487 | |
duke@435 | 488 | return method; |
duke@435 | 489 | } |
duke@435 | 490 | |
coleenp@6307 | 491 | void Rewriter::rewrite_bytecodes(TRAPS) { |
jrose@1161 | 492 | assert(_pool->cache() == NULL, "constant pool cache must not be set yet"); |
duke@435 | 493 | |
coleenp@4037 | 494 | // determine index maps for Method* rewriting |
jrose@1161 | 495 | compute_index_maps(); |
duke@435 | 496 | |
jrose@1161 | 497 | if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) { |
jrose@1291 | 498 | bool did_rewrite = false; |
jrose@1161 | 499 | int i = _methods->length(); |
duke@435 | 500 | while (i-- > 0) { |
coleenp@4037 | 501 | Method* method = _methods->at(i); |
duke@435 | 502 | if (method->intrinsic_id() == vmIntrinsics::_Object_init) { |
duke@435 | 503 | // rewrite the return bytecodes of Object.<init> to register the |
duke@435 | 504 | // object for finalization if needed. |
duke@435 | 505 | methodHandle m(THREAD, method); |
duke@435 | 506 | rewrite_Object_init(m, CHECK); |
jrose@1291 | 507 | did_rewrite = true; |
duke@435 | 508 | break; |
duke@435 | 509 | } |
duke@435 | 510 | } |
jrose@1291 | 511 | assert(did_rewrite, "must find Object::<init> to rewrite it"); |
duke@435 | 512 | } |
duke@435 | 513 | |
jrose@1161 | 514 | // rewrite methods, in two passes |
coleenp@2945 | 515 | int len = _methods->length(); |
coleenp@6121 | 516 | bool invokespecial_error = false; |
jrose@1161 | 517 | |
coleenp@2945 | 518 | for (int i = len-1; i >= 0; i--) { |
coleenp@4037 | 519 | Method* method = _methods->at(i); |
coleenp@6121 | 520 | scan_method(method, false, &invokespecial_error); |
coleenp@6121 | 521 | if (invokespecial_error) { |
coleenp@6121 | 522 | // If you get an error here, there is no reversing bytecodes |
coleenp@6121 | 523 | // This exception is stored for this class and no further attempt is |
coleenp@6121 | 524 | // made at verifying or rewriting. |
coleenp@6121 | 525 | THROW_MSG(vmSymbols::java_lang_InternalError(), |
coleenp@6121 | 526 | "This classfile overflows invokespecial for interfaces " |
coleenp@6121 | 527 | "and cannot be loaded"); |
coleenp@6121 | 528 | return; |
coleenp@6121 | 529 | } |
jrose@1161 | 530 | } |
jrose@1161 | 531 | |
coleenp@6081 | 532 | // May have to fix invokedynamic bytecodes if invokestatic/InterfaceMethodref |
coleenp@6081 | 533 | // entries had to be added. |
coleenp@6081 | 534 | patch_invokedynamic_bytecodes(); |
coleenp@6307 | 535 | } |
coleenp@6307 | 536 | |
coleenp@6307 | 537 | void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) { |
coleenp@6307 | 538 | ResourceMark rm(THREAD); |
coleenp@6307 | 539 | Rewriter rw(klass, klass->constants(), klass->methods(), CHECK); |
coleenp@6307 | 540 | // (That's all, folks.) |
coleenp@6307 | 541 | } |
coleenp@6307 | 542 | |
coleenp@6307 | 543 | |
coleenp@6307 | 544 | Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS) |
coleenp@6307 | 545 | : _klass(klass), |
coleenp@6307 | 546 | _pool(cpool), |
coleenp@6307 | 547 | _methods(methods) |
coleenp@6307 | 548 | { |
coleenp@6307 | 549 | |
coleenp@6307 | 550 | // Rewrite bytecodes - exception here exits. |
coleenp@6307 | 551 | rewrite_bytecodes(CHECK); |
coleenp@6307 | 552 | |
coleenp@6307 | 553 | // Stress restoring bytecodes |
coleenp@6307 | 554 | if (StressRewriter) { |
coleenp@6307 | 555 | restore_bytecodes(); |
coleenp@6307 | 556 | rewrite_bytecodes(CHECK); |
coleenp@6307 | 557 | } |
coleenp@6081 | 558 | |
jrose@1161 | 559 | // allocate constant pool cache, now that we've seen all the bytecodes |
coleenp@2945 | 560 | make_constant_pool_cache(THREAD); |
jrose@1161 | 561 | |
coleenp@2945 | 562 | // Restore bytecodes to their unrewritten state if there are exceptions |
coleenp@2945 | 563 | // rewriting bytecodes or allocating the cpCache |
coleenp@2945 | 564 | if (HAS_PENDING_EXCEPTION) { |
coleenp@6121 | 565 | restore_bytecodes(); |
coleenp@2945 | 566 | return; |
coleenp@2945 | 567 | } |
coleenp@2945 | 568 | |
coleenp@4395 | 569 | // Relocate after everything, but still do this under the is_rewritten flag, |
coleenp@4395 | 570 | // so methods with jsrs in custom class lists in aren't attempted to be |
coleenp@4395 | 571 | // rewritten in the RO section of the shared archive. |
coleenp@4395 | 572 | // Relocated bytecodes don't have to be restored, only the cp cache entries |
coleenp@6307 | 573 | int len = _methods->length(); |
coleenp@2945 | 574 | for (int i = len-1; i >= 0; i--) { |
coleenp@4395 | 575 | methodHandle m(THREAD, _methods->at(i)); |
jrose@1161 | 576 | |
jrose@1161 | 577 | if (m->has_jsrs()) { |
coleenp@4395 | 578 | m = rewrite_jsrs(m, THREAD); |
coleenp@4395 | 579 | // Restore bytecodes to their unrewritten state if there are exceptions |
coleenp@4395 | 580 | // relocating bytecodes. If some are relocated, that is ok because that |
coleenp@4395 | 581 | // doesn't affect constant pool to cpCache rewriting. |
coleenp@4395 | 582 | if (HAS_PENDING_EXCEPTION) { |
coleenp@6121 | 583 | restore_bytecodes(); |
coleenp@4395 | 584 | return; |
coleenp@4395 | 585 | } |
duke@435 | 586 | // Method might have gotten rewritten. |
coleenp@4037 | 587 | methods->at_put(i, m()); |
duke@435 | 588 | } |
duke@435 | 589 | } |
duke@435 | 590 | } |