1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/exceptions.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,311 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_UTILITIES_EXCEPTIONS_HPP 1.29 +#define SHARE_VM_UTILITIES_EXCEPTIONS_HPP 1.30 + 1.31 +#include "memory/allocation.hpp" 1.32 +#include "oops/oopsHierarchy.hpp" 1.33 +#include "utilities/sizes.hpp" 1.34 + 1.35 +// This file provides the basic support for exception handling in the VM. 1.36 +// Note: We do not use C++ exceptions to avoid compiler dependencies and 1.37 +// unpredictable performance. 1.38 +// 1.39 +// Scheme: Exceptions are stored with the thread. There is never more 1.40 +// than one pending exception per thread. All functions that can throw 1.41 +// an exception carry a THREAD argument (usually the last argument and 1.42 +// declared with the TRAPS macro). Throwing an exception means setting 1.43 +// a pending exception in the thread. Upon return from a function that 1.44 +// can throw an exception, we must check if an exception is pending. 1.45 +// The CHECK macros do this in a convenient way. Carrying around the 1.46 +// thread provides also convenient access to it (e.g. for Handle 1.47 +// creation, w/o the need for recomputation). 1.48 + 1.49 + 1.50 + 1.51 +// Forward declarations to be independent of the include structure. 1.52 +// This allows us to have exceptions.hpp included in top.hpp. 1.53 + 1.54 +class Thread; 1.55 +class Handle; 1.56 +class Symbol; 1.57 +class JavaCallArguments; 1.58 + 1.59 +// The ThreadShadow class is a helper class to access the _pending_exception 1.60 +// field of the Thread class w/o having access to the Thread's interface (for 1.61 +// include hierachy reasons). 1.62 + 1.63 +class ThreadShadow: public CHeapObj<mtThread> { 1.64 + friend class VMStructs; 1.65 + 1.66 + protected: 1.67 + oop _pending_exception; // Thread has gc actions. 1.68 + const char* _exception_file; // file information for exception (debugging only) 1.69 + int _exception_line; // line information for exception (debugging only) 1.70 + friend void check_ThreadShadow(); // checks _pending_exception offset 1.71 + 1.72 + // The following virtual exists only to force creation of a vtable. 1.73 + // We need ThreadShadow to have a vtable, even in product builds, 1.74 + // so that its layout will start at an offset of zero relative to Thread. 1.75 + // Some C++ compilers are so "clever" that they put the ThreadShadow 1.76 + // base class at offset 4 in Thread (after Thread's vtable), if they 1.77 + // notice that Thread has a vtable but ThreadShadow does not. 1.78 + virtual void unused_initial_virtual() { } 1.79 + 1.80 + public: 1.81 + oop pending_exception() const { return _pending_exception; } 1.82 + bool has_pending_exception() const { return _pending_exception != NULL; } 1.83 + const char* exception_file() const { return _exception_file; } 1.84 + int exception_line() const { return _exception_line; } 1.85 + 1.86 + // Code generation support 1.87 + static ByteSize pending_exception_offset() { return byte_offset_of(ThreadShadow, _pending_exception); } 1.88 + 1.89 + // use THROW whenever possible! 1.90 + void set_pending_exception(oop exception, const char* file, int line); 1.91 + 1.92 + // use CLEAR_PENDING_EXCEPTION whenever possible! 1.93 + void clear_pending_exception(); 1.94 + 1.95 + ThreadShadow() : _pending_exception(NULL), 1.96 + _exception_file(NULL), _exception_line(0) {} 1.97 +}; 1.98 + 1.99 + 1.100 +// Exceptions is a helper class that encapsulates all operations 1.101 +// that require access to the thread interface and which are 1.102 +// relatively rare. The Exceptions operations should only be 1.103 +// used directly if the macros below are insufficient. 1.104 + 1.105 +class Exceptions { 1.106 + static bool special_exception(Thread *thread, const char* file, int line, Handle exception); 1.107 + static bool special_exception(Thread* thread, const char* file, int line, Symbol* name, const char* message); 1.108 + public: 1.109 + // this enum is defined to indicate whether it is safe to 1.110 + // ignore the encoding scheme of the original message string. 1.111 + typedef enum { 1.112 + safe_to_utf8 = 0, 1.113 + unsafe_to_utf8 = 1 1.114 + } ExceptionMsgToUtf8Mode; 1.115 + // Throw exceptions: w/o message, w/ message & with formatted message. 1.116 + static void _throw_oop(Thread* thread, const char* file, int line, oop exception); 1.117 + static void _throw(Thread* thread, const char* file, int line, Handle exception, const char* msg = NULL); 1.118 + 1.119 + static void _throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message); 1.120 + static void _throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message, 1.121 + Handle loader, Handle protection_domain); 1.122 + 1.123 + static void _throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause); 1.124 + static void _throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause, 1.125 + Handle h_loader, Handle h_protection_domain); 1.126 + 1.127 + static void _throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause); 1.128 + static void _throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause, 1.129 + Handle h_loader, Handle h_protection_domain); 1.130 + 1.131 + static void _throw_args(Thread* thread, const char* file, int line, 1.132 + Symbol* name, Symbol* signature, 1.133 + JavaCallArguments* args); 1.134 + 1.135 + // There is no THROW... macro for this method. Caller should remember 1.136 + // to do a return after calling it. 1.137 + static void fthrow(Thread* thread, const char* file, int line, Symbol* name, 1.138 + const char* format, ...) ATTRIBUTE_PRINTF(5, 6); 1.139 + 1.140 + // Create and initialize a new exception 1.141 + static Handle new_exception(Thread* thread, Symbol* name, 1.142 + Symbol* signature, JavaCallArguments* args, 1.143 + Handle loader, Handle protection_domain); 1.144 + 1.145 + static Handle new_exception(Thread* thread, Symbol* name, 1.146 + Symbol* signature, JavaCallArguments* args, 1.147 + Handle cause, 1.148 + Handle loader, Handle protection_domain); 1.149 + 1.150 + static Handle new_exception(Thread* thread, Symbol* name, 1.151 + Handle cause, 1.152 + Handle loader, Handle protection_domain, 1.153 + ExceptionMsgToUtf8Mode to_utf8_safe = safe_to_utf8); 1.154 + 1.155 + static Handle new_exception(Thread* thread, Symbol* name, 1.156 + const char* message, Handle cause, 1.157 + Handle loader, Handle protection_domain, 1.158 + ExceptionMsgToUtf8Mode to_utf8_safe = safe_to_utf8); 1.159 + 1.160 + static Handle new_exception(Thread* thread, Symbol* name, 1.161 + const char* message, 1.162 + ExceptionMsgToUtf8Mode to_utf8_safe = safe_to_utf8); 1.163 + 1.164 + static void throw_stack_overflow_exception(Thread* thread, const char* file, int line, methodHandle method); 1.165 + 1.166 + // for AbortVMOnException flag 1.167 + NOT_PRODUCT(static void debug_check_abort(Handle exception, const char* message = NULL);) 1.168 + NOT_PRODUCT(static void debug_check_abort(const char *value_string, const char* message = NULL);) 1.169 +}; 1.170 + 1.171 + 1.172 +// The THREAD & TRAPS macros facilitate the declaration of functions that throw exceptions. 1.173 +// Convention: Use the TRAPS macro as the last argument of such a function; e.g.: 1.174 +// 1.175 +// int this_function_may_trap(int x, float y, TRAPS) 1.176 + 1.177 +#define THREAD __the_thread__ 1.178 +#define TRAPS Thread* THREAD 1.179 + 1.180 + 1.181 +// The CHECK... macros should be used to pass along a THREAD reference and to check for pending 1.182 +// exceptions. In special situations it is necessary to handle pending exceptions explicitly, 1.183 +// in these cases the PENDING_EXCEPTION helper macros should be used. 1.184 +// 1.185 +// Macro naming conventions: Macros that end with _ require a result value to be returned. They 1.186 +// are for functions with non-void result type. The result value is usually ignored because of 1.187 +// the exception and is only needed for syntactic correctness. The _0 ending is a shortcut for 1.188 +// _(0) since this is a frequent case. Example: 1.189 +// 1.190 +// int result = this_function_may_trap(x_arg, y_arg, CHECK_0); 1.191 +// 1.192 +// CAUTION: make sure that the function call using a CHECK macro is not the only statement of a 1.193 +// conditional branch w/o enclosing {} braces, since the CHECK macros expand into several state- 1.194 +// ments! 1.195 + 1.196 +#define PENDING_EXCEPTION (((ThreadShadow*)THREAD)->pending_exception()) 1.197 +#define HAS_PENDING_EXCEPTION (((ThreadShadow*)THREAD)->has_pending_exception()) 1.198 +#define CLEAR_PENDING_EXCEPTION (((ThreadShadow*)THREAD)->clear_pending_exception()) 1.199 + 1.200 +#define CHECK THREAD); if (HAS_PENDING_EXCEPTION) return ; (void)(0 1.201 +#define CHECK_(result) THREAD); if (HAS_PENDING_EXCEPTION) return result; (void)(0 1.202 +#define CHECK_0 CHECK_(0) 1.203 +#define CHECK_NH CHECK_(Handle()) 1.204 +#define CHECK_NULL CHECK_(NULL) 1.205 +#define CHECK_false CHECK_(false) 1.206 + 1.207 +#define CHECK_AND_CLEAR THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; return; } (void)(0 1.208 +#define CHECK_AND_CLEAR_(result) THREAD); if (HAS_PENDING_EXCEPTION) { CLEAR_PENDING_EXCEPTION; return result; } (void)(0 1.209 +#define CHECK_AND_CLEAR_0 CHECK_AND_CLEAR_(0) 1.210 +#define CHECK_AND_CLEAR_NH CHECK_AND_CLEAR_(Handle()) 1.211 +#define CHECK_AND_CLEAR_NULL CHECK_AND_CLEAR_(NULL) 1.212 +#define CHECK_AND_CLEAR_false CHECK_AND_CLEAR_(false) 1.213 + 1.214 +// The THROW... macros should be used to throw an exception. They require a THREAD variable to be 1.215 +// visible within the scope containing the THROW. Usually this is achieved by declaring the function 1.216 +// with a TRAPS argument. 1.217 + 1.218 +#define THREAD_AND_LOCATION THREAD, __FILE__, __LINE__ 1.219 + 1.220 +#define THROW_OOP(e) \ 1.221 + { Exceptions::_throw_oop(THREAD_AND_LOCATION, e); return; } 1.222 + 1.223 +#define THROW_HANDLE(e) \ 1.224 + { Exceptions::_throw(THREAD_AND_LOCATION, e); return; } 1.225 + 1.226 +#define THROW(name) \ 1.227 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, NULL); return; } 1.228 + 1.229 +#define THROW_MSG(name, message) \ 1.230 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message); return; } 1.231 + 1.232 +#define THROW_CAUSE(name, cause) \ 1.233 + { Exceptions::_throw_cause(THREAD_AND_LOCATION, name, cause); return; } 1.234 + 1.235 +#define THROW_MSG_LOADER(name, message, loader, protection_domain) \ 1.236 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message, loader, protection_domain); return; } 1.237 + 1.238 +#define THROW_ARG(name, signature, args) \ 1.239 + { Exceptions::_throw_args(THREAD_AND_LOCATION, name, signature, args); return; } 1.240 + 1.241 +#define THROW_OOP_(e, result) \ 1.242 + { Exceptions::_throw_oop(THREAD_AND_LOCATION, e); return result; } 1.243 + 1.244 +#define THROW_HANDLE_(e, result) \ 1.245 + { Exceptions::_throw(THREAD_AND_LOCATION, e); return result; } 1.246 + 1.247 +#define THROW_(name, result) \ 1.248 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, NULL); return result; } 1.249 + 1.250 +#define THROW_MSG_(name, message, result) \ 1.251 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message); return result; } 1.252 + 1.253 +#define THROW_MSG_LOADER_(name, message, loader, protection_domain, result) \ 1.254 + { Exceptions::_throw_msg(THREAD_AND_LOCATION, name, message, loader, protection_domain); return result; } 1.255 + 1.256 +#define THROW_ARG_(name, signature, args, result) \ 1.257 + { Exceptions::_throw_args(THREAD_AND_LOCATION, name, signature, args); return result; } 1.258 + 1.259 +#define THROW_MSG_CAUSE(name, message, cause) \ 1.260 + { Exceptions::_throw_msg_cause(THREAD_AND_LOCATION, name, message, cause); return; } 1.261 + 1.262 +#define THROW_MSG_CAUSE_(name, message, cause, result) \ 1.263 + { Exceptions::_throw_msg_cause(THREAD_AND_LOCATION, name, message, cause); return result; } 1.264 + 1.265 + 1.266 +#define THROW_OOP_0(e) THROW_OOP_(e, 0) 1.267 +#define THROW_HANDLE_0(e) THROW_HANDLE_(e, 0) 1.268 +#define THROW_0(name) THROW_(name, 0) 1.269 +#define THROW_MSG_0(name, message) THROW_MSG_(name, message, 0) 1.270 +#define THROW_WRAPPED_0(name, oop_to_wrap) THROW_WRAPPED_(name, oop_to_wrap, 0) 1.271 +#define THROW_ARG_0(name, signature, arg) THROW_ARG_(name, signature, arg, 0) 1.272 +#define THROW_MSG_CAUSE_0(name, message, cause) THROW_MSG_CAUSE_(name, message, cause, 0) 1.273 +#define THROW_MSG_CAUSE_NULL(name, message, cause) THROW_MSG_CAUSE_(name, message, cause, NULL) 1.274 + 1.275 +#define THROW_NULL(name) THROW_(name, NULL) 1.276 +#define THROW_MSG_NULL(name, message) THROW_MSG_(name, message, NULL) 1.277 + 1.278 +// The CATCH macro checks that no exception has been thrown by a function; it is used at 1.279 +// call sites about which is statically known that the callee cannot throw an exception 1.280 +// even though it is declared with TRAPS. 1.281 + 1.282 +#define CATCH \ 1.283 + THREAD); if (HAS_PENDING_EXCEPTION) { \ 1.284 + oop ex = PENDING_EXCEPTION; \ 1.285 + CLEAR_PENDING_EXCEPTION; \ 1.286 + ex->print(); \ 1.287 + ShouldNotReachHere(); \ 1.288 + } (void)(0 1.289 + 1.290 +// ExceptionMark is a stack-allocated helper class for local exception handling. 1.291 +// It is used with the EXCEPTION_MARK macro. 1.292 + 1.293 +class ExceptionMark { 1.294 + private: 1.295 + Thread* _thread; 1.296 + 1.297 + public: 1.298 + ExceptionMark(Thread*& thread); 1.299 + ~ExceptionMark(); 1.300 +}; 1.301 + 1.302 + 1.303 + 1.304 +// Use an EXCEPTION_MARK for 'local' exceptions. EXCEPTION_MARK makes sure that no 1.305 +// pending exception exists upon entering its scope and tests that no pending exception 1.306 +// exists when leaving the scope. 1.307 + 1.308 +// See also preserveException.hpp for PRESERVE_EXCEPTION_MARK macro, 1.309 +// which preserves pre-existing exceptions and does not allow new 1.310 +// exceptions. 1.311 + 1.312 +#define EXCEPTION_MARK Thread* THREAD = NULL; ExceptionMark __em(THREAD); 1.313 + 1.314 +#endif // SHARE_VM_UTILITIES_EXCEPTIONS_HPP