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