8303606: Memory leaks in Arguments::parse_each_vm_init_arg

Reviewed-by: dholmes, fparain
This commit is contained in:
Justin King 2023-03-13 16:23:34 +00:00
parent a95bc7acd0
commit 671a45219f
2 changed files with 17 additions and 7 deletions

View File

@ -70,7 +70,9 @@
#include <limits> #include <limits>
#define DEFAULT_JAVA_LAUNCHER "generic" static const char _default_java_launcher[] = "generic";
#define DEFAULT_JAVA_LAUNCHER _default_java_launcher
char* Arguments::_jvm_flags_file = nullptr; char* Arguments::_jvm_flags_file = nullptr;
char** Arguments::_jvm_flags_array = nullptr; char** Arguments::_jvm_flags_array = nullptr;
@ -323,15 +325,15 @@ bool needs_module_property_warning = false;
#define ENABLE_NATIVE_ACCESS "enable.native.access" #define ENABLE_NATIVE_ACCESS "enable.native.access"
#define ENABLE_NATIVE_ACCESS_LEN 20 #define ENABLE_NATIVE_ACCESS_LEN 20
void Arguments::add_init_library(const char* name, char* options) { void Arguments::add_init_library(const char* name, const char* options) {
_libraryList.add(new AgentLibrary(name, options, false, nullptr)); _libraryList.add(new AgentLibrary(name, options, false, nullptr));
} }
void Arguments::add_init_agent(const char* name, char* options, bool absolute_path) { void Arguments::add_init_agent(const char* name, const char* options, bool absolute_path) {
_agentList.add(new AgentLibrary(name, options, absolute_path, nullptr)); _agentList.add(new AgentLibrary(name, options, absolute_path, nullptr));
} }
void Arguments::add_instrument_agent(const char* name, char* options, bool absolute_path) { void Arguments::add_instrument_agent(const char* name, const char* options, bool absolute_path) {
_agentList.add(new AgentLibrary(name, options, absolute_path, nullptr, true)); _agentList.add(new AgentLibrary(name, options, absolute_path, nullptr, true));
} }
@ -1908,6 +1910,9 @@ void Arguments::process_java_compiler_argument(const char* arg) {
} }
void Arguments::process_java_launcher_argument(const char* launcher, void* extra_info) { void Arguments::process_java_launcher_argument(const char* launcher, void* extra_info) {
if (_sun_java_launcher != _default_java_launcher) {
os::free(const_cast<char*>(_sun_java_launcher));
}
_sun_java_launcher = os::strdup_check_oom(launcher); _sun_java_launcher = os::strdup_check_oom(launcher);
} }
@ -2368,6 +2373,8 @@ jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_m
} }
#endif // !INCLUDE_JVMTI #endif // !INCLUDE_JVMTI
add_init_library(name, options); add_init_library(name, options);
FREE_C_HEAP_ARRAY(char, name);
FREE_C_HEAP_ARRAY(char, options);
} }
} else if (match_option(option, "--add-reads=", &tail)) { } else if (match_option(option, "--add-reads=", &tail)) {
if (!create_numbered_module_property("jdk.module.addreads", tail, addreads_count++)) { if (!create_numbered_module_property("jdk.module.addreads", tail, addreads_count++)) {
@ -2438,6 +2445,8 @@ jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_m
} }
#endif // !INCLUDE_JVMTI #endif // !INCLUDE_JVMTI
add_init_agent(name, options, is_absolute_path); add_init_agent(name, options, is_absolute_path);
os::free(name);
os::free(options);
} }
// -javaagent // -javaagent
} else if (match_option(option, "-javaagent:", &tail)) { } else if (match_option(option, "-javaagent:", &tail)) {
@ -2451,6 +2460,7 @@ jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs* args, bool* patch_m
char *options = NEW_C_HEAP_ARRAY(char, length, mtArguments); char *options = NEW_C_HEAP_ARRAY(char, length, mtArguments);
jio_snprintf(options, length, "%s", tail); jio_snprintf(options, length, "%s", tail);
add_instrument_agent("instrument", options, false); add_instrument_agent("instrument", options, false);
FREE_C_HEAP_ARRAY(char, options);
// java agents need module java.instrument // java agents need module java.instrument
if (!create_numbered_module_property("jdk.module.addmods", "java.instrument", addmods_count++)) { if (!create_numbered_module_property("jdk.module.addmods", "java.instrument", addmods_count++)) {
return JNI_ENOMEM; return JNI_ENOMEM;

View File

@ -333,12 +333,12 @@ class Arguments : AllStatic {
// -Xrun arguments // -Xrun arguments
static AgentLibraryList _libraryList; static AgentLibraryList _libraryList;
static void add_init_library(const char* name, char* options); static void add_init_library(const char* name, const char* options);
// -agentlib and -agentpath arguments // -agentlib and -agentpath arguments
static AgentLibraryList _agentList; static AgentLibraryList _agentList;
static void add_init_agent(const char* name, char* options, bool absolute_path); static void add_init_agent(const char* name, const char* options, bool absolute_path);
static void add_instrument_agent(const char* name, char* options, bool absolute_path); static void add_instrument_agent(const char* name, const char* options, bool absolute_path);
// Late-binding agents not started via arguments // Late-binding agents not started via arguments
static void add_loaded_agent(AgentLibrary *agentLib); static void add_loaded_agent(AgentLibrary *agentLib);