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