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