1 | /* |
2 | * Copyright (c) 2003, 2019, 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 | 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, 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, 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 | void print_on(outputStream* str) const; |
80 | }; |
81 | |
82 | class StackMapStream : StackObj { |
83 | private: |
84 | Array<u1>* _data; |
85 | int _index; |
86 | public: |
87 | StackMapStream(Array<u1>* ah) |
88 | : _data(ah), _index(0) { |
89 | } |
90 | u1 get_u1(TRAPS) { |
91 | if (_data == NULL || _index >= _data->length()) { |
92 | stackmap_format_error("access beyond the end of attribute" , CHECK_0); |
93 | } |
94 | return _data->at(_index++); |
95 | } |
96 | u2 get_u2(TRAPS) { |
97 | if (_data == NULL || _index >= _data->length() - 1) { |
98 | stackmap_format_error("access beyond the end of attribute" , CHECK_0); |
99 | } |
100 | u2 res = Bytes::get_Java_u2(_data->adr_at(_index)); |
101 | _index += 2; |
102 | return res; |
103 | } |
104 | bool at_end() { |
105 | return (_data == NULL) || (_index == _data->length()); |
106 | } |
107 | static void stackmap_format_error(const char* msg, TRAPS); |
108 | }; |
109 | |
110 | class StackMapReader : StackObj { |
111 | private: |
112 | // information about the class and method |
113 | constantPoolHandle _cp; |
114 | ClassVerifier* _verifier; |
115 | StackMapStream* _stream; |
116 | char* _code_data; |
117 | int32_t _code_length; |
118 | |
119 | // information get from the attribute |
120 | int32_t _frame_count; // frame count |
121 | |
122 | int32_t chop(VerificationType* locals, int32_t length, int32_t chops); |
123 | VerificationType parse_verification_type(u1* flags, TRAPS); |
124 | void check_verification_type_array_size( |
125 | int32_t size, int32_t max_size, TRAPS) { |
126 | if (size < 0 || size > max_size) { |
127 | // Since this error could be caused someone rewriting the method |
128 | // but not knowing to update the stackmap data, we call the the |
129 | // verifier's error method, which may not throw an exception and |
130 | // failover to the old verifier instead. |
131 | _verifier->class_format_error( |
132 | "StackMapTable format error: bad type array size" ); |
133 | } |
134 | } |
135 | |
136 | enum { |
137 | SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247, |
138 | SAME_EXTENDED = 251, |
139 | FULL = 255 |
140 | }; |
141 | |
142 | public: |
143 | // Constructor |
144 | StackMapReader(ClassVerifier* v, StackMapStream* stream, char* code_data, |
145 | int32_t code_len, TRAPS); |
146 | |
147 | inline int32_t get_frame_count() const { return _frame_count; } |
148 | StackMapFrame* next(StackMapFrame* pre_frame, bool first, |
149 | u2 max_locals, u2 max_stack, TRAPS); |
150 | |
151 | void check_end(TRAPS) { |
152 | if (!_stream->at_end()) { |
153 | StackMapStream::stackmap_format_error("wrong attribute size" , CHECK); |
154 | } |
155 | } |
156 | }; |
157 | |
158 | #endif // SHARE_CLASSFILE_STACKMAPTABLE_HPP |
159 | |