1 /* 2 * Copyright (c) 1998, 2015, 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/classFileStream.hpp" 27 #include "classfile/javaClasses.hpp" 28 #include "classfile/stackMapTable.hpp" 29 #include "classfile/stackMapFrame.hpp" 30 #include "classfile/stackMapTableFormat.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "classfile/verifier.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "interpreter/bytecodes.hpp" 35 #include "interpreter/bytecodeStream.hpp" 36 #include "memory/oopFactory.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/instanceKlass.hpp" 39 #include "oops/oop.inline.hpp" 40 #include "oops/typeArrayOop.hpp" 41 #include "prims/jvm.h" 42 #include "runtime/fieldDescriptor.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/interfaceSupport.hpp" 45 #include "runtime/javaCalls.hpp" 46 #include "runtime/orderAccess.inline.hpp" 47 #include "runtime/os.hpp" 48 #include "utilities/bytes.hpp" 49 50 #define NOFAILOVER_MAJOR_VERSION 51 51 #define NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION 51 52 #define STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION 52 53 54 // Access to external entry for VerifyClassCodes - old byte code verifier 55 56 extern "C" { 57 typedef jboolean (*verify_byte_codes_fn_t)(JNIEnv *, jclass, char *, jint); 58 typedef jboolean (*verify_byte_codes_fn_new_t)(JNIEnv *, jclass, char *, jint, jint); 59 } 60 61 static void* volatile _verify_byte_codes_fn = NULL; 62 63 static volatile jint _is_new_verify_byte_codes_fn = (jint) true; 64 65 static void* verify_byte_codes_fn() { 66 if (_verify_byte_codes_fn == NULL) { 67 void *lib_handle = os::native_java_library(); 68 void *func = os::dll_lookup(lib_handle, "VerifyClassCodesForMajorVersion"); 69 OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func); 70 if (func == NULL) { 71 OrderAccess::release_store(&_is_new_verify_byte_codes_fn, false); 72 func = os::dll_lookup(lib_handle, "VerifyClassCodes"); 73 OrderAccess::release_store_ptr(&_verify_byte_codes_fn, func); 74 } 75 } 76 return (void*)_verify_byte_codes_fn; 77 } 78 79 80 // Methods in Verifier 81 82 bool Verifier::should_verify_for(oop class_loader, bool should_verify_class) { 83 return (class_loader == NULL || !should_verify_class) ? 84 BytecodeVerificationLocal : BytecodeVerificationRemote; 85 } 86 87 bool Verifier::relax_verify_for(oop loader) { 88 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader); 89 bool need_verify = 90 // verifyAll 91 (BytecodeVerificationLocal && BytecodeVerificationRemote) || 92 // verifyRemote 93 (!BytecodeVerificationLocal && BytecodeVerificationRemote && !trusted); 94 return !need_verify; 95 } 96 97 bool Verifier::verify(instanceKlassHandle klass, Verifier::Mode mode, bool should_verify_class, TRAPS) { 98 HandleMark hm; 99 ResourceMark rm(THREAD); 100 101 // Eagerly allocate the identity hash code for a klass. This is a fallout 102 // from 6320749 and 8059924: hash code generator is not supposed to be called 103 // during the safepoint, but it allows to sneak the hashcode in during 104 // verification. Without this eager hashcode generation, we may end up 105 // installing the hashcode during some other operation, which may be at 106 // safepoint -- blowing up the checks. It was previously done as the side 107 // effect (sic!) for external_name(), but instead of doing that, we opt to 108 // explicitly push the hashcode in here. This is signify the following block 109 // is IMPORTANT: 110 if (klass->java_mirror() != NULL) { 111 klass->java_mirror()->identity_hash(); 112 } 113 114 if (!is_eligible_for_verification(klass, should_verify_class)) { 115 return true; 116 } 117 118 // If the class should be verified, first see if we can use the split 119 // verifier. If not, or if verification fails and FailOverToOldVerifier 120 // is set, then call the inference verifier. 121 122 Symbol* exception_name = NULL; 123 const size_t message_buffer_len = klass->name()->utf8_length() + 1024; 124 char* message_buffer = NEW_RESOURCE_ARRAY(char, message_buffer_len); 125 char* exception_message = message_buffer; 126 127 const char* klassName = klass->external_name(); 128 bool can_failover = FailOverToOldVerifier && 129 klass->major_version() < NOFAILOVER_MAJOR_VERSION; 130 131 if (TraceClassInitialization) { 132 tty->print_cr("Start class verification for: %s", klassName); 133 } 134 if (klass->major_version() >= STACKMAP_ATTRIBUTE_MAJOR_VERSION) { 135 ClassVerifier split_verifier(klass, THREAD); 136 split_verifier.verify_class(THREAD); 137 exception_name = split_verifier.result(); 138 if (can_failover && !HAS_PENDING_EXCEPTION && 139 (exception_name == vmSymbols::java_lang_VerifyError() || 140 exception_name == vmSymbols::java_lang_ClassFormatError())) { 141 if (TraceClassInitialization || VerboseVerification) { 142 tty->print_cr( 143 "Fail over class verification to old verifier for: %s", klassName); 144 } 145 exception_name = inference_verify( 146 klass, message_buffer, message_buffer_len, THREAD); 147 } 148 if (exception_name != NULL) { 149 exception_message = split_verifier.exception_message(); 150 } 151 } else { 152 exception_name = inference_verify( 153 klass, message_buffer, message_buffer_len, THREAD); 154 } 155 156 if (TraceClassInitialization || VerboseVerification) { 157 if (HAS_PENDING_EXCEPTION) { 158 tty->print("Verification for %s has", klassName); 159 tty->print_cr(" exception pending %s ", 160 InstanceKlass::cast(PENDING_EXCEPTION->klass())->external_name()); 161 } else if (exception_name != NULL) { 162 tty->print_cr("Verification for %s failed", klassName); 163 } 164 tty->print_cr("End class verification for: %s", klassName); 165 } 166 167 if (HAS_PENDING_EXCEPTION) { 168 return false; // use the existing exception 169 } else if (exception_name == NULL) { 170 return true; // verifcation succeeded 171 } else { // VerifyError or ClassFormatError to be created and thrown 172 ResourceMark rm(THREAD); 173 instanceKlassHandle kls = 174 SystemDictionary::resolve_or_fail(exception_name, true, CHECK_false); 175 while (!kls.is_null()) { 176 if (kls == klass) { 177 // If the class being verified is the exception we're creating 178 // or one of it's superclasses, we're in trouble and are going 179 // to infinitely recurse when we try to initialize the exception. 180 // So bail out here by throwing the preallocated VM error. 181 THROW_OOP_(Universe::virtual_machine_error_instance(), false); 182 } 183 kls = kls->super(); 184 } 185 message_buffer[message_buffer_len - 1] = '\0'; // just to be sure 186 THROW_MSG_(exception_name, exception_message, false); 187 } 188 } 189 190 bool Verifier::is_eligible_for_verification(instanceKlassHandle klass, bool should_verify_class) { 191 Symbol* name = klass->name(); 192 Klass* refl_magic_klass = SystemDictionary::reflect_MagicAccessorImpl_klass(); 193 194 bool is_reflect = refl_magic_klass != NULL && klass->is_subtype_of(refl_magic_klass); 195 196 return (should_verify_for(klass->class_loader(), should_verify_class) && 197 // return if the class is a bootstrapping class 198 // or defineClass specified not to verify by default (flags override passed arg) 199 // We need to skip the following four for bootstraping 200 name != vmSymbols::java_lang_Object() && 201 name != vmSymbols::java_lang_Class() && 202 name != vmSymbols::java_lang_String() && 203 name != vmSymbols::java_lang_Throwable() && 204 205 // Can not verify the bytecodes for shared classes because they have 206 // already been rewritten to contain constant pool cache indices, 207 // which the verifier can't understand. 208 // Shared classes shouldn't have stackmaps either. 209 !klass()->is_shared() && 210 211 // As of the fix for 4486457 we disable verification for all of the 212 // dynamically-generated bytecodes associated with the 1.4 213 // reflection implementation, not just those associated with 214 // sun/reflect/SerializationConstructorAccessor. 215 // NOTE: this is called too early in the bootstrapping process to be 216 // guarded by Universe::is_gte_jdk14x_version(). 217 // Also for lambda generated code, gte jdk8 218 (!is_reflect)); 219 } 220 221 Symbol* Verifier::inference_verify( 222 instanceKlassHandle klass, char* message, size_t message_len, TRAPS) { 223 JavaThread* thread = (JavaThread*)THREAD; 224 JNIEnv *env = thread->jni_environment(); 225 226 void* verify_func = verify_byte_codes_fn(); 227 228 if (verify_func == NULL) { 229 jio_snprintf(message, message_len, "Could not link verifier"); 230 return vmSymbols::java_lang_VerifyError(); 231 } 232 233 ResourceMark rm(THREAD); 234 if (VerboseVerification) { 235 tty->print_cr("Verifying class %s with old format", klass->external_name()); 236 } 237 238 jclass cls = (jclass) JNIHandles::make_local(env, klass->java_mirror()); 239 jint result; 240 241 { 242 HandleMark hm(thread); 243 ThreadToNativeFromVM ttn(thread); 244 // ThreadToNativeFromVM takes care of changing thread_state, so safepoint 245 // code knows that we have left the VM 246 247 if (_is_new_verify_byte_codes_fn) { 248 verify_byte_codes_fn_new_t func = 249 CAST_TO_FN_PTR(verify_byte_codes_fn_new_t, verify_func); 250 result = (*func)(env, cls, message, (int)message_len, 251 klass->major_version()); 252 } else { 253 verify_byte_codes_fn_t func = 254 CAST_TO_FN_PTR(verify_byte_codes_fn_t, verify_func); 255 result = (*func)(env, cls, message, (int)message_len); 256 } 257 } 258 259 JNIHandles::destroy_local(cls); 260 261 // These numbers are chosen so that VerifyClassCodes interface doesn't need 262 // to be changed (still return jboolean (unsigned char)), and result is 263 // 1 when verification is passed. 264 if (result == 0) { 265 return vmSymbols::java_lang_VerifyError(); 266 } else if (result == 1) { 267 return NULL; // verified. 268 } else if (result == 2) { 269 THROW_MSG_(vmSymbols::java_lang_OutOfMemoryError(), message, NULL); 270 } else if (result == 3) { 271 return vmSymbols::java_lang_ClassFormatError(); 272 } else { 273 ShouldNotReachHere(); 274 return NULL; 275 } 276 } 277 278 TypeOrigin TypeOrigin::null() { 279 return TypeOrigin(); 280 } 281 TypeOrigin TypeOrigin::local(u2 index, StackMapFrame* frame) { 282 assert(frame != NULL, "Must have a frame"); 283 return TypeOrigin(CF_LOCALS, index, StackMapFrame::copy(frame), 284 frame->local_at(index)); 285 } 286 TypeOrigin TypeOrigin::stack(u2 index, StackMapFrame* frame) { 287 assert(frame != NULL, "Must have a frame"); 288 return TypeOrigin(CF_STACK, index, StackMapFrame::copy(frame), 289 frame->stack_at(index)); 290 } 291 TypeOrigin TypeOrigin::sm_local(u2 index, StackMapFrame* frame) { 292 assert(frame != NULL, "Must have a frame"); 293 return TypeOrigin(SM_LOCALS, index, StackMapFrame::copy(frame), 294 frame->local_at(index)); 295 } 296 TypeOrigin TypeOrigin::sm_stack(u2 index, StackMapFrame* frame) { 297 assert(frame != NULL, "Must have a frame"); 298 return TypeOrigin(SM_STACK, index, StackMapFrame::copy(frame), 299 frame->stack_at(index)); 300 } 301 TypeOrigin TypeOrigin::bad_index(u2 index) { 302 return TypeOrigin(BAD_INDEX, index, NULL, VerificationType::bogus_type()); 303 } 304 TypeOrigin TypeOrigin::cp(u2 index, VerificationType vt) { 305 return TypeOrigin(CONST_POOL, index, NULL, vt); 306 } 307 TypeOrigin TypeOrigin::signature(VerificationType vt) { 308 return TypeOrigin(SIG, 0, NULL, vt); 309 } 310 TypeOrigin TypeOrigin::implicit(VerificationType t) { 311 return TypeOrigin(IMPLICIT, 0, NULL, t); 312 } 313 TypeOrigin TypeOrigin::frame(StackMapFrame* frame) { 314 return TypeOrigin(FRAME_ONLY, 0, StackMapFrame::copy(frame), 315 VerificationType::bogus_type()); 316 } 317 318 void TypeOrigin::reset_frame() { 319 if (_frame != NULL) { 320 _frame->restore(); 321 } 322 } 323 324 void TypeOrigin::details(outputStream* ss) const { 325 _type.print_on(ss); 326 switch (_origin) { 327 case CF_LOCALS: 328 ss->print(" (current frame, locals[%d])", _index); 329 break; 330 case CF_STACK: 331 ss->print(" (current frame, stack[%d])", _index); 332 break; 333 case SM_LOCALS: 334 ss->print(" (stack map, locals[%d])", _index); 335 break; 336 case SM_STACK: 337 ss->print(" (stack map, stack[%d])", _index); 338 break; 339 case CONST_POOL: 340 ss->print(" (constant pool %d)", _index); 341 break; 342 case SIG: 343 ss->print(" (from method signature)"); 344 break; 345 case IMPLICIT: 346 case FRAME_ONLY: 347 case NONE: 348 default: 349 ; 350 } 351 } 352 353 #ifdef ASSERT 354 void TypeOrigin::print_on(outputStream* str) const { 355 str->print("{%d,%d,%p:", _origin, _index, _frame); 356 if (_frame != NULL) { 357 _frame->print_on(str); 358 } else { 359 str->print("null"); 360 } 361 str->print(","); 362 _type.print_on(str); 363 str->print("}"); 364 } 365 #endif 366 367 void ErrorContext::details(outputStream* ss, const Method* method) const { 368 if (is_valid()) { 369 ss->cr(); 370 ss->print_cr("Exception Details:"); 371 location_details(ss, method); 372 reason_details(ss); 373 frame_details(ss); 374 bytecode_details(ss, method); 375 handler_details(ss, method); 376 stackmap_details(ss, method); 377 } 378 } 379 380 void ErrorContext::reason_details(outputStream* ss) const { 381 streamIndentor si(ss); 382 ss->indent().print_cr("Reason:"); 383 streamIndentor si2(ss); 384 ss->indent().print("%s", ""); 385 switch (_fault) { 386 case INVALID_BYTECODE: 387 ss->print("Error exists in the bytecode"); 388 break; 389 case WRONG_TYPE: 390 if (_expected.is_valid()) { 391 ss->print("Type "); 392 _type.details(ss); 393 ss->print(" is not assignable to "); 394 _expected.details(ss); 395 } else { 396 ss->print("Invalid type: "); 397 _type.details(ss); 398 } 399 break; 400 case FLAGS_MISMATCH: 401 if (_expected.is_valid()) { 402 ss->print("Current frame's flags are not assignable " 403 "to stack map frame's."); 404 } else { 405 ss->print("Current frame's flags are invalid in this context."); 406 } 407 break; 408 case BAD_CP_INDEX: 409 ss->print("Constant pool index %d is invalid", _type.index()); 410 break; 411 case BAD_LOCAL_INDEX: 412 ss->print("Local index %d is invalid", _type.index()); 413 break; 414 case LOCALS_SIZE_MISMATCH: 415 ss->print("Current frame's local size doesn't match stackmap."); 416 break; 417 case STACK_SIZE_MISMATCH: 418 ss->print("Current frame's stack size doesn't match stackmap."); 419 break; 420 case STACK_OVERFLOW: 421 ss->print("Exceeded max stack size."); 422 break; 423 case STACK_UNDERFLOW: 424 ss->print("Attempt to pop empty stack."); 425 break; 426 case MISSING_STACKMAP: 427 ss->print("Expected stackmap frame at this location."); 428 break; 429 case BAD_STACKMAP: 430 ss->print("Invalid stackmap specification."); 431 break; 432 case UNKNOWN: 433 default: 434 ShouldNotReachHere(); 435 ss->print_cr("Unknown"); 436 } 437 ss->cr(); 438 } 439 440 void ErrorContext::location_details(outputStream* ss, const Method* method) const { 441 if (_bci != -1 && method != NULL) { 442 streamIndentor si(ss); 443 const char* bytecode_name = "<invalid>"; 444 if (method->validate_bci(_bci) != -1) { 445 Bytecodes::Code code = Bytecodes::code_or_bp_at(method->bcp_from(_bci)); 446 if (Bytecodes::is_defined(code)) { 447 bytecode_name = Bytecodes::name(code); 448 } else { 449 bytecode_name = "<illegal>"; 450 } 451 } 452 InstanceKlass* ik = method->method_holder(); 453 ss->indent().print_cr("Location:"); 454 streamIndentor si2(ss); 455 ss->indent().print_cr("%s.%s%s @%d: %s", 456 ik->name()->as_C_string(), method->name()->as_C_string(), 457 method->signature()->as_C_string(), _bci, bytecode_name); 458 } 459 } 460 461 void ErrorContext::frame_details(outputStream* ss) const { 462 streamIndentor si(ss); 463 if (_type.is_valid() && _type.frame() != NULL) { 464 ss->indent().print_cr("Current Frame:"); 465 streamIndentor si2(ss); 466 _type.frame()->print_on(ss); 467 } 468 if (_expected.is_valid() && _expected.frame() != NULL) { 469 ss->indent().print_cr("Stackmap Frame:"); 470 streamIndentor si2(ss); 471 _expected.frame()->print_on(ss); 472 } 473 } 474 475 void ErrorContext::bytecode_details(outputStream* ss, const Method* method) const { 476 if (method != NULL) { 477 streamIndentor si(ss); 478 ss->indent().print_cr("Bytecode:"); 479 streamIndentor si2(ss); 480 ss->print_data(method->code_base(), method->code_size(), false); 481 } 482 } 483 484 void ErrorContext::handler_details(outputStream* ss, const Method* method) const { 485 if (method != NULL) { 486 streamIndentor si(ss); 487 ExceptionTable table(method); 488 if (table.length() > 0) { 489 ss->indent().print_cr("Exception Handler Table:"); 490 streamIndentor si2(ss); 491 for (int i = 0; i < table.length(); ++i) { 492 ss->indent().print_cr("bci [%d, %d] => handler: %d", table.start_pc(i), 493 table.end_pc(i), table.handler_pc(i)); 494 } 495 } 496 } 497 } 498 499 void ErrorContext::stackmap_details(outputStream* ss, const Method* method) const { 500 if (method != NULL && method->has_stackmap_table()) { 501 streamIndentor si(ss); 502 ss->indent().print_cr("Stackmap Table:"); 503 Array<u1>* data = method->stackmap_data(); 504 stack_map_table* sm_table = 505 stack_map_table::at((address)data->adr_at(0)); 506 stack_map_frame* sm_frame = sm_table->entries(); 507 streamIndentor si2(ss); 508 int current_offset = -1; 509 for (u2 i = 0; i < sm_table->number_of_entries(); ++i) { 510 ss->indent(); 511 sm_frame->print_on(ss, current_offset); 512 ss->cr(); 513 current_offset += sm_frame->offset_delta(); 514 sm_frame = sm_frame->next(); 515 } 516 } 517 } 518 519 // Methods in ClassVerifier 520 521 ClassVerifier::ClassVerifier( 522 instanceKlassHandle klass, TRAPS) 523 : _thread(THREAD), _exception_type(NULL), _message(NULL), _klass(klass) { 524 _this_type = VerificationType::reference_type(klass->name()); 525 // Create list to hold symbols in reference area. 526 _symbols = new GrowableArray<Symbol*>(100, 0, NULL); 527 } 528 529 ClassVerifier::~ClassVerifier() { 530 // Decrement the reference count for any symbols created. 531 for (int i = 0; i < _symbols->length(); i++) { 532 Symbol* s = _symbols->at(i); 533 s->decrement_refcount(); 534 } 535 } 536 537 VerificationType ClassVerifier::object_type() const { 538 return VerificationType::reference_type(vmSymbols::java_lang_Object()); 539 } 540 541 TypeOrigin ClassVerifier::ref_ctx(const char* sig, TRAPS) { 542 VerificationType vt = VerificationType::reference_type( 543 create_temporary_symbol(sig, (int)strlen(sig), THREAD)); 544 return TypeOrigin::implicit(vt); 545 } 546 547 void ClassVerifier::verify_class(TRAPS) { 548 if (VerboseVerification) { 549 tty->print_cr("Verifying class %s with new format", 550 _klass->external_name()); 551 } 552 553 Array<Method*>* methods = _klass->methods(); 554 int num_methods = methods->length(); 555 556 for (int index = 0; index < num_methods; index++) { 557 // Check for recursive re-verification before each method. 558 if (was_recursively_verified()) return; 559 560 Method* m = methods->at(index); 561 if (m->is_native() || m->is_abstract() || m->is_overpass()) { 562 // If m is native or abstract, skip it. It is checked in class file 563 // parser that methods do not override a final method. Overpass methods 564 // are trusted since the VM generates them. 565 continue; 566 } 567 verify_method(methodHandle(THREAD, m), CHECK_VERIFY(this)); 568 } 569 570 if (VerboseVerification || TraceClassInitialization) { 571 if (was_recursively_verified()) 572 tty->print_cr("Recursive verification detected for: %s", 573 _klass->external_name()); 574 } 575 } 576 577 void ClassVerifier::verify_method(methodHandle m, TRAPS) { 578 HandleMark hm(THREAD); 579 _method = m; // initialize _method 580 if (VerboseVerification) { 581 tty->print_cr("Verifying method %s", m->name_and_sig_as_C_string()); 582 } 583 584 // For clang, the only good constant format string is a literal constant format string. 585 #define bad_type_msg "Bad type on operand stack in %s" 586 587 int32_t max_stack = m->verifier_max_stack(); 588 int32_t max_locals = m->max_locals(); 589 constantPoolHandle cp(THREAD, m->constants()); 590 591 if (!SignatureVerifier::is_valid_method_signature(m->signature())) { 592 class_format_error("Invalid method signature"); 593 return; 594 } 595 596 // Initial stack map frame: offset is 0, stack is initially empty. 597 StackMapFrame current_frame(max_locals, max_stack, this); 598 // Set initial locals 599 VerificationType return_type = current_frame.set_locals_from_arg( 600 m, current_type(), CHECK_VERIFY(this)); 601 602 int32_t stackmap_index = 0; // index to the stackmap array 603 604 u4 code_length = m->code_size(); 605 606 // Scan the bytecode and map each instruction's start offset to a number. 607 char* code_data = generate_code_data(m, code_length, CHECK_VERIFY(this)); 608 609 int ex_min = code_length; 610 int ex_max = -1; 611 // Look through each item on the exception table. Each of the fields must refer 612 // to a legal instruction. 613 verify_exception_handler_table( 614 code_length, code_data, ex_min, ex_max, CHECK_VERIFY(this)); 615 616 // Look through each entry on the local variable table and make sure 617 // its range of code array offsets is valid. (4169817) 618 if (m->has_localvariable_table()) { 619 verify_local_variable_table(code_length, code_data, CHECK_VERIFY(this)); 620 } 621 622 Array<u1>* stackmap_data = m->stackmap_data(); 623 StackMapStream stream(stackmap_data); 624 StackMapReader reader(this, &stream, code_data, code_length, THREAD); 625 StackMapTable stackmap_table(&reader, ¤t_frame, max_locals, max_stack, 626 code_data, code_length, CHECK_VERIFY(this)); 627 628 if (VerboseVerification) { 629 stackmap_table.print_on(tty); 630 } 631 632 RawBytecodeStream bcs(m); 633 634 // Scan the byte code linearly from the start to the end 635 bool no_control_flow = false; // Set to true when there is no direct control 636 // flow from current instruction to the next 637 // instruction in sequence 638 639 Bytecodes::Code opcode; 640 while (!bcs.is_last_bytecode()) { 641 // Check for recursive re-verification before each bytecode. 642 if (was_recursively_verified()) return; 643 644 opcode = bcs.raw_next(); 645 u2 bci = bcs.bci(); 646 647 // Set current frame's offset to bci 648 current_frame.set_offset(bci); 649 current_frame.set_mark(); 650 651 // Make sure every offset in stackmap table point to the beginning to 652 // an instruction. Match current_frame to stackmap_table entry with 653 // the same offset if exists. 654 stackmap_index = verify_stackmap_table( 655 stackmap_index, bci, ¤t_frame, &stackmap_table, 656 no_control_flow, CHECK_VERIFY(this)); 657 658 659 bool this_uninit = false; // Set to true when invokespecial <init> initialized 'this' 660 661 // Merge with the next instruction 662 { 663 u2 index; 664 int target; 665 VerificationType type, type2; 666 VerificationType atype; 667 668 #ifndef PRODUCT 669 if (VerboseVerification) { 670 current_frame.print_on(tty); 671 tty->print_cr("offset = %d, opcode = %s", bci, Bytecodes::name(opcode)); 672 } 673 #endif 674 675 // Make sure wide instruction is in correct format 676 if (bcs.is_wide()) { 677 if (opcode != Bytecodes::_iinc && opcode != Bytecodes::_iload && 678 opcode != Bytecodes::_aload && opcode != Bytecodes::_lload && 679 opcode != Bytecodes::_istore && opcode != Bytecodes::_astore && 680 opcode != Bytecodes::_lstore && opcode != Bytecodes::_fload && 681 opcode != Bytecodes::_dload && opcode != Bytecodes::_fstore && 682 opcode != Bytecodes::_dstore) { 683 /* Unreachable? RawBytecodeStream's raw_next() returns 'illegal' 684 * if we encounter a wide instruction that modifies an invalid 685 * opcode (not one of the ones listed above) */ 686 verify_error(ErrorContext::bad_code(bci), "Bad wide instruction"); 687 return; 688 } 689 } 690 691 switch (opcode) { 692 case Bytecodes::_nop : 693 no_control_flow = false; break; 694 case Bytecodes::_aconst_null : 695 current_frame.push_stack( 696 VerificationType::null_type(), CHECK_VERIFY(this)); 697 no_control_flow = false; break; 698 case Bytecodes::_iconst_m1 : 699 case Bytecodes::_iconst_0 : 700 case Bytecodes::_iconst_1 : 701 case Bytecodes::_iconst_2 : 702 case Bytecodes::_iconst_3 : 703 case Bytecodes::_iconst_4 : 704 case Bytecodes::_iconst_5 : 705 current_frame.push_stack( 706 VerificationType::integer_type(), CHECK_VERIFY(this)); 707 no_control_flow = false; break; 708 case Bytecodes::_lconst_0 : 709 case Bytecodes::_lconst_1 : 710 current_frame.push_stack_2( 711 VerificationType::long_type(), 712 VerificationType::long2_type(), CHECK_VERIFY(this)); 713 no_control_flow = false; break; 714 case Bytecodes::_fconst_0 : 715 case Bytecodes::_fconst_1 : 716 case Bytecodes::_fconst_2 : 717 current_frame.push_stack( 718 VerificationType::float_type(), CHECK_VERIFY(this)); 719 no_control_flow = false; break; 720 case Bytecodes::_dconst_0 : 721 case Bytecodes::_dconst_1 : 722 current_frame.push_stack_2( 723 VerificationType::double_type(), 724 VerificationType::double2_type(), CHECK_VERIFY(this)); 725 no_control_flow = false; break; 726 case Bytecodes::_sipush : 727 case Bytecodes::_bipush : 728 current_frame.push_stack( 729 VerificationType::integer_type(), CHECK_VERIFY(this)); 730 no_control_flow = false; break; 731 case Bytecodes::_ldc : 732 verify_ldc( 733 opcode, bcs.get_index_u1(), ¤t_frame, 734 cp, bci, CHECK_VERIFY(this)); 735 no_control_flow = false; break; 736 case Bytecodes::_ldc_w : 737 case Bytecodes::_ldc2_w : 738 verify_ldc( 739 opcode, bcs.get_index_u2(), ¤t_frame, 740 cp, bci, CHECK_VERIFY(this)); 741 no_control_flow = false; break; 742 case Bytecodes::_iload : 743 verify_iload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 744 no_control_flow = false; break; 745 case Bytecodes::_iload_0 : 746 case Bytecodes::_iload_1 : 747 case Bytecodes::_iload_2 : 748 case Bytecodes::_iload_3 : 749 index = opcode - Bytecodes::_iload_0; 750 verify_iload(index, ¤t_frame, CHECK_VERIFY(this)); 751 no_control_flow = false; break; 752 case Bytecodes::_lload : 753 verify_lload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 754 no_control_flow = false; break; 755 case Bytecodes::_lload_0 : 756 case Bytecodes::_lload_1 : 757 case Bytecodes::_lload_2 : 758 case Bytecodes::_lload_3 : 759 index = opcode - Bytecodes::_lload_0; 760 verify_lload(index, ¤t_frame, CHECK_VERIFY(this)); 761 no_control_flow = false; break; 762 case Bytecodes::_fload : 763 verify_fload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 764 no_control_flow = false; break; 765 case Bytecodes::_fload_0 : 766 case Bytecodes::_fload_1 : 767 case Bytecodes::_fload_2 : 768 case Bytecodes::_fload_3 : 769 index = opcode - Bytecodes::_fload_0; 770 verify_fload(index, ¤t_frame, CHECK_VERIFY(this)); 771 no_control_flow = false; break; 772 case Bytecodes::_dload : 773 verify_dload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 774 no_control_flow = false; break; 775 case Bytecodes::_dload_0 : 776 case Bytecodes::_dload_1 : 777 case Bytecodes::_dload_2 : 778 case Bytecodes::_dload_3 : 779 index = opcode - Bytecodes::_dload_0; 780 verify_dload(index, ¤t_frame, CHECK_VERIFY(this)); 781 no_control_flow = false; break; 782 case Bytecodes::_aload : 783 verify_aload(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 784 no_control_flow = false; break; 785 case Bytecodes::_aload_0 : 786 case Bytecodes::_aload_1 : 787 case Bytecodes::_aload_2 : 788 case Bytecodes::_aload_3 : 789 index = opcode - Bytecodes::_aload_0; 790 verify_aload(index, ¤t_frame, CHECK_VERIFY(this)); 791 no_control_flow = false; break; 792 case Bytecodes::_iaload : 793 type = current_frame.pop_stack( 794 VerificationType::integer_type(), CHECK_VERIFY(this)); 795 atype = current_frame.pop_stack( 796 VerificationType::reference_check(), CHECK_VERIFY(this)); 797 if (!atype.is_int_array()) { 798 verify_error(ErrorContext::bad_type(bci, 799 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)), 800 bad_type_msg, "iaload"); 801 return; 802 } 803 current_frame.push_stack( 804 VerificationType::integer_type(), CHECK_VERIFY(this)); 805 no_control_flow = false; break; 806 case Bytecodes::_baload : 807 type = current_frame.pop_stack( 808 VerificationType::integer_type(), CHECK_VERIFY(this)); 809 atype = current_frame.pop_stack( 810 VerificationType::reference_check(), CHECK_VERIFY(this)); 811 if (!atype.is_bool_array() && !atype.is_byte_array()) { 812 verify_error( 813 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 814 bad_type_msg, "baload"); 815 return; 816 } 817 current_frame.push_stack( 818 VerificationType::integer_type(), CHECK_VERIFY(this)); 819 no_control_flow = false; break; 820 case Bytecodes::_caload : 821 type = current_frame.pop_stack( 822 VerificationType::integer_type(), CHECK_VERIFY(this)); 823 atype = current_frame.pop_stack( 824 VerificationType::reference_check(), CHECK_VERIFY(this)); 825 if (!atype.is_char_array()) { 826 verify_error(ErrorContext::bad_type(bci, 827 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)), 828 bad_type_msg, "caload"); 829 return; 830 } 831 current_frame.push_stack( 832 VerificationType::integer_type(), CHECK_VERIFY(this)); 833 no_control_flow = false; break; 834 case Bytecodes::_saload : 835 type = current_frame.pop_stack( 836 VerificationType::integer_type(), CHECK_VERIFY(this)); 837 atype = current_frame.pop_stack( 838 VerificationType::reference_check(), CHECK_VERIFY(this)); 839 if (!atype.is_short_array()) { 840 verify_error(ErrorContext::bad_type(bci, 841 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)), 842 bad_type_msg, "saload"); 843 return; 844 } 845 current_frame.push_stack( 846 VerificationType::integer_type(), CHECK_VERIFY(this)); 847 no_control_flow = false; break; 848 case Bytecodes::_laload : 849 type = current_frame.pop_stack( 850 VerificationType::integer_type(), CHECK_VERIFY(this)); 851 atype = current_frame.pop_stack( 852 VerificationType::reference_check(), CHECK_VERIFY(this)); 853 if (!atype.is_long_array()) { 854 verify_error(ErrorContext::bad_type(bci, 855 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)), 856 bad_type_msg, "laload"); 857 return; 858 } 859 current_frame.push_stack_2( 860 VerificationType::long_type(), 861 VerificationType::long2_type(), CHECK_VERIFY(this)); 862 no_control_flow = false; break; 863 case Bytecodes::_faload : 864 type = current_frame.pop_stack( 865 VerificationType::integer_type(), CHECK_VERIFY(this)); 866 atype = current_frame.pop_stack( 867 VerificationType::reference_check(), CHECK_VERIFY(this)); 868 if (!atype.is_float_array()) { 869 verify_error(ErrorContext::bad_type(bci, 870 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)), 871 bad_type_msg, "faload"); 872 return; 873 } 874 current_frame.push_stack( 875 VerificationType::float_type(), CHECK_VERIFY(this)); 876 no_control_flow = false; break; 877 case Bytecodes::_daload : 878 type = current_frame.pop_stack( 879 VerificationType::integer_type(), CHECK_VERIFY(this)); 880 atype = current_frame.pop_stack( 881 VerificationType::reference_check(), CHECK_VERIFY(this)); 882 if (!atype.is_double_array()) { 883 verify_error(ErrorContext::bad_type(bci, 884 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)), 885 bad_type_msg, "daload"); 886 return; 887 } 888 current_frame.push_stack_2( 889 VerificationType::double_type(), 890 VerificationType::double2_type(), CHECK_VERIFY(this)); 891 no_control_flow = false; break; 892 case Bytecodes::_aaload : { 893 type = current_frame.pop_stack( 894 VerificationType::integer_type(), CHECK_VERIFY(this)); 895 atype = current_frame.pop_stack( 896 VerificationType::reference_check(), CHECK_VERIFY(this)); 897 if (!atype.is_reference_array()) { 898 verify_error(ErrorContext::bad_type(bci, 899 current_frame.stack_top_ctx(), 900 TypeOrigin::implicit(VerificationType::reference_check())), 901 bad_type_msg, "aaload"); 902 return; 903 } 904 if (atype.is_null()) { 905 current_frame.push_stack( 906 VerificationType::null_type(), CHECK_VERIFY(this)); 907 } else { 908 VerificationType component = 909 atype.get_component(this, CHECK_VERIFY(this)); 910 current_frame.push_stack(component, CHECK_VERIFY(this)); 911 } 912 no_control_flow = false; break; 913 } 914 case Bytecodes::_istore : 915 verify_istore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 916 no_control_flow = false; break; 917 case Bytecodes::_istore_0 : 918 case Bytecodes::_istore_1 : 919 case Bytecodes::_istore_2 : 920 case Bytecodes::_istore_3 : 921 index = opcode - Bytecodes::_istore_0; 922 verify_istore(index, ¤t_frame, CHECK_VERIFY(this)); 923 no_control_flow = false; break; 924 case Bytecodes::_lstore : 925 verify_lstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 926 no_control_flow = false; break; 927 case Bytecodes::_lstore_0 : 928 case Bytecodes::_lstore_1 : 929 case Bytecodes::_lstore_2 : 930 case Bytecodes::_lstore_3 : 931 index = opcode - Bytecodes::_lstore_0; 932 verify_lstore(index, ¤t_frame, CHECK_VERIFY(this)); 933 no_control_flow = false; break; 934 case Bytecodes::_fstore : 935 verify_fstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 936 no_control_flow = false; break; 937 case Bytecodes::_fstore_0 : 938 case Bytecodes::_fstore_1 : 939 case Bytecodes::_fstore_2 : 940 case Bytecodes::_fstore_3 : 941 index = opcode - Bytecodes::_fstore_0; 942 verify_fstore(index, ¤t_frame, CHECK_VERIFY(this)); 943 no_control_flow = false; break; 944 case Bytecodes::_dstore : 945 verify_dstore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 946 no_control_flow = false; break; 947 case Bytecodes::_dstore_0 : 948 case Bytecodes::_dstore_1 : 949 case Bytecodes::_dstore_2 : 950 case Bytecodes::_dstore_3 : 951 index = opcode - Bytecodes::_dstore_0; 952 verify_dstore(index, ¤t_frame, CHECK_VERIFY(this)); 953 no_control_flow = false; break; 954 case Bytecodes::_astore : 955 verify_astore(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 956 no_control_flow = false; break; 957 case Bytecodes::_astore_0 : 958 case Bytecodes::_astore_1 : 959 case Bytecodes::_astore_2 : 960 case Bytecodes::_astore_3 : 961 index = opcode - Bytecodes::_astore_0; 962 verify_astore(index, ¤t_frame, CHECK_VERIFY(this)); 963 no_control_flow = false; break; 964 case Bytecodes::_iastore : 965 type = current_frame.pop_stack( 966 VerificationType::integer_type(), CHECK_VERIFY(this)); 967 type2 = current_frame.pop_stack( 968 VerificationType::integer_type(), CHECK_VERIFY(this)); 969 atype = current_frame.pop_stack( 970 VerificationType::reference_check(), CHECK_VERIFY(this)); 971 if (!atype.is_int_array()) { 972 verify_error(ErrorContext::bad_type(bci, 973 current_frame.stack_top_ctx(), ref_ctx("[I", THREAD)), 974 bad_type_msg, "iastore"); 975 return; 976 } 977 no_control_flow = false; break; 978 case Bytecodes::_bastore : 979 type = current_frame.pop_stack( 980 VerificationType::integer_type(), CHECK_VERIFY(this)); 981 type2 = current_frame.pop_stack( 982 VerificationType::integer_type(), CHECK_VERIFY(this)); 983 atype = current_frame.pop_stack( 984 VerificationType::reference_check(), CHECK_VERIFY(this)); 985 if (!atype.is_bool_array() && !atype.is_byte_array()) { 986 verify_error( 987 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 988 bad_type_msg, "bastore"); 989 return; 990 } 991 no_control_flow = false; break; 992 case Bytecodes::_castore : 993 current_frame.pop_stack( 994 VerificationType::integer_type(), CHECK_VERIFY(this)); 995 current_frame.pop_stack( 996 VerificationType::integer_type(), CHECK_VERIFY(this)); 997 atype = current_frame.pop_stack( 998 VerificationType::reference_check(), CHECK_VERIFY(this)); 999 if (!atype.is_char_array()) { 1000 verify_error(ErrorContext::bad_type(bci, 1001 current_frame.stack_top_ctx(), ref_ctx("[C", THREAD)), 1002 bad_type_msg, "castore"); 1003 return; 1004 } 1005 no_control_flow = false; break; 1006 case Bytecodes::_sastore : 1007 current_frame.pop_stack( 1008 VerificationType::integer_type(), CHECK_VERIFY(this)); 1009 current_frame.pop_stack( 1010 VerificationType::integer_type(), CHECK_VERIFY(this)); 1011 atype = current_frame.pop_stack( 1012 VerificationType::reference_check(), CHECK_VERIFY(this)); 1013 if (!atype.is_short_array()) { 1014 verify_error(ErrorContext::bad_type(bci, 1015 current_frame.stack_top_ctx(), ref_ctx("[S", THREAD)), 1016 bad_type_msg, "sastore"); 1017 return; 1018 } 1019 no_control_flow = false; break; 1020 case Bytecodes::_lastore : 1021 current_frame.pop_stack_2( 1022 VerificationType::long2_type(), 1023 VerificationType::long_type(), CHECK_VERIFY(this)); 1024 current_frame.pop_stack( 1025 VerificationType::integer_type(), CHECK_VERIFY(this)); 1026 atype = current_frame.pop_stack( 1027 VerificationType::reference_check(), CHECK_VERIFY(this)); 1028 if (!atype.is_long_array()) { 1029 verify_error(ErrorContext::bad_type(bci, 1030 current_frame.stack_top_ctx(), ref_ctx("[J", THREAD)), 1031 bad_type_msg, "lastore"); 1032 return; 1033 } 1034 no_control_flow = false; break; 1035 case Bytecodes::_fastore : 1036 current_frame.pop_stack( 1037 VerificationType::float_type(), CHECK_VERIFY(this)); 1038 current_frame.pop_stack 1039 (VerificationType::integer_type(), CHECK_VERIFY(this)); 1040 atype = current_frame.pop_stack( 1041 VerificationType::reference_check(), CHECK_VERIFY(this)); 1042 if (!atype.is_float_array()) { 1043 verify_error(ErrorContext::bad_type(bci, 1044 current_frame.stack_top_ctx(), ref_ctx("[F", THREAD)), 1045 bad_type_msg, "fastore"); 1046 return; 1047 } 1048 no_control_flow = false; break; 1049 case Bytecodes::_dastore : 1050 current_frame.pop_stack_2( 1051 VerificationType::double2_type(), 1052 VerificationType::double_type(), CHECK_VERIFY(this)); 1053 current_frame.pop_stack( 1054 VerificationType::integer_type(), CHECK_VERIFY(this)); 1055 atype = current_frame.pop_stack( 1056 VerificationType::reference_check(), CHECK_VERIFY(this)); 1057 if (!atype.is_double_array()) { 1058 verify_error(ErrorContext::bad_type(bci, 1059 current_frame.stack_top_ctx(), ref_ctx("[D", THREAD)), 1060 bad_type_msg, "dastore"); 1061 return; 1062 } 1063 no_control_flow = false; break; 1064 case Bytecodes::_aastore : 1065 type = current_frame.pop_stack(object_type(), CHECK_VERIFY(this)); 1066 type2 = current_frame.pop_stack( 1067 VerificationType::integer_type(), CHECK_VERIFY(this)); 1068 atype = current_frame.pop_stack( 1069 VerificationType::reference_check(), CHECK_VERIFY(this)); 1070 // more type-checking is done at runtime 1071 if (!atype.is_reference_array()) { 1072 verify_error(ErrorContext::bad_type(bci, 1073 current_frame.stack_top_ctx(), 1074 TypeOrigin::implicit(VerificationType::reference_check())), 1075 bad_type_msg, "aastore"); 1076 return; 1077 } 1078 // 4938384: relaxed constraint in JVMS 3nd edition. 1079 no_control_flow = false; break; 1080 case Bytecodes::_pop : 1081 current_frame.pop_stack( 1082 VerificationType::category1_check(), CHECK_VERIFY(this)); 1083 no_control_flow = false; break; 1084 case Bytecodes::_pop2 : 1085 type = current_frame.pop_stack(CHECK_VERIFY(this)); 1086 if (type.is_category1()) { 1087 current_frame.pop_stack( 1088 VerificationType::category1_check(), CHECK_VERIFY(this)); 1089 } else if (type.is_category2_2nd()) { 1090 current_frame.pop_stack( 1091 VerificationType::category2_check(), CHECK_VERIFY(this)); 1092 } else { 1093 /* Unreachable? Would need a category2_1st on TOS 1094 * which does not appear possible. */ 1095 verify_error( 1096 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 1097 bad_type_msg, "pop2"); 1098 return; 1099 } 1100 no_control_flow = false; break; 1101 case Bytecodes::_dup : 1102 type = current_frame.pop_stack( 1103 VerificationType::category1_check(), CHECK_VERIFY(this)); 1104 current_frame.push_stack(type, CHECK_VERIFY(this)); 1105 current_frame.push_stack(type, CHECK_VERIFY(this)); 1106 no_control_flow = false; break; 1107 case Bytecodes::_dup_x1 : 1108 type = current_frame.pop_stack( 1109 VerificationType::category1_check(), CHECK_VERIFY(this)); 1110 type2 = current_frame.pop_stack( 1111 VerificationType::category1_check(), CHECK_VERIFY(this)); 1112 current_frame.push_stack(type, CHECK_VERIFY(this)); 1113 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1114 current_frame.push_stack(type, CHECK_VERIFY(this)); 1115 no_control_flow = false; break; 1116 case Bytecodes::_dup_x2 : 1117 { 1118 VerificationType type3; 1119 type = current_frame.pop_stack( 1120 VerificationType::category1_check(), CHECK_VERIFY(this)); 1121 type2 = current_frame.pop_stack(CHECK_VERIFY(this)); 1122 if (type2.is_category1()) { 1123 type3 = current_frame.pop_stack( 1124 VerificationType::category1_check(), CHECK_VERIFY(this)); 1125 } else if (type2.is_category2_2nd()) { 1126 type3 = current_frame.pop_stack( 1127 VerificationType::category2_check(), CHECK_VERIFY(this)); 1128 } else { 1129 /* Unreachable? Would need a category2_1st at stack depth 2 with 1130 * a category1 on TOS which does not appear possible. */ 1131 verify_error(ErrorContext::bad_type( 1132 bci, current_frame.stack_top_ctx()), bad_type_msg, "dup_x2"); 1133 return; 1134 } 1135 current_frame.push_stack(type, CHECK_VERIFY(this)); 1136 current_frame.push_stack(type3, CHECK_VERIFY(this)); 1137 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1138 current_frame.push_stack(type, CHECK_VERIFY(this)); 1139 no_control_flow = false; break; 1140 } 1141 case Bytecodes::_dup2 : 1142 type = current_frame.pop_stack(CHECK_VERIFY(this)); 1143 if (type.is_category1()) { 1144 type2 = current_frame.pop_stack( 1145 VerificationType::category1_check(), CHECK_VERIFY(this)); 1146 } else if (type.is_category2_2nd()) { 1147 type2 = current_frame.pop_stack( 1148 VerificationType::category2_check(), CHECK_VERIFY(this)); 1149 } else { 1150 /* Unreachable? Would need a category2_1st on TOS which does not 1151 * appear possible. */ 1152 verify_error( 1153 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 1154 bad_type_msg, "dup2"); 1155 return; 1156 } 1157 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1158 current_frame.push_stack(type, CHECK_VERIFY(this)); 1159 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1160 current_frame.push_stack(type, CHECK_VERIFY(this)); 1161 no_control_flow = false; break; 1162 case Bytecodes::_dup2_x1 : 1163 { 1164 VerificationType type3; 1165 type = current_frame.pop_stack(CHECK_VERIFY(this)); 1166 if (type.is_category1()) { 1167 type2 = current_frame.pop_stack( 1168 VerificationType::category1_check(), CHECK_VERIFY(this)); 1169 } else if (type.is_category2_2nd()) { 1170 type2 = current_frame.pop_stack( 1171 VerificationType::category2_check(), CHECK_VERIFY(this)); 1172 } else { 1173 /* Unreachable? Would need a category2_1st on TOS which does 1174 * not appear possible. */ 1175 verify_error( 1176 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 1177 bad_type_msg, "dup2_x1"); 1178 return; 1179 } 1180 type3 = current_frame.pop_stack( 1181 VerificationType::category1_check(), CHECK_VERIFY(this)); 1182 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1183 current_frame.push_stack(type, CHECK_VERIFY(this)); 1184 current_frame.push_stack(type3, CHECK_VERIFY(this)); 1185 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1186 current_frame.push_stack(type, CHECK_VERIFY(this)); 1187 no_control_flow = false; break; 1188 } 1189 case Bytecodes::_dup2_x2 : 1190 { 1191 VerificationType type3, type4; 1192 type = current_frame.pop_stack(CHECK_VERIFY(this)); 1193 if (type.is_category1()) { 1194 type2 = current_frame.pop_stack( 1195 VerificationType::category1_check(), CHECK_VERIFY(this)); 1196 } else if (type.is_category2_2nd()) { 1197 type2 = current_frame.pop_stack( 1198 VerificationType::category2_check(), CHECK_VERIFY(this)); 1199 } else { 1200 /* Unreachable? Would need a category2_1st on TOS which does 1201 * not appear possible. */ 1202 verify_error( 1203 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 1204 bad_type_msg, "dup2_x2"); 1205 return; 1206 } 1207 type3 = current_frame.pop_stack(CHECK_VERIFY(this)); 1208 if (type3.is_category1()) { 1209 type4 = current_frame.pop_stack( 1210 VerificationType::category1_check(), CHECK_VERIFY(this)); 1211 } else if (type3.is_category2_2nd()) { 1212 type4 = current_frame.pop_stack( 1213 VerificationType::category2_check(), CHECK_VERIFY(this)); 1214 } else { 1215 /* Unreachable? Would need a category2_1st on TOS after popping 1216 * a long/double or two category 1's, which does not 1217 * appear possible. */ 1218 verify_error( 1219 ErrorContext::bad_type(bci, current_frame.stack_top_ctx()), 1220 bad_type_msg, "dup2_x2"); 1221 return; 1222 } 1223 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1224 current_frame.push_stack(type, CHECK_VERIFY(this)); 1225 current_frame.push_stack(type4, CHECK_VERIFY(this)); 1226 current_frame.push_stack(type3, CHECK_VERIFY(this)); 1227 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1228 current_frame.push_stack(type, CHECK_VERIFY(this)); 1229 no_control_flow = false; break; 1230 } 1231 case Bytecodes::_swap : 1232 type = current_frame.pop_stack( 1233 VerificationType::category1_check(), CHECK_VERIFY(this)); 1234 type2 = current_frame.pop_stack( 1235 VerificationType::category1_check(), CHECK_VERIFY(this)); 1236 current_frame.push_stack(type, CHECK_VERIFY(this)); 1237 current_frame.push_stack(type2, CHECK_VERIFY(this)); 1238 no_control_flow = false; break; 1239 case Bytecodes::_iadd : 1240 case Bytecodes::_isub : 1241 case Bytecodes::_imul : 1242 case Bytecodes::_idiv : 1243 case Bytecodes::_irem : 1244 case Bytecodes::_ishl : 1245 case Bytecodes::_ishr : 1246 case Bytecodes::_iushr : 1247 case Bytecodes::_ior : 1248 case Bytecodes::_ixor : 1249 case Bytecodes::_iand : 1250 current_frame.pop_stack( 1251 VerificationType::integer_type(), CHECK_VERIFY(this)); 1252 // fall through 1253 case Bytecodes::_ineg : 1254 current_frame.pop_stack( 1255 VerificationType::integer_type(), CHECK_VERIFY(this)); 1256 current_frame.push_stack( 1257 VerificationType::integer_type(), CHECK_VERIFY(this)); 1258 no_control_flow = false; break; 1259 case Bytecodes::_ladd : 1260 case Bytecodes::_lsub : 1261 case Bytecodes::_lmul : 1262 case Bytecodes::_ldiv : 1263 case Bytecodes::_lrem : 1264 case Bytecodes::_land : 1265 case Bytecodes::_lor : 1266 case Bytecodes::_lxor : 1267 current_frame.pop_stack_2( 1268 VerificationType::long2_type(), 1269 VerificationType::long_type(), CHECK_VERIFY(this)); 1270 // fall through 1271 case Bytecodes::_lneg : 1272 current_frame.pop_stack_2( 1273 VerificationType::long2_type(), 1274 VerificationType::long_type(), CHECK_VERIFY(this)); 1275 current_frame.push_stack_2( 1276 VerificationType::long_type(), 1277 VerificationType::long2_type(), CHECK_VERIFY(this)); 1278 no_control_flow = false; break; 1279 case Bytecodes::_lshl : 1280 case Bytecodes::_lshr : 1281 case Bytecodes::_lushr : 1282 current_frame.pop_stack( 1283 VerificationType::integer_type(), CHECK_VERIFY(this)); 1284 current_frame.pop_stack_2( 1285 VerificationType::long2_type(), 1286 VerificationType::long_type(), CHECK_VERIFY(this)); 1287 current_frame.push_stack_2( 1288 VerificationType::long_type(), 1289 VerificationType::long2_type(), CHECK_VERIFY(this)); 1290 no_control_flow = false; break; 1291 case Bytecodes::_fadd : 1292 case Bytecodes::_fsub : 1293 case Bytecodes::_fmul : 1294 case Bytecodes::_fdiv : 1295 case Bytecodes::_frem : 1296 current_frame.pop_stack( 1297 VerificationType::float_type(), CHECK_VERIFY(this)); 1298 // fall through 1299 case Bytecodes::_fneg : 1300 current_frame.pop_stack( 1301 VerificationType::float_type(), CHECK_VERIFY(this)); 1302 current_frame.push_stack( 1303 VerificationType::float_type(), CHECK_VERIFY(this)); 1304 no_control_flow = false; break; 1305 case Bytecodes::_dadd : 1306 case Bytecodes::_dsub : 1307 case Bytecodes::_dmul : 1308 case Bytecodes::_ddiv : 1309 case Bytecodes::_drem : 1310 current_frame.pop_stack_2( 1311 VerificationType::double2_type(), 1312 VerificationType::double_type(), CHECK_VERIFY(this)); 1313 // fall through 1314 case Bytecodes::_dneg : 1315 current_frame.pop_stack_2( 1316 VerificationType::double2_type(), 1317 VerificationType::double_type(), CHECK_VERIFY(this)); 1318 current_frame.push_stack_2( 1319 VerificationType::double_type(), 1320 VerificationType::double2_type(), CHECK_VERIFY(this)); 1321 no_control_flow = false; break; 1322 case Bytecodes::_iinc : 1323 verify_iinc(bcs.get_index(), ¤t_frame, CHECK_VERIFY(this)); 1324 no_control_flow = false; break; 1325 case Bytecodes::_i2l : 1326 type = current_frame.pop_stack( 1327 VerificationType::integer_type(), CHECK_VERIFY(this)); 1328 current_frame.push_stack_2( 1329 VerificationType::long_type(), 1330 VerificationType::long2_type(), CHECK_VERIFY(this)); 1331 no_control_flow = false; break; 1332 case Bytecodes::_l2i : 1333 current_frame.pop_stack_2( 1334 VerificationType::long2_type(), 1335 VerificationType::long_type(), CHECK_VERIFY(this)); 1336 current_frame.push_stack( 1337 VerificationType::integer_type(), CHECK_VERIFY(this)); 1338 no_control_flow = false; break; 1339 case Bytecodes::_i2f : 1340 current_frame.pop_stack( 1341 VerificationType::integer_type(), CHECK_VERIFY(this)); 1342 current_frame.push_stack( 1343 VerificationType::float_type(), CHECK_VERIFY(this)); 1344 no_control_flow = false; break; 1345 case Bytecodes::_i2d : 1346 current_frame.pop_stack( 1347 VerificationType::integer_type(), CHECK_VERIFY(this)); 1348 current_frame.push_stack_2( 1349 VerificationType::double_type(), 1350 VerificationType::double2_type(), CHECK_VERIFY(this)); 1351 no_control_flow = false; break; 1352 case Bytecodes::_l2f : 1353 current_frame.pop_stack_2( 1354 VerificationType::long2_type(), 1355 VerificationType::long_type(), CHECK_VERIFY(this)); 1356 current_frame.push_stack( 1357 VerificationType::float_type(), CHECK_VERIFY(this)); 1358 no_control_flow = false; break; 1359 case Bytecodes::_l2d : 1360 current_frame.pop_stack_2( 1361 VerificationType::long2_type(), 1362 VerificationType::long_type(), CHECK_VERIFY(this)); 1363 current_frame.push_stack_2( 1364 VerificationType::double_type(), 1365 VerificationType::double2_type(), CHECK_VERIFY(this)); 1366 no_control_flow = false; break; 1367 case Bytecodes::_f2i : 1368 current_frame.pop_stack( 1369 VerificationType::float_type(), CHECK_VERIFY(this)); 1370 current_frame.push_stack( 1371 VerificationType::integer_type(), CHECK_VERIFY(this)); 1372 no_control_flow = false; break; 1373 case Bytecodes::_f2l : 1374 current_frame.pop_stack( 1375 VerificationType::float_type(), CHECK_VERIFY(this)); 1376 current_frame.push_stack_2( 1377 VerificationType::long_type(), 1378 VerificationType::long2_type(), CHECK_VERIFY(this)); 1379 no_control_flow = false; break; 1380 case Bytecodes::_f2d : 1381 current_frame.pop_stack( 1382 VerificationType::float_type(), CHECK_VERIFY(this)); 1383 current_frame.push_stack_2( 1384 VerificationType::double_type(), 1385 VerificationType::double2_type(), CHECK_VERIFY(this)); 1386 no_control_flow = false; break; 1387 case Bytecodes::_d2i : 1388 current_frame.pop_stack_2( 1389 VerificationType::double2_type(), 1390 VerificationType::double_type(), CHECK_VERIFY(this)); 1391 current_frame.push_stack( 1392 VerificationType::integer_type(), CHECK_VERIFY(this)); 1393 no_control_flow = false; break; 1394 case Bytecodes::_d2l : 1395 current_frame.pop_stack_2( 1396 VerificationType::double2_type(), 1397 VerificationType::double_type(), CHECK_VERIFY(this)); 1398 current_frame.push_stack_2( 1399 VerificationType::long_type(), 1400 VerificationType::long2_type(), CHECK_VERIFY(this)); 1401 no_control_flow = false; break; 1402 case Bytecodes::_d2f : 1403 current_frame.pop_stack_2( 1404 VerificationType::double2_type(), 1405 VerificationType::double_type(), CHECK_VERIFY(this)); 1406 current_frame.push_stack( 1407 VerificationType::float_type(), CHECK_VERIFY(this)); 1408 no_control_flow = false; break; 1409 case Bytecodes::_i2b : 1410 case Bytecodes::_i2c : 1411 case Bytecodes::_i2s : 1412 current_frame.pop_stack( 1413 VerificationType::integer_type(), CHECK_VERIFY(this)); 1414 current_frame.push_stack( 1415 VerificationType::integer_type(), CHECK_VERIFY(this)); 1416 no_control_flow = false; break; 1417 case Bytecodes::_lcmp : 1418 current_frame.pop_stack_2( 1419 VerificationType::long2_type(), 1420 VerificationType::long_type(), CHECK_VERIFY(this)); 1421 current_frame.pop_stack_2( 1422 VerificationType::long2_type(), 1423 VerificationType::long_type(), CHECK_VERIFY(this)); 1424 current_frame.push_stack( 1425 VerificationType::integer_type(), CHECK_VERIFY(this)); 1426 no_control_flow = false; break; 1427 case Bytecodes::_fcmpl : 1428 case Bytecodes::_fcmpg : 1429 current_frame.pop_stack( 1430 VerificationType::float_type(), CHECK_VERIFY(this)); 1431 current_frame.pop_stack( 1432 VerificationType::float_type(), CHECK_VERIFY(this)); 1433 current_frame.push_stack( 1434 VerificationType::integer_type(), CHECK_VERIFY(this)); 1435 no_control_flow = false; break; 1436 case Bytecodes::_dcmpl : 1437 case Bytecodes::_dcmpg : 1438 current_frame.pop_stack_2( 1439 VerificationType::double2_type(), 1440 VerificationType::double_type(), CHECK_VERIFY(this)); 1441 current_frame.pop_stack_2( 1442 VerificationType::double2_type(), 1443 VerificationType::double_type(), CHECK_VERIFY(this)); 1444 current_frame.push_stack( 1445 VerificationType::integer_type(), CHECK_VERIFY(this)); 1446 no_control_flow = false; break; 1447 case Bytecodes::_if_icmpeq: 1448 case Bytecodes::_if_icmpne: 1449 case Bytecodes::_if_icmplt: 1450 case Bytecodes::_if_icmpge: 1451 case Bytecodes::_if_icmpgt: 1452 case Bytecodes::_if_icmple: 1453 current_frame.pop_stack( 1454 VerificationType::integer_type(), CHECK_VERIFY(this)); 1455 // fall through 1456 case Bytecodes::_ifeq: 1457 case Bytecodes::_ifne: 1458 case Bytecodes::_iflt: 1459 case Bytecodes::_ifge: 1460 case Bytecodes::_ifgt: 1461 case Bytecodes::_ifle: 1462 current_frame.pop_stack( 1463 VerificationType::integer_type(), CHECK_VERIFY(this)); 1464 target = bcs.dest(); 1465 stackmap_table.check_jump_target( 1466 ¤t_frame, target, CHECK_VERIFY(this)); 1467 no_control_flow = false; break; 1468 case Bytecodes::_if_acmpeq : 1469 case Bytecodes::_if_acmpne : 1470 current_frame.pop_stack( 1471 VerificationType::reference_check(), CHECK_VERIFY(this)); 1472 // fall through 1473 case Bytecodes::_ifnull : 1474 case Bytecodes::_ifnonnull : 1475 current_frame.pop_stack( 1476 VerificationType::reference_check(), CHECK_VERIFY(this)); 1477 target = bcs.dest(); 1478 stackmap_table.check_jump_target 1479 (¤t_frame, target, CHECK_VERIFY(this)); 1480 no_control_flow = false; break; 1481 case Bytecodes::_goto : 1482 target = bcs.dest(); 1483 stackmap_table.check_jump_target( 1484 ¤t_frame, target, CHECK_VERIFY(this)); 1485 no_control_flow = true; break; 1486 case Bytecodes::_goto_w : 1487 target = bcs.dest_w(); 1488 stackmap_table.check_jump_target( 1489 ¤t_frame, target, CHECK_VERIFY(this)); 1490 no_control_flow = true; break; 1491 case Bytecodes::_tableswitch : 1492 case Bytecodes::_lookupswitch : 1493 verify_switch( 1494 &bcs, code_length, code_data, ¤t_frame, 1495 &stackmap_table, CHECK_VERIFY(this)); 1496 no_control_flow = true; break; 1497 case Bytecodes::_ireturn : 1498 type = current_frame.pop_stack( 1499 VerificationType::integer_type(), CHECK_VERIFY(this)); 1500 verify_return_value(return_type, type, bci, 1501 ¤t_frame, CHECK_VERIFY(this)); 1502 no_control_flow = true; break; 1503 case Bytecodes::_lreturn : 1504 type2 = current_frame.pop_stack( 1505 VerificationType::long2_type(), CHECK_VERIFY(this)); 1506 type = current_frame.pop_stack( 1507 VerificationType::long_type(), CHECK_VERIFY(this)); 1508 verify_return_value(return_type, type, bci, 1509 ¤t_frame, CHECK_VERIFY(this)); 1510 no_control_flow = true; break; 1511 case Bytecodes::_freturn : 1512 type = current_frame.pop_stack( 1513 VerificationType::float_type(), CHECK_VERIFY(this)); 1514 verify_return_value(return_type, type, bci, 1515 ¤t_frame, CHECK_VERIFY(this)); 1516 no_control_flow = true; break; 1517 case Bytecodes::_dreturn : 1518 type2 = current_frame.pop_stack( 1519 VerificationType::double2_type(), CHECK_VERIFY(this)); 1520 type = current_frame.pop_stack( 1521 VerificationType::double_type(), CHECK_VERIFY(this)); 1522 verify_return_value(return_type, type, bci, 1523 ¤t_frame, CHECK_VERIFY(this)); 1524 no_control_flow = true; break; 1525 case Bytecodes::_areturn : 1526 type = current_frame.pop_stack( 1527 VerificationType::reference_check(), CHECK_VERIFY(this)); 1528 verify_return_value(return_type, type, bci, 1529 ¤t_frame, CHECK_VERIFY(this)); 1530 no_control_flow = true; break; 1531 case Bytecodes::_return : 1532 if (return_type != VerificationType::bogus_type()) { 1533 verify_error(ErrorContext::bad_code(bci), 1534 "Method expects a return value"); 1535 return; 1536 } 1537 // Make sure "this" has been initialized if current method is an 1538 // <init> 1539 if (_method->name() == vmSymbols::object_initializer_name() && 1540 current_frame.flag_this_uninit()) { 1541 verify_error(ErrorContext::bad_code(bci), 1542 "Constructor must call super() or this() " 1543 "before return"); 1544 return; 1545 } 1546 no_control_flow = true; break; 1547 case Bytecodes::_getstatic : 1548 case Bytecodes::_putstatic : 1549 // pass TRUE, operand can be an array type for getstatic/putstatic. 1550 verify_field_instructions( 1551 &bcs, ¤t_frame, cp, true, CHECK_VERIFY(this)); 1552 no_control_flow = false; break; 1553 case Bytecodes::_getfield : 1554 case Bytecodes::_putfield : 1555 // pass FALSE, operand can't be an array type for getfield/putfield. 1556 verify_field_instructions( 1557 &bcs, ¤t_frame, cp, false, CHECK_VERIFY(this)); 1558 no_control_flow = false; break; 1559 case Bytecodes::_invokevirtual : 1560 case Bytecodes::_invokespecial : 1561 case Bytecodes::_invokestatic : 1562 verify_invoke_instructions( 1563 &bcs, code_length, ¤t_frame, 1564 &this_uninit, return_type, cp, CHECK_VERIFY(this)); 1565 no_control_flow = false; break; 1566 case Bytecodes::_invokeinterface : 1567 case Bytecodes::_invokedynamic : 1568 verify_invoke_instructions( 1569 &bcs, code_length, ¤t_frame, 1570 &this_uninit, return_type, cp, CHECK_VERIFY(this)); 1571 no_control_flow = false; break; 1572 case Bytecodes::_new : 1573 { 1574 index = bcs.get_index_u2(); 1575 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this)); 1576 VerificationType new_class_type = 1577 cp_index_to_type(index, cp, CHECK_VERIFY(this)); 1578 if (!new_class_type.is_object()) { 1579 verify_error(ErrorContext::bad_type(bci, 1580 TypeOrigin::cp(index, new_class_type)), 1581 "Illegal new instruction"); 1582 return; 1583 } 1584 type = VerificationType::uninitialized_type(bci); 1585 current_frame.push_stack(type, CHECK_VERIFY(this)); 1586 no_control_flow = false; break; 1587 } 1588 case Bytecodes::_newarray : 1589 type = get_newarray_type(bcs.get_index(), bci, CHECK_VERIFY(this)); 1590 current_frame.pop_stack( 1591 VerificationType::integer_type(), CHECK_VERIFY(this)); 1592 current_frame.push_stack(type, CHECK_VERIFY(this)); 1593 no_control_flow = false; break; 1594 case Bytecodes::_anewarray : 1595 verify_anewarray( 1596 bci, bcs.get_index_u2(), cp, ¤t_frame, CHECK_VERIFY(this)); 1597 no_control_flow = false; break; 1598 case Bytecodes::_arraylength : 1599 type = current_frame.pop_stack( 1600 VerificationType::reference_check(), CHECK_VERIFY(this)); 1601 if (!(type.is_null() || type.is_array())) { 1602 verify_error(ErrorContext::bad_type( 1603 bci, current_frame.stack_top_ctx()), 1604 bad_type_msg, "arraylength"); 1605 } 1606 current_frame.push_stack( 1607 VerificationType::integer_type(), CHECK_VERIFY(this)); 1608 no_control_flow = false; break; 1609 case Bytecodes::_checkcast : 1610 { 1611 index = bcs.get_index_u2(); 1612 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this)); 1613 current_frame.pop_stack(object_type(), CHECK_VERIFY(this)); 1614 VerificationType klass_type = cp_index_to_type( 1615 index, cp, CHECK_VERIFY(this)); 1616 current_frame.push_stack(klass_type, CHECK_VERIFY(this)); 1617 no_control_flow = false; break; 1618 } 1619 case Bytecodes::_instanceof : { 1620 index = bcs.get_index_u2(); 1621 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this)); 1622 current_frame.pop_stack(object_type(), CHECK_VERIFY(this)); 1623 current_frame.push_stack( 1624 VerificationType::integer_type(), CHECK_VERIFY(this)); 1625 no_control_flow = false; break; 1626 } 1627 case Bytecodes::_monitorenter : 1628 case Bytecodes::_monitorexit : 1629 current_frame.pop_stack( 1630 VerificationType::reference_check(), CHECK_VERIFY(this)); 1631 no_control_flow = false; break; 1632 case Bytecodes::_multianewarray : 1633 { 1634 index = bcs.get_index_u2(); 1635 u2 dim = *(bcs.bcp()+3); 1636 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this)); 1637 VerificationType new_array_type = 1638 cp_index_to_type(index, cp, CHECK_VERIFY(this)); 1639 if (!new_array_type.is_array()) { 1640 verify_error(ErrorContext::bad_type(bci, 1641 TypeOrigin::cp(index, new_array_type)), 1642 "Illegal constant pool index in multianewarray instruction"); 1643 return; 1644 } 1645 if (dim < 1 || new_array_type.dimensions() < dim) { 1646 verify_error(ErrorContext::bad_code(bci), 1647 "Illegal dimension in multianewarray instruction: %d", dim); 1648 return; 1649 } 1650 for (int i = 0; i < dim; i++) { 1651 current_frame.pop_stack( 1652 VerificationType::integer_type(), CHECK_VERIFY(this)); 1653 } 1654 current_frame.push_stack(new_array_type, CHECK_VERIFY(this)); 1655 no_control_flow = false; break; 1656 } 1657 case Bytecodes::_athrow : 1658 type = VerificationType::reference_type( 1659 vmSymbols::java_lang_Throwable()); 1660 current_frame.pop_stack(type, CHECK_VERIFY(this)); 1661 no_control_flow = true; break; 1662 default: 1663 // We only need to check the valid bytecodes in class file. 1664 // And jsr and ret are not in the new class file format in JDK1.5. 1665 verify_error(ErrorContext::bad_code(bci), 1666 "Bad instruction: %02x", opcode); 1667 no_control_flow = false; 1668 return; 1669 } // end switch 1670 } // end Merge with the next instruction 1671 1672 // Look for possible jump target in exception handlers and see if it 1673 // matches current_frame 1674 if (bci >= ex_min && bci < ex_max) { 1675 verify_exception_handler_targets( 1676 bci, this_uninit, ¤t_frame, &stackmap_table, CHECK_VERIFY(this)); 1677 } 1678 } // end while 1679 1680 // Make sure that control flow does not fall through end of the method 1681 if (!no_control_flow) { 1682 verify_error(ErrorContext::bad_code(code_length), 1683 "Control flow falls through code end"); 1684 return; 1685 } 1686 } 1687 1688 #undef bad_type_message 1689 1690 char* ClassVerifier::generate_code_data(methodHandle m, u4 code_length, TRAPS) { 1691 char* code_data = NEW_RESOURCE_ARRAY(char, code_length); 1692 memset(code_data, 0, sizeof(char) * code_length); 1693 RawBytecodeStream bcs(m); 1694 1695 while (!bcs.is_last_bytecode()) { 1696 if (bcs.raw_next() != Bytecodes::_illegal) { 1697 int bci = bcs.bci(); 1698 if (bcs.raw_code() == Bytecodes::_new) { 1699 code_data[bci] = NEW_OFFSET; 1700 } else { 1701 code_data[bci] = BYTECODE_OFFSET; 1702 } 1703 } else { 1704 verify_error(ErrorContext::bad_code(bcs.bci()), "Bad instruction"); 1705 return NULL; 1706 } 1707 } 1708 1709 return code_data; 1710 } 1711 1712 void ClassVerifier::verify_exception_handler_table(u4 code_length, char* code_data, int& min, int& max, TRAPS) { 1713 ExceptionTable exhandlers(_method()); 1714 int exlength = exhandlers.length(); 1715 constantPoolHandle cp (THREAD, _method->constants()); 1716 1717 for(int i = 0; i < exlength; i++) { 1718 u2 start_pc = exhandlers.start_pc(i); 1719 u2 end_pc = exhandlers.end_pc(i); 1720 u2 handler_pc = exhandlers.handler_pc(i); 1721 if (start_pc >= code_length || code_data[start_pc] == 0) { 1722 class_format_error("Illegal exception table start_pc %d", start_pc); 1723 return; 1724 } 1725 if (end_pc != code_length) { // special case: end_pc == code_length 1726 if (end_pc > code_length || code_data[end_pc] == 0) { 1727 class_format_error("Illegal exception table end_pc %d", end_pc); 1728 return; 1729 } 1730 } 1731 if (handler_pc >= code_length || code_data[handler_pc] == 0) { 1732 class_format_error("Illegal exception table handler_pc %d", handler_pc); 1733 return; 1734 } 1735 int catch_type_index = exhandlers.catch_type_index(i); 1736 if (catch_type_index != 0) { 1737 VerificationType catch_type = cp_index_to_type( 1738 catch_type_index, cp, CHECK_VERIFY(this)); 1739 VerificationType throwable = 1740 VerificationType::reference_type(vmSymbols::java_lang_Throwable()); 1741 bool is_subclass = throwable.is_assignable_from( 1742 catch_type, this, false, CHECK_VERIFY(this)); 1743 if (!is_subclass) { 1744 // 4286534: should throw VerifyError according to recent spec change 1745 verify_error(ErrorContext::bad_type(handler_pc, 1746 TypeOrigin::cp(catch_type_index, catch_type), 1747 TypeOrigin::implicit(throwable)), 1748 "Catch type is not a subclass " 1749 "of Throwable in exception handler %d", handler_pc); 1750 return; 1751 } 1752 } 1753 if (start_pc < min) min = start_pc; 1754 if (end_pc > max) max = end_pc; 1755 } 1756 } 1757 1758 void ClassVerifier::verify_local_variable_table(u4 code_length, char* code_data, TRAPS) { 1759 int localvariable_table_length = _method()->localvariable_table_length(); 1760 if (localvariable_table_length > 0) { 1761 LocalVariableTableElement* table = _method()->localvariable_table_start(); 1762 for (int i = 0; i < localvariable_table_length; i++) { 1763 u2 start_bci = table[i].start_bci; 1764 u2 length = table[i].length; 1765 1766 if (start_bci >= code_length || code_data[start_bci] == 0) { 1767 class_format_error( 1768 "Illegal local variable table start_pc %d", start_bci); 1769 return; 1770 } 1771 u4 end_bci = (u4)(start_bci + length); 1772 if (end_bci != code_length) { 1773 if (end_bci >= code_length || code_data[end_bci] == 0) { 1774 class_format_error( "Illegal local variable table length %d", length); 1775 return; 1776 } 1777 } 1778 } 1779 } 1780 } 1781 1782 u2 ClassVerifier::verify_stackmap_table(u2 stackmap_index, u2 bci, 1783 StackMapFrame* current_frame, 1784 StackMapTable* stackmap_table, 1785 bool no_control_flow, TRAPS) { 1786 if (stackmap_index < stackmap_table->get_frame_count()) { 1787 u2 this_offset = stackmap_table->get_offset(stackmap_index); 1788 if (no_control_flow && this_offset > bci) { 1789 verify_error(ErrorContext::missing_stackmap(bci), 1790 "Expecting a stack map frame"); 1791 return 0; 1792 } 1793 if (this_offset == bci) { 1794 ErrorContext ctx; 1795 // See if current stack map can be assigned to the frame in table. 1796 // current_frame is the stackmap frame got from the last instruction. 1797 // If matched, current_frame will be updated by this method. 1798 bool matches = stackmap_table->match_stackmap( 1799 current_frame, this_offset, stackmap_index, 1800 !no_control_flow, true, false, &ctx, CHECK_VERIFY_(this, 0)); 1801 if (!matches) { 1802 // report type error 1803 verify_error(ctx, "Instruction type does not match stack map"); 1804 return 0; 1805 } 1806 stackmap_index++; 1807 } else if (this_offset < bci) { 1808 // current_offset should have met this_offset. 1809 class_format_error("Bad stack map offset %d", this_offset); 1810 return 0; 1811 } 1812 } else if (no_control_flow) { 1813 verify_error(ErrorContext::bad_code(bci), "Expecting a stack map frame"); 1814 return 0; 1815 } 1816 return stackmap_index; 1817 } 1818 1819 void ClassVerifier::verify_exception_handler_targets(u2 bci, bool this_uninit, StackMapFrame* current_frame, 1820 StackMapTable* stackmap_table, TRAPS) { 1821 constantPoolHandle cp (THREAD, _method->constants()); 1822 ExceptionTable exhandlers(_method()); 1823 int exlength = exhandlers.length(); 1824 for(int i = 0; i < exlength; i++) { 1825 u2 start_pc = exhandlers.start_pc(i); 1826 u2 end_pc = exhandlers.end_pc(i); 1827 u2 handler_pc = exhandlers.handler_pc(i); 1828 int catch_type_index = exhandlers.catch_type_index(i); 1829 if(bci >= start_pc && bci < end_pc) { 1830 u1 flags = current_frame->flags(); 1831 if (this_uninit) { flags |= FLAG_THIS_UNINIT; } 1832 StackMapFrame* new_frame = current_frame->frame_in_exception_handler(flags); 1833 if (catch_type_index != 0) { 1834 // We know that this index refers to a subclass of Throwable 1835 VerificationType catch_type = cp_index_to_type( 1836 catch_type_index, cp, CHECK_VERIFY(this)); 1837 new_frame->push_stack(catch_type, CHECK_VERIFY(this)); 1838 } else { 1839 VerificationType throwable = 1840 VerificationType::reference_type(vmSymbols::java_lang_Throwable()); 1841 new_frame->push_stack(throwable, CHECK_VERIFY(this)); 1842 } 1843 ErrorContext ctx; 1844 bool matches = stackmap_table->match_stackmap( 1845 new_frame, handler_pc, true, false, true, &ctx, CHECK_VERIFY(this)); 1846 if (!matches) { 1847 verify_error(ctx, "Stack map does not match the one at " 1848 "exception handler %d", handler_pc); 1849 return; 1850 } 1851 } 1852 } 1853 } 1854 1855 void ClassVerifier::verify_cp_index( 1856 u2 bci, constantPoolHandle cp, int index, TRAPS) { 1857 int nconstants = cp->length(); 1858 if ((index <= 0) || (index >= nconstants)) { 1859 verify_error(ErrorContext::bad_cp_index(bci, index), 1860 "Illegal constant pool index %d in class %s", 1861 index, cp->pool_holder()->external_name()); 1862 return; 1863 } 1864 } 1865 1866 void ClassVerifier::verify_cp_type( 1867 u2 bci, int index, constantPoolHandle cp, unsigned int types, TRAPS) { 1868 1869 // In some situations, bytecode rewriting may occur while we're verifying. 1870 // In this case, a constant pool cache exists and some indices refer to that 1871 // instead. Be sure we don't pick up such indices by accident. 1872 // We must check was_recursively_verified() before we get here. 1873 guarantee(cp->cache() == NULL, "not rewritten yet"); 1874 1875 verify_cp_index(bci, cp, index, CHECK_VERIFY(this)); 1876 unsigned int tag = cp->tag_at(index).value(); 1877 if ((types & (1 << tag)) == 0) { 1878 verify_error(ErrorContext::bad_cp_index(bci, index), 1879 "Illegal type at constant pool entry %d in class %s", 1880 index, cp->pool_holder()->external_name()); 1881 return; 1882 } 1883 } 1884 1885 void ClassVerifier::verify_cp_class_type( 1886 u2 bci, int index, constantPoolHandle cp, TRAPS) { 1887 verify_cp_index(bci, cp, index, CHECK_VERIFY(this)); 1888 constantTag tag = cp->tag_at(index); 1889 if (!tag.is_klass() && !tag.is_unresolved_klass()) { 1890 verify_error(ErrorContext::bad_cp_index(bci, index), 1891 "Illegal type at constant pool entry %d in class %s", 1892 index, cp->pool_holder()->external_name()); 1893 return; 1894 } 1895 } 1896 1897 void ClassVerifier::verify_error(ErrorContext ctx, const char* msg, ...) { 1898 stringStream ss; 1899 1900 ctx.reset_frames(); 1901 _exception_type = vmSymbols::java_lang_VerifyError(); 1902 _error_context = ctx; 1903 va_list va; 1904 va_start(va, msg); 1905 ss.vprint(msg, va); 1906 va_end(va); 1907 _message = ss.as_string(); 1908 #ifdef ASSERT 1909 ResourceMark rm; 1910 const char* exception_name = _exception_type->as_C_string(); 1911 Exceptions::debug_check_abort(exception_name, NULL); 1912 #endif // ndef ASSERT 1913 } 1914 1915 void ClassVerifier::class_format_error(const char* msg, ...) { 1916 stringStream ss; 1917 _exception_type = vmSymbols::java_lang_ClassFormatError(); 1918 va_list va; 1919 va_start(va, msg); 1920 ss.vprint(msg, va); 1921 va_end(va); 1922 if (!_method.is_null()) { 1923 ss.print(" in method %s", _method->name_and_sig_as_C_string()); 1924 } 1925 _message = ss.as_string(); 1926 } 1927 1928 Klass* ClassVerifier::load_class(Symbol* name, TRAPS) { 1929 // Get current loader and protection domain first. 1930 oop loader = current_class()->class_loader(); 1931 oop protection_domain = current_class()->protection_domain(); 1932 1933 return SystemDictionary::resolve_or_fail( 1934 name, Handle(THREAD, loader), Handle(THREAD, protection_domain), 1935 true, THREAD); 1936 } 1937 1938 bool ClassVerifier::is_protected_access(instanceKlassHandle this_class, 1939 Klass* target_class, 1940 Symbol* field_name, 1941 Symbol* field_sig, 1942 bool is_method) { 1943 No_Safepoint_Verifier nosafepoint; 1944 1945 // If target class isn't a super class of this class, we don't worry about this case 1946 if (!this_class->is_subclass_of(target_class)) { 1947 return false; 1948 } 1949 // Check if the specified method or field is protected 1950 InstanceKlass* target_instance = InstanceKlass::cast(target_class); 1951 fieldDescriptor fd; 1952 if (is_method) { 1953 Method* m = target_instance->uncached_lookup_method(field_name, field_sig, Klass::normal); 1954 if (m != NULL && m->is_protected()) { 1955 if (!this_class->is_same_class_package(m->method_holder())) { 1956 return true; 1957 } 1958 } 1959 } else { 1960 Klass* member_klass = target_instance->find_field(field_name, field_sig, &fd); 1961 if (member_klass != NULL && fd.is_protected()) { 1962 if (!this_class->is_same_class_package(member_klass)) { 1963 return true; 1964 } 1965 } 1966 } 1967 return false; 1968 } 1969 1970 void ClassVerifier::verify_ldc( 1971 int opcode, u2 index, StackMapFrame* current_frame, 1972 constantPoolHandle cp, u2 bci, TRAPS) { 1973 verify_cp_index(bci, cp, index, CHECK_VERIFY(this)); 1974 constantTag tag = cp->tag_at(index); 1975 unsigned int types; 1976 if (opcode == Bytecodes::_ldc || opcode == Bytecodes::_ldc_w) { 1977 if (!tag.is_unresolved_klass()) { 1978 types = (1 << JVM_CONSTANT_Integer) | (1 << JVM_CONSTANT_Float) 1979 | (1 << JVM_CONSTANT_String) | (1 << JVM_CONSTANT_Class) 1980 | (1 << JVM_CONSTANT_MethodHandle) | (1 << JVM_CONSTANT_MethodType); 1981 // Note: The class file parser already verified the legality of 1982 // MethodHandle and MethodType constants. 1983 verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this)); 1984 } 1985 } else { 1986 assert(opcode == Bytecodes::_ldc2_w, "must be ldc2_w"); 1987 types = (1 << JVM_CONSTANT_Double) | (1 << JVM_CONSTANT_Long); 1988 verify_cp_type(bci, index, cp, types, CHECK_VERIFY(this)); 1989 } 1990 if (tag.is_string() && cp->is_pseudo_string_at(index)) { 1991 current_frame->push_stack(object_type(), CHECK_VERIFY(this)); 1992 } else if (tag.is_string()) { 1993 current_frame->push_stack( 1994 VerificationType::reference_type( 1995 vmSymbols::java_lang_String()), CHECK_VERIFY(this)); 1996 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 1997 current_frame->push_stack( 1998 VerificationType::reference_type( 1999 vmSymbols::java_lang_Class()), CHECK_VERIFY(this)); 2000 } else if (tag.is_int()) { 2001 current_frame->push_stack( 2002 VerificationType::integer_type(), CHECK_VERIFY(this)); 2003 } else if (tag.is_float()) { 2004 current_frame->push_stack( 2005 VerificationType::float_type(), CHECK_VERIFY(this)); 2006 } else if (tag.is_double()) { 2007 current_frame->push_stack_2( 2008 VerificationType::double_type(), 2009 VerificationType::double2_type(), CHECK_VERIFY(this)); 2010 } else if (tag.is_long()) { 2011 current_frame->push_stack_2( 2012 VerificationType::long_type(), 2013 VerificationType::long2_type(), CHECK_VERIFY(this)); 2014 } else if (tag.is_method_handle()) { 2015 current_frame->push_stack( 2016 VerificationType::reference_type( 2017 vmSymbols::java_lang_invoke_MethodHandle()), CHECK_VERIFY(this)); 2018 } else if (tag.is_method_type()) { 2019 current_frame->push_stack( 2020 VerificationType::reference_type( 2021 vmSymbols::java_lang_invoke_MethodType()), CHECK_VERIFY(this)); 2022 } else { 2023 /* Unreachable? verify_cp_type has already validated the cp type. */ 2024 verify_error( 2025 ErrorContext::bad_cp_index(bci, index), "Invalid index in ldc"); 2026 return; 2027 } 2028 } 2029 2030 void ClassVerifier::verify_switch( 2031 RawBytecodeStream* bcs, u4 code_length, char* code_data, 2032 StackMapFrame* current_frame, StackMapTable* stackmap_table, TRAPS) { 2033 int bci = bcs->bci(); 2034 address bcp = bcs->bcp(); 2035 address aligned_bcp = (address) round_to((intptr_t)(bcp + 1), jintSize); 2036 2037 if (_klass->major_version() < NONZERO_PADDING_BYTES_IN_SWITCH_MAJOR_VERSION) { 2038 // 4639449 & 4647081: padding bytes must be 0 2039 u2 padding_offset = 1; 2040 while ((bcp + padding_offset) < aligned_bcp) { 2041 if(*(bcp + padding_offset) != 0) { 2042 verify_error(ErrorContext::bad_code(bci), 2043 "Nonzero padding byte in lookupswitch or tableswitch"); 2044 return; 2045 } 2046 padding_offset++; 2047 } 2048 } 2049 2050 int default_offset = (int) Bytes::get_Java_u4(aligned_bcp); 2051 int keys, delta; 2052 current_frame->pop_stack( 2053 VerificationType::integer_type(), CHECK_VERIFY(this)); 2054 if (bcs->raw_code() == Bytecodes::_tableswitch) { 2055 jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize); 2056 jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize); 2057 if (low > high) { 2058 verify_error(ErrorContext::bad_code(bci), 2059 "low must be less than or equal to high in tableswitch"); 2060 return; 2061 } 2062 keys = high - low + 1; 2063 if (keys < 0) { 2064 verify_error(ErrorContext::bad_code(bci), "too many keys in tableswitch"); 2065 return; 2066 } 2067 delta = 1; 2068 } else { 2069 keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize); 2070 if (keys < 0) { 2071 verify_error(ErrorContext::bad_code(bci), 2072 "number of keys in lookupswitch less than 0"); 2073 return; 2074 } 2075 delta = 2; 2076 // Make sure that the lookupswitch items are sorted 2077 for (int i = 0; i < (keys - 1); i++) { 2078 jint this_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i)*jintSize); 2079 jint next_key = Bytes::get_Java_u4(aligned_bcp + (2+2*i+2)*jintSize); 2080 if (this_key >= next_key) { 2081 verify_error(ErrorContext::bad_code(bci), 2082 "Bad lookupswitch instruction"); 2083 return; 2084 } 2085 } 2086 } 2087 int target = bci + default_offset; 2088 stackmap_table->check_jump_target(current_frame, target, CHECK_VERIFY(this)); 2089 for (int i = 0; i < keys; i++) { 2090 // Because check_jump_target() may safepoint, the bytecode could have 2091 // moved, which means 'aligned_bcp' is no good and needs to be recalculated. 2092 aligned_bcp = (address)round_to((intptr_t)(bcs->bcp() + 1), jintSize); 2093 target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize); 2094 stackmap_table->check_jump_target( 2095 current_frame, target, CHECK_VERIFY(this)); 2096 } 2097 NOT_PRODUCT(aligned_bcp = NULL); // no longer valid at this point 2098 } 2099 2100 bool ClassVerifier::name_in_supers( 2101 Symbol* ref_name, instanceKlassHandle current) { 2102 Klass* super = current->super(); 2103 while (super != NULL) { 2104 if (super->name() == ref_name) { 2105 return true; 2106 } 2107 super = super->super(); 2108 } 2109 return false; 2110 } 2111 2112 void ClassVerifier::verify_field_instructions(RawBytecodeStream* bcs, 2113 StackMapFrame* current_frame, 2114 constantPoolHandle cp, 2115 bool allow_arrays, 2116 TRAPS) { 2117 u2 index = bcs->get_index_u2(); 2118 verify_cp_type(bcs->bci(), index, cp, 2119 1 << JVM_CONSTANT_Fieldref, CHECK_VERIFY(this)); 2120 2121 // Get field name and signature 2122 Symbol* field_name = cp->name_ref_at(index); 2123 Symbol* field_sig = cp->signature_ref_at(index); 2124 2125 if (!SignatureVerifier::is_valid_type_signature(field_sig)) { 2126 class_format_error( 2127 "Invalid signature for field in class %s referenced " 2128 "from constant pool index %d", _klass->external_name(), index); 2129 return; 2130 } 2131 2132 // Get referenced class type 2133 VerificationType ref_class_type = cp_ref_index_to_type( 2134 index, cp, CHECK_VERIFY(this)); 2135 if (!ref_class_type.is_object() && 2136 (!allow_arrays || !ref_class_type.is_array())) { 2137 verify_error(ErrorContext::bad_type(bcs->bci(), 2138 TypeOrigin::cp(index, ref_class_type)), 2139 "Expecting reference to class in class %s at constant pool index %d", 2140 _klass->external_name(), index); 2141 return; 2142 } 2143 VerificationType target_class_type = ref_class_type; 2144 2145 assert(sizeof(VerificationType) == sizeof(uintptr_t), 2146 "buffer type must match VerificationType size"); 2147 uintptr_t field_type_buffer[2]; 2148 VerificationType* field_type = (VerificationType*)field_type_buffer; 2149 // If we make a VerificationType[2] array directly, the compiler calls 2150 // to the c-runtime library to do the allocation instead of just 2151 // stack allocating it. Plus it would run constructors. This shows up 2152 // in performance profiles. 2153 2154 SignatureStream sig_stream(field_sig, false); 2155 VerificationType stack_object_type; 2156 int n = change_sig_to_verificationType( 2157 &sig_stream, field_type, CHECK_VERIFY(this)); 2158 u2 bci = bcs->bci(); 2159 bool is_assignable; 2160 switch (bcs->raw_code()) { 2161 case Bytecodes::_getstatic: { 2162 for (int i = 0; i < n; i++) { 2163 current_frame->push_stack(field_type[i], CHECK_VERIFY(this)); 2164 } 2165 break; 2166 } 2167 case Bytecodes::_putstatic: { 2168 for (int i = n - 1; i >= 0; i--) { 2169 current_frame->pop_stack(field_type[i], CHECK_VERIFY(this)); 2170 } 2171 break; 2172 } 2173 case Bytecodes::_getfield: { 2174 stack_object_type = current_frame->pop_stack( 2175 target_class_type, CHECK_VERIFY(this)); 2176 for (int i = 0; i < n; i++) { 2177 current_frame->push_stack(field_type[i], CHECK_VERIFY(this)); 2178 } 2179 goto check_protected; 2180 } 2181 case Bytecodes::_putfield: { 2182 for (int i = n - 1; i >= 0; i--) { 2183 current_frame->pop_stack(field_type[i], CHECK_VERIFY(this)); 2184 } 2185 stack_object_type = current_frame->pop_stack(CHECK_VERIFY(this)); 2186 2187 // The JVMS 2nd edition allows field initialization before the superclass 2188 // initializer, if the field is defined within the current class. 2189 fieldDescriptor fd; 2190 if (stack_object_type == VerificationType::uninitialized_this_type() && 2191 target_class_type.equals(current_type()) && 2192 _klass->find_local_field(field_name, field_sig, &fd)) { 2193 stack_object_type = current_type(); 2194 } 2195 is_assignable = target_class_type.is_assignable_from( 2196 stack_object_type, this, false, CHECK_VERIFY(this)); 2197 if (!is_assignable) { 2198 verify_error(ErrorContext::bad_type(bci, 2199 current_frame->stack_top_ctx(), 2200 TypeOrigin::cp(index, target_class_type)), 2201 "Bad type on operand stack in putfield"); 2202 return; 2203 } 2204 } 2205 check_protected: { 2206 if (_this_type == stack_object_type) 2207 break; // stack_object_type must be assignable to _current_class_type 2208 Symbol* ref_class_name = 2209 cp->klass_name_at(cp->klass_ref_index_at(index)); 2210 if (!name_in_supers(ref_class_name, current_class())) 2211 // stack_object_type must be assignable to _current_class_type since: 2212 // 1. stack_object_type must be assignable to ref_class. 2213 // 2. ref_class must be _current_class or a subclass of it. It can't 2214 // be a superclass of it. See revised JVMS 5.4.4. 2215 break; 2216 2217 Klass* ref_class_oop = load_class(ref_class_name, CHECK); 2218 if (is_protected_access(current_class(), ref_class_oop, field_name, 2219 field_sig, false)) { 2220 // It's protected access, check if stack object is assignable to 2221 // current class. 2222 is_assignable = current_type().is_assignable_from( 2223 stack_object_type, this, true, CHECK_VERIFY(this)); 2224 if (!is_assignable) { 2225 verify_error(ErrorContext::bad_type(bci, 2226 current_frame->stack_top_ctx(), 2227 TypeOrigin::implicit(current_type())), 2228 "Bad access to protected data in getfield"); 2229 return; 2230 } 2231 } 2232 break; 2233 } 2234 default: ShouldNotReachHere(); 2235 } 2236 } 2237 2238 // Look at the method's handlers. If the bci is in the handler's try block 2239 // then check if the handler_pc is already on the stack. If not, push it. 2240 void ClassVerifier::push_handlers(ExceptionTable* exhandlers, 2241 GrowableArray<u4>* handler_stack, 2242 u4 bci) { 2243 int exlength = exhandlers->length(); 2244 for(int x = 0; x < exlength; x++) { 2245 if (bci >= exhandlers->start_pc(x) && bci < exhandlers->end_pc(x)) { 2246 handler_stack->append_if_missing(exhandlers->handler_pc(x)); 2247 } 2248 } 2249 } 2250 2251 // Return TRUE if all code paths starting with start_bc_offset end in 2252 // bytecode athrow or loop. 2253 bool ClassVerifier::ends_in_athrow(u4 start_bc_offset) { 2254 ResourceMark rm; 2255 // Create bytecode stream. 2256 RawBytecodeStream bcs(method()); 2257 u4 code_length = method()->code_size(); 2258 bcs.set_start(start_bc_offset); 2259 u4 target; 2260 // Create stack for storing bytecode start offsets for if* and *switch. 2261 GrowableArray<u4>* bci_stack = new GrowableArray<u4>(30); 2262 // Create stack for handlers for try blocks containing this handler. 2263 GrowableArray<u4>* handler_stack = new GrowableArray<u4>(30); 2264 // Create list of visited branch opcodes (goto* and if*). 2265 GrowableArray<u4>* visited_branches = new GrowableArray<u4>(30); 2266 ExceptionTable exhandlers(_method()); 2267 2268 while (true) { 2269 if (bcs.is_last_bytecode()) { 2270 // if no more starting offsets to parse or if at the end of the 2271 // method then return false. 2272 if ((bci_stack->is_empty()) || ((u4)bcs.end_bci() == code_length)) 2273 return false; 2274 // Pop a bytecode starting offset and scan from there. 2275 bcs.set_start(bci_stack->pop()); 2276 } 2277 Bytecodes::Code opcode = bcs.raw_next(); 2278 u4 bci = bcs.bci(); 2279 2280 // If the bytecode is in a TRY block, push its handlers so they 2281 // will get parsed. 2282 push_handlers(&exhandlers, handler_stack, bci); 2283 2284 switch (opcode) { 2285 case Bytecodes::_if_icmpeq: 2286 case Bytecodes::_if_icmpne: 2287 case Bytecodes::_if_icmplt: 2288 case Bytecodes::_if_icmpge: 2289 case Bytecodes::_if_icmpgt: 2290 case Bytecodes::_if_icmple: 2291 case Bytecodes::_ifeq: 2292 case Bytecodes::_ifne: 2293 case Bytecodes::_iflt: 2294 case Bytecodes::_ifge: 2295 case Bytecodes::_ifgt: 2296 case Bytecodes::_ifle: 2297 case Bytecodes::_if_acmpeq: 2298 case Bytecodes::_if_acmpne: 2299 case Bytecodes::_ifnull: 2300 case Bytecodes::_ifnonnull: 2301 target = bcs.dest(); 2302 if (visited_branches->contains(bci)) { 2303 if (bci_stack->is_empty()) return true; 2304 // Pop a bytecode starting offset and scan from there. 2305 bcs.set_start(bci_stack->pop()); 2306 } else { 2307 if (target > bci) { // forward branch 2308 if (target >= code_length) return false; 2309 // Push the branch target onto the stack. 2310 bci_stack->push(target); 2311 // then, scan bytecodes starting with next. 2312 bcs.set_start(bcs.next_bci()); 2313 } else { // backward branch 2314 // Push bytecode offset following backward branch onto the stack. 2315 bci_stack->push(bcs.next_bci()); 2316 // Check bytecodes starting with branch target. 2317 bcs.set_start(target); 2318 } 2319 // Record target so we don't branch here again. 2320 visited_branches->append(bci); 2321 } 2322 break; 2323 2324 case Bytecodes::_goto: 2325 case Bytecodes::_goto_w: 2326 target = (opcode == Bytecodes::_goto ? bcs.dest() : bcs.dest_w()); 2327 if (visited_branches->contains(bci)) { 2328 if (bci_stack->is_empty()) return true; 2329 // Been here before, pop new starting offset from stack. 2330 bcs.set_start(bci_stack->pop()); 2331 } else { 2332 if (target >= code_length) return false; 2333 // Continue scanning from the target onward. 2334 bcs.set_start(target); 2335 // Record target so we don't branch here again. 2336 visited_branches->append(bci); 2337 } 2338 break; 2339 2340 // Check that all switch alternatives end in 'athrow' bytecodes. Since it 2341 // is difficult to determine where each switch alternative ends, parse 2342 // each switch alternative until either hit a 'return', 'athrow', or reach 2343 // the end of the method's bytecodes. This is gross but should be okay 2344 // because: 2345 // 1. tableswitch and lookupswitch byte codes in handlers for ctor explicit 2346 // constructor invocations should be rare. 2347 // 2. if each switch alternative ends in an athrow then the parsing should be 2348 // short. If there is no athrow then it is bogus code, anyway. 2349 case Bytecodes::_lookupswitch: 2350 case Bytecodes::_tableswitch: 2351 { 2352 address aligned_bcp = (address) round_to((intptr_t)(bcs.bcp() + 1), jintSize); 2353 u4 default_offset = Bytes::get_Java_u4(aligned_bcp) + bci; 2354 int keys, delta; 2355 if (opcode == Bytecodes::_tableswitch) { 2356 jint low = (jint)Bytes::get_Java_u4(aligned_bcp + jintSize); 2357 jint high = (jint)Bytes::get_Java_u4(aligned_bcp + 2*jintSize); 2358 // This is invalid, but let the regular bytecode verifier 2359 // report this because the user will get a better error message. 2360 if (low > high) return true; 2361 keys = high - low + 1; 2362 delta = 1; 2363 } else { 2364 keys = (int)Bytes::get_Java_u4(aligned_bcp + jintSize); 2365 delta = 2; 2366 } 2367 // Invalid, let the regular bytecode verifier deal with it. 2368 if (keys < 0) return true; 2369 2370 // Push the offset of the next bytecode onto the stack. 2371 bci_stack->push(bcs.next_bci()); 2372 2373 // Push the switch alternatives onto the stack. 2374 for (int i = 0; i < keys; i++) { 2375 u4 target = bci + (jint)Bytes::get_Java_u4(aligned_bcp+(3+i*delta)*jintSize); 2376 if (target > code_length) return false; 2377 bci_stack->push(target); 2378 } 2379 2380 // Start bytecode parsing for the switch at the default alternative. 2381 if (default_offset > code_length) return false; 2382 bcs.set_start(default_offset); 2383 break; 2384 } 2385 2386 case Bytecodes::_return: 2387 return false; 2388 2389 case Bytecodes::_athrow: 2390 { 2391 if (bci_stack->is_empty()) { 2392 if (handler_stack->is_empty()) { 2393 return true; 2394 } else { 2395 // Parse the catch handlers for try blocks containing athrow. 2396 bcs.set_start(handler_stack->pop()); 2397 } 2398 } else { 2399 // Pop a bytecode offset and starting scanning from there. 2400 bcs.set_start(bci_stack->pop()); 2401 } 2402 } 2403 break; 2404 2405 default: 2406 ; 2407 } // end switch 2408 } // end while loop 2409 2410 return false; 2411 } 2412 2413 void ClassVerifier::verify_invoke_init( 2414 RawBytecodeStream* bcs, u2 ref_class_index, VerificationType ref_class_type, 2415 StackMapFrame* current_frame, u4 code_length, bool *this_uninit, 2416 constantPoolHandle cp, TRAPS) { 2417 u2 bci = bcs->bci(); 2418 VerificationType type = current_frame->pop_stack( 2419 VerificationType::reference_check(), CHECK_VERIFY(this)); 2420 if (type == VerificationType::uninitialized_this_type()) { 2421 // The method must be an <init> method of this class or its superclass 2422 Klass* superk = current_class()->super(); 2423 if (ref_class_type.name() != current_class()->name() && 2424 ref_class_type.name() != superk->name()) { 2425 verify_error(ErrorContext::bad_type(bci, 2426 TypeOrigin::implicit(ref_class_type), 2427 TypeOrigin::implicit(current_type())), 2428 "Bad <init> method call"); 2429 return; 2430 } 2431 2432 // Check if this call is done from inside of a TRY block. If so, make 2433 // sure that all catch clause paths end in a throw. Otherwise, this 2434 // can result in returning an incomplete object. 2435 ExceptionTable exhandlers(_method()); 2436 int exlength = exhandlers.length(); 2437 for(int i = 0; i < exlength; i++) { 2438 u2 start_pc = exhandlers.start_pc(i); 2439 u2 end_pc = exhandlers.end_pc(i); 2440 2441 if (bci >= start_pc && bci < end_pc) { 2442 if (!ends_in_athrow(exhandlers.handler_pc(i))) { 2443 verify_error(ErrorContext::bad_code(bci), 2444 "Bad <init> method call from after the start of a try block"); 2445 return; 2446 } else if (VerboseVerification) { 2447 ResourceMark rm; 2448 tty->print_cr( 2449 "Survived call to ends_in_athrow(): %s", 2450 current_class()->name()->as_C_string()); 2451 } 2452 } 2453 } 2454 2455 current_frame->initialize_object(type, current_type()); 2456 *this_uninit = true; 2457 } else if (type.is_uninitialized()) { 2458 u2 new_offset = type.bci(); 2459 address new_bcp = bcs->bcp() - bci + new_offset; 2460 if (new_offset > (code_length - 3) || (*new_bcp) != Bytecodes::_new) { 2461 /* Unreachable? Stack map parsing ensures valid type and new 2462 * instructions have a valid BCI. */ 2463 verify_error(ErrorContext::bad_code(new_offset), 2464 "Expecting new instruction"); 2465 return; 2466 } 2467 u2 new_class_index = Bytes::get_Java_u2(new_bcp + 1); 2468 verify_cp_class_type(bci, new_class_index, cp, CHECK_VERIFY(this)); 2469 2470 // The method must be an <init> method of the indicated class 2471 VerificationType new_class_type = cp_index_to_type( 2472 new_class_index, cp, CHECK_VERIFY(this)); 2473 if (!new_class_type.equals(ref_class_type)) { 2474 verify_error(ErrorContext::bad_type(bci, 2475 TypeOrigin::cp(new_class_index, new_class_type), 2476 TypeOrigin::cp(ref_class_index, ref_class_type)), 2477 "Call to wrong <init> method"); 2478 return; 2479 } 2480 // According to the VM spec, if the referent class is a superclass of the 2481 // current class, and is in a different runtime package, and the method is 2482 // protected, then the objectref must be the current class or a subclass 2483 // of the current class. 2484 VerificationType objectref_type = new_class_type; 2485 if (name_in_supers(ref_class_type.name(), current_class())) { 2486 Klass* ref_klass = load_class(ref_class_type.name(), CHECK); 2487 Method* m = InstanceKlass::cast(ref_klass)->uncached_lookup_method( 2488 vmSymbols::object_initializer_name(), 2489 cp->signature_ref_at(bcs->get_index_u2()), 2490 Klass::normal); 2491 // Do nothing if method is not found. Let resolution detect the error. 2492 if (m != NULL) { 2493 instanceKlassHandle mh(THREAD, m->method_holder()); 2494 if (m->is_protected() && !mh->is_same_class_package(_klass())) { 2495 bool assignable = current_type().is_assignable_from( 2496 objectref_type, this, true, CHECK_VERIFY(this)); 2497 if (!assignable) { 2498 verify_error(ErrorContext::bad_type(bci, 2499 TypeOrigin::cp(new_class_index, objectref_type), 2500 TypeOrigin::implicit(current_type())), 2501 "Bad access to protected <init> method"); 2502 return; 2503 } 2504 } 2505 } 2506 } 2507 current_frame->initialize_object(type, new_class_type); 2508 } else { 2509 verify_error(ErrorContext::bad_type(bci, current_frame->stack_top_ctx()), 2510 "Bad operand type when invoking <init>"); 2511 return; 2512 } 2513 } 2514 2515 bool ClassVerifier::is_same_or_direct_interface( 2516 instanceKlassHandle klass, 2517 VerificationType klass_type, 2518 VerificationType ref_class_type) { 2519 if (ref_class_type.equals(klass_type)) return true; 2520 Array<Klass*>* local_interfaces = klass->local_interfaces(); 2521 if (local_interfaces != NULL) { 2522 for (int x = 0; x < local_interfaces->length(); x++) { 2523 Klass* k = local_interfaces->at(x); 2524 assert (k != NULL && k->is_interface(), "invalid interface"); 2525 if (ref_class_type.equals(VerificationType::reference_type(k->name()))) { 2526 return true; 2527 } 2528 } 2529 } 2530 return false; 2531 } 2532 2533 void ClassVerifier::verify_invoke_instructions( 2534 RawBytecodeStream* bcs, u4 code_length, StackMapFrame* current_frame, 2535 bool *this_uninit, VerificationType return_type, 2536 constantPoolHandle cp, TRAPS) { 2537 // Make sure the constant pool item is the right type 2538 u2 index = bcs->get_index_u2(); 2539 Bytecodes::Code opcode = bcs->raw_code(); 2540 unsigned int types; 2541 switch (opcode) { 2542 case Bytecodes::_invokeinterface: 2543 types = 1 << JVM_CONSTANT_InterfaceMethodref; 2544 break; 2545 case Bytecodes::_invokedynamic: 2546 types = 1 << JVM_CONSTANT_InvokeDynamic; 2547 break; 2548 case Bytecodes::_invokespecial: 2549 case Bytecodes::_invokestatic: 2550 types = (_klass->major_version() < STATIC_METHOD_IN_INTERFACE_MAJOR_VERSION) ? 2551 (1 << JVM_CONSTANT_Methodref) : 2552 ((1 << JVM_CONSTANT_InterfaceMethodref) | (1 << JVM_CONSTANT_Methodref)); 2553 break; 2554 default: 2555 types = 1 << JVM_CONSTANT_Methodref; 2556 } 2557 verify_cp_type(bcs->bci(), index, cp, types, CHECK_VERIFY(this)); 2558 2559 // Get method name and signature 2560 Symbol* method_name = cp->name_ref_at(index); 2561 Symbol* method_sig = cp->signature_ref_at(index); 2562 2563 if (!SignatureVerifier::is_valid_method_signature(method_sig)) { 2564 class_format_error( 2565 "Invalid method signature in class %s referenced " 2566 "from constant pool index %d", _klass->external_name(), index); 2567 return; 2568 } 2569 2570 // Get referenced class type 2571 VerificationType ref_class_type; 2572 if (opcode == Bytecodes::_invokedynamic) { 2573 if (_klass->major_version() < Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 2574 class_format_error( 2575 "invokedynamic instructions not supported by this class file version (%d), class %s", 2576 _klass->major_version(), _klass->external_name()); 2577 return; 2578 } 2579 } else { 2580 ref_class_type = cp_ref_index_to_type(index, cp, CHECK_VERIFY(this)); 2581 } 2582 2583 // For a small signature length, we just allocate 128 bytes instead 2584 // of parsing the signature once to find its size. 2585 // -3 is for '(', ')' and return descriptor; multiply by 2 is for 2586 // longs/doubles to be consertive. 2587 assert(sizeof(VerificationType) == sizeof(uintptr_t), 2588 "buffer type must match VerificationType size"); 2589 uintptr_t on_stack_sig_types_buffer[128]; 2590 // If we make a VerificationType[128] array directly, the compiler calls 2591 // to the c-runtime library to do the allocation instead of just 2592 // stack allocating it. Plus it would run constructors. This shows up 2593 // in performance profiles. 2594 2595 VerificationType* sig_types; 2596 int size = (method_sig->utf8_length() - 3) * 2; 2597 if (size > 128) { 2598 // Long and double occupies two slots here. 2599 ArgumentSizeComputer size_it(method_sig); 2600 size = size_it.size(); 2601 sig_types = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, VerificationType, size); 2602 } else{ 2603 sig_types = (VerificationType*)on_stack_sig_types_buffer; 2604 } 2605 SignatureStream sig_stream(method_sig); 2606 int sig_i = 0; 2607 while (!sig_stream.at_return_type()) { 2608 sig_i += change_sig_to_verificationType( 2609 &sig_stream, &sig_types[sig_i], CHECK_VERIFY(this)); 2610 sig_stream.next(); 2611 } 2612 int nargs = sig_i; 2613 2614 #ifdef ASSERT 2615 { 2616 ArgumentSizeComputer size_it(method_sig); 2617 assert(nargs == size_it.size(), "Argument sizes do not match"); 2618 assert(nargs <= (method_sig->utf8_length() - 3) * 2, "estimate of max size isn't conservative enough"); 2619 } 2620 #endif 2621 2622 // Check instruction operands 2623 u2 bci = bcs->bci(); 2624 if (opcode == Bytecodes::_invokeinterface) { 2625 address bcp = bcs->bcp(); 2626 // 4905268: count operand in invokeinterface should be nargs+1, not nargs. 2627 // JSR202 spec: The count operand of an invokeinterface instruction is valid if it is 2628 // the difference between the size of the operand stack before and after the instruction 2629 // executes. 2630 if (*(bcp+3) != (nargs+1)) { 2631 verify_error(ErrorContext::bad_code(bci), 2632 "Inconsistent args count operand in invokeinterface"); 2633 return; 2634 } 2635 if (*(bcp+4) != 0) { 2636 verify_error(ErrorContext::bad_code(bci), 2637 "Fourth operand byte of invokeinterface must be zero"); 2638 return; 2639 } 2640 } 2641 2642 if (opcode == Bytecodes::_invokedynamic) { 2643 address bcp = bcs->bcp(); 2644 if (*(bcp+3) != 0 || *(bcp+4) != 0) { 2645 verify_error(ErrorContext::bad_code(bci), 2646 "Third and fourth operand bytes of invokedynamic must be zero"); 2647 return; 2648 } 2649 } 2650 2651 if (method_name->byte_at(0) == '<') { 2652 // Make sure <init> can only be invoked by invokespecial 2653 if (opcode != Bytecodes::_invokespecial || 2654 method_name != vmSymbols::object_initializer_name()) { 2655 verify_error(ErrorContext::bad_code(bci), 2656 "Illegal call to internal method"); 2657 return; 2658 } 2659 } else if (opcode == Bytecodes::_invokespecial 2660 && !is_same_or_direct_interface(current_class(), current_type(), ref_class_type) 2661 && !ref_class_type.equals(VerificationType::reference_type( 2662 current_class()->super()->name()))) { 2663 bool subtype = false; 2664 bool have_imr_indirect = cp->tag_at(index).value() == JVM_CONSTANT_InterfaceMethodref; 2665 if (!current_class()->is_anonymous()) { 2666 subtype = ref_class_type.is_assignable_from( 2667 current_type(), this, false, CHECK_VERIFY(this)); 2668 } else { 2669 VerificationType host_klass_type = 2670 VerificationType::reference_type(current_class()->host_klass()->name()); 2671 subtype = ref_class_type.is_assignable_from(host_klass_type, this, false, CHECK_VERIFY(this)); 2672 2673 // If invokespecial of IMR, need to recheck for same or 2674 // direct interface relative to the host class 2675 have_imr_indirect = (have_imr_indirect && 2676 !is_same_or_direct_interface( 2677 InstanceKlass::cast(current_class()->host_klass()), 2678 host_klass_type, ref_class_type)); 2679 } 2680 if (!subtype) { 2681 verify_error(ErrorContext::bad_code(bci), 2682 "Bad invokespecial instruction: " 2683 "current class isn't assignable to reference class."); 2684 return; 2685 } else if (have_imr_indirect) { 2686 verify_error(ErrorContext::bad_code(bci), 2687 "Bad invokespecial instruction: " 2688 "interface method reference is in an indirect superinterface."); 2689 return; 2690 } 2691 2692 } 2693 // Match method descriptor with operand stack 2694 for (int i = nargs - 1; i >= 0; i--) { // Run backwards 2695 current_frame->pop_stack(sig_types[i], CHECK_VERIFY(this)); 2696 } 2697 // Check objectref on operand stack 2698 if (opcode != Bytecodes::_invokestatic && 2699 opcode != Bytecodes::_invokedynamic) { 2700 if (method_name == vmSymbols::object_initializer_name()) { // <init> method 2701 verify_invoke_init(bcs, index, ref_class_type, current_frame, 2702 code_length, this_uninit, cp, CHECK_VERIFY(this)); 2703 } else { // other methods 2704 // Ensures that target class is assignable to method class. 2705 if (opcode == Bytecodes::_invokespecial) { 2706 if (!current_class()->is_anonymous()) { 2707 current_frame->pop_stack(current_type(), CHECK_VERIFY(this)); 2708 } else { 2709 // anonymous class invokespecial calls: check if the 2710 // objectref is a subtype of the host_klass of the current class 2711 // to allow an anonymous class to reference methods in the host_klass 2712 VerificationType top = current_frame->pop_stack(CHECK_VERIFY(this)); 2713 VerificationType hosttype = 2714 VerificationType::reference_type(current_class()->host_klass()->name()); 2715 bool subtype = hosttype.is_assignable_from(top, this, false, CHECK_VERIFY(this)); 2716 if (!subtype) { 2717 verify_error( ErrorContext::bad_type(current_frame->offset(), 2718 current_frame->stack_top_ctx(), 2719 TypeOrigin::implicit(top)), 2720 "Bad type on operand stack"); 2721 return; 2722 } 2723 } 2724 } else if (opcode == Bytecodes::_invokevirtual) { 2725 VerificationType stack_object_type = 2726 current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this)); 2727 if (current_type() != stack_object_type) { 2728 assert(cp->cache() == NULL, "not rewritten yet"); 2729 Symbol* ref_class_name = 2730 cp->klass_name_at(cp->klass_ref_index_at(index)); 2731 // See the comments in verify_field_instructions() for 2732 // the rationale behind this. 2733 if (name_in_supers(ref_class_name, current_class())) { 2734 Klass* ref_class = load_class(ref_class_name, CHECK); 2735 if (is_protected_access( 2736 _klass, ref_class, method_name, method_sig, true)) { 2737 // It's protected access, check if stack object is 2738 // assignable to current class. 2739 bool is_assignable = current_type().is_assignable_from( 2740 stack_object_type, this, true, CHECK_VERIFY(this)); 2741 if (!is_assignable) { 2742 if (ref_class_type.name() == vmSymbols::java_lang_Object() 2743 && stack_object_type.is_array() 2744 && method_name == vmSymbols::clone_name()) { 2745 // Special case: arrays pretend to implement public Object 2746 // clone(). 2747 } else { 2748 verify_error(ErrorContext::bad_type(bci, 2749 current_frame->stack_top_ctx(), 2750 TypeOrigin::implicit(current_type())), 2751 "Bad access to protected data in invokevirtual"); 2752 return; 2753 } 2754 } 2755 } 2756 } 2757 } 2758 } else { 2759 assert(opcode == Bytecodes::_invokeinterface, "Unexpected opcode encountered"); 2760 current_frame->pop_stack(ref_class_type, CHECK_VERIFY(this)); 2761 } 2762 } 2763 } 2764 // Push the result type. 2765 if (sig_stream.type() != T_VOID) { 2766 if (method_name == vmSymbols::object_initializer_name()) { 2767 // <init> method must have a void return type 2768 /* Unreachable? Class file parser verifies that methods with '<' have 2769 * void return */ 2770 verify_error(ErrorContext::bad_code(bci), 2771 "Return type must be void in <init> method"); 2772 return; 2773 } 2774 VerificationType return_type[2]; 2775 int n = change_sig_to_verificationType( 2776 &sig_stream, return_type, CHECK_VERIFY(this)); 2777 for (int i = 0; i < n; i++) { 2778 current_frame->push_stack(return_type[i], CHECK_VERIFY(this)); // push types backwards 2779 } 2780 } 2781 } 2782 2783 VerificationType ClassVerifier::get_newarray_type( 2784 u2 index, u2 bci, TRAPS) { 2785 const char* from_bt[] = { 2786 NULL, NULL, NULL, NULL, "[Z", "[C", "[F", "[D", "[B", "[S", "[I", "[J", 2787 }; 2788 if (index < T_BOOLEAN || index > T_LONG) { 2789 verify_error(ErrorContext::bad_code(bci), "Illegal newarray instruction"); 2790 return VerificationType::bogus_type(); 2791 } 2792 2793 // from_bt[index] contains the array signature which has a length of 2 2794 Symbol* sig = create_temporary_symbol( 2795 from_bt[index], 2, CHECK_(VerificationType::bogus_type())); 2796 return VerificationType::reference_type(sig); 2797 } 2798 2799 void ClassVerifier::verify_anewarray( 2800 u2 bci, u2 index, constantPoolHandle cp, 2801 StackMapFrame* current_frame, TRAPS) { 2802 verify_cp_class_type(bci, index, cp, CHECK_VERIFY(this)); 2803 current_frame->pop_stack( 2804 VerificationType::integer_type(), CHECK_VERIFY(this)); 2805 2806 VerificationType component_type = 2807 cp_index_to_type(index, cp, CHECK_VERIFY(this)); 2808 int length; 2809 char* arr_sig_str; 2810 if (component_type.is_array()) { // it's an array 2811 const char* component_name = component_type.name()->as_utf8(); 2812 // add one dimension to component 2813 length = (int)strlen(component_name) + 1; 2814 arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length); 2815 arr_sig_str[0] = '['; 2816 strncpy(&arr_sig_str[1], component_name, length - 1); 2817 } else { // it's an object or interface 2818 const char* component_name = component_type.name()->as_utf8(); 2819 // add one dimension to component with 'L' prepended and ';' postpended. 2820 length = (int)strlen(component_name) + 3; 2821 arr_sig_str = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, length); 2822 arr_sig_str[0] = '['; 2823 arr_sig_str[1] = 'L'; 2824 strncpy(&arr_sig_str[2], component_name, length - 2); 2825 arr_sig_str[length - 1] = ';'; 2826 } 2827 Symbol* arr_sig = create_temporary_symbol( 2828 arr_sig_str, length, CHECK_VERIFY(this)); 2829 VerificationType new_array_type = VerificationType::reference_type(arr_sig); 2830 current_frame->push_stack(new_array_type, CHECK_VERIFY(this)); 2831 } 2832 2833 void ClassVerifier::verify_iload(u2 index, StackMapFrame* current_frame, TRAPS) { 2834 current_frame->get_local( 2835 index, VerificationType::integer_type(), CHECK_VERIFY(this)); 2836 current_frame->push_stack( 2837 VerificationType::integer_type(), CHECK_VERIFY(this)); 2838 } 2839 2840 void ClassVerifier::verify_lload(u2 index, StackMapFrame* current_frame, TRAPS) { 2841 current_frame->get_local_2( 2842 index, VerificationType::long_type(), 2843 VerificationType::long2_type(), CHECK_VERIFY(this)); 2844 current_frame->push_stack_2( 2845 VerificationType::long_type(), 2846 VerificationType::long2_type(), CHECK_VERIFY(this)); 2847 } 2848 2849 void ClassVerifier::verify_fload(u2 index, StackMapFrame* current_frame, TRAPS) { 2850 current_frame->get_local( 2851 index, VerificationType::float_type(), CHECK_VERIFY(this)); 2852 current_frame->push_stack( 2853 VerificationType::float_type(), CHECK_VERIFY(this)); 2854 } 2855 2856 void ClassVerifier::verify_dload(u2 index, StackMapFrame* current_frame, TRAPS) { 2857 current_frame->get_local_2( 2858 index, VerificationType::double_type(), 2859 VerificationType::double2_type(), CHECK_VERIFY(this)); 2860 current_frame->push_stack_2( 2861 VerificationType::double_type(), 2862 VerificationType::double2_type(), CHECK_VERIFY(this)); 2863 } 2864 2865 void ClassVerifier::verify_aload(u2 index, StackMapFrame* current_frame, TRAPS) { 2866 VerificationType type = current_frame->get_local( 2867 index, VerificationType::reference_check(), CHECK_VERIFY(this)); 2868 current_frame->push_stack(type, CHECK_VERIFY(this)); 2869 } 2870 2871 void ClassVerifier::verify_istore(u2 index, StackMapFrame* current_frame, TRAPS) { 2872 current_frame->pop_stack( 2873 VerificationType::integer_type(), CHECK_VERIFY(this)); 2874 current_frame->set_local( 2875 index, VerificationType::integer_type(), CHECK_VERIFY(this)); 2876 } 2877 2878 void ClassVerifier::verify_lstore(u2 index, StackMapFrame* current_frame, TRAPS) { 2879 current_frame->pop_stack_2( 2880 VerificationType::long2_type(), 2881 VerificationType::long_type(), CHECK_VERIFY(this)); 2882 current_frame->set_local_2( 2883 index, VerificationType::long_type(), 2884 VerificationType::long2_type(), CHECK_VERIFY(this)); 2885 } 2886 2887 void ClassVerifier::verify_fstore(u2 index, StackMapFrame* current_frame, TRAPS) { 2888 current_frame->pop_stack(VerificationType::float_type(), CHECK_VERIFY(this)); 2889 current_frame->set_local( 2890 index, VerificationType::float_type(), CHECK_VERIFY(this)); 2891 } 2892 2893 void ClassVerifier::verify_dstore(u2 index, StackMapFrame* current_frame, TRAPS) { 2894 current_frame->pop_stack_2( 2895 VerificationType::double2_type(), 2896 VerificationType::double_type(), CHECK_VERIFY(this)); 2897 current_frame->set_local_2( 2898 index, VerificationType::double_type(), 2899 VerificationType::double2_type(), CHECK_VERIFY(this)); 2900 } 2901 2902 void ClassVerifier::verify_astore(u2 index, StackMapFrame* current_frame, TRAPS) { 2903 VerificationType type = current_frame->pop_stack( 2904 VerificationType::reference_check(), CHECK_VERIFY(this)); 2905 current_frame->set_local(index, type, CHECK_VERIFY(this)); 2906 } 2907 2908 void ClassVerifier::verify_iinc(u2 index, StackMapFrame* current_frame, TRAPS) { 2909 VerificationType type = current_frame->get_local( 2910 index, VerificationType::integer_type(), CHECK_VERIFY(this)); 2911 current_frame->set_local(index, type, CHECK_VERIFY(this)); 2912 } 2913 2914 void ClassVerifier::verify_return_value( 2915 VerificationType return_type, VerificationType type, u2 bci, 2916 StackMapFrame* current_frame, TRAPS) { 2917 if (return_type == VerificationType::bogus_type()) { 2918 verify_error(ErrorContext::bad_type(bci, 2919 current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)), 2920 "Method expects a return value"); 2921 return; 2922 } 2923 bool match = return_type.is_assignable_from(type, this, false, CHECK_VERIFY(this)); 2924 if (!match) { 2925 verify_error(ErrorContext::bad_type(bci, 2926 current_frame->stack_top_ctx(), TypeOrigin::signature(return_type)), 2927 "Bad return type"); 2928 return; 2929 } 2930 } 2931 2932 // The verifier creates symbols which are substrings of Symbols. 2933 // These are stored in the verifier until the end of verification so that 2934 // they can be reference counted. 2935 Symbol* ClassVerifier::create_temporary_symbol(const Symbol *s, int begin, 2936 int end, TRAPS) { 2937 Symbol* sym = SymbolTable::new_symbol(s, begin, end, CHECK_NULL); 2938 _symbols->push(sym); 2939 return sym; 2940 } 2941 2942 Symbol* ClassVerifier::create_temporary_symbol(const char *s, int length, TRAPS) { 2943 Symbol* sym = SymbolTable::new_symbol(s, length, CHECK_NULL); 2944 _symbols->push(sym); 2945 return sym; 2946 }