< 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 
  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/fieldLayoutBuilder.hpp>
  35 #include "classfile/javaClasses.inline.hpp"
  36 #include "classfile/moduleEntry.hpp"
  37 #include "classfile/packageEntry.hpp"
  38 #include "classfile/symbolTable.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/verificationType.hpp"
  41 #include "classfile/verifier.hpp"
  42 #include "classfile/vmSymbols.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/oopFactory.hpp"
  48 #include "memory/resourceArea.hpp"
  49 #include "memory/universe.hpp"
  50 #include "oops/annotations.hpp"
  51 #include "oops/constantPool.inline.hpp"
  52 #include "oops/fieldStreams.hpp"
  53 #include "oops/instanceKlass.hpp"
  54 #include "oops/instanceMirrorKlass.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/klassVtable.hpp"
  57 #include "oops/metadata.hpp"
  58 #include "oops/method.inline.hpp"
  59 #include "oops/oop.inline.hpp"
  60 #include "oops/symbol.hpp"
  61 #include "oops/valueKlass.hpp"
  62 #include "prims/jvmtiExport.hpp"
  63 #include "prims/jvmtiThreadState.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/fieldDescriptor.inline.hpp"
  66 #include "runtime/handles.inline.hpp"
  67 #include "runtime/javaCalls.hpp"
  68 #include "runtime/os.hpp"
  69 #include "runtime/perfData.hpp"
  70 #include "runtime/reflection.hpp"
  71 #include "runtime/safepointVerifiers.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/timer.hpp"
  74 #include "services/classLoadingService.hpp"
  75 #include "services/threadService.hpp"
  76 #include "utilities/align.hpp"
  77 #include "utilities/bitMap.inline.hpp"
  78 #include "utilities/copy.hpp"
  79 #include "utilities/exceptions.hpp"
  80 #include "utilities/globalDefinitions.hpp"
  81 #include "utilities/growableArray.hpp"
  82 #include "utilities/macros.hpp"
  83 #include "utilities/ostream.hpp"
  84 #include "utilities/resourceHash.hpp"
  85 #include "utilities/utf8.hpp"


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




















































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






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


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


















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


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


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


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


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


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


< prev index next >