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