< prev index next >

src/share/vm/classfile/klassFactory.cpp

Print this page




   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 "classfile/classFileParser.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoaderData.hpp"

  29 #include "classfile/klassFactory.hpp"


  30 #include "memory/resourceArea.hpp"
  31 #include "prims/jvmtiEnvBase.hpp"

  32 #include "trace/traceMacros.hpp"
  33 






























































  34 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
  35                                                    Symbol* name,
  36                                                    ClassLoaderData* loader_data,
  37                                                    Handle protection_domain,
  38                                                    JvmtiCachedClassFileData** cached_class_file,
  39                                                    TRAPS) {
  40 
  41   assert(stream != NULL, "invariant");
  42 
  43   if (JvmtiExport::should_post_class_file_load_hook()) {
  44     assert(THREAD->is_Java_thread(), "must be a JavaThread");
  45     const JavaThread* jt = (JavaThread*)THREAD;
  46 
  47     Handle class_loader(THREAD, loader_data->class_loader());
  48 
  49     // Get the cached class file bytes (if any) from the class that
  50     // is being redefined or retransformed. We use jvmti_thread_state()
  51     // instead of JvmtiThreadState::state_for(jt) so we don't allocate
  52     // a JvmtiThreadState any earlier than necessary. This will help
  53     // avoid the bug described by 7126851.


  80       // JVMTI agent has modified class file data.
  81       // Set new class file stream using JVMTI agent modified class file data.
  82       stream = new ClassFileStream(ptr,
  83                                    end_ptr - ptr,
  84                                    stream->source(),
  85                                    stream->need_verify());
  86     }
  87   }
  88 
  89   return stream;
  90 }
  91 
  92 
  93 instanceKlassHandle KlassFactory::create_from_stream(ClassFileStream* stream,
  94                                                      Symbol* name,
  95                                                      ClassLoaderData* loader_data,
  96                                                      Handle protection_domain,
  97                                                      const InstanceKlass* host_klass,
  98                                                      GrowableArray<Handle>* cp_patches,
  99                                                      TRAPS) {
 100 
 101   assert(stream != NULL, "invariant");
 102   assert(loader_data != NULL, "invariant");
 103   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 104 
 105   ResourceMark rm;
 106   HandleMark hm;
 107 
 108   JvmtiCachedClassFileData* cached_class_file = NULL;
 109 
 110   ClassFileStream* old_stream = stream;
 111 
 112   // Skip this processing for VM anonymous classes
 113   if (host_klass == NULL) {
 114     stream = check_class_file_load_hook(stream,
 115                                         name,
 116                                         loader_data,
 117                                         protection_domain,
 118                                         &cached_class_file,
 119                                         CHECK_NULL);
 120   }


 124                          loader_data,
 125                          protection_domain,
 126                          host_klass,
 127                          cp_patches,
 128                          ClassFileParser::BROADCAST, // publicity level
 129                          CHECK_NULL);
 130 
 131   instanceKlassHandle result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
 132   assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
 133 
 134   if (result.is_null()) {
 135     return NULL;
 136   }
 137 
 138   if (cached_class_file != NULL) {
 139     // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
 140     result->set_cached_class_file(cached_class_file);
 141   }
 142 
 143   TRACE_KLASS_CREATION(result, parser, THREAD);






















 144 
 145   return result;
 146 }


   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 "classfile/classFileParser.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoaderData.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/klassFactory.hpp"
  31 #include "classfile/sharedClassUtil.hpp"
  32 #include "memory/metaspaceShared.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "prims/jvmtiEnvBase.hpp"
  35 #include "prims/jvmtiRedefineClasses.hpp"
  36 #include "trace/traceMacros.hpp"
  37 
  38 // called during initial loading of a shared class
  39 instanceKlassHandle KlassFactory::check_shared_class_file_load_hook(
  40                                           instanceKlassHandle ik,
  41                                           Symbol* class_name, 
  42                                           Handle class_loader,
  43                                           Handle protection_domain, TRAPS) {
  44 #if INCLUDE_CDS && INCLUDE_JVMTI
  45   assert(ik.not_null(), "sanity");
  46   assert(ik()->is_shared(), "expecting a shared class");
  47 
  48   if (JvmtiExport::should_post_class_file_load_hook()) {
  49     assert(THREAD->is_Java_thread(), "must be JavaThread");
  50 
  51     // Post the CFLH
  52     JvmtiCachedClassFileData* cached_class_file = NULL;
  53     JvmtiCachedClassFileData* archived_class_data = ik->get_archived_class_data();
  54     assert(archived_class_data != NULL, "shared class has no archived class data");
  55     unsigned char* ptr =
  56         VM_RedefineClasses::get_cached_class_file_bytes(archived_class_data);
  57     unsigned char* end_ptr =
  58         ptr + VM_RedefineClasses::get_cached_class_file_len(archived_class_data);
  59     unsigned char* old_ptr = ptr;
  60     JvmtiExport::post_class_file_load_hook(class_name,
  61                                            class_loader,
  62                                            protection_domain,
  63                                            &ptr,
  64                                            &end_ptr,
  65                                            &cached_class_file);
  66     if (old_ptr != ptr) {
  67       // JVMTI agent has modified class file data.
  68       // Set new class file stream using JVMTI agent modified class file data.
  69       ClassLoaderData* loader_data =
  70         ClassLoaderData::class_loader_data(class_loader());
  71       int path_index = ik->shared_classpath_index();
  72       SharedClassPathEntry* ent =
  73         (SharedClassPathEntry*)FileMapInfo::shared_classpath(path_index);
  74       ClassFileStream* stream = new ClassFileStream(ptr,
  75                                                     end_ptr - ptr,
  76                                                     ent->_name,
  77                                                     ClassFileStream::verify);
  78       ClassFileParser parser(stream,
  79                              class_name,
  80                              loader_data,
  81                              protection_domain,
  82                              NULL,
  83                              NULL,
  84                              ClassFileParser::BROADCAST, // publicity level
  85                              CHECK_NULL);
  86       instanceKlassHandle new_ik = parser.create_instance_klass(true /* changed_by_loadhook */,
  87                                                                 CHECK_NULL);
  88       if (cached_class_file != NULL) {
  89         new_ik->set_cached_class_file(cached_class_file);
  90       }
  91       return new_ik;
  92     }
  93   }
  94 #endif
  95 
  96   return NULL;
  97 }
  98 
  99 
 100 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
 101                                                    Symbol* name,
 102                                                    ClassLoaderData* loader_data,
 103                                                    Handle protection_domain,
 104                                                    JvmtiCachedClassFileData** cached_class_file,
 105                                                    TRAPS) {
 106 
 107   assert(stream != NULL, "invariant");
 108 
 109   if (JvmtiExport::should_post_class_file_load_hook()) {
 110     assert(THREAD->is_Java_thread(), "must be a JavaThread");
 111     const JavaThread* jt = (JavaThread*)THREAD;
 112 
 113     Handle class_loader(THREAD, loader_data->class_loader());
 114 
 115     // Get the cached class file bytes (if any) from the class that
 116     // is being redefined or retransformed. We use jvmti_thread_state()
 117     // instead of JvmtiThreadState::state_for(jt) so we don't allocate
 118     // a JvmtiThreadState any earlier than necessary. This will help
 119     // avoid the bug described by 7126851.


 146       // JVMTI agent has modified class file data.
 147       // Set new class file stream using JVMTI agent modified class file data.
 148       stream = new ClassFileStream(ptr,
 149                                    end_ptr - ptr,
 150                                    stream->source(),
 151                                    stream->need_verify());
 152     }
 153   }
 154 
 155   return stream;
 156 }
 157 
 158 
 159 instanceKlassHandle KlassFactory::create_from_stream(ClassFileStream* stream,
 160                                                      Symbol* name,
 161                                                      ClassLoaderData* loader_data,
 162                                                      Handle protection_domain,
 163                                                      const InstanceKlass* host_klass,
 164                                                      GrowableArray<Handle>* cp_patches,
 165                                                      TRAPS) {

 166   assert(stream != NULL, "invariant");
 167   assert(loader_data != NULL, "invariant");
 168   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 169 
 170   ResourceMark rm;
 171   HandleMark hm;
 172 
 173   JvmtiCachedClassFileData* cached_class_file = NULL;
 174 
 175   ClassFileStream* old_stream = stream;
 176 
 177   // Skip this processing for VM anonymous classes
 178   if (host_klass == NULL) {
 179     stream = check_class_file_load_hook(stream,
 180                                         name,
 181                                         loader_data,
 182                                         protection_domain,
 183                                         &cached_class_file,
 184                                         CHECK_NULL);
 185   }


 189                          loader_data,
 190                          protection_domain,
 191                          host_klass,
 192                          cp_patches,
 193                          ClassFileParser::BROADCAST, // publicity level
 194                          CHECK_NULL);
 195 
 196   instanceKlassHandle result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
 197   assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
 198 
 199   if (result.is_null()) {
 200     return NULL;
 201   }
 202 
 203   if (cached_class_file != NULL) {
 204     // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
 205     result->set_cached_class_file(cached_class_file);
 206   }
 207 
 208   TRACE_KLASS_CREATION(result, parser, THREAD);
 209 
 210 #if INCLUDE_CDS && INCLUDE_JVMTI
 211   if (DumpSharedSpaces) {
 212     assert(cached_class_file == NULL, "Sanity");
 213     // Archive the class stream data into the optional data section
 214     JvmtiCachedClassFileData *p;
 215     int len;
 216     const unsigned char *bytes;
 217     // event based tracing might set cached_class_file
 218     if ((bytes = result->get_cached_class_file_bytes()) != NULL) {
 219       len = result->get_cached_class_file_len();
 220     } else {
 221       len = stream->length();
 222       bytes = stream->buffer();
 223     } 
 224     p = (JvmtiCachedClassFileData*)MetaspaceShared::optional_data_space_alloc(
 225                     offset_of(JvmtiCachedClassFileData, data) + len);
 226     p->length = len;
 227     memcpy(p->data, bytes, len);
 228     result->set_archived_class_data(p);
 229   }
 230 #endif
 231 
 232   return result;
 233 }
< prev index next >