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