20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/verificationType.hpp"
32 #include "classfile/verifier.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/gcLocker.hpp"
36 #include "memory/oopFactory.hpp"
37 #include "memory/universe.inline.hpp"
38 #include "oops/constantPoolOop.hpp"
39 #include "oops/instanceKlass.hpp"
40 #include "oops/klass.inline.hpp"
41 #include "oops/klassOop.hpp"
42 #include "oops/klassVtable.hpp"
43 #include "oops/methodOop.hpp"
44 #include "oops/symbol.hpp"
45 #include "prims/jvmtiExport.hpp"
46 #include "runtime/javaCalls.hpp"
47 #include "runtime/perfData.hpp"
48 #include "runtime/reflection.hpp"
49 #include "runtime/signature.hpp"
50 #include "runtime/timer.hpp"
51 #include "services/classLoadingService.hpp"
52 #include "services/threadService.hpp"
53
54 // We generally try to create the oops directly when parsing, rather than
55 // allocating temporary data structures and copying the bytes twice. A
56 // temporary area is only needed when parsing utf8 entries in the constant
57 // pool and when parsing line number tables.
58
59 // We add assert in debug mode when class format is not checked.
2589 int runtime_visible_annotations_length,
2590 u1* runtime_invisible_annotations,
2591 int runtime_invisible_annotations_length, TRAPS) {
2592 typeArrayHandle annotations;
2593 if (runtime_visible_annotations != NULL ||
2594 runtime_invisible_annotations != NULL) {
2595 typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2596 runtime_invisible_annotations_length, CHECK_(annotations));
2597 annotations = typeArrayHandle(THREAD, anno);
2598 if (runtime_visible_annotations != NULL) {
2599 memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2600 }
2601 if (runtime_invisible_annotations != NULL) {
2602 memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2603 }
2604 }
2605 return annotations;
2606 }
2607
2608
2609 static void initialize_static_field(fieldDescriptor* fd, TRAPS) {
2610 KlassHandle h_k (THREAD, fd->field_holder());
2611 assert(h_k.not_null() && fd->is_static(), "just checking");
2612 if (fd->has_initial_value()) {
2613 BasicType t = fd->field_type();
2614 switch (t) {
2615 case T_BYTE:
2616 h_k()->byte_field_put(fd->offset(), fd->int_initial_value());
2617 break;
2618 case T_BOOLEAN:
2619 h_k()->bool_field_put(fd->offset(), fd->int_initial_value());
2620 break;
2621 case T_CHAR:
2622 h_k()->char_field_put(fd->offset(), fd->int_initial_value());
2623 break;
2624 case T_SHORT:
2625 h_k()->short_field_put(fd->offset(), fd->int_initial_value());
2626 break;
2627 case T_INT:
2628 h_k()->int_field_put(fd->offset(), fd->int_initial_value());
2629 break;
2630 case T_FLOAT:
2631 h_k()->float_field_put(fd->offset(), fd->float_initial_value());
2632 break;
2633 case T_DOUBLE:
2634 h_k()->double_field_put(fd->offset(), fd->double_initial_value());
2635 break;
2636 case T_LONG:
2637 h_k()->long_field_put(fd->offset(), fd->long_initial_value());
2638 break;
2639 case T_OBJECT:
2640 {
2641 #ifdef ASSERT
2642 TempNewSymbol sym = SymbolTable::new_symbol("Ljava/lang/String;", CHECK);
2643 assert(fd->signature() == sym, "just checking");
2644 #endif
2645 oop string = fd->string_initial_value(CHECK);
2646 h_k()->obj_field_put(fd->offset(), string);
2647 }
2648 break;
2649 default:
2650 THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
2651 "Illegal ConstantValue attribute in class file");
2652 }
2653 }
2654 }
2655
2656
2657 void ClassFileParser::java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
2658 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS) {
2659 // This code is for compatibility with earlier jdk's that do not
2660 // have the "discovered" field in java.lang.ref.Reference. For 1.5
2661 // the check for the "discovered" field should issue a warning if
2662 // the field is not found. For 1.6 this code should be issue a
2663 // fatal error if the "discovered" field is not found.
2664 //
2665 // Increment fac.nonstatic_oop_count so that the start of the
2666 // next type of non-static oops leaves room for the fake oop.
2667 // Do not increment next_nonstatic_oop_offset so that the
2668 // fake oop is place after the java.lang.ref.Reference oop
2669 // fields.
2670 //
2671 // Check the fields in java.lang.ref.Reference for the "discovered"
2672 // field. If it is not present, artifically create a field for it.
2673 // This allows this VM to run on early JDK where the field is not
2674 // present.
2675 int reference_sig_index = 0;
2676 int reference_name_index = 0;
2752 fields_with_fix->ushort_at_put(i + instanceKlass::access_flags_offset,
2753 flags);
2754
2755 assert(fields_with_fix->ushort_at(i + instanceKlass::name_index_offset)
2756 == reference_name_index, "The fake reference name is incorrect");
2757 assert(fields_with_fix->ushort_at(i + instanceKlass::signature_index_offset)
2758 == reference_sig_index, "The fake reference signature is incorrect");
2759 // The type of the field is stored in the low_offset entry during
2760 // parsing.
2761 assert(fields_with_fix->ushort_at(i + instanceKlass::low_offset) ==
2762 NONSTATIC_OOP, "The fake reference type is incorrect");
2763
2764 // "fields" is allocated in the permanent generation. Disgard
2765 // it and let it be collected.
2766 (*fields_ptr) = fields_with_fix;
2767 }
2768 return;
2769 }
2770
2771
2772 void ClassFileParser::java_lang_Class_fix_pre(objArrayHandle* methods_ptr,
2773 FieldAllocationCount *fac_ptr, TRAPS) {
2774 // Add fake fields for java.lang.Class instances
2775 //
2776 // This is not particularly nice. We should consider adding a
2777 // private transient object field at the Java level to
2778 // java.lang.Class. Alternatively we could add a subclass of
2779 // instanceKlass which provides an accessor and size computer for
2780 // this field, but that appears to be more code than this hack.
2781 //
2782 // NOTE that we wedge these in at the beginning rather than the
2783 // end of the object because the Class layout changed between JDK
2784 // 1.3 and JDK 1.4 with the new reflection implementation; some
2785 // nonstatic oop fields were added at the Java level. The offsets
2786 // of these fake fields can't change between these two JDK
2787 // versions because when the offsets are computed at bootstrap
2788 // time we don't know yet which version of the JDK we're running in.
2789
2790 // The values below are fake but will force two non-static oop fields and
2791 // a corresponding non-static oop map block to be allocated.
2792 const int extra = java_lang_Class::number_of_fake_oop_fields;
2793 fac_ptr->nonstatic_oop_count += extra;
2794 }
2795
2796
2797 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2798 // Cause the extra fake fields in java.lang.Class to show up before
2799 // the Java fields for layout compatibility between 1.3 and 1.4
2800 // Incrementing next_nonstatic_oop_offset here advances the
2801 // location where the real java fields are placed.
2802 const int extra = java_lang_Class::number_of_fake_oop_fields;
2803 (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2804 }
2805
2806
2807 // Force MethodHandle.vmentry to be an unmanaged pointer.
2808 // There is no way for a classfile to express this, so we must help it.
2809 void ClassFileParser::java_lang_invoke_MethodHandle_fix_pre(constantPoolHandle cp,
2810 typeArrayHandle fields,
2811 FieldAllocationCount *fac_ptr,
2812 TRAPS) {
2813 // Add fake fields for java.lang.invoke.MethodHandle instances
3188 int orig_nonstatic_field_size = 0;
3189 #endif
3190 int static_field_size = 0;
3191 int next_static_oop_offset;
3192 int next_static_double_offset;
3193 int next_static_word_offset;
3194 int next_static_short_offset;
3195 int next_static_byte_offset;
3196 int next_static_type_offset;
3197 int next_nonstatic_oop_offset;
3198 int next_nonstatic_double_offset;
3199 int next_nonstatic_word_offset;
3200 int next_nonstatic_short_offset;
3201 int next_nonstatic_byte_offset;
3202 int next_nonstatic_type_offset;
3203 int first_nonstatic_oop_offset;
3204 int first_nonstatic_field_offset;
3205 int next_nonstatic_field_offset;
3206
3207 // Calculate the starting byte offsets
3208 next_static_oop_offset = (instanceKlass::header_size() +
3209 align_object_offset(vtable_size) +
3210 align_object_offset(itable_size)) * wordSize;
3211 next_static_double_offset = next_static_oop_offset +
3212 (fac.static_oop_count * heapOopSize);
3213 if ( fac.static_double_count &&
3214 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3215 Universe::field_type_should_be_aligned(T_LONG)) ) {
3216 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3217 }
3218
3219 next_static_word_offset = next_static_double_offset +
3220 (fac.static_double_count * BytesPerLong);
3221 next_static_short_offset = next_static_word_offset +
3222 (fac.static_word_count * BytesPerInt);
3223 next_static_byte_offset = next_static_short_offset +
3224 (fac.static_short_count * BytesPerShort);
3225 next_static_type_offset = align_size_up((next_static_byte_offset +
3226 fac.static_byte_count ), wordSize );
3227 static_field_size = (next_static_type_offset -
3228 next_static_oop_offset) / wordSize;
3229 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3230 nonstatic_field_size * heapOopSize;
3231 next_nonstatic_field_offset = first_nonstatic_field_offset;
3232
3233 // Add fake fields for java.lang.Class instances (also see below)
3234 if (class_name == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3235 java_lang_Class_fix_pre(&methods, &fac, CHECK_(nullHandle));
3236 }
3237
3238 // adjust the vmentry field declaration in java.lang.invoke.MethodHandle
3239 if (EnableMethodHandles && class_name == vmSymbols::java_lang_invoke_MethodHandle() && class_loader.is_null()) {
3240 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3241 }
3242 if (AllowTransitionalJSR292 &&
3243 EnableMethodHandles && class_name == vmSymbols::java_dyn_MethodHandle() && class_loader.is_null()) {
3244 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3245 }
3246 if (AllowTransitionalJSR292 &&
3247 EnableMethodHandles && class_name == vmSymbols::sun_dyn_MethodHandleImpl() && class_loader.is_null()) {
3248 // allow vmentry field in MethodHandleImpl also
3249 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3250 }
3251
3252 // Add a fake "discovered" field if it is not present
3253 // for compatibility with earlier jdk's.
3254 if (class_name == vmSymbols::java_lang_ref_Reference()
3255 && class_loader.is_null()) {
3256 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
3257 }
3549
3550 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3551 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3552
3553 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3554
3555 // Number of non-static oop map blocks allocated at end of klass.
3556 const unsigned int total_oop_map_count =
3557 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3558 first_nonstatic_oop_offset);
3559
3560 // Compute reference type
3561 ReferenceType rt;
3562 if (super_klass() == NULL) {
3563 rt = REF_NONE;
3564 } else {
3565 rt = super_klass->reference_type();
3566 }
3567
3568 // We can now create the basic klassOop for this klass
3569 klassOop ik = oopFactory::new_instanceKlass(vtable_size, itable_size,
3570 static_field_size,
3571 total_oop_map_count,
3572 rt, CHECK_(nullHandle));
3573 instanceKlassHandle this_klass (THREAD, ik);
3574
3575 assert(this_klass->static_field_size() == static_field_size, "sanity");
3576 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3577 "sanity");
3578
3579 // Fill in information already parsed
3580 this_klass->set_access_flags(access_flags);
3581 this_klass->set_should_verify_class(verify);
3582 jint lh = Klass::instance_layout_helper(instance_size, false);
3583 this_klass->set_layout_helper(lh);
3584 assert(this_klass->oop_is_instance(), "layout is correct");
3585 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3586 // Not yet: supers are done below to support the new subtype-checking fields
3587 //this_klass->set_super(super_klass());
3588 this_klass->set_class_loader(class_loader());
3589 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3590 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3591 this_klass->set_static_oop_field_size(fac.static_oop_count);
3592 cp->set_pool_holder(this_klass());
3593 error_handler.set_in_error(false); // turn off error handler for cp
3594 this_klass->set_constants(cp());
3595 this_klass->set_local_interfaces(local_interfaces());
3596 this_klass->set_fields(fields());
3597 this_klass->set_methods(methods());
3598 if (has_final_method) {
3599 this_klass->set_has_final_method();
3600 }
3601 this_klass->set_method_ordering(method_ordering());
3602 // The instanceKlass::_methods_jmethod_ids cache and the
3603 // instanceKlass::_methods_cached_itable_indices cache are
3604 // both managed on the assumption that the initial cache
3605 // size is equal to the number of methods in the class. If
3606 // that changes, then instanceKlass::idnum_can_increment()
3607 // has to be changed accordingly.
3608 this_klass->set_initial_method_idnum(methods->length());
3609 this_klass->set_name(cp->klass_name_at(this_class_index));
3610 if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself
3611 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3632 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3633 this_klass->set_cached_class_file(cached_class_file_bytes,
3634 cached_class_file_length);
3635 }
3636
3637 // Miranda methods
3638 if ((num_miranda_methods > 0) ||
3639 // if this class introduced new miranda methods or
3640 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3641 // super class exists and this class inherited miranda methods
3642 ) {
3643 this_klass->set_has_miranda_methods(); // then set a flag
3644 }
3645
3646 // Additional attributes
3647 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle));
3648
3649 // Make sure this is the end of class file stream
3650 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3651
3652 // Initialize static fields
3653 this_klass->do_local_static_fields(&initialize_static_field, CHECK_(nullHandle));
3654
3655 // VerifyOops believes that once this has been set, the object is completely loaded.
3656 // Compute transitive closure of interfaces this class implements
3657 this_klass->set_transitive_interfaces(transitive_interfaces());
3658
3659 // Fill in information needed to compute superclasses.
3660 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
3661
3662 // Initialize itable offset tables
3663 klassItable::setup_itable_offset_table(this_klass);
3664
3665 // Do final class setup
3666 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3667
3668 set_precomputed_flags(this_klass);
3669
3670 // reinitialize modifiers, using the InnerClasses attribute
3671 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3672 this_klass->set_modifier_flags(computed_modifiers);
3673
3674 // check if this class can access its super class
3675 check_super_class_access(this_klass, CHECK_(nullHandle));
3676
3677 // check if this class can access its superinterfaces
3678 check_super_interface_access(this_klass, CHECK_(nullHandle));
3679
3680 // check if this class overrides any final method
3681 check_final_method_override(this_klass, CHECK_(nullHandle));
3682
3683 // check that if this class is an interface then it doesn't have static methods
3684 if (this_klass->is_interface()) {
3685 check_illegal_static_method(this_klass, CHECK_(nullHandle));
3686 }
3687
3688 ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()),
3689 false /* not shared class */);
3690
3691 if (TraceClassLoading) {
3692 // print in a single call to reduce interleaving of output
3693 if (cfs->source() != NULL) {
3694 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3695 cfs->source());
3696 } else if (class_loader.is_null()) {
3697 if (THREAD->is_Java_thread()) {
3698 klassOop caller = ((JavaThread*)THREAD)->security_get_caller_class(1);
3699 tty->print("[Loaded %s by instance of %s]\n",
3700 this_klass->external_name(),
3701 instanceKlass::cast(caller)->external_name());
3702 } else {
3703 tty->print("[Loaded %s]\n", this_klass->external_name());
3704 }
3705 } else {
3706 ResourceMark rm;
3707 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classFileParser.hpp"
27 #include "classfile/classLoader.hpp"
28 #include "classfile/javaClasses.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/verificationType.hpp"
32 #include "classfile/verifier.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/gcLocker.hpp"
36 #include "memory/oopFactory.hpp"
37 #include "memory/universe.inline.hpp"
38 #include "oops/constantPoolOop.hpp"
39 #include "oops/instanceKlass.hpp"
40 #include "oops/instanceMirrorKlass.hpp"
41 #include "oops/klass.inline.hpp"
42 #include "oops/klassOop.hpp"
43 #include "oops/klassVtable.hpp"
44 #include "oops/methodOop.hpp"
45 #include "oops/symbol.hpp"
46 #include "prims/jvmtiExport.hpp"
47 #include "runtime/javaCalls.hpp"
48 #include "runtime/perfData.hpp"
49 #include "runtime/reflection.hpp"
50 #include "runtime/signature.hpp"
51 #include "runtime/timer.hpp"
52 #include "services/classLoadingService.hpp"
53 #include "services/threadService.hpp"
54
55 // We generally try to create the oops directly when parsing, rather than
56 // allocating temporary data structures and copying the bytes twice. A
57 // temporary area is only needed when parsing utf8 entries in the constant
58 // pool and when parsing line number tables.
59
60 // We add assert in debug mode when class format is not checked.
2590 int runtime_visible_annotations_length,
2591 u1* runtime_invisible_annotations,
2592 int runtime_invisible_annotations_length, TRAPS) {
2593 typeArrayHandle annotations;
2594 if (runtime_visible_annotations != NULL ||
2595 runtime_invisible_annotations != NULL) {
2596 typeArrayOop anno = oopFactory::new_permanent_byteArray(runtime_visible_annotations_length +
2597 runtime_invisible_annotations_length, CHECK_(annotations));
2598 annotations = typeArrayHandle(THREAD, anno);
2599 if (runtime_visible_annotations != NULL) {
2600 memcpy(annotations->byte_at_addr(0), runtime_visible_annotations, runtime_visible_annotations_length);
2601 }
2602 if (runtime_invisible_annotations != NULL) {
2603 memcpy(annotations->byte_at_addr(runtime_visible_annotations_length), runtime_invisible_annotations, runtime_invisible_annotations_length);
2604 }
2605 }
2606 return annotations;
2607 }
2608
2609
2610 void ClassFileParser::java_lang_ref_Reference_fix_pre(typeArrayHandle* fields_ptr,
2611 constantPoolHandle cp, FieldAllocationCount *fac_ptr, TRAPS) {
2612 // This code is for compatibility with earlier jdk's that do not
2613 // have the "discovered" field in java.lang.ref.Reference. For 1.5
2614 // the check for the "discovered" field should issue a warning if
2615 // the field is not found. For 1.6 this code should be issue a
2616 // fatal error if the "discovered" field is not found.
2617 //
2618 // Increment fac.nonstatic_oop_count so that the start of the
2619 // next type of non-static oops leaves room for the fake oop.
2620 // Do not increment next_nonstatic_oop_offset so that the
2621 // fake oop is place after the java.lang.ref.Reference oop
2622 // fields.
2623 //
2624 // Check the fields in java.lang.ref.Reference for the "discovered"
2625 // field. If it is not present, artifically create a field for it.
2626 // This allows this VM to run on early JDK where the field is not
2627 // present.
2628 int reference_sig_index = 0;
2629 int reference_name_index = 0;
2705 fields_with_fix->ushort_at_put(i + instanceKlass::access_flags_offset,
2706 flags);
2707
2708 assert(fields_with_fix->ushort_at(i + instanceKlass::name_index_offset)
2709 == reference_name_index, "The fake reference name is incorrect");
2710 assert(fields_with_fix->ushort_at(i + instanceKlass::signature_index_offset)
2711 == reference_sig_index, "The fake reference signature is incorrect");
2712 // The type of the field is stored in the low_offset entry during
2713 // parsing.
2714 assert(fields_with_fix->ushort_at(i + instanceKlass::low_offset) ==
2715 NONSTATIC_OOP, "The fake reference type is incorrect");
2716
2717 // "fields" is allocated in the permanent generation. Disgard
2718 // it and let it be collected.
2719 (*fields_ptr) = fields_with_fix;
2720 }
2721 return;
2722 }
2723
2724
2725 void ClassFileParser::java_lang_Class_fix_pre(int* nonstatic_field_size,
2726 FieldAllocationCount *fac_ptr) {
2727 // Add fake fields for java.lang.Class instances
2728 //
2729 // This is not particularly nice. We should consider adding a
2730 // private transient object field at the Java level to
2731 // java.lang.Class. Alternatively we could add a subclass of
2732 // instanceKlass which provides an accessor and size computer for
2733 // this field, but that appears to be more code than this hack.
2734 //
2735 // NOTE that we wedge these in at the beginning rather than the
2736 // end of the object because the Class layout changed between JDK
2737 // 1.3 and JDK 1.4 with the new reflection implementation; some
2738 // nonstatic oop fields were added at the Java level. The offsets
2739 // of these fake fields can't change between these two JDK
2740 // versions because when the offsets are computed at bootstrap
2741 // time we don't know yet which version of the JDK we're running in.
2742
2743 // The values below are fake but will force three non-static oop fields and
2744 // a corresponding non-static oop map block to be allocated.
2745 const int extra = java_lang_Class::number_of_fake_oop_fields;
2746 fac_ptr->nonstatic_oop_count += extra;
2747
2748 // Reserve some leading space for fake ints
2749 *nonstatic_field_size += align_size_up(java_lang_Class::hc_number_of_fake_int_fields * BytesPerInt, heapOopSize) / heapOopSize;
2750 }
2751
2752
2753 void ClassFileParser::java_lang_Class_fix_post(int* next_nonstatic_oop_offset_ptr) {
2754 // Cause the extra fake fields in java.lang.Class to show up before
2755 // the Java fields for layout compatibility between 1.3 and 1.4
2756 // Incrementing next_nonstatic_oop_offset here advances the
2757 // location where the real java fields are placed.
2758 const int extra = java_lang_Class::number_of_fake_oop_fields;
2759 (*next_nonstatic_oop_offset_ptr) += (extra * heapOopSize);
2760 }
2761
2762
2763 // Force MethodHandle.vmentry to be an unmanaged pointer.
2764 // There is no way for a classfile to express this, so we must help it.
2765 void ClassFileParser::java_lang_invoke_MethodHandle_fix_pre(constantPoolHandle cp,
2766 typeArrayHandle fields,
2767 FieldAllocationCount *fac_ptr,
2768 TRAPS) {
2769 // Add fake fields for java.lang.invoke.MethodHandle instances
3144 int orig_nonstatic_field_size = 0;
3145 #endif
3146 int static_field_size = 0;
3147 int next_static_oop_offset;
3148 int next_static_double_offset;
3149 int next_static_word_offset;
3150 int next_static_short_offset;
3151 int next_static_byte_offset;
3152 int next_static_type_offset;
3153 int next_nonstatic_oop_offset;
3154 int next_nonstatic_double_offset;
3155 int next_nonstatic_word_offset;
3156 int next_nonstatic_short_offset;
3157 int next_nonstatic_byte_offset;
3158 int next_nonstatic_type_offset;
3159 int first_nonstatic_oop_offset;
3160 int first_nonstatic_field_offset;
3161 int next_nonstatic_field_offset;
3162
3163 // Calculate the starting byte offsets
3164 next_static_oop_offset = instanceMirrorKlass::offset_of_static_fields();
3165 next_static_double_offset = next_static_oop_offset +
3166 (fac.static_oop_count * heapOopSize);
3167 if ( fac.static_double_count &&
3168 (Universe::field_type_should_be_aligned(T_DOUBLE) ||
3169 Universe::field_type_should_be_aligned(T_LONG)) ) {
3170 next_static_double_offset = align_size_up(next_static_double_offset, BytesPerLong);
3171 }
3172
3173 next_static_word_offset = next_static_double_offset +
3174 (fac.static_double_count * BytesPerLong);
3175 next_static_short_offset = next_static_word_offset +
3176 (fac.static_word_count * BytesPerInt);
3177 next_static_byte_offset = next_static_short_offset +
3178 (fac.static_short_count * BytesPerShort);
3179 next_static_type_offset = align_size_up((next_static_byte_offset +
3180 fac.static_byte_count ), wordSize );
3181 static_field_size = (next_static_type_offset -
3182 next_static_oop_offset) / wordSize;
3183
3184 // Add fake fields for java.lang.Class instances (also see below)
3185 if (class_name == vmSymbols::java_lang_Class() && class_loader.is_null()) {
3186 java_lang_Class_fix_pre(&nonstatic_field_size, &fac);
3187 }
3188
3189 first_nonstatic_field_offset = instanceOopDesc::base_offset_in_bytes() +
3190 nonstatic_field_size * heapOopSize;
3191 next_nonstatic_field_offset = first_nonstatic_field_offset;
3192
3193 // adjust the vmentry field declaration in java.lang.invoke.MethodHandle
3194 if (EnableMethodHandles && class_name == vmSymbols::java_lang_invoke_MethodHandle() && class_loader.is_null()) {
3195 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3196 }
3197 if (AllowTransitionalJSR292 &&
3198 EnableMethodHandles && class_name == vmSymbols::java_dyn_MethodHandle() && class_loader.is_null()) {
3199 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3200 }
3201 if (AllowTransitionalJSR292 &&
3202 EnableMethodHandles && class_name == vmSymbols::sun_dyn_MethodHandleImpl() && class_loader.is_null()) {
3203 // allow vmentry field in MethodHandleImpl also
3204 java_lang_invoke_MethodHandle_fix_pre(cp, fields, &fac, CHECK_(nullHandle));
3205 }
3206
3207 // Add a fake "discovered" field if it is not present
3208 // for compatibility with earlier jdk's.
3209 if (class_name == vmSymbols::java_lang_ref_Reference()
3210 && class_loader.is_null()) {
3211 java_lang_ref_Reference_fix_pre(&fields, cp, &fac, CHECK_(nullHandle));
3212 }
3504
3505 next_nonstatic_type_offset = align_size_up(notaligned_offset, wordSize );
3506 instance_size = align_object_size(next_nonstatic_type_offset / wordSize);
3507
3508 assert(instance_size == align_object_size(align_size_up((instanceOopDesc::base_offset_in_bytes() + nonstatic_field_size*heapOopSize), wordSize) / wordSize), "consistent layout helper value");
3509
3510 // Number of non-static oop map blocks allocated at end of klass.
3511 const unsigned int total_oop_map_count =
3512 compute_oop_map_count(super_klass, nonstatic_oop_map_count,
3513 first_nonstatic_oop_offset);
3514
3515 // Compute reference type
3516 ReferenceType rt;
3517 if (super_klass() == NULL) {
3518 rt = REF_NONE;
3519 } else {
3520 rt = super_klass->reference_type();
3521 }
3522
3523 // We can now create the basic klassOop for this klass
3524 klassOop ik = oopFactory::new_instanceKlass(name, vtable_size, itable_size,
3525 static_field_size,
3526 total_oop_map_count,
3527 rt, CHECK_(nullHandle));
3528 instanceKlassHandle this_klass (THREAD, ik);
3529
3530 assert(this_klass->static_field_size() == static_field_size, "sanity");
3531 assert(this_klass->nonstatic_oop_map_count() == total_oop_map_count,
3532 "sanity");
3533
3534 // Fill in information already parsed
3535 this_klass->set_access_flags(access_flags);
3536 this_klass->set_should_verify_class(verify);
3537 jint lh = Klass::instance_layout_helper(instance_size, false);
3538 this_klass->set_layout_helper(lh);
3539 assert(this_klass->oop_is_instance(), "layout is correct");
3540 assert(this_klass->size_helper() == instance_size, "correct size_helper");
3541 // Not yet: supers are done below to support the new subtype-checking fields
3542 //this_klass->set_super(super_klass());
3543 this_klass->set_class_loader(class_loader());
3544 this_klass->set_nonstatic_field_size(nonstatic_field_size);
3545 this_klass->set_has_nonstatic_fields(has_nonstatic_fields);
3546 this_klass->set_static_oop_field_count(fac.static_oop_count);
3547 cp->set_pool_holder(this_klass());
3548 error_handler.set_in_error(false); // turn off error handler for cp
3549 this_klass->set_constants(cp());
3550 this_klass->set_local_interfaces(local_interfaces());
3551 this_klass->set_fields(fields());
3552 this_klass->set_methods(methods());
3553 if (has_final_method) {
3554 this_klass->set_has_final_method();
3555 }
3556 this_klass->set_method_ordering(method_ordering());
3557 // The instanceKlass::_methods_jmethod_ids cache and the
3558 // instanceKlass::_methods_cached_itable_indices cache are
3559 // both managed on the assumption that the initial cache
3560 // size is equal to the number of methods in the class. If
3561 // that changes, then instanceKlass::idnum_can_increment()
3562 // has to be changed accordingly.
3563 this_klass->set_initial_method_idnum(methods->length());
3564 this_klass->set_name(cp->klass_name_at(this_class_index));
3565 if (LinkWellKnownClasses || is_anonymous()) // I am well known to myself
3566 cp->klass_at_put(this_class_index, this_klass()); // eagerly resolve
3587 // JVMTI: we have an instanceKlass now, tell it about the cached bytes
3588 this_klass->set_cached_class_file(cached_class_file_bytes,
3589 cached_class_file_length);
3590 }
3591
3592 // Miranda methods
3593 if ((num_miranda_methods > 0) ||
3594 // if this class introduced new miranda methods or
3595 (super_klass.not_null() && (super_klass->has_miranda_methods()))
3596 // super class exists and this class inherited miranda methods
3597 ) {
3598 this_klass->set_has_miranda_methods(); // then set a flag
3599 }
3600
3601 // Additional attributes
3602 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle));
3603
3604 // Make sure this is the end of class file stream
3605 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3606
3607 // VerifyOops believes that once this has been set, the object is completely loaded.
3608 // Compute transitive closure of interfaces this class implements
3609 this_klass->set_transitive_interfaces(transitive_interfaces());
3610
3611 // Fill in information needed to compute superclasses.
3612 this_klass->initialize_supers(super_klass(), CHECK_(nullHandle));
3613
3614 // Initialize itable offset tables
3615 klassItable::setup_itable_offset_table(this_klass);
3616
3617 // Do final class setup
3618 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3619
3620 set_precomputed_flags(this_klass);
3621
3622 // reinitialize modifiers, using the InnerClasses attribute
3623 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3624 this_klass->set_modifier_flags(computed_modifiers);
3625
3626 // check if this class can access its super class
3627 check_super_class_access(this_klass, CHECK_(nullHandle));
3628
3629 // check if this class can access its superinterfaces
3630 check_super_interface_access(this_klass, CHECK_(nullHandle));
3631
3632 // check if this class overrides any final method
3633 check_final_method_override(this_klass, CHECK_(nullHandle));
3634
3635 // check that if this class is an interface then it doesn't have static methods
3636 if (this_klass->is_interface()) {
3637 check_illegal_static_method(this_klass, CHECK_(nullHandle));
3638 }
3639
3640 // Allocate mirror and initialize static fields
3641 java_lang_Class::create_mirror(this_klass, CHECK_(nullHandle));
3642
3643 ClassLoadingService::notify_class_loaded(instanceKlass::cast(this_klass()),
3644 false /* not shared class */);
3645
3646 if (TraceClassLoading) {
3647 // print in a single call to reduce interleaving of output
3648 if (cfs->source() != NULL) {
3649 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
3650 cfs->source());
3651 } else if (class_loader.is_null()) {
3652 if (THREAD->is_Java_thread()) {
3653 klassOop caller = ((JavaThread*)THREAD)->security_get_caller_class(1);
3654 tty->print("[Loaded %s by instance of %s]\n",
3655 this_klass->external_name(),
3656 instanceKlass::cast(caller)->external_name());
3657 } else {
3658 tty->print("[Loaded %s]\n", this_klass->external_name());
3659 }
3660 } else {
3661 ResourceMark rm;
3662 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
|