duke@435: /* kamg@3992: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP stefank@2314: #define SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP stefank@2314: stefank@2314: #include "classfile/verificationType.hpp" stefank@2314: #include "classfile/verifier.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #include "utilities/exceptions.hpp" stefank@2314: duke@435: // A StackMapFrame represents one frame in the stack map attribute. duke@435: kamg@3992: class TypeContext; kamg@3992: duke@435: enum { duke@435: FLAG_THIS_UNINIT = 0x01 duke@435: }; duke@435: duke@435: class StackMapFrame : public ResourceObj { duke@435: private: duke@435: int32_t _offset; duke@435: duke@435: // See comment in StackMapTable about _frame_count about why these duke@435: // fields are int32_t instead of u2. duke@435: int32_t _locals_size; // number of valid type elements in _locals duke@435: int32_t _stack_size; // number of valid type elements in _stack duke@435: kamg@3992: int32_t _stack_mark; // Records the size of the stack prior to an kamg@3992: // instruction modification, to allow rewinding kamg@3992: // when/if an error occurs. kamg@3992: duke@435: int32_t _max_locals; duke@435: int32_t _max_stack; duke@435: duke@435: u1 _flags; duke@435: VerificationType* _locals; // local variable type array duke@435: VerificationType* _stack; // operand stack type array duke@435: duke@435: ClassVerifier* _verifier; // the verifier verifying this method duke@435: kamg@3992: StackMapFrame(const StackMapFrame& cp) : kamg@3992: _offset(cp._offset), _locals_size(cp._locals_size), kamg@3992: _stack_size(cp._stack_size), _stack_mark(cp._stack_mark), kamg@3992: _max_locals(cp._max_locals), _max_stack(cp._max_stack), kamg@3992: _flags(cp._flags) { kamg@3992: _locals = NEW_RESOURCE_ARRAY(VerificationType, _max_locals); kamg@3992: for (int i = 0; i < _max_locals; ++i) { kamg@3992: if (i < _locals_size) { kamg@3992: _locals[i] = cp._locals[i]; kamg@3992: } else { kamg@3992: _locals[i] = VerificationType::bogus_type(); kamg@3992: } kamg@3992: } kamg@3992: int ss = MAX2(_stack_size, _stack_mark); kamg@3992: _stack = NEW_RESOURCE_ARRAY(VerificationType, _max_stack); kamg@3992: for (int i = 0; i < _max_stack; ++i) { kamg@3992: if (i < ss) { kamg@3992: _stack[i] = cp._stack[i]; kamg@3992: } else { kamg@3992: _stack[i] = VerificationType::bogus_type(); kamg@3992: } kamg@3992: } kamg@3992: _verifier = NULL; kamg@3992: } kamg@3992: duke@435: public: duke@435: // constructors duke@435: duke@435: // This constructor is used by the type checker to allocate frames duke@435: // in type state, which have _max_locals and _max_stack array elements duke@435: // in _locals and _stack. duke@435: StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier); duke@435: duke@435: // This constructor is used to initialize stackmap frames in stackmap table, duke@435: // which have _locals_size and _stack_size array elements in _locals and _stack. duke@435: StackMapFrame(int32_t offset, duke@435: u1 flags, duke@435: u2 locals_size, duke@435: u2 stack_size, duke@435: u2 max_locals, duke@435: u2 max_stack, duke@435: VerificationType* locals, duke@435: VerificationType* stack, duke@435: ClassVerifier* v) : _offset(offset), _flags(flags), duke@435: _locals_size(locals_size), duke@435: _stack_size(stack_size), kamg@3992: _stack_mark(-1), duke@435: _max_locals(max_locals), duke@435: _max_stack(max_stack), duke@435: _locals(locals), _stack(stack), duke@435: _verifier(v) { } duke@435: kamg@3992: static StackMapFrame* copy(StackMapFrame* smf) { kamg@3992: return new StackMapFrame(*smf); kamg@3992: } kamg@3992: duke@435: inline void set_offset(int32_t offset) { _offset = offset; } duke@435: inline void set_verifier(ClassVerifier* v) { _verifier = v; } duke@435: inline void set_flags(u1 flags) { _flags = flags; } duke@435: inline void set_locals_size(u2 locals_size) { _locals_size = locals_size; } kamg@3992: inline void set_stack_size(u2 stack_size) { _stack_size = _stack_mark = stack_size; } duke@435: inline void clear_stack() { _stack_size = 0; } duke@435: inline int32_t offset() const { return _offset; } duke@435: inline ClassVerifier* verifier() const { return _verifier; } duke@435: inline u1 flags() const { return _flags; } duke@435: inline int32_t locals_size() const { return _locals_size; } duke@435: inline VerificationType* locals() const { return _locals; } duke@435: inline int32_t stack_size() const { return _stack_size; } duke@435: inline VerificationType* stack() const { return _stack; } duke@435: inline int32_t max_locals() const { return _max_locals; } duke@435: inline int32_t max_stack() const { return _max_stack; } duke@435: inline bool flag_this_uninit() const { return _flags & FLAG_THIS_UNINIT; } duke@435: duke@435: // Set locals and stack types to bogus duke@435: inline void reset() { duke@435: int32_t i; duke@435: for (i = 0; i < _max_locals; i++) { duke@435: _locals[i] = VerificationType::bogus_type(); duke@435: } duke@435: for (i = 0; i < _max_stack; i++) { duke@435: _stack[i] = VerificationType::bogus_type(); duke@435: } duke@435: } duke@435: duke@435: // Return a StackMapFrame with the same local variable array and empty stack. duke@435: // Stack array is allocate with unused one element. duke@435: StackMapFrame* frame_in_exception_handler(u1 flags); duke@435: duke@435: // Set local variable type array based on m's signature. duke@435: VerificationType set_locals_from_arg( duke@435: const methodHandle m, VerificationType thisKlass, TRAPS); duke@435: duke@435: // Search local variable type array and stack type array. duke@435: // Return true if an uninitialized object is found. duke@435: bool has_new_object() const; duke@435: duke@435: // Search local variable type array and stack type array. duke@435: // Set every element with type of old_object to new_object. duke@435: void initialize_object( duke@435: VerificationType old_object, VerificationType new_object); duke@435: duke@435: // Copy local variable type array in src into this local variable type array. duke@435: void copy_locals(const StackMapFrame* src); duke@435: duke@435: // Copy stack type array in src into this stack type array. duke@435: void copy_stack(const StackMapFrame* src); duke@435: duke@435: // Return true if this stack map frame is assignable to target. kamg@3992: bool is_assignable_to( kamg@3992: const StackMapFrame* target, bool is_exception_handler, kamg@3992: ErrorContext* ctx, TRAPS) const; kamg@3992: kamg@3992: inline void set_mark() { kamg@3992: #ifdef DEBUG kamg@3992: // Put bogus type to indicate it's no longer valid. kamg@3992: if (_stack_mark != -1) { simonis@4526: for (int i = _stack_mark - 1; i >= _stack_size; --i) { kamg@3992: _stack[i] = VerificationType::bogus_type(); kamg@3992: } kamg@3992: } kamg@3992: #endif // def DEBUG kamg@3992: _stack_mark = _stack_size; kamg@3992: } kamg@3992: kamg@3992: // Used when an error occurs and we want to reset the stack to the state kamg@3992: // it was before operands were popped off. kamg@3992: void restore() { kamg@3992: if (_stack_mark != -1) { kamg@3992: _stack_size = _stack_mark; kamg@3992: } kamg@3992: } duke@435: duke@435: // Push type into stack type array. duke@435: inline void push_stack(VerificationType type, TRAPS) { duke@435: assert(!type.is_check(), "Must be a real type"); duke@435: if (_stack_size >= _max_stack) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::stack_overflow(_offset, this), kamg@3992: "Operand stack overflow"); duke@435: return; duke@435: } duke@435: _stack[_stack_size++] = type; duke@435: } duke@435: duke@435: inline void push_stack_2( duke@435: VerificationType type1, VerificationType type2, TRAPS) { duke@435: assert(type1.is_long() || type1.is_double(), "must be long/double"); duke@435: assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); duke@435: if (_stack_size >= _max_stack - 1) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::stack_overflow(_offset, this), kamg@3992: "Operand stack overflow"); duke@435: return; duke@435: } duke@435: _stack[_stack_size++] = type1; duke@435: _stack[_stack_size++] = type2; duke@435: } duke@435: duke@435: // Pop and return the top type on stack without verifying. duke@435: inline VerificationType pop_stack(TRAPS) { duke@435: if (_stack_size <= 0) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::stack_underflow(_offset, this), kamg@3992: "Operand stack underflow"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: VerificationType top = _stack[--_stack_size]; duke@435: return top; duke@435: } duke@435: duke@435: // Pop and return the top type on stack type array after verifying it duke@435: // is assignable to type. duke@435: inline VerificationType pop_stack(VerificationType type, TRAPS) { duke@435: if (_stack_size != 0) { duke@435: VerificationType top = _stack[_stack_size - 1]; duke@435: bool subtype = type.is_assignable_from( coleenp@2497: top, verifier(), CHECK_(VerificationType::bogus_type())); duke@435: if (subtype) { kamg@3992: --_stack_size; duke@435: return top; duke@435: } duke@435: } duke@435: return pop_stack_ex(type, THREAD); duke@435: } duke@435: duke@435: inline void pop_stack_2( duke@435: VerificationType type1, VerificationType type2, TRAPS) { duke@435: assert(type1.is_long2() || type1.is_double2(), "must be long/double"); duke@435: assert(type2.is_long() || type2.is_double(), "must be long/double_2"); duke@435: if (_stack_size >= 2) { duke@435: VerificationType top1 = _stack[_stack_size - 1]; coleenp@2497: bool subtype1 = type1.is_assignable_from(top1, verifier(), CHECK); duke@435: VerificationType top2 = _stack[_stack_size - 2]; coleenp@2497: bool subtype2 = type2.is_assignable_from(top2, verifier(), CHECK); duke@435: if (subtype1 && subtype2) { duke@435: _stack_size -= 2; duke@435: return; duke@435: } duke@435: } duke@435: pop_stack_ex(type1, THREAD); duke@435: pop_stack_ex(type2, THREAD); duke@435: } duke@435: kamg@3992: VerificationType local_at(int index) { kamg@3992: return _locals[index]; kamg@3992: } kamg@3992: kamg@3992: VerificationType stack_at(int index) { kamg@3992: return _stack[index]; kamg@3992: } kamg@3992: duke@435: // Uncommon case that throws exceptions. duke@435: VerificationType pop_stack_ex(VerificationType type, TRAPS); duke@435: duke@435: // Return the type at index in local variable array after verifying duke@435: // it is assignable to type. duke@435: VerificationType get_local(int32_t index, VerificationType type, TRAPS); duke@435: // For long/double. duke@435: void get_local_2( duke@435: int32_t index, VerificationType type1, VerificationType type2, TRAPS); duke@435: duke@435: // Set element at index in local variable array to type. duke@435: void set_local(int32_t index, VerificationType type, TRAPS); duke@435: // For long/double. duke@435: void set_local_2( duke@435: int32_t index, VerificationType type1, VerificationType type2, TRAPS); duke@435: duke@435: // Private auxiliary method used only in is_assignable_to(StackMapFrame). duke@435: // Returns true if src is assignable to target. kamg@3992: int is_assignable_to( duke@435: VerificationType* src, VerificationType* target, int32_t len, TRAPS) const; duke@435: kamg@2585: bool has_flag_match_exception(const StackMapFrame* target) const; kamg@2585: kamg@3992: TypeOrigin stack_top_ctx(); kamg@3992: kamg@3992: void print_on(outputStream* str) const; duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP