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