src/share/vm/classfile/stackMapFrame.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6872
c77d5db18942
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP
aoqi@0 26 #define SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP
aoqi@0 27
aoqi@0 28 #include "classfile/verificationType.hpp"
aoqi@0 29 #include "classfile/verifier.hpp"
aoqi@0 30 #include "oops/method.hpp"
aoqi@0 31 #include "runtime/handles.hpp"
aoqi@0 32 #include "runtime/signature.hpp"
aoqi@0 33 #include "utilities/exceptions.hpp"
aoqi@0 34
aoqi@0 35 // A StackMapFrame represents one frame in the stack map attribute.
aoqi@0 36
aoqi@0 37 class TypeContext;
aoqi@0 38
aoqi@0 39 enum {
aoqi@0 40 FLAG_THIS_UNINIT = 0x01
aoqi@0 41 };
aoqi@0 42
aoqi@0 43 class StackMapFrame : public ResourceObj {
aoqi@0 44 private:
aoqi@0 45 int32_t _offset;
aoqi@0 46
aoqi@0 47 // See comment in StackMapTable about _frame_count about why these
aoqi@0 48 // fields are int32_t instead of u2.
aoqi@0 49 int32_t _locals_size; // number of valid type elements in _locals
aoqi@0 50 int32_t _stack_size; // number of valid type elements in _stack
aoqi@0 51
aoqi@0 52 int32_t _stack_mark; // Records the size of the stack prior to an
aoqi@0 53 // instruction modification, to allow rewinding
aoqi@0 54 // when/if an error occurs.
aoqi@0 55
aoqi@0 56 int32_t _max_locals;
aoqi@0 57 int32_t _max_stack;
aoqi@0 58
aoqi@0 59 u1 _flags;
aoqi@0 60 VerificationType* _locals; // local variable type array
aoqi@0 61 VerificationType* _stack; // operand stack type array
aoqi@0 62
aoqi@0 63 ClassVerifier* _verifier; // the verifier verifying this method
aoqi@0 64
aoqi@0 65 StackMapFrame(const StackMapFrame& cp) :
aoqi@0 66 _offset(cp._offset), _locals_size(cp._locals_size),
aoqi@0 67 _stack_size(cp._stack_size), _stack_mark(cp._stack_mark),
aoqi@0 68 _max_locals(cp._max_locals), _max_stack(cp._max_stack),
aoqi@0 69 _flags(cp._flags) {
aoqi@0 70 _locals = NEW_RESOURCE_ARRAY(VerificationType, _max_locals);
aoqi@0 71 for (int i = 0; i < _max_locals; ++i) {
aoqi@0 72 if (i < _locals_size) {
aoqi@0 73 _locals[i] = cp._locals[i];
aoqi@0 74 } else {
aoqi@0 75 _locals[i] = VerificationType::bogus_type();
aoqi@0 76 }
aoqi@0 77 }
aoqi@0 78 int ss = MAX2(_stack_size, _stack_mark);
aoqi@0 79 _stack = NEW_RESOURCE_ARRAY(VerificationType, _max_stack);
aoqi@0 80 for (int i = 0; i < _max_stack; ++i) {
aoqi@0 81 if (i < ss) {
aoqi@0 82 _stack[i] = cp._stack[i];
aoqi@0 83 } else {
aoqi@0 84 _stack[i] = VerificationType::bogus_type();
aoqi@0 85 }
aoqi@0 86 }
aoqi@0 87 _verifier = NULL;
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 public:
aoqi@0 91 // constructors
aoqi@0 92
aoqi@0 93 // This constructor is used by the type checker to allocate frames
aoqi@0 94 // in type state, which have _max_locals and _max_stack array elements
aoqi@0 95 // in _locals and _stack.
aoqi@0 96 StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier);
aoqi@0 97
aoqi@0 98 // This constructor is used to initialize stackmap frames in stackmap table,
aoqi@0 99 // which have _locals_size and _stack_size array elements in _locals and _stack.
aoqi@0 100 StackMapFrame(int32_t offset,
aoqi@0 101 u1 flags,
aoqi@0 102 u2 locals_size,
aoqi@0 103 u2 stack_size,
aoqi@0 104 u2 max_locals,
aoqi@0 105 u2 max_stack,
aoqi@0 106 VerificationType* locals,
aoqi@0 107 VerificationType* stack,
aoqi@0 108 ClassVerifier* v) : _offset(offset), _flags(flags),
aoqi@0 109 _locals_size(locals_size),
aoqi@0 110 _stack_size(stack_size),
aoqi@0 111 _stack_mark(-1),
aoqi@0 112 _max_locals(max_locals),
aoqi@0 113 _max_stack(max_stack),
aoqi@0 114 _locals(locals), _stack(stack),
aoqi@0 115 _verifier(v) { }
aoqi@0 116
aoqi@0 117 static StackMapFrame* copy(StackMapFrame* smf) {
aoqi@0 118 return new StackMapFrame(*smf);
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 inline void set_offset(int32_t offset) { _offset = offset; }
aoqi@0 122 inline void set_verifier(ClassVerifier* v) { _verifier = v; }
aoqi@0 123 inline void set_flags(u1 flags) { _flags = flags; }
aoqi@0 124 inline void set_locals_size(u2 locals_size) { _locals_size = locals_size; }
aoqi@0 125 inline void set_stack_size(u2 stack_size) { _stack_size = _stack_mark = stack_size; }
aoqi@0 126 inline void clear_stack() { _stack_size = 0; }
aoqi@0 127 inline int32_t offset() const { return _offset; }
aoqi@0 128 inline ClassVerifier* verifier() const { return _verifier; }
aoqi@0 129 inline u1 flags() const { return _flags; }
aoqi@0 130 inline int32_t locals_size() const { return _locals_size; }
aoqi@0 131 inline VerificationType* locals() const { return _locals; }
aoqi@0 132 inline int32_t stack_size() const { return _stack_size; }
aoqi@0 133 inline VerificationType* stack() const { return _stack; }
aoqi@0 134 inline int32_t max_locals() const { return _max_locals; }
aoqi@0 135 inline int32_t max_stack() const { return _max_stack; }
aoqi@0 136 inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; }
aoqi@0 137
aoqi@0 138 // Set locals and stack types to bogus
aoqi@0 139 inline void reset() {
aoqi@0 140 int32_t i;
aoqi@0 141 for (i = 0; i < _max_locals; i++) {
aoqi@0 142 _locals[i] = VerificationType::bogus_type();
aoqi@0 143 }
aoqi@0 144 for (i = 0; i < _max_stack; i++) {
aoqi@0 145 _stack[i] = VerificationType::bogus_type();
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 // Return a StackMapFrame with the same local variable array and empty stack.
aoqi@0 150 // Stack array is allocate with unused one element.
aoqi@0 151 StackMapFrame* frame_in_exception_handler(u1 flags);
aoqi@0 152
aoqi@0 153 // Set local variable type array based on m's signature.
aoqi@0 154 VerificationType set_locals_from_arg(
aoqi@0 155 const methodHandle m, VerificationType thisKlass, TRAPS);
aoqi@0 156
aoqi@0 157 // Search local variable type array and stack type array.
aoqi@0 158 // Set every element with type of old_object to new_object.
aoqi@0 159 void initialize_object(
aoqi@0 160 VerificationType old_object, VerificationType new_object);
aoqi@0 161
aoqi@0 162 // Copy local variable type array in src into this local variable type array.
aoqi@0 163 void copy_locals(const StackMapFrame* src);
aoqi@0 164
aoqi@0 165 // Copy stack type array in src into this stack type array.
aoqi@0 166 void copy_stack(const StackMapFrame* src);
aoqi@0 167
aoqi@0 168 // Return true if this stack map frame is assignable to target.
aoqi@0 169 bool is_assignable_to(
aoqi@0 170 const StackMapFrame* target, bool is_exception_handler,
aoqi@0 171 ErrorContext* ctx, TRAPS) const;
aoqi@0 172
aoqi@0 173 inline void set_mark() {
aoqi@0 174 #ifdef ASSERT
aoqi@0 175 // Put bogus type to indicate it's no longer valid.
aoqi@0 176 if (_stack_mark != -1) {
aoqi@0 177 for (int i = _stack_mark - 1; i >= _stack_size; --i) {
aoqi@0 178 _stack[i] = VerificationType::bogus_type();
aoqi@0 179 }
aoqi@0 180 }
aoqi@0 181 #endif // def ASSERT
aoqi@0 182 _stack_mark = _stack_size;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 // Used when an error occurs and we want to reset the stack to the state
aoqi@0 186 // it was before operands were popped off.
aoqi@0 187 void restore() {
aoqi@0 188 if (_stack_mark != -1) {
aoqi@0 189 _stack_size = _stack_mark;
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 // Push type into stack type array.
aoqi@0 194 inline void push_stack(VerificationType type, TRAPS) {
aoqi@0 195 assert(!type.is_check(), "Must be a real type");
aoqi@0 196 if (_stack_size >= _max_stack) {
aoqi@0 197 verifier()->verify_error(
aoqi@0 198 ErrorContext::stack_overflow(_offset, this),
aoqi@0 199 "Operand stack overflow");
aoqi@0 200 return;
aoqi@0 201 }
aoqi@0 202 _stack[_stack_size++] = type;
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 inline void push_stack_2(
aoqi@0 206 VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 207 assert(type1.is_long() || type1.is_double(), "must be long/double");
aoqi@0 208 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
aoqi@0 209 if (_stack_size >= _max_stack - 1) {
aoqi@0 210 verifier()->verify_error(
aoqi@0 211 ErrorContext::stack_overflow(_offset, this),
aoqi@0 212 "Operand stack overflow");
aoqi@0 213 return;
aoqi@0 214 }
aoqi@0 215 _stack[_stack_size++] = type1;
aoqi@0 216 _stack[_stack_size++] = type2;
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Pop and return the top type on stack without verifying.
aoqi@0 220 inline VerificationType pop_stack(TRAPS) {
aoqi@0 221 if (_stack_size <= 0) {
aoqi@0 222 verifier()->verify_error(
aoqi@0 223 ErrorContext::stack_underflow(_offset, this),
aoqi@0 224 "Operand stack underflow");
aoqi@0 225 return VerificationType::bogus_type();
aoqi@0 226 }
aoqi@0 227 VerificationType top = _stack[--_stack_size];
aoqi@0 228 return top;
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 // Pop and return the top type on stack type array after verifying it
aoqi@0 232 // is assignable to type.
aoqi@0 233 inline VerificationType pop_stack(VerificationType type, TRAPS) {
aoqi@0 234 if (_stack_size != 0) {
aoqi@0 235 VerificationType top = _stack[_stack_size - 1];
aoqi@0 236 bool subtype = type.is_assignable_from(
aoqi@0 237 top, verifier(), false, CHECK_(VerificationType::bogus_type()));
aoqi@0 238 if (subtype) {
aoqi@0 239 --_stack_size;
aoqi@0 240 return top;
aoqi@0 241 }
aoqi@0 242 }
aoqi@0 243 return pop_stack_ex(type, THREAD);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 inline void pop_stack_2(
aoqi@0 247 VerificationType type1, VerificationType type2, TRAPS) {
aoqi@0 248 assert(type1.is_long2() || type1.is_double2(), "must be long/double");
aoqi@0 249 assert(type2.is_long() || type2.is_double(), "must be long/double_2");
aoqi@0 250 if (_stack_size >= 2) {
aoqi@0 251 VerificationType top1 = _stack[_stack_size - 1];
aoqi@0 252 bool subtype1 = type1.is_assignable_from(top1, verifier(), false, CHECK);
aoqi@0 253 VerificationType top2 = _stack[_stack_size - 2];
aoqi@0 254 bool subtype2 = type2.is_assignable_from(top2, verifier(), false, CHECK);
aoqi@0 255 if (subtype1 && subtype2) {
aoqi@0 256 _stack_size -= 2;
aoqi@0 257 return;
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260 pop_stack_ex(type1, THREAD);
aoqi@0 261 pop_stack_ex(type2, THREAD);
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 VerificationType local_at(int index) {
aoqi@0 265 return _locals[index];
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 VerificationType stack_at(int index) {
aoqi@0 269 return _stack[index];
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 // Uncommon case that throws exceptions.
aoqi@0 273 VerificationType pop_stack_ex(VerificationType type, TRAPS);
aoqi@0 274
aoqi@0 275 // Return the type at index in local variable array after verifying
aoqi@0 276 // it is assignable to type.
aoqi@0 277 VerificationType get_local(int32_t index, VerificationType type, TRAPS);
aoqi@0 278 // For long/double.
aoqi@0 279 void get_local_2(
aoqi@0 280 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
aoqi@0 281
aoqi@0 282 // Set element at index in local variable array to type.
aoqi@0 283 void set_local(int32_t index, VerificationType type, TRAPS);
aoqi@0 284 // For long/double.
aoqi@0 285 void set_local_2(
aoqi@0 286 int32_t index, VerificationType type1, VerificationType type2, TRAPS);
aoqi@0 287
aoqi@0 288 // Private auxiliary method used only in is_assignable_to(StackMapFrame).
aoqi@0 289 // Returns true if src is assignable to target.
aoqi@0 290 int is_assignable_to(
aoqi@0 291 VerificationType* src, VerificationType* target, int32_t len, TRAPS) const;
aoqi@0 292
aoqi@0 293 bool has_flag_match_exception(const StackMapFrame* target) const;
aoqi@0 294
aoqi@0 295 TypeOrigin stack_top_ctx();
aoqi@0 296
aoqi@0 297 void print_on(outputStream* str) const;
aoqi@0 298 };
aoqi@0 299
aoqi@0 300 #endif // SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP

mercurial