1 /*
  2  * Copyright (c) 1999, 2025, 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_CI_CIINSTANCEKLASS_HPP
 26 #define SHARE_CI_CIINSTANCEKLASS_HPP
 27 
 28 #include "ci/ciConstantPoolCache.hpp"
 29 #include "ci/ciFlags.hpp"
 30 #include "ci/ciKlass.hpp"
 31 #include "ci/ciSymbol.hpp"
 32 #include "oops/instanceKlass.hpp"
 33 
 34 // ciInstanceKlass
 35 //
 36 // This class represents a Klass* in the HotSpot virtual machine
 37 // whose Klass part is an InstanceKlass.  It may or may not
 38 // be loaded.
 39 class ciInstanceKlass : public ciKlass {
 40   CI_PACKAGE_ACCESS
 41   friend class ciBytecodeStream;
 42   friend class ciEnv;
 43   friend class ciExceptionHandler;
 44   friend class ciMethod;
 45   friend class ciField;
 46   friend class ciReplay;
 47 
 48 private:
 49   enum SubklassValue { subklass_unknown, subklass_false, subklass_true };
 50 
 51   jobject                _loader;
 52 
 53   InstanceKlass::ClassState _init_state;           // state of class
 54   bool                   _is_shared;
 55   bool                   _has_finalizer;
 56   SubklassValue          _has_subklass;
 57   bool                   _has_nonstatic_fields;
 58   bool                   _has_nonstatic_concrete_methods;
 59   bool                   _is_hidden;
 60   bool                   _is_record;
 61   bool                   _has_trusted_loader;
 62 
 63   ciFlags                _flags;
 64 
 65   // Lazy fields get filled in only upon request.
 66   ciInstanceKlass*       _super;
 67   ciInstance*            _java_mirror;
 68 
 69   ciConstantPoolCache*   _field_cache;  // cached map index->field
 70 
 71   // Fields declared in the bytecode (without nested fields in flat fields),
 72   // ordered in JavaFieldStream order, with superclasses first (i.e. from lang.java.Object
 73   // to most derived class).
 74   const GrowableArray<ciField*>* _declared_nonstatic_fields;
 75 
 76   // Fields laid out in memory (flat fields are expanded into their components). The ciField object
 77   // for each primitive component has the holder being this ciInstanceKlass or one of its
 78   // superclasses.
 79   // Fields are in the same order as in _declared_nonstatic_fields, but flat fields are replaced by
 80   // the list of their own fields, ordered the same way (hierarchy traversed top-down, in
 81   // JavaFieldStream order).
 82   const GrowableArray<ciField*>* _nonstatic_fields;
 83 
 84   int                    _has_injected_fields; // any non static injected fields? lazily initialized.
 85 
 86   // The possible values of the _implementor fall into following three cases:
 87   //   null: no implementor.
 88   //   A ciInstanceKlass that's not itself: one implementor.
 89   //   Itself: more than one implementor.
 90   ciInstanceKlass*       _implementor;
 91   GrowableArray<ciInstanceKlass*>* _transitive_interfaces;
 92 
 93   void compute_injected_fields();
 94   bool compute_injected_fields_helper();
 95   void compute_transitive_interfaces();
 96 
 97 protected:
 98   ciInstanceKlass(Klass* k);
 99   ciInstanceKlass(ciSymbol* name, jobject loader, BasicType bt = T_OBJECT); // for unloaded klasses
100 
101   InstanceKlass* get_instanceKlass() const {
102     return InstanceKlass::cast(get_Klass());
103   }
104 
105   oop loader();
106   jobject loader_handle();
107 
108   const char* type_string() { return "ciInstanceKlass"; }
109 
110   bool is_in_package_impl(const char* packagename, int len);
111 
112   void print_impl(outputStream* st);
113 
114   ciConstantPoolCache* field_cache();
115 
116   bool is_shared() { return _is_shared; }
117 
118   void compute_shared_init_state();
119   bool compute_shared_has_subklass();
120   void compute_nonstatic_fields();
121   void compute_nonstatic_fields_impl(const GrowableArray<ciField*>* super_declared_fields, const GrowableArray<ciField*>* super_fields);
122   bool compute_has_trusted_loader();
123 
124   // Update the init_state for shared klasses
125   void update_if_shared(InstanceKlass::ClassState expected) {
126     if (_is_shared && _init_state != expected) {
127       if (is_loaded()) compute_shared_init_state();
128     }
129   }
130 
131 public:
132   // Has this klass been initialized?
133   bool                   is_initialized() {
134     update_if_shared(InstanceKlass::fully_initialized);
135     return _init_state == InstanceKlass::fully_initialized;
136   }
137   bool                   is_not_initialized() {
138     update_if_shared(InstanceKlass::fully_initialized);
139     return _init_state < InstanceKlass::being_initialized;
140   }
141   // Is this klass being initialized?
142   bool                   is_being_initialized() {
143     update_if_shared(InstanceKlass::being_initialized);
144     return _init_state == InstanceKlass::being_initialized;
145   }
146   // Has this klass been linked?
147   bool                   is_linked() {
148     update_if_shared(InstanceKlass::linked);
149     return _init_state >= InstanceKlass::linked;
150   }
151   // Is this klass in error state?
152   bool                   is_in_error_state() {
153     update_if_shared(InstanceKlass::initialization_error);
154     return _init_state == InstanceKlass::initialization_error;
155   }
156 
157   // General klass information.
158   ciFlags                flags()          {
159     assert(is_loaded(), "must be loaded");
160     return _flags;
161   }
162   bool                   has_finalizer()  {
163     assert(is_loaded(), "must be loaded");
164     return _has_finalizer; }
165   bool                   has_subklass()   {
166     assert(is_loaded(), "must be loaded");
167     // Ignore cached subklass_false case.
168     // It could be invalidated by concurrent class loading and
169     // can result in type paradoxes during compilation when
170     // a subclass is observed, but has_subklass() returns false.
171     if (_has_subklass == subklass_true) {
172       return true;
173     }
174     if (flags().is_final()) {
175       return false;
176     }
177     return compute_shared_has_subklass();
178   }
179 
180   jint                   layout_helper_size_in_bytes()  {
181     return Klass::layout_helper_size_in_bytes(layout_helper());
182   }
183   jint                   size_helper()  {
184     return (Klass::layout_helper_size_in_bytes(layout_helper())
185             >> LogHeapWordSize);
186   }
187   jint                   has_nonstatic_fields()  {
188     assert(is_loaded(), "must be loaded");
189     return _has_nonstatic_fields; }
190   ciInstanceKlass*       super();
191   jint                   nof_implementors() {
192     ciInstanceKlass* impl;
193     assert(is_loaded(), "must be loaded");
194     impl = implementor();
195     if (impl == nullptr) {
196       return 0;
197     } else if (impl != this) {
198       return 1;
199     } else {
200       return 2;
201     }
202   }
203   bool has_nonstatic_concrete_methods()  {
204     assert(is_loaded(), "must be loaded");
205     return _has_nonstatic_concrete_methods;
206   }
207 
208   bool is_hidden() const {
209     return _is_hidden;
210   }
211 
212   bool is_record() const {
213     return _is_record;
214   }
215 
216   ciInstanceKlass* get_canonical_holder(int offset);
217   ciField* get_field_by_offset(int field_offset, bool is_static);
218   ciField* get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static);
219   // Get field descriptor at field_offset ignoring flattening
220   ciField* get_non_flat_field_by_offset(int field_offset);
221   // Get the index of the declared field that contains this offset
222   int field_index_by_offset(int offset);
223 
224   // Total number of nonstatic fields (including inherited)
225   int nof_declared_nonstatic_fields() {
226     if (_declared_nonstatic_fields == nullptr) {
227       compute_nonstatic_fields();
228     }
229     return _declared_nonstatic_fields->length();
230   }
231 
232   int nof_nonstatic_fields() {
233     if (_nonstatic_fields == nullptr) {
234       compute_nonstatic_fields();
235     }
236     return _nonstatic_fields->length();
237   }
238 
239   bool has_injected_fields() {
240     if (_has_injected_fields == -1) {
241       compute_injected_fields();
242     }
243     return _has_injected_fields > 0 ? true : false;
244   }
245 
246   bool has_object_fields() const;
247 
248   ciField* declared_nonstatic_field_at(int i) {
249     assert(_declared_nonstatic_fields != nullptr, "should be initialized");
250     return _declared_nonstatic_fields->at(i);
251   }
252 
253   ciField* nonstatic_field_at(int i) {
254     assert(_nonstatic_fields != nullptr, "");
255     return _nonstatic_fields->at(i);
256   }
257 
258   ciInstanceKlass* unique_concrete_subklass();
259   bool has_finalizable_subclass();
260 
261   bool contains_field_offset(int offset);
262 
263   // Get the instance of java.lang.Class corresponding to
264   // this klass.  This instance is used for locking of
265   // synchronized static methods of this klass.
266   ciInstance*            java_mirror();
267 
268   // Java access flags
269   bool is_public      () { return flags().is_public(); }
270   bool is_final       () { return flags().is_final(); }
271   bool is_interface   () { return flags().is_interface(); }
272   bool is_abstract    () { return flags().is_abstract(); }
273   bool is_abstract_value_klass() { return is_abstract() && !flags().is_identity(); }
274 
275   ciMethod* find_method(ciSymbol* name, ciSymbol* signature);
276   // Note:  To find a method from name and type strings, use ciSymbol::make,
277   // but consider adding to vmSymbols.hpp instead.
278 
279   bool is_leaf_type();
280   ciInstanceKlass* implementor();
281 
282   ciInstanceKlass* unique_implementor() {
283     assert(is_loaded(), "must be loaded");
284     ciInstanceKlass* impl = implementor();
285     return (impl != this ? impl : nullptr);
286   }
287 
288   virtual bool can_be_inline_klass(bool is_exact = false);
289 
290   // Is the defining class loader of this class the default loader?
291   bool uses_default_loader() const;
292 
293   bool is_java_lang_Object() const;
294 
295   BasicType box_klass_type() const;
296   bool is_box_klass() const;
297   bool is_boxed_value_offset(int offset) const;
298 
299   // Is this klass in the given package?
300   bool is_in_package(const char* packagename) {
301     return is_in_package(packagename, (int) strlen(packagename));
302   }
303   bool is_in_package(const char* packagename, int len);
304 
305   // What kind of ciObject is this?
306   bool is_instance_klass() const { return true; }
307 
308   virtual ciKlass* exact_klass() {
309     if (is_loaded() && is_final() && !is_interface()) {
310       return this;
311     }
312     return nullptr;
313   }
314 
315   bool can_be_instantiated() {
316     assert(is_loaded(), "must be loaded");
317     return !is_interface() && !is_abstract();
318   }
319 
320   bool has_trusted_loader() const {
321     return _has_trusted_loader;
322   }
323   GrowableArray<ciInstanceKlass*>* transitive_interfaces() const;
324 
325   // Replay support
326 
327   // Dump the current state of this klass for compilation replay.
328   virtual void dump_replay_data(outputStream* out);
329 
330   static void dump_replay_instanceKlass(outputStream* out, InstanceKlass* ik);
331 
332 
333   // Return stable class name suitable for replay file.
334   const char *replay_name() const;
335 
336 #ifdef ASSERT
337   bool debug_final_field_at(int offset);
338   bool debug_stable_field_at(int offset);
339 #endif
340 };
341 
342 #endif // SHARE_CI_CIINSTANCEKLASS_HPP