1 /* 2 * Copyright (c) 2011, 2026, 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_FIELDINFO_HPP 26 #define SHARE_OOPS_FIELDINFO_HPP 27 28 #include "memory/allocation.hpp" 29 #include "oops/layoutKind.hpp" 30 #include "oops/typeArrayOop.hpp" 31 #include "utilities/accessFlags.hpp" 32 #include "utilities/unsigned5.hpp" 33 #include "utilities/vmEnums.hpp" 34 35 static constexpr u4 flag_mask(int pos) { 36 return (u4)1 << pos; 37 } 38 39 40 // Helper class for access to the underlying Array<u1> used to 41 // store the compressed stream of FieldInfo 42 template<typename ARR, typename OFF> 43 struct ArrayHelper { 44 uint8_t operator()(ARR a, OFF i) const { return a->at(i); }; 45 void operator()(ARR a, OFF i, uint8_t b) const { a->at_put(i,b); }; 46 // So, an expression ArrayWriterHelper() acts like these lambdas: 47 // auto get = [&](ARR a, OFF i){ return a[i]; }; 48 // auto set = [&](ARR a, OFF i, uint8_t x){ a[i] = x; }; 49 }; 50 51 // This class represents the field information contained in the fields 52 // array of an InstanceKlass. Currently it's laid on top an array of 53 // Java shorts but in the future it could simply be used as a real 54 // array type. FieldInfo generally shouldn't be used directly. 55 // Fields should be queried either through InstanceKlass or through 56 // the various FieldStreams. 57 class FieldInfo { 58 friend class fieldDescriptor; 59 friend class JavaFieldStream; 60 friend class ClassFileParser; 61 friend class FieldInfoStream; 62 friend class FieldStreamBase; 63 friend class FieldInfoReader; 64 friend class VMStructs; 65 66 public: 67 68 class FieldFlags { 69 friend class VMStructs; 70 friend class JVMCIVMStructs; 71 friend class FieldDesc; 72 73 // The ordering of this enum is totally internal. More frequent 74 // flags should come earlier than less frequent ones, because 75 // earlier ones compress better. 76 enum FieldFlagBitPosition { 77 _ff_null_free_inline_type, // field's type is an inline type and the field is null free 78 _ff_flat, // field is a flat field, optional section includes a layout kind 79 _ff_null_marker, // field has a null marker, optional section includes the null marker offset 80 _ff_initialized, // has ConstantValue initializer attribute 81 _ff_injected, // internal field injected by the JVM 82 _ff_generic, // has a generic signature 83 _ff_stable, // trust as stable b/c declared as @Stable 84 _ff_contended // is contended, may have contention-group 85 }; 86 87 // Some but not all of the flag bits signal the presence of an 88 // additional 32-bit item in the field record. 89 static const u4 _optional_item_bit_mask = 90 flag_mask((int)_ff_initialized) | 91 flag_mask((int)_ff_generic) | 92 flag_mask((int)_ff_contended) | 93 flag_mask((int)_ff_flat) | 94 flag_mask((int)_ff_null_marker); 95 96 // boilerplate: 97 u4 _flags; 98 99 bool test_flag(FieldFlagBitPosition pos) const { 100 return (_flags & flag_mask(pos)) != 0; 101 } 102 void update_flag(FieldFlagBitPosition pos, bool z) { 103 if (z) _flags |= flag_mask(pos); 104 else _flags &= ~flag_mask(pos); 105 } 106 107 public: 108 FieldFlags(u4 flags) { 109 _flags = flags; 110 } 111 u4 as_uint() const { return _flags; } 112 bool has_any_optionals() const { 113 return (_flags & _optional_item_bit_mask) != 0; 114 } 115 116 bool is_initialized() const { return test_flag(_ff_initialized); } 117 bool is_null_free_inline_type() const { return test_flag(_ff_null_free_inline_type); } 118 bool is_flat() const { return test_flag(_ff_flat); } 119 bool is_injected() const { return test_flag(_ff_injected); } 120 bool is_generic() const { return test_flag(_ff_generic); } 121 bool is_stable() const { return test_flag(_ff_stable); } 122 bool is_contended() const { return test_flag(_ff_contended); } 123 bool has_null_marker() const { return test_flag(_ff_null_marker); } 124 125 void update_initialized(bool z) { update_flag(_ff_initialized, z); } 126 void update_null_free_inline_type(bool z) { update_flag(_ff_null_free_inline_type, z); } 127 void update_flat(bool z) { update_flag(_ff_flat, z); } 128 void update_injected(bool z) { update_flag(_ff_injected, z); } 129 void update_generic(bool z) { update_flag(_ff_generic, z); } 130 void update_stable(bool z) { update_flag(_ff_stable, z); } 131 void update_contended(bool z) { update_flag(_ff_contended, z); } 132 void update_null_marker(bool z) { update_flag(_ff_null_marker, z); } 133 }; 134 135 private: 136 // The following items are the unpacked bitwise information content 137 // of a field record. Per-field metadata extracted from the class 138 // file are stored logically as a group of these items. The 139 // classfile parser produces these records in a temporary array, and 140 // then compresses them into a FieldInfoStream. 141 // 142 u4 _index; // which field it is 143 u2 _name_index; // index in CP of name 144 u2 _signature_index; // index in CP of descriptor 145 u4 _offset; // offset in object layout 146 AccessFlags _access_flags; // access flags (JVM spec) 147 FieldFlags _field_flags; // VM defined flags (not JVM spec) 148 LayoutKind _layout_kind; // LayoutKind if the field is flat 149 u4 _null_marker_offset; // null marker offset for this field in the object layout 150 u2 _initializer_index; // index from ConstantValue attr (or 0) 151 u2 _generic_signature_index; // index from GenericSignature attr (or 0) 152 u2 _contention_group; // index from @Contended group item (or 0) 153 154 public: 155 156 FieldInfo() : _index(0), 157 _name_index(0), 158 _signature_index(0), 159 _offset(0), 160 _access_flags(AccessFlags(0)), 161 _field_flags(FieldFlags(0)), 162 _layout_kind(LayoutKind::UNKNOWN), 163 _null_marker_offset(0), 164 _initializer_index(0), 165 _generic_signature_index(0), 166 _contention_group(0) { } 167 168 FieldInfo(AccessFlags access_flags, u2 name_index, u2 signature_index, u2 initval_index, FieldInfo::FieldFlags fflags) : 169 _index(0), 170 _name_index(name_index), 171 _signature_index(signature_index), 172 _offset(0), 173 _access_flags(access_flags), 174 _field_flags(fflags), 175 _layout_kind(LayoutKind::UNKNOWN), 176 _null_marker_offset(0), 177 _initializer_index(initval_index), 178 _generic_signature_index(0), 179 _contention_group(0) { 180 if (initval_index != 0) { 181 _field_flags.update_initialized(true); 182 } 183 } 184 185 u4 index() const { return _index; } 186 void set_index(u4 index) { _index = index; } 187 u2 name_index() const { return _name_index; } 188 void set_name_index(u2 index) { _name_index = index; } 189 u2 signature_index() const { return _signature_index; } 190 void set_signature_index(u2 index) { _signature_index = index; } 191 u4 offset() const { return _offset; } 192 void set_offset(u4 offset) { _offset = offset; } 193 AccessFlags access_flags() const { return _access_flags; } 194 FieldFlags field_flags() const { return _field_flags; } 195 FieldFlags* field_flags_addr() { return &_field_flags; } 196 LayoutKind layout_kind() const { return _layout_kind; } 197 void set_layout_kind(LayoutKind lk) { 198 assert(_field_flags.is_flat(), "Must be"); 199 _layout_kind = lk; 200 } 201 u4 null_marker_offset() const { return _null_marker_offset; } 202 void set_null_marker_offset(u4 offset) { 203 _field_flags.update_null_marker(true); 204 _null_marker_offset = offset; 205 } 206 u2 initializer_index() const { return _initializer_index; } 207 void set_initializer_index(u2 index) { _initializer_index = index; } 208 u2 generic_signature_index() const { return _generic_signature_index; } 209 void set_generic_signature_index(u2 index) { _generic_signature_index = index; } 210 u2 contention_group() const { return _contention_group; } 211 212 bool is_contended() const { 213 return _field_flags.is_contended(); 214 } 215 216 u2 contended_group() const { 217 assert(is_contended(), ""); 218 return _contention_group; 219 } 220 221 void set_contended_group(u2 group) { 222 _field_flags.update_contended(true); 223 _contention_group = group; 224 } 225 226 bool is_offset_set() const { 227 return _offset != 0; 228 } 229 230 inline Symbol* name(ConstantPool* cp) const; 231 232 inline Symbol* signature(ConstantPool* cp) const; 233 234 inline Symbol* lookup_symbol(int symbol_index) const; 235 236 void print(outputStream* os, ConstantPool* cp); 237 void static print_from_growable_array(outputStream* os, GrowableArray<FieldInfo>* array, ConstantPool* cp); 238 }; 239 240 class FieldInfoStream; 241 242 // Gadget for sizing and/or writing a stream of field records. 243 template<typename CON> 244 class Mapper { 245 CON* _consumer; // can be UNSIGNED5::Writer or UNSIGNED5::Sizer 246 int _next_index; 247 public: 248 Mapper(CON* consumer) : _consumer(consumer) { _next_index = 0; } 249 int next_index() const { return _next_index; } 250 void set_next_index(int next_index) { _next_index = next_index; } 251 CON* consumer() const { return _consumer; } 252 void map_field_info(const FieldInfo& fi); 253 }; 254 255 // Gadget for decoding and reading the stream of field records. 256 class FieldInfoReader { 257 UNSIGNED5::Reader<const u1*, int> _r; 258 int _next_index; 259 260 public: 261 FieldInfoReader(const Array<u1>* fi); 262 263 private: 264 inline uint32_t next_uint() { return _r.next_uint(); } 265 void skip(int n) { int s = _r.try_skip(n); assert(s == n,""); } 266 267 public: 268 void read_field_counts(int* java_fields, int* injected_fields); 269 int has_next() const { return _r.position() < _r.limit(); } 270 int position() const { return _r.position(); } 271 int next_index() const { return _next_index; } 272 void read_name_and_signature(u2* name_index, u2* signature_index); 273 void read_field_info(FieldInfo& fi); 274 275 int search_table_lookup(const Array<u1>* search_table, const Symbol* name, const Symbol* signature, ConstantPool* cp, int java_fields); 276 277 // skip a whole field record, both required and optional bits 278 FieldInfoReader& skip_field_info(); 279 280 // Skip to the nth field. If the reader is freshly initialized to 281 // the zero index, this will call skip_field_info() n times. 282 FieldInfoReader& skip_to_field_info(int n); 283 284 // for random access, if you know where to go up front: 285 FieldInfoReader& set_position_and_next_index(int position, int next_index); 286 }; 287 288 // The format of the stream, after decompression, is a series of 289 // integers organized like this: 290 // 291 // FieldInfoStream := j=num_java_fields k=num_injected_fields Field[j+k] End 292 // Field := name sig offset access flags Optionals(flags) 293 // Optionals(i) := initval?[i&is_init] // ConstantValue attr 294 // gsig?[i&is_generic] // signature attr 295 // group?[i&is_contended] // Contended anno (group) 296 // End = 0 297 // 298 class FieldInfoStream : AllStatic { 299 friend class fieldDescriptor; 300 friend class JavaFieldStream; 301 friend class FieldStreamBase; 302 friend class ClassFileParser; 303 friend class FieldInfoReader; 304 friend class FieldInfoComparator; 305 306 private: 307 static int compare_name_and_sig(const Symbol* n1, const Symbol* s1, const Symbol* n2, const Symbol* s2); 308 309 public: 310 static int num_java_fields(const Array<u1>* fis); 311 static int num_injected_java_fields(const Array<u1>* fis); 312 static int num_total_fields(const Array<u1>* fis); 313 314 static Array<u1>* create_FieldInfoStream(GrowableArray<FieldInfo>* fields, int java_fields, int injected_fields, 315 ClassLoaderData* loader_data, TRAPS); 316 static Array<u1>* create_search_table(ConstantPool* cp, const Array<u1>* fis, ClassLoaderData* loader_data, TRAPS); 317 static GrowableArray<FieldInfo>* create_FieldInfoArray(const Array<u1>* fis, int* java_fields_count, int* injected_fields_count); 318 static void print_from_fieldinfo_stream(Array<u1>* fis, outputStream* os, ConstantPool* cp); 319 320 DEBUG_ONLY(static void validate_search_table(ConstantPool* cp, const Array<u1>* fis, const Array<u1>* search_table);) 321 322 static void print_search_table(outputStream* st, ConstantPool* cp, const Array<u1>* fis, const Array<u1>* search_table); 323 }; 324 325 class FieldStatus { 326 enum FieldStatusBitPosition { 327 _fs_access_watched, // field access is watched by JVMTI 328 _fs_modification_watched, // field modification is watched by JVMTI 329 _fs_strict_static_unset, // JVM_ACC_STRICT_INIT static field has not yet been set 330 _fs_strict_static_unread, // SS field has not yet been read 331 _initialized_final_update // (static) final field updated outside (class) initializer 332 }; 333 334 // boilerplate: 335 u1 _flags; 336 static constexpr u1 flag_mask(FieldStatusBitPosition pos) { return checked_cast<u1>(1 << pos); } 337 bool test_flag(FieldStatusBitPosition pos) { return (_flags & flag_mask(pos)) != 0; } 338 // this performs an atomic update on a live status byte! 339 void update_flag(FieldStatusBitPosition pos, bool z); 340 // out-of-line functions do a CAS-loop 341 static void atomic_set_bits(u1& flags, u1 mask); 342 static void atomic_clear_bits(u1& flags, u1 mask); 343 344 public: 345 FieldStatus() { _flags = 0; } 346 FieldStatus(u1 flags) { _flags = flags; } 347 u1 as_uint() { return _flags; } 348 349 bool is_access_watched() { return test_flag(_fs_access_watched); } 350 bool is_modification_watched() { return test_flag(_fs_modification_watched); } 351 bool is_strict_static_unset() { return test_flag(_fs_strict_static_unset); } 352 bool is_strict_static_unread() { return test_flag(_fs_strict_static_unread); } 353 bool is_initialized_final_update() { return test_flag(_initialized_final_update); } 354 355 void update_access_watched(bool z); 356 void update_modification_watched(bool z); 357 void update_strict_static_unset(bool z); 358 void update_strict_static_unread(bool z); 359 void update_initialized_final_update(bool z); 360 }; 361 362 #endif // SHARE_OOPS_FIELDINFO_HPP --- EOF ---