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 return; 134 } 135 // check if uninitialized objects exist on backward branches 136 check_new_object(frame, target, CHECK_VERIFY(frame->verifier())); 137 } 138 139 void StackMapTable::check_new_object( 140 const StackMapFrame* frame, int32_t target, TRAPS) const { 141 int frame_index = get_index_from_offset(target); 142 assert(frame_index >= 0 && frame_index < _frame_count, "bad frame index"); 143 if (frame->offset() > target && 144 frame->has_unique_new_object(_frame_array[frame_index])) { 145 frame->verifier()->verify_error( 146 ErrorContext::bad_code(frame->offset()), 147 "Uninitialized object exists on backward branch %d", target); 148 return; 149 } 150 } 151 152 void StackMapTable::print_on(outputStream* str) const { 153 str->indent().print_cr("StackMapTable: frame_count = %d", _frame_count); 154 str->indent().print_cr("table = { "); 155 { 156 streamIndentor si(str); 157 for (int32_t i = 0; i < _frame_count; ++i) { 158 _frame_array[i]->print_on(str); 159 } 160 } 161 str->print_cr(" }"); 162 } 163 164 int32_t StackMapReader::chop( 165 VerificationType* locals, int32_t length, int32_t chops) { 166 if (locals == NULL) return -1; 167 int32_t pos = length - 1; 168 for (int32_t i=0; i<chops; i++) { 169 if (locals[pos].is_category2_2nd()) { 170 pos -= 2; 171 } else { 172 pos --; 173 } 174 if (pos<0 && i<(chops-1)) return -1; 175 } 176 return pos+1; 177 } 178 179 VerificationType StackMapReader::parse_verification_type(u1* flags, TRAPS) { 180 u1 tag = _stream->get_u1(THREAD); 181 if (tag < (u1)ITEM_UninitializedThis) { 182 return VerificationType::from_tag(tag); 183 } 184 if (tag == ITEM_Object) { 185 u2 class_index = _stream->get_u2(THREAD); 186 int nconstants = _cp->length(); 187 if ((class_index <= 0 || class_index >= nconstants) || 188 (!_cp->tag_at(class_index).is_klass() && 189 !_cp->tag_at(class_index).is_unresolved_klass())) { 190 _stream->stackmap_format_error("bad class index", THREAD); 191 return VerificationType::bogus_type(); 192 } 193 return VerificationType::reference_type(_cp->klass_name_at(class_index)); 194 } 195 if (tag == ITEM_UninitializedThis) { 196 if (flags != NULL) { 197 *flags |= FLAG_THIS_UNINIT; 198 } 199 return VerificationType::uninitialized_this_type(); 200 } 201 if (tag == ITEM_Uninitialized) { 202 u2 offset = _stream->get_u2(THREAD); 203 if (offset >= _code_length || 204 _code_data[offset] != ClassVerifier::NEW_OFFSET) { 205 ResourceMark rm(THREAD); 206 _verifier->class_format_error( 207 "StackMapTable format error: bad offset for Uninitialized"); 208 return VerificationType::bogus_type(); 209 } 210 return VerificationType::uninitialized_type(offset); 211 } 212 _stream->stackmap_format_error("bad verification type", THREAD); 213 return VerificationType::bogus_type(); 214 } 215 216 StackMapFrame* StackMapReader::next( 217 StackMapFrame* pre_frame, bool first, u2 max_locals, u2 max_stack, TRAPS) { 218 StackMapFrame* frame; 219 int offset; 220 VerificationType* locals = NULL; 221 u1 frame_type = _stream->get_u1(THREAD); 222 if (frame_type < 64) { 223 // same_frame 224 if (first) { 225 offset = frame_type; 226 // Can't share the locals array since that is updated by the verifier. 227 if (pre_frame->locals_size() > 0) { 228 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 229 THREAD, VerificationType, pre_frame->locals_size()); 230 } 231 } else { 232 offset = pre_frame->offset() + frame_type + 1; 233 locals = pre_frame->locals(); 234 } 235 frame = new StackMapFrame( 236 offset, pre_frame->flags(), pre_frame->locals_size(), 0, 237 max_locals, max_stack, locals, NULL, _verifier); 238 if (first && locals != NULL) { 239 frame->copy_locals(pre_frame); 240 } 241 return frame; 242 } 243 if (frame_type < 128) { 244 // same_locals_1_stack_item_frame 245 if (first) { 246 offset = frame_type - 64; 247 // Can't share the locals array since that is updated by the verifier. 248 if (pre_frame->locals_size() > 0) { 249 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 250 THREAD, VerificationType, pre_frame->locals_size()); 251 } 252 } else { 253 offset = pre_frame->offset() + frame_type - 63; 254 locals = pre_frame->locals(); 255 } 256 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( 257 THREAD, VerificationType, 2); 258 u2 stack_size = 1; 259 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); 260 if (stack[0].is_category2()) { 261 stack[1] = stack[0].to_category2_2nd(); 262 stack_size = 2; 263 } 264 check_verification_type_array_size( 265 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 266 frame = new StackMapFrame( 267 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, 268 max_locals, max_stack, locals, stack, _verifier); 269 if (first && locals != NULL) { 270 frame->copy_locals(pre_frame); 271 } 272 return frame; 273 } 274 275 u2 offset_delta = _stream->get_u2(THREAD); 276 277 if (frame_type < SAME_LOCALS_1_STACK_ITEM_EXTENDED) { 278 // reserved frame types 279 _stream->stackmap_format_error( 280 "reserved frame type", CHECK_VERIFY_(_verifier, NULL)); 281 } 282 283 if (frame_type == SAME_LOCALS_1_STACK_ITEM_EXTENDED) { 284 // same_locals_1_stack_item_frame_extended 285 if (first) { 286 offset = offset_delta; 287 // Can't share the locals array since that is updated by the verifier. 288 if (pre_frame->locals_size() > 0) { 289 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 290 THREAD, VerificationType, pre_frame->locals_size()); 291 } 292 } else { 293 offset = pre_frame->offset() + offset_delta + 1; 294 locals = pre_frame->locals(); 295 } 296 VerificationType* stack = NEW_RESOURCE_ARRAY_IN_THREAD( 297 THREAD, VerificationType, 2); 298 u2 stack_size = 1; 299 stack[0] = parse_verification_type(NULL, CHECK_VERIFY_(_verifier, NULL)); 300 if (stack[0].is_category2()) { 301 stack[1] = stack[0].to_category2_2nd(); 302 stack_size = 2; 303 } 304 check_verification_type_array_size( 305 stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 306 frame = new StackMapFrame( 307 offset, pre_frame->flags(), pre_frame->locals_size(), stack_size, 308 max_locals, max_stack, locals, stack, _verifier); 309 if (first && locals != NULL) { 310 frame->copy_locals(pre_frame); 311 } 312 return frame; 313 } 314 315 if (frame_type <= SAME_EXTENDED) { 316 // chop_frame or same_frame_extended 317 locals = pre_frame->locals(); 318 int length = pre_frame->locals_size(); 319 int chops = SAME_EXTENDED - frame_type; 320 int new_length = length; 321 u1 flags = pre_frame->flags(); 322 if (chops != 0) { 323 new_length = chop(locals, length, chops); 324 check_verification_type_array_size( 325 new_length, max_locals, CHECK_VERIFY_(_verifier, NULL)); 326 // Recompute flags since uninitializedThis could have been chopped. 327 flags = 0; 328 for (int i=0; i<new_length; i++) { 329 if (locals[i].is_uninitialized_this()) { 330 flags |= FLAG_THIS_UNINIT; 331 break; 332 } 333 } 334 } 335 if (first) { 336 offset = offset_delta; 337 // Can't share the locals array since that is updated by the verifier. 338 if (new_length > 0) { 339 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 340 THREAD, VerificationType, new_length); 341 } else { 342 locals = NULL; 343 } 344 } else { 345 offset = pre_frame->offset() + offset_delta + 1; 346 } 347 frame = new StackMapFrame( 348 offset, flags, new_length, 0, max_locals, max_stack, 349 locals, NULL, _verifier); 350 if (first && locals != NULL) { 351 frame->copy_locals(pre_frame); 352 } 353 return frame; 354 } else if (frame_type < SAME_EXTENDED + 4) { 355 // append_frame 356 int appends = frame_type - SAME_EXTENDED; 357 int real_length = pre_frame->locals_size(); 358 int new_length = real_length + appends*2; 359 locals = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, new_length); 360 VerificationType* pre_locals = pre_frame->locals(); 361 int i; 362 for (i=0; i<pre_frame->locals_size(); i++) { 363 locals[i] = pre_locals[i]; 364 } 365 u1 flags = pre_frame->flags(); 366 for (i=0; i<appends; i++) { 367 locals[real_length] = parse_verification_type(&flags, THREAD); 368 if (locals[real_length].is_category2()) { 369 locals[real_length + 1] = locals[real_length].to_category2_2nd(); 370 ++real_length; 371 } 372 ++real_length; 373 } 374 check_verification_type_array_size( 375 real_length, max_locals, CHECK_VERIFY_(_verifier, NULL)); 376 if (first) { 377 offset = offset_delta; 378 } else { 379 offset = pre_frame->offset() + offset_delta + 1; 380 } 381 frame = new StackMapFrame( 382 offset, flags, real_length, 0, max_locals, 383 max_stack, locals, NULL, _verifier); 384 return frame; 385 } 386 if (frame_type == FULL) { 387 // full_frame 388 u1 flags = 0; 389 u2 locals_size = _stream->get_u2(THREAD); 390 int real_locals_size = 0; 391 if (locals_size > 0) { 392 locals = NEW_RESOURCE_ARRAY_IN_THREAD( 393 THREAD, VerificationType, locals_size*2); 394 } 395 int i; 396 for (i=0; i<locals_size; i++) { 397 locals[real_locals_size] = parse_verification_type(&flags, THREAD); 398 if (locals[real_locals_size].is_category2()) { 399 locals[real_locals_size + 1] = 400 locals[real_locals_size].to_category2_2nd(); 401 ++real_locals_size; 402 } 403 ++real_locals_size; 404 } 405 check_verification_type_array_size( 406 real_locals_size, max_locals, CHECK_VERIFY_(_verifier, NULL)); 407 u2 stack_size = _stream->get_u2(THREAD); 408 int real_stack_size = 0; 409 VerificationType* stack = NULL; 410 if (stack_size > 0) { 411 stack = NEW_RESOURCE_ARRAY_IN_THREAD( 412 THREAD, VerificationType, stack_size*2); 413 } 414 for (i=0; i<stack_size; i++) { 415 stack[real_stack_size] = parse_verification_type(NULL, THREAD); 416 if (stack[real_stack_size].is_category2()) { 417 stack[real_stack_size + 1] = stack[real_stack_size].to_category2_2nd(); 418 ++real_stack_size; 419 } 420 ++real_stack_size; 421 } 422 check_verification_type_array_size( 423 real_stack_size, max_stack, CHECK_VERIFY_(_verifier, NULL)); 424 if (first) { 425 offset = offset_delta; 426 } else { 427 offset = pre_frame->offset() + offset_delta + 1; 428 } 429 frame = new StackMapFrame( 430 offset, flags, real_locals_size, real_stack_size, 431 max_locals, max_stack, locals, stack, _verifier); 432 return frame; 433 } 434 435 _stream->stackmap_format_error( 436 "reserved frame type", CHECK_VERIFY_(pre_frame->verifier(), NULL)); 437 return NULL; 438 }