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