From ca863fd5ab309c502ea36f2d778ae3de5674e24f Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Mon, 20 Jan 2025 19:13:59 +0000 Subject: [PATCH] 8347758: modules.cpp leaks string returned from get_numbered_property_as_sorted_string() Reviewed-by: dholmes, iklam, ccheung --- src/hotspot/share/classfile/modules.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hotspot/share/classfile/modules.cpp b/src/hotspot/share/classfile/modules.cpp index 2e538c578d9..0aadefc340c 100644 --- a/src/hotspot/share/classfile/modules.cpp +++ b/src/hotspot/share/classfile/modules.cpp @@ -610,12 +610,14 @@ void Modules::serialize(SerializeClosure* soc) { } void Modules::dump_native_access_flag() { + ResourceMark rm; const char* native_access_names = get_native_access_flags_as_sorted_string(); if (native_access_names != nullptr) { _archived_native_access_flags = ArchiveBuilder::current()->ro_strdup(native_access_names); } } +// Caller needs ResourceMark const char* Modules::get_native_access_flags_as_sorted_string() { return get_numbered_property_as_sorted_string("jdk.module.enable.native.access"); } @@ -623,6 +625,7 @@ const char* Modules::get_native_access_flags_as_sorted_string() { void Modules::serialize_native_access_flags(SerializeClosure* soc) { soc->do_ptr(&_archived_native_access_flags); if (soc->reading()) { + ResourceMark rm; check_archived_flag_consistency(_archived_native_access_flags, get_native_access_flags_as_sorted_string(), "jdk.module.enable.native.access"); // Don't hold onto the pointer, in case we might decide to unmap the archive. @@ -631,12 +634,14 @@ void Modules::serialize_native_access_flags(SerializeClosure* soc) { } void Modules::dump_addmods_names() { + ResourceMark rm; const char* addmods_names = get_addmods_names_as_sorted_string(); if (addmods_names != nullptr) { _archived_addmods_names = ArchiveBuilder::current()->ro_strdup(addmods_names); } } +// Caller needs ResourceMark const char* Modules::get_addmods_names_as_sorted_string() { return get_numbered_property_as_sorted_string("jdk.module.addmods"); } @@ -644,6 +649,7 @@ const char* Modules::get_addmods_names_as_sorted_string() { void Modules::serialize_addmods_names(SerializeClosure* soc) { soc->do_ptr(&_archived_addmods_names); if (soc->reading()) { + ResourceMark rm; check_archived_flag_consistency(_archived_addmods_names, get_addmods_names_as_sorted_string(), "jdk.module.addmods"); // Don't hold onto the pointer, in case we might decide to unmap the archive. @@ -651,8 +657,8 @@ void Modules::serialize_addmods_names(SerializeClosure* soc) { } } +// Caller needs ResourceMark const char* Modules::get_numbered_property_as_sorted_string(const char* property) { - ResourceMark rm; // theoretical string size limit for decimal int, but the following loop will end much sooner due to // OS command-line size limit. const int max_digits = 10; @@ -705,7 +711,7 @@ const char* Modules::get_numbered_property_as_sorted_string(const char* property } } - return (st.size() > 0) ? os::strdup(st.as_string()) : nullptr; // Example: "java.base,java.compiler" + return (st.size() > 0) ? st.as_string() : nullptr; // Example: "java.base,java.compiler" } void Modules::define_archived_modules(Handle h_platform_loader, Handle h_system_loader, TRAPS) {