< prev index next >

src/hotspot/share/classfile/classFileParser.cpp

Print this page




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */

  24 #include "precompiled.hpp"
  25 #include "jvm.h"
  26 #include "aot/aotLoader.hpp"
  27 #include "classfile/classFileParser.hpp"
  28 #include "classfile/classFileStream.hpp"
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/classLoaderData.inline.hpp"
  31 #include "classfile/defaultMethods.hpp"
  32 #include "classfile/dictionary.hpp"
  33 #include "classfile/javaClasses.inline.hpp"
  34 #include "classfile/moduleEntry.hpp"
  35 #include "classfile/packageEntry.hpp"
  36 #include "classfile/symbolTable.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/verificationType.hpp"
  39 #include "classfile/verifier.hpp"
  40 #include "classfile/vmSymbols.hpp"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/oopFactory.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "memory/universe.hpp"
  48 #include "oops/annotations.hpp"
  49 #include "oops/constantPool.inline.hpp"
  50 #include "oops/fieldStreams.hpp"
  51 #include "oops/instanceKlass.hpp"
  52 #include "oops/instanceMirrorKlass.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/klassVtable.hpp"
  55 #include "oops/metadata.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "oops/symbol.hpp"
  59 #include "oops/valueKlass.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/arguments.hpp"

  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/javaCalls.hpp"
  65 #include "runtime/os.hpp"
  66 #include "runtime/perfData.hpp"
  67 #include "runtime/reflection.hpp"
  68 #include "runtime/safepointVerifiers.hpp"
  69 #include "runtime/signature.hpp"
  70 #include "runtime/timer.hpp"
  71 #include "services/classLoadingService.hpp"
  72 #include "services/threadService.hpp"
  73 #include "utilities/align.hpp"
  74 #include "utilities/bitMap.inline.hpp"
  75 #include "utilities/copy.hpp"
  76 #include "utilities/exceptions.hpp"
  77 #include "utilities/globalDefinitions.hpp"
  78 #include "utilities/growableArray.hpp"
  79 #include "utilities/macros.hpp"
  80 #include "utilities/ostream.hpp"
  81 #include "utilities/resourceHash.hpp"
  82 #include "utilities/utf8.hpp"


