/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "memory/allocation.hpp"
#include "oops/oopsHierarchy.hpp"
// This file provides the basic support for exception handling in the VM.
// Note: We do not use C++ exceptions to avoid compiler dependencies and
// unpredictable performance.
//
// Scheme: Exceptions are stored with the thread. There is never more
// than one pending exception per thread. All functions that can throw
// an exception carry a THREAD argument (usually the last argument and
// declared with the TRAPS macro). Throwing an exception means setting
// a pending exception in the thread. Upon return from a function that
// can throw an exception, we must check if an exception is pending.
// The CHECK macros do this in a convenient way. Carrying around the
// thread provides also convenient access to it (e.g. for Handle
// creation, w/o the need for recomputation).
// Forward declarations to be independent of the include structure.
// This allows us to have exceptions.hpp included in top.hpp.
class Thread;
class Handle;
class Symbol;
class JavaCallArguments;
// The ThreadShadow class is a helper class to access the _pending_exception
// field of the Thread class w/o having access to the Thread's interface (for
// include hierachy reasons).
friend class VMStructs;
protected:
friend void check_ThreadShadow(); // checks _pending_exception offset
// The following virtual exists only to force creation of a vtable.
// We need ThreadShadow to have a vtable, even in product builds,
// so that its layout will start at an offset of zero relative to Thread.
// Some C++ compilers are so "clever" that they put the ThreadShadow
// base class at offset 4 in Thread (after Thread's vtable), if they
// notice that Thread has a vtable but ThreadShadow does not.
virtual void unused_initial_virtual() { }
public:
// Code generation support
static ByteSize pending_exception_offset() { return byte_offset_of(ThreadShadow, _pending_exception); }
// use THROW whenever possible!
// use CLEAR_PENDING_EXCEPTION whenever possible!
void clear_pending_exception();
};
// Exceptions is a helper class that encapsulates all operations
// that require access to the thread interface and which are
// relatively rare. The Exceptions operations should only be
// used directly if the macros below are insufficient.
class Exceptions {
static bool special_exception(Thread* thread, const char* file, int line, Symbol* name, const char* message);
public:
// this enum is defined to indicate whether it is safe to
// ignore the encoding scheme of the original message string.
typedef enum {
safe_to_utf8 = 0,
// Throw exceptions: w/o message, w/ message & with formatted message.
static void _throw(Thread* thread, const char* file, int line, Handle exception, const char* msg = NULL);
static void _throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message);
static void _throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message,
static void _throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause);
static void _throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause,
// There is no THROW... macro for this method. Caller should remember
// to do a return after calling it.
const char* format, ...);
// Create and initialize a new exception
const char* message,
static void throw_stack_overflow_exception(Thread* thread, const char* file, int line, methodHandle method);
// for AbortVMOnException flag
};
// The THREAD & TRAPS macros facilitate the declaration of functions that throw exceptions.
// Convention: Use the TRAPS macro as the last argument of such a function; e.g.:
//
// int this_function_may_trap(int x, float y, TRAPS)
// The CHECK... macros should be used to pass along a THREAD reference and to check for pending
// exceptions. In special situations it is necessary to handle pending exceptions explicitly,
// in these cases the PENDING_EXCEPTION helper macros should be used.
//
// Macro naming conventions: Macros that end with _ require a result value to be returned. They
// are for functions with non-void result type. The result value is usually ignored because of
// the exception and is only needed for syntactic correctness. The _0 ending is a shortcut for
// _(0) since this is a frequent case. Example:
//
// int result = this_function_may_trap(x_arg, y_arg, CHECK_0);
//
// CAUTION: make sure that the function call using a CHECK macro is not the only statement of a
// conditional branch w/o enclosing {} braces, since the CHECK macros expand into several state-
// ments!
#define CHECK_AND_CLEAR_(result) THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; return result; } (0
// The THROW... macros should be used to throw an exception. They require a THREAD variable to be
// visible within the scope containing the THROW. Usually this is achieved by declaring the function
// with a TRAPS argument.
#define THROW_OOP(e) \
#define THROW_HANDLE(e) \
{ Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message, loader, protection_domain); return result; }
// The CATCH macro checks that no exception has been thrown by a function; it is used at
// call sites about which is statically known that the callee cannot throw an exception
// even though it is declared with TRAPS.
#define CATCH \
THREAD); if (HAS_PENDING_EXCEPTION) { \
ShouldNotReachHere(); \
} (0
// ExceptionMark is a stack-allocated helper class for local exception handling.
// It is used with the EXCEPTION_MARK macro.
class ExceptionMark {
private:
public:
~ExceptionMark();
};
// Use an EXCEPTION_MARK for 'local' exceptions. EXCEPTION_MARK makes sure that no
// pending exception exists upon entering its scope and tests that no pending exception
// exists when leaving the scope.
// See also preserveException.hpp for PRESERVE_EXCEPTION_MARK macro,
// which preserves pre-existing exceptions and does not allow new
// exceptions.
#endif // SHARE_VM_UTILITIES_EXCEPTIONS_HPP