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