duke@435: /* trims@2708: * Copyright (c) 2003, 2011, 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/stackMapTable.hpp" stefank@2314: #include "classfile/verifier.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/fieldType.hpp" stefank@2314: #include "runtime/handles.inline.hpp" duke@435: duke@435: StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame, duke@435: u2 max_locals, u2 max_stack, duke@435: char* code_data, int code_len, TRAPS) { duke@435: _code_length = code_len; duke@435: _frame_count = reader->get_frame_count(); duke@435: if (_frame_count > 0) { duke@435: _frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, duke@435: StackMapFrame*, _frame_count); duke@435: StackMapFrame* pre_frame = init_frame; duke@435: for (int32_t i = 0; i < _frame_count; i++) { duke@435: StackMapFrame* frame = reader->next( duke@435: pre_frame, i == 0, max_locals, max_stack, duke@435: CHECK_VERIFY(pre_frame->verifier())); duke@435: _frame_array[i] = frame; duke@435: int offset = frame->offset(); duke@435: if (offset >= code_len || code_data[offset] == 0) { duke@435: frame->verifier()->verify_error("StackMapTable error: bad offset"); duke@435: return; duke@435: } duke@435: pre_frame = frame; duke@435: } duke@435: } duke@435: reader->check_end(CHECK); duke@435: } duke@435: duke@435: // This method is only called by method in StackMapTable. duke@435: int StackMapTable::get_index_from_offset(int32_t offset) const { duke@435: int i = 0; duke@435: for (; i < _frame_count; i++) { duke@435: if (_frame_array[i]->offset() == offset) { duke@435: return i; duke@435: } duke@435: } duke@435: return i; // frame with offset doesn't exist in the array duke@435: } duke@435: duke@435: bool StackMapTable::match_stackmap( duke@435: StackMapFrame* frame, int32_t target, duke@435: bool match, bool update, TRAPS) const { duke@435: int index = get_index_from_offset(target); duke@435: duke@435: return match_stackmap( duke@435: frame, target, index, match, duke@435: update, CHECK_VERIFY_(frame->verifier(), false)); duke@435: } duke@435: duke@435: // Match and/or update current_frame to the frame in stackmap table with duke@435: // specified offset and frame index. Return true if the two frames match. duke@435: // duke@435: // The values of match and update are: _match__update_ duke@435: // duke@435: // checking a branch target/exception handler: true false duke@435: // linear bytecode verification following an duke@435: // unconditional branch: false true duke@435: // linear bytecode verification not following an duke@435: // unconditional branch: true true duke@435: bool StackMapTable::match_stackmap( duke@435: StackMapFrame* frame, int32_t target, int32_t frame_index, duke@435: bool match, bool update, TRAPS) const { duke@435: if (frame_index < 0 || frame_index >= _frame_count) { duke@435: frame->verifier()->verify_error(frame->offset(), duke@435: "Expecting a stackmap frame at branch target %d", target); duke@435: return false; duke@435: } duke@435: duke@435: bool result = true; duke@435: StackMapFrame *stackmap_frame = _frame_array[frame_index]; duke@435: if (match) { kamg@2754: // when checking handler target, match == true && update == false kamg@2754: bool is_exception_handler = !update; duke@435: // Has direct control flow from last instruction, need to match the two duke@435: // frames. duke@435: result = frame->is_assignable_to( kamg@2754: stackmap_frame, is_exception_handler, kamg@2754: CHECK_VERIFY_(frame->verifier(), false)); duke@435: } duke@435: if (update) { duke@435: // Use the frame in stackmap table as current frame duke@435: int lsize = stackmap_frame->locals_size(); duke@435: int ssize = stackmap_frame->stack_size(); duke@435: if (frame->locals_size() > lsize || frame->stack_size() > ssize) { duke@435: // Make sure unused type array items are all _bogus_type. duke@435: frame->reset(); duke@435: } duke@435: frame->set_locals_size(lsize); duke@435: frame->copy_locals(stackmap_frame); duke@435: frame->set_stack_size(ssize); duke@435: frame->copy_stack(stackmap_frame); duke@435: frame->set_flags(stackmap_frame->flags()); duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: void StackMapTable::check_jump_target( duke@435: StackMapFrame* frame, int32_t target, TRAPS) const { duke@435: bool match = match_stackmap( duke@435: frame, target, true, false, CHECK_VERIFY(frame->verifier())); duke@435: if (!match || (target < 0 || target >= _code_length)) { duke@435: frame->verifier()->verify_error(frame->offset(), duke@435: "Inconsistent stackmap frames at branch target %d", target); duke@435: return; duke@435: } duke@435: // check if uninitialized objects exist on backward branches duke@435: check_new_object(frame, target, CHECK_VERIFY(frame->verifier())); duke@435: } duke@435: duke@435: void StackMapTable::check_new_object( duke@435: const StackMapFrame* frame, int32_t target, TRAPS) const { duke@435: if (frame->offset() > target && frame->has_new_object()) { duke@435: frame->verifier()->verify_error(frame->offset(), duke@435: "Uninitialized object exists on backward branch %d", target); duke@435: return; duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void StackMapTable::print() const { duke@435: tty->print_cr("StackMapTable: frame_count = %d", _frame_count); duke@435: tty->print_cr("table = { "); duke@435: for (int32_t i = 0; i < _frame_count; i++) { duke@435: _frame_array[i]->print(); duke@435: } duke@435: tty->print_cr(" }"); duke@435: } duke@435: duke@435: #endif duke@435: duke@435: int32_t StackMapReader::chop( duke@435: VerificationType* locals, int32_t length, int32_t chops) { kamg@2124: if (locals == NULL) return -1; duke@435: int32_t pos = length - 1; duke@435: for (int32_t i=0; iget_u1(THREAD); duke@435: if (tag < (u1)ITEM_UninitializedThis) { duke@435: return VerificationType::from_tag(tag); duke@435: } duke@435: if (tag == ITEM_Object) { duke@435: u2 class_index = _stream->get_u2(THREAD); duke@435: int nconstants = _cp->length(); duke@435: if ((class_index <= 0 || class_index >= nconstants) || duke@435: (!_cp->tag_at(class_index).is_klass() && duke@435: !_cp->tag_at(class_index).is_unresolved_klass())) { duke@435: _stream->stackmap_format_error("bad class index", THREAD); duke@435: return VerificationType::bogus_type(); duke@435: } coleenp@2497: return VerificationType::reference_type(_cp->klass_name_at(class_index)); duke@435: } duke@435: if (tag == ITEM_UninitializedThis) { duke@435: if (flags != NULL) { duke@435: *flags |= FLAG_THIS_UNINIT; duke@435: } duke@435: return VerificationType::uninitialized_this_type(); duke@435: } duke@435: if (tag == ITEM_Uninitialized) { duke@435: u2 offset = _stream->get_u2(THREAD); duke@435: if (offset >= _code_length || duke@435: _code_data[offset] != ClassVerifier::NEW_OFFSET) { duke@435: ResourceMark rm(THREAD); duke@435: _verifier->class_format_error( duke@435: "StackMapTable format error: bad offset for Uninitialized"); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: return VerificationType::uninitialized_type(offset); duke@435: } duke@435: _stream->stackmap_format_error("bad verification type", THREAD); duke@435: return VerificationType::bogus_type(); duke@435: } duke@435: duke@435: StackMapFrame* StackMapReader::next( duke@435: StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) { duke@435: StackMapFrame* frame; duke@435: int offset; duke@435: VerificationType* locals = NULL; duke@435: u1 frame_type = _stream->get_u1(THREAD); duke@435: if (frame_type < 64) { duke@435: // same_frame duke@435: if (first) { duke@435: offset = frame_type; duke@435: // Can't share the locals array since that is updated by the verifier. duke@435: if (pre_frame->locals_size() > 0) { duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, pre_frame->locals_size()); duke@435: } duke@435: } else { duke@435: offset = pre_frame->offset() + frame_type + 1; duke@435: locals = pre_frame->locals(); duke@435: } duke@435: frame = new StackMapFrame( duke@435: offset, pre_frame->flags(), pre_frame->locals_size(), 0, duke@435: max_locals, max_stack, locals, NULL, _verifier); duke@435: if (first && locals != NULL) { duke@435: frame->copy_locals(pre_frame); duke@435: } duke@435: return frame; duke@435: } duke@435: if (frame_type < 128) { duke@435: // same_locals_1_stack_item_frame duke@435: if (first) { duke@435: offset = frame_type - 64; duke@435: // Can't share the locals array since that is updated by the verifier. duke@435: if (pre_frame->locals_size() > 0) { duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, pre_frame->locals_size()); duke@435: } duke@435: } else { duke@435: offset = pre_frame->offset() + frame_type - 63; duke@435: locals = pre_frame->locals(); duke@435: } duke@435: VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, 2); duke@435: u2 stack_size = 1; duke@435: stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); duke@435: if (stack[0].is_category2()) { duke@435: stack[1] = stack[0].to_category2_2nd(); duke@435: stack_size = 2; duke@435: } duke@435: check_verification_type_array_size( duke@435: stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); duke@435: frame = new StackMapFrame( duke@435: offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, duke@435: max_locals, max_stack, locals, stack, _verifier); duke@435: if (first && locals != NULL) { duke@435: frame->copy_locals(pre_frame); duke@435: } duke@435: return frame; duke@435: } duke@435: duke@435: u2 offset_delta = _stream->get_u2(THREAD); duke@435: duke@435: if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) { duke@435: // reserved frame types duke@435: _stream->stackmap_format_error( duke@435: "reserved frame type", CHECK_VERIFY_(_verifier, NULL)); duke@435: } duke@435: duke@435: if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) { duke@435: // same_locals_1_stack_item_frame_extended duke@435: if (first) { duke@435: offset = offset_delta; duke@435: // Can't share the locals array since that is updated by the verifier. duke@435: if (pre_frame->locals_size() > 0) { duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, pre_frame->locals_size()); duke@435: } duke@435: } else { duke@435: offset = pre_frame->offset() + offset_delta + 1; duke@435: locals = pre_frame->locals(); duke@435: } duke@435: VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, 2); duke@435: u2 stack_size = 1; duke@435: stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); duke@435: if (stack[0].is_category2()) { duke@435: stack[1] = stack[0].to_category2_2nd(); duke@435: stack_size = 2; duke@435: } duke@435: check_verification_type_array_size( duke@435: stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); duke@435: frame = new StackMapFrame( duke@435: offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, duke@435: max_locals, max_stack, locals, stack, _verifier); duke@435: if (first && locals != NULL) { duke@435: frame->copy_locals(pre_frame); duke@435: } duke@435: return frame; duke@435: } duke@435: duke@435: if (frame_type <= SAME_EXTENDED) { duke@435: // chop_frame or same_frame_extended duke@435: locals = pre_frame->locals(); duke@435: int length = pre_frame->locals_size(); duke@435: int chops = SAME_EXTENDED - frame_type; duke@435: int new_length = length; duke@435: u1 flags = pre_frame->flags(); duke@435: if (chops != 0) { duke@435: new_length = chop(locals, length, chops); duke@435: check_verification_type_array_size( duke@435: new_length, max_locals, CHECK_VERIFY_(_verifier, NULL)); duke@435: // Recompute flags since uninitializedThis could have been chopped. duke@435: flags = 0; duke@435: for (int i=0; i 0) { duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, new_length); duke@435: } else { duke@435: locals = NULL; duke@435: } duke@435: } else { duke@435: offset = pre_frame->offset() + offset_delta + 1; duke@435: } duke@435: frame = new StackMapFrame( duke@435: offset, flags, new_length, 0, max_locals, max_stack, duke@435: locals, NULL, _verifier); duke@435: if (first && locals != NULL) { duke@435: frame->copy_locals(pre_frame); duke@435: } duke@435: return frame; duke@435: } else if (frame_type < SAME_EXTENDED + 4) { duke@435: // append_frame duke@435: int appends = frame_type - SAME_EXTENDED; duke@435: int real_length = pre_frame->locals_size(); duke@435: int new_length = real_length + appends*2; duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length); duke@435: VerificationType* pre_locals = pre_frame->locals(); duke@435: int i; duke@435: for (i=0; ilocals_size(); i++) { duke@435: locals[i] = pre_locals[i]; duke@435: } duke@435: u1 flags = pre_frame->flags(); duke@435: for (i=0; ioffset() + offset_delta + 1; duke@435: } duke@435: frame = new StackMapFrame( duke@435: offset, flags, real_length, 0, max_locals, duke@435: max_stack, locals, NULL, _verifier); duke@435: return frame; duke@435: } duke@435: if (frame_type == FULL) { duke@435: // full_frame duke@435: u1 flags = 0; duke@435: u2 locals_size = _stream->get_u2(THREAD); duke@435: int real_locals_size = 0; duke@435: if (locals_size > 0) { duke@435: locals = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, locals_size*2); duke@435: } duke@435: int i; duke@435: for (i=0; iget_u2(THREAD); duke@435: int real_stack_size = 0; duke@435: VerificationType* stack = NULL; duke@435: if (stack_size > 0) { duke@435: stack = NEW_RESOURCE_ARRAY_IN_THREAD( duke@435: THREAD, VerificationType, stack_size*2); duke@435: } duke@435: for (i=0; ioffset() + offset_delta + 1; duke@435: } duke@435: frame = new StackMapFrame( duke@435: offset, flags, real_locals_size, real_stack_size, duke@435: max_locals, max_stack, locals, stack, _verifier); duke@435: return frame; duke@435: } duke@435: duke@435: _stream->stackmap_format_error( duke@435: "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL)); duke@435: return NULL; duke@435: }