1 /*
  2  * Copyright (c) 2015, 2023, 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_CDS_CLASSLISTPARSER_HPP
 26 #define SHARE_CDS_CLASSLISTPARSER_HPP
 27 
 28 #include "utilities/exceptions.hpp"
 29 #include "utilities/globalDefinitions.hpp"
 30 #include "utilities/growableArray.hpp"
 31 #include "utilities/resizeableResourceHash.hpp"
 32 
 33 #define LAMBDA_PROXY_TAG "@lambda-proxy"
 34 #define LAMBDA_FORM_TAG  "@lambda-form-invoker"
 35 
 36 class constantPoolHandle;
 37 class Thread;
 38 
 39 class CDSIndyInfo {
 40   GrowableArray<const char*>* _items;
 41 public:
 42   CDSIndyInfo() : _items(nullptr) {}
 43   void add_item(const char* item) {
 44     if (_items == nullptr) {
 45       _items = new GrowableArray<const char*>(9);
 46     }
 47     assert(_items != nullptr, "sanity");
 48     _items->append(item);
 49   }
 50   void add_ref_kind(int ref_kind) {
 51     switch (ref_kind) {
 52     case JVM_REF_getField         : _items->append("REF_getField"); break;
 53     case JVM_REF_getStatic        : _items->append("REF_getStatic"); break;
 54     case JVM_REF_putField         : _items->append("REF_putField"); break;
 55     case JVM_REF_putStatic        : _items->append("REF_putStatic"); break;
 56     case JVM_REF_invokeVirtual    : _items->append("REF_invokeVirtual"); break;
 57     case JVM_REF_invokeStatic     : _items->append("REF_invokeStatic"); break;
 58     case JVM_REF_invokeSpecial    : _items->append("REF_invokeSpecial"); break;
 59     case JVM_REF_newInvokeSpecial : _items->append("REF_newInvokeSpecial"); break;
 60     case JVM_REF_invokeInterface  : _items->append("REF_invokeInterface"); break;
 61     default                       : ShouldNotReachHere();
 62     }
 63   }
 64   GrowableArray<const char*>* items() {
 65     return _items;
 66   }
 67 };
 68 
 69 class ClassListParser : public StackObj {
 70 public:
 71   enum ParseMode {
 72     _parse_all,
 73     _parse_lambda_forms_invokers_only,
 74   };
 75 
 76 private:
 77   // Must be C_HEAP allocated -- we don't want nested resource allocations.
 78   typedef ResizeableResourceHashtable<int, InstanceKlass*,
 79                                       AnyObj::C_HEAP, mtClassShared> ID2KlassTable;
 80 
 81   enum {
 82     _unspecified      = -999,
 83 
 84     // Max number of bytes allowed per line in the classlist.
 85     // Theoretically Java class names could be 65535 bytes in length. Also, an input line
 86     // could have a very long path name up to JVM_MAXPATHLEN bytes in length. In reality,
 87     // 4K bytes is more than enough.
 88     _max_allowed_line_len = 4096,
 89     _line_buf_extra       = 10, // for detecting input too long
 90     _line_buf_size        = _max_allowed_line_len + _line_buf_extra
 91   };
 92 
 93   // Use a small initial size in debug build to test resizing logic
 94   static const int INITIAL_TABLE_SIZE = DEBUG_ONLY(17) NOT_DEBUG(1987);
 95   static const int MAX_TABLE_SIZE = 61333;
 96   static volatile Thread* _parsing_thread; // the thread that created _instance
 97   static ClassListParser* _instance; // the singleton.
 98   const char* _classlist_file;
 99   FILE* _file;
100 
101   ID2KlassTable _id2klass_table;
102 
103   // The following field contains information from the *current* line being
104   // parsed.
105   char                _line[_line_buf_size];  // The buffer that holds the current line. Some characters in
106                                               // the buffer may be overwritten by '\0' during parsing.
107   int                 _line_len;              // Original length of the input line.
108   int                 _line_no;               // Line number for current line being parsed
109   const char*         _class_name;
110   GrowableArray<const char*>* _indy_items;    // items related to invoke dynamic for archiving lambda proxy classes
111   int                 _id;
112   int                 _super;
113   GrowableArray<int>* _interfaces;
114   bool                _interfaces_specified;
115   const char*         _source;
116   bool                _lambda_form_line;
117   ParseMode           _parse_mode;
118 
119   bool parse_int_option(const char* option_name, int* value);
120   bool parse_uint_option(const char* option_name, int* value);
121   InstanceKlass* load_class_from_source(Symbol* class_name, TRAPS);
122   ID2KlassTable* id2klass_table() {
123     return &_id2klass_table;
124   }
125   InstanceKlass* lookup_class_by_id(int id);
126   void print_specified_interfaces();
127   void print_actual_interfaces(InstanceKlass *ik);
128   bool is_matching_cp_entry(const constantPoolHandle &pool, int cp_index, TRAPS);
129 
130   void resolve_indy(JavaThread* current, Symbol* class_name_symbol);
131   void resolve_indy_impl(Symbol* class_name_symbol, TRAPS);
132   bool parse_one_line();
133   Klass* load_current_class(Symbol* class_name_symbol, TRAPS);
134 
135   ClassListParser(const char* file, ParseMode _parse_mode);
136   ~ClassListParser();
137 
138 public:
139   static int parse_classlist(const char* classlist_path, ParseMode parse_mode, TRAPS) {
140     ClassListParser parser(classlist_path, parse_mode);
141     return parser.parse(THREAD); // returns the number of classes loaded.
142   }
143 
144   static bool is_parsing_thread();
145   static ClassListParser* instance() {
146     assert(is_parsing_thread(), "call this only in the thread that created ClassListParsing::_instance");
147     assert(_instance != nullptr, "must be");
148     return _instance;
149   }
150 
151   int parse(TRAPS);
152   void split_tokens_by_whitespace(int offset);
153   int split_at_tag_from_line();
154   bool parse_at_tags();
155   char* _token;
156   void error(const char* msg, ...);
157   void parse_int(int* value);
158   void parse_uint(int* value);
159   bool try_parse_uint(int* value);
160   bool skip_token(const char* option_name);
161   void skip_whitespaces();
162   void skip_non_whitespaces();
163 
164   bool is_id_specified() {
165     return _id != _unspecified;
166   }
167   bool is_super_specified() {
168     return _super != _unspecified;
169   }
170   bool are_interfaces_specified() {
171     return _interfaces->length() > 0;
172   }
173   int id() {
174     assert(is_id_specified(), "do not query unspecified id");
175     return _id;
176   }
177   int super() {
178     assert(is_super_specified(), "do not query unspecified super");
179     return _super;
180   }
181   void check_already_loaded(const char* which, int id) {
182     if (!id2klass_table()->contains(id)) {
183       error("%s id %d is not yet loaded", which, id);
184     }
185   }
186 
187   const char* current_class_name() {
188     return _class_name;
189   }
190 
191   bool is_loading_from_source();
192 
193   bool lambda_form_line() { return _lambda_form_line; }
194 
195   // Look up the super or interface of the current class being loaded
196   // (in this->load_current_class()).
197   InstanceKlass* lookup_super_for_current_class(Symbol* super_name);
198   InstanceKlass* lookup_interface_for_current_class(Symbol* interface_name);
199 
200   static void populate_cds_indy_info(const constantPoolHandle &pool, int cp_index, CDSIndyInfo* cii, TRAPS);
201 };
202 #endif // SHARE_CDS_CLASSLISTPARSER_HPP