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