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