1 /*
   2  * Copyright (c) 2003, 2018, 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/constantPool.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/fieldType.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 
  34 StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
  35                              u2 max_locals, u2 max_stack,
  36                              char* code_data, int code_len, TRAPS) {
  37   _code_length = code_len;
  38   _frame_count = reader->get_frame_count();
  39   if (_frame_count > 0) {
  40     _frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
  41                                                 StackMapFrame*, _frame_count);
  42     StackMapFrame* pre_frame = init_frame;
  43     for (int32_t i = 0; i < _frame_count; i++) {
  44       StackMapFrame* frame = reader->next(
  45         pre_frame, i == 0, max_locals, max_stack,
  46         CHECK_VERIFY(pre_frame->verifier()));
  47       _frame_array[i] = frame;
  48       int offset = frame->offset();
  49       if (offset >= code_len || code_data[offset] == 0) {
  50         frame->verifier()->verify_error(
  51             ErrorContext::bad_stackmap(i, frame),
  52             "StackMapTable error: bad offset");
  53         return;
  54       }
  55       pre_frame = frame;
  56     }
  57   }
  58   reader->check_end(CHECK);
  59 }
  60 
  61 // This method is only called by method in StackMapTable.
  62 int StackMapTable::get_index_from_offset(int32_t offset) const {
  63   int i = 0;
  64   for (; i < _frame_count; i++) {
  65     if (_frame_array[i]->offset() == offset) {
  66       return i;
  67     }
  68   }
  69   return i;  // frame with offset doesn't exist in the array
  70 }
  71 
  72 bool StackMapTable::match_stackmap(
  73     StackMapFrame* frame, int32_t target,
  74     bool match, bool update, ErrorContext* ctx, TRAPS) const {
  75   int index = get_index_from_offset(target);
  76   return match_stackmap(frame, target, index, match, update, ctx, THREAD);
  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:                             true   false
  85 // checking an exception handler:                        true   false
  86 // linear bytecode verification following an
  87 // unconditional branch:                                 false  true
  88 // linear bytecode verification not following an
  89 // unconditional branch:                                 true   true
  90 bool StackMapTable::match_stackmap(
  91     StackMapFrame* frame, int32_t target, int32_t frame_index,
  92     bool match, bool update, ErrorContext* ctx, TRAPS) const {
  93   if (frame_index < 0 || frame_index >= _frame_count) {
  94     *ctx = ErrorContext::missing_stackmap(frame->offset());
  95     frame->verifier()->verify_error(
  96         *ctx, "Expecting a stackmap frame at branch target %d", target);
  97     return false;
  98   }
  99 
 100   StackMapFrame *stackmap_frame = _frame_array[frame_index];
 101   bool result = true;
 102   if (match) {
 103     // Has direct control flow from last instruction, need to match the two
 104     // frames.
 105     result = frame->is_assignable_to(stackmap_frame,
 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   }
 134 }
 135 
 136 void StackMapTable::print_on(outputStream* str) const {
 137   str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count);
 138   str->indent().print_cr("table = { ");
 139   {
 140     streamIndentor si(str);
 141     for (int32_t i = 0; i < _frame_count; ++i) {
 142       _frame_array[i]->print_on(str);
 143     }
 144   }
 145   str->print_cr(" }");
 146 }
 147 
 148 StackMapReader::StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
 149                                int32_t code_len, TRAPS) :
 150                                _verifier(v), _stream(stream),
 151                                _code_data(code_data), _code_length(code_len) {
 152   methodHandle m = v->method();
 153   if (m->has_stackmap_table()) {
 154     _cp = constantPoolHandle(THREAD, m->constants());
 155     _frame_count = _stream->get_u2(CHECK);
 156   } else {
 157     // There's no stackmap table present. Frame count and size are 0.
 158     _frame_count = 0;
 159   }
 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     Symbol* klass_name = _cp->klass_name_at(class_index);
 192     if (klass_name->is_Q_signature()) {
 193       Symbol* fund_name = klass_name->fundamental_name(THREAD);
 194       if (fund_name == NULL) {
 195         _stream->stackmap_format_error("TBD something bad happened", THREAD);
 196         return VerificationType::bogus_type();
 197       }
 198       return VerificationType::valuetype_type(fund_name);
 199     } else {
 200       return VerificationType::reference_type(klass_name);
 201     }
 202   }
 203   if (tag == ITEM_UninitializedThis) {
 204     if (flags != NULL) {
 205       *flags |= FLAG_THIS_UNINIT;
 206     }
 207     return VerificationType::uninitialized_this_type();
 208   }
 209   if (tag == ITEM_Uninitialized) {
 210     u2 offset = _stream->get_u2(THREAD);
 211     if (offset >= _code_length ||
 212         _code_data[offset] != ClassVerifier::NEW_OFFSET) {
 213       _verifier->class_format_error(
 214         "StackMapTable format error: bad offset for Uninitialized");
 215       return VerificationType::bogus_type();
 216     }
 217     return VerificationType::uninitialized_type(offset);
 218   }
 219   _stream->stackmap_format_error("bad verification type", THREAD);
 220   return VerificationType::bogus_type();
 221 }
 222 
 223 StackMapFrame* StackMapReader::next(
 224     StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {
 225   StackMapFrame* frame;
 226   int offset;
 227   VerificationType* locals = NULL;
 228   u1 frame_type = _stream->get_u1(THREAD);
 229   if (frame_type < 64) {
 230     // same_frame
 231     if (first) {
 232       offset = frame_type;
 233       // Can't share the locals array since that is updated by the verifier.
 234       if (pre_frame->locals_size() > 0) {
 235         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
 236           THREAD, VerificationType, pre_frame->locals_size());
 237       }
 238     } else {
 239       offset = pre_frame->offset() + frame_type + 1;
 240       locals = pre_frame->locals();
 241     }
 242     frame = new StackMapFrame(
 243       offset, pre_frame->flags(), pre_frame->locals_size(), 0,
 244       max_locals, max_stack, locals, NULL, _verifier);
 245     if (first && locals != NULL) {
 246       frame->copy_locals(pre_frame);
 247     }
 248     return frame;
 249   }
 250   if (frame_type < 128) {
 251     // same_locals_1_stack_item_frame
 252     if (first) {
 253       offset = frame_type - 64;
 254       // Can't share the locals array since that is updated by the verifier.
 255       if (pre_frame->locals_size() > 0) {
 256         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
 257           THREAD, VerificationType, pre_frame->locals_size());
 258       }
 259     } else {
 260       offset = pre_frame->offset() + frame_type - 63;
 261       locals = pre_frame->locals();
 262     }
 263     VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
 264       THREAD, VerificationType, 2);
 265     u2 stack_size = 1;
 266     stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
 267     if (stack[0].is_category2()) {
 268       stack[1] = stack[0].to_category2_2nd();
 269       stack_size = 2;
 270     }
 271     check_verification_type_array_size(
 272       stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
 273     frame = new StackMapFrame(
 274       offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
 275       max_locals, max_stack, locals, stack, _verifier);
 276     if (first && locals != NULL) {
 277       frame->copy_locals(pre_frame);
 278     }
 279     return frame;
 280   }
 281 
 282   u2 offset_delta = _stream->get_u2(THREAD);
 283 
 284   if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
 285     // reserved frame types
 286     _stream->stackmap_format_error(
 287       "reserved frame type", CHECK_VERIFY_(_verifier, NULL));
 288   }
 289 
 290   if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
 291     // same_locals_1_stack_item_frame_extended
 292     if (first) {
 293       offset = offset_delta;
 294       // Can't share the locals array since that is updated by the verifier.
 295       if (pre_frame->locals_size() > 0) {
 296         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
 297           THREAD, VerificationType, pre_frame->locals_size());
 298       }
 299     } else {
 300       offset = pre_frame->offset() + offset_delta + 1;
 301       locals = pre_frame->locals();
 302     }
 303     VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
 304       THREAD, VerificationType, 2);
 305     u2 stack_size = 1;
 306     stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
 307     if (stack[0].is_category2()) {
 308       stack[1] = stack[0].to_category2_2nd();
 309       stack_size = 2;
 310     }
 311     check_verification_type_array_size(
 312       stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
 313     frame = new StackMapFrame(
 314       offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
 315       max_locals, max_stack, locals, stack, _verifier);
 316     if (first && locals != NULL) {
 317       frame->copy_locals(pre_frame);
 318     }
 319     return frame;
 320   }
 321 
 322   if (frame_type <= SAME_EXTENDED) {
 323     // chop_frame or same_frame_extended
 324     locals = pre_frame->locals();
 325     int length = pre_frame->locals_size();
 326     int chops = SAME_EXTENDED - frame_type;
 327     int new_length = length;
 328     u1 flags = pre_frame->flags();
 329     if (chops != 0) {
 330       new_length = chop(locals, length, chops);
 331       check_verification_type_array_size(
 332         new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
 333       // Recompute flags since uninitializedThis could have been chopped.
 334       flags = 0;
 335       for (int i=0; i<new_length; i++) {
 336         if (locals[i].is_uninitialized_this()) {
 337           flags |= FLAG_THIS_UNINIT;
 338           break;
 339         }
 340       }
 341     }
 342     if (first) {
 343       offset = offset_delta;
 344       // Can't share the locals array since that is updated by the verifier.
 345       if (new_length > 0) {
 346         locals = NEW_RESOURCE_ARRAY_IN_THREAD(
 347           THREAD, VerificationType, new_length);
 348       } else {
 349         locals = NULL;
 350       }
 351     } else {
 352       offset = pre_frame->offset() + offset_delta + 1;
 353     }
 354     frame = new StackMapFrame(
 355       offset, flags, new_length, 0, max_locals, max_stack,
 356       locals, NULL, _verifier);
 357     if (first && locals != NULL) {
 358       frame->copy_locals(pre_frame);
 359     }
 360     return frame;
 361   } else if (frame_type < SAME_EXTENDED + 4) {
 362     // append_frame
 363     int appends = frame_type - SAME_EXTENDED;
 364     int real_length = pre_frame->locals_size();
 365     int new_length = real_length + appends*2;
 366     locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);
 367     VerificationType* pre_locals = pre_frame->locals();
 368     int i;
 369     for (i=0; i<pre_frame->locals_size(); i++) {
 370       locals[i] = pre_locals[i];
 371     }
 372     u1 flags = pre_frame->flags();
 373     for (i=0; i<appends; i++) {
 374       locals[real_length] = parse_verification_type(&flags, THREAD);
 375       if (locals[real_length].is_category2()) {
 376         locals[real_length + 1] = locals[real_length].to_category2_2nd();
 377         ++real_length;
 378       }
 379       ++real_length;
 380     }
 381     check_verification_type_array_size(
 382       real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
 383     if (first) {
 384       offset = offset_delta;
 385     } else {
 386       offset = pre_frame->offset() + offset_delta + 1;
 387     }
 388     frame = new StackMapFrame(
 389       offset, flags, real_length, 0, max_locals,
 390       max_stack, locals, NULL, _verifier);
 391     return frame;
 392   }
 393   if (frame_type == FULL) {
 394     // full_frame
 395     u1 flags = 0;
 396     u2 locals_size = _stream->get_u2(THREAD);
 397     int real_locals_size = 0;
 398     if (locals_size > 0) {
 399       locals = NEW_RESOURCE_ARRAY_IN_THREAD(
 400         THREAD, VerificationType, locals_size*2);
 401     }
 402     int i;
 403     for (i=0; i<locals_size; i++) {
 404       locals[real_locals_size] = parse_verification_type(&flags, THREAD);
 405       if (locals[real_locals_size].is_category2()) {
 406         locals[real_locals_size + 1] =
 407           locals[real_locals_size].to_category2_2nd();
 408         ++real_locals_size;
 409       }
 410       ++real_locals_size;
 411     }
 412     check_verification_type_array_size(
 413       real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));
 414     u2 stack_size = _stream->get_u2(THREAD);
 415     int real_stack_size = 0;
 416     VerificationType* stack = NULL;
 417     if (stack_size > 0) {
 418       stack = NEW_RESOURCE_ARRAY_IN_THREAD(
 419         THREAD, VerificationType, stack_size*2);
 420     }
 421     for (i=0; i<stack_size; i++) {
 422       stack[real_stack_size] = parse_verification_type(NULL, THREAD);
 423       if (stack[real_stack_size].is_category2()) {
 424         stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();
 425         ++real_stack_size;
 426       }
 427       ++real_stack_size;
 428     }
 429     check_verification_type_array_size(
 430       real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
 431     if (first) {
 432       offset = offset_delta;
 433     } else {
 434       offset = pre_frame->offset() + offset_delta + 1;
 435     }
 436     frame = new StackMapFrame(
 437       offset, flags, real_locals_size, real_stack_size,
 438       max_locals, max_stack, locals, stack, _verifier);
 439     return frame;
 440   }
 441 
 442   _stream->stackmap_format_error(
 443     "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));
 444   return NULL;
 445 }