aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/init.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/threadCritical.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/exceptions.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: // Implementation of ThreadShadow aoqi@0: void check_ThreadShadow() { aoqi@0: const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); aoqi@0: const ByteSize offset2 = Thread::pending_exception_offset(); aoqi@0: if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { aoqi@0: assert(exception != NULL && exception->is_oop(), "invalid exception oop"); aoqi@0: _pending_exception = exception; aoqi@0: _exception_file = file; aoqi@0: _exception_line = line; aoqi@0: } aoqi@0: aoqi@0: void ThreadShadow::clear_pending_exception() { aoqi@0: if (TraceClearedExceptions) { aoqi@0: if (_pending_exception != NULL) { aoqi@0: tty->print_cr("Thread::clear_pending_exception: cleared exception:"); aoqi@0: _pending_exception->print(); aoqi@0: } aoqi@0: } aoqi@0: _pending_exception = NULL; aoqi@0: _exception_file = NULL; aoqi@0: _exception_line = 0; aoqi@0: } aoqi@0: // Implementation of Exceptions aoqi@0: aoqi@0: bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) { aoqi@0: // bootstrapping check aoqi@0: if (!Universe::is_fully_initialized()) { aoqi@0: vm_exit_during_initialization(h_exception); aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // Check for trying to throw stack overflow before initialization is complete aoqi@0: // to prevent infinite recursion trying to initialize stack overflow without aoqi@0: // adequate stack space. aoqi@0: // This can happen with stress testing a large value of StackShadowPages aoqi@0: if (h_exception()->klass() == SystemDictionary::StackOverflowError_klass()) { aoqi@0: InstanceKlass* ik = InstanceKlass::cast(h_exception->klass()); aoqi@0: assert(ik->is_initialized(), aoqi@0: "need to increase min_stack_allowed calculation"); aoqi@0: } aoqi@0: #endif // ASSERT aoqi@0: aoqi@0: if (thread->is_VM_thread() iklam@7089: || thread->is_Compiler_thread() iklam@7089: || DumpSharedSpaces ) { aoqi@0: // We do not care what kind of exception we get for the vm-thread or a thread which aoqi@0: // is compiling. We just install a dummy exception object iklam@7089: // iklam@7089: // We also cannot throw a proper exception when dumping, because we cannot run iklam@7089: // Java bytecodes now. A dummy exception will suffice. aoqi@0: thread->set_pending_exception(Universe::vm_exception(), file, line); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: bool Exceptions::special_exception(Thread* thread, const char* file, int line, Symbol* h_name, const char* message) { aoqi@0: // bootstrapping check aoqi@0: if (!Universe::is_fully_initialized()) { aoqi@0: if (h_name == NULL) { aoqi@0: // atleast an informative message. aoqi@0: vm_exit_during_initialization("Exception", message); aoqi@0: } else { aoqi@0: vm_exit_during_initialization(h_name, message); aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: if (thread->is_VM_thread() iklam@7089: || thread->is_Compiler_thread() iklam@7089: || DumpSharedSpaces ) { aoqi@0: // We do not care what kind of exception we get for the vm-thread or a thread which aoqi@0: // is compiling. We just install a dummy exception object iklam@7089: // iklam@7089: // We also cannot throw a proper exception when dumping, because we cannot run iklam@7089: // Java bytecodes now. A dummy exception will suffice. aoqi@0: thread->set_pending_exception(Universe::vm_exception(), file, line); aoqi@0: return true; aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // This method should only be called from generated code, aoqi@0: // therefore the exception oop should be in the oopmap. aoqi@0: void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) { aoqi@0: assert(exception != NULL, "exception should not be NULL"); aoqi@0: Handle h_exception = Handle(thread, exception); aoqi@0: _throw(thread, file, line, h_exception); aoqi@0: } aoqi@0: aoqi@0: void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) { aoqi@0: ResourceMark rm; aoqi@0: assert(h_exception() != NULL, "exception should not be NULL"); aoqi@0: aoqi@0: // tracing (do this up front - so it works during boot strapping) aoqi@0: if (TraceExceptions) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("Exception <%s%s%s> (" INTPTR_FORMAT ") \n" aoqi@0: "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT, aoqi@0: h_exception->print_value_string(), aoqi@0: message ? ": " : "", message ? message : "", aoqi@0: (address)h_exception(), file, line, thread); aoqi@0: } aoqi@0: // for AbortVMOnException flag aoqi@0: NOT_PRODUCT(Exceptions::debug_check_abort(h_exception, message)); aoqi@0: aoqi@0: // Check for special boot-strapping/vm-thread handling aoqi@0: if (special_exception(thread, file, line, h_exception)) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); aoqi@0: aoqi@0: // set the pending exception aoqi@0: thread->set_pending_exception(h_exception(), file, line); aoqi@0: aoqi@0: // vm log aoqi@0: Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]", aoqi@0: h_exception->print_value_string(), message ? ": " : "", message ? message : "", aoqi@0: (address)h_exception(), file, line); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message, aoqi@0: Handle h_loader, Handle h_protection_domain) { aoqi@0: // Check for special boot-strapping/vm-thread handling aoqi@0: if (special_exception(thread, file, line, name, message)) return; aoqi@0: // Create and throw exception aoqi@0: Handle h_cause(thread, NULL); aoqi@0: Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); aoqi@0: _throw(thread, file, line, h_exception, message); aoqi@0: } aoqi@0: aoqi@0: void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause, aoqi@0: Handle h_loader, Handle h_protection_domain) { aoqi@0: // Check for special boot-strapping/vm-thread handling aoqi@0: if (special_exception(thread, file, line, name, message)) return; aoqi@0: // Create and throw exception and init cause aoqi@0: Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); aoqi@0: _throw(thread, file, line, h_exception, message); aoqi@0: } aoqi@0: aoqi@0: void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause, aoqi@0: Handle h_loader, Handle h_protection_domain) { aoqi@0: // Check for special boot-strapping/vm-thread handling aoqi@0: if (special_exception(thread, file, line, h_cause)) return; aoqi@0: // Create and throw exception aoqi@0: Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain); aoqi@0: _throw(thread, file, line, h_exception, NULL); aoqi@0: } aoqi@0: aoqi@0: void Exceptions::_throw_args(Thread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) { aoqi@0: // Check for special boot-strapping/vm-thread handling aoqi@0: if (special_exception(thread, file, line, name, NULL)) return; aoqi@0: // Create and throw exception aoqi@0: Handle h_loader(thread, NULL); aoqi@0: Handle h_prot(thread, NULL); aoqi@0: Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot); aoqi@0: _throw(thread, file, line, exception); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Methods for default parameters. aoqi@0: // NOTE: These must be here (and not in the header file) because of include circularities. aoqi@0: void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) { aoqi@0: _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL)); aoqi@0: } aoqi@0: void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) { aoqi@0: _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL)); aoqi@0: } aoqi@0: void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) { aoqi@0: _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL)); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, methodHandle method) { aoqi@0: Handle exception; aoqi@0: if (!THREAD->has_pending_exception()) { aoqi@0: Klass* k = SystemDictionary::StackOverflowError_klass(); aoqi@0: oop e = InstanceKlass::cast(k)->allocate_instance(CHECK); aoqi@0: exception = Handle(THREAD, e); // fill_in_stack trace does gc aoqi@0: assert(InstanceKlass::cast(k)->is_initialized(), "need to increase min_stack_allowed calculation"); aoqi@0: if (StackTraceInThrowable) { aoqi@0: java_lang_Throwable::fill_in_stack_trace(exception, method()); aoqi@0: } aoqi@0: } else { aoqi@0: // if prior exception, throw that one instead aoqi@0: exception = Handle(THREAD, THREAD->pending_exception()); aoqi@0: } aoqi@0: _throw(THREAD, file, line, exception); aoqi@0: } aoqi@0: aoqi@0: void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) { aoqi@0: const int max_msg_size = 1024; aoqi@0: va_list ap; aoqi@0: va_start(ap, format); aoqi@0: char msg[max_msg_size]; aoqi@0: vsnprintf(msg, max_msg_size, format, ap); aoqi@0: msg[max_msg_size-1] = '\0'; aoqi@0: va_end(ap); aoqi@0: _throw_msg(thread, file, line, h_name, msg); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Creates an exception oop, calls the method with the given signature. aoqi@0: // and returns a Handle aoqi@0: Handle Exceptions::new_exception(Thread *thread, Symbol* name, aoqi@0: Symbol* signature, JavaCallArguments *args, aoqi@0: Handle h_loader, Handle h_protection_domain) { aoqi@0: assert(Universe::is_fully_initialized(), aoqi@0: "cannot be called during initialization"); aoqi@0: assert(thread->is_Java_thread(), "can only be called by a Java thread"); aoqi@0: assert(!thread->has_pending_exception(), "already has exception"); aoqi@0: aoqi@0: Handle h_exception; aoqi@0: aoqi@0: // Resolve exception klass aoqi@0: Klass* ik = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread); aoqi@0: instanceKlassHandle klass(thread, ik); aoqi@0: aoqi@0: if (!thread->has_pending_exception()) { aoqi@0: assert(klass.not_null(), "klass must exist"); aoqi@0: // We are about to create an instance - so make sure that klass is initialized aoqi@0: klass->initialize(thread); aoqi@0: if (!thread->has_pending_exception()) { aoqi@0: // Allocate new exception aoqi@0: h_exception = klass->allocate_instance_handle(thread); aoqi@0: if (!thread->has_pending_exception()) { aoqi@0: JavaValue result(T_VOID); aoqi@0: args->set_receiver(h_exception); aoqi@0: // Call constructor aoqi@0: JavaCalls::call_special(&result, klass, aoqi@0: vmSymbols::object_initializer_name(), aoqi@0: signature, aoqi@0: args, aoqi@0: thread); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Check if another exception was thrown in the process, if so rethrow that one aoqi@0: if (thread->has_pending_exception()) { aoqi@0: h_exception = Handle(thread, thread->pending_exception()); aoqi@0: thread->clear_pending_exception(); aoqi@0: } aoqi@0: return h_exception; aoqi@0: } aoqi@0: aoqi@0: // Creates an exception oop, calls the method with the given signature. aoqi@0: // and returns a Handle aoqi@0: // Initializes the cause if cause non-null aoqi@0: Handle Exceptions::new_exception(Thread *thread, Symbol* name, aoqi@0: Symbol* signature, JavaCallArguments *args, aoqi@0: Handle h_cause, aoqi@0: Handle h_loader, Handle h_protection_domain) { aoqi@0: Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain); aoqi@0: aoqi@0: // Future: object initializer should take a cause argument aoqi@0: if (h_cause.not_null()) { aoqi@0: assert(h_cause->is_a(SystemDictionary::Throwable_klass()), aoqi@0: "exception cause is not a subclass of java/lang/Throwable"); aoqi@0: JavaValue result1(T_OBJECT); aoqi@0: JavaCallArguments args1; aoqi@0: args1.set_receiver(h_exception); aoqi@0: args1.push_oop(h_cause); aoqi@0: JavaCalls::call_virtual(&result1, h_exception->klass(), aoqi@0: vmSymbols::initCause_name(), aoqi@0: vmSymbols::throwable_throwable_signature(), aoqi@0: &args1, aoqi@0: thread); aoqi@0: } aoqi@0: aoqi@0: // Check if another exception was thrown in the process, if so rethrow that one aoqi@0: if (thread->has_pending_exception()) { aoqi@0: h_exception = Handle(thread, thread->pending_exception()); aoqi@0: thread->clear_pending_exception(); aoqi@0: } aoqi@0: return h_exception; aoqi@0: } aoqi@0: aoqi@0: // Convenience method. Calls either the () or (Throwable) method when aoqi@0: // creating a new exception aoqi@0: Handle Exceptions::new_exception(Thread* thread, Symbol* name, aoqi@0: Handle h_cause, aoqi@0: Handle h_loader, Handle h_protection_domain, aoqi@0: ExceptionMsgToUtf8Mode to_utf8_safe) { aoqi@0: JavaCallArguments args; aoqi@0: Symbol* signature = NULL; aoqi@0: if (h_cause.is_null()) { aoqi@0: signature = vmSymbols::void_method_signature(); aoqi@0: } else { aoqi@0: signature = vmSymbols::throwable_void_signature(); aoqi@0: args.push_oop(h_cause); aoqi@0: } aoqi@0: return new_exception(thread, name, signature, &args, h_loader, h_protection_domain); aoqi@0: } aoqi@0: aoqi@0: // Convenience method. Calls either the () or (String) method when aoqi@0: // creating a new exception aoqi@0: Handle Exceptions::new_exception(Thread* thread, Symbol* name, aoqi@0: const char* message, Handle h_cause, aoqi@0: Handle h_loader, Handle h_protection_domain, aoqi@0: ExceptionMsgToUtf8Mode to_utf8_safe) { aoqi@0: JavaCallArguments args; aoqi@0: Symbol* signature = NULL; aoqi@0: if (message == NULL) { aoqi@0: signature = vmSymbols::void_method_signature(); aoqi@0: } else { aoqi@0: // We want to allocate storage, but we can't do that if there's aoqi@0: // a pending exception, so we preserve any pending exception aoqi@0: // around the allocation. aoqi@0: // If we get an exception from the allocation, prefer that to aoqi@0: // the exception we are trying to build, or the pending exception. aoqi@0: // This is sort of like what PRESERVE_EXCEPTION_MARK does, except aoqi@0: // for the preferencing and the early returns. aoqi@0: Handle incoming_exception(thread, NULL); aoqi@0: if (thread->has_pending_exception()) { aoqi@0: incoming_exception = Handle(thread, thread->pending_exception()); aoqi@0: thread->clear_pending_exception(); aoqi@0: } aoqi@0: Handle msg; aoqi@0: if (to_utf8_safe == safe_to_utf8) { aoqi@0: // Make a java UTF8 string. aoqi@0: msg = java_lang_String::create_from_str(message, thread); aoqi@0: } else { aoqi@0: // Make a java string keeping the encoding scheme of the original string. aoqi@0: msg = java_lang_String::create_from_platform_dependent_str(message, thread); aoqi@0: } aoqi@0: if (thread->has_pending_exception()) { aoqi@0: Handle exception(thread, thread->pending_exception()); aoqi@0: thread->clear_pending_exception(); aoqi@0: return exception; aoqi@0: } aoqi@0: if (incoming_exception.not_null()) { aoqi@0: return incoming_exception; aoqi@0: } aoqi@0: args.push_oop(msg); aoqi@0: signature = vmSymbols::string_void_signature(); aoqi@0: } aoqi@0: return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain); aoqi@0: } aoqi@0: aoqi@0: // Another convenience method that creates handles for null class loaders and aoqi@0: // protection domains and null causes. aoqi@0: // If the last parameter 'to_utf8_mode' is safe_to_utf8, aoqi@0: // it means we can safely ignore the encoding scheme of the message string and aoqi@0: // convert it directly to a java UTF8 string. Otherwise, we need to take the aoqi@0: // encoding scheme of the string into account. One thing we should do at some aoqi@0: // point is to push this flag down to class java_lang_String since other aoqi@0: // classes may need similar functionalities. aoqi@0: Handle Exceptions::new_exception(Thread* thread, Symbol* name, aoqi@0: const char* message, aoqi@0: ExceptionMsgToUtf8Mode to_utf8_safe) { aoqi@0: aoqi@0: Handle h_loader(thread, NULL); aoqi@0: Handle h_prot(thread, NULL); aoqi@0: Handle h_cause(thread, NULL); aoqi@0: return Exceptions::new_exception(thread, name, message, h_cause, h_loader, aoqi@0: h_prot, to_utf8_safe); aoqi@0: } aoqi@0: aoqi@0: // Implementation of ExceptionMark aoqi@0: aoqi@0: ExceptionMark::ExceptionMark(Thread*& thread) { aoqi@0: thread = Thread::current(); aoqi@0: _thread = thread; aoqi@0: if (_thread->has_pending_exception()) { aoqi@0: oop exception = _thread->pending_exception(); aoqi@0: _thread->clear_pending_exception(); // Needed to avoid infinite recursion aoqi@0: exception->print(); aoqi@0: fatal("ExceptionMark constructor expects no pending exceptions"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: ExceptionMark::~ExceptionMark() { aoqi@0: if (_thread->has_pending_exception()) { aoqi@0: Handle exception(_thread, _thread->pending_exception()); aoqi@0: _thread->clear_pending_exception(); // Needed to avoid infinite recursion aoqi@0: if (is_init_completed()) { aoqi@0: exception->print(); aoqi@0: fatal("ExceptionMark destructor expects no pending exceptions"); aoqi@0: } else { aoqi@0: vm_exit_during_initialization(exception); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // ---------------------------------------------------------------------------------------- aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // caller frees value_string if necessary aoqi@0: void Exceptions::debug_check_abort(const char *value_string, const char* message) { aoqi@0: if (AbortVMOnException != NULL && value_string != NULL && aoqi@0: strstr(value_string, AbortVMOnException)) { aoqi@0: if (AbortVMOnExceptionMessage == NULL || message == NULL || aoqi@0: strcmp(message, AbortVMOnExceptionMessage) == 0) { aoqi@0: fatal(err_msg("Saw %s, aborting", value_string)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void Exceptions::debug_check_abort(Handle exception, const char* message) { aoqi@0: if (AbortVMOnException != NULL) { aoqi@0: ResourceMark rm; aoqi@0: if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) { aoqi@0: oop msg = java_lang_Throwable::message(exception); aoqi@0: if (msg != NULL) { aoqi@0: message = java_lang_String::as_utf8_string(msg); aoqi@0: } aoqi@0: } aoqi@0: debug_check_abort(InstanceKlass::cast(exception()->klass())->external_name(), message); aoqi@0: } aoqi@0: } aoqi@0: #endif