Wed, 19 Nov 2014 12:52:56 -0800
Merge
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 */
25 #include "precompiled.hpp"
26 #include "classfile/stackMapTable.hpp"
27 #include "classfile/verifier.hpp"
28 #include "memory/resourceArea.hpp"
29 #include "oops/oop.inline.hpp"
30 #include "runtime/fieldType.hpp"
31 #include "runtime/handles.inline.hpp"
33 StackMapTable::StackMapTable(StackMapReader* reader, StackMapFrame* init_frame,
34 u2 max_locals, u2 max_stack,
35 char* code_data, int code_len, TRAPS) {
36 _code_length = code_len;
37 _frame_count = reader->get_frame_count();
38 if (_frame_count > 0) {
39 _frame_array = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD,
40 StackMapFrame*, _frame_count);
41 StackMapFrame* pre_frame = init_frame;
42 for (int32_t i = 0; i < _frame_count; i++) {
43 StackMapFrame* frame = reader->next(
44 pre_frame, i == 0, max_locals, max_stack,
45 CHECK_VERIFY(pre_frame->verifier()));
46 _frame_array[i] = frame;
47 int offset = frame->offset();
48 if (offset >= code_len || code_data[offset] == 0) {
49 frame->verifier()->verify_error(
50 ErrorContext::bad_stackmap(i, frame),
51 "StackMapTable error: bad offset");
52 return;
53 }
54 pre_frame = frame;
55 }
56 }
57 reader->check_end(CHECK);
58 }
60 // This method is only called by method in StackMapTable.
61 int StackMapTable::get_index_from_offset(int32_t offset) const {
62 int i = 0;
63 for (; i < _frame_count; i++) {
64 if (_frame_array[i]->offset() == offset) {
65 return i;
66 }
67 }
68 return i; // frame with offset doesn't exist in the array
69 }
71 bool StackMapTable::match_stackmap(
72 StackMapFrame* frame, int32_t target,
73 bool match, bool update, bool handler, ErrorContext* ctx, TRAPS) const {
74 int index = get_index_from_offset(target);
75 return match_stackmap(frame, target, index, match, update, handler, ctx, THREAD);
76 }
78 // Match and/or update current_frame to the frame in stackmap table with
79 // specified offset and frame index. Return true if the two frames match.
80 // handler is true if the frame in stackmap_table is for an exception handler.
81 //
82 // The values of match and update are: _match__update__handler
83 //
84 // checking a branch target: true false false
85 // checking an exception handler: true false true
86 // linear bytecode verification following an
87 // unconditional branch: false true false
88 // linear bytecode verification not following an
89 // unconditional branch: true true false
90 bool StackMapTable::match_stackmap(
91 StackMapFrame* frame, int32_t target, int32_t frame_index,
92 bool match, bool update, bool handler, ErrorContext* ctx, TRAPS) const {
93 if (frame_index < 0 || frame_index >= _frame_count) {
94 *ctx = ErrorContext::missing_stackmap(frame->offset());
95 frame->verifier()->verify_error(
96 *ctx, "Expecting a stackmap frame at branch target %d", target);
97 return false;
98 }
100 StackMapFrame *stackmap_frame = _frame_array[frame_index];
101 bool result = true;
102 if (match) {
103 // Has direct control flow from last instruction, need to match the two
104 // frames.
105 result = frame->is_assignable_to(stackmap_frame, handler,
106 ctx, CHECK_VERIFY_(frame->verifier(), result));
107 }
108 if (update) {
109 // Use the frame in stackmap table as current frame
110 int lsize = stackmap_frame->locals_size();
111 int ssize = stackmap_frame->stack_size();
112 if (frame->locals_size() > lsize || frame->stack_size() > ssize) {
113 // Make sure unused type array items are all _bogus_type.
114 frame->reset();
115 }
116 frame->set_locals_size(lsize);
117 frame->copy_locals(stackmap_frame);
118 frame->set_stack_size(ssize);
119 frame->copy_stack(stackmap_frame);
120 frame->set_flags(stackmap_frame->flags());
121 }
122 return result;
123 }
125 void StackMapTable::check_jump_target(
126 StackMapFrame* frame, int32_t target, TRAPS) const {
127 ErrorContext ctx;
128 bool match = match_stackmap(
129 frame, target, true, false, false, &ctx, CHECK_VERIFY(frame->verifier()));
130 if (!match || (target < 0 || target >= _code_length)) {
131 frame->verifier()->verify_error(ctx,
132 "Inconsistent stackmap frames at branch target %d", target);
133 }
134 }
136 void StackMapTable::print_on(outputStream* str) const {
137 str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count);
138 str->indent().print_cr("table = { ");
139 {
140 streamIndentor si(str);
141 for (int32_t i = 0; i < _frame_count; ++i) {
142 _frame_array[i]->print_on(str);
143 }
144 }
145 str->print_cr(" }");
146 }
148 int32_t StackMapReader::chop(
149 VerificationType* locals, int32_t length, int32_t chops) {
150 if (locals == NULL) return -1;
151 int32_t pos = length - 1;
152 for (int32_t i=0; i<chops; i++) {
153 if (locals[pos].is_category2_2nd()) {
154 pos -= 2;
155 } else {
156 pos --;
157 }
158 if (pos<0 && i<(chops-1)) return -1;
159 }
160 return pos+1;
161 }
163 VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) {
164 u1 tag = _stream->get_u1(THREAD);
165 if (tag < (u1)ITEM_UninitializedThis) {
166 return VerificationType::from_tag(tag);
167 }
168 if (tag == ITEM_Object) {
169 u2 class_index = _stream->get_u2(THREAD);
170 int nconstants = _cp->length();
171 if ((class_index <= 0 || class_index >= nconstants) ||
172 (!_cp->tag_at(class_index).is_klass() &&
173 !_cp->tag_at(class_index).is_unresolved_klass())) {
174 _stream->stackmap_format_error("bad class index", THREAD);
175 return VerificationType::bogus_type();
176 }
177 return VerificationType::reference_type(_cp->klass_name_at(class_index));
178 }
179 if (tag == ITEM_UninitializedThis) {
180 if (flags != NULL) {
181 *flags |= FLAG_THIS_UNINIT;
182 }
183 return VerificationType::uninitialized_this_type();
184 }
185 if (tag == ITEM_Uninitialized) {
186 u2 offset = _stream->get_u2(THREAD);
187 if (offset >= _code_length ||
188 _code_data[offset] != ClassVerifier::NEW_OFFSET) {
189 ResourceMark rm(THREAD);
190 _verifier->class_format_error(
191 "StackMapTable format error: bad offset for Uninitialized");
192 return VerificationType::bogus_type();
193 }
194 return VerificationType::uninitialized_type(offset);
195 }
196 _stream->stackmap_format_error("bad verification type", THREAD);
197 return VerificationType::bogus_type();
198 }
200 StackMapFrame* StackMapReader::next(
201 StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) {
202 StackMapFrame* frame;
203 int offset;
204 VerificationType* locals = NULL;
205 u1 frame_type = _stream->get_u1(THREAD);
206 if (frame_type < 64) {
207 // same_frame
208 if (first) {
209 offset = frame_type;
210 // Can't share the locals array since that is updated by the verifier.
211 if (pre_frame->locals_size() > 0) {
212 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
213 THREAD, VerificationType, pre_frame->locals_size());
214 }
215 } else {
216 offset = pre_frame->offset() + frame_type + 1;
217 locals = pre_frame->locals();
218 }
219 frame = new StackMapFrame(
220 offset, pre_frame->flags(), pre_frame->locals_size(), 0,
221 max_locals, max_stack, locals, NULL, _verifier);
222 if (first && locals != NULL) {
223 frame->copy_locals(pre_frame);
224 }
225 return frame;
226 }
227 if (frame_type < 128) {
228 // same_locals_1_stack_item_frame
229 if (first) {
230 offset = frame_type - 64;
231 // Can't share the locals array since that is updated by the verifier.
232 if (pre_frame->locals_size() > 0) {
233 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
234 THREAD, VerificationType, pre_frame->locals_size());
235 }
236 } else {
237 offset = pre_frame->offset() + frame_type - 63;
238 locals = pre_frame->locals();
239 }
240 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
241 THREAD, VerificationType, 2);
242 u2 stack_size = 1;
243 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
244 if (stack[0].is_category2()) {
245 stack[1] = stack[0].to_category2_2nd();
246 stack_size = 2;
247 }
248 check_verification_type_array_size(
249 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
250 frame = new StackMapFrame(
251 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
252 max_locals, max_stack, locals, stack, _verifier);
253 if (first && locals != NULL) {
254 frame->copy_locals(pre_frame);
255 }
256 return frame;
257 }
259 u2 offset_delta = _stream->get_u2(THREAD);
261 if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
262 // reserved frame types
263 _stream->stackmap_format_error(
264 "reserved frame type", CHECK_VERIFY_(_verifier, NULL));
265 }
267 if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
268 // same_locals_1_stack_item_frame_extended
269 if (first) {
270 offset = offset_delta;
271 // Can't share the locals array since that is updated by the verifier.
272 if (pre_frame->locals_size() > 0) {
273 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
274 THREAD, VerificationType, pre_frame->locals_size());
275 }
276 } else {
277 offset = pre_frame->offset() + offset_delta + 1;
278 locals = pre_frame->locals();
279 }
280 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD(
281 THREAD, VerificationType, 2);
282 u2 stack_size = 1;
283 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL));
284 if (stack[0].is_category2()) {
285 stack[1] = stack[0].to_category2_2nd();
286 stack_size = 2;
287 }
288 check_verification_type_array_size(
289 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
290 frame = new StackMapFrame(
291 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size,
292 max_locals, max_stack, locals, stack, _verifier);
293 if (first && locals != NULL) {
294 frame->copy_locals(pre_frame);
295 }
296 return frame;
297 }
299 if (frame_type <= SAME_EXTENDED) {
300 // chop_frame or same_frame_extended
301 locals = pre_frame->locals();
302 int length = pre_frame->locals_size();
303 int chops = SAME_EXTENDED - frame_type;
304 int new_length = length;
305 u1 flags = pre_frame->flags();
306 if (chops != 0) {
307 new_length = chop(locals, length, chops);
308 check_verification_type_array_size(
309 new_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
310 // Recompute flags since uninitializedThis could have been chopped.
311 flags = 0;
312 for (int i=0; i<new_length; i++) {
313 if (locals[i].is_uninitialized_this()) {
314 flags |= FLAG_THIS_UNINIT;
315 break;
316 }
317 }
318 }
319 if (first) {
320 offset = offset_delta;
321 // Can't share the locals array since that is updated by the verifier.
322 if (new_length > 0) {
323 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
324 THREAD, VerificationType, new_length);
325 } else {
326 locals = NULL;
327 }
328 } else {
329 offset = pre_frame->offset() + offset_delta + 1;
330 }
331 frame = new StackMapFrame(
332 offset, flags, new_length, 0, max_locals, max_stack,
333 locals, NULL, _verifier);
334 if (first && locals != NULL) {
335 frame->copy_locals(pre_frame);
336 }
337 return frame;
338 } else if (frame_type < SAME_EXTENDED + 4) {
339 // append_frame
340 int appends = frame_type - SAME_EXTENDED;
341 int real_length = pre_frame->locals_size();
342 int new_length = real_length + appends*2;
343 locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length);
344 VerificationType* pre_locals = pre_frame->locals();
345 int i;
346 for (i=0; i<pre_frame->locals_size(); i++) {
347 locals[i] = pre_locals[i];
348 }
349 u1 flags = pre_frame->flags();
350 for (i=0; i<appends; i++) {
351 locals[real_length] = parse_verification_type(&flags, THREAD);
352 if (locals[real_length].is_category2()) {
353 locals[real_length + 1] = locals[real_length].to_category2_2nd();
354 ++real_length;
355 }
356 ++real_length;
357 }
358 check_verification_type_array_size(
359 real_length, max_locals, CHECK_VERIFY_(_verifier, NULL));
360 if (first) {
361 offset = offset_delta;
362 } else {
363 offset = pre_frame->offset() + offset_delta + 1;
364 }
365 frame = new StackMapFrame(
366 offset, flags, real_length, 0, max_locals,
367 max_stack, locals, NULL, _verifier);
368 return frame;
369 }
370 if (frame_type == FULL) {
371 // full_frame
372 u1 flags = 0;
373 u2 locals_size = _stream->get_u2(THREAD);
374 int real_locals_size = 0;
375 if (locals_size > 0) {
376 locals = NEW_RESOURCE_ARRAY_IN_THREAD(
377 THREAD, VerificationType, locals_size*2);
378 }
379 int i;
380 for (i=0; i<locals_size; i++) {
381 locals[real_locals_size] = parse_verification_type(&flags, THREAD);
382 if (locals[real_locals_size].is_category2()) {
383 locals[real_locals_size + 1] =
384 locals[real_locals_size].to_category2_2nd();
385 ++real_locals_size;
386 }
387 ++real_locals_size;
388 }
389 check_verification_type_array_size(
390 real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL));
391 u2 stack_size = _stream->get_u2(THREAD);
392 int real_stack_size = 0;
393 VerificationType* stack = NULL;
394 if (stack_size > 0) {
395 stack = NEW_RESOURCE_ARRAY_IN_THREAD(
396 THREAD, VerificationType, stack_size*2);
397 }
398 for (i=0; i<stack_size; i++) {
399 stack[real_stack_size] = parse_verification_type(NULL, THREAD);
400 if (stack[real_stack_size].is_category2()) {
401 stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd();
402 ++real_stack_size;
403 }
404 ++real_stack_size;
405 }
406 check_verification_type_array_size(
407 real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL));
408 if (first) {
409 offset = offset_delta;
410 } else {
411 offset = pre_frame->offset() + offset_delta + 1;
412 }
413 frame = new StackMapFrame(
414 offset, flags, real_locals_size, real_stack_size,
415 max_locals, max_stack, locals, stack, _verifier);
416 return frame;
417 }
419 _stream->stackmap_format_error(
420 "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL));
421 return NULL;
422 }