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