1067       guarantee_property(value_type.is_int(),
1068                          "Inconsistent constant value type in class file %s",
1069                          CHECK);
1070       break;
1071     }
1072     case T_OBJECT: {
1073       guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
1074                          && value_type.is_string()),
1075                          "Bad string initial value in class file %s",
1076                          CHECK);
1077       break;
1078     }
1079     default: {
1080       classfile_parse_error("Unable to set initial value %u in class file %s",
1081                              constantvalue_index,
1082                              CHECK);
1083     }
1084   }
1085 }
1086 
1087 class AnnotationCollector : public ResourceObj{
1088 public:
1089   enum Location { _in_field, _in_method, _in_class };
1090   enum ID {
1091     _unknown = 0,
1092     _method_CallerSensitive,
1093     _method_ForceInline,
1094     _method_DontInline,
1095     _method_InjectedProfile,
1096     _method_LambdaForm_Compiled,
1097     _method_Hidden,
1098     _method_HotSpotIntrinsicCandidate,
1099     _jdk_internal_vm_annotation_Contended,
1100     _field_Stable,
1101     _jdk_internal_vm_annotation_ReservedStackAccess,
1102     _annotation_LIMIT
1103   };
1104   const Location _location;
1105   int _annotations_present;
1106   u2 _contended_group;
1107 
1108   AnnotationCollector(Location location)
1109     : _location(location), _annotations_present(0)
1110   {
1111     assert((int)_annotation_LIMIT <= (int)sizeof(_annotations_present) * BitsPerByte, "");
1112   }
1113   // If this annotation name has an ID, report it (or _none).
1114   ID annotation_index(const ClassLoaderData* loader_data, const Symbol* name);
1115   // Set the annotation name:
1116   void set_annotation(ID id) {
1117     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1118     _annotations_present |= nth_bit((int)id);
1119   }
1120 
1121   void remove_annotation(ID id) {
1122     assert((int)id >= 0 && (int)id < (int)_annotation_LIMIT, "oob");
1123     _annotations_present &= ~nth_bit((int)id);
1124   }
1125 
1126   // Report if the annotation is present.
1127   bool has_any_annotations() const { return _annotations_present != 0; }
1128   bool has_annotation(ID id) const { return (nth_bit((int)id) & _annotations_present) != 0; }
1129 
1130   void set_contended_group(u2 group) { _contended_group = group; }
1131   u2 contended_group() const { return _contended_group; }
1132 
1133   bool is_contended() const { return has_annotation(_jdk_internal_vm_annotation_Contended); }
1134 
1135   void set_stable(bool stable) { set_annotation(_field_Stable); }
1136   bool is_stable() const { return has_annotation(_field_Stable); }
1137 };
1138 
1139 // This class also doubles as a holder for metadata cleanup.
1140 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector {
1141 private:
1142   ClassLoaderData* _loader_data;
1143   AnnotationArray* _field_annotations;
1144   AnnotationArray* _field_type_annotations;
1145 public:
1146   FieldAnnotationCollector(ClassLoaderData* loader_data) :
1147     AnnotationCollector(_in_field),
1148     _loader_data(loader_data),
1149     _field_annotations(NULL),
1150     _field_type_annotations(NULL) {}
1151   ~FieldAnnotationCollector();
1152   void apply_to(FieldInfo* f);
1153   AnnotationArray* field_annotations()      { return _field_annotations; }
1154   AnnotationArray* field_type_annotations() { return _field_type_annotations; }
1155 
1156   void set_field_annotations(AnnotationArray* a)      { _field_annotations = a; }
1157   void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }
1158 };
1159 
1160 class MethodAnnotationCollector : public AnnotationCollector{
1161 public:
1162   MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
1163   void apply_to(const methodHandle& m);
1164 };
1165 
1166 class ClassFileParser::ClassAnnotationCollector : public AnnotationCollector{
1167 public:
1168   ClassAnnotationCollector() : AnnotationCollector(_in_class) { }
1169   void apply_to(InstanceKlass* ik);
1170 };
1171 
1172 
1173 static int skip_annotation_value(const u1*, int, int); // fwd decl
1174 
1175 // Safely increment index by val if does not pass limit
1176 #define SAFE_ADD(index, limit, val) \
1177 if (index >= limit - val) return limit; \
1178 index += val;
1179 
1180 // Skip an annotation.  Return >=limit if there is any problem.
1181 static int skip_annotation(const u1* buffer, int limit, int index) {
1182   assert(buffer != NULL, "invariant");
1183   // annotation := atype:u2 do(nmem:u2) {member:u2 value}
1184   // value := switch (tag:u1) { ... }
1185   SAFE_ADD(index, limit, 4); // skip atype and read nmem
1186   int nmem = Bytes::get_Java_u2((address)buffer + index - 2);
1187   while (--nmem >= 0 && index < limit) {
1188     SAFE_ADD(index, limit, 2); // skip member
1189     index = skip_annotation_value(buffer, limit, index);
1190   }
1191   return index;


3869         fs.name()->as_klass_external_name(),
3870         fs.signature()->as_klass_external_name());
3871     }
3872   }
3873   tty->print("  @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3874   tty->print("  @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3875   tty->print("  @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3876   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3877     if (fs.access_flags().is_static()) {
3878       tty->print("  @%3d \"%s\" %s\n",
3879         fs.offset(),
3880         fs.name()->as_klass_external_name(),
3881         fs.signature()->as_klass_external_name());
3882     }
3883   }
3884   tty->print("  @%3d %s\n", static_fields_end, "--- static fields end ---");
3885   tty->print("\n");
3886 }
3887 #endif
3888 
3889 // Values needed for oopmap and InstanceKlass creation
3890 class ClassFileParser::FieldLayoutInfo : public ResourceObj {
3891  public:
3892   OopMapBlocksBuilder* oop_map_blocks;
3893   int           instance_size;
3894   int           nonstatic_field_size;
3895   int           static_field_size;
3896   bool          has_nonstatic_fields;
3897 };
3898 
3899 // Utility to collect and compact oop maps during layout
3900 class ClassFileParser::OopMapBlocksBuilder : public ResourceObj {
3901  public:
3902   OopMapBlock*  nonstatic_oop_maps;
3903   unsigned int  nonstatic_oop_map_count;
3904   unsigned int  max_nonstatic_oop_maps;
3905 
3906  public:
3907   OopMapBlocksBuilder(unsigned int  max_blocks, TRAPS) {
3908     max_nonstatic_oop_maps = max_blocks;
3909     nonstatic_oop_map_count = 0;
3910     if (max_blocks == 0) {
3911       nonstatic_oop_maps = NULL;
3912     } else {
3913       nonstatic_oop_maps = NEW_RESOURCE_ARRAY_IN_THREAD(
3914         THREAD, OopMapBlock, max_nonstatic_oop_maps);
3915       memset(nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3916     }
3917   }
3918 
3919   OopMapBlock* last_oop_map() const {
3920     assert(nonstatic_oop_map_count > 0, "Has no oop maps");
3921     return nonstatic_oop_maps + (nonstatic_oop_map_count - 1);
3922   }
3923 
3924   // addition of super oop maps
3925   void initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {
3926     assert(nof_blocks && nonstatic_oop_map_count == 0 &&
3927         nof_blocks <= max_nonstatic_oop_maps, "invariant");
3928 
3929     memcpy(nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);
3930     nonstatic_oop_map_count += nof_blocks;
3931   }
3932 
3933   // collection of oops
3934   void add(int offset, int count) {
3935     if (nonstatic_oop_map_count == 0) {
3936       nonstatic_oop_map_count++;
3937     }
3938     OopMapBlock*  nonstatic_oop_map = last_oop_map();
3939     if (nonstatic_oop_map->count() == 0) {  // Unused map, set it up
3940       nonstatic_oop_map->set_offset(offset);
3941       nonstatic_oop_map->set_count(count);
3942     } else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add
3943       nonstatic_oop_map->increment_count(count);
3944     } else { // Need a new one...
3945       nonstatic_oop_map_count++;
3946       assert(nonstatic_oop_map_count <= max_nonstatic_oop_maps, "range check");
3947       nonstatic_oop_map = last_oop_map();
3948       nonstatic_oop_map->set_offset(offset);
3949       nonstatic_oop_map->set_count(count);
3950     }
3951   }
3952 
3953   // general purpose copy, e.g. into allocated instanceKlass
3954   void copy(OopMapBlock* dst) {
3955     if (nonstatic_oop_map_count != 0) {
3956       memcpy(dst, nonstatic_oop_maps, sizeof(OopMapBlock) * nonstatic_oop_map_count);
3957     }
3958   }
3959 
3960   // Sort and compact adjacent blocks
3961   void compact(TRAPS) {
3962     if (nonstatic_oop_map_count <= 1) {
3963       return;
3964     }
3965     /*
3966      * Since field layout sneeks in oops before values, we will be able to condense
3967      * blocks. There is potential to compact between super, own refs and values
3968      * containing refs.
3969      *
3970      * Currently compaction is slightly limited due to values being 8 byte aligned.
3971      * This may well change: FixMe if doesn't, the code below is fairly general purpose
3972      * and maybe it doesn't need to be.
3973      */
3974     qsort(nonstatic_oop_maps, nonstatic_oop_map_count, sizeof(OopMapBlock),
3975         (_sort_Fn)OopMapBlock::compare_offset);
3976     if (nonstatic_oop_map_count < 2) {
3977       return;
3978     }
3979 
3980      //Make a temp copy, and iterate through and copy back into the orig
3981     ResourceMark rm(THREAD);


3983         nonstatic_oop_map_count);
3984     OopMapBlock* oop_maps_copy_end = oop_maps_copy + nonstatic_oop_map_count;
3985     copy(oop_maps_copy);
3986     OopMapBlock*  nonstatic_oop_map = nonstatic_oop_maps;
3987     unsigned int new_count = 1;
3988     oop_maps_copy++;
3989     while(oop_maps_copy < oop_maps_copy_end) {
3990       assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");
3991       if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {
3992         nonstatic_oop_map->increment_count(oop_maps_copy->count());
3993       } else {
3994         nonstatic_oop_map++;
3995         new_count++;
3996         nonstatic_oop_map->set_offset(oop_maps_copy->offset());
3997         nonstatic_oop_map->set_count(oop_maps_copy->count());
3998       }
3999       oop_maps_copy++;
4000     }
4001     assert(new_count <= nonstatic_oop_map_count, "end up with more maps after compact() ?");
4002     nonstatic_oop_map_count = new_count;
4003   }
4004 
4005   void print_on(outputStream* st) const {
4006     st->print_cr("  OopMapBlocks: %3d  /%3d", nonstatic_oop_map_count, max_nonstatic_oop_maps);
4007     if (nonstatic_oop_map_count > 0) {
4008       OopMapBlock* map = nonstatic_oop_maps;
4009       OopMapBlock* last_map = last_oop_map();
4010       assert(map <= last_map, "Last less than first");
4011       while (map <= last_map) {
4012         st->print_cr("    Offset: %3d  -%3d Count: %3d", map->offset(),
4013             map->offset() + map->offset_span() - heapOopSize, map->count());
4014         map++;
4015       }
4016     }
4017   }
4018 
4019   void print_value_on(outputStream* st) const {
4020     print_on(st);
4021   }
4022 
4023 };
4024 
4025 void ClassFileParser::throwValueTypeLimitation(THREAD_AND_LOCATION_DECL,
4026                                                const char* msg,
4027                                                const Symbol* name,
4028                                                const Symbol* sig) const {
4029 
4030   ResourceMark rm(THREAD);
4031   if (name == NULL || sig == NULL) {
4032     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4033         vmSymbols::java_lang_ClassFormatError(),
4034         "class: %s - %s", _class_name->as_C_string(), msg);
4035   }
4036   else {
4037     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
4038         vmSymbols::java_lang_ClassFormatError(),
4039         "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
4040         _class_name->as_C_string(), msg);
4041   }
4042 }
4043 


