1 /*
   2 * Copyright (c) 2015, 2016, 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 #include "precompiled.hpp"
  26 #include "classfile/classFileParser.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/classLoaderData.hpp"
  30 #include "classfile/classLoaderData.inline.hpp"
  31 #include "classfile/klassFactory.hpp"
  32 #include "classfile/sharedClassUtil.hpp"
  33 #include "memory/metaspaceShared.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "prims/jvmtiEnvBase.hpp"
  36 #include "prims/jvmtiRedefineClasses.hpp"
  37 #include "trace/traceMacros.hpp"
  38 
  39 // called during initial loading of a shared class
  40 InstanceKlass* KlassFactory::check_shared_class_file_load_hook(
  41                                           InstanceKlass* ik,
  42                                           Symbol* class_name,
  43                                           Handle class_loader,
  44                                           Handle protection_domain, TRAPS) {
  45 #if INCLUDE_CDS && INCLUDE_JVMTI
  46   assert(ik != NULL, "sanity");
  47   assert(ik->is_shared(), "expecting a shared class");
  48 
  49   if (JvmtiExport::should_post_class_file_load_hook()) {
  50     assert(THREAD->is_Java_thread(), "must be JavaThread");
  51 
  52     // Post the CFLH
  53     JvmtiCachedClassFileData* cached_class_file = NULL;
  54     JvmtiCachedClassFileData* archived_class_data = ik->get_archived_class_data();
  55     assert(archived_class_data != NULL, "shared class has no archived class data");
  56     unsigned char* ptr =
  57         VM_RedefineClasses::get_cached_class_file_bytes(archived_class_data);
  58     unsigned char* end_ptr =
  59         ptr + VM_RedefineClasses::get_cached_class_file_len(archived_class_data);
  60     unsigned char* old_ptr = ptr;
  61     JvmtiExport::post_class_file_load_hook(class_name,
  62                                            class_loader,
  63                                            protection_domain,
  64                                            &ptr,
  65                                            &end_ptr,
  66                                            &cached_class_file);
  67     if (old_ptr != ptr) {
  68       // JVMTI agent has modified class file data.
  69       // Set new class file stream using JVMTI agent modified class file data.
  70       ClassLoaderData* loader_data =
  71         ClassLoaderData::class_loader_data(class_loader());
  72       int path_index = ik->shared_classpath_index();
  73       SharedClassPathEntry* ent =
  74         (SharedClassPathEntry*)FileMapInfo::shared_classpath(path_index);
  75       ClassFileStream* stream = new ClassFileStream(ptr,
  76                                                     end_ptr - ptr,
  77                                                     ent == NULL ? NULL : ent->_name,
  78                                                     ClassFileStream::verify);
  79       ClassFileParser parser(stream,
  80                              class_name,
  81                              loader_data,
  82                              protection_domain,
  83                              NULL,
  84                              NULL,
  85                              ClassFileParser::BROADCAST, // publicity level
  86                              CHECK_NULL);
  87       InstanceKlass* new_ik = parser.create_instance_klass(true /* changed_by_loadhook */,
  88                                                            CHECK_NULL);
  89       if (cached_class_file != NULL) {
  90         new_ik->set_cached_class_file(cached_class_file);
  91       }
  92 
  93       if (class_loader.is_null()) {
  94         ResourceMark rm;
  95         ClassLoader::add_package(class_name->as_C_string(), path_index, THREAD);
  96       }
  97 
  98       return new_ik;
  99     }
 100   }
 101 #endif
 102 
 103   return NULL;
 104 }
 105 
 106 
 107 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
 108                                                    Symbol* name,
 109                                                    ClassLoaderData* loader_data,
 110                                                    Handle protection_domain,
 111                                                    JvmtiCachedClassFileData** cached_class_file,
 112                                                    TRAPS) {
 113 
 114   assert(stream != NULL, "invariant");
 115 
 116   if (JvmtiExport::should_post_class_file_load_hook()) {
 117     assert(THREAD->is_Java_thread(), "must be a JavaThread");
 118     const JavaThread* jt = (JavaThread*)THREAD;
 119 
 120     Handle class_loader(THREAD, loader_data->class_loader());
 121 
 122     // Get the cached class file bytes (if any) from the class that
 123     // is being redefined or retransformed. We use jvmti_thread_state()
 124     // instead of JvmtiThreadState::state_for(jt) so we don't allocate
 125     // a JvmtiThreadState any earlier than necessary. This will help
 126     // avoid the bug described by 7126851.
 127 
 128     JvmtiThreadState* state = jt->jvmti_thread_state();
 129 
 130     if (state != NULL) {
 131       Klass* k = state->get_class_being_redefined();
 132 
 133       if (k != NULL) {
 134         InstanceKlass* class_being_redefined = InstanceKlass::cast(k);
 135         *cached_class_file = class_being_redefined->get_cached_class_file();
 136       }
 137     }
 138 
 139     unsigned char* ptr = const_cast<unsigned char*>(stream->buffer());
 140     unsigned char* end_ptr = ptr + stream->length();
 141 
 142     JvmtiExport::post_class_file_load_hook(name,
 143                                            class_loader,
 144                                            protection_domain,
 145                                            &ptr,
 146                                            &end_ptr,
 147                                            cached_class_file);
 148 
 149     if (ptr != stream->buffer()) {
 150       // JVMTI agent has modified class file data.
 151       // Set new class file stream using JVMTI agent modified class file data.
 152       stream = new ClassFileStream(ptr,
 153                                    end_ptr - ptr,
 154                                    stream->source(),
 155                                    stream->need_verify());
 156     }
 157   }
 158 
 159   return stream;
 160 }
 161 
 162 
 163 InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream,
 164                                                 Symbol* name,
 165                                                 ClassLoaderData* loader_data,
 166                                                 Handle protection_domain,
 167                                                 const InstanceKlass* host_klass,
 168                                                 GrowableArray<Handle>* cp_patches,
 169                                                 TRAPS) {
 170   assert(stream != NULL, "invariant");
 171   assert(loader_data != NULL, "invariant");
 172   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 173 
 174   ResourceMark rm;
 175   HandleMark hm;
 176 
 177   JvmtiCachedClassFileData* cached_class_file = NULL;
 178 
 179   ClassFileStream* old_stream = stream;
 180 
 181   // Skip this processing for VM anonymous classes
 182   if (host_klass == NULL) {
 183     stream = check_class_file_load_hook(stream,
 184                                         name,
 185                                         loader_data,
 186                                         protection_domain,
 187                                         &cached_class_file,
 188                                         CHECK_NULL);
 189   }
 190 
 191   ClassFileParser parser(stream,
 192                          name,
 193                          loader_data,
 194                          protection_domain,
 195                          host_klass,
 196                          cp_patches,
 197                          ClassFileParser::BROADCAST, // publicity level
 198                          CHECK_NULL);
 199 
 200   InstanceKlass* result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
 201   assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
 202 
 203   if (result == NULL) {
 204     return NULL;
 205   }
 206 
 207   if (cached_class_file != NULL) {
 208     // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
 209     result->set_cached_class_file(cached_class_file);
 210   }
 211 
 212   if (InstanceKlass::should_store_fingerprint()) {
 213     result->store_fingerprint(!result->is_anonymous() ? stream->compute_fingerprint() : 0);
 214   }
 215 
 216   TRACE_KLASS_CREATION(result, parser, THREAD);
 217 
 218 #if INCLUDE_CDS && INCLUDE_JVMTI
 219   if (DumpSharedSpaces) {
 220     assert(cached_class_file == NULL, "Sanity");
 221     // Archive the class stream data into the optional data section
 222     JvmtiCachedClassFileData *p;
 223     int len;
 224     const unsigned char *bytes;
 225     // event based tracing might set cached_class_file
 226     if ((bytes = result->get_cached_class_file_bytes()) != NULL) {
 227       len = result->get_cached_class_file_len();
 228     } else {
 229       len = stream->length();
 230       bytes = stream->buffer();
 231     }
 232     p = (JvmtiCachedClassFileData*)MetaspaceShared::optional_data_space_alloc(
 233                     offset_of(JvmtiCachedClassFileData, data) + len);
 234     p->length = len;
 235     memcpy(p->data, bytes, len);
 236     result->set_archived_class_data(p);
 237   }
 238 #endif
 239 
 240   return result;
 241 }