src/hotspot/share/classfile/klassFactory.cpp
changeset 53884 1a7b57d02107
parent 52692 bffef37beacb
child 54340 2221f042556d
equal deleted inserted replaced
53883:f41793b5b83f 53884:1a7b57d02107
     1 /*
     1 /*
     2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
     3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4 *
     4 *
     5 * This code is free software; you can redistribute it and/or modify it
     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
     6 * under the terms of the GNU General Public License version 2 only, as
     7 * published by the Free Software Foundation.
     7 * published by the Free Software Foundation.
    44 // called during initial loading of a shared class
    44 // called during initial loading of a shared class
    45 InstanceKlass* KlassFactory::check_shared_class_file_load_hook(
    45 InstanceKlass* KlassFactory::check_shared_class_file_load_hook(
    46                                           InstanceKlass* ik,
    46                                           InstanceKlass* ik,
    47                                           Symbol* class_name,
    47                                           Symbol* class_name,
    48                                           Handle class_loader,
    48                                           Handle class_loader,
    49                                           Handle protection_domain, TRAPS) {
    49                                           Handle protection_domain,
       
    50                                           const ClassFileStream *cfs,
       
    51                                           TRAPS) {
    50 #if INCLUDE_CDS && INCLUDE_JVMTI
    52 #if INCLUDE_CDS && INCLUDE_JVMTI
    51   assert(ik != NULL, "sanity");
    53   assert(ik != NULL, "sanity");
    52   assert(ik->is_shared(), "expecting a shared class");
    54   assert(ik->is_shared(), "expecting a shared class");
    53 
       
    54   if (JvmtiExport::should_post_class_file_load_hook()) {
    55   if (JvmtiExport::should_post_class_file_load_hook()) {
    55     assert(THREAD->is_Java_thread(), "must be JavaThread");
    56     assert(THREAD->is_Java_thread(), "must be JavaThread");
    56 
    57 
    57     // Post the CFLH
    58     // Post the CFLH
    58     JvmtiCachedClassFileData* cached_class_file = NULL;
    59     JvmtiCachedClassFileData* cached_class_file = NULL;
    59     JvmtiCachedClassFileData* archived_class_data = ik->get_archived_class_data();
    60     if (cfs == NULL) {
    60     assert(archived_class_data != NULL, "shared class has no archived class data");
    61       cfs = FileMapInfo::open_stream_for_jvmti(ik, CHECK_NULL);
    61     unsigned char* ptr =
    62     }
    62         VM_RedefineClasses::get_cached_class_file_bytes(archived_class_data);
    63     unsigned char* ptr = (unsigned char*)cfs->buffer();
    63     unsigned char* end_ptr =
    64     unsigned char* end_ptr = ptr + cfs->length();
    64         ptr + VM_RedefineClasses::get_cached_class_file_len(archived_class_data);
       
    65     unsigned char* old_ptr = ptr;
    65     unsigned char* old_ptr = ptr;
    66     JvmtiExport::post_class_file_load_hook(class_name,
    66     JvmtiExport::post_class_file_load_hook(class_name,
    67                                            class_loader,
    67                                            class_loader,
    68                                            protection_domain,
    68                                            protection_domain,
    69                                            &ptr,
    69                                            &ptr,
    73       // JVMTI agent has modified class file data.
    73       // JVMTI agent has modified class file data.
    74       // Set new class file stream using JVMTI agent modified class file data.
    74       // Set new class file stream using JVMTI agent modified class file data.
    75       ClassLoaderData* loader_data =
    75       ClassLoaderData* loader_data =
    76         ClassLoaderData::class_loader_data(class_loader());
    76         ClassLoaderData::class_loader_data(class_loader());
    77       int path_index = ik->shared_classpath_index();
    77       int path_index = ik->shared_classpath_index();
    78       const char* pathname;
       
    79       if (path_index < 0) {
       
    80         // shared classes loaded by user defined class loader
       
    81         // do not have shared_classpath_index
       
    82         ModuleEntry* mod_entry = ik->module();
       
    83         if (mod_entry != NULL && (mod_entry->location() != NULL)) {
       
    84           ResourceMark rm;
       
    85           pathname = (const char*)(mod_entry->location()->as_C_string());
       
    86         } else {
       
    87           pathname = "";
       
    88         }
       
    89       } else {
       
    90         SharedClassPathEntry* ent =
       
    91           (SharedClassPathEntry*)FileMapInfo::shared_path(path_index);
       
    92         pathname = ent == NULL ? NULL : ent->name();
       
    93       }
       
    94       ClassFileStream* stream = new ClassFileStream(ptr,
    78       ClassFileStream* stream = new ClassFileStream(ptr,
    95                                                     end_ptr - ptr,
    79                                                     end_ptr - ptr,
    96                                                     pathname,
    80                                                     cfs->source(),
    97                                                     ClassFileStream::verify);
    81                                                     ClassFileStream::verify);
    98       ClassFileParser parser(stream,
    82       ClassFileParser parser(stream,
    99                              class_name,
    83                              class_name,
   100                              loader_data,
    84                              loader_data,
   101                              protection_domain,
    85                              protection_domain,
   234   JFR_ONLY(ON_KLASS_CREATION(result, parser, THREAD);)
   218   JFR_ONLY(ON_KLASS_CREATION(result, parser, THREAD);)
   235 
   219 
   236 #if INCLUDE_CDS
   220 #if INCLUDE_CDS
   237   if (DumpSharedSpaces) {
   221   if (DumpSharedSpaces) {
   238     ClassLoader::record_result(result, stream, THREAD);
   222     ClassLoader::record_result(result, stream, THREAD);
   239 #if INCLUDE_JVMTI
       
   240     assert(cached_class_file == NULL, "Sanity");
       
   241     // Archive the class stream data into the optional data section
       
   242     JvmtiCachedClassFileData *p;
       
   243     int len;
       
   244     const unsigned char *bytes;
       
   245     // event based tracing might set cached_class_file
       
   246     if ((bytes = result->get_cached_class_file_bytes()) != NULL) {
       
   247       len = result->get_cached_class_file_len();
       
   248     } else {
       
   249       len = stream->length();
       
   250       bytes = stream->buffer();
       
   251     }
       
   252     p = (JvmtiCachedClassFileData*)os::malloc(offset_of(JvmtiCachedClassFileData, data) + len, mtInternal);
       
   253     p->length = len;
       
   254     memcpy(p->data, bytes, len);
       
   255     result->set_archived_class_data(p);
       
   256 #endif // INCLUDE_JVMTI
       
   257   }
   223   }
   258 #endif // INCLUDE_CDS
   224 #endif // INCLUDE_CDS
   259 
   225 
   260   return result;
   226   return result;
   261 }
   227 }