Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/exceptions.cpp
32285 views
/*1* Copyright (c) 1998, 2018, 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/systemDictionary.hpp"26#include "classfile/vmSymbols.hpp"27#include "compiler/compileBroker.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/init.hpp"30#include "runtime/java.hpp"31#include "runtime/javaCalls.hpp"32#include "runtime/os.hpp"33#include "runtime/thread.inline.hpp"34#include "runtime/threadCritical.hpp"35#include "utilities/events.hpp"36#include "utilities/exceptions.hpp"3738PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3940// Implementation of ThreadShadow41void check_ThreadShadow() {42const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception);43const ByteSize offset2 = Thread::pending_exception_offset();44if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly");45}464748void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) {49assert(exception != NULL && exception->is_oop(), "invalid exception oop");50_pending_exception = exception;51_exception_file = file;52_exception_line = line;53}5455void ThreadShadow::clear_pending_exception() {56if (TraceClearedExceptions) {57if (_pending_exception != NULL) {58tty->print_cr("Thread::clear_pending_exception: cleared exception:");59_pending_exception->print();60}61}62_pending_exception = NULL;63_exception_file = NULL;64_exception_line = 0;65}66// Implementation of Exceptions6768bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) {69// bootstrapping check70if (!Universe::is_fully_initialized()) {71vm_exit_during_initialization(h_exception);72ShouldNotReachHere();73}7475#ifdef ASSERT76// Check for trying to throw stack overflow before initialization is complete77// to prevent infinite recursion trying to initialize stack overflow without78// adequate stack space.79// This can happen with stress testing a large value of StackShadowPages80if (h_exception()->klass() == SystemDictionary::StackOverflowError_klass()) {81InstanceKlass* ik = InstanceKlass::cast(h_exception->klass());82assert(ik->is_initialized(),83"need to increase min_stack_allowed calculation");84}85#endif // ASSERT8687if (thread->is_VM_thread()88|| thread->is_Compiler_thread()89|| DumpSharedSpaces ) {90// We do not care what kind of exception we get for the vm-thread or a thread which91// is compiling. We just install a dummy exception object92//93// We also cannot throw a proper exception when dumping, because we cannot run94// Java bytecodes now. A dummy exception will suffice.95thread->set_pending_exception(Universe::vm_exception(), file, line);96return true;97}9899return false;100}101102bool Exceptions::special_exception(Thread* thread, const char* file, int line, Symbol* h_name, const char* message) {103// bootstrapping check104if (!Universe::is_fully_initialized()) {105if (h_name == NULL) {106// atleast an informative message.107vm_exit_during_initialization("Exception", message);108} else {109vm_exit_during_initialization(h_name, message);110}111ShouldNotReachHere();112}113114if (thread->is_VM_thread()115|| thread->is_Compiler_thread()116|| DumpSharedSpaces ) {117// We do not care what kind of exception we get for the vm-thread or a thread which118// is compiling. We just install a dummy exception object119//120// We also cannot throw a proper exception when dumping, because we cannot run121// Java bytecodes now. A dummy exception will suffice.122thread->set_pending_exception(Universe::vm_exception(), file, line);123return true;124}125return false;126}127128// This method should only be called from generated code,129// therefore the exception oop should be in the oopmap.130void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) {131assert(exception != NULL, "exception should not be NULL");132Handle h_exception = Handle(thread, exception);133_throw(thread, file, line, h_exception);134}135136void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) {137ResourceMark rm;138assert(h_exception() != NULL, "exception should not be NULL");139140// tracing (do this up front - so it works during boot strapping)141if (TraceExceptions) {142ttyLocker ttyl;143tty->print_cr("Exception <%s%s%s> (" INTPTR_FORMAT ") \n"144"thrown [%s, line %d]\nfor thread " INTPTR_FORMAT,145h_exception->print_value_string(),146message ? ": " : "", message ? message : "",147(address)h_exception(), file, line, thread);148}149// for AbortVMOnException flag150NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message));151152// Check for special boot-strapping/vm-thread handling153if (special_exception(thread, file, line, h_exception)) {154return;155}156157if (h_exception->is_a(SystemDictionary::OutOfMemoryError_klass())) {158count_out_of_memory_exceptions(h_exception);159}160161assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable");162163// set the pending exception164thread->set_pending_exception(h_exception(), file, line);165166// vm log167Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]",168h_exception->print_value_string(), message ? ": " : "", message ? message : "",169(address)h_exception(), file, line);170}171172173void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message,174Handle h_loader, Handle h_protection_domain) {175// Check for special boot-strapping/vm-thread handling176if (special_exception(thread, file, line, name, message)) return;177// Create and throw exception178Handle h_cause(thread, NULL);179Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);180_throw(thread, file, line, h_exception, message);181}182183void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause,184Handle h_loader, Handle h_protection_domain) {185// Check for special boot-strapping/vm-thread handling186if (special_exception(thread, file, line, name, message)) return;187// Create and throw exception and init cause188Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain);189_throw(thread, file, line, h_exception, message);190}191192void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause,193Handle h_loader, Handle h_protection_domain) {194// Check for special boot-strapping/vm-thread handling195if (special_exception(thread, file, line, h_cause)) return;196// Create and throw exception197Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain);198_throw(thread, file, line, h_exception, NULL);199}200201void Exceptions::_throw_args(Thread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) {202// Check for special boot-strapping/vm-thread handling203if (special_exception(thread, file, line, name, NULL)) return;204// Create and throw exception205Handle h_loader(thread, NULL);206Handle h_prot(thread, NULL);207Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot);208_throw(thread, file, line, exception);209}210211212// Methods for default parameters.213// NOTE: These must be here (and not in the header file) because of include circularities.214void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) {215_throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL));216}217void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) {218_throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL));219}220void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) {221_throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL));222}223224225void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, methodHandle method) {226Handle exception;227if (!THREAD->has_pending_exception()) {228Klass* k = SystemDictionary::StackOverflowError_klass();229oop e = InstanceKlass::cast(k)->allocate_instance(CHECK);230exception = Handle(THREAD, e); // fill_in_stack trace does gc231assert(InstanceKlass::cast(k)->is_initialized(), "need to increase min_stack_allowed calculation");232if (StackTraceInThrowable) {233java_lang_Throwable::fill_in_stack_trace(exception, method());234}235// Increment counter for hs_err file reporting236Atomic::inc(&Exceptions::_stack_overflow_errors);237} else {238// if prior exception, throw that one instead239exception = Handle(THREAD, THREAD->pending_exception());240}241_throw(THREAD, file, line, exception);242}243244void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) {245const int max_msg_size = 1024;246va_list ap;247va_start(ap, format);248char msg[max_msg_size];249os::vsnprintf(msg, max_msg_size, format, ap);250va_end(ap);251_throw_msg(thread, file, line, h_name, msg);252}253254255// Creates an exception oop, calls the <init> method with the given signature.256// and returns a Handle257Handle Exceptions::new_exception(Thread *thread, Symbol* name,258Symbol* signature, JavaCallArguments *args,259Handle h_loader, Handle h_protection_domain) {260assert(Universe::is_fully_initialized(),261"cannot be called during initialization");262assert(thread->is_Java_thread(), "can only be called by a Java thread");263assert(!thread->has_pending_exception(), "already has exception");264265Handle h_exception;266267// Resolve exception klass268Klass* ik = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread);269instanceKlassHandle klass(thread, ik);270271if (!thread->has_pending_exception()) {272assert(klass.not_null(), "klass must exist");273// We are about to create an instance - so make sure that klass is initialized274klass->initialize(thread);275if (!thread->has_pending_exception()) {276// Allocate new exception277h_exception = klass->allocate_instance_handle(thread);278if (!thread->has_pending_exception()) {279JavaValue result(T_VOID);280args->set_receiver(h_exception);281// Call constructor282JavaCalls::call_special(&result, klass,283vmSymbols::object_initializer_name(),284signature,285args,286thread);287}288}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(Thread *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(SystemDictionary::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(Thread* 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(Thread* 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 PRESERVE_EXCEPTION_MARK 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(Thread* 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}411412413// Exception counting for hs_err file414volatile int Exceptions::_stack_overflow_errors = 0;415volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0;416volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0;417volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0;418419void Exceptions::count_out_of_memory_exceptions(Handle exception) {420if (exception() == Universe::out_of_memory_error_metaspace()) {421Atomic::inc(&_out_of_memory_error_metaspace_errors);422} else if (exception() == Universe::out_of_memory_error_class_metaspace()) {423Atomic::inc(&_out_of_memory_error_class_metaspace_errors);424} else {425// everything else reported as java heap OOM426Atomic::inc(&_out_of_memory_error_java_heap_errors);427}428}429430void print_oom_count(outputStream* st, const char *err, int count) {431if (count > 0) {432st->print_cr("OutOfMemoryError %s=%d", err, count);433}434}435436bool Exceptions::has_exception_counts() {437return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors +438_out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0;439}440441void Exceptions::print_exception_counts_on_error(outputStream* st) {442print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors);443print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors);444print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors);445if (_stack_overflow_errors > 0) {446st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors);447}448}449450// Implementation of ExceptionMark451452ExceptionMark::ExceptionMark(Thread*& thread) {453thread = Thread::current();454_thread = thread;455if (_thread->has_pending_exception()) {456oop exception = _thread->pending_exception();457_thread->clear_pending_exception(); // Needed to avoid infinite recursion458exception->print();459fatal("ExceptionMark constructor expects no pending exceptions");460}461}462463464ExceptionMark::~ExceptionMark() {465if (_thread->has_pending_exception()) {466Handle exception(_thread, _thread->pending_exception());467_thread->clear_pending_exception(); // Needed to avoid infinite recursion468if (is_init_completed()) {469exception->print();470fatal("ExceptionMark destructor expects no pending exceptions");471} else {472vm_exit_during_initialization(exception);473}474}475}476477// ----------------------------------------------------------------------------------------478479#ifndef PRODUCT480// caller frees value_string if necessary481void Exceptions::debug_check_abort(const char *value_string, const char* message) {482if (AbortVMOnException != NULL && value_string != NULL &&483strstr(value_string, AbortVMOnException)) {484if (AbortVMOnExceptionMessage == NULL || message == NULL ||485strcmp(message, AbortVMOnExceptionMessage) == 0) {486fatal(err_msg("Saw %s, aborting", value_string));487}488}489}490491void Exceptions::debug_check_abort(Handle exception, const char* message) {492if (AbortVMOnException != NULL) {493ResourceMark rm;494if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) {495oop msg = java_lang_Throwable::message(exception);496if (msg != NULL) {497message = java_lang_String::as_utf8_string(msg);498}499}500debug_check_abort(InstanceKlass::cast(exception()->klass())->external_name(), message);501}502}503#endif504505506