4651   // assert without actually having the fields.
4652   assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4653          is_contended_class ||
4654          (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4655 
4656   // Number of non-static oop map blocks allocated at end of klass.
4657   nonstatic_oop_maps->compact(THREAD);
4658 
4659 #ifndef PRODUCT
4660   if ((PrintFieldLayout && !is_value_type()) ||
4661       (PrintValueLayout && (is_value_type() || has_nonstatic_value_fields))) {
4662     print_field_layout(_class_name,
4663           _fields,
4664           cp,
4665           instance_size,
4666           nonstatic_fields_start,
4667           nonstatic_fields_end,
4668           static_fields_end);
4669     nonstatic_oop_maps->print_on(tty);
4670     tty->print("\n");





4671   }
4672 
4673 #endif
4674   // Pass back information needed for InstanceKlass creation
4675   info->oop_map_blocks = nonstatic_oop_maps;
4676   info->instance_size = instance_size;
4677   info->static_field_size = static_field_size;
4678   info->nonstatic_field_size = nonstatic_field_size;
4679   info->has_nonstatic_fields = has_nonstatic_fields;
4680 }
4681 
4682 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik, TRAPS) {
4683   assert(ik != NULL, "invariant");
4684 
4685   const Klass* const super = ik->super();
4686 
4687   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4688   // in which case we don't have to register objects as finalizable
4689   if (!_has_empty_finalizer) {
4690     if (_has_finalizer ||


6025   int nfields = ik->java_fields_count();
6026   if (ik->is_value()) nfields++;
6027   for (int i = 0; i < nfields; i++) {
6028     if (ik->field_access_flags(i) & JVM_ACC_FLATTENABLE) {
6029       Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK);
6030       // Value classes must have been pre-loaded
6031       Klass* klass = SystemDictionary::find(klass_name,
6032           Handle(THREAD, ik->class_loader()),
6033           Handle(THREAD, ik->protection_domain()), CHECK);
6034       assert(klass != NULL, "Sanity check");
6035       assert(klass->access_flags().is_value_type(), "Value type expected");
6036       ik->set_value_field_klass(i, klass);
6037       klass_name->decrement_refcount();
6038     } else if (is_value_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0)
6039         && ((ik->field_access_flags(i) & JVM_ACC_STATIC) != 0)) {
6040       ValueKlass::cast(ik)->set_default_value_offset(ik->field_offset(i));
6041     }
6042   }
6043 
6044   if (is_value_type()) {



6045     ValueKlass::cast(ik)->initialize_calling_convention(CHECK);
6046   }
6047 
6048   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
6049 
6050   if (!is_internal()) {
6051     if (log_is_enabled(Info, class, load)) {
6052       ResourceMark rm;
6053       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
6054       ik->print_class_load_logging(_loader_data, module_name, _stream);
6055     }
6056 
6057     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
6058         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
6059         log_is_enabled(Info, class, preview)) {
6060       ResourceMark rm;
6061       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
6062                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
6063     }
6064 


6717   _itable_size = _access_flags.is_interface() ? 0 :
6718     klassItable::compute_itable_size(_transitive_interfaces);
6719 
6720   assert(_fac != NULL, "invariant");
6721   assert(_parsed_annotations != NULL, "invariant");
6722 
6723 
6724   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6725     if (fs.is_flattenable()) {
6726       // Pre-load value class
6727       Klass* klass = SystemDictionary::resolve_flattenable_field_or_fail(&fs,
6728           Handle(THREAD, _loader_data->class_loader()),
6729           _protection_domain, true, CHECK);
6730       assert(klass != NULL, "Sanity check");
6731       assert(klass->access_flags().is_value_type(), "Value type expected");
6732       _has_flattenable_fields = true;
6733     }
6734   }
6735 
6736   _field_info = new FieldLayoutInfo();











