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