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 #ifndef SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP
  26 #define SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP
  27 
  28 #include "classfile/verificationType.hpp"
  29 #include "classfile/verifier.hpp"
  30 #include "oops/methodOop.hpp"
  31 #include "runtime/handles.hpp"
  32 #include "runtime/signature.hpp"
  33 #include "utilities/exceptions.hpp"
  34 
  35 // A StackMapFrame represents one frame in the stack map attribute.
  36 
  37 enum {
  38   FLAG_THIS_UNINIT = 0x01
  39 };
  40 
  41 class StackMapFrame : public ResourceObj {
  42  private:
  43   int32_t _offset;
  44 
  45   // See comment in StackMapTable about _frame_count about why these
  46   // fields are int32_t instead of u2.
  47   int32_t _locals_size;  // number of valid type elements in _locals
  48   int32_t _stack_size;   // number of valid type elements in _stack
  49 
  50   int32_t _max_locals;
  51   int32_t _max_stack;
  52 
  53   u1 _flags;
  54   VerificationType* _locals; // local variable type array
  55   VerificationType* _stack;  // operand stack type array
  56 
  57   ClassVerifier* _verifier;  // the verifier verifying this method
  58 
  59  public:
  60   // constructors
  61 
  62   // This constructor is used by the type checker to allocate frames
  63   // in type state, which have _max_locals and _max_stack array elements
  64   // in _locals and _stack.
  65   StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* verifier);
  66 
  67   // This constructor is used to initialize stackmap frames in stackmap table,
  68   // which have _locals_size and _stack_size array elements in _locals and _stack.
  69   StackMapFrame(int32_t offset,
  70                 u1 flags,
  71                 u2 locals_size,
  72                 u2 stack_size,
  73                 u2 max_locals,
  74                 u2 max_stack,
  75                 VerificationType* locals,
  76                 VerificationType* stack,
  77                 ClassVerifier* v) : _offset(offset), _flags(flags),
  78                                     _locals_size(locals_size),
  79                                     _stack_size(stack_size),
  80                                     _max_locals(max_locals),
  81                                     _max_stack(max_stack),
  82                                     _locals(locals), _stack(stack),
  83                                     _verifier(v) { }
  84 
  85   inline void set_offset(int32_t offset)      { _offset = offset; }
  86   inline void set_verifier(ClassVerifier* v)  { _verifier = v; }
  87   inline void set_flags(u1 flags)             { _flags = flags; }
  88   inline void set_locals_size(u2 locals_size) { _locals_size = locals_size; }
  89   inline void set_stack_size(u2 stack_size)   { _stack_size = stack_size; }
  90   inline void clear_stack()                   { _stack_size = 0; }
  91   inline int32_t offset()   const             { return _offset; }
  92   inline ClassVerifier* verifier() const      { return _verifier; }
  93   inline u1 flags() const                     { return _flags; }
  94   inline int32_t locals_size() const          { return _locals_size; }
  95   inline VerificationType* locals() const     { return _locals; }
  96   inline int32_t stack_size() const           { return _stack_size; }
  97   inline VerificationType* stack() const      { return _stack; }
  98   inline int32_t max_locals() const           { return _max_locals; }
  99   inline int32_t max_stack() const            { return _max_stack; }
 100   inline bool flag_this_uninit() const        { return _flags & FLAG_THIS_UNINIT; }
 101 
 102   // Set locals and stack types to bogus
 103   inline void reset() {
 104     int32_t i;
 105     for (i = 0; i < _max_locals; i++) {
 106       _locals[i] = VerificationType::bogus_type();
 107     }
 108     for (i = 0; i < _max_stack; i++) {
 109       _stack[i] = VerificationType::bogus_type();
 110     }
 111   }
 112 
 113   // Return a StackMapFrame with the same local variable array and empty stack.
 114   // Stack array is allocate with unused one element.
 115   StackMapFrame* frame_in_exception_handler(u1 flags);
 116 
 117   // Set local variable type array based on m's signature.
 118   VerificationType set_locals_from_arg(
 119     const methodHandle m, VerificationType thisKlass, TRAPS);
 120 
 121   // Search local variable type array and stack type array.
 122   // Return true if an uninitialized object is found.
 123   bool has_new_object() const;
 124 
 125   // Search local variable type array and stack type array.
 126   // Set every element with type of old_object to new_object.
 127   void initialize_object(
 128     VerificationType old_object, VerificationType new_object);
 129 
 130   // Copy local variable type array in src into this local variable type array.
 131   void copy_locals(const StackMapFrame* src);
 132 
 133   // Copy stack type array in src into this stack type array.
 134   void copy_stack(const StackMapFrame* src);
 135 
 136   // Return true if this stack map frame is assignable to target.
 137   bool is_assignable_to(const StackMapFrame* target, TRAPS) const;
 138 
 139   // Push type into stack type array.
 140   inline void push_stack(VerificationType type, TRAPS) {
 141     assert(!type.is_check(), "Must be a real type");
 142     if (_stack_size >= _max_stack) {
 143       verifier()->verify_error(_offset, "Operand stack overflow");
 144       return;
 145     }
 146     _stack[_stack_size++] = type;
 147   }
 148 
 149   inline void push_stack_2(
 150       VerificationType type1, VerificationType type2, TRAPS) {
 151     assert(type1.is_long() || type1.is_double(), "must be long/double");
 152     assert(type2.is_long2() || type2.is_double2(), "must be long/double_2");
 153     if (_stack_size >= _max_stack - 1) {
 154       verifier()->verify_error(_offset, "Operand stack overflow");
 155       return;
 156     }
 157     _stack[_stack_size++] = type1;
 158     _stack[_stack_size++] = type2;
 159   }
 160 
 161   // Pop and return the top type on stack without verifying.
 162   inline VerificationType pop_stack(TRAPS) {
 163     if (_stack_size <= 0) {
 164       verifier()->verify_error(_offset, "Operand stack underflow");
 165       return VerificationType::bogus_type();
 166     }
 167     // Put bogus type to indicate it's no longer valid.
 168     // Added to make it consistent with the other pop_stack method.
 169     VerificationType top = _stack[--_stack_size];
 170     NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); )
 171     return top;
 172   }
 173 
 174   // Pop and return the top type on stack type array after verifying it
 175   // is assignable to type.
 176   inline VerificationType pop_stack(VerificationType type, TRAPS) {
 177     if (_stack_size != 0) {
 178       VerificationType top = _stack[_stack_size - 1];
 179       bool subtype = type.is_assignable_from(
 180         top, verifier(), CHECK_(VerificationType::bogus_type()));
 181       if (subtype) {
 182         _stack_size --;
 183         NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); )
 184         return top;
 185       }
 186     }
 187     return pop_stack_ex(type, THREAD);
 188   }
 189 
 190   inline void pop_stack_2(
 191       VerificationType type1, VerificationType type2, TRAPS) {
 192     assert(type1.is_long2() || type1.is_double2(), "must be long/double");
 193     assert(type2.is_long() || type2.is_double(), "must be long/double_2");
 194     if (_stack_size >= 2) {
 195       VerificationType top1 = _stack[_stack_size - 1];
 196       bool subtype1 = type1.is_assignable_from(top1, verifier(), CHECK);
 197       VerificationType top2 = _stack[_stack_size - 2];
 198       bool subtype2 = type2.is_assignable_from(top2, verifier(), CHECK);
 199       if (subtype1 && subtype2) {
 200         _stack_size -= 2;
 201         NOT_PRODUCT( _stack[_stack_size] = VerificationType::bogus_type(); )
 202         NOT_PRODUCT( _stack[_stack_size+1] = VerificationType::bogus_type(); )
 203         return;
 204       }
 205     }
 206     pop_stack_ex(type1, THREAD);
 207     pop_stack_ex(type2, THREAD);
 208   }
 209 
 210   // Uncommon case that throws exceptions.
 211   VerificationType pop_stack_ex(VerificationType type, TRAPS);
 212 
 213   // Return the type at index in local variable array after verifying
 214   // it is assignable to type.
 215   VerificationType get_local(int32_t index, VerificationType type, TRAPS);
 216   // For long/double.
 217   void get_local_2(
 218     int32_t index, VerificationType type1, VerificationType type2, TRAPS);
 219 
 220   // Set element at index in local variable array to type.
 221   void set_local(int32_t index, VerificationType type, TRAPS);
 222   // For long/double.
 223   void set_local_2(
 224     int32_t index, VerificationType type1, VerificationType type2, TRAPS);
 225 
 226   // Private auxiliary method used only in is_assignable_to(StackMapFrame).
 227   // Returns true if src is assignable to target.
 228   bool is_assignable_to(
 229     VerificationType* src, VerificationType* target, int32_t len, TRAPS) const;
 230 
 231   bool has_flag_match_exception(const StackMapFrame* target) const;
 232 
 233   // Debugging
 234   void print() const PRODUCT_RETURN;
 235 };
 236 
 237 #endif // SHARE_VM_CLASSFILE_STACKMAPFRAME_HPP