1 /*
2 * Copyright (c) 2003, 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_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 HashTable<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& satisfied) {
206 // Successor must have same (or more) unsatisfied debts as current frame.
207 if (!satisfied) {
208 bool* target_satisfied = target_table->get(key);
209 guarantee(target_satisfied != nullptr, "Must be present");
210 if (*target_satisfied == true) {
211 compatible = false;
212 }
213 }
214 };
215 _assert_unset_fields->iterate_all(is_unset);
216 return compatible;
217 }
218
219 void unsatisfied_strict_fields_error(InstanceKlass* ik, int bci);
220 static void print_strict_fields(AssertUnsetFieldTable* table);
221
222 // Set locals and stack types to bogus
223 inline void reset() {
224 int32_t i;
225 for (i = 0; i < _max_locals; i++) {
226 _locals[i] = VerificationType::bogus_type();
227 }
228 for (i = 0; i < _max_stack; i++) {
229 _stack[i] = VerificationType::bogus_type();
230 }
231 }
232
233 // Return a StackMapFrame with the same local variable array and empty stack.
234 // Stack array is allocate with unused one element.
235 StackMapFrame* frame_in_exception_handler(u1 flags);
236
237 // Set local variable type array based on m's signature.
238 VerificationType set_locals_from_arg(
239 const methodHandle& m, VerificationType thisKlass);
240
241 // Search local variable type array and stack type array.
242 // Set every element with type of old_object to new_object.
243 void initialize_object(
244 VerificationType old_object, VerificationType new_object);
245
246 // Copy local variable type array in src into this local variable type array.
247 void copy_locals(const StackMapFrame* src);
248
249 // Copy stack type array in src into this stack type array.
250 void copy_stack(const StackMapFrame* src);
251
252 // Return true if this stack map frame is assignable to target.
253 bool is_assignable_to(
254 const StackMapFrame* target, ErrorContext* ctx, TRAPS) const;
255
256 inline void set_mark() {
257 #ifdef ASSERT
258 // Put bogus type to indicate it's no longer valid.
259 if (_stack_mark != -1) {
260 for (int i = _stack_mark - 1; i >= _stack_size; --i) {
261 _stack[i] = VerificationType::bogus_type();
262 }
263 }
264 #endif // def ASSERT
265 _stack_mark = _stack_size;
266 }
267
268 // Used when an error occurs and we want to reset the stack to the state
269 // it was before operands were popped off.
270 void restore() {
271 if (_stack_mark != -1) {
272 _stack_size = _stack_mark;
273 }
274 }
275
276 // Push type into stack type array.
277 inline void push_stack(VerificationType type, TRAPS) {
278 assert(!type.is_check(), "Must be a real type");
279 if (_stack_size >= _max_stack) {
280 verifier()->verify_error(
281 ErrorContext::stack_overflow(_offset, this),
282 "Operand stack overflow");
283 return;
284 }
285 _stack[_stack_size++] = type;
286 }
287
288 inline void push_stack_2(
289 VerificationType type1, VerificationType type2, TRAPS) {
290 assert(type1.is_long() || type1.is_double(), "must be long/double");
291 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
292 if (_stack_size >= _max_stack - 1) {
293 verifier()->verify_error(
294 ErrorContext::stack_overflow(_offset, this),
295 "Operand stack overflow");
296 return;
297 }
298 _stack[_stack_size++] = type1;
299 _stack[_stack_size++] = type2;
300 }
301
302 // Pop and return the top type on stack without verifying.
303 inline VerificationType pop_stack(TRAPS) {
304 if (_stack_size <= 0) {
305 verifier()->verify_error(
306 ErrorContext::stack_underflow(_offset, this),
307 "Operand stack underflow");
308 return VerificationType::bogus_type();
309 }
310 VerificationType top = _stack[--_stack_size];
311 return top;
312 }
313
314 // Pop and return the top type on stack type array after verifying it
315 // is assignable to type.
316 inline VerificationType pop_stack(VerificationType type, TRAPS) {
317 if (_stack_size != 0) {
318 VerificationType top = _stack[_stack_size - 1];
319 bool subtype = type.is_assignable_from(
320 top, verifier(), false, CHECK_(VerificationType::bogus_type()));
321 if (subtype) {
322 --_stack_size;
323 return top;
324 }
325 }
326 return pop_stack_ex(type, THREAD);
327 }
328
329 inline void pop_stack_2(
330 VerificationType type1, VerificationType type2, TRAPS) {
331 assert(type1.is_long2() || type1.is_double2(), "must be long/double");
332 assert(type2.is_long() || type2.is_double(), "must be long/double_2");
333 if (_stack_size >= 2) {
334 VerificationType top1 = _stack[_stack_size - 1];
335 bool subtype1 = type1.is_assignable_from(top1, verifier(), false, CHECK);
336 VerificationType top2 = _stack[_stack_size - 2];
337 bool subtype2 = type2.is_assignable_from(top2, verifier(), false, CHECK);
338 if (subtype1 && subtype2) {
339 _stack_size -= 2;
340 return;
341 }
342 }
343 pop_stack_ex(type1, THREAD);
344 pop_stack_ex(type2, THREAD);
345 }
346
347 VerificationType local_at(int index) {
348 return _locals[index];
349 }
350
351 VerificationType stack_at(int index) {
352 return _stack[index];
353 }
354
355 // Uncommon case that throws exceptions.
356 VerificationType pop_stack_ex(VerificationType type, TRAPS);
357
358 // Return the type at index in local variable array after verifying
359 // it is assignable to type.
360 VerificationType get_local(int32_t index, VerificationType type, TRAPS);
361 // For long/double.
362 void get_local_2(
363 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
364
365 // Set element at index in local variable array to type.
366 void set_local(int32_t index, VerificationType type, TRAPS);
367 // For long/double.
368 void set_local_2(
369 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
370
371 // Private auxiliary method used only in is_assignable_to(StackMapFrame).
372 // Returns true if src is assignable to target.
373 int is_assignable_to(
374 VerificationType* src, VerificationType* target, int32_t len, TRAPS) const;
375
376 TypeOrigin stack_top_ctx();
377
378 void print_on(outputStream* str) const;
379 };
380
381 #endif // SHARE_CLASSFILE_STACKMAPFRAME_HPP