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 *
57 #include "runtime/os.hpp"
58 #include "runtime/safepoint.hpp"
59 #include "runtime/safepointMechanism.hpp"
60 #include "runtime/synchronizer.hpp"
61 #include "runtime/vm_version.hpp"
62 #include "services/management.hpp"
63 #include "utilities/align.hpp"
64 #include "utilities/checkedCast.hpp"
65 #include "utilities/debug.hpp"
66 #include "utilities/defaultStream.hpp"
67 #include "utilities/macros.hpp"
68 #include "utilities/parseInteger.hpp"
69 #include "utilities/powerOfTwo.hpp"
70 #include "utilities/stringUtils.hpp"
71 #include "utilities/systemMemoryBarrier.hpp"
72 #if INCLUDE_JFR
73 #include "jfr/jfr.hpp"
74 #endif
75
76 #include <limits>
77
78 static const char _default_java_launcher[] = "generic";
79
80 #define DEFAULT_JAVA_LAUNCHER _default_java_launcher
81
82 char* Arguments::_jvm_flags_file = nullptr;
83 char** Arguments::_jvm_flags_array = nullptr;
84 int Arguments::_num_jvm_flags = 0;
85 char** Arguments::_jvm_args_array = nullptr;
86 int Arguments::_num_jvm_args = 0;
87 char* Arguments::_java_command = nullptr;
88 SystemProperty* Arguments::_system_properties = nullptr;
89 size_t Arguments::_conservative_max_heap_alignment = 0;
90 Arguments::Mode Arguments::_mode = _mixed;
91 const char* Arguments::_java_vendor_url_bug = nullptr;
92 const char* Arguments::_sun_java_launcher = DEFAULT_JAVA_LAUNCHER;
93 bool Arguments::_sun_java_launcher_is_altjvm = false;
94
95 // These parameters are reset in method parse_vm_init_args()
96 bool Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods;
1781 }
1782 _sun_java_launcher = os::strdup_check_oom(launcher);
1783 }
1784
1785 bool Arguments::created_by_java_launcher() {
1786 assert(_sun_java_launcher != nullptr, "property must have value");
1787 return strcmp(DEFAULT_JAVA_LAUNCHER, _sun_java_launcher) != 0;
1788 }
1789
1790 bool Arguments::sun_java_launcher_is_altjvm() {
1791 return _sun_java_launcher_is_altjvm;
1792 }
1793
1794 //===========================================================================================================
1795 // Parsing of main arguments
1796
1797 unsigned int addreads_count = 0;
1798 unsigned int addexports_count = 0;
1799 unsigned int addopens_count = 0;
1800 unsigned int addmods_count = 0;
1801 unsigned int patch_mod_count = 0;
1802 unsigned int enable_native_access_count = 0;
1803
1804 // Check the consistency of vm_init_args
1805 bool Arguments::check_vm_args_consistency() {
1806 // Method for adding checks for flag consistency.
1807 // The intent is to warn the user of all possible conflicts,
1808 // before returning an error.
1809 // Note: Needs platform-dependent factoring.
1810 bool status = true;
1811
1812 if (TLABRefillWasteFraction == 0) {
1813 jio_fprintf(defaultStream::error_stream(),
1814 "TLABRefillWasteFraction should be a denominator, "
1815 "not " SIZE_FORMAT "\n",
1816 TLABRefillWasteFraction);
1817 status = false;
1818 }
1819
1820 status = CompilerConfig::check_args_consistency(status);
1821 #if INCLUDE_JVMCI
1828 }
1829 }
1830 }
1831 #endif
1832
1833 #if INCLUDE_JFR
1834 if (status && (FlightRecorderOptions || StartFlightRecording)) {
1835 if (!create_numbered_module_property("jdk.module.addmods", "jdk.jfr", addmods_count++)) {
1836 return false;
1837 }
1838 }
1839 #endif
1840
1841 #ifndef SUPPORT_RESERVED_STACK_AREA
1842 if (StackReservedPages != 0) {
1843 FLAG_SET_CMDLINE(StackReservedPages, 0);
1844 warning("Reserved Stack Area not supported on this platform");
1845 }
1846 #endif
1847
1848 #if !defined(X86) && !defined(AARCH64) && !defined(RISCV64) && !defined(ARM) && !defined(PPC64) && !defined(S390)
1849 if (LockingMode == LM_LIGHTWEIGHT) {
1850 FLAG_SET_CMDLINE(LockingMode, LM_LEGACY);
1851 warning("New lightweight locking not supported on this platform");
1852 }
1853 #endif
1854
1855 #if !defined(X86) && !defined(AARCH64) && !defined(PPC64) && !defined(RISCV64) && !defined(S390)
1856 if (LockingMode == LM_MONITOR) {
1857 jio_fprintf(defaultStream::error_stream(),
1858 "LockingMode == 0 (LM_MONITOR) is not fully implemented on this architecture\n");
1859 return false;
1860 }
1861 #endif
1862 #if defined(X86) && !defined(ZERO)
1863 if (LockingMode == LM_MONITOR && UseRTMForStackLocks) {
1864 jio_fprintf(defaultStream::error_stream(),
1865 "LockingMode == 0 (LM_MONITOR) and -XX:+UseRTMForStackLocks are mutually exclusive\n");
1866
1867 return false;
1954 }
1955
1956 jio_fprintf(defaultStream::error_stream(), "Property count limit exceeded: %s, limit=%d\n", prop_base_name, props_count_limit);
1957 return false;
1958 }
1959
1960 Arguments::ArgsRange Arguments::parse_memory_size(const char* s,
1961 julong* long_arg,
1962 julong min_size,
1963 julong max_size) {
1964 if (!parse_integer(s, long_arg)) return arg_unreadable;
1965 return check_memory_size(*long_arg, min_size, max_size);
1966 }
1967
1968 // Parse JavaVMInitArgs structure
1969
1970 jint Arguments::parse_vm_init_args(const JavaVMInitArgs *vm_options_args,
1971 const JavaVMInitArgs *java_tool_options_args,
1972 const JavaVMInitArgs *java_options_args,
1973 const JavaVMInitArgs *cmd_line_args) {
1974 bool patch_mod_javabase = false;
1975
1976 // Save default settings for some mode flags
1977 Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods;
1978 Arguments::_UseOnStackReplacement = UseOnStackReplacement;
1979 Arguments::_ClipInlining = ClipInlining;
1980 Arguments::_BackgroundCompilation = BackgroundCompilation;
1981
1982 // Remember the default value of SharedBaseAddress.
1983 Arguments::_default_SharedBaseAddress = SharedBaseAddress;
1984
1985 // Setup flags for mixed which is the default
1986 set_mode_flags(_mixed);
1987
1988 // Parse args structure generated from java.base vm options resource
1989 jint result = parse_each_vm_init_arg(vm_options_args, &patch_mod_javabase, JVMFlagOrigin::JIMAGE_RESOURCE);
1990 if (result != JNI_OK) {
1991 return result;
1992 }
1993
1994 // Parse args structure generated from JAVA_TOOL_OPTIONS environment
1995 // variable (if present).
1996 result = parse_each_vm_init_arg(java_tool_options_args, &patch_mod_javabase, JVMFlagOrigin::ENVIRON_VAR);
1997 if (result != JNI_OK) {
1998 return result;
1999 }
2000
2001 // Parse args structure generated from the command line flags.
2002 result = parse_each_vm_init_arg(cmd_line_args, &patch_mod_javabase, JVMFlagOrigin::COMMAND_LINE);
2003 if (result != JNI_OK) {
2004 return result;
2005 }
2006
2007 // Parse args structure generated from the _JAVA_OPTIONS environment
2008 // variable (if present) (mimics classic VM)
2009 result = parse_each_vm_init_arg(java_options_args, &patch_mod_javabase, JVMFlagOrigin::ENVIRON_VAR);
2010 if (result != JNI_OK) {
2011 return result;
2012 }
2013
2014 // Disable CDS for exploded image
2015 if (!has_jimage()) {
2016 no_shared_spaces("CDS disabled on exploded JDK");
2017 }
2018
2019 // We need to ensure processor and memory resources have been properly
2020 // configured - which may rely on arguments we just processed - before
2021 // doing the final argument processing. Any argument processing that
2022 // needs to know about processor and memory resources must occur after
2023 // this point.
2024
2025 os::init_container_support();
2026
2027 SystemMemoryBarrier::initialize();
2028
2029 // Do final processing now that all arguments have been parsed
2030 result = finalize_vm_init_args(patch_mod_javabase);
2031 if (result != JNI_OK) {
2032 return result;
2033 }
2034
2035 return JNI_OK;
2036 }
2037
2038 // Checks if name in command-line argument -agent{lib,path}:name[=options]
2039 // represents a valid JDWP agent. is_path==true denotes that we
2040 // are dealing with -agentpath (case where name is a path), otherwise with
2041 // -agentlib
2042 bool valid_jdwp_agent(char *name, bool is_path) {
2043 char *_name;
2044 const char *_jdwp = "jdwp";
2045 size_t _len_jdwp, _len_prefix;
2046
2047 if (is_path) {
2048 if ((_name = strrchr(name, (int) *os::file_separator())) == nullptr) {
2049 return false;
2050 }
2063 _name += _len_jdwp;
2064 }
2065 else {
2066 return false;
2067 }
2068
2069 if (strcmp(_name, JNI_LIB_SUFFIX) != 0) {
2070 return false;
2071 }
2072
2073 return true;
2074 }
2075
2076 if (strcmp(name, _jdwp) == 0) {
2077 return true;
2078 }
2079
2080 return false;
2081 }
2082
2083 int Arguments::process_patch_mod_option(const char* patch_mod_tail, bool* patch_mod_javabase) {
2084 // --patch-module=<module>=<file>(<pathsep><file>)*
2085 assert(patch_mod_tail != nullptr, "Unexpected null patch-module value");
2086 // Find the equal sign between the module name and the path specification
2087 const char* module_equal = strchr(patch_mod_tail, '=');
2088 if (module_equal == nullptr) {
2089 jio_fprintf(defaultStream::output_stream(), "Missing '=' in --patch-module specification\n");
2090 return JNI_ERR;
2091 } else {
2092 // Pick out the module name
2093 size_t module_len = module_equal - patch_mod_tail;
2094 char* module_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, module_len+1, mtArguments);
2095 if (module_name != nullptr) {
2096 memcpy(module_name, patch_mod_tail, module_len);
2097 *(module_name + module_len) = '\0';
2098 // The path piece begins one past the module_equal sign
2099 add_patch_mod_prefix(module_name, module_equal + 1, patch_mod_javabase);
2100 FREE_C_HEAP_ARRAY(char, module_name);
2101 if (!create_numbered_module_property("jdk.module.patch", patch_mod_tail, patch_mod_count++)) {
2102 return JNI_ENOMEM;
2103 }
2104 } else {
2105 return JNI_ENOMEM;
2106 }
2107 }
2108 return JNI_OK;
2109 }
2110
2111 // Parse -Xss memory string parameter and convert to ThreadStackSize in K.
2112 jint Arguments::parse_xss(const JavaVMOption* option, const char* tail, intx* out_ThreadStackSize) {
2113 // The min and max sizes match the values in globals.hpp, but scaled
2114 // with K. The values have been chosen so that alignment with page
2115 // size doesn't change the max value, which makes the conversions
2116 // back and forth between Xss value and ThreadStackSize value easier.
2117 // The values have also been chosen to fit inside a 32-bit signed type.
2118 const julong min_ThreadStackSize = 0;
2119 const julong max_ThreadStackSize = 1 * M;
2120
2121 // Make sure the above values match the range set in globals.hpp
2122 const JVMTypedFlagLimit<intx>* limit = JVMFlagLimit::get_range_at(FLAG_MEMBER_ENUM(ThreadStackSize))->cast<intx>();
2123 assert(min_ThreadStackSize == static_cast<julong>(limit->min()), "must be");
2124 assert(max_ThreadStackSize == static_cast<julong>(limit->max()), "must be");
2125
2126 const julong min_size = min_ThreadStackSize * K;
2127 const julong max_size = max_ThreadStackSize * K;
2128
2129 assert(is_aligned(max_size, os::vm_page_size()), "Implementation assumption");
2130
2145 assert(size <= size_aligned,
2146 "Overflow: " JULONG_FORMAT " " JULONG_FORMAT,
2147 size, size_aligned);
2148
2149 const julong size_in_K = size_aligned / K;
2150 assert(size_in_K < (julong)max_intx,
2151 "size_in_K doesn't fit in the type of ThreadStackSize: " JULONG_FORMAT,
2152 size_in_K);
2153
2154 // Check that code expanding ThreadStackSize to a page aligned number of bytes won't overflow.
2155 const julong max_expanded = align_up(size_in_K * K, os::vm_page_size());
2156 assert(max_expanded < max_uintx && max_expanded >= size_in_K,
2157 "Expansion overflowed: " JULONG_FORMAT " " JULONG_FORMAT,
2158 max_expanded, size_in_K);
2159
2160 *out_ThreadStackSize = (intx)size_in_K;
2161
2162 return JNI_OK;
2163 }
2164
2165 jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_mod_javabase, JVMFlagOrigin origin) {
2166 // For match_option to return remaining or value part of option string
2167 const char* tail;
2168
2169 // iterate over arguments
2170 for (int index = 0; index < args->nOptions; index++) {
2171 bool is_absolute_path = false; // for -agentpath vs -agentlib
2172
2173 const JavaVMOption* option = args->options + index;
2174
2175 if (!match_option(option, "-Djava.class.path", &tail) &&
2176 !match_option(option, "-Dsun.java.command", &tail) &&
2177 !match_option(option, "-Dsun.java.launcher", &tail)) {
2178
2179 // add all jvm options to the jvm_args string. This string
2180 // is used later to set the java.vm.args PerfData string constant.
2181 // the -Djava.class.path and the -Dsun.java.command options are
2182 // omitted from jvm_args string as each have their own PerfData
2183 // string constant object.
2184 build_jvm_args(option->optionString);
2185 }
2272 return JNI_ENOMEM;
2273 }
2274 } else if (match_option(option, "--enable-native-access=", &tail)) {
2275 if (!create_numbered_module_property("jdk.module.enable.native.access", tail, enable_native_access_count++)) {
2276 return JNI_ENOMEM;
2277 }
2278 } else if (match_option(option, "--limit-modules=", &tail)) {
2279 if (!create_module_property("jdk.module.limitmods", tail, InternalProperty)) {
2280 return JNI_ENOMEM;
2281 }
2282 } else if (match_option(option, "--module-path=", &tail)) {
2283 if (!create_module_property("jdk.module.path", tail, ExternalProperty)) {
2284 return JNI_ENOMEM;
2285 }
2286 } else if (match_option(option, "--upgrade-module-path=", &tail)) {
2287 if (!create_module_property("jdk.module.upgrade.path", tail, ExternalProperty)) {
2288 return JNI_ENOMEM;
2289 }
2290 } else if (match_option(option, "--patch-module=", &tail)) {
2291 // --patch-module=<module>=<file>(<pathsep><file>)*
2292 int res = process_patch_mod_option(tail, patch_mod_javabase);
2293 if (res != JNI_OK) {
2294 return res;
2295 }
2296 } else if (match_option(option, "--illegal-access=", &tail)) {
2297 char version[256];
2298 JDK_Version::jdk(17).to_string(version, sizeof(version));
2299 warning("Ignoring option %s; support was removed in %s", option->optionString, version);
2300 // -agentlib and -agentpath
2301 } else if (match_option(option, "-agentlib:", &tail) ||
2302 (is_absolute_path = match_option(option, "-agentpath:", &tail))) {
2303 if(tail != nullptr) {
2304 const char* pos = strchr(tail, '=');
2305 char* name;
2306 if (pos == nullptr) {
2307 name = os::strdup_check_oom(tail, mtArguments);
2308 } else {
2309 size_t len = pos - tail;
2310 name = NEW_C_HEAP_ARRAY(char, len + 1, mtArguments);
2311 memcpy(name, tail, len);
2312 name[len] = '\0';
2333 jio_fprintf(defaultStream::error_stream(),
2334 "Instrumentation agents are not supported in this VM\n");
2335 return JNI_ERR;
2336 #else
2337 if (tail != nullptr) {
2338 size_t length = strlen(tail) + 1;
2339 char *options = NEW_C_HEAP_ARRAY(char, length, mtArguments);
2340 jio_snprintf(options, length, "%s", tail);
2341 JvmtiAgentList::add("instrument", options, false);
2342 FREE_C_HEAP_ARRAY(char, options);
2343
2344 // java agents need module java.instrument
2345 if (!create_numbered_module_property("jdk.module.addmods", "java.instrument", addmods_count++)) {
2346 return JNI_ENOMEM;
2347 }
2348 }
2349 #endif // !INCLUDE_JVMTI
2350 // --enable_preview
2351 } else if (match_option(option, "--enable-preview")) {
2352 set_enable_preview();
2353 // -Xnoclassgc
2354 } else if (match_option(option, "-Xnoclassgc")) {
2355 if (FLAG_SET_CMDLINE(ClassUnloading, false) != JVMFlag::SUCCESS) {
2356 return JNI_EINVAL;
2357 }
2358 // -Xbatch
2359 } else if (match_option(option, "-Xbatch")) {
2360 if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
2361 return JNI_EINVAL;
2362 }
2363 // -Xmn for compatibility with other JVM vendors
2364 } else if (match_option(option, "-Xmn", &tail)) {
2365 julong long_initial_young_size = 0;
2366 ArgsRange errcode = parse_memory_size(tail, &long_initial_young_size, 1);
2367 if (errcode != arg_in_range) {
2368 jio_fprintf(defaultStream::error_stream(),
2369 "Invalid initial young generation size: %s\n", option->optionString);
2370 describe_range_error(errcode);
2371 return JNI_EINVAL;
2372 }
2815 // Unknown option
2816 } else if (is_bad_option(option, args->ignoreUnrecognized)) {
2817 return JNI_ERR;
2818 }
2819 }
2820
2821 // PrintSharedArchiveAndExit will turn on
2822 // -Xshare:on
2823 // -Xlog:class+path=info
2824 if (PrintSharedArchiveAndExit) {
2825 UseSharedSpaces = true;
2826 RequireSharedSpaces = true;
2827 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, path));
2828 }
2829
2830 fix_appclasspath();
2831
2832 return JNI_OK;
2833 }
2834
2835 void Arguments::add_patch_mod_prefix(const char* module_name, const char* path, bool* patch_mod_javabase) {
2836 // For java.base check for duplicate --patch-module options being specified on the command line.
2837 // This check is only required for java.base, all other duplicate module specifications
2838 // will be checked during module system initialization. The module system initialization
2839 // will throw an ExceptionInInitializerError if this situation occurs.
2840 if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
2841 if (*patch_mod_javabase) {
2842 vm_exit_during_initialization("Cannot specify " JAVA_BASE_NAME " more than once to --patch-module");
2843 } else {
2844 *patch_mod_javabase = true;
2845 }
2846 }
2847
2848 // Create GrowableArray lazily, only if --patch-module has been specified
2849 if (_patch_mod_prefix == nullptr) {
2850 _patch_mod_prefix = new (mtArguments) GrowableArray<ModulePatchPath*>(10, mtArguments);
2851 }
2852
2853 _patch_mod_prefix->push(new ModulePatchPath(module_name, path));
2854 }
2855
2856 // Remove all empty paths from the app classpath (if IgnoreEmptyClassPaths is enabled)
2857 //
2858 // This is necessary because some apps like to specify classpath like -cp foo.jar:${XYZ}:bar.jar
2859 // in their start-up scripts. If XYZ is empty, the classpath will look like "-cp foo.jar::bar.jar".
2860 // Java treats such empty paths as if the user specified "-cp foo.jar:.:bar.jar". I.e., an empty
2861 // path is treated as the current directory.
2862 //
2863 // This causes problems with CDS, which requires that all directories specified in the classpath
2864 // must be empty. In most cases, applications do NOT want to load classes from the current
2865 // directory anyway. Adding -XX:+IgnoreEmptyClassPaths will make these applications' start-up
2866 // scripts compatible with CDS.
2867 void Arguments::fix_appclasspath() {
2868 if (IgnoreEmptyClassPaths) {
2869 const char separator = *os::path_separator();
2870 const char* src = _java_class_path->value();
2871
2872 // skip over all the leading empty paths
2873 while (*src == separator) {
2876
2877 char* copy = os::strdup_check_oom(src, mtArguments);
2878
2879 // trim all trailing empty paths
2880 for (char* tail = copy + strlen(copy) - 1; tail >= copy && *tail == separator; tail--) {
2881 *tail = '\0';
2882 }
2883
2884 char from[3] = {separator, separator, '\0'};
2885 char to [2] = {separator, '\0'};
2886 while (StringUtils::replace_no_expand(copy, from, to) > 0) {
2887 // Keep replacing "::" -> ":" until we have no more "::" (non-windows)
2888 // Keep replacing ";;" -> ";" until we have no more ";;" (windows)
2889 }
2890
2891 _java_class_path->set_writeable_value(copy);
2892 FreeHeap(copy); // a copy was made by set_value, so don't need this anymore
2893 }
2894 }
2895
2896 jint Arguments::finalize_vm_init_args(bool patch_mod_javabase) {
2897 // check if the default lib/endorsed directory exists; if so, error
2898 char path[JVM_MAXPATHLEN];
2899 const char* fileSep = os::file_separator();
2900 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sendorsed", Arguments::get_java_home(), fileSep, fileSep);
2901
2902 DIR* dir = os::opendir(path);
2903 if (dir != nullptr) {
2904 jio_fprintf(defaultStream::output_stream(),
2905 "<JAVA_HOME>/lib/endorsed is not supported. Endorsed standards and standalone APIs\n"
2906 "in modular form will be supported via the concept of upgradeable modules.\n");
2907 os::closedir(dir);
2908 return JNI_ERR;
2909 }
2910
2911 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sext", Arguments::get_java_home(), fileSep, fileSep);
2912 dir = os::opendir(path);
2913 if (dir != nullptr) {
2914 jio_fprintf(defaultStream::output_stream(),
2915 "<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; "
2916 "Use -classpath instead.\n.");
2950 if (FLAG_IS_DEFAULT(UseLargePages) &&
2951 MaxHeapSize < LargePageHeapSizeThreshold) {
2952 // No need for large granularity pages w/small heaps.
2953 // Note that large pages are enabled/disabled for both the
2954 // Java heap and the code cache.
2955 FLAG_SET_DEFAULT(UseLargePages, false);
2956 }
2957
2958 UNSUPPORTED_OPTION(ProfileInterpreter);
2959 #endif
2960
2961 // Parse the CompilationMode flag
2962 if (!CompilationModeFlag::initialize()) {
2963 return JNI_ERR;
2964 }
2965
2966 if (!check_vm_args_consistency()) {
2967 return JNI_ERR;
2968 }
2969
2970 if (!CDSConfig::check_vm_args_consistency(patch_mod_javabase, mode_flag_cmd_line)) {
2971 return JNI_ERR;
2972 }
2973
2974 #ifndef CAN_SHOW_REGISTERS_ON_ASSERT
2975 UNSUPPORTED_OPTION(ShowRegistersOnAssert);
2976 #endif // CAN_SHOW_REGISTERS_ON_ASSERT
2977
2978 return JNI_OK;
2979 }
2980
2981 // Helper class for controlling the lifetime of JavaVMInitArgs
2982 // objects. The contents of the JavaVMInitArgs are guaranteed to be
2983 // deleted on the destruction of the ScopedVMInitArgs object.
2984 class ScopedVMInitArgs : public StackObj {
2985 private:
2986 JavaVMInitArgs _args;
2987 char* _container_name;
2988 bool _is_set;
2989 char* _vm_options_file_arg;
2990
3707 #ifdef ZERO
3708 // Clear flags not supported on zero.
3709 FLAG_SET_DEFAULT(ProfileInterpreter, false);
3710 #endif // ZERO
3711
3712 if (PrintAssembly && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
3713 warning("PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output");
3714 DebugNonSafepoints = true;
3715 }
3716
3717 if (FLAG_IS_CMDLINE(CompressedClassSpaceSize) && !UseCompressedClassPointers) {
3718 warning("Setting CompressedClassSpaceSize has no effect when compressed class pointers are not used");
3719 }
3720
3721 // Treat the odd case where local verification is enabled but remote
3722 // verification is not as if both were enabled.
3723 if (BytecodeVerificationLocal && !BytecodeVerificationRemote) {
3724 log_info(verification)("Turning on remote verification because local verification is on");
3725 FLAG_SET_DEFAULT(BytecodeVerificationRemote, true);
3726 }
3727
3728 #ifndef PRODUCT
3729 if (!LogVMOutput && FLAG_IS_DEFAULT(LogVMOutput)) {
3730 if (use_vm_log()) {
3731 LogVMOutput = true;
3732 }
3733 }
3734 #endif // PRODUCT
3735
3736 if (PrintCommandLineFlags) {
3737 JVMFlag::printSetFlags(tty);
3738 }
3739
3740 #if COMPILER2_OR_JVMCI
3741 if (!FLAG_IS_DEFAULT(EnableVectorSupport) && !EnableVectorSupport) {
3742 if (!FLAG_IS_DEFAULT(EnableVectorReboxing) && EnableVectorReboxing) {
3743 warning("Disabling EnableVectorReboxing since EnableVectorSupport is turned off.");
3744 }
3745 FLAG_SET_DEFAULT(EnableVectorReboxing, false);
3746
|
1 /*
2 * Copyright (c) 1997, 2024, 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 *
57 #include "runtime/os.hpp"
58 #include "runtime/safepoint.hpp"
59 #include "runtime/safepointMechanism.hpp"
60 #include "runtime/synchronizer.hpp"
61 #include "runtime/vm_version.hpp"
62 #include "services/management.hpp"
63 #include "utilities/align.hpp"
64 #include "utilities/checkedCast.hpp"
65 #include "utilities/debug.hpp"
66 #include "utilities/defaultStream.hpp"
67 #include "utilities/macros.hpp"
68 #include "utilities/parseInteger.hpp"
69 #include "utilities/powerOfTwo.hpp"
70 #include "utilities/stringUtils.hpp"
71 #include "utilities/systemMemoryBarrier.hpp"
72 #if INCLUDE_JFR
73 #include "jfr/jfr.hpp"
74 #endif
75
76 #include <limits>
77 #include <string.h>
78
79 static const char _default_java_launcher[] = "generic";
80
81 #define DEFAULT_JAVA_LAUNCHER _default_java_launcher
82
83 char* Arguments::_jvm_flags_file = nullptr;
84 char** Arguments::_jvm_flags_array = nullptr;
85 int Arguments::_num_jvm_flags = 0;
86 char** Arguments::_jvm_args_array = nullptr;
87 int Arguments::_num_jvm_args = 0;
88 char* Arguments::_java_command = nullptr;
89 SystemProperty* Arguments::_system_properties = nullptr;
90 size_t Arguments::_conservative_max_heap_alignment = 0;
91 Arguments::Mode Arguments::_mode = _mixed;
92 const char* Arguments::_java_vendor_url_bug = nullptr;
93 const char* Arguments::_sun_java_launcher = DEFAULT_JAVA_LAUNCHER;
94 bool Arguments::_sun_java_launcher_is_altjvm = false;
95
96 // These parameters are reset in method parse_vm_init_args()
97 bool Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods;
1782 }
1783 _sun_java_launcher = os::strdup_check_oom(launcher);
1784 }
1785
1786 bool Arguments::created_by_java_launcher() {
1787 assert(_sun_java_launcher != nullptr, "property must have value");
1788 return strcmp(DEFAULT_JAVA_LAUNCHER, _sun_java_launcher) != 0;
1789 }
1790
1791 bool Arguments::sun_java_launcher_is_altjvm() {
1792 return _sun_java_launcher_is_altjvm;
1793 }
1794
1795 //===========================================================================================================
1796 // Parsing of main arguments
1797
1798 unsigned int addreads_count = 0;
1799 unsigned int addexports_count = 0;
1800 unsigned int addopens_count = 0;
1801 unsigned int addmods_count = 0;
1802 unsigned int enable_native_access_count = 0;
1803
1804 // Check the consistency of vm_init_args
1805 bool Arguments::check_vm_args_consistency() {
1806 // Method for adding checks for flag consistency.
1807 // The intent is to warn the user of all possible conflicts,
1808 // before returning an error.
1809 // Note: Needs platform-dependent factoring.
1810 bool status = true;
1811
1812 if (TLABRefillWasteFraction == 0) {
1813 jio_fprintf(defaultStream::error_stream(),
1814 "TLABRefillWasteFraction should be a denominator, "
1815 "not " SIZE_FORMAT "\n",
1816 TLABRefillWasteFraction);
1817 status = false;
1818 }
1819
1820 status = CompilerConfig::check_args_consistency(status);
1821 #if INCLUDE_JVMCI
1828 }
1829 }
1830 }
1831 #endif
1832
1833 #if INCLUDE_JFR
1834 if (status && (FlightRecorderOptions || StartFlightRecording)) {
1835 if (!create_numbered_module_property("jdk.module.addmods", "jdk.jfr", addmods_count++)) {
1836 return false;
1837 }
1838 }
1839 #endif
1840
1841 #ifndef SUPPORT_RESERVED_STACK_AREA
1842 if (StackReservedPages != 0) {
1843 FLAG_SET_CMDLINE(StackReservedPages, 0);
1844 warning("Reserved Stack Area not supported on this platform");
1845 }
1846 #endif
1847
1848 if (AMD64_ONLY(false &&) AARCH64_ONLY(false &&) !FLAG_IS_DEFAULT(InlineTypePassFieldsAsArgs)) {
1849 FLAG_SET_CMDLINE(InlineTypePassFieldsAsArgs, false);
1850 warning("InlineTypePassFieldsAsArgs is not supported on this platform");
1851 }
1852
1853 if (AMD64_ONLY(false &&) AARCH64_ONLY(false &&) !FLAG_IS_DEFAULT(InlineTypeReturnedAsFields)) {
1854 FLAG_SET_CMDLINE(InlineTypeReturnedAsFields, false);
1855 warning("InlineTypeReturnedAsFields is not supported on this platform");
1856 }
1857
1858 // Valhalla missing LM_LIGHTWEIGHT support just now
1859 if (EnableValhalla && LockingMode != LM_LEGACY) {
1860 FLAG_SET_CMDLINE(LockingMode, LM_LEGACY);
1861 }
1862 #if !defined(X86) && !defined(AARCH64) && !defined(RISCV64) && !defined(ARM) && !defined(PPC64) && !defined(S390)
1863 if (LockingMode == LM_LIGHTWEIGHT) {
1864 FLAG_SET_CMDLINE(LockingMode, LM_LEGACY);
1865 warning("New lightweight locking not supported on this platform");
1866 }
1867 #endif
1868
1869 #if !defined(X86) && !defined(AARCH64) && !defined(PPC64) && !defined(RISCV64) && !defined(S390)
1870 if (LockingMode == LM_MONITOR) {
1871 jio_fprintf(defaultStream::error_stream(),
1872 "LockingMode == 0 (LM_MONITOR) is not fully implemented on this architecture\n");
1873 return false;
1874 }
1875 #endif
1876 #if defined(X86) && !defined(ZERO)
1877 if (LockingMode == LM_MONITOR && UseRTMForStackLocks) {
1878 jio_fprintf(defaultStream::error_stream(),
1879 "LockingMode == 0 (LM_MONITOR) and -XX:+UseRTMForStackLocks are mutually exclusive\n");
1880
1881 return false;
1968 }
1969
1970 jio_fprintf(defaultStream::error_stream(), "Property count limit exceeded: %s, limit=%d\n", prop_base_name, props_count_limit);
1971 return false;
1972 }
1973
1974 Arguments::ArgsRange Arguments::parse_memory_size(const char* s,
1975 julong* long_arg,
1976 julong min_size,
1977 julong max_size) {
1978 if (!parse_integer(s, long_arg)) return arg_unreadable;
1979 return check_memory_size(*long_arg, min_size, max_size);
1980 }
1981
1982 // Parse JavaVMInitArgs structure
1983
1984 jint Arguments::parse_vm_init_args(const JavaVMInitArgs *vm_options_args,
1985 const JavaVMInitArgs *java_tool_options_args,
1986 const JavaVMInitArgs *java_options_args,
1987 const JavaVMInitArgs *cmd_line_args) {
1988 // Save default settings for some mode flags
1989 Arguments::_AlwaysCompileLoopMethods = AlwaysCompileLoopMethods;
1990 Arguments::_UseOnStackReplacement = UseOnStackReplacement;
1991 Arguments::_ClipInlining = ClipInlining;
1992 Arguments::_BackgroundCompilation = BackgroundCompilation;
1993
1994 // Remember the default value of SharedBaseAddress.
1995 Arguments::_default_SharedBaseAddress = SharedBaseAddress;
1996
1997 // Setup flags for mixed which is the default
1998 set_mode_flags(_mixed);
1999
2000 // Parse args structure generated from java.base vm options resource
2001 jint result = parse_each_vm_init_arg(vm_options_args, JVMFlagOrigin::JIMAGE_RESOURCE);
2002 if (result != JNI_OK) {
2003 return result;
2004 }
2005
2006 // Parse args structure generated from JAVA_TOOL_OPTIONS environment
2007 // variable (if present).
2008 result = parse_each_vm_init_arg(java_tool_options_args, JVMFlagOrigin::ENVIRON_VAR);
2009 if (result != JNI_OK) {
2010 return result;
2011 }
2012
2013 // Parse args structure generated from the command line flags.
2014 result = parse_each_vm_init_arg(cmd_line_args, JVMFlagOrigin::COMMAND_LINE);
2015 if (result != JNI_OK) {
2016 return result;
2017 }
2018
2019 // Parse args structure generated from the _JAVA_OPTIONS environment
2020 // variable (if present) (mimics classic VM)
2021 result = parse_each_vm_init_arg(java_options_args, JVMFlagOrigin::ENVIRON_VAR);
2022 if (result != JNI_OK) {
2023 return result;
2024 }
2025
2026 // Disable CDS for exploded image
2027 if (!has_jimage()) {
2028 no_shared_spaces("CDS disabled on exploded JDK");
2029 }
2030
2031 // We need to ensure processor and memory resources have been properly
2032 // configured - which may rely on arguments we just processed - before
2033 // doing the final argument processing. Any argument processing that
2034 // needs to know about processor and memory resources must occur after
2035 // this point.
2036
2037 os::init_container_support();
2038
2039 SystemMemoryBarrier::initialize();
2040
2041 // Do final processing now that all arguments have been parsed
2042 result = finalize_vm_init_args();
2043 if (result != JNI_OK) {
2044 return result;
2045 }
2046
2047 return JNI_OK;
2048 }
2049
2050 // Checks if name in command-line argument -agent{lib,path}:name[=options]
2051 // represents a valid JDWP agent. is_path==true denotes that we
2052 // are dealing with -agentpath (case where name is a path), otherwise with
2053 // -agentlib
2054 bool valid_jdwp_agent(char *name, bool is_path) {
2055 char *_name;
2056 const char *_jdwp = "jdwp";
2057 size_t _len_jdwp, _len_prefix;
2058
2059 if (is_path) {
2060 if ((_name = strrchr(name, (int) *os::file_separator())) == nullptr) {
2061 return false;
2062 }
2075 _name += _len_jdwp;
2076 }
2077 else {
2078 return false;
2079 }
2080
2081 if (strcmp(_name, JNI_LIB_SUFFIX) != 0) {
2082 return false;
2083 }
2084
2085 return true;
2086 }
2087
2088 if (strcmp(name, _jdwp) == 0) {
2089 return true;
2090 }
2091
2092 return false;
2093 }
2094
2095 int Arguments::process_patch_mod_option(const char* patch_mod_tail) {
2096 // --patch-module=<module>=<file>(<pathsep><file>)*
2097 assert(patch_mod_tail != nullptr, "Unexpected null patch-module value");
2098 // Find the equal sign between the module name and the path specification
2099 const char* module_equal = strchr(patch_mod_tail, '=');
2100 if (module_equal == nullptr) {
2101 jio_fprintf(defaultStream::output_stream(), "Missing '=' in --patch-module specification\n");
2102 return JNI_ERR;
2103 } else {
2104 // Pick out the module name
2105 size_t module_len = module_equal - patch_mod_tail;
2106 char* module_name = NEW_C_HEAP_ARRAY_RETURN_NULL(char, module_len+1, mtArguments);
2107 if (module_name != nullptr) {
2108 memcpy(module_name, patch_mod_tail, module_len);
2109 *(module_name + module_len) = '\0';
2110 // The path piece begins one past the module_equal sign
2111 add_patch_mod_prefix(module_name, module_equal + 1, false /* no append */, false /* no cds */);
2112 FREE_C_HEAP_ARRAY(char, module_name);
2113 } else {
2114 return JNI_ENOMEM;
2115 }
2116 }
2117 return JNI_OK;
2118 }
2119
2120 // VALUECLASS_STR must match string used in the build
2121 #define VALUECLASS_STR "valueclasses"
2122 #define VALUECLASS_JAR "-" VALUECLASS_STR ".jar"
2123
2124 // Finalize --patch-module args and --enable-preview related to value class module patches.
2125 // Create all numbered properties passing module patches.
2126 int Arguments::finalize_patch_module() {
2127 // If --enable-preview and EnableValhalla is true, each module may have value classes that
2128 // are to be patched into the module.
2129 // For each <module>-valueclasses.jar in <JAVA_HOME>/lib/valueclasses/
2130 // appends the equivalent of --patch-module <module>=<JAVA_HOME>/lib/valueclasses/<module>-valueclasses.jar
2131 if (enable_preview() && EnableValhalla) {
2132 char * valueclasses_dir = AllocateHeap(JVM_MAXPATHLEN, mtArguments);
2133 const char * fileSep = os::file_separator();
2134
2135 jio_snprintf(valueclasses_dir, JVM_MAXPATHLEN, "%s%slib%s" VALUECLASS_STR "%s",
2136 Arguments::get_java_home(), fileSep, fileSep, fileSep);
2137 DIR* dir = os::opendir(valueclasses_dir);
2138 if (dir != nullptr) {
2139 char * module_name = AllocateHeap(JVM_MAXPATHLEN, mtArguments);
2140 char * path = AllocateHeap(JVM_MAXPATHLEN, mtArguments);
2141
2142 for (dirent * entry = os::readdir(dir); entry != nullptr; entry = os::readdir(dir)) {
2143 // Test if file ends-with "-valueclasses.jar"
2144 int len = (int)strlen(entry->d_name) - (sizeof(VALUECLASS_JAR) - 1);
2145 if (len <= 0 || strcmp(&entry->d_name[len], VALUECLASS_JAR) != 0) {
2146 continue; // too short or not the expected suffix
2147 }
2148
2149 strcpy(module_name, entry->d_name);
2150 module_name[len] = '\0'; // truncate to just module-name
2151
2152 jio_snprintf(path, JVM_MAXPATHLEN, "%s%s", valueclasses_dir, &entry->d_name);
2153 add_patch_mod_prefix(module_name, path, true /* append */, true /* cds OK*/);
2154 log_info(class)("--enable-preview appending value classes for module %s: %s", module_name, entry->d_name);
2155 }
2156 FreeHeap(module_name);
2157 FreeHeap(path);
2158 os::closedir(dir);
2159 }
2160 FreeHeap(valueclasses_dir);
2161 }
2162
2163 // Create numbered properties for each module that has been patched either
2164 // by --patch-module or --enable-preview
2165 // Format is "jdk.module.patch.<n>=<module_name>=<path>"
2166 if (_patch_mod_prefix != nullptr) {
2167 char * prop_value = AllocateHeap(JVM_MAXPATHLEN + JVM_MAXPATHLEN + 1, mtArguments);
2168 unsigned int patch_mod_count = 0;
2169
2170 for (GrowableArrayIterator<ModulePatchPath *> it = _patch_mod_prefix->begin();
2171 it != _patch_mod_prefix->end(); ++it) {
2172 jio_snprintf(prop_value, JVM_MAXPATHLEN + JVM_MAXPATHLEN + 1, "%s=%s",
2173 (*it)->module_name(), (*it)->path_string());
2174 if (!create_numbered_module_property("jdk.module.patch", prop_value, patch_mod_count++)) {
2175 FreeHeap(prop_value);
2176 return JNI_ENOMEM;
2177 }
2178 }
2179 FreeHeap(prop_value);
2180 }
2181 return JNI_OK;
2182 }
2183
2184 // Parse -Xss memory string parameter and convert to ThreadStackSize in K.
2185 jint Arguments::parse_xss(const JavaVMOption* option, const char* tail, intx* out_ThreadStackSize) {
2186 // The min and max sizes match the values in globals.hpp, but scaled
2187 // with K. The values have been chosen so that alignment with page
2188 // size doesn't change the max value, which makes the conversions
2189 // back and forth between Xss value and ThreadStackSize value easier.
2190 // The values have also been chosen to fit inside a 32-bit signed type.
2191 const julong min_ThreadStackSize = 0;
2192 const julong max_ThreadStackSize = 1 * M;
2193
2194 // Make sure the above values match the range set in globals.hpp
2195 const JVMTypedFlagLimit<intx>* limit = JVMFlagLimit::get_range_at(FLAG_MEMBER_ENUM(ThreadStackSize))->cast<intx>();
2196 assert(min_ThreadStackSize == static_cast<julong>(limit->min()), "must be");
2197 assert(max_ThreadStackSize == static_cast<julong>(limit->max()), "must be");
2198
2199 const julong min_size = min_ThreadStackSize * K;
2200 const julong max_size = max_ThreadStackSize * K;
2201
2202 assert(is_aligned(max_size, os::vm_page_size()), "Implementation assumption");
2203
2218 assert(size <= size_aligned,
2219 "Overflow: " JULONG_FORMAT " " JULONG_FORMAT,
2220 size, size_aligned);
2221
2222 const julong size_in_K = size_aligned / K;
2223 assert(size_in_K < (julong)max_intx,
2224 "size_in_K doesn't fit in the type of ThreadStackSize: " JULONG_FORMAT,
2225 size_in_K);
2226
2227 // Check that code expanding ThreadStackSize to a page aligned number of bytes won't overflow.
2228 const julong max_expanded = align_up(size_in_K * K, os::vm_page_size());
2229 assert(max_expanded < max_uintx && max_expanded >= size_in_K,
2230 "Expansion overflowed: " JULONG_FORMAT " " JULONG_FORMAT,
2231 max_expanded, size_in_K);
2232
2233 *out_ThreadStackSize = (intx)size_in_K;
2234
2235 return JNI_OK;
2236 }
2237
2238 jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, JVMFlagOrigin origin) {
2239 // For match_option to return remaining or value part of option string
2240 const char* tail;
2241
2242 // iterate over arguments
2243 for (int index = 0; index < args->nOptions; index++) {
2244 bool is_absolute_path = false; // for -agentpath vs -agentlib
2245
2246 const JavaVMOption* option = args->options + index;
2247
2248 if (!match_option(option, "-Djava.class.path", &tail) &&
2249 !match_option(option, "-Dsun.java.command", &tail) &&
2250 !match_option(option, "-Dsun.java.launcher", &tail)) {
2251
2252 // add all jvm options to the jvm_args string. This string
2253 // is used later to set the java.vm.args PerfData string constant.
2254 // the -Djava.class.path and the -Dsun.java.command options are
2255 // omitted from jvm_args string as each have their own PerfData
2256 // string constant object.
2257 build_jvm_args(option->optionString);
2258 }
2345 return JNI_ENOMEM;
2346 }
2347 } else if (match_option(option, "--enable-native-access=", &tail)) {
2348 if (!create_numbered_module_property("jdk.module.enable.native.access", tail, enable_native_access_count++)) {
2349 return JNI_ENOMEM;
2350 }
2351 } else if (match_option(option, "--limit-modules=", &tail)) {
2352 if (!create_module_property("jdk.module.limitmods", tail, InternalProperty)) {
2353 return JNI_ENOMEM;
2354 }
2355 } else if (match_option(option, "--module-path=", &tail)) {
2356 if (!create_module_property("jdk.module.path", tail, ExternalProperty)) {
2357 return JNI_ENOMEM;
2358 }
2359 } else if (match_option(option, "--upgrade-module-path=", &tail)) {
2360 if (!create_module_property("jdk.module.upgrade.path", tail, ExternalProperty)) {
2361 return JNI_ENOMEM;
2362 }
2363 } else if (match_option(option, "--patch-module=", &tail)) {
2364 // --patch-module=<module>=<file>(<pathsep><file>)*
2365 int res = process_patch_mod_option(tail);
2366 if (res != JNI_OK) {
2367 return res;
2368 }
2369 } else if (match_option(option, "--illegal-access=", &tail)) {
2370 char version[256];
2371 JDK_Version::jdk(17).to_string(version, sizeof(version));
2372 warning("Ignoring option %s; support was removed in %s", option->optionString, version);
2373 // -agentlib and -agentpath
2374 } else if (match_option(option, "-agentlib:", &tail) ||
2375 (is_absolute_path = match_option(option, "-agentpath:", &tail))) {
2376 if(tail != nullptr) {
2377 const char* pos = strchr(tail, '=');
2378 char* name;
2379 if (pos == nullptr) {
2380 name = os::strdup_check_oom(tail, mtArguments);
2381 } else {
2382 size_t len = pos - tail;
2383 name = NEW_C_HEAP_ARRAY(char, len + 1, mtArguments);
2384 memcpy(name, tail, len);
2385 name[len] = '\0';
2406 jio_fprintf(defaultStream::error_stream(),
2407 "Instrumentation agents are not supported in this VM\n");
2408 return JNI_ERR;
2409 #else
2410 if (tail != nullptr) {
2411 size_t length = strlen(tail) + 1;
2412 char *options = NEW_C_HEAP_ARRAY(char, length, mtArguments);
2413 jio_snprintf(options, length, "%s", tail);
2414 JvmtiAgentList::add("instrument", options, false);
2415 FREE_C_HEAP_ARRAY(char, options);
2416
2417 // java agents need module java.instrument
2418 if (!create_numbered_module_property("jdk.module.addmods", "java.instrument", addmods_count++)) {
2419 return JNI_ENOMEM;
2420 }
2421 }
2422 #endif // !INCLUDE_JVMTI
2423 // --enable_preview
2424 } else if (match_option(option, "--enable-preview")) {
2425 set_enable_preview();
2426 // --enable-preview enables Valhalla, EnableValhalla VM option will eventually be removed before integration
2427 if (FLAG_SET_CMDLINE(EnableValhalla, true) != JVMFlag::SUCCESS) {
2428 return JNI_EINVAL;
2429 }
2430 // -Xnoclassgc
2431 } else if (match_option(option, "-Xnoclassgc")) {
2432 if (FLAG_SET_CMDLINE(ClassUnloading, false) != JVMFlag::SUCCESS) {
2433 return JNI_EINVAL;
2434 }
2435 // -Xbatch
2436 } else if (match_option(option, "-Xbatch")) {
2437 if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
2438 return JNI_EINVAL;
2439 }
2440 // -Xmn for compatibility with other JVM vendors
2441 } else if (match_option(option, "-Xmn", &tail)) {
2442 julong long_initial_young_size = 0;
2443 ArgsRange errcode = parse_memory_size(tail, &long_initial_young_size, 1);
2444 if (errcode != arg_in_range) {
2445 jio_fprintf(defaultStream::error_stream(),
2446 "Invalid initial young generation size: %s\n", option->optionString);
2447 describe_range_error(errcode);
2448 return JNI_EINVAL;
2449 }
2892 // Unknown option
2893 } else if (is_bad_option(option, args->ignoreUnrecognized)) {
2894 return JNI_ERR;
2895 }
2896 }
2897
2898 // PrintSharedArchiveAndExit will turn on
2899 // -Xshare:on
2900 // -Xlog:class+path=info
2901 if (PrintSharedArchiveAndExit) {
2902 UseSharedSpaces = true;
2903 RequireSharedSpaces = true;
2904 LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, path));
2905 }
2906
2907 fix_appclasspath();
2908
2909 return JNI_OK;
2910 }
2911
2912 void Arguments::add_patch_mod_prefix(const char* module_name, const char* path, bool allow_append, bool allow_cds) {
2913 if (!allow_cds) {
2914 CDSConfig::set_module_patching_disables_cds();
2915 if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
2916 CDSConfig::set_java_base_module_patching_disables_cds();
2917 }
2918 }
2919
2920 // Create GrowableArray lazily, only if --patch-module has been specified
2921 if (_patch_mod_prefix == nullptr) {
2922 _patch_mod_prefix = new (mtArguments) GrowableArray<ModulePatchPath*>(10, mtArguments);
2923 }
2924
2925 // Scan patches for matching module
2926 int i = _patch_mod_prefix->find_if([&](ModulePatchPath* patch) {
2927 return (strcmp(module_name, patch->module_name()) == 0);
2928 });
2929 if (i == -1) {
2930 _patch_mod_prefix->push(new ModulePatchPath(module_name, path));
2931 } else {
2932 if (allow_append) {
2933 // append path to existing module entry
2934 _patch_mod_prefix->at(i)->append_path(path);
2935 } else {
2936 if (strcmp(module_name, JAVA_BASE_NAME) == 0) {
2937 vm_exit_during_initialization("Cannot specify " JAVA_BASE_NAME " more than once to --patch-module");
2938 } else {
2939 vm_exit_during_initialization("Cannot specify a module more than once to --patch-module", module_name);
2940 }
2941 }
2942 }
2943 }
2944
2945 // Remove all empty paths from the app classpath (if IgnoreEmptyClassPaths is enabled)
2946 //
2947 // This is necessary because some apps like to specify classpath like -cp foo.jar:${XYZ}:bar.jar
2948 // in their start-up scripts. If XYZ is empty, the classpath will look like "-cp foo.jar::bar.jar".
2949 // Java treats such empty paths as if the user specified "-cp foo.jar:.:bar.jar". I.e., an empty
2950 // path is treated as the current directory.
2951 //
2952 // This causes problems with CDS, which requires that all directories specified in the classpath
2953 // must be empty. In most cases, applications do NOT want to load classes from the current
2954 // directory anyway. Adding -XX:+IgnoreEmptyClassPaths will make these applications' start-up
2955 // scripts compatible with CDS.
2956 void Arguments::fix_appclasspath() {
2957 if (IgnoreEmptyClassPaths) {
2958 const char separator = *os::path_separator();
2959 const char* src = _java_class_path->value();
2960
2961 // skip over all the leading empty paths
2962 while (*src == separator) {
2965
2966 char* copy = os::strdup_check_oom(src, mtArguments);
2967
2968 // trim all trailing empty paths
2969 for (char* tail = copy + strlen(copy) - 1; tail >= copy && *tail == separator; tail--) {
2970 *tail = '\0';
2971 }
2972
2973 char from[3] = {separator, separator, '\0'};
2974 char to [2] = {separator, '\0'};
2975 while (StringUtils::replace_no_expand(copy, from, to) > 0) {
2976 // Keep replacing "::" -> ":" until we have no more "::" (non-windows)
2977 // Keep replacing ";;" -> ";" until we have no more ";;" (windows)
2978 }
2979
2980 _java_class_path->set_writeable_value(copy);
2981 FreeHeap(copy); // a copy was made by set_value, so don't need this anymore
2982 }
2983 }
2984
2985 jint Arguments::finalize_vm_init_args() {
2986 // check if the default lib/endorsed directory exists; if so, error
2987 char path[JVM_MAXPATHLEN];
2988 const char* fileSep = os::file_separator();
2989 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sendorsed", Arguments::get_java_home(), fileSep, fileSep);
2990
2991 DIR* dir = os::opendir(path);
2992 if (dir != nullptr) {
2993 jio_fprintf(defaultStream::output_stream(),
2994 "<JAVA_HOME>/lib/endorsed is not supported. Endorsed standards and standalone APIs\n"
2995 "in modular form will be supported via the concept of upgradeable modules.\n");
2996 os::closedir(dir);
2997 return JNI_ERR;
2998 }
2999
3000 jio_snprintf(path, JVM_MAXPATHLEN, "%s%slib%sext", Arguments::get_java_home(), fileSep, fileSep);
3001 dir = os::opendir(path);
3002 if (dir != nullptr) {
3003 jio_fprintf(defaultStream::output_stream(),
3004 "<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; "
3005 "Use -classpath instead.\n.");
3039 if (FLAG_IS_DEFAULT(UseLargePages) &&
3040 MaxHeapSize < LargePageHeapSizeThreshold) {
3041 // No need for large granularity pages w/small heaps.
3042 // Note that large pages are enabled/disabled for both the
3043 // Java heap and the code cache.
3044 FLAG_SET_DEFAULT(UseLargePages, false);
3045 }
3046
3047 UNSUPPORTED_OPTION(ProfileInterpreter);
3048 #endif
3049
3050 // Parse the CompilationMode flag
3051 if (!CompilationModeFlag::initialize()) {
3052 return JNI_ERR;
3053 }
3054
3055 if (!check_vm_args_consistency()) {
3056 return JNI_ERR;
3057 }
3058
3059 // finalize --module-patch and related --enable-preview
3060 if (finalize_patch_module() != JNI_OK) {
3061 return JNI_ERR;
3062 }
3063
3064 if (!CDSConfig::check_vm_args_consistency(mode_flag_cmd_line)) {
3065 return JNI_ERR;
3066 }
3067
3068 #ifndef CAN_SHOW_REGISTERS_ON_ASSERT
3069 UNSUPPORTED_OPTION(ShowRegistersOnAssert);
3070 #endif // CAN_SHOW_REGISTERS_ON_ASSERT
3071
3072 return JNI_OK;
3073 }
3074
3075 // Helper class for controlling the lifetime of JavaVMInitArgs
3076 // objects. The contents of the JavaVMInitArgs are guaranteed to be
3077 // deleted on the destruction of the ScopedVMInitArgs object.
3078 class ScopedVMInitArgs : public StackObj {
3079 private:
3080 JavaVMInitArgs _args;
3081 char* _container_name;
3082 bool _is_set;
3083 char* _vm_options_file_arg;
3084
3801 #ifdef ZERO
3802 // Clear flags not supported on zero.
3803 FLAG_SET_DEFAULT(ProfileInterpreter, false);
3804 #endif // ZERO
3805
3806 if (PrintAssembly && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
3807 warning("PrintAssembly is enabled; turning on DebugNonSafepoints to gain additional output");
3808 DebugNonSafepoints = true;
3809 }
3810
3811 if (FLAG_IS_CMDLINE(CompressedClassSpaceSize) && !UseCompressedClassPointers) {
3812 warning("Setting CompressedClassSpaceSize has no effect when compressed class pointers are not used");
3813 }
3814
3815 // Treat the odd case where local verification is enabled but remote
3816 // verification is not as if both were enabled.
3817 if (BytecodeVerificationLocal && !BytecodeVerificationRemote) {
3818 log_info(verification)("Turning on remote verification because local verification is on");
3819 FLAG_SET_DEFAULT(BytecodeVerificationRemote, true);
3820 }
3821 if (!EnableValhalla || (is_interpreter_only() && !CDSConfig::is_dumping_archive() && !UseSharedSpaces)) {
3822 // Disable calling convention optimizations if inline types are not supported.
3823 // Also these aren't useful in -Xint. However, don't disable them when dumping or using
3824 // the CDS archive, as the values must match between dumptime and runtime.
3825 InlineTypePassFieldsAsArgs = false;
3826 InlineTypeReturnedAsFields = false;
3827 }
3828
3829 #ifndef PRODUCT
3830 if (!LogVMOutput && FLAG_IS_DEFAULT(LogVMOutput)) {
3831 if (use_vm_log()) {
3832 LogVMOutput = true;
3833 }
3834 }
3835 #endif // PRODUCT
3836
3837 if (PrintCommandLineFlags) {
3838 JVMFlag::printSetFlags(tty);
3839 }
3840
3841 #if COMPILER2_OR_JVMCI
3842 if (!FLAG_IS_DEFAULT(EnableVectorSupport) && !EnableVectorSupport) {
3843 if (!FLAG_IS_DEFAULT(EnableVectorReboxing) && EnableVectorReboxing) {
3844 warning("Disabling EnableVectorReboxing since EnableVectorSupport is turned off.");
3845 }
3846 FLAG_SET_DEFAULT(EnableVectorReboxing, false);
3847
|