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 "runtime/atomic.hpp"
  34 #include "runtime/orderAccess.hpp"
  35 #include "utilities/align.hpp"
  36 #include "utilities/events.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 #include "utilities/macros.hpp"
  39 #include "utilities/vmError.hpp"
  40 
  41 #include <dirent.h>
  42 #include <dlfcn.h>
  43 #include <grp.h>
  44 #include <pwd.h>
  45 #include <pthread.h>
  46 #include <signal.h>
  47 #include <sys/mman.h>
  48 #include <sys/resource.h>
  49 #include <sys/utsname.h>
  50 #include <time.h>
  51 #include <unistd.h>
  52 
  53 // Todo: provide a os::get_max_process_id() or similar. Number of processes
  54 // may have been configured, can be read more accurately from proc fs etc.
  55 #ifndef MAX_PID
  56 #define MAX_PID INT_MAX
  57 #endif
  58 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
  59 
  60 #define ROOT_UID 0
  61 
  62 #ifndef MAP_ANONYMOUS
  63   #define MAP_ANONYMOUS MAP_ANON
  64 #endif
  65 
  66 #define check_with_errno(check_type, cond, msg)                             \
  67   do {                                                                      \
  68     int err = errno;                                                        \
  69     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  70                os::errno_name(err));                                        \
  71 } while (false)
  72 
  73 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  74 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  75 
  76 // Check core dump limit and report possible place where core can be found
  77 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  78   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  79     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
  80     VMError::record_coredump_status(buffer, false);
  81     return;
  82   }
  83 
  84   int n;
  85   struct rlimit rlim;
  86   bool success;
  87 
  88   char core_path[PATH_MAX];
  89   n = get_core_path(core_path, PATH_MAX);
  90 
  91   if (n <= 0) {
  92     jio_snprintf(buffer, bufferSize, "core.%d (may not exist)", current_process_id());
  93     success = true;
  94 #ifdef LINUX
  95   } else if (core_path[0] == '"') { // redirect to user process
  96     jio_snprintf(buffer, bufferSize, "Core dumps may be processed with %s", core_path);
  97     success = true;
  98 #endif
  99   } else if (getrlimit(RLIMIT_CORE, &rlim) != 0) {
 100     jio_snprintf(buffer, bufferSize, "%s (may not exist)", core_path);
 101     success = true;
 102   } else {
 103     switch(rlim.rlim_cur) {
 104       case RLIM_INFINITY:
 105         jio_snprintf(buffer, bufferSize, "%s", core_path);
 106         success = true;
 107         break;
 108       case 0:
 109         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
 110         success = false;
 111         break;
 112       default:
 113         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);
 114         success = true;
 115         break;
 116     }
 117   }
 118 
 119   VMError::record_coredump_status(buffer, success);
 120 }
 121 
 122 int os::get_native_stack(address* stack, int frames, int toSkip) {
 123   int frame_idx = 0;
 124   int num_of_frames;  // number of frames captured
 125   frame fr = os::current_frame();
 126   while (fr.pc() && frame_idx < frames) {
 127     if (toSkip > 0) {
 128       toSkip --;
 129     } else {
 130       stack[frame_idx ++] = fr.pc();
 131     }
 132     if (fr.fp() == NULL || fr.cb() != NULL ||
 133         fr.sender_pc() == NULL || os::is_first_C_frame(&fr)) break;
 134 
 135     if (fr.sender_pc() && !os::is_first_C_frame(&fr)) {
 136       fr = os::get_sender_for_C_frame(&fr);
 137     } else {
 138       break;
 139     }
 140   }
 141   num_of_frames = frame_idx;
 142   for (; frame_idx < frames; frame_idx ++) {
 143     stack[frame_idx] = NULL;
 144   }
 145 
 146   return num_of_frames;
 147 }
 148 
 149 
 150 bool os::unsetenv(const char* name) {
 151   assert(name != NULL, "Null pointer");
 152   return (::unsetenv(name) == 0);
 153 }
 154 
 155 int os::get_last_error() {
 156   return errno;
 157 }
 158 
 159 size_t os::lasterror(char *buf, size_t len) {
 160   if (errno == 0)  return 0;
 161 
 162   const char *s = os::strerror(errno);
 163   size_t n = ::strlen(s);
 164   if (n >= len) {
 165     n = len - 1;
 166   }
 167   ::strncpy(buf, s, n);
 168   buf[n] = '\0';
 169   return n;
 170 }
 171 
 172 void os::wait_for_keypress_at_exit(void) {
 173   // don't do anything on posix platforms
 174   return;
 175 }
 176 
 177 int os::create_file_for_heap(const char* dir) {
 178 
 179   const char name_template[] = "/jvmheap.XXXXXX";
 180 
 181   size_t fullname_len = strlen(dir) + strlen(name_template);
 182   char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);
 183   if (fullname == NULL) {
 184     vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 185     return -1;
 186   }
 187   int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);
 188   assert((size_t)n == fullname_len, "Unexpected number of characters in string");
 189 
 190   os::native_path(fullname);
 191 
 192   // set the file creation mask.
 193   mode_t file_mode = S_IRUSR | S_IWUSR;
 194 
 195   // create a new file.
 196   int fd = mkstemp(fullname);
 197 
 198   if (fd < 0) {
 199     warning("Could not create file for heap with template %s", fullname);
 200     os::free(fullname);
 201     return -1;
 202   }
 203 
 204   // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 205   int ret = unlink(fullname);
 206   assert_with_errno(ret == 0, "unlink returned error");
 207 
 208   os::free(fullname);
 209   return fd;
 210 }
 211 
 212 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 213   char * addr;
 214   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 215   if (requested_addr != NULL) {
 216     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 217     flags |= MAP_FIXED;
 218   }
 219 
 220   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 221   // touch an uncommitted page. Otherwise, the read/write might
 222   // succeed if we have enough swap space to back the physical page.
 223   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 224                        flags, -1, 0);
 225 
 226   if (addr != MAP_FAILED) {
 227     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 228     return addr;
 229   }
 230   return NULL;
 231 }
 232 
 233 static int util_posix_fallocate(int fd, off_t offset, off_t len) {
 234 #ifdef __APPLE__
 235   fstore_t store = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, len };
 236   // First we try to get a continuous chunk of disk space
 237   int ret = fcntl(fd, F_PREALLOCATE, &store);
 238   if (ret == -1) {
 239     // Maybe we are too fragmented, try to allocate non-continuous range
 240     store.fst_flags = F_ALLOCATEALL;
 241     ret = fcntl(fd, F_PREALLOCATE, &store);
 242   }
 243   if(ret != -1) {
 244     return ftruncate(fd, len);
 245   }
 246   return -1;
 247 #else
 248   return posix_fallocate(fd, offset, len);
 249 #endif
 250 }
 251 
 252 // Map the given address range to the provided file descriptor.
 253 char* os::map_memory_to_file(char* base, size_t size, int fd) {
 254   assert(fd != -1, "File descriptor is not valid");
 255 
 256   // allocate space for the file
 257   int ret = util_posix_fallocate(fd, 0, (off_t)size);
 258   if (ret != 0) {
 259     vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory. error(%d)", ret));
 260     return NULL;
 261   }
 262 
 263   int prot = PROT_READ | PROT_WRITE;
 264   int flags = MAP_SHARED;
 265   if (base != NULL) {
 266     flags |= MAP_FIXED;
 267   }
 268   char* addr = (char*)mmap(base, size, prot, flags, fd, 0);
 269 
 270   if (addr == MAP_FAILED) {
 271     warning("Failed mmap to file. (%s)", os::strerror(errno));
 272     return NULL;
 273   }
 274   if (base != NULL && addr != base) {
 275     if (!os::release_memory(addr, size)) {
 276       warning("Could not release memory on unsuccessful file mapping");
 277     }
 278     return NULL;
 279   }
 280   return addr;
 281 }
 282 
 283 char* os::replace_existing_mapping_with_file_mapping(char* base, size_t size, int fd) {
 284   assert(fd != -1, "File descriptor is not valid");
 285   assert(base != NULL, "Base cannot be NULL");
 286 
 287   return map_memory_to_file(base, size, fd);
 288 }
 289 
 290 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 291 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 292 // rather than unmapping and remapping the whole chunk to get requested alignment.
 293 char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
 294   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 295       "Alignment must be a multiple of allocation granularity (page size)");
 296   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 297 
 298   size_t extra_size = size + alignment;
 299   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 300 
 301   char* extra_base;
 302   if (file_desc != -1) {
 303     // For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because
 304     // we need to deal with shrinking of the file space later when we release extra memory after alignment.
 305     // We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory.
 306     // So here to call a helper function while reserve memory for us. After we have a aligned base,
 307     // we will replace anonymous mapping with file mapping.
 308     extra_base = reserve_mmapped_memory(extra_size, NULL);
 309     if (extra_base != NULL) {
 310       MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC);
 311     }
 312   } else {
 313     extra_base = os::reserve_memory(extra_size, NULL, alignment);
 314   }
 315 
 316   if (extra_base == NULL) {
 317     return NULL;
 318   }
 319 
 320   // Do manual alignment
 321   char* aligned_base = align_up(extra_base, alignment);
 322 
 323   // [  |                                       |  ]
 324   // ^ extra_base
 325   //    ^ extra_base + begin_offset == aligned_base
 326   //     extra_base + begin_offset + size       ^
 327   //                       extra_base + extra_size ^
 328   // |<>| == begin_offset
 329   //                              end_offset == |<>|
 330   size_t begin_offset = aligned_base - extra_base;
 331   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 332 
 333   if (begin_offset > 0) {
 334       os::release_memory(extra_base, begin_offset);
 335   }
 336 
 337   if (end_offset > 0) {
 338       os::release_memory(extra_base + begin_offset + size, end_offset);
 339   }
 340 
 341   if (file_desc != -1) {
 342     // After we have an aligned address, we can replace anonymous mapping with file mapping
 343     if (replace_existing_mapping_with_file_mapping(aligned_base, size, file_desc) == NULL) {
 344       vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
 345     }
 346     MemTracker::record_virtual_memory_commit((address)aligned_base, size, CALLER_PC);
 347   }
 348   return aligned_base;
 349 }
 350 
 351 int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
 352   // All supported POSIX platforms provide C99 semantics.
 353   int result = ::vsnprintf(buf, len, fmt, args);
 354   // If an encoding error occurred (result < 0) then it's not clear
 355   // whether the buffer is NUL terminated, so ensure it is.
 356   if ((result < 0) && (len > 0)) {
 357     buf[len - 1] = '\0';
 358   }
 359   return result;
 360 }
 361 
 362 int os::get_fileno(FILE* fp) {
 363   return NOT_AIX(::)fileno(fp);
 364 }
 365 
 366 struct tm* os::gmtime_pd(const time_t* clock, struct tm*  res) {
 367   return gmtime_r(clock, res);
 368 }
 369 
 370 void os::Posix::print_load_average(outputStream* st) {
 371   st->print("load average:");
 372   double loadavg[3];
 373   int res = os::loadavg(loadavg, 3);
 374   if (res != -1) {
 375     st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
 376   } else {
 377     st->print(" Unavailable");
 378   }
 379   st->cr();
 380 }
 381 
 382 void os::Posix::print_rlimit_info(outputStream* st) {
 383   st->print("rlimit:");
 384   struct rlimit rlim;
 385 
 386   st->print(" STACK ");
 387   getrlimit(RLIMIT_STACK, &rlim);
 388   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 389   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 390 
 391   st->print(", CORE ");
 392   getrlimit(RLIMIT_CORE, &rlim);
 393   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 394   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 395 
 396   // Isn't there on solaris
 397 #if defined(AIX)
 398   st->print(", NPROC ");
 399   st->print("%d", sysconf(_SC_CHILD_MAX));
 400 #elif !defined(SOLARIS)
 401   st->print(", NPROC ");
 402   getrlimit(RLIMIT_NPROC, &rlim);
 403   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 404   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 405 #endif
 406 
 407   st->print(", NOFILE ");
 408   getrlimit(RLIMIT_NOFILE, &rlim);
 409   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 410   else st->print(UINT64_FORMAT, uint64_t(rlim.rlim_cur));
 411 
 412   st->print(", AS ");
 413   getrlimit(RLIMIT_AS, &rlim);
 414   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 415   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 416 
 417   st->print(", DATA ");
 418   getrlimit(RLIMIT_DATA, &rlim);
 419   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 420   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 421 
 422   st->print(", FSIZE ");
 423   getrlimit(RLIMIT_FSIZE, &rlim);
 424   if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
 425   else st->print(UINT64_FORMAT "k", uint64_t(rlim.rlim_cur) / 1024);
 426 
 427   st->cr();
 428 }
 429 
 430 void os::Posix::print_uname_info(outputStream* st) {
 431   // kernel
 432   st->print("uname:");
 433   struct utsname name;
 434   uname(&name);
 435   st->print("%s ", name.sysname);
 436 #ifdef ASSERT
 437   st->print("%s ", name.nodename);
 438 #endif
 439   st->print("%s ", name.release);
 440   st->print("%s ", name.version);
 441   st->print("%s", name.machine);
 442   st->cr();
 443 }
 444 
 445 void os::Posix::print_umask(outputStream* st, mode_t umsk) {
 446   st->print((umsk & S_IRUSR) ? "r" : "-");
 447   st->print((umsk & S_IWUSR) ? "w" : "-");
 448   st->print((umsk & S_IXUSR) ? "x" : "-");
 449   st->print((umsk & S_IRGRP) ? "r" : "-");
 450   st->print((umsk & S_IWGRP) ? "w" : "-");
 451   st->print((umsk & S_IXGRP) ? "x" : "-");
 452   st->print((umsk & S_IROTH) ? "r" : "-");
 453   st->print((umsk & S_IWOTH) ? "w" : "-");
 454   st->print((umsk & S_IXOTH) ? "x" : "-");
 455 }
 456 
 457 void os::Posix::print_user_info(outputStream* st) {
 458   unsigned id = (unsigned) ::getuid();
 459   st->print("uid  : %u ", id);
 460   id = (unsigned) ::geteuid();
 461   st->print("euid : %u ", id);
 462   id = (unsigned) ::getgid();
 463   st->print("gid  : %u ", id);
 464   id = (unsigned) ::getegid();
 465   st->print_cr("egid : %u", id);
 466   st->cr();
 467 
 468   mode_t umsk = ::umask(0);
 469   ::umask(umsk);
 470   st->print("umask: %04o (", (unsigned) umsk);
 471   print_umask(st, umsk);
 472   st->print_cr(")");
 473   st->cr();
 474 }
 475 
 476 
 477 bool os::get_host_name(char* buf, size_t buflen) {
 478   struct utsname name;
 479   uname(&name);
 480   jio_snprintf(buf, buflen, "%s", name.nodename);
 481   return true;
 482 }
 483 
 484 bool os::has_allocatable_memory_limit(julong* limit) {
 485   struct rlimit rlim;
 486   int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
 487   // if there was an error when calling getrlimit, assume that there is no limitation
 488   // on virtual memory.
 489   bool result;
 490   if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
 491     result = false;
 492   } else {
 493     *limit = (julong)rlim.rlim_cur;
 494     result = true;
 495   }
 496 #ifdef _LP64
 497   return result;
 498 #else
 499   // arbitrary virtual space limit for 32 bit Unices found by testing. If
 500   // getrlimit above returned a limit, bound it with this limit. Otherwise
 501   // directly use it.
 502   const julong max_virtual_limit = (julong)3800*M;
 503   if (result) {
 504     *limit = MIN2(*limit, max_virtual_limit);
 505   } else {
 506     *limit = max_virtual_limit;
 507   }
 508 
 509   // bound by actually allocatable memory. The algorithm uses two bounds, an
 510   // upper and a lower limit. The upper limit is the current highest amount of
 511   // memory that could not be allocated, the lower limit is the current highest
 512   // amount of memory that could be allocated.
 513   // The algorithm iteratively refines the result by halving the difference
 514   // between these limits, updating either the upper limit (if that value could
 515   // not be allocated) or the lower limit (if the that value could be allocated)
 516   // until the difference between these limits is "small".
 517 
 518   // the minimum amount of memory we care about allocating.
 519   const julong min_allocation_size = M;
 520 
 521   julong upper_limit = *limit;
 522 
 523   // first check a few trivial cases
 524   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 525     *limit = upper_limit;
 526   } else if (!is_allocatable(min_allocation_size)) {
 527     // we found that not even min_allocation_size is allocatable. Return it
 528     // anyway. There is no point to search for a better value any more.
 529     *limit = min_allocation_size;
 530   } else {
 531     // perform the binary search.
 532     julong lower_limit = min_allocation_size;
 533     while ((upper_limit - lower_limit) > min_allocation_size) {
 534       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 535       temp_limit = align_down(temp_limit, min_allocation_size);
 536       if (is_allocatable(temp_limit)) {
 537         lower_limit = temp_limit;
 538       } else {
 539         upper_limit = temp_limit;
 540       }
 541     }
 542     *limit = lower_limit;
 543   }
 544   return true;
 545 #endif
 546 }
 547 
 548 const char* os::get_current_directory(char *buf, size_t buflen) {
 549   return getcwd(buf, buflen);
 550 }
 551 
 552 FILE* os::open(int fd, const char* mode) {
 553   return ::fdopen(fd, mode);
 554 }
 555 
 556 ssize_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
 557   return ::pread(fd, buf, nBytes, offset);
 558 }
 559 
 560 void os::flockfile(FILE* fp) {
 561   ::flockfile(fp);
 562 }
 563 
 564 void os::funlockfile(FILE* fp) {
 565   ::funlockfile(fp);
 566 }
 567 
 568 DIR* os::opendir(const char* dirname) {
 569   assert(dirname != NULL, "just checking");
 570   return ::opendir(dirname);
 571 }
 572 
 573 struct dirent* os::readdir(DIR* dirp) {
 574   assert(dirp != NULL, "just checking");
 575   return ::readdir(dirp);
 576 }
 577 
 578 int os::closedir(DIR *dirp) {
 579   assert(dirp != NULL, "just checking");
 580   return ::closedir(dirp);
 581 }
 582 
 583 // Builds a platform dependent Agent_OnLoad_<lib_name> function name
 584 // which is used to find statically linked in agents.
 585 // Parameters:
 586 //            sym_name: Symbol in library we are looking for
 587 //            lib_name: Name of library to look in, NULL for shared libs.
 588 //            is_absolute_path == true if lib_name is absolute path to agent
 589 //                                     such as "/a/b/libL.so"
 590 //            == false if only the base name of the library is passed in
 591 //               such as "L"
 592 char* os::build_agent_function_name(const char *sym_name, const char *lib_name,
 593                                     bool is_absolute_path) {
 594   char *agent_entry_name;
 595   size_t len;
 596   size_t name_len;
 597   size_t prefix_len = strlen(JNI_LIB_PREFIX);
 598   size_t suffix_len = strlen(JNI_LIB_SUFFIX);
 599   const char *start;
 600 
 601   if (lib_name != NULL) {
 602     name_len = strlen(lib_name);
 603     if (is_absolute_path) {
 604       // Need to strip path, prefix and suffix
 605       if ((start = strrchr(lib_name, *os::file_separator())) != NULL) {
 606         lib_name = ++start;
 607       }
 608       if (strlen(lib_name) <= (prefix_len + suffix_len)) {
 609         return NULL;
 610       }
 611       lib_name += prefix_len;
 612       name_len = strlen(lib_name) - suffix_len;
 613     }
 614   }
 615   len = (lib_name != NULL ? name_len : 0) + strlen(sym_name) + 2;
 616   agent_entry_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, len, mtThread);
 617   if (agent_entry_name == NULL) {
 618     return NULL;
 619   }
 620   strcpy(agent_entry_name, sym_name);
 621   if (lib_name != NULL) {
 622     strcat(agent_entry_name, "_");
 623     strncat(agent_entry_name, lib_name, name_len);
 624   }
 625   return agent_entry_name;
 626 }
 627 
 628 
 629 void os::naked_short_nanosleep(jlong ns) {
 630   struct timespec req;
 631   assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");
 632   req.tv_sec = 0;
 633   req.tv_nsec = ns;
 634   ::nanosleep(&req, NULL);
 635   return;
 636 }
 637 
 638 void os::naked_short_sleep(jlong ms) {
 639   assert(ms < MILLIUNITS, "Un-interruptable sleep, short time use only");
 640   os::naked_short_nanosleep(ms * (NANOUNITS / MILLIUNITS));
 641   return;
 642 }
 643 
 644 static const struct {
 645   int sig; const char* name;
 646 }
 647  g_signal_info[] =
 648   {
 649   {  SIGABRT,     "SIGABRT" },
 650 #ifdef SIGAIO
 651   {  SIGAIO,      "SIGAIO" },
 652 #endif
 653   {  SIGALRM,     "SIGALRM" },
 654 #ifdef SIGALRM1
 655   {  SIGALRM1,    "SIGALRM1" },
 656 #endif
 657   {  SIGBUS,      "SIGBUS" },
 658 #ifdef SIGCANCEL
 659   {  SIGCANCEL,   "SIGCANCEL" },
 660 #endif
 661   {  SIGCHLD,     "SIGCHLD" },
 662 #ifdef SIGCLD
 663   {  SIGCLD,      "SIGCLD" },
 664 #endif
 665   {  SIGCONT,     "SIGCONT" },
 666 #ifdef SIGCPUFAIL
 667   {  SIGCPUFAIL,  "SIGCPUFAIL" },
 668 #endif
 669 #ifdef SIGDANGER
 670   {  SIGDANGER,   "SIGDANGER" },
 671 #endif
 672 #ifdef SIGDIL
 673   {  SIGDIL,      "SIGDIL" },
 674 #endif
 675 #ifdef SIGEMT
 676   {  SIGEMT,      "SIGEMT" },
 677 #endif
 678   {  SIGFPE,      "SIGFPE" },
 679 #ifdef SIGFREEZE
 680   {  SIGFREEZE,   "SIGFREEZE" },
 681 #endif
 682 #ifdef SIGGFAULT
 683   {  SIGGFAULT,   "SIGGFAULT" },
 684 #endif
 685 #ifdef SIGGRANT
 686   {  SIGGRANT,    "SIGGRANT" },
 687 #endif
 688   {  SIGHUP,      "SIGHUP" },
 689   {  SIGILL,      "SIGILL" },
 690 #ifdef SIGINFO
 691   {  SIGINFO,     "SIGINFO" },
 692 #endif
 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(&_event, v, v - 1) == 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(&_event, v, v - 1) == 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(&_event, 1) >= 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(&_counter, 0) > 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   // Can't access interrupt state now that we are _thread_blocked. If we've
2081   // been interrupted since we checked above then _counter will be > 0.
2082 
2083   // Don't wait if cannot get lock since interference arises from
2084   // unparking.
2085   if (pthread_mutex_trylock(_mutex) != 0) {
2086     return;
2087   }
2088 
2089   int status;
2090   if (_counter > 0)  { // no wait needed
2091     _counter = 0;
2092     status = pthread_mutex_unlock(_mutex);
2093     assert_status(status == 0, status, "invariant");
2094     // Paranoia to ensure our locked and lock-free paths interact
2095     // correctly with each other and Java-level accesses.
2096     OrderAccess::fence();
2097     return;
2098   }
2099 
2100   OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
2101   jt->set_suspend_equivalent();
2102   // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
2103 
2104   assert(_cur_index == -1, "invariant");
2105   if (time == 0) {
2106     _cur_index = REL_INDEX; // arbitrary choice when not timed
2107     status = pthread_cond_wait(&_cond[_cur_index], _mutex);
2108     assert_status(status == 0, status, "cond_timedwait");
2109   }
2110   else {
2111     _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
2112     status = pthread_cond_timedwait(&_cond[_cur_index], _mutex, &absTime);
2113     assert_status(status == 0 || status == ETIMEDOUT,
2114                   status, "cond_timedwait");
2115   }
2116   _cur_index = -1;
2117 
2118   _counter = 0;
2119   status = pthread_mutex_unlock(_mutex);
2120   assert_status(status == 0, status, "invariant");
2121   // Paranoia to ensure our locked and lock-free paths interact
2122   // correctly with each other and Java-level accesses.
2123   OrderAccess::fence();
2124 
2125   // If externally suspended while waiting, re-suspend
2126   if (jt->handle_special_suspend_equivalent_condition()) {
2127     jt->java_suspend_self();
2128   }
2129 }
2130 
2131 void Parker::unpark() {
2132   int status = pthread_mutex_lock(_mutex);
2133   assert_status(status == 0, status, "invariant");
2134   const int s = _counter;
2135   _counter = 1;
2136   // must capture correct index before unlocking
2137   int index = _cur_index;
2138   status = pthread_mutex_unlock(_mutex);
2139   assert_status(status == 0, status, "invariant");
2140 
2141   // Note that we signal() *after* dropping the lock for "immortal" Events.
2142   // This is safe and avoids a common class of futile wakeups.  In rare
2143   // circumstances this can cause a thread to return prematurely from
2144   // cond_{timed}wait() but the spurious wakeup is benign and the victim
2145   // will simply re-test the condition and re-park itself.
2146   // This provides particular benefit if the underlying platform does not
2147   // provide wait morphing.
2148 
2149   if (s < 1 && index != -1) {
2150     // thread is definitely parked
2151     status = pthread_cond_signal(&_cond[index]);
2152     assert_status(status == 0, status, "invariant");
2153   }
2154 }
2155 
2156 // Platform Mutex/Monitor implementation
2157 
2158 #if PLATFORM_MONITOR_IMPL_INDIRECT
2159 
2160 os::PlatformMutex::Mutex::Mutex() : _next(NULL) {
2161   int status = pthread_mutex_init(&_mutex, _mutexAttr);
2162   assert_status(status == 0, status, "mutex_init");
2163 }
2164 
2165 os::PlatformMutex::Mutex::~Mutex() {
2166   int status = pthread_mutex_destroy(&_mutex);
2167   assert_status(status == 0, status, "mutex_destroy");
2168 }
2169 
2170 pthread_mutex_t os::PlatformMutex::_freelist_lock;
2171 os::PlatformMutex::Mutex* os::PlatformMutex::_mutex_freelist = NULL;
2172 
2173 void os::PlatformMutex::init() {
2174   int status = pthread_mutex_init(&_freelist_lock, _mutexAttr);
2175   assert_status(status == 0, status, "freelist lock init");
2176 }
2177 
2178 struct os::PlatformMutex::WithFreeListLocked : public StackObj {
2179   WithFreeListLocked() {
2180     int status = pthread_mutex_lock(&_freelist_lock);
2181     assert_status(status == 0, status, "freelist lock");
2182   }
2183 
2184   ~WithFreeListLocked() {
2185     int status = pthread_mutex_unlock(&_freelist_lock);
2186     assert_status(status == 0, status, "freelist unlock");
2187   }
2188 };
2189 
2190 os::PlatformMutex::PlatformMutex() {
2191   {
2192     WithFreeListLocked wfl;
2193     _impl = _mutex_freelist;
2194     if (_impl != NULL) {
2195       _mutex_freelist = _impl->_next;
2196       _impl->_next = NULL;
2197       return;
2198     }
2199   }
2200   _impl = new Mutex();
2201 }
2202 
2203 os::PlatformMutex::~PlatformMutex() {
2204   WithFreeListLocked wfl;
2205   assert(_impl->_next == NULL, "invariant");
2206   _impl->_next = _mutex_freelist;
2207   _mutex_freelist = _impl;
2208 }
2209 
2210 os::PlatformMonitor::Cond::Cond() : _next(NULL) {
2211   int status = pthread_cond_init(&_cond, _condAttr);
2212   assert_status(status == 0, status, "cond_init");
2213 }
2214 
2215 os::PlatformMonitor::Cond::~Cond() {
2216   int status = pthread_cond_destroy(&_cond);
2217   assert_status(status == 0, status, "cond_destroy");
2218 }
2219 
2220 os::PlatformMonitor::Cond* os::PlatformMonitor::_cond_freelist = NULL;
2221 
2222 os::PlatformMonitor::PlatformMonitor() {
2223   {
2224     WithFreeListLocked wfl;
2225     _impl = _cond_freelist;
2226     if (_impl != NULL) {
2227       _cond_freelist = _impl->_next;
2228       _impl->_next = NULL;
2229       return;
2230     }
2231   }
2232   _impl = new Cond();
2233 }
2234 
2235 os::PlatformMonitor::~PlatformMonitor() {
2236   WithFreeListLocked wfl;
2237   assert(_impl->_next == NULL, "invariant");
2238   _impl->_next = _cond_freelist;
2239   _cond_freelist = _impl;
2240 }
2241 
2242 #else
2243 
2244 os::PlatformMutex::PlatformMutex() {
2245   int status = pthread_mutex_init(&_mutex, _mutexAttr);
2246   assert_status(status == 0, status, "mutex_init");
2247 }
2248 
2249 os::PlatformMutex::~PlatformMutex() {
2250   int status = pthread_mutex_destroy(&_mutex);
2251   assert_status(status == 0, status, "mutex_destroy");
2252 }
2253 
2254 os::PlatformMonitor::PlatformMonitor() {
2255   int status = pthread_cond_init(&_cond, _condAttr);
2256   assert_status(status == 0, status, "cond_init");
2257 }
2258 
2259 os::PlatformMonitor::~PlatformMonitor() {
2260   int status = pthread_cond_destroy(&_cond);
2261   assert_status(status == 0, status, "cond_destroy");
2262 }
2263 
2264 #endif // PLATFORM_MONITOR_IMPL_INDIRECT
2265 
2266 // Must already be locked
2267 int os::PlatformMonitor::wait(jlong millis) {
2268   assert(millis >= 0, "negative timeout");
2269   if (millis > 0) {
2270     struct timespec abst;
2271     // We have to watch for overflow when converting millis to nanos,
2272     // but if millis is that large then we will end up limiting to
2273     // MAX_SECS anyway, so just do that here.
2274     if (millis / MILLIUNITS > MAX_SECS) {
2275       millis = jlong(MAX_SECS) * MILLIUNITS;
2276     }
2277     to_abstime(&abst, millis * (NANOUNITS / MILLIUNITS), false, false);
2278 
2279     int ret = OS_TIMEOUT;
2280     int status = pthread_cond_timedwait(cond(), mutex(), &abst);
2281     assert_status(status == 0 || status == ETIMEDOUT,
2282                   status, "cond_timedwait");
2283     if (status == 0) {
2284       ret = OS_OK;
2285     }
2286     return ret;
2287   } else {
2288     int status = pthread_cond_wait(cond(), mutex());
2289     assert_status(status == 0, status, "cond_wait");
2290     return OS_OK;
2291   }
2292 }
2293 
2294 #endif // !SOLARIS