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