hotspot/src/share/vm/classfile/klassFactory.cpp
author alanb
Tue, 03 May 2016 09:09:20 +0100
changeset 37773 e5b3e9732c3c
parent 36508 5f9eee6b383b
child 40016 bf6fcd467a7b
permissions -rw-r--r--
8154956: Module system implementation refresh (4/2016) Reviewed-by: acorn, jiangli, ccheung, hseigel Contributed-by: alan.bateman@oracle.com, lois.foltan@oracle.com, harold.seigel@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     1
/*
36386
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
     2
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     4
*
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     5
* This code is free software; you can redistribute it and/or modify it
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     6
* under the terms of the GNU General Public License version 2 only, as
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     7
* published by the Free Software Foundation.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     8
*
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
     9
* This code is distributed in the hope that it will be useful, but WITHOUT
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    11
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    12
* version 2 for more details (a copy is included in the LICENSE file that
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    13
* accompanied this code).
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    14
*
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    15
* You should have received a copy of the GNU General Public License version
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    16
* 2 along with this work; if not, write to the Free Software Foundation,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    18
*
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    20
* or visit www.oracle.com if you need additional information or have any
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    21
* questions.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    22
*
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    23
*/
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    24
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    25
#include "precompiled.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    26
#include "classfile/classFileParser.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    27
#include "classfile/classFileStream.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    28
#include "classfile/classLoaderData.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    29
#include "classfile/klassFactory.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    30
#include "memory/resourceArea.hpp"
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    31
#include "prims/jvmtiEnvBase.hpp"
36386
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
    32
#include "trace/traceMacros.hpp"
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    33
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    34
static ClassFileStream* prologue(ClassFileStream* stream,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    35
                                 Symbol* name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    36
                                 ClassLoaderData* loader_data,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    37
                                 Handle protection_domain,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    38
                                 JvmtiCachedClassFileData** cached_class_file,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    39
                                 TRAPS) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    40
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    41
  assert(stream != NULL, "invariant");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    42
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    43
  if (JvmtiExport::should_post_class_file_load_hook()) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    44
    assert(THREAD->is_Java_thread(), "must be a JavaThread");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    45
    const JavaThread* jt = (JavaThread*)THREAD;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    46
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    47
    Handle class_loader(THREAD, loader_data->class_loader());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    48
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    49
    // Get the cached class file bytes (if any) from the class that
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    50
    // is being redefined or retransformed. We use jvmti_thread_state()
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    51
    // instead of JvmtiThreadState::state_for(jt) so we don't allocate
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    52
    // a JvmtiThreadState any earlier than necessary. This will help
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    53
    // avoid the bug described by 7126851.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    54
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    55
    JvmtiThreadState* state = jt->jvmti_thread_state();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    56
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    57
    if (state != NULL) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    58
      KlassHandle* h_class_being_redefined =
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    59
        state->get_class_being_redefined();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    60
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    61
      if (h_class_being_redefined != NULL) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    62
        instanceKlassHandle ikh_class_being_redefined =
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    63
          instanceKlassHandle(THREAD, (*h_class_being_redefined)());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    64
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    65
        *cached_class_file = ikh_class_being_redefined->get_cached_class_file();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    66
      }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    67
    }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    68
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    69
    unsigned char* ptr = const_cast<unsigned char*>(stream->buffer());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    70
    unsigned char* end_ptr = ptr + stream->length();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    71
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    72
    JvmtiExport::post_class_file_load_hook(name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    73
                                           class_loader,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    74
                                           protection_domain,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    75
                                           &ptr,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    76
                                           &end_ptr,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    77
                                           cached_class_file);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    78
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    79
    if (ptr != stream->buffer()) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    80
      // JVMTI agent has modified class file data.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    81
      // Set new class file stream using JVMTI agent modified class file data.
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    82
      stream = new ClassFileStream(ptr,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    83
                                   end_ptr - ptr,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    84
                                   stream->source(),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    85
                                   stream->need_verify());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    86
    }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    87
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    88
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    89
  return stream;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    90
}
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    91
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    92
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    93
instanceKlassHandle KlassFactory::create_from_stream(ClassFileStream* stream,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    94
                                                     Symbol* name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    95
                                                     ClassLoaderData* loader_data,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    96
                                                     Handle protection_domain,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    97
                                                     const Klass* host_klass,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    98
                                                     GrowableArray<Handle>* cp_patches,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
    99
                                                     TempNewSymbol* parsed_name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   100
                                                     TRAPS) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   101
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   102
  assert(stream != NULL, "invariant");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   103
  assert(loader_data != NULL, "invariant");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   104
  assert(THREAD->is_Java_thread(), "must be a JavaThread");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   105
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   106
  bool changed_by_loadhook = false;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   107
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   108
  ResourceMark rm;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   109
  HandleMark hm;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   110
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   111
  JvmtiCachedClassFileData* cached_class_file = NULL;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   112
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   113
  ClassFileStream* old_stream = stream;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   114
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   115
  stream = prologue(stream,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   116
                    name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   117
                    loader_data,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   118
                    protection_domain,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   119
                    &cached_class_file,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   120
                    CHECK_NULL);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   121
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   122
  ClassFileParser parser(stream,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   123
                         name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   124
                         loader_data,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   125
                         protection_domain,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   126
                         parsed_name,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   127
                         host_klass,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   128
                         cp_patches,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   129
                         ClassFileParser::BROADCAST, // publicity level
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   130
                         CHECK_NULL);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   131
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   132
  instanceKlassHandle result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   133
  assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   134
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   135
  if (result.is_null()) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   136
    return NULL;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   137
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   138
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   139
  if (cached_class_file != NULL) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   140
    // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   141
    result->set_cached_class_file(cached_class_file);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   142
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   143
36386
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
   144
  TRACE_KLASS_CREATION(result, parser, THREAD);
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
   145
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   146
  return result;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   147
}