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