Path: blob/master/src/hotspot/share/utilities/exceptions.cpp
40949 views
/*1* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"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/os.hpp"40#include "runtime/thread.inline.hpp"41#include "runtime/threadCritical.hpp"42#include "runtime/atomic.hpp"43#include "utilities/events.hpp"44#include "utilities/exceptions.hpp"4546// Implementation of ThreadShadow47void check_ThreadShadow() {48const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception);49const ByteSize offset2 = Thread::pending_exception_offset();50if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly");51}525354void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) {55assert(exception != NULL && oopDesc::is_oop(exception), "invalid exception oop");56_pending_exception = exception;57_exception_file = file;58_exception_line = line;59}6061void ThreadShadow::clear_pending_exception() {62LogTarget(Debug, exceptions) lt;63if (_pending_exception != NULL && lt.is_enabled()) {64ResourceMark rm;65LogStream ls(lt);66ls.print("Thread::clear_pending_exception: cleared exception:");67_pending_exception->print_on(&ls);68}69_pending_exception = NULL;70_exception_file = NULL;71_exception_line = 0;72}7374void ThreadShadow::clear_pending_nonasync_exception() {75// Do not clear probable async exceptions.76if (!_pending_exception->is_a(vmClasses::ThreadDeath_klass()) &&77(_pending_exception->klass() != vmClasses::InternalError_klass() ||78java_lang_InternalError::during_unsafe_access(_pending_exception) != JNI_TRUE)) {79clear_pending_exception();80}81}8283// Implementation of Exceptions8485bool Exceptions::special_exception(JavaThread* thread, const char* file, int line, Handle h_exception) {86// bootstrapping check87if (!Universe::is_fully_initialized()) {88vm_exit_during_initialization(h_exception);89ShouldNotReachHere();90}9192#ifdef ASSERT93// Check for trying to throw stack overflow before initialization is complete94// to prevent infinite recursion trying to initialize stack overflow without95// adequate stack space.96// This can happen with stress testing a large value of StackShadowPages97if (h_exception()->klass() == vmClasses::StackOverflowError_klass()) {98InstanceKlass* ik = InstanceKlass::cast(h_exception->klass());99assert(ik->is_initialized(),100"need to increase java_thread_min_stack_allowed calculation");101}102#endif // ASSERT103104if (!thread->can_call_java()) {105// We do not care what kind of exception we get for a thread which106// is compiling. We just install a dummy exception object107thread->set_pending_exception(Universe::vm_exception(), file, line);108return true;109}110111return false;112}113114bool Exceptions::special_exception(JavaThread* thread, const char* file, int line, Symbol* h_name, const char* message) {115// bootstrapping check116if (!Universe::is_fully_initialized()) {117if (h_name == NULL) {118// atleast an informative message.119vm_exit_during_initialization("Exception", message);120} else {121vm_exit_during_initialization(h_name, message);122}123ShouldNotReachHere();124}125126if (!thread->can_call_java()) {127// We do not care what kind of exception we get for a thread which128// is compiling. We just install a dummy exception object129thread->set_pending_exception(Universe::vm_exception(), file, line);130return true;131}132return false;133}134135// This method should only be called from generated code,136// therefore the exception oop should be in the oopmap.137void Exceptions::_throw_oop(JavaThread* thread, const char* file, int line, oop exception) {138assert(exception != NULL, "exception should not be NULL");139Handle h_exception(thread, exception);140_throw(thread, file, line, h_exception);141}142143void Exceptions::_throw(JavaThread* thread, const char* file, int line, Handle h_exception, const char* message) {144ResourceMark rm(thread);145assert(h_exception() != NULL, "exception should not be NULL");146147// tracing (do this up front - so it works during boot strapping)148// Note, the print_value_string() argument is not called unless logging is enabled!149log_info(exceptions)("Exception <%s%s%s> (" INTPTR_FORMAT ") \n"150"thrown [%s, line %d]\nfor thread " INTPTR_FORMAT,151h_exception->print_value_string(),152message ? ": " : "", message ? message : "",153p2i(h_exception()), file, line, p2i(thread));154155// for AbortVMOnException flag156Exceptions::debug_check_abort(h_exception, message);157158// Check for special boot-strapping/compiler-thread handling159if (special_exception(thread, file, line, h_exception)) {160return;161}162163if (h_exception->is_a(vmClasses::OutOfMemoryError_klass())) {164count_out_of_memory_exceptions(h_exception);165}166167if (h_exception->is_a(vmClasses::LinkageError_klass())) {168Atomic::inc(&_linkage_errors);169}170171assert(h_exception->is_a(vmClasses::Throwable_klass()), "exception is not a subclass of java/lang/Throwable");172173// set the pending exception174thread->set_pending_exception(h_exception(), file, line);175176// vm log177Events::log_exception(thread, h_exception, message, file, line);178}179180181void Exceptions::_throw_msg(JavaThread* thread, const char* file, int line, Symbol* name, const char* message,182Handle h_loader, Handle h_protection_domain) {183// Check for special boot-strapping/compiler-thread handling184if (special_exception(thread, file, line, name, message)) return;185// Create and throw exception186Handle h_cause(thread, NULL);187Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);188_throw(thread, file, line, h_exception, message);189}190191void Exceptions::_throw_msg_cause(JavaThread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause,192Handle h_loader, Handle h_protection_domain) {193// Check for special boot-strapping/compiler-thread handling194if (special_exception(thread, file, line, name, message)) return;195// Create and throw exception and init cause196Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);197_throw(thread, file, line, h_exception, message);198}199200void Exceptions::_throw_cause(JavaThread* thread, const char* file, int line, Symbol* name, Handle h_cause,201Handle h_loader, Handle h_protection_domain) {202// Check for special boot-strapping/compiler-thread handling203if (special_exception(thread, file, line, h_cause)) return;204// Create and throw exception205Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain);206_throw(thread, file, line, h_exception, NULL);207}208209void Exceptions::_throw_args(JavaThread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) {210// Check for special boot-strapping/compiler-thread handling211if (special_exception(thread, file, line, name, NULL)) return;212// Create and throw exception213Handle h_loader(thread, NULL);214Handle h_prot(thread, NULL);215Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot);216_throw(thread, file, line, exception);217}218219220// Methods for default parameters.221// NOTE: These must be here (and not in the header file) because of include circularities.222void Exceptions::_throw_msg_cause(JavaThread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) {223_throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL));224}225void Exceptions::_throw_msg(JavaThread* thread, const char* file, int line, Symbol* name, const char* message) {226_throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL));227}228void Exceptions::_throw_cause(JavaThread* thread, const char* file, int line, Symbol* name, Handle h_cause) {229_throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL));230}231232233void Exceptions::throw_stack_overflow_exception(JavaThread* THREAD, const char* file, int line, const methodHandle& method) {234Handle exception;235if (!THREAD->has_pending_exception()) {236InstanceKlass* k = vmClasses::StackOverflowError_klass();237oop e = k->allocate_instance(CHECK);238exception = Handle(THREAD, e); // fill_in_stack trace does gc239assert(k->is_initialized(), "need to increase java_thread_min_stack_allowed calculation");240if (StackTraceInThrowable) {241java_lang_Throwable::fill_in_stack_trace(exception, method);242}243// Increment counter for hs_err file reporting244Atomic::inc(&Exceptions::_stack_overflow_errors);245} else {246// if prior exception, throw that one instead247exception = Handle(THREAD, THREAD->pending_exception());248}249_throw(THREAD, file, line, exception);250}251252void Exceptions::throw_unsafe_access_internal_error(JavaThread* thread, const char* file, int line, const char* message) {253Handle h_exception = new_exception(thread, vmSymbols::java_lang_InternalError(), message);254java_lang_InternalError::set_during_unsafe_access(h_exception());255_throw(thread, file, line, h_exception, message);256}257258void Exceptions::fthrow(JavaThread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) {259const int max_msg_size = 1024;260va_list ap;261va_start(ap, format);262char msg[max_msg_size];263os::vsnprintf(msg, max_msg_size, format, ap);264va_end(ap);265_throw_msg(thread, file, line, h_name, msg);266}267268269// Creates an exception oop, calls the <init> method with the given signature.270// and returns a Handle271Handle Exceptions::new_exception(JavaThread* thread, Symbol* name,272Symbol* signature, JavaCallArguments *args,273Handle h_loader, Handle h_protection_domain) {274assert(Universe::is_fully_initialized(),275"cannot be called during initialization");276assert(!thread->has_pending_exception(), "already has exception");277278Handle h_exception;279280// Resolve exception klass, and check for pending exception below.281Klass* klass = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread);282283if (!thread->has_pending_exception()) {284assert(klass != NULL, "klass must exist");285h_exception = JavaCalls::construct_new_instance(InstanceKlass::cast(klass),286signature,287args,288thread);289}290291// Check if another exception was thrown in the process, if so rethrow that one292if (thread->has_pending_exception()) {293h_exception = Handle(thread, thread->pending_exception());294thread->clear_pending_exception();295}296return h_exception;297}298299// Creates an exception oop, calls the <init> method with the given signature.300// and returns a Handle301// Initializes the cause if cause non-null302Handle Exceptions::new_exception(JavaThread* thread, Symbol* name,303Symbol* signature, JavaCallArguments *args,304Handle h_cause,305Handle h_loader, Handle h_protection_domain) {306Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain);307308// Future: object initializer should take a cause argument309if (h_cause.not_null()) {310assert(h_cause->is_a(vmClasses::Throwable_klass()),311"exception cause is not a subclass of java/lang/Throwable");312JavaValue result1(T_OBJECT);313JavaCallArguments args1;314args1.set_receiver(h_exception);315args1.push_oop(h_cause);316JavaCalls::call_virtual(&result1, h_exception->klass(),317vmSymbols::initCause_name(),318vmSymbols::throwable_throwable_signature(),319&args1,320thread);321}322323// Check if another exception was thrown in the process, if so rethrow that one324if (thread->has_pending_exception()) {325h_exception = Handle(thread, thread->pending_exception());326thread->clear_pending_exception();327}328return h_exception;329}330331// Convenience method. Calls either the <init>() or <init>(Throwable) method when332// creating a new exception333Handle Exceptions::new_exception(JavaThread* thread, Symbol* name,334Handle h_cause,335Handle h_loader, Handle h_protection_domain,336ExceptionMsgToUtf8Mode to_utf8_safe) {337JavaCallArguments args;338Symbol* signature = NULL;339if (h_cause.is_null()) {340signature = vmSymbols::void_method_signature();341} else {342signature = vmSymbols::throwable_void_signature();343args.push_oop(h_cause);344}345return new_exception(thread, name, signature, &args, h_loader, h_protection_domain);346}347348// Convenience method. Calls either the <init>() or <init>(String) method when349// creating a new exception350Handle Exceptions::new_exception(JavaThread* thread, Symbol* name,351const char* message, Handle h_cause,352Handle h_loader, Handle h_protection_domain,353ExceptionMsgToUtf8Mode to_utf8_safe) {354JavaCallArguments args;355Symbol* signature = NULL;356if (message == NULL) {357signature = vmSymbols::void_method_signature();358} else {359// We want to allocate storage, but we can't do that if there's360// a pending exception, so we preserve any pending exception361// around the allocation.362// If we get an exception from the allocation, prefer that to363// the exception we are trying to build, or the pending exception.364// This is sort of like what PreserveExceptionMark does, except365// for the preferencing and the early returns.366Handle incoming_exception(thread, NULL);367if (thread->has_pending_exception()) {368incoming_exception = Handle(thread, thread->pending_exception());369thread->clear_pending_exception();370}371Handle msg;372if (to_utf8_safe == safe_to_utf8) {373// Make a java UTF8 string.374msg = java_lang_String::create_from_str(message, thread);375} else {376// Make a java string keeping the encoding scheme of the original string.377msg = java_lang_String::create_from_platform_dependent_str(message, thread);378}379if (thread->has_pending_exception()) {380Handle exception(thread, thread->pending_exception());381thread->clear_pending_exception();382return exception;383}384if (incoming_exception.not_null()) {385return incoming_exception;386}387args.push_oop(msg);388signature = vmSymbols::string_void_signature();389}390return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain);391}392393// Another convenience method that creates handles for null class loaders and394// protection domains and null causes.395// If the last parameter 'to_utf8_mode' is safe_to_utf8,396// it means we can safely ignore the encoding scheme of the message string and397// convert it directly to a java UTF8 string. Otherwise, we need to take the398// encoding scheme of the string into account. One thing we should do at some399// point is to push this flag down to class java_lang_String since other400// classes may need similar functionalities.401Handle Exceptions::new_exception(JavaThread* thread, Symbol* name,402const char* message,403ExceptionMsgToUtf8Mode to_utf8_safe) {404405Handle h_loader(thread, NULL);406Handle h_prot(thread, NULL);407Handle h_cause(thread, NULL);408return Exceptions::new_exception(thread, name, message, h_cause, h_loader,409h_prot, to_utf8_safe);410}411412// invokedynamic uses wrap_dynamic_exception for:413// - bootstrap method resolution414// - post call to MethodHandleNatives::linkCallSite415// dynamically computed constant uses wrap_dynamic_exception for:416// - bootstrap method resolution417// - post call to MethodHandleNatives::linkDynamicConstant418void Exceptions::wrap_dynamic_exception(bool is_indy, JavaThread* THREAD) {419if (THREAD->has_pending_exception()) {420bool log_indy = log_is_enabled(Debug, methodhandles, indy) && is_indy;421bool log_condy = log_is_enabled(Debug, methodhandles, condy) && !is_indy;422LogStreamHandle(Debug, methodhandles, indy) lsh_indy;423LogStreamHandle(Debug, methodhandles, condy) lsh_condy;424LogStream* ls = NULL;425if (log_indy) {426ls = &lsh_indy;427} else if (log_condy) {428ls = &lsh_condy;429}430oop exception = THREAD->pending_exception();431432// See the "Linking Exceptions" section for the invokedynamic instruction433// in JVMS 6.5.434if (exception->is_a(vmClasses::Error_klass())) {435// Pass through an Error, including BootstrapMethodError, any other form436// of linkage error, or say ThreadDeath/OutOfMemoryError437if (ls != NULL) {438ls->print_cr("bootstrap method invocation wraps BSME around " INTPTR_FORMAT, p2i((void *)exception));439exception->print_on(ls);440}441return;442}443444// Otherwise wrap the exception in a BootstrapMethodError445if (ls != NULL) {446ls->print_cr("%s throws BSME for " INTPTR_FORMAT, is_indy ? "invokedynamic" : "dynamic constant", p2i((void *)exception));447exception->print_on(ls);448}449Handle nested_exception(THREAD, exception);450THREAD->clear_pending_exception();451THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)452}453}454455// Exception counting for hs_err file456volatile int Exceptions::_stack_overflow_errors = 0;457volatile int Exceptions::_linkage_errors = 0;458volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0;459volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0;460volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0;461462void Exceptions::count_out_of_memory_exceptions(Handle exception) {463if (exception() == Universe::out_of_memory_error_metaspace()) {464Atomic::inc(&_out_of_memory_error_metaspace_errors);465} else if (exception() == Universe::out_of_memory_error_class_metaspace()) {466Atomic::inc(&_out_of_memory_error_class_metaspace_errors);467} else {468// everything else reported as java heap OOM469Atomic::inc(&_out_of_memory_error_java_heap_errors);470}471}472473void print_oom_count(outputStream* st, const char *err, int count) {474if (count > 0) {475st->print_cr("OutOfMemoryError %s=%d", err, count);476}477}478479bool Exceptions::has_exception_counts() {480return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors +481_out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0;482}483484void Exceptions::print_exception_counts_on_error(outputStream* st) {485print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors);486print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors);487print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors);488if (_stack_overflow_errors > 0) {489st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors);490}491if (_linkage_errors > 0) {492st->print_cr("LinkageErrors=%d", _linkage_errors);493}494}495496// Implementation of ExceptionMark497498ExceptionMark::ExceptionMark(JavaThread* thread) {499assert(thread == JavaThread::current(), "must be");500_thread = thread;501check_no_pending_exception();502}503504ExceptionMark::ExceptionMark() {505_thread = JavaThread::current();506check_no_pending_exception();507}508509inline void ExceptionMark::check_no_pending_exception() {510if (_thread->has_pending_exception()) {511oop exception = _thread->pending_exception();512_thread->clear_pending_exception(); // Needed to avoid infinite recursion513exception->print();514fatal("ExceptionMark constructor expects no pending exceptions");515}516}517518519ExceptionMark::~ExceptionMark() {520if (_thread->has_pending_exception()) {521Handle exception(_thread, _thread->pending_exception());522_thread->clear_pending_exception(); // Needed to avoid infinite recursion523if (is_init_completed()) {524exception->print();525fatal("ExceptionMark destructor expects no pending exceptions");526} else {527vm_exit_during_initialization(exception);528}529}530}531532// ----------------------------------------------------------------------------------------533534// caller frees value_string if necessary535void Exceptions::debug_check_abort(const char *value_string, const char* message) {536if (AbortVMOnException != NULL && value_string != NULL &&537strstr(value_string, AbortVMOnException)) {538if (AbortVMOnExceptionMessage == NULL || (message != NULL &&539strstr(message, AbortVMOnExceptionMessage))) {540fatal("Saw %s, aborting", value_string);541}542}543}544545void Exceptions::debug_check_abort(Handle exception, const char* message) {546if (AbortVMOnException != NULL) {547debug_check_abort_helper(exception, message);548}549}550551void Exceptions::debug_check_abort_helper(Handle exception, const char* message) {552ResourceMark rm;553if (message == NULL && exception->is_a(vmClasses::Throwable_klass())) {554oop msg = java_lang_Throwable::message(exception());555if (msg != NULL) {556message = java_lang_String::as_utf8_string(msg);557}558}559debug_check_abort(exception()->klass()->external_name(), message);560}561562// for logging exceptions563void Exceptions::log_exception(Handle exception, const char* message) {564ResourceMark rm;565Symbol* detail_message = java_lang_Throwable::detail_message(exception());566if (detail_message != NULL) {567log_info(exceptions)("Exception <%s: %s>\n thrown in %s",568exception->print_value_string(),569detail_message->as_C_string(),570message);571} else {572log_info(exceptions)("Exception <%s>\n thrown in %s",573exception->print_value_string(),574message);575}576}577578579