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_FINALIMAGERECIPES_HPP
26 #define SHARE_CDS_FINALIMAGERECIPES_HPP
27 
28 #include "oops/oopsHierarchy.hpp"
29 #include "utilities/exceptions.hpp"
30 
31 class InstanceKlass;
32 class Klass;
33 
34 template <typename T> class GrowableArray;
35 template <typename T> class Array;
36 
37 // This class is used only by the "one step training workflow". It records the
38 // "recipes" for creating the final CDS image.
39 // - The recipes are recorded when CDSConfig::is_dumping_preimage_static_archive() is true;
40 // - The recipes are applied when CDSConfig::is_dumping_final_static_archive() is true;
41 class FinalImageRecipes {
42   // The classes who have resolved at least one indy CP entry during the training run.
43   // _indy_cp_indices[i] is a list of all resolved CP entries for _indy_klasses[i].
44   Array<InstanceKlass*>* _indy_klasses;
45   Array<Array<int>*>*    _indy_cp_indices;
46 
47   // The RefectionData for  _reflect_klasses[i] should be initialized with _reflect_flags[i]
48   Array<InstanceKlass*>* _reflect_klasses;
49   Array<int>*            _reflect_flags;
50 
51   static GrowableArray<InstanceKlass*>* _tmp_reflect_klasses;
52   static GrowableArray<int>* _tmp_reflect_flags;
53 
54   struct TmpDynamicProxyClassInfo {
55     int _loader_type;
56     int _access_flags;
57     const char* _proxy_name;
58     GrowableArray<Klass*>* _interfaces;
59   };
60 
61   struct DynamicProxyClassInfo {
62     int _loader_type;
63     int _access_flags;
64     const char* _proxy_name;
65     Array<Klass*>* _interfaces;
66   };
67 
68   Array<DynamicProxyClassInfo>* _dynamic_proxy_classes;
69 
70   static GrowableArray<TmpDynamicProxyClassInfo>* _tmp_dynamic_proxy_classes;
71 
72   FinalImageRecipes() : _indy_klasses(nullptr), _indy_cp_indices(nullptr),
73                         _reflect_klasses(nullptr), _reflect_flags(nullptr),
74                         _dynamic_proxy_classes(nullptr) {}
75 
76   void* operator new(size_t size) throw();
77 
78   // Called when dumping preimage
79   void record_recipes_impl();
80 
81   // Called when dumping final image
82   void apply_recipes_for_dynamic_proxies(TRAPS);
83   void apply_recipes_for_invokedynamic(TRAPS);
84   void apply_recipes_for_reflection_data(JavaThread* current);
85 
86 public:
87   static void serialize(SerializeClosure* soc, bool is_static_archive);
88 
89   // Called when dumping preimage
90   static void add_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags);
91   static void add_reflection_data_flags(InstanceKlass* ik, TRAPS);
92   static void record_recipes();
93 
94   // Called when dumping final image
95   static void apply_recipes(TRAPS);
96 };
97 
98 #endif // SHARE_CDS_FINALIMAGERECIPES_HPP