1 /*
  2  * Copyright (c) 2003, 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_AOTCLASSLOCATION_HPP
 26 #define SHARE_CDS_AOTCLASSLOCATION_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "oops/array.hpp"
 30 #include "utilities/exceptions.hpp"
 31 #include "utilities/growableArray.hpp"
 32 #include "utilities/globalDefinitions.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 class AllClassLocationStreams;
 36 class ClassLocationStream;
 37 class LogStream;
 38 
 39 // An AOTClassLocation is a location where the application is configured to load Java classes
 40 // from. It can be:
 41 // - the location of $JAVA_HOME/lib/modules
 42 // - an entry in -Xbootclasspath/a
 43 // - an entry in -classpath
 44 // - a JAR file specified using --module-path.
 45 //
 46 // AOTClassLocation is similar to java.security.CodeSource, except:
 47 // - Only local files/dirs are allowed. Directories must be empty. Network locations are not allowed.
 48 // - No code signing information is recorded.
 49 //
 50 // We avoid using pointers in AOTClassLocation to avoid runtime pointer relocation. Each AOTClassLocation
 51 // is a variable-size structure:
 52 //    [ all fields specified below (sizeof(AOTClassLocation) bytes)          ]
 53 //    [ path (_path_length bytes, including the terminating zero)         ]
 54 //    [ manifest (_manifest_length bytes, including the terminating zero) ]
 55 class AOTClassLocation {
 56 public:
 57   enum class Group : int {
 58     MODULES_IMAGE,
 59     BOOT_CLASSPATH,
 60     APP_CLASSPATH,
 61     MODULE_PATH
 62   };
 63 private:
 64   enum class FileType : int {
 65     NORMAL,
 66     DIR,
 67     NOT_EXIST
 68   };
 69   size_t   _path_length;     // does NOT include terminating zero
 70   size_t   _manifest_length; // does NOT include terminating zero
 71   bool     _check_time;
 72   bool     _from_cpattr;
 73   bool     _is_multi_release_jar; // is this a JAR file that has multi-release classes?
 74   FileType _file_type;
 75   Group    _group;
 76   int      _index; // index of this AOTClassLocation inside AOTClassLocationConfig::_class_locations
 77   time_t   _timestamp;
 78   int64_t  _filesize;
 79 
 80   static size_t header_size()      { return sizeof(AOTClassLocation); } // bytes
 81   size_t path_offset()       const { return header_size(); }
 82   size_t manifest_offset()   const { return path_offset() + _path_length + 1; }
 83   static char* read_manifest(JavaThread* current, const char* path, size_t& manifest_length);
 84 
 85 public:
 86   static AOTClassLocation* allocate(JavaThread* current, const char* path, int index, Group group,
 87                                     bool from_cpattr = false, bool is_jrt = false);
 88 
 89   size_t total_size()                const { return manifest_offset() + _manifest_length + 1; }
 90   const char* path()                 const { return ((const char*)this) + path_offset();  }
 91   size_t manifest_length()           const { return _manifest_length; }
 92   const char* manifest()             const { return ((const char*)this) + manifest_offset(); }
 93   bool must_exist()                  const { return _file_type != FileType::NOT_EXIST; }
 94   bool must_not_exist()              const { return _file_type == FileType::NOT_EXIST; }
 95   bool is_dir()                      const { return _file_type == FileType::DIR; }
 96   int index()                        const { return _index; }
 97   bool is_modules_image()            const { return _group == Group::MODULES_IMAGE; }
 98   bool from_boot_classpath()         const { return _group == Group::BOOT_CLASSPATH; }
 99   bool from_app_classpath()          const { return _group == Group::APP_CLASSPATH; }
100   bool from_module_path()            const { return _group == Group::MODULE_PATH; }
101   bool is_multi_release_jar()        const { return _is_multi_release_jar; }
102 
103   // Only boot/app classpaths can contain unnamed module
104   bool has_unnamed_module()          const { return from_boot_classpath() || from_app_classpath(); }
105 
106   char* get_cpattr() const;
107   AOTClassLocation* write_to_archive() const;
108 
109   // Returns true IFF this AOTClassLocation is discovered from the -classpath or -Xbootclasspath/a by parsing the
110   // "Class-Path" attribute of a JAR file.
111   bool from_cpattr() const { return _from_cpattr; }
112   const char* file_type_string() const;
113   bool check(const char* runtime_path, bool has_aot_linked_classes) const;
114 };
115 
116 // AOTClassLocationConfig
117 //
118 // Keep track of the set of AOTClassLocations used when an AOTCache is created.
119 // To load the AOTCache in a production run, the JVM must be using a compatible set of
120 // AOTClassLocations (subjected to AOTClassLocationConfig::validate()).
121 //
122 // In general, validation is performed on the AOTClassLocations to ensure the code locations used
123 // during AOTCache creation are the same as when the AOTCache is used during runtime.
124 // Non-existent entries are recorded during AOTCache creation. Those non-existent entries,
125 // if they are specified at runtime, must not exist.
126 //
127 // Some details on validation:
128 // - the boot classpath can be appended to at runtime if there's no app classpath and no
129 //   module path specified when an AOTCache is created;
130 // - the app classpath can be appended to at runtime;
131 // - the module path at runtime can be a superset of the one specified during AOTCache creation.
132 
133 class AOTClassLocationConfig : public CHeapObj<mtClassShared> {
134   using Group = AOTClassLocation::Group;
135   using GrowableClassLocationArray = GrowableArrayCHeap<AOTClassLocation*, mtClassShared>;
136 
137   // Note: both of the following are non-null if we are dumping a dynamic archive.
138   static AOTClassLocationConfig* _dumptime_instance;
139   static const AOTClassLocationConfig* _runtime_instance;
140 
141   Array<AOTClassLocation*>* _class_locations; // jrt -> -Xbootclasspath/a -> -classpath -> --module_path
142   int _boot_classpath_end;
143   int _app_classpath_end;
144   int _module_end;
145   bool _has_non_jar_modules;
146   bool _has_platform_classes;
147   bool _has_app_classes;
148   int  _max_used_index;
149   size_t _dumptime_lcp_len;
150 
151   // accessors
152   Array<AOTClassLocation*>* class_locations() const { return _class_locations; }
153 
154   void parse(JavaThread* current, GrowableClassLocationArray& tmp_array, ClassLocationStream& css,
155              Group group, bool parse_manifest);
156   void add_class_location(JavaThread* current, GrowableClassLocationArray& tmp_array, const char* path,
157                        Group group, bool parse_manifest, bool from_cpattr);
158   void dumptime_init_helper(TRAPS);
159 
160   bool check_classpaths(bool is_boot_classpath, bool has_aot_linked_classes,
161                         int index_start, int index_end, ClassLocationStream& runtime_css,
162                         bool use_lcp_match, const char* runtime_lcp, size_t runtime_lcp_len) const;
163   bool check_module_paths(bool has_aot_linked_classes, int index_start, int index_end, ClassLocationStream& runtime_css,
164                           bool* has_extra_module_paths) const;
165   bool file_exists(const char* filename) const;
166   bool check_paths_existence(ClassLocationStream& runtime_css) const;
167 
168   static const char* substitute(const char* path, size_t remove_prefix_len,
169                                 const char* prepend, size_t prepend_len);
170   static const char* find_lcp(ClassLocationStream& css, size_t& lcp_len);
171   bool need_lcp_match(AllClassLocationStreams& all_css) const;
172   bool need_lcp_match_helper(int start, int end, ClassLocationStream& css) const;
173 
174   template <typename FUNC> void dumptime_iterate_helper(FUNC func) const {
175     assert(_class_locations != nullptr, "sanity");
176     int n = _class_locations->length();
177     for (int i = 0; i < n; i++) {
178       if (!func(_class_locations->at(i))) {
179         break;
180       }
181     }
182   }
183 
184   template <typename FUNC> void iterate(FUNC func) const {
185     int n = class_locations()->length();
186     for (int i = 0; i < n; i++) {
187       if (!func(class_locations()->at(i))) {
188         break;
189       }
190     }
191   }
192 
193   void check_nonempty_dirs() const;
194   bool need_to_check_app_classpath() const {
195     return (num_app_classpaths() > 0) && (_max_used_index >= app_cp_start_index()) && has_platform_or_app_classes();
196   }
197 
198   void print_dumptime_classpath(LogStream& ls, int index_start, int index_limit,
199                                 bool do_substitute, size_t remove_prefix_len,
200                                 const char* prepend, size_t prepend_len) const;
201 public:
202   static AOTClassLocationConfig* dumptime() {
203     assert(_dumptime_instance != nullptr, "can only be called when dumping an AOT cache");
204     return _dumptime_instance;
205   }
206 
207   static const AOTClassLocationConfig* runtime() {
208     assert(_runtime_instance != nullptr, "can only be called when using an AOT cache");
209     return _runtime_instance;
210   }
211 
212   // Common accessors
213   int boot_cp_start_index()          const { return 1; }
214   int boot_cp_end_index()            const { return _boot_classpath_end; }
215   int app_cp_start_index()           const { return boot_cp_end_index(); }
216   int app_cp_end_index()             const { return _app_classpath_end; }
217   int module_path_start_index()      const { return app_cp_end_index(); }
218   int module_path_end_index()        const { return _module_end; }
219   bool has_platform_or_app_classes() const { return _has_app_classes || _has_platform_classes; }
220   bool has_non_jar_modules()         const { return _has_non_jar_modules; }
221   int num_boot_classpaths()          const { return boot_cp_end_index() - boot_cp_start_index(); }
222   int num_app_classpaths()           const { return app_cp_end_index() - app_cp_start_index(); }
223   int num_module_paths()             const { return module_path_end_index() - module_path_start_index(); }
224 
225   int length() const {
226     return _class_locations->length();
227   }
228 
229   const AOTClassLocation* class_location_at(int index) const;
230   int get_module_shared_path_index(Symbol* location) const;
231 
232   // Functions used only during dumptime
233   static void dumptime_init(JavaThread* current);
234 
235   static void dumptime_set_has_app_classes() {
236     _dumptime_instance->_has_app_classes = true;
237   }
238 
239   static void dumptime_set_has_platform_classes() {
240     _dumptime_instance->_has_platform_classes = true;
241   }
242 
243   static void dumptime_update_max_used_index(int index) {
244     if (_dumptime_instance == nullptr) {
245       assert(index == 0, "sanity");
246     } else if (_dumptime_instance->_max_used_index < index) {
247       _dumptime_instance->_max_used_index = index;
248     }
249   }
250 
251   static void dumptime_check_nonempty_dirs() {
252     _dumptime_instance->check_nonempty_dirs();
253   }
254 
255   static bool dumptime_is_ready() {
256     return _dumptime_instance != nullptr;
257   }
258   template <typename FUNC> static void dumptime_iterate(FUNC func) {
259     _dumptime_instance->dumptime_iterate_helper(func);
260   }
261 
262   AOTClassLocationConfig* write_to_archive() const;
263 
264   // Functions used only during runtime
265   bool validate(bool has_aot_linked_classes, bool* has_extra_module_paths) const;
266 };
267 
268 
269 #endif // SHARE_CDS_AOTCLASSLOCATION_HPP