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 "classfile/javaClasses.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "classfile/vmClasses.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "compiler/compileBroker.hpp" 30 #include "logging/log.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "memory/universe.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/handles.inline.hpp" 36 #include "runtime/init.hpp" 37 #include "runtime/java.hpp" 38 #include "runtime/javaCalls.hpp" 39 #include "runtime/javaThread.hpp" 40 #include "runtime/os.hpp" 41 #include "runtime/threadCritical.hpp" 42 #include "runtime/atomic.hpp" 43 #include "utilities/events.hpp" 44 #include "utilities/exceptions.hpp" 45 #include "utilities/utf8.hpp" 46 47 // Limit exception message components to 64K (the same max as Symbols) 48 #define MAX_LEN 65535 49 50 // Implementation of ThreadShadow 51 void check_ThreadShadow() { 52 const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); 53 const ByteSize offset2 = Thread::pending_exception_offset(); 54 if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); 55 } 56 57 58 void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { 59 assert(exception != nullptr && oopDesc::is_oop(exception), "invalid exception oop"); 60 _pending_exception = exception; 61 _exception_file = file; 62 _exception_line = line; 63 } 64 65 void ThreadShadow::clear_pending_exception() { 66 LogTarget(Debug, exceptions) lt; 67 if (_pending_exception != nullptr && lt.is_enabled()) { 68 ResourceMark rm; 69 LogStream ls(lt); 70 ls.print("Thread::clear_pending_exception: cleared exception:"); 71 _pending_exception->print_on(&ls); 72 } 73 _pending_exception = nullptr; 74 _exception_file = nullptr; 75 _exception_line = 0; 76 } 77 78 void ThreadShadow::clear_pending_nonasync_exception() { 79 // Do not clear probable async exceptions. 80 if ((_pending_exception->klass() != vmClasses::InternalError_klass() || 81 java_lang_InternalError::during_unsafe_access(_pending_exception) != JNI_TRUE)) { 82 clear_pending_exception(); 83 } 84 } 85 86 void ThreadShadow::set_pending_preempted_exception() { 87 assert(!has_pending_exception(), ""); 88 // We always install the same dummy exception since we only want 89 // to use the TRAPS mechaninsm to bail out from all methods until 90 // reaching the one using the CHECK_AND_CLEAR_PREEMPTED macro. 91 set_pending_exception(Universe::preempted_exception_instance(), __FILE__, __LINE__); 92 } 93 94 void ThreadShadow::clear_pending_preempted_exception() { 95 if (_pending_exception->klass() == vmClasses::PreemptedException_klass()) { 96 clear_pending_exception(); 97 } 98 } 99 100 #ifdef ASSERT 101 void ThreadShadow::check_preempted_exception() { 102 assert(has_pending_exception(), ""); 103 assert(pending_exception()->is_a(vmClasses::PreemptedException_klass()), ""); 104 } 105 #endif 106 107 // Implementation of Exceptions 108 109 bool Exceptions::special_exception(JavaThread* thread, const char* file, int line, Handle h_exception, Symbol* h_name, const char* message) { 110 assert(h_exception.is_null() != (h_name == nullptr), "either exception (" PTR_FORMAT ") or " 111 "symbol (" PTR_FORMAT ") must be non-null but not both", p2i(h_exception()), p2i(h_name)); 112 113 // bootstrapping check 114 if (!Universe::is_fully_initialized()) { 115 if (h_exception.not_null()) { 116 vm_exit_during_initialization(h_exception); 117 } else if (h_name == nullptr) { 118 // at least an informative message. 119 vm_exit_during_initialization("Exception", message); 120 } else { 121 vm_exit_during_initialization(h_name, message); 122 } 123 ShouldNotReachHere(); 124 } 125 126 #ifdef ASSERT 127 // Check for trying to throw stack overflow before initialization is complete 128 // to prevent infinite recursion trying to initialize stack overflow without 129 // adequate stack space. 130 // This can happen with stress testing a large value of StackShadowPages 131 if (h_exception.not_null() && h_exception()->klass() == vmClasses::StackOverflowError_klass()) { 132 InstanceKlass* ik = InstanceKlass::cast(h_exception->klass()); 133 assert(ik->is_initialized(), 134 "need to increase java_thread_min_stack_allowed calculation"); 135 } 136 #endif // ASSERT 137 138 if (h_exception.is_null() && !thread->can_call_java()) { 139 ResourceMark rm(thread); 140 const char* exc_value = h_name != nullptr ? h_name->as_C_string() : "null"; 141 log_info(exceptions)("Thread cannot call Java so instead of throwing exception <%.*s%s%.*s> (" PTR_FORMAT ") \n" 142 "at [%s, line %d]\nfor thread " PTR_FORMAT ",\n" 143 "throwing pre-allocated exception: %s", 144 MAX_LEN, exc_value, message ? ": " : "", 145 MAX_LEN, message ? message : "", 146 p2i(h_exception()), file, line, p2i(thread), 147 Universe::vm_exception()->print_value_string()); 148 // We do not care what kind of exception we get for a thread which 149 // is compiling. We just install a dummy exception object 150 thread->set_pending_exception(Universe::vm_exception(), file, line); 151 return true; 152 } 153 154 return false; 155 } 156 157 // This method should only be called from generated code, 158 // therefore the exception oop should be in the oopmap. 159 void Exceptions::_throw_oop(JavaThread* thread, const char* file, int line, oop exception) { 160 assert(exception != nullptr, "exception should not be null"); 161 Handle h_exception(thread, exception); 162 _throw(thread, file, line, h_exception); 163 } 164 165 void Exceptions::_throw(JavaThread* thread, const char* file, int line, Handle h_exception, const char* message) { 166 ResourceMark rm(thread); 167 assert(h_exception() != nullptr, "exception should not be null"); 168 169 // tracing (do this up front - so it works during boot strapping) 170 // Note, the print_value_string() argument is not called unless logging is enabled! 171 log_info(exceptions)("Exception <%.*s%s%.*s> (" PTR_FORMAT ") \n" 172 "thrown [%s, line %d]\nfor thread " PTR_FORMAT, 173 MAX_LEN, h_exception->print_value_string(), 174 message ? ": " : "", 175 MAX_LEN, message ? message : "", 176 p2i(h_exception()), file, line, p2i(thread)); 177 178 // for AbortVMOnException flag 179 Exceptions::debug_check_abort(h_exception, message); 180 181 // Check for special boot-strapping/compiler-thread handling 182 if (special_exception(thread, file, line, h_exception)) { 183 return; 184 } 185 186 if (h_exception->is_a(vmClasses::VirtualMachineError_klass())) { 187 // Remove the ScopedValue bindings in case we got a virtual machine 188 // Error while we were trying to manipulate ScopedValue bindings. 189 thread->clear_scopedValueBindings(); 190 191 if (h_exception->is_a(vmClasses::OutOfMemoryError_klass())) { 192 count_out_of_memory_exceptions(h_exception); 193 } 194 } 195 196 if (h_exception->is_a(vmClasses::LinkageError_klass())) { 197 Atomic::inc(&_linkage_errors, memory_order_relaxed); 198 } 199 200 assert(h_exception->is_a(vmClasses::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); 201 202 // set the pending exception 203 thread->set_pending_exception(h_exception(), file, line); 204 205 // vm log 206 Events::log_exception(thread, h_exception, message, file, line, MAX_LEN); 207 } 208 209 210 void Exceptions::_throw_msg(JavaThread* thread, const char* file, int line, Symbol* name, const char* message, 211 Handle h_loader) { 212 // Check for special boot-strapping/compiler-thread handling 213 if (special_exception(thread, file, line, Handle(), name, message)) return; 214 // Create and throw exception 215 Handle h_cause(thread, nullptr); 216 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader); 217 _throw(thread, file, line, h_exception, message); 218 } 219 220 void Exceptions::_throw_msg_cause(JavaThread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause, 221 Handle h_loader) { 222 // Check for special boot-strapping/compiler-thread handling 223 if (special_exception(thread, file, line, Handle(), name, message)) return; 224 // Create and throw exception and init cause 225 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader); 226 _throw(thread, file, line, h_exception, message); 227 } 228 229 void Exceptions::_throw_cause(JavaThread* thread, const char* file, int line, Symbol* name, Handle h_cause, 230 Handle h_loader) { 231 // Check for special boot-strapping/compiler-thread handling 232 if (special_exception(thread, file, line, Handle(), name)) return; 233 // Create and throw exception 234 Handle h_exception = new_exception(thread, name, h_cause, h_loader); 235 _throw(thread, file, line, h_exception, nullptr); 236 } 237 238 void Exceptions::_throw_args(JavaThread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) { 239 // Check for special boot-strapping/compiler-thread handling 240 if (special_exception(thread, file, line, Handle(), name, nullptr)) return; 241 // Create and throw exception 242 Handle h_loader(thread, nullptr); 243 Handle h_prot(thread, nullptr); 244 Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot); 245 _throw(thread, file, line, exception); 246 } 247 248 249 // Methods for default parameters. 250 // NOTE: These must be here (and not in the header file) because of include circularities. 251 void Exceptions::_throw_msg_cause(JavaThread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) { 252 _throw_msg_cause(thread, file, line, name, message, h_cause, Handle()); 253 } 254 void Exceptions::_throw_msg(JavaThread* thread, const char* file, int line, Symbol* name, const char* message) { 255 _throw_msg(thread, file, line, name, message, Handle()); 256 } 257 void Exceptions::_throw_cause(JavaThread* thread, const char* file, int line, Symbol* name, Handle h_cause) { 258 _throw_cause(thread, file, line, name, h_cause, Handle()); 259 } 260 261 262 void Exceptions::throw_stack_overflow_exception(JavaThread* THREAD, const char* file, int line, const methodHandle& method) { 263 Handle exception; 264 if (!THREAD->has_pending_exception()) { 265 InstanceKlass* k = vmClasses::StackOverflowError_klass(); 266 oop e = k->allocate_instance(CHECK); 267 exception = Handle(THREAD, e); // fill_in_stack trace does gc 268 assert(k->is_initialized(), "need to increase java_thread_min_stack_allowed calculation"); 269 if (StackTraceInThrowable) { 270 java_lang_Throwable::fill_in_stack_trace(exception, method); 271 } 272 // Increment counter for hs_err file reporting 273 Atomic::inc(&Exceptions::_stack_overflow_errors, memory_order_relaxed); 274 } else { 275 // if prior exception, throw that one instead 276 exception = Handle(THREAD, THREAD->pending_exception()); 277 } 278 _throw(THREAD, file, line, exception); 279 } 280 281 // All callers are expected to have ensured that the incoming expanded format string 282 // will be within reasonable limits - specifically we will never hit the INT_MAX limit 283 // of os::vsnprintf when it tries to report how big a buffer is needed. Even so we 284 // further limit the formatted output to 1024 characters. 285 void Exceptions::fthrow(JavaThread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) { 286 const int max_msg_size = 1024; 287 va_list ap; 288 va_start(ap, format); 289 char msg[max_msg_size]; 290 int ret = os::vsnprintf(msg, max_msg_size, format, ap); 291 va_end(ap); 292 293 // If ret == -1 then either there was a format conversion error, or the required buffer size 294 // exceeds INT_MAX and so couldn't be returned (undocumented behaviour of vsnprintf). Depending 295 // on the platform the buffer may be filled to its capacity (Linux), filled to the conversion 296 // that encountered the overflow (macOS), or is empty (Windows), so it is possible we 297 // have a truncated UTF-8 sequence. Similarly, if the buffer was too small and ret >= max_msg_size 298 // we may also have a truncated UTF-8 sequence. In such cases we need to fix the buffer so the UTF-8 299 // sequence is valid. 300 assert(ret != -1, "Caller should have ensured the incoming format string is size limited!"); 301 if (ret == -1 || ret >= max_msg_size) { 302 int len = (int) strlen(msg); 303 if (len > 0) { 304 // Truncation will only happen if the buffer was filled by vsnprintf, 305 // otherwise vsnprintf already terminated filling it at a well-defined point. 306 // But as this is not a clearly specified area we will perform our own UTF8 307 // truncation anyway - though for those well-defined termination points it 308 // will be a no-op. 309 UTF8::truncate_to_legal_utf8((unsigned char*)msg, len + 1); 310 } 311 } 312 // UTF8::is_legal_utf8 should actually be called is_legal_utf8_class_name as the final 313 // parameter controls a check for a specific character appearing in the "name", which is only 314 // allowed for classfile versions <= 47. We pass `true` so that we allow such strings as this code 315 // know nothing about the actual string content. 316 assert(UTF8::is_legal_utf8((const unsigned char*)msg, strlen(msg), true), "must be"); 317 _throw_msg(thread, file, line, h_name, msg); 318 } 319 320 321 // Creates an exception oop, calls the <init> method with the given signature. 322 // and returns a Handle 323 Handle Exceptions::new_exception(JavaThread* thread, Symbol* name, 324 Symbol* signature, JavaCallArguments *args, 325 Handle h_loader) { 326 assert(Universe::is_fully_initialized(), 327 "cannot be called during initialization"); 328 assert(!thread->has_pending_exception(), "already has exception"); 329 330 Handle h_exception; 331 332 // Resolve exception klass, and check for pending exception below. 333 Klass* klass = SystemDictionary::resolve_or_fail(name, h_loader, true, thread); 334 335 if (!thread->has_pending_exception()) { 336 assert(klass != nullptr, "klass must exist"); 337 h_exception = JavaCalls::construct_new_instance(InstanceKlass::cast(klass), 338 signature, 339 args, 340 thread); 341 } 342 343 // Check if another exception was thrown in the process, if so rethrow that one 344 if (thread->has_pending_exception()) { 345 h_exception = Handle(thread, thread->pending_exception()); 346 thread->clear_pending_exception(); 347 } 348 return h_exception; 349 } 350 351 // Creates an exception oop, calls the <init> method with the given signature. 352 // and returns a Handle 353 // Initializes the cause if cause non-null 354 Handle Exceptions::new_exception(JavaThread* thread, Symbol* name, 355 Symbol* signature, JavaCallArguments *args, 356 Handle h_cause, 357 Handle h_loader) { 358 Handle h_exception = new_exception(thread, name, signature, args, h_loader); 359 360 // Future: object initializer should take a cause argument 361 if (h_cause.not_null()) { 362 assert(h_cause->is_a(vmClasses::Throwable_klass()), 363 "exception cause is not a subclass of java/lang/Throwable"); 364 JavaValue result1(T_OBJECT); 365 JavaCallArguments args1; 366 args1.set_receiver(h_exception); 367 args1.push_oop(h_cause); 368 JavaCalls::call_virtual(&result1, h_exception->klass(), 369 vmSymbols::initCause_name(), 370 vmSymbols::throwable_throwable_signature(), 371 &args1, 372 thread); 373 } 374 375 // Check if another exception was thrown in the process, if so rethrow that one 376 if (thread->has_pending_exception()) { 377 h_exception = Handle(thread, thread->pending_exception()); 378 thread->clear_pending_exception(); 379 } 380 return h_exception; 381 } 382 383 // Convenience method. Calls either the <init>() or <init>(Throwable) method when 384 // creating a new exception 385 Handle Exceptions::new_exception(JavaThread* thread, Symbol* name, 386 Handle h_cause, 387 Handle h_loader, 388 ExceptionMsgToUtf8Mode to_utf8_safe) { 389 JavaCallArguments args; 390 Symbol* signature = nullptr; 391 if (h_cause.is_null()) { 392 signature = vmSymbols::void_method_signature(); 393 } else { 394 signature = vmSymbols::throwable_void_signature(); 395 args.push_oop(h_cause); 396 } 397 return new_exception(thread, name, signature, &args, h_loader); 398 } 399 400 // Convenience method. Calls either the <init>() or <init>(String) method when 401 // creating a new exception 402 Handle Exceptions::new_exception(JavaThread* thread, Symbol* name, 403 const char* message, Handle h_cause, 404 Handle h_loader, 405 ExceptionMsgToUtf8Mode to_utf8_safe) { 406 JavaCallArguments args; 407 Symbol* signature = nullptr; 408 if (message == nullptr) { 409 signature = vmSymbols::void_method_signature(); 410 } else { 411 // There should be no pending exception. The caller is responsible for not calling 412 // this with a pending exception. 413 Handle incoming_exception; 414 if (thread->has_pending_exception()) { 415 incoming_exception = Handle(thread, thread->pending_exception()); 416 thread->clear_pending_exception(); 417 ResourceMark rm(thread); 418 assert(incoming_exception.is_null(), "Pending exception while throwing %s %s", name->as_C_string(), message); 419 } 420 Handle msg; 421 if (to_utf8_safe == safe_to_utf8) { 422 // Make a java UTF8 string. 423 msg = java_lang_String::create_from_str(message, thread); 424 } else { 425 // Make a java string keeping the encoding scheme of the original string. 426 msg = java_lang_String::create_from_platform_dependent_str(message, thread); 427 } 428 // If we get an exception from the allocation, prefer that to 429 // the exception we are trying to build, or the pending exception (in product mode) 430 if (thread->has_pending_exception()) { 431 Handle exception(thread, thread->pending_exception()); 432 thread->clear_pending_exception(); 433 return exception; 434 } 435 if (incoming_exception.not_null()) { 436 return incoming_exception; 437 } 438 args.push_oop(msg); 439 signature = vmSymbols::string_void_signature(); 440 } 441 return new_exception(thread, name, signature, &args, h_cause, h_loader); 442 } 443 444 // Another convenience method that creates handles for null class loaders and null causes. 445 // If the last parameter 'to_utf8_mode' is safe_to_utf8, 446 // it means we can safely ignore the encoding scheme of the message string and 447 // convert it directly to a java UTF8 string. Otherwise, we need to take the 448 // encoding scheme of the string into account. One thing we should do at some 449 // point is to push this flag down to class java_lang_String since other 450 // classes may need similar functionalities. 451 Handle Exceptions::new_exception(JavaThread* thread, Symbol* name, 452 const char* message, 453 ExceptionMsgToUtf8Mode to_utf8_safe) { 454 455 Handle h_loader; 456 Handle h_cause; 457 return Exceptions::new_exception(thread, name, message, h_cause, h_loader, 458 to_utf8_safe); 459 } 460 461 // invokedynamic uses wrap_dynamic_exception for: 462 // - bootstrap method resolution 463 // - post call to MethodHandleNatives::linkCallSite 464 // dynamically computed constant uses wrap_dynamic_exception for: 465 // - bootstrap method resolution 466 // - post call to MethodHandleNatives::linkDynamicConstant 467 void Exceptions::wrap_dynamic_exception(bool is_indy, JavaThread* THREAD) { 468 if (THREAD->has_pending_exception()) { 469 bool log_indy = log_is_enabled(Debug, methodhandles, indy) && is_indy; 470 bool log_condy = log_is_enabled(Debug, methodhandles, condy) && !is_indy; 471 LogStreamHandle(Debug, methodhandles, indy) lsh_indy; 472 LogStreamHandle(Debug, methodhandles, condy) lsh_condy; 473 LogStream* ls = nullptr; 474 if (log_indy) { 475 ls = &lsh_indy; 476 } else if (log_condy) { 477 ls = &lsh_condy; 478 } 479 oop exception = THREAD->pending_exception(); 480 481 // See the "Linking Exceptions" section for the invokedynamic instruction 482 // in JVMS 6.5. 483 if (exception->is_a(vmClasses::Error_klass())) { 484 // Pass through an Error, including BootstrapMethodError, any other form 485 // of linkage error, or say OutOfMemoryError 486 if (ls != nullptr) { 487 ResourceMark rm(THREAD); 488 ls->print_cr("bootstrap method invocation wraps BSME around " PTR_FORMAT, p2i(exception)); 489 exception->print_on(ls); 490 } 491 return; 492 } 493 494 // Otherwise wrap the exception in a BootstrapMethodError 495 if (ls != nullptr) { 496 ResourceMark rm(THREAD); 497 ls->print_cr("%s throws BSME for " PTR_FORMAT, is_indy ? "invokedynamic" : "dynamic constant", p2i(exception)); 498 exception->print_on(ls); 499 } 500 Handle nested_exception(THREAD, exception); 501 THREAD->clear_pending_exception(); 502 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 503 } 504 } 505 506 // Exception counting for hs_err file 507 volatile int Exceptions::_stack_overflow_errors = 0; 508 volatile int Exceptions::_linkage_errors = 0; 509 volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0; 510 volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0; 511 volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0; 512 513 void Exceptions::count_out_of_memory_exceptions(Handle exception) { 514 if (Universe::is_out_of_memory_error_metaspace(exception())) { 515 Atomic::inc(&_out_of_memory_error_metaspace_errors, memory_order_relaxed); 516 } else if (Universe::is_out_of_memory_error_class_metaspace(exception())) { 517 Atomic::inc(&_out_of_memory_error_class_metaspace_errors, memory_order_relaxed); 518 } else { 519 // everything else reported as java heap OOM 520 Atomic::inc(&_out_of_memory_error_java_heap_errors, memory_order_relaxed); 521 } 522 } 523 524 static void print_oom_count(outputStream* st, const char *err, int count) { 525 if (count > 0) { 526 st->print_cr("OutOfMemoryError %s=%d", err, count); 527 } 528 } 529 530 bool Exceptions::has_exception_counts() { 531 return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors + 532 _out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0; 533 } 534 535 void Exceptions::print_exception_counts_on_error(outputStream* st) { 536 print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors); 537 print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors); 538 print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors); 539 if (_stack_overflow_errors > 0) { 540 st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors); 541 } 542 if (_linkage_errors > 0) { 543 st->print_cr("LinkageErrors=%d", _linkage_errors); 544 } 545 } 546 547 // Implementation of ExceptionMark 548 549 ExceptionMark::ExceptionMark(JavaThread* thread) { 550 assert(thread == JavaThread::current(), "must be"); 551 _thread = thread; 552 check_no_pending_exception(); 553 } 554 555 ExceptionMark::ExceptionMark() { 556 _thread = JavaThread::current(); 557 check_no_pending_exception(); 558 } 559 560 inline void ExceptionMark::check_no_pending_exception() { 561 if (_thread->has_pending_exception()) { 562 oop exception = _thread->pending_exception(); 563 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 564 exception->print(); 565 fatal("ExceptionMark constructor expects no pending exceptions"); 566 } 567 } 568 569 570 ExceptionMark::~ExceptionMark() { 571 if (_thread->has_pending_exception()) { 572 Handle exception(_thread, _thread->pending_exception()); 573 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 574 if (is_init_completed()) { 575 exception->print(); 576 fatal("ExceptionMark destructor expects no pending exceptions"); 577 } else { 578 vm_exit_during_initialization(exception); 579 } 580 } 581 } 582 583 // ---------------------------------------------------------------------------------------- 584 585 // caller frees value_string if necessary 586 void Exceptions::debug_check_abort(const char *value_string, const char* message) { 587 if (AbortVMOnException != nullptr && value_string != nullptr && 588 strstr(value_string, AbortVMOnException)) { 589 if (AbortVMOnExceptionMessage == nullptr || (message != nullptr && 590 strstr(message, AbortVMOnExceptionMessage))) { 591 if (message == nullptr) { 592 fatal("Saw %s, aborting", value_string); 593 } else { 594 fatal("Saw %s: %s, aborting", value_string, message); 595 } 596 } 597 } 598 } 599 600 void Exceptions::debug_check_abort(Handle exception, const char* message) { 601 if (AbortVMOnException != nullptr) { 602 debug_check_abort_helper(exception, message); 603 } 604 } 605 606 void Exceptions::debug_check_abort_helper(Handle exception, const char* message) { 607 ResourceMark rm; 608 if (message == nullptr && exception->is_a(vmClasses::Throwable_klass())) { 609 oop msg = java_lang_Throwable::message(exception()); 610 if (msg != nullptr) { 611 message = java_lang_String::as_utf8_string(msg); 612 } 613 } 614 debug_check_abort(exception()->klass()->external_name(), message); 615 } 616 617 // for logging exceptions 618 void Exceptions::log_exception(Handle exception, const char* message) { 619 ResourceMark rm; 620 const char* detail_message = java_lang_Throwable::message_as_utf8(exception()); 621 if (detail_message != nullptr) { 622 log_info(exceptions)("Exception <%.*s: %.*s>\n thrown in %.*s", 623 MAX_LEN, exception->print_value_string(), 624 MAX_LEN, detail_message, 625 MAX_LEN, message); 626 } else { 627 log_info(exceptions)("Exception <%.*s>\n thrown in %.*s", 628 MAX_LEN, exception->print_value_string(), 629 MAX_LEN, message); 630 } 631 }