|
1 /* |
|
2 * Copyright (c) 2003, 2014, 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 #include "precompiled.hpp" |
|
26 #include "classfile/stackMapFrame.hpp" |
|
27 #include "classfile/verifier.hpp" |
|
28 #include "memory/resourceArea.hpp" |
|
29 #include "oops/oop.inline.hpp" |
|
30 #include "oops/symbol.hpp" |
|
31 #include "runtime/handles.inline.hpp" |
|
32 #include "utilities/globalDefinitions.hpp" |
|
33 |
|
34 StackMapFrame::StackMapFrame(u2 max_locals, u2 max_stack, ClassVerifier* v) : |
|
35 _offset(0), _locals_size(0), _stack_size(0), |
|
36 _stack_mark(0), _flags(0), _max_locals(max_locals), |
|
37 _max_stack(max_stack), _verifier(v) { |
|
38 Thread* thr = v->thread(); |
|
39 _locals = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_locals); |
|
40 _stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, max_stack); |
|
41 int32_t i; |
|
42 for(i = 0; i < max_locals; i++) { |
|
43 _locals[i] = VerificationType::bogus_type(); |
|
44 } |
|
45 for(i = 0; i < max_stack; i++) { |
|
46 _stack[i] = VerificationType::bogus_type(); |
|
47 } |
|
48 } |
|
49 |
|
50 StackMapFrame* StackMapFrame::frame_in_exception_handler(u1 flags) { |
|
51 Thread* thr = _verifier->thread(); |
|
52 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(thr, VerificationType, 1); |
|
53 StackMapFrame* frame = new StackMapFrame(_offset, flags, _locals_size, 0, _max_locals, _max_stack, _locals, stack, _verifier); |
|
54 return frame; |
|
55 } |
|
56 |
|
57 void StackMapFrame::initialize_object( |
|
58 VerificationType old_object, VerificationType new_object) { |
|
59 int32_t i; |
|
60 for (i = 0; i < _max_locals; i++) { |
|
61 if (_locals[i].equals(old_object)) { |
|
62 _locals[i] = new_object; |
|
63 } |
|
64 } |
|
65 for (i = 0; i < _stack_size; i++) { |
|
66 if (_stack[i].equals(old_object)) { |
|
67 _stack[i] = new_object; |
|
68 } |
|
69 } |
|
70 if (old_object == VerificationType::uninitialized_this_type()) { |
|
71 // "this" has been initialized - reset flags |
|
72 _flags = 0; |
|
73 } |
|
74 } |
|
75 |
|
76 VerificationType StackMapFrame::set_locals_from_arg( |
|
77 const methodHandle m, VerificationType thisKlass, TRAPS) { |
|
78 SignatureStream ss(m->signature()); |
|
79 int init_local_num = 0; |
|
80 if (!m->is_static()) { |
|
81 init_local_num++; |
|
82 // add one extra argument for instance method |
|
83 if (m->name() == vmSymbols::object_initializer_name() && |
|
84 thisKlass.name() != vmSymbols::java_lang_Object()) { |
|
85 _locals[0] = VerificationType::uninitialized_this_type(); |
|
86 _flags |= FLAG_THIS_UNINIT; |
|
87 } else { |
|
88 _locals[0] = thisKlass; |
|
89 } |
|
90 } |
|
91 |
|
92 // local num may be greater than size of parameters because long/double occupies two slots |
|
93 while(!ss.at_return_type()) { |
|
94 init_local_num += _verifier->change_sig_to_verificationType( |
|
95 &ss, &_locals[init_local_num], |
|
96 CHECK_VERIFY_(verifier(), VerificationType::bogus_type())); |
|
97 ss.next(); |
|
98 } |
|
99 _locals_size = init_local_num; |
|
100 |
|
101 switch (ss.type()) { |
|
102 case T_OBJECT: |
|
103 case T_ARRAY: |
|
104 { |
|
105 Symbol* sig = ss.as_symbol(CHECK_(VerificationType::bogus_type())); |
|
106 // Create another symbol to save as signature stream unreferences |
|
107 // this symbol. |
|
108 Symbol* sig_copy = |
|
109 verifier()->create_temporary_symbol(sig, 0, sig->utf8_length(), |
|
110 CHECK_(VerificationType::bogus_type())); |
|
111 assert(sig_copy == sig, "symbols don't match"); |
|
112 return VerificationType::reference_type(sig_copy); |
|
113 } |
|
114 case T_INT: return VerificationType::integer_type(); |
|
115 case T_BYTE: return VerificationType::byte_type(); |
|
116 case T_CHAR: return VerificationType::char_type(); |
|
117 case T_SHORT: return VerificationType::short_type(); |
|
118 case T_BOOLEAN: return VerificationType::boolean_type(); |
|
119 case T_FLOAT: return VerificationType::float_type(); |
|
120 case T_DOUBLE: return VerificationType::double_type(); |
|
121 case T_LONG: return VerificationType::long_type(); |
|
122 case T_VOID: return VerificationType::bogus_type(); |
|
123 default: |
|
124 ShouldNotReachHere(); |
|
125 } |
|
126 return VerificationType::bogus_type(); |
|
127 } |
|
128 |
|
129 void StackMapFrame::copy_locals(const StackMapFrame* src) { |
|
130 int32_t len = src->locals_size() < _locals_size ? |
|
131 src->locals_size() : _locals_size; |
|
132 for (int32_t i = 0; i < len; i++) { |
|
133 _locals[i] = src->locals()[i]; |
|
134 } |
|
135 } |
|
136 |
|
137 void StackMapFrame::copy_stack(const StackMapFrame* src) { |
|
138 int32_t len = src->stack_size() < _stack_size ? |
|
139 src->stack_size() : _stack_size; |
|
140 for (int32_t i = 0; i < len; i++) { |
|
141 _stack[i] = src->stack()[i]; |
|
142 } |
|
143 } |
|
144 |
|
145 // Returns the location of the first mismatch, or 'len' if there are no |
|
146 // mismatches |
|
147 int StackMapFrame::is_assignable_to( |
|
148 VerificationType* from, VerificationType* to, int32_t len, TRAPS) const { |
|
149 int32_t i = 0; |
|
150 for (i = 0; i < len; i++) { |
|
151 if (!to[i].is_assignable_from(from[i], verifier(), false, THREAD)) { |
|
152 break; |
|
153 } |
|
154 } |
|
155 return i; |
|
156 } |
|
157 |
|
158 bool StackMapFrame::has_flag_match_exception( |
|
159 const StackMapFrame* target) const { |
|
160 // We allow flags of {UninitThis} to assign to {} if-and-only-if the |
|
161 // target frame does not depend upon the current type. |
|
162 // This is slightly too strict, as we need only enforce that the |
|
163 // slots that were initialized by the <init> (the things that were |
|
164 // UninitializedThis before initialize_object() converted them) are unused. |
|
165 // However we didn't save that information so we'll enforce this upon |
|
166 // anything that might have been initialized. This is a rare situation |
|
167 // and javac never generates code that would end up here, but some profilers |
|
168 // (such as NetBeans) might, when adding exception handlers in <init> |
|
169 // methods to cover the invokespecial instruction. See 7020118. |
|
170 |
|
171 assert(max_locals() == target->max_locals() && |
|
172 stack_size() == target->stack_size(), "StackMap sizes must match"); |
|
173 |
|
174 VerificationType top = VerificationType::top_type(); |
|
175 VerificationType this_type = verifier()->current_type(); |
|
176 |
|
177 if (!flag_this_uninit() || target->flags() != 0) { |
|
178 return false; |
|
179 } |
|
180 |
|
181 for (int i = 0; i < target->locals_size(); ++i) { |
|
182 if (locals()[i] == this_type && target->locals()[i] != top) { |
|
183 return false; |
|
184 } |
|
185 } |
|
186 |
|
187 for (int i = 0; i < target->stack_size(); ++i) { |
|
188 if (stack()[i] == this_type && target->stack()[i] != top) { |
|
189 return false; |
|
190 } |
|
191 } |
|
192 |
|
193 return true; |
|
194 } |
|
195 |
|
196 bool StackMapFrame::is_assignable_to( |
|
197 const StackMapFrame* target, bool is_exception_handler, |
|
198 ErrorContext* ctx, TRAPS) const { |
|
199 if (_max_locals != target->max_locals()) { |
|
200 *ctx = ErrorContext::locals_size_mismatch( |
|
201 _offset, (StackMapFrame*)this, (StackMapFrame*)target); |
|
202 return false; |
|
203 } |
|
204 if (_stack_size != target->stack_size()) { |
|
205 *ctx = ErrorContext::stack_size_mismatch( |
|
206 _offset, (StackMapFrame*)this, (StackMapFrame*)target); |
|
207 return false; |
|
208 } |
|
209 // Only need to compare type elements up to target->locals() or target->stack(). |
|
210 // The remaining type elements in this state can be ignored because they are |
|
211 // assignable to bogus type. |
|
212 int mismatch_loc; |
|
213 mismatch_loc = is_assignable_to( |
|
214 _locals, target->locals(), target->locals_size(), THREAD); |
|
215 if (mismatch_loc != target->locals_size()) { |
|
216 *ctx = ErrorContext::bad_type(target->offset(), |
|
217 TypeOrigin::local(mismatch_loc, (StackMapFrame*)this), |
|
218 TypeOrigin::sm_local(mismatch_loc, (StackMapFrame*)target)); |
|
219 return false; |
|
220 } |
|
221 mismatch_loc = is_assignable_to(_stack, target->stack(), _stack_size, THREAD); |
|
222 if (mismatch_loc != _stack_size) { |
|
223 *ctx = ErrorContext::bad_type(target->offset(), |
|
224 TypeOrigin::stack(mismatch_loc, (StackMapFrame*)this), |
|
225 TypeOrigin::sm_stack(mismatch_loc, (StackMapFrame*)target)); |
|
226 return false; |
|
227 } |
|
228 |
|
229 bool match_flags = (_flags | target->flags()) == target->flags(); |
|
230 if (match_flags || is_exception_handler && has_flag_match_exception(target)) { |
|
231 return true; |
|
232 } else { |
|
233 *ctx = ErrorContext::bad_flags(target->offset(), |
|
234 (StackMapFrame*)this, (StackMapFrame*)target); |
|
235 return false; |
|
236 } |
|
237 } |
|
238 |
|
239 VerificationType StackMapFrame::pop_stack_ex(VerificationType type, TRAPS) { |
|
240 if (_stack_size <= 0) { |
|
241 verifier()->verify_error( |
|
242 ErrorContext::stack_underflow(_offset, this), |
|
243 "Operand stack underflow"); |
|
244 return VerificationType::bogus_type(); |
|
245 } |
|
246 VerificationType top = _stack[--_stack_size]; |
|
247 bool subtype = type.is_assignable_from( |
|
248 top, verifier(), false, CHECK_(VerificationType::bogus_type())); |
|
249 if (!subtype) { |
|
250 verifier()->verify_error( |
|
251 ErrorContext::bad_type(_offset, stack_top_ctx(), |
|
252 TypeOrigin::implicit(type)), |
|
253 "Bad type on operand stack"); |
|
254 return VerificationType::bogus_type(); |
|
255 } |
|
256 return top; |
|
257 } |
|
258 |
|
259 VerificationType StackMapFrame::get_local( |
|
260 int32_t index, VerificationType type, TRAPS) { |
|
261 if (index >= _max_locals) { |
|
262 verifier()->verify_error( |
|
263 ErrorContext::bad_local_index(_offset, index), |
|
264 "Local variable table overflow"); |
|
265 return VerificationType::bogus_type(); |
|
266 } |
|
267 bool subtype = type.is_assignable_from(_locals[index], |
|
268 verifier(), false, CHECK_(VerificationType::bogus_type())); |
|
269 if (!subtype) { |
|
270 verifier()->verify_error( |
|
271 ErrorContext::bad_type(_offset, |
|
272 TypeOrigin::local(index, this), |
|
273 TypeOrigin::implicit(type)), |
|
274 "Bad local variable type"); |
|
275 return VerificationType::bogus_type(); |
|
276 } |
|
277 if(index >= _locals_size) { _locals_size = index + 1; } |
|
278 return _locals[index]; |
|
279 } |
|
280 |
|
281 void StackMapFrame::get_local_2( |
|
282 int32_t index, VerificationType type1, VerificationType type2, TRAPS) { |
|
283 assert(type1.is_long() || type1.is_double(), "must be long/double"); |
|
284 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); |
|
285 if (index >= _locals_size - 1) { |
|
286 verifier()->verify_error( |
|
287 ErrorContext::bad_local_index(_offset, index), |
|
288 "get long/double overflows locals"); |
|
289 return; |
|
290 } |
|
291 bool subtype = type1.is_assignable_from(_locals[index], verifier(), false, CHECK); |
|
292 if (!subtype) { |
|
293 verifier()->verify_error( |
|
294 ErrorContext::bad_type(_offset, |
|
295 TypeOrigin::local(index, this), TypeOrigin::implicit(type1)), |
|
296 "Bad local variable type"); |
|
297 } else { |
|
298 subtype = type2.is_assignable_from(_locals[index + 1], verifier(), false, CHECK); |
|
299 if (!subtype) { |
|
300 /* Unreachable? All local store routines convert a split long or double |
|
301 * into a TOP during the store. So we should never end up seeing an |
|
302 * orphaned half. */ |
|
303 verifier()->verify_error( |
|
304 ErrorContext::bad_type(_offset, |
|
305 TypeOrigin::local(index + 1, this), TypeOrigin::implicit(type2)), |
|
306 "Bad local variable type"); |
|
307 } |
|
308 } |
|
309 } |
|
310 |
|
311 void StackMapFrame::set_local(int32_t index, VerificationType type, TRAPS) { |
|
312 assert(!type.is_check(), "Must be a real type"); |
|
313 if (index >= _max_locals) { |
|
314 verifier()->verify_error( |
|
315 ErrorContext::bad_local_index(_offset, index), |
|
316 "Local variable table overflow"); |
|
317 return; |
|
318 } |
|
319 // If type at index is double or long, set the next location to be unusable |
|
320 if (_locals[index].is_double() || _locals[index].is_long()) { |
|
321 assert((index + 1) < _locals_size, "Local variable table overflow"); |
|
322 _locals[index + 1] = VerificationType::bogus_type(); |
|
323 } |
|
324 // If type at index is double_2 or long_2, set the previous location to be unusable |
|
325 if (_locals[index].is_double2() || _locals[index].is_long2()) { |
|
326 assert(index >= 1, "Local variable table underflow"); |
|
327 _locals[index - 1] = VerificationType::bogus_type(); |
|
328 } |
|
329 _locals[index] = type; |
|
330 if (index >= _locals_size) { |
|
331 #ifdef ASSERT |
|
332 for (int i=_locals_size; i<index; i++) { |
|
333 assert(_locals[i] == VerificationType::bogus_type(), |
|
334 "holes must be bogus type"); |
|
335 } |
|
336 #endif |
|
337 _locals_size = index + 1; |
|
338 } |
|
339 } |
|
340 |
|
341 void StackMapFrame::set_local_2( |
|
342 int32_t index, VerificationType type1, VerificationType type2, TRAPS) { |
|
343 assert(type1.is_long() || type1.is_double(), "must be long/double"); |
|
344 assert(type2.is_long2() || type2.is_double2(), "must be long/double_2"); |
|
345 if (index >= _max_locals - 1) { |
|
346 verifier()->verify_error( |
|
347 ErrorContext::bad_local_index(_offset, index), |
|
348 "Local variable table overflow"); |
|
349 return; |
|
350 } |
|
351 // If type at index+1 is double or long, set the next location to be unusable |
|
352 if (_locals[index+1].is_double() || _locals[index+1].is_long()) { |
|
353 assert((index + 2) < _locals_size, "Local variable table overflow"); |
|
354 _locals[index + 2] = VerificationType::bogus_type(); |
|
355 } |
|
356 // If type at index is double_2 or long_2, set the previous location to be unusable |
|
357 if (_locals[index].is_double2() || _locals[index].is_long2()) { |
|
358 assert(index >= 1, "Local variable table underflow"); |
|
359 _locals[index - 1] = VerificationType::bogus_type(); |
|
360 } |
|
361 _locals[index] = type1; |
|
362 _locals[index+1] = type2; |
|
363 if (index >= _locals_size - 1) { |
|
364 #ifdef ASSERT |
|
365 for (int i=_locals_size; i<index; i++) { |
|
366 assert(_locals[i] == VerificationType::bogus_type(), |
|
367 "holes must be bogus type"); |
|
368 } |
|
369 #endif |
|
370 _locals_size = index + 2; |
|
371 } |
|
372 } |
|
373 |
|
374 TypeOrigin StackMapFrame::stack_top_ctx() { |
|
375 return TypeOrigin::stack(_stack_size, this); |
|
376 } |
|
377 |
|
378 void StackMapFrame::print_on(outputStream* str) const { |
|
379 str->indent().print_cr("bci: @%d", _offset); |
|
380 str->indent().print_cr("flags: {%s }", |
|
381 flag_this_uninit() ? " flagThisUninit" : ""); |
|
382 str->indent().print("locals: {"); |
|
383 for (int32_t i = 0; i < _locals_size; ++i) { |
|
384 str->print(" "); |
|
385 _locals[i].print_on(str); |
|
386 if (i != _locals_size - 1) { |
|
387 str->print(","); |
|
388 } |
|
389 } |
|
390 str->print_cr(" }"); |
|
391 str->indent().print("stack: {"); |
|
392 for (int32_t j = 0; j < _stack_size; ++j) { |
|
393 str->print(" "); |
|
394 _stack[j].print_on(str); |
|
395 if (j != _stack_size - 1) { |
|
396 str->print(","); |
|
397 } |
|
398 } |
|
399 str->print_cr(" }"); |
|
400 } |