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