1 /*
   2  * Copyright (c) 2015, 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_MEMORY_CLASSLISTPARSER_HPP
  26 #define SHARE_VM_MEMORY_CLASSLISTPARSER_HPP
  27 
  28 #include "utilities/exceptions.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 #include "utilities/hashtable.hpp"
  31 
  32 class CDSClassInfo;
  33 
  34 // Look up from ID -> InstanceKlass*
  35 class ID2KlassTable : public Hashtable<InstanceKlass*, mtClass> {
  36 public:
  37   ID2KlassTable() : Hashtable<InstanceKlass*, mtClass>(1987, sizeof(HashtableEntry<InstanceKlass*, mtClass>)) { }
  38   void add(int id, InstanceKlass* klass) {
  39     unsigned int hash = (unsigned int)id;
  40     HashtableEntry<InstanceKlass*, mtClass>* entry = new_entry(hash, klass);
  41     add_entry(hash_to_index(hash), entry);
  42   }
  43 
  44   InstanceKlass* lookup(int id) {
  45     unsigned int hash = (unsigned int)id;
  46     int index = hash_to_index(id);
  47     for (HashtableEntry<InstanceKlass*, mtClass>* e = bucket(index); e != NULL; e = e->next()) {
  48       if (e->hash() == hash) {
  49         return e->literal();
  50       }
  51     }
  52     return NULL;
  53   }
  54 };
  55 
  56 class ClassListParser : public StackObj {
  57   enum {
  58     _unspecified      = -999,
  59 
  60     // Max number of bytes allowed per line in the classlist.
  61     // Theoretically Java class names could be 65535 bytes in length. Also, an input line
  62     // could have a very long path name up to JVM_MAXPATHLEN bytes in length. In reality,
  63     // 4K bytes is more than enough.
  64     _max_allowed_line_len = 4096,
  65     _line_buf_extra       = 10, // for detecting input too long
  66     _line_buf_size        = _max_allowed_line_len + _line_buf_extra
  67   };
  68 
  69   static ClassListParser* _instance; // the singleton.
  70   const char* _classlist_file;
  71   FILE* _file;
  72 
  73   ID2KlassTable _id2klass_table;
  74 
  75   // The following field contains information from the *current* line being
  76   // parsed.
  77   char                _line[_line_buf_size];  // The buffer that holds the current line. Some characters in
  78                                               // the buffer may be overwritten by '\0' during parsing.
  79   int                 _line_len;              // Original length of the input line.
  80   int                 _line_no;               // Line number for current line being parsed
  81   const char*         _class_name;
  82   int                 _id;
  83   int                 _super;
  84   GrowableArray<int>* _interfaces;
  85   bool                _interfaces_specified;
  86   const char*         _source;
  87 
  88   bool parse_int_option(const char* option_name, int* value);
  89   InstanceKlass* load_class_from_source(Symbol* class_name, TRAPS);
  90   ID2KlassTable *table() {
  91     return &_id2klass_table;
  92   }
  93   InstanceKlass* lookup_class_by_id(int id);
  94   void print_specified_interfaces();
  95   void print_actual_interfaces(InstanceKlass *ik);
  96 public:
  97   ClassListParser(const char* file);
  98   ~ClassListParser();
  99 
 100   static ClassListParser* instance() {
 101     return _instance;
 102   }
 103   bool parse_one_line();
 104   char* _token;
 105   void error(const char* msg, ...);
 106   void parse_int(int* value);
 107   bool try_parse_int(int* value);
 108   bool skip_token(const char* option_name);
 109   void skip_whitespaces();
 110   void skip_non_whitespaces();
 111 
 112   bool is_id_specified() {
 113     return _id != _unspecified;
 114   }
 115   bool is_super_specified() {
 116     return _super != _unspecified;
 117   }
 118   bool are_interfaces_specified() {
 119     return _interfaces->length() > 0;
 120   }
 121   int id() {
 122     assert(is_id_specified(), "do not query unspecified id");
 123     return _id;
 124   }
 125   int super() {
 126     assert(is_super_specified(), "do not query unspecified super");
 127     return _super;
 128   }
 129   void check_already_loaded(const char* which, int id) {
 130     if (_id2klass_table.lookup(id) == NULL) {
 131       error("%s id %d is not yet loaded", which, id);
 132     }
 133   }
 134 
 135   const char* current_class_name() {
 136     return _class_name;
 137   }
 138 
 139   InstanceKlass* load_current_class(TRAPS);
 140 
 141   bool is_loading_from_source();
 142 
 143   // Look up the super or interface of the current class being loaded
 144   // (in this->load_current_class()).
 145   InstanceKlass* lookup_super_for_current_class(Symbol* super_name);
 146   InstanceKlass* lookup_interface_for_current_class(Symbol* interface_name);
 147 };
 148 #endif