Tue, 28 Jul 2009 12:12:40 -0700
6862919: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 07/09
Reviewed-by: 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::impl_name_ref_at(int which, bool uncached) {
253 int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
254 return symbol_at(name_index);
255 }
258 symbolOop constantPoolOopDesc::impl_signature_ref_at(int which, bool uncached) {
259 int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
260 return symbol_at(signature_index);
261 }
264 int constantPoolOopDesc::impl_name_and_type_ref_index_at(int which, bool uncached) {
265 jint ref_index = field_or_method_at(which, uncached);
266 return extract_high_short_from_int(ref_index);
267 }
270 int constantPoolOopDesc::impl_klass_ref_index_at(int which, bool uncached) {
271 jint ref_index = field_or_method_at(which, uncached);
272 return extract_low_short_from_int(ref_index);
273 }
277 int constantPoolOopDesc::map_instruction_operand_to_index(int operand) {
278 if (constantPoolCacheOopDesc::is_secondary_index(operand)) {
279 return cache()->main_entry_at(operand)->constant_pool_index();
280 }
281 assert((int)(u2)operand == operand, "clean u2");
282 int index = Bytes::swap_u2(operand);
283 return cache()->entry_at(index)->constant_pool_index();
284 }
287 void constantPoolOopDesc::verify_constant_pool_resolve(constantPoolHandle this_oop, KlassHandle k, TRAPS) {
288 if (k->oop_is_instance() || k->oop_is_objArray()) {
289 instanceKlassHandle holder (THREAD, this_oop->pool_holder());
290 klassOop elem_oop = k->oop_is_instance() ? k() : objArrayKlass::cast(k())->bottom_klass();
291 KlassHandle element (THREAD, elem_oop);
293 // The element type could be a typeArray - we only need the access check if it is
294 // an reference to another class
295 if (element->oop_is_instance()) {
296 LinkResolver::check_klass_accessability(holder, element, CHECK);
297 }
298 }
299 }
302 int constantPoolOopDesc::name_ref_index_at(int which_nt) {
303 jint ref_index = name_and_type_at(which_nt);
304 return extract_low_short_from_int(ref_index);
305 }
308 int constantPoolOopDesc::signature_ref_index_at(int which_nt) {
309 jint ref_index = name_and_type_at(which_nt);
310 return extract_high_short_from_int(ref_index);
311 }
314 klassOop constantPoolOopDesc::klass_ref_at(int which, TRAPS) {
315 return klass_at(klass_ref_index_at(which), CHECK_NULL);
316 }
319 symbolOop constantPoolOopDesc::klass_name_at(int which) {
320 assert(tag_at(which).is_unresolved_klass() || tag_at(which).is_klass(),
321 "Corrupted constant pool");
322 // A resolved constantPool entry will contain a klassOop, otherwise a symbolOop.
323 // It is not safe to rely on the tag bit's here, since we don't have a lock, and the entry and
324 // tag is not updated atomicly.
325 oop entry = *(obj_at_addr(which));
326 if (entry->is_klass()) {
327 // Already resolved - return entry's name.
328 return klassOop(entry)->klass_part()->name();
329 } else {
330 assert(entry->is_symbol(), "must be either symbol or klass");
331 return (symbolOop)entry;
332 }
333 }
335 symbolOop constantPoolOopDesc::klass_ref_at_noresolve(int which) {
336 jint ref_index = klass_ref_index_at(which);
337 return klass_at_noresolve(ref_index);
338 }
340 char* constantPoolOopDesc::string_at_noresolve(int which) {
341 // Test entry type in case string is resolved while in here.
342 oop entry = *(obj_at_addr(which));
343 if (entry->is_symbol()) {
344 return ((symbolOop)entry)->as_C_string();
345 } else if (java_lang_String::is_instance(entry)) {
346 return java_lang_String::as_utf8_string(entry);
347 } else {
348 return (char*)"<pseudo-string>";
349 }
350 }
353 BasicType constantPoolOopDesc::basic_type_for_signature_at(int which) {
354 return FieldType::basic_type(symbol_at(which));
355 }
358 void constantPoolOopDesc::resolve_string_constants_impl(constantPoolHandle this_oop, TRAPS) {
359 for (int index = 1; index < this_oop->length(); index++) { // Index 0 is unused
360 if (this_oop->tag_at(index).is_unresolved_string()) {
361 this_oop->string_at(index, CHECK);
362 }
363 }
364 }
366 oop constantPoolOopDesc::string_at_impl(constantPoolHandle this_oop, int which, TRAPS) {
367 oop entry = *(this_oop->obj_at_addr(which));
368 if (entry->is_symbol()) {
369 ObjectLocker ol(this_oop, THREAD);
370 if (this_oop->tag_at(which).is_unresolved_string()) {
371 // Intern string
372 symbolOop sym = this_oop->unresolved_string_at(which);
373 entry = StringTable::intern(sym, CHECK_(constantPoolOop(NULL)));
374 this_oop->string_at_put(which, entry);
375 } else {
376 // Another thread beat us and interned string, read string from constant pool
377 entry = this_oop->resolved_string_at(which);
378 }
379 }
380 assert(java_lang_String::is_instance(entry), "must be string");
381 return entry;
382 }
385 bool constantPoolOopDesc::is_pseudo_string_at(int which) {
386 oop entry = *(obj_at_addr(which));
387 if (entry->is_symbol())
388 // Not yet resolved, but it will resolve to a string.
389 return false;
390 else if (java_lang_String::is_instance(entry))
391 return false; // actually, it might be a non-interned or non-perm string
392 else
393 // truly pseudo
394 return true;
395 }
398 bool constantPoolOopDesc::klass_name_at_matches(instanceKlassHandle k,
399 int which) {
400 // Names are interned, so we can compare symbolOops directly
401 symbolOop cp_name = klass_name_at(which);
402 return (cp_name == k->name());
403 }
406 int constantPoolOopDesc::pre_resolve_shared_klasses(TRAPS) {
407 ResourceMark rm;
408 int count = 0;
409 for (int index = 1; index < tags()->length(); index++) { // Index 0 is unused
410 if (tag_at(index).is_unresolved_string()) {
411 // Intern string
412 symbolOop sym = unresolved_string_at(index);
413 oop entry = StringTable::intern(sym, CHECK_(-1));
414 string_at_put(index, entry);
415 }
416 }
417 return count;
418 }
421 // Iterate over symbols which are used as class, field, method names and
422 // signatures (in preparation for writing to the shared archive).
424 void constantPoolOopDesc::shared_symbols_iterate(OopClosure* closure) {
425 for (int index = 1; index < length(); index++) { // Index 0 is unused
426 switch (tag_at(index).value()) {
428 case JVM_CONSTANT_UnresolvedClass:
429 closure->do_oop(obj_at_addr(index));
430 break;
432 case JVM_CONSTANT_NameAndType:
433 {
434 int i = *int_at_addr(index);
435 closure->do_oop(obj_at_addr((unsigned)i >> 16));
436 closure->do_oop(obj_at_addr((unsigned)i & 0xffff));
437 }
438 break;
440 case JVM_CONSTANT_Class:
441 case JVM_CONSTANT_InterfaceMethodref:
442 case JVM_CONSTANT_Fieldref:
443 case JVM_CONSTANT_Methodref:
444 case JVM_CONSTANT_Integer:
445 case JVM_CONSTANT_Float:
446 // Do nothing! Not an oop.
447 // These constant types do not reference symbols at this point.
448 break;
450 case JVM_CONSTANT_String:
451 // Do nothing! Not a symbol.
452 break;
454 case JVM_CONSTANT_UnresolvedString:
455 case JVM_CONSTANT_Utf8:
456 // These constants are symbols, but unless these symbols are
457 // actually to be used for something, we don't want to mark them.
458 break;
460 case JVM_CONSTANT_Long:
461 case JVM_CONSTANT_Double:
462 // Do nothing! Not an oop. (But takes two pool entries.)
463 ++index;
464 break;
466 default:
467 ShouldNotReachHere();
468 break;
469 }
470 }
471 }
474 // Iterate over the [one] tags array (in preparation for writing to the
475 // shared archive).
477 void constantPoolOopDesc::shared_tags_iterate(OopClosure* closure) {
478 closure->do_oop(tags_addr());
479 }
482 // Iterate over String objects (in preparation for writing to the shared
483 // archive).
485 void constantPoolOopDesc::shared_strings_iterate(OopClosure* closure) {
486 for (int index = 1; index < length(); index++) { // Index 0 is unused
487 switch (tag_at(index).value()) {
489 case JVM_CONSTANT_UnresolvedClass:
490 case JVM_CONSTANT_NameAndType:
491 // Do nothing! Not a String.
492 break;
494 case JVM_CONSTANT_Class:
495 case JVM_CONSTANT_InterfaceMethodref:
496 case JVM_CONSTANT_Fieldref:
497 case JVM_CONSTANT_Methodref:
498 case JVM_CONSTANT_Integer:
499 case JVM_CONSTANT_Float:
500 // Do nothing! Not an oop.
501 // These constant types do not reference symbols at this point.
502 break;
504 case JVM_CONSTANT_String:
505 closure->do_oop(obj_at_addr(index));
506 break;
508 case JVM_CONSTANT_UnresolvedString:
509 case JVM_CONSTANT_Utf8:
510 // These constants are symbols, but unless these symbols are
511 // actually to be used for something, we don't want to mark them.
512 break;
514 case JVM_CONSTANT_Long:
515 case JVM_CONSTANT_Double:
516 // Do nothing! Not an oop. (But takes two pool entries.)
517 ++index;
518 break;
520 default:
521 ShouldNotReachHere();
522 break;
523 }
524 }
525 }
528 // Compare this constant pool's entry at index1 to the constant pool
529 // cp2's entry at index2.
530 bool constantPoolOopDesc::compare_entry_to(int index1, constantPoolHandle cp2,
531 int index2, TRAPS) {
533 jbyte t1 = tag_at(index1).value();
534 jbyte t2 = cp2->tag_at(index2).value();
537 // JVM_CONSTANT_UnresolvedClassInError is equal to JVM_CONSTANT_UnresolvedClass
538 // when comparing
539 if (t1 == JVM_CONSTANT_UnresolvedClassInError) {
540 t1 = JVM_CONSTANT_UnresolvedClass;
541 }
542 if (t2 == JVM_CONSTANT_UnresolvedClassInError) {
543 t2 = JVM_CONSTANT_UnresolvedClass;
544 }
546 if (t1 != t2) {
547 // Not the same entry type so there is nothing else to check. Note
548 // that this style of checking will consider resolved/unresolved
549 // class pairs and resolved/unresolved string pairs as different.
550 // From the constantPoolOop API point of view, this is correct
551 // behavior. See constantPoolKlass::merge() to see how this plays
552 // out in the context of constantPoolOop merging.
553 return false;
554 }
556 switch (t1) {
557 case JVM_CONSTANT_Class:
558 {
559 klassOop k1 = klass_at(index1, CHECK_false);
560 klassOop k2 = cp2->klass_at(index2, CHECK_false);
561 if (k1 == k2) {
562 return true;
563 }
564 } break;
566 case JVM_CONSTANT_ClassIndex:
567 {
568 int recur1 = klass_index_at(index1);
569 int recur2 = cp2->klass_index_at(index2);
570 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
571 if (match) {
572 return true;
573 }
574 } break;
576 case JVM_CONSTANT_Double:
577 {
578 jdouble d1 = double_at(index1);
579 jdouble d2 = cp2->double_at(index2);
580 if (d1 == d2) {
581 return true;
582 }
583 } break;
585 case JVM_CONSTANT_Fieldref:
586 case JVM_CONSTANT_InterfaceMethodref:
587 case JVM_CONSTANT_Methodref:
588 {
589 int recur1 = uncached_klass_ref_index_at(index1);
590 int recur2 = cp2->uncached_klass_ref_index_at(index2);
591 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
592 if (match) {
593 recur1 = uncached_name_and_type_ref_index_at(index1);
594 recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
595 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
596 if (match) {
597 return true;
598 }
599 }
600 } break;
602 case JVM_CONSTANT_Float:
603 {
604 jfloat f1 = float_at(index1);
605 jfloat f2 = cp2->float_at(index2);
606 if (f1 == f2) {
607 return true;
608 }
609 } break;
611 case JVM_CONSTANT_Integer:
612 {
613 jint i1 = int_at(index1);
614 jint i2 = cp2->int_at(index2);
615 if (i1 == i2) {
616 return true;
617 }
618 } break;
620 case JVM_CONSTANT_Long:
621 {
622 jlong l1 = long_at(index1);
623 jlong l2 = cp2->long_at(index2);
624 if (l1 == l2) {
625 return true;
626 }
627 } break;
629 case JVM_CONSTANT_NameAndType:
630 {
631 int recur1 = name_ref_index_at(index1);
632 int recur2 = cp2->name_ref_index_at(index2);
633 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
634 if (match) {
635 recur1 = signature_ref_index_at(index1);
636 recur2 = cp2->signature_ref_index_at(index2);
637 match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
638 if (match) {
639 return true;
640 }
641 }
642 } break;
644 case JVM_CONSTANT_String:
645 {
646 oop s1 = string_at(index1, CHECK_false);
647 oop s2 = cp2->string_at(index2, CHECK_false);
648 if (s1 == s2) {
649 return true;
650 }
651 } break;
653 case JVM_CONSTANT_StringIndex:
654 {
655 int recur1 = string_index_at(index1);
656 int recur2 = cp2->string_index_at(index2);
657 bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
658 if (match) {
659 return true;
660 }
661 } break;
663 case JVM_CONSTANT_UnresolvedClass:
664 {
665 symbolOop k1 = unresolved_klass_at(index1);
666 symbolOop k2 = cp2->unresolved_klass_at(index2);
667 if (k1 == k2) {
668 return true;
669 }
670 } break;
672 case JVM_CONSTANT_UnresolvedString:
673 {
674 symbolOop s1 = unresolved_string_at(index1);
675 symbolOop s2 = cp2->unresolved_string_at(index2);
676 if (s1 == s2) {
677 return true;
678 }
679 } break;
681 case JVM_CONSTANT_Utf8:
682 {
683 symbolOop s1 = symbol_at(index1);
684 symbolOop s2 = cp2->symbol_at(index2);
685 if (s1 == s2) {
686 return true;
687 }
688 } break;
690 // Invalid is used as the tag for the second constant pool entry
691 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
692 // not be seen by itself.
693 case JVM_CONSTANT_Invalid: // fall through
695 default:
696 ShouldNotReachHere();
697 break;
698 }
700 return false;
701 } // end compare_entry_to()
704 // Copy this constant pool's entries at start_i to end_i (inclusive)
705 // to the constant pool to_cp's entries starting at to_i. A total of
706 // (end_i - start_i) + 1 entries are copied.
707 void constantPoolOopDesc::copy_cp_to(int start_i, int end_i,
708 constantPoolHandle to_cp, int to_i, TRAPS) {
710 int dest_i = to_i; // leave original alone for debug purposes
712 for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
713 copy_entry_to(src_i, to_cp, dest_i, CHECK);
715 switch (tag_at(src_i).value()) {
716 case JVM_CONSTANT_Double:
717 case JVM_CONSTANT_Long:
718 // double and long take two constant pool entries
719 src_i += 2;
720 dest_i += 2;
721 break;
723 default:
724 // all others take one constant pool entry
725 src_i++;
726 dest_i++;
727 break;
728 }
729 }
730 } // end copy_cp_to()
733 // Copy this constant pool's entry at from_i to the constant pool
734 // to_cp's entry at to_i.
735 void constantPoolOopDesc::copy_entry_to(int from_i, constantPoolHandle to_cp,
736 int to_i, TRAPS) {
738 switch (tag_at(from_i).value()) {
739 case JVM_CONSTANT_Class:
740 {
741 klassOop k = klass_at(from_i, CHECK);
742 to_cp->klass_at_put(to_i, k);
743 } break;
745 case JVM_CONSTANT_ClassIndex:
746 {
747 jint ki = klass_index_at(from_i);
748 to_cp->klass_index_at_put(to_i, ki);
749 } break;
751 case JVM_CONSTANT_Double:
752 {
753 jdouble d = double_at(from_i);
754 to_cp->double_at_put(to_i, d);
755 // double takes two constant pool entries so init second entry's tag
756 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
757 } break;
759 case JVM_CONSTANT_Fieldref:
760 {
761 int class_index = uncached_klass_ref_index_at(from_i);
762 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
763 to_cp->field_at_put(to_i, class_index, name_and_type_index);
764 } break;
766 case JVM_CONSTANT_Float:
767 {
768 jfloat f = float_at(from_i);
769 to_cp->float_at_put(to_i, f);
770 } break;
772 case JVM_CONSTANT_Integer:
773 {
774 jint i = int_at(from_i);
775 to_cp->int_at_put(to_i, i);
776 } break;
778 case JVM_CONSTANT_InterfaceMethodref:
779 {
780 int class_index = uncached_klass_ref_index_at(from_i);
781 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
782 to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
783 } break;
785 case JVM_CONSTANT_Long:
786 {
787 jlong l = long_at(from_i);
788 to_cp->long_at_put(to_i, l);
789 // long takes two constant pool entries so init second entry's tag
790 to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
791 } break;
793 case JVM_CONSTANT_Methodref:
794 {
795 int class_index = uncached_klass_ref_index_at(from_i);
796 int name_and_type_index = uncached_name_and_type_ref_index_at(from_i);
797 to_cp->method_at_put(to_i, class_index, name_and_type_index);
798 } break;
800 case JVM_CONSTANT_NameAndType:
801 {
802 int name_ref_index = name_ref_index_at(from_i);
803 int signature_ref_index = signature_ref_index_at(from_i);
804 to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
805 } break;
807 case JVM_CONSTANT_String:
808 {
809 oop s = string_at(from_i, CHECK);
810 to_cp->string_at_put(to_i, s);
811 } break;
813 case JVM_CONSTANT_StringIndex:
814 {
815 jint si = string_index_at(from_i);
816 to_cp->string_index_at_put(to_i, si);
817 } break;
819 case JVM_CONSTANT_UnresolvedClass:
820 {
821 symbolOop k = unresolved_klass_at(from_i);
822 to_cp->unresolved_klass_at_put(to_i, k);
823 } break;
825 case JVM_CONSTANT_UnresolvedClassInError:
826 {
827 symbolOop k = unresolved_klass_at(from_i);
828 to_cp->unresolved_klass_at_put(to_i, k);
829 to_cp->tag_at_put(to_i, JVM_CONSTANT_UnresolvedClassInError);
830 } break;
833 case JVM_CONSTANT_UnresolvedString:
834 {
835 symbolOop s = unresolved_string_at(from_i);
836 to_cp->unresolved_string_at_put(to_i, s);
837 } break;
839 case JVM_CONSTANT_Utf8:
840 {
841 symbolOop s = symbol_at(from_i);
842 to_cp->symbol_at_put(to_i, s);
843 } break;
845 // Invalid is used as the tag for the second constant pool entry
846 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
847 // not be seen by itself.
848 case JVM_CONSTANT_Invalid: // fall through
850 default:
851 {
852 jbyte bad_value = tag_at(from_i).value(); // leave a breadcrumb
853 ShouldNotReachHere();
854 } break;
855 }
856 } // end copy_entry_to()
859 // Search constant pool search_cp for an entry that matches this
860 // constant pool's entry at pattern_i. Returns the index of a
861 // matching entry or zero (0) if there is no matching entry.
862 int constantPoolOopDesc::find_matching_entry(int pattern_i,
863 constantPoolHandle search_cp, TRAPS) {
865 // index zero (0) is not used
866 for (int i = 1; i < search_cp->length(); i++) {
867 bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
868 if (found) {
869 return i;
870 }
871 }
873 return 0; // entry not found; return unused index zero (0)
874 } // end find_matching_entry()
877 #ifndef PRODUCT
879 const char* constantPoolOopDesc::printable_name_at(int which) {
881 constantTag tag = tag_at(which);
883 if (tag.is_unresolved_string() || tag.is_string()) {
884 return string_at_noresolve(which);
885 } else if (tag.is_klass() || tag.is_unresolved_klass()) {
886 return klass_name_at(which)->as_C_string();
887 } else if (tag.is_symbol()) {
888 return symbol_at(which)->as_C_string();
889 }
890 return "";
891 }
893 #endif // PRODUCT
896 // JVMTI GetConstantPool support
898 // For temporary use until code is stable.
899 #define DBG(code)
901 static const char* WARN_MSG = "Must not be such entry!";
903 static void print_cpool_bytes(jint cnt, u1 *bytes) {
904 jint size = 0;
905 u2 idx1, idx2;
907 for (jint idx = 1; idx < cnt; idx++) {
908 jint ent_size = 0;
909 u1 tag = *bytes++;
910 size++; // count tag
912 printf("const #%03d, tag: %02d ", idx, tag);
913 switch(tag) {
914 case JVM_CONSTANT_Invalid: {
915 printf("Invalid");
916 break;
917 }
918 case JVM_CONSTANT_Unicode: {
919 printf("Unicode %s", WARN_MSG);
920 break;
921 }
922 case JVM_CONSTANT_Utf8: {
923 u2 len = Bytes::get_Java_u2(bytes);
924 char str[128];
925 if (len > 127) {
926 len = 127;
927 }
928 strncpy(str, (char *) (bytes+2), len);
929 str[len] = '\0';
930 printf("Utf8 \"%s\"", str);
931 ent_size = 2 + len;
932 break;
933 }
934 case JVM_CONSTANT_Integer: {
935 u4 val = Bytes::get_Java_u4(bytes);
936 printf("int %d", *(int *) &val);
937 ent_size = 4;
938 break;
939 }
940 case JVM_CONSTANT_Float: {
941 u4 val = Bytes::get_Java_u4(bytes);
942 printf("float %5.3ff", *(float *) &val);
943 ent_size = 4;
944 break;
945 }
946 case JVM_CONSTANT_Long: {
947 u8 val = Bytes::get_Java_u8(bytes);
948 printf("long "INT64_FORMAT, *(jlong *) &val);
949 ent_size = 8;
950 idx++; // Long takes two cpool slots
951 break;
952 }
953 case JVM_CONSTANT_Double: {
954 u8 val = Bytes::get_Java_u8(bytes);
955 printf("double %5.3fd", *(jdouble *)&val);
956 ent_size = 8;
957 idx++; // Double takes two cpool slots
958 break;
959 }
960 case JVM_CONSTANT_Class: {
961 idx1 = Bytes::get_Java_u2(bytes);
962 printf("class #%03d", idx1);
963 ent_size = 2;
964 break;
965 }
966 case JVM_CONSTANT_String: {
967 idx1 = Bytes::get_Java_u2(bytes);
968 printf("String #%03d", idx1);
969 ent_size = 2;
970 break;
971 }
972 case JVM_CONSTANT_Fieldref: {
973 idx1 = Bytes::get_Java_u2(bytes);
974 idx2 = Bytes::get_Java_u2(bytes+2);
975 printf("Field #%03d, #%03d", (int) idx1, (int) idx2);
976 ent_size = 4;
977 break;
978 }
979 case JVM_CONSTANT_Methodref: {
980 idx1 = Bytes::get_Java_u2(bytes);
981 idx2 = Bytes::get_Java_u2(bytes+2);
982 printf("Method #%03d, #%03d", idx1, idx2);
983 ent_size = 4;
984 break;
985 }
986 case JVM_CONSTANT_InterfaceMethodref: {
987 idx1 = Bytes::get_Java_u2(bytes);
988 idx2 = Bytes::get_Java_u2(bytes+2);
989 printf("InterfMethod #%03d, #%03d", idx1, idx2);
990 ent_size = 4;
991 break;
992 }
993 case JVM_CONSTANT_NameAndType: {
994 idx1 = Bytes::get_Java_u2(bytes);
995 idx2 = Bytes::get_Java_u2(bytes+2);
996 printf("NameAndType #%03d, #%03d", idx1, idx2);
997 ent_size = 4;
998 break;
999 }
1000 case JVM_CONSTANT_ClassIndex: {
1001 printf("ClassIndex %s", WARN_MSG);
1002 break;
1003 }
1004 case JVM_CONSTANT_UnresolvedClass: {
1005 printf("UnresolvedClass: %s", WARN_MSG);
1006 break;
1007 }
1008 case JVM_CONSTANT_UnresolvedClassInError: {
1009 printf("UnresolvedClassInErr: %s", WARN_MSG);
1010 break;
1011 }
1012 case JVM_CONSTANT_StringIndex: {
1013 printf("StringIndex: %s", WARN_MSG);
1014 break;
1015 }
1016 case JVM_CONSTANT_UnresolvedString: {
1017 printf("UnresolvedString: %s", WARN_MSG);
1018 break;
1019 }
1020 }
1021 printf(";\n");
1022 bytes += ent_size;
1023 size += ent_size;
1024 }
1025 printf("Cpool size: %d\n", size);
1026 fflush(0);
1027 return;
1028 } /* end print_cpool_bytes */
1031 // Returns size of constant pool entry.
1032 jint constantPoolOopDesc::cpool_entry_size(jint idx) {
1033 switch(tag_at(idx).value()) {
1034 case JVM_CONSTANT_Invalid:
1035 case JVM_CONSTANT_Unicode:
1036 return 1;
1038 case JVM_CONSTANT_Utf8:
1039 return 3 + symbol_at(idx)->utf8_length();
1041 case JVM_CONSTANT_Class:
1042 case JVM_CONSTANT_String:
1043 case JVM_CONSTANT_ClassIndex:
1044 case JVM_CONSTANT_UnresolvedClass:
1045 case JVM_CONSTANT_UnresolvedClassInError:
1046 case JVM_CONSTANT_StringIndex:
1047 case JVM_CONSTANT_UnresolvedString:
1048 return 3;
1050 case JVM_CONSTANT_Integer:
1051 case JVM_CONSTANT_Float:
1052 case JVM_CONSTANT_Fieldref:
1053 case JVM_CONSTANT_Methodref:
1054 case JVM_CONSTANT_InterfaceMethodref:
1055 case JVM_CONSTANT_NameAndType:
1056 return 5;
1058 case JVM_CONSTANT_Long:
1059 case JVM_CONSTANT_Double:
1060 return 9;
1061 }
1062 assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1063 return 1;
1064 } /* end cpool_entry_size */
1067 // SymbolHashMap is used to find a constant pool index from a string.
1068 // This function fills in SymbolHashMaps, one for utf8s and one for
1069 // class names, returns size of the cpool raw bytes.
1070 jint constantPoolOopDesc::hash_entries_to(SymbolHashMap *symmap,
1071 SymbolHashMap *classmap) {
1072 jint size = 0;
1074 for (u2 idx = 1; idx < length(); idx++) {
1075 u2 tag = tag_at(idx).value();
1076 size += cpool_entry_size(idx);
1078 switch(tag) {
1079 case JVM_CONSTANT_Utf8: {
1080 symbolOop sym = symbol_at(idx);
1081 symmap->add_entry(sym, idx);
1082 DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1083 break;
1084 }
1085 case JVM_CONSTANT_Class:
1086 case JVM_CONSTANT_UnresolvedClass:
1087 case JVM_CONSTANT_UnresolvedClassInError: {
1088 symbolOop sym = klass_name_at(idx);
1089 classmap->add_entry(sym, idx);
1090 DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1091 break;
1092 }
1093 case JVM_CONSTANT_Long:
1094 case JVM_CONSTANT_Double: {
1095 idx++; // Both Long and Double take two cpool slots
1096 break;
1097 }
1098 }
1099 }
1100 return size;
1101 } /* end hash_utf8_entries_to */
1104 // Copy cpool bytes.
1105 // Returns:
1106 // 0, in case of OutOfMemoryError
1107 // -1, in case of internal error
1108 // > 0, count of the raw cpool bytes that have been copied
1109 int constantPoolOopDesc::copy_cpool_bytes(int cpool_size,
1110 SymbolHashMap* tbl,
1111 unsigned char *bytes) {
1112 u2 idx1, idx2;
1113 jint size = 0;
1114 jint cnt = length();
1115 unsigned char *start_bytes = bytes;
1117 for (jint idx = 1; idx < cnt; idx++) {
1118 u1 tag = tag_at(idx).value();
1119 jint ent_size = cpool_entry_size(idx);
1121 assert(size + ent_size <= cpool_size, "Size mismatch");
1123 *bytes = tag;
1124 DBG(printf("#%03hd tag=%03hd, ", idx, tag));
1125 switch(tag) {
1126 case JVM_CONSTANT_Invalid: {
1127 DBG(printf("JVM_CONSTANT_Invalid"));
1128 break;
1129 }
1130 case JVM_CONSTANT_Unicode: {
1131 assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1132 DBG(printf("JVM_CONSTANT_Unicode"));
1133 break;
1134 }
1135 case JVM_CONSTANT_Utf8: {
1136 symbolOop sym = symbol_at(idx);
1137 char* str = sym->as_utf8();
1138 // Warning! It's crashing on x86 with len = sym->utf8_length()
1139 int len = (int) strlen(str);
1140 Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1141 for (int i = 0; i < len; i++) {
1142 bytes[3+i] = (u1) str[i];
1143 }
1144 DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1145 break;
1146 }
1147 case JVM_CONSTANT_Integer: {
1148 jint val = int_at(idx);
1149 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1150 break;
1151 }
1152 case JVM_CONSTANT_Float: {
1153 jfloat val = float_at(idx);
1154 Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1155 break;
1156 }
1157 case JVM_CONSTANT_Long: {
1158 jlong val = long_at(idx);
1159 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1160 idx++; // Long takes two cpool slots
1161 break;
1162 }
1163 case JVM_CONSTANT_Double: {
1164 jdouble val = double_at(idx);
1165 Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1166 idx++; // Double takes two cpool slots
1167 break;
1168 }
1169 case JVM_CONSTANT_Class:
1170 case JVM_CONSTANT_UnresolvedClass:
1171 case JVM_CONSTANT_UnresolvedClassInError: {
1172 *bytes = JVM_CONSTANT_Class;
1173 symbolOop sym = klass_name_at(idx);
1174 idx1 = tbl->symbol_to_value(sym);
1175 assert(idx1 != 0, "Have not found a hashtable entry");
1176 Bytes::put_Java_u2((address) (bytes+1), idx1);
1177 DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1178 break;
1179 }
1180 case JVM_CONSTANT_String: {
1181 unsigned int hash;
1182 char *str = string_at_noresolve(idx);
1183 symbolOop sym = SymbolTable::lookup_only(str, (int) strlen(str), hash);
1184 if (sym == NULL) {
1185 // sym can be NULL if string refers to incorrectly encoded JVM_CONSTANT_Utf8
1186 // this can happen with JVM TI; see CR 6839599 for more details
1187 oop string = *(obj_at_addr(idx));
1188 assert(java_lang_String::is_instance(string),"Not a String");
1189 DBG(printf("Error #%03hd tag=%03hd\n", idx, tag));
1190 idx1 = 0;
1191 for (int j = 0; j < tbl->table_size() && idx1 == 0; j++) {
1192 for (SymbolHashMapEntry* cur = tbl->bucket(j); cur != NULL; cur = cur->next()) {
1193 int length;
1194 sym = cur->symbol();
1195 jchar* chars = sym->as_unicode(length);
1196 if (java_lang_String::equals(string, chars, length)) {
1197 idx1 = cur->value();
1198 DBG(printf("Index found: %d\n",idx1));
1199 break;
1200 }
1201 }
1202 }
1203 } else {
1204 idx1 = tbl->symbol_to_value(sym);
1205 }
1206 assert(idx1 != 0, "Have not found a hashtable entry");
1207 Bytes::put_Java_u2((address) (bytes+1), idx1);
1208 DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, str));
1209 break;
1210 }
1211 case JVM_CONSTANT_UnresolvedString: {
1212 *bytes = JVM_CONSTANT_String;
1213 symbolOop sym = unresolved_string_at(idx);
1214 idx1 = tbl->symbol_to_value(sym);
1215 assert(idx1 != 0, "Have not found a hashtable entry");
1216 Bytes::put_Java_u2((address) (bytes+1), idx1);
1217 DBG(char *str = sym->as_utf8());
1218 DBG(printf("JVM_CONSTANT_UnresolvedString: idx=#%03hd, %s", idx1, str));
1219 break;
1220 }
1221 case JVM_CONSTANT_Fieldref:
1222 case JVM_CONSTANT_Methodref:
1223 case JVM_CONSTANT_InterfaceMethodref: {
1224 idx1 = uncached_klass_ref_index_at(idx);
1225 idx2 = uncached_name_and_type_ref_index_at(idx);
1226 Bytes::put_Java_u2((address) (bytes+1), idx1);
1227 Bytes::put_Java_u2((address) (bytes+3), idx2);
1228 DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1229 break;
1230 }
1231 case JVM_CONSTANT_NameAndType: {
1232 idx1 = name_ref_index_at(idx);
1233 idx2 = signature_ref_index_at(idx);
1234 Bytes::put_Java_u2((address) (bytes+1), idx1);
1235 Bytes::put_Java_u2((address) (bytes+3), idx2);
1236 DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1237 break;
1238 }
1239 case JVM_CONSTANT_ClassIndex: {
1240 *bytes = JVM_CONSTANT_Class;
1241 idx1 = klass_index_at(idx);
1242 Bytes::put_Java_u2((address) (bytes+1), idx1);
1243 DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1244 break;
1245 }
1246 case JVM_CONSTANT_StringIndex: {
1247 *bytes = JVM_CONSTANT_String;
1248 idx1 = string_index_at(idx);
1249 Bytes::put_Java_u2((address) (bytes+1), idx1);
1250 DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1251 break;
1252 }
1253 }
1254 DBG(printf("\n"));
1255 bytes += ent_size;
1256 size += ent_size;
1257 }
1258 assert(size == cpool_size, "Size mismatch");
1260 // Keep temorarily for debugging until it's stable.
1261 DBG(print_cpool_bytes(cnt, start_bytes));
1262 return (int)(bytes - start_bytes);
1263 } /* end copy_cpool_bytes */
1266 void SymbolHashMap::add_entry(symbolOop sym, u2 value) {
1267 char *str = sym->as_utf8();
1268 unsigned int hash = compute_hash(str, sym->utf8_length());
1269 unsigned int index = hash % table_size();
1271 // check if already in map
1272 // we prefer the first entry since it is more likely to be what was used in
1273 // the class file
1274 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1275 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1276 if (en->hash() == hash && en->symbol() == sym) {
1277 return; // already there
1278 }
1279 }
1281 SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
1282 entry->set_next(bucket(index));
1283 _buckets[index].set_entry(entry);
1284 assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1285 }
1287 SymbolHashMapEntry* SymbolHashMap::find_entry(symbolOop sym) {
1288 assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
1289 char *str = sym->as_utf8();
1290 int len = sym->utf8_length();
1291 unsigned int hash = SymbolHashMap::compute_hash(str, len);
1292 unsigned int index = hash % table_size();
1293 for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
1294 assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
1295 if (en->hash() == hash && en->symbol() == sym) {
1296 return en;
1297 }
1298 }
1299 return NULL;
1300 }