1 /*
   2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "jvm.h"
  26 #include "logging/log.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "os_posix.inline.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 #include "runtime/frame.inline.hpp"
  31 #include "runtime/interfaceSupport.inline.hpp"
  32 #include "services/memTracker.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/events.hpp"
  35 #include "utilities/formatBuffer.hpp"
  36 #include "utilities/macros.hpp"
  37 #include "utilities/vmError.hpp"
  38 
  39 #include <dirent.h>
  40 #include <dlfcn.h>
  41 #include <grp.h>
  42 #include <pwd.h>
  43 #include <pthread.h>
  44 #include <signal.h>
  45 #include <sys/mman.h>
  46 #include <sys/resource.h>
  47 #include <sys/utsname.h>
  48 #include <time.h>
  49 #include <unistd.h>
  50 
  51 // Todo: provide a os::get_max_process_id() or similar. Number of processes
  52 // may have been configured, can be read more accurately from proc fs etc.
  53 #ifndef MAX_PID
  54 #define MAX_PID INT_MAX
  55 #endif
  56 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
  57 
  58 #define ROOT_UID 0
  59 
  60 #ifndef MAP_ANONYMOUS
  61   #define MAP_ANONYMOUS MAP_ANON
  62 #endif
  63 
  64 #define check_with_errno(check_type, cond, msg)                             \
  65   do {                                                                      \
  66     int err = errno;                                                        \
  67     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  68                os::errno_name(err));                                        \
  69 } while (false)
  70 
  71 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  72 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  73 
  74 // Check core dump limit and report possible place where core can be found
  75 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  76   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  77     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
  78     VMError::record_coredump_status(buffer, false);
  79     return;
  80   }
  81 
  82   int n;
  83   struct rlimit rlim;
  84   bool success;
  85 
  86   char core_path[PATH_MAX];
  87   n = get_core_path(core_path, PATH_MAX);
  88 
  89   if (n <= 0) {
  90     jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());
  91     success = true;
  92 #ifdef LINUX
  93   } else if (core_path[0] == '"') { // redirect to user process
  94     jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);
  95     success = true;
  96 #endif
  97   } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
  98     jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);
  99     success = true;
 100   } else {
 101     switch(rlim.rlim_cur) {
 102       case RLIM_INFINITY:
 103         jio_snprintf(buffer, bufferSize, "%s", core_path);
 104         success = true;
 105         break;
 106       case 0:
 107         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
 108         success = false;
 109         break;
 110       default:
 111         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);
 112         success = true;
 113         break;
 114     }
 115   }
 116 
 117   VMError::record_coredump_status(buffer, success);
 118 }
 119 
 120 int os::get_native_stack(address* stack, int frames, int toSkip) {
 121   int frame_idx = 0;
 122   int num_of_frames;  // number of frames captured
 123   frame fr = os::current_frame();
 124   while (fr.pc() && frame_idx < frames) {
 125     if (toSkip > 0) {
 126       toSkip --;
 127     } else {
 128       stack[frame_idx ++] = fr.pc();
 129     }
 130     if (fr.fp() == NULL || fr.cb() != NULL ||
 131         fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;
 132 
 133     if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
 134       fr = os::get_sender_for_C_frame(&fr);
 135     } else {
 136       break;
 137     }
 138   }
 139   num_of_frames = frame_idx;
 140   for (; frame_idx < frames; frame_idx ++) {
 141     stack[frame_idx] = NULL;
 142   }
 143 
 144   return num_of_frames;
 145 }
 146 
 147 
 148 bool os::unsetenv(const char* name) {
 149   assert(name != NULL, "Null pointer");
 150   return (::unsetenv(name) == 0);
 151 }
 152 
 153 int os::get_last_error() {
 154   return errno;
 155 }
 156 
 157 size_t os::lasterror(char *buf, size_t len) {
 158   if (errno == 0)  return 0;
 159 
 160   const char *s = os::strerror(errno);
 161   size_t n = ::strlen(s);
 162   if (n >= len) {
 163     n = len - 1;
 164   }
 165   ::strncpy(buf, s, n);
 166   buf[n] = '\0';
 167   return n;
 168 }
 169 
 170 bool os::is_debugger_attached() {
 171   // not implemented
 172   return false;
 173 }
 174 
 175 void os::wait_for_keypress_at_exit(void) {
 176   // don't do anything on posix platforms
 177   return;
 178 }
 179 
 180 int os::create_file_for_heap(const char* dir) {
 181 
 182   const char name_template[] = "/jvmheap.XXXXXX";
 183 
 184   size_t fullname_len = strlen(dir) + strlen(name_template);
 185   char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);
 186   if (fullname == NULL) {
 187     vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 188     return -1;
 189   }
 190   int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);
 191   assert((size_t)n == fullname_len, "Unexpected number of characters in string");
 192 
 193   os::native_path(fullname);
 194 
 195   // set the file creation mask.
 196   mode_t file_mode = S_IRUSR | S_IWUSR;
 197 
 198   // create a new file.
 199   int fd = mkstemp(fullname);
 200 
 201   if (fd < 0) {
 202     warning("Could not create file for heap with template %s", fullname);
 203     os::free(fullname);
 204     return -1;
 205   }
 206 
 207   // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 208   int ret = unlink(fullname);
 209   assert_with_errno(ret == 0, "unlink returned error");
 210 
 211   os::free(fullname);
 212   return fd;
 213 }
 214 
 215 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 216   char * addr;
 217   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 218   if (requested_addr != NULL) {
 219     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 220     flags |= MAP_FIXED;
 221   }
 222 
 223   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 224   // touch an uncommitted page. Otherwise, the read/write might
 225   // succeed if we have enough swap space to back the physical page.
 226   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 227                        flags, -1, 0);
 228 
 229   if (addr != MAP_FAILED) {
 230     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 231     return addr;
 232   }
 233   return NULL;
 234 }
 235 
 236 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
 237 #ifdef __APPLE__
 238   fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
 239   // First we try to get a continuous chunk of disk space
 240   int ret = fcntl(fd, F_PREALLOCATE, &store);
 241   if (ret == -1) {
 242     // Maybe we are too fragmented, try to allocate non-continuous range
 243     store.fst_flags = F_ALLOCATEALL;
 244     ret = fcntl(fd, F_PREALLOCATE, &store);
 245   }
 246   if(ret != -1) {
 247     return ftruncate(fd, len);
 248   }
 249   return -1;
 250 #else
 251   return posix_fallocate(fd, offset, len);
 252 #endif
 253 }
 254 
 255 // Map the given address range to the provided file descriptor.
 256 char* os::map_memory_to_file(char* base, size_t size, int fd) {
 257   assert(fd != -1, "File descriptor is not valid");
 258 
 259   // allocate space for the file
 260   int ret = util_posix_fallocate(fd, 0, (off_t)size);
 261   if (ret != 0) {
 262     vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));
 263     return NULL;
 264   }
 265 
 266   int prot = PROT_READ | PROT_WRITE;
 267   int flags = MAP_SHARED;
 268   if (base != NULL) {
 269     flags |= MAP_FIXED;
 270   }
 271   char* addr = (char*)mmap(base, size, prot, flags, fd, 0);
 272 
 273   if (addr == MAP_FAILED) {
 274     warning("Failed mmap to file. (%s)", os::strerror(errno));
 275     return NULL;
 276   }
 277   if (base != NULL && addr != base) {
 278     if (!os::release_memory(addr, size)) {
 279       warning("Could not release memory on unsuccessful file mapping");
 280     }
 281     return NULL;
 282   }
 283   return addr;
 284 }
 285 
 286 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
 287   assert(fd != -1, "File descriptor is not valid");
 288   assert(base != NULL, "Base cannot be NULL");
 289 
 290   return map_memory_to_file(base, size, fd);
 291 }
 292 
 293 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 294 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 295 // rather than unmapping and remapping the whole chunk to get requested alignment.
 296 char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
 297   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 298       "Alignment must be a multiple of allocation granularity (page size)");
 299   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 300 
 301   size_t extra_size = size + alignment;
 302   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 303 
 304   char* extra_base;
 305   if (file_desc != -1) {
 306     // For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because
 307     // we need to deal with shrinking of the file space later when we release extra memory after alignment.
 308     // We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory.
 309     // So here to call a helper function while reserve memory for us. After we have a aligned base,
 310     // we will replace anonymous mapping with file mapping.
 311     extra_base = reserve_mmapped_memory(extra_size, NULL);
 312     if (extra_base != NULL) {
 313       MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC);
 314     }
 315   } else {
 316     extra_base = os::reserve_memory(extra_size, NULL, alignment);
 317   }
 318 
 319   if (extra_base == NULL) {
 320     return NULL;
 321   }
 322 
 323   // Do manual alignment
 324   char* aligned_base = align_up(extra_base, alignment);
 325 
 326   // [  |                                       |  ]
 327   // ^ extra_base
 328   //    ^ extra_base + begin_offset == aligned_base
 329   //     extra_base + begin_offset + size       ^
 330   //                       extra_base + extra_size ^
 331   // |<>| == begin_offset
 332   //                              end_offset == |<>|
 333   size_t begin_offset = aligned_base - extra_base;
 334   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 335 
 336   if (begin_offset > 0) {
 337       os::release_memory(extra_base, begin_offset);
 338   }
 339 
 340   if (end_offset > 0) {
 341       os::release_memory(extra_base + begin_offset + size, end_offset);
 342   }
 343 
 344   if (file_desc != -1) {
 345     // After we have an aligned address, we can replace anonymous mapping with file mapping
 346     if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {
 347       vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
 348     }
 349     MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);
 350   }
 351   return aligned_base;
 352 }
 353 
 354 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
 355   // All supported POSIX platforms provide C99 semantics.
 356   int result = ::vsnprintf(buf, len, fmt, args);
 357   // If an encoding error occurred (result < 0) then it's not clear
 358   // whether the buffer is NUL terminated, so ensure it is.
 359   if ((result < 0) && (len > 0)) {
 360     buf[len - 1] = '\0';
 361   }
 362   return result;
 363 }
 364 
 365 int os::get_fileno(FILE* fp) {
 366   return NOT_AIX(::)fileno(fp);
 367 }
 368 
 369 struct tm* os::gmtime_pd(const time_t* clock, struct tm*  res) {
 370   return gmtime_r(clock, res);
 371 }
 372 
 373 void os::Posix::print_load_average(outputStream* st) {
 374   st->print("load average:");
 375   double loadavg[3];
 376   int res = os::loadavg(loadavg, 3);
 377   if (res != -1) {
 378     st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 379   } else {
 380     st->print(" Unavailable");
 381   }
 382   st->cr();
 383 }
 384 
 385 void os::Posix::print_rlimit_info(outputStream* st) {
 386   st->print("rlimit:");
 387   struct rlimit rlim;
 388 
 389   st->print(" STACK ");
 390   getrlimit(RLIMIT_STACK, &rlim);
 391   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 392   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 393 
 394   st->print(", CORE ");
 395   getrlimit(RLIMIT_CORE, &rlim);
 396   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 397   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 398 
 399   // Isn't there on solaris
 400 #if defined(AIX)
 401   st->print(", NPROC ");
 402   st->print("%d", sysconf(_SC_CHILD_MAX));
 403 #elif !defined(SOLARIS)
 404   st->print(", NPROC ");
 405   getrlimit(RLIMIT_NPROC, &rlim);
 406   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 407   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 408 #endif
 409 
 410   st->print(", NOFILE ");
 411   getrlimit(RLIMIT_NOFILE, &rlim);
 412   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 413   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 414 
 415   st->print(", AS ");
 416   getrlimit(RLIMIT_AS, &rlim);
 417   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 418   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 419 
 420   st->print(", DATA ");
 421   getrlimit(RLIMIT_DATA, &rlim);
 422   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 423   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 424 
 425   st->print(", FSIZE ");
 426   getrlimit(RLIMIT_FSIZE, &rlim);
 427   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 428   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 429 
 430   st->cr();
 431 }
 432 
 433 void os::Posix::print_uname_info(outputStream* st) {
 434   // kernel
 435   st->print("uname:");
 436   struct utsname name;
 437   uname(&name);
 438   st->print("%s ", name.sysname);
 439 #ifdef ASSERT
 440   st->print("%s ", name.nodename);
 441 #endif
 442   st->print("%s ", name.release);
 443   st->print("%s ", name.version);
 444   st->print("%s", name.machine);
 445   st->cr();
 446 }
 447 
 448 void os::Posix::print_umask(outputStream* st, mode_t umsk) {
 449   st->print((umsk & S_IRUSR) ? "r" : "-");
 450   st->print((umsk & S_IWUSR) ? "w" : "-");
 451   st->print((umsk & S_IXUSR) ? "x" : "-");
 452   st->print((umsk & S_IRGRP) ? "r" : "-");
 453   st->print((umsk & S_IWGRP) ? "w" : "-");
 454   st->print((umsk & S_IXGRP) ? "x" : "-");
 455   st->print((umsk & S_IROTH) ? "r" : "-");
 456   st->print((umsk & S_IWOTH) ? "w" : "-");
 457   st->print((umsk & S_IXOTH) ? "x" : "-");
 458 }
 459 
 460 void os::Posix::print_user_info(outputStream* st) {
 461   unsigned id = (unsigned) ::getuid();
 462   st->print("uid  : %u ", id);
 463   id = (unsigned) ::geteuid();
 464   st->print("euid : %u ", id);
 465   id = (unsigned) ::getgid();
 466   st->print("gid  : %u ", id);
 467   id = (unsigned) ::getegid();
 468   st->print_cr("egid : %u", id);
 469   st->cr();
 470 
 471   mode_t umsk = ::umask(0);
 472   ::umask(umsk);
 473   st->print("umask: %04o (", (unsigned) umsk);
 474   print_umask(st, umsk);
 475   st->print_cr(")");
 476   st->cr();
 477 }
 478 
 479 
 480 bool os::get_host_name(char* buf, size_t buflen) {
 481   struct utsname name;
 482   uname(&name);
 483   jio_snprintf(buf, buflen, "%s", name.nodename);
 484   return true;
 485 }
 486 
 487 bool os::has_allocatable_memory_limit(julong* limit) {
 488   struct rlimit rlim;
 489   int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
 490   // if there was an error when calling getrlimit, assume that there is no limitation
 491   // on virtual memory.
 492   bool result;
 493   if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
 494     result = false;
 495   } else {
 496     *limit = (julong)rlim.rlim_cur;
 497     result = true;
 498   }
 499 #ifdef _LP64
 500   return result;
 501 #else
 502   // arbitrary virtual space limit for 32 bit Unices found by testing. If
 503   // getrlimit above returned a limit, bound it with this limit. Otherwise
 504   // directly use it.
 505   const julong max_virtual_limit = (julong)3800*M;
 506   if (result) {
 507     *limit = MIN2(*limit, max_virtual_limit);
 508   } else {
 509     *limit = max_virtual_limit;
 510   }
 511 
 512   // bound by actually allocatable memory. The algorithm uses two bounds, an
 513   // upper and a lower limit. The upper limit is the current highest amount of
 514   // memory that could not be allocated, the lower limit is the current highest
 515   // amount of memory that could be allocated.
 516   // The algorithm iteratively refines the result by halving the difference
 517   // between these limits, updating either the upper limit (if that value could
 518   // not be allocated) or the lower limit (if the that value could be allocated)
 519   // until the difference between these limits is "small".
 520 
 521   // the minimum amount of memory we care about allocating.
 522   const julong min_allocation_size = M;
 523 
 524   julong upper_limit = *limit;
 525 
 526   // first check a few trivial cases
 527   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 528     *limit = upper_limit;
 529   } else if (!is_allocatable(min_allocation_size)) {
 530     // we found that not even min_allocation_size is allocatable. Return it
 531     // anyway. There is no point to search for a better value any more.
 532     *limit = min_allocation_size;
 533   } else {
 534     // perform the binary search.
 535     julong lower_limit = min_allocation_size;
 536     while ((upper_limit - lower_limit) > min_allocation_size) {
 537       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 538       temp_limit = align_down(temp_limit, min_allocation_size);
 539       if (is_allocatable(temp_limit)) {
 540         lower_limit = temp_limit;
 541       } else {
 542         upper_limit = temp_limit;
 543       }
 544     }
 545     *limit = lower_limit;
 546   }
 547   return true;
 548 #endif
 549 }
 550 
 551 const char* os::get_current_directory(char *buf, size_t buflen) {
 552   return getcwd(buf, buflen);
 553 }
 554 
 555 FILE* os::open(int fd, const char* mode) {
 556   return ::fdopen(fd, mode);
 557 }
 558 
 559 ssize_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
 560   return ::pread(fd, buf, nBytes, offset);
 561 }
 562 
 563 void os::flockfile(FILE* fp) {
 564   ::flockfile(fp);
 565 }
 566 
 567 void os::funlockfile(FILE* fp) {
 568   ::funlockfile(fp);
 569 }
 570 
 571 DIR* os::opendir(const char* dirname) {
 572   assert(dirname != NULL, "just checking");
 573   return ::opendir(dirname);
 574 }
 575 
 576 struct dirent* os::readdir(DIR* dirp) {
 577   assert(dirp != NULL, "just checking");
 578   return ::readdir(dirp);
 579 }
 580 
 581 int os::closedir(DIR *dirp) {
 582   assert(dirp != NULL, "just checking");
 583   return ::closedir(dirp);
 584 }
 585 
 586 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
 587 // which is used to find statically linked in agents.
 588 // Parameters:
 589 //            sym_name: Symbol in library we are looking for
 590 //            lib_name: Name of library to look in, NULL for shared libs.
 591 //            is_absolute_path == true if lib_name is absolute path to agent
 592 //                                     such as "/a/b/libL.so"
 593 //            == false if only the base name of the library is passed in
 594 //               such as "L"
 595 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
 596                                     bool is_absolute_path) {
 597   char *agent_entry_name;
 598   size_t len;
 599   size_t name_len;
 600   size_t prefix_len = strlen(JNI_LIB_PREFIX);
 601   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
 602   const char *start;
 603 
 604   if (lib_name != NULL) {
 605     name_len = strlen(lib_name);
 606     if (is_absolute_path) {
 607       // Need to strip path, prefix and suffix
 608       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
 609         lib_name = ++start;
 610       }
 611       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 612         return NULL;
 613       }
 614       lib_name += prefix_len;
 615       name_len = strlen(lib_name) - suffix_len;
 616     }
 617   }
 618   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 619   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 620   if (agent_entry_name == NULL) {
 621     return NULL;
 622   }
 623   strcpy(agent_entry_name, sym_name);
 624   if (lib_name != NULL) {
 625     strcat(agent_entry_name, "_");
 626     strncat(agent_entry_name, lib_name, name_len);
 627   }
 628   return agent_entry_name;
 629 }
 630 
 631 
 632 void os::naked_short_nanosleep(jlong ns) {
 633   struct timespec req;
 634   assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
 635   req.tv_sec = 0;
 636   req.tv_nsec = ns;
 637   ::nanosleep(&req, NULL);
 638   return;
 639 }
 640 
 641 void os::naked_short_sleep(jlong ms) {
 642   assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");
 643   os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS));
 644   return;
 645 }
 646 
 647 static const struct {
 648   int sig; const char* name;
 649 }
 650  g_signal_info[] =
 651   {
 652   {  SIGABRT,     "SIGABRT" },
 653 #ifdef SIGAIO
 654   {  SIGAIO,      "SIGAIO" },
 655 #endif
 656   {  SIGALRM,     "SIGALRM" },
 657 #ifdef SIGALRM1
 658   {  SIGALRM1,    "SIGALRM1" },
 659 #endif
 660   {  SIGBUS,      "SIGBUS" },
 661 #ifdef SIGCANCEL
 662   {  SIGCANCEL,   "SIGCANCEL" },
 663 #endif
 664   {  SIGCHLD,     "SIGCHLD" },
 665 #ifdef SIGCLD
 666   {  SIGCLD,      "SIGCLD" },
 667 #endif
 668   {  SIGCONT,     "SIGCONT" },
 669 #ifdef SIGCPUFAIL
 670   {  SIGCPUFAIL,  "SIGCPUFAIL" },
 671 #endif
 672 #ifdef SIGDANGER
 673   {  SIGDANGER,   "SIGDANGER" },
 674 #endif
 675 #ifdef SIGDIL
 676   {  SIGDIL,      "SIGDIL" },
 677 #endif
 678 #ifdef SIGEMT
 679   {  SIGEMT,      "SIGEMT" },
 680 #endif
 681   {  SIGFPE,      "SIGFPE" },
 682 #ifdef SIGFREEZE
 683   {  SIGFREEZE,   "SIGFREEZE" },
 684 #endif
 685 #ifdef SIGGFAULT
 686   {  SIGGFAULT,   "SIGGFAULT" },
 687 #endif
 688 #ifdef SIGGRANT
 689   {  SIGGRANT,    "SIGGRANT" },
 690 #endif
 691   {  SIGHUP,      "SIGHUP" },
 692   {  SIGILL,      "SIGILL" },
 693   {  SIGINT,      "SIGINT" },
 694 #ifdef SIGIO
 695   {  SIGIO,       "SIGIO" },
 696 #endif
 697 #ifdef SIGIOINT
 698   {  SIGIOINT,    "SIGIOINT" },
 699 #endif
 700 #ifdef SIGIOT
 701 // SIGIOT is there for BSD compatibility, but on most Unices just a
 702 // synonym for SIGABRT. The result should be "SIGABRT", not
 703 // "SIGIOT".
 704 #if (SIGIOT != SIGABRT )
 705   {  SIGIOT,      "SIGIOT" },
 706 #endif
 707 #endif
 708 #ifdef SIGKAP
 709   {  SIGKAP,      "SIGKAP" },
 710 #endif
 711   {  SIGKILL,     "SIGKILL" },
 712 #ifdef SIGLOST
 713   {  SIGLOST,     "SIGLOST" },
 714 #endif
 715 #ifdef SIGLWP
 716   {  SIGLWP,      "SIGLWP" },
 717 #endif
 718 #ifdef SIGLWPTIMER
 719   {  SIGLWPTIMER, "SIGLWPTIMER" },
 720 #endif
 721 #ifdef SIGMIGRATE
 722   {  SIGMIGRATE,  "SIGMIGRATE" },
 723 #endif
 724 #ifdef SIGMSG
 725   {  SIGMSG,      "SIGMSG" },
 726 #endif
 727   {  SIGPIPE,     "SIGPIPE" },
 728 #ifdef SIGPOLL
 729   {  SIGPOLL,     "SIGPOLL" },
 730 #endif
 731 #ifdef SIGPRE
 732   {  SIGPRE,      "SIGPRE" },
 733 #endif
 734   {  SIGPROF,     "SIGPROF" },
 735 #ifdef SIGPTY
 736   {  SIGPTY,      "SIGPTY" },
 737 #endif
 738 #ifdef SIGPWR
 739   {  SIGPWR,      "SIGPWR" },
 740 #endif
 741   {  SIGQUIT,     "SIGQUIT" },
 742 #ifdef SIGRECONFIG
 743   {  SIGRECONFIG, "SIGRECONFIG" },
 744 #endif
 745 #ifdef SIGRECOVERY
 746   {  SIGRECOVERY, "SIGRECOVERY" },
 747 #endif
 748 #ifdef SIGRESERVE
 749   {  SIGRESERVE,  "SIGRESERVE" },
 750 #endif
 751 #ifdef SIGRETRACT
 752   {  SIGRETRACT,  "SIGRETRACT" },
 753 #endif
 754 #ifdef SIGSAK
 755   {  SIGSAK,      "SIGSAK" },
 756 #endif
 757   {  SIGSEGV,     "SIGSEGV" },
 758 #ifdef SIGSOUND
 759   {  SIGSOUND,    "SIGSOUND" },
 760 #endif
 761 #ifdef SIGSTKFLT
 762   {  SIGSTKFLT,    "SIGSTKFLT" },
 763 #endif
 764   {  SIGSTOP,     "SIGSTOP" },
 765   {  SIGSYS,      "SIGSYS" },
 766 #ifdef SIGSYSERROR
 767   {  SIGSYSERROR, "SIGSYSERROR" },
 768 #endif
 769 #ifdef SIGTALRM
 770   {  SIGTALRM,    "SIGTALRM" },
 771 #endif
 772   {  SIGTERM,     "SIGTERM" },
 773 #ifdef SIGTHAW
 774   {  SIGTHAW,     "SIGTHAW" },
 775 #endif
 776   {  SIGTRAP,     "SIGTRAP" },
 777 #ifdef SIGTSTP
 778   {  SIGTSTP,     "SIGTSTP" },
 779 #endif
 780   {  SIGTTIN,     "SIGTTIN" },
 781   {  SIGTTOU,     "SIGTTOU" },
 782 #ifdef SIGURG
 783   {  SIGURG,      "SIGURG" },
 784 #endif
 785   {  SIGUSR1,     "SIGUSR1" },
 786   {  SIGUSR2,     "SIGUSR2" },
 787 #ifdef SIGVIRT
 788   {  SIGVIRT,     "SIGVIRT" },
 789 #endif
 790   {  SIGVTALRM,   "SIGVTALRM" },
 791 #ifdef SIGWAITING
 792   {  SIGWAITING,  "SIGWAITING" },
 793 #endif
 794 #ifdef SIGWINCH
 795   {  SIGWINCH,    "SIGWINCH" },
 796 #endif
 797 #ifdef SIGWINDOW
 798   {  SIGWINDOW,   "SIGWINDOW" },
 799 #endif
 800   {  SIGXCPU,     "SIGXCPU" },
 801   {  SIGXFSZ,     "SIGXFSZ" },
 802 #ifdef SIGXRES
 803   {  SIGXRES,     "SIGXRES" },
 804 #endif
 805   { -1, NULL }
 806 };
 807 
 808 // Returned string is a constant. For unknown signals "UNKNOWN" is returned.
 809 const char* os::Posix::get_signal_name(int sig, char* out, size_t outlen) {
 810 
 811   const char* ret = NULL;
 812 
 813 #ifdef SIGRTMIN
 814   if (sig >= SIGRTMIN && sig <= SIGRTMAX) {
 815     if (sig == SIGRTMIN) {
 816       ret = "SIGRTMIN";
 817     } else if (sig == SIGRTMAX) {
 818       ret = "SIGRTMAX";
 819     } else {
 820       jio_snprintf(out, outlen, "SIGRTMIN+%d", sig - SIGRTMIN);
 821       return out;
 822     }
 823   }
 824 #endif
 825 
 826   if (sig > 0) {
 827     for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
 828       if (g_signal_info[idx].sig == sig) {
 829         ret = g_signal_info[idx].name;
 830         break;
 831       }
 832     }
 833   }
 834 
 835   if (!ret) {
 836     if (!is_valid_signal(sig)) {
 837       ret = "INVALID";
 838     } else {
 839       ret = "UNKNOWN";
 840     }
 841   }
 842 
 843   if (out && outlen > 0) {
 844     strncpy(out, ret, outlen);
 845     out[outlen - 1] = '\0';
 846   }
 847   return out;
 848 }
 849 
 850 int os::Posix::get_signal_number(const char* signal_name) {
 851   char tmp[30];
 852   const char* s = signal_name;
 853   if (s[0] != 'S' || s[1] != 'I' || s[2] != 'G') {
 854     jio_snprintf(tmp, sizeof(tmp), "SIG%s", signal_name);
 855     s = tmp;
 856   }
 857   for (int idx = 0; g_signal_info[idx].sig != -1; idx ++) {
 858     if (strcmp(g_signal_info[idx].name, s) == 0) {
 859       return g_signal_info[idx].sig;
 860     }
 861   }
 862   return -1;
 863 }
 864 
 865 int os::get_signal_number(const char* signal_name) {
 866   return os::Posix::get_signal_number(signal_name);
 867 }
 868 
 869 // Returns true if signal number is valid.
 870 bool os::Posix::is_valid_signal(int sig) {
 871   // MacOS not really POSIX compliant: sigaddset does not return
 872   // an error for invalid signal numbers. However, MacOS does not
 873   // support real time signals and simply seems to have just 33
 874   // signals with no holes in the signal range.
 875 #ifdef __APPLE__
 876   return sig >= 1 && sig < NSIG;
 877 #else
 878   // Use sigaddset to check for signal validity.
 879   sigset_t set;
 880   sigemptyset(&set);
 881   if (sigaddset(&set, sig) == -1 && errno == EINVAL) {
 882     return false;
 883   }
 884   return true;
 885 #endif
 886 }
 887 
 888 bool os::Posix::is_sig_ignored(int sig) {
 889   struct sigaction oact;
 890   sigaction(sig, (struct sigaction*)NULL, &oact);
 891   void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*,  oact.sa_sigaction)
 892                                  : CAST_FROM_FN_PTR(void*,  oact.sa_handler);
 893   if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN)) {
 894     return true;
 895   } else {
 896     return false;
 897   }
 898 }
 899 
 900 // Returns:
 901 // NULL for an invalid signal number
 902 // "SIG<num>" for a valid but unknown signal number
 903 // signal name otherwise.
 904 const char* os::exception_name(int sig, char* buf, size_t size) {
 905   if (!os::Posix::is_valid_signal(sig)) {
 906     return NULL;
 907   }
 908   const char* const name = os::Posix::get_signal_name(sig, buf, size);
 909   if (strcmp(name, "UNKNOWN") == 0) {
 910     jio_snprintf(buf, size, "SIG%d", sig);
 911   }
 912   return buf;
 913 }
 914 
 915 #define NUM_IMPORTANT_SIGS 32
 916 // Returns one-line short description of a signal set in a user provided buffer.
 917 const char* os::Posix::describe_signal_set_short(const sigset_t* set, char* buffer, size_t buf_size) {
 918   assert(buf_size == (NUM_IMPORTANT_SIGS + 1), "wrong buffer size");
 919   // Note: for shortness, just print out the first 32. That should
 920   // cover most of the useful ones, apart from realtime signals.
 921   for (int sig = 1; sig <= NUM_IMPORTANT_SIGS; sig++) {
 922     const int rc = sigismember(set, sig);
 923     if (rc == -1 && errno == EINVAL) {
 924       buffer[sig-1] = '?';
 925     } else {
 926       buffer[sig-1] = rc == 0 ? '0' : '1';
 927     }
 928   }
 929   buffer[NUM_IMPORTANT_SIGS] = 0;
 930   return buffer;
 931 }
 932 
 933 // Prints one-line description of a signal set.
 934 void os::Posix::print_signal_set_short(outputStream* st, const sigset_t* set) {
 935   char buf[NUM_IMPORTANT_SIGS + 1];
 936   os::Posix::describe_signal_set_short(set, buf, sizeof(buf));
 937   st->print("%s", buf);
 938 }
 939 
 940 // Writes one-line description of a combination of sigaction.sa_flags into a user
 941 // provided buffer. Returns that buffer.
 942 const char* os::Posix::describe_sa_flags(int flags, char* buffer, size_t size) {
 943   char* p = buffer;
 944   size_t remaining = size;
 945   bool first = true;
 946   int idx = 0;
 947 
 948   assert(buffer, "invalid argument");
 949 
 950   if (size == 0) {
 951     return buffer;
 952   }
 953 
 954   strncpy(buffer, "none", size);
 955 
 956   const struct {
 957     // NB: i is an unsigned int here because SA_RESETHAND is on some
 958     // systems 0x80000000, which is implicitly unsigned.  Assignining
 959     // it to an int field would be an overflow in unsigned-to-signed
 960     // conversion.
 961     unsigned int i;
 962     const char* s;
 963   } flaginfo [] = {
 964     { SA_NOCLDSTOP, "SA_NOCLDSTOP" },
 965     { SA_ONSTACK,   "SA_ONSTACK"   },
 966     { SA_RESETHAND, "SA_RESETHAND" },
 967     { SA_RESTART,   "SA_RESTART"   },
 968     { SA_SIGINFO,   "SA_SIGINFO"   },
 969     { SA_NOCLDWAIT, "SA_NOCLDWAIT" },
 970     { SA_NODEFER,   "SA_NODEFER"   },
 971 #ifdef AIX
 972     { SA_ONSTACK,   "SA_ONSTACK"   },
 973     { SA_OLDSTYLE,  "SA_OLDSTYLE"  },
 974 #endif
 975     { 0, NULL }
 976   };
 977 
 978   for (idx = 0; flaginfo[idx].s && remaining > 1; idx++) {
 979     if (flags & flaginfo[idx].i) {
 980       if (first) {
 981         jio_snprintf(p, remaining, "%s", flaginfo[idx].s);
 982         first = false;
 983       } else {
 984         jio_snprintf(p, remaining, "|%s", flaginfo[idx].s);
 985       }
 986       const size_t len = strlen(p);
 987       p += len;
 988       remaining -= len;
 989     }
 990   }
 991 
 992   buffer[size - 1] = '\0';
 993 
 994   return buffer;
 995 }
 996 
 997 // Prints one-line description of a combination of sigaction.sa_flags.
 998 void os::Posix::print_sa_flags(outputStream* st, int flags) {
 999   char buffer[0x100];
1000   os::Posix::describe_sa_flags(flags, buffer, sizeof(buffer));
1001   st->print("%s", buffer);
1002 }
1003 
1004 // Helper function for os::Posix::print_siginfo_...():
1005 // return a textual description for signal code.
1006 struct enum_sigcode_desc_t {
1007   const char* s_name;
1008   const char* s_desc;
1009 };
1010 
1011 static bool get_signal_code_description(const siginfo_t* si, enum_sigcode_desc_t* out) {
1012 
1013   const struct {
1014     int sig; int code; const char* s_code; const char* s_desc;
1015   } t1 [] = {
1016     { SIGILL,  ILL_ILLOPC,   "ILL_ILLOPC",   "Illegal opcode." },
1017     { SIGILL,  ILL_ILLOPN,   "ILL_ILLOPN",   "Illegal operand." },
1018     { SIGILL,  ILL_ILLADR,   "ILL_ILLADR",   "Illegal addressing mode." },
1019     { SIGILL,  ILL_ILLTRP,   "ILL_ILLTRP",   "Illegal trap." },
1020     { SIGILL,  ILL_PRVOPC,   "ILL_PRVOPC",   "Privileged opcode." },
1021     { SIGILL,  ILL_PRVREG,   "ILL_PRVREG",   "Privileged register." },
1022     { SIGILL,  ILL_COPROC,   "ILL_COPROC",   "Coprocessor error." },
1023     { SIGILL,  ILL_BADSTK,   "ILL_BADSTK",   "Internal stack error." },
1024 #if defined(IA64) && defined(LINUX)
1025     { SIGILL,  ILL_BADIADDR, "ILL_BADIADDR", "Unimplemented instruction address" },
1026     { SIGILL,  ILL_BREAK,    "ILL_BREAK",    "Application Break instruction" },
1027 #endif
1028     { SIGFPE,  FPE_INTDIV,   "FPE_INTDIV",   "Integer divide by zero." },
1029     { SIGFPE,  FPE_INTOVF,   "FPE_INTOVF",   "Integer overflow." },
1030     { SIGFPE,  FPE_FLTDIV,   "FPE_FLTDIV",   "Floating-point divide by zero." },
1031     { SIGFPE,  FPE_FLTOVF,   "FPE_FLTOVF",   "Floating-point overflow." },
1032     { SIGFPE,  FPE_FLTUND,   "FPE_FLTUND",   "Floating-point underflow." },
1033     { SIGFPE,  FPE_FLTRES,   "FPE_FLTRES",   "Floating-point inexact result." },
1034     { SIGFPE,  FPE_FLTINV,   "FPE_FLTINV",   "Invalid floating-point operation." },
1035     { SIGFPE,  FPE_FLTSUB,   "FPE_FLTSUB",   "Subscript out of range." },
1036     { SIGSEGV, SEGV_MAPERR,  "SEGV_MAPERR",  "Address not mapped to object." },
1037     { SIGSEGV, SEGV_ACCERR,  "SEGV_ACCERR",  "Invalid permissions for mapped object." },
1038 #ifdef AIX
1039     // no explanation found what keyerr would be
1040     { SIGSEGV, SEGV_KEYERR,  "SEGV_KEYERR",  "key error" },
1041 #endif
1042 #if defined(IA64) && !defined(AIX)
1043     { SIGSEGV, SEGV_PSTKOVF, "SEGV_PSTKOVF", "Paragraph stack overflow" },
1044 #endif
1045 #if defined(__sparc) && defined(SOLARIS)
1046 // define Solaris Sparc M7 ADI SEGV signals
1047 #if !defined(SEGV_ACCADI)
1048 #define SEGV_ACCADI 3
1049 #endif
1050     { SIGSEGV, SEGV_ACCADI,  "SEGV_ACCADI",  "ADI not enabled for mapped object." },
1051 #if !defined(SEGV_ACCDERR)
1052 #define SEGV_ACCDERR 4
1053 #endif
1054     { SIGSEGV, SEGV_ACCDERR, "SEGV_ACCDERR", "ADI disrupting exception." },
1055 #if !defined(SEGV_ACCPERR)
1056 #define SEGV_ACCPERR 5
1057 #endif
1058     { SIGSEGV, SEGV_ACCPERR, "SEGV_ACCPERR", "ADI precise exception." },
1059 #endif // defined(__sparc) && defined(SOLARIS)
1060     { SIGBUS,  BUS_ADRALN,   "BUS_ADRALN",   "Invalid address alignment." },
1061     { SIGBUS,  BUS_ADRERR,   "BUS_ADRERR",   "Nonexistent physical address." },
1062     { SIGBUS,  BUS_OBJERR,   "BUS_OBJERR",   "Object-specific hardware error." },
1063     { SIGTRAP, TRAP_BRKPT,   "TRAP_BRKPT",   "Process breakpoint." },
1064     { SIGTRAP, TRAP_TRACE,   "TRAP_TRACE",   "Process trace trap." },
1065     { SIGCHLD, CLD_EXITED,   "CLD_EXITED",   "Child has exited." },
1066     { SIGCHLD, CLD_KILLED,   "CLD_KILLED",   "Child has terminated abnormally and did not create a core file." },
1067     { SIGCHLD, CLD_DUMPED,   "CLD_DUMPED",   "Child has terminated abnormally and created a core file." },
1068     { SIGCHLD, CLD_TRAPPED,  "CLD_TRAPPED",  "Traced child has trapped." },
1069     { SIGCHLD, CLD_STOPPED,  "CLD_STOPPED",  "Child has stopped." },
1070     { SIGCHLD, CLD_CONTINUED,"CLD_CONTINUED","Stopped child has continued." },
1071 #ifdef SIGPOLL
1072     { SIGPOLL, POLL_OUT,     "POLL_OUT",     "Output buffers available." },
1073     { SIGPOLL, POLL_MSG,     "POLL_MSG",     "Input message available." },
1074     { SIGPOLL, POLL_ERR,     "POLL_ERR",     "I/O error." },
1075     { SIGPOLL, POLL_PRI,     "POLL_PRI",     "High priority input available." },
1076     { SIGPOLL, POLL_HUP,     "POLL_HUP",     "Device disconnected. [Option End]" },
1077 #endif
1078     { -1, -1, NULL, NULL }
1079   };
1080 
1081   // Codes valid in any signal context.
1082   const struct {
1083     int code; const char* s_code; const char* s_desc;
1084   } t2 [] = {
1085     { SI_USER,      "SI_USER",     "Signal sent by kill()." },
1086     { SI_QUEUE,     "SI_QUEUE",    "Signal sent by the sigqueue()." },
1087     { SI_TIMER,     "SI_TIMER",    "Signal generated by expiration of a timer set by timer_settime()." },
1088     { SI_ASYNCIO,   "SI_ASYNCIO",  "Signal generated by completion of an asynchronous I/O request." },
1089     { SI_MESGQ,     "SI_MESGQ",    "Signal generated by arrival of a message on an empty message queue." },
1090     // Linux specific
1091 #ifdef SI_TKILL
1092     { SI_TKILL,     "SI_TKILL",    "Signal sent by tkill (pthread_kill)" },
1093 #endif
1094 #ifdef SI_DETHREAD
1095     { SI_DETHREAD,  "SI_DETHREAD", "Signal sent by execve() killing subsidiary threads" },
1096 #endif
1097 #ifdef SI_KERNEL
1098     { SI_KERNEL,    "SI_KERNEL",   "Signal sent by kernel." },
1099 #endif
1100 #ifdef SI_SIGIO
1101     { SI_SIGIO,     "SI_SIGIO",    "Signal sent by queued SIGIO" },
1102 #endif
1103 
1104 #ifdef AIX
1105     { SI_UNDEFINED, "SI_UNDEFINED","siginfo contains partial information" },
1106     { SI_EMPTY,     "SI_EMPTY",    "siginfo contains no useful information" },
1107 #endif
1108 
1109 #ifdef __sun
1110     { SI_NOINFO,    "SI_NOINFO",   "No signal information" },
1111     { SI_RCTL,      "SI_RCTL",     "kernel generated signal via rctl action" },
1112     { SI_LWP,       "SI_LWP",      "Signal sent via lwp_kill" },
1113 #endif
1114 
1115     { -1, NULL, NULL }
1116   };
1117 
1118   const char* s_code = NULL;
1119   const char* s_desc = NULL;
1120 
1121   for (int i = 0; t1[i].sig != -1; i ++) {
1122     if (t1[i].sig == si->si_signo && t1[i].code == si->si_code) {
1123       s_code = t1[i].s_code;
1124       s_desc = t1[i].s_desc;
1125       break;
1126     }
1127   }
1128 
1129   if (s_code == NULL) {
1130     for (int i = 0; t2[i].s_code != NULL; i ++) {
1131       if (t2[i].code == si->si_code) {
1132         s_code = t2[i].s_code;
1133         s_desc = t2[i].s_desc;
1134       }
1135     }
1136   }
1137 
1138   if (s_code == NULL) {
1139     out->s_name = "unknown";
1140     out->s_desc = "unknown";
1141     return false;
1142   }
1143 
1144   out->s_name = s_code;
1145   out->s_desc = s_desc;
1146 
1147   return true;
1148 }
1149 
1150 bool os::signal_sent_by_kill(const void* siginfo) {
1151   const siginfo_t* const si = (const siginfo_t*)siginfo;
1152   return si->si_code == SI_USER || si->si_code == SI_QUEUE
1153 #ifdef SI_TKILL
1154          || si->si_code == SI_TKILL
1155 #endif
1156   ;
1157 }
1158 
1159 void os::print_siginfo(outputStream* os, const void* si0) {
1160 
1161   const siginfo_t* const si = (const siginfo_t*) si0;
1162 
1163   char buf[20];
1164   os->print("siginfo:");
1165 
1166   if (!si) {
1167     os->print(" <null>");
1168     return;
1169   }
1170 
1171   const int sig = si->si_signo;
1172 
1173   os->print(" si_signo: %d (%s)", sig, os::Posix::get_signal_name(sig, buf, sizeof(buf)));
1174 
1175   enum_sigcode_desc_t ed;
1176   get_signal_code_description(si, &ed);
1177   os->print(", si_code: %d (%s)", si->si_code, ed.s_name);
1178 
1179   if (si->si_errno) {
1180     os->print(", si_errno: %d", si->si_errno);
1181   }
1182 
1183   // Output additional information depending on the signal code.
1184 
1185   // Note: Many implementations lump si_addr, si_pid, si_uid etc. together as unions,
1186   // so it depends on the context which member to use. For synchronous error signals,
1187   // we print si_addr, unless the signal was sent by another process or thread, in
1188   // which case we print out pid or tid of the sender.
1189   if (signal_sent_by_kill(si)) {
1190     const pid_t pid = si->si_pid;
1191     os->print(", si_pid: %ld", (long) pid);
1192     if (IS_VALID_PID(pid)) {
1193       const pid_t me = getpid();
1194       if (me == pid) {
1195         os->print(" (current process)");
1196       }
1197     } else {
1198       os->print(" (invalid)");
1199     }
1200     os->print(", si_uid: %ld", (long) si->si_uid);
1201     if (sig == SIGCHLD) {
1202       os->print(", si_status: %d", si->si_status);
1203     }
1204   } else if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
1205              sig == SIGTRAP || sig == SIGFPE) {
1206     os->print(", si_addr: " PTR_FORMAT, p2i(si->si_addr));
1207 #ifdef SIGPOLL
1208   } else if (sig == SIGPOLL) {
1209     os->print(", si_band: %ld", si->si_band);
1210 #endif
1211   }
1212 
1213 }
1214 
1215 bool os::signal_thread(Thread* thread, int sig, const char* reason) {
1216   OSThread* osthread = thread->osthread();
1217   if (osthread) {
1218 #if defined (SOLARIS)
1219     // Note: we cannot use pthread_kill on Solaris - not because
1220     // its missing, but because we do not have the pthread_t id.
1221     int status = thr_kill(osthread->thread_id(), sig);
1222 #else
1223     int status = pthread_kill(osthread->pthread_id(), sig);
1224 #endif
1225     if (status == 0) {
1226       Events::log(Thread::current(), "sent signal %d to Thread " INTPTR_FORMAT " because %s.",
1227                   sig, p2i(thread), reason);
1228       return true;
1229     }
1230   }
1231   return false;
1232 }
1233 
1234 int os::Posix::unblock_thread_signal_mask(const sigset_t *set) {
1235   return pthread_sigmask(SIG_UNBLOCK, set, NULL);
1236 }
1237 
1238 address os::Posix::ucontext_get_pc(const ucontext_t* ctx) {
1239 #if defined(AIX)
1240    return Aix::ucontext_get_pc(ctx);
1241 #elif defined(BSD)
1242    return Bsd::ucontext_get_pc(ctx);
1243 #elif defined(LINUX)
1244    return Linux::ucontext_get_pc(ctx);
1245 #elif defined(SOLARIS)
1246    return Solaris::ucontext_get_pc(ctx);
1247 #else
1248    VMError::report_and_die("unimplemented ucontext_get_pc");
1249 #endif
1250 }
1251 
1252 void os::Posix::ucontext_set_pc(ucontext_t* ctx, address pc) {
1253 #if defined(AIX)
1254    Aix::ucontext_set_pc(ctx, pc);
1255 #elif defined(BSD)
1256    Bsd::ucontext_set_pc(ctx, pc);
1257 #elif defined(LINUX)
1258    Linux::ucontext_set_pc(ctx, pc);
1259 #elif defined(SOLARIS)
1260    Solaris::ucontext_set_pc(ctx, pc);
1261 #else
1262    VMError::report_and_die("unimplemented ucontext_get_pc");
1263 #endif
1264 }
1265 
1266 char* os::Posix::describe_pthread_attr(char* buf, size_t buflen, const pthread_attr_t* attr) {
1267   size_t stack_size = 0;
1268   size_t guard_size = 0;
1269   int detachstate = 0;
1270   pthread_attr_getstacksize(attr, &stack_size);
1271   pthread_attr_getguardsize(attr, &guard_size);
1272   // Work around linux NPTL implementation error, see also os::create_thread() in os_linux.cpp.
1273   LINUX_ONLY(stack_size -= guard_size);
1274   pthread_attr_getdetachstate(attr, &detachstate);
1275   jio_snprintf(buf, buflen, "stacksize: " SIZE_FORMAT "k, guardsize: " SIZE_FORMAT "k, %s",
1276     stack_size / 1024, guard_size / 1024,
1277     (detachstate == PTHREAD_CREATE_DETACHED ? "detached" : "joinable"));
1278   return buf;
1279 }
1280 
1281 char* os::Posix::realpath(const char* filename, char* outbuf, size_t outbuflen) {
1282 
1283   if (filename == NULL || outbuf == NULL || outbuflen < 1) {
1284     assert(false, "os::Posix::realpath: invalid arguments.");
1285     errno = EINVAL;
1286     return NULL;
1287   }
1288 
1289   char* result = NULL;
1290 
1291   // This assumes platform realpath() is implemented according to POSIX.1-2008.
1292   // POSIX.1-2008 allows to specify NULL for the output buffer, in which case
1293   // output buffer is dynamically allocated and must be ::free()'d by the caller.
1294   char* p = ::realpath(filename, NULL);
1295   if (p != NULL) {
1296     if (strlen(p) < outbuflen) {
1297       strcpy(outbuf, p);
1298       result = outbuf;
1299     } else {
1300       errno = ENAMETOOLONG;
1301     }
1302     ::free(p); // *not* os::free
1303   } else {
1304     // Fallback for platforms struggling with modern Posix standards (AIX 5.3, 6.1). If realpath
1305     // returns EINVAL, this may indicate that realpath is not POSIX.1-2008 compatible and
1306     // that it complains about the NULL we handed down as user buffer.
1307     // In this case, use the user provided buffer but at least check whether realpath caused
1308     // a memory overwrite.
1309     if (errno == EINVAL) {
1310       outbuf[outbuflen - 1] = '\0';
1311       p = ::realpath(filename, outbuf);
1312       if (p != NULL) {
1313         guarantee(outbuf[outbuflen - 1] == '\0', "realpath buffer overwrite detected.");
1314         result = p;
1315       }
1316     }
1317   }
1318   return result;
1319 
1320 }
1321 
1322 int os::stat(const char *path, struct stat *sbuf) {
1323   return ::stat(path, sbuf);
1324 }
1325 
1326 char * os::native_path(char *path) {
1327   return path;
1328 }
1329 
1330 bool os::same_files(const char* file1, const char* file2) {
1331   if (strcmp(file1, file2) == 0) {
1332     return true;
1333   }
1334 
1335   bool is_same = false;
1336   struct stat st1;
1337   struct stat st2;
1338 
1339   if (os::stat(file1, &st1) < 0) {
1340     return false;
1341   }
1342 
1343   if (os::stat(file2, &st2) < 0) {
1344     return false;
1345   }
1346 
1347   if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
1348     // same files
1349     is_same = true;
1350   }
1351   return is_same;
1352 }
1353 
1354 // Check minimum allowable stack sizes for thread creation and to initialize
1355 // the java system classes, including StackOverflowError - depends on page
1356 // size.
1357 // The space needed for frames during startup is platform dependent. It
1358 // depends on word size, platform calling conventions, C frame layout and
1359 // interpreter/C1/C2 design decisions. Therefore this is given in a
1360 // platform (os/cpu) dependent constant.
1361 // To this, space for guard mechanisms is added, which depends on the
1362 // page size which again depends on the concrete system the VM is running
1363 // on. Space for libc guard pages is not included in this size.
1364 jint os::Posix::set_minimum_stack_sizes() {
1365   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1366 
1367   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1368                                    JavaThread::stack_guard_zone_size() +
1369                                    JavaThread::stack_shadow_zone_size();
1370 
1371   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
1372   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1373 
1374   size_t stack_size_in_bytes = ThreadStackSize * K;
1375   if (stack_size_in_bytes != 0 &&
1376       stack_size_in_bytes < _java_thread_min_stack_allowed) {
1377     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
1378     // ThreadStackSize so we go with "Java thread stack size" instead
1379     // of "ThreadStackSize" to be more friendly.
1380     tty->print_cr("\nThe Java thread stack size specified is too small. "
1381                   "Specify at least " SIZE_FORMAT "k",
1382                   _java_thread_min_stack_allowed / K);
1383     return JNI_ERR;
1384   }
1385 
1386   // Make the stack size a multiple of the page size so that
1387   // the yellow/red zones can be guarded.
1388   JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));
1389 
1390   // Reminder: a compiler thread is a Java thread.
1391   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
1392                                        JavaThread::stack_guard_zone_size() +
1393                                        JavaThread::stack_shadow_zone_size();
1394 
1395   _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
1396   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
1397 
1398   stack_size_in_bytes = CompilerThreadStackSize * K;
1399   if (stack_size_in_bytes != 0 &&
1400       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
1401     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
1402                   "Specify at least " SIZE_FORMAT "k",
1403                   _compiler_thread_min_stack_allowed / K);
1404     return JNI_ERR;
1405   }
1406 
1407   _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
1408   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
1409 
1410   stack_size_in_bytes = VMThreadStackSize * K;
1411   if (stack_size_in_bytes != 0 &&
1412       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
1413     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
1414                   "Specify at least " SIZE_FORMAT "k",
1415                   _vm_internal_thread_min_stack_allowed / K);
1416     return JNI_ERR;
1417   }
1418   return JNI_OK;
1419 }
1420 
1421 // Called when creating the thread.  The minimum stack sizes have already been calculated
1422 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
1423   size_t stack_size;
1424   if (req_stack_size == 0) {
1425     stack_size = default_stack_size(thr_type);
1426   } else {
1427     stack_size = req_stack_size;
1428   }
1429 
1430   switch (thr_type) {
1431   case os::java_thread:
1432     // Java threads use ThreadStackSize which default value can be
1433     // changed with the flag -Xss
1434     if (req_stack_size == 0 && JavaThread::stack_size_at_create() > 0) {
1435       // no requested size and we have a more specific default value
1436       stack_size = JavaThread::stack_size_at_create();
1437     }
1438     stack_size = MAX2(stack_size,
1439                       _java_thread_min_stack_allowed);
1440     break;
1441   case os::compiler_thread:
1442     if (req_stack_size == 0 && CompilerThreadStackSize > 0) {
1443       // no requested size and we have a more specific default value
1444       stack_size = (size_t)(CompilerThreadStackSize * K);
1445     }
1446     stack_size = MAX2(stack_size,
1447                       _compiler_thread_min_stack_allowed);
1448     break;
1449   case os::vm_thread:
1450   case os::pgc_thread:
1451   case os::cgc_thread:
1452   case os::watcher_thread:
1453   default:  // presume the unknown thr_type is a VM internal
1454     if (req_stack_size == 0 && VMThreadStackSize > 0) {
1455       // no requested size and we have a more specific default value
1456       stack_size = (size_t)(VMThreadStackSize * K);
1457     }
1458 
1459     stack_size = MAX2(stack_size,
1460                       _vm_internal_thread_min_stack_allowed);
1461     break;
1462   }
1463 
1464   // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1465   // Be careful not to round up to 0. Align down in that case.
1466   if (stack_size <= SIZE_MAX - vm_page_size()) {
1467     stack_size = align_up(stack_size, vm_page_size());
1468   } else {
1469     stack_size = align_down(stack_size, vm_page_size());
1470   }
1471 
1472   return stack_size;
1473 }
1474 
1475 bool os::Posix::is_root(uid_t uid){
1476     return ROOT_UID == uid;
1477 }
1478 
1479 bool os::Posix::matches_effective_uid_or_root(uid_t uid) {
1480     return is_root(uid) || geteuid() == uid;
1481 }
1482 
1483 bool os::Posix::matches_effective_uid_and_gid_or_root(uid_t uid, gid_t gid) {
1484     return is_root(uid) || (geteuid() == uid && getegid() == gid);
1485 }
1486 
1487 Thread* os::ThreadCrashProtection::_protected_thread = NULL;
1488 os::ThreadCrashProtection* os::ThreadCrashProtection::_crash_protection = NULL;
1489 volatile intptr_t os::ThreadCrashProtection::_crash_mux = 0;
1490 
1491 os::ThreadCrashProtection::ThreadCrashProtection() {
1492 }
1493 
1494 /*
1495  * See the caveats for this class in os_posix.hpp
1496  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1497  * method and returns false. If none of the signals are raised, returns true.
1498  * The callback is supposed to provide the method that should be protected.
1499  */
1500 bool os::ThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1501   sigset_t saved_sig_mask;
1502 
1503   Thread::muxAcquire(&_crash_mux, "CrashProtection");
1504 
1505   _protected_thread = Thread::current_or_null();
1506   assert(_protected_thread != NULL, "Cannot crash protect a NULL thread");
1507 
1508   // we cannot rely on sigsetjmp/siglongjmp to save/restore the signal mask
1509   // since on at least some systems (OS X) siglongjmp will restore the mask
1510   // for the process, not the thread
1511   pthread_sigmask(0, NULL, &saved_sig_mask);
1512   if (sigsetjmp(_jmpbuf, 0) == 0) {
1513     // make sure we can see in the signal handler that we have crash protection
1514     // installed
1515     _crash_protection = this;
1516     cb.call();
1517     // and clear the crash protection
1518     _crash_protection = NULL;
1519     _protected_thread = NULL;
1520     Thread::muxRelease(&_crash_mux);
1521     return true;
1522   }
1523   // this happens when we siglongjmp() back
1524   pthread_sigmask(SIG_SETMASK, &saved_sig_mask, NULL);
1525   _crash_protection = NULL;
1526   _protected_thread = NULL;
1527   Thread::muxRelease(&_crash_mux);
1528   return false;
1529 }
1530 
1531 void os::ThreadCrashProtection::restore() {
1532   assert(_crash_protection != NULL, "must have crash protection");
1533   siglongjmp(_jmpbuf, 1);
1534 }
1535 
1536 void os::ThreadCrashProtection::check_crash_protection(int sig,
1537     Thread* thread) {
1538 
1539   if (thread != NULL &&
1540       thread == _protected_thread &&
1541       _crash_protection != NULL) {
1542 
1543     if (sig == SIGSEGV || sig == SIGBUS) {
1544       _crash_protection->restore();
1545     }
1546   }
1547 }
1548 
1549 // Shared clock/time and other supporting routines for pthread_mutex/cond
1550 // initialization. This is enabled on Solaris but only some of the clock/time
1551 // functionality is actually used there.
1552 
1553 // Shared condattr object for use with relative timed-waits. Will be associated
1554 // with CLOCK_MONOTONIC if available to avoid issues with time-of-day changes,
1555 // but otherwise whatever default is used by the platform - generally the
1556 // time-of-day clock.
1557 static pthread_condattr_t _condAttr[1];
1558 
1559 // Shared mutexattr to explicitly set the type to PTHREAD_MUTEX_NORMAL as not
1560 // all systems (e.g. FreeBSD) map the default to "normal".
1561 static pthread_mutexattr_t _mutexAttr[1];
1562 
1563 // common basic initialization that is always supported
1564 static void pthread_init_common(void) {
1565   int status;
1566   if ((status = pthread_condattr_init(_condAttr)) != 0) {
1567     fatal("pthread_condattr_init: %s", os::strerror(status));
1568   }
1569   if ((status = pthread_mutexattr_init(_mutexAttr)) != 0) {
1570     fatal("pthread_mutexattr_init: %s", os::strerror(status));
1571   }
1572   if ((status = pthread_mutexattr_settype(_mutexAttr, PTHREAD_MUTEX_NORMAL)) != 0) {
1573     fatal("pthread_mutexattr_settype: %s", os::strerror(status));
1574   }
1575   // Solaris has it's own PlatformMutex, distinct from the one for POSIX.
1576   NOT_SOLARIS(os::PlatformMutex::init();)
1577 }
1578 
1579 #ifndef SOLARIS
1580 sigset_t sigs;
1581 struct sigaction sigact[NSIG];
1582 
1583 struct sigaction* os::Posix::get_preinstalled_handler(int sig) {
1584   if (sigismember(&sigs, sig)) {
1585     return &sigact[sig];
1586   }
1587   return NULL;
1588 }
1589 
1590 void os::Posix::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
1591   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
1592   sigact[sig] = oldAct;
1593   sigaddset(&sigs, sig);
1594 }
1595 #endif
1596 
1597 // Not all POSIX types and API's are available on all notionally "posix"
1598 // platforms. If we have build-time support then we will check for actual
1599 // runtime support via dlopen/dlsym lookup. This allows for running on an
1600 // older OS version compared to the build platform. But if there is no
1601 // build time support then there cannot be any runtime support as we do not
1602 // know what the runtime types would be (for example clockid_t might be an
1603 // int or int64_t).
1604 //
1605 #ifdef SUPPORTS_CLOCK_MONOTONIC
1606 
1607 // This means we have clockid_t, clock_gettime et al and CLOCK_MONOTONIC
1608 
1609 int (*os::Posix::_clock_gettime)(clockid_t, struct timespec *) = NULL;
1610 int (*os::Posix::_clock_getres)(clockid_t, struct timespec *) = NULL;
1611 
1612 static int (*_pthread_condattr_setclock)(pthread_condattr_t *, clockid_t) = NULL;
1613 
1614 static bool _use_clock_monotonic_condattr = false;
1615 
1616 // Determine what POSIX API's are present and do appropriate
1617 // configuration.
1618 void os::Posix::init(void) {
1619 
1620   // NOTE: no logging available when this is called. Put logging
1621   // statements in init_2().
1622 
1623   // 1. Check for CLOCK_MONOTONIC support.
1624 
1625   void* handle = NULL;
1626 
1627   // For linux we need librt, for other OS we can find
1628   // this function in regular libc.
1629 #ifdef NEEDS_LIBRT
1630   // We do dlopen's in this particular order due to bug in linux
1631   // dynamic loader (see 6348968) leading to crash on exit.
1632   handle = dlopen("librt.so.1", RTLD_LAZY);
1633   if (handle == NULL) {
1634     handle = dlopen("librt.so", RTLD_LAZY);
1635   }
1636 #endif
1637 
1638   if (handle == NULL) {
1639     handle = RTLD_DEFAULT;
1640   }
1641 
1642   int (*clock_getres_func)(clockid_t, struct timespec*) =
1643     (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
1644   int (*clock_gettime_func)(clockid_t, struct timespec*) =
1645     (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
1646   if (clock_getres_func != NULL && clock_gettime_func != NULL) {
1647     // We assume that if both clock_gettime and clock_getres support
1648     // CLOCK_MONOTONIC then the OS provides true high-res monotonic clock.
1649     struct timespec res;
1650     struct timespec tp;
1651     if (clock_getres_func(CLOCK_MONOTONIC, &res) == 0 &&
1652         clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1653       // Yes, monotonic clock is supported.
1654       _clock_gettime = clock_gettime_func;
1655       _clock_getres = clock_getres_func;
1656     } else {
1657 #ifdef NEEDS_LIBRT
1658       // Close librt if there is no monotonic clock.
1659       if (handle != RTLD_DEFAULT) {
1660         dlclose(handle);
1661       }
1662 #endif
1663     }
1664   }
1665 
1666   // 2. Check for pthread_condattr_setclock support.
1667 
1668   // libpthread is already loaded.
1669   int (*condattr_setclock_func)(pthread_condattr_t*, clockid_t) =
1670     (int (*)(pthread_condattr_t*, clockid_t))dlsym(RTLD_DEFAULT,
1671                                                    "pthread_condattr_setclock");
1672   if (condattr_setclock_func != NULL) {
1673     _pthread_condattr_setclock = condattr_setclock_func;
1674   }
1675 
1676   // Now do general initialization.
1677 
1678   pthread_init_common();
1679 
1680 #ifndef SOLARIS
1681   int status;
1682   if (_pthread_condattr_setclock != NULL && _clock_gettime != NULL) {
1683     if ((status = _pthread_condattr_setclock(_condAttr, CLOCK_MONOTONIC)) != 0) {
1684       if (status == EINVAL) {
1685         _use_clock_monotonic_condattr = false;
1686         warning("Unable to use monotonic clock with relative timed-waits" \
1687                 " - changes to the time-of-day clock may have adverse affects");
1688       } else {
1689         fatal("pthread_condattr_setclock: %s", os::strerror(status));
1690       }
1691     } else {
1692       _use_clock_monotonic_condattr = true;
1693     }
1694   }
1695 #endif // !SOLARIS
1696 
1697 }
1698 
1699 void os::Posix::init_2(void) {
1700 #ifndef SOLARIS
1701   log_info(os)("Use of CLOCK_MONOTONIC is%s supported",
1702                (_clock_gettime != NULL ? "" : " not"));
1703   log_info(os)("Use of pthread_condattr_setclock is%s supported",
1704                (_pthread_condattr_setclock != NULL ? "" : " not"));
1705   log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with %s",
1706                _use_clock_monotonic_condattr ? "CLOCK_MONOTONIC" : "the default clock");
1707   sigemptyset(&sigs);
1708 #endif // !SOLARIS
1709 }
1710 
1711 #else // !SUPPORTS_CLOCK_MONOTONIC
1712 
1713 void os::Posix::init(void) {
1714   pthread_init_common();
1715 }
1716 
1717 void os::Posix::init_2(void) {
1718 #ifndef SOLARIS
1719   log_info(os)("Use of CLOCK_MONOTONIC is not supported");
1720   log_info(os)("Use of pthread_condattr_setclock is not supported");
1721   log_info(os)("Relative timed-wait using pthread_cond_timedwait is associated with the default clock");
1722   sigemptyset(&sigs);
1723 #endif // !SOLARIS
1724 }
1725 
1726 #endif // SUPPORTS_CLOCK_MONOTONIC
1727 
1728 // Utility to convert the given timeout to an absolute timespec
1729 // (based on the appropriate clock) to use with pthread_cond_timewait,
1730 // and sem_timedwait().
1731 // The clock queried here must be the clock used to manage the
1732 // timeout of the condition variable or semaphore.
1733 //
1734 // The passed in timeout value is either a relative time in nanoseconds
1735 // or an absolute time in milliseconds. A relative timeout will be
1736 // associated with CLOCK_MONOTONIC if available, unless the real-time clock
1737 // is explicitly requested; otherwise, or if absolute,
1738 // the default time-of-day clock will be used.
1739 
1740 // Given time is a 64-bit value and the time_t used in the timespec is
1741 // sometimes a signed-32-bit value we have to watch for overflow if times
1742 // way in the future are given. Further on Solaris versions
1743 // prior to 10 there is a restriction (see cond_timedwait) that the specified
1744 // number of seconds, in abstime, is less than current_time + 100000000.
1745 // As it will be over 20 years before "now + 100000000" will overflow we can
1746 // ignore overflow and just impose a hard-limit on seconds using the value
1747 // of "now + 100000000". This places a limit on the timeout of about 3.17
1748 // years from "now".
1749 //
1750 #define MAX_SECS 100000000
1751 
1752 // Calculate a new absolute time that is "timeout" nanoseconds from "now".
1753 // "unit" indicates the unit of "now_part_sec" (may be nanos or micros depending
1754 // on which clock API is being used).
1755 static void calc_rel_time(timespec* abstime, jlong timeout, jlong now_sec,
1756                           jlong now_part_sec, jlong unit) {
1757   time_t max_secs = now_sec + MAX_SECS;
1758 
1759   jlong seconds = timeout / NANOUNITS;
1760   timeout %= NANOUNITS; // remaining nanos
1761 
1762   if (seconds >= MAX_SECS) {
1763     // More seconds than we can add, so pin to max_secs.
1764     abstime->tv_sec = max_secs;
1765     abstime->tv_nsec = 0;
1766   } else {
1767     abstime->tv_sec = now_sec  + seconds;
1768     long nanos = (now_part_sec * (NANOUNITS / unit)) + timeout;
1769     if (nanos >= NANOUNITS) { // overflow
1770       abstime->tv_sec += 1;
1771       nanos -= NANOUNITS;
1772     }
1773     abstime->tv_nsec = nanos;
1774   }
1775 }
1776 
1777 // Unpack the given deadline in milliseconds since the epoch, into the given timespec.
1778 // The current time in seconds is also passed in to enforce an upper bound as discussed above.
1779 // This is only used with gettimeofday, when clock_gettime is not available.
1780 static void unpack_abs_time(timespec* abstime, jlong deadline, jlong now_sec) {
1781   time_t max_secs = now_sec + MAX_SECS;
1782 
1783   jlong seconds = deadline / MILLIUNITS;
1784   jlong millis = deadline % MILLIUNITS;
1785 
1786   if (seconds >= max_secs) {
1787     // Absolute seconds exceeds allowed max, so pin to max_secs.
1788     abstime->tv_sec = max_secs;
1789     abstime->tv_nsec = 0;
1790   } else {
1791     abstime->tv_sec = seconds;
1792     abstime->tv_nsec = millis * (NANOUNITS / MILLIUNITS);
1793   }
1794 }
1795 
1796 static jlong millis_to_nanos(jlong millis) {
1797   // We have to watch for overflow when converting millis to nanos,
1798   // but if millis is that large then we will end up limiting to
1799   // MAX_SECS anyway, so just do that here.
1800   if (millis / MILLIUNITS > MAX_SECS) {
1801     millis = jlong(MAX_SECS) * MILLIUNITS;
1802   }
1803   return millis * (NANOUNITS / MILLIUNITS);
1804 }
1805 
1806 static void to_abstime(timespec* abstime, jlong timeout,
1807                        bool isAbsolute, bool isRealtime) {
1808   DEBUG_ONLY(int max_secs = MAX_SECS;)
1809 
1810   if (timeout < 0) {
1811     timeout = 0;
1812   }
1813 
1814 #ifdef SUPPORTS_CLOCK_MONOTONIC
1815 
1816   clockid_t clock = CLOCK_MONOTONIC;
1817   // need to ensure we have a runtime check for clock_gettime support
1818   if (!isAbsolute && os::Posix::supports_monotonic_clock()) {
1819     if (!_use_clock_monotonic_condattr || isRealtime) {
1820       clock = CLOCK_REALTIME;
1821     }
1822     struct timespec now;
1823     int status = os::Posix::clock_gettime(clock, &now);
1824     assert_status(status == 0, status, "clock_gettime");
1825     calc_rel_time(abstime, timeout, now.tv_sec, now.tv_nsec, NANOUNITS);
1826     DEBUG_ONLY(max_secs += now.tv_sec;)
1827   } else {
1828 
1829 #else
1830 
1831   { // Match the block scope.
1832 
1833 #endif // SUPPORTS_CLOCK_MONOTONIC
1834 
1835     // Time-of-day clock is all we can reliably use.
1836     struct timeval now;
1837     int status = gettimeofday(&now, NULL);
1838     assert_status(status == 0, errno, "gettimeofday");
1839     if (isAbsolute) {
1840       unpack_abs_time(abstime, timeout, now.tv_sec);
1841     } else {
1842       calc_rel_time(abstime, timeout, now.tv_sec, now.tv_usec, MICROUNITS);
1843     }
1844     DEBUG_ONLY(max_secs += now.tv_sec;)
1845   }
1846 
1847   assert(abstime->tv_sec >= 0, "tv_sec < 0");
1848   assert(abstime->tv_sec <= max_secs, "tv_sec > max_secs");
1849   assert(abstime->tv_nsec >= 0, "tv_nsec < 0");
1850   assert(abstime->tv_nsec < NANOUNITS, "tv_nsec >= NANOUNITS");
1851 }
1852 
1853 // Create an absolute time 'millis' milliseconds in the future, using the
1854 // real-time (time-of-day) clock. Used by PosixSemaphore.
1855 void os::Posix::to_RTC_abstime(timespec* abstime, int64_t millis) {
1856   to_abstime(abstime, millis_to_nanos(millis),
1857              false /* not absolute */,
1858              true  /* use real-time clock */);
1859 }
1860 
1861 // Shared pthread_mutex/cond based PlatformEvent implementation.
1862 // Not currently usable by Solaris.
1863 
1864 #ifndef SOLARIS
1865 
1866 // PlatformEvent
1867 //
1868 // Assumption:
1869 //    Only one parker can exist on an event, which is why we allocate
1870 //    them per-thread. Multiple unparkers can coexist.
1871 //
1872 // _event serves as a restricted-range semaphore.
1873 //   -1 : thread is blocked, i.e. there is a waiter
1874 //    0 : neutral: thread is running or ready,
1875 //        could have been signaled after a wait started
1876 //    1 : signaled - thread is running or ready
1877 //
1878 //    Having three states allows for some detection of bad usage - see
1879 //    comments on unpark().
1880 
1881 os::PlatformEvent::PlatformEvent() {
1882   int status = pthread_cond_init(_cond, _condAttr);
1883   assert_status(status == 0, status, "cond_init");
1884   status = pthread_mutex_init(_mutex, _mutexAttr);
1885   assert_status(status == 0, status, "mutex_init");
1886   _event   = 0;
1887   _nParked = 0;
1888 }
1889 
1890 void os::PlatformEvent::park() {       // AKA "down()"
1891   // Transitions for _event:
1892   //   -1 => -1 : illegal
1893   //    1 =>  0 : pass - return immediately
1894   //    0 => -1 : block; then set _event to 0 before returning
1895 
1896   // Invariant: Only the thread associated with the PlatformEvent
1897   // may call park().
1898   assert(_nParked == 0, "invariant");
1899 
1900   int v;
1901 
1902   // atomically decrement _event
1903   for (;;) {
1904     v = _event;
1905     if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1906   }
1907   guarantee(v >= 0, "invariant");
1908 
1909   if (v == 0) { // Do this the hard way by blocking ...
1910     int status = pthread_mutex_lock(_mutex);
1911     assert_status(status == 0, status, "mutex_lock");
1912     guarantee(_nParked == 0, "invariant");
1913     ++_nParked;
1914     while (_event < 0) {
1915       // OS-level "spurious wakeups" are ignored
1916       status = pthread_cond_wait(_cond, _mutex);
1917       assert_status(status == 0, status, "cond_wait");
1918     }
1919     --_nParked;
1920 
1921     _event = 0;
1922     status = pthread_mutex_unlock(_mutex);
1923     assert_status(status == 0, status, "mutex_unlock");
1924     // Paranoia to ensure our locked and lock-free paths interact
1925     // correctly with each other.
1926     OrderAccess::fence();
1927   }
1928   guarantee(_event >= 0, "invariant");
1929 }
1930 
1931 int os::PlatformEvent::park(jlong millis) {
1932   // Transitions for _event:
1933   //   -1 => -1 : illegal
1934   //    1 =>  0 : pass - return immediately
1935   //    0 => -1 : block; then set _event to 0 before returning
1936 
1937   // Invariant: Only the thread associated with the Event/PlatformEvent
1938   // may call park().
1939   assert(_nParked == 0, "invariant");
1940 
1941   int v;
1942   // atomically decrement _event
1943   for (;;) {
1944     v = _event;
1945     if (Atomic::cmpxchg(v - 1, &_event, v) == v) break;
1946   }
1947   guarantee(v >= 0, "invariant");
1948 
1949   if (v == 0) { // Do this the hard way by blocking ...
1950     struct timespec abst;
1951     to_abstime(&abst, millis_to_nanos(millis), false, false);
1952 
1953     int ret = OS_TIMEOUT;
1954     int status = pthread_mutex_lock(_mutex);
1955     assert_status(status == 0, status, "mutex_lock");
1956     guarantee(_nParked == 0, "invariant");
1957     ++_nParked;
1958 
1959     while (_event < 0) {
1960       status = pthread_cond_timedwait(_cond, _mutex, &abst);
1961       assert_status(status == 0 || status == ETIMEDOUT,
1962                     status, "cond_timedwait");
1963       // OS-level "spurious wakeups" are ignored unless the archaic
1964       // FilterSpuriousWakeups is set false. That flag should be obsoleted.
1965       if (!FilterSpuriousWakeups) break;
1966       if (status == ETIMEDOUT) break;
1967     }
1968     --_nParked;
1969 
1970     if (_event >= 0) {
1971       ret = OS_OK;
1972     }
1973 
1974     _event = 0;
1975     status = pthread_mutex_unlock(_mutex);
1976     assert_status(status == 0, status, "mutex_unlock");
1977     // Paranoia to ensure our locked and lock-free paths interact
1978     // correctly with each other.
1979     OrderAccess::fence();
1980     return ret;
1981   }
1982   return OS_OK;
1983 }
1984 
1985 void os::PlatformEvent::unpark() {
1986   // Transitions for _event:
1987   //    0 => 1 : just return
1988   //    1 => 1 : just return
1989   //   -1 => either 0 or 1; must signal target thread
1990   //         That is, we can safely transition _event from -1 to either
1991   //         0 or 1.
1992   // See also: "Semaphores in Plan 9" by Mullender & Cox
1993   //
1994   // Note: Forcing a transition from "-1" to "1" on an unpark() means
1995   // that it will take two back-to-back park() calls for the owning
1996   // thread to block. This has the benefit of forcing a spurious return
1997   // from the first park() call after an unpark() call which will help
1998   // shake out uses of park() and unpark() without checking state conditions
1999   // properly. This spurious return doesn't manifest itself in any user code
2000   // but only in the correctly written condition checking loops of ObjectMonitor,
2001   // Mutex/Monitor, Thread::muxAcquire and JavaThread::sleep
2002 
2003   if (Atomic::xchg(1, &_event) >= 0) return;
2004 
2005   int status = pthread_mutex_lock(_mutex);
2006   assert_status(status == 0, status, "mutex_lock");
2007   int anyWaiters = _nParked;
2008   assert(anyWaiters == 0 || anyWaiters == 1, "invariant");
2009   status = pthread_mutex_unlock(_mutex);
2010   assert_status(status == 0, status, "mutex_unlock");
2011 
2012   // Note that we signal() *after* dropping the lock for "immortal" Events.
2013   // This is safe and avoids a common class of futile wakeups.  In rare
2014   // circumstances this can cause a thread to return prematurely from
2015   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2016   // will simply re-test the condition and re-park itself.
2017   // This provides particular benefit if the underlying platform does not
2018   // provide wait morphing.
2019 
2020   if (anyWaiters != 0) {
2021     status = pthread_cond_signal(_cond);
2022     assert_status(status == 0, status, "cond_signal");
2023   }
2024 }
2025 
2026 // JSR166 support
2027 
2028  os::PlatformParker::PlatformParker() {
2029   int status;
2030   status = pthread_cond_init(&_cond[REL_INDEX], _condAttr);
2031   assert_status(status == 0, status, "cond_init rel");
2032   status = pthread_cond_init(&_cond[ABS_INDEX], NULL);
2033   assert_status(status == 0, status, "cond_init abs");
2034   status = pthread_mutex_init(_mutex, _mutexAttr);
2035   assert_status(status == 0, status, "mutex_init");
2036   _cur_index = -1; // mark as unused
2037 }
2038 
2039 // Parker::park decrements count if > 0, else does a condvar wait.  Unpark
2040 // sets count to 1 and signals condvar.  Only one thread ever waits
2041 // on the condvar. Contention seen when trying to park implies that someone
2042 // is unparking you, so don't wait. And spurious returns are fine, so there
2043 // is no need to track notifications.
2044 
2045 void Parker::park(bool isAbsolute, jlong time) {
2046 
2047   // Optional fast-path check:
2048   // Return immediately if a permit is available.
2049   // We depend on Atomic::xchg() having full barrier semantics
2050   // since we are doing a lock-free update to _counter.
2051   if (Atomic::xchg(0, &_counter) > 0) return;
2052 
2053   Thread* thread = Thread::current();
2054   assert(thread->is_Java_thread(), "Must be JavaThread");
2055   JavaThread *jt = (JavaThread *)thread;
2056 
2057   // Optional optimization -- avoid state transitions if there's
2058   // an interrupt pending.
2059   if (jt->is_interrupted(false)) {
2060     return;
2061   }
2062 
2063   // Next, demultiplex/decode time arguments
2064   struct timespec absTime;
2065   if (time < 0 || (isAbsolute && time == 0)) { // don't wait at all
2066     return;
2067   }
2068   if (time > 0) {
2069     to_abstime(&absTime, time, isAbsolute, false);
2070   }
2071 
2072   // Enter safepoint region
2073   // Beware of deadlocks such as 6317397.
2074   // The per-thread Parker:: mutex is a classic leaf-lock.
2075   // In particular a thread must never block on the Threads_lock while
2076   // holding the Parker:: mutex.  If safepoints are pending both the
2077   // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
2078   ThreadBlockInVM tbivm(jt);
2079 
2080   // Don't wait if cannot get lock since interference arises from
2081   // unparking. Also re-check interrupt before trying wait.
2082   if (jt->is_interrupted(false) ||
2083       pthread_mutex_trylock(_mutex) != 0) {
2084     return;
2085   }
2086 
2087   int status;
2088   if (_counter > 0)  { // no wait needed
2089     _counter = 0;
2090     status = pthread_mutex_unlock(_mutex);
2091     assert_status(status == 0, status, "invariant");
2092     // Paranoia to ensure our locked and lock-free paths interact
2093     // correctly with each other and Java-level accesses.
2094     OrderAccess::fence();
2095     return;
2096   }
2097 
2098   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
2099   jt->set_suspend_equivalent();
2100   // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2101 
2102   assert(_cur_index == -1, "invariant");
2103   if (time == 0) {
2104     _cur_index = REL_INDEX; // arbitrary choice when not timed
2105     status = pthread_cond_wait(&_cond[_cur_index], _mutex);
2106     assert_status(status == 0, status, "cond_timedwait");
2107   }
2108   else {
2109     _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
2110     status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);
2111     assert_status(status == 0 || status == ETIMEDOUT,
2112                   status, "cond_timedwait");
2113   }
2114   _cur_index = -1;
2115 
2116   _counter = 0;
2117   status = pthread_mutex_unlock(_mutex);
2118   assert_status(status == 0, status, "invariant");
2119   // Paranoia to ensure our locked and lock-free paths interact
2120   // correctly with each other and Java-level accesses.
2121   OrderAccess::fence();
2122 
2123   // If externally suspended while waiting, re-suspend
2124   if (jt->handle_special_suspend_equivalent_condition()) {
2125     jt->java_suspend_self();
2126   }
2127 }
2128 
2129 void Parker::unpark() {
2130   int status = pthread_mutex_lock(_mutex);
2131   assert_status(status == 0, status, "invariant");
2132   const int s = _counter;
2133   _counter = 1;
2134   // must capture correct index before unlocking
2135   int index = _cur_index;
2136   status = pthread_mutex_unlock(_mutex);
2137   assert_status(status == 0, status, "invariant");
2138 
2139   // Note that we signal() *after* dropping the lock for "immortal" Events.
2140   // This is safe and avoids a common class of futile wakeups.  In rare
2141   // circumstances this can cause a thread to return prematurely from
2142   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2143   // will simply re-test the condition and re-park itself.
2144   // This provides particular benefit if the underlying platform does not
2145   // provide wait morphing.
2146 
2147   if (s < 1 && index != -1) {
2148     // thread is definitely parked
2149     status = pthread_cond_signal(&_cond[index]);
2150     assert_status(status == 0, status, "invariant");
2151   }
2152 }
2153 
2154 // Platform Mutex/Monitor implementation
2155 
2156 #if PLATFORM_MONITOR_IMPL_INDIRECT
2157 
2158 os::PlatformMutex::Mutex::Mutex() : _next(NULL) {
2159   int status = pthread_mutex_init(&_mutex, _mutexAttr);
2160   assert_status(status == 0, status, "mutex_init");
2161 }
2162 
2163 os::PlatformMutex::Mutex::~Mutex() {
2164   int status = pthread_mutex_destroy(&_mutex);
2165   assert_status(status == 0, status, "mutex_destroy");
2166 }
2167 
2168 pthread_mutex_t os::PlatformMutex::_freelist_lock;
2169 os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;
2170 
2171 void os::PlatformMutex::init() {
2172   int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);
2173   assert_status(status == 0, status, "freelist lock init");
2174 }
2175 
2176 struct os::PlatformMutex::WithFreeListLocked : public StackObj {
2177   WithFreeListLocked() {
2178     int status = pthread_mutex_lock(&_freelist_lock);
2179     assert_status(status == 0, status, "freelist lock");
2180   }
2181 
2182   ~WithFreeListLocked() {
2183     int status = pthread_mutex_unlock(&_freelist_lock);
2184     assert_status(status == 0, status, "freelist unlock");
2185   }
2186 };
2187 
2188 os::PlatformMutex::PlatformMutex() {
2189   {
2190     WithFreeListLocked wfl;
2191     _impl = _mutex_freelist;
2192     if (_impl != NULL) {
2193       _mutex_freelist = _impl->_next;
2194       _impl->_next = NULL;
2195       return;
2196     }
2197   }
2198   _impl = new Mutex();
2199 }
2200 
2201 os::PlatformMutex::~PlatformMutex() {
2202   WithFreeListLocked wfl;
2203   assert(_impl->_next == NULL, "invariant");
2204   _impl->_next = _mutex_freelist;
2205   _mutex_freelist = _impl;
2206 }
2207 
2208 os::PlatformMonitor::Cond::Cond() : _next(NULL) {
2209   int status = pthread_cond_init(&_cond, _condAttr);
2210   assert_status(status == 0, status, "cond_init");
2211 }
2212 
2213 os::PlatformMonitor::Cond::~Cond() {
2214   int status = pthread_cond_destroy(&_cond);
2215   assert_status(status == 0, status, "cond_destroy");
2216 }
2217 
2218 os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;
2219 
2220 os::PlatformMonitor::PlatformMonitor() {
2221   {
2222     WithFreeListLocked wfl;
2223     _impl = _cond_freelist;
2224     if (_impl != NULL) {
2225       _cond_freelist = _impl->_next;
2226       _impl->_next = NULL;
2227       return;
2228     }
2229   }
2230   _impl = new Cond();
2231 }
2232 
2233 os::PlatformMonitor::~PlatformMonitor() {
2234   WithFreeListLocked wfl;
2235   assert(_impl->_next == NULL, "invariant");
2236   _impl->_next = _cond_freelist;
2237   _cond_freelist = _impl;
2238 }
2239 
2240 #else
2241 
2242 os::PlatformMutex::PlatformMutex() {
2243   int status = pthread_mutex_init(&_mutex, _mutexAttr);
2244   assert_status(status == 0, status, "mutex_init");
2245 }
2246 
2247 os::PlatformMutex::~PlatformMutex() {
2248   int status = pthread_mutex_destroy(&_mutex);
2249   assert_status(status == 0, status, "mutex_destroy");
2250 }
2251 
2252 os::PlatformMonitor::PlatformMonitor() {
2253   int status = pthread_cond_init(&_cond, _condAttr);
2254   assert_status(status == 0, status, "cond_init");
2255 }
2256 
2257 os::PlatformMonitor::~PlatformMonitor() {
2258   int status = pthread_cond_destroy(&_cond);
2259   assert_status(status == 0, status, "cond_destroy");
2260 }
2261 
2262 #endif // PLATFORM_MONITOR_IMPL_INDIRECT
2263 
2264 // Must already be locked
2265 int os::PlatformMonitor::wait(jlong millis) {
2266   assert(millis >= 0, "negative timeout");
2267   if (millis > 0) {
2268     struct timespec abst;
2269     // We have to watch for overflow when converting millis to nanos,
2270     // but if millis is that large then we will end up limiting to
2271     // MAX_SECS anyway, so just do that here.
2272     if (millis / MILLIUNITS > MAX_SECS) {
2273       millis = jlong(MAX_SECS) * MILLIUNITS;
2274     }
2275     to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false, false);
2276 
2277     int ret = OS_TIMEOUT;
2278     int status = pthread_cond_timedwait(cond(), mutex(), &abst);
2279     assert_status(status == 0 || status == ETIMEDOUT,
2280                   status, "cond_timedwait");
2281     if (status == 0) {
2282       ret = OS_OK;
2283     }
2284     return ret;
2285   } else {
2286     int status = pthread_cond_wait(cond(), mutex());
2287     assert_status(status == 0, status, "cond_wait");
2288     return OS_OK;
2289   }
2290 }
2291 
2292 #endif // !SOLARIS