6737   layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);

6738 
6739   // Compute reference typ
6740   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6741 
6742 }
6743 
6744 void ClassFileParser::set_klass(InstanceKlass* klass) {
6745 
6746 #ifdef ASSERT
6747   if (klass != NULL) {
6748     assert(NULL == _klass, "leaking?");
6749   }
6750 #endif
6751 
6752   _klass = klass;
6753 }
6754 
6755 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6756 
6757 #ifdef ASSERT
6758   if (klass != NULL) {
6759     assert(NULL == _klass_to_deallocate, "leaking?");




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 #include <classfile/fieldLayoutBuilder.hpp>
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/defaultMethods.hpp"
  33 #include "classfile/dictionary.hpp"
  34 #include "classfile/javaClasses.inline.hpp"
  35 #include "classfile/moduleEntry.hpp"
  36 #include "classfile/packageEntry.hpp"
  37 #include "classfile/symbolTable.hpp"
  38 #include "classfile/systemDictionary.hpp"
  39 #include "classfile/verificationType.hpp"
  40 #include "classfile/verifier.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allocation.hpp"
  45 #include "memory/metadataFactory.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "oops/annotations.hpp"
  50 #include "oops/constantPool.inline.hpp"
  51 #include "oops/fieldStreams.hpp"
  52 #include "oops/instanceKlass.hpp"
  53 #include "oops/instanceMirrorKlass.hpp"
  54 #include "oops/klass.inline.hpp"
  55 #include "oops/klassVtable.hpp"
  56 #include "oops/metadata.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/valueKlass.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/jvmtiThreadState.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/fieldDescriptor.inline.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/os.hpp"
  68 #include "runtime/perfData.hpp"
  69 #include "runtime/reflection.hpp"
  70 #include "runtime/safepointVerifiers.hpp"
  71 #include "runtime/signature.hpp"
  72 #include "runtime/timer.hpp"
  73 #include "services/classLoadingService.hpp"
  74 #include "services/threadService.hpp"
  75 #include "utilities/align.hpp"
  76 #include "utilities/bitMap.inline.hpp"
  77 #include "utilities/copy.hpp"
  78 #include "utilities/exceptions.hpp"
  79 #include "utilities/globalDefinitions.hpp"
  80 #include "utilities/growableArray.hpp"
  81 #include "utilities/macros.hpp"
  82 #include "utilities/ostream.hpp"
  83 #include "utilities/resourceHash.hpp"
  84 #include "utilities/utf8.hpp"


1069       guarantee_property(value_type.is_int(),
1070                          "Inconsistent constant value type in class file %s",
1071                          CHECK);
1072       break;
1073     }
1074     case T_OBJECT: {
1075       guarantee_property((cp->symbol_at(signature_index)->equals("Ljava/lang/String;")
1076                          && value_type.is_string()),
1077                          "Bad string initial value in class file %s",
1078                          CHECK);
1079       break;
1080     }
1081     default: {
1082       classfile_parse_error("Unable to set initial value %u in class file %s",
1083                              constantvalue_index,
1084                              CHECK);
1085     }
1086   }
1087 }
1088 




















































