1 /*
   2  * Copyright (c) 1997, 2023, 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 "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/moduleEntry.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmClasses.hpp"
  30 #include "classfile/vmSymbols.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "gc/shared/gcVMOperations.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "jvm.h"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.hpp"
  42 #include "oops/compressedKlass.hpp"
  43 #include "oops/compressedOops.inline.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "prims/jvm_misc.hpp"
  46 #include "runtime/arguments.hpp"
  47 #include "runtime/atomic.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/interfaceSupport.inline.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/javaCalls.hpp"
  53 #include "runtime/javaThread.hpp"
  54 #include "runtime/jniHandles.hpp"
  55 #include "runtime/mutexLocker.hpp"
  56 #include "runtime/os.inline.hpp"
  57 #include "runtime/osThread.hpp"
  58 #include "runtime/safefetch.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/threadCrashProtection.hpp"
  61 #include "runtime/threadSMR.hpp"
  62 #include "runtime/vmOperations.hpp"
  63 #include "runtime/vm_version.hpp"
  64 #include "sanitizers/address.hpp"
  65 #include "services/attachListener.hpp"
  66 #include "services/mallocTracker.hpp"
  67 #include "services/mallocHeader.inline.hpp"
  68 #include "services/memTracker.inline.hpp"
  69 #include "services/nmtPreInit.hpp"
  70 #include "services/nmtCommon.hpp"
  71 #include "services/threadService.hpp"
  72 #include "utilities/align.hpp"
  73 #include "utilities/count_trailing_zeros.hpp"
  74 #include "utilities/defaultStream.hpp"
  75 #include "utilities/events.hpp"
  76 #include "utilities/powerOfTwo.hpp"
  77 
  78 #ifndef _WINDOWS
  79 # include <poll.h>
  80 #endif
  81 
  82 # include <signal.h>
  83 # include <errno.h>
  84 
  85 OSThread*         os::_starting_thread    = nullptr;
  86 volatile unsigned int os::_rand_seed      = 1234567;
  87 int               os::_processor_count    = 0;
  88 int               os::_initial_active_processor_count = 0;
  89 os::PageSizes     os::_page_sizes;
  90 
  91 DEBUG_ONLY(bool os::_mutex_init_done = false;)
  92 
  93 int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
  94   va_list args;
  95   va_start(args, fmt);
  96   int result = os::vsnprintf(buf, len, fmt, args);
  97   va_end(args);
  98   return result;
  99 }
 100 
 101 int os::snprintf_checked(char* buf, size_t len, const char* fmt, ...) {
 102   va_list args;
 103   va_start(args, fmt);
 104   int result = os::vsnprintf(buf, len, fmt, args);
 105   va_end(args);
 106   assert(result >= 0, "os::snprintf error");
 107   assert(static_cast<size_t>(result) < len, "os::snprintf truncated");
 108   return result;
 109 }
 110 
 111 // Fill in buffer with current local time as an ISO-8601 string.
 112 // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz.
 113 // Returns buffer, or null if it failed.
 114 char* os::iso8601_time(char* buffer, size_t buffer_length, bool utc) {
 115   const jlong now = javaTimeMillis();
 116   return os::iso8601_time(now, buffer, buffer_length, utc);
 117 }
 118 
 119 // Fill in buffer with an ISO-8601 string corresponding to the given javaTimeMillis value
 120 // E.g., yyyy-mm-ddThh:mm:ss-zzzz.
 121 // Returns buffer, or null if it failed.
 122 // This would mostly be a call to
 123 //     strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
 124 // except that on Windows the %z behaves badly, so we do it ourselves.
 125 // Also, people wanted milliseconds on there,
 126 // and strftime doesn't do milliseconds.
 127 char* os::iso8601_time(jlong milliseconds_since_19700101, char* buffer, size_t buffer_length, bool utc) {
 128   // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"
 129 
 130   // Sanity check the arguments
 131   if (buffer == nullptr) {
 132     assert(false, "null buffer");
 133     return nullptr;
 134   }
 135   if (buffer_length < os::iso8601_timestamp_size) {
 136     assert(false, "buffer_length too small");
 137     return nullptr;
 138   }
 139   const int milliseconds_per_microsecond = 1000;
 140   const time_t seconds_since_19700101 =
 141     milliseconds_since_19700101 / milliseconds_per_microsecond;
 142   const int milliseconds_after_second =
 143     milliseconds_since_19700101 % milliseconds_per_microsecond;
 144   // Convert the time value to a tm and timezone variable
 145   struct tm time_struct;
 146   if (utc) {
 147     if (gmtime_pd(&seconds_since_19700101, &time_struct) == nullptr) {
 148       assert(false, "Failed gmtime_pd");
 149       return nullptr;
 150     }
 151   } else {
 152     if (localtime_pd(&seconds_since_19700101, &time_struct) == nullptr) {
 153       assert(false, "Failed localtime_pd");
 154       return nullptr;
 155     }
 156   }
 157 
 158   const time_t seconds_per_minute = 60;
 159   const time_t minutes_per_hour = 60;
 160   const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
 161 
 162   // No offset when dealing with UTC
 163   time_t UTC_to_local = 0;
 164   if (!utc) {
 165 #if defined(_ALLBSD_SOURCE) || defined(_GNU_SOURCE)
 166     UTC_to_local = -(time_struct.tm_gmtoff);
 167 #elif defined(_WINDOWS)
 168     long zone;
 169     _get_timezone(&zone);
 170     UTC_to_local = static_cast<time_t>(zone);
 171 #else
 172     UTC_to_local = timezone;
 173 #endif
 174 
 175     // tm_gmtoff already includes adjustment for daylight saving
 176 #if !defined(_ALLBSD_SOURCE) && !defined(_GNU_SOURCE)
 177     // If daylight savings time is in effect,
 178     // we are 1 hour East of our time zone
 179     if (time_struct.tm_isdst > 0) {
 180       UTC_to_local = UTC_to_local - seconds_per_hour;
 181     }
 182 #endif
 183   }
 184 
 185   // Compute the time zone offset.
 186   //    localtime_pd() sets timezone to the difference (in seconds)
 187   //    between UTC and local time.
 188   //    ISO 8601 says we need the difference between local time and UTC,
 189   //    we change the sign of the localtime_pd() result.
 190   const time_t local_to_UTC = -(UTC_to_local);
 191   // Then we have to figure out if if we are ahead (+) or behind (-) UTC.
 192   char sign_local_to_UTC = '+';
 193   time_t abs_local_to_UTC = local_to_UTC;
 194   if (local_to_UTC < 0) {
 195     sign_local_to_UTC = '-';
 196     abs_local_to_UTC = -(abs_local_to_UTC);
 197   }
 198   // Convert time zone offset seconds to hours and minutes.
 199   const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour);
 200   const time_t zone_min =
 201     ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
 202 
 203   // Print an ISO 8601 date and time stamp into the buffer
 204   const int year = 1900 + time_struct.tm_year;
 205   const int month = 1 + time_struct.tm_mon;
 206   const int printed = jio_snprintf(buffer, buffer_length,
 207                                    "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d",
 208                                    year,
 209                                    month,
 210                                    time_struct.tm_mday,
 211                                    time_struct.tm_hour,
 212                                    time_struct.tm_min,
 213                                    time_struct.tm_sec,
 214                                    milliseconds_after_second,
 215                                    sign_local_to_UTC,
 216                                    zone_hours,
 217                                    zone_min);
 218   if (printed == 0) {
 219     assert(false, "Failed jio_printf");
 220     return nullptr;
 221   }
 222   return buffer;
 223 }
 224 
 225 OSReturn os::set_priority(Thread* thread, ThreadPriority p) {
 226   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 227 
 228   if ((p >= MinPriority && p <= MaxPriority) ||
 229       (p == CriticalPriority && thread->is_ConcurrentGC_thread())) {
 230     int priority = java_to_os_priority[p];
 231     return set_native_priority(thread, priority);
 232   } else {
 233     assert(false, "Should not happen");
 234     return OS_ERR;
 235   }
 236 }
 237 
 238 // The mapping from OS priority back to Java priority may be inexact because
 239 // Java priorities can map M:1 with native priorities. If you want the definite
 240 // Java priority then use JavaThread::java_priority()
 241 OSReturn os::get_priority(const Thread* const thread, ThreadPriority& priority) {
 242   int p;
 243   int os_prio;
 244   OSReturn ret = get_native_priority(thread, &os_prio);
 245   if (ret != OS_OK) return ret;
 246 
 247   if (java_to_os_priority[MaxPriority] > java_to_os_priority[MinPriority]) {
 248     for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] > os_prio; p--) ;
 249   } else {
 250     // niceness values are in reverse order
 251     for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] < os_prio; p--) ;
 252   }
 253   priority = (ThreadPriority)p;
 254   return OS_OK;
 255 }
 256 
 257 bool os::dll_build_name(char* buffer, size_t size, const char* fname) {
 258   int n = jio_snprintf(buffer, size, "%s%s%s", JNI_LIB_PREFIX, fname, JNI_LIB_SUFFIX);
 259   return (n != -1);
 260 }
 261 
 262 #if !defined(LINUX) && !defined(_WINDOWS)
 263 bool os::committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size) {
 264   committed_start = start;
 265   committed_size = size;
 266   return true;
 267 }
 268 #endif
 269 
 270 // Helper for dll_locate_lib.
 271 // Pass buffer and printbuffer as we already printed the path to buffer
 272 // when we called get_current_directory. This way we avoid another buffer
 273 // of size MAX_PATH.
 274 static bool conc_path_file_and_check(char *buffer, char *printbuffer, size_t printbuflen,
 275                                      const char* pname, char lastchar, const char* fname) {
 276 
 277   // Concatenate path and file name, but don't print double path separators.
 278   const char *filesep = (WINDOWS_ONLY(lastchar == ':' ||) lastchar == os::file_separator()[0]) ?
 279                         "" : os::file_separator();
 280   int ret = jio_snprintf(printbuffer, printbuflen, "%s%s%s", pname, filesep, fname);
 281   // Check whether file exists.
 282   if (ret != -1) {
 283     struct stat statbuf;
 284     return os::stat(buffer, &statbuf) == 0;
 285   }
 286   return false;
 287 }
 288 
 289 // Frees all memory allocated on the heap for the
 290 // supplied array of arrays of chars (a), where n
 291 // is the number of elements in the array.
 292 static void free_array_of_char_arrays(char** a, size_t n) {
 293       while (n > 0) {
 294           n--;
 295           if (a[n] != nullptr) {
 296             FREE_C_HEAP_ARRAY(char, a[n]);
 297           }
 298       }
 299       FREE_C_HEAP_ARRAY(char*, a);
 300 }
 301 
 302 bool os::dll_locate_lib(char *buffer, size_t buflen,
 303                         const char* pname, const char* fname) {
 304   bool retval = false;
 305 
 306   size_t fullfnamelen = strlen(JNI_LIB_PREFIX) + strlen(fname) + strlen(JNI_LIB_SUFFIX);
 307   char* fullfname = NEW_C_HEAP_ARRAY(char, fullfnamelen + 1, mtInternal);
 308   if (dll_build_name(fullfname, fullfnamelen + 1, fname)) {
 309     const size_t pnamelen = pname ? strlen(pname) : 0;
 310 
 311     if (pnamelen == 0) {
 312       // If no path given, use current working directory.
 313       const char* p = get_current_directory(buffer, buflen);
 314       if (p != nullptr) {
 315         const size_t plen = strlen(buffer);
 316         const char lastchar = buffer[plen - 1];
 317         retval = conc_path_file_and_check(buffer, &buffer[plen], buflen - plen,
 318                                           "", lastchar, fullfname);
 319       }
 320     } else if (strchr(pname, *os::path_separator()) != nullptr) {
 321       // A list of paths. Search for the path that contains the library.
 322       size_t n;
 323       char** pelements = split_path(pname, &n, fullfnamelen);
 324       if (pelements != nullptr) {
 325         for (size_t i = 0; i < n; i++) {
 326           char* path = pelements[i];
 327           // Really shouldn't be null, but check can't hurt.
 328           size_t plen = (path == nullptr) ? 0 : strlen(path);
 329           if (plen == 0) {
 330             continue; // Skip the empty path values.
 331           }
 332           const char lastchar = path[plen - 1];
 333           retval = conc_path_file_and_check(buffer, buffer, buflen, path, lastchar, fullfname);
 334           if (retval) break;
 335         }
 336         // Release the storage allocated by split_path.
 337         free_array_of_char_arrays(pelements, n);
 338       }
 339     } else {
 340       // A definite path.
 341       const char lastchar = pname[pnamelen-1];
 342       retval = conc_path_file_and_check(buffer, buffer, buflen, pname, lastchar, fullfname);
 343     }
 344   }
 345 
 346   FREE_C_HEAP_ARRAY(char*, fullfname);
 347   return retval;
 348 }
 349 
 350 // --------------------- sun.misc.Signal (optional) ---------------------
 351 
 352 
 353 // SIGBREAK is sent by the keyboard to query the VM state
 354 #ifndef SIGBREAK
 355 #define SIGBREAK SIGQUIT
 356 #endif
 357 
 358 // sigexitnum_pd is a platform-specific special signal used for terminating the Signal thread.
 359 
 360 
 361 static void signal_thread_entry(JavaThread* thread, TRAPS) {
 362   os::set_priority(thread, NearMaxPriority);
 363   while (true) {
 364     int sig;
 365     {
 366       // FIXME : Currently we have not decided what should be the status
 367       //         for this java thread blocked here. Once we decide about
 368       //         that we should fix this.
 369       sig = os::signal_wait();
 370     }
 371     if (sig == os::sigexitnum_pd()) {
 372        // Terminate the signal thread
 373        return;
 374     }
 375 
 376     switch (sig) {
 377       case SIGBREAK: {
 378 #if INCLUDE_SERVICES
 379         // Check if the signal is a trigger to start the Attach Listener - in that
 380         // case don't print stack traces.
 381         if (!DisableAttachMechanism) {
 382           // Attempt to transit state to AL_INITIALIZING.
 383           AttachListenerState cur_state = AttachListener::transit_state(AL_INITIALIZING, AL_NOT_INITIALIZED);
 384           if (cur_state == AL_INITIALIZING) {
 385             // Attach Listener has been started to initialize. Ignore this signal.
 386             continue;
 387           } else if (cur_state == AL_NOT_INITIALIZED) {
 388             // Start to initialize.
 389             if (AttachListener::is_init_trigger()) {
 390               // Attach Listener has been initialized.
 391               // Accept subsequent request.
 392               continue;
 393             } else {
 394               // Attach Listener could not be started.
 395               // So we need to transit the state to AL_NOT_INITIALIZED.
 396               AttachListener::set_state(AL_NOT_INITIALIZED);
 397             }
 398           } else if (AttachListener::check_socket_file()) {
 399             // Attach Listener has been started, but unix domain socket file
 400             // does not exist. So restart Attach Listener.
 401             continue;
 402           }
 403         }
 404 #endif
 405         // Print stack traces
 406         // Any SIGBREAK operations added here should make sure to flush
 407         // the output stream (e.g. tty->flush()) after output.  See 4803766.
 408         // Each module also prints an extra carriage return after its output.
 409         VM_PrintThreads op(tty, PrintConcurrentLocks, false /* no extended info */, true /* print JNI handle info */);
 410         VMThread::execute(&op);
 411         VM_FindDeadlocks op1(tty);
 412         VMThread::execute(&op1);
 413         Universe::print_heap_at_SIGBREAK();
 414         if (PrintClassHistogram) {
 415           VM_GC_HeapInspection op1(tty, true /* force full GC before heap inspection */);
 416           VMThread::execute(&op1);
 417         }
 418         if (JvmtiExport::should_post_data_dump()) {
 419           JvmtiExport::post_data_dump();
 420         }
 421         break;
 422       }
 423       default: {
 424         // Dispatch the signal to java
 425         HandleMark hm(THREAD);
 426         Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_misc_Signal(), THREAD);
 427         if (klass != nullptr) {
 428           JavaValue result(T_VOID);
 429           JavaCallArguments args;
 430           args.push_int(sig);
 431           JavaCalls::call_static(
 432             &result,
 433             klass,
 434             vmSymbols::dispatch_name(),
 435             vmSymbols::int_void_signature(),
 436             &args,
 437             THREAD
 438           );
 439         }
 440         if (HAS_PENDING_EXCEPTION) {
 441           // tty is initialized early so we don't expect it to be null, but
 442           // if it is we can't risk doing an initialization that might
 443           // trigger additional out-of-memory conditions
 444           if (tty != nullptr) {
 445             char klass_name[256];
 446             char tmp_sig_name[16];
 447             const char* sig_name = "UNKNOWN";
 448             InstanceKlass::cast(PENDING_EXCEPTION->klass())->
 449               name()->as_klass_external_name(klass_name, 256);
 450             if (os::exception_name(sig, tmp_sig_name, 16) != nullptr)
 451               sig_name = tmp_sig_name;
 452             warning("Exception %s occurred dispatching signal %s to handler"
 453                     "- the VM may need to be forcibly terminated",
 454                     klass_name, sig_name );
 455           }
 456           CLEAR_PENDING_EXCEPTION;
 457         }
 458       }
 459     }
 460   }
 461 }
 462 
 463 void os::init_before_ergo() {
 464   initialize_initial_active_processor_count();
 465   // We need to initialize large page support here because ergonomics takes some
 466   // decisions depending on large page support and the calculated large page size.
 467   large_page_init();
 468 
 469   StackOverflow::initialize_stack_zone_sizes();
 470 
 471   // VM version initialization identifies some characteristics of the
 472   // platform that are used during ergonomic decisions.
 473   VM_Version::init_before_ergo();
 474 }
 475 
 476 void os::initialize_jdk_signal_support(TRAPS) {
 477   if (!ReduceSignalUsage) {
 478     // Setup JavaThread for processing signals
 479     const char* name = "Signal Dispatcher";
 480     Handle thread_oop = JavaThread::create_system_thread_object(name, true /* visible */, CHECK);
 481 
 482     JavaThread* thread = new JavaThread(&signal_thread_entry);
 483     JavaThread::vm_exit_on_osthread_failure(thread);
 484 
 485     JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NearMaxPriority);
 486   }
 487 }
 488 
 489 
 490 void os::terminate_signal_thread() {
 491   if (!ReduceSignalUsage)
 492     signal_notify(sigexitnum_pd());
 493 }
 494 
 495 
 496 // --------------------- loading libraries ---------------------
 497 
 498 typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
 499 extern struct JavaVM_ main_vm;
 500 
 501 static void* _native_java_library = nullptr;
 502 
 503 void* os::native_java_library() {
 504   if (_native_java_library == nullptr) {
 505     char buffer[JVM_MAXPATHLEN];
 506     char ebuf[1024];
 507 
 508     // Load java dll
 509     if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
 510                        "java")) {
 511       _native_java_library = dll_load(buffer, ebuf, sizeof(ebuf));
 512     }
 513     if (_native_java_library == nullptr) {
 514       vm_exit_during_initialization("Unable to load native library", ebuf);
 515     }
 516 
 517 #if defined(__OpenBSD__)
 518     // Work-around OpenBSD's lack of $ORIGIN support by pre-loading libnet.so
 519     // ignore errors
 520     if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
 521                        "net")) {
 522       dll_load(buffer, ebuf, sizeof(ebuf));
 523     }
 524 #endif
 525   }
 526   return _native_java_library;
 527 }
 528 
 529 /*
 530  * Support for finding Agent_On(Un)Load/Attach<_lib_name> if it exists.
 531  * If check_lib == true then we are looking for an
 532  * Agent_OnLoad_lib_name or Agent_OnAttach_lib_name function to determine if
 533  * this library is statically linked into the image.
 534  * If check_lib == false then we will look for the appropriate symbol in the
 535  * executable if agent_lib->is_static_lib() == true or in the shared library
 536  * referenced by 'handle'.
 537  */
 538 void* os::find_agent_function(AgentLibrary *agent_lib, bool check_lib,
 539                               const char *syms[], size_t syms_len) {
 540   assert(agent_lib != nullptr, "sanity check");
 541   const char *lib_name;
 542   void *handle = agent_lib->os_lib();
 543   void *entryName = nullptr;
 544   char *agent_function_name;
 545   size_t i;
 546 
 547   // If checking then use the agent name otherwise test is_static_lib() to
 548   // see how to process this lookup
 549   lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : nullptr);
 550   for (i = 0; i < syms_len; i++) {
 551     agent_function_name = build_agent_function_name(syms[i], lib_name, agent_lib->is_absolute_path());
 552     if (agent_function_name == nullptr) {
 553       break;
 554     }
 555     entryName = dll_lookup(handle, agent_function_name);
 556     FREE_C_HEAP_ARRAY(char, agent_function_name);
 557     if (entryName != nullptr) {
 558       break;
 559     }
 560   }
 561   return entryName;
 562 }
 563 
 564 // See if the passed in agent is statically linked into the VM image.
 565 bool os::find_builtin_agent(AgentLibrary *agent_lib, const char *syms[],
 566                             size_t syms_len) {
 567   void *ret;
 568   void *proc_handle;
 569   void *save_handle;
 570 
 571   assert(agent_lib != nullptr, "sanity check");
 572   if (agent_lib->name() == nullptr) {
 573     return false;
 574   }
 575   proc_handle = get_default_process_handle();
 576   // Check for Agent_OnLoad/Attach_lib_name function
 577   save_handle = agent_lib->os_lib();
 578   // We want to look in this process' symbol table.
 579   agent_lib->set_os_lib(proc_handle);
 580   ret = find_agent_function(agent_lib, true, syms, syms_len);
 581   if (ret != nullptr) {
 582     // Found an entry point like Agent_OnLoad_lib_name so we have a static agent
 583     agent_lib->set_valid();
 584     agent_lib->set_static_lib(true);
 585     return true;
 586   }
 587   agent_lib->set_os_lib(save_handle);
 588   return false;
 589 }
 590 
 591 // --------------------- heap allocation utilities ---------------------
 592 
 593 char *os::strdup(const char *str, MEMFLAGS flags) {
 594   size_t size = strlen(str);
 595   char *dup_str = (char *)malloc(size + 1, flags);
 596   if (dup_str == nullptr) return nullptr;
 597   strcpy(dup_str, str);
 598   return dup_str;
 599 }
 600 
 601 char* os::strdup_check_oom(const char* str, MEMFLAGS flags) {
 602   char* p = os::strdup(str, flags);
 603   if (p == nullptr) {
 604     vm_exit_out_of_memory(strlen(str) + 1, OOM_MALLOC_ERROR, "os::strdup_check_oom");
 605   }
 606   return p;
 607 }
 608 
 609 #ifdef ASSERT
 610 static void check_crash_protection() {
 611   assert(!ThreadCrashProtection::is_crash_protected(Thread::current_or_null()),
 612          "not allowed when crash protection is set");
 613 }
 614 static void break_if_ptr_caught(void* ptr) {
 615   if (p2i(ptr) == (intptr_t)MallocCatchPtr) {
 616     log_warning(malloc, free)("ptr caught: " PTR_FORMAT, p2i(ptr));
 617     breakpoint();
 618   }
 619 }
 620 #endif // ASSERT
 621 
 622 void* os::malloc(size_t size, MEMFLAGS flags) {
 623   return os::malloc(size, flags, CALLER_PC);
 624 }
 625 
 626 void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 627 
 628   // Special handling for NMT preinit phase before arguments are parsed
 629   void* rc = nullptr;
 630   if (NMTPreInit::handle_malloc(&rc, size)) {
 631     // No need to fill with 0 because DumpSharedSpaces doesn't use these
 632     // early allocations.
 633     return rc;
 634   }
 635 
 636   DEBUG_ONLY(check_crash_protection());
 637 
 638   // On malloc(0), implementations of malloc(3) have the choice to return either
 639   // null or a unique non-null pointer. To unify libc behavior across our platforms
 640   // we chose the latter.
 641   size = MAX2((size_t)1, size);
 642 
 643   // Observe MallocLimit
 644   if (MemTracker::check_exceeds_limit(size, memflags)) {
 645     return nullptr;
 646   }
 647 
 648   const size_t outer_size = size + MemTracker::overhead_per_malloc();
 649 
 650   // Check for overflow.
 651   if (outer_size < size) {
 652     return nullptr;
 653   }
 654 
 655   ALLOW_C_FUNCTION(::malloc, void* const outer_ptr = ::malloc(outer_size);)
 656   if (outer_ptr == nullptr) {
 657     return nullptr;
 658   }
 659 
 660   void* const inner_ptr = MemTracker::record_malloc((address)outer_ptr, size, memflags, stack);
 661 
 662   if (DumpSharedSpaces) {
 663     // Need to deterministically fill all the alignment gaps in C++ structures.
 664     ::memset(inner_ptr, 0, size);
 665   } else {
 666     DEBUG_ONLY(::memset(inner_ptr, uninitBlockPad, size);)
 667   }
 668   DEBUG_ONLY(break_if_ptr_caught(inner_ptr);)
 669   return inner_ptr;
 670 }
 671 
 672 void* os::realloc(void *memblock, size_t size, MEMFLAGS flags) {
 673   return os::realloc(memblock, size, flags, CALLER_PC);
 674 }
 675 
 676 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
 677 
 678   // Special handling for NMT preinit phase before arguments are parsed
 679   void* rc = nullptr;
 680   if (NMTPreInit::handle_realloc(&rc, memblock, size, memflags)) {
 681     return rc;
 682   }
 683 
 684   if (memblock == nullptr) {
 685     return os::malloc(size, memflags, stack);
 686   }
 687 
 688   DEBUG_ONLY(check_crash_protection());
 689 
 690   // On realloc(p, 0), implementers of realloc(3) have the choice to return either
 691   // null or a unique non-null pointer. To unify libc behavior across our platforms
 692   // we chose the latter.
 693   size = MAX2((size_t)1, size);
 694 
 695   if (MemTracker::enabled()) {
 696     // NMT realloc handling
 697 
 698     const size_t new_outer_size = size + MemTracker::overhead_per_malloc();
 699 
 700     // Handle size overflow.
 701     if (new_outer_size < size) {
 702       return nullptr;
 703     }
 704 
 705     const size_t old_size = MallocTracker::malloc_header(memblock)->size();
 706 
 707     // Observe MallocLimit
 708     if ((size > old_size) && MemTracker::check_exceeds_limit(size - old_size, memflags)) {
 709       return nullptr;
 710     }
 711 
 712     // Perform integrity checks on and mark the old block as dead *before* calling the real realloc(3) since it
 713     // may invalidate the old block, including its header.
 714     MallocHeader* header = MallocHeader::resolve_checked(memblock);
 715     assert(memflags == header->flags(), "weird NMT flags mismatch (new:\"%s\" != old:\"%s\")\n",
 716            NMTUtil::flag_to_name(memflags), NMTUtil::flag_to_name(header->flags()));
 717     const MallocHeader::FreeInfo free_info = header->free_info();
 718 
 719     header->mark_block_as_dead();
 720 
 721     // the real realloc
 722     ALLOW_C_FUNCTION(::realloc, void* const new_outer_ptr = ::realloc(header, new_outer_size);)
 723 
 724     if (new_outer_ptr == nullptr) {
 725       // realloc(3) failed and the block still exists.
 726       // We have however marked it as dead, revert this change.
 727       header->revive();
 728       return nullptr;
 729     }
 730     // realloc(3) succeeded, variable header now points to invalid memory and we need to deaccount the old block.
 731     MemTracker::deaccount(free_info);
 732 
 733     // After a successful realloc(3), we account the resized block with its new size
 734     // to NMT.
 735     void* const new_inner_ptr = MemTracker::record_malloc(new_outer_ptr, size, memflags, stack);
 736 
 737 #ifdef ASSERT
 738     assert(old_size == free_info.size, "Sanity");
 739     if (old_size < size) {
 740       // We also zap the newly extended region.
 741       ::memset((char*)new_inner_ptr + old_size, uninitBlockPad, size - old_size);
 742     }
 743 #endif
 744 
 745     rc = new_inner_ptr;
 746 
 747   } else {
 748 
 749     // NMT disabled.
 750     ALLOW_C_FUNCTION(::realloc, rc = ::realloc(memblock, size);)
 751     if (rc == nullptr) {
 752       return nullptr;
 753     }
 754 
 755   }
 756 
 757   DEBUG_ONLY(break_if_ptr_caught(rc);)
 758 
 759   return rc;
 760 }
 761 
 762 void  os::free(void *memblock) {
 763 
 764   // Special handling for NMT preinit phase before arguments are parsed
 765   if (NMTPreInit::handle_free(memblock)) {
 766     return;
 767   }
 768 
 769   if (memblock == nullptr) {
 770     return;
 771   }
 772 
 773   DEBUG_ONLY(break_if_ptr_caught(memblock);)
 774 
 775   // When NMT is enabled this checks for heap overwrites, then deaccounts the old block.
 776   void* const old_outer_ptr = MemTracker::record_free(memblock);
 777 
 778   ALLOW_C_FUNCTION(::free, ::free(old_outer_ptr);)
 779 }
 780 
 781 void os::init_random(unsigned int initval) {
 782   _rand_seed = initval;
 783 }
 784 
 785 
 786 int os::next_random(unsigned int rand_seed) {
 787   /* standard, well-known linear congruential random generator with
 788    * next_rand = (16807*seed) mod (2**31-1)
 789    * see
 790    * (1) "Random Number Generators: Good Ones Are Hard to Find",
 791    *      S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
 792    * (2) "Two Fast Implementations of the 'Minimal Standard' Random
 793    *     Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
 794   */
 795   const unsigned int a = 16807;
 796   const unsigned int m = 2147483647;
 797   const int q = m / a;        assert(q == 127773, "weird math");
 798   const int r = m % a;        assert(r == 2836, "weird math");
 799 
 800   // compute az=2^31p+q
 801   unsigned int lo = a * (rand_seed & 0xFFFF);
 802   unsigned int hi = a * (rand_seed >> 16);
 803   lo += (hi & 0x7FFF) << 16;
 804 
 805   // if q overflowed, ignore the overflow and increment q
 806   if (lo > m) {
 807     lo &= m;
 808     ++lo;
 809   }
 810   lo += hi >> 15;
 811 
 812   // if (p+q) overflowed, ignore the overflow and increment (p+q)
 813   if (lo > m) {
 814     lo &= m;
 815     ++lo;
 816   }
 817   return lo;
 818 }
 819 
 820 int os::random() {
 821   // Make updating the random seed thread safe.
 822   while (true) {
 823     unsigned int seed = _rand_seed;
 824     unsigned int rand = next_random(seed);
 825     if (Atomic::cmpxchg(&_rand_seed, seed, rand, memory_order_relaxed) == seed) {
 826       return static_cast<int>(rand);
 827     }
 828   }
 829 }
 830 
 831 // The INITIALIZED state is distinguished from the SUSPENDED state because the
 832 // conditions in which a thread is first started are different from those in which
 833 // a suspension is resumed.  These differences make it hard for us to apply the
 834 // tougher checks when starting threads that we want to do when resuming them.
 835 // However, when start_thread is called as a result of Thread.start, on a Java
 836 // thread, the operation is synchronized on the Java Thread object.  So there
 837 // cannot be a race to start the thread and hence for the thread to exit while
 838 // we are working on it.  Non-Java threads that start Java threads either have
 839 // to do so in a context in which races are impossible, or should do appropriate
 840 // locking.
 841 
 842 void os::start_thread(Thread* thread) {
 843   OSThread* osthread = thread->osthread();
 844   osthread->set_state(RUNNABLE);
 845   pd_start_thread(thread);
 846 }
 847 
 848 void os::abort(bool dump_core) {
 849   abort(dump_core && CreateCoredumpOnCrash, nullptr, nullptr);
 850 }
 851 
 852 //---------------------------------------------------------------------------
 853 // Helper functions for fatal error handler
 854 
 855 bool os::print_function_and_library_name(outputStream* st,
 856                                          address addr,
 857                                          char* buf, int buflen,
 858                                          bool shorten_paths,
 859                                          bool demangle,
 860                                          bool strip_arguments) {
 861   // If no scratch buffer given, allocate one here on stack.
 862   // (used during error handling; its a coin toss, really, if on-stack allocation
 863   //  is worse than (raw) C-heap allocation in that case).
 864   char* p = buf;
 865   if (p == nullptr) {
 866     p = (char*)::alloca(O_BUFLEN);
 867     buflen = O_BUFLEN;
 868   }
 869   int offset = 0;
 870   bool have_function_name = dll_address_to_function_name(addr, p, buflen,
 871                                                          &offset, demangle);
 872   bool is_function_descriptor = false;
 873 #ifdef HAVE_FUNCTION_DESCRIPTORS
 874   // When we deal with a function descriptor instead of a real code pointer, try to
 875   // resolve it. There is a small chance that a random pointer given to this function
 876   // may just happen to look like a valid descriptor, but this is rare and worth the
 877   // risk to see resolved function names. But we will print a little suffix to mark
 878   // this as a function descriptor for the reader (see below).
 879   if (!have_function_name && os::is_readable_pointer(addr)) {
 880     address addr2 = (address)os::resolve_function_descriptor(addr);
 881     if (have_function_name = is_function_descriptor =
 882         dll_address_to_function_name(addr2, p, buflen, &offset, demangle)) {
 883       addr = addr2;
 884     }
 885   }
 886 #endif // HAVE_FUNCTION_DESCRIPTORS
 887 
 888   if (have_function_name) {
 889     // Print function name, optionally demangled
 890     if (demangle && strip_arguments) {
 891       char* args_start = strchr(p, '(');
 892       if (args_start != nullptr) {
 893         *args_start = '\0';
 894       }
 895     }
 896     // Print offset. Omit printing if offset is zero, which makes the output
 897     // more readable if we print function pointers.
 898     if (offset == 0) {
 899       st->print("%s", p);
 900     } else {
 901       st->print("%s+%d", p, offset);
 902     }
 903   } else {
 904     st->print(PTR_FORMAT, p2i(addr));
 905   }
 906   offset = 0;
 907 
 908   const bool have_library_name = dll_address_to_library_name(addr, p, buflen, &offset);
 909   if (have_library_name) {
 910     // Cut path parts
 911     if (shorten_paths) {
 912       char* p2 = strrchr(p, os::file_separator()[0]);
 913       if (p2 != nullptr) {
 914         p = p2 + 1;
 915       }
 916     }
 917     st->print(" in %s", p);
 918     if (!have_function_name) { // Omit offset if we already printed the function offset
 919       st->print("+%d", offset);
 920     }
 921   }
 922 
 923   // Write a trailing marker if this was a function descriptor
 924   if (have_function_name && is_function_descriptor) {
 925     st->print_raw(" (FD)");
 926   }
 927 
 928   return have_function_name || have_library_name;
 929 }
 930 
 931 ATTRIBUTE_NO_ASAN static void print_hex_readable_pointer(outputStream* st, address p,
 932                                                          int unitsize) {
 933   switch (unitsize) {
 934     case 1: st->print("%02x", *(u1*)p); break;
 935     case 2: st->print("%04x", *(u2*)p); break;
 936     case 4: st->print("%08x", *(u4*)p); break;
 937     case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break;
 938   }
 939 }
 940 
 941 void os::print_hex_dump(outputStream* st, address start, address end, int unitsize,
 942                         int bytes_per_line, address logical_start) {
 943   assert(unitsize == 1 || unitsize == 2 || unitsize == 4 || unitsize == 8, "just checking");
 944 
 945   start = align_down(start, unitsize);
 946   logical_start = align_down(logical_start, unitsize);
 947   bytes_per_line = align_up(bytes_per_line, 8);
 948 
 949   int cols = 0;
 950   int cols_per_line = bytes_per_line / unitsize;
 951 
 952   address p = start;
 953   address logical_p = logical_start;
 954 
 955   // Print out the addresses as if we were starting from logical_start.
 956   st->print(PTR_FORMAT ":   ", p2i(logical_p));
 957   while (p < end) {
 958     if (is_readable_pointer(p)) {
 959       print_hex_readable_pointer(st, p, unitsize);
 960     } else {
 961       st->print("%*.*s", 2*unitsize, 2*unitsize, "????????????????");
 962     }
 963     p += unitsize;
 964     logical_p += unitsize;
 965     cols++;
 966     if (cols >= cols_per_line && p < end) {
 967        cols = 0;
 968        st->cr();
 969        st->print(PTR_FORMAT ":   ", p2i(logical_p));
 970     } else {
 971        st->print(" ");
 972     }
 973   }
 974   st->cr();
 975 }
 976 
 977 void os::print_dhm(outputStream* st, const char* startStr, long sec) {
 978   long days    = sec/86400;
 979   long hours   = (sec/3600) - (days * 24);
 980   long minutes = (sec/60) - (days * 1440) - (hours * 60);
 981   if (startStr == nullptr) startStr = "";
 982   st->print_cr("%s %ld days %ld:%02ld hours", startStr, days, hours, minutes);
 983 }
 984 
 985 void os::print_tos(outputStream* st, address sp) {
 986   st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
 987   print_hex_dump(st, sp, sp + 512, sizeof(intptr_t));
 988 }
 989 
 990 void os::print_instructions(outputStream* st, address pc, int unitsize) {
 991   st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
 992   print_hex_dump(st, pc - 256, pc + 256, unitsize);
 993 }
 994 
 995 void os::print_environment_variables(outputStream* st, const char** env_list) {
 996   if (env_list) {
 997     st->print_cr("Environment Variables:");
 998 
 999     for (int i = 0; env_list[i] != nullptr; i++) {
1000       char *envvar = ::getenv(env_list[i]);
1001       if (envvar != nullptr) {
1002         st->print("%s", env_list[i]);
1003         st->print("=");
1004         st->print("%s", envvar);
1005         // Use separate cr() printing to avoid unnecessary buffer operations that might cause truncation.
1006         st->cr();
1007       }
1008     }
1009   }
1010 }
1011 
1012 void os::print_cpu_info(outputStream* st, char* buf, size_t buflen) {
1013   // cpu
1014   st->print("CPU:");
1015 #if defined(__APPLE__) && !defined(ZERO)
1016    if (VM_Version::is_cpu_emulated()) {
1017      st->print(" (EMULATED)");
1018    }
1019 #endif
1020   st->print(" total %d", os::processor_count());
1021   // It's not safe to query number of active processors after crash
1022   // st->print("(active %d)", os::active_processor_count()); but we can
1023   // print the initial number of active processors.
1024   // We access the raw value here because the assert in the accessor will
1025   // fail if the crash occurs before initialization of this value.
1026   st->print(" (initial active %d)", _initial_active_processor_count);
1027   st->print(" %s", VM_Version::features_string());
1028   st->cr();
1029   pd_print_cpu_info(st, buf, buflen);
1030 }
1031 
1032 // Print a one line string summarizing the cpu, number of cores, memory, and operating system version
1033 void os::print_summary_info(outputStream* st, char* buf, size_t buflen) {
1034   st->print("Host: ");
1035 #ifndef PRODUCT
1036   if (get_host_name(buf, buflen)) {
1037     st->print("%s, ", buf);
1038   }
1039 #endif // PRODUCT
1040   get_summary_cpu_info(buf, buflen);
1041   st->print("%s, ", buf);
1042   size_t mem = physical_memory()/G;
1043   if (mem == 0) {  // for low memory systems
1044     mem = physical_memory()/M;
1045     st->print("%d cores, " SIZE_FORMAT "M, ", processor_count(), mem);
1046   } else {
1047     st->print("%d cores, " SIZE_FORMAT "G, ", processor_count(), mem);
1048   }
1049   get_summary_os_info(buf, buflen);
1050   st->print_raw(buf);
1051   st->cr();
1052 }
1053 
1054 void os::print_date_and_time(outputStream *st, char* buf, size_t buflen) {
1055   const int secs_per_day  = 86400;
1056   const int secs_per_hour = 3600;
1057   const int secs_per_min  = 60;
1058 
1059   time_t tloc;
1060   (void)time(&tloc);
1061   char* timestring = ctime(&tloc);  // ctime adds newline.
1062   // edit out the newline
1063   char* nl = strchr(timestring, '\n');
1064   if (nl != nullptr) {
1065     *nl = '\0';
1066   }
1067 
1068   struct tm tz;
1069   if (localtime_pd(&tloc, &tz) != nullptr) {
1070     wchar_t w_buf[80];
1071     size_t n = ::wcsftime(w_buf, 80, L"%Z", &tz);
1072     if (n > 0) {
1073       ::wcstombs(buf, w_buf, buflen);
1074       st->print("Time: %s %s", timestring, buf);
1075     } else {
1076       st->print("Time: %s", timestring);
1077     }
1078   } else {
1079     st->print("Time: %s", timestring);
1080   }
1081 
1082   double t = os::elapsedTime();
1083   // NOTE: a crash using printf("%f",...) on Linux was historically noted here.
1084   int eltime = (int)t;  // elapsed time in seconds
1085   int eltimeFraction = (int) ((t - eltime) * 1000000);
1086 
1087   // print elapsed time in a human-readable format:
1088   int eldays = eltime / secs_per_day;
1089   int day_secs = eldays * secs_per_day;
1090   int elhours = (eltime - day_secs) / secs_per_hour;
1091   int hour_secs = elhours * secs_per_hour;
1092   int elmins = (eltime - day_secs - hour_secs) / secs_per_min;
1093   int minute_secs = elmins * secs_per_min;
1094   int elsecs = (eltime - day_secs - hour_secs - minute_secs);
1095   st->print_cr(" elapsed time: %d.%06d seconds (%dd %dh %dm %ds)", eltime, eltimeFraction, eldays, elhours, elmins, elsecs);
1096 }
1097 
1098 
1099 // Check if pointer can be read from (4-byte read access).
1100 // Helps to prove validity of a non-null pointer.
1101 // Returns true in very early stages of VM life when stub is not yet generated.
1102 bool os::is_readable_pointer(const void* p) {
1103   int* const aligned = (int*) align_down((intptr_t)p, 4);
1104   int cafebabe = 0xcafebabe;  // tester value 1
1105   int deadbeef = 0xdeadbeef;  // tester value 2
1106   return (SafeFetch32(aligned, cafebabe) != cafebabe) || (SafeFetch32(aligned, deadbeef) != deadbeef);
1107 }
1108 
1109 bool os::is_readable_range(const void* from, const void* to) {
1110   if ((uintptr_t)from >= (uintptr_t)to) return false;
1111   for (uintptr_t p = align_down((uintptr_t)from, min_page_size()); p < (uintptr_t)to; p += min_page_size()) {
1112     if (!is_readable_pointer((const void*)p)) {
1113       return false;
1114     }
1115   }
1116   return true;
1117 }
1118 
1119 
1120 // moved from debug.cpp (used to be find()) but still called from there
1121 // The verbose parameter is only set by the debug code in one case
1122 void os::print_location(outputStream* st, intptr_t x, bool verbose) {
1123   address addr = (address)x;
1124   // Handle null first, so later checks don't need to protect against it.
1125   if (addr == nullptr) {
1126     st->print_cr("0x0 is nullptr");
1127     return;
1128   }
1129 
1130   // Check if addr points into a code blob.
1131   CodeBlob* b = CodeCache::find_blob(addr);
1132   if (b != nullptr) {
1133     b->dump_for_addr(addr, st, verbose);
1134     return;
1135   }
1136 
1137   // Check if addr points into Java heap.
1138   if (Universe::heap()->print_location(st, addr)) {
1139     return;
1140   }
1141 
1142   bool accessible = is_readable_pointer(addr);
1143 
1144   // Check if addr is a JNI handle.
1145   if (align_down((intptr_t)addr, sizeof(intptr_t)) != 0 && accessible) {
1146     if (JNIHandles::is_global_handle((jobject) addr)) {
1147       st->print_cr(INTPTR_FORMAT " is a global jni handle", p2i(addr));
1148       return;
1149     }
1150     if (JNIHandles::is_weak_global_handle((jobject) addr)) {
1151       st->print_cr(INTPTR_FORMAT " is a weak global jni handle", p2i(addr));
1152       return;
1153     }
1154   }
1155 
1156   // Check if addr belongs to a Java thread.
1157   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
1158     // If the addr is a java thread print information about that.
1159     if (addr == (address)thread) {
1160       if (verbose) {
1161         thread->print_on(st);
1162       } else {
1163         st->print_cr(INTPTR_FORMAT " is a thread", p2i(addr));
1164       }
1165       return;
1166     }
1167     // If the addr is in the stack region for this thread then report that
1168     // and print thread info
1169     if (thread->is_in_full_stack(addr)) {
1170       st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: "
1171                    INTPTR_FORMAT, p2i(addr), p2i(thread));
1172       if (verbose) thread->print_on(st);
1173       return;
1174     }
1175   }
1176 
1177   // Check if in metaspace and print types that have vptrs
1178   if (Metaspace::contains(addr)) {
1179     if (Klass::is_valid((Klass*)addr)) {
1180       st->print_cr(INTPTR_FORMAT " is a pointer to class: ", p2i(addr));
1181       ((Klass*)addr)->print_on(st);
1182     } else if (Method::is_valid_method((const Method*)addr)) {
1183       ((Method*)addr)->print_value_on(st);
1184       st->cr();
1185     } else {
1186       // Use addr->print() from the debugger instead (not here)
1187       st->print_cr(INTPTR_FORMAT " is pointing into metadata", p2i(addr));
1188     }
1189     return;
1190   }
1191 
1192   // Compressed klass needs to be decoded first.
1193 #ifdef _LP64
1194   if (UseCompressedClassPointers && ((uintptr_t)addr &~ (uintptr_t)max_juint) == 0) {
1195     narrowKlass narrow_klass = (narrowKlass)(uintptr_t)addr;
1196     Klass* k = CompressedKlassPointers::decode_raw(narrow_klass);
1197 
1198     if (Klass::is_valid(k)) {
1199       st->print_cr(UINT32_FORMAT " is a compressed pointer to class: " INTPTR_FORMAT, narrow_klass, p2i((HeapWord*)k));
1200       k->print_on(st);
1201       return;
1202     }
1203   }
1204 #endif
1205 
1206   // Try an OS specific find
1207   if (os::find(addr, st)) {
1208     return;
1209   }
1210 
1211   if (accessible) {
1212     st->print(INTPTR_FORMAT " points into unknown readable memory:", p2i(addr));
1213     if (is_aligned(addr, sizeof(intptr_t))) {
1214       st->print(" " PTR_FORMAT " |", *(intptr_t*)addr);
1215     }
1216     for (address p = addr; p < align_up(addr + 1, sizeof(intptr_t)); ++p) {
1217       st->print(" %02x", *(u1*)p);
1218     }
1219     st->cr();
1220     return;
1221   }
1222 
1223   st->print_cr(INTPTR_FORMAT " is an unknown value", p2i(addr));
1224 }
1225 
1226 bool is_pointer_bad(intptr_t* ptr) {
1227   return !is_aligned(ptr, sizeof(uintptr_t)) || !os::is_readable_pointer(ptr);
1228 }
1229 
1230 // Looks like all platforms can use the same function to check if C
1231 // stack is walkable beyond current frame.
1232 // Returns true if this is not the case, i.e. the frame is possibly
1233 // the first C frame on the stack.
1234 bool os::is_first_C_frame(frame* fr) {
1235 
1236 #ifdef _WINDOWS
1237   return true; // native stack isn't walkable on windows this way.
1238 #endif
1239   // Load up sp, fp, sender sp and sender fp, check for reasonable values.
1240   // Check usp first, because if that's bad the other accessors may fault
1241   // on some architectures.  Ditto ufp second, etc.
1242 
1243   if (is_pointer_bad(fr->sp())) return true;
1244 
1245   uintptr_t ufp    = (uintptr_t)fr->fp();
1246   if (is_pointer_bad(fr->fp())) return true;
1247 
1248   uintptr_t old_sp = (uintptr_t)fr->sender_sp();
1249   if ((uintptr_t)fr->sender_sp() == (uintptr_t)-1 || is_pointer_bad(fr->sender_sp())) return true;
1250 
1251   uintptr_t old_fp = (uintptr_t)fr->link_or_null();
1252   if (old_fp == 0 || old_fp == (uintptr_t)-1 || old_fp == ufp ||
1253     is_pointer_bad(fr->link_or_null())) return true;
1254 
1255   // stack grows downwards; if old_fp is below current fp or if the stack
1256   // frame is too large, either the stack is corrupted or fp is not saved
1257   // on stack (i.e. on x86, ebp may be used as general register). The stack
1258   // is not walkable beyond current frame.
1259   if (old_fp < ufp) return true;
1260   if (old_fp - ufp > 64 * K) return true;
1261 
1262   return false;
1263 }
1264 
1265 // Set up the boot classpath.
1266 
1267 char* os::format_boot_path(const char* format_string,
1268                            const char* home,
1269                            int home_len,
1270                            char fileSep,
1271                            char pathSep) {
1272     assert((fileSep == '/' && pathSep == ':') ||
1273            (fileSep == '\\' && pathSep == ';'), "unexpected separator chars");
1274 
1275     // Scan the format string to determine the length of the actual
1276     // boot classpath, and handle platform dependencies as well.
1277     int formatted_path_len = 0;
1278     const char* p;
1279     for (p = format_string; *p != 0; ++p) {
1280         if (*p == '%') formatted_path_len += home_len - 1;
1281         ++formatted_path_len;
1282     }
1283 
1284     char* formatted_path = NEW_C_HEAP_ARRAY(char, formatted_path_len + 1, mtInternal);
1285 
1286     // Create boot classpath from format, substituting separator chars and
1287     // java home directory.
1288     char* q = formatted_path;
1289     for (p = format_string; *p != 0; ++p) {
1290         switch (*p) {
1291         case '%':
1292             strcpy(q, home);
1293             q += home_len;
1294             break;
1295         case '/':
1296             *q++ = fileSep;
1297             break;
1298         case ':':
1299             *q++ = pathSep;
1300             break;
1301         default:
1302             *q++ = *p;
1303         }
1304     }
1305     *q = '\0';
1306 
1307     assert((q - formatted_path) == formatted_path_len, "formatted_path size botched");
1308     return formatted_path;
1309 }
1310 
1311 // This function is a proxy to fopen, it tries to add a non standard flag ('e' or 'N')
1312 // that ensures automatic closing of the file on exec. If it can not find support in
1313 // the underlying c library, it will make an extra system call (fcntl) to ensure automatic
1314 // closing of the file on exec.
1315 FILE* os::fopen(const char* path, const char* mode) {
1316   char modified_mode[20];
1317   assert(strlen(mode) + 1 < sizeof(modified_mode), "mode chars plus one extra must fit in buffer");
1318   os::snprintf_checked(modified_mode, sizeof(modified_mode), "%s" LINUX_ONLY("e") BSD_ONLY("e") WINDOWS_ONLY("N"), mode);
1319   FILE* file = ::fopen(path, modified_mode);
1320 
1321 #if !(defined LINUX || defined BSD || defined _WINDOWS)
1322   // assume fcntl FD_CLOEXEC support as a backup solution when 'e' or 'N'
1323   // is not supported as mode in fopen
1324   if (file != nullptr) {
1325     int fd = fileno(file);
1326     if (fd != -1) {
1327       int fd_flags = fcntl(fd, F_GETFD);
1328       if (fd_flags != -1) {
1329         fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC);
1330       }
1331     }
1332   }
1333 #endif
1334 
1335   return file;
1336 }
1337 
1338 bool os::set_boot_path(char fileSep, char pathSep) {
1339   const char* home = Arguments::get_java_home();
1340   int home_len = (int)strlen(home);
1341 
1342   struct stat st;
1343 
1344   // modular image if "modules" jimage exists
1345   char* jimage = format_boot_path("%/lib/" MODULES_IMAGE_NAME, home, home_len, fileSep, pathSep);
1346   if (jimage == nullptr) return false;
1347   bool has_jimage = (os::stat(jimage, &st) == 0);
1348   if (has_jimage) {
1349     Arguments::set_boot_class_path(jimage, true);
1350     FREE_C_HEAP_ARRAY(char, jimage);
1351     return true;
1352   }
1353   FREE_C_HEAP_ARRAY(char, jimage);
1354 
1355   // check if developer build with exploded modules
1356   char* base_classes = format_boot_path("%/modules/" JAVA_BASE_NAME, home, home_len, fileSep, pathSep);
1357   if (base_classes == nullptr) return false;
1358   if (os::stat(base_classes, &st) == 0) {
1359     Arguments::set_boot_class_path(base_classes, false);
1360     FREE_C_HEAP_ARRAY(char, base_classes);
1361     return true;
1362   }
1363   FREE_C_HEAP_ARRAY(char, base_classes);
1364 
1365   return false;
1366 }
1367 
1368 bool os::file_exists(const char* filename) {
1369   struct stat statbuf;
1370   if (filename == nullptr || strlen(filename) == 0) {
1371     return false;
1372   }
1373   return os::stat(filename, &statbuf) == 0;
1374 }
1375 
1376 // Splits a path, based on its separator, the number of
1377 // elements is returned back in "elements".
1378 // file_name_length is used as a modifier for each path's
1379 // length when compared to JVM_MAXPATHLEN. So if you know
1380 // each returned path will have something appended when
1381 // in use, you can pass the length of that in
1382 // file_name_length, to ensure we detect if any path
1383 // exceeds the maximum path length once prepended onto
1384 // the sub-path/file name.
1385 // It is the callers responsibility to:
1386 //   a> check the value of "elements", which may be 0.
1387 //   b> ignore any empty path elements
1388 //   c> free up the data.
1389 char** os::split_path(const char* path, size_t* elements, size_t file_name_length) {
1390   *elements = (size_t)0;
1391   if (path == nullptr || strlen(path) == 0 || file_name_length == (size_t)nullptr) {
1392     return nullptr;
1393   }
1394   const char psepchar = *os::path_separator();
1395   char* inpath = NEW_C_HEAP_ARRAY(char, strlen(path) + 1, mtInternal);
1396   strcpy(inpath, path);
1397   size_t count = 1;
1398   char* p = strchr(inpath, psepchar);
1399   // Get a count of elements to allocate memory
1400   while (p != nullptr) {
1401     count++;
1402     p++;
1403     p = strchr(p, psepchar);
1404   }
1405 
1406   char** opath = NEW_C_HEAP_ARRAY(char*, count, mtInternal);
1407 
1408   // do the actual splitting
1409   p = inpath;
1410   for (size_t i = 0 ; i < count ; i++) {
1411     size_t len = strcspn(p, os::path_separator());
1412     if (len + file_name_length > JVM_MAXPATHLEN) {
1413       // release allocated storage before exiting the vm
1414       free_array_of_char_arrays(opath, i++);
1415       vm_exit_during_initialization("The VM tried to use a path that exceeds the maximum path length for "
1416                                     "this system. Review path-containing parameters and properties, such as "
1417                                     "sun.boot.library.path, to identify potential sources for this path.");
1418     }
1419     // allocate the string and add terminator storage
1420     char* s = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
1421     strncpy(s, p, len);
1422     s[len] = '\0';
1423     opath[i] = s;
1424     p += len + 1;
1425   }
1426   FREE_C_HEAP_ARRAY(char, inpath);
1427   *elements = count;
1428   return opath;
1429 }
1430 
1431 // Returns true if the current stack pointer is above the stack shadow
1432 // pages, false otherwise.
1433 bool os::stack_shadow_pages_available(Thread *thread, const methodHandle& method, address sp) {
1434   if (!thread->is_Java_thread()) return false;
1435   // Check if we have StackShadowPages above the guard zone. This parameter
1436   // is dependent on the depth of the maximum VM call stack possible from
1437   // the handler for stack overflow.  'instanceof' in the stack overflow
1438   // handler or a println uses at least 8k stack of VM and native code
1439   // respectively.
1440   const int framesize_in_bytes =
1441     Interpreter::size_top_interpreter_activation(method()) * wordSize;
1442 
1443   address limit = JavaThread::cast(thread)->stack_overflow_state()->shadow_zone_safe_limit();
1444   return sp > (limit + framesize_in_bytes);
1445 }
1446 
1447 size_t os::page_size_for_region(size_t region_size, size_t min_pages, bool must_be_aligned) {
1448   assert(min_pages > 0, "sanity");
1449   if (UseLargePages) {
1450     const size_t max_page_size = region_size / min_pages;
1451 
1452     for (size_t page_size = page_sizes().largest(); page_size != 0;
1453          page_size = page_sizes().next_smaller(page_size)) {
1454       if (page_size <= max_page_size) {
1455         if (!must_be_aligned || is_aligned(region_size, page_size)) {
1456           return page_size;
1457         }
1458       }
1459     }
1460   }
1461 
1462   return vm_page_size();
1463 }
1464 
1465 size_t os::page_size_for_region_aligned(size_t region_size, size_t min_pages) {
1466   return page_size_for_region(region_size, min_pages, true);
1467 }
1468 
1469 size_t os::page_size_for_region_unaligned(size_t region_size, size_t min_pages) {
1470   return page_size_for_region(region_size, min_pages, false);
1471 }
1472 
1473 #ifndef MAX_PATH
1474 #define MAX_PATH    (2 * K)
1475 #endif
1476 
1477 void os::pause() {
1478   char filename[MAX_PATH];
1479   if (PauseAtStartupFile && PauseAtStartupFile[0]) {
1480     jio_snprintf(filename, MAX_PATH, "%s", PauseAtStartupFile);
1481   } else {
1482     jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
1483   }
1484 
1485   int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1486   if (fd != -1) {
1487     struct stat buf;
1488     ::close(fd);
1489     while (::stat(filename, &buf) == 0) {
1490 #if defined(_WINDOWS)
1491       Sleep(100);
1492 #else
1493       (void)::poll(nullptr, 0, 100);
1494 #endif
1495     }
1496   } else {
1497     jio_fprintf(stderr,
1498                 "Could not open pause file '%s', continuing immediately.\n", filename);
1499   }
1500 }
1501 
1502 static const char* errno_to_string (int e, bool short_text) {
1503   #define ALL_SHARED_ENUMS(X) \
1504     X(E2BIG, "Argument list too long") \
1505     X(EACCES, "Permission denied") \
1506     X(EADDRINUSE, "Address in use") \
1507     X(EADDRNOTAVAIL, "Address not available") \
1508     X(EAFNOSUPPORT, "Address family not supported") \
1509     X(EAGAIN, "Resource unavailable, try again") \
1510     X(EALREADY, "Connection already in progress") \
1511     X(EBADF, "Bad file descriptor") \
1512     X(EBADMSG, "Bad message") \
1513     X(EBUSY, "Device or resource busy") \
1514     X(ECANCELED, "Operation canceled") \
1515     X(ECHILD, "No child processes") \
1516     X(ECONNABORTED, "Connection aborted") \
1517     X(ECONNREFUSED, "Connection refused") \
1518     X(ECONNRESET, "Connection reset") \
1519     X(EDEADLK, "Resource deadlock would occur") \
1520     X(EDESTADDRREQ, "Destination address required") \
1521     X(EDOM, "Mathematics argument out of domain of function") \
1522     X(EEXIST, "File exists") \
1523     X(EFAULT, "Bad address") \
1524     X(EFBIG, "File too large") \
1525     X(EHOSTUNREACH, "Host is unreachable") \
1526     X(EIDRM, "Identifier removed") \
1527     X(EILSEQ, "Illegal byte sequence") \
1528     X(EINPROGRESS, "Operation in progress") \
1529     X(EINTR, "Interrupted function") \
1530     X(EINVAL, "Invalid argument") \
1531     X(EIO, "I/O error") \
1532     X(EISCONN, "Socket is connected") \
1533     X(EISDIR, "Is a directory") \
1534     X(ELOOP, "Too many levels of symbolic links") \
1535     X(EMFILE, "Too many open files") \
1536     X(EMLINK, "Too many links") \
1537     X(EMSGSIZE, "Message too large") \
1538     X(ENAMETOOLONG, "Filename too long") \
1539     X(ENETDOWN, "Network is down") \
1540     X(ENETRESET, "Connection aborted by network") \
1541     X(ENETUNREACH, "Network unreachable") \
1542     X(ENFILE, "Too many files open in system") \
1543     X(ENOBUFS, "No buffer space available") \
1544     X(ENODATA, "No message is available on the STREAM head read queue") \
1545     X(ENODEV, "No such device") \
1546     X(ENOENT, "No such file or directory") \
1547     X(ENOEXEC, "Executable file format error") \
1548     X(ENOLCK, "No locks available") \
1549     X(ENOLINK, "Reserved") \
1550     X(ENOMEM, "Not enough space") \
1551     X(ENOMSG, "No message of the desired type") \
1552     X(ENOPROTOOPT, "Protocol not available") \
1553     X(ENOSPC, "No space left on device") \
1554     X(ENOSR, "No STREAM resources") \
1555     X(ENOSTR, "Not a STREAM") \
1556     X(ENOSYS, "Function not supported") \
1557     X(ENOTCONN, "The socket is not connected") \
1558     X(ENOTDIR, "Not a directory") \
1559     X(ENOTEMPTY, "Directory not empty") \
1560     X(ENOTSOCK, "Not a socket") \
1561     X(ENOTSUP, "Not supported") \
1562     X(ENOTTY, "Inappropriate I/O control operation") \
1563     X(ENXIO, "No such device or address") \
1564     X(EOPNOTSUPP, "Operation not supported on socket") \
1565     X(EOVERFLOW, "Value too large to be stored in data type") \
1566     X(EPERM, "Operation not permitted") \
1567     X(EPIPE, "Broken pipe") \
1568     X(EPROTO, "Protocol error") \
1569     X(EPROTONOSUPPORT, "Protocol not supported") \
1570     X(EPROTOTYPE, "Protocol wrong type for socket") \
1571     X(ERANGE, "Result too large") \
1572     X(EROFS, "Read-only file system") \
1573     X(ESPIPE, "Invalid seek") \
1574     X(ESRCH, "No such process") \
1575     X(ETIME, "Stream ioctl() timeout") \
1576     X(ETIMEDOUT, "Connection timed out") \
1577     X(ETXTBSY, "Text file busy") \
1578     X(EWOULDBLOCK, "Operation would block") \
1579     X(EXDEV, "Cross-device link")
1580 
1581   #define DEFINE_ENTRY(e, text) { e, #e, text },
1582 
1583   static const struct {
1584     int v;
1585     const char* short_text;
1586     const char* long_text;
1587   } table [] = {
1588 
1589     ALL_SHARED_ENUMS(DEFINE_ENTRY)
1590 
1591     // The following enums are not defined on all platforms.
1592     #ifdef ESTALE
1593     DEFINE_ENTRY(ESTALE, "Reserved")
1594     #endif
1595     #ifdef EDQUOT
1596     DEFINE_ENTRY(EDQUOT, "Reserved")
1597     #endif
1598     #ifdef EMULTIHOP
1599     DEFINE_ENTRY(EMULTIHOP, "Reserved")
1600     #endif
1601 
1602     // End marker.
1603     { -1, "Unknown errno", "Unknown error" }
1604 
1605   };
1606 
1607   #undef DEFINE_ENTRY
1608   #undef ALL_FLAGS
1609 
1610   int i = 0;
1611   while (table[i].v != -1 && table[i].v != e) {
1612     i ++;
1613   }
1614 
1615   return short_text ? table[i].short_text : table[i].long_text;
1616 
1617 }
1618 
1619 const char* os::strerror(int e) {
1620   return errno_to_string(e, false);
1621 }
1622 
1623 const char* os::errno_name(int e) {
1624   return errno_to_string(e, true);
1625 }
1626 
1627 #define trace_page_size_params(size) byte_size_in_exact_unit(size), exact_unit_for_byte_size(size)
1628 
1629 void os::trace_page_sizes(const char* str,
1630                           const size_t region_min_size,
1631                           const size_t region_max_size,
1632                           const size_t page_size,
1633                           const char* base,
1634                           const size_t size) {
1635 
1636   log_info(pagesize)("%s: "
1637                      " min=" SIZE_FORMAT "%s"
1638                      " max=" SIZE_FORMAT "%s"
1639                      " base=" PTR_FORMAT
1640                      " page_size=" SIZE_FORMAT "%s"
1641                      " size=" SIZE_FORMAT "%s",
1642                      str,
1643                      trace_page_size_params(region_min_size),
1644                      trace_page_size_params(region_max_size),
1645                      p2i(base),
1646                      trace_page_size_params(page_size),
1647                      trace_page_size_params(size));
1648 }
1649 
1650 void os::trace_page_sizes_for_requested_size(const char* str,
1651                                              const size_t requested_size,
1652                                              const size_t page_size,
1653                                              const size_t alignment,
1654                                              const char* base,
1655                                              const size_t size) {
1656 
1657   log_info(pagesize)("%s:"
1658                      " req_size=" SIZE_FORMAT "%s"
1659                      " base=" PTR_FORMAT
1660                      " page_size=" SIZE_FORMAT "%s"
1661                      " alignment=" SIZE_FORMAT "%s"
1662                      " size=" SIZE_FORMAT "%s",
1663                      str,
1664                      trace_page_size_params(requested_size),
1665                      p2i(base),
1666                      trace_page_size_params(page_size),
1667                      trace_page_size_params(alignment),
1668                      trace_page_size_params(size));
1669 }
1670 
1671 
1672 // This is the working definition of a server class machine:
1673 // >= 2 physical CPU's and >=2GB of memory, with some fuzz
1674 // because the graphics memory (?) sometimes masks physical memory.
1675 // If you want to change the definition of a server class machine
1676 // on some OS or platform, e.g., >=4GB on Windows platforms,
1677 // then you'll have to parameterize this method based on that state,
1678 // as was done for logical processors here, or replicate and
1679 // specialize this method for each platform.  (Or fix os to have
1680 // some inheritance structure and use subclassing.  Sigh.)
1681 // If you want some platform to always or never behave as a server
1682 // class machine, change the setting of AlwaysActAsServerClassMachine
1683 // and NeverActAsServerClassMachine in globals*.hpp.
1684 bool os::is_server_class_machine() {
1685   // First check for the early returns
1686   if (NeverActAsServerClassMachine) {
1687     return false;
1688   }
1689   if (AlwaysActAsServerClassMachine) {
1690     return true;
1691   }
1692   // Then actually look at the machine
1693   bool         result            = false;
1694   const unsigned int    server_processors = 2;
1695   const julong server_memory     = 2UL * G;
1696   // We seem not to get our full complement of memory.
1697   //     We allow some part (1/8?) of the memory to be "missing",
1698   //     based on the sizes of DIMMs, and maybe graphics cards.
1699   const julong missing_memory   = 256UL * M;
1700 
1701   /* Is this a server class machine? */
1702   if ((os::active_processor_count() >= (int)server_processors) &&
1703       (os::physical_memory() >= (server_memory - missing_memory))) {
1704     const unsigned int logical_processors =
1705       VM_Version::logical_processors_per_package();
1706     if (logical_processors > 1) {
1707       const unsigned int physical_packages =
1708         os::active_processor_count() / logical_processors;
1709       if (physical_packages >= server_processors) {
1710         result = true;
1711       }
1712     } else {
1713       result = true;
1714     }
1715   }
1716   return result;
1717 }
1718 
1719 void os::initialize_initial_active_processor_count() {
1720   assert(_initial_active_processor_count == 0, "Initial active processor count already set.");
1721   _initial_active_processor_count = active_processor_count();
1722   log_debug(os)("Initial active processor count set to %d" , _initial_active_processor_count);
1723 }
1724 
1725 bool os::create_stack_guard_pages(char* addr, size_t bytes) {
1726   return os::pd_create_stack_guard_pages(addr, bytes);
1727 }
1728 
1729 char* os::reserve_memory(size_t bytes, bool executable, MEMFLAGS flags) {
1730   char* result = pd_reserve_memory(bytes, executable);
1731   if (result != nullptr) {
1732     MemTracker::record_virtual_memory_reserve(result, bytes, CALLER_PC, flags);
1733   }
1734   return result;
1735 }
1736 
1737 char* os::attempt_reserve_memory_at(char* addr, size_t bytes, bool executable) {
1738   char* result = pd_attempt_reserve_memory_at(addr, bytes, executable);
1739   if (result != nullptr) {
1740     MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
1741   } else {
1742     log_debug(os)("Attempt to reserve memory at " INTPTR_FORMAT " for "
1743                  SIZE_FORMAT " bytes failed, errno %d", p2i(addr), bytes, get_last_error());
1744   }
1745   return result;
1746 }
1747 
1748 static void assert_nonempty_range(const char* addr, size_t bytes) {
1749   assert(addr != nullptr && bytes > 0, "invalid range [" PTR_FORMAT ", " PTR_FORMAT ")",
1750          p2i(addr), p2i(addr) + bytes);
1751 }
1752 
1753 bool os::commit_memory(char* addr, size_t bytes, bool executable) {
1754   assert_nonempty_range(addr, bytes);
1755   bool res = pd_commit_memory(addr, bytes, executable);
1756   if (res) {
1757     MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1758   }
1759   return res;
1760 }
1761 
1762 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
1763                               bool executable) {
1764   assert_nonempty_range(addr, size);
1765   bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
1766   if (res) {
1767     MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1768   }
1769   return res;
1770 }
1771 
1772 void os::commit_memory_or_exit(char* addr, size_t bytes, bool executable,
1773                                const char* mesg) {
1774   assert_nonempty_range(addr, bytes);
1775   pd_commit_memory_or_exit(addr, bytes, executable, mesg);
1776   MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
1777 }
1778 
1779 void os::commit_memory_or_exit(char* addr, size_t size, size_t alignment_hint,
1780                                bool executable, const char* mesg) {
1781   assert_nonempty_range(addr, size);
1782   os::pd_commit_memory_or_exit(addr, size, alignment_hint, executable, mesg);
1783   MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
1784 }
1785 
1786 bool os::uncommit_memory(char* addr, size_t bytes, bool executable) {
1787   assert_nonempty_range(addr, bytes);
1788   bool res;
1789   if (MemTracker::enabled()) {
1790     Tracker tkr(Tracker::uncommit);
1791     res = pd_uncommit_memory(addr, bytes, executable);
1792     if (res) {
1793       tkr.record((address)addr, bytes);
1794     }
1795   } else {
1796     res = pd_uncommit_memory(addr, bytes, executable);
1797   }
1798   return res;
1799 }
1800 
1801 bool os::release_memory(char* addr, size_t bytes) {
1802   assert_nonempty_range(addr, bytes);
1803   bool res;
1804   if (MemTracker::enabled()) {
1805     // Note: Tracker contains a ThreadCritical.
1806     Tracker tkr(Tracker::release);
1807     res = pd_release_memory(addr, bytes);
1808     if (res) {
1809       tkr.record((address)addr, bytes);
1810     }
1811   } else {
1812     res = pd_release_memory(addr, bytes);
1813   }
1814   if (!res) {
1815     log_info(os)("os::release_memory failed (" PTR_FORMAT ", " SIZE_FORMAT ")", p2i(addr), bytes);
1816   }
1817   return res;
1818 }
1819 
1820 // Prints all mappings
1821 void os::print_memory_mappings(outputStream* st) {
1822   os::print_memory_mappings(nullptr, (size_t)-1, st);
1823 }
1824 
1825 // Pretouching must use a store, not just a load.  On many OSes loads from
1826 // fresh memory would be satisfied from a single mapped page containing all
1827 // zeros.  We need to store something to each page to get them backed by
1828 // their own memory, which is the effect we want here.  An atomic add of
1829 // zero is used instead of a simple store, allowing the memory to be used
1830 // while pretouch is in progress, rather than requiring users of the memory
1831 // to wait until the entire range has been touched.  This is technically
1832 // a UB data race, but doesn't cause any problems for us.
1833 void os::pretouch_memory(void* start, void* end, size_t page_size) {
1834   assert(start <= end, "invalid range: " PTR_FORMAT " -> " PTR_FORMAT, p2i(start), p2i(end));
1835   assert(is_power_of_2(page_size), "page size misaligned: %zu", page_size);
1836   assert(page_size >= sizeof(int), "page size too small: %zu", page_size);
1837   if (start < end) {
1838     // We're doing concurrent-safe touch and memory state has page
1839     // granularity, so we can touch anywhere in a page.  Touch at the
1840     // beginning of each page to simplify iteration.
1841     char* cur = static_cast<char*>(align_down(start, page_size));
1842     void* last = align_down(static_cast<char*>(end) - 1, page_size);
1843     assert(cur <= last, "invariant");
1844     // Iterate from first page through last (inclusive), being careful to
1845     // avoid overflow if the last page abuts the end of the address range.
1846     for ( ; true; cur += page_size) {
1847       Atomic::add(reinterpret_cast<int*>(cur), 0, memory_order_relaxed);
1848       if (cur >= last) break;
1849     }
1850   }
1851 }
1852 
1853 char* os::map_memory_to_file(size_t bytes, int file_desc) {
1854   // Could have called pd_reserve_memory() followed by replace_existing_mapping_with_file_mapping(),
1855   // but AIX may use SHM in which case its more trouble to detach the segment and remap memory to the file.
1856   // On all current implementations null is interpreted as any available address.
1857   char* result = os::map_memory_to_file(nullptr /* addr */, bytes, file_desc);
1858   if (result != nullptr) {
1859     MemTracker::record_virtual_memory_reserve_and_commit(result, bytes, CALLER_PC);
1860   }
1861   return result;
1862 }
1863 
1864 char* os::attempt_map_memory_to_file_at(char* addr, size_t bytes, int file_desc) {
1865   char* result = pd_attempt_map_memory_to_file_at(addr, bytes, file_desc);
1866   if (result != nullptr) {
1867     MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC);
1868   }
1869   return result;
1870 }
1871 
1872 char* os::map_memory(int fd, const char* file_name, size_t file_offset,
1873                            char *addr, size_t bytes, bool read_only,
1874                            bool allow_exec, MEMFLAGS flags) {
1875   char* result = pd_map_memory(fd, file_name, file_offset, addr, bytes, read_only, allow_exec);
1876   if (result != nullptr) {
1877     MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC, flags);
1878   }
1879   return result;
1880 }
1881 
1882 char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
1883                              char *addr, size_t bytes, bool read_only,
1884                              bool allow_exec) {
1885   return pd_remap_memory(fd, file_name, file_offset, addr, bytes,
1886                     read_only, allow_exec);
1887 }
1888 
1889 bool os::unmap_memory(char *addr, size_t bytes) {
1890   bool result;
1891   if (MemTracker::enabled()) {
1892     Tracker tkr(Tracker::release);
1893     result = pd_unmap_memory(addr, bytes);
1894     if (result) {
1895       tkr.record((address)addr, bytes);
1896     }
1897   } else {
1898     result = pd_unmap_memory(addr, bytes);
1899   }
1900   return result;
1901 }
1902 
1903 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
1904   pd_free_memory(addr, bytes, alignment_hint);
1905 }
1906 
1907 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
1908   pd_realign_memory(addr, bytes, alignment_hint);
1909 }
1910 
1911 char* os::reserve_memory_special(size_t size, size_t alignment, size_t page_size,
1912                                  char* addr, bool executable) {
1913 
1914   assert(is_aligned(addr, alignment), "Unaligned request address");
1915 
1916   char* result = pd_reserve_memory_special(size, alignment, page_size, addr, executable);
1917   if (result != nullptr) {
1918     // The memory is committed
1919     MemTracker::record_virtual_memory_reserve_and_commit((address)result, size, CALLER_PC);
1920   }
1921 
1922   return result;
1923 }
1924 
1925 bool os::release_memory_special(char* addr, size_t bytes) {
1926   bool res;
1927   if (MemTracker::enabled()) {
1928     // Note: Tracker contains a ThreadCritical.
1929     Tracker tkr(Tracker::release);
1930     res = pd_release_memory_special(addr, bytes);
1931     if (res) {
1932       tkr.record((address)addr, bytes);
1933     }
1934   } else {
1935     res = pd_release_memory_special(addr, bytes);
1936   }
1937   return res;
1938 }
1939 
1940 // Convenience wrapper around naked_short_sleep to allow for longer sleep
1941 // times. Only for use by non-JavaThreads.
1942 void os::naked_sleep(jlong millis) {
1943   assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
1944   const jlong limit = 999;
1945   while (millis > limit) {
1946     naked_short_sleep(limit);
1947     millis -= limit;
1948   }
1949   naked_short_sleep(millis);
1950 }
1951 
1952 
1953 ////// Implementation of PageSizes
1954 
1955 void os::PageSizes::add(size_t page_size) {
1956   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
1957   _v |= page_size;
1958 }
1959 
1960 bool os::PageSizes::contains(size_t page_size) const {
1961   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
1962   return (_v & page_size) != 0;
1963 }
1964 
1965 size_t os::PageSizes::next_smaller(size_t page_size) const {
1966   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
1967   size_t v2 = _v & (page_size - 1);
1968   if (v2 == 0) {
1969     return 0;
1970   }
1971   return round_down_power_of_2(v2);
1972 }
1973 
1974 size_t os::PageSizes::next_larger(size_t page_size) const {
1975   assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
1976   if (page_size == max_power_of_2<size_t>()) { // Shift by 32/64 would be UB
1977     return 0;
1978   }
1979   // Remove current and smaller page sizes
1980   size_t v2 = _v & ~(page_size + (page_size - 1));
1981   if (v2 == 0) {
1982     return 0;
1983   }
1984   return (size_t)1 << count_trailing_zeros(v2);
1985 }
1986 
1987 size_t os::PageSizes::largest() const {
1988   const size_t max = max_power_of_2<size_t>();
1989   if (contains(max)) {
1990     return max;
1991   }
1992   return next_smaller(max);
1993 }
1994 
1995 size_t os::PageSizes::smallest() const {
1996   // Strictly speaking the set should not contain sizes < os::vm_page_size().
1997   // But this is not enforced.
1998   return next_larger(1);
1999 }
2000 
2001 void os::PageSizes::print_on(outputStream* st) const {
2002   bool first = true;
2003   for (size_t sz = smallest(); sz != 0; sz = next_larger(sz)) {
2004     if (first) {
2005       first = false;
2006     } else {
2007       st->print_raw(", ");
2008     }
2009     if (sz < M) {
2010       st->print(SIZE_FORMAT "k", sz / K);
2011     } else if (sz < G) {
2012       st->print(SIZE_FORMAT "M", sz / M);
2013     } else {
2014       st->print(SIZE_FORMAT "G", sz / G);
2015     }
2016   }
2017   if (first) {
2018     st->print("empty");
2019   }
2020 }
2021 
2022 // Check minimum allowable stack sizes for thread creation and to initialize
2023 // the java system classes, including StackOverflowError - depends on page
2024 // size.
2025 // The space needed for frames during startup is platform dependent. It
2026 // depends on word size, platform calling conventions, C frame layout and
2027 // interpreter/C1/C2 design decisions. Therefore this is given in a
2028 // platform (os/cpu) dependent constant.
2029 // To this, space for guard mechanisms is added, which depends on the
2030 // page size which again depends on the concrete system the VM is running
2031 // on. Space for libc guard pages is not included in this size.
2032 jint os::set_minimum_stack_sizes() {
2033 
2034   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
2035                                    StackOverflow::stack_guard_zone_size() +
2036                                    StackOverflow::stack_shadow_zone_size();
2037 
2038   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
2039   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, _os_min_stack_allowed);
2040 
2041   size_t stack_size_in_bytes = ThreadStackSize * K;
2042   if (stack_size_in_bytes != 0 &&
2043       stack_size_in_bytes < _java_thread_min_stack_allowed) {
2044     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
2045     // ThreadStackSize so we go with "Java thread stack size" instead
2046     // of "ThreadStackSize" to be more friendly.
2047     tty->print_cr("\nThe Java thread stack size specified is too small. "
2048                   "Specify at least " SIZE_FORMAT "k",
2049                   _java_thread_min_stack_allowed / K);
2050     return JNI_ERR;
2051   }
2052 
2053   // Make the stack size a multiple of the page size so that
2054   // the yellow/red zones can be guarded.
2055   JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));
2056 
2057   // Reminder: a compiler thread is a Java thread.
2058   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
2059                                        StackOverflow::stack_guard_zone_size() +
2060                                        StackOverflow::stack_shadow_zone_size();
2061 
2062   _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
2063   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, _os_min_stack_allowed);
2064 
2065   stack_size_in_bytes = CompilerThreadStackSize * K;
2066   if (stack_size_in_bytes != 0 &&
2067       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
2068     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
2069                   "Specify at least " SIZE_FORMAT "k",
2070                   _compiler_thread_min_stack_allowed / K);
2071     return JNI_ERR;
2072   }
2073 
2074   _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
2075   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, _os_min_stack_allowed);
2076 
2077   stack_size_in_bytes = VMThreadStackSize * K;
2078   if (stack_size_in_bytes != 0 &&
2079       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
2080     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
2081                   "Specify at least " SIZE_FORMAT "k",
2082                   _vm_internal_thread_min_stack_allowed / K);
2083     return JNI_ERR;
2084   }
2085   return JNI_OK;
2086 }