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 instanceKlassHandle KlassFactory::check_shared_class_file_load_hook(
  41                                           instanceKlassHandle ik,
  42                                           Symbol* class_name,
  43                                           Handle class_loader,
  44                                           Handle protection_domain, TRAPS) {
  45 #if INCLUDE_CDS && INCLUDE_JVMTI
  46   assert(ik.not_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       instanceKlassHandle 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       KlassHandle* h_class_being_redefined =
 132         state->get_class_being_redefined();
 133 
 134       if (h_class_being_redefined != NULL) {
 135         instanceKlassHandle ikh_class_being_redefined =
 136           instanceKlassHandle(THREAD, (*h_class_being_redefined)());
 137 
 138         *cached_class_file = ikh_class_being_redefined->get_cached_class_file();
 139       }
 140     }
 141 
 142     unsigned char* ptr = const_cast<unsigned char*>(stream->buffer());
 143     unsigned char* end_ptr = ptr + stream->length();
 144 
 145     JvmtiExport::post_class_file_load_hook(name,
 146                                            class_loader,
 147                                            protection_domain,
 148                                            &ptr,
 149                                            &end_ptr,
 150                                            cached_class_file);
 151 
 152     if (ptr != stream->buffer()) {
 153       // JVMTI agent has modified class file data.
 154       // Set new class file stream using JVMTI agent modified class file data.
 155       stream = new ClassFileStream(ptr,
 156                                    end_ptr - ptr,
 157                                    stream->source(),
 158                                    stream->need_verify());
 159     }
 160   }
 161 
 162   return stream;
 163 }
 164 
 165 
 166 instanceKlassHandle KlassFactory::create_from_stream(ClassFileStream* stream,
 167                                                      Symbol* name,
 168                                                      ClassLoaderData* loader_data,
 169                                                      Handle protection_domain,
 170                                                      const InstanceKlass* host_klass,
 171                                                      GrowableArray<Handle>* cp_patches,
 172                                                      TRAPS) {
 173   assert(stream != NULL, "invariant");
 174   assert(loader_data != NULL, "invariant");
 175   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 176 
 177   ResourceMark rm;
 178   HandleMark hm;
 179 
 180   JvmtiCachedClassFileData* cached_class_file = NULL;
 181 
 182   ClassFileStream* old_stream = stream;
 183 
 184   // Skip this processing for VM anonymous classes
 185   if (host_klass == NULL) {
 186     stream = check_class_file_load_hook(stream,
 187                                         name,
 188                                         loader_data,
 189                                         protection_domain,
 190                                         &cached_class_file,
 191                                         CHECK_NULL);
 192   }
 193 
 194   ClassFileParser parser(stream,
 195                          name,
 196                          loader_data,
 197                          protection_domain,
 198                          host_klass,
 199                          cp_patches,
 200                          ClassFileParser::BROADCAST, // publicity level
 201                          CHECK_NULL);
 202 
 203   instanceKlassHandle result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
 204   assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
 205 
 206   if (result.is_null()) {
 207     return NULL;
 208   }
 209 
 210   if (cached_class_file != NULL) {
 211     // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
 212     result->set_cached_class_file(cached_class_file);
 213   }
 214 
 215   if (InstanceKlass::should_store_fingerprint()) {
 216     result->store_fingerprint(!result->is_anonymous() ? stream->compute_fingerprint() : 0);
 217   }
 218 
 219   TRACE_KLASS_CREATION(result, parser, THREAD);
 220 
 221 #if INCLUDE_CDS && INCLUDE_JVMTI
 222   if (DumpSharedSpaces) {
 223     assert(cached_class_file == NULL, "Sanity");
 224     // Archive the class stream data into the optional data section
 225     JvmtiCachedClassFileData *p;
 226     int len;
 227     const unsigned char *bytes;
 228     // event based tracing might set cached_class_file
 229     if ((bytes = result->get_cached_class_file_bytes()) != NULL) {
 230       len = result->get_cached_class_file_len();
 231     } else {
 232       len = stream->length();
 233       bytes = stream->buffer();
 234     }
 235     p = (JvmtiCachedClassFileData*)MetaspaceShared::optional_data_space_alloc(
 236                     offset_of(JvmtiCachedClassFileData, data) + len);
 237     p->length = len;
 238     memcpy(p->data, bytes, len);
 239     result->set_archived_class_data(p);
 240   }
 241 #endif
 242 
 243   return result;
 244 }