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/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" 32 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 } 59 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 } 70 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 } 77 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 } 99 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 } 124 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 } 135 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 } 147 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 } 162 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 _verifier->class_format_error( 190 "StackMapTable format error: bad offset for Uninitialized"); 191 return VerificationType::bogus_type(); 192 } 193 return VerificationType::uninitialized_type(offset); 194 } 195 _stream->stackmap_format_error("bad verification type", THREAD); 196 return VerificationType::bogus_type(); 197 } 198 199 StackMapFrame* StackMapReader::next( 200 StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) { 201 StackMapFrame* frame; 202 int offset; 203 VerificationType* locals = NULL; 204 u1 frame_type = _stream->get_u1(THREAD); 205 if (frame_type < 64) { 206 // same_frame 207 if (first) { 208 offset = frame_type; 209 // Can't share the locals array since that is updated by the verifier. 210 if (pre_frame->locals_size() > 0) { 211 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 212 THREAD, VerificationType, pre_frame->locals_size()); 213 } 214 } else { 215 offset = pre_frame->offset() + frame_type + 1; 216 locals = pre_frame->locals(); 217 } 218 frame = new StackMapFrame( 219 offset, pre_frame->flags(), pre_frame->locals_size(), 0, 220 max_locals, max_stack, locals, NULL, _verifier); 221 if (first && locals != NULL) { 222 frame->copy_locals(pre_frame); 223 } 224 return frame; 225 } 226 if (frame_type < 128) { 227 // same_locals_1_stack_item_frame 228 if (first) { 229 offset = frame_type - 64; 230 // Can't share the locals array since that is updated by the verifier. 231 if (pre_frame->locals_size() > 0) { 232 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 233 THREAD, VerificationType, pre_frame->locals_size()); 234 } 235 } else { 236 offset = pre_frame->offset() + frame_type - 63; 237 locals = pre_frame->locals(); 238 } 239 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( 240 THREAD, VerificationType, 2); 241 u2 stack_size = 1; 242 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); 243 if (stack[0].is_category2()) { 244 stack[1] = stack[0].to_category2_2nd(); 245 stack_size = 2; 246 } 247 check_verification_type_array_size( 248 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 249 frame = new StackMapFrame( 250 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, 251 max_locals, max_stack, locals, stack, _verifier); 252 if (first && locals != NULL) { 253 frame->copy_locals(pre_frame); 254 } 255 return frame; 256 } 257 258 u2 offset_delta = _stream->get_u2(THREAD); 259 260 if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) { 261 // reserved frame types 262 _stream->stackmap_format_error( 263 "reserved frame type", CHECK_VERIFY_(_verifier, NULL)); 264 } 265 266 if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) { 267 // same_locals_1_stack_item_frame_extended 268 if (first) { 269 offset = offset_delta; 270 // Can't share the locals array since that is updated by the verifier. 271 if (pre_frame->locals_size() > 0) { 272 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 273 THREAD, VerificationType, pre_frame->locals_size()); 274 } 275 } else { 276 offset = pre_frame->offset() + offset_delta + 1; 277 locals = pre_frame->locals(); 278 } 279 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( 280 THREAD, VerificationType, 2); 281 u2 stack_size = 1; 282 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); 283 if (stack[0].is_category2()) { 284 stack[1] = stack[0].to_category2_2nd(); 285 stack_size = 2; 286 } 287 check_verification_type_array_size( 288 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 289 frame = new StackMapFrame( 290 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, 291 max_locals, max_stack, locals, stack, _verifier); 292 if (first && locals != NULL) { 293 frame->copy_locals(pre_frame); 294 } 295 return frame; 296 } 297 298 if (frame_type <= SAME_EXTENDED) { 299 // chop_frame or same_frame_extended 300 locals = pre_frame->locals(); 301 int length = pre_frame->locals_size(); 302 int chops = SAME_EXTENDED - frame_type; 303 int new_length = length; 304 u1 flags = pre_frame->flags(); 305 if (chops != 0) { 306 new_length = chop(locals, length, chops); 307 check_verification_type_array_size( 308 new_length, max_locals, CHECK_VERIFY_(_verifier, NULL)); 309 // Recompute flags since uninitializedThis could have been chopped. 310 flags = 0; 311 for (int i=0; i<new_length; i++) { 312 if (locals[i].is_uninitialized_this()) { 313 flags |= FLAG_THIS_UNINIT; 314 break; 315 } 316 } 317 } 318 if (first) { 319 offset = offset_delta; 320 // Can't share the locals array since that is updated by the verifier. 321 if (new_length > 0) { 322 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 323 THREAD, VerificationType, new_length); 324 } else { 325 locals = NULL; 326 } 327 } else { 328 offset = pre_frame->offset() + offset_delta + 1; 329 } 330 frame = new StackMapFrame( 331 offset, flags, new_length, 0, max_locals, max_stack, 332 locals, NULL, _verifier); 333 if (first && locals != NULL) { 334 frame->copy_locals(pre_frame); 335 } 336 return frame; 337 } else if (frame_type < SAME_EXTENDED + 4) { 338 // append_frame 339 int appends = frame_type - SAME_EXTENDED; 340 int real_length = pre_frame->locals_size(); 341 int new_length = real_length + appends*2; 342 locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length); 343 VerificationType* pre_locals = pre_frame->locals(); 344 int i; 345 for (i=0; i<pre_frame->locals_size(); i++) { 346 locals[i] = pre_locals[i]; 347 } 348 u1 flags = pre_frame->flags(); 349 for (i=0; i<appends; i++) { 350 locals[real_length] = parse_verification_type(&flags, THREAD); 351 if (locals[real_length].is_category2()) { 352 locals[real_length + 1] = locals[real_length].to_category2_2nd(); 353 ++real_length; 354 } 355 ++real_length; 356 } 357 check_verification_type_array_size( 358 real_length, max_locals, CHECK_VERIFY_(_verifier, NULL)); 359 if (first) { 360 offset = offset_delta; 361 } else { 362 offset = pre_frame->offset() + offset_delta + 1; 363 } 364 frame = new StackMapFrame( 365 offset, flags, real_length, 0, max_locals, 366 max_stack, locals, NULL, _verifier); 367 return frame; 368 } 369 if (frame_type == FULL) { 370 // full_frame 371 u1 flags = 0; 372 u2 locals_size = _stream->get_u2(THREAD); 373 int real_locals_size = 0; 374 if (locals_size > 0) { 375 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 376 THREAD, VerificationType, locals_size*2); 377 } 378 int i; 379 for (i=0; i<locals_size; i++) { 380 locals[real_locals_size] = parse_verification_type(&flags, THREAD); 381 if (locals[real_locals_size].is_category2()) { 382 locals[real_locals_size + 1] = 383 locals[real_locals_size].to_category2_2nd(); 384 ++real_locals_size; 385 } 386 ++real_locals_size; 387 } 388 check_verification_type_array_size( 389 real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL)); 390 u2 stack_size = _stream->get_u2(THREAD); 391 int real_stack_size = 0; 392 VerificationType* stack = NULL; 393 if (stack_size > 0) { 394 stack = NEW_RESOURCE_ARRAY_IN_THREAD( 395 THREAD, VerificationType, stack_size*2); 396 } 397 for (i=0; i<stack_size; i++) { 398 stack[real_stack_size] = parse_verification_type(NULL, THREAD); 399 if (stack[real_stack_size].is_category2()) { 400 stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd(); 401 ++real_stack_size; 402 } 403 ++real_stack_size; 404 } 405 check_verification_type_array_size( 406 real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 407 if (first) { 408 offset = offset_delta; 409 } else { 410 offset = pre_frame->offset() + offset_delta + 1; 411 } 412 frame = new StackMapFrame( 413 offset, flags, real_locals_size, real_stack_size, 414 max_locals, max_stack, locals, stack, _verifier); 415 return frame; 416 } 417 418 _stream->stackmap_format_error( 419 "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL)); 420 return NULL; 421 }