Mon, 09 Mar 2009 13:28:46 -0700
6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair
1 /*
2 * Copyright 1997-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_constantPoolOop.cpp.incl"
28 void constantPoolOopDesc::set_flag_at(FlagBit fb) {
29 const int MAX_STATE_CHANGES = 2;
30 for (int i = MAX_STATE_CHANGES + 10; i > 0; i--) {
31 int oflags = _flags;
32 int nflags = oflags | (1 << (int)fb);
33 if (Atomic::cmpxchg(nflags, &_flags, oflags) == oflags)
34 return;
35 }
36 assert(false, "failed to cmpxchg flags");
37 _flags |= (1 << (int)fb); // better than nothing
38 }
40 klassOop constantPoolOopDesc::klass_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
41 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
42 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
43 // tag is not updated atomicly.
44 oop entry = *(this_oop->obj_at_addr(which));
45 if (entry->is_klass()) {
46 // Already resolved - return entry.
47 return (klassOop)entry;
48 }
50 // Acquire lock on constant oop while doing update. After we get the lock, we check if another object
51 // already has updated the object
52 assert(THREAD->is_Java_thread(), "must be a Java thread");
53 bool do_resolve = false;
54 bool in_error = false;
56 symbolHandle name;
57 Handle loader;
58 { ObjectLocker ol(this_oop, THREAD);
60 if (this_oop->tag_at(which).is_unresolved_klass()) {
61 if (this_oop->tag_at(which).is_unresolved_klass_in_error()) {
62 in_error = true;
63 } else {
64 do_resolve = true;
65 name = symbolHandle(THREAD, this_oop->unresolved_klass_at(which));
66 loader = Handle(THREAD, instanceKlass::cast(this_oop->pool_holder())->class_loader());
67 }
68 }
69 } // unlocking constantPool
72 // The original attempt to resolve this constant pool entry failed so find the
73 // original error and throw it again (JVMS 5.4.3).
74 if (in_error) {
75 symbolOop error = SystemDictionary::find_resolution_error(this_oop, which);
76 guarantee(error != (symbolOop)NULL, "tag mismatch with resolution error table");
77 ResourceMark rm;
78 // exception text will be the class name
79 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
80 THROW_MSG_0(error, className);
81 }
83 if (do_resolve) {
84 // this_oop must be unlocked during resolve_or_fail
85 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
86 Handle h_prot (THREAD, protection_domain);
87 klassOop k_oop = SystemDictionary::resolve_or_fail(name, loader, h_prot, true, THREAD);
88 KlassHandle k;
89 if (!HAS_PENDING_EXCEPTION) {
90 k = KlassHandle(THREAD, k_oop);
91 // Do access check for klasses
92 verify_constant_pool_resolve(this_oop, k, THREAD);
93 }
95 // Failed to resolve class. We must record the errors so that subsequent attempts
96 // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
97 if (HAS_PENDING_EXCEPTION) {
98 ResourceMark rm;
99 symbolHandle error(PENDING_EXCEPTION->klass()->klass_part()->name());
101 bool throw_orig_error = false;
102 {
103 ObjectLocker ol (this_oop, THREAD);
105 // some other thread has beaten us and has resolved the class.
106 if (this_oop->tag_at(which).is_klass()) {
107 CLEAR_PENDING_EXCEPTION;
108 entry = this_oop->resolved_klass_at(which);
109 return (klassOop)entry;
110 }
112 if (!PENDING_EXCEPTION->
113 is_a(SystemDictionary::linkageError_klass())) {
114 // Just throw the exception and don't prevent these classes from
115 // being loaded due to virtual machine errors like StackOverflow
116 // and OutOfMemoryError, etc, or if the thread was hit by stop()
117 // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
118 }
119 else if (!this_oop->tag_at(which).is_unresolved_klass_in_error()) {
120 SystemDictionary::add_resolution_error(this_oop, which, error);
121 this_oop->tag_at_put(which, JVM_CONSTANT_UnresolvedClassInError);
122 } else {
123 // some other thread has put the class in error state.
124 error = symbolHandle(SystemDictionary::find_resolution_error(this_oop, which));
125 assert(!error.is_null(), "checking");
126 throw_orig_error = true;
127 }
128 } // unlocked
130 if (throw_orig_error) {
131 CLEAR_PENDING_EXCEPTION;
132 ResourceMark rm;
133 const char* className = this_oop->unresolved_klass_at(which)->as_C_string();
134 THROW_MSG_0(error, className);
135 }
137 return 0;
138 }
140 if (TraceClassResolution && !k()->klass_part()->oop_is_array()) {
141 // skip resolving the constant pool so that this code get's
142 // called the next time some bytecodes refer to this class.
143 ResourceMark rm;
144 int line_number = -1;
145 const char * source_file = NULL;
146 if (JavaThread::current()->has_last_Java_frame()) {
147 // try to identify the method which called this function.
148 vframeStream vfst(JavaThread::current());
149 if (!vfst.at_end()) {
150 line_number = vfst.method()->line_number_from_bci(vfst.bci());
151 symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
152 if (s != NULL) {
153 source_file = s->as_C_string();
154 }
155 }
156 }
157 if (k() != this_oop->pool_holder()) {
158 // only print something if the classes are different
159 if (source_file != NULL) {
160 tty->print("RESOLVE %s %s %s:%d\n",
161 instanceKlass::cast(this_oop->pool_holder())->external_name(),
162 instanceKlass::cast(k())->external_name(), source_file, line_number);
163 } else {
164 tty->print("RESOLVE %s %s\n",
165 instanceKlass::cast(this_oop->pool_holder())->external_name(),
166 instanceKlass::cast(k())->external_name());
167 }
168 }
169 return k();
170 } else {
171 ObjectLocker ol (this_oop, THREAD);
172 // Only updated constant pool - if it is resolved.
173 do_resolve = this_oop->tag_at(which).is_unresolved_klass();
174 if (do_resolve) {
175 this_oop->klass_at_put(which, k());
176 }
177 }
178 }
180 entry = this_oop->resolved_klass_at(which);
181 assert(entry->is_klass(), "must be resolved at this point");
182 return (klassOop)entry;
183 }
186 // Does not update constantPoolOop - to avoid any exception throwing. Used
187 // by compiler and exception handling. Also used to avoid classloads for
188 // instanceof operations. Returns NULL if the class has not been loaded or
189 // if the verification of constant pool failed
190 klassOop constantPoolOopDesc::klass_at_if_loaded(constantPoolHandle this_oop, int which) {
191 oop entry = *this_oop->obj_at_addr(which);
192 if (entry->is_klass()) {
193 return (klassOop)entry;
194 } else {
195 assert(entry->is_symbol(), "must be either symbol or klass");
196 Thread *thread = Thread::current();
197 symbolHandle name (thread, (symbolOop)entry);
198 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
199 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
200 Handle h_prot (thread, protection_domain);
201 Handle h_loader (thread, loader);
202 klassOop k = SystemDictionary::find(name, h_loader, h_prot, thread);
204 if (k != NULL) {
205 // Make sure that resolving is legal
206 EXCEPTION_MARK;
207 KlassHandle klass(THREAD, k);
208 // return NULL if verification fails
209 verify_constant_pool_resolve(this_oop, klass, THREAD);
210 if (HAS_PENDING_EXCEPTION) {
211 CLEAR_PENDING_EXCEPTION;
212 return NULL;
213 }
214 return klass();
215 } else {
216 return k;
217 }
218 }
219 }
222 klassOop constantPoolOopDesc::klass_ref_at_if_loaded(constantPoolHandle this_oop, int which) {
223 return klass_at_if_loaded(this_oop, this_oop->klass_ref_index_at(which));
224 }
227 // This is an interface for the compiler that allows accessing non-resolved entries
228 // in the constant pool - but still performs the validations tests. Must be used
229 // in a pre-parse of the compiler - to determine what it can do and not do.
230 // Note: We cannot update the ConstantPool from the vm_thread.
231 klassOop constantPoolOopDesc::klass_ref_at_if_loaded_check(constantPoolHandle this_oop, int index, TRAPS) {
232 int which = this_oop->klass_ref_index_at(index);
233 oop entry = *this_oop->obj_at_addr(which);
234 if (entry->is_klass()) {
235 return (klassOop)entry;
236 } else {
237 assert(entry->is_symbol(), "must be either symbol or klass");
238 symbolHandle name (THREAD, (symbolOop)entry);
239 oop loader = instanceKlass::cast(this_oop->pool_holder())->class_loader();
240 oop protection_domain = Klass::cast(this_oop->pool_holder())->protection_domain();
241 Handle h_loader(THREAD, loader);
242 Handle h_prot (THREAD, protection_domain);
243 KlassHandle k(THREAD, SystemDictionary::find(name, h_loader, h_prot, THREAD));
245 // Do access check for klasses
246 if( k.not_null() ) verify_constant_pool_resolve(this_oop, k, CHECK_NULL);
247 return k();
248 }
249 }
252 symbolOop constantPoolOopDesc::uncached_name_ref_at(int which) {
253 jint ref_index = name_and_type_at(uncached_name_and_type_ref_index_at(which));
254 int name_index = extract_low_short_from_int(ref_index);
255 return symbol_at(name_index);
256 }
259 symbolOop constantPoolOopDesc::uncached_signature_ref_at(int which) {
260 jint ref_index = name_and_type_at(uncached_name_and_type_ref_index_at(which));
261 int signature_index = extract_high_short_from_int(ref_index);
262 return symbol_at(signature_index);
263 }
266 int constantPoolOopDesc::uncached_name_and_type_ref_index_at(int which) {
267 jint ref_index = field_or_method_at(which, true);
268 return extract_high_short_from_int(ref_index);
269 }
272 int constantPoolOopDesc::uncached_klass_ref_index_at(int which) {
273 jint ref_index = field_or_method_at(which, true);
274 return extract_low_short_from_int(ref_index);
275 }
278 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
279 if (k->oop_is_instance() || k->oop_is_objArray()) {
280 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
281 klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
282 KlassHandle element (THREAD, elem_oop);
284 // The element type could be a typeArray - we only need the access check if it is
285 // an reference to another class
286 if (element->oop_is_instance()) {
287 LinkResolver::check_klass_accessability(holder, element, CHECK);
288 }
289 }
290 }
293 int constantPoolOopDesc::klass_ref_index_at(int which) {
294 jint ref_index = field_or_method_at(which, false);
295 return extract_low_short_from_int(ref_index);
296 }
299 int constantPoolOopDesc::name_and_type_ref_index_at(int which) {
300 jint ref_index = field_or_method_at(which, false);
301 return extract_high_short_from_int(ref_index);
302 }
305 int constantPoolOopDesc::name_ref_index_at(int which) {
306 jint ref_index = name_and_type_at(which);
307 return extract_low_short_from_int(ref_index);
308 }
311 int constantPoolOopDesc::signature_ref_index_at(int which) {
312 jint ref_index = name_and_type_at(which);
313 return extract_high_short_from_int(ref_index);
314 }
317 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
318 return klass_at(klass_ref_index_at(which), CHECK_NULL);
319 }
322 symbolOop constantPoolOopDesc::klass_name_at(int which) {
323 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
324 "Corrupted constant pool");
325 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
326 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
327 // tag is not updated atomicly.
328 oop entry = *(obj_at_addr(which));
329 if (entry->is_klass()) {
330 // Already resolved - return entry's name.
331 return klassOop(entry)->klass_part()->name();
332 } else {
333 assert(entry->is_symbol(), "must be either symbol or klass");
334 return (symbolOop)entry;
335 }
336 }
338 symbolOop constantPoolOopDesc::klass_ref_at_noresolve(int which) {
339 jint ref_index = klass_ref_index_at(which);
340 return klass_at_noresolve(ref_index);
341 }
343 char* constantPoolOopDesc::string_at_noresolve(int which) {
344 // Test entry type in case string is resolved while in here.
345 oop entry = *(obj_at_addr(which));
346 if (entry->is_symbol()) {
347 return ((symbolOop)entry)->as_C_string();
348 } else if (java_lang_String::is_instance(entry)) {
349 return java_lang_String::as_utf8_string(entry);
350 } else {
351 return (char*)"<pseudo-string>";
352 }
353 }
356 symbolOop constantPoolOopDesc::name_ref_at(int which) {
357 jint ref_index = name_and_type_at(name_and_type_ref_index_at(which));
358 int name_index = extract_low_short_from_int(ref_index);
359 return symbol_at(name_index);
360 }
363 symbolOop constantPoolOopDesc::signature_ref_at(int which) {
364 jint ref_index = name_and_type_at(name_and_type_ref_index_at(which));
365 int signature_index = extract_high_short_from_int(ref_index);
366 return symbol_at(signature_index);
367 }
370 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
371 return FieldType::basic_type(symbol_at(which));
372 }
375 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
376 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
377 if (this_oop->tag_at(index).is_unresolved_string()) {
378 this_oop->string_at(index, CHECK);
379 }
380 }
381 }
383 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
384 oop entry = *(this_oop->obj_at_addr(which));
385 if (entry->is_symbol()) {
386 ObjectLocker ol(this_oop, THREAD);
387 if (this_oop->tag_at(which).is_unresolved_string()) {
388 // Intern string
389 symbolOop sym = this_oop->unresolved_string_at(which);
390 entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
391 this_oop->string_at_put(which, entry);
392 } else {
393 // Another thread beat us and interned string, read string from constant pool
394 entry = this_oop->resolved_string_at(which);
395 }
396 }
397 assert(java_lang_String::is_instance(entry), "must be string");
398 return entry;
399 }
402 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
403 oop entry = *(obj_at_addr(which));
404 if (entry->is_symbol())
405 // Not yet resolved, but it will resolve to a string.
406 return false;
407 else if (java_lang_String::is_instance(entry))
408 return false; // actually, it might be a non-interned or non-perm string
409 else
410 // truly pseudo
411 return true;
412 }
415 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
416 int which) {
417 // Names are interned, so we can compare symbolOops directly
418 symbolOop cp_name = klass_name_at(which);
419 return (cp_name == k->name());
420 }
423 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
424 ResourceMark rm;
425 int count = 0;
426 for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
427 if (tag_at(index).is_unresolved_string()) {
428 // Intern string
429 symbolOop sym = unresolved_string_at(index);
430 oop entry = StringTable::intern(sym, CHECK_(-1));
431 string_at_put(index, entry);
432 }
433 }
434 return count;
435 }
438 // Iterate over symbols which are used as class, field, method names and
439 // signatures (in preparation for writing to the shared archive).
441 void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) {
442 for (int index = 1; index < length(); index++) { // Index 0 is unused
443 switch (tag_at(index).value()) {
445 case JVM_CONSTANT_UnresolvedClass:
446 closure->do_oop(obj_at_addr(index));
447 break;
449 case JVM_CONSTANT_NameAndType:
450 {
451 int i = *int_at_addr(index);
452 closure->do_oop(obj_at_addr((unsigned)i >> 16));
453 closure->do_oop(obj_at_addr((unsigned)i & 0xffff));
454 }
455 break;
457 case JVM_CONSTANT_Class:
458 case JVM_CONSTANT_InterfaceMethodref:
459 case JVM_CONSTANT_Fieldref:
460 case JVM_CONSTANT_Methodref:
461 case JVM_CONSTANT_Integer:
462 case JVM_CONSTANT_Float:
463 // Do nothing! Not an oop.
464 // These constant types do not reference symbols at this point.
465 break;
467 case JVM_CONSTANT_String:
468 // Do nothing! Not a symbol.
469 break;
471 case JVM_CONSTANT_UnresolvedString:
472 case JVM_CONSTANT_Utf8:
473 // These constants are symbols, but unless these symbols are
474 // actually to be used for something, we don't want to mark them.
475 break;
477 case JVM_CONSTANT_Long:
478 case JVM_CONSTANT_Double:
479 // Do nothing! Not an oop. (But takes two pool entries.)
480 ++index;
481 break;
483 default:
484 ShouldNotReachHere();
485 break;
486 }
487 }
488 }
491 // Iterate over the [one] tags array (in preparation for writing to the
492 // shared archive).
494 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
495 closure->do_oop(tags_addr());
496 }
499 // Iterate over String objects (in preparation for writing to the shared
500 // archive).
502 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
503 for (int index = 1; index < length(); index++) { // Index 0 is unused
504 switch (tag_at(index).value()) {
506 case JVM_CONSTANT_UnresolvedClass:
507 case JVM_CONSTANT_NameAndType:
508 // Do nothing! Not a String.
509 break;
511 case JVM_CONSTANT_Class:
512 case JVM_CONSTANT_InterfaceMethodref:
513 case JVM_CONSTANT_Fieldref:
514 case JVM_CONSTANT_Methodref:
515 case JVM_CONSTANT_Integer:
516 case JVM_CONSTANT_Float:
517 // Do nothing! Not an oop.
518 // These constant types do not reference symbols at this point.
519 break;
521 case JVM_CONSTANT_String:
522 closure->do_oop(obj_at_addr(index));
523 break;
525 case JVM_CONSTANT_UnresolvedString:
526 case JVM_CONSTANT_Utf8:
527 // These constants are symbols, but unless these symbols are
528 // actually to be used for something, we don't want to mark them.
529 break;
531 case JVM_CONSTANT_Long:
532 case JVM_CONSTANT_Double:
533 // Do nothing! Not an oop. (But takes two pool entries.)
534 ++index;
535 break;
537 default:
538 ShouldNotReachHere();
539 break;
540 }
541 }
542 }
545 // Compare this constant pool's entry at index1 to the constant pool
546 // cp2's entry at index2.
547 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
548 int index2, TRAPS) {
550 jbyte t1 = tag_at(index1).value();
551 jbyte t2 = cp2->tag_at(index2).value();
554 // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
555 // when comparing
556 if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
557 t1 = JVM_CONSTANT_UnresolvedClass;
558 }
559 if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
560 t2 = JVM_CONSTANT_UnresolvedClass;
561 }
563 if (t1 != t2) {
564 // Not the same entry type so there is nothing else to check. Note
565 // that this style of checking will consider resolved/unresolved
566 // class pairs and resolved/unresolved string pairs as different.
567 // From the constantPoolOop API point of view, this is correct
568 // behavior. See constantPoolKlass::merge() to see how this plays
569 // out in the context of constantPoolOop merging.
570 return false;
571 }
573 switch (t1) {
574 case JVM_CONSTANT_Class:
575 {
576 klassOop k1 = klass_at(index1, CHECK_false);
577 klassOop k2 = cp2->klass_at(index2, CHECK_false);
578 if (k1 == k2) {
579 return true;
580 }
581 } break;
583 case JVM_CONSTANT_ClassIndex:
584 {
585 int recur1 = klass_index_at(index1);
586 int recur2 = cp2->klass_index_at(index2);
587 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
588 if (match) {
589 return true;
590 }
591 } break;
593 case JVM_CONSTANT_Double:
594 {
595 jdouble d1 = double_at(index1);
596 jdouble d2 = cp2->double_at(index2);
597 if (d1 == d2) {
598 return true;
599 }
600 } break;
602 case JVM_CONSTANT_Fieldref:
603 case JVM_CONSTANT_InterfaceMethodref:
604 case JVM_CONSTANT_Methodref:
605 {
606 int recur1 = uncached_klass_ref_index_at(index1);
607 int recur2 = cp2->uncached_klass_ref_index_at(index2);
608 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
609 if (match) {
610 recur1 = uncached_name_and_type_ref_index_at(index1);
611 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
612 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
613 if (match) {
614 return true;
615 }
616 }
617 } break;
619 case JVM_CONSTANT_Float:
620 {
621 jfloat f1 = float_at(index1);
622 jfloat f2 = cp2->float_at(index2);
623 if (f1 == f2) {
624 return true;
625 }
626 } break;
628 case JVM_CONSTANT_Integer:
629 {
630 jint i1 = int_at(index1);
631 jint i2 = cp2->int_at(index2);
632 if (i1 == i2) {
633 return true;
634 }
635 } break;
637 case JVM_CONSTANT_Long:
638 {
639 jlong l1 = long_at(index1);
640 jlong l2 = cp2->long_at(index2);
641 if (l1 == l2) {
642 return true;
643 }
644 } break;
646 case JVM_CONSTANT_NameAndType:
647 {
648 int recur1 = name_ref_index_at(index1);
649 int recur2 = cp2->name_ref_index_at(index2);
650 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
651 if (match) {
652 recur1 = signature_ref_index_at(index1);
653 recur2 = cp2->signature_ref_index_at(index2);
654 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
655 if (match) {
656 return true;
657 }
658 }
659 } break;
661 case JVM_CONSTANT_String:
662 {
663 oop s1 = string_at(index1, CHECK_false);
664 oop s2 = cp2->string_at(index2, CHECK_false);
665 if (s1 == s2) {
666 return true;
667 }
668 } break;
670 case JVM_CONSTANT_StringIndex:
671 {
672 int recur1 = string_index_at(index1);
673 int recur2 = cp2->string_index_at(index2);
674 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
675 if (match) {
676 return true;
677 }
678 } break;
680 case JVM_CONSTANT_UnresolvedClass:
681 {
682 symbolOop k1 = unresolved_klass_at(index1);
683 symbolOop k2 = cp2->unresolved_klass_at(index2);
684 if (k1 == k2) {
685 return true;
686 }
687 } break;
689 case JVM_CONSTANT_UnresolvedString:
690 {
691 symbolOop s1 = unresolved_string_at(index1);
692 symbolOop s2 = cp2->unresolved_string_at(index2);
693 if (s1 == s2) {
694 return true;
695 }
696 } break;
698 case JVM_CONSTANT_Utf8:
699 {
700 symbolOop s1 = symbol_at(index1);
701 symbolOop s2 = cp2->symbol_at(index2);
702 if (s1 == s2) {
703 return true;
704 }
705 } break;
707 // Invalid is used as the tag for the second constant pool entry
708 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
709 // not be seen by itself.
710 case JVM_CONSTANT_Invalid: // fall through
712 default:
713 ShouldNotReachHere();
714 break;
715 }
717 return false;
718 } // end compare_entry_to()
721 // Copy this constant pool's entries at start_i to end_i (inclusive)
722 // to the constant pool to_cp's entries starting at to_i. A total of
723 // (end_i - start_i) + 1 entries are copied.
724 void constantPoolOopDesc::copy_cp_to(int start_i, int end_i,
725 constantPoolHandle to_cp, int to_i, TRAPS) {
727 int dest_i = to_i; // leave original alone for debug purposes
729 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
730 copy_entry_to(src_i, to_cp, dest_i, CHECK);
732 switch (tag_at(src_i).value()) {
733 case JVM_CONSTANT_Double:
734 case JVM_CONSTANT_Long:
735 // double and long take two constant pool entries
736 src_i += 2;
737 dest_i += 2;
738 break;
740 default:
741 // all others take one constant pool entry
742 src_i++;
743 dest_i++;
744 break;
745 }
746 }
747 } // end copy_cp_to()
750 // Copy this constant pool's entry at from_i to the constant pool
751 // to_cp's entry at to_i.
752 void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp,
753 int to_i, TRAPS) {
755 switch (tag_at(from_i).value()) {
756 case JVM_CONSTANT_Class:
757 {
758 klassOop k = klass_at(from_i, CHECK);
759 to_cp->klass_at_put(to_i, k);
760 } break;
762 case JVM_CONSTANT_ClassIndex:
763 {
764 jint ki = klass_index_at(from_i);
765 to_cp->klass_index_at_put(to_i, ki);
766 } break;
768 case JVM_CONSTANT_Double:
769 {
770 jdouble d = double_at(from_i);
771 to_cp->double_at_put(to_i, d);
772 // double takes two constant pool entries so init second entry's tag
773 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
774 } break;
776 case JVM_CONSTANT_Fieldref:
777 {
778 int class_index = uncached_klass_ref_index_at(from_i);
779 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
780 to_cp->field_at_put(to_i, class_index, name_and_type_index);
781 } break;
783 case JVM_CONSTANT_Float:
784 {
785 jfloat f = float_at(from_i);
786 to_cp->float_at_put(to_i, f);
787 } break;
789 case JVM_CONSTANT_Integer:
790 {
791 jint i = int_at(from_i);
792 to_cp->int_at_put(to_i, i);
793 } break;
795 case JVM_CONSTANT_InterfaceMethodref:
796 {
797 int class_index = uncached_klass_ref_index_at(from_i);
798 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
799 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
800 } break;
802 case JVM_CONSTANT_Long:
803 {
804 jlong l = long_at(from_i);
805 to_cp->long_at_put(to_i, l);
806 // long takes two constant pool entries so init second entry's tag
807 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
808 } break;
810 case JVM_CONSTANT_Methodref:
811 {
812 int class_index = uncached_klass_ref_index_at(from_i);
813 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
814 to_cp->method_at_put(to_i, class_index, name_and_type_index);
815 } break;
817 case JVM_CONSTANT_NameAndType:
818 {
819 int name_ref_index = name_ref_index_at(from_i);
820 int signature_ref_index = signature_ref_index_at(from_i);
821 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
822 } break;
824 case JVM_CONSTANT_String:
825 {
826 oop s = string_at(from_i, CHECK);
827 to_cp->string_at_put(to_i, s);
828 } break;
830 case JVM_CONSTANT_StringIndex:
831 {
832 jint si = string_index_at(from_i);
833 to_cp->string_index_at_put(to_i, si);
834 } break;
836 case JVM_CONSTANT_UnresolvedClass:
837 {
838 symbolOop k = unresolved_klass_at(from_i);
839 to_cp->unresolved_klass_at_put(to_i, k);
840 } break;
842 case JVM_CONSTANT_UnresolvedClassInError:
843 {
844 symbolOop k = unresolved_klass_at(from_i);
845 to_cp->unresolved_klass_at_put(to_i, k);
846 to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
847 } break;
850 case JVM_CONSTANT_UnresolvedString:
851 {
852 symbolOop s = unresolved_string_at(from_i);
853 to_cp->unresolved_string_at_put(to_i, s);
854 } break;
856 case JVM_CONSTANT_Utf8:
857 {
858 symbolOop s = symbol_at(from_i);
859 to_cp->symbol_at_put(to_i, s);
860 } break;
862 // Invalid is used as the tag for the second constant pool entry
863 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
864 // not be seen by itself.
865 case JVM_CONSTANT_Invalid: // fall through
867 default:
868 {
869 jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb
870 ShouldNotReachHere();
871 } break;
872 }
873 } // end copy_entry_to()
876 // Search constant pool search_cp for an entry that matches this
877 // constant pool's entry at pattern_i. Returns the index of a
878 // matching entry or zero (0) if there is no matching entry.
879 int constantPoolOopDesc::find_matching_entry(int pattern_i,
880 constantPoolHandle search_cp, TRAPS) {
882 // index zero (0) is not used
883 for (int i = 1; i < search_cp->length(); i++) {
884 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
885 if (found) {
886 return i;
887 }
888 }
890 return 0; // entry not found; return unused index zero (0)
891 } // end find_matching_entry()
894 #ifndef PRODUCT
896 const char* constantPoolOopDesc::printable_name_at(int which) {
898 constantTag tag = tag_at(which);
900 if (tag.is_unresolved_string() || tag.is_string()) {
901 return string_at_noresolve(which);
902 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
903 return klass_name_at(which)->as_C_string();
904 } else if (tag.is_symbol()) {
905 return symbol_at(which)->as_C_string();
906 }
907 return "";
908 }
910 #endif // PRODUCT
913 // JVMTI GetConstantPool support
915 // For temporary use until code is stable.
916 #define DBG(code)
918 static const char* WARN_MSG = "Must not be such entry!";
920 static void print_cpool_bytes(jint cnt, u1 *bytes) {
921 jint size = 0;
922 u2 idx1, idx2;
924 for (jint idx = 1; idx < cnt; idx++) {
925 jint ent_size = 0;
926 u1 tag = *bytes++;
927 size++; // count tag
929 printf("const #%03d, tag: %02d ", idx, tag);
930 switch(tag) {
931 case JVM_CONSTANT_Invalid: {
932 printf("Invalid");
933 break;
934 }
935 case JVM_CONSTANT_Unicode: {
936 printf("Unicode %s", WARN_MSG);
937 break;
938 }
939 case JVM_CONSTANT_Utf8: {
940 u2 len = Bytes::get_Java_u2(bytes);
941 char str[128];
942 if (len > 127) {
943 len = 127;
944 }
945 strncpy(str, (char *) (bytes+2), len);
946 str[len] = '\0';
947 printf("Utf8 \"%s\"", str);
948 ent_size = 2 + len;
949 break;
950 }
951 case JVM_CONSTANT_Integer: {
952 u4 val = Bytes::get_Java_u4(bytes);
953 printf("int %d", *(int *) &val);
954 ent_size = 4;
955 break;
956 }
957 case JVM_CONSTANT_Float: {
958 u4 val = Bytes::get_Java_u4(bytes);
959 printf("float %5.3ff", *(float *) &val);
960 ent_size = 4;
961 break;
962 }
963 case JVM_CONSTANT_Long: {
964 u8 val = Bytes::get_Java_u8(bytes);
965 printf("long "INT64_FORMAT, *(jlong *) &val);
966 ent_size = 8;
967 idx++; // Long takes two cpool slots
968 break;
969 }
970 case JVM_CONSTANT_Double: {
971 u8 val = Bytes::get_Java_u8(bytes);
972 printf("double %5.3fd", *(jdouble *)&val);
973 ent_size = 8;
974 idx++; // Double takes two cpool slots
975 break;
976 }
977 case JVM_CONSTANT_Class: {
978 idx1 = Bytes::get_Java_u2(bytes);
979 printf("class #%03d", idx1);
980 ent_size = 2;
981 break;
982 }
983 case JVM_CONSTANT_String: {
984 idx1 = Bytes::get_Java_u2(bytes);
985 printf("String #%03d", idx1);
986 ent_size = 2;
987 break;
988 }
989 case JVM_CONSTANT_Fieldref: {
990 idx1 = Bytes::get_Java_u2(bytes);
991 idx2 = Bytes::get_Java_u2(bytes+2);
992 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
993 ent_size = 4;
994 break;
995 }
996 case JVM_CONSTANT_Methodref: {
997 idx1 = Bytes::get_Java_u2(bytes);
998 idx2 = Bytes::get_Java_u2(bytes+2);
999 printf("Method #%03d, #%03d", idx1, idx2);
1000 ent_size = 4;
1001 break;
1002 }
1003 case JVM_CONSTANT_InterfaceMethodref: {
1004 idx1 = Bytes::get_Java_u2(bytes);
1005 idx2 = Bytes::get_Java_u2(bytes+2);
1006 printf("InterfMethod #%03d, #%03d", idx1, idx2);
1007 ent_size = 4;
1008 break;
1009 }
1010 case JVM_CONSTANT_NameAndType: {
1011 idx1 = Bytes::get_Java_u2(bytes);
1012 idx2 = Bytes::get_Java_u2(bytes+2);
1013 printf("NameAndType #%03d, #%03d", idx1, idx2);
1014 ent_size = 4;
1015 break;
1016 }
1017 case JVM_CONSTANT_ClassIndex: {
1018 printf("ClassIndex %s", WARN_MSG);
1019 break;
1020 }
1021 case JVM_CONSTANT_UnresolvedClass: {
1022 printf("UnresolvedClass: %s", WARN_MSG);
1023 break;
1024 }
1025 case JVM_CONSTANT_UnresolvedClassInError: {
1026 printf("UnresolvedClassInErr: %s", WARN_MSG);
1027 break;
1028 }
1029 case JVM_CONSTANT_StringIndex: {
1030 printf("StringIndex: %s", WARN_MSG);
1031 break;
1032 }
1033 case JVM_CONSTANT_UnresolvedString: {
1034 printf("UnresolvedString: %s", WARN_MSG);
1035 break;
1036 }
1037 }
1038 printf(";\n");
1039 bytes += ent_size;
1040 size += ent_size;
1041 }
1042 printf("Cpool size: %d\n", size);
1043 fflush(0);
1044 return;
1045 } /* end print_cpool_bytes */
1048 // Returns size of constant pool entry.
1049 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
1050 switch(tag_at(idx).value()) {
1051 case JVM_CONSTANT_Invalid:
1052 case JVM_CONSTANT_Unicode:
1053 return 1;
1055 case JVM_CONSTANT_Utf8:
1056 return 3 + symbol_at(idx)->utf8_length();
1058 case JVM_CONSTANT_Class:
1059 case JVM_CONSTANT_String:
1060 case JVM_CONSTANT_ClassIndex:
1061 case JVM_CONSTANT_UnresolvedClass:
1062 case JVM_CONSTANT_UnresolvedClassInError:
1063 case JVM_CONSTANT_StringIndex:
1064 case JVM_CONSTANT_UnresolvedString:
1065 return 3;
1067 case JVM_CONSTANT_Integer:
1068 case JVM_CONSTANT_Float:
1069 case JVM_CONSTANT_Fieldref:
1070 case JVM_CONSTANT_Methodref:
1071 case JVM_CONSTANT_InterfaceMethodref:
1072 case JVM_CONSTANT_NameAndType:
1073 return 5;
1075 case JVM_CONSTANT_Long:
1076 case JVM_CONSTANT_Double:
1077 return 9;
1078 }
1079 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1080 return 1;
1081 } /* end cpool_entry_size */
1084 // SymbolHashMap is used to find a constant pool index from a string.
1085 // This function fills in SymbolHashMaps, one for utf8s and one for
1086 // class names, returns size of the cpool raw bytes.
1087 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
1088 SymbolHashMap *classmap) {
1089 jint size = 0;
1091 for (u2 idx = 1; idx < length(); idx++) {
1092 u2 tag = tag_at(idx).value();
1093 size += cpool_entry_size(idx);
1095 switch(tag) {
1096 case JVM_CONSTANT_Utf8: {
1097 symbolOop sym = symbol_at(idx);
1098 symmap->add_entry(sym, idx);
1099 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1100 break;
1101 }
1102 case JVM_CONSTANT_Class:
1103 case JVM_CONSTANT_UnresolvedClass:
1104 case JVM_CONSTANT_UnresolvedClassInError: {
1105 symbolOop sym = klass_name_at(idx);
1106 classmap->add_entry(sym, idx);
1107 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1108 break;
1109 }
1110 case JVM_CONSTANT_Long:
1111 case JVM_CONSTANT_Double: {
1112 idx++; // Both Long and Double take two cpool slots
1113 break;
1114 }
1115 }
1116 }
1117 return size;
1118 } /* end hash_utf8_entries_to */
1121 // Copy cpool bytes.
1122 // Returns:
1123 // 0, in case of OutOfMemoryError
1124 // -1, in case of internal error
1125 // > 0, count of the raw cpool bytes that have been copied
1126 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
1127 SymbolHashMap* tbl,
1128 unsigned char *bytes) {
1129 u2 idx1, idx2;
1130 jint size = 0;
1131 jint cnt = length();
1132 unsigned char *start_bytes = bytes;
1134 for (jint idx = 1; idx < cnt; idx++) {
1135 u1 tag = tag_at(idx).value();
1136 jint ent_size = cpool_entry_size(idx);
1138 assert(size + ent_size <= cpool_size, "Size mismatch");
1140 *bytes = tag;
1141 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1142 switch(tag) {
1143 case JVM_CONSTANT_Invalid: {
1144 DBG(printf("JVM_CONSTANT_Invalid"));
1145 break;
1146 }
1147 case JVM_CONSTANT_Unicode: {
1148 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1149 DBG(printf("JVM_CONSTANT_Unicode"));
1150 break;
1151 }
1152 case JVM_CONSTANT_Utf8: {
1153 symbolOop sym = symbol_at(idx);
1154 char* str = sym->as_utf8();
1155 // Warning! It's crashing on x86 with len = sym->utf8_length()
1156 int len = (int) strlen(str);
1157 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1158 for (int i = 0; i < len; i++) {
1159 bytes[3+i] = (u1) str[i];
1160 }
1161 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1162 break;
1163 }
1164 case JVM_CONSTANT_Integer: {
1165 jint val = int_at(idx);
1166 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1167 break;
1168 }
1169 case JVM_CONSTANT_Float: {
1170 jfloat val = float_at(idx);
1171 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1172 break;
1173 }
1174 case JVM_CONSTANT_Long: {
1175 jlong val = long_at(idx);
1176 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1177 idx++; // Long takes two cpool slots
1178 break;
1179 }
1180 case JVM_CONSTANT_Double: {
1181 jdouble val = double_at(idx);
1182 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1183 idx++; // Double takes two cpool slots
1184 break;
1185 }
1186 case JVM_CONSTANT_Class:
1187 case JVM_CONSTANT_UnresolvedClass:
1188 case JVM_CONSTANT_UnresolvedClassInError: {
1189 *bytes = JVM_CONSTANT_Class;
1190 symbolOop sym = klass_name_at(idx);
1191 idx1 = tbl->symbol_to_value(sym);
1192 assert(idx1 != 0, "Have not found a hashtable entry");
1193 Bytes::put_Java_u2((address) (bytes+1), idx1);
1194 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1195 break;
1196 }
1197 case JVM_CONSTANT_String: {
1198 unsigned int hash;
1199 char *str = string_at_noresolve(idx);
1200 symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
1201 idx1 = tbl->symbol_to_value(sym);
1202 assert(idx1 != 0, "Have not found a hashtable entry");
1203 Bytes::put_Java_u2((address) (bytes+1), idx1);
1204 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
1205 break;
1206 }
1207 case JVM_CONSTANT_UnresolvedString: {
1208 *bytes = JVM_CONSTANT_String;
1209 symbolOop sym = unresolved_string_at(idx);
1210 idx1 = tbl->symbol_to_value(sym);
1211 assert(idx1 != 0, "Have not found a hashtable entry");
1212 Bytes::put_Java_u2((address) (bytes+1), idx1);
1213 DBG(char *str = sym->as_utf8());
1214 DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
1215 break;
1216 }
1217 case JVM_CONSTANT_Fieldref:
1218 case JVM_CONSTANT_Methodref:
1219 case JVM_CONSTANT_InterfaceMethodref: {
1220 idx1 = uncached_klass_ref_index_at(idx);
1221 idx2 = uncached_name_and_type_ref_index_at(idx);
1222 Bytes::put_Java_u2((address) (bytes+1), idx1);
1223 Bytes::put_Java_u2((address) (bytes+3), idx2);
1224 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1225 break;
1226 }
1227 case JVM_CONSTANT_NameAndType: {
1228 idx1 = name_ref_index_at(idx);
1229 idx2 = signature_ref_index_at(idx);
1230 Bytes::put_Java_u2((address) (bytes+1), idx1);
1231 Bytes::put_Java_u2((address) (bytes+3), idx2);
1232 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1233 break;
1234 }
1235 case JVM_CONSTANT_ClassIndex: {
1236 *bytes = JVM_CONSTANT_Class;
1237 idx1 = klass_index_at(idx);
1238 Bytes::put_Java_u2((address) (bytes+1), idx1);
1239 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1240 break;
1241 }
1242 case JVM_CONSTANT_StringIndex: {
1243 *bytes = JVM_CONSTANT_String;
1244 idx1 = string_index_at(idx);
1245 Bytes::put_Java_u2((address) (bytes+1), idx1);
1246 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1247 break;
1248 }
1249 }
1250 DBG(printf("\n"));
1251 bytes += ent_size;
1252 size += ent_size;
1253 }
1254 assert(size == cpool_size, "Size mismatch");
1256 // Keep temorarily for debugging until it's stable.
1257 DBG(print_cpool_bytes(cnt, start_bytes));
1258 return (int)(bytes - start_bytes);
1259 } /* end copy_cpool_bytes */
1262 void SymbolHashMap::add_entry(symbolOop sym, u2 value) {
1263 char *str = sym->as_utf8();
1264 unsigned int hash = compute_hash(str, sym->utf8_length());
1265 unsigned int index = hash % table_size();
1267 // check if already in map
1268 // we prefer the first entry since it is more likely to be what was used in
1269 // the class file
1270 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1271 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1272 if (en->hash() == hash && en->symbol() == sym) {
1273 return; // already there
1274 }
1275 }
1277 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
1278 entry->set_next(bucket(index));
1279 _buckets[index].set_entry(entry);
1280 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1281 }
1283 SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop sym) {
1284 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
1285 char *str = sym->as_utf8();
1286 int len = sym->utf8_length();
1287 unsigned int hash = SymbolHashMap::compute_hash(str, len);
1288 unsigned int index = hash % table_size();
1289 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1290 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1291 if (en->hash() == hash && en->symbol() == sym) {
1292 return en;
1293 }
1294 }
1295 return NULL;
1296 }