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 // We are about to create an instance - so make sure that klass is initialized 268 InstanceKlass* ik = InstanceKlass::cast(klass); 269 ik->initialize(thread); 270 if (!thread->has_pending_exception()) { 271 // Allocate new exception 272 h_exception = ik->allocate_instance_handle(thread); 273 if (!thread->has_pending_exception()) { 274 JavaValue result(T_VOID); 275 args->set_receiver(h_exception); 276 // Call constructor 277 JavaCalls::call_special(&result, ik, 278 vmSymbols::object_initializer_name(), 279 signature, 280 args, 281 thread); 282 } 283 } 284 } 285 286 // Check if another exception was thrown in the process, if so rethrow that one 287 if (thread->has_pending_exception()) { 288 h_exception = Handle(thread, thread->pending_exception()); 289 thread->clear_pending_exception(); 290 } 291 return h_exception; 292 } 293 294 // Creates an exception oop, calls the <init> method with the given signature. 295 // and returns a Handle 296 // Initializes the cause if cause non-null 297 Handle Exceptions::new_exception(Thread *thread, Symbol* name, 298 Symbol* signature, JavaCallArguments *args, 299 Handle h_cause, 300 Handle h_loader, Handle h_protection_domain) { 301 Handle h_exception = new_exception(thread, name, signature, args, h_loader, h_protection_domain); 302 303 // Future: object initializer should take a cause argument 304 if (h_cause.not_null()) { 305 assert(h_cause->is_a(SystemDictionary::Throwable_klass()), 306 "exception cause is not a subclass of java/lang/Throwable"); 307 JavaValue result1(T_OBJECT); 308 JavaCallArguments args1; 309 args1.set_receiver(h_exception); 310 args1.push_oop(h_cause); 311 JavaCalls::call_virtual(&result1, h_exception->klass(), 312 vmSymbols::initCause_name(), 313 vmSymbols::throwable_throwable_signature(), 314 &args1, 315 thread); 316 } 317 318 // Check if another exception was thrown in the process, if so rethrow that one 319 if (thread->has_pending_exception()) { 320 h_exception = Handle(thread, thread->pending_exception()); 321 thread->clear_pending_exception(); 322 } 323 return h_exception; 324 } 325 326 // Convenience method. Calls either the <init>() or <init>(Throwable) method when 327 // creating a new exception 328 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 329 Handle h_cause, 330 Handle h_loader, Handle h_protection_domain, 331 ExceptionMsgToUtf8Mode to_utf8_safe) { 332 JavaCallArguments args; 333 Symbol* signature = NULL; 334 if (h_cause.is_null()) { 335 signature = vmSymbols::void_method_signature(); 336 } else { 337 signature = vmSymbols::throwable_void_signature(); 338 args.push_oop(h_cause); 339 } 340 return new_exception(thread, name, signature, &args, h_loader, h_protection_domain); 341 } 342 343 // Convenience method. Calls either the <init>() or <init>(String) method when 344 // creating a new exception 345 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 346 const char* message, Handle h_cause, 347 Handle h_loader, Handle h_protection_domain, 348 ExceptionMsgToUtf8Mode to_utf8_safe) { 349 JavaCallArguments args; 350 Symbol* signature = NULL; 351 if (message == NULL) { 352 signature = vmSymbols::void_method_signature(); 353 } else { 354 // We want to allocate storage, but we can't do that if there's 355 // a pending exception, so we preserve any pending exception 356 // around the allocation. 357 // If we get an exception from the allocation, prefer that to 358 // the exception we are trying to build, or the pending exception. 359 // This is sort of like what PRESERVE_EXCEPTION_MARK does, except 360 // for the preferencing and the early returns. 361 Handle incoming_exception(thread, NULL); 362 if (thread->has_pending_exception()) { 363 incoming_exception = Handle(thread, thread->pending_exception()); 364 thread->clear_pending_exception(); 365 } 366 Handle msg; 367 if (to_utf8_safe == safe_to_utf8) { 368 // Make a java UTF8 string. 369 msg = java_lang_String::create_from_str(message, thread); 370 } else { 371 // Make a java string keeping the encoding scheme of the original string. 372 msg = java_lang_String::create_from_platform_dependent_str(message, thread); 373 } 374 if (thread->has_pending_exception()) { 375 Handle exception(thread, thread->pending_exception()); 376 thread->clear_pending_exception(); 377 return exception; 378 } 379 if (incoming_exception.not_null()) { 380 return incoming_exception; 381 } 382 args.push_oop(msg); 383 signature = vmSymbols::string_void_signature(); 384 } 385 return new_exception(thread, name, signature, &args, h_cause, h_loader, h_protection_domain); 386 } 387 388 // Another convenience method that creates handles for null class loaders and 389 // protection domains and null causes. 390 // If the last parameter 'to_utf8_mode' is safe_to_utf8, 391 // it means we can safely ignore the encoding scheme of the message string and 392 // convert it directly to a java UTF8 string. Otherwise, we need to take the 393 // encoding scheme of the string into account. One thing we should do at some 394 // point is to push this flag down to class java_lang_String since other 395 // classes may need similar functionalities. 396 Handle Exceptions::new_exception(Thread* thread, Symbol* name, 397 const char* message, 398 ExceptionMsgToUtf8Mode to_utf8_safe) { 399 400 Handle h_loader(thread, NULL); 401 Handle h_prot(thread, NULL); 402 Handle h_cause(thread, NULL); 403 return Exceptions::new_exception(thread, name, message, h_cause, h_loader, 404 h_prot, to_utf8_safe); 405 } 406 407 // invokedynamic uses wrap_dynamic_exception for: 408 // - bootstrap method resolution 409 // - post call to MethodHandleNatives::linkCallSite 410 // dynamically computed constant uses wrap_dynamic_exception for: 411 // - bootstrap method resolution 412 // - post call to MethodHandleNatives::linkDynamicConstant 413 void Exceptions::wrap_dynamic_exception(Thread* THREAD) { 414 if (THREAD->has_pending_exception()) { 415 oop exception = THREAD->pending_exception(); 416 // See the "Linking Exceptions" section for the invokedynamic instruction 417 // in JVMS 6.5. 418 if (exception->is_a(SystemDictionary::Error_klass())) { 419 // Pass through an Error, including BootstrapMethodError, any other form 420 // of linkage error, or say ThreadDeath/OutOfMemoryError 421 if (TraceMethodHandles) { 422 tty->print_cr("[constant/invoke]dynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)exception)); 423 exception->print(); 424 } 425 return; 426 } 427 428 // Otherwise wrap the exception in a BootstrapMethodError 429 if (TraceMethodHandles) { 430 tty->print_cr("[constant/invoke]dynamic throws BSME for " INTPTR_FORMAT, p2i((void *)exception)); 431 exception->print(); 432 } 433 Handle nested_exception(THREAD, exception); 434 THREAD->clear_pending_exception(); 435 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 436 } 437 } 438 439 // Exception counting for hs_err file 440 volatile int Exceptions::_stack_overflow_errors = 0; 441 volatile int Exceptions::_out_of_memory_error_java_heap_errors = 0; 442 volatile int Exceptions::_out_of_memory_error_metaspace_errors = 0; 443 volatile int Exceptions::_out_of_memory_error_class_metaspace_errors = 0; 444 445 void Exceptions::count_out_of_memory_exceptions(Handle exception) { 446 if (exception() == Universe::out_of_memory_error_metaspace()) { 447 Atomic::inc(&_out_of_memory_error_metaspace_errors); 448 } else if (exception() == Universe::out_of_memory_error_class_metaspace()) { 449 Atomic::inc(&_out_of_memory_error_class_metaspace_errors); 450 } else { 451 // everything else reported as java heap OOM 452 Atomic::inc(&_out_of_memory_error_java_heap_errors); 453 } 454 } 455 456 void print_oom_count(outputStream* st, const char *err, int count) { 457 if (count > 0) { 458 st->print_cr("OutOfMemoryError %s=%d", err, count); 459 } 460 } 461 462 bool Exceptions::has_exception_counts() { 463 return (_stack_overflow_errors + _out_of_memory_error_java_heap_errors + 464 _out_of_memory_error_metaspace_errors + _out_of_memory_error_class_metaspace_errors) > 0; 465 } 466 467 void Exceptions::print_exception_counts_on_error(outputStream* st) { 468 print_oom_count(st, "java_heap_errors", _out_of_memory_error_java_heap_errors); 469 print_oom_count(st, "metaspace_errors", _out_of_memory_error_metaspace_errors); 470 print_oom_count(st, "class_metaspace_errors", _out_of_memory_error_class_metaspace_errors); 471 if (_stack_overflow_errors > 0) { 472 st->print_cr("StackOverflowErrors=%d", _stack_overflow_errors); 473 } 474 } 475 476 // Implementation of ExceptionMark 477 478 ExceptionMark::ExceptionMark(Thread*& thread) { 479 thread = Thread::current(); 480 _thread = thread; 481 if (_thread->has_pending_exception()) { 482 oop exception = _thread->pending_exception(); 483 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 484 exception->print(); 485 fatal("ExceptionMark constructor expects no pending exceptions"); 486 } 487 } 488 489 490 ExceptionMark::~ExceptionMark() { 491 if (_thread->has_pending_exception()) { 492 Handle exception(_thread, _thread->pending_exception()); 493 _thread->clear_pending_exception(); // Needed to avoid infinite recursion 494 if (is_init_completed()) { 495 exception->print(); 496 fatal("ExceptionMark destructor expects no pending exceptions"); 497 } else { 498 vm_exit_during_initialization(exception); 499 } 500 } 501 } 502 503 // ---------------------------------------------------------------------------------------- 504 505 // caller frees value_string if necessary 506 void Exceptions::debug_check_abort(const char *value_string, const char* message) { 507 if (AbortVMOnException != NULL && value_string != NULL && 508 strstr(value_string, AbortVMOnException)) { 509 if (AbortVMOnExceptionMessage == NULL || (message != NULL && 510 strstr(message, AbortVMOnExceptionMessage))) { 511 fatal("Saw %s, aborting", value_string); 512 } 513 } 514 } 515 516 void Exceptions::debug_check_abort(Handle exception, const char* message) { 517 if (AbortVMOnException != NULL) { 518 debug_check_abort_helper(exception, message); 519 } 520 } 521 522 void Exceptions::debug_check_abort_helper(Handle exception, const char* message) { 523 ResourceMark rm; 524 if (message == NULL && exception->is_a(SystemDictionary::Throwable_klass())) { 525 oop msg = java_lang_Throwable::message(exception()); 526 if (msg != NULL) { 527 message = java_lang_String::as_utf8_string(msg); 528 } 529 } 530 debug_check_abort(exception()->klass()->external_name(), message); 531 } 532 533 // for logging exceptions 534 void Exceptions::log_exception(Handle exception, stringStream tempst) { 535 ResourceMark rm; 536 Symbol* message = java_lang_Throwable::detail_message(exception()); 537 if (message != NULL) { 538 log_info(exceptions)("Exception <%s: %s>\n thrown in %s", 539 exception->print_value_string(), 540 message->as_C_string(), 541 tempst.as_string()); 542 } else { 543 log_info(exceptions)("Exception <%s>\n thrown in %s", 544 exception->print_value_string(), 545 tempst.as_string()); 546 } 547 }