src/share/vm/classfile/stackMapTable.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8703
02a3d0dcbedd
parent 8604
04d83ba48607
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
kevinw@8703 2 * Copyright (c) 2003, 2016, 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,
kevinw@8703 73 bool match, bool update, ErrorContext* ctx, TRAPS) const {
aoqi@0 74 int index = get_index_from_offset(target);
kevinw@8703 75 return match_stackmap(frame, target, index, match, update, 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 //
kevinw@8703 81 // The values of match and update are: _match__update
aoqi@0 82 //
kevinw@8703 83 // checking a branch target: true false
kevinw@8703 84 // checking an exception handler: true false
aoqi@0 85 // linear bytecode verification following an
kevinw@8703 86 // unconditional branch: false true
aoqi@0 87 // linear bytecode verification not following an
kevinw@8703 88 // unconditional branch: true true
aoqi@0 89 bool StackMapTable::match_stackmap(
aoqi@0 90 StackMapFrame* frame, int32_t target, int32_t frame_index,
kevinw@8703 91 bool match, bool update, ErrorContext* ctx, TRAPS) const {
aoqi@0 92 if (frame_index < 0 || frame_index >= _frame_count) {
aoqi@0 93 *ctx = ErrorContext::missing_stackmap(frame->offset());
aoqi@0 94 frame->verifier()->verify_error(
aoqi@0 95 *ctx, "Expecting a stackmap frame at branch target %d", target);
aoqi@0 96 return false;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 StackMapFrame *stackmap_frame = _frame_array[frame_index];
aoqi@0 100 bool result = true;
aoqi@0 101 if (match) {
aoqi@0 102 // Has direct control flow from last instruction, need to match the two
aoqi@0 103 // frames.
kevinw@8703 104 result = frame->is_assignable_to(stackmap_frame,
aoqi@0 105 ctx, CHECK_VERIFY_(frame->verifier(), result));
aoqi@0 106 }
aoqi@0 107 if (update) {
aoqi@0 108 // Use the frame in stackmap table as current frame
aoqi@0 109 int lsize = stackmap_frame->locals_size();
aoqi@0 110 int ssize = stackmap_frame->stack_size();
aoqi@0 111 if (frame->locals_size() > lsize || frame->stack_size() > ssize) {
aoqi@0 112 // Make sure unused type array items are all _bogus_type.
aoqi@0 113 frame->reset();
aoqi@0 114 }
aoqi@0 115 frame->set_locals_size(lsize);
aoqi@0 116 frame->copy_locals(stackmap_frame);
aoqi@0 117 frame->set_stack_size(ssize);
aoqi@0 118 frame->copy_stack(stackmap_frame);
aoqi@0 119 frame->set_flags(stackmap_frame->flags());
aoqi@0 120 }
aoqi@0 121 return result;
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 void StackMapTable::check_jump_target(
aoqi@0 125 StackMapFrame* frame, int32_t target, TRAPS) const {
aoqi@0 126 ErrorContext ctx;
aoqi@0 127 bool match = match_stackmap(
kevinw@8703 128 frame, target, true, false, &ctx, CHECK_VERIFY(frame->verifier()));
aoqi@0 129 if (!match || (target < 0 || target >= _code_length)) {
aoqi@0 130 frame->verifier()->verify_error(ctx,
aoqi@0 131 "Inconsistent stackmap frames at branch target %d", target);
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 void StackMapTable::print_on(outputStream* str) const {
aoqi@0 136 str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count);
aoqi@0 137 str->indent().print_cr("table = { ");
aoqi@0 138 {
aoqi@0 139 streamIndentor si(str);
aoqi@0 140 for (int32_t i = 0; i < _frame_count; ++i) {
aoqi@0 141 _frame_array[i]->print_on(str);
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 str->print_cr(" }");
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 int32_t StackMapReader::chop(
aoqi@0 148 VerificationType* locals, int32_t length, int32_t chops) {
aoqi@0 149 if (locals == NULL) return -1;
aoqi@0 150 int32_t pos = length - 1;
aoqi@0 151 for (int32_t i=0; i<chops; i++) {
aoqi@0 152 if (locals[pos].is_category2_2nd()) {
aoqi@0 153 pos -= 2;
aoqi@0 154 } else {
aoqi@0 155 pos --;
aoqi@0 156 }
aoqi@0 157 if (pos<0 && i<(chops-1)) return -1;
aoqi@0 158 }
aoqi@0 159 return pos+1;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) {
aoqi@0 163 u1 tag = _stream->get_u1(THREAD);
aoqi@0 164 if (tag < (u1)ITEM_UninitializedThis) {
aoqi@0 165 return VerificationType::from_tag(tag);
aoqi@0 166 }
aoqi@0 167 if (tag == ITEM_Object) {
aoqi@0 168 u2 class_index = _stream->get_u2(THREAD);
aoqi@0 169 int nconstants = _cp->length();
aoqi@0 170 if ((class_index <= 0 || class_index >= nconstants) ||
aoqi@0 171 (!_cp->tag_at(class_index).is_klass() &&
aoqi@0 172 !_cp->tag_at(class_index).is_unresolved_klass())) {
aoqi@0 173 _stream->stackmap_format_error("bad class index", THREAD);
aoqi@0 174 return VerificationType::bogus_type();
aoqi@0 175 }
aoqi@0 176 return VerificationType::reference_type(_cp->klass_name_at(class_index));
aoqi@0 177 }
aoqi@0 178 if (tag == ITEM_UninitializedThis) {
aoqi@0 179 if (flags != NULL) {
aoqi@0 180 *flags |= FLAG_THIS_UNINIT;
aoqi@0 181 }
aoqi@0 182 return VerificationType::uninitialized_this_type();
aoqi@0 183 }
aoqi@0 184 if (tag == ITEM_Uninitialized) {
aoqi@0 185 u2 offset = _stream->get_u2(THREAD);
aoqi@0 186 if (offset >= _code_length ||
aoqi@0 187 _code_data[offset] != ClassVerifier::NEW_OFFSET) {
aoqi@0 188 _verifier->class_format_error(
aoqi@0 189 "StackMapTable format error: bad offset for Uninitialized");
aoqi@0 190 return VerificationType::bogus_type();
aoqi@0 191 }
aoqi@0 192 return VerificationType::uninitialized_type(offset);
aoqi@0 193 }
aoqi@0 194 _stream->stackmap_format_error("bad verification type", THREAD);
aoqi@0 195 return VerificationType::bogus_type();
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 StackMapFrame* StackMapReader::next(
aoqi@0 199 StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {
aoqi@0 200 StackMapFrame* frame;
aoqi@0 201 int offset;
aoqi@0 202 VerificationType* locals = NULL;
aoqi@0 203 u1 frame_type = _stream->get_u1(THREAD);
aoqi@0 204 if (frame_type < 64) {
aoqi@0 205 // same_frame
aoqi@0 206 if (first) {
aoqi@0 207 offset = frame_type;
aoqi@0 208 // Can't share the locals array since that is updated by the verifier.
aoqi@0 209 if (pre_frame->locals_size() > 0) {
aoqi@0 210 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 211 THREAD, VerificationType, pre_frame->locals_size());
aoqi@0 212 }
aoqi@0 213 } else {
aoqi@0 214 offset = pre_frame->offset() + frame_type + 1;
aoqi@0 215 locals = pre_frame->locals();
aoqi@0 216 }
aoqi@0 217 frame = new StackMapFrame(
aoqi@0 218 offset, pre_frame->flags(), pre_frame->locals_size(), 0,
aoqi@0 219 max_locals, max_stack, locals, NULL, _verifier);
aoqi@0 220 if (first && locals != NULL) {
aoqi@0 221 frame->copy_locals(pre_frame);
aoqi@0 222 }
aoqi@0 223 return frame;
aoqi@0 224 }
aoqi@0 225 if (frame_type < 128) {
aoqi@0 226 // same_locals_1_stack_item_frame
aoqi@0 227 if (first) {
aoqi@0 228 offset = frame_type - 64;
aoqi@0 229 // Can't share the locals array since that is updated by the verifier.
aoqi@0 230 if (pre_frame->locals_size() > 0) {
aoqi@0 231 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 232 THREAD, VerificationType, pre_frame->locals_size());
aoqi@0 233 }
aoqi@0 234 } else {
aoqi@0 235 offset = pre_frame->offset() + frame_type - 63;
aoqi@0 236 locals = pre_frame->locals();
aoqi@0 237 }
aoqi@0 238 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 239 THREAD, VerificationType, 2);
aoqi@0 240 u2 stack_size = 1;
aoqi@0 241 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 242 if (stack[0].is_category2()) {
aoqi@0 243 stack[1] = stack[0].to_category2_2nd();
aoqi@0 244 stack_size = 2;
aoqi@0 245 }
aoqi@0 246 check_verification_type_array_size(
aoqi@0 247 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 248 frame = new StackMapFrame(
aoqi@0 249 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
aoqi@0 250 max_locals, max_stack, locals, stack, _verifier);
aoqi@0 251 if (first && locals != NULL) {
aoqi@0 252 frame->copy_locals(pre_frame);
aoqi@0 253 }
aoqi@0 254 return frame;
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 u2 offset_delta = _stream->get_u2(THREAD);
aoqi@0 258
aoqi@0 259 if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
aoqi@0 260 // reserved frame types
aoqi@0 261 _stream->stackmap_format_error(
aoqi@0 262 "reserved frame type", CHECK_VERIFY_(_verifier, NULL));
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
aoqi@0 266 // same_locals_1_stack_item_frame_extended
aoqi@0 267 if (first) {
aoqi@0 268 offset = offset_delta;
aoqi@0 269 // Can't share the locals array since that is updated by the verifier.
aoqi@0 270 if (pre_frame->locals_size() > 0) {
aoqi@0 271 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 272 THREAD, VerificationType, pre_frame->locals_size());
aoqi@0 273 }
aoqi@0 274 } else {
aoqi@0 275 offset = pre_frame->offset() + offset_delta + 1;
aoqi@0 276 locals = pre_frame->locals();
aoqi@0 277 }
aoqi@0 278 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 279 THREAD, VerificationType, 2);
aoqi@0 280 u2 stack_size = 1;
aoqi@0 281 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 282 if (stack[0].is_category2()) {
aoqi@0 283 stack[1] = stack[0].to_category2_2nd();
aoqi@0 284 stack_size = 2;
aoqi@0 285 }
aoqi@0 286 check_verification_type_array_size(
aoqi@0 287 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 288 frame = new StackMapFrame(
aoqi@0 289 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
aoqi@0 290 max_locals, max_stack, locals, stack, _verifier);
aoqi@0 291 if (first && locals != NULL) {
aoqi@0 292 frame->copy_locals(pre_frame);
aoqi@0 293 }
aoqi@0 294 return frame;
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 if (frame_type <= SAME_EXTENDED) {
aoqi@0 298 // chop_frame or same_frame_extended
aoqi@0 299 locals = pre_frame->locals();
aoqi@0 300 int length = pre_frame->locals_size();
aoqi@0 301 int chops = SAME_EXTENDED - frame_type;
aoqi@0 302 int new_length = length;
aoqi@0 303 u1 flags = pre_frame->flags();
aoqi@0 304 if (chops != 0) {
aoqi@0 305 new_length = chop(locals, length, chops);
aoqi@0 306 check_verification_type_array_size(
aoqi@0 307 new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 308 // Recompute flags since uninitializedThis could have been chopped.
aoqi@0 309 flags = 0;
aoqi@0 310 for (int i=0; i<new_length; i++) {
aoqi@0 311 if (locals[i].is_uninitialized_this()) {
aoqi@0 312 flags |= FLAG_THIS_UNINIT;
aoqi@0 313 break;
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316 }
aoqi@0 317 if (first) {
aoqi@0 318 offset = offset_delta;
aoqi@0 319 // Can't share the locals array since that is updated by the verifier.
aoqi@0 320 if (new_length > 0) {
aoqi@0 321 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 322 THREAD, VerificationType, new_length);
aoqi@0 323 } else {
aoqi@0 324 locals = NULL;
aoqi@0 325 }
aoqi@0 326 } else {
aoqi@0 327 offset = pre_frame->offset() + offset_delta + 1;
aoqi@0 328 }
aoqi@0 329 frame = new StackMapFrame(
aoqi@0 330 offset, flags, new_length, 0, max_locals, max_stack,
aoqi@0 331 locals, NULL, _verifier);
aoqi@0 332 if (first && locals != NULL) {
aoqi@0 333 frame->copy_locals(pre_frame);
aoqi@0 334 }
aoqi@0 335 return frame;
aoqi@0 336 } else if (frame_type < SAME_EXTENDED + 4) {
aoqi@0 337 // append_frame
aoqi@0 338 int appends = frame_type - SAME_EXTENDED;
aoqi@0 339 int real_length = pre_frame->locals_size();
aoqi@0 340 int new_length = real_length + appends*2;
aoqi@0 341 locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);
aoqi@0 342 VerificationType* pre_locals = pre_frame->locals();
aoqi@0 343 int i;
aoqi@0 344 for (i=0; i<pre_frame->locals_size(); i++) {
aoqi@0 345 locals[i] = pre_locals[i];
aoqi@0 346 }
aoqi@0 347 u1 flags = pre_frame->flags();
aoqi@0 348 for (i=0; i<appends; i++) {
aoqi@0 349 locals[real_length] = parse_verification_type(&flags, THREAD);
aoqi@0 350 if (locals[real_length].is_category2()) {
aoqi@0 351 locals[real_length + 1] = locals[real_length].to_category2_2nd();
aoqi@0 352 ++real_length;
aoqi@0 353 }
aoqi@0 354 ++real_length;
aoqi@0 355 }
aoqi@0 356 check_verification_type_array_size(
aoqi@0 357 real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 358 if (first) {
aoqi@0 359 offset = offset_delta;
aoqi@0 360 } else {
aoqi@0 361 offset = pre_frame->offset() + offset_delta + 1;
aoqi@0 362 }
aoqi@0 363 frame = new StackMapFrame(
aoqi@0 364 offset, flags, real_length, 0, max_locals,
aoqi@0 365 max_stack, locals, NULL, _verifier);
aoqi@0 366 return frame;
aoqi@0 367 }
aoqi@0 368 if (frame_type == FULL) {
aoqi@0 369 // full_frame
aoqi@0 370 u1 flags = 0;
aoqi@0 371 u2 locals_size = _stream->get_u2(THREAD);
aoqi@0 372 int real_locals_size = 0;
aoqi@0 373 if (locals_size > 0) {
aoqi@0 374 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 375 THREAD, VerificationType, locals_size*2);
aoqi@0 376 }
aoqi@0 377 int i;
aoqi@0 378 for (i=0; i<locals_size; i++) {
aoqi@0 379 locals[real_locals_size] = parse_verification_type(&flags, THREAD);
aoqi@0 380 if (locals[real_locals_size].is_category2()) {
aoqi@0 381 locals[real_locals_size + 1] =
aoqi@0 382 locals[real_locals_size].to_category2_2nd();
aoqi@0 383 ++real_locals_size;
aoqi@0 384 }
aoqi@0 385 ++real_locals_size;
aoqi@0 386 }
aoqi@0 387 check_verification_type_array_size(
aoqi@0 388 real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 389 u2 stack_size = _stream->get_u2(THREAD);
aoqi@0 390 int real_stack_size = 0;
aoqi@0 391 VerificationType* stack = NULL;
aoqi@0 392 if (stack_size > 0) {
aoqi@0 393 stack = NEW_RESOURCE_ARRAY_IN_THREAD(
aoqi@0 394 THREAD, VerificationType, stack_size*2);
aoqi@0 395 }
aoqi@0 396 for (i=0; i<stack_size; i++) {
aoqi@0 397 stack[real_stack_size] = parse_verification_type(NULL, THREAD);
aoqi@0 398 if (stack[real_stack_size].is_category2()) {
aoqi@0 399 stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();
aoqi@0 400 ++real_stack_size;
aoqi@0 401 }
aoqi@0 402 ++real_stack_size;
aoqi@0 403 }
aoqi@0 404 check_verification_type_array_size(
aoqi@0 405 real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
aoqi@0 406 if (first) {
aoqi@0 407 offset = offset_delta;
aoqi@0 408 } else {
aoqi@0 409 offset = pre_frame->offset() + offset_delta + 1;
aoqi@0 410 }
aoqi@0 411 frame = new StackMapFrame(
aoqi@0 412 offset, flags, real_locals_size, real_stack_size,
aoqi@0 413 max_locals, max_stack, locals, stack, _verifier);
aoqi@0 414 return frame;
aoqi@0 415 }
aoqi@0 416
aoqi@0 417 _stream->stackmap_format_error(
aoqi@0 418 "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));
aoqi@0 419 return NULL;
aoqi@0 420 }

mercurial