1 /* 2 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "compiler/compileBroker.hpp" 29 #include "logging/log.hpp" 30 #include "logging/logStream.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/init.hpp" 35 #include "runtime/java.hpp" 36 #include "runtime/javaCalls.hpp" 37 #include "runtime/os.hpp" 38 #include "runtime/thread.inline.hpp" 39 #include "runtime/threadCritical.hpp" 40 #include "utilities/events.hpp" 41 #include "utilities/exceptions.hpp" 42 43 // Implementation of ThreadShadow 44 void check_ThreadShadow() { 45 const ByteSize offset1 = byte_offset_of(ThreadShadow, _pending_exception); 46 const ByteSize offset2 = Thread::pending_exception_offset(); 47 if (offset1 != offset2) fatal("ThreadShadow::_pending_exception is not positioned correctly"); 48 } 49 50 51 void ThreadShadow::set_pending_exception(oop exception, const char* file, int line) { 52 assert(exception != NULL && oopDesc::is_oop(exception), "invalid exception oop"); 53 _pending_exception = exception; 54 _exception_file = file; 55 _exception_line = line; 56 } 57 58 void ThreadShadow::clear_pending_exception() { 59 LogTarget(Debug, exceptions) lt; 60 if (_pending_exception != NULL && lt.is_enabled()) { 61 ResourceMark rm; 62 LogStream ls(lt); 63 ls.print("Thread::clear_pending_exception: cleared exception:"); 64 _pending_exception->print_on(&ls); 65 } 66 _pending_exception = NULL; 67 _exception_file = NULL; 68 _exception_line = 0; 69 } 70 // Implementation of Exceptions 71 72 bool Exceptions::special_exception(Thread* thread, const char* file, int line, Handle h_exception) { 73 // bootstrapping check 74 if (!Universe::is_fully_initialized()) { 75 vm_exit_during_initialization(h_exception); 76 ShouldNotReachHere(); 77 } 78 79 #ifdef ASSERT 80 // Check for trying to throw stack overflow before initialization is complete 81 // to prevent infinite recursion trying to initialize stack overflow without 82 // adequate stack space. 83 // This can happen with stress testing a large value of StackShadowPages 84 if (h_exception()->klass() == SystemDictionary::StackOverflowError_klass()) { 85 InstanceKlass* ik = InstanceKlass::cast(h_exception->klass()); 86 assert(ik->is_initialized(), 87 "need to increase java_thread_min_stack_allowed calculation"); 88 } 89 #endif // ASSERT 90 91 if (thread->is_VM_thread() 92 || !thread->can_call_java()) { 93 // We do not care what kind of exception we get for the vm-thread or a thread which 94 // is compiling. We just install a dummy exception object 95 thread->set_pending_exception(Universe::vm_exception(), file, line); 96 return true; 97 } 98 99 return false; 100 } 101 102 bool Exceptions::special_exception(Thread* thread, const char* file, int line, Symbol* h_name, const char* message) { 103 // bootstrapping check 104 if (!Universe::is_fully_initialized()) { 105 if (h_name == NULL) { 106 // atleast an informative message. 107 vm_exit_during_initialization("Exception", message); 108 } else { 109 vm_exit_during_initialization(h_name, message); 110 } 111 ShouldNotReachHere(); 112 } 113 114 if (thread->is_VM_thread() 115 || !thread->can_call_java()) { 116 // We do not care what kind of exception we get for the vm-thread or a thread which 117 // is compiling. We just install a dummy exception object 118 thread->set_pending_exception(Universe::vm_exception(), file, line); 119 return true; 120 } 121 return false; 122 } 123 124 // This method should only be called from generated code, 125 // therefore the exception oop should be in the oopmap. 126 void Exceptions::_throw_oop(Thread* thread, const char* file, int line, oop exception) { 127 assert(exception != NULL, "exception should not be NULL"); 128 Handle h_exception(thread, exception); 129 _throw(thread, file, line, h_exception); 130 } 131 132 void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) { 133 ResourceMark rm; 134 assert(h_exception() != NULL, "exception should not be NULL"); 135 136 // tracing (do this up front - so it works during boot strapping) 137 log_info(exceptions)("Exception <%s%s%s> (" INTPTR_FORMAT ") \n" 138 "thrown [%s, line %d]\nfor thread " INTPTR_FORMAT, 139 h_exception->print_value_string(), 140 message ? ": " : "", message ? message : "", 141 p2i(h_exception()), file, line, p2i(thread)); 142 // for AbortVMOnException flag 143 Exceptions::debug_check_abort(h_exception, message); 144 145 // Check for special boot-strapping/vm-thread handling 146 if (special_exception(thread, file, line, h_exception)) { 147 return; 148 } 149 150 if (h_exception->is_a(SystemDictionary::OutOfMemoryError_klass())) { 151 count_out_of_memory_exceptions(h_exception); 152 } 153 154 assert(h_exception->is_a(SystemDictionary::Throwable_klass()), "exception is not a subclass of java/lang/Throwable"); 155 156 // set the pending exception 157 thread->set_pending_exception(h_exception(), file, line); 158 159 // vm log 160 if (LogEvents){ 161 Events::log_exception(thread, "Exception <%s%s%s> (" INTPTR_FORMAT ") thrown at [%s, line %d]", 162 h_exception->print_value_string(), message ? ": " : "", message ? message : "", 163 p2i(h_exception()), file, line); 164 } 165 } 166 167 168 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message, 169 Handle h_loader, Handle h_protection_domain) { 170 // Check for special boot-strapping/vm-thread handling 171 if (special_exception(thread, file, line, name, message)) return; 172 // Create and throw exception 173 Handle h_cause(thread, NULL); 174 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); 175 _throw(thread, file, line, h_exception, message); 176 } 177 178 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause, 179 Handle h_loader, Handle h_protection_domain) { 180 // Check for special boot-strapping/vm-thread handling 181 if (special_exception(thread, file, line, name, message)) return; 182 // Create and throw exception and init cause 183 Handle h_exception = new_exception(thread, name, message, h_cause, h_loader, h_protection_domain); 184 _throw(thread, file, line, h_exception, message); 185 } 186 187 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause, 188 Handle h_loader, Handle h_protection_domain) { 189 // Check for special boot-strapping/vm-thread handling 190 if (special_exception(thread, file, line, h_cause)) return; 191 // Create and throw exception 192 Handle h_exception = new_exception(thread, name, h_cause, h_loader, h_protection_domain); 193 _throw(thread, file, line, h_exception, NULL); 194 } 195 196 void Exceptions::_throw_args(Thread* thread, const char* file, int line, Symbol* name, Symbol* signature, JavaCallArguments *args) { 197 // Check for special boot-strapping/vm-thread handling 198 if (special_exception(thread, file, line, name, NULL)) return; 199 // Create and throw exception 200 Handle h_loader(thread, NULL); 201 Handle h_prot(thread, NULL); 202 Handle exception = new_exception(thread, name, signature, args, h_loader, h_prot); 203 _throw(thread, file, line, exception); 204 } 205 206 207 // Methods for default parameters. 208 // NOTE: These must be here (and not in the header file) because of include circularities. 209 void Exceptions::_throw_msg_cause(Thread* thread, const char* file, int line, Symbol* name, const char* message, Handle h_cause) { 210 _throw_msg_cause(thread, file, line, name, message, h_cause, Handle(thread, NULL), Handle(thread, NULL)); 211 } 212 void Exceptions::_throw_msg(Thread* thread, const char* file, int line, Symbol* name, const char* message) { 213 _throw_msg(thread, file, line, name, message, Handle(thread, NULL), Handle(thread, NULL)); 214 } 215 void Exceptions::_throw_cause(Thread* thread, const char* file, int line, Symbol* name, Handle h_cause) { 216 _throw_cause(thread, file, line, name, h_cause, Handle(thread, NULL), Handle(thread, NULL)); 217 } 218 219 220 void Exceptions::throw_stack_overflow_exception(Thread* THREAD, const char* file, int line, const methodHandle& method) { 221 Handle exception; 222 if (!THREAD->has_pending_exception()) { 223 InstanceKlass* k = SystemDictionary::StackOverflowError_klass(); 224 oop e = k->allocate_instance(CHECK); 225 exception = Handle(THREAD, e); // fill_in_stack trace does gc 226 assert(k->is_initialized(), "need to increase java_thread_min_stack_allowed calculation"); 227 if (StackTraceInThrowable) { 228 java_lang_Throwable::fill_in_stack_trace(exception, method()); 229 } 230 // Increment counter for hs_err file reporting 231 Atomic::inc(&Exceptions::_stack_overflow_errors); 232 } else { 233 // if prior exception, throw that one instead 234 exception = Handle(THREAD, THREAD->pending_exception()); 235 } 236 _throw(THREAD, file, line, exception); 237 } 238 239 void Exceptions::fthrow(Thread* thread, const char* file, int line, Symbol* h_name, const char* format, ...) { 240 const int max_msg_size = 1024; 241 va_list ap; 242 va_start(ap, format); 243 char msg[max_msg_size]; 244 os::vsnprintf(msg, max_msg_size, format, ap); 245 va_end(ap); 246 _throw_msg(thread, file, line, h_name, msg); 247 } 248 249 250 // Creates an exception oop, calls the <init> method with the given signature. 251 // and returns a Handle 252 Handle Exceptions::new_exception(Thread *thread, Symbol* name, 253 Symbol* signature, JavaCallArguments *args, 254 Handle h_loader, Handle h_protection_domain) { 255 assert(Universe::is_fully_initialized(), 256 "cannot be called during initialization"); 257 assert(thread->is_Java_thread(), "can only be called by a Java thread"); 258 assert(!thread->has_pending_exception(), "already has exception"); 259 260 Handle h_exception; 261 262 // Resolve exception klass, and check for pending exception below. 263 Klass* klass = SystemDictionary::resolve_or_fail(name, h_loader, h_protection_domain, true, thread); 264 265 if (!thread->has_pending_exception()) { 266 assert(klass != NULL, "klass must exist"); 267 h_exception = JavaCalls::construct_new_instance(InstanceKlass::cast(klass), 268 signature, 269 args, 270 thread); 271 } 272 273 // Check if another exception was thrown in the process, if so rethrow that one 274 if (thread->has_pending_exception()) { 275 h_exception = Handle(thread, thread->pending_exception()); 276 thread->clear_pending_exception(); 277 } 278 return h_exception; 279 } 280 281 // Creates an exception oop, calls the <init> method with the given signature. 282 // and returns a Handle 283 // Initializes the cause if cause non-null 284 Handle Exceptions::new_exception(Thread *thread, Symbol* name, 285 Symbol* signature, JavaCallArguments *args, 286 Handle h_cause, 287 Handle h_loader, Handle h_protection_domain) { 288 Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain); 289 290 // Future: object initializer should take a cause argument 291 if (h_cause.not_null()) { 292 assert(h_cause->is_a(SystemDictionary::Throwable_klass()), 293 "exception cause is not a subclass of java/lang/Throwable"); 294 JavaValue result1(T_OBJECT); 295 JavaCallArguments args1; 296 args1.set_receiver(h_exception); 297 args1.push_oop(h_cause); 298 JavaCalls::call_virtual(&result1, h_exception->klass(), 299 vmSymbols::initCause_name(), 300 vmSymbols::throwable_throwable_signature(), 301 &args1, 302 thread); 303 } 304 305 // Check if another exception was thrown in the process, if so rethrow that one 306 if (thread->has_pending_exception()) { 307 h_exception = Handle(thread, thread->pending_exception()); 308 thread->clear_pending_exception(); 309 } 310 return h_exception; 311 } 312 313 // Convenience method. Calls either the <init>() or <init>(Throwable) method when 314 // creating a new exception 315 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 316 Handle h_cause, 317 Handle h_loader, Handle h_protection_domain, 318 ExceptionMsgToUtf8Mode to_utf8_safe) { 319 JavaCallArguments args; 320 Symbol* signature = NULL; 321 if (h_cause.is_null()) { 322 signature = vmSymbols::void_method_signature(); 323 } else { 324 signature = vmSymbols::throwable_void_signature(); 325 args.push_oop(h_cause); 326 } 327 return new_exception(thread, name, signature, &args, h_loader, h_protection_domain); 328 } 329 330 // Convenience method. Calls either the <init>() or <init>(String) method when 331 // creating a new exception 332 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 333 const char* message, Handle h_cause, 334 Handle h_loader, Handle h_protection_domain, 335 ExceptionMsgToUtf8Mode to_utf8_safe) { 336 JavaCallArguments args; 337 Symbol* signature = NULL; 338 if (message == NULL) { 339 signature = vmSymbols::void_method_signature(); 340 } else { 341 // We want to allocate storage, but we can't do that if there's 342 // a pending exception, so we preserve any pending exception 343 // around the allocation. 344 // If we get an exception from the allocation, prefer that to 345 // the exception we are trying to build, or the pending exception. 346 // This is sort of like what PRESERVE_EXCEPTION_MARK does, except 347 // for the preferencing and the early returns. 348 Handle incoming_exception(thread, NULL); 349 if (thread->has_pending_exception()) { 350 incoming_exception = Handle(thread, thread->pending_exception()); 351 thread->clear_pending_exception(); 352 } 353 Handle msg; 354 if (to_utf8_safe == safe_to_utf8) { 355 // Make a java UTF8 string. 356 msg = java_lang_String::create_from_str(message, thread); 357 } else { 358 // Make a java string keeping the encoding scheme of the original string. 359 msg = java_lang_String::create_from_platform_dependent_str(message, thread); 360 } 361 if (thread->has_pending_exception()) { 362 Handle exception(thread, thread->pending_exception()); 363 thread->clear_pending_exception(); 364 return exception; 365 } 366 if (incoming_exception.not_null()) { 367 return incoming_exception; 368 } 369 args.push_oop(msg); 370 signature = vmSymbols::string_void_signature(); 371 } 372 return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain); 373 } 374 375 // Another convenience method that creates handles for null class loaders and 376 // protection domains and null causes. 377 // If the last parameter 'to_utf8_mode' is safe_to_utf8, 378 // it means we can safely ignore the encoding scheme of the message string and 379 // convert it directly to a java UTF8 string. Otherwise, we need to take the 380 // encoding scheme of the string into account. One thing we should do at some 381 // point is to push this flag down to class java_lang_String since other 382 // classes may need similar functionalities. 383 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 384 const char* message, 385 ExceptionMsgToUtf8Mode to_utf8_safe) { 386 387 Handle h_loader(thread, NULL); 388 Handle h_prot(thread, NULL); 389 Handle h_cause(thread, NULL); 390 return Exceptions::new_exception(thread, name, message, h_cause, h_loader, 391 h_prot, to_utf8_safe); 392 } 393 394 // invokedynamic uses wrap_dynamic_exception for: 395 // - bootstrap method resolution 396 // - post call to MethodHandleNatives::linkCallSite 397 // dynamically computed constant uses wrap_dynamic_exception for: 398 // - bootstrap method resolution 399 // - post call to MethodHandleNatives::linkDynamicConstant 400 void Exceptions::wrap_dynamic_exception(Thread* THREAD) { 401 if (THREAD->has_pending_exception()) { 402 oop exception = THREAD->pending_exception(); 403 // See the "Linking Exceptions" section for the invokedynamic instruction 404 // in JVMS 6.5. 405 if (exception->is_a(SystemDictionary::Error_klass())) { 406 // Pass through an Error, including BootstrapMethodError, any other form 407 // of linkage error, or say ThreadDeath/OutOfMemoryError 408 if (TraceMethodHandles) { 409 tty->print_cr("[constant/invoke]dynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)exception)); 410 exception->print(); 411 } 412 return; 413 } 414 415 // Otherwise wrap the exception in a BootstrapMethodError 416 if (TraceMethodHandles) { 417 tty->print_cr("[constant/invoke]dynamic throws BSME for " INTPTR_FORMAT, p2i((void *)exception)); 418 exception->print(); 419 } 420 Handle nested_exception(THREAD, exception); 421 THREAD->clear_pending_exception(); 422 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 423 } 424 } 425 426 // Exception counting for hs_err file 427 volatile int Exceptions::_stack_overflow_errors = 0; 428 volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0; 429 volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0; 430 volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0; 431 432 void Exceptions::count_out_of_memory_exceptions(Handle exception) { 433 if (oopDesc::equals(exception(), Universe::out_of_memory_error_metaspace())) { 434 Atomic::inc(&_out_of_memory_error_metaspace_errors); 435 } else if (oopDesc::equals(exception(), Universe::out_of_memory_error_class_metaspace())) { 436 Atomic::inc(&_out_of_memory_error_class_metaspace_errors); 437 } else { 438 // everything else reported as java heap OOM 439 Atomic::inc(&_out_of_memory_error_java_heap_errors); 440 } 441 } 442 443 void print_oom_count(outputStream* st, const char *err, int count) { 444 if (count > 0) { 445 st->print_cr("OutOfMemoryError %s=%d", err, count); 446 } 447 } 448 449 bool Exceptions::has_exception_counts() { 450 return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors + 451 _out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0; 452 } 453 454 void Exceptions::print_exception_counts_on_error(outputStream* st) { 455 print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors); 456 print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors); 457 print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors); 458 if (_stack_overflow_errors > 0) { 459 st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors); 460 } 461 } 462 463 // Implementation of ExceptionMark 464 465 ExceptionMark::ExceptionMark(Thread*& thread) { 466 thread = Thread::current(); 467 _thread = thread; 468 if (_thread->has_pending_exception()) { 469 oop exception = _thread->pending_exception(); 470 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 471 exception->print(); 472 fatal("ExceptionMark constructor expects no pending exceptions"); 473 } 474 } 475 476 477 ExceptionMark::~ExceptionMark() { 478 if (_thread->has_pending_exception()) { 479 Handle exception(_thread, _thread->pending_exception()); 480 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 481 if (is_init_completed()) { 482 exception->print(); 483 fatal("ExceptionMark destructor expects no pending exceptions"); 484 } else { 485 vm_exit_during_initialization(exception); 486 } 487 } 488 } 489 490 // ---------------------------------------------------------------------------------------- 491 492 // caller frees value_string if necessary 493 void Exceptions::debug_check_abort(const char *value_string, const char* message) { 494 if (AbortVMOnException != NULL && value_string != NULL && 495 strstr(value_string, AbortVMOnException)) { 496 if (AbortVMOnExceptionMessage == NULL || (message != NULL && 497 strstr(message, AbortVMOnExceptionMessage))) { 498 fatal("Saw %s, aborting", value_string); 499 } 500 } 501 } 502 503 void Exceptions::debug_check_abort(Handle exception, const char* message) { 504 if (AbortVMOnException != NULL) { 505 debug_check_abort_helper(exception, message); 506 } 507 } 508 509 void Exceptions::debug_check_abort_helper(Handle exception, const char* message) { 510 ResourceMark rm; 511 if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) { 512 oop msg = java_lang_Throwable::message(exception()); 513 if (msg != NULL) { 514 message = java_lang_String::as_utf8_string(msg); 515 } 516 } 517 debug_check_abort(exception()->klass()->external_name(), message); 518 } 519 520 // for logging exceptions 521 void Exceptions::log_exception(Handle exception, stringStream tempst) { 522 ResourceMark rm; 523 Symbol* message = java_lang_Throwable::detail_message(exception()); 524 if (message != NULL) { 525 log_info(exceptions)("Exception <%s: %s>\n thrown in %s", 526 exception->print_value_string(), 527 message->as_C_string(), 528 tempst.as_string()); 529 } else { 530 log_info(exceptions)("Exception <%s>\n thrown in %s", 531 exception->print_value_string(), 532 tempst.as_string()); 533 } 534 }