2020-10-10 02:06:52 +00:00
|
|
|
/*
|
2025-01-21 10:21:24 +00:00
|
|
|
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
|
2020-10-10 02:06:52 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2025-04-09 01:15:40 +00:00
|
|
|
#include "cds/aotClassFilter.hpp"
|
2021-05-16 02:26:46 +00:00
|
|
|
#include "cds/archiveBuilder.hpp"
|
2025-04-09 01:15:40 +00:00
|
|
|
#include "cds/cdsConfig.hpp"
|
|
|
|
#include "cds/lambdaFormInvokers.inline.hpp"
|
2021-04-23 04:13:02 +00:00
|
|
|
#include "cds/metaspaceShared.hpp"
|
2023-06-29 04:28:30 +00:00
|
|
|
#include "cds/regeneratedClasses.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
#include "classfile/classFileStream.hpp"
|
2024-11-15 22:28:54 +00:00
|
|
|
#include "classfile/classLoadInfo.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
#include "classfile/javaClasses.inline.hpp"
|
|
|
|
#include "classfile/klassFactory.hpp"
|
|
|
|
#include "classfile/symbolTable.hpp"
|
|
|
|
#include "classfile/systemDictionary.hpp"
|
|
|
|
#include "classfile/systemDictionaryShared.hpp"
|
2021-02-02 23:40:29 +00:00
|
|
|
#include "classfile/vmClasses.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
#include "classfile/vmSymbols.hpp"
|
|
|
|
#include "logging/log.hpp"
|
|
|
|
#include "memory/oopFactory.hpp"
|
|
|
|
#include "memory/resourceArea.hpp"
|
|
|
|
#include "oops/instanceKlass.hpp"
|
2021-12-08 15:59:37 +00:00
|
|
|
#include "oops/klass.inline.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
#include "oops/objArrayKlass.hpp"
|
|
|
|
#include "oops/objArrayOop.hpp"
|
|
|
|
#include "oops/oop.inline.hpp"
|
2021-12-08 15:59:37 +00:00
|
|
|
#include "oops/oopHandle.inline.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
#include "oops/typeArrayOop.inline.hpp"
|
|
|
|
#include "runtime/handles.inline.hpp"
|
|
|
|
#include "runtime/javaCalls.hpp"
|
2021-05-16 02:26:46 +00:00
|
|
|
#include "runtime/mutexLocker.hpp"
|
2020-10-10 02:06:52 +00:00
|
|
|
|
2021-05-16 02:26:46 +00:00
|
|
|
GrowableArrayCHeap<char*, mtClassShared>* LambdaFormInvokers::_lambdaform_lines = nullptr;
|
2024-11-06 18:46:06 +00:00
|
|
|
Array<u4>* LambdaFormInvokers::_static_archive_invokers = nullptr;
|
2021-05-16 02:26:46 +00:00
|
|
|
|
|
|
|
#define NUM_FILTER 4
|
|
|
|
static const char* filter[NUM_FILTER] = {"java.lang.invoke.Invokers$Holder",
|
|
|
|
"java.lang.invoke.DirectMethodHandle$Holder",
|
|
|
|
"java.lang.invoke.DelegatingMethodHandle$Holder",
|
|
|
|
"java.lang.invoke.LambdaForm$Holder"};
|
|
|
|
|
|
|
|
static bool should_be_archived(char* line) {
|
|
|
|
for (int k = 0; k < NUM_FILTER; k++) {
|
|
|
|
if (strstr(line, filter[k]) != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#undef NUM_FILTER
|
2020-10-10 02:06:52 +00:00
|
|
|
|
|
|
|
void LambdaFormInvokers::append(char* line) {
|
2021-05-16 02:26:46 +00:00
|
|
|
MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
|
2023-01-20 09:57:20 +00:00
|
|
|
if (_lambdaform_lines == nullptr) {
|
2021-05-16 02:26:46 +00:00
|
|
|
_lambdaform_lines = new GrowableArrayCHeap<char*, mtClassShared>(150);
|
2020-10-10 02:06:52 +00:00
|
|
|
}
|
|
|
|
_lambdaform_lines->append(line);
|
|
|
|
}
|
|
|
|
|
2021-12-08 15:59:37 +00:00
|
|
|
|
2021-05-16 02:26:46 +00:00
|
|
|
// convenient output
|
|
|
|
class PrintLambdaFormMessage {
|
|
|
|
public:
|
|
|
|
PrintLambdaFormMessage() {
|
2025-05-20 03:44:25 +00:00
|
|
|
log_info(aot)("Regenerate MethodHandle Holder classes...");
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
~PrintLambdaFormMessage() {
|
2025-05-20 03:44:25 +00:00
|
|
|
log_info(aot)("Regenerate MethodHandle Holder classes...done");
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-04-09 01:15:40 +00:00
|
|
|
class LambdaFormInvokersClassFilterMark : public AOTClassFilter::FilterMark {
|
|
|
|
public:
|
|
|
|
bool is_aot_tooling_class(InstanceKlass* ik) {
|
|
|
|
if (ik->name()->index_of_at(0, "$Species_", 9) > 0) {
|
|
|
|
// Classes like java.lang.invoke.BoundMethodHandle$Species_L should be included in AOT cache
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
|
|
|
|
// Regenerated holder classes should be included in AOT cache.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Treat all other classes loaded during LambdaFormInvokers::regenerate_holder_classes() as
|
|
|
|
// "AOT tooling classes".
|
|
|
|
return true;
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
2025-04-09 01:15:40 +00:00
|
|
|
};
|
2021-05-16 02:26:46 +00:00
|
|
|
|
2025-04-09 01:15:40 +00:00
|
|
|
void LambdaFormInvokers::regenerate_holder_classes(TRAPS) {
|
|
|
|
if (!CDSConfig::is_dumping_regenerated_lambdaform_invokers()) {
|
2024-11-15 22:28:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-04-09 01:15:40 +00:00
|
|
|
PrintLambdaFormMessage plm;
|
|
|
|
if (_lambdaform_lines == nullptr || _lambdaform_lines->length() == 0) {
|
2025-05-20 03:44:25 +00:00
|
|
|
log_info(aot)("Nothing to regenerate for holder classes");
|
2024-11-15 22:28:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-10 02:06:52 +00:00
|
|
|
ResourceMark rm(THREAD);
|
|
|
|
|
2025-04-09 01:15:40 +00:00
|
|
|
// Filter out AOT tooling classes like java.lang.invoke.GenerateJLIClassesHelper, etc.
|
|
|
|
LambdaFormInvokersClassFilterMark filter_mark;
|
|
|
|
|
2020-10-10 02:06:52 +00:00
|
|
|
Symbol* cds_name = vmSymbols::jdk_internal_misc_CDS();
|
|
|
|
Klass* cds_klass = SystemDictionary::resolve_or_null(cds_name, THREAD);
|
2023-01-20 09:57:20 +00:00
|
|
|
guarantee(cds_klass != nullptr, "jdk/internal/misc/CDS must exist!");
|
2020-10-10 02:06:52 +00:00
|
|
|
|
2021-05-16 02:26:46 +00:00
|
|
|
HandleMark hm(THREAD);
|
2020-10-10 02:06:52 +00:00
|
|
|
int len = _lambdaform_lines->length();
|
2021-05-16 02:26:46 +00:00
|
|
|
objArrayHandle list_lines;
|
|
|
|
{
|
|
|
|
MutexLocker ml(Thread::current(), LambdaFormInvokers_lock);
|
|
|
|
list_lines = oopFactory::new_objArray_handle(vmClasses::String_klass(), len, CHECK);
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
Handle h_line = java_lang_String::create_from_str(_lambdaform_lines->at(i), CHECK);
|
|
|
|
list_lines->obj_at_put(i, h_line());
|
|
|
|
}
|
|
|
|
} // Before calling into java, release vm lock.
|
2020-10-10 02:06:52 +00:00
|
|
|
//
|
|
|
|
// Object[] CDS.generateLambdaFormHolderClasses(String[] lines)
|
|
|
|
// the returned Object[] layout:
|
|
|
|
// name, byte[], name, byte[] ....
|
|
|
|
Symbol* method = vmSymbols::generateLambdaFormHolderClasses();
|
|
|
|
Symbol* signrs = vmSymbols::generateLambdaFormHolderClasses_signature();
|
|
|
|
|
|
|
|
JavaValue result(T_OBJECT);
|
|
|
|
JavaCalls::call_static(&result, cds_klass, method, signrs, list_lines, THREAD);
|
|
|
|
|
|
|
|
if (HAS_PENDING_EXCEPTION) {
|
2021-05-16 02:26:46 +00:00
|
|
|
if (!PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
|
2025-05-20 05:46:34 +00:00
|
|
|
log_error(aot)("%s: %s", PENDING_EXCEPTION->klass()->external_name(),
|
2021-05-16 02:26:46 +00:00
|
|
|
java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
|
2023-11-20 17:09:55 +00:00
|
|
|
if (CDSConfig::is_dumping_static_archive()) {
|
2025-05-20 03:44:25 +00:00
|
|
|
log_error(aot)("Failed to generate LambdaForm holder classes. Is your classlist out of date?");
|
2021-05-16 02:26:46 +00:00
|
|
|
} else {
|
2025-05-20 03:44:25 +00:00
|
|
|
log_error(aot)("Failed to generate LambdaForm holder classes. Was the base archive generated with an outdated classlist?");
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
CLEAR_PENDING_EXCEPTION;
|
|
|
|
}
|
2020-10-10 02:06:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-16 08:29:11 +00:00
|
|
|
objArrayHandle h_array(THREAD, (objArrayOop)result.get_oop());
|
2020-10-10 02:06:52 +00:00
|
|
|
int sz = h_array->length();
|
|
|
|
assert(sz % 2 == 0 && sz >= 2, "Must be even size of length");
|
|
|
|
for (int i = 0; i < sz; i+= 2) {
|
|
|
|
Handle h_name(THREAD, h_array->obj_at(i));
|
|
|
|
typeArrayHandle h_bytes(THREAD, (typeArrayOop)h_array->obj_at(i+1));
|
2023-01-20 09:57:20 +00:00
|
|
|
assert(h_name != nullptr, "Class name is null");
|
|
|
|
assert(h_bytes != nullptr, "Class bytes is null");
|
2020-10-10 02:06:52 +00:00
|
|
|
|
|
|
|
char *class_name = java_lang_String::as_utf8_string(h_name());
|
2022-07-21 21:32:30 +00:00
|
|
|
if (strstr(class_name, "java/lang/invoke/BoundMethodHandle$Species_") != nullptr) {
|
|
|
|
// The species classes are already loaded into the system dictionary
|
|
|
|
// during the execution of CDS.generateLambdaFormHolderClasses(). No
|
|
|
|
// need to regenerate.
|
|
|
|
TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
|
|
|
|
Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
|
2023-01-20 09:57:20 +00:00
|
|
|
assert(klass != nullptr, "must already be loaded");
|
2022-07-21 21:32:30 +00:00
|
|
|
if (!klass->is_shared() && klass->shared_classpath_index() < 0) {
|
|
|
|
// Fake it, so that it will be included into the archive.
|
|
|
|
klass->set_shared_classpath_index(0);
|
|
|
|
// Set the "generated" bit, so it won't interfere with JVMTI.
|
|
|
|
// See SystemDictionaryShared::find_builtin_class().
|
|
|
|
klass->set_is_generated_shared_class();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int len = h_bytes->length();
|
|
|
|
// make a copy of class bytes so GC will not affect us.
|
|
|
|
char *buf = NEW_RESOURCE_ARRAY(char, len);
|
|
|
|
memcpy(buf, (char*)h_bytes->byte_at_addr(0), len);
|
2024-11-14 12:08:42 +00:00
|
|
|
ClassFileStream st((u1*)buf, len, nullptr);
|
2022-07-21 21:32:30 +00:00
|
|
|
regenerate_class(class_name, st, CHECK);
|
|
|
|
}
|
2022-02-04 19:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 21:32:30 +00:00
|
|
|
void LambdaFormInvokers::regenerate_class(char* class_name, ClassFileStream& st, TRAPS) {
|
|
|
|
TempNewSymbol class_name_sym = SymbolTable::new_symbol(class_name);
|
|
|
|
Klass* klass = SystemDictionary::resolve_or_null(class_name_sym, THREAD);
|
2023-01-20 09:57:20 +00:00
|
|
|
assert(klass != nullptr, "must exist");
|
2020-10-10 02:06:52 +00:00
|
|
|
assert(klass->is_instance_klass(), "Should be");
|
|
|
|
|
|
|
|
ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
|
|
|
|
Handle protection_domain;
|
|
|
|
ClassLoadInfo cl_info(protection_domain);
|
|
|
|
|
|
|
|
InstanceKlass* result = KlassFactory::create_from_stream(&st,
|
2022-07-21 21:32:30 +00:00
|
|
|
class_name_sym,
|
2020-10-10 02:06:52 +00:00
|
|
|
cld,
|
|
|
|
cl_info,
|
|
|
|
CHECK);
|
|
|
|
|
2021-12-08 15:59:37 +00:00
|
|
|
assert(result->java_mirror() != nullptr, "must be");
|
2023-06-29 04:28:30 +00:00
|
|
|
RegeneratedClasses::add_class(InstanceKlass::cast(klass), result);
|
2021-12-08 15:59:37 +00:00
|
|
|
|
2023-03-23 13:47:06 +00:00
|
|
|
result->add_to_hierarchy(THREAD);
|
2023-03-13 09:34:00 +00:00
|
|
|
|
2020-10-10 02:06:52 +00:00
|
|
|
// new class not linked yet.
|
2021-03-18 04:15:58 +00:00
|
|
|
MetaspaceShared::try_link_class(THREAD, result);
|
2020-10-10 02:06:52 +00:00
|
|
|
assert(!HAS_PENDING_EXCEPTION, "Invariant");
|
|
|
|
|
2022-07-21 21:32:30 +00:00
|
|
|
result->set_is_generated_shared_class();
|
2022-07-27 23:52:30 +00:00
|
|
|
if (!klass->is_shared()) {
|
|
|
|
SystemDictionaryShared::set_excluded(InstanceKlass::cast(klass)); // exclude the existing class from dump
|
|
|
|
}
|
2025-05-20 03:44:25 +00:00
|
|
|
log_info(aot, lambda)("Regenerated class %s, old: " INTPTR_FORMAT " new: " INTPTR_FORMAT,
|
2022-07-21 21:32:30 +00:00
|
|
|
class_name, p2i(klass), p2i(result));
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LambdaFormInvokers::dump_static_archive_invokers() {
|
|
|
|
if (_lambdaform_lines != nullptr && _lambdaform_lines->length() > 0) {
|
|
|
|
int count = 0;
|
|
|
|
int len = _lambdaform_lines->length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
char* str = _lambdaform_lines->at(i);
|
|
|
|
if (should_be_archived(str)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count > 0) {
|
2024-11-06 18:46:06 +00:00
|
|
|
_static_archive_invokers = ArchiveBuilder::new_ro_array<u4>(count);
|
2021-05-16 02:26:46 +00:00
|
|
|
int index = 0;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
char* str = _lambdaform_lines->at(i);
|
|
|
|
if (should_be_archived(str)) {
|
|
|
|
size_t str_len = strlen(str) + 1; // including terminating zero
|
|
|
|
Array<char>* line = ArchiveBuilder::new_ro_array<char>((int)str_len);
|
|
|
|
strncpy(line->adr_at(0), str, str_len);
|
|
|
|
|
2024-11-06 18:46:06 +00:00
|
|
|
_static_archive_invokers->at_put(index, ArchiveBuilder::current()->any_to_offset_u4(line));
|
2021-05-16 02:26:46 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(index == count, "Should match");
|
|
|
|
}
|
2025-05-20 03:44:25 +00:00
|
|
|
log_debug(aot)("Total LF lines stored into %s: %d", CDSConfig::type_of_archive_being_written(), count);
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LambdaFormInvokers::read_static_archive_invokers() {
|
|
|
|
if (_static_archive_invokers != nullptr) {
|
|
|
|
for (int i = 0; i < _static_archive_invokers->length(); i++) {
|
2024-11-06 18:46:06 +00:00
|
|
|
u4 offset = _static_archive_invokers->at(i);
|
2024-11-27 20:10:28 +00:00
|
|
|
Array<char>* line = ArchiveUtils::offset_to_archived_address<Array<char>*>(offset);
|
2021-05-16 02:26:46 +00:00
|
|
|
char* str = line->adr_at(0);
|
|
|
|
append(str);
|
|
|
|
}
|
2025-05-20 03:44:25 +00:00
|
|
|
log_debug(aot)("Total LF lines read from %s: %d", CDSConfig::type_of_archive_being_loaded(), _static_archive_invokers->length());
|
2021-05-16 02:26:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LambdaFormInvokers::serialize(SerializeClosure* soc) {
|
2023-05-15 16:33:37 +00:00
|
|
|
soc->do_ptr(&_static_archive_invokers);
|
2025-02-25 22:56:25 +00:00
|
|
|
if (soc->reading() && CDSConfig::is_dumping_final_static_archive()) {
|
2025-04-09 01:15:40 +00:00
|
|
|
if (!CDSConfig::is_dumping_aot_linked_classes()) {
|
|
|
|
// See CDSConfig::is_dumping_regenerated_lambdaform_invokers() -- a dynamic archive can
|
|
|
|
// regenerate lambda form invokers only if the base archive does not contain aot-linked classes.
|
|
|
|
// If so, we copy the contents of _static_archive_invokers (from the preimage) into
|
|
|
|
//_lambdaform_lines, which will be written as _static_archive_invokers into final static archive.
|
|
|
|
LambdaFormInvokers::read_static_archive_invokers();
|
|
|
|
}
|
2025-02-25 22:56:25 +00:00
|
|
|
_static_archive_invokers = nullptr;
|
|
|
|
}
|
2020-10-10 02:06:52 +00:00
|
|
|
}
|