src/share/vm/classfile/stackMapTable.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

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

mercurial