7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.classfile.impl.verifier;
26
27 import java.util.Arrays;
28
29 /// From `stackMapFrame.cpp`.
30 class VerificationFrame {
31
32 public static final int FLAG_THIS_UNINIT = 0x01;
33
34 private int _offset;
35 private int _locals_size, _stack_size;
36 private int _stack_mark;
37 private final int _max_locals, _max_stack;
38 private int _flags;
39 private final VerificationType[] _locals, _stack;
40 private final VerifierImpl _verifier;
41
42 public VerificationFrame(int offset, int flags, int locals_size, int stack_size, int max_locals, int max_stack, VerificationType[] locals, VerificationType[] stack, VerifierImpl v) {
43 this._offset = offset;
44 this._locals_size = locals_size;
45 this._stack_size = stack_size;
46 this._stack_mark = -1;
47 this._max_locals = max_locals;
48 this._max_stack = max_stack;
49 this._flags = flags;
50 this._locals = locals;
51 this._stack = stack;
52 this._verifier = v;
53 }
54
55 @Override
56 public String toString() {
57 return "frame @" + _offset + " with locals " + (_locals == null ? "[]" : Arrays.asList(_locals)) + " and stack " + (_stack == null ? "[]" : Arrays.asList(_stack));
58 }
59
60 void set_offset(int offset) {
61 this._offset = offset;
62 }
63
64 void set_flags(int flags) {
65 _flags = flags;
66 }
67
68 void set_locals_size(int locals_size) {
69 _locals_size = locals_size;
70 }
71
92 VerificationType[] locals() {
93 return _locals;
94 }
95
96 int stack_size() {
97 return _stack_size;
98 }
99
100 VerificationType[] stack() {
101 return _stack;
102 }
103
104 int max_locals() {
105 return _max_locals;
106 }
107
108 boolean flag_this_uninit() {
109 return (_flags & FLAG_THIS_UNINIT) == FLAG_THIS_UNINIT;
110 }
111
112 void reset() {
113 for (int i = 0; i < _max_locals; i++) {
114 _locals[i] = VerificationType.bogus_type;
115 }
116 for (int i = 0; i < _max_stack; i++) {
117 _stack[i] = VerificationType.bogus_type;
118 }
119 }
120
121 void set_mark() {
122 if (_stack_mark != -1) {
123 for (int i = _stack_mark - 1; i >= _stack_size; --i) {
124 _stack[i] = VerificationType.bogus_type;
125 }
126 _stack_mark = _stack_size;
127 }
128 }
129
130 void push_stack(VerificationType type) {
131 if (type.is_check()) _verifier.verifyError("Must be a real type");
164 return pop_stack_ex(type);
165 }
166
167 void pop_stack_2(VerificationType type1, VerificationType type2) {
168 if (!(type1.is_long2() || type1.is_double2())) _verifier.verifyError("must be long/double");
169 if (!(type2.is_long() || type2.is_double())) _verifier.verifyError("must be long/double_2");
170 if (_stack_size >= 2) {
171 VerificationType top1 = _stack[_stack_size - 1];
172 boolean subtype1 = type1.is_assignable_from(top1, verifier());
173 VerificationType top2 = _stack[_stack_size - 2];
174 boolean subtype2 = type2.is_assignable_from(top2, verifier());
175 if (subtype1 && subtype2) {
176 _stack_size -= 2;
177 return;
178 }
179 }
180 pop_stack_ex(type1);
181 pop_stack_ex(type2);
182 }
183
184 VerificationFrame(int max_locals, int max_stack, VerifierImpl verifier) {
185 _offset = 0;
186 _locals_size = 0;
187 _stack_size = 0;
188 _stack_mark = 0;
189 _max_locals = max_locals;
190 _max_stack = max_stack;
191 _flags = 0;
192 _verifier = verifier;
193 _locals = new VerificationType[max_locals];
194 _stack = new VerificationType[max_stack];
195 for (int i = 0; i < max_locals; i++) {
196 _locals[i] = VerificationType.bogus_type;
197 }
198 for (int i = 0; i < max_stack; i++) {
199 _stack[i] = VerificationType.bogus_type;
200 }
201 }
202
203 VerificationFrame frame_in_exception_handler(int flags) {
204 return new VerificationFrame(_offset, flags, _locals_size, 0,
205 _max_locals, _max_stack, _locals, new VerificationType[1],
206 _verifier);
207 }
208
209 void initialize_object(VerificationType old_object, VerificationType new_object) {
210 int i;
211 for (i = 0; i < _max_locals; i++) {
212 if (_locals[i].equals(old_object)) {
213 _locals[i] = new_object;
214 }
215 }
216 for (i = 0; i < _stack_size; i++) {
217 if (_stack[i].equals(old_object)) {
218 _stack[i] = new_object;
219 }
220 }
221 if (old_object.is_uninitialized_this(_verifier)) {
222 _flags = 0;
223 }
224 }
225
226 VerificationType set_locals_from_arg(VerificationWrapper.MethodWrapper m, VerificationType thisKlass) {
282 return i;
283 }
284
285 boolean is_assignable_to(VerificationFrame target) {
286 if (_max_locals != target.max_locals()) {
287 _verifier.verifyError("Locals size mismatch", this, target);
288 }
289 if (_stack_size != target.stack_size()) {
290 _verifier.verifyError("Stack size mismatch", this, target);
291 }
292 int mismatch_loc;
293 mismatch_loc = is_assignable_to(_locals, target.locals(), target.locals_size());
294 if (mismatch_loc != target.locals_size()) {
295 _verifier.verifyError("Bad type", this, target);
296 }
297 mismatch_loc = is_assignable_to(_stack, target.stack(), _stack_size);
298 if (mismatch_loc != _stack_size) {
299 _verifier.verifyError("Bad type", this, target);
300 }
301
302 if ((_flags | target.flags()) == target.flags()) {
303 return true;
304 } else {
305 _verifier.verifyError("Bad flags", this, target);
306 }
307 return false;
308 }
309
310 VerificationType pop_stack_ex(VerificationType type) {
311 if (_stack_size <= 0) {
312 _verifier.verifyError("Operand stack underflow");
313 }
314 VerificationType top = _stack[--_stack_size];
315 boolean subtype = type.is_assignable_from(top, verifier());
316 if (!subtype) {
317 _verifier.verifyError("Bad type on operand stack");
318 }
319 return top;
320 }
321
|
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.internal.classfile.impl.verifier;
26
27 import java.lang.classfile.constantpool.NameAndTypeEntry;
28 import java.lang.classfile.constantpool.Utf8Entry;
29 import java.util.Arrays;
30 import java.util.Set;
31
32 import jdk.internal.classfile.impl.TemporaryConstantPool;
33
34 /// From `stackMapFrame.cpp`.
35 class VerificationFrame {
36
37 public static final int FLAG_THIS_UNINIT = 0x01;
38
39 private int _offset;
40 private int _locals_size, _stack_size;
41 private int _stack_mark;
42 private final int _max_locals, _max_stack;
43 private int _flags;
44 private final VerificationType[] _locals, _stack;
45 private Set<NameAndTypeEntry> _assert_unset_fields;
46 private final VerifierImpl _verifier;
47
48 public VerificationFrame(int offset, int flags, int locals_size, int stack_size, int max_locals, int max_stack,
49 VerificationType[] locals, VerificationType[] stack, Set<NameAndTypeEntry> assert_unset_fields, VerifierImpl v) {
50 this._offset = offset;
51 this._locals_size = locals_size;
52 this._stack_size = stack_size;
53 this._stack_mark = -1;
54 this._max_locals = max_locals;
55 this._max_stack = max_stack;
56 this._flags = flags;
57 this._locals = locals;
58 this._stack = stack;
59 this._assert_unset_fields = assert_unset_fields;
60 this._verifier = v;
61 }
62
63 @Override
64 public String toString() {
65 return "frame @" + _offset + " with locals " + (_locals == null ? "[]" : Arrays.asList(_locals)) + " and stack " + (_stack == null ? "[]" : Arrays.asList(_stack));
66 }
67
68 void set_offset(int offset) {
69 this._offset = offset;
70 }
71
72 void set_flags(int flags) {
73 _flags = flags;
74 }
75
76 void set_locals_size(int locals_size) {
77 _locals_size = locals_size;
78 }
79
100 VerificationType[] locals() {
101 return _locals;
102 }
103
104 int stack_size() {
105 return _stack_size;
106 }
107
108 VerificationType[] stack() {
109 return _stack;
110 }
111
112 int max_locals() {
113 return _max_locals;
114 }
115
116 boolean flag_this_uninit() {
117 return (_flags & FLAG_THIS_UNINIT) == FLAG_THIS_UNINIT;
118 }
119
120 Set<NameAndTypeEntry> assert_unset_fields() {
121 return _assert_unset_fields;
122 }
123
124 void set_assert_unset_fields(Set<NameAndTypeEntry> table) {
125 _assert_unset_fields = table;
126 }
127
128 // Called when verifying putfields to mark strict instance fields as satisfied
129 boolean satisfy_unset_field(Utf8Entry name, Utf8Entry signature) {
130 var nat = TemporaryConstantPool.INSTANCE.nameAndTypeEntry(name, signature);
131 return _assert_unset_fields.remove(nat);
132 }
133
134 // Verify that all strict fields have been initialized
135 // Strict fields must be initialized before the super constructor is called
136 boolean verify_unset_fields_satisfied() {
137 return _assert_unset_fields.isEmpty();
138 }
139
140 // Merge incoming unset strict fields from StackMapTable with
141 // initial strict instance fields
142 Set<NameAndTypeEntry> merge_unset_fields(Set<NameAndTypeEntry> new_fields) {
143 // ClassFile API: We track all strict fields in another structure, noop here
144 return new_fields;
145 }
146
147 boolean verify_unset_fields_compatibility(Set<NameAndTypeEntry> target_table) {
148 for (var e : _assert_unset_fields) {
149 if (!target_table.contains(e))
150 return false;
151 }
152 return true;
153 }
154
155 void unsatisfied_strict_fields_error(VerificationWrapper klass, int bci) {
156 _verifier.verifyError("All strict final fields must be initialized before super(): %d field(s), %s"
157 .formatted(_assert_unset_fields.size(), _assert_unset_fields));
158 }
159
160 void print_strict_fields(Set<NameAndTypeEntry> table) {
161 // ignore, we don't do stdout/err
162 }
163
164 void reset() {
165 for (int i = 0; i < _max_locals; i++) {
166 _locals[i] = VerificationType.bogus_type;
167 }
168 for (int i = 0; i < _max_stack; i++) {
169 _stack[i] = VerificationType.bogus_type;
170 }
171 }
172
173 void set_mark() {
174 if (_stack_mark != -1) {
175 for (int i = _stack_mark - 1; i >= _stack_size; --i) {
176 _stack[i] = VerificationType.bogus_type;
177 }
178 _stack_mark = _stack_size;
179 }
180 }
181
182 void push_stack(VerificationType type) {
183 if (type.is_check()) _verifier.verifyError("Must be a real type");
216 return pop_stack_ex(type);
217 }
218
219 void pop_stack_2(VerificationType type1, VerificationType type2) {
220 if (!(type1.is_long2() || type1.is_double2())) _verifier.verifyError("must be long/double");
221 if (!(type2.is_long() || type2.is_double())) _verifier.verifyError("must be long/double_2");
222 if (_stack_size >= 2) {
223 VerificationType top1 = _stack[_stack_size - 1];
224 boolean subtype1 = type1.is_assignable_from(top1, verifier());
225 VerificationType top2 = _stack[_stack_size - 2];
226 boolean subtype2 = type2.is_assignable_from(top2, verifier());
227 if (subtype1 && subtype2) {
228 _stack_size -= 2;
229 return;
230 }
231 }
232 pop_stack_ex(type1);
233 pop_stack_ex(type2);
234 }
235
236 VerificationFrame(int max_locals, int max_stack, Set<NameAndTypeEntry> initial_strict_fields, VerifierImpl verifier) {
237 _offset = 0;
238 _locals_size = 0;
239 _stack_size = 0;
240 _stack_mark = 0;
241 _max_locals = max_locals;
242 _max_stack = max_stack;
243 _flags = 0;
244 _verifier = verifier;
245 _locals = new VerificationType[max_locals];
246 _stack = new VerificationType[max_stack];
247 for (int i = 0; i < max_locals; i++) {
248 _locals[i] = VerificationType.bogus_type;
249 }
250 for (int i = 0; i < max_stack; i++) {
251 _stack[i] = VerificationType.bogus_type;
252 }
253 _assert_unset_fields = initial_strict_fields;
254 }
255
256 VerificationFrame frame_in_exception_handler(int flags) {
257 return new VerificationFrame(_offset, flags, _locals_size, 0,
258 _max_locals, _max_stack, _locals, new VerificationType[1],
259 _assert_unset_fields, _verifier);
260 }
261
262 void initialize_object(VerificationType old_object, VerificationType new_object) {
263 int i;
264 for (i = 0; i < _max_locals; i++) {
265 if (_locals[i].equals(old_object)) {
266 _locals[i] = new_object;
267 }
268 }
269 for (i = 0; i < _stack_size; i++) {
270 if (_stack[i].equals(old_object)) {
271 _stack[i] = new_object;
272 }
273 }
274 if (old_object.is_uninitialized_this(_verifier)) {
275 _flags = 0;
276 }
277 }
278
279 VerificationType set_locals_from_arg(VerificationWrapper.MethodWrapper m, VerificationType thisKlass) {
335 return i;
336 }
337
338 boolean is_assignable_to(VerificationFrame target) {
339 if (_max_locals != target.max_locals()) {
340 _verifier.verifyError("Locals size mismatch", this, target);
341 }
342 if (_stack_size != target.stack_size()) {
343 _verifier.verifyError("Stack size mismatch", this, target);
344 }
345 int mismatch_loc;
346 mismatch_loc = is_assignable_to(_locals, target.locals(), target.locals_size());
347 if (mismatch_loc != target.locals_size()) {
348 _verifier.verifyError("Bad type", this, target);
349 }
350 mismatch_loc = is_assignable_to(_stack, target.stack(), _stack_size);
351 if (mismatch_loc != _stack_size) {
352 _verifier.verifyError("Bad type", this, target);
353 }
354
355 // Check that assert unset fields are compatible
356 boolean compatible = verify_unset_fields_compatibility(target.assert_unset_fields());
357 if (!compatible) {
358 print_strict_fields(assert_unset_fields());
359 print_strict_fields(target.assert_unset_fields());
360 _verifier.verifyError("Strict fields mismatch from %s to %s".formatted(
361 assert_unset_fields(), target.assert_unset_fields()), this, target);
362 return false;
363 }
364
365 if ((_flags | target.flags()) == target.flags()) {
366 return true;
367 } else {
368 _verifier.verifyError("Bad flags", this, target);
369 }
370 return false;
371 }
372
373 VerificationType pop_stack_ex(VerificationType type) {
374 if (_stack_size <= 0) {
375 _verifier.verifyError("Operand stack underflow");
376 }
377 VerificationType top = _stack[--_stack_size];
378 boolean subtype = type.is_assignable_from(top, verifier());
379 if (!subtype) {
380 _verifier.verifyError("Bad type on operand stack");
381 }
382 return top;
383 }
384
|