1 /*
2 * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "jvm.h"
27 #include "classfile/classLoaderData.hpp"
28 #include "classfile/javaClasses.inline.hpp"
29 #include "classfile/metadataOnStackMark.hpp"
30 #include "classfile/stringTable.hpp"
31 #include "classfile/systemDictionary.hpp"
32 #include "classfile/vmSymbols.hpp"
33 #include "interpreter/bootstrapInfo.hpp"
34 #include "interpreter/linkResolver.hpp"
35 #include "logging/log.hpp"
36 #include "logging/logStream.hpp"
37 #include "memory/allocation.inline.hpp"
38 #include "memory/heapShared.hpp"
39 #include "memory/metadataFactory.hpp"
40 #include "memory/metaspaceClosure.hpp"
41 #include "memory/metaspaceShared.hpp"
42 #include "memory/oopFactory.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "oops/array.hpp"
46 #include "oops/constantPool.inline.hpp"
47 #include "oops/cpCache.inline.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/klass.inline.hpp"
50 #include "oops/objArrayKlass.hpp"
51 #include "oops/objArrayOop.inline.hpp"
52 #include "oops/oop.inline.hpp"
53 #include "oops/typeArrayOop.inline.hpp"
54 #include "prims/jvmtiExport.hpp"
55 #include "runtime/atomic.hpp"
56 #include "runtime/handles.inline.hpp"
57 #include "runtime/init.hpp"
58 #include "runtime/javaCalls.hpp"
59 #include "runtime/signature.hpp"
60 #include "runtime/thread.inline.hpp"
61 #include "runtime/vframe.inline.hpp"
62 #include "utilities/copy.hpp"
63
allocate(ClassLoaderData * loader_data,int length,TRAPS)64 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
65 Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
66 int size = ConstantPool::size(length);
67 return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
68 }
69
copy_fields(const ConstantPool * orig)70 void ConstantPool::copy_fields(const ConstantPool* orig) {
71 // Preserve dynamic constant information from the original pool
72 if (orig->has_dynamic_constant()) {
73 set_has_dynamic_constant();
74 }
75
76 // Copy class version
77 set_major_version(orig->major_version());
78 set_minor_version(orig->minor_version());
79
80 set_source_file_name_index(orig->source_file_name_index());
81 set_generic_signature_index(orig->generic_signature_index());
82 }
83
84 #ifdef ASSERT
85
86 // MetaspaceObj allocation invariant is calloc equivalent memory
87 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
tag_array_is_zero_initialized(Array<u1> * tags)88 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
89 assert(tags != NULL, "invariant");
90 const int length = tags->length();
91 for (int index = 0; index < length; ++index) {
92 if (JVM_CONSTANT_Invalid != tags->at(index)) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 #endif
100
ConstantPool(Array<u1> * tags)101 ConstantPool::ConstantPool(Array<u1>* tags) :
102 _tags(tags),
103 _length(tags->length()) {
104
105 assert(_tags != NULL, "invariant");
106 assert(tags->length() == _length, "invariant");
107 assert(tag_array_is_zero_initialized(tags), "invariant");
108 assert(0 == flags(), "invariant");
109 assert(0 == version(), "invariant");
110 assert(NULL == _pool_holder, "invariant");
111 }
112
deallocate_contents(ClassLoaderData * loader_data)113 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
114 if (cache() != NULL) {
115 MetadataFactory::free_metadata(loader_data, cache());
116 set_cache(NULL);
117 }
118
119 MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
120 set_resolved_klasses(NULL);
121
122 MetadataFactory::free_array<jushort>(loader_data, operands());
123 set_operands(NULL);
124
125 release_C_heap_structures();
126
127 // free tag array
128 MetadataFactory::free_array<u1>(loader_data, tags());
129 set_tags(NULL);
130 }
131
release_C_heap_structures()132 void ConstantPool::release_C_heap_structures() {
133 // walk constant pool and decrement symbol reference counts
134 unreference_symbols();
135 }
136
metaspace_pointers_do(MetaspaceClosure * it)137 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) {
138 log_trace(cds)("Iter(ConstantPool): %p", this);
139
140 it->push(&_tags, MetaspaceClosure::_writable);
141 it->push(&_cache);
142 it->push(&_pool_holder);
143 it->push(&_operands);
144 it->push(&_resolved_klasses, MetaspaceClosure::_writable);
145
146 for (int i = 0; i < length(); i++) {
147 // The only MSO's embedded in the CP entries are Symbols:
148 // JVM_CONSTANT_String (normal and pseudo)
149 // JVM_CONSTANT_Utf8
150 constantTag ctag = tag_at(i);
151 if (ctag.is_string() || ctag.is_utf8()) {
152 it->push(symbol_at_addr(i));
153 }
154 }
155 }
156
resolved_references() const157 objArrayOop ConstantPool::resolved_references() const {
158 return (objArrayOop)_cache->resolved_references();
159 }
160
161 // Called from outside constant pool resolution where a resolved_reference array
162 // may not be present.
resolved_references_or_null() const163 objArrayOop ConstantPool::resolved_references_or_null() const {
164 if (_cache == NULL) {
165 return NULL;
166 } else {
167 return (objArrayOop)_cache->resolved_references();
168 }
169 }
170
171 // Create resolved_references array and mapping array for original cp indexes
172 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
173 // to map it back for resolving and some unlikely miscellaneous uses.
174 // The objects created by invokedynamic are appended to this list.
initialize_resolved_references(ClassLoaderData * loader_data,const intStack & reference_map,int constant_pool_map_length,TRAPS)175 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
176 const intStack& reference_map,
177 int constant_pool_map_length,
178 TRAPS) {
179 // Initialized the resolved object cache.
180 int map_length = reference_map.length();
181 if (map_length > 0) {
182 // Only need mapping back to constant pool entries. The map isn't used for
183 // invokedynamic resolved_reference entries. For invokedynamic entries,
184 // the constant pool cache index has the mapping back to both the constant
185 // pool and to the resolved reference index.
186 if (constant_pool_map_length > 0) {
187 Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
188
189 for (int i = 0; i < constant_pool_map_length; i++) {
190 int x = reference_map.at(i);
191 assert(x == (int)(jushort) x, "klass index is too big");
192 om->at_put(i, (jushort)x);
193 }
194 set_reference_map(om);
195 }
196
197 // Create Java array for holding resolved strings, methodHandles,
198 // methodTypes, invokedynamic and invokehandle appendix objects, etc.
199 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
200 Handle refs_handle (THREAD, (oop)stom); // must handleize.
201 set_resolved_references(loader_data->add_handle(refs_handle));
202 }
203 }
204
allocate_resolved_klasses(ClassLoaderData * loader_data,int num_klasses,TRAPS)205 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
206 // A ConstantPool can't possibly have 0xffff valid class entries,
207 // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
208 // entry for the class's name. So at most we will have 0xfffe class entries.
209 // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
210 // UnresolvedKlass entries that are temporarily created during class redefinition.
211 assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
212 assert(resolved_klasses() == NULL, "sanity");
213 Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
214 set_resolved_klasses(rk);
215 }
216
initialize_unresolved_klasses(ClassLoaderData * loader_data,TRAPS)217 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
218 int len = length();
219 int num_klasses = 0;
220 for (int i = 1; i <len; i++) {
221 switch (tag_at(i).value()) {
222 case JVM_CONSTANT_ClassIndex:
223 {
224 const int class_index = klass_index_at(i);
225 unresolved_klass_at_put(i, class_index, num_klasses++);
226 }
227 break;
228 #ifndef PRODUCT
229 case JVM_CONSTANT_Class:
230 case JVM_CONSTANT_UnresolvedClass:
231 case JVM_CONSTANT_UnresolvedClassInError:
232 // All of these should have been reverted back to ClassIndex before calling
233 // this function.
234 ShouldNotReachHere();
235 #endif
236 }
237 }
238 allocate_resolved_klasses(loader_data, num_klasses, THREAD);
239 }
240
241 // Unsafe anonymous class support:
klass_at_put(int class_index,int name_index,int resolved_klass_index,Klass * k,Symbol * name)242 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
243 assert(is_within_bounds(class_index), "index out of bounds");
244 assert(is_within_bounds(name_index), "index out of bounds");
245 assert((resolved_klass_index & 0xffff0000) == 0, "must be");
246 *int_at_addr(class_index) =
247 build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
248
249 symbol_at_put(name_index, name);
250 name->increment_refcount();
251 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
252 Atomic::release_store(adr, k);
253
254 // The interpreter assumes when the tag is stored, the klass is resolved
255 // and the Klass* non-NULL, so we need hardware store ordering here.
256 if (k != NULL) {
257 release_tag_at_put(class_index, JVM_CONSTANT_Class);
258 } else {
259 release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
260 }
261 }
262
263 // Unsafe anonymous class support:
klass_at_put(int class_index,Klass * k)264 void ConstantPool::klass_at_put(int class_index, Klass* k) {
265 assert(k != NULL, "must be valid klass");
266 CPKlassSlot kslot = klass_slot_at(class_index);
267 int resolved_klass_index = kslot.resolved_klass_index();
268 Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
269 Atomic::release_store(adr, k);
270
271 // The interpreter assumes when the tag is stored, the klass is resolved
272 // and the Klass* non-NULL, so we need hardware store ordering here.
273 release_tag_at_put(class_index, JVM_CONSTANT_Class);
274 }
275
276 #if INCLUDE_CDS_JAVA_HEAP
277 // Archive the resolved references
archive_resolved_references(Thread * THREAD)278 void ConstantPool::archive_resolved_references(Thread* THREAD) {
279 if (_cache == NULL) {
280 return; // nothing to do
281 }
282
283 InstanceKlass *ik = pool_holder();
284 if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
285 ik->is_shared_app_class())) {
286 // Archiving resolved references for classes from non-builtin loaders
287 // is not yet supported.
288 return;
289 }
290
291 objArrayOop rr = resolved_references();
292 Array<u2>* ref_map = reference_map();
293 if (rr != NULL) {
294 int ref_map_len = ref_map == NULL ? 0 : ref_map->length();
295 int rr_len = rr->length();
296 for (int i = 0; i < rr_len; i++) {
297 oop obj = rr->obj_at(i);
298 rr->obj_at_put(i, NULL);
299 if (obj != NULL && i < ref_map_len) {
300 int index = object_to_cp_index(i);
301 if (tag_at(index).is_string()) {
302 oop archived_string = HeapShared::find_archived_heap_object(obj);
303 // Update the reference to point to the archived copy
304 // of this string.
305 // If the string is too large to archive, NULL is
306 // stored into rr. At run time, string_at_impl() will create and intern
307 // the string.
308 rr->obj_at_put(i, archived_string);
309 }
310 }
311 }
312
313 oop archived = HeapShared::archive_heap_object(rr, THREAD);
314 // If the resolved references array is not archived (too large),
315 // the 'archived' object is NULL. No need to explicitly check
316 // the return value of archive_heap_object here. At runtime, the
317 // resolved references will be created using the normal process
318 // when there is no archived value.
319 _cache->set_archived_references(archived);
320 }
321 }
322
resolve_class_constants(TRAPS)323 void ConstantPool::resolve_class_constants(TRAPS) {
324 assert(DumpSharedSpaces, "used during dump time only");
325 // The _cache may be NULL if the _pool_holder klass fails verification
326 // at dump time due to missing dependencies.
327 if (cache() == NULL || reference_map() == NULL) {
328 return; // nothing to do
329 }
330
331 constantPoolHandle cp(THREAD, this);
332 for (int index = 1; index < length(); index++) { // Index 0 is unused
333 if (tag_at(index).is_string() && !cp->is_pseudo_string_at(index)) {
334 int cache_index = cp->cp_to_object_index(index);
335 string_at_impl(cp, index, cache_index, CHECK);
336 }
337 }
338 }
339
add_dumped_interned_strings()340 void ConstantPool::add_dumped_interned_strings() {
341 objArrayOop rr = resolved_references();
342 if (rr != NULL) {
343 int rr_len = rr->length();
344 for (int i = 0; i < rr_len; i++) {
345 oop p = rr->obj_at(i);
346 if (java_lang_String::is_instance(p)) {
347 HeapShared::add_to_dumped_interned_strings(p);
348 }
349 }
350 }
351 }
352 #endif
353
354 // CDS support. Create a new resolved_references array.
restore_unshareable_info(TRAPS)355 void ConstantPool::restore_unshareable_info(TRAPS) {
356 assert(is_constantPool(), "ensure C++ vtable is restored");
357 assert(on_stack(), "should always be set for shared constant pools");
358 assert(is_shared(), "should always be set for shared constant pools");
359 assert(_cache != NULL, "constant pool _cache should not be NULL");
360
361 // Only create the new resolved references array if it hasn't been attempted before
362 if (resolved_references() != NULL) return;
363
364 // restore the C++ vtable from the shared archive
365 restore_vtable();
366
367 if (SystemDictionary::Object_klass_loaded()) {
368 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
369 #if INCLUDE_CDS_JAVA_HEAP
370 if (HeapShared::open_archive_heap_region_mapped() &&
371 _cache->archived_references() != NULL) {
372 oop archived = _cache->archived_references();
373 // Create handle for the archived resolved reference array object
374 Handle refs_handle(THREAD, archived);
375 set_resolved_references(loader_data->add_handle(refs_handle));
376 _cache->clear_archived_references();
377 } else
378 #endif
379 {
380 // No mapped archived resolved reference array
381 // Recreate the object array and add to ClassLoaderData.
382 int map_length = resolved_reference_length();
383 if (map_length > 0) {
384 objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
385 Handle refs_handle(THREAD, (oop)stom); // must handleize.
386 set_resolved_references(loader_data->add_handle(refs_handle));
387 }
388 }
389 }
390 }
391
remove_unshareable_info()392 void ConstantPool::remove_unshareable_info() {
393 // Resolved references are not in the shared archive.
394 // Save the length for restoration. It is not necessarily the same length
395 // as reference_map.length() if invokedynamic is saved. It is needed when
396 // re-creating the resolved reference array if archived heap data cannot be map
397 // at runtime.
398 set_resolved_reference_length(
399 resolved_references() != NULL ? resolved_references()->length() : 0);
400 set_resolved_references(OopHandle());
401
402 // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
403 // The _on_stack flag is used to prevent ConstantPools from deallocation during
404 // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
405 // we always set _on_stack to true to avoid having to change _flags during runtime.
406 _flags |= (_on_stack | _is_shared);
407 int num_klasses = 0;
408 for (int index = 1; index < length(); index++) { // Index 0 is unused
409 if (tag_at(index).is_unresolved_klass_in_error()) {
410 tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
411 } else if (tag_at(index).is_method_handle_in_error()) {
412 tag_at_put(index, JVM_CONSTANT_MethodHandle);
413 } else if (tag_at(index).is_method_type_in_error()) {
414 tag_at_put(index, JVM_CONSTANT_MethodType);
415 } else if (tag_at(index).is_dynamic_constant_in_error()) {
416 tag_at_put(index, JVM_CONSTANT_Dynamic);
417 }
418 if (tag_at(index).is_klass()) {
419 // This class was resolved as a side effect of executing Java code
420 // during dump time. We need to restore it back to an UnresolvedClass,
421 // so that the proper class loading and initialization can happen
422 // at runtime.
423 bool clear_it = true;
424 if (pool_holder()->is_hidden() && index == pool_holder()->this_class_index()) {
425 // All references to a hidden class's own field/methods are through this
426 // index. We cannot clear it. See comments in ClassFileParser::fill_instance_klass.
427 clear_it = false;
428 }
429 if (clear_it) {
430 CPKlassSlot kslot = klass_slot_at(index);
431 int resolved_klass_index = kslot.resolved_klass_index();
432 int name_index = kslot.name_index();
433 assert(tag_at(name_index).is_symbol(), "sanity");
434 resolved_klasses()->at_put(resolved_klass_index, NULL);
435 tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
436 assert(klass_name_at(index) == symbol_at(name_index), "sanity");
437 }
438 }
439 }
440 if (cache() != NULL) {
441 cache()->remove_unshareable_info();
442 }
443 }
444
cp_to_object_index(int cp_index)445 int ConstantPool::cp_to_object_index(int cp_index) {
446 // this is harder don't do this so much.
447 int i = reference_map()->find(cp_index);
448 // We might not find the index for jsr292 call.
449 return (i < 0) ? _no_index_sentinel : i;
450 }
451
string_at_put(int which,int obj_index,oop str)452 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
453 resolved_references()->obj_at_put(obj_index, str);
454 }
455
trace_class_resolution(const constantPoolHandle & this_cp,Klass * k)456 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
457 ResourceMark rm;
458 int line_number = -1;
459 const char * source_file = NULL;
460 if (JavaThread::current()->has_last_Java_frame()) {
461 // try to identify the method which called this function.
462 vframeStream vfst(JavaThread::current());
463 if (!vfst.at_end()) {
464 line_number = vfst.method()->line_number_from_bci(vfst.bci());
465 Symbol* s = vfst.method()->method_holder()->source_file_name();
466 if (s != NULL) {
467 source_file = s->as_C_string();
468 }
469 }
470 }
471 if (k != this_cp->pool_holder()) {
472 // only print something if the classes are different
473 if (source_file != NULL) {
474 log_debug(class, resolve)("%s %s %s:%d",
475 this_cp->pool_holder()->external_name(),
476 k->external_name(), source_file, line_number);
477 } else {
478 log_debug(class, resolve)("%s %s",
479 this_cp->pool_holder()->external_name(),
480 k->external_name());
481 }
482 }
483 }
484
klass_at_impl(const constantPoolHandle & this_cp,int which,bool save_resolution_error,TRAPS)485 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
486 bool save_resolution_error, TRAPS) {
487 JavaThread* javaThread = THREAD->as_Java_thread();
488
489 // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
490 // It is not safe to rely on the tag bit's here, since we don't have a lock, and
491 // the entry and tag is not updated atomicly.
492 CPKlassSlot kslot = this_cp->klass_slot_at(which);
493 int resolved_klass_index = kslot.resolved_klass_index();
494 int name_index = kslot.name_index();
495 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
496
497 Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
498 if (klass != NULL) {
499 return klass;
500 }
501
502 // This tag doesn't change back to unresolved class unless at a safepoint.
503 if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
504 // The original attempt to resolve this constant pool entry failed so find the
505 // class of the original error and throw another error of the same class
506 // (JVMS 5.4.3).
507 // If there is a detail message, pass that detail message to the error.
508 // The JVMS does not strictly require us to duplicate the same detail message,
509 // or any internal exception fields such as cause or stacktrace. But since the
510 // detail message is often a class name or other literal string, we will repeat it
511 // if we can find it in the symbol table.
512 throw_resolution_error(this_cp, which, CHECK_NULL);
513 ShouldNotReachHere();
514 }
515
516 Handle mirror_handle;
517 Symbol* name = this_cp->symbol_at(name_index);
518 Handle loader (THREAD, this_cp->pool_holder()->class_loader());
519 Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
520
521 Klass* k;
522 {
523 // Turn off the single stepping while doing class resolution
524 JvmtiHideSingleStepping jhss(javaThread);
525 k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
526 } // JvmtiHideSingleStepping jhss(javaThread);
527
528 if (!HAS_PENDING_EXCEPTION) {
529 // preserve the resolved klass from unloading
530 mirror_handle = Handle(THREAD, k->java_mirror());
531 // Do access check for klasses
532 verify_constant_pool_resolve(this_cp, k, THREAD);
533 }
534
535 // Failed to resolve class. We must record the errors so that subsequent attempts
536 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
537 if (HAS_PENDING_EXCEPTION) {
538 if (save_resolution_error) {
539 save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
540 // If CHECK_NULL above doesn't return the exception, that means that
541 // some other thread has beaten us and has resolved the class.
542 // To preserve old behavior, we return the resolved class.
543 klass = this_cp->resolved_klasses()->at(resolved_klass_index);
544 assert(klass != NULL, "must be resolved if exception was cleared");
545 return klass;
546 } else {
547 return NULL; // return the pending exception
548 }
549 }
550
551 // logging for class+resolve.
552 if (log_is_enabled(Debug, class, resolve)){
553 trace_class_resolution(this_cp, k);
554 }
555 Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
556 Atomic::release_store(adr, k);
557 // The interpreter assumes when the tag is stored, the klass is resolved
558 // and the Klass* stored in _resolved_klasses is non-NULL, so we need
559 // hardware store ordering here.
560 this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
561 return k;
562 }
563
564
565 // Does not update ConstantPool* - to avoid any exception throwing. Used
566 // by compiler and exception handling. Also used to avoid classloads for
567 // instanceof operations. Returns NULL if the class has not been loaded or
568 // if the verification of constant pool failed
klass_at_if_loaded(const constantPoolHandle & this_cp,int which)569 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
570 CPKlassSlot kslot = this_cp->klass_slot_at(which);
571 int resolved_klass_index = kslot.resolved_klass_index();
572 int name_index = kslot.name_index();
573 assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
574
575 Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
576 if (k != NULL) {
577 return k;
578 } else {
579 Thread *thread = Thread::current();
580 Symbol* name = this_cp->symbol_at(name_index);
581 oop loader = this_cp->pool_holder()->class_loader();
582 oop protection_domain = this_cp->pool_holder()->protection_domain();
583 Handle h_prot (thread, protection_domain);
584 Handle h_loader (thread, loader);
585 Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
586
587 // Avoid constant pool verification at a safepoint, which takes the Module_lock.
588 if (k != NULL && !SafepointSynchronize::is_at_safepoint()) {
589 // Make sure that resolving is legal
590 EXCEPTION_MARK;
591 // return NULL if verification fails
592 verify_constant_pool_resolve(this_cp, k, THREAD);
593 if (HAS_PENDING_EXCEPTION) {
594 CLEAR_PENDING_EXCEPTION;
595 return NULL;
596 }
597 return k;
598 } else {
599 return k;
600 }
601 }
602 }
603
method_at_if_loaded(const constantPoolHandle & cpool,int which)604 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool,
605 int which) {
606 if (cpool->cache() == NULL) return NULL; // nothing to load yet
607 int cache_index = decode_cpcache_index(which, true);
608 if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
609 // FIXME: should be an assert
610 log_debug(class, resolve)("bad operand %d in:", which); cpool->print();
611 return NULL;
612 }
613 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
614 return e->method_if_resolved(cpool);
615 }
616
617
has_appendix_at_if_loaded(const constantPoolHandle & cpool,int which)618 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
619 if (cpool->cache() == NULL) return false; // nothing to load yet
620 int cache_index = decode_cpcache_index(which, true);
621 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
622 return e->has_appendix();
623 }
624
appendix_at_if_loaded(const constantPoolHandle & cpool,int which)625 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
626 if (cpool->cache() == NULL) return NULL; // nothing to load yet
627 int cache_index = decode_cpcache_index(which, true);
628 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
629 return e->appendix_if_resolved(cpool);
630 }
631
632
has_local_signature_at_if_loaded(const constantPoolHandle & cpool,int which)633 bool ConstantPool::has_local_signature_at_if_loaded(const constantPoolHandle& cpool, int which) {
634 if (cpool->cache() == NULL) return false; // nothing to load yet
635 int cache_index = decode_cpcache_index(which, true);
636 ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
637 return e->has_local_signature();
638 }
639
impl_name_ref_at(int which,bool uncached)640 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
641 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
642 return symbol_at(name_index);
643 }
644
645
impl_signature_ref_at(int which,bool uncached)646 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
647 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
648 return symbol_at(signature_index);
649 }
650
impl_name_and_type_ref_index_at(int which,bool uncached)651 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
652 int i = which;
653 if (!uncached && cache() != NULL) {
654 if (ConstantPool::is_invokedynamic_index(which)) {
655 // Invokedynamic index is index into the constant pool cache
656 int pool_index = invokedynamic_bootstrap_ref_index_at(which);
657 pool_index = bootstrap_name_and_type_ref_index_at(pool_index);
658 assert(tag_at(pool_index).is_name_and_type(), "");
659 return pool_index;
660 }
661 // change byte-ordering and go via cache
662 i = remap_instruction_operand_from_cache(which);
663 } else {
664 if (tag_at(which).has_bootstrap()) {
665 int pool_index = bootstrap_name_and_type_ref_index_at(which);
666 assert(tag_at(pool_index).is_name_and_type(), "");
667 return pool_index;
668 }
669 }
670 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
671 assert(!tag_at(i).has_bootstrap(), "Must be handled above");
672 jint ref_index = *int_at_addr(i);
673 return extract_high_short_from_int(ref_index);
674 }
675
impl_tag_ref_at(int which,bool uncached)676 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) {
677 int pool_index = which;
678 if (!uncached && cache() != NULL) {
679 if (ConstantPool::is_invokedynamic_index(which)) {
680 // Invokedynamic index is index into resolved_references
681 pool_index = invokedynamic_bootstrap_ref_index_at(which);
682 } else {
683 // change byte-ordering and go via cache
684 pool_index = remap_instruction_operand_from_cache(which);
685 }
686 }
687 return tag_at(pool_index);
688 }
689
impl_klass_ref_index_at(int which,bool uncached)690 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
691 guarantee(!ConstantPool::is_invokedynamic_index(which),
692 "an invokedynamic instruction does not have a klass");
693 int i = which;
694 if (!uncached && cache() != NULL) {
695 // change byte-ordering and go via cache
696 i = remap_instruction_operand_from_cache(which);
697 }
698 assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
699 jint ref_index = *int_at_addr(i);
700 return extract_low_short_from_int(ref_index);
701 }
702
703
704
remap_instruction_operand_from_cache(int operand)705 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
706 int cpc_index = operand;
707 DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
708 assert((int)(u2)cpc_index == cpc_index, "clean u2");
709 int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
710 return member_index;
711 }
712
713
verify_constant_pool_resolve(const constantPoolHandle & this_cp,Klass * k,TRAPS)714 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) {
715 if (!(k->is_instance_klass() || k->is_objArray_klass())) {
716 return; // short cut, typeArray klass is always accessible
717 }
718 Klass* holder = this_cp->pool_holder();
719 LinkResolver::check_klass_accessibility(holder, k, CHECK);
720 }
721
722
name_ref_index_at(int which_nt)723 int ConstantPool::name_ref_index_at(int which_nt) {
724 jint ref_index = name_and_type_at(which_nt);
725 return extract_low_short_from_int(ref_index);
726 }
727
728
signature_ref_index_at(int which_nt)729 int ConstantPool::signature_ref_index_at(int which_nt) {
730 jint ref_index = name_and_type_at(which_nt);
731 return extract_high_short_from_int(ref_index);
732 }
733
734
klass_ref_at(int which,TRAPS)735 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
736 return klass_at(klass_ref_index_at(which), THREAD);
737 }
738
klass_name_at(int which) const739 Symbol* ConstantPool::klass_name_at(int which) const {
740 return symbol_at(klass_slot_at(which).name_index());
741 }
742
klass_ref_at_noresolve(int which)743 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
744 jint ref_index = klass_ref_index_at(which);
745 return klass_at_noresolve(ref_index);
746 }
747
uncached_klass_ref_at_noresolve(int which)748 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
749 jint ref_index = uncached_klass_ref_index_at(which);
750 return klass_at_noresolve(ref_index);
751 }
752
string_at_noresolve(int which)753 char* ConstantPool::string_at_noresolve(int which) {
754 return unresolved_string_at(which)->as_C_string();
755 }
756
basic_type_for_signature_at(int which) const757 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
758 return Signature::basic_type(symbol_at(which));
759 }
760
761
resolve_string_constants_impl(const constantPoolHandle & this_cp,TRAPS)762 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
763 for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
764 if (this_cp->tag_at(index).is_string()) {
765 this_cp->string_at(index, CHECK);
766 }
767 }
768 }
769
exception_message(const constantPoolHandle & this_cp,int which,constantTag tag,oop pending_exception)770 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
771 // Dig out the detailed message to reuse if possible
772 Symbol* message = java_lang_Throwable::detail_message(pending_exception);
773 if (message != NULL) {
774 return message;
775 }
776
777 // Return specific message for the tag
778 switch (tag.value()) {
779 case JVM_CONSTANT_UnresolvedClass:
780 // return the class name in the error message
781 message = this_cp->klass_name_at(which);
782 break;
783 case JVM_CONSTANT_MethodHandle:
784 // return the method handle name in the error message
785 message = this_cp->method_handle_name_ref_at(which);
786 break;
787 case JVM_CONSTANT_MethodType:
788 // return the method type signature in the error message
789 message = this_cp->method_type_signature_at(which);
790 break;
791 case JVM_CONSTANT_Dynamic:
792 // return the name of the condy in the error message
793 message = this_cp->uncached_name_ref_at(which);
794 break;
795 default:
796 ShouldNotReachHere();
797 }
798
799 return message;
800 }
801
throw_resolution_error(const constantPoolHandle & this_cp,int which,TRAPS)802 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
803 Symbol* message = NULL;
804 Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message);
805 assert(error != NULL, "checking");
806 CLEAR_PENDING_EXCEPTION;
807 if (message != NULL) {
808 ResourceMark rm;
809 THROW_MSG(error, message->as_C_string());
810 } else {
811 THROW(error);
812 }
813 }
814
815 // If resolution for Class, Dynamic constant, MethodHandle or MethodType fails, save the
816 // exception in the resolution error table, so that the same exception is thrown again.
save_and_throw_exception(const constantPoolHandle & this_cp,int which,constantTag tag,TRAPS)817 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which,
818 constantTag tag, TRAPS) {
819 Symbol* error = PENDING_EXCEPTION->klass()->name();
820
821 int error_tag = tag.error_value();
822
823 if (!PENDING_EXCEPTION->
824 is_a(SystemDictionary::LinkageError_klass())) {
825 // Just throw the exception and don't prevent these classes from
826 // being loaded due to virtual machine errors like StackOverflow
827 // and OutOfMemoryError, etc, or if the thread was hit by stop()
828 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
829 } else if (this_cp->tag_at(which).value() != error_tag) {
830 Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
831 SystemDictionary::add_resolution_error(this_cp, which, error, message);
832 // CAS in the tag. If a thread beat us to registering this error that's fine.
833 // If another thread resolved the reference, this is a race condition. This
834 // thread may have had a security manager or something temporary.
835 // This doesn't deterministically get an error. So why do we save this?
836 // We save this because jvmti can add classes to the bootclass path after
837 // this error, so it needs to get the same error if the error is first.
838 jbyte old_tag = Atomic::cmpxchg((jbyte*)this_cp->tag_addr_at(which),
839 (jbyte)tag.value(),
840 (jbyte)error_tag);
841 if (old_tag != error_tag && old_tag != tag.value()) {
842 // MethodHandles and MethodType doesn't change to resolved version.
843 assert(this_cp->tag_at(which).is_klass(), "Wrong tag value");
844 // Forget the exception and use the resolved class.
845 CLEAR_PENDING_EXCEPTION;
846 }
847 } else {
848 // some other thread put this in error state
849 throw_resolution_error(this_cp, which, CHECK);
850 }
851 }
852
constant_tag_at(int which)853 constantTag ConstantPool::constant_tag_at(int which) {
854 constantTag tag = tag_at(which);
855 if (tag.is_dynamic_constant() ||
856 tag.is_dynamic_constant_in_error()) {
857 BasicType bt = basic_type_for_constant_at(which);
858 // dynamic constant could return an array, treat as object
859 return constantTag::ofBasicType(is_reference_type(bt) ? T_OBJECT : bt);
860 }
861 return tag;
862 }
863
basic_type_for_constant_at(int which)864 BasicType ConstantPool::basic_type_for_constant_at(int which) {
865 constantTag tag = tag_at(which);
866 if (tag.is_dynamic_constant() ||
867 tag.is_dynamic_constant_in_error()) {
868 // have to look at the signature for this one
869 Symbol* constant_type = uncached_signature_ref_at(which);
870 return Signature::basic_type(constant_type);
871 }
872 return tag.basic_type();
873 }
874
875 // Called to resolve constants in the constant pool and return an oop.
876 // Some constant pool entries cache their resolved oop. This is also
877 // called to create oops from constants to use in arguments for invokedynamic
resolve_constant_at_impl(const constantPoolHandle & this_cp,int index,int cache_index,bool * status_return,TRAPS)878 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp,
879 int index, int cache_index,
880 bool* status_return, TRAPS) {
881 oop result_oop = NULL;
882 Handle throw_exception;
883
884 if (cache_index == _possible_index_sentinel) {
885 // It is possible that this constant is one which is cached in the objects.
886 // We'll do a linear search. This should be OK because this usage is rare.
887 // FIXME: If bootstrap specifiers stress this code, consider putting in
888 // a reverse index. Binary search over a short array should do it.
889 assert(index > 0, "valid index");
890 cache_index = this_cp->cp_to_object_index(index);
891 }
892 assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
893 assert(index == _no_index_sentinel || index >= 0, "");
894
895 if (cache_index >= 0) {
896 result_oop = this_cp->resolved_references()->obj_at(cache_index);
897 if (result_oop != NULL) {
898 if (result_oop == Universe::the_null_sentinel()) {
899 DEBUG_ONLY(int temp_index = (index >= 0 ? index : this_cp->object_to_cp_index(cache_index)));
900 assert(this_cp->tag_at(temp_index).is_dynamic_constant(), "only condy uses the null sentinel");
901 result_oop = NULL;
902 }
903 if (status_return != NULL) (*status_return) = true;
904 return result_oop;
905 // That was easy...
906 }
907 index = this_cp->object_to_cp_index(cache_index);
908 }
909
910 jvalue prim_value; // temp used only in a few cases below
911
912 constantTag tag = this_cp->tag_at(index);
913
914 if (status_return != NULL) {
915 // don't trigger resolution if the constant might need it
916 switch (tag.value()) {
917 case JVM_CONSTANT_Class:
918 {
919 CPKlassSlot kslot = this_cp->klass_slot_at(index);
920 int resolved_klass_index = kslot.resolved_klass_index();
921 if (this_cp->resolved_klasses()->at(resolved_klass_index) == NULL) {
922 (*status_return) = false;
923 return NULL;
924 }
925 // the klass is waiting in the CP; go get it
926 break;
927 }
928 case JVM_CONSTANT_String:
929 case JVM_CONSTANT_Integer:
930 case JVM_CONSTANT_Float:
931 case JVM_CONSTANT_Long:
932 case JVM_CONSTANT_Double:
933 // these guys trigger OOM at worst
934 break;
935 default:
936 (*status_return) = false;
937 return NULL;
938 }
939 // from now on there is either success or an OOME
940 (*status_return) = true;
941 }
942
943 switch (tag.value()) {
944
945 case JVM_CONSTANT_UnresolvedClass:
946 case JVM_CONSTANT_UnresolvedClassInError:
947 case JVM_CONSTANT_Class:
948 {
949 assert(cache_index == _no_index_sentinel, "should not have been set");
950 Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
951 // ldc wants the java mirror.
952 result_oop = resolved->java_mirror();
953 break;
954 }
955
956 case JVM_CONSTANT_Dynamic:
957 {
958 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop.
959 BootstrapInfo bootstrap_specifier(this_cp, index);
960
961 // The initial step in resolving an unresolved symbolic reference to a
962 // dynamically-computed constant is to resolve the symbolic reference to a
963 // method handle which will be the bootstrap method for the dynamically-computed
964 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap
965 // method fails, then a MethodHandleInError is stored at the corresponding
966 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to
967 // set a DynamicConstantInError here since any subsequent use of this
968 // bootstrap method will encounter the resolution of MethodHandleInError.
969 // Both the first, (resolution of the BSM and its static arguments), and the second tasks,
970 // (invocation of the BSM), of JVMS Section 5.4.3.6 occur within invoke_bootstrap_method()
971 // for the bootstrap_specifier created above.
972 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
973 Exceptions::wrap_dynamic_exception(/* is_indy */ false, THREAD);
974 if (HAS_PENDING_EXCEPTION) {
975 // Resolution failure of the dynamically-computed constant, save_and_throw_exception
976 // will check for a LinkageError and store a DynamicConstantInError.
977 save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
978 }
979 result_oop = bootstrap_specifier.resolved_value()();
980 BasicType type = Signature::basic_type(bootstrap_specifier.signature());
981 if (!is_reference_type(type)) {
982 // Make sure the primitive value is properly boxed.
983 // This is a JDK responsibility.
984 const char* fail = NULL;
985 if (result_oop == NULL) {
986 fail = "null result instead of box";
987 } else if (!is_java_primitive(type)) {
988 // FIXME: support value types via unboxing
989 fail = "can only handle references and primitives";
990 } else if (!java_lang_boxing_object::is_instance(result_oop, type)) {
991 fail = "primitive is not properly boxed";
992 }
993 if (fail != NULL) {
994 // Since this exception is not a LinkageError, throw exception
995 // but do not save a DynamicInError resolution result.
996 // See section 5.4.3 of the VM spec.
997 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail);
998 }
999 }
1000
1001 LogTarget(Debug, methodhandles, condy) lt_condy;
1002 if (lt_condy.is_enabled()) {
1003 LogStream ls(lt_condy);
1004 bootstrap_specifier.print_msg_on(&ls, "resolve_constant_at_impl");
1005 }
1006 break;
1007 }
1008
1009 case JVM_CONSTANT_String:
1010 assert(cache_index != _no_index_sentinel, "should have been set");
1011 if (this_cp->is_pseudo_string_at(index)) {
1012 result_oop = this_cp->pseudo_string_at(index, cache_index);
1013 break;
1014 }
1015 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL);
1016 break;
1017
1018 case JVM_CONSTANT_DynamicInError:
1019 case JVM_CONSTANT_MethodHandleInError:
1020 case JVM_CONSTANT_MethodTypeInError:
1021 {
1022 throw_resolution_error(this_cp, index, CHECK_NULL);
1023 break;
1024 }
1025
1026 case JVM_CONSTANT_MethodHandle:
1027 {
1028 int ref_kind = this_cp->method_handle_ref_kind_at(index);
1029 int callee_index = this_cp->method_handle_klass_index_at(index);
1030 Symbol* name = this_cp->method_handle_name_ref_at(index);
1031 Symbol* signature = this_cp->method_handle_signature_ref_at(index);
1032 constantTag m_tag = this_cp->tag_at(this_cp->method_handle_index_at(index));
1033 { ResourceMark rm(THREAD);
1034 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
1035 ref_kind, index, this_cp->method_handle_index_at(index),
1036 callee_index, name->as_C_string(), signature->as_C_string());
1037 }
1038
1039 Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL);
1040
1041 // Check constant pool method consistency
1042 if ((callee->is_interface() && m_tag.is_method()) ||
1043 ((!callee->is_interface() && m_tag.is_interface_method()))) {
1044 ResourceMark rm(THREAD);
1045 stringStream ss;
1046 ss.print("Inconsistent constant pool data in classfile for class %s. "
1047 "Method '", callee->name()->as_C_string());
1048 signature->print_as_signature_external_return_type(&ss);
1049 ss.print(" %s(", name->as_C_string());
1050 signature->print_as_signature_external_parameters(&ss);
1051 ss.print(")' at index %d is %s and should be %s",
1052 index,
1053 callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
1054 callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
1055 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1056 }
1057
1058 Klass* klass = this_cp->pool_holder();
1059 Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
1060 callee, name, signature,
1061 THREAD);
1062 result_oop = value();
1063 if (HAS_PENDING_EXCEPTION) {
1064 save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
1065 }
1066 break;
1067 }
1068
1069 case JVM_CONSTANT_MethodType:
1070 {
1071 Symbol* signature = this_cp->method_type_signature_at(index);
1072 { ResourceMark rm(THREAD);
1073 log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
1074 index, this_cp->method_type_index_at(index),
1075 signature->as_C_string());
1076 }
1077 Klass* klass = this_cp->pool_holder();
1078 Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
1079 result_oop = value();
1080 if (HAS_PENDING_EXCEPTION) {
1081 save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
1082 }
1083 break;
1084 }
1085
1086 case JVM_CONSTANT_Integer:
1087 assert(cache_index == _no_index_sentinel, "should not have been set");
1088 prim_value.i = this_cp->int_at(index);
1089 result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
1090 break;
1091
1092 case JVM_CONSTANT_Float:
1093 assert(cache_index == _no_index_sentinel, "should not have been set");
1094 prim_value.f = this_cp->float_at(index);
1095 result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
1096 break;
1097
1098 case JVM_CONSTANT_Long:
1099 assert(cache_index == _no_index_sentinel, "should not have been set");
1100 prim_value.j = this_cp->long_at(index);
1101 result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
1102 break;
1103
1104 case JVM_CONSTANT_Double:
1105 assert(cache_index == _no_index_sentinel, "should not have been set");
1106 prim_value.d = this_cp->double_at(index);
1107 result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
1108 break;
1109
1110 default:
1111 DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
1112 this_cp(), index, cache_index, tag.value()));
1113 assert(false, "unexpected constant tag");
1114 break;
1115 }
1116
1117 if (cache_index >= 0) {
1118 // Benign race condition: resolved_references may already be filled in.
1119 // The important thing here is that all threads pick up the same result.
1120 // It doesn't matter which racing thread wins, as long as only one
1121 // result is used by all threads, and all future queries.
1122 oop new_result = (result_oop == NULL ? Universe::the_null_sentinel() : result_oop);
1123 oop old_result = this_cp->resolved_references()
1124 ->atomic_compare_exchange_oop(cache_index, new_result, NULL);
1125 if (old_result == NULL) {
1126 return result_oop; // was installed
1127 } else {
1128 // Return the winning thread's result. This can be different than
1129 // the result here for MethodHandles.
1130 if (old_result == Universe::the_null_sentinel())
1131 old_result = NULL;
1132 return old_result;
1133 }
1134 } else {
1135 assert(result_oop != Universe::the_null_sentinel(), "");
1136 return result_oop;
1137 }
1138 }
1139
uncached_string_at(int which,TRAPS)1140 oop ConstantPool::uncached_string_at(int which, TRAPS) {
1141 Symbol* sym = unresolved_string_at(which);
1142 oop str = StringTable::intern(sym, CHECK_(NULL));
1143 assert(java_lang_String::is_instance(str), "must be string");
1144 return str;
1145 }
1146
copy_bootstrap_arguments_at_impl(const constantPoolHandle & this_cp,int index,int start_arg,int end_arg,objArrayHandle info,int pos,bool must_resolve,Handle if_not_available,TRAPS)1147 void ConstantPool::copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index,
1148 int start_arg, int end_arg,
1149 objArrayHandle info, int pos,
1150 bool must_resolve, Handle if_not_available,
1151 TRAPS) {
1152 int argc;
1153 int limit = pos + end_arg - start_arg;
1154 // checks: index in range [0..this_cp->length),
1155 // tag at index, start..end in range [0..argc],
1156 // info array non-null, pos..limit in [0..info.length]
1157 if ((0 >= index || index >= this_cp->length()) ||
1158 !(this_cp->tag_at(index).is_invoke_dynamic() ||
1159 this_cp->tag_at(index).is_dynamic_constant()) ||
1160 (0 > start_arg || start_arg > end_arg) ||
1161 (end_arg > (argc = this_cp->bootstrap_argument_count_at(index))) ||
1162 (0 > pos || pos > limit) ||
1163 (info.is_null() || limit > info->length())) {
1164 // An index or something else went wrong; throw an error.
1165 // Since this is an internal API, we don't expect this,
1166 // so we don't bother to craft a nice message.
1167 THROW_MSG(vmSymbols::java_lang_LinkageError(), "bad BSM argument access");
1168 }
1169 // now we can loop safely
1170 int info_i = pos;
1171 for (int i = start_arg; i < end_arg; i++) {
1172 int arg_index = this_cp->bootstrap_argument_index_at(index, i);
1173 oop arg_oop;
1174 if (must_resolve) {
1175 arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK);
1176 } else {
1177 bool found_it = false;
1178 arg_oop = this_cp->find_cached_constant_at(arg_index, found_it, CHECK);
1179 if (!found_it) arg_oop = if_not_available();
1180 }
1181 info->obj_at_put(info_i++, arg_oop);
1182 }
1183 }
1184
string_at_impl(const constantPoolHandle & this_cp,int which,int obj_index,TRAPS)1185 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) {
1186 // If the string has already been interned, this entry will be non-null
1187 oop str = this_cp->resolved_references()->obj_at(obj_index);
1188 assert(str != Universe::the_null_sentinel(), "");
1189 if (str != NULL) return str;
1190 Symbol* sym = this_cp->unresolved_string_at(which);
1191 str = StringTable::intern(sym, CHECK_(NULL));
1192 this_cp->string_at_put(which, obj_index, str);
1193 assert(java_lang_String::is_instance(str), "must be string");
1194 return str;
1195 }
1196
1197
klass_name_at_matches(const InstanceKlass * k,int which)1198 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
1199 // Names are interned, so we can compare Symbol*s directly
1200 Symbol* cp_name = klass_name_at(which);
1201 return (cp_name == k->name());
1202 }
1203
1204
1205 // Iterate over symbols and decrement ones which are Symbol*s
1206 // This is done during GC.
1207 // Only decrement the UTF8 symbols. Strings point to
1208 // these symbols but didn't increment the reference count.
unreference_symbols()1209 void ConstantPool::unreference_symbols() {
1210 for (int index = 1; index < length(); index++) { // Index 0 is unused
1211 constantTag tag = tag_at(index);
1212 if (tag.is_symbol()) {
1213 symbol_at(index)->decrement_refcount();
1214 }
1215 }
1216 }
1217
1218
1219 // Compare this constant pool's entry at index1 to the constant pool
1220 // cp2's entry at index2.
compare_entry_to(int index1,const constantPoolHandle & cp2,int index2,TRAPS)1221 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1222 int index2, TRAPS) {
1223
1224 // The error tags are equivalent to non-error tags when comparing
1225 jbyte t1 = tag_at(index1).non_error_value();
1226 jbyte t2 = cp2->tag_at(index2).non_error_value();
1227
1228 if (t1 != t2) {
1229 // Not the same entry type so there is nothing else to check. Note
1230 // that this style of checking will consider resolved/unresolved
1231 // class pairs as different.
1232 // From the ConstantPool* API point of view, this is correct
1233 // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1234 // plays out in the context of ConstantPool* merging.
1235 return false;
1236 }
1237
1238 switch (t1) {
1239 case JVM_CONSTANT_Class:
1240 {
1241 Klass* k1 = klass_at(index1, CHECK_false);
1242 Klass* k2 = cp2->klass_at(index2, CHECK_false);
1243 if (k1 == k2) {
1244 return true;
1245 }
1246 } break;
1247
1248 case JVM_CONSTANT_ClassIndex:
1249 {
1250 int recur1 = klass_index_at(index1);
1251 int recur2 = cp2->klass_index_at(index2);
1252 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1253 if (match) {
1254 return true;
1255 }
1256 } break;
1257
1258 case JVM_CONSTANT_Double:
1259 {
1260 jdouble d1 = double_at(index1);
1261 jdouble d2 = cp2->double_at(index2);
1262 if (d1 == d2) {
1263 return true;
1264 }
1265 } break;
1266
1267 case JVM_CONSTANT_Fieldref:
1268 case JVM_CONSTANT_InterfaceMethodref:
1269 case JVM_CONSTANT_Methodref:
1270 {
1271 int recur1 = uncached_klass_ref_index_at(index1);
1272 int recur2 = cp2->uncached_klass_ref_index_at(index2);
1273 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1274 if (match) {
1275 recur1 = uncached_name_and_type_ref_index_at(index1);
1276 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1277 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1278 if (match) {
1279 return true;
1280 }
1281 }
1282 } break;
1283
1284 case JVM_CONSTANT_Float:
1285 {
1286 jfloat f1 = float_at(index1);
1287 jfloat f2 = cp2->float_at(index2);
1288 if (f1 == f2) {
1289 return true;
1290 }
1291 } break;
1292
1293 case JVM_CONSTANT_Integer:
1294 {
1295 jint i1 = int_at(index1);
1296 jint i2 = cp2->int_at(index2);
1297 if (i1 == i2) {
1298 return true;
1299 }
1300 } break;
1301
1302 case JVM_CONSTANT_Long:
1303 {
1304 jlong l1 = long_at(index1);
1305 jlong l2 = cp2->long_at(index2);
1306 if (l1 == l2) {
1307 return true;
1308 }
1309 } break;
1310
1311 case JVM_CONSTANT_NameAndType:
1312 {
1313 int recur1 = name_ref_index_at(index1);
1314 int recur2 = cp2->name_ref_index_at(index2);
1315 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1316 if (match) {
1317 recur1 = signature_ref_index_at(index1);
1318 recur2 = cp2->signature_ref_index_at(index2);
1319 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1320 if (match) {
1321 return true;
1322 }
1323 }
1324 } break;
1325
1326 case JVM_CONSTANT_StringIndex:
1327 {
1328 int recur1 = string_index_at(index1);
1329 int recur2 = cp2->string_index_at(index2);
1330 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1331 if (match) {
1332 return true;
1333 }
1334 } break;
1335
1336 case JVM_CONSTANT_UnresolvedClass:
1337 {
1338 Symbol* k1 = klass_name_at(index1);
1339 Symbol* k2 = cp2->klass_name_at(index2);
1340 if (k1 == k2) {
1341 return true;
1342 }
1343 } break;
1344
1345 case JVM_CONSTANT_MethodType:
1346 {
1347 int k1 = method_type_index_at(index1);
1348 int k2 = cp2->method_type_index_at(index2);
1349 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1350 if (match) {
1351 return true;
1352 }
1353 } break;
1354
1355 case JVM_CONSTANT_MethodHandle:
1356 {
1357 int k1 = method_handle_ref_kind_at(index1);
1358 int k2 = cp2->method_handle_ref_kind_at(index2);
1359 if (k1 == k2) {
1360 int i1 = method_handle_index_at(index1);
1361 int i2 = cp2->method_handle_index_at(index2);
1362 bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1363 if (match) {
1364 return true;
1365 }
1366 }
1367 } break;
1368
1369 case JVM_CONSTANT_Dynamic:
1370 {
1371 int k1 = bootstrap_name_and_type_ref_index_at(index1);
1372 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2);
1373 int i1 = bootstrap_methods_attribute_index(index1);
1374 int i2 = cp2->bootstrap_methods_attribute_index(index2);
1375 // separate statements and variables because CHECK_false is used
1376 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1377 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1378 return (match_entry && match_operand);
1379 } break;
1380
1381 case JVM_CONSTANT_InvokeDynamic:
1382 {
1383 int k1 = bootstrap_name_and_type_ref_index_at(index1);
1384 int k2 = cp2->bootstrap_name_and_type_ref_index_at(index2);
1385 int i1 = bootstrap_methods_attribute_index(index1);
1386 int i2 = cp2->bootstrap_methods_attribute_index(index2);
1387 // separate statements and variables because CHECK_false is used
1388 bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1389 bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1390 return (match_entry && match_operand);
1391 } break;
1392
1393 case JVM_CONSTANT_String:
1394 {
1395 Symbol* s1 = unresolved_string_at(index1);
1396 Symbol* s2 = cp2->unresolved_string_at(index2);
1397 if (s1 == s2) {
1398 return true;
1399 }
1400 } break;
1401
1402 case JVM_CONSTANT_Utf8:
1403 {
1404 Symbol* s1 = symbol_at(index1);
1405 Symbol* s2 = cp2->symbol_at(index2);
1406 if (s1 == s2) {
1407 return true;
1408 }
1409 } break;
1410
1411 // Invalid is used as the tag for the second constant pool entry
1412 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1413 // not be seen by itself.
1414 case JVM_CONSTANT_Invalid: // fall through
1415
1416 default:
1417 ShouldNotReachHere();
1418 break;
1419 }
1420
1421 return false;
1422 } // end compare_entry_to()
1423
1424
1425 // Resize the operands array with delta_len and delta_size.
1426 // Used in RedefineClasses for CP merge.
resize_operands(int delta_len,int delta_size,TRAPS)1427 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1428 int old_len = operand_array_length(operands());
1429 int new_len = old_len + delta_len;
1430 int min_len = (delta_len > 0) ? old_len : new_len;
1431
1432 int old_size = operands()->length();
1433 int new_size = old_size + delta_size;
1434 int min_size = (delta_size > 0) ? old_size : new_size;
1435
1436 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1437 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1438
1439 // Set index in the resized array for existing elements only
1440 for (int idx = 0; idx < min_len; idx++) {
1441 int offset = operand_offset_at(idx); // offset in original array
1442 operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1443 }
1444 // Copy the bootstrap specifiers only
1445 Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1446 new_ops->adr_at(2*new_len),
1447 (min_size - 2*min_len) * sizeof(u2));
1448 // Explicitly deallocate old operands array.
1449 // Note, it is not needed for 7u backport.
1450 if ( operands() != NULL) { // the safety check
1451 MetadataFactory::free_array<u2>(loader_data, operands());
1452 }
1453 set_operands(new_ops);
1454 } // end resize_operands()
1455
1456
1457 // Extend the operands array with the length and size of the ext_cp operands.
1458 // Used in RedefineClasses for CP merge.
extend_operands(const constantPoolHandle & ext_cp,TRAPS)1459 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) {
1460 int delta_len = operand_array_length(ext_cp->operands());
1461 if (delta_len == 0) {
1462 return; // nothing to do
1463 }
1464 int delta_size = ext_cp->operands()->length();
1465
1466 assert(delta_len > 0 && delta_size > 0, "extended operands array must be bigger");
1467
1468 if (operand_array_length(operands()) == 0) {
1469 ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1470 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1471 // The first element index defines the offset of second part
1472 operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1473 set_operands(new_ops);
1474 } else {
1475 resize_operands(delta_len, delta_size, CHECK);
1476 }
1477
1478 } // end extend_operands()
1479
1480
1481 // Shrink the operands array to a smaller array with new_len length.
1482 // Used in RedefineClasses for CP merge.
shrink_operands(int new_len,TRAPS)1483 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1484 int old_len = operand_array_length(operands());
1485 if (new_len == old_len) {
1486 return; // nothing to do
1487 }
1488 assert(new_len < old_len, "shrunken operands array must be smaller");
1489
1490 int free_base = operand_next_offset_at(new_len - 1);
1491 int delta_len = new_len - old_len;
1492 int delta_size = 2*delta_len + free_base - operands()->length();
1493
1494 resize_operands(delta_len, delta_size, CHECK);
1495
1496 } // end shrink_operands()
1497
1498
copy_operands(const constantPoolHandle & from_cp,const constantPoolHandle & to_cp,TRAPS)1499 void ConstantPool::copy_operands(const constantPoolHandle& from_cp,
1500 const constantPoolHandle& to_cp,
1501 TRAPS) {
1502
1503 int from_oplen = operand_array_length(from_cp->operands());
1504 int old_oplen = operand_array_length(to_cp->operands());
1505 if (from_oplen != 0) {
1506 ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1507 // append my operands to the target's operands array
1508 if (old_oplen == 0) {
1509 // Can't just reuse from_cp's operand list because of deallocation issues
1510 int len = from_cp->operands()->length();
1511 Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1512 Copy::conjoint_memory_atomic(
1513 from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1514 to_cp->set_operands(new_ops);
1515 } else {
1516 int old_len = to_cp->operands()->length();
1517 int from_len = from_cp->operands()->length();
1518 int old_off = old_oplen * sizeof(u2);
1519 int from_off = from_oplen * sizeof(u2);
1520 // Use the metaspace for the destination constant pool
1521 Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1522 int fillp = 0, len = 0;
1523 // first part of dest
1524 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1525 new_operands->adr_at(fillp),
1526 (len = old_off) * sizeof(u2));
1527 fillp += len;
1528 // first part of src
1529 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1530 new_operands->adr_at(fillp),
1531 (len = from_off) * sizeof(u2));
1532 fillp += len;
1533 // second part of dest
1534 Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1535 new_operands->adr_at(fillp),
1536 (len = old_len - old_off) * sizeof(u2));
1537 fillp += len;
1538 // second part of src
1539 Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1540 new_operands->adr_at(fillp),
1541 (len = from_len - from_off) * sizeof(u2));
1542 fillp += len;
1543 assert(fillp == new_operands->length(), "");
1544
1545 // Adjust indexes in the first part of the copied operands array.
1546 for (int j = 0; j < from_oplen; j++) {
1547 int offset = operand_offset_at(new_operands, old_oplen + j);
1548 assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1549 offset += old_len; // every new tuple is preceded by old_len extra u2's
1550 operand_offset_at_put(new_operands, old_oplen + j, offset);
1551 }
1552
1553 // replace target operands array with combined array
1554 to_cp->set_operands(new_operands);
1555 }
1556 }
1557 } // end copy_operands()
1558
1559
1560 // Copy this constant pool's entries at start_i to end_i (inclusive)
1561 // to the constant pool to_cp's entries starting at to_i. A total of
1562 // (end_i - start_i) + 1 entries are copied.
copy_cp_to_impl(const constantPoolHandle & from_cp,int start_i,int end_i,const constantPoolHandle & to_cp,int to_i,TRAPS)1563 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i,
1564 const constantPoolHandle& to_cp, int to_i, TRAPS) {
1565
1566
1567 int dest_i = to_i; // leave original alone for debug purposes
1568
1569 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1570 copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1571
1572 switch (from_cp->tag_at(src_i).value()) {
1573 case JVM_CONSTANT_Double:
1574 case JVM_CONSTANT_Long:
1575 // double and long take two constant pool entries
1576 src_i += 2;
1577 dest_i += 2;
1578 break;
1579
1580 default:
1581 // all others take one constant pool entry
1582 src_i++;
1583 dest_i++;
1584 break;
1585 }
1586 }
1587 copy_operands(from_cp, to_cp, CHECK);
1588
1589 } // end copy_cp_to_impl()
1590
1591
1592 // Copy this constant pool's entry at from_i to the constant pool
1593 // to_cp's entry at to_i.
copy_entry_to(const constantPoolHandle & from_cp,int from_i,const constantPoolHandle & to_cp,int to_i,TRAPS)1594 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1595 const constantPoolHandle& to_cp, int to_i,
1596 TRAPS) {
1597
1598 int tag = from_cp->tag_at(from_i).value();
1599 switch (tag) {
1600 case JVM_CONSTANT_ClassIndex:
1601 {
1602 jint ki = from_cp->klass_index_at(from_i);
1603 to_cp->klass_index_at_put(to_i, ki);
1604 } break;
1605
1606 case JVM_CONSTANT_Double:
1607 {
1608 jdouble d = from_cp->double_at(from_i);
1609 to_cp->double_at_put(to_i, d);
1610 // double takes two constant pool entries so init second entry's tag
1611 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1612 } break;
1613
1614 case JVM_CONSTANT_Fieldref:
1615 {
1616 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1617 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1618 to_cp->field_at_put(to_i, class_index, name_and_type_index);
1619 } break;
1620
1621 case JVM_CONSTANT_Float:
1622 {
1623 jfloat f = from_cp->float_at(from_i);
1624 to_cp->float_at_put(to_i, f);
1625 } break;
1626
1627 case JVM_CONSTANT_Integer:
1628 {
1629 jint i = from_cp->int_at(from_i);
1630 to_cp->int_at_put(to_i, i);
1631 } break;
1632
1633 case JVM_CONSTANT_InterfaceMethodref:
1634 {
1635 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1636 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1637 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1638 } break;
1639
1640 case JVM_CONSTANT_Long:
1641 {
1642 jlong l = from_cp->long_at(from_i);
1643 to_cp->long_at_put(to_i, l);
1644 // long takes two constant pool entries so init second entry's tag
1645 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1646 } break;
1647
1648 case JVM_CONSTANT_Methodref:
1649 {
1650 int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1651 int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1652 to_cp->method_at_put(to_i, class_index, name_and_type_index);
1653 } break;
1654
1655 case JVM_CONSTANT_NameAndType:
1656 {
1657 int name_ref_index = from_cp->name_ref_index_at(from_i);
1658 int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1659 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1660 } break;
1661
1662 case JVM_CONSTANT_StringIndex:
1663 {
1664 jint si = from_cp->string_index_at(from_i);
1665 to_cp->string_index_at_put(to_i, si);
1666 } break;
1667
1668 case JVM_CONSTANT_Class:
1669 case JVM_CONSTANT_UnresolvedClass:
1670 case JVM_CONSTANT_UnresolvedClassInError:
1671 {
1672 // Revert to JVM_CONSTANT_ClassIndex
1673 int name_index = from_cp->klass_slot_at(from_i).name_index();
1674 assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1675 to_cp->klass_index_at_put(to_i, name_index);
1676 } break;
1677
1678 case JVM_CONSTANT_String:
1679 {
1680 Symbol* s = from_cp->unresolved_string_at(from_i);
1681 to_cp->unresolved_string_at_put(to_i, s);
1682 } break;
1683
1684 case JVM_CONSTANT_Utf8:
1685 {
1686 Symbol* s = from_cp->symbol_at(from_i);
1687 // Need to increase refcount, the old one will be thrown away and deferenced
1688 s->increment_refcount();
1689 to_cp->symbol_at_put(to_i, s);
1690 } break;
1691
1692 case JVM_CONSTANT_MethodType:
1693 case JVM_CONSTANT_MethodTypeInError:
1694 {
1695 jint k = from_cp->method_type_index_at(from_i);
1696 to_cp->method_type_index_at_put(to_i, k);
1697 } break;
1698
1699 case JVM_CONSTANT_MethodHandle:
1700 case JVM_CONSTANT_MethodHandleInError:
1701 {
1702 int k1 = from_cp->method_handle_ref_kind_at(from_i);
1703 int k2 = from_cp->method_handle_index_at(from_i);
1704 to_cp->method_handle_index_at_put(to_i, k1, k2);
1705 } break;
1706
1707 case JVM_CONSTANT_Dynamic:
1708 case JVM_CONSTANT_DynamicInError:
1709 {
1710 int k1 = from_cp->bootstrap_methods_attribute_index(from_i);
1711 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i);
1712 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1713 to_cp->dynamic_constant_at_put(to_i, k1, k2);
1714 } break;
1715
1716 case JVM_CONSTANT_InvokeDynamic:
1717 {
1718 int k1 = from_cp->bootstrap_methods_attribute_index(from_i);
1719 int k2 = from_cp->bootstrap_name_and_type_ref_index_at(from_i);
1720 k1 += operand_array_length(to_cp->operands()); // to_cp might already have operands
1721 to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1722 } break;
1723
1724 // Invalid is used as the tag for the second constant pool entry
1725 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1726 // not be seen by itself.
1727 case JVM_CONSTANT_Invalid: // fall through
1728
1729 default:
1730 {
1731 ShouldNotReachHere();
1732 } break;
1733 }
1734 } // end copy_entry_to()
1735
1736 // Search constant pool search_cp for an entry that matches this
1737 // constant pool's entry at pattern_i. Returns the index of a
1738 // matching entry or zero (0) if there is no matching entry.
find_matching_entry(int pattern_i,const constantPoolHandle & search_cp,TRAPS)1739 int ConstantPool::find_matching_entry(int pattern_i,
1740 const constantPoolHandle& search_cp, TRAPS) {
1741
1742 // index zero (0) is not used
1743 for (int i = 1; i < search_cp->length(); i++) {
1744 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1745 if (found) {
1746 return i;
1747 }
1748 }
1749
1750 return 0; // entry not found; return unused index zero (0)
1751 } // end find_matching_entry()
1752
1753
1754 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1755 // cp2's bootstrap specifier at idx2.
compare_operand_to(int idx1,const constantPoolHandle & cp2,int idx2,TRAPS)1756 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) {
1757 int k1 = operand_bootstrap_method_ref_index_at(idx1);
1758 int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1759 bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1760
1761 if (!match) {
1762 return false;
1763 }
1764 int argc = operand_argument_count_at(idx1);
1765 if (argc == cp2->operand_argument_count_at(idx2)) {
1766 for (int j = 0; j < argc; j++) {
1767 k1 = operand_argument_index_at(idx1, j);
1768 k2 = cp2->operand_argument_index_at(idx2, j);
1769 match = compare_entry_to(k1, cp2, k2, CHECK_false);
1770 if (!match) {
1771 return false;
1772 }
1773 }
1774 return true; // got through loop; all elements equal
1775 }
1776 return false;
1777 } // end compare_operand_to()
1778
1779 // Search constant pool search_cp for a bootstrap specifier that matches
1780 // this constant pool's bootstrap specifier data at pattern_i index.
1781 // Return the index of a matching bootstrap attribute record or (-1) if there is no match.
find_matching_operand(int pattern_i,const constantPoolHandle & search_cp,int search_len,TRAPS)1782 int ConstantPool::find_matching_operand(int pattern_i,
1783 const constantPoolHandle& search_cp, int search_len, TRAPS) {
1784 for (int i = 0; i < search_len; i++) {
1785 bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1786 if (found) {
1787 return i;
1788 }
1789 }
1790 return -1; // bootstrap specifier data not found; return unused index (-1)
1791 } // end find_matching_operand()
1792
1793
1794 #ifndef PRODUCT
1795
printable_name_at(int which)1796 const char* ConstantPool::printable_name_at(int which) {
1797
1798 constantTag tag = tag_at(which);
1799
1800 if (tag.is_string()) {
1801 return string_at_noresolve(which);
1802 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1803 return klass_name_at(which)->as_C_string();
1804 } else if (tag.is_symbol()) {
1805 return symbol_at(which)->as_C_string();
1806 }
1807 return "";
1808 }
1809
1810 #endif // PRODUCT
1811
1812
1813 // JVMTI GetConstantPool support
1814
1815 // For debugging of constant pool
1816 const bool debug_cpool = false;
1817
1818 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1819
print_cpool_bytes(jint cnt,u1 * bytes)1820 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1821 const char* WARN_MSG = "Must not be such entry!";
1822 jint size = 0;
1823 u2 idx1, idx2;
1824
1825 for (jint idx = 1; idx < cnt; idx++) {
1826 jint ent_size = 0;
1827 u1 tag = *bytes++;
1828 size++; // count tag
1829
1830 printf("const #%03d, tag: %02d ", idx, tag);
1831 switch(tag) {
1832 case JVM_CONSTANT_Invalid: {
1833 printf("Invalid");
1834 break;
1835 }
1836 case JVM_CONSTANT_Unicode: {
1837 printf("Unicode %s", WARN_MSG);
1838 break;
1839 }
1840 case JVM_CONSTANT_Utf8: {
1841 u2 len = Bytes::get_Java_u2(bytes);
1842 char str[128];
1843 if (len > 127) {
1844 len = 127;
1845 }
1846 strncpy(str, (char *) (bytes+2), len);
1847 str[len] = '\0';
1848 printf("Utf8 \"%s\"", str);
1849 ent_size = 2 + len;
1850 break;
1851 }
1852 case JVM_CONSTANT_Integer: {
1853 u4 val = Bytes::get_Java_u4(bytes);
1854 printf("int %d", *(int *) &val);
1855 ent_size = 4;
1856 break;
1857 }
1858 case JVM_CONSTANT_Float: {
1859 u4 val = Bytes::get_Java_u4(bytes);
1860 printf("float %5.3ff", *(float *) &val);
1861 ent_size = 4;
1862 break;
1863 }
1864 case JVM_CONSTANT_Long: {
1865 u8 val = Bytes::get_Java_u8(bytes);
1866 printf("long " INT64_FORMAT, (int64_t) *(jlong *) &val);
1867 ent_size = 8;
1868 idx++; // Long takes two cpool slots
1869 break;
1870 }
1871 case JVM_CONSTANT_Double: {
1872 u8 val = Bytes::get_Java_u8(bytes);
1873 printf("double %5.3fd", *(jdouble *)&val);
1874 ent_size = 8;
1875 idx++; // Double takes two cpool slots
1876 break;
1877 }
1878 case JVM_CONSTANT_Class: {
1879 idx1 = Bytes::get_Java_u2(bytes);
1880 printf("class #%03d", idx1);
1881 ent_size = 2;
1882 break;
1883 }
1884 case JVM_CONSTANT_String: {
1885 idx1 = Bytes::get_Java_u2(bytes);
1886 printf("String #%03d", idx1);
1887 ent_size = 2;
1888 break;
1889 }
1890 case JVM_CONSTANT_Fieldref: {
1891 idx1 = Bytes::get_Java_u2(bytes);
1892 idx2 = Bytes::get_Java_u2(bytes+2);
1893 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
1894 ent_size = 4;
1895 break;
1896 }
1897 case JVM_CONSTANT_Methodref: {
1898 idx1 = Bytes::get_Java_u2(bytes);
1899 idx2 = Bytes::get_Java_u2(bytes+2);
1900 printf("Method #%03d, #%03d", idx1, idx2);
1901 ent_size = 4;
1902 break;
1903 }
1904 case JVM_CONSTANT_InterfaceMethodref: {
1905 idx1 = Bytes::get_Java_u2(bytes);
1906 idx2 = Bytes::get_Java_u2(bytes+2);
1907 printf("InterfMethod #%03d, #%03d", idx1, idx2);
1908 ent_size = 4;
1909 break;
1910 }
1911 case JVM_CONSTANT_NameAndType: {
1912 idx1 = Bytes::get_Java_u2(bytes);
1913 idx2 = Bytes::get_Java_u2(bytes+2);
1914 printf("NameAndType #%03d, #%03d", idx1, idx2);
1915 ent_size = 4;
1916 break;
1917 }
1918 case JVM_CONSTANT_ClassIndex: {
1919 printf("ClassIndex %s", WARN_MSG);
1920 break;
1921 }
1922 case JVM_CONSTANT_UnresolvedClass: {
1923 printf("UnresolvedClass: %s", WARN_MSG);
1924 break;
1925 }
1926 case JVM_CONSTANT_UnresolvedClassInError: {
1927 printf("UnresolvedClassInErr: %s", WARN_MSG);
1928 break;
1929 }
1930 case JVM_CONSTANT_StringIndex: {
1931 printf("StringIndex: %s", WARN_MSG);
1932 break;
1933 }
1934 }
1935 printf(";\n");
1936 bytes += ent_size;
1937 size += ent_size;
1938 }
1939 printf("Cpool size: %d\n", size);
1940 fflush(0);
1941 return;
1942 } /* end print_cpool_bytes */
1943
1944
1945 // Returns size of constant pool entry.
cpool_entry_size(jint idx)1946 jint ConstantPool::cpool_entry_size(jint idx) {
1947 switch(tag_at(idx).value()) {
1948 case JVM_CONSTANT_Invalid:
1949 case JVM_CONSTANT_Unicode:
1950 return 1;
1951
1952 case JVM_CONSTANT_Utf8:
1953 return 3 + symbol_at(idx)->utf8_length();
1954
1955 case JVM_CONSTANT_Class:
1956 case JVM_CONSTANT_String:
1957 case JVM_CONSTANT_ClassIndex:
1958 case JVM_CONSTANT_UnresolvedClass:
1959 case JVM_CONSTANT_UnresolvedClassInError:
1960 case JVM_CONSTANT_StringIndex:
1961 case JVM_CONSTANT_MethodType:
1962 case JVM_CONSTANT_MethodTypeInError:
1963 return 3;
1964
1965 case JVM_CONSTANT_MethodHandle:
1966 case JVM_CONSTANT_MethodHandleInError:
1967 return 4; //tag, ref_kind, ref_index
1968
1969 case JVM_CONSTANT_Integer:
1970 case JVM_CONSTANT_Float:
1971 case JVM_CONSTANT_Fieldref:
1972 case JVM_CONSTANT_Methodref:
1973 case JVM_CONSTANT_InterfaceMethodref:
1974 case JVM_CONSTANT_NameAndType:
1975 return 5;
1976
1977 case JVM_CONSTANT_Dynamic:
1978 case JVM_CONSTANT_DynamicInError:
1979 case JVM_CONSTANT_InvokeDynamic:
1980 // u1 tag, u2 bsm, u2 nt
1981 return 5;
1982
1983 case JVM_CONSTANT_Long:
1984 case JVM_CONSTANT_Double:
1985 return 9;
1986 }
1987 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1988 return 1;
1989 } /* end cpool_entry_size */
1990
1991
1992 // SymbolHashMap is used to find a constant pool index from a string.
1993 // This function fills in SymbolHashMaps, one for utf8s and one for
1994 // class names, returns size of the cpool raw bytes.
hash_entries_to(SymbolHashMap * symmap,SymbolHashMap * classmap)1995 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1996 SymbolHashMap *classmap) {
1997 jint size = 0;
1998
1999 for (u2 idx = 1; idx < length(); idx++) {
2000 u2 tag = tag_at(idx).value();
2001 size += cpool_entry_size(idx);
2002
2003 switch(tag) {
2004 case JVM_CONSTANT_Utf8: {
2005 Symbol* sym = symbol_at(idx);
2006 symmap->add_entry(sym, idx);
2007 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
2008 break;
2009 }
2010 case JVM_CONSTANT_Class:
2011 case JVM_CONSTANT_UnresolvedClass:
2012 case JVM_CONSTANT_UnresolvedClassInError: {
2013 Symbol* sym = klass_name_at(idx);
2014 classmap->add_entry(sym, idx);
2015 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
2016 break;
2017 }
2018 case JVM_CONSTANT_Long:
2019 case JVM_CONSTANT_Double: {
2020 idx++; // Both Long and Double take two cpool slots
2021 break;
2022 }
2023 }
2024 }
2025 return size;
2026 } /* end hash_utf8_entries_to */
2027
2028
2029 // Copy cpool bytes.
2030 // Returns:
2031 // 0, in case of OutOfMemoryError
2032 // -1, in case of internal error
2033 // > 0, count of the raw cpool bytes that have been copied
copy_cpool_bytes(int cpool_size,SymbolHashMap * tbl,unsigned char * bytes)2034 int ConstantPool::copy_cpool_bytes(int cpool_size,
2035 SymbolHashMap* tbl,
2036 unsigned char *bytes) {
2037 u2 idx1, idx2;
2038 jint size = 0;
2039 jint cnt = length();
2040 unsigned char *start_bytes = bytes;
2041
2042 for (jint idx = 1; idx < cnt; idx++) {
2043 u1 tag = tag_at(idx).value();
2044 jint ent_size = cpool_entry_size(idx);
2045
2046 assert(size + ent_size <= cpool_size, "Size mismatch");
2047
2048 *bytes = tag;
2049 DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag));
2050 switch(tag) {
2051 case JVM_CONSTANT_Invalid: {
2052 DBG(printf("JVM_CONSTANT_Invalid"));
2053 break;
2054 }
2055 case JVM_CONSTANT_Unicode: {
2056 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
2057 DBG(printf("JVM_CONSTANT_Unicode"));
2058 break;
2059 }
2060 case JVM_CONSTANT_Utf8: {
2061 Symbol* sym = symbol_at(idx);
2062 char* str = sym->as_utf8();
2063 // Warning! It's crashing on x86 with len = sym->utf8_length()
2064 int len = (int) strlen(str);
2065 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
2066 for (int i = 0; i < len; i++) {
2067 bytes[3+i] = (u1) str[i];
2068 }
2069 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
2070 break;
2071 }
2072 case JVM_CONSTANT_Integer: {
2073 jint val = int_at(idx);
2074 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2075 break;
2076 }
2077 case JVM_CONSTANT_Float: {
2078 jfloat val = float_at(idx);
2079 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
2080 break;
2081 }
2082 case JVM_CONSTANT_Long: {
2083 jlong val = long_at(idx);
2084 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2085 idx++; // Long takes two cpool slots
2086 break;
2087 }
2088 case JVM_CONSTANT_Double: {
2089 jdouble val = double_at(idx);
2090 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
2091 idx++; // Double takes two cpool slots
2092 break;
2093 }
2094 case JVM_CONSTANT_Class:
2095 case JVM_CONSTANT_UnresolvedClass:
2096 case JVM_CONSTANT_UnresolvedClassInError: {
2097 *bytes = JVM_CONSTANT_Class;
2098 Symbol* sym = klass_name_at(idx);
2099 idx1 = tbl->symbol_to_value(sym);
2100 assert(idx1 != 0, "Have not found a hashtable entry");
2101 Bytes::put_Java_u2((address) (bytes+1), idx1);
2102 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
2103 break;
2104 }
2105 case JVM_CONSTANT_String: {
2106 *bytes = JVM_CONSTANT_String;
2107 Symbol* sym = unresolved_string_at(idx);
2108 idx1 = tbl->symbol_to_value(sym);
2109 assert(idx1 != 0, "Have not found a hashtable entry");
2110 Bytes::put_Java_u2((address) (bytes+1), idx1);
2111 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
2112 break;
2113 }
2114 case JVM_CONSTANT_Fieldref:
2115 case JVM_CONSTANT_Methodref:
2116 case JVM_CONSTANT_InterfaceMethodref: {
2117 idx1 = uncached_klass_ref_index_at(idx);
2118 idx2 = uncached_name_and_type_ref_index_at(idx);
2119 Bytes::put_Java_u2((address) (bytes+1), idx1);
2120 Bytes::put_Java_u2((address) (bytes+3), idx2);
2121 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
2122 break;
2123 }
2124 case JVM_CONSTANT_NameAndType: {
2125 idx1 = name_ref_index_at(idx);
2126 idx2 = signature_ref_index_at(idx);
2127 Bytes::put_Java_u2((address) (bytes+1), idx1);
2128 Bytes::put_Java_u2((address) (bytes+3), idx2);
2129 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
2130 break;
2131 }
2132 case JVM_CONSTANT_ClassIndex: {
2133 *bytes = JVM_CONSTANT_Class;
2134 idx1 = klass_index_at(idx);
2135 Bytes::put_Java_u2((address) (bytes+1), idx1);
2136 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
2137 break;
2138 }
2139 case JVM_CONSTANT_StringIndex: {
2140 *bytes = JVM_CONSTANT_String;
2141 idx1 = string_index_at(idx);
2142 Bytes::put_Java_u2((address) (bytes+1), idx1);
2143 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
2144 break;
2145 }
2146 case JVM_CONSTANT_MethodHandle:
2147 case JVM_CONSTANT_MethodHandleInError: {
2148 *bytes = JVM_CONSTANT_MethodHandle;
2149 int kind = method_handle_ref_kind_at(idx);
2150 idx1 = method_handle_index_at(idx);
2151 *(bytes+1) = (unsigned char) kind;
2152 Bytes::put_Java_u2((address) (bytes+2), idx1);
2153 DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
2154 break;
2155 }
2156 case JVM_CONSTANT_MethodType:
2157 case JVM_CONSTANT_MethodTypeInError: {
2158 *bytes = JVM_CONSTANT_MethodType;
2159 idx1 = method_type_index_at(idx);
2160 Bytes::put_Java_u2((address) (bytes+1), idx1);
2161 DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
2162 break;
2163 }
2164 case JVM_CONSTANT_Dynamic:
2165 case JVM_CONSTANT_DynamicInError: {
2166 *bytes = tag;
2167 idx1 = extract_low_short_from_int(*int_at_addr(idx));
2168 idx2 = extract_high_short_from_int(*int_at_addr(idx));
2169 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4");
2170 Bytes::put_Java_u2((address) (bytes+1), idx1);
2171 Bytes::put_Java_u2((address) (bytes+3), idx2);
2172 DBG(printf("JVM_CONSTANT_Dynamic: %hd %hd", idx1, idx2));
2173 break;
2174 }
2175 case JVM_CONSTANT_InvokeDynamic: {
2176 *bytes = tag;
2177 idx1 = extract_low_short_from_int(*int_at_addr(idx));
2178 idx2 = extract_high_short_from_int(*int_at_addr(idx));
2179 assert(idx2 == bootstrap_name_and_type_ref_index_at(idx), "correct half of u4");
2180 Bytes::put_Java_u2((address) (bytes+1), idx1);
2181 Bytes::put_Java_u2((address) (bytes+3), idx2);
2182 DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
2183 break;
2184 }
2185 }
2186 DBG(printf("\n"));
2187 bytes += ent_size;
2188 size += ent_size;
2189 }
2190 assert(size == cpool_size, "Size mismatch");
2191
2192 // Keep temorarily for debugging until it's stable.
2193 DBG(print_cpool_bytes(cnt, start_bytes));
2194 return (int)(bytes - start_bytes);
2195 } /* end copy_cpool_bytes */
2196
2197 #undef DBG
2198
2199
set_on_stack(const bool value)2200 void ConstantPool::set_on_stack(const bool value) {
2201 if (value) {
2202 // Only record if it's not already set.
2203 if (!on_stack()) {
2204 assert(!is_shared(), "should always be set for shared constant pools");
2205 _flags |= _on_stack;
2206 MetadataOnStackMark::record(this);
2207 }
2208 } else {
2209 // Clearing is done single-threadedly.
2210 if (!is_shared()) {
2211 _flags &= ~_on_stack;
2212 }
2213 }
2214 }
2215
2216 // JSR 292 support for patching constant pool oops after the class is linked and
2217 // the oop array for resolved references are created.
2218 // We can't do this during classfile parsing, which is how the other indexes are
2219 // patched. The other patches are applied early for some error checking
2220 // so only defer the pseudo_strings.
patch_resolved_references(GrowableArray<Handle> * cp_patches)2221 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
2222 for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
2223 Handle patch = cp_patches->at(index);
2224 if (patch.not_null()) {
2225 assert (tag_at(index).is_string(), "should only be string left");
2226 // Patching a string means pre-resolving it.
2227 // The spelling in the constant pool is ignored.
2228 // The constant reference may be any object whatever.
2229 // If it is not a real interned string, the constant is referred
2230 // to as a "pseudo-string", and must be presented to the CP
2231 // explicitly, because it may require scavenging.
2232 int obj_index = cp_to_object_index(index);
2233 pseudo_string_at_put(index, obj_index, patch());
2234 DEBUG_ONLY(cp_patches->at_put(index, Handle());)
2235 }
2236 }
2237 #ifdef ASSERT
2238 // Ensure that all the patches have been used.
2239 for (int index = 0; index < cp_patches->length(); index++) {
2240 assert(cp_patches->at(index).is_null(),
2241 "Unused constant pool patch at %d in class file %s",
2242 index,
2243 pool_holder()->external_name());
2244 }
2245 #endif // ASSERT
2246 }
2247
2248 // Printing
2249
print_on(outputStream * st) const2250 void ConstantPool::print_on(outputStream* st) const {
2251 assert(is_constantPool(), "must be constantPool");
2252 st->print_cr("%s", internal_name());
2253 if (flags() != 0) {
2254 st->print(" - flags: 0x%x", flags());
2255 if (has_preresolution()) st->print(" has_preresolution");
2256 if (on_stack()) st->print(" on_stack");
2257 st->cr();
2258 }
2259 if (pool_holder() != NULL) {
2260 st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
2261 }
2262 st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
2263 st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
2264 st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
2265 st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
2266
2267 for (int index = 1; index < length(); index++) { // Index 0 is unused
2268 ((ConstantPool*)this)->print_entry_on(index, st);
2269 switch (tag_at(index).value()) {
2270 case JVM_CONSTANT_Long :
2271 case JVM_CONSTANT_Double :
2272 index++; // Skip entry following eigth-byte constant
2273 }
2274
2275 }
2276 st->cr();
2277 }
2278
2279 // Print one constant pool entry
print_entry_on(const int index,outputStream * st)2280 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2281 EXCEPTION_MARK;
2282 st->print(" - %3d : ", index);
2283 tag_at(index).print_on(st);
2284 st->print(" : ");
2285 switch (tag_at(index).value()) {
2286 case JVM_CONSTANT_Class :
2287 { Klass* k = klass_at(index, CATCH);
2288 guarantee(k != NULL, "need klass");
2289 k->print_value_on(st);
2290 st->print(" {" PTR_FORMAT "}", p2i(k));
2291 }
2292 break;
2293 case JVM_CONSTANT_Fieldref :
2294 case JVM_CONSTANT_Methodref :
2295 case JVM_CONSTANT_InterfaceMethodref :
2296 st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2297 st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2298 break;
2299 case JVM_CONSTANT_String :
2300 if (is_pseudo_string_at(index)) {
2301 oop anObj = pseudo_string_at(index);
2302 anObj->print_value_on(st);
2303 st->print(" {" PTR_FORMAT "}", p2i(anObj));
2304 } else {
2305 unresolved_string_at(index)->print_value_on(st);
2306 }
2307 break;
2308 case JVM_CONSTANT_Integer :
2309 st->print("%d", int_at(index));
2310 break;
2311 case JVM_CONSTANT_Float :
2312 st->print("%f", float_at(index));
2313 break;
2314 case JVM_CONSTANT_Long :
2315 st->print_jlong(long_at(index));
2316 break;
2317 case JVM_CONSTANT_Double :
2318 st->print("%lf", double_at(index));
2319 break;
2320 case JVM_CONSTANT_NameAndType :
2321 st->print("name_index=%d", name_ref_index_at(index));
2322 st->print(" signature_index=%d", signature_ref_index_at(index));
2323 break;
2324 case JVM_CONSTANT_Utf8 :
2325 symbol_at(index)->print_value_on(st);
2326 break;
2327 case JVM_CONSTANT_ClassIndex: {
2328 int name_index = *int_at_addr(index);
2329 st->print("klass_index=%d ", name_index);
2330 symbol_at(name_index)->print_value_on(st);
2331 }
2332 break;
2333 case JVM_CONSTANT_UnresolvedClass : // fall-through
2334 case JVM_CONSTANT_UnresolvedClassInError: {
2335 CPKlassSlot kslot = klass_slot_at(index);
2336 int resolved_klass_index = kslot.resolved_klass_index();
2337 int name_index = kslot.name_index();
2338 assert(tag_at(name_index).is_symbol(), "sanity");
2339
2340 Klass* klass = resolved_klasses()->at(resolved_klass_index);
2341 if (klass != NULL) {
2342 klass->print_value_on(st);
2343 } else {
2344 symbol_at(name_index)->print_value_on(st);
2345 }
2346 }
2347 break;
2348 case JVM_CONSTANT_MethodHandle :
2349 case JVM_CONSTANT_MethodHandleInError :
2350 st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2351 st->print(" ref_index=%d", method_handle_index_at(index));
2352 break;
2353 case JVM_CONSTANT_MethodType :
2354 case JVM_CONSTANT_MethodTypeInError :
2355 st->print("signature_index=%d", method_type_index_at(index));
2356 break;
2357 case JVM_CONSTANT_Dynamic :
2358 case JVM_CONSTANT_DynamicInError :
2359 {
2360 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(index));
2361 st->print(" type_index=%d", bootstrap_name_and_type_ref_index_at(index));
2362 int argc = bootstrap_argument_count_at(index);
2363 if (argc > 0) {
2364 for (int arg_i = 0; arg_i < argc; arg_i++) {
2365 int arg = bootstrap_argument_index_at(index, arg_i);
2366 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2367 }
2368 st->print("}");
2369 }
2370 }
2371 break;
2372 case JVM_CONSTANT_InvokeDynamic :
2373 {
2374 st->print("bootstrap_method_index=%d", bootstrap_method_ref_index_at(index));
2375 st->print(" name_and_type_index=%d", bootstrap_name_and_type_ref_index_at(index));
2376 int argc = bootstrap_argument_count_at(index);
2377 if (argc > 0) {
2378 for (int arg_i = 0; arg_i < argc; arg_i++) {
2379 int arg = bootstrap_argument_index_at(index, arg_i);
2380 st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2381 }
2382 st->print("}");
2383 }
2384 }
2385 break;
2386 default:
2387 ShouldNotReachHere();
2388 break;
2389 }
2390 st->cr();
2391 }
2392
print_value_on(outputStream * st) const2393 void ConstantPool::print_value_on(outputStream* st) const {
2394 assert(is_constantPool(), "must be constantPool");
2395 st->print("constant pool [%d]", length());
2396 if (has_preresolution()) st->print("/preresolution");
2397 if (operands() != NULL) st->print("/operands[%d]", operands()->length());
2398 print_address_on(st);
2399 if (pool_holder() != NULL) {
2400 st->print(" for ");
2401 pool_holder()->print_value_on(st);
2402 bool extra = (pool_holder()->constants() != this);
2403 if (extra) st->print(" (extra)");
2404 }
2405 if (cache() != NULL) {
2406 st->print(" cache=" PTR_FORMAT, p2i(cache()));
2407 }
2408 }
2409
2410 // Verification
2411
verify_on(outputStream * st)2412 void ConstantPool::verify_on(outputStream* st) {
2413 guarantee(is_constantPool(), "object must be constant pool");
2414 for (int i = 0; i< length(); i++) {
2415 constantTag tag = tag_at(i);
2416 if (tag.is_klass() || tag.is_unresolved_klass()) {
2417 guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2418 } else if (tag.is_symbol()) {
2419 CPSlot entry = slot_at(i);
2420 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2421 } else if (tag.is_string()) {
2422 CPSlot entry = slot_at(i);
2423 guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2424 }
2425 }
2426 if (pool_holder() != NULL) {
2427 // Note: pool_holder() can be NULL in temporary constant pools
2428 // used during constant pool merging
2429 guarantee(pool_holder()->is_klass(), "should be klass");
2430 }
2431 }
2432
2433
~SymbolHashMap()2434 SymbolHashMap::~SymbolHashMap() {
2435 SymbolHashMapEntry* next;
2436 for (int i = 0; i < _table_size; i++) {
2437 for (SymbolHashMapEntry* cur = bucket(i); cur != NULL; cur = next) {
2438 next = cur->next();
2439 delete(cur);
2440 }
2441 }
2442 FREE_C_HEAP_ARRAY(SymbolHashMapBucket, _buckets);
2443 }
2444
add_entry(Symbol * sym,u2 value)2445 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2446 char *str = sym->as_utf8();
2447 unsigned int hash = compute_hash(str, sym->utf8_length());
2448 unsigned int index = hash % table_size();
2449
2450 // check if already in map
2451 // we prefer the first entry since it is more likely to be what was used in
2452 // the class file
2453 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2454 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2455 if (en->hash() == hash && en->symbol() == sym) {
2456 return; // already there
2457 }
2458 }
2459
2460 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2461 entry->set_next(bucket(index));
2462 _buckets[index].set_entry(entry);
2463 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2464 }
2465
find_entry(Symbol * sym)2466 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2467 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2468 char *str = sym->as_utf8();
2469 int len = sym->utf8_length();
2470 unsigned int hash = SymbolHashMap::compute_hash(str, len);
2471 unsigned int index = hash % table_size();
2472 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2473 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2474 if (en->hash() == hash && en->symbol() == sym) {
2475 return en;
2476 }
2477 }
2478 return NULL;
2479 }
2480
initialize_table(int table_size)2481 void SymbolHashMap::initialize_table(int table_size) {
2482 _table_size = table_size;
2483 _buckets = NEW_C_HEAP_ARRAY(SymbolHashMapBucket, table_size, mtSymbol);
2484 for (int index = 0; index < table_size; index++) {
2485 _buckets[index].clear();
2486 }
2487 }
2488