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