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