1 /* 2 * Copyright (c) 2003, 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_CLASSFILE_STACKMAPFRAME_HPP 26 #define SHARE_CLASSFILE_STACKMAPFRAME_HPP 27 28 #include "classfile/verificationType.hpp" 29 #include "classfile/verifier.hpp" 30 #include "oops/method.hpp" 31 #include "runtime/handles.hpp" 32 #include "runtime/signature.hpp" 33 #include "utilities/exceptions.hpp" 34 35 // A StackMapFrame represents one frame in the stack map attribute. 36 37 class TypeContext; 38 39 enum { 40 FLAG_THIS_UNINIT = 0x01 41 }; 42 43 class StackMapFrame : public ResourceObj { 44 public: 45 static unsigned int nameandsig_hash(NameAndSig const& field) { 46 Symbol* name = field._name; 47 return (unsigned int) name->identity_hash(); 48 } 49 50 static inline bool nameandsig_equals(NameAndSig const& f1, NameAndSig const& f2) { 51 return f1._name == f2._name && 52 f1._signature == f2._signature; 53 } 54 55 // Maps a strict field's name and signature to whether or not it was initialized 56 typedef ResourceHashtable<NameAndSig, bool, 17, 57 AnyObj::RESOURCE_AREA, mtInternal, 58 nameandsig_hash, nameandsig_equals> AssertUnsetFieldTable; 59 private: 60 int32_t _offset; 61 62 // See comment in StackMapTable about _frame_count about why these 63 // fields are int32_t instead of u2. 64 int32_t _locals_size; // number of valid type elements in _locals 65 int32_t _stack_size; // number of valid type elements in _stack 66 67 int32_t _stack_mark; // Records the size of the stack prior to an 68 // instruction modification, to allow rewinding 69 // when/if an error occurs. 70 71 u2 _max_locals; 72 u2 _max_stack; 73 74 u1 _flags; 75 VerificationType* _locals; // local variable type array 76 VerificationType* _stack; // operand stack type array 77 78 AssertUnsetFieldTable* _assert_unset_fields; // List of unsatisfied strict fields in the basic block 79 80 ClassVerifier* _verifier; // the verifier verifying this method 81 82 StackMapFrame(const StackMapFrame& cp) : 83 ResourceObj(cp), 84 _offset(cp._offset), _locals_size(cp._locals_size), 85 _stack_size(cp._stack_size), _stack_mark(cp._stack_mark), 86 _max_locals(cp._max_locals), _max_stack(cp._max_stack), 87 _flags(cp._flags) { 88 _locals = NEW_RESOURCE_ARRAY(VerificationType, _max_locals); 89 for (int i = 0; i < _max_locals; ++i) { 90 if (i < _locals_size) { 91 _locals[i] = cp._locals[i]; 92 } else { 93 _locals[i] = VerificationType::bogus_type(); 94 } 95 } 96 int ss = MAX2(_stack_size, _stack_mark); 97 _stack = NEW_RESOURCE_ARRAY(VerificationType, _max_stack); 98 for (int i = 0; i < _max_stack; ++i) { 99 if (i < ss) { 100 _stack[i] = cp._stack[i]; 101 } else { 102 _stack[i] = VerificationType::bogus_type(); 103 } 104 } 105 _assert_unset_fields = cp._assert_unset_fields; 106 _verifier = nullptr; 107 } 108 109 public: 110 // constructors 111 112 // This constructor is used by the type checker to allocate frames 113 // in type state, which have _max_locals and _max_stack array elements 114 // in _locals and _stack. 115 StackMapFrame(u2 max_locals, u2 max_stack, AssertUnsetFieldTable* initial_strict_fields, ClassVerifier* verifier); 116 117 // This constructor is used to initialize stackmap frames in stackmap table, 118 // which have _locals_size and _stack_size array elements in _locals and _stack. 119 StackMapFrame(int32_t offset, 120 u1 flags, 121 int32_t locals_size, 122 int32_t stack_size, 123 u2 max_locals, 124 u2 max_stack, 125 VerificationType* locals, 126 VerificationType* stack, 127 AssertUnsetFieldTable* assert_unset_fields, 128 ClassVerifier* v) : _offset(offset), 129 _locals_size(locals_size), 130 _stack_size(stack_size), 131 _stack_mark(-1), 132 _max_locals(max_locals), 133 _max_stack(max_stack), _flags(flags), 134 _locals(locals), _stack(stack), 135 _assert_unset_fields(assert_unset_fields), 136 _verifier(v) { } 137 138 static StackMapFrame* copy(StackMapFrame* smf) { 139 return new StackMapFrame(*smf); 140 } 141 142 inline void set_offset(int32_t offset) { _offset = offset; } 143 inline void set_verifier(ClassVerifier* v) { _verifier = v; } 144 inline void set_flags(u1 flags) { _flags = flags; } 145 inline void set_locals_size(int32_t locals_size) { _locals_size = locals_size; } 146 inline void set_stack_size(int32_t stack_size) { _stack_size = _stack_mark = stack_size; } 147 inline void clear_stack() { _stack_size = 0; } 148 inline int32_t offset() const { return _offset; } 149 inline ClassVerifier* verifier() const { return _verifier; } 150 inline u1 flags() const { return _flags; } 151 inline int32_t locals_size() const { return _locals_size; } 152 inline VerificationType* locals() const { return _locals; } 153 inline int32_t stack_size() const { return _stack_size; } 154 inline VerificationType* stack() const { return _stack; } 155 inline u2 max_locals() const { return _max_locals; } 156 inline u2 max_stack() const { return _max_stack; } 157 inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; } 158 159 AssertUnsetFieldTable* assert_unset_fields() const { 160 return _assert_unset_fields; 161 } 162 163 void set_assert_unset_fields(AssertUnsetFieldTable* table) { 164 _assert_unset_fields = table; 165 } 166 167 // Called when verifying putfields to mark strict instance fields as satisfied 168 bool satisfy_unset_field(Symbol* name, Symbol* signature) { 169 NameAndSig dummy_field(name, signature); 170 171 if (_assert_unset_fields->contains(dummy_field)) { 172 _assert_unset_fields->put(dummy_field, true); 173 return true; 174 } 175 return false; 176 } 177 178 // Verify that all strict fields have been initialized 179 // Strict fields must be initialized before the super constructor is called 180 bool verify_unset_fields_satisfied() { 181 bool all_satisfied = true; 182 auto check_satisfied = [&] (const NameAndSig& key, const bool& value) { 183 all_satisfied &= value; 184 }; 185 _assert_unset_fields->iterate_all(check_satisfied); 186 return all_satisfied; 187 } 188 189 // Merge incoming unset strict fields from StackMapTable with 190 // initial strict instance fields 191 AssertUnsetFieldTable* merge_unset_fields(AssertUnsetFieldTable* new_fields) { 192 auto merge_satisfied = [&] (const NameAndSig& key, const bool& value) { 193 if (!new_fields->contains(key)) { 194 new_fields->put(key, true); 195 } 196 }; 197 _assert_unset_fields->iterate_all(merge_satisfied); 198 return new_fields; 199 } 200 201 // Verify that strict fields are compatible between the current frame and the successor 202 // Called during merging of frames 203 bool verify_unset_fields_compatibility(AssertUnsetFieldTable* target_table) const { 204 bool compatible = true; 205 auto is_unset = [&] (const NameAndSig& key, const bool& value) { 206 // Successor must have same debts as current frame 207 if (!value) { 208 if (*target_table->get(key) == true) { 209 compatible = false; 210 } 211 } 212 }; 213 _assert_unset_fields->iterate_all(is_unset); 214 return compatible; 215 } 216 217 void unsatisfied_strict_fields_error(InstanceKlass* ik, int bci); 218 static void print_strict_fields(AssertUnsetFieldTable* table); 219 220 // Set locals and stack types to bogus 221 inline void reset() { 222 int32_t i; 223 for (i = 0; i < _max_locals; i++) { 224 _locals[i] = VerificationType::bogus_type(); 225 } 226 for (i = 0; i < _max_stack; i++) { 227 _stack[i] = VerificationType::bogus_type(); 228 } 229 } 230 231 // Return a StackMapFrame with the same local variable array and empty stack. 232 // Stack array is allocate with unused one element. 233 StackMapFrame* frame_in_exception_handler(u1 flags); 234 235 // Set local variable type array based on m's signature. 236 VerificationType set_locals_from_arg( 237 const methodHandle& m, VerificationType thisKlass); 238 239 // Search local variable type array and stack type array. 240 // Set every element with type of old_object to new_object. 241 void initialize_object( 242 VerificationType old_object, VerificationType new_object); 243 244 // Copy local variable type array in src into this local variable type array. 245 void copy_locals(const StackMapFrame* src); 246 247 // Copy stack type array in src into this stack type array. 248 void copy_stack(const StackMapFrame* src); 249 250 // Return true if this stack map frame is assignable to target. 251 bool is_assignable_to( 252 const StackMapFrame* target, ErrorContext* ctx, TRAPS) const; 253 254 inline void set_mark() { 255 #ifdef ASSERT 256 // Put bogus type to indicate it's no longer valid. 257 if (_stack_mark != -1) { 258 for (int i = _stack_mark - 1; i >= _stack_size; --i) { 259 _stack[i] = VerificationType::bogus_type(); 260 } 261 } 262 #endif // def ASSERT 263 _stack_mark = _stack_size; 264 } 265 266 // Used when an error occurs and we want to reset the stack to the state 267 // it was before operands were popped off. 268 void restore() { 269 if (_stack_mark != -1) { 270 _stack_size = _stack_mark; 271 } 272 } 273 274 // Push type into stack type array. 275 inline void push_stack(VerificationType type, TRAPS) { 276 assert(!type.is_check(), "Must be a real type"); 277 if (_stack_size >= _max_stack) { 278 verifier()->verify_error( 279 ErrorContext::stack_overflow(_offset, this), 280 "Operand stack overflow"); 281 return; 282 } 283 _stack[_stack_size++] = type; 284 } 285 286 inline void push_stack_2( 287 VerificationType type1, VerificationType type2, TRAPS) { 288 assert(type1.is_long() || type1.is_double(), "must be long/double"); 289 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); 290 if (_stack_size >= _max_stack - 1) { 291 verifier()->verify_error( 292 ErrorContext::stack_overflow(_offset, this), 293 "Operand stack overflow"); 294 return; 295 } 296 _stack[_stack_size++] = type1; 297 _stack[_stack_size++] = type2; 298 } 299 300 // Pop and return the top type on stack without verifying. 301 inline VerificationType pop_stack(TRAPS) { 302 if (_stack_size <= 0) { 303 verifier()->verify_error( 304 ErrorContext::stack_underflow(_offset, this), 305 "Operand stack underflow"); 306 return VerificationType::bogus_type(); 307 } 308 VerificationType top = _stack[--_stack_size]; 309 return top; 310 } 311 312 // Pop and return the top type on stack type array after verifying it 313 // is assignable to type. 314 inline VerificationType pop_stack(VerificationType type, TRAPS) { 315 if (_stack_size != 0) { 316 VerificationType top = _stack[_stack_size - 1]; 317 bool subtype = type.is_assignable_from( 318 top, verifier(), false, CHECK_(VerificationType::bogus_type())); 319 if (subtype) { 320 --_stack_size; 321 return top; 322 } 323 } 324 return pop_stack_ex(type, THREAD); 325 } 326 327 inline void pop_stack_2( 328 VerificationType type1, VerificationType type2, TRAPS) { 329 assert(type1.is_long2() || type1.is_double2(), "must be long/double"); 330 assert(type2.is_long() || type2.is_double(), "must be long/double_2"); 331 if (_stack_size >= 2) { 332 VerificationType top1 = _stack[_stack_size - 1]; 333 bool subtype1 = type1.is_assignable_from(top1, verifier(), false, CHECK); 334 VerificationType top2 = _stack[_stack_size - 2]; 335 bool subtype2 = type2.is_assignable_from(top2, verifier(), false, CHECK); 336 if (subtype1 && subtype2) { 337 _stack_size -= 2; 338 return; 339 } 340 } 341 pop_stack_ex(type1, THREAD); 342 pop_stack_ex(type2, THREAD); 343 } 344 345 VerificationType local_at(int index) { 346 return _locals[index]; 347 } 348 349 VerificationType stack_at(int index) { 350 return _stack[index]; 351 } 352 353 // Uncommon case that throws exceptions. 354 VerificationType pop_stack_ex(VerificationType type, TRAPS); 355 356 // Return the type at index in local variable array after verifying 357 // it is assignable to type. 358 VerificationType get_local(int32_t index, VerificationType type, TRAPS); 359 // For long/double. 360 void get_local_2( 361 int32_t index, VerificationType type1, VerificationType type2, TRAPS); 362 363 // Set element at index in local variable array to type. 364 void set_local(int32_t index, VerificationType type, TRAPS); 365 // For long/double. 366 void set_local_2( 367 int32_t index, VerificationType type1, VerificationType type2, TRAPS); 368 369 // Private auxiliary method used only in is_assignable_to(StackMapFrame). 370 // Returns true if src is assignable to target. 371 int is_assignable_to( 372 VerificationType* src, VerificationType* target, int32_t len, TRAPS) const; 373 374 TypeOrigin stack_top_ctx(); 375 376 void print_on(outputStream* str) const; 377 }; 378 379 #endif // SHARE_CLASSFILE_STACKMAPFRAME_HPP