1 /*
   2  * Copyright (c) 1999, 2021, 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 
  26 #include "jvm.h"
  27 #ifdef LINUX
  28 #include "classfile/classLoader.hpp"
  29 #endif
  30 #include "jvmtifiles/jvmti.h"
  31 #include "logging/log.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "os_posix.inline.hpp"
  34 #include "runtime/globals_extension.hpp"
  35 #include "runtime/osThread.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 #include "runtime/frame.inline.hpp"
  38 #include "runtime/interfaceSupport.inline.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "services/attachListener.hpp"
  41 #include "services/memTracker.hpp"
  42 #include "runtime/arguments.hpp"
  43 #include "runtime/atomic.hpp"
  44 #include "runtime/java.hpp"
  45 #include "runtime/orderAccess.hpp"
  46 #include "runtime/perfMemory.hpp"
  47 #include "utilities/align.hpp"
  48 #include "utilities/events.hpp"
  49 #include "utilities/formatBuffer.hpp"
  50 #include "utilities/macros.hpp"
  51 #include "utilities/vmError.hpp"
  52 
  53 #include <dirent.h>
  54 #include <dlfcn.h>
  55 #include <grp.h>
  56 #include <netdb.h>
  57 #include <pwd.h>
  58 #include <pthread.h>
  59 #include <signal.h>
  60 #include <sys/mman.h>
  61 #include <sys/resource.h>
  62 #include <sys/socket.h>
  63 #include <sys/time.h>
  64 #include <sys/times.h>
  65 #include <sys/types.h>
  66 #include <sys/utsname.h>
  67 #include <sys/wait.h>
  68 #include <time.h>
  69 #include <unistd.h>
  70 #include <utmpx.h>
  71 
  72 #ifdef __APPLE__
  73   #include <crt_externs.h>
  74 #endif
  75 
  76 #define ROOT_UID 0
  77 
  78 #ifndef MAP_ANONYMOUS
  79   #define MAP_ANONYMOUS MAP_ANON
  80 #endif
  81 
  82 #define check_with_errno(check_type, cond, msg)                             \
  83   do {                                                                      \
  84     int err = errno;                                                        \
  85     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  86                os::errno_name(err));                                        \
  87 } while (false)
  88 
  89 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  90 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  91 
  92 static jlong initial_time_count = 0;
  93 
  94 static int clock_tics_per_sec = 100;
  95 
  96 // Check core dump limit and report possible place where core can be found
  97 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  98   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  99     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
 100     VMError::record_coredump_status(buffer, false);
 101     return;
 102   }
 103 
 104   int n;
 105   struct rlimit rlim;
 106   bool success;
 107 
 108   char core_path[PATH_MAX];
 109   n = get_core_path(core_path, PATH_MAX);
 110 
 111   if (n <= 0) {
 112     jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());
 113     success = true;
 114 #ifdef LINUX
 115   } else if (core_path[0] == '"') { // redirect to user process
 116     jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);
 117     success = true;
 118 #endif
 119   } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
 120     jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);
 121     success = true;
 122   } else {
 123     switch(rlim.rlim_cur) {
 124       case RLIM_INFINITY:
 125         jio_snprintf(buffer, bufferSize, "%s", core_path);
 126         success = true;
 127         break;
 128       case 0:
 129         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
 130         success = false;
 131         break;
 132       default:
 133         jio_snprintf(buffer, bufferSize, "%s (max size " UINT64_FORMAT " kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, uint64_t(rlim.rlim_cur) / 1024);
 134         success = true;
 135         break;
 136     }
 137   }
 138 
 139   VMError::record_coredump_status(buffer, success);
 140 }
 141 
 142 // native stack isn't walkable for RISCV this way.
 143 #if !defined(RISCV) || defined(ZERO)
 144 int os::get_native_stack(address* stack, int frames, int toSkip) {
 145   int frame_idx = 0;
 146   int num_of_frames;  // number of frames captured
 147   frame fr = os::current_frame();
 148   while (fr.pc() && frame_idx < frames) {
 149     if (toSkip > 0) {
 150       toSkip --;
 151     } else {
 152       stack[frame_idx ++] = fr.pc();
 153     }
 154     if (fr.fp() == NULL || fr.cb() != NULL ||
 155         fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;
 156 
 157     if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
 158       fr = os::get_sender_for_C_frame(&fr);
 159     } else {
 160       break;
 161     }
 162   }
 163   num_of_frames = frame_idx;
 164   for (; frame_idx < frames; frame_idx ++) {
 165     stack[frame_idx] = NULL;
 166   }
 167 
 168   return num_of_frames;
 169 }
 170 #endif
 171 
 172 int os::get_last_error() {
 173   return errno;
 174 }
 175 
 176 size_t os::lasterror(char *buf, size_t len) {
 177   if (errno == 0)  return 0;
 178 
 179   const char *s = os::strerror(errno);
 180   size_t n = ::strlen(s);
 181   if (n >= len) {
 182     n = len - 1;
 183   }
 184   ::strncpy(buf, s, n);
 185   buf[n] = '\0';
 186   return n;
 187 }
 188 
 189 // Return true if user is running as root.
 190 bool os::have_special_privileges() {
 191   static bool privileges = (getuid() != geteuid()) || (getgid() != getegid());
 192   return privileges;
 193 }
 194 
 195 void os::wait_for_keypress_at_exit(void) {
 196   // don't do anything on posix platforms
 197   return;
 198 }
 199 
 200 int os::create_file_for_heap(const char* dir) {
 201   int fd;
 202 
 203 #if defined(LINUX) && defined(O_TMPFILE)
 204   char* native_dir = os::strdup(dir);
 205   if (native_dir == NULL) {
 206     vm_exit_during_initialization(err_msg("strdup failed during creation of backing file for heap (%s)", os::strerror(errno)));
 207     return -1;
 208   }
 209   os::native_path(native_dir);
 210   fd = os::open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
 211   os::free(native_dir);
 212 
 213   if (fd == -1)
 214 #endif
 215   {
 216     const char name_template[] = "/jvmheap.XXXXXX";
 217 
 218     size_t fullname_len = strlen(dir) + strlen(name_template);
 219     char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);
 220     if (fullname == NULL) {
 221       vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 222       return -1;
 223     }
 224     int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);
 225     assert((size_t)n == fullname_len, "Unexpected number of characters in string");
 226 
 227     os::native_path(fullname);
 228 
 229     // create a new file.
 230     fd = mkstemp(fullname);
 231 
 232     if (fd < 0) {
 233       warning("Could not create file for heap with template %s", fullname);
 234       os::free(fullname);
 235       return -1;
 236     } else {
 237       // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 238       int ret = unlink(fullname);
 239       assert_with_errno(ret == 0, "unlink returned error");
 240     }
 241 
 242     os::free(fullname);
 243   }
 244 
 245   return fd;
 246 }
 247 
 248 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 249   char * addr;
 250   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 251   if (requested_addr != NULL) {
 252     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 253     flags |= MAP_FIXED;
 254   }
 255 
 256   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 257   // touch an uncommitted page. Otherwise, the read/write might
 258   // succeed if we have enough swap space to back the physical page.
 259   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 260                        flags, -1, 0);
 261 
 262   if (addr != MAP_FAILED) {
 263     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 264     return addr;
 265   }
 266   return NULL;
 267 }
 268 
 269 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
 270 #ifdef __APPLE__
 271   fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
 272   // First we try to get a continuous chunk of disk space
 273   int ret = fcntl(fd, F_PREALLOCATE, &store);
 274   if (ret == -1) {
 275     // Maybe we are too fragmented, try to allocate non-continuous range
 276     store.fst_flags = F_ALLOCATEALL;
 277     ret = fcntl(fd, F_PREALLOCATE, &store);
 278   }
 279   if(ret != -1) {
 280     return ftruncate(fd, len);
 281   }
 282   return -1;
 283 #else
 284   return posix_fallocate(fd, offset, len);
 285 #endif
 286 }
 287 
 288 // Map the given address range to the provided file descriptor.
 289 char* os::map_memory_to_file(char* base, size_t size, int fd) {
 290   assert(fd != -1, "File descriptor is not valid");
 291 
 292   // allocate space for the file
 293   int ret = util_posix_fallocate(fd, 0, (off_t)size);
 294   if (ret != 0) {
 295     vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));
 296     return NULL;
 297   }
 298 
 299   int prot = PROT_READ | PROT_WRITE;
 300   int flags = MAP_SHARED;
 301   if (base != NULL) {
 302     flags |= MAP_FIXED;
 303   }
 304   char* addr = (char*)mmap(base, size, prot, flags, fd, 0);
 305 
 306   if (addr == MAP_FAILED) {
 307     warning("Failed mmap to file. (%s)", os::strerror(errno));
 308     return NULL;
 309   }
 310   if (base != NULL && addr != base) {
 311     if (!os::release_memory(addr, size)) {
 312       warning("Could not release memory on unsuccessful file mapping");
 313     }
 314     return NULL;
 315   }
 316   return addr;
 317 }
 318 
 319 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
 320   assert(fd != -1, "File descriptor is not valid");
 321   assert(base != NULL, "Base cannot be NULL");
 322 
 323   return map_memory_to_file(base, size, fd);
 324 }
 325 
 326 static size_t calculate_aligned_extra_size(size_t size, size_t alignment) {
 327   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 328       "Alignment must be a multiple of allocation granularity (page size)");
 329   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 330 
 331   size_t extra_size = size + alignment;
 332   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 333   return extra_size;
 334 }
 335 
 336 // After a bigger chunk was mapped, unmaps start and end parts to get the requested alignment.
 337 static char* chop_extra_memory(size_t size, size_t alignment, char* extra_base, size_t extra_size) {
 338   // Do manual alignment
 339   char* aligned_base = align_up(extra_base, alignment);
 340 
 341   // [  |                                       |  ]
 342   // ^ extra_base
 343   //    ^ extra_base + begin_offset == aligned_base
 344   //     extra_base + begin_offset + size       ^
 345   //                       extra_base + extra_size ^
 346   // |<>| == begin_offset
 347   //                              end_offset == |<>|
 348   size_t begin_offset = aligned_base - extra_base;
 349   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 350 
 351   if (begin_offset > 0) {
 352       os::release_memory(extra_base, begin_offset);
 353   }
 354 
 355   if (end_offset > 0) {
 356       os::release_memory(extra_base + begin_offset + size, end_offset);
 357   }
 358 
 359   return aligned_base;
 360 }
 361 
 362 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 363 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 364 // rather than unmapping and remapping the whole chunk to get requested alignment.
 365 char* os::reserve_memory_aligned(size_t size, size_t alignment, bool exec) {
 366   size_t extra_size = calculate_aligned_extra_size(size, alignment);
 367   char* extra_base = os::reserve_memory(extra_size, exec);
 368   if (extra_base == NULL) {
 369     return NULL;
 370   }
 371   return chop_extra_memory(size, alignment, extra_base, extra_size);
 372 }
 373 
 374 char* os::map_memory_to_file_aligned(size_t size, size_t alignment, int file_desc) {
 375   size_t extra_size = calculate_aligned_extra_size(size, alignment);
 376   // For file mapping, we do not call os:map_memory_to_file(size,fd) since:
 377   // - we later chop away parts of the mapping using os::release_memory and that could fail if the
 378   //   original mmap call had been tied to an fd.
 379   // - The memory API os::reserve_memory uses is an implementation detail. It may (and usually is)
 380   //   mmap but it also may System V shared memory which cannot be uncommitted as a whole, so
 381   //   chopping off and unmapping excess bits back and front (see below) would not work.
 382   char* extra_base = reserve_mmapped_memory(extra_size, NULL);
 383   if (extra_base == NULL) {
 384     return NULL;
 385   }
 386   char* aligned_base = chop_extra_memory(size, alignment, extra_base, extra_size);
 387   // After we have an aligned address, we can replace anonymous mapping with file mapping
 388   if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {
 389     vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
 390   }
 391   MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);
 392   return aligned_base;
 393 }
 394 
 395 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
 396   // All supported POSIX platforms provide C99 semantics.
 397   int result = ::vsnprintf(buf, len, fmt, args);
 398   // If an encoding error occurred (result < 0) then it's not clear
 399   // whether the buffer is NUL terminated, so ensure it is.
 400   if ((result < 0) && (len > 0)) {
 401     buf[len - 1] = '\0';
 402   }
 403   return result;
 404 }
 405 
 406 int os::get_fileno(FILE* fp) {
 407   return NOT_AIX(::)fileno(fp);
 408 }
 409 
 410 struct tm* os::gmtime_pd(const time_t* clock, struct tm*  res) {
 411   return gmtime_r(clock, res);
 412 }
 413 
 414 void os::Posix::print_load_average(outputStream* st) {
 415   st->print("load average: ");
 416   double loadavg[3];
 417   int res = os::loadavg(loadavg, 3);
 418   if (res != -1) {
 419     st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 420   } else {
 421     st->print(" Unavailable");
 422   }
 423   st->cr();
 424 }
 425 
 426 // boot/uptime information;
 427 // unfortunately it does not work on macOS and Linux because the utx chain has no entry
 428 // for reboot at least on my test machines
 429 void os::Posix::print_uptime_info(outputStream* st) {
 430   int bootsec = -1;
 431   int currsec = time(NULL);
 432   struct utmpx* ent;
 433   setutxent();
 434   while ((ent = getutxent())) {
 435     if (!strcmp("system boot", ent->ut_line)) {
 436       bootsec = ent->ut_tv.tv_sec;
 437       break;
 438     }
 439   }
 440 
 441   if (bootsec != -1) {
 442     os::print_dhm(st, "OS uptime:", (long) (currsec-bootsec));
 443   }
 444 }
 445 
 446 static void print_rlimit(outputStream* st, const char* msg,
 447                          int resource, bool output_k = false) {
 448   struct rlimit rlim;
 449 
 450   st->print(" %s ", msg);
 451   int res = getrlimit(resource, &rlim);
 452   if (res == -1) {
 453     st->print("could not obtain value");
 454   } else {
 455     // soft limit
 456     if (rlim.rlim_cur == RLIM_INFINITY) { st->print("infinity"); }
 457     else {
 458       if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024); }
 459       else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur)); }
 460     }
 461     // hard limit
 462     st->print("/");
 463     if (rlim.rlim_max == RLIM_INFINITY) { st->print("infinity"); }
 464     else {
 465       if (output_k) { st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_max) / 1024); }
 466       else { st->print(UINT64_FORMAT, uint64_t(rlim.rlim_max)); }
 467     }
 468   }
 469 }
 470 
 471 void os::Posix::print_rlimit_info(outputStream* st) {
 472   st->print("rlimit (soft/hard):");
 473   print_rlimit(st, "STACK", RLIMIT_STACK, true);
 474   print_rlimit(st, ", CORE", RLIMIT_CORE, true);
 475 
 476 #if defined(AIX)
 477   st->print(", NPROC ");
 478   st->print("%d", sysconf(_SC_CHILD_MAX));
 479 
 480   print_rlimit(st, ", THREADS", RLIMIT_THREADS);
 481 #else
 482   print_rlimit(st, ", NPROC", RLIMIT_NPROC);
 483 #endif
 484 
 485   print_rlimit(st, ", NOFILE", RLIMIT_NOFILE);
 486   print_rlimit(st, ", AS", RLIMIT_AS, true);
 487   print_rlimit(st, ", CPU", RLIMIT_CPU);
 488   print_rlimit(st, ", DATA", RLIMIT_DATA, true);
 489 
 490   // maximum size of files that the process may create
 491   print_rlimit(st, ", FSIZE", RLIMIT_FSIZE, true);
 492 
 493 #if defined(LINUX) || defined(__APPLE__)
 494   // maximum number of bytes of memory that may be locked into RAM
 495   // (rounded down to the nearest  multiple of system pagesize)
 496   print_rlimit(st, ", MEMLOCK", RLIMIT_MEMLOCK, true);
 497 #endif
 498 
 499   // MacOS; The maximum size (in bytes) to which a process's resident set size may grow.
 500 #if defined(__APPLE__)
 501   print_rlimit(st, ", RSS", RLIMIT_RSS, true);
 502 #endif
 503 
 504   st->cr();
 505 }
 506 
 507 void os::Posix::print_uname_info(outputStream* st) {
 508   // kernel
 509   st->print("uname: ");
 510   struct utsname name;
 511   uname(&name);
 512   st->print("%s ", name.sysname);
 513 #ifdef ASSERT
 514   st->print("%s ", name.nodename);
 515 #endif
 516   st->print("%s ", name.release);
 517   st->print("%s ", name.version);
 518   st->print("%s", name.machine);
 519   st->cr();
 520 }
 521 
 522 void os::Posix::print_umask(outputStream* st, mode_t umsk) {
 523   st->print((umsk & S_IRUSR) ? "r" : "-");
 524   st->print((umsk & S_IWUSR) ? "w" : "-");
 525   st->print((umsk & S_IXUSR) ? "x" : "-");
 526   st->print((umsk & S_IRGRP) ? "r" : "-");
 527   st->print((umsk & S_IWGRP) ? "w" : "-");
 528   st->print((umsk & S_IXGRP) ? "x" : "-");
 529   st->print((umsk & S_IROTH) ? "r" : "-");
 530   st->print((umsk & S_IWOTH) ? "w" : "-");
 531   st->print((umsk & S_IXOTH) ? "x" : "-");
 532 }
 533 
 534 void os::Posix::print_user_info(outputStream* st) {
 535   unsigned id = (unsigned) ::getuid();
 536   st->print("uid  : %u ", id);
 537   id = (unsigned) ::geteuid();
 538   st->print("euid : %u ", id);
 539   id = (unsigned) ::getgid();
 540   st->print("gid  : %u ", id);
 541   id = (unsigned) ::getegid();
 542   st->print_cr("egid : %u", id);
 543   st->cr();
 544 
 545   mode_t umsk = ::umask(0);
 546   ::umask(umsk);
 547   st->print("umask: %04o (", (unsigned) umsk);
 548   print_umask(st, umsk);
 549   st->print_cr(")");
 550   st->cr();
 551 }
 552 
 553 
 554 bool os::get_host_name(char* buf, size_t buflen) {
 555   struct utsname name;
 556   uname(&name);
 557   jio_snprintf(buf, buflen, "%s", name.nodename);
 558   return true;
 559 }
 560 
 561 #ifndef _LP64
 562 // Helper, on 32bit, for os::has_allocatable_memory_limit
 563 static bool is_allocatable(size_t s) {
 564   if (s < 2 * G) {
 565     return true;
 566   }
 567   // Use raw anonymous mmap here; no need to go through any
 568   // of our reservation layers. We will unmap right away.
 569   void* p = ::mmap(NULL, s, PROT_NONE,
 570                    MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS, -1, 0);
 571   if (p == MAP_FAILED) {
 572     return false;
 573   } else {
 574     ::munmap(p, s);
 575     return true;
 576   }
 577 }
 578 #endif // !_LP64
 579 
 580 
 581 bool os::has_allocatable_memory_limit(size_t* limit) {
 582   struct rlimit rlim;
 583   int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
 584   // if there was an error when calling getrlimit, assume that there is no limitation
 585   // on virtual memory.
 586   bool result;
 587   if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
 588     result = false;
 589   } else {
 590     *limit = (size_t)rlim.rlim_cur;
 591     result = true;
 592   }
 593 #ifdef _LP64
 594   return result;
 595 #else
 596   // arbitrary virtual space limit for 32 bit Unices found by testing. If
 597   // getrlimit above returned a limit, bound it with this limit. Otherwise
 598   // directly use it.
 599   const size_t max_virtual_limit = 3800*M;
 600   if (result) {
 601     *limit = MIN2(*limit, max_virtual_limit);
 602   } else {
 603     *limit = max_virtual_limit;
 604   }
 605 
 606   // bound by actually allocatable memory. The algorithm uses two bounds, an
 607   // upper and a lower limit. The upper limit is the current highest amount of
 608   // memory that could not be allocated, the lower limit is the current highest
 609   // amount of memory that could be allocated.
 610   // The algorithm iteratively refines the result by halving the difference
 611   // between these limits, updating either the upper limit (if that value could
 612   // not be allocated) or the lower limit (if the that value could be allocated)
 613   // until the difference between these limits is "small".
 614 
 615   // the minimum amount of memory we care about allocating.
 616   const size_t min_allocation_size = M;
 617 
 618   size_t upper_limit = *limit;
 619 
 620   // first check a few trivial cases
 621   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 622     *limit = upper_limit;
 623   } else if (!is_allocatable(min_allocation_size)) {
 624     // we found that not even min_allocation_size is allocatable. Return it
 625     // anyway. There is no point to search for a better value any more.
 626     *limit = min_allocation_size;
 627   } else {
 628     // perform the binary search.
 629     size_t lower_limit = min_allocation_size;
 630     while ((upper_limit - lower_limit) > min_allocation_size) {
 631       size_t temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 632       temp_limit = align_down(temp_limit, min_allocation_size);
 633       if (is_allocatable(temp_limit)) {
 634         lower_limit = temp_limit;
 635       } else {
 636         upper_limit = temp_limit;
 637       }
 638     }
 639     *limit = lower_limit;
 640   }
 641   return true;
 642 #endif
 643 }
 644 
 645 void* os::get_default_process_handle() {
 646 #ifdef __APPLE__
 647   // MacOS X needs to use RTLD_FIRST instead of RTLD_LAZY
 648   // to avoid finding unexpected symbols on second (or later)
 649   // loads of a library.
 650   return (void*)::dlopen(NULL, RTLD_FIRST);
 651 #else
 652   return (void*)::dlopen(NULL, RTLD_LAZY);
 653 #endif
 654 }
 655 
 656 void* os::dll_lookup(void* handle, const char* name) {
 657   return dlsym(handle, name);
 658 }
 659 
 660 void os::dll_unload(void *lib) {
 661   ::dlclose(lib);
 662 }
 663 
 664 jlong os::lseek(int fd, jlong offset, int whence) {
 665   return (jlong) BSD_ONLY(::lseek) NOT_BSD(::lseek64)(fd, offset, whence);
 666 }
 667 
 668 int os::fsync(int fd) {
 669   return ::fsync(fd);
 670 }
 671 
 672 int os::ftruncate(int fd, jlong length) {
 673    return BSD_ONLY(::ftruncate) NOT_BSD(::ftruncate64)(fd, length);
 674 }
 675 
 676 const char* os::get_current_directory(char *buf, size_t buflen) {
 677   return getcwd(buf, buflen);
 678 }
 679 
 680 FILE* os::open(int fd, const char* mode) {
 681   return ::fdopen(fd, mode);
 682 }
 683 
 684 size_t os::write(int fd, const void *buf, unsigned int nBytes) {
 685   size_t res;
 686   RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
 687   return res;
 688 }
 689 
 690 ssize_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
 691   return ::pread(fd, buf, nBytes, offset);
 692 }
 693 
 694 int os::close(int fd) {
 695   return ::close(fd);
 696 }
 697 
 698 void os::flockfile(FILE* fp) {
 699   ::flockfile(fp);
 700 }
 701 
 702 void os::funlockfile(FILE* fp) {
 703   ::funlockfile(fp);
 704 }
 705 
 706 DIR* os::opendir(const char* dirname) {
 707   assert(dirname != NULL, "just checking");
 708   return ::opendir(dirname);
 709 }
 710 
 711 struct dirent* os::readdir(DIR* dirp) {
 712   assert(dirp != NULL, "just checking");
 713   return ::readdir(dirp);
 714 }
 715 
 716 int os::closedir(DIR *dirp) {
 717   assert(dirp != NULL, "just checking");
 718   return ::closedir(dirp);
 719 }
 720 
 721 int os::socket_close(int fd) {
 722   return ::close(fd);
 723 }
 724 
 725 int os::socket(int domain, int type, int protocol) {
 726   return ::socket(domain, type, protocol);
 727 }
 728 
 729 int os::recv(int fd, char* buf, size_t nBytes, uint flags) {
 730   RESTARTABLE_RETURN_INT(::recv(fd, buf, nBytes, flags));
 731 }
 732 
 733 int os::send(int fd, char* buf, size_t nBytes, uint flags) {
 734   RESTARTABLE_RETURN_INT(::send(fd, buf, nBytes, flags));
 735 }
 736 
 737 int os::raw_send(int fd, char* buf, size_t nBytes, uint flags) {
 738   return os::send(fd, buf, nBytes, flags);
 739 }
 740 
 741 int os::connect(int fd, struct sockaddr* him, socklen_t len) {
 742   RESTARTABLE_RETURN_INT(::connect(fd, him, len));
 743 }
 744 
 745 struct hostent* os::get_host_by_name(char* name) {
 746   return ::gethostbyname(name);
 747 }
 748 
 749 void os::exit(int num) {
 750   ::exit(num);
 751 }
 752 
 753 void os::_exit(int num) {
 754   ::_exit(num);
 755 }
 756 
 757 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
 758 // which is used to find statically linked in agents.
 759 // Parameters:
 760 //            sym_name: Symbol in library we are looking for
 761 //            lib_name: Name of library to look in, NULL for shared libs.
 762 //            is_absolute_path == true if lib_name is absolute path to agent
 763 //                                     such as "/a/b/libL.so"
 764 //            == false if only the base name of the library is passed in
 765 //               such as "L"
 766 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
 767                                     bool is_absolute_path) {
 768   char *agent_entry_name;
 769   size_t len;
 770   size_t name_len;
 771   size_t prefix_len = strlen(JNI_LIB_PREFIX);
 772   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
 773   const char *start;
 774 
 775   if (lib_name != NULL) {
 776     name_len = strlen(lib_name);
 777     if (is_absolute_path) {
 778       // Need to strip path, prefix and suffix
 779       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
 780         lib_name = ++start;
 781       }
 782       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 783         return NULL;
 784       }
 785       lib_name += prefix_len;
 786       name_len = strlen(lib_name) - suffix_len;
 787     }
 788   }
 789   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 790   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 791   if (agent_entry_name == NULL) {
 792     return NULL;
 793   }
 794   strcpy(agent_entry_name, sym_name);
 795   if (lib_name != NULL) {
 796     strcat(agent_entry_name, "_");
 797     strncat(agent_entry_name, lib_name, name_len);
 798   }
 799   return agent_entry_name;
 800 }
 801 
 802 
 803 void os::naked_short_nanosleep(jlong ns) {
 804   struct timespec req;
 805   assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
 806   req.tv_sec = 0;
 807   req.tv_nsec = ns;
 808   ::nanosleep(&req, NULL);
 809   return;
 810 }
 811 
 812 void os::naked_short_sleep(jlong ms) {
 813   assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");
 814   os::naked_short_nanosleep(millis_to_nanos(ms));
 815   return;
 816 }
 817 
 818 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
 819   size_t stack_size = 0;
 820   size_t guard_size = 0;
 821   int detachstate = 0;
 822   pthread_attr_getstacksize(attr, &stack_size);
 823   pthread_attr_getguardsize(attr, &guard_size);
 824   // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
 825   LINUX_ONLY(stack_size -= guard_size);
 826   pthread_attr_getdetachstate(attr, &detachstate);
 827   jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
 828     stack_size / 1024, guard_size / 1024,
 829     (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
 830   return buf;
 831 }
 832 
 833 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
 834 
 835   if (filename == NULL || outbuf == NULL || outbuflen < 1) {
 836     assert(false, "os::Posix::realpath: invalid arguments.");
 837     errno = EINVAL;
 838     return NULL;
 839   }
 840 
 841   char* result = NULL;
 842 
 843   // This assumes platform realpath() is implemented according to POSIX.1-2008.
 844   // POSIX.1-2008 allows to specify NULL for the output buffer, in which case
 845   // output buffer is dynamically allocated and must be ::free()'d by the caller.
 846   char* p = ::realpath(filename, NULL);
 847   if (p != NULL) {
 848     if (strlen(p) < outbuflen) {
 849       strcpy(outbuf, p);
 850       result = outbuf;
 851     } else {
 852       errno = ENAMETOOLONG;
 853     }
 854     ::free(p); // *not* os::free
 855   } else {
 856     // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath
 857     // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and
 858     // that it complains about the NULL we handed down as user buffer.
 859     // In this case, use the user provided buffer but at least check whether realpath caused
 860     // a memory overwrite.
 861     if (errno == EINVAL) {
 862       outbuf[outbuflen - 1] = '\0';
 863       p = ::realpath(filename, outbuf);
 864       if (p != NULL) {
 865         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
 866         result = p;
 867       }
 868     }
 869   }
 870   return result;
 871 
 872 }
 873 
 874 int os::stat(const char *path, struct stat *sbuf) {
 875   return ::stat(path, sbuf);
 876 }
 877 
 878 char * os::native_path(char *path) {
 879   return path;
 880 }
 881 
 882 bool os::same_files(const char* file1, const char* file2) {
 883   if (file1 == nullptr && file2 == nullptr) {
 884     return true;
 885   }
 886 
 887   if (file1 == nullptr || file2 == nullptr) {
 888     return false;
 889   }
 890 
 891   if (strcmp(file1, file2) == 0) {
 892     return true;
 893   }
 894 
 895   bool is_same = false;
 896   struct stat st1;
 897   struct stat st2;
 898 
 899   if (os::stat(file1, &st1) < 0) {
 900     return false;
 901   }
 902 
 903   if (os::stat(file2, &st2) < 0) {
 904     return false;
 905   }
 906 
 907   if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
 908     // same files
 909     is_same = true;
 910   }
 911   return is_same;
 912 }
 913 
 914 // Check minimum allowable stack sizes for thread creation and to initialize
 915 // the java system classes, including StackOverflowError - depends on page
 916 // size.
 917 // The space needed for frames during startup is platform dependent. It
 918 // depends on word size, platform calling conventions, C frame layout and
 919 // interpreter/C1/C2 design decisions. Therefore this is given in a
 920 // platform (os/cpu) dependent constant.
 921 // To this, space for guard mechanisms is added, which depends on the
 922 // page size which again depends on the concrete system the VM is running
 923 // on. Space for libc guard pages is not included in this size.
 924 jint os::Posix::set_minimum_stack_sizes() {
 925   size_t os_min_stack_allowed = PTHREAD_STACK_MIN;
 926 
 927   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
 928                                    StackOverflow::stack_guard_zone_size() +
 929                                    StackOverflow::stack_shadow_zone_size();
 930 
 931   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
 932   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
 933 
 934   size_t stack_size_in_bytes = ThreadStackSize * K;
 935   if (stack_size_in_bytes != 0 &&
 936       stack_size_in_bytes < _java_thread_min_stack_allowed) {
 937     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
 938     // ThreadStackSize so we go with "Java thread stack size" instead
 939     // of "ThreadStackSize" to be more friendly.
 940     tty->print_cr("\nThe Java thread stack size specified is too small. "
 941                   "Specify at least " SIZE_FORMAT "k",
 942                   _java_thread_min_stack_allowed / K);
 943     return JNI_ERR;
 944   }
 945 
 946   // Make the stack size a multiple of the page size so that
 947   // the yellow/red zones can be guarded.
 948   JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));
 949 
 950   // Reminder: a compiler thread is a Java thread.
 951   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
 952                                        StackOverflow::stack_guard_zone_size() +
 953                                        StackOverflow::stack_shadow_zone_size();
 954 
 955   _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
 956   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
 957 
 958   stack_size_in_bytes = CompilerThreadStackSize * K;
 959   if (stack_size_in_bytes != 0 &&
 960       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
 961     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
 962                   "Specify at least " SIZE_FORMAT "k",
 963                   _compiler_thread_min_stack_allowed / K);
 964     return JNI_ERR;
 965   }
 966 
 967   _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
 968   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
 969 
 970   stack_size_in_bytes = VMThreadStackSize * K;
 971   if (stack_size_in_bytes != 0 &&
 972       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
 973     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
 974                   "Specify at least " SIZE_FORMAT "k",
 975                   _vm_internal_thread_min_stack_allowed / K);
 976     return JNI_ERR;
 977   }
 978   return JNI_OK;
 979 }
 980 
 981 // Called when creating the thread.  The minimum stack sizes have already been calculated
 982 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
 983   size_t stack_size;
 984   if (req_stack_size == 0) {
 985     stack_size = default_stack_size(thr_type);
 986   } else {
 987     stack_size = req_stack_size;
 988   }
 989 
 990   switch (thr_type) {
 991   case os::java_thread:
 992     // Java threads use ThreadStackSize which default value can be
 993     // changed with the flag -Xss
 994     if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {
 995       // no requested size and we have a more specific default value
 996       stack_size = JavaThread::stack_size_at_create();
 997     }
 998     stack_size = MAX2(stack_size,
 999                       _java_thread_min_stack_allowed);
1000     break;
1001   case os::compiler_thread:
1002     if (req_stack_size == 0 && CompilerThreadStackSize > 0) {
1003       // no requested size and we have a more specific default value
1004       stack_size = (size_t)(CompilerThreadStackSize * K);
1005     }
1006     stack_size = MAX2(stack_size,
1007                       _compiler_thread_min_stack_allowed);
1008     break;
1009   case os::vm_thread:
1010   case os::gc_thread:
1011   case os::watcher_thread:
1012   default:  // presume the unknown thr_type is a VM internal
1013     if (req_stack_size == 0 && VMThreadStackSize > 0) {
1014       // no requested size and we have a more specific default value
1015       stack_size = (size_t)(VMThreadStackSize * K);
1016     }
1017 
1018     stack_size = MAX2(stack_size,
1019                       _vm_internal_thread_min_stack_allowed);
1020     break;
1021   }
1022 
1023   // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1024   // Be careful not to round up to 0. Align down in that case.
1025   if (stack_size <= SIZE_MAX - vm_page_size()) {
1026     stack_size = align_up(stack_size, vm_page_size());
1027   } else {
1028     stack_size = align_down(stack_size, vm_page_size());
1029   }
1030 
1031   return stack_size;
1032 }
1033 
1034 #ifndef ZERO
1035 #ifndef ARM
1036 static bool get_frame_at_stack_banging_point(JavaThread* thread, address pc, const void* ucVoid, frame* fr) {
1037   if (Interpreter::contains(pc)) {
1038     // interpreter performs stack banging after the fixed frame header has
1039     // been generated while the compilers perform it before. To maintain
1040     // semantic consistency between interpreted and compiled frames, the
1041     // method returns the Java sender of the current frame.
1042     *fr = os::fetch_frame_from_context(ucVoid);
1043     if (!fr->is_first_java_frame()) {
1044       // get_frame_at_stack_banging_point() is only called when we
1045       // have well defined stacks so java_sender() calls do not need
1046       // to assert safe_for_sender() first.
1047       *fr = fr->java_sender();
1048     }
1049   } else {
1050     // more complex code with compiled code
1051     assert(!Interpreter::contains(pc), "Interpreted methods should have been handled above");
1052     CodeBlob* cb = CodeCache::find_blob(pc);
1053     if (cb == NULL || !cb->is_nmethod() || cb->is_frame_complete_at(pc)) {
1054       // Not sure where the pc points to, fallback to default
1055       // stack overflow handling
1056       return false;
1057     } else {
1058       // in compiled code, the stack banging is performed just after the return pc
1059       // has been pushed on the stack
1060       *fr = os::fetch_compiled_frame_from_context(ucVoid);
1061       if (!fr->is_java_frame()) {
1062         assert(!fr->is_first_frame(), "Safety check");
1063         // See java_sender() comment above.
1064         *fr = fr->java_sender();
1065       }
1066     }
1067   }
1068   assert(fr->is_java_frame(), "Safety check");
1069   return true;
1070 }
1071 #endif // ARM
1072 
1073 // This return true if the signal handler should just continue, ie. return after calling this
1074 bool os::Posix::handle_stack_overflow(JavaThread* thread, address addr, address pc,
1075                                       const void* ucVoid, address* stub) {
1076   // stack overflow
1077   StackOverflow* overflow_state = thread->stack_overflow_state();
1078   if (overflow_state->in_stack_yellow_reserved_zone(addr)) {
1079     if (thread->thread_state() == _thread_in_Java) {
1080 #ifndef ARM
1081       // arm32 doesn't have this
1082       if (overflow_state->in_stack_reserved_zone(addr)) {
1083         frame fr;
1084         if (get_frame_at_stack_banging_point(thread, pc, ucVoid, &fr)) {
1085           assert(fr.is_java_frame(), "Must be a Java frame");
1086           frame activation =
1087             SharedRuntime::look_for_reserved_stack_annotated_method(thread, fr);
1088           if (activation.sp() != NULL) {
1089             overflow_state->disable_stack_reserved_zone();
1090             if (activation.is_interpreted_frame()) {
1091               overflow_state->set_reserved_stack_activation((address)(activation.fp()
1092                 // Some platforms use frame pointers for interpreter frames, others use initial sp.
1093 #if !defined(PPC64) && !defined(S390)
1094                 + frame::interpreter_frame_initial_sp_offset
1095 #endif
1096                 ));
1097             } else {
1098               overflow_state->set_reserved_stack_activation((address)activation.unextended_sp());
1099             }
1100             return true; // just continue
1101           }
1102         }
1103       }
1104 #endif // ARM
1105       // Throw a stack overflow exception.  Guard pages will be reenabled
1106       // while unwinding the stack.
1107       overflow_state->disable_stack_yellow_reserved_zone();
1108       *stub = SharedRuntime::continuation_for_implicit_exception(thread, pc, SharedRuntime::STACK_OVERFLOW);
1109     } else {
1110       // Thread was in the vm or native code.  Return and try to finish.
1111       overflow_state->disable_stack_yellow_reserved_zone();
1112       return true; // just continue
1113     }
1114   } else if (overflow_state->in_stack_red_zone(addr)) {
1115     // Fatal red zone violation.  Disable the guard pages and fall through
1116     // to handle_unexpected_exception way down below.
1117     overflow_state->disable_stack_red_zone();
1118     tty->print_raw_cr("An irrecoverable stack overflow has occurred.");
1119 
1120     // This is a likely cause, but hard to verify. Let's just print
1121     // it as a hint.
1122     tty->print_raw_cr("Please check if any of your loaded .so files has "
1123                       "enabled executable stack (see man page execstack(8))");
1124 
1125   } else {
1126 #if !defined(AIX) && !defined(__APPLE__)
1127     // bsd and aix don't have this
1128 
1129     // Accessing stack address below sp may cause SEGV if current
1130     // thread has MAP_GROWSDOWN stack. This should only happen when
1131     // current thread was created by user code with MAP_GROWSDOWN flag
1132     // and then attached to VM. See notes in os_linux.cpp.
1133     if (thread->osthread()->expanding_stack() == 0) {
1134        thread->osthread()->set_expanding_stack();
1135        if (os::Linux::manually_expand_stack(thread, addr)) {
1136          thread->osthread()->clear_expanding_stack();
1137          return true; // just continue
1138        }
1139        thread->osthread()->clear_expanding_stack();
1140     } else {
1141        fatal("recursive segv. expanding stack.");
1142     }
1143 #else
1144     tty->print_raw_cr("SIGSEGV happened inside stack but outside yellow and red zone.");
1145 #endif // AIX or BSD
1146   }
1147   return false;
1148 }
1149 #endif // ZERO
1150 
1151 bool os::Posix::is_root(uid_t uid){
1152     return ROOT_UID == uid;
1153 }
1154 
1155 bool os::Posix::matches_effective_uid_or_root(uid_t uid) {
1156     return is_root(uid) || geteuid() == uid;
1157 }
1158 
1159 bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) {
1160     return is_root(uid) || (geteuid() == uid && getegid() == gid);
1161 }
1162 
1163 Thread* os::ThreadCrashProtection::_protected_thread = NULL;
1164 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;
1165 
1166 os::ThreadCrashProtection::ThreadCrashProtection() {
1167   _protected_thread = Thread::current();
1168   assert(_protected_thread->is_JfrSampler_thread(), "should be JFRSampler");
1169 }
1170 
1171 /*
1172  * See the caveats for this class in os_posix.hpp
1173  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1174  * method and returns false. If none of the signals are raised, returns true.
1175  * The callback is supposed to provide the method that should be protected.
1176  */
1177 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1178   sigset_t saved_sig_mask;
1179 
1180   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
1181   // since on at least some systems (OS X) siglongjmp will restore the mask
1182   // for the process, not the thread
1183   pthread_sigmask(0, NULL, &saved_sig_mask);
1184   if (sigsetjmp(_jmpbuf, 0) == 0) {
1185     // make sure we can see in the signal handler that we have crash protection
1186     // installed
1187     _crash_protection = this;
1188     cb.call();
1189     // and clear the crash protection
1190     _crash_protection = NULL;
1191     _protected_thread = NULL;
1192     return true;
1193   }
1194   // this happens when we siglongjmp() back
1195   pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
1196   _crash_protection = NULL;
1197   _protected_thread = NULL;
1198   return false;
1199 }
1200 
1201 void os::ThreadCrashProtection::restore() {
1202   assert(_crash_protection != NULL, "must have crash protection");
1203   siglongjmp(_jmpbuf, 1);
1204 }
1205 
1206 void os::ThreadCrashProtection::check_crash_protection(int sig,
1207     Thread* thread) {
1208 
1209   if (thread != NULL &&
1210       thread == _protected_thread &&
1211       _crash_protection != NULL) {
1212 
1213     if (sig == SIGSEGV || sig == SIGBUS) {
1214       _crash_protection->restore();
1215     }
1216   }
1217 }
1218 
1219 // Shared clock/time and other supporting routines for pthread_mutex/cond
1220 // initialization. This is enabled on Solaris but only some of the clock/time
1221 // functionality is actually used there.
1222 
1223 // Shared condattr object for use with relative timed-waits. Will be associated
1224 // with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,
1225 // but otherwise whatever default is used by the platform - generally the
1226 // time-of-day clock.
1227 static pthread_condattr_t _condAttr[1];
1228 
1229 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1230 // all systems (e.g. FreeBSD) map the default to "normal".
1231 static pthread_mutexattr_t _mutexAttr[1];
1232 
1233 // common basic initialization that is always supported
1234 static void pthread_init_common(void) {
1235   int status;
1236   if ((status = pthread_condattr_init(_condAttr)) != 0) {
1237     fatal("pthread_condattr_init: %s", os::strerror(status));
1238   }
1239   if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1240     fatal("pthread_mutexattr_init: %s", os::strerror(status));
1241   }
1242   if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1243     fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1244   }
1245   os::PlatformMutex::init();
1246 }
1247 
1248 static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t) = NULL;
1249 
1250 static bool _use_clock_monotonic_condattr = false;
1251 
1252 // Determine what POSIX API's are present and do appropriate
1253 // configuration.
1254 void os::Posix::init(void) {
1255 #if defined(_ALLBSD_SOURCE)
1256   clock_tics_per_sec = CLK_TCK;
1257 #else
1258   clock_tics_per_sec = sysconf(_SC_CLK_TCK);
1259 #endif
1260   // NOTE: no logging available when this is called. Put logging
1261   // statements in init_2().
1262 
1263   // Check for pthread_condattr_setclock support.
1264 
1265   // libpthread is already loaded.
1266   int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =
1267     (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,
1268                                                    "pthread_condattr_setclock");
1269   if (condattr_setclock_func != NULL) {
1270     _pthread_condattr_setclock = condattr_setclock_func;
1271   }
1272 
1273   // Now do general initialization.
1274 
1275   pthread_init_common();
1276 
1277   int status;
1278   if (_pthread_condattr_setclock != NULL) {
1279     if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {
1280       if (status == EINVAL) {
1281         _use_clock_monotonic_condattr = false;
1282         warning("Unable to use monotonic clock with relative timed-waits" \
1283                 " - changes to the time-of-day clock may have adverse affects");
1284       } else {
1285         fatal("pthread_condattr_setclock: %s", os::strerror(status));
1286       }
1287     } else {
1288       _use_clock_monotonic_condattr = true;
1289     }
1290   }
1291 
1292   initial_time_count = javaTimeNanos();
1293 }
1294 
1295 void os::Posix::init_2(void) {
1296   log_info(os)("Use of CLOCK_MONOTONIC is supported");
1297   log_info(os)("Use of pthread_condattr_setclock is%s supported",
1298                (_pthread_condattr_setclock != NULL ? "" : " not"));
1299   log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",
1300                _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");
1301 }
1302 
1303 // Utility to convert the given timeout to an absolute timespec
1304 // (based on the appropriate clock) to use with pthread_cond_timewait,
1305 // and sem_timedwait().
1306 // The clock queried here must be the clock used to manage the
1307 // timeout of the condition variable or semaphore.
1308 //
1309 // The passed in timeout value is either a relative time in nanoseconds
1310 // or an absolute time in milliseconds. A relative timeout will be
1311 // associated with CLOCK_MONOTONIC if available, unless the real-time clock
1312 // is explicitly requested; otherwise, or if absolute,
1313 // the default time-of-day clock will be used.
1314 
1315 // Given time is a 64-bit value and the time_t used in the timespec is
1316 // sometimes a signed-32-bit value we have to watch for overflow if times
1317 // way in the future are given. Further on Solaris versions
1318 // prior to 10 there is a restriction (see cond_timedwait) that the specified
1319 // number of seconds, in abstime, is less than current_time + 100000000.
1320 // As it will be over 20 years before "now + 100000000" will overflow we can
1321 // ignore overflow and just impose a hard-limit on seconds using the value
1322 // of "now + 100000000". This places a limit on the timeout of about 3.17
1323 // years from "now".
1324 //
1325 #define MAX_SECS 100000000
1326 
1327 // Calculate a new absolute time that is "timeout" nanoseconds from "now".
1328 // "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending
1329 // on which clock API is being used).
1330 static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,
1331                           jlong now_part_sec, jlong unit) {
1332   time_t max_secs = now_sec + MAX_SECS;
1333 
1334   jlong seconds = timeout / NANOUNITS;
1335   timeout %= NANOUNITS; // remaining nanos
1336 
1337   if (seconds >= MAX_SECS) {
1338     // More seconds than we can add, so pin to max_secs.
1339     abstime->tv_sec = max_secs;
1340     abstime->tv_nsec = 0;
1341   } else {
1342     abstime->tv_sec = now_sec  + seconds;
1343     long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;
1344     if (nanos >= NANOUNITS) { // overflow
1345       abstime->tv_sec += 1;
1346       nanos -= NANOUNITS;
1347     }
1348     abstime->tv_nsec = nanos;
1349   }
1350 }
1351 
1352 // Unpack the given deadline in milliseconds since the epoch, into the given timespec.
1353 // The current time in seconds is also passed in to enforce an upper bound as discussed above.
1354 static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {
1355   time_t max_secs = now_sec + MAX_SECS;
1356 
1357   jlong seconds = deadline / MILLIUNITS;
1358   jlong millis = deadline % MILLIUNITS;
1359 
1360   if (seconds >= max_secs) {
1361     // Absolute seconds exceeds allowed max, so pin to max_secs.
1362     abstime->tv_sec = max_secs;
1363     abstime->tv_nsec = 0;
1364   } else {
1365     abstime->tv_sec = seconds;
1366     abstime->tv_nsec = millis_to_nanos(millis);
1367   }
1368 }
1369 
1370 static jlong millis_to_nanos_bounded(jlong millis) {
1371   // We have to watch for overflow when converting millis to nanos,
1372   // but if millis is that large then we will end up limiting to
1373   // MAX_SECS anyway, so just do that here.
1374   if (millis / MILLIUNITS > MAX_SECS) {
1375     millis = jlong(MAX_SECS) * MILLIUNITS;
1376   }
1377   return millis_to_nanos(millis);
1378 }
1379 
1380 static void to_abstime(timespec* abstime, jlong timeout,
1381                        bool isAbsolute, bool isRealtime) {
1382   DEBUG_ONLY(int max_secs = MAX_SECS;)
1383 
1384   if (timeout < 0) {
1385     timeout = 0;
1386   }
1387 
1388   clockid_t clock = CLOCK_MONOTONIC;
1389   if (isAbsolute || (!_use_clock_monotonic_condattr || isRealtime)) {
1390     clock = CLOCK_REALTIME;
1391   }
1392 
1393   struct timespec now;
1394   int status = clock_gettime(clock, &now);
1395   assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
1396 
1397   if (!isAbsolute) {
1398     calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);
1399   } else {
1400     unpack_abs_time(abstime, timeout, now.tv_sec);
1401   }
1402   DEBUG_ONLY(max_secs += now.tv_sec;)
1403 
1404   assert(abstime->tv_sec >= 0, "tv_sec < 0");
1405   assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");
1406   assert(abstime->tv_nsec >= 0, "tv_nsec < 0");
1407   assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");
1408 }
1409 
1410 // Create an absolute time 'millis' milliseconds in the future, using the
1411 // real-time (time-of-day) clock. Used by PosixSemaphore.
1412 void os::Posix::to_RTC_abstime(timespec* abstime, int64_t millis) {
1413   to_abstime(abstime, millis_to_nanos_bounded(millis),
1414              false /* not absolute */,
1415              true  /* use real-time clock */);
1416 }
1417 
1418 // Common (partly) shared time functions
1419 
1420 jlong os::javaTimeMillis() {
1421   struct timespec ts;
1422   int status = clock_gettime(CLOCK_REALTIME, &ts);
1423   assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
1424   return jlong(ts.tv_sec) * MILLIUNITS +
1425     jlong(ts.tv_nsec) / NANOUNITS_PER_MILLIUNIT;
1426 }
1427 
1428 void os::javaTimeSystemUTC(jlong &seconds, jlong &nanos) {
1429   struct timespec ts;
1430   int status = clock_gettime(CLOCK_REALTIME, &ts);
1431   assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
1432   seconds = jlong(ts.tv_sec);
1433   nanos = jlong(ts.tv_nsec);
1434 }
1435 
1436 // macOS and AIX have platform specific implementations for javaTimeNanos()
1437 // using native clock/timer access APIs. These have historically worked well
1438 // for those platforms, but it may be possible for them to switch to the
1439 // generic clock_gettime mechanism in the future.
1440 #if !defined(__APPLE__) && !defined(AIX)
1441 
1442 jlong os::javaTimeNanos() {
1443   struct timespec tp;
1444   int status = clock_gettime(CLOCK_MONOTONIC, &tp);
1445   assert(status == 0, "clock_gettime error: %s", os::strerror(errno));
1446   jlong result = jlong(tp.tv_sec) * NANOSECS_PER_SEC + jlong(tp.tv_nsec);
1447   return result;
1448 }
1449 
1450 // for timer info max values which include all bits
1451 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
1452 
1453 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
1454   // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
1455   info_ptr->max_value = ALL_64_BITS;
1456   info_ptr->may_skip_backward = false;      // not subject to resetting or drifting
1457   info_ptr->may_skip_forward = false;       // not subject to resetting or drifting
1458   info_ptr->kind = JVMTI_TIMER_ELAPSED;     // elapsed not CPU time
1459 }
1460 #endif // ! APPLE && !AIX
1461 
1462 // Time since start-up in seconds to a fine granularity.
1463 double os::elapsedTime() {
1464   return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
1465 }
1466 
1467 jlong os::elapsed_counter() {
1468   return os::javaTimeNanos() - initial_time_count;
1469 }
1470 
1471 jlong os::elapsed_frequency() {
1472   return NANOSECS_PER_SEC; // nanosecond resolution
1473 }
1474 
1475 bool os::supports_vtime() { return true; }
1476 
1477 // Return the real, user, and system times in seconds from an
1478 // arbitrary fixed point in the past.
1479 bool os::getTimesSecs(double* process_real_time,
1480                       double* process_user_time,
1481                       double* process_system_time) {
1482   struct tms ticks;
1483   clock_t real_ticks = times(&ticks);
1484 
1485   if (real_ticks == (clock_t) (-1)) {
1486     return false;
1487   } else {
1488     double ticks_per_second = (double) clock_tics_per_sec;
1489     *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
1490     *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
1491     *process_real_time = ((double) real_ticks) / ticks_per_second;
1492 
1493     return true;
1494   }
1495 }
1496 
1497 char * os::local_time_string(char *buf, size_t buflen) {
1498   struct tm t;
1499   time_t long_time;
1500   time(&long_time);
1501   localtime_r(&long_time, &t);
1502   jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
1503                t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
1504                t.tm_hour, t.tm_min, t.tm_sec);
1505   return buf;
1506 }
1507 
1508 struct tm* os::localtime_pd(const time_t* clock, struct tm*  res) {
1509   return localtime_r(clock, res);
1510 }
1511 
1512 
1513 // Shared pthread_mutex/cond based PlatformEvent implementation.
1514 // Not currently usable by Solaris.
1515 
1516 
1517 // PlatformEvent
1518 //
1519 // Assumption:
1520 //    Only one parker can exist on an event, which is why we allocate
1521 //    them per-thread. Multiple unparkers can coexist.
1522 //
1523 // _event serves as a restricted-range semaphore.
1524 //   -1 : thread is blocked, i.e. there is a waiter
1525 //    0 : neutral: thread is running or ready,
1526 //        could have been signaled after a wait started
1527 //    1 : signaled - thread is running or ready
1528 //
1529 //    Having three states allows for some detection of bad usage - see
1530 //    comments on unpark().
1531 
1532 os::PlatformEvent::PlatformEvent() {
1533   int status = pthread_cond_init(_cond, _condAttr);
1534   assert_status(status == 0, status, "cond_init");
1535   status = pthread_mutex_init(_mutex, _mutexAttr);
1536   assert_status(status == 0, status, "mutex_init");
1537   _event   = 0;
1538   _nParked = 0;
1539 }
1540 
1541 void os::PlatformEvent::park() {       // AKA "down()"
1542   // Transitions for _event:
1543   //   -1 => -1 : illegal
1544   //    1 =>  0 : pass - return immediately
1545   //    0 => -1 : block; then set _event to 0 before returning
1546 
1547   // Invariant: Only the thread associated with the PlatformEvent
1548   // may call park().
1549   assert(_nParked == 0, "invariant");
1550 
1551   int v;
1552 
1553   // atomically decrement _event
1554   for (;;) {
1555     v = _event;
1556     if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;
1557   }
1558   guarantee(v >= 0, "invariant");
1559 
1560   if (v == 0) { // Do this the hard way by blocking ...
1561     int status = pthread_mutex_lock(_mutex);
1562     assert_status(status == 0, status, "mutex_lock");
1563     guarantee(_nParked == 0, "invariant");
1564     ++_nParked;
1565     while (_event < 0) {
1566       // OS-level "spurious wakeups" are ignored
1567       status = pthread_cond_wait(_cond, _mutex);
1568       assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),
1569                     status, "cond_wait");
1570     }
1571     --_nParked;
1572 
1573     _event = 0;
1574     status = pthread_mutex_unlock(_mutex);
1575     assert_status(status == 0, status, "mutex_unlock");
1576     // Paranoia to ensure our locked and lock-free paths interact
1577     // correctly with each other.
1578     OrderAccess::fence();
1579   }
1580   guarantee(_event >= 0, "invariant");
1581 }
1582 
1583 int os::PlatformEvent::park(jlong millis) {
1584   // Transitions for _event:
1585   //   -1 => -1 : illegal
1586   //    1 =>  0 : pass - return immediately
1587   //    0 => -1 : block; then set _event to 0 before returning
1588 
1589   // Invariant: Only the thread associated with the Event/PlatformEvent
1590   // may call park().
1591   assert(_nParked == 0, "invariant");
1592 
1593   int v;
1594   // atomically decrement _event
1595   for (;;) {
1596     v = _event;
1597     if (Atomic::cmpxchg(&_event, v, v - 1) == v) break;
1598   }
1599   guarantee(v >= 0, "invariant");
1600 
1601   if (v == 0) { // Do this the hard way by blocking ...
1602     struct timespec abst;
1603     to_abstime(&abst, millis_to_nanos_bounded(millis), false, false);
1604 
1605     int ret = OS_TIMEOUT;
1606     int status = pthread_mutex_lock(_mutex);
1607     assert_status(status == 0, status, "mutex_lock");
1608     guarantee(_nParked == 0, "invariant");
1609     ++_nParked;
1610 
1611     while (_event < 0) {
1612       status = pthread_cond_timedwait(_cond, _mutex, &abst);
1613       assert_status(status == 0 || status == ETIMEDOUT,
1614                     status, "cond_timedwait");
1615       // OS-level "spurious wakeups" are ignored unless the archaic
1616       // FilterSpuriousWakeups is set false. That flag should be obsoleted.
1617       if (!FilterSpuriousWakeups) break;
1618       if (status == ETIMEDOUT) break;
1619     }
1620     --_nParked;
1621 
1622     if (_event >= 0) {
1623       ret = OS_OK;
1624     }
1625 
1626     _event = 0;
1627     status = pthread_mutex_unlock(_mutex);
1628     assert_status(status == 0, status, "mutex_unlock");
1629     // Paranoia to ensure our locked and lock-free paths interact
1630     // correctly with each other.
1631     OrderAccess::fence();
1632     return ret;
1633   }
1634   return OS_OK;
1635 }
1636 
1637 void os::PlatformEvent::unpark() {
1638   // Transitions for _event:
1639   //    0 => 1 : just return
1640   //    1 => 1 : just return
1641   //   -1 => either 0 or 1; must signal target thread
1642   //         That is, we can safely transition _event from -1 to either
1643   //         0 or 1.
1644   // See also: "Semaphores in Plan 9" by Mullender & Cox
1645   //
1646   // Note: Forcing a transition from "-1" to "1" on an unpark() means
1647   // that it will take two back-to-back park() calls for the owning
1648   // thread to block. This has the benefit of forcing a spurious return
1649   // from the first park() call after an unpark() call which will help
1650   // shake out uses of park() and unpark() without checking state conditions
1651   // properly. This spurious return doesn't manifest itself in any user code
1652   // but only in the correctly written condition checking loops of ObjectMonitor,
1653   // Mutex/Monitor, and JavaThread::sleep
1654 
1655   if (Atomic::xchg(&_event, 1) >= 0) return;
1656 
1657   int status = pthread_mutex_lock(_mutex);
1658   assert_status(status == 0, status, "mutex_lock");
1659   int anyWaiters = _nParked;
1660   assert(anyWaiters == 0 || anyWaiters == 1, "invariant");
1661   status = pthread_mutex_unlock(_mutex);
1662   assert_status(status == 0, status, "mutex_unlock");
1663 
1664   // Note that we signal() *after* dropping the lock for "immortal" Events.
1665   // This is safe and avoids a common class of futile wakeups.  In rare
1666   // circumstances this can cause a thread to return prematurely from
1667   // cond_{timed}wait() but the spurious wakeup is benign and the victim
1668   // will simply re-test the condition and re-park itself.
1669   // This provides particular benefit if the underlying platform does not
1670   // provide wait morphing.
1671 
1672   if (anyWaiters != 0) {
1673     status = pthread_cond_signal(_cond);
1674     assert_status(status == 0, status, "cond_signal");
1675   }
1676 }
1677 
1678 // JSR166 support
1679 
1680  os::PlatformParker::PlatformParker() : _counter(0), _cur_index(-1) {
1681   int status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);
1682   assert_status(status == 0, status, "cond_init rel");
1683   status = pthread_cond_init(&_cond[ABS_INDEX], NULL);
1684   assert_status(status == 0, status, "cond_init abs");
1685   status = pthread_mutex_init(_mutex, _mutexAttr);
1686   assert_status(status == 0, status, "mutex_init");
1687 }
1688 
1689 os::PlatformParker::~PlatformParker() {
1690   int status = pthread_cond_destroy(&_cond[REL_INDEX]);
1691   assert_status(status == 0, status, "cond_destroy rel");
1692   status = pthread_cond_destroy(&_cond[ABS_INDEX]);
1693   assert_status(status == 0, status, "cond_destroy abs");
1694   status = pthread_mutex_destroy(_mutex);
1695   assert_status(status == 0, status, "mutex_destroy");
1696 }
1697 
1698 // Parker::park decrements count if > 0, else does a condvar wait.  Unpark
1699 // sets count to 1 and signals condvar.  Only one thread ever waits
1700 // on the condvar. Contention seen when trying to park implies that someone
1701 // is unparking you, so don't wait. And spurious returns are fine, so there
1702 // is no need to track notifications.
1703 
1704 void Parker::park(bool isAbsolute, jlong time) {
1705 
1706   // Optional fast-path check:
1707   // Return immediately if a permit is available.
1708   // We depend on Atomic::xchg() having full barrier semantics
1709   // since we are doing a lock-free update to _counter.
1710   if (Atomic::xchg(&_counter, 0) > 0) return;
1711 
1712   JavaThread *jt = JavaThread::current();
1713 
1714   // Optional optimization -- avoid state transitions if there's
1715   // an interrupt pending.
1716   if (jt->is_interrupted(false)) {
1717     return;
1718   }
1719 
1720   // Next, demultiplex/decode time arguments
1721   struct timespec absTime;
1722   if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all
1723     return;
1724   }
1725   if (time > 0) {
1726     to_abstime(&absTime, time, isAbsolute, false);
1727   }
1728 
1729   // Enter safepoint region
1730   // Beware of deadlocks such as 6317397.
1731   // The per-thread Parker:: mutex is a classic leaf-lock.
1732   // In particular a thread must never block on the Threads_lock while
1733   // holding the Parker:: mutex.  If safepoints are pending both the
1734   // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
1735   ThreadBlockInVM tbivm(jt);
1736 
1737   // Can't access interrupt state now that we are _thread_blocked. If we've
1738   // been interrupted since we checked above then _counter will be > 0.
1739 
1740   // Don't wait if cannot get lock since interference arises from
1741   // unparking.
1742   if (pthread_mutex_trylock(_mutex) != 0) {
1743     return;
1744   }
1745 
1746   int status;
1747   if (_counter > 0)  { // no wait needed
1748     _counter = 0;
1749     status = pthread_mutex_unlock(_mutex);
1750     assert_status(status == 0, status, "invariant");
1751     // Paranoia to ensure our locked and lock-free paths interact
1752     // correctly with each other and Java-level accesses.
1753     OrderAccess::fence();
1754     return;
1755   }
1756 
1757   OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
1758 
1759   assert(_cur_index == -1, "invariant");
1760   if (time == 0) {
1761     _cur_index = REL_INDEX; // arbitrary choice when not timed
1762     status = pthread_cond_wait(&_cond[_cur_index], _mutex);
1763     assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),
1764                   status, "cond_wait");
1765   }
1766   else {
1767     _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
1768     status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);
1769     assert_status(status == 0 || status == ETIMEDOUT,
1770                   status, "cond_timedwait");
1771   }
1772   _cur_index = -1;
1773 
1774   _counter = 0;
1775   status = pthread_mutex_unlock(_mutex);
1776   assert_status(status == 0, status, "invariant");
1777   // Paranoia to ensure our locked and lock-free paths interact
1778   // correctly with each other and Java-level accesses.
1779   OrderAccess::fence();
1780 }
1781 
1782 void Parker::unpark() {
1783   int status = pthread_mutex_lock(_mutex);
1784   assert_status(status == 0, status, "invariant");
1785   const int s = _counter;
1786   _counter = 1;
1787   // must capture correct index before unlocking
1788   int index = _cur_index;
1789   status = pthread_mutex_unlock(_mutex);
1790   assert_status(status == 0, status, "invariant");
1791 
1792   // Note that we signal() *after* dropping the lock for "immortal" Events.
1793   // This is safe and avoids a common class of futile wakeups.  In rare
1794   // circumstances this can cause a thread to return prematurely from
1795   // cond_{timed}wait() but the spurious wakeup is benign and the victim
1796   // will simply re-test the condition and re-park itself.
1797   // This provides particular benefit if the underlying platform does not
1798   // provide wait morphing.
1799 
1800   if (s < 1 && index != -1) {
1801     // thread is definitely parked
1802     status = pthread_cond_signal(&_cond[index]);
1803     assert_status(status == 0, status, "invariant");
1804   }
1805 }
1806 
1807 // Platform Mutex/Monitor implementation
1808 
1809 #if PLATFORM_MONITOR_IMPL_INDIRECT
1810 
1811 os::PlatformMutex::Mutex::Mutex() : _next(NULL) {
1812   int status = pthread_mutex_init(&_mutex, _mutexAttr);
1813   assert_status(status == 0, status, "mutex_init");
1814 }
1815 
1816 os::PlatformMutex::Mutex::~Mutex() {
1817   int status = pthread_mutex_destroy(&_mutex);
1818   assert_status(status == 0, status, "mutex_destroy");
1819 }
1820 
1821 pthread_mutex_t os::PlatformMutex::_freelist_lock;
1822 os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;
1823 
1824 void os::PlatformMutex::init() {
1825   int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);
1826   assert_status(status == 0, status, "freelist lock init");
1827 }
1828 
1829 struct os::PlatformMutex::WithFreeListLocked : public StackObj {
1830   WithFreeListLocked() {
1831     int status = pthread_mutex_lock(&_freelist_lock);
1832     assert_status(status == 0, status, "freelist lock");
1833   }
1834 
1835   ~WithFreeListLocked() {
1836     int status = pthread_mutex_unlock(&_freelist_lock);
1837     assert_status(status == 0, status, "freelist unlock");
1838   }
1839 };
1840 
1841 os::PlatformMutex::PlatformMutex() {
1842   {
1843     WithFreeListLocked wfl;
1844     _impl = _mutex_freelist;
1845     if (_impl != NULL) {
1846       _mutex_freelist = _impl->_next;
1847       _impl->_next = NULL;
1848       return;
1849     }
1850   }
1851   _impl = new Mutex();
1852 }
1853 
1854 os::PlatformMutex::~PlatformMutex() {
1855   WithFreeListLocked wfl;
1856   assert(_impl->_next == NULL, "invariant");
1857   _impl->_next = _mutex_freelist;
1858   _mutex_freelist = _impl;
1859 }
1860 
1861 os::PlatformMonitor::Cond::Cond() : _next(NULL) {
1862   int status = pthread_cond_init(&_cond, _condAttr);
1863   assert_status(status == 0, status, "cond_init");
1864 }
1865 
1866 os::PlatformMonitor::Cond::~Cond() {
1867   int status = pthread_cond_destroy(&_cond);
1868   assert_status(status == 0, status, "cond_destroy");
1869 }
1870 
1871 os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;
1872 
1873 os::PlatformMonitor::PlatformMonitor() {
1874   {
1875     WithFreeListLocked wfl;
1876     _impl = _cond_freelist;
1877     if (_impl != NULL) {
1878       _cond_freelist = _impl->_next;
1879       _impl->_next = NULL;
1880       return;
1881     }
1882   }
1883   _impl = new Cond();
1884 }
1885 
1886 os::PlatformMonitor::~PlatformMonitor() {
1887   WithFreeListLocked wfl;
1888   assert(_impl->_next == NULL, "invariant");
1889   _impl->_next = _cond_freelist;
1890   _cond_freelist = _impl;
1891 }
1892 
1893 #else
1894 
1895 os::PlatformMutex::PlatformMutex() {
1896   int status = pthread_mutex_init(&_mutex, _mutexAttr);
1897   assert_status(status == 0, status, "mutex_init");
1898 }
1899 
1900 os::PlatformMutex::~PlatformMutex() {
1901   int status = pthread_mutex_destroy(&_mutex);
1902   assert_status(status == 0, status, "mutex_destroy");
1903 }
1904 
1905 os::PlatformMonitor::PlatformMonitor() {
1906   int status = pthread_cond_init(&_cond, _condAttr);
1907   assert_status(status == 0, status, "cond_init");
1908 }
1909 
1910 os::PlatformMonitor::~PlatformMonitor() {
1911   int status = pthread_cond_destroy(&_cond);
1912   assert_status(status == 0, status, "cond_destroy");
1913 }
1914 
1915 #endif // PLATFORM_MONITOR_IMPL_INDIRECT
1916 
1917 // Must already be locked
1918 int os::PlatformMonitor::wait(jlong millis) {
1919   assert(millis >= 0, "negative timeout");
1920   if (millis > 0) {
1921     struct timespec abst;
1922     // We have to watch for overflow when converting millis to nanos,
1923     // but if millis is that large then we will end up limiting to
1924     // MAX_SECS anyway, so just do that here.
1925     if (millis / MILLIUNITS > MAX_SECS) {
1926       millis = jlong(MAX_SECS) * MILLIUNITS;
1927     }
1928     to_abstime(&abst, millis_to_nanos(millis), false, false);
1929 
1930     int ret = OS_TIMEOUT;
1931     int status = pthread_cond_timedwait(cond(), mutex(), &abst);
1932     assert_status(status == 0 || status == ETIMEDOUT,
1933                   status, "cond_timedwait");
1934     if (status == 0) {
1935       ret = OS_OK;
1936     }
1937     return ret;
1938   } else {
1939     int status = pthread_cond_wait(cond(), mutex());
1940     assert_status(status == 0 MACOS_ONLY(|| status == ETIMEDOUT),
1941                   status, "cond_wait");
1942     return OS_OK;
1943   }
1944 }
1945 
1946 // Darwin has no "environ" in a dynamic library.
1947 #ifdef __APPLE__
1948   #define environ (*_NSGetEnviron())
1949 #else
1950   extern char** environ;
1951 #endif
1952 
1953 char** os::get_environ() { return environ; }
1954 
1955 // Run the specified command in a separate process. Return its exit value,
1956 // or -1 on failure (e.g. can't fork a new process).
1957 // Notes: -Unlike system(), this function can be called from signal handler. It
1958 //         doesn't block SIGINT et al.
1959 //        -this function is unsafe to use in non-error situations, mainly
1960 //         because the child process will inherit all parent descriptors.
1961 int os::fork_and_exec(const char* cmd, bool prefer_vfork) {
1962   const char * argv[4] = {"sh", "-c", cmd, NULL};
1963 
1964   pid_t pid ;
1965 
1966   char** env = os::get_environ();
1967 
1968   // Use always vfork on AIX, since its safe and helps with analyzing OOM situations.
1969   // Otherwise leave it up to the caller.
1970   AIX_ONLY(prefer_vfork = true;)
1971   #ifdef __APPLE__
1972   pid = ::fork();
1973   #else
1974   pid = prefer_vfork ? ::vfork() : ::fork();
1975   #endif
1976 
1977   if (pid < 0) {
1978     // fork failed
1979     return -1;
1980 
1981   } else if (pid == 0) {
1982     // child process
1983 
1984     ::execve("/bin/sh", (char* const*)argv, env);
1985 
1986     // execve failed
1987     ::_exit(-1);
1988 
1989   } else  {
1990     // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
1991     // care about the actual exit code, for now.
1992 
1993     int status;
1994 
1995     // Wait for the child process to exit.  This returns immediately if
1996     // the child has already exited. */
1997     while (::waitpid(pid, &status, 0) < 0) {
1998       switch (errno) {
1999       case ECHILD: return 0;
2000       case EINTR: break;
2001       default: return -1;
2002       }
2003     }
2004 
2005     if (WIFEXITED(status)) {
2006       // The child exited normally; get its exit code.
2007       return WEXITSTATUS(status);
2008     } else if (WIFSIGNALED(status)) {
2009       // The child exited because of a signal
2010       // The best value to return is 0x80 + signal number,
2011       // because that is what all Unix shells do, and because
2012       // it allows callers to distinguish between process exit and
2013       // process death by signal.
2014       return 0x80 + WTERMSIG(status);
2015     } else {
2016       // Unknown exit code; pass it through
2017       return status;
2018     }
2019   }
2020 }
2021 
2022 ////////////////////////////////////////////////////////////////////////////////
2023 // runtime exit support
2024 
2025 // Note: os::shutdown() might be called very early during initialization, or
2026 // called from signal handler. Before adding something to os::shutdown(), make
2027 // sure it is async-safe and can handle partially initialized VM.
2028 void os::shutdown() {
2029 
2030   // allow PerfMemory to attempt cleanup of any persistent resources
2031   perfMemory_exit();
2032 
2033   // needs to remove object in file system
2034   AttachListener::abort();
2035 
2036   // flush buffered output, finish log files
2037   ostream_abort();
2038 
2039   // Check for abort hook
2040   abort_hook_t abort_hook = Arguments::abort_hook();
2041   if (abort_hook != NULL) {
2042     abort_hook();
2043   }
2044 
2045 }
2046 
2047 // Note: os::abort() might be called very early during initialization, or
2048 // called from signal handler. Before adding something to os::abort(), make
2049 // sure it is async-safe and can handle partially initialized VM.
2050 // Also note we can abort while other threads continue to run, so we can
2051 // easily trigger secondary faults in those threads. To reduce the likelihood
2052 // of that we use _exit rather than exit, so that no atexit hooks get run.
2053 // But note that os::shutdown() could also trigger secondary faults.
2054 void os::abort(bool dump_core, void* siginfo, const void* context) {
2055   os::shutdown();
2056   if (dump_core) {
2057     LINUX_ONLY(if (DumpPrivateMappingsInCore) ClassLoader::close_jrt_image();)
2058     ::abort(); // dump core
2059   }
2060   ::_exit(1);
2061 }
2062 
2063 // Die immediately, no exit hook, no abort hook, no cleanup.
2064 // Dump a core file, if possible, for debugging.
2065 void os::die() {
2066   if (TestUnresponsiveErrorHandler && !CreateCoredumpOnCrash) {
2067     // For TimeoutInErrorHandlingTest.java, we just kill the VM
2068     // and don't take the time to generate a core file.
2069     os::signal_raise(SIGKILL);
2070   } else {
2071     ::abort();
2072   }
2073 }