hotspot/src/share/vm/classfile/klassFactory.cpp
author rprotacio
Mon, 22 Aug 2016 11:06:18 -0400
changeset 40658 50dd5daad1e6
parent 40016 bf6fcd467a7b
child 40923 10fe1c28b9f6
permissions -rw-r--r--
8163973: VM Anonymous classes should not call Class File Load Hooks Summary: Ensures CFLH's are not called for VM anonymous classes Reviewed-by: lfoltan, dholmes, coleenp, vlivanov, acorn
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
40658
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    34
static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    35
                                                   Symbol* name,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    36
                                                   ClassLoaderData* loader_data,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    37
                                                   Handle protection_domain,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    38
                                                   JvmtiCachedClassFileData** cached_class_file,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
    39
                                                   TRAPS) {
34666
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
                                                     TRAPS) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   100
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   101
  assert(stream != NULL, "invariant");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   102
  assert(loader_data != NULL, "invariant");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   103
  assert(THREAD->is_Java_thread(), "must be a JavaThread");
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   104
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   105
  ResourceMark rm;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   106
  HandleMark hm;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   107
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   108
  JvmtiCachedClassFileData* cached_class_file = NULL;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   109
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   110
  ClassFileStream* old_stream = stream;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   111
40658
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   112
  // Skip this processing for VM anonymous classes
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   113
  if (host_klass == NULL) {
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   114
    stream = check_class_file_load_hook(stream,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   115
                                        name,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   116
                                        loader_data,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   117
                                        protection_domain,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   118
                                        &cached_class_file,
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   119
                                        CHECK_NULL);
50dd5daad1e6 8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents: 40016
diff changeset
   120
  }
34666
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
                         host_klass,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   127
                         cp_patches,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   128
                         ClassFileParser::BROADCAST, // publicity level
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   129
                         CHECK_NULL);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   130
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   131
  instanceKlassHandle result = parser.create_instance_klass(old_stream != stream, CHECK_NULL);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36386
diff changeset
   132
  assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant");
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   133
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   134
  if (result.is_null()) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   135
    return NULL;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   136
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   137
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   138
  if (cached_class_file != NULL) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   139
    // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   140
    result->set_cached_class_file(cached_class_file);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   141
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   142
36386
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
   143
  TRACE_KLASS_CREATION(result, parser, THREAD);
d7f5e6df26df 8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents: 34666
diff changeset
   144
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   145
  return result;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents:
diff changeset
   146
}