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