1 /* 2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2014, Red Hat Inc. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 // no precompiled headers 27 #include "jvm.h" 28 #include "asm/macroAssembler.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/vmSymbols.hpp" 32 #include "code/codeCache.hpp" 33 #include "code/icBuffer.hpp" 34 #include "code/vtableStubs.hpp" 35 #include "code/nativeInst.hpp" 36 #include "interpreter/interpreter.hpp" 37 #include "memory/allocation.inline.hpp" 38 #include "os_share_linux.hpp" 39 #include "prims/jniFastGetField.hpp" 40 #include "prims/jvm_misc.hpp" 41 #include "runtime/arguments.hpp" 42 #include "runtime/extendedPC.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/interfaceSupport.inline.hpp" 45 #include "runtime/java.hpp" 46 #include "runtime/javaCalls.hpp" 47 #include "runtime/mutexLocker.hpp" 48 #include "runtime/osThread.hpp" 49 #include "runtime/safepointMechanism.hpp" 50 #include "runtime/sharedRuntime.hpp" 51 #include "runtime/stubRoutines.hpp" 52 #include "runtime/thread.inline.hpp" 53 #include "runtime/timer.hpp" 54 #include "utilities/debug.hpp" 55 #include "utilities/events.hpp" 56 #include "utilities/vmError.hpp" 57 58 // put OS-includes here 59 # include <sys/types.h> 60 # include <sys/mman.h> 61 # include <pthread.h> 62 # include <signal.h> 63 # include <errno.h> 64 # include <dlfcn.h> 65 # include <stdlib.h> 66 # include <stdio.h> 67 # include <unistd.h> 68 # include <sys/resource.h> 69 # include <pthread.h> 70 # include <sys/stat.h> 71 # include <sys/time.h> 72 # include <sys/utsname.h> 73 # include <sys/socket.h> 74 # include <sys/wait.h> 75 # include <pwd.h> 76 # include <poll.h> 77 # include <ucontext.h> 78 # include <fpu_control.h> 79 80 #define REG_FP 29 81 #define REG_LR 30 82 83 NOINLINE address os::current_stack_pointer() { 84 return (address)__builtin_frame_address(0); 85 } 86 87 char* os::non_memory_address_word() { 88 // Must never look like an address returned by reserve_memory, 89 // even in its subfields (as defined by the CPU immediate fields, 90 // if the CPU splits constants across multiple instructions). 91 92 return (char*) 0xffffffffffff; 93 } 94 95 address os::Linux::ucontext_get_pc(const ucontext_t * uc) { 96 return (address)uc->uc_mcontext.pc; 97 } 98 99 void os::Linux::ucontext_set_pc(ucontext_t * uc, address pc) { 100 uc->uc_mcontext.pc = (intptr_t)pc; 101 } 102 103 intptr_t* os::Linux::ucontext_get_sp(const ucontext_t * uc) { 104 return (intptr_t*)uc->uc_mcontext.sp; 105 } 106 107 intptr_t* os::Linux::ucontext_get_fp(const ucontext_t * uc) { 108 return (intptr_t*)uc->uc_mcontext.regs[REG_FP]; 109 } 110 111 // For Forte Analyzer AsyncGetCallTrace profiling support - thread 112 // is currently interrupted by SIGPROF. 113 // os::Solaris::fetch_frame_from_ucontext() tries to skip nested signal 114 // frames. Currently we don't do that on Linux, so it's the same as 115 // os::fetch_frame_from_context(). 116 ExtendedPC os::Linux::fetch_frame_from_ucontext(Thread* thread, 117 const ucontext_t* uc, intptr_t** ret_sp, intptr_t** ret_fp) { 118 119 assert(thread != NULL, "just checking"); 120 assert(ret_sp != NULL, "just checking"); 121 assert(ret_fp != NULL, "just checking"); 122 123 return os::fetch_frame_from_context(uc, ret_sp, ret_fp); 124 } 125 126 ExtendedPC os::fetch_frame_from_context(const void* ucVoid, 127 intptr_t** ret_sp, intptr_t** ret_fp) { 128 129 ExtendedPC epc; 130 const ucontext_t* uc = (const ucontext_t*)ucVoid; 131 132 if (uc != NULL) { 133 epc = ExtendedPC(os::Linux::ucontext_get_pc(uc)); 134 if (ret_sp) *ret_sp = os::Linux::ucontext_get_sp(uc); 135 if (ret_fp) *ret_fp = os::Linux::ucontext_get_fp(uc); 136 } else { 137 // construct empty ExtendedPC for return value checking 138 epc = ExtendedPC(NULL); 139 if (ret_sp) *ret_sp = (intptr_t *)NULL; 140 if (ret_fp) *ret_fp = (intptr_t *)NULL; 141 } 142 143 return epc; 144 } 145 146 frame os::fetch_frame_from_context(const void* ucVoid) { 147 intptr_t* sp; 148 intptr_t* fp; 149 ExtendedPC epc = fetch_frame_from_context(ucVoid, &sp, &fp); 150 return frame(sp, fp, epc.pc()); 151 } 152 153 bool os::Linux::get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr) { 154 address pc = (address) os::Linux::ucontext_get_pc(uc); 155 if (Interpreter::contains(pc)) { 156 // interpreter performs stack banging after the fixed frame header has 157 // been generated while the compilers perform it before. To maintain 158 // semantic consistency between interpreted and compiled frames, the 159 // method returns the Java sender of the current frame. 160 *fr = os::fetch_frame_from_context(uc); 161 if (!fr->is_first_java_frame()) { 162 assert(fr->safe_for_sender(thread), "Safety check"); 163 *fr = fr->java_sender(); 164 } 165 } else { 166 // more complex code with compiled code 167 assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above"); 168 CodeBlob* cb = CodeCache::find_blob(pc); 169 if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) { 170 // Not sure where the pc points to, fallback to default 171 // stack overflow handling 172 return false; 173 } else { 174 // In compiled code, the stack banging is performed before LR 175 // has been saved in the frame. LR is live, and SP and FP 176 // belong to the caller. 177 intptr_t* fp = os::Linux::ucontext_get_fp(uc); 178 intptr_t* sp = os::Linux::ucontext_get_sp(uc); 179 address pc = (address)(uc->uc_mcontext.regs[REG_LR] 180 - NativeInstruction::instruction_size); 181 *fr = frame(sp, fp, pc); 182 if (!fr->is_java_frame()) { 183 assert(fr->safe_for_sender(thread), "Safety check"); 184 assert(!fr->is_first_frame(), "Safety check"); 185 *fr = fr->java_sender(); 186 } 187 } 188 } 189 assert(fr->is_java_frame(), "Safety check"); 190 return true; 191 } 192 193 // By default, gcc always saves frame pointer rfp on this stack. This 194 // may get turned off by -fomit-frame-pointer. 195 frame os::get_sender_for_C_frame(frame* fr) { 196 return frame(fr->link(), fr->link(), fr->sender_pc()); 197 } 198 199 NOINLINE frame os::current_frame() { 200 intptr_t *fp = *(intptr_t **)__builtin_frame_address(0); 201 frame myframe((intptr_t*)os::current_stack_pointer(), 202 (intptr_t*)fp, 203 CAST_FROM_FN_PTR(address, os::current_frame)); 204 if (os::is_first_C_frame(&myframe)) { 205 // stack is not walkable 206 return frame(); 207 } else { 208 return os::get_sender_for_C_frame(&myframe); 209 } 210 } 211 212 extern "C" JNIEXPORT int 213 JVM_handle_linux_signal(int sig, 214 siginfo_t* info, 215 void* ucVoid, 216 int abort_if_unrecognized) { 217 ucontext_t* uc = (ucontext_t*) ucVoid; 218 219 Thread* t = Thread::current_or_null_safe(); 220 221 // Must do this before SignalHandlerMark, if crash protection installed we will longjmp away 222 // (no destructors can be run) 223 os::ThreadCrashProtection::check_crash_protection(sig, t); 224 225 SignalHandlerMark shm(t); 226 227 // Note: it's not uncommon that JNI code uses signal/sigset to install 228 // then restore certain signal handler (e.g. to temporarily block SIGPIPE, 229 // or have a SIGILL handler when detecting CPU type). When that happens, 230 // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To 231 // avoid unnecessary crash when libjsig is not preloaded, try handle signals 232 // that do not require siginfo/ucontext first. 233 234 if (sig == SIGPIPE || sig == SIGXFSZ) { 235 // allow chained handler to go first 236 if (os::Linux::chained_handler(sig, info, ucVoid)) { 237 return true; 238 } else { 239 // Ignoring SIGPIPE/SIGXFSZ - see bugs 4229104 or 6499219 240 return true; 241 } 242 } 243 244 #ifdef CAN_SHOW_REGISTERS_ON_ASSERT 245 if ((sig == SIGSEGV || sig == SIGBUS) && info != NULL && info->si_addr == g_assert_poison) { 246 if (handle_assert_poison_fault(ucVoid, info->si_addr)) { 247 return 1; 248 } 249 } 250 #endif 251 252 JavaThread* thread = NULL; 253 VMThread* vmthread = NULL; 254 if (os::Linux::signal_handlers_are_installed) { 255 if (t != NULL ){ 256 if(t->is_Java_thread()) { 257 thread = (JavaThread*)t; 258 } 259 else if(t->is_VM_thread()){ 260 vmthread = (VMThread *)t; 261 } 262 } 263 } 264 /* 265 NOTE: does not seem to work on linux. 266 if (info == NULL || info->si_code <= 0 || info->si_code == SI_NOINFO) { 267 // can't decode this kind of signal 268 info = NULL; 269 } else { 270 assert(sig == info->si_signo, "bad siginfo"); 271 } 272 */ 273 // decide if this trap can be handled by a stub 274 address stub = NULL; 275 276 address pc = NULL; 277 278 //%note os_trap_1 279 if (info != NULL && uc != NULL && thread != NULL) { 280 pc = (address) os::Linux::ucontext_get_pc(uc); 281 282 if (StubRoutines::is_safefetch_fault(pc)) { 283 os::Linux::ucontext_set_pc(uc, StubRoutines::continuation_for_safefetch_fault(pc)); 284 return 1; 285 } 286 287 address addr = (address) info->si_addr; 288 289 // Make sure the high order byte is sign extended, as it may be masked away by the hardware. 290 if ((uintptr_t(addr) & (uintptr_t(1) << 55)) != 0) { 291 addr = address(uintptr_t(addr) | (uintptr_t(0xFF) << 56)); 292 } 293 294 // Handle ALL stack overflow variations here 295 if (sig == SIGSEGV) { 296 // check if fault address is within thread stack 297 if (thread->is_in_full_stack(addr)) { 298 // stack overflow 299 if (thread->in_stack_yellow_reserved_zone(addr)) { 300 if (thread->thread_state() == _thread_in_Java) { 301 if (thread->in_stack_reserved_zone(addr)) { 302 frame fr; 303 if (os::Linux::get_frame_at_stack_banging_point(thread, uc, &fr)) { 304 assert(fr.is_java_frame(), "Must be a Java frame"); 305 frame activation = 306 SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr); 307 if (activation.sp() != NULL) { 308 thread->disable_stack_reserved_zone(); 309 if (activation.is_interpreted_frame()) { 310 thread->set_reserved_stack_activation((address)( 311 activation.fp() + frame::interpreter_frame_initial_sp_offset)); 312 } else { 313 thread->set_reserved_stack_activation((address)activation.unextended_sp()); 314 } 315 return 1; 316 } 317 } 318 } 319 // Throw a stack overflow exception. Guard pages will be reenabled 320 // while unwinding the stack. 321 thread->disable_stack_yellow_reserved_zone(); 322 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW); 323 } else { 324 // Thread was in the vm or native code. Return and try to finish. 325 thread->disable_stack_yellow_reserved_zone(); 326 return 1; 327 } 328 } else if (thread->in_stack_red_zone(addr)) { 329 // Fatal red zone violation. Disable the guard pages and fall through 330 // to handle_unexpected_exception way down below. 331 thread->disable_stack_red_zone(); 332 tty->print_raw_cr("An irrecoverable stack overflow has occurred."); 333 334 // This is a likely cause, but hard to verify. Let's just print 335 // it as a hint. 336 tty->print_raw_cr("Please check if any of your loaded .so files has " 337 "enabled executable stack (see man page execstack(8))"); 338 } else { 339 // Accessing stack address below sp may cause SEGV if current 340 // thread has MAP_GROWSDOWN stack. This should only happen when 341 // current thread was created by user code with MAP_GROWSDOWN flag 342 // and then attached to VM. See notes in os_linux.cpp. 343 if (thread->osthread()->expanding_stack() == 0) { 344 thread->osthread()->set_expanding_stack(); 345 if (os::Linux::manually_expand_stack(thread, addr)) { 346 thread->osthread()->clear_expanding_stack(); 347 return 1; 348 } 349 thread->osthread()->clear_expanding_stack(); 350 } else { 351 fatal("recursive segv. expanding stack."); 352 } 353 } 354 } 355 } 356 357 if (thread->thread_state() == _thread_in_Java) { 358 // Java thread running in Java code => find exception handler if any 359 // a fault inside compiled code, the interpreter, or a stub 360 361 // Handle signal from NativeJump::patch_verified_entry(). 362 if ((sig == SIGILL || sig == SIGTRAP) 363 && nativeInstruction_at(pc)->is_sigill_zombie_not_entrant()) { 364 if (TraceTraps) { 365 tty->print_cr("trap: zombie_not_entrant (%s)", (sig == SIGTRAP) ? "SIGTRAP" : "SIGILL"); 366 } 367 stub = SharedRuntime::get_handle_wrong_method_stub(); 368 } else if (sig == SIGSEGV && SafepointMechanism::is_poll_address((address)info->si_addr)) { 369 stub = SharedRuntime::get_poll_stub(pc); 370 } else if (sig == SIGBUS /* && info->si_code == BUS_OBJERR */) { 371 // BugId 4454115: A read from a MappedByteBuffer can fault 372 // here if the underlying file has been truncated. 373 // Do not crash the VM in such a case. 374 CodeBlob* cb = CodeCache::find_blob_unsafe(pc); 375 CompiledMethod* nm = (cb != NULL) ? cb->as_compiled_method_or_null() : NULL; 376 bool is_unsafe_arraycopy = (thread->doing_unsafe_access() && UnsafeCopyMemory::contains_pc(pc)); 377 if ((nm != NULL && nm->has_unsafe_access()) || is_unsafe_arraycopy) { 378 address next_pc = pc + NativeCall::instruction_size; 379 if (is_unsafe_arraycopy) { 380 next_pc = UnsafeCopyMemory::page_error_continue_pc(pc); 381 } 382 stub = SharedRuntime::handle_unsafe_access(thread, next_pc); 383 } 384 } 385 else 386 387 if (sig == SIGFPE && 388 (info->si_code == FPE_INTDIV || info->si_code == FPE_FLTDIV)) { 389 stub = 390 SharedRuntime:: 391 continuation_for_implicit_exception(thread, 392 pc, 393 SharedRuntime:: 394 IMPLICIT_DIVIDE_BY_ZERO); 395 } else if (sig == SIGSEGV && 396 MacroAssembler::uses_implicit_null_check((void*)addr)) { 397 // Determination of interpreter/vtable stub/compiled code null exception 398 stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::IMPLICIT_NULL); 399 } 400 } else if ((thread->thread_state() == _thread_in_vm || 401 thread->thread_state() == _thread_in_native) && 402 sig == SIGBUS && /* info->si_code == BUS_OBJERR && */ 403 thread->doing_unsafe_access()) { 404 address next_pc = pc + NativeCall::instruction_size; 405 if (UnsafeCopyMemory::contains_pc(pc)) { 406 next_pc = UnsafeCopyMemory::page_error_continue_pc(pc); 407 } 408 stub = SharedRuntime::handle_unsafe_access(thread, next_pc); 409 } 410 411 // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in 412 // and the heap gets shrunk before the field access. 413 if ((sig == SIGSEGV) || (sig == SIGBUS)) { 414 address addr = JNI_FastGetField::find_slowcase_pc(pc); 415 if (addr != (address)-1) { 416 stub = addr; 417 } 418 } 419 } 420 421 if (stub != NULL) { 422 // save all thread context in case we need to restore it 423 if (thread != NULL) thread->set_saved_exception_pc(pc); 424 425 os::Linux::ucontext_set_pc(uc, stub); 426 return true; 427 } 428 429 // signal-chaining 430 if (os::Linux::chained_handler(sig, info, ucVoid)) { 431 return true; 432 } 433 434 if (!abort_if_unrecognized) { 435 // caller wants another chance, so give it to him 436 return false; 437 } 438 439 if (pc == NULL && uc != NULL) { 440 pc = os::Linux::ucontext_get_pc(uc); 441 } 442 443 // unmask current signal 444 sigset_t newset; 445 sigemptyset(&newset); 446 sigaddset(&newset, sig); 447 sigprocmask(SIG_UNBLOCK, &newset, NULL); 448 449 VMError::report_and_die(t, sig, pc, info, ucVoid); 450 451 ShouldNotReachHere(); 452 return true; // Mute compiler 453 } 454 455 void os::Linux::init_thread_fpu_state(void) { 456 } 457 458 int os::Linux::get_fpu_control_word(void) { 459 return 0; 460 } 461 462 void os::Linux::set_fpu_control_word(int fpu_control) { 463 } 464 465 bool os::is_allocatable(size_t bytes) { 466 return true; 467 } 468 469 //////////////////////////////////////////////////////////////////////////////// 470 // thread stack 471 472 // Minimum usable stack sizes required to get to user code. Space for 473 // HotSpot guard pages is added later. 474 size_t os::Posix::_compiler_thread_min_stack_allowed = 72 * K; 475 size_t os::Posix::_java_thread_min_stack_allowed = 72 * K; 476 size_t os::Posix::_vm_internal_thread_min_stack_allowed = 72 * K; 477 478 // return default stack size for thr_type 479 size_t os::Posix::default_stack_size(os::ThreadType thr_type) { 480 // default stack size (compiler thread needs larger stack) 481 size_t s = (thr_type == os::compiler_thread ? 4 * M : 1 * M); 482 return s; 483 } 484 485 ///////////////////////////////////////////////////////////////////////////// 486 // helper functions for fatal error handler 487 488 void os::print_context(outputStream *st, const void *context) { 489 if (context == NULL) return; 490 491 const ucontext_t *uc = (const ucontext_t*)context; 492 st->print_cr("Registers:"); 493 for (int r = 0; r < 31; r++) { 494 st->print("R%-2d=", r); 495 print_location(st, uc->uc_mcontext.regs[r]); 496 } 497 st->cr(); 498 499 intptr_t *sp = (intptr_t *)os::Linux::ucontext_get_sp(uc); 500 st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp)); 501 print_hex_dump(st, (address)sp, (address)(sp + 8*sizeof(intptr_t)), sizeof(intptr_t)); 502 st->cr(); 503 504 // Note: it may be unsafe to inspect memory near pc. For example, pc may 505 // point to garbage if entry point in an nmethod is corrupted. Leave 506 // this at the end, and hope for the best. 507 address pc = os::Linux::ucontext_get_pc(uc); 508 print_instructions(st, pc, sizeof(char)); 509 st->cr(); 510 } 511 512 void os::print_register_info(outputStream *st, const void *context) { 513 if (context == NULL) return; 514 515 const ucontext_t *uc = (const ucontext_t*)context; 516 517 st->print_cr("Register to memory mapping:"); 518 st->cr(); 519 520 // this is horrendously verbose but the layout of the registers in the 521 // context does not match how we defined our abstract Register set, so 522 // we can't just iterate through the gregs area 523 524 // this is only for the "general purpose" registers 525 526 for (int r = 0; r < 31; r++) 527 st->print_cr( "R%d=" INTPTR_FORMAT, r, (uintptr_t)uc->uc_mcontext.regs[r]); 528 st->cr(); 529 } 530 531 void os::setup_fpu() { 532 } 533 534 #ifndef PRODUCT 535 void os::verify_stack_alignment() { 536 assert(((intptr_t)os::current_stack_pointer() & (StackAlignmentInBytes-1)) == 0, "incorrect stack alignment"); 537 } 538 #endif 539 540 int os::extra_bang_size_in_bytes() { 541 // AArch64 does not require the additional stack bang. 542 return 0; 543 } 544 545 extern "C" { 546 int SpinPause() { 547 return 0; 548 } 549 550 void _Copy_conjoint_jshorts_atomic(const jshort* from, jshort* to, size_t count) { 551 if (from > to) { 552 const jshort *end = from + count; 553 while (from < end) 554 *(to++) = *(from++); 555 } 556 else if (from < to) { 557 const jshort *end = from; 558 from += count - 1; 559 to += count - 1; 560 while (from >= end) 561 *(to--) = *(from--); 562 } 563 } 564 void _Copy_conjoint_jints_atomic(const jint* from, jint* to, size_t count) { 565 if (from > to) { 566 const jint *end = from + count; 567 while (from < end) 568 *(to++) = *(from++); 569 } 570 else if (from < to) { 571 const jint *end = from; 572 from += count - 1; 573 to += count - 1; 574 while (from >= end) 575 *(to--) = *(from--); 576 } 577 } 578 void _Copy_conjoint_jlongs_atomic(const jlong* from, jlong* to, size_t count) { 579 if (from > to) { 580 const jlong *end = from + count; 581 while (from < end) 582 os::atomic_copy64(from++, to++); 583 } 584 else if (from < to) { 585 const jlong *end = from; 586 from += count - 1; 587 to += count - 1; 588 while (from >= end) 589 os::atomic_copy64(from--, to--); 590 } 591 } 592 593 void _Copy_arrayof_conjoint_bytes(const HeapWord* from, 594 HeapWord* to, 595 size_t count) { 596 memmove(to, from, count); 597 } 598 void _Copy_arrayof_conjoint_jshorts(const HeapWord* from, 599 HeapWord* to, 600 size_t count) { 601 memmove(to, from, count * 2); 602 } 603 void _Copy_arrayof_conjoint_jints(const HeapWord* from, 604 HeapWord* to, 605 size_t count) { 606 memmove(to, from, count * 4); 607 } 608 void _Copy_arrayof_conjoint_jlongs(const HeapWord* from, 609 HeapWord* to, 610 size_t count) { 611 memmove(to, from, count * 8); 612 } 613 };