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 #include "precompiled.hpp"
26 #include "cds/cds_globals.hpp"
27 #include "cds/classListWriter.hpp"
28 #include "cds/lambdaFormInvokers.inline.hpp"
29 #include "classfile/classFileStream.hpp"
30 #include "classfile/classLoader.hpp"
31 #include "classfile/classLoaderData.hpp"
32 #include "classfile/classLoaderDataGraph.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "classfile/systemDictionaryShared.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "oops/constantPool.inline.hpp"
37 #include "oops/instanceKlass.hpp"
38 #include "runtime/mutexLocker.hpp"
39
40 fileStream* ClassListWriter::_classlist_file = nullptr;
41
42 void ClassListWriter::init() {
43 // For -XX:DumpLoadedClassList=<file> option
44 if (DumpLoadedClassList != nullptr) {
45 const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
46 _classlist_file = new(mtInternal)
47 fileStream(list_name);
48 _classlist_file->print_cr("# NOTE: Do not modify this file.");
49 _classlist_file->print_cr("#");
50 _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
51 _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
52 _classlist_file->print_cr("#");
53 FREE_C_HEAP_ARRAY(char, list_name);
54 }
55 }
56
57 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {
111 bool is_builtin_loader = SystemDictionaryShared::is_builtin_loader(loader_data);
112 if (!is_builtin_loader) {
113 // class may be loaded from shared archive
114 if (!k->is_shared()) {
115 if (cfs == nullptr || cfs->source() == nullptr) {
116 // CDS static dump only handles unregistered class with known source.
117 return;
118 }
119 if (strncmp(cfs->source(), "file:", 5) != 0) {
120 return;
121 }
122 } else {
123 // Shared unregistered classes are skipped since their real source are not recorded in shared space.
124 return;
125 }
126 if (!SystemDictionaryShared::add_unregistered_class(Thread::current(), (InstanceKlass*)k)) {
127 return;
128 }
129 }
130
131 // filter out java/lang/invoke/BoundMethodHandle$Species...
132 if (cfs != nullptr && cfs->source() != nullptr && strcmp(cfs->source(), "_ClassSpecializer_generateConcreteSpeciesCode") == 0) {
133 return;
134 }
135
136 {
137 InstanceKlass* super = k->java_super();
138 if (super != nullptr && !has_id(super)) {
139 return;
140 }
141
142 Array<InstanceKlass*>* interfaces = k->local_interfaces();
143 int len = interfaces->length();
144 for (int i = 0; i < len; i++) {
145 InstanceKlass* intf = interfaces->at(i);
146 if (!has_id(intf)) {
147 return;
148 }
149 }
150 }
151
152 if (k->is_hidden()) {
153 return;
183 #endif
184 }
185
186 stream->cr();
187 stream->flush();
188 }
189
190 void ClassListWriter::delete_classlist() {
191 if (_classlist_file != nullptr) {
192 delete _classlist_file;
193 }
194 }
195
196 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
197 public:
198 void do_cld(ClassLoaderData* cld) {
199 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
200 if (klass->is_instance_klass()) {
201 InstanceKlass* ik = InstanceKlass::cast(klass);
202 write_resolved_constants_for(ik);
203 }
204 }
205 }
206 };
207
208 void ClassListWriter::write_resolved_constants() {
209 if (!is_enabled()) {
210 return;
211 }
212 MutexLocker lock(ClassLoaderDataGraph_lock);
213 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
214
215 WriteResolveConstantsCLDClosure closure;
216 ClassLoaderDataGraph::loaded_cld_do(&closure);
217 }
218
219 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
220 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
221 ik->is_hidden()) {
222 return;
223 }
224 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
225 return;
226 }
227 if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
228 // This class is regenerated during JDK build process, so the classlist
229 // may not match the version that's in the real jdk image.
230 return;
231 }
232
233 if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
234 return;
235 }
236
237 ResourceMark rm;
238 ConstantPool* cp = ik->constants();
239 GrowableArray<bool> list(cp->length(), cp->length(), false);
240 bool print = false;
241
242 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
243 switch (cp->tag_at(cp_index).value()) {
244 case JVM_CONSTANT_Class:
245 {
246 Klass* k = cp->resolved_klass_at(cp_index);
247 if (k->is_instance_klass()) {
248 list.at_put(cp_index, true);
249 print = true;
250 }
251 }
252 break;
253 }
254 }
255
256 if (cp->cache() != nullptr) {
257 Array<ResolvedFieldEntry>* field_entries = cp->cache()->resolved_field_entries();
258 if (field_entries != nullptr) {
259 for (int i = 0; i < field_entries->length(); i++) {
260 ResolvedFieldEntry* rfe = field_entries->adr_at(i);
261 if (rfe->is_resolved(Bytecodes::_getfield) ||
262 rfe->is_resolved(Bytecodes::_putfield)) {
263 list.at_put(rfe->constant_pool_index(), true);
264 print = true;
265 }
266 }
267 }
268
269 Array<ResolvedMethodEntry>* method_entries = cp->cache()->resolved_method_entries();
270 if (method_entries != nullptr) {
271 for (int i = 0; i < method_entries->length(); i++) {
272 ResolvedMethodEntry* rme = method_entries->adr_at(i);
273 if (rme->is_resolved(Bytecodes::_invokevirtual) ||
274 rme->is_resolved(Bytecodes::_invokespecial) ||
275 rme->is_resolved(Bytecodes::_invokeinterface)) {
276 list.at_put(rme->constant_pool_index(), true);
277 print = true;
278 }
279 }
280 }
281 }
282
283 if (print) {
284 outputStream* stream = _classlist_file;
285 stream->print("@cp %s", ik->name()->as_C_string());
286 for (int i = 0; i < list.length(); i++) {
287 if (list.at(i)) {
288 constantTag cp_tag = cp->tag_at(i).value();
289 assert(cp_tag.value() == JVM_CONSTANT_Class ||
290 cp_tag.value() == JVM_CONSTANT_Fieldref ||
291 cp_tag.value() == JVM_CONSTANT_Methodref||
292 cp_tag.value() == JVM_CONSTANT_InterfaceMethodref, "sanity");
293 stream->print(" %d", i);
294 }
295 }
296 stream->cr();
297 }
298 }
|
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 #include "precompiled.hpp"
26 #include "cds/aotConstantPoolResolver.hpp"
27 #include "cds/cds_globals.hpp"
28 #include "cds/classListParser.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/lambdaFormInvokers.inline.hpp"
31 #include "classfile/classFileStream.hpp"
32 #include "classfile/classLoader.hpp"
33 #include "classfile/classLoaderData.hpp"
34 #include "classfile/classLoaderDataGraph.hpp"
35 #include "classfile/moduleEntry.hpp"
36 #include "classfile/symbolTable.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/constantPool.inline.hpp"
40 #include "oops/instanceKlass.hpp"
41 #include "runtime/javaCalls.hpp"
42 #include "runtime/mutexLocker.hpp"
43
44 fileStream* ClassListWriter::_classlist_file = nullptr;
45
46 void ClassListWriter::init() {
47 // For -XX:DumpLoadedClassList=<file> option
48 if (DumpLoadedClassList != nullptr) {
49 const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
50 _classlist_file = new(mtInternal)
51 fileStream(list_name);
52 _classlist_file->print_cr("# NOTE: Do not modify this file.");
53 _classlist_file->print_cr("#");
54 _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
55 _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
56 _classlist_file->print_cr("#");
57 FREE_C_HEAP_ARRAY(char, list_name);
58 }
59 }
60
61 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {
115 bool is_builtin_loader = SystemDictionaryShared::is_builtin_loader(loader_data);
116 if (!is_builtin_loader) {
117 // class may be loaded from shared archive
118 if (!k->is_shared()) {
119 if (cfs == nullptr || cfs->source() == nullptr) {
120 // CDS static dump only handles unregistered class with known source.
121 return;
122 }
123 if (strncmp(cfs->source(), "file:", 5) != 0) {
124 return;
125 }
126 } else {
127 // Shared unregistered classes are skipped since their real source are not recorded in shared space.
128 return;
129 }
130 if (!SystemDictionaryShared::add_unregistered_class(Thread::current(), (InstanceKlass*)k)) {
131 return;
132 }
133 }
134
135 if (cfs != nullptr && cfs->source() != nullptr) {
136 if (strcmp(cfs->source(), "_ClassSpecializer_generateConcreteSpeciesCode") == 0) {
137 return;
138 }
139
140 if (strncmp(cfs->source(), "__", 2) == 0) {
141 // generated class: __dynamic_proxy__, __JVM_LookupDefineClass__, etc
142 return;
143 }
144 }
145
146 {
147 InstanceKlass* super = k->java_super();
148 if (super != nullptr && !has_id(super)) {
149 return;
150 }
151
152 Array<InstanceKlass*>* interfaces = k->local_interfaces();
153 int len = interfaces->length();
154 for (int i = 0; i < len; i++) {
155 InstanceKlass* intf = interfaces->at(i);
156 if (!has_id(intf)) {
157 return;
158 }
159 }
160 }
161
162 if (k->is_hidden()) {
163 return;
193 #endif
194 }
195
196 stream->cr();
197 stream->flush();
198 }
199
200 void ClassListWriter::delete_classlist() {
201 if (_classlist_file != nullptr) {
202 delete _classlist_file;
203 }
204 }
205
206 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
207 public:
208 void do_cld(ClassLoaderData* cld) {
209 for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
210 if (klass->is_instance_klass()) {
211 InstanceKlass* ik = InstanceKlass::cast(klass);
212 write_resolved_constants_for(ik);
213 write_array_info_for(ik); // FIXME: piggybacking on WriteResolveConstantsCLDClosure is misleading
214 }
215 }
216 }
217 };
218
219 void ClassListWriter::write_array_info_for(InstanceKlass* ik) {
220 ObjArrayKlass* oak = ik->array_klasses();
221 if (oak != nullptr) {
222 while (oak->higher_dimension() != nullptr) {
223 oak = oak->higher_dimension();
224 }
225 ResourceMark rm;
226 outputStream* stream = _classlist_file;
227 stream->print_cr("%s %s %d", ClassListParser::ARRAY_TAG, ik->name()->as_C_string(), oak->dimension());
228 }
229 }
230
231 void ClassListWriter::write_resolved_constants() {
232 if (!is_enabled()) {
233 return;
234 }
235 MutexLocker lock(ClassLoaderDataGraph_lock);
236 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
237
238 WriteResolveConstantsCLDClosure closure;
239 ClassLoaderDataGraph::loaded_cld_do(&closure);
240 }
241
242 void ClassListWriter::write_reflection_data() {
243 if (!is_enabled()) {
244 return;
245 }
246 auto collector = [&] (const InstanceKlass* ik, int id) {
247 write_reflection_data_for(const_cast<InstanceKlass*>(ik));
248 };
249 _id_table->iterate_all(collector);
250 }
251
252 void ClassListWriter::write_reflection_data_for(InstanceKlass* ik) {
253 ResourceMark rm;
254 outputStream* stream = _classlist_file;
255 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) || ik->is_hidden()) {
256 return; // ignore
257 }
258 if (java_lang_Class::has_reflection_data(ik->java_mirror())) {
259 EXCEPTION_MARK;
260 int rd_flags = AOTConstantPoolResolver::class_reflection_data_flags(ik, THREAD);
261 if (!HAS_PENDING_EXCEPTION) {
262 // We can't hold the lock when doing the upcall inside class_reflection_data_flags()
263 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
264 stream->print_cr("%s %s %d", ClassListParser::CLASS_REFLECTION_DATA_TAG, ik->name()->as_C_string(), rd_flags);
265 }
266 }
267 }
268
269 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
270 if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
271 ik->is_hidden()) {
272 return;
273 }
274 if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
275 return;
276 }
277 if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
278 // This class is regenerated during JDK build process, so the classlist
279 // may not match the version that's in the real jdk image.
280 return;
281 }
282
283 if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
284 return;
285 }
286
287 ResourceMark rm;
288 ConstantPool* cp = ik->constants();
289 GrowableArray<bool> list(cp->length(), cp->length(), false);
290 bool print = false;
291
292 for (int cp_index = 1; cp_index < cp->length(); cp_index++) { // Index 0 is unused
293 switch (cp->tag_at(cp_index).value()) {
294 case JVM_CONSTANT_Class:
295 {
296 Klass* k = cp->resolved_klass_at(cp_index);
297 if (k->is_instance_klass()) {
298 list.at_put(cp_index, true);
299 print = true;
300 }
301 }
302 break;
303 }
304 }
305
306 if (cp->cache() != nullptr) {
307 Array<ResolvedIndyEntry>* indy_entries = cp->cache()->resolved_indy_entries();
308 if (indy_entries != nullptr) {
309 for (int i = 0; i < indy_entries->length(); i++) {
310 ResolvedIndyEntry* rie = indy_entries->adr_at(i);
311 int cp_index = rie->constant_pool_index();
312 if (rie->is_resolved()) {
313 list.at_put(cp_index, true);
314 print = true;
315 }
316 }
317 }
318
319 Array<ResolvedFieldEntry>* field_entries = cp->cache()->resolved_field_entries();
320 if (field_entries != nullptr) {
321 for (int i = 0; i < field_entries->length(); i++) {
322 ResolvedFieldEntry* rfe = field_entries->adr_at(i);
323 if (rfe->is_resolved(Bytecodes::_getfield) ||
324 rfe->is_resolved(Bytecodes::_putfield)) {
325 list.at_put(rfe->constant_pool_index(), true);
326 print = true;
327 }
328 }
329 }
330
331 Array<ResolvedMethodEntry>* method_entries = cp->cache()->resolved_method_entries();
332 if (method_entries != nullptr) {
333 for (int i = 0; i < method_entries->length(); i++) {
334 ResolvedMethodEntry* rme = method_entries->adr_at(i);
335 if (rme->is_resolved(Bytecodes::_invokevirtual) ||
336 rme->is_resolved(Bytecodes::_invokespecial) ||
337 rme->is_resolved(Bytecodes::_invokeinterface) ||
338 rme->is_resolved(Bytecodes::_invokestatic) ||
339 rme->is_resolved(Bytecodes::_invokehandle)) {
340 list.at_put(rme->constant_pool_index(), true);
341 print = true;
342 }
343 }
344 }
345 }
346
347 if (print) {
348 outputStream* stream = _classlist_file;
349 stream->print("@cp %s", ik->name()->as_C_string());
350 for (int i = 0; i < list.length(); i++) {
351 if (list.at(i)) {
352 constantTag cp_tag = cp->tag_at(i).value();
353 assert(cp_tag.value() == JVM_CONSTANT_Class ||
354 cp_tag.value() == JVM_CONSTANT_Fieldref ||
355 cp_tag.value() == JVM_CONSTANT_Methodref||
356 cp_tag.value() == JVM_CONSTANT_InterfaceMethodref ||
357 cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity");
358 stream->print(" %d", i);
359 }
360 }
361 stream->cr();
362 }
363 }
364
365 void ClassListWriter::write_loader_negative_lookup_cache_for(oop loader, const char* loader_type) {
366 TempNewSymbol method = SymbolTable::new_symbol("negativeLookupCacheContents");
367 TempNewSymbol signature = SymbolTable::new_symbol("()Ljava/lang/String;");
368
369 EXCEPTION_MARK;
370 HandleMark hm(THREAD);
371
372 JavaValue result(T_OBJECT);
373 JavaCalls::call_virtual(&result,
374 Handle(THREAD, loader),
375 loader->klass(),
376 method,
377 signature,
378 CHECK);
379
380 if (HAS_PENDING_EXCEPTION) {
381 log_warning(cds)("Error during BuiltinClassLoader::negativeLookupCacheContents() call for %s loader", loader_type);
382 CLEAR_PENDING_EXCEPTION;
383 return;
384 } else if (result.get_oop() == nullptr) {
385 return;
386 }
387
388 ResourceMark rm;
389 const char* cache_contents = java_lang_String::as_utf8_string(result.get_oop());
390 log_debug(cds)("%s loader negative cache: %s", loader_type, cache_contents);
391
392 outputStream* stream = _classlist_file;
393 const size_t buffer_size = strlen(ClassListParser::LOADER_NEGATIVE_CACHE_TAG) + 1 /* for space*/
394 + strlen(loader_type) + 1 /* for space */ + strlen(cache_contents) + 1 /* for null character */;
395 char* buffer = NEW_C_HEAP_ARRAY(char, buffer_size, mtInternal);
396 _classlist_file->set_scratch_buffer(buffer, buffer_size);
397 MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
398 stream->print("%s %s %s", ClassListParser::LOADER_NEGATIVE_CACHE_TAG, loader_type, cache_contents);
399 stream->cr();
400 _classlist_file->set_scratch_buffer(nullptr, 0);
401 }
402
403 void ClassListWriter::write_loader_negative_lookup_cache() {
404 if (!is_enabled()) {
405 return;
406 }
407
408 write_loader_negative_lookup_cache_for(SystemDictionary::java_platform_loader(), "platform");
409 write_loader_negative_lookup_cache_for(SystemDictionary::java_system_loader(), "app");
410 }
|