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