1/*
2 * Copyright (c) 2003, 2019, 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
37class TypeContext;
38
39enum {
40 FLAG_THIS_UNINIT = 0x01
41};
42
43class 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 int32_t _max_locals;
57 int32_t _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 = NULL;
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 u2 locals_size,
104 u2 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(u2 locals_size) { _locals_size = locals_size; }
126 inline void set_stack_size(u2 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 int32_t max_locals() const { return _max_locals; }
136 inline int32_t 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, TRAPS);
157
158 // Search local variable type array and stack type array.
159 // Set every element with type of old_object to new_object.
160 void initialize_object(
161 VerificationType old_object, VerificationType new_object);
162
163 // Copy local variable type array in src into this local variable type array.
164 void copy_locals(const StackMapFrame* src);
165
166 // Copy stack type array in src into this stack type array.
167 void copy_stack(const StackMapFrame* src);
168
169 // Return true if this stack map frame is assignable to target.
170 bool is_assignable_to(
171 const StackMapFrame* target, ErrorContext* ctx, TRAPS) const;
172
173 inline void set_mark() {
174#ifdef ASSERT
175 // Put bogus type to indicate it's no longer valid.
176 if (_stack_mark != -1) {
177 for (int i = _stack_mark - 1; i >= _stack_size; --i) {
178 _stack[i] = VerificationType::bogus_type();
179 }
180 }
181#endif // def ASSERT
182 _stack_mark = _stack_size;
183 }
184
185 // Used when an error occurs and we want to reset the stack to the state
186 // it was before operands were popped off.
187 void restore() {
188 if (_stack_mark != -1) {
189 _stack_size = _stack_mark;
190 }
191 }
192
193 // Push type into stack type array.
194 inline void push_stack(VerificationType type, TRAPS) {
195 assert(!type.is_check(), "Must be a real type");
196 if (_stack_size >= _max_stack) {
197 verifier()->verify_error(
198 ErrorContext::stack_overflow(_offset, this),
199 "Operand stack overflow");
200 return;
201 }
202 _stack[_stack_size++] = type;
203 }
204
205 inline void push_stack_2(
206 VerificationType type1, VerificationType type2, TRAPS) {
207 assert(type1.is_long() || type1.is_double(), "must be long/double");
208 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
209 if (_stack_size >= _max_stack - 1) {
210 verifier()->verify_error(
211 ErrorContext::stack_overflow(_offset, this),
212 "Operand stack overflow");
213 return;
214 }
215 _stack[_stack_size++] = type1;
216 _stack[_stack_size++] = type2;
217 }
218
219 // Pop and return the top type on stack without verifying.
220 inline VerificationType pop_stack(TRAPS) {
221 if (_stack_size <= 0) {
222 verifier()->verify_error(
223 ErrorContext::stack_underflow(_offset, this),
224 "Operand stack underflow");
225 return VerificationType::bogus_type();
226 }
227 VerificationType top = _stack[--_stack_size];
228 return top;
229 }
230
231 // Pop and return the top type on stack type array after verifying it
232 // is assignable to type.
233 inline VerificationType pop_stack(VerificationType type, TRAPS) {
234 if (_stack_size != 0) {
235 VerificationType top = _stack[_stack_size - 1];
236 bool subtype = type.is_assignable_from(
237 top, verifier(), false, CHECK_(VerificationType::bogus_type()));
238 if (subtype) {
239 --_stack_size;
240 return top;
241 }
242 }
243 return pop_stack_ex(type, THREAD);
244 }
245
246 inline void pop_stack_2(
247 VerificationType type1, VerificationType type2, TRAPS) {
248 assert(type1.is_long2() || type1.is_double2(), "must be long/double");
249 assert(type2.is_long() || type2.is_double(), "must be long/double_2");
250 if (_stack_size >= 2) {
251 VerificationType top1 = _stack[_stack_size - 1];
252 bool subtype1 = type1.is_assignable_from(top1, verifier(), false, CHECK);
253 VerificationType top2 = _stack[_stack_size - 2];
254 bool subtype2 = type2.is_assignable_from(top2, verifier(), false, CHECK);
255 if (subtype1 && subtype2) {
256 _stack_size -= 2;
257 return;
258 }
259 }
260 pop_stack_ex(type1, THREAD);
261 pop_stack_ex(type2, THREAD);
262 }
263
264 VerificationType local_at(int index) {
265 return _locals[index];
266 }
267
268 VerificationType stack_at(int index) {
269 return _stack[index];
270 }
271
272 // Uncommon case that throws exceptions.
273 VerificationType pop_stack_ex(VerificationType type, TRAPS);
274
275 // Return the type at index in local variable array after verifying
276 // it is assignable to type.
277 VerificationType get_local(int32_t index, VerificationType type, TRAPS);
278 // For long/double.
279 void get_local_2(
280 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
281
282 // Set element at index in local variable array to type.
283 void set_local(int32_t index, VerificationType type, TRAPS);
284 // For long/double.
285 void set_local_2(
286 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
287
288 // Private auxiliary method used only in is_assignable_to(StackMapFrame).
289 // Returns true if src is assignable to target.
290 int is_assignable_to(
291 VerificationType* src, VerificationType* target, int32_t len, TRAPS) const;
292
293 TypeOrigin stack_top_ctx();
294
295 void print_on(outputStream* str) const;
296};
297
298#endif // SHARE_CLASSFILE_STACKMAPFRAME_HPP
299