1 /*
  2  * Copyright (c) 2011, 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_OOPS_FIELDSTREAMS_HPP
 26 #define SHARE_OOPS_FIELDSTREAMS_HPP
 27 
 28 #include "oops/fieldInfo.hpp"
 29 #include "oops/instanceKlass.hpp"
 30 #include "runtime/fieldDescriptor.hpp"
 31 
 32 // The is the base class for iteration over the fields array
 33 // describing the declared fields in the class.  Several subclasses
 34 // are provided depending on the kind of iteration required.  The
 35 // JavaFieldStream is for iterating over regular Java fields and it
 36 // generally the preferred iterator.  InternalFieldStream only
 37 // iterates over fields that have been injected by the JVM.
 38 // AllFieldStream exposes all fields and should only be used in rare
 39 // cases.
 40 // HierarchicalFieldStream allows to also iterate over fields of supertypes.
 41 class FieldStreamBase : public StackObj {
 42  protected:
 43   const Array<u1>*    _fieldinfo_stream;
 44   FieldInfoReader     _reader;
 45   constantPoolHandle  _constants;
 46   int                 _index;
 47   int                 _limit;
 48 
 49   FieldInfo           _fi_buf;
 50   fieldDescriptor     _fd_buf;
 51 
 52   FieldInfo const * field() const {
 53     assert(!done(), "no more fields");
 54     return &_fi_buf;
 55   }
 56 
 57   inline FieldStreamBase(const Array<u1>* fieldinfo_stream, ConstantPool* constants, int start, int limit);
 58 
 59   inline FieldStreamBase(const Array<u1>* fieldinfo_stream, ConstantPool* constants);
 60 
 61  private:
 62    void initialize() {
 63     int java_fields_count;
 64     int injected_fields_count;
 65     _reader.read_field_counts(&java_fields_count, &injected_fields_count);
 66     if (_limit < _index) {
 67       _limit = java_fields_count + injected_fields_count;
 68     } else {
 69       assert( _limit <= java_fields_count + injected_fields_count, "Safety check");
 70     }
 71     if (_limit != 0) {
 72       _reader.read_field_info(_fi_buf);
 73     }
 74    }
 75 
 76  public:
 77   inline FieldStreamBase(InstanceKlass* klass);
 78 
 79   // accessors
 80   int index() const                 { return _index; }
 81   InstanceKlass* field_holder() const { return _constants->pool_holder(); }
 82 
 83   void next() {
 84     _index += 1;
 85     if (done()) return;
 86     _reader.read_field_info(_fi_buf);
 87   }
 88   bool done() const { return _index >= _limit; }
 89 
 90   // Accessors for current field
 91   AccessFlags access_flags() const {
 92     return field()->access_flags();
 93   }
 94 
 95   FieldInfo::FieldFlags field_flags() const {
 96     return field()->field_flags();
 97   }
 98 
 99   Symbol* name() const {
100     return field()->name(_constants());
101   }
102 
103   Symbol* signature() const {
104     return field()->signature(_constants());
105   }
106 
107   Symbol* generic_signature() const {
108     if (field()->field_flags().is_generic()) {
109       return _constants->symbol_at(field()->generic_signature_index());
110     } else {
111       return nullptr;
112     }
113   }
114 
115   int offset() const {
116     return field()->offset();
117   }
118 
119   bool is_null_free_inline_type() {
120     return field()->field_flags().is_null_free_inline_type();
121   }
122 
123   bool is_flat() const {
124     return field()->field_flags().is_flat();
125   }
126 
127   bool is_contended() const {
128     return field()->is_contended();
129   }
130 
131   int contended_group() const {
132     return field()->contended_group();
133   }
134 
135   int null_marker_offset() const {
136     return field()->null_marker_offset();
137   }
138 
139   // Convenient methods
140 
141   const FieldInfo& to_FieldInfo() const {
142     return _fi_buf;
143   }
144 
145   int num_total_fields() const {
146     return FieldInfoStream::num_total_fields(_fieldinfo_stream);
147   }
148 
149   // bridge to a heavier API:
150   fieldDescriptor& field_descriptor() const {
151     fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
152     field.reinitialize(field_holder(), to_FieldInfo());
153     return field;
154   }
155 };
156 
157 // Iterate over only the Java fields
158 class JavaFieldStream : public FieldStreamBase {
159   Array<u1>* _search_table;
160 
161  public:
162   JavaFieldStream(const InstanceKlass* k): FieldStreamBase(k->fieldinfo_stream(), k->constants(), 0, k->java_fields_count()),
163     _search_table(k->fieldinfo_search_table()) {}
164 
165   u2 name_index() const {
166     assert(!field()->field_flags().is_injected(), "regular only");
167     return field()->name_index();
168   }
169 
170   u2 signature_index() const {
171     assert(!field()->field_flags().is_injected(), "regular only");
172     return field()->signature_index();
173   }
174 
175   u2 generic_signature_index() const {
176     assert(!field()->field_flags().is_injected(), "regular only");
177     if (field()->field_flags().is_generic()) {
178       return field()->generic_signature_index();
179     }
180     return 0;
181   }
182 
183   u2 initval_index() const {
184     assert(!field()->field_flags().is_injected(), "regular only");
185     return field()->initializer_index();
186   }
187 
188   // Performs either a linear search or binary search through the stream
189   // looking for a matching name/signature combo
190   bool lookup(const Symbol* name, const Symbol* signature);
191 };
192 
193 
194 // Iterate over only the internal fields
195 class InternalFieldStream : public FieldStreamBase {
196  public:
197   InternalFieldStream(InstanceKlass* k):      FieldStreamBase(k->fieldinfo_stream(), k->constants(), k->java_fields_count(), 0) {}
198 };
199 
200 
201 class AllFieldStream : public FieldStreamBase {
202  public:
203   AllFieldStream(const InstanceKlass* k):      FieldStreamBase(k->fieldinfo_stream(), k->constants()) {}
204 };
205 
206 /* Very generally, a base class for a stream adapter, a derived class just implements
207  * current_stream that returns a FieldStreamType, and this adapter takes care of providing
208  * the methods of FieldStreamBase.
209  *
210  * In practice, this is used to provide a stream over the fields of a class and its superclasses
211  * and interfaces. The derived class of HierarchicalFieldStreamBase decides in which order we iterate
212  * on the superclasses (and interfaces), and the template parameter FieldStreamType is the underlying
213  * stream we use to iterate over the fields each class. Methods such as done and next are still up to
214  * the derived classes, allowing them to iterate over the class hierarchy, but also skip elements that
215  * the underlying FieldStreamType would otherwise include.
216  */
217 template<typename FieldStreamType>
218 class HierarchicalFieldStreamBase : public StackObj {
219   virtual FieldStreamType& current_stream() = 0;
220   virtual const FieldStreamType& current_stream() const = 0;
221 
222 public:
223   // bridge functions from FieldStreamBase
224   int index() const {
225     return current_stream().index();
226   }
227 
228   AccessFlags access_flags() const {
229     return current_stream().access_flags();
230   }
231 
232   FieldInfo::FieldFlags field_flags() const {
233     return current_stream().field_flags();
234   }
235 
236   Symbol* name() const {
237     return current_stream().name();
238   }
239 
240   Symbol* signature() const {
241     return current_stream().signature();
242   }
243 
244   Symbol* generic_signature() const {
245     return current_stream().generic_signature();
246   }
247 
248   int offset() const {
249     return current_stream().offset();
250   }
251 
252   bool is_contended() const {
253     return current_stream().is_contended();
254   }
255 
256   int contended_group() const {
257     return current_stream().contended_group();
258   }
259 
260   FieldInfo to_FieldInfo() {
261     return current_stream().to_FieldInfo();
262   }
263 
264   fieldDescriptor& field_descriptor() const {
265     return current_stream().field_descriptor();
266   }
267 
268   bool is_flat() const {
269     return current_stream().is_flat();
270   }
271 
272   bool is_null_free_inline_type() {
273     return current_stream().is_null_free_inline_type();
274   }
275 
276   int null_marker_offset() {
277     return current_stream().null_marker_offset();
278   }
279 };
280 
281 /* Iterate over fields including the ones declared in supertypes.
282  * Derived classes are traversed before base classes, and interfaces
283  * at the end.
284  */
285 template<typename FieldStreamType>
286 class HierarchicalFieldStream final : public HierarchicalFieldStreamBase<FieldStreamType>  {
287  private:
288   const Array<InstanceKlass*>* _interfaces;
289   InstanceKlass* _next_klass; // null indicates no more type to visit
290   FieldStreamType _current_stream;
291   int _interface_index;
292 
293   void prepare() {
294     _next_klass = next_klass_with_fields();
295     // special case: the initial klass has no fields. If any supertype has any fields, use that directly.
296     // if no such supertype exists, done() will return false already.
297     next_stream_if_done();
298   }
299 
300   InstanceKlass* next_klass_with_fields() {
301     assert(_next_klass != nullptr, "reached end of types already");
302     InstanceKlass* result = _next_klass;
303     do  {
304       if (!result->is_interface() && result->super() != nullptr) {
305         result = result->super();
306       } else if (_interface_index > 0) {
307         result = _interfaces->at(--_interface_index);
308       } else {
309         return nullptr; // we did not find any more supertypes with fields
310       }
311     } while (FieldStreamType(result).done());
312     return result;
313   }
314 
315   // sets _current_stream to the next if the current is done and any more is available
316   void next_stream_if_done() {
317     if (_next_klass != nullptr && _current_stream.done()) {
318       _current_stream = FieldStreamType(_next_klass);
319       assert(!_current_stream.done(), "created empty stream");
320       _next_klass = next_klass_with_fields();
321     }
322   }
323 
324   FieldStreamType& current_stream() override { return _current_stream; }
325   const FieldStreamType& current_stream() const override { return _current_stream; }
326 
327  public:
328   explicit HierarchicalFieldStream(InstanceKlass* klass) :
329     _interfaces(klass->transitive_interfaces()),
330     _next_klass(klass),
331     _current_stream(FieldStreamType(klass)),
332     _interface_index(_interfaces->length()) {
333       prepare();
334   }
335 
336   void next() {
337     _current_stream.next();
338     next_stream_if_done();
339   }
340 
341   bool done() const { return _next_klass == nullptr && _current_stream.done(); }
342 };
343 
344 /* Iterates on the fields of a class and its super-class top-down (java.lang.Object first)
345  * Doesn't traverse interfaces for now, because it's not clear which order would make sense
346  * Let's decide when or if the need arises. Since we are not traversing interfaces, we
347  * wouldn't get all the static fields, and since the current use-case of this stream does not
348  * care about static fields, we restrict it to regular non-static fields.
349  */
350 class TopDownHierarchicalNonStaticFieldStreamBase final : public HierarchicalFieldStreamBase<JavaFieldStream> {
351   GrowableArray<InstanceKlass*>* _super_types;  // Self and super type, bottom up
352   int _current_stream_index;
353   JavaFieldStream _current_stream;
354 
355   void next_stream_if_needed() {
356     precond(_current_stream_index >= 0);
357     while (_current_stream.done()) {
358       _current_stream_index--;
359       if (_current_stream_index < 0) {
360         return;
361       }
362       _current_stream = JavaFieldStream(_super_types->at(_current_stream_index));
363     }
364   }
365 
366   GrowableArray<InstanceKlass*>* get_super_types(InstanceKlass* klass) {
367     auto super_types = new GrowableArray<InstanceKlass*>();
368     do {
369       super_types->push(klass);
370     } while ((klass = klass->java_super()) != nullptr);
371     return super_types;
372   }
373 
374   void raw_next() {
375     _current_stream.next();
376     next_stream_if_needed();
377   }
378 
379   void closest_nonstatic() {
380     while (!done() && access_flags().is_static()) {
381       raw_next();
382     }
383   }
384 
385   JavaFieldStream& current_stream() override { return _current_stream; }
386   const JavaFieldStream& current_stream() const override { return _current_stream; }
387 
388  public:
389   explicit TopDownHierarchicalNonStaticFieldStreamBase(InstanceKlass* klass) :
390     _super_types(get_super_types(klass)),
391     _current_stream_index(_super_types->length() - 1),
392     _current_stream(JavaFieldStream(_super_types->at(_current_stream_index))) {
393     next_stream_if_needed();
394     closest_nonstatic();
395   }
396 
397   void next() {
398     raw_next();
399     closest_nonstatic();
400   }
401 
402   bool done() const { return _current_stream_index < 0; }
403 };
404 
405 #endif // SHARE_OOPS_FIELDSTREAMS_HPP