1 /* 2 * Copyright (c) 2003, 2025, 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_CLASSFILE_STACKMAPTABLE_HPP 26 #define SHARE_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 GrowableArray<StackMapFrame*>* _frame_array; 48 49 public: 50 StackMapTable(StackMapReader* reader, TRAPS); 51 52 inline int32_t get_frame_count() const { return _frame_count; } 53 inline int get_offset(int index) const { 54 return _frame_array->at(index)->offset(); 55 } 56 57 // Match and/or update current_frame to the frame in stackmap table with 58 // specified offset. Return true if the two frames match. 59 bool match_stackmap( 60 StackMapFrame* current_frame, int32_t offset, 61 bool match, bool update, ErrorContext* ctx, TRAPS) const; 62 // Match and/or update current_frame to the frame in stackmap table with 63 // specified offset and frame index. Return true if the two frames match. 64 bool match_stackmap( 65 StackMapFrame* current_frame, int32_t offset, int32_t frame_index, 66 bool match, bool update, ErrorContext* ctx, TRAPS) const; 67 68 // Check jump instructions. Make sure there are no uninitialized 69 // instances on backward branch. 70 void check_jump_target(StackMapFrame* frame, int32_t target, TRAPS) const; 71 72 // The following methods are only used inside this class. 73 74 // Returns the frame array index where the frame with offset is stored. 75 int get_index_from_offset(int32_t offset) const; 76 77 void print_on(outputStream* str) const; 78 }; 79 80 class StackMapStream : StackObj { 81 private: 82 Array<u1>* _data; 83 int _index; 84 public: 85 StackMapStream(Array<u1>* ah) 86 : _data(ah), _index(0) { 87 } 88 u1 get_u1(TRAPS) { 89 if (_data == nullptr || _index >= _data->length()) { 90 stackmap_format_error("access beyond the end of attribute", CHECK_0); 91 } 92 return _data->at(_index++); 93 } 94 u2 get_u2(TRAPS) { 95 if (_data == nullptr || _index >= _data->length() - 1) { 96 stackmap_format_error("access beyond the end of attribute", CHECK_0); 97 } 98 u2 res = Bytes::get_Java_u2(_data->adr_at(_index)); 99 _index += 2; 100 return res; 101 } 102 bool at_end() { 103 return (_data == nullptr) || (_index == _data->length()); 104 } 105 static void stackmap_format_error(const char* msg, TRAPS); 106 }; 107 108 class StackMapReader : StackObj { 109 private: 110 // information about the class and method 111 constantPoolHandle _cp; 112 ClassVerifier* _verifier; 113 StackMapStream* _stream; 114 char* _code_data; 115 int32_t _code_length; 116 117 // information from the attribute 118 int32_t _frame_count; 119 120 // Number of frames parsed 121 int32_t _parsed_frame_count; 122 123 // Previous frame buffer 124 StackMapFrame* _prev_frame; 125 126 // information from method 127 u2 _max_locals; 128 u2 _max_stack; 129 130 // Contains assert_unset_fields generated from classfile 131 StackMapFrame::AssertUnsetFieldTable* _assert_unset_fields_buffer; 132 133 // Check if reading first entry 134 bool _first; 135 136 StackMapFrame* next_helper(TRAPS); 137 void check_offset(StackMapFrame* frame); 138 void check_size(TRAPS); 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 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 ASSERT_UNSET_FIELDS = 246, 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, 163 char* code_data, int32_t code_len, 164 StackMapFrame* init_frame, 165 u2 max_locals, u2 max_stack, 166 StackMapFrame::AssertUnsetFieldTable* initial_strict_fields, TRAPS); 167 168 inline int32_t get_frame_count() const { return _frame_count; } 169 inline StackMapFrame* prev_frame() const { return _prev_frame; } 170 inline char* code_data() const { return _code_data; } 171 inline int32_t code_length() const { return _code_length; } 172 inline bool at_end() const { return _stream->at_end(); } 173 174 StackMapFrame* next(TRAPS); 175 void check_end(TRAPS); 176 }; 177 178 #endif // SHARE_CLASSFILE_STACKMAPTABLE_HPP