1089 // This class also doubles as a holder for metadata cleanup.
1090 class ClassFileParser::FieldAnnotationCollector : public AnnotationCollector {
1091 private:
1092   ClassLoaderData* _loader_data;
1093   AnnotationArray* _field_annotations;
1094   AnnotationArray* _field_type_annotations;
1095 public:
1096   FieldAnnotationCollector(ClassLoaderData* loader_data) :
1097     AnnotationCollector(_in_field),
1098     _loader_data(loader_data),
1099     _field_annotations(NULL),
1100     _field_type_annotations(NULL) {}
1101   ~FieldAnnotationCollector();
1102   void apply_to(FieldInfo* f);
1103   AnnotationArray* field_annotations()      { return _field_annotations; }
1104   AnnotationArray* field_type_annotations() { return _field_type_annotations; }
1105 
1106   void set_field_annotations(AnnotationArray* a)      { _field_annotations = a; }
1107   void set_field_type_annotations(AnnotationArray* a) { _field_type_annotations = a; }
1108 };
1109 
1110 class MethodAnnotationCollector : public AnnotationCollector{
1111 public:
1112   MethodAnnotationCollector() : AnnotationCollector(_in_method) { }
1113   void apply_to(const methodHandle& m);
1114 };
1115 






1116 
1117 static int skip_annotation_value(const u1*, int, int); // fwd decl
1118 
1119 // Safely increment index by val if does not pass limit
1120 #define SAFE_ADD(index, limit, val) \
1121 if (index >= limit - val) return limit; \
1122 index += val;
1123 
1124 // Skip an annotation.  Return >=limit if there is any problem.
1125 static int skip_annotation(const u1* buffer, int limit, int index) {
1126   assert(buffer != NULL, "invariant");
1127   // annotation := atype:u2 do(nmem:u2) {member:u2 value}
1128   // value := switch (tag:u1) { ... }
1129   SAFE_ADD(index, limit, 4); // skip atype and read nmem
1130   int nmem = Bytes::get_Java_u2((address)buffer + index - 2);
1131   while (--nmem >= 0 && index < limit) {
1132     SAFE_ADD(index, limit, 2); // skip member
1133     index = skip_annotation_value(buffer, limit, index);
1134   }
1135   return index;


3813         fs.name()->as_klass_external_name(),
3814         fs.signature()->as_klass_external_name());
3815     }
3816   }
3817   tty->print("  @%3d %s\n", instance_fields_end, "--- instance fields end ---");
3818   tty->print("  @%3d %s\n", instance_size * wordSize, "--- instance ends ---");
3819   tty->print("  @%3d %s\n", InstanceMirrorKlass::offset_of_static_fields(), "--- static fields start ---");
3820   for (AllFieldStream fs(fields, cp); !fs.done(); fs.next()) {
3821     if (fs.access_flags().is_static()) {
3822       tty->print("  @%3d \"%s\" %s\n",
3823         fs.offset(),
3824         fs.name()->as_klass_external_name(),
3825         fs.signature()->as_klass_external_name());
3826     }
3827   }
3828   tty->print("  @%3d %s\n", static_fields_end, "--- static fields end ---");
3829   tty->print("\n");
3830 }
3831 #endif
3832 
3833 OopMapBlocksBuilder::OopMapBlocksBuilder(unsigned int  max_blocks, TRAPS) {


















3834   max_nonstatic_oop_maps = max_blocks;
3835   nonstatic_oop_map_count = 0;
3836   if (max_blocks == 0) {
3837     nonstatic_oop_maps = NULL;
3838   } else {
3839     nonstatic_oop_maps = NEW_RESOURCE_ARRAY_IN_THREAD(
3840         THREAD, OopMapBlock, max_nonstatic_oop_maps);
3841     memset(nonstatic_oop_maps, 0, sizeof(OopMapBlock) * max_blocks);
3842   }
3843 }
3844 
3845 OopMapBlock* OopMapBlocksBuilder::last_oop_map() const {
3846   assert(nonstatic_oop_map_count > 0, "Has no oop maps");
3847   return nonstatic_oop_maps + (nonstatic_oop_map_count - 1);
3848 }
3849 
3850 // addition of super oop maps
3851 void OopMapBlocksBuilder::initialize_inherited_blocks(OopMapBlock* blocks, unsigned int nof_blocks) {
3852   assert(nof_blocks && nonstatic_oop_map_count == 0 &&
3853       nof_blocks <= max_nonstatic_oop_maps, "invariant");
3854 
3855   memcpy(nonstatic_oop_maps, blocks, sizeof(OopMapBlock) * nof_blocks);
3856   nonstatic_oop_map_count += nof_blocks;
3857 }
3858 
3859 // collection of oops
3860 void OopMapBlocksBuilder::add(int offset, int count) {
3861   if (nonstatic_oop_map_count == 0) {
3862     nonstatic_oop_map_count++;
3863   }
3864   OopMapBlock*  nonstatic_oop_map = last_oop_map();
3865   if (nonstatic_oop_map->count() == 0) {  // Unused map, set it up
3866     nonstatic_oop_map->set_offset(offset);
3867     nonstatic_oop_map->set_count(count);
3868   } else if (nonstatic_oop_map->is_contiguous(offset)) { // contiguous, add
3869     nonstatic_oop_map->increment_count(count);
3870   } else { // Need a new one...
3871     nonstatic_oop_map_count++;
3872     assert(nonstatic_oop_map_count <= max_nonstatic_oop_maps, "range check");
3873     nonstatic_oop_map = last_oop_map();
3874     nonstatic_oop_map->set_offset(offset);
3875     nonstatic_oop_map->set_count(count);
3876   }
3877 }
3878 
3879 // general purpose copy, e.g. into allocated instanceKlass
3880 void OopMapBlocksBuilder::copy(OopMapBlock* dst) {
3881   if (nonstatic_oop_map_count != 0) {
3882     memcpy(dst, nonstatic_oop_maps, sizeof(OopMapBlock) * nonstatic_oop_map_count);
3883   }
3884 }
3885 
3886 // Sort and compact adjacent blocks
3887 void OopMapBlocksBuilder::compact(TRAPS) {
3888   if (nonstatic_oop_map_count <= 1) {
3889     return;
3890   }
3891   /*
3892    * Since field layout sneeks in oops before values, we will be able to condense
3893    * blocks. There is potential to compact between super, own refs and values
3894    * containing refs.
3895    *
3896    * Currently compaction is slightly limited due to values being 8 byte aligned.
3897    * This may well change: FixMe if doesn't, the code below is fairly general purpose
3898    * and maybe it doesn't need to be.
3899    */
3900   qsort(nonstatic_oop_maps, nonstatic_oop_map_count, sizeof(OopMapBlock),
3901       (_sort_Fn)OopMapBlock::compare_offset);
3902   if (nonstatic_oop_map_count < 2) {
3903     return;
3904   }
3905 
3906   //Make a temp copy, and iterate through and copy back into the orig
3907   ResourceMark rm(THREAD);


3909       nonstatic_oop_map_count);
3910   OopMapBlock* oop_maps_copy_end = oop_maps_copy + nonstatic_oop_map_count;
3911   copy(oop_maps_copy);
3912   OopMapBlock*  nonstatic_oop_map = nonstatic_oop_maps;
3913   unsigned int new_count = 1;
3914   oop_maps_copy++;
3915   while(oop_maps_copy < oop_maps_copy_end) {
3916     assert(nonstatic_oop_map->offset() < oop_maps_copy->offset(), "invariant");
3917     if (nonstatic_oop_map->is_contiguous(oop_maps_copy->offset())) {
3918       nonstatic_oop_map->increment_count(oop_maps_copy->count());
3919     } else {
3920       nonstatic_oop_map++;
3921       new_count++;
3922       nonstatic_oop_map->set_offset(oop_maps_copy->offset());
3923       nonstatic_oop_map->set_count(oop_maps_copy->count());
3924     }
3925     oop_maps_copy++;
3926   }
3927   assert(new_count <= nonstatic_oop_map_count, "end up with more maps after compact() ?");
3928   nonstatic_oop_map_count = new_count;
3929 }
3930 
3931 void OopMapBlocksBuilder::print_on(outputStream* st) const {
3932   st->print_cr("  OopMapBlocks: %3d  /%3d", nonstatic_oop_map_count, max_nonstatic_oop_maps);
3933   if (nonstatic_oop_map_count > 0) {
3934     OopMapBlock* map = nonstatic_oop_maps;
3935     OopMapBlock* last_map = last_oop_map();
3936     assert(map <= last_map, "Last less than first");
3937     while (map <= last_map) {
3938       st->print_cr("    Offset: %3d  -%3d Count: %3d", map->offset(),
3939           map->offset() + map->offset_span() - heapOopSize, map->count());
3940       map++;
3941     }
3942   }
3943 }
3944 
3945 void OopMapBlocksBuilder::print_value_on(outputStream* st) const {
3946   print_on(st);
3947 }


