1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact 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 #ifndef SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP
  27 
  28 #include "memory/referenceType.hpp"
  29 #include "runtime/handles.inline.hpp"
  30 #include "oops/constantPool.hpp"
  31 #include "oops/typeArrayOop.hpp"
  32 #include "utilities/accessFlags.hpp"
  33 
  34 class Annotations;
  35 template <typename T>
  36 class Array;
  37 class ClassFileStream;
  38 class ClassLoaderData;
  39 class CompressedLineNumberWriteStream;
  40 class ConstMethod;
  41 class FieldInfo;
  42 template <typename T>
  43 class GrowableArray;
  44 class InstanceKlass;
  45 class Symbol;
  46 class TempNewSymbol;
  47 
  48 // Parser for for .class files
  49 //
  50 // The bytes describing the class file structure is read from a Stream object
  51 
  52 class ClassFileParser VALUE_OBJ_CLASS_SPEC {
  53 
  54  class ClassAnnotationCollector;
  55  class FieldAllocationCount;
  56  class FieldAnnotationCollector;
  57  class FieldLayoutInfo;
  58 
  59  public:
  60   // The ClassFileParser has an associated "publicity" level
  61   // It is used to control which subsystems (if any)
  62   // will observe the parsing (logging, events, tracing).
  63   // Default level is "BROADCAST", which is equivalent to
  64   // a "public" parsing attempt.
  65   //
  66   // "INTERNAL" level should be entirely private to the
  67   // caller - this allows for internal reuse of ClassFileParser
  68   //
  69   enum Publicity {
  70     INTERNAL,
  71     BROADCAST,
  72     NOF_PUBLICITY_LEVELS
  73   };
  74 
  75   enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
  76 
  77  private:
  78   const ClassFileStream* _stream; // Actual input stream
  79   const Symbol* _requested_name;
  80   Symbol* _class_name;
  81   mutable ClassLoaderData* _loader_data;
  82   const InstanceKlass* _host_klass;
  83   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  84   int _num_patched_klasses;
  85   int _max_num_patched_klasses;
  86   int _orig_cp_size;
  87   int _first_patched_klass_resolved_index;
  88 
  89   // Metadata created before the instance klass is created.  Must be deallocated
  90   // if not transferred to the InstanceKlass upon successful class loading
  91   // in which case these pointers have been set to NULL.
  92   const InstanceKlass* _super_klass;
  93   ConstantPool* _cp;
  94   Array<u2>* _fields;
  95   Array<Method*>* _methods;
  96   Array<u2>* _inner_classes;
  97   Array<Klass*>* _local_interfaces;
  98   Array<Klass*>* _transitive_interfaces;
  99   Annotations* _combined_annotations;
 100   AnnotationArray* _annotations;
 101   AnnotationArray* _type_annotations;
 102   Array<AnnotationArray*>* _fields_annotations;
 103   Array<AnnotationArray*>* _fields_type_annotations;
 104   InstanceKlass* _klass;  // InstanceKlass* once created.
 105   InstanceKlass* _klass_to_deallocate; // an InstanceKlass* to be destroyed
 106 
 107   ClassAnnotationCollector* _parsed_annotations;
 108   FieldAllocationCount* _fac;
 109   FieldLayoutInfo* _field_info;
 110   const intArray* _method_ordering;
 111   GrowableArray<Method*>* _all_mirandas;
 112 
 113   enum { fixed_buffer_size = 128 };
 114   u_char _linenumbertable_buffer[fixed_buffer_size];
 115 
 116   // Size of Java vtable (in words)
 117   int _vtable_size;
 118   int _itable_size;
 119 
 120   int _num_miranda_methods;
 121 
 122   ReferenceType _rt;
 123   Handle _protection_domain;
 124   AccessFlags _access_flags;
 125 
 126   // for tracing and notifications
 127   Publicity _pub_level;
 128 
 129   // Used to keep track of whether a constant pool item 19 or 20 is found.  These
 130   // correspond to CONSTANT_Module and CONSTANT_Package tags and are not allowed
 131   // in regular class files.  For class file version >= 53, a CFE cannot be thrown
 132   // immediately when these are seen because a NCDFE must be thrown if the class's
 133   // access_flags have ACC_MODULE set.  But, the access_flags haven't been looked
 134   // at yet.  So, the bad constant pool item is cached here.  A value of zero
 135   // means that no constant pool item 19 or 20 was found.
 136   short _bad_constant_seen;
 137 
 138   // class attributes parsed before the instance klass is created:
 139   bool _synthetic_flag;
 140   int _sde_length;
 141   const char* _sde_buffer;
 142   u2 _sourcefile_index;
 143   u2 _generic_signature_index;
 144 
 145   u2 _major_version;
 146   u2 _minor_version;
 147   u2 _this_class_index;
 148   u2 _super_class_index;
 149   u2 _itfs_len;
 150   u2 _java_fields_count;
 151 
 152   bool _need_verify;
 153   bool _relax_verify;
 154 
 155   bool _has_nonstatic_concrete_methods;
 156   bool _declares_nonstatic_concrete_methods;
 157   bool _has_final_method;
 158 
 159   // precomputed flags
 160   bool _has_finalizer;
 161   bool _has_empty_finalizer;
 162   bool _has_vanilla_constructor;
 163   int _max_bootstrap_specifier_index;  // detects BSS values
 164 
 165   void parse_stream(const ClassFileStream* const stream, TRAPS);
 166 
 167   void post_process_parsed_stream(const ClassFileStream* const stream,
 168                                   ConstantPool* cp,
 169                                   TRAPS);
 170 
 171   void prepend_host_package_name(const InstanceKlass* host_klass, TRAPS);
 172   void fix_anonymous_class_name(TRAPS);
 173 
 174   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH, TRAPS);
 175   void set_klass(InstanceKlass* instance);
 176 
 177   void set_class_bad_constant_seen(short bad_constant);
 178   short class_bad_constant_seen() { return  _bad_constant_seen; }
 179   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 180   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 181   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 182   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 183 
 184   void create_combined_annotations(TRAPS);
 185   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 186   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 187   void clear_class_metadata();
 188 
 189   // Constant pool parsing
 190   void parse_constant_pool_entries(const ClassFileStream* const stream,
 191                                    ConstantPool* cp,
 192                                    const int length,
 193                                    TRAPS);
 194 
 195   void parse_constant_pool(const ClassFileStream* const cfs,
 196                            ConstantPool* const cp,
 197                            const int length,
 198                            TRAPS);
 199 
 200   // Interface parsing
 201   void parse_interfaces(const ClassFileStream* const stream,
 202                         const int itfs_len,
 203                         ConstantPool* const cp,
 204                         bool* has_nonstatic_concrete_methods,
 205                         TRAPS);
 206 
 207   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 208                                          const int super_class_index,
 209                                          const bool need_verify,
 210                                          TRAPS);
 211 
 212   // Field parsing
 213   void parse_field_attributes(const ClassFileStream* const cfs,
 214                               u2 attributes_count,
 215                               bool is_static,
 216                               u2 signature_index,
 217                               u2* const constantvalue_index_addr,
 218                               bool* const is_synthetic_addr,
 219                               u2* const generic_signature_index_addr,
 220                               FieldAnnotationCollector* parsed_annotations,
 221                               TRAPS);
 222 
 223   void parse_fields(const ClassFileStream* const cfs,
 224                     bool is_interface,
 225                     FieldAllocationCount* const fac,
 226                     ConstantPool* cp,
 227                     const int cp_size,
 228                     u2* const java_fields_count_ptr,
 229                     TRAPS);
 230 
 231   // Method parsing
 232   Method* parse_method(const ClassFileStream* const cfs,
 233                        bool is_interface,
 234                        const ConstantPool* cp,
 235                        AccessFlags* const promoted_flags,
 236                        TRAPS);
 237 
 238   void parse_methods(const ClassFileStream* const cfs,
 239                      bool is_interface,
 240                      AccessFlags* const promoted_flags,
 241                      bool* const has_final_method,
 242                      bool* const declares_nonstatic_concrete_methods,
 243                      TRAPS);
 244 
 245   const u2* parse_exception_table(const ClassFileStream* const stream,
 246                                   u4 code_length,
 247                                   u4 exception_table_length,
 248                                   TRAPS);
 249 
 250   void parse_linenumber_table(u4 code_attribute_length,
 251                               u4 code_length,
 252                               CompressedLineNumberWriteStream**const write_stream,
 253                               TRAPS);
 254 
 255   const u2* parse_localvariable_table(const ClassFileStream* const cfs,
 256                                       u4 code_length,
 257                                       u2 max_locals,
 258                                       u4 code_attribute_length,
 259                                       u2* const localvariable_table_length,
 260                                       bool isLVTT,
 261                                       TRAPS);
 262 
 263   const u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 264                                      u2* const checked_exceptions_length,
 265                                      u4 method_attribute_length,
 266                                      TRAPS);
 267 
 268   void parse_type_array(u2 array_length,
 269                         u4 code_length,
 270                         u4* const u1_index,
 271                         u4* const u2_index,
 272                         u1* const u1_array,
 273                         u2* const u2_array,
 274                         TRAPS);
 275 
 276   // Classfile attribute parsing
 277   u2 parse_generic_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 278   void parse_classfile_sourcefile_attribute(const ClassFileStream* const cfs, TRAPS);
 279   void parse_classfile_source_debug_extension_attribute(const ClassFileStream* const cfs,
 280                                                         int length,
 281                                                         TRAPS);
 282 
 283   u2   parse_classfile_inner_classes_attribute(const ClassFileStream* const cfs,
 284                                                const u1* const inner_classes_attribute_start,
 285                                                bool parsed_enclosingmethod_attribute,
 286                                                u2 enclosing_method_class_index,
 287                                                u2 enclosing_method_method_index,
 288                                                TRAPS);
 289 
 290   void parse_classfile_attributes(const ClassFileStream* const cfs,
 291                                   ConstantPool* cp,
 292                                   ClassAnnotationCollector* parsed_annotations,
 293                                   TRAPS);
 294 
 295   void parse_classfile_synthetic_attribute(TRAPS);
 296   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 297   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 298                                                    ConstantPool* cp,
 299                                                    u4 attribute_length,
 300                                                    TRAPS);
 301 
 302   // Annotations handling
 303   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 304                                         int runtime_visible_annotations_length,
 305                                         const u1* const runtime_invisible_annotations,
 306                                         int runtime_invisible_annotations_length,
 307                                         TRAPS);
 308 
 309   void set_precomputed_flags(InstanceKlass* k);
 310 
 311   // Format checker methods
 312   void classfile_parse_error(const char* msg, TRAPS) const;
 313   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 314   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 315   void classfile_parse_error(const char* msg,
 316                              int index,
 317                              const char *name,
 318                              TRAPS) const;
 319   void classfile_parse_error(const char* msg,
 320                              const char* name,
 321                              const char* signature,
 322                              TRAPS) const;
 323 
 324   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 325     if (!b) { classfile_parse_error(msg, CHECK); }
 326   }
 327 
 328   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 329   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 330 
 331   inline void assert_property(bool b, const char* msg, TRAPS) const {
 332 #ifdef ASSERT
 333     if (!b) {
 334       report_assert_property_failure(msg, THREAD);
 335     }
 336 #endif
 337   }
 338 
 339   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 340 #ifdef ASSERT
 341     if (!b) {
 342       report_assert_property_failure(msg, index, THREAD);
 343     }
 344 #endif
 345   }
 346 
 347   inline void check_property(bool property,
 348                              const char* msg,
 349                              int index,
 350                              TRAPS) const {
 351     if (_need_verify) {
 352       guarantee_property(property, msg, index, CHECK);
 353     } else {
 354       assert_property(property, msg, index, CHECK);
 355     }
 356   }
 357 
 358   inline void check_property(bool property, const char* msg, TRAPS) const {
 359     if (_need_verify) {
 360       guarantee_property(property, msg, CHECK);
 361     } else {
 362       assert_property(property, msg, CHECK);
 363     }
 364   }
 365 
 366   inline void guarantee_property(bool b,
 367                                  const char* msg,
 368                                  int index,
 369                                  TRAPS) const {
 370     if (!b) { classfile_parse_error(msg, index, CHECK); }
 371   }
 372 
 373   inline void guarantee_property(bool b,
 374                                  const char* msg,
 375                                  const char *name,
 376                                  TRAPS) const {
 377     if (!b) { classfile_parse_error(msg, name, CHECK); }
 378   }
 379 
 380   inline void guarantee_property(bool b,
 381                                  const char* msg,
 382                                  int index,
 383                                  const char *name,
 384                                  TRAPS) const {
 385     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 386   }
 387 
 388   void throwIllegalSignature(const char* type,
 389                              const Symbol* name,
 390                              const Symbol* sig,
 391                              TRAPS) const;
 392 
 393   void verify_constantvalue(const ConstantPool* const cp,
 394                             int constantvalue_index,
 395                             int signature_index,
 396                             TRAPS) const;
 397 
 398   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 399   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 400   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 401   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 402 
 403   void verify_legal_field_signature(const Symbol* fieldname,
 404                                     const Symbol* signature,
 405                                     TRAPS) const;
 406   int  verify_legal_method_signature(const Symbol* methodname,
 407                                      const Symbol* signature,
 408                                      TRAPS) const;
 409 
 410   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 411   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 412   void verify_legal_method_modifiers(jint flags,
 413                                      bool is_interface,
 414                                      const Symbol* name,
 415                                      TRAPS) const;
 416 
 417   const char* skip_over_field_signature(const char* signature,
 418                                         bool void_ok,
 419                                         unsigned int length,
 420                                         TRAPS) const;
 421 
 422   bool has_cp_patch_at(int index) const {
 423     assert(index >= 0, "oob");
 424     return (_cp_patches != NULL
 425             && index < _cp_patches->length()
 426             && _cp_patches->adr_at(index)->not_null());
 427   }
 428 
 429   Handle cp_patch_at(int index) const {
 430     assert(has_cp_patch_at(index), "oob");
 431     return _cp_patches->at(index);
 432   }
 433 
 434   Handle clear_cp_patch_at(int index) {
 435     Handle patch = cp_patch_at(index);
 436     _cp_patches->at_put(index, Handle());
 437     assert(!has_cp_patch_at(index), "");
 438     return patch;
 439   }
 440 
 441   void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name);
 442   void patch_constant_pool(ConstantPool* cp,
 443                            int index,
 444                            Handle patch,
 445                            TRAPS);
 446 
 447   // Wrapper for constantTag.is_klass_[or_]reference.
 448   // In older versions of the VM, Klass*s cannot sneak into early phases of
 449   // constant pool construction, but in later versions they can.
 450   // %%% Let's phase out the old is_klass_reference.
 451   bool valid_klass_reference_at(int index) const {
 452     return _cp->is_within_bounds(index) &&
 453              _cp->tag_at(index).is_klass_or_reference();
 454   }
 455 
 456   // Checks that the cpool index is in range and is a utf8
 457   bool valid_symbol_at(int cpool_index) const {
 458     return _cp->is_within_bounds(cpool_index) &&
 459              _cp->tag_at(cpool_index).is_utf8();
 460   }
 461 
 462   void copy_localvariable_table(const ConstMethod* cm,
 463                                 int lvt_cnt,
 464                                 u2* const localvariable_table_length,
 465                                 const u2**const localvariable_table_start,
 466                                 int lvtt_cnt,
 467                                 u2* const localvariable_type_table_length,
 468                                 const u2** const localvariable_type_table_start,
 469                                 TRAPS);
 470 
 471   void copy_method_annotations(ConstMethod* cm,
 472                                const u1* runtime_visible_annotations,
 473                                int runtime_visible_annotations_length,
 474                                const u1* runtime_invisible_annotations,
 475                                int runtime_invisible_annotations_length,
 476                                const u1* runtime_visible_parameter_annotations,
 477                                int runtime_visible_parameter_annotations_length,
 478                                const u1* runtime_invisible_parameter_annotations,
 479                                int runtime_invisible_parameter_annotations_length,
 480                                const u1* runtime_visible_type_annotations,
 481                                int runtime_visible_type_annotations_length,
 482                                const u1* runtime_invisible_type_annotations,
 483                                int runtime_invisible_type_annotations_length,
 484                                const u1* annotation_default,
 485                                int annotation_default_length,
 486                                TRAPS);
 487 
 488   // lays out fields in class and returns the total oopmap count
 489   void layout_fields(ConstantPool* cp,
 490                      const FieldAllocationCount* fac,
 491                      const ClassAnnotationCollector* parsed_annotations,
 492                      FieldLayoutInfo* info,
 493                      TRAPS);
 494 
 495  public:
 496   ClassFileParser(ClassFileStream* stream,
 497                   Symbol* name,
 498                   ClassLoaderData* loader_data,
 499                   Handle protection_domain,
 500                   const InstanceKlass* host_klass,
 501                   GrowableArray<Handle>* cp_patches,
 502                   Publicity pub_level,
 503                   TRAPS);
 504 
 505   ~ClassFileParser();
 506 
 507   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, TRAPS);
 508 
 509   const ClassFileStream* clone_stream() const;
 510 
 511   void set_klass_to_deallocate(InstanceKlass* klass);
 512 
 513   int static_field_size() const;
 514   int total_oop_map_count() const;
 515   jint layout_size() const;
 516 
 517   int vtable_size() const { return _vtable_size; }
 518   int itable_size() const { return _itable_size; }
 519 
 520   u2 this_class_index() const { return _this_class_index; }
 521   u2 super_class_index() const { return _super_class_index; }
 522 
 523   bool is_anonymous() const { return _host_klass != NULL; }
 524   bool is_interface() const { return _access_flags.is_interface(); }
 525 
 526   const InstanceKlass* host_klass() const { return _host_klass; }
 527   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 528   ClassLoaderData* loader_data() const { return _loader_data; }
 529   const Symbol* class_name() const { return _class_name; }
 530   const Klass* super_klass() const { return _super_klass; }
 531 
 532   ReferenceType reference_type() const { return _rt; }
 533   AccessFlags access_flags() const { return _access_flags; }
 534 
 535   bool is_internal() const { return INTERNAL == _pub_level; }
 536 
 537   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 538 
 539 #ifdef ASSERT
 540   static bool is_internal_format(Symbol* class_name);
 541 #endif
 542 
 543 };
 544 
 545 #endif // SHARE_VM_CLASSFILE_CLASSFILEPARSER_HPP