author | jwilhelm |
Thu, 22 Jun 2017 00:51:07 +0200 | |
changeset 46554 | aa1cfd918c4f |
parent 46329 | 53ccc37bda19 |
child 46746 | ea379ebb9447 |
permissions | -rw-r--r-- |
34666 | 1 |
/* |
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
34666 | 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" |
|
41182 | 28 |
#include "classfile/classLoader.hpp" |
34666 | 29 |
#include "classfile/classLoaderData.hpp" |
41182 | 30 |
#include "classfile/classLoaderData.inline.hpp" |
34666 | 31 |
#include "classfile/klassFactory.hpp" |
41182 | 32 |
#include "classfile/sharedClassUtil.hpp" |
33 |
#include "memory/metaspaceShared.hpp" |
|
34666 | 34 |
#include "memory/resourceArea.hpp" |
35 |
#include "prims/jvmtiEnvBase.hpp" |
|
41182 | 36 |
#include "prims/jvmtiRedefineClasses.hpp" |
36386
d7f5e6df26df
8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents:
34666
diff
changeset
|
37 |
#include "trace/traceMacros.hpp" |
34666 | 38 |
|
41182 | 39 |
// called during initial loading of a shared class |
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
40 |
InstanceKlass* KlassFactory::check_shared_class_file_load_hook( |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
41 |
InstanceKlass* ik, |
41182 | 42 |
Symbol* class_name, |
43 |
Handle class_loader, |
|
44 |
Handle protection_domain, TRAPS) { |
|
45 |
#if INCLUDE_CDS && INCLUDE_JVMTI |
|
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
46 |
assert(ik != NULL, "sanity"); |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
47 |
assert(ik->is_shared(), "expecting a shared class"); |
41182 | 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, |
|
41739
4a4b9f6a4306
8167333: Invalid source path info might be used when creating ClassFileStream after CFLH transforms a shared classes in some cases
jiangli
parents:
41182
diff
changeset
|
77 |
ent == NULL ? NULL : ent->_name, |
41182 | 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); |
|
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
87 |
InstanceKlass* new_ik = parser.create_instance_klass(true /* changed_by_loadhook */, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
88 |
CHECK_NULL); |
41182 | 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 |
||
40658
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
107 |
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
|
108 |
Symbol* name, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
109 |
ClassLoaderData* loader_data, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
110 |
Handle protection_domain, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
111 |
JvmtiCachedClassFileData** cached_class_file, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
112 |
TRAPS) { |
34666 | 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) { |
|
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
131 |
Klass* k = state->get_class_being_redefined(); |
34666 | 132 |
|
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
133 |
if (k != NULL) { |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
134 |
InstanceKlass* class_being_redefined = InstanceKlass::cast(k); |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
135 |
*cached_class_file = class_being_redefined->get_cached_class_file(); |
34666 | 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 |
||
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
163 |
InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
164 |
Symbol* name, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
165 |
ClassLoaderData* loader_data, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
166 |
Handle protection_domain, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
167 |
const InstanceKlass* host_klass, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
168 |
GrowableArray<Handle>* cp_patches, |
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
169 |
TRAPS) { |
34666 | 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 |
||
36508 | 179 |
ClassFileStream* old_stream = stream; |
180 |
||
40658
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
181 |
// Skip this processing for VM anonymous classes |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
182 |
if (host_klass == NULL) { |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
183 |
stream = check_class_file_load_hook(stream, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
184 |
name, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
185 |
loader_data, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
186 |
protection_domain, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
187 |
&cached_class_file, |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
188 |
CHECK_NULL); |
50dd5daad1e6
8163973: VM Anonymous classes should not call Class File Load Hooks
rprotacio
parents:
40016
diff
changeset
|
189 |
} |
34666 | 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 |
||
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
200 |
InstanceKlass* result = parser.create_instance_klass(old_stream != stream, CHECK_NULL); |
36508 | 201 |
assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant"); |
34666 | 202 |
|
46329
53ccc37bda19
8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents:
42650
diff
changeset
|
203 |
if (result == NULL) { |
34666 | 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 |
||
42650 | 212 |
if (InstanceKlass::should_store_fingerprint()) { |
213 |
result->store_fingerprint(!result->is_anonymous() ? stream->compute_fingerprint() : 0); |
|
214 |
} |
|
215 |
||
36386
d7f5e6df26df
8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents:
34666
diff
changeset
|
216 |
TRACE_KLASS_CREATION(result, parser, THREAD); |
d7f5e6df26df
8147442: Event-based tracing to allow for tracing Klass creation
mgronlun
parents:
34666
diff
changeset
|
217 |
|
41182 | 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 |
||
34666 | 240 |
return result; |
241 |
} |