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