3948 
3949 void ClassFileParser::throwValueTypeLimitation(THREAD_AND_LOCATION_DECL,
3950                                                const char* msg,
3951                                                const Symbol* name,
3952                                                const Symbol* sig) const {
3953 
3954   ResourceMark rm(THREAD);
3955   if (name == NULL || sig == NULL) {
3956     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
3957         vmSymbols::java_lang_ClassFormatError(),
3958         "class: %s - %s", _class_name->as_C_string(), msg);
3959   }
3960   else {
3961     Exceptions::fthrow(THREAD_AND_LOCATION_ARGS,
3962         vmSymbols::java_lang_ClassFormatError(),
3963         "\"%s\" sig: \"%s\" class: %s - %s", name->as_C_string(), sig->as_C_string(),
3964         _class_name->as_C_string(), msg);
3965   }
3966 }
3967 


4575   // assert without actually having the fields.
4576   assert((notaligned_nonstatic_fields_end == nonstatic_fields_start) ||
4577          is_contended_class ||
4578          (nonstatic_fields_count > 0), "double-check nonstatic start/end");
4579 
4580   // Number of non-static oop map blocks allocated at end of klass.
4581   nonstatic_oop_maps->compact(THREAD);
4582 
4583 #ifndef PRODUCT
4584   if ((PrintFieldLayout && !is_value_type()) ||
4585       (PrintValueLayout && (is_value_type() || has_nonstatic_value_fields))) {
4586     print_field_layout(_class_name,
4587           _fields,
4588           cp,
4589           instance_size,
4590           nonstatic_fields_start,
4591           nonstatic_fields_end,
4592           static_fields_end);
4593     nonstatic_oop_maps->print_on(tty);
4594     tty->print("\n");
4595     tty->print_cr("Instance size = %d", instance_size);
4596     tty->print_cr("Nonstatic_field_size = %d", nonstatic_field_size);
4597     tty->print_cr("Static_field_size = %d", static_field_size);
4598     tty->print_cr("Has nonstatic fields = %d", has_nonstatic_fields);
4599     tty->print_cr("---");
4600   }
4601 
4602 #endif
4603   // Pass back information needed for InstanceKlass creation
4604   info->oop_map_blocks = nonstatic_oop_maps;
4605   info->instance_size = instance_size;
4606   info->static_field_size = static_field_size;
4607   info->nonstatic_field_size = nonstatic_field_size;
4608   info->has_nonstatic_fields = has_nonstatic_fields;
4609 }
4610 
4611 void ClassFileParser::set_precomputed_flags(InstanceKlass* ik, TRAPS) {
4612   assert(ik != NULL, "invariant");
4613 
4614   const Klass* const super = ik->super();
4615 
4616   // Check if this klass has an empty finalize method (i.e. one with return bytecode only),
4617   // in which case we don't have to register objects as finalizable
4618   if (!_has_empty_finalizer) {
4619     if (_has_finalizer ||


5954   int nfields = ik->java_fields_count();
5955   if (ik->is_value()) nfields++;
5956   for (int i = 0; i < nfields; i++) {
5957     if (ik->field_access_flags(i) & JVM_ACC_FLATTENABLE) {
5958       Symbol* klass_name = ik->field_signature(i)->fundamental_name(CHECK);
5959       // Value classes must have been pre-loaded
5960       Klass* klass = SystemDictionary::find(klass_name,
5961           Handle(THREAD, ik->class_loader()),
5962           Handle(THREAD, ik->protection_domain()), CHECK);
5963       assert(klass != NULL, "Sanity check");
5964       assert(klass->access_flags().is_value_type(), "Value type expected");
5965       ik->set_value_field_klass(i, klass);
5966       klass_name->decrement_refcount();
5967     } else if (is_value_type() && ((ik->field_access_flags(i) & JVM_ACC_FIELD_INTERNAL) != 0)
5968         && ((ik->field_access_flags(i) & JVM_ACC_STATIC) != 0)) {
5969       ValueKlass::cast(ik)->set_default_value_offset(ik->field_offset(i));
5970     }
5971   }
5972 
5973   if (is_value_type()) {
5974     ValueKlass::cast(ik)->set_alignment(_alignment);
5975     ValueKlass::cast(ik)->set_first_field_offset(_first_field_offset);
5976     ValueKlass::cast(ik)->set_exact_size_in_bytes(_exact_size_in_bytes);
5977     ValueKlass::cast(ik)->initialize_calling_convention(CHECK);
5978   }
5979 
5980   ClassLoadingService::notify_class_loaded(ik, false /* not shared class */);
5981 
5982   if (!is_internal()) {
5983     if (log_is_enabled(Info, class, load)) {
5984       ResourceMark rm;
5985       const char* module_name = (module_entry->name() == NULL) ? UNNAMED_MODULE : module_entry->name()->as_C_string();
5986       ik->print_class_load_logging(_loader_data, module_name, _stream);
5987     }
5988 
5989     if (ik->minor_version() == JAVA_PREVIEW_MINOR_VERSION &&
5990         ik->major_version() == JVM_CLASSFILE_MAJOR_VERSION &&
5991         log_is_enabled(Info, class, preview)) {
5992       ResourceMark rm;
5993       log_info(class, preview)("Loading class %s that depends on preview features (class file version %d.65535)",
5994                                ik->external_name(), JVM_CLASSFILE_MAJOR_VERSION);
5995     }
5996 


6649   _itable_size = _access_flags.is_interface() ? 0 :
6650     klassItable::compute_itable_size(_transitive_interfaces);
6651 
6652   assert(_fac != NULL, "invariant");
6653   assert(_parsed_annotations != NULL, "invariant");
6654 
6655 
6656   for (AllFieldStream fs(_fields, cp); !fs.done(); fs.next()) {
6657     if (fs.is_flattenable()) {
6658       // Pre-load value class
6659       Klass* klass = SystemDictionary::resolve_flattenable_field_or_fail(&fs,
6660           Handle(THREAD, _loader_data->class_loader()),
6661           _protection_domain, true, CHECK);
6662       assert(klass != NULL, "Sanity check");
6663       assert(klass->access_flags().is_value_type(), "Value type expected");
6664       _has_flattenable_fields = true;
6665     }
6666   }
6667 
6668   _field_info = new FieldLayoutInfo();
6669   if (UseNewLayout) {
6670     FieldLayoutBuilder lb(this, _field_info);
6671     if (this->is_value_type()) {
6672       lb.compute_inline_class_layout(CHECK);
6673       _alignment = lb.get_alignment();
6674       _first_field_offset = lb.get_first_field_offset();
6675       _exact_size_in_bytes = lb.get_exact_size_in_byte();
6676     } else {
6677       lb.compute_regular_layout(CHECK);
6678     }
6679   } else {
6680     layout_fields(cp, _fac, _parsed_annotations, _field_info, CHECK);
6681 }
6682 
6683   // Compute reference type
6684   _rt = (NULL ==_super_klass) ? REF_NONE : _super_klass->reference_type();
6685 
6686 }
6687 
6688 void ClassFileParser::set_klass(InstanceKlass* klass) {
6689 
6690 #ifdef ASSERT
6691   if (klass != NULL) {
6692     assert(NULL == _klass, "leaking?");
6693   }
6694 #endif
6695 
6696   _klass = klass;
6697 }
6698 
6699 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
6700 
6701 #ifdef ASSERT
6702   if (klass != NULL) {
6703     assert(NULL == _klass_to_deallocate, "leaking?");


< prev index next >