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/stackMapFrame.hpp"
  27 #include "classfile/verifier.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "oops/symbolOop.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) :
  35                       _offset(0), _locals_size(0), _stack_size(0), _flags(0),
  36                       _max_locals(max_locals), _max_stack(max_stack),
  37                       _verifier(v) {
  38   Thread* thr = v->thread();
  39   _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals);
  40   _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack);
  41   int32_t i;
  42   for(i = 0; i < max_locals; i++) {
  43     _locals[i] = VerificationType::bogus_type();
  44   }
  45   for(i = 0; i < max_stack; i++) {
  46     _stack[i] = VerificationType::bogus_type();
  47   }
  48 }
  49 
  50 StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) {
  51   Thread* thr = _verifier->thread();
  52   VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1);
  53   StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier);
  54   return frame;
  55 }
  56 
  57 bool StackMapFrame::has_new_object() const {
  58   int32_t i;
  59   for (i = 0; i < _max_locals; i++) {
  60     if (_locals[i].is_uninitialized()) {
  61       return true;
  62     }
  63   }
  64   for (i = 0; i < _stack_size; i++) {
  65     if (_stack[i].is_uninitialized()) {
  66       return true;
  67     }
  68   }
  69   return false;
  70 }
  71 
  72 void StackMapFrame::initialize_object(
  73     VerificationType old_object, VerificationType new_object) {
  74   int32_t i;
  75   for (i = 0; i < _max_locals; i++) {
  76     if (_locals[i].equals(old_object)) {
  77       _locals[i] = new_object;
  78     }
  79   }
  80   for (i = 0; i < _stack_size; i++) {
  81     if (_stack[i].equals(old_object)) {
  82       _stack[i] = new_object;
  83     }
  84   }
  85   if (old_object == VerificationType::uninitialized_this_type()) {
  86     // "this" has been initialized - reset flags
  87     _flags = 0;
  88   }
  89 }
  90 
  91 VerificationType StackMapFrame::set_locals_from_arg(
  92     const methodHandle m, VerificationType thisKlass, TRAPS) {
  93   symbolHandle signature(THREAD, m->signature());
  94   SignatureStream ss(signature);
  95   int init_local_num = 0;
  96   if (!m->is_static()) {
  97     init_local_num++;
  98     // add one extra argument for instance method
  99     if (m->name() == vmSymbols::object_initializer_name() &&
 100        thisKlass.name() != vmSymbols::java_lang_Object()) {
 101       _locals[0] = VerificationType::uninitialized_this_type();
 102       _flags |= FLAG_THIS_UNINIT;
 103     } else {
 104       _locals[0] = thisKlass;
 105     }
 106   }
 107 
 108   // local num may be greater than size of parameters because long/double occupies two slots
 109   while(!ss.at_return_type()) {
 110     init_local_num += _verifier->change_sig_to_verificationType(
 111       &ss, &_locals[init_local_num],
 112       CHECK_VERIFY_(verifier(), VerificationType::bogus_type()));
 113     ss.next();
 114   }
 115   _locals_size = init_local_num;
 116 
 117   switch (ss.type()) {
 118     case T_OBJECT:
 119     case T_ARRAY:
 120     {
 121       symbolOop sig = ss.as_symbol(CHECK_(VerificationType::bogus_type()));
 122       return VerificationType::reference_type(symbolHandle(THREAD, sig));
 123     }
 124     case T_INT:     return VerificationType::integer_type();
 125     case T_BYTE:    return VerificationType::byte_type();
 126     case T_CHAR:    return VerificationType::char_type();
 127     case T_SHORT:   return VerificationType::short_type();
 128     case T_BOOLEAN: return VerificationType::boolean_type();
 129     case T_FLOAT:   return VerificationType::float_type();
 130     case T_DOUBLE:  return VerificationType::double_type();
 131     case T_LONG:    return VerificationType::long_type();
 132     case T_VOID:    return VerificationType::bogus_type();
 133     default:
 134       ShouldNotReachHere();
 135   }
 136   return VerificationType::bogus_type();
 137 }
 138 
 139 void StackMapFrame::copy_locals(const StackMapFrame* src) {
 140   int32_t len = src->locals_size() < _locals_size ?
 141     src->locals_size() : _locals_size;
 142   for (int32_t i = 0; i < len; i++) {
 143     _locals[i] = src->locals()[i];
 144   }
 145 }
 146 
 147 void StackMapFrame::copy_stack(const StackMapFrame* src) {
 148   int32_t len = src->stack_size() < _stack_size ?
 149     src->stack_size() : _stack_size;
 150   for (int32_t i = 0; i < len; i++) {
 151     _stack[i] = src->stack()[i];
 152   }
 153 }
 154 
 155 
 156 bool StackMapFrame::is_assignable_to(
 157     VerificationType* from, VerificationType* to, int32_t len, TRAPS) const {
 158   for (int32_t i = 0; i < len; i++) {
 159     bool subtype = to[i].is_assignable_from(
 160       from[i], verifier()->current_class(), THREAD);
 161     if (!subtype) {
 162       return false;
 163     }
 164   }
 165   return true;
 166 }
 167 
 168 bool StackMapFrame::is_assignable_to(const StackMapFrame* target, TRAPS) const {
 169   if (_max_locals != target->max_locals() || _stack_size != target->stack_size()) {
 170     return false;
 171   }
 172   // Only need to compare type elements up to target->locals() or target->stack().
 173   // The remaining type elements in this state can be ignored because they are
 174   // assignable to bogus type.
 175   bool match_locals = is_assignable_to(
 176     _locals, target->locals(), target->locals_size(), CHECK_false);
 177   bool match_stack = is_assignable_to(
 178     _stack, target->stack(), _stack_size, CHECK_false);
 179   bool match_flags = (_flags | target->flags()) == target->flags();
 180   return (match_locals && match_stack && match_flags);
 181 }
 182 
 183 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) {
 184   if (_stack_size <= 0) {
 185     verifier()->verify_error(_offset, "Operand stack underflow");
 186     return VerificationType::bogus_type();
 187   }
 188   VerificationType top = _stack[--_stack_size];
 189   bool subtype = type.is_assignable_from(
 190     top, verifier()->current_class(), CHECK_(VerificationType::bogus_type()));
 191   if (!subtype) {
 192     verifier()->verify_error(_offset, "Bad type on operand stack");
 193     return VerificationType::bogus_type();
 194   }
 195   NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); )
 196   return top;
 197 }
 198 
 199 VerificationType StackMapFrame::get_local(
 200     int32_t index, VerificationType type, TRAPS) {
 201   if (index >= _max_locals) {
 202     verifier()->verify_error(_offset, "Local variable table overflow");
 203     return VerificationType::bogus_type();
 204   }
 205   bool subtype = type.is_assignable_from(_locals[index],
 206     verifier()->current_class(), CHECK_(VerificationType::bogus_type()));
 207   if (!subtype) {
 208     verifier()->verify_error(_offset, "Bad local variable type");
 209     return VerificationType::bogus_type();
 210   }
 211   if(index >= _locals_size) { _locals_size = index + 1; }
 212   return _locals[index];
 213 }
 214 
 215 void StackMapFrame::get_local_2(
 216     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
 217   assert(type1.is_long() || type1.is_double(), "must be long/double");
 218   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
 219   if (index >= _locals_size - 1) {
 220     verifier()->verify_error(_offset, "get long/double overflows locals");
 221     return;
 222   }
 223   bool subtype1 = type1.is_assignable_from(
 224     _locals[index], verifier()->current_class(), CHECK);
 225   bool subtype2 = type2.is_assignable_from(
 226     _locals[index+1], verifier()->current_class(), CHECK);
 227   if (!subtype1 || !subtype2) {
 228     verifier()->verify_error(_offset, "Bad local variable type");
 229     return;
 230   }
 231 }
 232 
 233 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) {
 234   assert(!type.is_check(), "Must be a real type");
 235   if (index >= _max_locals) {
 236     verifier()->verify_error("Local variable table overflow", _offset);
 237     return;
 238   }
 239   // If type at index is double or long, set the next location to be unusable
 240   if (_locals[index].is_double() || _locals[index].is_long()) {
 241     assert((index + 1) < _locals_size, "Local variable table overflow");
 242     _locals[index + 1] = VerificationType::bogus_type();
 243   }
 244   // If type at index is double_2 or long_2, set the previous location to be unusable
 245   if (_locals[index].is_double2() || _locals[index].is_long2()) {
 246     assert(index >= 1, "Local variable table underflow");
 247     _locals[index - 1] = VerificationType::bogus_type();
 248   }
 249   _locals[index] = type;
 250   if (index >= _locals_size) {
 251 #ifdef ASSERT
 252     for (int i=_locals_size; i<index; i++) {
 253       assert(_locals[i] == VerificationType::bogus_type(),
 254              "holes must be bogus type");
 255     }
 256 #endif
 257     _locals_size = index + 1;
 258   }
 259 }
 260 
 261 void StackMapFrame::set_local_2(
 262     int32_t index, VerificationType type1, VerificationType type2, TRAPS) {
 263   assert(type1.is_long() || type1.is_double(), "must be long/double");
 264   assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
 265   if (index >= _max_locals - 1) {
 266     verifier()->verify_error("Local variable table overflow", _offset);
 267     return;
 268   }
 269   // If type at index+1 is double or long, set the next location to be unusable
 270   if (_locals[index+1].is_double() || _locals[index+1].is_long()) {
 271     assert((index + 2) < _locals_size, "Local variable table overflow");
 272     _locals[index + 2] = VerificationType::bogus_type();
 273   }
 274   // If type at index is double_2 or long_2, set the previous location to be unusable
 275   if (_locals[index].is_double2() || _locals[index].is_long2()) {
 276     assert(index >= 1, "Local variable table underflow");
 277     _locals[index - 1] = VerificationType::bogus_type();
 278   }
 279   _locals[index] = type1;
 280   _locals[index+1] = type2;
 281   if (index >= _locals_size - 1) {
 282 #ifdef ASSERT
 283     for (int i=_locals_size; i<index; i++) {
 284       assert(_locals[i] == VerificationType::bogus_type(),
 285              "holes must be bogus type");
 286     }
 287 #endif
 288     _locals_size = index + 2;
 289   }
 290 }
 291 
 292 #ifndef PRODUCT
 293 
 294 void StackMapFrame::print() const {
 295   tty->print_cr("stackmap_frame[%d]:", _offset);
 296   tty->print_cr("flags = 0x%x", _flags);
 297   tty->print("locals[%d] = { ", _locals_size);
 298   for (int32_t i = 0; i < _locals_size; i++) {
 299     _locals[i].print_on(tty);
 300   }
 301   tty->print_cr(" }");
 302   tty->print("stack[%d] = { ", _stack_size);
 303   for (int32_t j = 0; j < _stack_size; j++) {
 304     _stack[j].print_on(tty);
 305   }
 306   tty->print_cr(" }");
 307 }
 308 
 309 #endif