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