Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/stackMapTable.cpp
32285 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/stackMapTable.hpp"26#include "classfile/verifier.hpp"27#include "memory/resourceArea.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/fieldType.hpp"30#include "runtime/handles.inline.hpp"3132StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,33u2 max_locals, u2 max_stack,34char* code_data, int code_len, TRAPS) {35_code_length = code_len;36_frame_count = reader->get_frame_count();37if (_frame_count > 0) {38_frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,39StackMapFrame*, _frame_count);40StackMapFrame* pre_frame = init_frame;41for (int32_t i = 0; i < _frame_count; i++) {42StackMapFrame* frame = reader->next(43pre_frame, i == 0, max_locals, max_stack,44CHECK_VERIFY(pre_frame->verifier()));45_frame_array[i] = frame;46int offset = frame->offset();47if (offset >= code_len || code_data[offset] == 0) {48frame->verifier()->verify_error(49ErrorContext::bad_stackmap(i, frame),50"StackMapTable error: bad offset");51return;52}53pre_frame = frame;54}55}56reader->check_end(CHECK);57}5859// This method is only called by method in StackMapTable.60int StackMapTable::get_index_from_offset(int32_t offset) const {61int i = 0;62for (; i < _frame_count; i++) {63if (_frame_array[i]->offset() == offset) {64return i;65}66}67return i; // frame with offset doesn't exist in the array68}6970bool StackMapTable::match_stackmap(71StackMapFrame* frame, int32_t target,72bool match, bool update, ErrorContext* ctx, TRAPS) const {73int index = get_index_from_offset(target);74return match_stackmap(frame, target, index, match, update, ctx, THREAD);75}7677// Match and/or update current_frame to the frame in stackmap table with78// specified offset and frame index. Return true if the two frames match.79//80// The values of match and update are: _match__update81//82// checking a branch target: true false83// checking an exception handler: true false84// linear bytecode verification following an85// unconditional branch: false true86// linear bytecode verification not following an87// unconditional branch: true true88bool StackMapTable::match_stackmap(89StackMapFrame* frame, int32_t target, int32_t frame_index,90bool match, bool update, ErrorContext* ctx, TRAPS) const {91if (frame_index < 0 || frame_index >= _frame_count) {92*ctx = ErrorContext::missing_stackmap(frame->offset());93frame->verifier()->verify_error(94*ctx, "Expecting a stackmap frame at branch target %d", target);95return false;96}9798StackMapFrame *stackmap_frame = _frame_array[frame_index];99bool result = true;100if (match) {101// Has direct control flow from last instruction, need to match the two102// frames.103result = frame->is_assignable_to(stackmap_frame,104ctx, CHECK_VERIFY_(frame->verifier(), result));105}106if (update) {107// Use the frame in stackmap table as current frame108int lsize = stackmap_frame->locals_size();109int ssize = stackmap_frame->stack_size();110if (frame->locals_size() > lsize || frame->stack_size() > ssize) {111// Make sure unused type array items are all _bogus_type.112frame->reset();113}114frame->set_locals_size(lsize);115frame->copy_locals(stackmap_frame);116frame->set_stack_size(ssize);117frame->copy_stack(stackmap_frame);118frame->set_flags(stackmap_frame->flags());119}120return result;121}122123void StackMapTable::check_jump_target(124StackMapFrame* frame, int32_t target, TRAPS) const {125ErrorContext ctx;126bool match = match_stackmap(127frame, target, true, false, &ctx, CHECK_VERIFY(frame->verifier()));128if (!match || (target < 0 || target >= _code_length)) {129frame->verifier()->verify_error(ctx,130"Inconsistent stackmap frames at branch target %d", target);131}132}133134void StackMapTable::print_on(outputStream* str) const {135str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count);136str->indent().print_cr("table = { ");137{138streamIndentor si(str);139for (int32_t i = 0; i < _frame_count; ++i) {140_frame_array[i]->print_on(str);141}142}143str->print_cr(" }");144}145146int32_t StackMapReader::chop(147VerificationType* locals, int32_t length, int32_t chops) {148if (locals == NULL) return -1;149int32_t pos = length - 1;150for (int32_t i=0; i<chops; i++) {151if (locals[pos].is_category2_2nd()) {152pos -= 2;153} else {154pos --;155}156if (pos<0 && i<(chops-1)) return -1;157}158return pos+1;159}160161VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) {162u1 tag = _stream->get_u1(THREAD);163if (tag < (u1)ITEM_UninitializedThis) {164return VerificationType::from_tag(tag);165}166if (tag == ITEM_Object) {167u2 class_index = _stream->get_u2(THREAD);168int nconstants = _cp->length();169if ((class_index <= 0 || class_index >= nconstants) ||170(!_cp->tag_at(class_index).is_klass() &&171!_cp->tag_at(class_index).is_unresolved_klass())) {172_stream->stackmap_format_error("bad class index", THREAD);173return VerificationType::bogus_type();174}175return VerificationType::reference_type(_cp->klass_name_at(class_index));176}177if (tag == ITEM_UninitializedThis) {178if (flags != NULL) {179*flags |= FLAG_THIS_UNINIT;180}181return VerificationType::uninitialized_this_type();182}183if (tag == ITEM_Uninitialized) {184u2 offset = _stream->get_u2(THREAD);185if (offset >= _code_length ||186_code_data[offset] != ClassVerifier::NEW_OFFSET) {187_verifier->class_format_error(188"StackMapTable format error: bad offset for Uninitialized");189return VerificationType::bogus_type();190}191return VerificationType::uninitialized_type(offset);192}193_stream->stackmap_format_error("bad verification type", THREAD);194return VerificationType::bogus_type();195}196197StackMapFrame* StackMapReader::next(198StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {199StackMapFrame* frame;200int offset;201VerificationType* locals = NULL;202u1 frame_type = _stream->get_u1(THREAD);203if (frame_type < 64) {204// same_frame205if (first) {206offset = frame_type;207// Can't share the locals array since that is updated by the verifier.208if (pre_frame->locals_size() > 0) {209locals = NEW_RESOURCE_ARRAY_IN_THREAD(210THREAD, VerificationType, pre_frame->locals_size());211}212} else {213offset = pre_frame->offset() + frame_type + 1;214locals = pre_frame->locals();215}216frame = new StackMapFrame(217offset, pre_frame->flags(), pre_frame->locals_size(), 0,218max_locals, max_stack, locals, NULL, _verifier);219if (first && locals != NULL) {220frame->copy_locals(pre_frame);221}222return frame;223}224if (frame_type < 128) {225// same_locals_1_stack_item_frame226if (first) {227offset = frame_type - 64;228// Can't share the locals array since that is updated by the verifier.229if (pre_frame->locals_size() > 0) {230locals = NEW_RESOURCE_ARRAY_IN_THREAD(231THREAD, VerificationType, pre_frame->locals_size());232}233} else {234offset = pre_frame->offset() + frame_type - 63;235locals = pre_frame->locals();236}237VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(238THREAD, VerificationType, 2);239u2 stack_size = 1;240stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));241if (stack[0].is_category2()) {242stack[1] = stack[0].to_category2_2nd();243stack_size = 2;244}245check_verification_type_array_size(246stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));247frame = new StackMapFrame(248offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,249max_locals, max_stack, locals, stack, _verifier);250if (first && locals != NULL) {251frame->copy_locals(pre_frame);252}253return frame;254}255256u2 offset_delta = _stream->get_u2(THREAD);257258if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {259// reserved frame types260_stream->stackmap_format_error(261"reserved frame type", CHECK_VERIFY_(_verifier, NULL));262}263264if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {265// same_locals_1_stack_item_frame_extended266if (first) {267offset = offset_delta;268// Can't share the locals array since that is updated by the verifier.269if (pre_frame->locals_size() > 0) {270locals = NEW_RESOURCE_ARRAY_IN_THREAD(271THREAD, VerificationType, pre_frame->locals_size());272}273} else {274offset = pre_frame->offset() + offset_delta + 1;275locals = pre_frame->locals();276}277VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(278THREAD, VerificationType, 2);279u2 stack_size = 1;280stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));281if (stack[0].is_category2()) {282stack[1] = stack[0].to_category2_2nd();283stack_size = 2;284}285check_verification_type_array_size(286stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));287frame = new StackMapFrame(288offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,289max_locals, max_stack, locals, stack, _verifier);290if (first && locals != NULL) {291frame->copy_locals(pre_frame);292}293return frame;294}295296if (frame_type <= SAME_EXTENDED) {297// chop_frame or same_frame_extended298locals = pre_frame->locals();299int length = pre_frame->locals_size();300int chops = SAME_EXTENDED - frame_type;301int new_length = length;302u1 flags = pre_frame->flags();303if (chops != 0) {304new_length = chop(locals, length, chops);305check_verification_type_array_size(306new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));307// Recompute flags since uninitializedThis could have been chopped.308flags = 0;309for (int i=0; i<new_length; i++) {310if (locals[i].is_uninitialized_this()) {311flags |= FLAG_THIS_UNINIT;312break;313}314}315}316if (first) {317offset = offset_delta;318// Can't share the locals array since that is updated by the verifier.319if (new_length > 0) {320locals = NEW_RESOURCE_ARRAY_IN_THREAD(321THREAD, VerificationType, new_length);322} else {323locals = NULL;324}325} else {326offset = pre_frame->offset() + offset_delta + 1;327}328frame = new StackMapFrame(329offset, flags, new_length, 0, max_locals, max_stack,330locals, NULL, _verifier);331if (first && locals != NULL) {332frame->copy_locals(pre_frame);333}334return frame;335} else if (frame_type < SAME_EXTENDED + 4) {336// append_frame337int appends = frame_type - SAME_EXTENDED;338int real_length = pre_frame->locals_size();339int new_length = real_length + appends*2;340locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);341VerificationType* pre_locals = pre_frame->locals();342int i;343for (i=0; i<pre_frame->locals_size(); i++) {344locals[i] = pre_locals[i];345}346u1 flags = pre_frame->flags();347for (i=0; i<appends; i++) {348locals[real_length] = parse_verification_type(&flags, THREAD);349if (locals[real_length].is_category2()) {350locals[real_length + 1] = locals[real_length].to_category2_2nd();351++real_length;352}353++real_length;354}355check_verification_type_array_size(356real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));357if (first) {358offset = offset_delta;359} else {360offset = pre_frame->offset() + offset_delta + 1;361}362frame = new StackMapFrame(363offset, flags, real_length, 0, max_locals,364max_stack, locals, NULL, _verifier);365return frame;366}367if (frame_type == FULL) {368// full_frame369u1 flags = 0;370u2 locals_size = _stream->get_u2(THREAD);371int real_locals_size = 0;372if (locals_size > 0) {373locals = NEW_RESOURCE_ARRAY_IN_THREAD(374THREAD, VerificationType, locals_size*2);375}376int i;377for (i=0; i<locals_size; i++) {378locals[real_locals_size] = parse_verification_type(&flags, THREAD);379if (locals[real_locals_size].is_category2()) {380locals[real_locals_size + 1] =381locals[real_locals_size].to_category2_2nd();382++real_locals_size;383}384++real_locals_size;385}386check_verification_type_array_size(387real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));388u2 stack_size = _stream->get_u2(THREAD);389int real_stack_size = 0;390VerificationType* stack = NULL;391if (stack_size > 0) {392stack = NEW_RESOURCE_ARRAY_IN_THREAD(393THREAD, VerificationType, stack_size*2);394}395for (i=0; i<stack_size; i++) {396stack[real_stack_size] = parse_verification_type(NULL, THREAD);397if (stack[real_stack_size].is_category2()) {398stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();399++real_stack_size;400}401++real_stack_size;402}403check_verification_type_array_size(404real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));405if (first) {406offset = offset_delta;407} else {408offset = pre_frame->offset() + offset_delta + 1;409}410frame = new StackMapFrame(411offset, flags, real_locals_size, real_stack_size,412max_locals, max_stack, locals, stack, _verifier);413return frame;414}415416_stream->stackmap_format_error(417"reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));418return NULL;419}420421422