Mon, 12 Aug 2019 18:30:40 +0300
8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens
1 /*
2 * Copyright (c) 1997, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "classfile/metadataOnStackMark.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "interpreter/linkResolver.hpp"
33 #include "memory/heapInspection.hpp"
34 #include "memory/metadataFactory.hpp"
35 #include "memory/oopFactory.hpp"
36 #include "oops/constantPool.hpp"
37 #include "oops/instanceKlass.hpp"
38 #include "oops/objArrayKlass.hpp"
39 #include "runtime/fieldType.hpp"
40 #include "runtime/init.hpp"
41 #include "runtime/javaCalls.hpp"
42 #include "runtime/signature.hpp"
43 #include "runtime/vframe.hpp"
45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
47 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
48 // Tags are RW but comment below applies to tags also.
49 Array<u1>* tags = MetadataFactory::new_writeable_array<u1>(loader_data, length, 0, CHECK_NULL);
51 int size = ConstantPool::size(length);
53 // CDS considerations:
54 // Allocate read-write but may be able to move to read-only at dumping time
55 // if all the klasses are resolved. The only other field that is writable is
56 // the resolved_references array, which is recreated at startup time.
57 // But that could be moved to InstanceKlass (although a pain to access from
58 // assembly code). Maybe it could be moved to the cpCache which is RW.
59 return new (loader_data, size, false, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
60 }
62 ConstantPool::ConstantPool(Array<u1>* tags) {
63 set_length(tags->length());
64 set_tags(NULL);
65 set_cache(NULL);
66 set_reference_map(NULL);
67 set_resolved_references(NULL);
68 set_operands(NULL);
69 set_pool_holder(NULL);
70 set_flags(0);
72 // only set to non-zero if constant pool is merged by RedefineClasses
73 set_version(0);
74 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
76 // initialize tag array
77 int length = tags->length();
78 for (int index = 0; index < length; index++) {
79 tags->at_put(index, JVM_CONSTANT_Invalid);
80 }
81 set_tags(tags);
82 }
84 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
85 MetadataFactory::free_metadata(loader_data, cache());
86 set_cache(NULL);
87 MetadataFactory::free_array<u2>(loader_data, reference_map());
88 set_reference_map(NULL);
90 MetadataFactory::free_array<jushort>(loader_data, operands());
91 set_operands(NULL);
93 release_C_heap_structures();
95 // free tag array
96 MetadataFactory::free_array<u1>(loader_data, tags());
97 set_tags(NULL);
98 }
100 void ConstantPool::release_C_heap_structures() {
101 // walk constant pool and decrement symbol reference counts
102 unreference_symbols();
104 delete _lock;
105 set_lock(NULL);
106 }
108 objArrayOop ConstantPool::resolved_references() const {
109 return (objArrayOop)JNIHandles::resolve(_resolved_references);
110 }
112 // Called from outside constant pool resolution where a resolved_reference array
113 // may not be present.
114 objArrayOop ConstantPool::resolved_references_or_null() const {
115 if (_cache == NULL) {
116 return NULL;
117 } else {
118 return (objArrayOop)JNIHandles::resolve(_resolved_references);
119 }
120 }
122 // Create resolved_references array and mapping array for original cp indexes
123 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
124 // to map it back for resolving and some unlikely miscellaneous uses.
125 // The objects created by invokedynamic are appended to this list.
126 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
127 intStack reference_map,
128 int constant_pool_map_length,
129 TRAPS) {
130 // Initialized the resolved object cache.
131 int map_length = reference_map.length();
132 if (map_length > 0) {
133 // Only need mapping back to constant pool entries. The map isn't used for
134 // invokedynamic resolved_reference entries. For invokedynamic entries,
135 // the constant pool cache index has the mapping back to both the constant
136 // pool and to the resolved reference index.
137 if (constant_pool_map_length > 0) {
138 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
140 for (int i = 0; i < constant_pool_map_length; i++) {
141 int x = reference_map.at(i);
142 assert(x == (int)(jushort) x, "klass index is too big");
143 om->at_put(i, (jushort)x);
144 }
145 set_reference_map(om);
146 }
148 // Create Java array for holding resolved strings, methodHandles,
149 // methodTypes, invokedynamic and invokehandle appendix objects, etc.
150 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
151 Handle refs_handle (THREAD, (oop)stom); // must handleize.
152 set_resolved_references(loader_data->add_handle(refs_handle));
153 }
154 }
156 // CDS support. Create a new resolved_references array.
157 void ConstantPool::restore_unshareable_info(TRAPS) {
159 // Only create the new resolved references array and lock if it hasn't been
160 // attempted before
161 if (resolved_references() != NULL) return;
163 // restore the C++ vtable from the shared archive
164 restore_vtable();
166 if (SystemDictionary::Object_klass_loaded()) {
167 // Recreate the object array and add to ClassLoaderData.
168 int map_length = resolved_reference_length();
169 if (map_length > 0) {
170 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
171 Handle refs_handle (THREAD, (oop)stom); // must handleize.
173 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
174 set_resolved_references(loader_data->add_handle(refs_handle));
175 }
177 // Also need to recreate the mutex. Make sure this matches the constructor
178 set_lock(new Monitor(Monitor::nonleaf + 2, "A constant pool lock"));
179 }
180 }
182 void ConstantPool::remove_unshareable_info() {
183 // Resolved references are not in the shared archive.
184 // Save the length for restoration. It is not necessarily the same length
185 // as reference_map.length() if invokedynamic is saved.
186 set_resolved_reference_length(
187 resolved_references() != NULL ? resolved_references()->length() : 0);
188 set_resolved_references(NULL);
189 set_lock(NULL);
190 }
192 int ConstantPool::cp_to_object_index(int cp_index) {
193 // this is harder don't do this so much.
194 int i = reference_map()->find(cp_index);
195 // We might not find the index for jsr292 call.
196 return (i < 0) ? _no_index_sentinel : i;
197 }
199 Klass* ConstantPool::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
200 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
201 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
202 // tag is not updated atomicly.
204 CPSlot entry = this_oop->slot_at(which);
205 if (entry.is_resolved()) {
206 assert(entry.get_klass()->is_klass(), "must be");
207 // Already resolved - return entry.
208 return entry.get_klass();
209 }
211 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
212 // already has updated the object
213 assert(THREAD->is_Java_thread(), "must be a Java thread");
214 bool do_resolve = false;
215 bool in_error = false;
217 // Create a handle for the mirror. This will preserve the resolved class
218 // until the loader_data is registered.
219 Handle mirror_handle;
221 Symbol* name = NULL;
222 Handle loader;
223 { MonitorLockerEx ml(this_oop->lock());
225 if (this_oop->tag_at(which).is_unresolved_klass()) {
226 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
227 in_error = true;
228 } else {
229 do_resolve = true;
230 name = this_oop->unresolved_klass_at(which);
231 loader = Handle(THREAD, this_oop->pool_holder()->class_loader());
232 }
233 }
234 } // unlocking constantPool
237 // The original attempt to resolve this constant pool entry failed so find the
238 // original error and throw it again (JVMS 5.4.3).
239 if (in_error) {
240 Symbol* error = SystemDictionary::find_resolution_error(this_oop, which);
241 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
242 ResourceMark rm;
243 // exception text will be the class name
244 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
245 THROW_MSG_0(error, className);
246 }
248 if (do_resolve) {
249 // this_oop must be unlocked during resolve_or_fail
250 oop protection_domain = this_oop->pool_holder()->protection_domain();
251 Handle h_prot (THREAD, protection_domain);
252 Klass* k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
253 KlassHandle k;
254 if (!HAS_PENDING_EXCEPTION) {
255 k = KlassHandle(THREAD, k_oop);
256 // preserve the resolved klass.
257 mirror_handle = Handle(THREAD, k_oop->java_mirror());
258 // Do access check for klasses
259 verify_constant_pool_resolve(this_oop, k, THREAD);
260 }
262 // Failed to resolve class. We must record the errors so that subsequent attempts
263 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
264 if (HAS_PENDING_EXCEPTION) {
265 ResourceMark rm;
266 Symbol* error = PENDING_EXCEPTION->klass()->name();
268 bool throw_orig_error = false;
269 {
270 MonitorLockerEx ml(this_oop->lock());
272 // some other thread has beaten us and has resolved the class.
273 if (this_oop->tag_at(which).is_klass()) {
274 CLEAR_PENDING_EXCEPTION;
275 entry = this_oop->resolved_klass_at(which);
276 return entry.get_klass();
277 }
279 if (!PENDING_EXCEPTION->
280 is_a(SystemDictionary::LinkageError_klass())) {
281 // Just throw the exception and don't prevent these classes from
282 // being loaded due to virtual machine errors like StackOverflow
283 // and OutOfMemoryError, etc, or if the thread was hit by stop()
284 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
285 }
286 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
287 SystemDictionary::add_resolution_error(this_oop, which, error);
288 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
289 } else {
290 // some other thread has put the class in error state.
291 error = SystemDictionary::find_resolution_error(this_oop, which);
292 assert(error != NULL, "checking");
293 throw_orig_error = true;
294 }
295 } // unlocked
297 if (throw_orig_error) {
298 CLEAR_PENDING_EXCEPTION;
299 ResourceMark rm;
300 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
301 THROW_MSG_0(error, className);
302 }
304 return 0;
305 }
307 if (TraceClassResolution && !k()->oop_is_array()) {
308 // skip resolving the constant pool so that this code get's
309 // called the next time some bytecodes refer to this class.
310 ResourceMark rm;
311 int line_number = -1;
312 const char * source_file = NULL;
313 if (JavaThread::current()->has_last_Java_frame()) {
314 // try to identify the method which called this function.
315 vframeStream vfst(JavaThread::current());
316 if (!vfst.at_end()) {
317 line_number = vfst.method()->line_number_from_bci(vfst.bci());
318 Symbol* s = vfst.method()->method_holder()->source_file_name();
319 if (s != NULL) {
320 source_file = s->as_C_string();
321 }
322 }
323 }
324 if (k() != this_oop->pool_holder()) {
325 // only print something if the classes are different
326 if (source_file != NULL) {
327 tty->print("RESOLVE %s %s %s:%d\n",
328 this_oop->pool_holder()->external_name(),
329 InstanceKlass::cast(k())->external_name(), source_file, line_number);
330 } else {
331 tty->print("RESOLVE %s %s\n",
332 this_oop->pool_holder()->external_name(),
333 InstanceKlass::cast(k())->external_name());
334 }
335 }
336 return k();
337 } else {
338 MonitorLockerEx ml(this_oop->lock());
339 // Only updated constant pool - if it is resolved.
340 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
341 if (do_resolve) {
342 this_oop->klass_at_put(which, k());
343 }
344 }
345 }
347 entry = this_oop->resolved_klass_at(which);
348 assert(entry.is_resolved() && entry.get_klass()->is_klass(), "must be resolved at this point");
349 return entry.get_klass();
350 }
353 // Does not update ConstantPool* - to avoid any exception throwing. Used
354 // by compiler and exception handling. Also used to avoid classloads for
355 // instanceof operations. Returns NULL if the class has not been loaded or
356 // if the verification of constant pool failed
357 Klass* ConstantPool::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
358 CPSlot entry = this_oop->slot_at(which);
359 if (entry.is_resolved()) {
360 assert(entry.get_klass()->is_klass(), "must be");
361 return entry.get_klass();
362 } else {
363 assert(entry.is_unresolved(), "must be either symbol or klass");
364 Thread *thread = Thread::current();
365 Symbol* name = entry.get_symbol();
366 oop loader = this_oop->pool_holder()->class_loader();
367 oop protection_domain = this_oop->pool_holder()->protection_domain();
368 Handle h_prot (thread, protection_domain);
369 Handle h_loader (thread, loader);
370 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
372 if (k != NULL) {
373 // Make sure that resolving is legal
374 EXCEPTION_MARK;
375 KlassHandle klass(THREAD, k);
376 // return NULL if verification fails
377 verify_constant_pool_resolve(this_oop, klass, THREAD);
378 if (HAS_PENDING_EXCEPTION) {
379 CLEAR_PENDING_EXCEPTION;
380 return NULL;
381 }
382 return klass();
383 } else {
384 return k;
385 }
386 }
387 }
390 Klass* ConstantPool::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
391 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
392 }
395 Method* ConstantPool::method_at_if_loaded(constantPoolHandle cpool,
396 int which) {
397 if (cpool->cache() == NULL) return NULL; // nothing to load yet
398 int cache_index = decode_cpcache_index(which, true);
399 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
400 // FIXME: should be an assert
401 if (PrintMiscellaneous && (Verbose||WizardMode)) {
402 tty->print_cr("bad operand %d in:", which); cpool->print();
403 }
404 return NULL;
405 }
406 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
407 return e->method_if_resolved(cpool);
408 }
411 bool ConstantPool::has_appendix_at_if_loaded(constantPoolHandle cpool, int which) {
412 if (cpool->cache() == NULL) return false; // nothing to load yet
413 int cache_index = decode_cpcache_index(which, true);
414 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
415 return e->has_appendix();
416 }
418 oop ConstantPool::appendix_at_if_loaded(constantPoolHandle cpool, int which) {
419 if (cpool->cache() == NULL) return NULL; // nothing to load yet
420 int cache_index = decode_cpcache_index(which, true);
421 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
422 return e->appendix_if_resolved(cpool);
423 }
426 bool ConstantPool::has_method_type_at_if_loaded(constantPoolHandle cpool, int which) {
427 if (cpool->cache() == NULL) return false; // nothing to load yet
428 int cache_index = decode_cpcache_index(which, true);
429 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
430 return e->has_method_type();
431 }
433 oop ConstantPool::method_type_at_if_loaded(constantPoolHandle cpool, int which) {
434 if (cpool->cache() == NULL) return NULL; // nothing to load yet
435 int cache_index = decode_cpcache_index(which, true);
436 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
437 return e->method_type_if_resolved(cpool);
438 }
441 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
442 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
443 return symbol_at(name_index);
444 }
447 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
448 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
449 return symbol_at(signature_index);
450 }
453 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
454 int i = which;
455 if (!uncached && cache() != NULL) {
456 if (ConstantPool::is_invokedynamic_index(which)) {
457 // Invokedynamic index is index into resolved_references
458 int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
459 pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
460 assert(tag_at(pool_index).is_name_and_type(), "");
461 return pool_index;
462 }
463 // change byte-ordering and go via cache
464 i = remap_instruction_operand_from_cache(which);
465 } else {
466 if (tag_at(which).is_invoke_dynamic()) {
467 int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
468 assert(tag_at(pool_index).is_name_and_type(), "");
469 return pool_index;
470 }
471 }
472 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
473 assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
474 jint ref_index = *int_at_addr(i);
475 return extract_high_short_from_int(ref_index);
476 }
479 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
480 guarantee(!ConstantPool::is_invokedynamic_index(which),
481 "an invokedynamic instruction does not have a klass");
482 int i = which;
483 if (!uncached && cache() != NULL) {
484 // change byte-ordering and go via cache
485 i = remap_instruction_operand_from_cache(which);
486 }
487 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
488 jint ref_index = *int_at_addr(i);
489 return extract_low_short_from_int(ref_index);
490 }
494 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
495 int cpc_index = operand;
496 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
497 assert((int)(u2)cpc_index == cpc_index, "clean u2");
498 int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
499 return member_index;
500 }
503 void ConstantPool::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
504 if (k->oop_is_instance() || k->oop_is_objArray()) {
505 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
506 Klass* elem_oop = k->oop_is_instance() ? k() : ObjArrayKlass::cast(k())->bottom_klass();
507 KlassHandle element (THREAD, elem_oop);
509 // The element type could be a typeArray - we only need the access check if it is
510 // an reference to another class
511 if (element->oop_is_instance()) {
512 LinkResolver::check_klass_accessability(holder, element, CHECK);
513 }
514 }
515 }
518 int ConstantPool::name_ref_index_at(int which_nt) {
519 jint ref_index = name_and_type_at(which_nt);
520 return extract_low_short_from_int(ref_index);
521 }
524 int ConstantPool::signature_ref_index_at(int which_nt) {
525 jint ref_index = name_and_type_at(which_nt);
526 return extract_high_short_from_int(ref_index);
527 }
530 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
531 return klass_at(klass_ref_index_at(which), THREAD);
532 }
535 Symbol* ConstantPool::klass_name_at(int which) {
536 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
537 "Corrupted constant pool");
538 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
539 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
540 // tag is not updated atomicly.
541 CPSlot entry = slot_at(which);
542 if (entry.is_resolved()) {
543 // Already resolved - return entry's name.
544 assert(entry.get_klass()->is_klass(), "must be");
545 return entry.get_klass()->name();
546 } else {
547 assert(entry.is_unresolved(), "must be either symbol or klass");
548 return entry.get_symbol();
549 }
550 }
552 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
553 jint ref_index = klass_ref_index_at(which);
554 return klass_at_noresolve(ref_index);
555 }
557 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
558 jint ref_index = uncached_klass_ref_index_at(which);
559 return klass_at_noresolve(ref_index);
560 }
562 char* ConstantPool::string_at_noresolve(int which) {
563 Symbol* s = unresolved_string_at(which);
564 if (s == NULL) {
565 return (char*)"<pseudo-string>";
566 } else {
567 return unresolved_string_at(which)->as_C_string();
568 }
569 }
571 BasicType ConstantPool::basic_type_for_signature_at(int which) {
572 return FieldType::basic_type(symbol_at(which));
573 }
576 void ConstantPool::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
577 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
578 if (this_oop->tag_at(index).is_string()) {
579 this_oop->string_at(index, CHECK);
580 }
581 }
582 }
584 // Resolve all the classes in the constant pool. If they are all resolved,
585 // the constant pool is read-only. Enhancement: allocate cp entries to
586 // another metaspace, and copy to read-only or read-write space if this
587 // bit is set.
588 bool ConstantPool::resolve_class_constants(TRAPS) {
589 constantPoolHandle cp(THREAD, this);
590 for (int index = 1; index < length(); index++) { // Index 0 is unused
591 if (tag_at(index).is_unresolved_klass() &&
592 klass_at_if_loaded(cp, index) == NULL) {
593 return false;
594 }
595 }
596 // set_preresolution(); or some bit for future use
597 return true;
598 }
600 // If resolution for MethodHandle or MethodType fails, save the exception
601 // in the resolution error table, so that the same exception is thrown again.
602 void ConstantPool::save_and_throw_exception(constantPoolHandle this_oop, int which,
603 int tag, TRAPS) {
604 ResourceMark rm;
605 Symbol* error = PENDING_EXCEPTION->klass()->name();
606 MonitorLockerEx ml(this_oop->lock()); // lock cpool to change tag.
608 int error_tag = (tag == JVM_CONSTANT_MethodHandle) ?
609 JVM_CONSTANT_MethodHandleInError : JVM_CONSTANT_MethodTypeInError;
611 if (!PENDING_EXCEPTION->
612 is_a(SystemDictionary::LinkageError_klass())) {
613 // Just throw the exception and don't prevent these classes from
614 // being loaded due to virtual machine errors like StackOverflow
615 // and OutOfMemoryError, etc, or if the thread was hit by stop()
616 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
618 } else if (this_oop->tag_at(which).value() != error_tag) {
619 SystemDictionary::add_resolution_error(this_oop, which, error);
620 this_oop->tag_at_put(which, error_tag);
621 } else {
622 // some other thread has put the class in error state.
623 error = SystemDictionary::find_resolution_error(this_oop, which);
624 assert(error != NULL, "checking");
625 CLEAR_PENDING_EXCEPTION;
626 THROW_MSG(error, "");
627 }
628 }
631 // Called to resolve constants in the constant pool and return an oop.
632 // Some constant pool entries cache their resolved oop. This is also
633 // called to create oops from constants to use in arguments for invokedynamic
634 oop ConstantPool::resolve_constant_at_impl(constantPoolHandle this_oop, int index, int cache_index, TRAPS) {
635 oop result_oop = NULL;
636 Handle throw_exception;
638 if (cache_index == _possible_index_sentinel) {
639 // It is possible that this constant is one which is cached in the objects.
640 // We'll do a linear search. This should be OK because this usage is rare.
641 assert(index > 0, "valid index");
642 cache_index = this_oop->cp_to_object_index(index);
643 }
644 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
645 assert(index == _no_index_sentinel || index >= 0, "");
647 if (cache_index >= 0) {
648 result_oop = this_oop->resolved_references()->obj_at(cache_index);
649 if (result_oop != NULL) {
650 return result_oop;
651 // That was easy...
652 }
653 index = this_oop->object_to_cp_index(cache_index);
654 }
656 jvalue prim_value; // temp used only in a few cases below
658 int tag_value = this_oop->tag_at(index).value();
660 switch (tag_value) {
662 case JVM_CONSTANT_UnresolvedClass:
663 case JVM_CONSTANT_UnresolvedClassInError:
664 case JVM_CONSTANT_Class:
665 {
666 assert(cache_index == _no_index_sentinel, "should not have been set");
667 Klass* resolved = klass_at_impl(this_oop, index, CHECK_NULL);
668 // ldc wants the java mirror.
669 result_oop = resolved->java_mirror();
670 break;
671 }
673 case JVM_CONSTANT_String:
674 assert(cache_index != _no_index_sentinel, "should have been set");
675 if (this_oop->is_pseudo_string_at(index)) {
676 result_oop = this_oop->pseudo_string_at(index, cache_index);
677 break;
678 }
679 result_oop = string_at_impl(this_oop, index, cache_index, CHECK_NULL);
680 break;
682 case JVM_CONSTANT_MethodHandleInError:
683 case JVM_CONSTANT_MethodTypeInError:
684 {
685 Symbol* error = SystemDictionary::find_resolution_error(this_oop, index);
686 guarantee(error != (Symbol*)NULL, "tag mismatch with resolution error table");
687 ResourceMark rm;
688 THROW_MSG_0(error, "");
689 break;
690 }
692 case JVM_CONSTANT_MethodHandle:
693 {
694 int ref_kind = this_oop->method_handle_ref_kind_at(index);
695 int callee_index = this_oop->method_handle_klass_index_at(index);
696 Symbol* name = this_oop->method_handle_name_ref_at(index);
697 Symbol* signature = this_oop->method_handle_signature_ref_at(index);
698 if (PrintMiscellaneous)
699 tty->print_cr("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
700 ref_kind, index, this_oop->method_handle_index_at(index),
701 callee_index, name->as_C_string(), signature->as_C_string());
702 KlassHandle callee;
703 { Klass* k = klass_at_impl(this_oop, callee_index, CHECK_NULL);
704 callee = KlassHandle(THREAD, k);
705 }
706 KlassHandle klass(THREAD, this_oop->pool_holder());
707 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
708 callee, name, signature,
709 THREAD);
710 result_oop = value();
711 if (HAS_PENDING_EXCEPTION) {
712 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
713 }
714 break;
715 }
717 case JVM_CONSTANT_MethodType:
718 {
719 Symbol* signature = this_oop->method_type_signature_at(index);
720 if (PrintMiscellaneous)
721 tty->print_cr("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
722 index, this_oop->method_type_index_at(index),
723 signature->as_C_string());
724 KlassHandle klass(THREAD, this_oop->pool_holder());
725 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
726 result_oop = value();
727 if (HAS_PENDING_EXCEPTION) {
728 save_and_throw_exception(this_oop, index, tag_value, CHECK_NULL);
729 }
730 break;
731 }
733 case JVM_CONSTANT_Integer:
734 assert(cache_index == _no_index_sentinel, "should not have been set");
735 prim_value.i = this_oop->int_at(index);
736 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
737 break;
739 case JVM_CONSTANT_Float:
740 assert(cache_index == _no_index_sentinel, "should not have been set");
741 prim_value.f = this_oop->float_at(index);
742 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
743 break;
745 case JVM_CONSTANT_Long:
746 assert(cache_index == _no_index_sentinel, "should not have been set");
747 prim_value.j = this_oop->long_at(index);
748 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
749 break;
751 case JVM_CONSTANT_Double:
752 assert(cache_index == _no_index_sentinel, "should not have been set");
753 prim_value.d = this_oop->double_at(index);
754 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
755 break;
757 default:
758 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
759 this_oop(), index, cache_index, tag_value) );
760 assert(false, "unexpected constant tag");
761 break;
762 }
764 if (cache_index >= 0) {
765 // Cache the oop here also.
766 Handle result_handle(THREAD, result_oop);
767 MonitorLockerEx ml(this_oop->lock()); // don't know if we really need this
768 oop result = this_oop->resolved_references()->obj_at(cache_index);
769 // Benign race condition: resolved_references may already be filled in while we were trying to lock.
770 // The important thing here is that all threads pick up the same result.
771 // It doesn't matter which racing thread wins, as long as only one
772 // result is used by all threads, and all future queries.
773 // That result may be either a resolved constant or a failure exception.
774 if (result == NULL) {
775 this_oop->resolved_references()->obj_at_put(cache_index, result_handle());
776 return result_handle();
777 } else {
778 // Return the winning thread's result. This can be different than
779 // result_handle() for MethodHandles.
780 return result;
781 }
782 } else {
783 return result_oop;
784 }
785 }
787 oop ConstantPool::uncached_string_at(int which, TRAPS) {
788 Symbol* sym = unresolved_string_at(which);
789 oop str = StringTable::intern(sym, CHECK_(NULL));
790 assert(java_lang_String::is_instance(str), "must be string");
791 return str;
792 }
795 oop ConstantPool::resolve_bootstrap_specifier_at_impl(constantPoolHandle this_oop, int index, TRAPS) {
796 assert(this_oop->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
798 Handle bsm;
799 int argc;
800 {
801 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
802 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
803 // It is accompanied by the optional arguments.
804 int bsm_index = this_oop->invoke_dynamic_bootstrap_method_ref_index_at(index);
805 oop bsm_oop = this_oop->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
806 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
807 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
808 }
810 // Extract the optional static arguments.
811 argc = this_oop->invoke_dynamic_argument_count_at(index);
812 if (argc == 0) return bsm_oop;
814 bsm = Handle(THREAD, bsm_oop);
815 }
817 objArrayHandle info;
818 {
819 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
820 info = objArrayHandle(THREAD, info_oop);
821 }
823 info->obj_at_put(0, bsm());
824 for (int i = 0; i < argc; i++) {
825 int arg_index = this_oop->invoke_dynamic_argument_index_at(index, i);
826 oop arg_oop = this_oop->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
827 info->obj_at_put(1+i, arg_oop);
828 }
830 return info();
831 }
833 oop ConstantPool::string_at_impl(constantPoolHandle this_oop, int which, int obj_index, TRAPS) {
834 // If the string has already been interned, this entry will be non-null
835 oop str = this_oop->resolved_references()->obj_at(obj_index);
836 if (str != NULL) return str;
837 Symbol* sym = this_oop->unresolved_string_at(which);
838 str = StringTable::intern(sym, CHECK_(NULL));
839 this_oop->string_at_put(which, obj_index, str);
840 assert(java_lang_String::is_instance(str), "must be string");
841 return str;
842 }
845 bool ConstantPool::klass_name_at_matches(instanceKlassHandle k,
846 int which) {
847 // Names are interned, so we can compare Symbol*s directly
848 Symbol* cp_name = klass_name_at(which);
849 return (cp_name == k->name());
850 }
853 // Iterate over symbols and decrement ones which are Symbol*s.
854 // This is done during GC so do not need to lock constantPool unless we
855 // have per-thread safepoints.
856 // Only decrement the UTF8 symbols. Unresolved classes and strings point to
857 // these symbols but didn't increment the reference count.
858 void ConstantPool::unreference_symbols() {
859 for (int index = 1; index < length(); index++) { // Index 0 is unused
860 constantTag tag = tag_at(index);
861 if (tag.is_symbol()) {
862 symbol_at(index)->decrement_refcount();
863 }
864 }
865 }
868 // Compare this constant pool's entry at index1 to the constant pool
869 // cp2's entry at index2.
870 bool ConstantPool::compare_entry_to(int index1, constantPoolHandle cp2,
871 int index2, TRAPS) {
873 // The error tags are equivalent to non-error tags when comparing
874 jbyte t1 = tag_at(index1).non_error_value();
875 jbyte t2 = cp2->tag_at(index2).non_error_value();
877 if (t1 != t2) {
878 // Not the same entry type so there is nothing else to check. Note
879 // that this style of checking will consider resolved/unresolved
880 // class pairs as different.
881 // From the ConstantPool* API point of view, this is correct
882 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
883 // plays out in the context of ConstantPool* merging.
884 return false;
885 }
887 switch (t1) {
888 case JVM_CONSTANT_Class:
889 {
890 Klass* k1 = klass_at(index1, CHECK_false);
891 Klass* k2 = cp2->klass_at(index2, CHECK_false);
892 if (k1 == k2) {
893 return true;
894 }
895 } break;
897 case JVM_CONSTANT_ClassIndex:
898 {
899 int recur1 = klass_index_at(index1);
900 int recur2 = cp2->klass_index_at(index2);
901 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
902 if (match) {
903 return true;
904 }
905 } break;
907 case JVM_CONSTANT_Double:
908 {
909 jdouble d1 = double_at(index1);
910 jdouble d2 = cp2->double_at(index2);
911 if (d1 == d2) {
912 return true;
913 }
914 } break;
916 case JVM_CONSTANT_Fieldref:
917 case JVM_CONSTANT_InterfaceMethodref:
918 case JVM_CONSTANT_Methodref:
919 {
920 int recur1 = uncached_klass_ref_index_at(index1);
921 int recur2 = cp2->uncached_klass_ref_index_at(index2);
922 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
923 if (match) {
924 recur1 = uncached_name_and_type_ref_index_at(index1);
925 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
926 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
927 if (match) {
928 return true;
929 }
930 }
931 } break;
933 case JVM_CONSTANT_Float:
934 {
935 jfloat f1 = float_at(index1);
936 jfloat f2 = cp2->float_at(index2);
937 if (f1 == f2) {
938 return true;
939 }
940 } break;
942 case JVM_CONSTANT_Integer:
943 {
944 jint i1 = int_at(index1);
945 jint i2 = cp2->int_at(index2);
946 if (i1 == i2) {
947 return true;
948 }
949 } break;
951 case JVM_CONSTANT_Long:
952 {
953 jlong l1 = long_at(index1);
954 jlong l2 = cp2->long_at(index2);
955 if (l1 == l2) {
956 return true;
957 }
958 } break;
960 case JVM_CONSTANT_NameAndType:
961 {
962 int recur1 = name_ref_index_at(index1);
963 int recur2 = cp2->name_ref_index_at(index2);
964 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
965 if (match) {
966 recur1 = signature_ref_index_at(index1);
967 recur2 = cp2->signature_ref_index_at(index2);
968 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
969 if (match) {
970 return true;
971 }
972 }
973 } break;
975 case JVM_CONSTANT_StringIndex:
976 {
977 int recur1 = string_index_at(index1);
978 int recur2 = cp2->string_index_at(index2);
979 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
980 if (match) {
981 return true;
982 }
983 } break;
985 case JVM_CONSTANT_UnresolvedClass:
986 {
987 Symbol* k1 = unresolved_klass_at(index1);
988 Symbol* k2 = cp2->unresolved_klass_at(index2);
989 if (k1 == k2) {
990 return true;
991 }
992 } break;
994 case JVM_CONSTANT_MethodType:
995 {
996 int k1 = method_type_index_at_error_ok(index1);
997 int k2 = cp2->method_type_index_at_error_ok(index2);
998 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
999 if (match) {
1000 return true;
1001 }
1002 } break;
1004 case JVM_CONSTANT_MethodHandle:
1005 {
1006 int k1 = method_handle_ref_kind_at_error_ok(index1);
1007 int k2 = cp2->method_handle_ref_kind_at_error_ok(index2);
1008 if (k1 == k2) {
1009 int i1 = method_handle_index_at_error_ok(index1);
1010 int i2 = cp2->method_handle_index_at_error_ok(index2);
1011 bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1012 if (match) {
1013 return true;
1014 }
1015 }
1016 } break;
1018 case JVM_CONSTANT_InvokeDynamic:
1019 {
1020 int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
1021 int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
1022 int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
1023 int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
1024 // separate statements and variables because CHECK_false is used
1025 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1026 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1027 return (match_entry && match_operand);
1028 } break;
1030 case JVM_CONSTANT_String:
1031 {
1032 Symbol* s1 = unresolved_string_at(index1);
1033 Symbol* s2 = cp2->unresolved_string_at(index2);
1034 if (s1 == s2) {
1035 return true;
1036 }
1037 } break;
1039 case JVM_CONSTANT_Utf8:
1040 {
1041 Symbol* s1 = symbol_at(index1);
1042 Symbol* s2 = cp2->symbol_at(index2);
1043 if (s1 == s2) {
1044 return true;
1045 }
1046 } break;
1048 // Invalid is used as the tag for the second constant pool entry
1049 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1050 // not be seen by itself.
1051 case JVM_CONSTANT_Invalid: // fall through
1053 default:
1054 ShouldNotReachHere();
1055 break;
1056 }
1058 return false;
1059 } // end compare_entry_to()
1062 // Resize the operands array with delta_len and delta_size.
1063 // Used in RedefineClasses for CP merge.
1064 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1065 int old_len = operand_array_length(operands());
1066 int new_len = old_len + delta_len;
1067 int min_len = (delta_len > 0) ? old_len : new_len;
1069 int old_size = operands()->length();
1070 int new_size = old_size + delta_size;
1071 int min_size = (delta_size > 0) ? old_size : new_size;
1073 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1074 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1076 // Set index in the resized array for existing elements only
1077 for (int idx = 0; idx < min_len; idx++) {
1078 int offset = operand_offset_at(idx); // offset in original array
1079 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1080 }
1081 // Copy the bootstrap specifiers only
1082 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1083 new_ops->adr_at(2*new_len),
1084 (min_size - 2*min_len) * sizeof(u2));
1085 // Explicitly deallocate old operands array.
1086 // Note, it is not needed for 7u backport.
1087 if ( operands() != NULL) { // the safety check
1088 MetadataFactory::free_array<u2>(loader_data, operands());
1089 }
1090 set_operands(new_ops);
1091 } // end resize_operands()
1094 // Extend the operands array with the length and size of the ext_cp operands.
1095 // Used in RedefineClasses for CP merge.
1096 void ConstantPool::extend_operands(constantPoolHandle ext_cp, TRAPS) {
1097 int delta_len = operand_array_length(ext_cp->operands());
1098 if (delta_len == 0) {
1099 return; // nothing to do
1100 }
1101 int delta_size = ext_cp->operands()->length();
1103 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger");
1105 if (operand_array_length(operands()) == 0) {
1106 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1107 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1108 // The first element index defines the offset of second part
1109 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1110 set_operands(new_ops);
1111 } else {
1112 resize_operands(delta_len, delta_size, CHECK);
1113 }
1115 } // end extend_operands()
1118 // Shrink the operands array to a smaller array with new_len length.
1119 // Used in RedefineClasses for CP merge.
1120 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1121 int old_len = operand_array_length(operands());
1122 if (new_len == old_len) {
1123 return; // nothing to do
1124 }
1125 assert(new_len < old_len, "shrunken operands array must be smaller");
1127 int free_base = operand_next_offset_at(new_len - 1);
1128 int delta_len = new_len - old_len;
1129 int delta_size = 2*delta_len + free_base - operands()->length();
1131 resize_operands(delta_len, delta_size, CHECK);
1133 } // end shrink_operands()
1136 void ConstantPool::copy_operands(constantPoolHandle from_cp,
1137 constantPoolHandle to_cp,
1138 TRAPS) {
1140 int from_oplen = operand_array_length(from_cp->operands());
1141 int old_oplen = operand_array_length(to_cp->operands());
1142 if (from_oplen != 0) {
1143 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1144 // append my operands to the target's operands array
1145 if (old_oplen == 0) {
1146 // Can't just reuse from_cp's operand list because of deallocation issues
1147 int len = from_cp->operands()->length();
1148 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1149 Copy::conjoint_memory_atomic(
1150 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1151 to_cp->set_operands(new_ops);
1152 } else {
1153 int old_len = to_cp->operands()->length();
1154 int from_len = from_cp->operands()->length();
1155 int old_off = old_oplen * sizeof(u2);
1156 int from_off = from_oplen * sizeof(u2);
1157 // Use the metaspace for the destination constant pool
1158 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1159 int fillp = 0, len = 0;
1160 // first part of dest
1161 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1162 new_operands->adr_at(fillp),
1163 (len = old_off) * sizeof(u2));
1164 fillp += len;
1165 // first part of src
1166 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1167 new_operands->adr_at(fillp),
1168 (len = from_off) * sizeof(u2));
1169 fillp += len;
1170 // second part of dest
1171 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1172 new_operands->adr_at(fillp),
1173 (len = old_len - old_off) * sizeof(u2));
1174 fillp += len;
1175 // second part of src
1176 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1177 new_operands->adr_at(fillp),
1178 (len = from_len - from_off) * sizeof(u2));
1179 fillp += len;
1180 assert(fillp == new_operands->length(), "");
1182 // Adjust indexes in the first part of the copied operands array.
1183 for (int j = 0; j < from_oplen; j++) {
1184 int offset = operand_offset_at(new_operands, old_oplen + j);
1185 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1186 offset += old_len; // every new tuple is preceded by old_len extra u2's
1187 operand_offset_at_put(new_operands, old_oplen + j, offset);
1188 }
1190 // replace target operands array with combined array
1191 to_cp->set_operands(new_operands);
1192 }
1193 }
1194 } // end copy_operands()
1197 // Copy this constant pool's entries at start_i to end_i (inclusive)
1198 // to the constant pool to_cp's entries starting at to_i. A total of
1199 // (end_i - start_i) + 1 entries are copied.
1200 void ConstantPool::copy_cp_to_impl(constantPoolHandle from_cp, int start_i, int end_i,
1201 constantPoolHandle to_cp, int to_i, TRAPS) {
1204 int dest_i = to_i; // leave original alone for debug purposes
1206 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1207 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1209 switch (from_cp->tag_at(src_i).value()) {
1210 case JVM_CONSTANT_Double:
1211 case JVM_CONSTANT_Long:
1212 // double and long take two constant pool entries
1213 src_i += 2;
1214 dest_i += 2;
1215 break;
1217 default:
1218 // all others take one constant pool entry
1219 src_i++;
1220 dest_i++;
1221 break;
1222 }
1223 }
1224 copy_operands(from_cp, to_cp, CHECK);
1226 } // end copy_cp_to_impl()
1229 // Copy this constant pool's entry at from_i to the constant pool
1230 // to_cp's entry at to_i.
1231 void ConstantPool::copy_entry_to(constantPoolHandle from_cp, int from_i,
1232 constantPoolHandle to_cp, int to_i,
1233 TRAPS) {
1235 int tag = from_cp->tag_at(from_i).value();
1236 switch (tag) {
1237 case JVM_CONSTANT_Class:
1238 {
1239 Klass* k = from_cp->klass_at(from_i, CHECK);
1240 to_cp->klass_at_put(to_i, k);
1241 } break;
1243 case JVM_CONSTANT_ClassIndex:
1244 {
1245 jint ki = from_cp->klass_index_at(from_i);
1246 to_cp->klass_index_at_put(to_i, ki);
1247 } break;
1249 case JVM_CONSTANT_Double:
1250 {
1251 jdouble d = from_cp->double_at(from_i);
1252 to_cp->double_at_put(to_i, d);
1253 // double takes two constant pool entries so init second entry's tag
1254 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1255 } break;
1257 case JVM_CONSTANT_Fieldref:
1258 {
1259 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1260 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1261 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1262 } break;
1264 case JVM_CONSTANT_Float:
1265 {
1266 jfloat f = from_cp->float_at(from_i);
1267 to_cp->float_at_put(to_i, f);
1268 } break;
1270 case JVM_CONSTANT_Integer:
1271 {
1272 jint i = from_cp->int_at(from_i);
1273 to_cp->int_at_put(to_i, i);
1274 } break;
1276 case JVM_CONSTANT_InterfaceMethodref:
1277 {
1278 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1279 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1280 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1281 } break;
1283 case JVM_CONSTANT_Long:
1284 {
1285 jlong l = from_cp->long_at(from_i);
1286 to_cp->long_at_put(to_i, l);
1287 // long takes two constant pool entries so init second entry's tag
1288 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1289 } break;
1291 case JVM_CONSTANT_Methodref:
1292 {
1293 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1294 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1295 to_cp->method_at_put(to_i, class_index, name_and_type_index);
1296 } break;
1298 case JVM_CONSTANT_NameAndType:
1299 {
1300 int name_ref_index = from_cp->name_ref_index_at(from_i);
1301 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1302 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1303 } break;
1305 case JVM_CONSTANT_StringIndex:
1306 {
1307 jint si = from_cp->string_index_at(from_i);
1308 to_cp->string_index_at_put(to_i, si);
1309 } break;
1311 case JVM_CONSTANT_UnresolvedClass:
1312 case JVM_CONSTANT_UnresolvedClassInError:
1313 {
1314 // Can be resolved after checking tag, so check the slot first.
1315 CPSlot entry = from_cp->slot_at(from_i);
1316 if (entry.is_resolved()) {
1317 assert(entry.get_klass()->is_klass(), "must be");
1318 // Already resolved
1319 to_cp->klass_at_put(to_i, entry.get_klass());
1320 } else {
1321 to_cp->unresolved_klass_at_put(to_i, entry.get_symbol());
1322 }
1323 } break;
1325 case JVM_CONSTANT_String:
1326 {
1327 Symbol* s = from_cp->unresolved_string_at(from_i);
1328 to_cp->unresolved_string_at_put(to_i, s);
1329 } break;
1331 case JVM_CONSTANT_Utf8:
1332 {
1333 Symbol* s = from_cp->symbol_at(from_i);
1334 // Need to increase refcount, the old one will be thrown away and deferenced
1335 s->increment_refcount();
1336 to_cp->symbol_at_put(to_i, s);
1337 } break;
1339 case JVM_CONSTANT_MethodType:
1340 case JVM_CONSTANT_MethodTypeInError:
1341 {
1342 jint k = from_cp->method_type_index_at_error_ok(from_i);
1343 to_cp->method_type_index_at_put(to_i, k);
1344 } break;
1346 case JVM_CONSTANT_MethodHandle:
1347 case JVM_CONSTANT_MethodHandleInError:
1348 {
1349 int k1 = from_cp->method_handle_ref_kind_at_error_ok(from_i);
1350 int k2 = from_cp->method_handle_index_at_error_ok(from_i);
1351 to_cp->method_handle_index_at_put(to_i, k1, k2);
1352 } break;
1354 case JVM_CONSTANT_InvokeDynamic:
1355 {
1356 int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1357 int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1358 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1359 to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1360 } break;
1362 // Invalid is used as the tag for the second constant pool entry
1363 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1364 // not be seen by itself.
1365 case JVM_CONSTANT_Invalid: // fall through
1367 default:
1368 {
1369 ShouldNotReachHere();
1370 } break;
1371 }
1372 } // end copy_entry_to()
1375 // Search constant pool search_cp for an entry that matches this
1376 // constant pool's entry at pattern_i. Returns the index of a
1377 // matching entry or zero (0) if there is no matching entry.
1378 int ConstantPool::find_matching_entry(int pattern_i,
1379 constantPoolHandle search_cp, TRAPS) {
1381 // index zero (0) is not used
1382 for (int i = 1; i < search_cp->length(); i++) {
1383 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1384 if (found) {
1385 return i;
1386 }
1387 }
1389 return 0; // entry not found; return unused index zero (0)
1390 } // end find_matching_entry()
1393 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1394 // cp2's bootstrap specifier at idx2.
1395 bool ConstantPool::compare_operand_to(int idx1, constantPoolHandle cp2, int idx2, TRAPS) {
1396 int k1 = operand_bootstrap_method_ref_index_at(idx1);
1397 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1398 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1400 if (!match) {
1401 return false;
1402 }
1403 int argc = operand_argument_count_at(idx1);
1404 if (argc == cp2->operand_argument_count_at(idx2)) {
1405 for (int j = 0; j < argc; j++) {
1406 k1 = operand_argument_index_at(idx1, j);
1407 k2 = cp2->operand_argument_index_at(idx2, j);
1408 match = compare_entry_to(k1, cp2, k2, CHECK_false);
1409 if (!match) {
1410 return false;
1411 }
1412 }
1413 return true; // got through loop; all elements equal
1414 }
1415 return false;
1416 } // end compare_operand_to()
1418 // Search constant pool search_cp for a bootstrap specifier that matches
1419 // this constant pool's bootstrap specifier at pattern_i index.
1420 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
1421 int ConstantPool::find_matching_operand(int pattern_i,
1422 constantPoolHandle search_cp, int search_len, TRAPS) {
1423 for (int i = 0; i < search_len; i++) {
1424 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1425 if (found) {
1426 return i;
1427 }
1428 }
1429 return -1; // bootstrap specifier not found; return unused index (-1)
1430 } // end find_matching_operand()
1433 #ifndef PRODUCT
1435 const char* ConstantPool::printable_name_at(int which) {
1437 constantTag tag = tag_at(which);
1439 if (tag.is_string()) {
1440 return string_at_noresolve(which);
1441 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1442 return klass_name_at(which)->as_C_string();
1443 } else if (tag.is_symbol()) {
1444 return symbol_at(which)->as_C_string();
1445 }
1446 return "";
1447 }
1449 #endif // PRODUCT
1452 // JVMTI GetConstantPool support
1454 // For debugging of constant pool
1455 const bool debug_cpool = false;
1457 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1459 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1460 const char* WARN_MSG = "Must not be such entry!";
1461 jint size = 0;
1462 u2 idx1, idx2;
1464 for (jint idx = 1; idx < cnt; idx++) {
1465 jint ent_size = 0;
1466 u1 tag = *bytes++;
1467 size++; // count tag
1469 printf("const #%03d, tag: %02d ", idx, tag);
1470 switch(tag) {
1471 case JVM_CONSTANT_Invalid: {
1472 printf("Invalid");
1473 break;
1474 }
1475 case JVM_CONSTANT_Unicode: {
1476 printf("Unicode %s", WARN_MSG);
1477 break;
1478 }
1479 case JVM_CONSTANT_Utf8: {
1480 u2 len = Bytes::get_Java_u2(bytes);
1481 char str[128];
1482 if (len > 127) {
1483 len = 127;
1484 }
1485 strncpy(str, (char *) (bytes+2), len);
1486 str[len] = '\0';
1487 printf("Utf8 \"%s\"", str);
1488 ent_size = 2 + len;
1489 break;
1490 }
1491 case JVM_CONSTANT_Integer: {
1492 u4 val = Bytes::get_Java_u4(bytes);
1493 printf("int %d", *(int *) &val);
1494 ent_size = 4;
1495 break;
1496 }
1497 case JVM_CONSTANT_Float: {
1498 u4 val = Bytes::get_Java_u4(bytes);
1499 printf("float %5.3ff", *(float *) &val);
1500 ent_size = 4;
1501 break;
1502 }
1503 case JVM_CONSTANT_Long: {
1504 u8 val = Bytes::get_Java_u8(bytes);
1505 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val);
1506 ent_size = 8;
1507 idx++; // Long takes two cpool slots
1508 break;
1509 }
1510 case JVM_CONSTANT_Double: {
1511 u8 val = Bytes::get_Java_u8(bytes);
1512 printf("double %5.3fd", *(jdouble *)&val);
1513 ent_size = 8;
1514 idx++; // Double takes two cpool slots
1515 break;
1516 }
1517 case JVM_CONSTANT_Class: {
1518 idx1 = Bytes::get_Java_u2(bytes);
1519 printf("class #%03d", idx1);
1520 ent_size = 2;
1521 break;
1522 }
1523 case JVM_CONSTANT_String: {
1524 idx1 = Bytes::get_Java_u2(bytes);
1525 printf("String #%03d", idx1);
1526 ent_size = 2;
1527 break;
1528 }
1529 case JVM_CONSTANT_Fieldref: {
1530 idx1 = Bytes::get_Java_u2(bytes);
1531 idx2 = Bytes::get_Java_u2(bytes+2);
1532 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
1533 ent_size = 4;
1534 break;
1535 }
1536 case JVM_CONSTANT_Methodref: {
1537 idx1 = Bytes::get_Java_u2(bytes);
1538 idx2 = Bytes::get_Java_u2(bytes+2);
1539 printf("Method #%03d, #%03d", idx1, idx2);
1540 ent_size = 4;
1541 break;
1542 }
1543 case JVM_CONSTANT_InterfaceMethodref: {
1544 idx1 = Bytes::get_Java_u2(bytes);
1545 idx2 = Bytes::get_Java_u2(bytes+2);
1546 printf("InterfMethod #%03d, #%03d", idx1, idx2);
1547 ent_size = 4;
1548 break;
1549 }
1550 case JVM_CONSTANT_NameAndType: {
1551 idx1 = Bytes::get_Java_u2(bytes);
1552 idx2 = Bytes::get_Java_u2(bytes+2);
1553 printf("NameAndType #%03d, #%03d", idx1, idx2);
1554 ent_size = 4;
1555 break;
1556 }
1557 case JVM_CONSTANT_ClassIndex: {
1558 printf("ClassIndex %s", WARN_MSG);
1559 break;
1560 }
1561 case JVM_CONSTANT_UnresolvedClass: {
1562 printf("UnresolvedClass: %s", WARN_MSG);
1563 break;
1564 }
1565 case JVM_CONSTANT_UnresolvedClassInError: {
1566 printf("UnresolvedClassInErr: %s", WARN_MSG);
1567 break;
1568 }
1569 case JVM_CONSTANT_StringIndex: {
1570 printf("StringIndex: %s", WARN_MSG);
1571 break;
1572 }
1573 }
1574 printf(";\n");
1575 bytes += ent_size;
1576 size += ent_size;
1577 }
1578 printf("Cpool size: %d\n", size);
1579 fflush(0);
1580 return;
1581 } /* end print_cpool_bytes */
1584 // Returns size of constant pool entry.
1585 jint ConstantPool::cpool_entry_size(jint idx) {
1586 switch(tag_at(idx).value()) {
1587 case JVM_CONSTANT_Invalid:
1588 case JVM_CONSTANT_Unicode:
1589 return 1;
1591 case JVM_CONSTANT_Utf8:
1592 return 3 + symbol_at(idx)->utf8_length();
1594 case JVM_CONSTANT_Class:
1595 case JVM_CONSTANT_String:
1596 case JVM_CONSTANT_ClassIndex:
1597 case JVM_CONSTANT_UnresolvedClass:
1598 case JVM_CONSTANT_UnresolvedClassInError:
1599 case JVM_CONSTANT_StringIndex:
1600 case JVM_CONSTANT_MethodType:
1601 case JVM_CONSTANT_MethodTypeInError:
1602 return 3;
1604 case JVM_CONSTANT_MethodHandle:
1605 case JVM_CONSTANT_MethodHandleInError:
1606 return 4; //tag, ref_kind, ref_index
1608 case JVM_CONSTANT_Integer:
1609 case JVM_CONSTANT_Float:
1610 case JVM_CONSTANT_Fieldref:
1611 case JVM_CONSTANT_Methodref:
1612 case JVM_CONSTANT_InterfaceMethodref:
1613 case JVM_CONSTANT_NameAndType:
1614 return 5;
1616 case JVM_CONSTANT_InvokeDynamic:
1617 // u1 tag, u2 bsm, u2 nt
1618 return 5;
1620 case JVM_CONSTANT_Long:
1621 case JVM_CONSTANT_Double:
1622 return 9;
1623 }
1624 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1625 return 1;
1626 } /* end cpool_entry_size */
1629 // SymbolHashMap is used to find a constant pool index from a string.
1630 // This function fills in SymbolHashMaps, one for utf8s and one for
1631 // class names, returns size of the cpool raw bytes.
1632 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1633 SymbolHashMap *classmap) {
1634 jint size = 0;
1636 for (u2 idx = 1; idx < length(); idx++) {
1637 u2 tag = tag_at(idx).value();
1638 size += cpool_entry_size(idx);
1640 switch(tag) {
1641 case JVM_CONSTANT_Utf8: {
1642 Symbol* sym = symbol_at(idx);
1643 symmap->add_entry(sym, idx);
1644 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1645 break;
1646 }
1647 case JVM_CONSTANT_Class:
1648 case JVM_CONSTANT_UnresolvedClass:
1649 case JVM_CONSTANT_UnresolvedClassInError: {
1650 Symbol* sym = klass_name_at(idx);
1651 classmap->add_entry(sym, idx);
1652 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1653 break;
1654 }
1655 case JVM_CONSTANT_Long:
1656 case JVM_CONSTANT_Double: {
1657 idx++; // Both Long and Double take two cpool slots
1658 break;
1659 }
1660 }
1661 }
1662 return size;
1663 } /* end hash_utf8_entries_to */
1666 // Copy cpool bytes.
1667 // Returns:
1668 // 0, in case of OutOfMemoryError
1669 // -1, in case of internal error
1670 // > 0, count of the raw cpool bytes that have been copied
1671 int ConstantPool::copy_cpool_bytes(int cpool_size,
1672 SymbolHashMap* tbl,
1673 unsigned char *bytes) {
1674 u2 idx1, idx2;
1675 jint size = 0;
1676 jint cnt = length();
1677 unsigned char *start_bytes = bytes;
1679 for (jint idx = 1; idx < cnt; idx++) {
1680 u1 tag = tag_at(idx).value();
1681 jint ent_size = cpool_entry_size(idx);
1683 assert(size + ent_size <= cpool_size, "Size mismatch");
1685 *bytes = tag;
1686 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1687 switch(tag) {
1688 case JVM_CONSTANT_Invalid: {
1689 DBG(printf("JVM_CONSTANT_Invalid"));
1690 break;
1691 }
1692 case JVM_CONSTANT_Unicode: {
1693 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1694 DBG(printf("JVM_CONSTANT_Unicode"));
1695 break;
1696 }
1697 case JVM_CONSTANT_Utf8: {
1698 Symbol* sym = symbol_at(idx);
1699 char* str = sym->as_utf8();
1700 // Warning! It's crashing on x86 with len = sym->utf8_length()
1701 int len = (int) strlen(str);
1702 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1703 for (int i = 0; i < len; i++) {
1704 bytes[3+i] = (u1) str[i];
1705 }
1706 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1707 break;
1708 }
1709 case JVM_CONSTANT_Integer: {
1710 jint val = int_at(idx);
1711 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1712 break;
1713 }
1714 case JVM_CONSTANT_Float: {
1715 jfloat val = float_at(idx);
1716 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1717 break;
1718 }
1719 case JVM_CONSTANT_Long: {
1720 jlong val = long_at(idx);
1721 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1722 idx++; // Long takes two cpool slots
1723 break;
1724 }
1725 case JVM_CONSTANT_Double: {
1726 jdouble val = double_at(idx);
1727 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1728 idx++; // Double takes two cpool slots
1729 break;
1730 }
1731 case JVM_CONSTANT_Class:
1732 case JVM_CONSTANT_UnresolvedClass:
1733 case JVM_CONSTANT_UnresolvedClassInError: {
1734 *bytes = JVM_CONSTANT_Class;
1735 Symbol* sym = klass_name_at(idx);
1736 idx1 = tbl->symbol_to_value(sym);
1737 assert(idx1 != 0, "Have not found a hashtable entry");
1738 Bytes::put_Java_u2((address) (bytes+1), idx1);
1739 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1740 break;
1741 }
1742 case JVM_CONSTANT_String: {
1743 *bytes = JVM_CONSTANT_String;
1744 Symbol* sym = unresolved_string_at(idx);
1745 idx1 = tbl->symbol_to_value(sym);
1746 assert(idx1 != 0, "Have not found a hashtable entry");
1747 Bytes::put_Java_u2((address) (bytes+1), idx1);
1748 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
1749 break;
1750 }
1751 case JVM_CONSTANT_Fieldref:
1752 case JVM_CONSTANT_Methodref:
1753 case JVM_CONSTANT_InterfaceMethodref: {
1754 idx1 = uncached_klass_ref_index_at(idx);
1755 idx2 = uncached_name_and_type_ref_index_at(idx);
1756 Bytes::put_Java_u2((address) (bytes+1), idx1);
1757 Bytes::put_Java_u2((address) (bytes+3), idx2);
1758 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1759 break;
1760 }
1761 case JVM_CONSTANT_NameAndType: {
1762 idx1 = name_ref_index_at(idx);
1763 idx2 = signature_ref_index_at(idx);
1764 Bytes::put_Java_u2((address) (bytes+1), idx1);
1765 Bytes::put_Java_u2((address) (bytes+3), idx2);
1766 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1767 break;
1768 }
1769 case JVM_CONSTANT_ClassIndex: {
1770 *bytes = JVM_CONSTANT_Class;
1771 idx1 = klass_index_at(idx);
1772 Bytes::put_Java_u2((address) (bytes+1), idx1);
1773 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1774 break;
1775 }
1776 case JVM_CONSTANT_StringIndex: {
1777 *bytes = JVM_CONSTANT_String;
1778 idx1 = string_index_at(idx);
1779 Bytes::put_Java_u2((address) (bytes+1), idx1);
1780 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1781 break;
1782 }
1783 case JVM_CONSTANT_MethodHandle:
1784 case JVM_CONSTANT_MethodHandleInError: {
1785 *bytes = JVM_CONSTANT_MethodHandle;
1786 int kind = method_handle_ref_kind_at_error_ok(idx);
1787 idx1 = method_handle_index_at_error_ok(idx);
1788 *(bytes+1) = (unsigned char) kind;
1789 Bytes::put_Java_u2((address) (bytes+2), idx1);
1790 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1791 break;
1792 }
1793 case JVM_CONSTANT_MethodType:
1794 case JVM_CONSTANT_MethodTypeInError: {
1795 *bytes = JVM_CONSTANT_MethodType;
1796 idx1 = method_type_index_at_error_ok(idx);
1797 Bytes::put_Java_u2((address) (bytes+1), idx1);
1798 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1799 break;
1800 }
1801 case JVM_CONSTANT_InvokeDynamic: {
1802 *bytes = tag;
1803 idx1 = extract_low_short_from_int(*int_at_addr(idx));
1804 idx2 = extract_high_short_from_int(*int_at_addr(idx));
1805 assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1806 Bytes::put_Java_u2((address) (bytes+1), idx1);
1807 Bytes::put_Java_u2((address) (bytes+3), idx2);
1808 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1809 break;
1810 }
1811 }
1812 DBG(printf("\n"));
1813 bytes += ent_size;
1814 size += ent_size;
1815 }
1816 assert(size == cpool_size, "Size mismatch");
1818 // Keep temorarily for debugging until it's stable.
1819 DBG(print_cpool_bytes(cnt, start_bytes));
1820 return (int)(bytes - start_bytes);
1821 } /* end copy_cpool_bytes */
1823 #undef DBG
1826 void ConstantPool::set_on_stack(const bool value) {
1827 if (value) {
1828 int old_flags = *const_cast<volatile int *>(&_flags);
1829 while ((old_flags & _on_stack) == 0) {
1830 int new_flags = old_flags | _on_stack;
1831 int result = Atomic::cmpxchg(new_flags, &_flags, old_flags);
1833 if (result == old_flags) {
1834 // Succeeded.
1835 MetadataOnStackMark::record(this, Thread::current());
1836 return;
1837 }
1838 old_flags = result;
1839 }
1840 } else {
1841 // Clearing is done single-threadedly.
1842 _flags &= ~_on_stack;
1843 }
1844 }
1846 // JSR 292 support for patching constant pool oops after the class is linked and
1847 // the oop array for resolved references are created.
1848 // We can't do this during classfile parsing, which is how the other indexes are
1849 // patched. The other patches are applied early for some error checking
1850 // so only defer the pseudo_strings.
1851 void ConstantPool::patch_resolved_references(
1852 GrowableArray<Handle>* cp_patches) {
1853 assert(EnableInvokeDynamic, "");
1854 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
1855 Handle patch = cp_patches->at(index);
1856 if (patch.not_null()) {
1857 assert (tag_at(index).is_string(), "should only be string left");
1858 // Patching a string means pre-resolving it.
1859 // The spelling in the constant pool is ignored.
1860 // The constant reference may be any object whatever.
1861 // If it is not a real interned string, the constant is referred
1862 // to as a "pseudo-string", and must be presented to the CP
1863 // explicitly, because it may require scavenging.
1864 int obj_index = cp_to_object_index(index);
1865 pseudo_string_at_put(index, obj_index, patch());
1866 DEBUG_ONLY(cp_patches->at_put(index, Handle());)
1867 }
1868 }
1869 #ifdef ASSERT
1870 // Ensure that all the patches have been used.
1871 for (int index = 0; index < cp_patches->length(); index++) {
1872 assert(cp_patches->at(index).is_null(),
1873 err_msg("Unused constant pool patch at %d in class file %s",
1874 index,
1875 pool_holder()->external_name()));
1876 }
1877 #endif // ASSERT
1878 }
1880 #ifndef PRODUCT
1882 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
1883 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
1884 guarantee(obj->is_constantPool(), "object must be constant pool");
1885 constantPoolHandle cp(THREAD, (ConstantPool*)obj);
1886 guarantee(cp->pool_holder() != NULL, "must be fully loaded");
1888 for (int i = 0; i< cp->length(); i++) {
1889 if (cp->tag_at(i).is_unresolved_klass()) {
1890 // This will force loading of the class
1891 Klass* klass = cp->klass_at(i, CHECK);
1892 if (klass->oop_is_instance()) {
1893 // Force initialization of class
1894 InstanceKlass::cast(klass)->initialize(CHECK);
1895 }
1896 }
1897 }
1898 }
1900 #endif
1903 // Printing
1905 void ConstantPool::print_on(outputStream* st) const {
1906 EXCEPTION_MARK;
1907 assert(is_constantPool(), "must be constantPool");
1908 st->print_cr("%s", internal_name());
1909 if (flags() != 0) {
1910 st->print(" - flags: 0x%x", flags());
1911 if (has_preresolution()) st->print(" has_preresolution");
1912 if (on_stack()) st->print(" on_stack");
1913 st->cr();
1914 }
1915 if (pool_holder() != NULL) {
1916 st->print_cr(" - holder: " INTPTR_FORMAT, pool_holder());
1917 }
1918 st->print_cr(" - cache: " INTPTR_FORMAT, cache());
1919 st->print_cr(" - resolved_references: " INTPTR_FORMAT, (void *)resolved_references());
1920 st->print_cr(" - reference_map: " INTPTR_FORMAT, reference_map());
1922 for (int index = 1; index < length(); index++) { // Index 0 is unused
1923 ((ConstantPool*)this)->print_entry_on(index, st);
1924 switch (tag_at(index).value()) {
1925 case JVM_CONSTANT_Long :
1926 case JVM_CONSTANT_Double :
1927 index++; // Skip entry following eigth-byte constant
1928 }
1930 }
1931 st->cr();
1932 }
1934 // Print one constant pool entry
1935 void ConstantPool::print_entry_on(const int index, outputStream* st) {
1936 EXCEPTION_MARK;
1937 st->print(" - %3d : ", index);
1938 tag_at(index).print_on(st);
1939 st->print(" : ");
1940 switch (tag_at(index).value()) {
1941 case JVM_CONSTANT_Class :
1942 { Klass* k = klass_at(index, CATCH);
1943 guarantee(k != NULL, "need klass");
1944 k->print_value_on(st);
1945 st->print(" {0x%lx}", (address)k);
1946 }
1947 break;
1948 case JVM_CONSTANT_Fieldref :
1949 case JVM_CONSTANT_Methodref :
1950 case JVM_CONSTANT_InterfaceMethodref :
1951 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
1952 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
1953 break;
1954 case JVM_CONSTANT_String :
1955 if (is_pseudo_string_at(index)) {
1956 oop anObj = pseudo_string_at(index);
1957 anObj->print_value_on(st);
1958 st->print(" {0x%lx}", (address)anObj);
1959 } else {
1960 unresolved_string_at(index)->print_value_on(st);
1961 }
1962 break;
1963 case JVM_CONSTANT_Integer :
1964 st->print("%d", int_at(index));
1965 break;
1966 case JVM_CONSTANT_Float :
1967 st->print("%f", float_at(index));
1968 break;
1969 case JVM_CONSTANT_Long :
1970 st->print_jlong(long_at(index));
1971 break;
1972 case JVM_CONSTANT_Double :
1973 st->print("%lf", double_at(index));
1974 break;
1975 case JVM_CONSTANT_NameAndType :
1976 st->print("name_index=%d", name_ref_index_at(index));
1977 st->print(" signature_index=%d", signature_ref_index_at(index));
1978 break;
1979 case JVM_CONSTANT_Utf8 :
1980 symbol_at(index)->print_value_on(st);
1981 break;
1982 case JVM_CONSTANT_UnresolvedClass : // fall-through
1983 case JVM_CONSTANT_UnresolvedClassInError: {
1984 // unresolved_klass_at requires lock or safe world.
1985 CPSlot entry = slot_at(index);
1986 if (entry.is_resolved()) {
1987 entry.get_klass()->print_value_on(st);
1988 } else {
1989 entry.get_symbol()->print_value_on(st);
1990 }
1991 }
1992 break;
1993 case JVM_CONSTANT_MethodHandle :
1994 case JVM_CONSTANT_MethodHandleInError :
1995 st->print("ref_kind=%d", method_handle_ref_kind_at_error_ok(index));
1996 st->print(" ref_index=%d", method_handle_index_at_error_ok(index));
1997 break;
1998 case JVM_CONSTANT_MethodType :
1999 case JVM_CONSTANT_MethodTypeInError :
2000 st->print("signature_index=%d", method_type_index_at_error_ok(index));
2001 break;
2002 case JVM_CONSTANT_InvokeDynamic :
2003 {
2004 st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2005 st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2006 int argc = invoke_dynamic_argument_count_at(index);
2007 if (argc > 0) {
2008 for (int arg_i = 0; arg_i < argc; arg_i++) {
2009 int arg = invoke_dynamic_argument_index_at(index, arg_i);
2010 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2011 }
2012 st->print("}");
2013 }
2014 }
2015 break;
2016 default:
2017 ShouldNotReachHere();
2018 break;
2019 }
2020 st->cr();
2021 }
2023 void ConstantPool::print_value_on(outputStream* st) const {
2024 assert(is_constantPool(), "must be constantPool");
2025 st->print("constant pool [%d]", length());
2026 if (has_preresolution()) st->print("/preresolution");
2027 if (operands() != NULL) st->print("/operands[%d]", operands()->length());
2028 print_address_on(st);
2029 st->print(" for ");
2030 pool_holder()->print_value_on(st);
2031 if (pool_holder() != NULL) {
2032 bool extra = (pool_holder()->constants() != this);
2033 if (extra) st->print(" (extra)");
2034 }
2035 if (cache() != NULL) {
2036 st->print(" cache=" PTR_FORMAT, cache());
2037 }
2038 }
2040 #if INCLUDE_SERVICES
2041 // Size Statistics
2042 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2043 sz->_cp_all_bytes += (sz->_cp_bytes = sz->count(this));
2044 sz->_cp_all_bytes += (sz->_cp_tags_bytes = sz->count_array(tags()));
2045 sz->_cp_all_bytes += (sz->_cp_cache_bytes = sz->count(cache()));
2046 sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2047 sz->_cp_all_bytes += (sz->_cp_refmap_bytes = sz->count_array(reference_map()));
2049 sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2050 sz->_cp_refmap_bytes;
2051 sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2052 }
2053 #endif // INCLUDE_SERVICES
2055 // Verification
2057 void ConstantPool::verify_on(outputStream* st) {
2058 guarantee(is_constantPool(), "object must be constant pool");
2059 for (int i = 0; i< length(); i++) {
2060 constantTag tag = tag_at(i);
2061 CPSlot entry = slot_at(i);
2062 if (tag.is_klass()) {
2063 if (entry.is_resolved()) {
2064 guarantee(entry.get_klass()->is_klass(), "should be klass");
2065 }
2066 } else if (tag.is_unresolved_klass()) {
2067 if (entry.is_resolved()) {
2068 guarantee(entry.get_klass()->is_klass(), "should be klass");
2069 }
2070 } else if (tag.is_symbol()) {
2071 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2072 } else if (tag.is_string()) {
2073 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2074 }
2075 }
2076 if (cache() != NULL) {
2077 // Note: cache() can be NULL before a class is completely setup or
2078 // in temporary constant pools used during constant pool merging
2079 guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2080 }
2081 if (pool_holder() != NULL) {
2082 // Note: pool_holder() can be NULL in temporary constant pools
2083 // used during constant pool merging
2084 guarantee(pool_holder()->is_klass(), "should be klass");
2085 }
2086 }
2089 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2090 char *str = sym->as_utf8();
2091 unsigned int hash = compute_hash(str, sym->utf8_length());
2092 unsigned int index = hash % table_size();
2094 // check if already in map
2095 // we prefer the first entry since it is more likely to be what was used in
2096 // the class file
2097 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2098 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2099 if (en->hash() == hash && en->symbol() == sym) {
2100 return; // already there
2101 }
2102 }
2104 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2105 entry->set_next(bucket(index));
2106 _buckets[index].set_entry(entry);
2107 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2108 }
2110 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2111 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2112 char *str = sym->as_utf8();
2113 int len = sym->utf8_length();
2114 unsigned int hash = SymbolHashMap::compute_hash(str, len);
2115 unsigned int index = hash % table_size();
2116 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2117 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2118 if (en->hash() == hash && en->symbol() == sym) {
2119 return en;
2120 }
2121 }
2122 return NULL;
2123 }