1 /*
2 * Copyright (c) 2003, 2023, 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 *
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 private:
45 int32_t _offset;
46
47 // See comment in StackMapTable about _frame_count about why these
48 // fields are int32_t instead of u2.
49 int32_t _locals_size; // number of valid type elements in _locals
50 int32_t _stack_size; // number of valid type elements in _stack
51
52 int32_t _stack_mark; // Records the size of the stack prior to an
53 // instruction modification, to allow rewinding
54 // when/if an error occurs.
55
56 u2 _max_locals;
57 u2 _max_stack;
58
59 u1 _flags;
60 VerificationType* _locals; // local variable type array
61 VerificationType* _stack; // operand stack type array
62
63 ClassVerifier* _verifier; // the verifier verifying this method
64
65 StackMapFrame(const StackMapFrame& cp) :
66 ResourceObj(cp),
67 _offset(cp._offset), _locals_size(cp._locals_size),
68 _stack_size(cp._stack_size), _stack_mark(cp._stack_mark),
69 _max_locals(cp._max_locals), _max_stack(cp._max_stack),
70 _flags(cp._flags) {
71 _locals = NEW_RESOURCE_ARRAY(VerificationType, _max_locals);
72 for (int i = 0; i < _max_locals; ++i) {
73 if (i < _locals_size) {
74 _locals[i] = cp._locals[i];
75 } else {
76 _locals[i] = VerificationType::bogus_type();
77 }
78 }
79 int ss = MAX2(_stack_size, _stack_mark);
80 _stack = NEW_RESOURCE_ARRAY(VerificationType, _max_stack);
81 for (int i = 0; i < _max_stack; ++i) {
82 if (i < ss) {
83 _stack[i] = cp._stack[i];
84 } else {
85 _stack[i] = VerificationType::bogus_type();
86 }
87 }
88 _verifier = nullptr;
89 }
90
91 public:
92 // constructors
93
94 // This constructor is used by the type checker to allocate frames
95 // in type state, which have _max_locals and _max_stack array elements
96 // in _locals and _stack.
97 StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier);
98
99 // This constructor is used to initialize stackmap frames in stackmap table,
100 // which have _locals_size and _stack_size array elements in _locals and _stack.
101 StackMapFrame(int32_t offset,
102 u1 flags,
103 int32_t locals_size,
104 int32_t stack_size,
105 u2 max_locals,
106 u2 max_stack,
107 VerificationType* locals,
108 VerificationType* stack,
109 ClassVerifier* v) : _offset(offset),
110 _locals_size(locals_size),
111 _stack_size(stack_size),
112 _stack_mark(-1),
113 _max_locals(max_locals),
114 _max_stack(max_stack), _flags(flags),
115 _locals(locals), _stack(stack),
116 _verifier(v) { }
117
118 static StackMapFrame* copy(StackMapFrame* smf) {
119 return new StackMapFrame(*smf);
120 }
121
122 inline void set_offset(int32_t offset) { _offset = offset; }
123 inline void set_verifier(ClassVerifier* v) { _verifier = v; }
124 inline void set_flags(u1 flags) { _flags = flags; }
125 inline void set_locals_size(int32_t locals_size) { _locals_size = locals_size; }
126 inline void set_stack_size(int32_t stack_size) { _stack_size = _stack_mark = stack_size; }
127 inline void clear_stack() { _stack_size = 0; }
128 inline int32_t offset() const { return _offset; }
129 inline ClassVerifier* verifier() const { return _verifier; }
130 inline u1 flags() const { return _flags; }
131 inline int32_t locals_size() const { return _locals_size; }
132 inline VerificationType* locals() const { return _locals; }
133 inline int32_t stack_size() const { return _stack_size; }
134 inline VerificationType* stack() const { return _stack; }
135 inline u2 max_locals() const { return _max_locals; }
136 inline u2 max_stack() const { return _max_stack; }
137 inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; }
138
139 // Set locals and stack types to bogus
140 inline void reset() {
141 int32_t i;
142 for (i = 0; i < _max_locals; i++) {
143 _locals[i] = VerificationType::bogus_type();
144 }
145 for (i = 0; i < _max_stack; i++) {
146 _stack[i] = VerificationType::bogus_type();
147 }
148 }
149
150 // Return a StackMapFrame with the same local variable array and empty stack.
151 // Stack array is allocate with unused one element.
152 StackMapFrame* frame_in_exception_handler(u1 flags);
153
154 // Set local variable type array based on m's signature.
155 VerificationType set_locals_from_arg(
156 const methodHandle& m, VerificationType thisKlass);
157
158 // Search local variable type array and stack type array.
|
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 *
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.
|