Wed, 03 Jul 2013 11:50:29 -0700
Merge
1 /*
2 * Copyright (c) 1998, 2013, 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 */
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/resourceArea.hpp"
31 #include "oops/generateOopMap.hpp"
32 #include "prims/methodHandles.hpp"
34 // Computes a CPC map (new_index -> original_index) for constant pool entries
35 // that are referred to by the interpreter at runtime via the constant pool cache.
36 // Also computes a CP map (original_index -> new_index).
37 // Marks entries in CP which require additional processing.
38 void Rewriter::compute_index_maps() {
39 const int length = _pool->length();
40 init_maps(length);
41 bool saw_mh_symbol = false;
42 for (int i = 0; i < length; i++) {
43 int tag = _pool->tag_at(i).value();
44 switch (tag) {
45 case JVM_CONSTANT_InterfaceMethodref:
46 case JVM_CONSTANT_Fieldref : // fall through
47 case JVM_CONSTANT_Methodref : // fall through
48 add_cp_cache_entry(i);
49 break;
50 case JVM_CONSTANT_String:
51 case JVM_CONSTANT_MethodHandle : // fall through
52 case JVM_CONSTANT_MethodType : // fall through
53 add_resolved_references_entry(i);
54 break;
55 case JVM_CONSTANT_Utf8:
56 if (_pool->symbol_at(i) == vmSymbols::java_lang_invoke_MethodHandle())
57 saw_mh_symbol = true;
58 break;
59 }
60 }
62 // Record limits of resolved reference map for constant pool cache indices
63 record_map_limits();
65 guarantee((int)_cp_cache_map.length()-1 <= (int)((u2)-1),
66 "all cp cache indexes fit in a u2");
68 if (saw_mh_symbol)
69 _method_handle_invokers.initialize(length, (int)0);
70 }
72 // Unrewrite the bytecodes if an error occurs.
73 void Rewriter::restore_bytecodes() {
74 int len = _methods->length();
76 for (int i = len-1; i >= 0; i--) {
77 Method* method = _methods->at(i);
78 scan_method(method, true);
79 }
80 }
82 // Creates a constant pool cache given a CPC map
83 void Rewriter::make_constant_pool_cache(TRAPS) {
84 const int length = _cp_cache_map.length();
85 ClassLoaderData* loader_data = _pool->pool_holder()->class_loader_data();
86 ConstantPoolCache* cache =
87 ConstantPoolCache::allocate(loader_data, length, _cp_cache_map,
88 _invokedynamic_references_map, CHECK);
90 // initialize object cache in constant pool
91 _pool->initialize_resolved_references(loader_data, _resolved_references_map,
92 _resolved_reference_limit,
93 CHECK);
94 _pool->set_cache(cache);
95 cache->set_constant_pool(_pool());
96 }
100 // The new finalization semantics says that registration of
101 // finalizable objects must be performed on successful return from the
102 // Object.<init> constructor. We could implement this trivially if
103 // <init> were never rewritten but since JVMTI allows this to occur, a
104 // more complicated solution is required. A special return bytecode
105 // is used only by Object.<init> to signal the finalization
106 // registration point. Additionally local 0 must be preserved so it's
107 // available to pass to the registration function. For simplicty we
108 // require that local 0 is never overwritten so it's available as an
109 // argument for registration.
111 void Rewriter::rewrite_Object_init(methodHandle method, TRAPS) {
112 RawBytecodeStream bcs(method);
113 while (!bcs.is_last_bytecode()) {
114 Bytecodes::Code opcode = bcs.raw_next();
115 switch (opcode) {
116 case Bytecodes::_return: *bcs.bcp() = Bytecodes::_return_register_finalizer; break;
118 case Bytecodes::_istore:
119 case Bytecodes::_lstore:
120 case Bytecodes::_fstore:
121 case Bytecodes::_dstore:
122 case Bytecodes::_astore:
123 if (bcs.get_index() != 0) continue;
125 // fall through
126 case Bytecodes::_istore_0:
127 case Bytecodes::_lstore_0:
128 case Bytecodes::_fstore_0:
129 case Bytecodes::_dstore_0:
130 case Bytecodes::_astore_0:
131 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
132 "can't overwrite local 0 in Object.<init>");
133 break;
134 }
135 }
136 }
139 // Rewrite a classfile-order CP index into a native-order CPC index.
140 void Rewriter::rewrite_member_reference(address bcp, int offset, bool reverse) {
141 address p = bcp + offset;
142 if (!reverse) {
143 int cp_index = Bytes::get_Java_u2(p);
144 int cache_index = cp_entry_to_cp_cache(cp_index);
145 Bytes::put_native_u2(p, cache_index);
146 if (!_method_handle_invokers.is_empty())
147 maybe_rewrite_invokehandle(p - 1, cp_index, cache_index, reverse);
148 } else {
149 int cache_index = Bytes::get_native_u2(p);
150 int pool_index = cp_cache_entry_pool_index(cache_index);
151 Bytes::put_Java_u2(p, pool_index);
152 if (!_method_handle_invokers.is_empty())
153 maybe_rewrite_invokehandle(p - 1, pool_index, cache_index, reverse);
154 }
155 }
158 // Adjust the invocation bytecode for a signature-polymorphic method (MethodHandle.invoke, etc.)
159 void Rewriter::maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse) {
160 if (!reverse) {
161 if ((*opc) == (u1)Bytecodes::_invokevirtual ||
162 // allow invokespecial as an alias, although it would be very odd:
163 (*opc) == (u1)Bytecodes::_invokespecial) {
164 assert(_pool->tag_at(cp_index).is_method(), "wrong index");
165 // Determine whether this is a signature-polymorphic method.
166 if (cp_index >= _method_handle_invokers.length()) return;
167 int status = _method_handle_invokers[cp_index];
168 assert(status >= -1 && status <= 1, "oob tri-state");
169 if (status == 0) {
170 if (_pool->klass_ref_at_noresolve(cp_index) == vmSymbols::java_lang_invoke_MethodHandle() &&
171 MethodHandles::is_signature_polymorphic_name(SystemDictionary::MethodHandle_klass(),
172 _pool->name_ref_at(cp_index))) {
173 // we may need a resolved_refs entry for the appendix
174 add_invokedynamic_resolved_references_entries(cp_index, cache_index);
175 status = +1;
176 } else {
177 status = -1;
178 }
179 _method_handle_invokers[cp_index] = status;
180 }
181 // We use a special internal bytecode for such methods (if non-static).
182 // The basic reason for this is that such methods need an extra "appendix" argument
183 // to transmit the call site's intended call type.
184 if (status > 0) {
185 (*opc) = (u1)Bytecodes::_invokehandle;
186 }
187 }
188 } else {
189 // Do not need to look at cp_index.
190 if ((*opc) == (u1)Bytecodes::_invokehandle) {
191 (*opc) = (u1)Bytecodes::_invokevirtual;
192 // Ignore corner case of original _invokespecial instruction.
193 // This is safe because (a) the signature polymorphic method was final, and
194 // (b) the implementation of MethodHandle will not call invokespecial on it.
195 }
196 }
197 }
200 void Rewriter::rewrite_invokedynamic(address bcp, int offset, bool reverse) {
201 address p = bcp + offset;
202 assert(p[-1] == Bytecodes::_invokedynamic, "not invokedynamic bytecode");
203 if (!reverse) {
204 int cp_index = Bytes::get_Java_u2(p);
205 int cache_index = add_invokedynamic_cp_cache_entry(cp_index);
206 add_invokedynamic_resolved_references_entries(cp_index, cache_index);
207 // Replace the trailing four bytes with a CPC index for the dynamic
208 // call site. Unlike other CPC entries, there is one per bytecode,
209 // not just one per distinct CP entry. In other words, the
210 // CPC-to-CP relation is many-to-one for invokedynamic entries.
211 // This means we must use a larger index size than u2 to address
212 // all these entries. That is the main reason invokedynamic
213 // must have a five-byte instruction format. (Of course, other JVM
214 // implementations can use the bytes for other purposes.)
215 Bytes::put_native_u4(p, ConstantPool::encode_invokedynamic_index(cache_index));
216 // Note: We use native_u4 format exclusively for 4-byte indexes.
217 } else {
218 // callsite index
219 int cache_index = ConstantPool::decode_invokedynamic_index(
220 Bytes::get_native_u4(p));
221 int cp_index = cp_cache_entry_pool_index(cache_index);
222 assert(_pool->tag_at(cp_index).is_invoke_dynamic(), "wrong index");
223 // zero out 4 bytes
224 Bytes::put_Java_u4(p, 0);
225 Bytes::put_Java_u2(p, cp_index);
226 }
227 }
230 // Rewrite some ldc bytecodes to _fast_aldc
231 void Rewriter::maybe_rewrite_ldc(address bcp, int offset, bool is_wide,
232 bool reverse) {
233 if (!reverse) {
234 assert((*bcp) == (is_wide ? Bytecodes::_ldc_w : Bytecodes::_ldc), "not ldc bytecode");
235 address p = bcp + offset;
236 int cp_index = is_wide ? Bytes::get_Java_u2(p) : (u1)(*p);
237 constantTag tag = _pool->tag_at(cp_index).value();
238 if (tag.is_method_handle() || tag.is_method_type() || tag.is_string()) {
239 int ref_index = cp_entry_to_resolved_references(cp_index);
240 if (is_wide) {
241 (*bcp) = Bytecodes::_fast_aldc_w;
242 assert(ref_index == (u2)ref_index, "index overflow");
243 Bytes::put_native_u2(p, ref_index);
244 } else {
245 (*bcp) = Bytecodes::_fast_aldc;
246 assert(ref_index == (u1)ref_index, "index overflow");
247 (*p) = (u1)ref_index;
248 }
249 }
250 } else {
251 Bytecodes::Code rewritten_bc =
252 (is_wide ? Bytecodes::_fast_aldc_w : Bytecodes::_fast_aldc);
253 if ((*bcp) == rewritten_bc) {
254 address p = bcp + offset;
255 int ref_index = is_wide ? Bytes::get_native_u2(p) : (u1)(*p);
256 int pool_index = resolved_references_entry_to_pool_index(ref_index);
257 if (is_wide) {
258 (*bcp) = Bytecodes::_ldc_w;
259 assert(pool_index == (u2)pool_index, "index overflow");
260 Bytes::put_Java_u2(p, pool_index);
261 } else {
262 (*bcp) = Bytecodes::_ldc;
263 assert(pool_index == (u1)pool_index, "index overflow");
264 (*p) = (u1)pool_index;
265 }
266 }
267 }
268 }
271 // Rewrites a method given the index_map information
272 void Rewriter::scan_method(Method* method, bool reverse) {
274 int nof_jsrs = 0;
275 bool has_monitor_bytecodes = false;
277 {
278 // We cannot tolerate a GC in this block, because we've
279 // cached the bytecodes in 'code_base'. If the Method*
280 // moves, the bytecodes will also move.
281 No_Safepoint_Verifier nsv;
282 Bytecodes::Code c;
284 // Bytecodes and their length
285 const address code_base = method->code_base();
286 const int code_length = method->code_size();
288 int bc_length;
289 for (int bci = 0; bci < code_length; bci += bc_length) {
290 address bcp = code_base + bci;
291 int prefix_length = 0;
292 c = (Bytecodes::Code)(*bcp);
294 // Since we have the code, see if we can get the length
295 // directly. Some more complicated bytecodes will report
296 // a length of zero, meaning we need to make another method
297 // call to calculate the length.
298 bc_length = Bytecodes::length_for(c);
299 if (bc_length == 0) {
300 bc_length = Bytecodes::length_at(method, bcp);
302 // length_at will put us at the bytecode after the one modified
303 // by 'wide'. We don't currently examine any of the bytecodes
304 // modified by wide, but in case we do in the future...
305 if (c == Bytecodes::_wide) {
306 prefix_length = 1;
307 c = (Bytecodes::Code)bcp[1];
308 }
309 }
311 assert(bc_length != 0, "impossible bytecode length");
313 switch (c) {
314 case Bytecodes::_lookupswitch : {
315 #ifndef CC_INTERP
316 Bytecode_lookupswitch bc(method, bcp);
317 (*bcp) = (
318 bc.number_of_pairs() < BinarySwitchThreshold
319 ? Bytecodes::_fast_linearswitch
320 : Bytecodes::_fast_binaryswitch
321 );
322 #endif
323 break;
324 }
325 case Bytecodes::_fast_linearswitch:
326 case Bytecodes::_fast_binaryswitch: {
327 #ifndef CC_INTERP
328 (*bcp) = Bytecodes::_lookupswitch;
329 #endif
330 break;
331 }
332 case Bytecodes::_getstatic : // fall through
333 case Bytecodes::_putstatic : // fall through
334 case Bytecodes::_getfield : // fall through
335 case Bytecodes::_putfield : // fall through
336 case Bytecodes::_invokevirtual : // fall through
337 case Bytecodes::_invokespecial : // fall through
338 case Bytecodes::_invokestatic :
339 case Bytecodes::_invokeinterface:
340 case Bytecodes::_invokehandle : // if reverse=true
341 rewrite_member_reference(bcp, prefix_length+1, reverse);
342 break;
343 case Bytecodes::_invokedynamic:
344 rewrite_invokedynamic(bcp, prefix_length+1, reverse);
345 break;
346 case Bytecodes::_ldc:
347 case Bytecodes::_fast_aldc: // if reverse=true
348 maybe_rewrite_ldc(bcp, prefix_length+1, false, reverse);
349 break;
350 case Bytecodes::_ldc_w:
351 case Bytecodes::_fast_aldc_w: // if reverse=true
352 maybe_rewrite_ldc(bcp, prefix_length+1, true, reverse);
353 break;
354 case Bytecodes::_jsr : // fall through
355 case Bytecodes::_jsr_w : nof_jsrs++; break;
356 case Bytecodes::_monitorenter : // fall through
357 case Bytecodes::_monitorexit : has_monitor_bytecodes = true; break;
358 }
359 }
360 }
362 // Update access flags
363 if (has_monitor_bytecodes) {
364 method->set_has_monitor_bytecodes();
365 }
367 // The present of a jsr bytecode implies that the method might potentially
368 // have to be rewritten, so we run the oopMapGenerator on the method
369 if (nof_jsrs > 0) {
370 method->set_has_jsrs();
371 // Second pass will revisit this method.
372 assert(method->has_jsrs(), "didn't we just set this?");
373 }
374 }
376 // After constant pool is created, revisit methods containing jsrs.
377 methodHandle Rewriter::rewrite_jsrs(methodHandle method, TRAPS) {
378 ResourceMark rm(THREAD);
379 ResolveOopMapConflicts romc(method);
380 methodHandle original_method = method;
381 method = romc.do_potential_rewrite(CHECK_(methodHandle()));
382 // Update monitor matching info.
383 if (romc.monitor_safe()) {
384 method->set_guaranteed_monitor_matching();
385 }
387 return method;
388 }
390 void Rewriter::rewrite(instanceKlassHandle klass, TRAPS) {
391 ResourceMark rm(THREAD);
392 Rewriter rw(klass, klass->constants(), klass->methods(), CHECK);
393 // (That's all, folks.)
394 }
397 Rewriter::Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS)
398 : _klass(klass),
399 _pool(cpool),
400 _methods(methods)
401 {
402 assert(_pool->cache() == NULL, "constant pool cache must not be set yet");
404 // determine index maps for Method* rewriting
405 compute_index_maps();
407 if (RegisterFinalizersAtInit && _klass->name() == vmSymbols::java_lang_Object()) {
408 bool did_rewrite = false;
409 int i = _methods->length();
410 while (i-- > 0) {
411 Method* method = _methods->at(i);
412 if (method->intrinsic_id() == vmIntrinsics::_Object_init) {
413 // rewrite the return bytecodes of Object.<init> to register the
414 // object for finalization if needed.
415 methodHandle m(THREAD, method);
416 rewrite_Object_init(m, CHECK);
417 did_rewrite = true;
418 break;
419 }
420 }
421 assert(did_rewrite, "must find Object::<init> to rewrite it");
422 }
424 // rewrite methods, in two passes
425 int len = _methods->length();
427 for (int i = len-1; i >= 0; i--) {
428 Method* method = _methods->at(i);
429 scan_method(method);
430 }
432 // allocate constant pool cache, now that we've seen all the bytecodes
433 make_constant_pool_cache(THREAD);
435 // Restore bytecodes to their unrewritten state if there are exceptions
436 // rewriting bytecodes or allocating the cpCache
437 if (HAS_PENDING_EXCEPTION) {
438 restore_bytecodes();
439 return;
440 }
442 // Relocate after everything, but still do this under the is_rewritten flag,
443 // so methods with jsrs in custom class lists in aren't attempted to be
444 // rewritten in the RO section of the shared archive.
445 // Relocated bytecodes don't have to be restored, only the cp cache entries
446 for (int i = len-1; i >= 0; i--) {
447 methodHandle m(THREAD, _methods->at(i));
449 if (m->has_jsrs()) {
450 m = rewrite_jsrs(m, THREAD);
451 // Restore bytecodes to their unrewritten state if there are exceptions
452 // relocating bytecodes. If some are relocated, that is ok because that
453 // doesn't affect constant pool to cpCache rewriting.
454 if (HAS_PENDING_EXCEPTION) {
455 restore_bytecodes();
456 return;
457 }
458 // Method might have gotten rewritten.
459 methods->at_put(i, m());
460 }
461 }
462 }