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