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