1 /*
   2  * Copyright (c) 1997, 2019, 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_CLASSFILE_CLASSFILEPARSER_HPP
  26 #define SHARE_CLASSFILE_CLASSFILEPARSER_HPP
  27 
  28 #include "memory/referenceType.hpp"
  29 #include "oops/annotations.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 {
  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   };
  73 
  74   enum { LegalClass, LegalField, LegalMethod }; // used to verify unqualified names
  75 
  76  private:
  77   // Potentially unaligned pointer to various 16-bit entries in the class file
  78   typedef void unsafe_u2;
  79 
  80   const ClassFileStream* _stream; // Actual input stream
  81   Symbol* _class_name;
  82   mutable ClassLoaderData* _loader_data;
  83   const InstanceKlass* _unsafe_anonymous_host;
  84   GrowableArray<Handle>* _cp_patches; // overrides for CP entries
  85   const bool _is_nonfindable;
  86   const bool _can_access_vm_annotations;
  87   int _num_patched_klasses;
  88   int _max_num_patched_klasses;
  89   int _orig_cp_size;
  90   int _first_patched_klass_resolved_index;
  91 
  92   // Metadata created before the instance klass is created.  Must be deallocated
  93   // if not transferred to the InstanceKlass upon successful class loading
  94   // in which case these pointers have been set to NULL.
  95   const InstanceKlass* _super_klass;
  96   ConstantPool* _cp;
  97   Array<u2>* _fields;
  98   Array<Method*>* _methods;
  99   Array<u2>* _inner_classes;
 100   Array<u2>* _nest_members;
 101   u2 _nest_host;
 102   Array<InstanceKlass*>* _local_interfaces;
 103   Array<InstanceKlass*>* _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 
 164   // precomputed flags
 165   bool _has_finalizer;
 166   bool _has_empty_finalizer;
 167   bool _has_vanilla_constructor;
 168   int _max_bootstrap_specifier_index;  // detects BSS values
 169 
 170   void parse_stream(const ClassFileStream* const stream, TRAPS);
 171 
 172   void post_process_parsed_stream(const ClassFileStream* const stream,
 173                                   ConstantPool* cp,
 174                                   TRAPS);
 175 
 176   void prepend_host_package_name(const InstanceKlass* unsafe_anonymous_host, TRAPS);
 177   void fix_unsafe_anonymous_class_name(TRAPS);
 178 
 179   void fill_instance_klass(InstanceKlass* ik, bool cf_changed_in_CFLH,
 180                            InstanceKlass* dynamic_nest_host,
 181                            Handle classData, TRAPS);
 182 
 183   void set_klass(InstanceKlass* instance);
 184 
 185   void set_class_bad_constant_seen(short bad_constant);
 186   short class_bad_constant_seen() { return  _bad_constant_seen; }
 187   void set_class_synthetic_flag(bool x)        { _synthetic_flag = x; }
 188   void set_class_sourcefile_index(u2 x)        { _sourcefile_index = x; }
 189   void set_class_generic_signature_index(u2 x) { _generic_signature_index = x; }
 190   void set_class_sde_buffer(const char* x, int len)  { _sde_buffer = x; _sde_length = len; }
 191 
 192   void create_combined_annotations(TRAPS);
 193   void apply_parsed_class_attributes(InstanceKlass* k);  // update k
 194   void apply_parsed_class_metadata(InstanceKlass* k, int fields_count, TRAPS);
 195   void clear_class_metadata();
 196 
 197   // Constant pool parsing
 198   void parse_constant_pool_entries(const ClassFileStream* const stream,
 199                                    ConstantPool* cp,
 200                                    const int length,
 201                                    TRAPS);
 202 
 203   void parse_constant_pool(const ClassFileStream* const cfs,
 204                            ConstantPool* const cp,
 205                            const int length,
 206                            TRAPS);
 207 
 208   // Interface parsing
 209   void parse_interfaces(const ClassFileStream* const stream,
 210                         const int itfs_len,
 211                         ConstantPool* const cp,
 212                         bool* has_nonstatic_concrete_methods,
 213                         TRAPS);
 214 
 215   const InstanceKlass* parse_super_class(ConstantPool* const cp,
 216                                          const int super_class_index,
 217                                          const bool need_verify,
 218                                          TRAPS);
 219 
 220   // Field parsing
 221   void parse_field_attributes(const ClassFileStream* const cfs,
 222                               u2 attributes_count,
 223                               bool is_static,
 224                               u2 signature_index,
 225                               u2* const constantvalue_index_addr,
 226                               bool* const is_synthetic_addr,
 227                               u2* const generic_signature_index_addr,
 228                               FieldAnnotationCollector* parsed_annotations,
 229                               TRAPS);
 230 
 231   void parse_fields(const ClassFileStream* const cfs,
 232                     bool is_interface,
 233                     FieldAllocationCount* const fac,
 234                     ConstantPool* cp,
 235                     const int cp_size,
 236                     u2* const java_fields_count_ptr,
 237                     TRAPS);
 238 
 239   // Method parsing
 240   Method* parse_method(const ClassFileStream* const cfs,
 241                        bool is_interface,
 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                      AccessFlags* const promoted_flags,
 249                      bool* const has_final_method,
 250                      bool* const declares_nonstatic_concrete_methods,
 251                      TRAPS);
 252 
 253   const unsafe_u2* parse_exception_table(const ClassFileStream* const stream,
 254                                          u4 code_length,
 255                                          u4 exception_table_length,
 256                                          TRAPS);
 257 
 258   void parse_linenumber_table(u4 code_attribute_length,
 259                               u4 code_length,
 260                               CompressedLineNumberWriteStream**const write_stream,
 261                               TRAPS);
 262 
 263   const unsafe_u2* parse_localvariable_table(const ClassFileStream* const cfs,
 264                                              u4 code_length,
 265                                              u2 max_locals,
 266                                              u4 code_attribute_length,
 267                                              u2* const localvariable_table_length,
 268                                              bool isLVTT,
 269                                              TRAPS);
 270 
 271   const unsafe_u2* parse_checked_exceptions(const ClassFileStream* const cfs,
 272                                             u2* const checked_exceptions_length,
 273                                             u4 method_attribute_length,
 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   u2 parse_classfile_nest_members_attribute(const ClassFileStream* const cfs,
 291                                             const u1* const nest_members_attribute_start,
 292                                             TRAPS);
 293 
 294   void parse_classfile_attributes(const ClassFileStream* const cfs,
 295                                   ConstantPool* cp,
 296                                   ClassAnnotationCollector* parsed_annotations,
 297                                   TRAPS);
 298 
 299   void parse_classfile_synthetic_attribute(TRAPS);
 300   void parse_classfile_signature_attribute(const ClassFileStream* const cfs, TRAPS);
 301   void parse_classfile_bootstrap_methods_attribute(const ClassFileStream* const cfs,
 302                                                    ConstantPool* cp,
 303                                                    u4 attribute_length,
 304                                                    TRAPS);
 305 
 306   // Annotations handling
 307   AnnotationArray* assemble_annotations(const u1* const runtime_visible_annotations,
 308                                         int runtime_visible_annotations_length,
 309                                         const u1* const runtime_invisible_annotations,
 310                                         int runtime_invisible_annotations_length,
 311                                         TRAPS);
 312 
 313   void set_precomputed_flags(InstanceKlass* k);
 314 
 315   // Format checker methods
 316   void classfile_parse_error(const char* msg, TRAPS) const;
 317   void classfile_parse_error(const char* msg, int index, TRAPS) const;
 318   void classfile_parse_error(const char* msg, const char *name, TRAPS) const;
 319   void classfile_parse_error(const char* msg,
 320                              int index,
 321                              const char *name,
 322                              TRAPS) const;
 323   void classfile_parse_error(const char* msg,
 324                              const char* name,
 325                              const char* signature,
 326                              TRAPS) const;
 327 
 328   inline void guarantee_property(bool b, const char* msg, TRAPS) const {
 329     if (!b) { classfile_parse_error(msg, CHECK); }
 330   }
 331 
 332   void report_assert_property_failure(const char* msg, TRAPS) const PRODUCT_RETURN;
 333   void report_assert_property_failure(const char* msg, int index, TRAPS) const PRODUCT_RETURN;
 334 
 335   inline void assert_property(bool b, const char* msg, TRAPS) const {
 336 #ifdef ASSERT
 337     if (!b) {
 338       report_assert_property_failure(msg, THREAD);
 339     }
 340 #endif
 341   }
 342 
 343   inline void assert_property(bool b, const char* msg, int index, TRAPS) const {
 344 #ifdef ASSERT
 345     if (!b) {
 346       report_assert_property_failure(msg, index, THREAD);
 347     }
 348 #endif
 349   }
 350 
 351   inline void check_property(bool property,
 352                              const char* msg,
 353                              int index,
 354                              TRAPS) const {
 355     if (_need_verify) {
 356       guarantee_property(property, msg, index, CHECK);
 357     } else {
 358       assert_property(property, msg, index, CHECK);
 359     }
 360   }
 361 
 362   inline void check_property(bool property, const char* msg, TRAPS) const {
 363     if (_need_verify) {
 364       guarantee_property(property, msg, CHECK);
 365     } else {
 366       assert_property(property, msg, CHECK);
 367     }
 368   }
 369 
 370   inline void guarantee_property(bool b,
 371                                  const char* msg,
 372                                  int index,
 373                                  TRAPS) const {
 374     if (!b) { classfile_parse_error(msg, index, CHECK); }
 375   }
 376 
 377   inline void guarantee_property(bool b,
 378                                  const char* msg,
 379                                  const char *name,
 380                                  TRAPS) const {
 381     if (!b) { classfile_parse_error(msg, name, CHECK); }
 382   }
 383 
 384   inline void guarantee_property(bool b,
 385                                  const char* msg,
 386                                  int index,
 387                                  const char *name,
 388                                  TRAPS) const {
 389     if (!b) { classfile_parse_error(msg, index, name, CHECK); }
 390   }
 391 
 392   void throwIllegalSignature(const char* type,
 393                              const Symbol* name,
 394                              const Symbol* sig,
 395                              TRAPS) const;
 396 
 397   void verify_constantvalue(const ConstantPool* const cp,
 398                             int constantvalue_index,
 399                             int signature_index,
 400                             TRAPS) const;
 401 
 402   void verify_legal_utf8(const unsigned char* buffer, int length, TRAPS) const;
 403   void verify_legal_class_name(const Symbol* name, TRAPS) const;
 404   void verify_legal_field_name(const Symbol* name, TRAPS) const;
 405   void verify_legal_method_name(const Symbol* name, TRAPS) const;
 406 
 407   void verify_legal_field_signature(const Symbol* fieldname,
 408                                     const Symbol* signature,
 409                                     TRAPS) const;
 410   int  verify_legal_method_signature(const Symbol* methodname,
 411                                      const Symbol* signature,
 412                                      TRAPS) const;
 413 
 414   void verify_legal_class_modifiers(jint flags, TRAPS) const;
 415   void verify_legal_field_modifiers(jint flags, bool is_interface, TRAPS) const;
 416   void verify_legal_method_modifiers(jint flags,
 417                                      bool is_interface,
 418                                      const Symbol* name,
 419                                      TRAPS) const;
 420 
 421   const char* skip_over_field_signature(const char* signature,
 422                                         bool void_ok,
 423                                         unsigned int length,
 424                                         TRAPS) const;
 425 
 426   bool has_cp_patch_at(int index) const {
 427     assert(index >= 0, "oob");
 428     return (_cp_patches != NULL
 429             && index < _cp_patches->length()
 430             && _cp_patches->adr_at(index)->not_null());
 431   }
 432 
 433   Handle cp_patch_at(int index) const {
 434     assert(has_cp_patch_at(index), "oob");
 435     return _cp_patches->at(index);
 436   }
 437 
 438   Handle clear_cp_patch_at(int index);
 439 
 440   void patch_class(ConstantPool* cp, int class_index, Klass* k, Symbol* name);
 441   void patch_constant_pool(ConstantPool* cp,
 442                            int index,
 443                            Handle patch,
 444                            TRAPS);
 445 
 446   // Wrapper for constantTag.is_klass_[or_]reference.
 447   // In older versions of the VM, Klass*s cannot sneak into early phases of
 448   // constant pool construction, but in later versions they can.
 449   // %%% Let's phase out the old is_klass_reference.
 450   bool valid_klass_reference_at(int index) const {
 451     return _cp->is_within_bounds(index) &&
 452              _cp->tag_at(index).is_klass_or_reference();
 453   }
 454 
 455   // Checks that the cpool index is in range and is a utf8
 456   bool valid_symbol_at(int cpool_index) const {
 457     return _cp->is_within_bounds(cpool_index) &&
 458              _cp->tag_at(cpool_index).is_utf8();
 459   }
 460 
 461   void copy_localvariable_table(const ConstMethod* cm,
 462                                 int lvt_cnt,
 463                                 u2* const localvariable_table_length,
 464                                 const unsafe_u2** const localvariable_table_start,
 465                                 int lvtt_cnt,
 466                                 u2* const localvariable_type_table_length,
 467                                 const unsafe_u2** const localvariable_type_table_start,
 468                                 TRAPS);
 469 
 470   void copy_method_annotations(ConstMethod* cm,
 471                                const u1* runtime_visible_annotations,
 472                                int runtime_visible_annotations_length,
 473                                const u1* runtime_invisible_annotations,
 474                                int runtime_invisible_annotations_length,
 475                                const u1* runtime_visible_parameter_annotations,
 476                                int runtime_visible_parameter_annotations_length,
 477                                const u1* runtime_invisible_parameter_annotations,
 478                                int runtime_invisible_parameter_annotations_length,
 479                                const u1* runtime_visible_type_annotations,
 480                                int runtime_visible_type_annotations_length,
 481                                const u1* runtime_invisible_type_annotations,
 482                                int runtime_invisible_type_annotations_length,
 483                                const u1* annotation_default,
 484                                int annotation_default_length,
 485                                TRAPS);
 486 
 487   // lays out fields in class and returns the total oopmap count
 488   void layout_fields(ConstantPool* cp,
 489                      const FieldAllocationCount* fac,
 490                      const ClassAnnotationCollector* parsed_annotations,
 491                      FieldLayoutInfo* info,
 492                      TRAPS);
 493 
 494  public:
 495   ClassFileParser(ClassFileStream* stream,
 496                   Symbol* name,
 497                   ClassLoaderData* loader_data,
 498                   Handle protection_domain,
 499                   const InstanceKlass* unsafe_anonymous_host,
 500                   GrowableArray<Handle>* cp_patches,
 501                   const bool is_nonfindable,
 502                   const bool can_access_vm_annotations,
 503                   Publicity pub_level,
 504                   TRAPS);
 505 
 506   ~ClassFileParser();
 507 
 508   InstanceKlass* create_instance_klass(bool cf_changed_in_CFLH, InstanceKlass* dynamic_nest_host, Handle classData, TRAPS);
 509 
 510   const ClassFileStream* clone_stream() const;
 511 
 512   void set_klass_to_deallocate(InstanceKlass* klass);
 513 
 514   int static_field_size() const;
 515   int total_oop_map_count() const;
 516   jint layout_size() const;
 517 
 518   int vtable_size() const { return _vtable_size; }
 519   int itable_size() const { return _itable_size; }
 520 
 521   u2 this_class_index() const { return _this_class_index; }
 522 
 523   bool is_unsafe_anonymous() const { return _unsafe_anonymous_host != NULL; }
 524   bool is_nonfindable() const { return _is_nonfindable; }
 525   bool is_interface() const { return _access_flags.is_interface(); }
 526 
 527   const InstanceKlass* unsafe_anonymous_host() const { return _unsafe_anonymous_host; }
 528   const GrowableArray<Handle>* cp_patches() const { return _cp_patches; }
 529   ClassLoaderData* loader_data() const { return _loader_data; }
 530   const Symbol* class_name() const { return _class_name; }
 531   const InstanceKlass* super_klass() const { return _super_klass; }
 532 
 533   ReferenceType reference_type() const { return _rt; }
 534   AccessFlags access_flags() const { return _access_flags; }
 535 
 536   bool is_internal() const { return INTERNAL == _pub_level; }
 537 
 538   static bool verify_unqualified_name(const char* name, unsigned int length, int type);
 539 
 540 #ifdef ASSERT
 541   static bool is_internal_format(Symbol* class_name);
 542 #endif
 543 
 544 };
 545 
 546 #endif // SHARE_CLASSFILE_CLASSFILEPARSER_HPP