annotate src/share/vm/classfile/stackMapFrame.hpp @ 8202:02a3d0dcbedd

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