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: #include "precompiled.hpp" stefank@2314: #include "classfile/stackMapFrame.hpp" stefank@2314: #include "classfile/verifier.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/oop.inline.hpp" coleenp@2497: #include "oops/symbol.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "utilities/globalDefinitions.hpp" duke@435: duke@435: StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) : kamg@3992: _offset(0), _locals_size(0), _stack_size(0), kamg@3992: _stack_mark(0), _flags(0), _max_locals(max_locals), kamg@3992: _max_stack(max_stack), _verifier(v) { duke@435: Thread* thr = v->thread(); duke@435: _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals); duke@435: _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack); 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: StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) { duke@435: Thread* thr = _verifier->thread(); duke@435: VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1); duke@435: StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier); duke@435: return frame; duke@435: } duke@435: duke@435: bool StackMapFrame::has_new_object() const { duke@435: int32_t i; duke@435: for (i = 0; i < _max_locals; i++) { duke@435: if (_locals[i].is_uninitialized()) { duke@435: return true; duke@435: } duke@435: } duke@435: for (i = 0; i < _stack_size; i++) { duke@435: if (_stack[i].is_uninitialized()) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: void StackMapFrame::initialize_object( duke@435: VerificationType old_object, VerificationType new_object) { duke@435: int32_t i; duke@435: for (i = 0; i < _max_locals; i++) { duke@435: if (_locals[i].equals(old_object)) { duke@435: _locals[i] = new_object; duke@435: } duke@435: } duke@435: for (i = 0; i < _stack_size; i++) { duke@435: if (_stack[i].equals(old_object)) { duke@435: _stack[i] = new_object; duke@435: } duke@435: } duke@435: if (old_object == VerificationType::uninitialized_this_type()) { duke@435: // "this" has been initialized - reset flags duke@435: _flags = 0; duke@435: } duke@435: } duke@435: duke@435: VerificationType StackMapFrame::set_locals_from_arg( duke@435: const methodHandle m, VerificationType thisKlass, TRAPS) { coleenp@2497: SignatureStream ss(m->signature()); duke@435: int init_local_num = 0; duke@435: if (!m->is_static()) { duke@435: init_local_num++; duke@435: // add one extra argument for instance method duke@435: if (m->name() == vmSymbols::object_initializer_name() && duke@435: thisKlass.name() != vmSymbols::java_lang_Object()) { duke@435: _locals[0] = VerificationType::uninitialized_this_type(); duke@435: _flags |= FLAG_THIS_UNINIT; duke@435: } else { duke@435: _locals[0] = thisKlass; duke@435: } duke@435: } duke@435: duke@435: // local num may be greater than size of parameters because long/double occupies two slots duke@435: while(!ss.at_return_type()) { duke@435: init_local_num += _verifier->change_sig_to_verificationType( duke@435: &ss, &_locals[init_local_num], duke@435: CHECK_VERIFY_(verifier(), VerificationType::bogus_type())); duke@435: ss.next(); duke@435: } duke@435: _locals_size = init_local_num; duke@435: duke@435: switch (ss.type()) { duke@435: case T_OBJECT: duke@435: case T_ARRAY: duke@435: { coleenp@2497: Symbol* sig = ss.as_symbol(CHECK_(VerificationType::bogus_type())); coleenp@2497: // Create another symbol to save as signature stream unreferences coleenp@2497: // this symbol. coleenp@2497: Symbol* sig_copy = coleenp@2497: verifier()->create_temporary_symbol(sig, 0, sig->utf8_length(), coleenp@2497: CHECK_(VerificationType::bogus_type())); coleenp@2497: assert(sig_copy == sig, "symbols don't match"); coleenp@2497: return VerificationType::reference_type(sig_copy); duke@435: } duke@435: case T_INT: return VerificationType::integer_type(); duke@435: case T_BYTE: return VerificationType::byte_type(); duke@435: case T_CHAR: return VerificationType::char_type(); duke@435: case T_SHORT: return VerificationType::short_type(); duke@435: case T_BOOLEAN: return VerificationType::boolean_type(); duke@435: case T_FLOAT: return VerificationType::float_type(); duke@435: case T_DOUBLE: return VerificationType::double_type(); duke@435: case T_LONG: return VerificationType::long_type(); duke@435: case T_VOID: return VerificationType::bogus_type(); duke@435: default: duke@435: ShouldNotReachHere(); duke@435: } duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: duke@435: void StackMapFrame::copy_locals(const StackMapFrame* src) { duke@435: int32_t len = src->locals_size() < _locals_size ? duke@435: src->locals_size() : _locals_size; duke@435: for (int32_t i = 0; i < len; i++) { duke@435: _locals[i] = src->locals()[i]; duke@435: } duke@435: } duke@435: duke@435: void StackMapFrame::copy_stack(const StackMapFrame* src) { duke@435: int32_t len = src->stack_size() < _stack_size ? duke@435: src->stack_size() : _stack_size; duke@435: for (int32_t i = 0; i < len; i++) { duke@435: _stack[i] = src->stack()[i]; duke@435: } duke@435: } duke@435: kamg@3992: // Returns the location of the first mismatch, or 'len' if there are no kamg@3992: // mismatches kamg@3992: int StackMapFrame::is_assignable_to( duke@435: VerificationType* from, VerificationType* to, int32_t len, TRAPS) const { kamg@3992: int32_t i = 0; kamg@3992: for (i = 0; i < len; i++) { kamg@3992: if (!to[i].is_assignable_from(from[i], verifier(), THREAD)) { kamg@3992: break; duke@435: } duke@435: } kamg@3992: return i; duke@435: } duke@435: kamg@2585: bool StackMapFrame::has_flag_match_exception( kamg@2585: const StackMapFrame* target) const { kamg@2585: // We allow flags of {UninitThis} to assign to {} if-and-only-if the kamg@2585: // target frame does not depend upon the current type. kamg@2585: // This is slightly too strict, as we need only enforce that the kamg@2585: // slots that were initialized by the (the things that were kamg@2585: // UninitializedThis before initialize_object() converted them) are unused. kamg@2585: // However we didn't save that information so we'll enforce this upon kamg@2585: // anything that might have been initialized. This is a rare situation kamg@2585: // and javac never generates code that would end up here, but some profilers kamg@2585: // (such as NetBeans) might, when adding exception handlers in kamg@2585: // methods to cover the invokespecial instruction. See 7020118. kamg@2585: kamg@2585: assert(max_locals() == target->max_locals() && kamg@2585: stack_size() == target->stack_size(), "StackMap sizes must match"); kamg@2585: kamg@2585: VerificationType top = VerificationType::top_type(); kamg@2585: VerificationType this_type = verifier()->current_type(); kamg@2585: kamg@2585: if (!flag_this_uninit() || target->flags() != 0) { kamg@2585: return false; kamg@2585: } kamg@2585: kamg@2585: for (int i = 0; i < target->locals_size(); ++i) { kamg@2585: if (locals()[i] == this_type && target->locals()[i] != top) { kamg@2585: return false; kamg@2585: } kamg@2585: } kamg@2585: kamg@2585: for (int i = 0; i < target->stack_size(); ++i) { kamg@2585: if (stack()[i] == this_type && target->stack()[i] != top) { kamg@2585: return false; kamg@2585: } kamg@2585: } kamg@2585: kamg@2585: return true; kamg@2585: } kamg@2585: kamg@2754: bool StackMapFrame::is_assignable_to( kamg@3992: const StackMapFrame* target, bool is_exception_handler, kamg@3992: ErrorContext* ctx, TRAPS) const { kamg@3992: if (_max_locals != target->max_locals()) { kamg@3992: *ctx = ErrorContext::locals_size_mismatch( kamg@3992: _offset, (StackMapFrame*)this, (StackMapFrame*)target); kamg@3992: return false; kamg@3992: } kamg@3992: if (_stack_size != target->stack_size()) { kamg@3992: *ctx = ErrorContext::stack_size_mismatch( kamg@3992: _offset, (StackMapFrame*)this, (StackMapFrame*)target); duke@435: return false; duke@435: } duke@435: // Only need to compare type elements up to target->locals() or target->stack(). duke@435: // The remaining type elements in this state can be ignored because they are duke@435: // assignable to bogus type. kamg@3992: int mismatch_loc; kamg@3992: mismatch_loc = is_assignable_to( kamg@3992: _locals, target->locals(), target->locals_size(), THREAD); kamg@3992: if (mismatch_loc != target->locals_size()) { kamg@3992: *ctx = ErrorContext::bad_type(target->offset(), kamg@3992: TypeOrigin::local(mismatch_loc, (StackMapFrame*)this), kamg@3992: TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target)); kamg@3992: return false; kamg@3992: } kamg@3992: mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD); kamg@3992: if (mismatch_loc != _stack_size) { kamg@3992: *ctx = ErrorContext::bad_type(target->offset(), kamg@3992: TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this), kamg@3992: TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target)); kamg@3992: return false; kamg@3992: } kamg@3992: duke@435: bool match_flags = (_flags | target->flags()) == target->flags(); kamg@3992: if (match_flags || is_exception_handler && has_flag_match_exception(target)) { kamg@3992: return true; kamg@3992: } else { kamg@3992: *ctx = ErrorContext::bad_flags(target->offset(), kamg@3992: (StackMapFrame*)this, (StackMapFrame*)target); kamg@3992: return false; kamg@3992: } duke@435: } duke@435: duke@435: VerificationType StackMapFrame::pop_stack_ex(VerificationType type, 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: bool subtype = type.is_assignable_from( coleenp@2497: top, verifier(), CHECK_(VerificationType::bogus_type())); duke@435: if (!subtype) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_type(_offset, stack_top_ctx(), kamg@3992: TypeOrigin::implicit(type)), kamg@3992: "Bad type on operand stack"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: return top; duke@435: } duke@435: duke@435: VerificationType StackMapFrame::get_local( duke@435: int32_t index, VerificationType type, TRAPS) { duke@435: if (index >= _max_locals) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_local_index(_offset, index), kamg@3992: "Local variable table overflow"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: bool subtype = type.is_assignable_from(_locals[index], coleenp@2497: verifier(), CHECK_(VerificationType::bogus_type())); duke@435: if (!subtype) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_type(_offset, kamg@3992: TypeOrigin::local(index, this), kamg@3992: TypeOrigin::implicit(type)), kamg@3992: "Bad local variable type"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: if(index >= _locals_size) { _locals_size = index + 1; } duke@435: return _locals[index]; duke@435: } duke@435: duke@435: void StackMapFrame::get_local_2( duke@435: int32_t index, 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 (index >= _locals_size - 1) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_local_index(_offset, index), kamg@3992: "get long/double overflows locals"); duke@435: return; duke@435: } kamg@3992: bool subtype = type1.is_assignable_from(_locals[index], verifier(), CHECK); kamg@3992: if (!subtype) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_type(_offset, kamg@3992: TypeOrigin::local(index, this), TypeOrigin::implicit(type1)), kamg@3992: "Bad local variable type"); kamg@3992: } else { kamg@3992: subtype = type2.is_assignable_from(_locals[index + 1], verifier(), CHECK); kamg@3992: if (!subtype) { kamg@3992: /* Unreachable? All local store routines convert a split long or double kamg@3992: * into a TOP during the store. So we should never end up seeing an kamg@3992: * orphaned half. */ kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_type(_offset, kamg@3992: TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)), kamg@3992: "Bad local variable type"); kamg@3992: } duke@435: } duke@435: } duke@435: duke@435: void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) { duke@435: assert(!type.is_check(), "Must be a real type"); duke@435: if (index >= _max_locals) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_local_index(_offset, index), kamg@3992: "Local variable table overflow"); duke@435: return; duke@435: } duke@435: // If type at index is double or long, set the next location to be unusable duke@435: if (_locals[index].is_double() || _locals[index].is_long()) { duke@435: assert((index + 1) < _locals_size, "Local variable table overflow"); duke@435: _locals[index + 1] = VerificationType::bogus_type(); duke@435: } duke@435: // If type at index is double_2 or long_2, set the previous location to be unusable duke@435: if (_locals[index].is_double2() || _locals[index].is_long2()) { duke@435: assert(index >= 1, "Local variable table underflow"); duke@435: _locals[index - 1] = VerificationType::bogus_type(); duke@435: } duke@435: _locals[index] = type; duke@435: if (index >= _locals_size) { duke@435: #ifdef ASSERT duke@435: for (int i=_locals_size; i= _max_locals - 1) { kamg@3992: verifier()->verify_error( kamg@3992: ErrorContext::bad_local_index(_offset, index), kamg@3992: "Local variable table overflow"); duke@435: return; duke@435: } duke@435: // If type at index+1 is double or long, set the next location to be unusable duke@435: if (_locals[index+1].is_double() || _locals[index+1].is_long()) { duke@435: assert((index + 2) < _locals_size, "Local variable table overflow"); duke@435: _locals[index + 2] = VerificationType::bogus_type(); duke@435: } duke@435: // If type at index is double_2 or long_2, set the previous location to be unusable duke@435: if (_locals[index].is_double2() || _locals[index].is_long2()) { duke@435: assert(index >= 1, "Local variable table underflow"); duke@435: _locals[index - 1] = VerificationType::bogus_type(); duke@435: } duke@435: _locals[index] = type1; duke@435: _locals[index+1] = type2; duke@435: if (index >= _locals_size - 1) { duke@435: #ifdef ASSERT duke@435: for (int i=_locals_size; iindent().print_cr("bci: @%d", _offset); kamg@3992: str->indent().print_cr("flags: {%s }", kamg@3992: flag_this_uninit() ? " flagThisUninit" : ""); kamg@3992: str->indent().print("locals: {"); kamg@3992: for (int32_t i = 0; i < _locals_size; ++i) { kamg@3992: str->print(" "); kamg@3992: _locals[i].print_on(str); kamg@3992: if (i != _locals_size - 1) { kamg@3992: str->print(","); kamg@3992: } kamg@3992: } kamg@3992: str->print_cr(" }"); kamg@3992: str->indent().print("stack: {"); kamg@3992: for (int32_t j = 0; j < _stack_size; ++j) { kamg@3992: str->print(" "); kamg@3992: _stack[j].print_on(str); kamg@3992: if (j != _stack_size - 1) { kamg@3992: str->print(","); kamg@3992: } kamg@3992: } kamg@3992: str->print_cr(" }"); kamg@3992: }