1 /*
2 * Copyright (c) 2022, 2025, 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 #ifndef SHARE_CDS_AOTCONSTANTPOOLRESOLVER_HPP
26 #define SHARE_CDS_AOTCONSTANTPOOLRESOLVER_HPP
27
28 #include "interpreter/bytecodes.hpp"
29 #include "memory/allocation.hpp"
30 #include "memory/allStatic.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "runtime/handles.hpp"
33 #include "utilities/exceptions.hpp"
34 #include "utilities/hashTable.hpp"
35 #include "utilities/macros.hpp"
36
37 class ConstantPool;
38 class constantPoolHandle;
39 class InstanceKlass;
40 class Klass;
41
42 template <typename T> class GrowableArray;
43
44 // AOTConstantPoolResolver is used to perform ahead-of-time linking of ConstantPool entries
45 // for archived InstanceKlasses.
46 //
47 // At run time, Java classes are loaded dynamically and may be replaced with JVMTI.
48 // Therefore, we take care to prelink only the ConstantPool entries that are
49 // guatanteed to resolve to the same results at both dump time and run time.
50 //
51 // For example, a JVM_CONSTANT_Class reference to a supertype can be safely resolved
52 // at dump time, because at run time we will load a class from the CDS archive only
53 // if all of its supertypes are loaded from the CDS archive.
54 class AOTConstantPoolResolver : AllStatic {
55 static const int TABLE_SIZE = 15889; // prime number
56 using ClassesTable = HashTable<InstanceKlass*, bool, TABLE_SIZE, AnyObj::C_HEAP, mtClassShared> ;
57 static ClassesTable* _processed_classes;
58
59 #ifdef ASSERT
60 template <typename T> static bool is_in_archivebuilder_buffer(T p) {
61 return is_in_archivebuilder_buffer((address)(p));
62 }
63 static bool is_in_archivebuilder_buffer(address p);
64 #endif
65
66 static void resolve_string(constantPoolHandle cp, int cp_index, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
67 static bool is_class_resolution_deterministic(InstanceKlass* cp_holder, Klass* resolved_class);
68 static bool is_indy_resolution_deterministic(ConstantPool* cp, int cp_index);
69
70 // helper
71 static Klass* resolve_boot_class_or_fail(const char* class_name, TRAPS);
72
73 // java/lang/reflect/Proxy caching
74 static void init_dynamic_proxy_cache(TRAPS);
75
76 static Klass* find_loaded_class(Thread* current, oop class_loader, Symbol* name);
77 static Klass* find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index);
78
79 // fmi = FieldRef/MethodRef/InterfaceMethodRef
80 static void maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
81 GrowableArray<bool>* resolve_fmi_list, TRAPS);
82
83 static bool check_methodtype_signature(ConstantPool* cp, Symbol* sig, Klass** return_type_ret = nullptr);
84 static bool check_lambda_metafactory_signature(ConstantPool* cp, Symbol* sig);
85 static bool check_lambda_metafactory_methodtype_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i);
86 static bool check_lambda_metafactory_methodhandle_arg(ConstantPool* cp, int bsms_attribute_index, int arg_i);
87
88 static bool check_signature_for_reflection_data(InstanceKlass* ik, Symbol* sig, bool is_method);
89 static bool can_archive_reflection_data_declared_fields(InstanceKlass* ik);
90 static bool can_archive_reflection_data_declared_methods(InstanceKlass* ik);
91 public:
92 static void preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
93 static void preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
94 static void preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
95 static void preresolve_string_cp_entries(InstanceKlass* ik, TRAPS);
96
97 // java/lang/Class$ReflectionData caching
98 static bool can_archive_reflection_data(InstanceKlass* k);
99 static int class_reflection_data_flags(InstanceKlass* ik, TRAPS);
100 static void generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags);
101
102 // java/lang/reflect/Proxy caching
103 static void trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags);
104 static void define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS);
105
106 static bool is_resolution_deterministic(ConstantPool* cp, int cp_index);
107 };
108
109 #endif // SHARE_CDS_AOTCONSTANTPOOLRESOLVER_HPP