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