1 /*
  2  * Copyright (c) 2024, 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_AOTCLASSLINKER_HPP
 26 #define SHARE_CDS_AOTCLASSLINKER_HPP
 27 
 28 #include "interpreter/bytecodes.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "memory/allStatic.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "utilities/exceptions.hpp"
 33 #include "utilities/macros.hpp"
 34 #include "utilities/growableArray.hpp"
 35 #include "utilities/resourceHash.hpp"
 36 
 37 class AOTLinkedClassTable;
 38 class InstanceKlass;
 39 class SerializeClosure;
 40 template <typename T> class Array;
 41 
 42 
 43 // AOTClassLinker is used during the AOTCache Assembly Phase.
 44 // It links eligible classes before they are written into the AOTCache
 45 //
 46 // The classes linked by AOTClassLinker are recorded in an AOTLinkedClassTable,
 47 // which is also written into the AOTCache.
 48 //
 49 // AOTClassLinker is enabled by the -XX:+AOTClassLinking option. If this option
 50 // is disabled, an empty AOTLinkedClassTable will be included in the AOTCache.
 51 //
 52 // For each class C in the AOTLinkedClassTable, the following properties for C
 53 // are assigned by AOTClassLinker and cannot be changed thereafter.
 54 //     - The CodeSource for C
 55 //     - The bytecodes in C
 56 //     - The supertypes of C
 57 //     - The ClassLoader, Package and Module of C
 58 //     - The visibility of C
 59 //
 60 // During an Production Run, the JVM can use an AOTCache with an AOTLinkedClassTable
 61 // only if it's guaranteed to produce the same results for the above set of properties
 62 // for each class C in the AOTLinkedClassTable.
 63 //
 64 // For example,
 65 //     - C may be loaded from a different CodeSource when the CLASSPATH is changed.
 66 //     - Some JVMTI agent may allow the bytecodes of C to be modified.
 67 //     - C may be made invisible by module options such as --add-modules
 68 // In such situations, the JVM will refuse to load the AOTCache.
 69 //
 70 class AOTClassLinker :  AllStatic {
 71   using ClassesTable = ResourceHashtable<InstanceKlass*, bool, 15889, AnyObj::C_HEAP, mtClassShared>;
 72 
 73   // Classes loaded inside vmClasses::resolve_all()
 74   static ClassesTable* _vm_classes;
 75 
 76   // Classes that should be automatically loaded into system dictionary at VM start-up
 77   static ClassesTable* _candidates;
 78 
 79   // Sorted list such that super types come first.
 80   static GrowableArrayCHeap<InstanceKlass*, mtClassShared>* _sorted_candidates;
 81 
 82   DEBUG_ONLY(static bool is_initialized());
 83 
 84   static void add_vm_class(InstanceKlass* ik);
 85   static void add_candidate(InstanceKlass* ik);
 86 
 87   static Array<InstanceKlass*>* write_classes(oop class_loader, bool is_javabase);
 88   static int num_initiated_classes(oop loader1, oop loader2);
 89 
 90 public:
 91   static void initialize();
 92   static void add_candidates();
 93   static void write_to_archive();
 94   static void dispose();
 95 
 96   // Is this class resolved as part of vmClasses::resolve_all()?
 97   static bool is_vm_class(InstanceKlass* ik);
 98 
 99   // When CDS is enabled, is ik guatanteed to be linked at deployment time (and
100   // cannot be replaced by JVMTI, etc)?
101   // This is a necessary (not but sufficient) condition for keeping a direct pointer
102   // to ik in AOT-computed data (such as ConstantPool entries in archived classes,
103   // or in AOT-compiled code).
104   static bool is_candidate(InstanceKlass* ik);
105 
106   // Request that ik to be added to the candidates table. This will return succeed only if
107   // ik is allowed to be aot-linked.
108   static bool try_add_candidate(InstanceKlass* ik);
109 
110   static int num_app_initiated_classes();
111   static int num_platform_initiated_classes();
112 };
113 
114 #endif // SHARE_CDS_AOTCLASSLINKER_HPP