1 /*
   2  * Copyright (c) 2003, 2011, 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_STACKMAPTABLE_HPP
  26 #define SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP
  27 
  28 #include "classfile/stackMapFrame.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "oops/constantPoolOop.hpp"
  31 #include "oops/methodOop.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 #ifdef TARGET_ARCH_x86
  34 # include "bytes_x86.hpp"
  35 #endif
  36 #ifdef TARGET_ARCH_sparc
  37 # include "bytes_sparc.hpp"
  38 #endif
  39 #ifdef TARGET_ARCH_zero
  40 # include "bytes_zero.hpp"
  41 #endif
  42 #ifdef TARGET_ARCH_arm
  43 # include "bytes_arm.hpp"
  44 #endif
  45 #ifdef TARGET_ARCH_ppc
  46 # include "bytes_ppc.hpp"
  47 #endif
  48 
  49 class StackMapReader;
  50 
  51 // StackMapTable class is the StackMap table used by type checker
  52 class StackMapTable : public StackObj {
  53  private:
  54   // Logically, the _frame_count (as well as many fields in the StackFrame)
  55   // should be a u2, but if we defined the variable as that type it will
  56   // be difficult to detect/recover from overflow or underflow conditions.
  57   // Widening the type and making it signed will help detect these.
  58   int32_t              _code_length;
  59   int32_t              _frame_count;     // Stackmap frame count
  60   StackMapFrame**       _frame_array;
  61 
  62  public:
  63   StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
  64                 u2 max_locals, u2 max_stack,
  65                 char* code_data, int code_len, TRAPS);
  66 
  67   inline int32_t get_frame_count() const { return _frame_count; }
  68   inline int get_offset(int index) const {
  69     return _frame_array[index]->offset();
  70   }
  71 
  72   // Match and/or update current_frame to the frame in stackmap table with
  73   // specified offset. Return true if the two frames match.
  74   bool match_stackmap(
  75     StackMapFrame* current_frame, int32_t offset,
  76     bool match, bool update, TRAPS) const;
  77   // Match and/or update current_frame to the frame in stackmap table with
  78   // specified offset and frame index. Return true if the two frames match.
  79   bool match_stackmap(
  80     StackMapFrame* current_frame, int32_t offset, int32_t frame_index,
  81     bool match, bool update, TRAPS) const;
  82 
  83   // Check jump instructions. Make sure there are no uninitialized
  84   // instances on backward branch.
  85   void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const;
  86 
  87   // The following methods are only used inside this class.
  88 
  89   // Returns the frame array index where the frame with offset is stored.
  90   int get_index_from_offset(int32_t offset) const;
  91 
  92   // Make sure that there's no uninitialized object exist on backward branch.
  93   void check_new_object(
  94     const StackMapFrame* frame, int32_t target, TRAPS) const;
  95 
  96   // Debugging
  97   void print() const PRODUCT_RETURN;
  98 };
  99 
 100 class StackMapStream : StackObj {
 101  private:
 102   typeArrayHandle _data;
 103   int _index;
 104  public:
 105   StackMapStream(typeArrayHandle ah)
 106     : _data(ah), _index(0) {
 107   }
 108   u1 get_u1(TRAPS) {
 109     if (_data == NULL || _index >= _data->length()) {
 110       stackmap_format_error("access beyond the end of attribute", CHECK_0);
 111     }
 112     return _data->byte_at(_index++);
 113   }
 114   u2 get_u2(TRAPS) {
 115     if (_data == NULL || _index >= _data->length() - 1) {
 116       stackmap_format_error("access beyond the end of attribute", CHECK_0);
 117     }
 118     u2 res = Bytes::get_Java_u2((u1*)_data->byte_at_addr(_index));
 119     _index += 2;
 120     return res;
 121   }
 122   bool at_end() {
 123     return (_data == NULL) || (_index == _data->length());
 124   }
 125   static void stackmap_format_error(const char* msg, TRAPS);
 126 };
 127 
 128 class StackMapReader : StackObj {
 129  private:
 130   // information about the class and method
 131   constantPoolHandle  _cp;
 132   ClassVerifier* _verifier;
 133   StackMapStream* _stream;
 134   char* _code_data;
 135   int32_t _code_length;
 136 
 137   // information get from the attribute
 138   int32_t  _frame_count;       // frame count
 139 
 140   int32_t chop(VerificationType* locals, int32_t length, int32_t chops);
 141   VerificationType parse_verification_type(u1* flags, TRAPS);
 142   void check_verification_type_array_size(
 143       int32_t size, int32_t max_size, TRAPS) {
 144     if (size < 0 || size > max_size) {
 145       // Since this error could be caused someone rewriting the method
 146       // but not knowing to update the stackmap data, we call the the
 147       // verifier's error method, which may not throw an exception and
 148       // failover to the old verifier instead.
 149       _verifier->class_format_error(
 150         "StackMapTable format error: bad type array size");
 151     }
 152   }
 153 
 154   enum {
 155     SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247,
 156     SAME_EXTENDED = 251,
 157     FULL = 255
 158   };
 159 
 160  public:
 161   // Constructor
 162   StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data,
 163                  int32_t code_len, TRAPS) :
 164                  _verifier(v), _stream(stream),
 165                  _code_data(code_data), _code_length(code_len) {
 166     methodHandle m = v->method();
 167     if (m->has_stackmap_table()) {
 168       _cp = constantPoolHandle(THREAD, m->constants());
 169       _frame_count = _stream->get_u2(CHECK);
 170     } else {
 171       // There's no stackmap table present. Frame count and size are 0.
 172       _frame_count = 0;
 173     }
 174   }
 175 
 176   inline int32_t get_frame_count() const                { return _frame_count; }
 177   StackMapFrame* next(StackMapFrame* pre_frame, bool first,
 178                       u2 max_locals, u2 max_stack, TRAPS);
 179 
 180   void check_end(TRAPS) {
 181     if (!_stream->at_end()) {
 182       StackMapStream::stackmap_format_error("wrong attribute size", CHECK);
 183     }
 184   }
 185 };
 186 
 187 #endif // SHARE_VM_CLASSFILE_STACKMAPTABLE_HPP