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, int bci, int offset, 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 friend class VM_RedefineClasses;
110 private:
111 // information about the class and method
112 constantPoolHandle _cp;
113 ClassVerifier* _verifier;
114 StackMapStream* _stream;
115 char* _code_data;
116 int32_t _code_length;
117
118 // information from the attribute
119 int32_t _frame_count;
120
121 // Number of frames parsed
122 int32_t _parsed_frame_count;
123
124 // Previous frame buffer
125 StackMapFrame* _prev_frame;
126
127 // information from method
128 u2 _max_locals;
129 u2 _max_stack;
130
131 // Contains assert_unset_fields generated from classfile
132 StackMapFrame::AssertUnsetFieldTable* _assert_unset_fields_buffer;
133
134 // Check if reading first entry
135 bool _first;
136
137 StackMapFrame* next_helper(TRAPS);
138 void check_offset(StackMapFrame* frame);
139 void check_size(TRAPS);
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
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_FRAME_START = 0,
156 SAME_FRAME_END = 63,
157 SAME_LOCALS_1_STACK_ITEM_FRAME_START = 64,
158 SAME_LOCALS_1_STACK_ITEM_FRAME_END = 127,
159 RESERVED_START = 128,
160 RESERVED_END = 245,
161 EARLY_LARVAL = 246,
162 SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247,
163 CHOP_FRAME_START = 248,
164 CHOP_FRAME_END = 250,
165 SAME_FRAME_EXTENDED = 251,
166 APPEND_FRAME_START = 252,
167 APPEND_FRAME_END = 254,
168 FULL_FRAME = 255
169 };
170
171 public:
172 // Constructor
173 StackMapReader(ClassVerifier* v, StackMapStream* stream,
174 char* code_data, int32_t code_len,
175 StackMapFrame* init_frame,
176 u2 max_locals, u2 max_stack,
177 StackMapFrame::AssertUnsetFieldTable* initial_strict_fields, TRAPS);
178
179 inline int32_t get_frame_count() const { return _frame_count; }
180 inline StackMapFrame* prev_frame() const { return _prev_frame; }
181 inline char* code_data() const { return _code_data; }
182 inline int32_t code_length() const { return _code_length; }
183 inline bool at_end() const { return _stream->at_end(); }
184
185 StackMapFrame* next(TRAPS);
186 void check_end(TRAPS);
187 };
188
189 #endif // SHARE_CLASSFILE_STACKMAPTABLE_HPP