1 /*
 2  * Copyright (c) 2022, 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_CLASSPRELINKER_HPP
26 #define SHARE_CDS_CLASSPRELINKER_HPP
27 
28 #include "oops/oopsHierarchy.hpp"
29 #include "memory/allStatic.hpp"
30 #include "memory/allocation.hpp"
31 #include "runtime/handles.hpp"
32 #include "utilities/exceptions.hpp"
33 #include "utilities/macros.hpp"
34 #include "utilities/resourceHash.hpp"
35 
36 class ConstantPool;
37 class constantPoolHandle;
38 class InstanceKlass;
39 class Klass;
40 
41 // ClassPrelinker is used to perform ahead-of-time linking of ConstantPool entries
42 // for archived InstanceKlasses.
43 //
44 // At run time, Java classes are loaded dynamically and may be replaced with JVMTI.
45 // Therefore, we take care to prelink only the ConstantPool entries that are
46 // guatanteed to resolve to the same results at both dump time and run time.
47 //
48 // For example, a JVM_CONSTANT_Class reference to a supertype can be safely resolved
49 // at dump time, because at run time we will load a class from the CDS archive only
50 // if all of its supertypes are loaded from the CDS archive.
51 class ClassPrelinker :  AllStatic {
52   using ClassesTable = ResourceHashtable<InstanceKlass*, bool, 15889, AnyObj::C_HEAP, mtClassShared> ;
53   static ClassesTable* _processed_classes;
54   static ClassesTable* _vm_classes;
55 
56   static void add_one_vm_class(InstanceKlass* ik);
57 
58 #ifdef ASSERT
59   static bool is_in_archivebuilder_buffer(address p);
60 #endif
61 
62   template <typename T>
63   static bool is_in_archivebuilder_buffer(T p) {
64     return is_in_archivebuilder_buffer((address)(p));
65   }
66   static void resolve_string(constantPoolHandle cp, int cp_index, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
67   static Klass* maybe_resolve_class(constantPoolHandle cp, int cp_index, TRAPS);
68   static bool can_archive_resolved_klass(InstanceKlass* cp_holder, Klass* resolved_klass);
69   static Klass* find_loaded_class(JavaThread* THREAD, oop class_loader, Symbol* name);
70 
71 public:
72   static void initialize();
73   static void dispose();
74 
75   // Is this class resolved as part of vmClasses::resolve_all()? If so, these
76   // classes are guatanteed to be loaded at runtime (and cannot be replaced by JVMTI)
77   // when CDS is enabled. Therefore, we can safely keep a direct reference to these
78   // classes.
79   static bool is_vm_class(InstanceKlass* ik);
80 
81   // Resolve all constant pool entries that are safe to be stored in the
82   // CDS archive.
83   static void dumptime_resolve_constants(InstanceKlass* ik, TRAPS);
84 
85   // Can we resolve the klass entry at cp_index in this constant pool, and store
86   // the result in the CDS archive? Returns true if cp_index is guaranteed to
87   // resolve to the same InstanceKlass* at both dump time and run time.
88   static bool can_archive_resolved_klass(ConstantPool* cp, int cp_index);
89 };
90 
91 #endif // SHARE_CDS_CLASSPRELINKER_HPP