Print this page
rev 1021 : 6858164: invokedynamic code needs some cleanup (post-6655638)
Note: The bug ID for this change set was erroneously used to call for review of 6815692.
Summary: Fix several crashers, remove needless paths for boxed-style bootstrap method call, refactor & simplify APIs for rewriter constantPoolOop, remove sun.dyn.CallSiteImpl
Reviewed-by: ?
rev 1024 : imported patch indy-cleanup-6893081.patch
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/classfile/systemDictionary.cpp
+++ new/src/share/vm/classfile/systemDictionary.cpp
1 1 /*
2 2 * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 21 * have any questions.
22 22 *
23 23 */
24 24
25 25 # include "incls/_precompiled.incl"
26 26 # include "incls/_systemDictionary.cpp.incl"
27 27
28 28
29 29 Dictionary* SystemDictionary::_dictionary = NULL;
30 30 PlaceholderTable* SystemDictionary::_placeholders = NULL;
31 31 Dictionary* SystemDictionary::_shared_dictionary = NULL;
32 32 LoaderConstraintTable* SystemDictionary::_loader_constraints = NULL;
33 33 ResolutionErrorTable* SystemDictionary::_resolution_errors = NULL;
34 34 SymbolPropertyTable* SystemDictionary::_invoke_method_table = NULL;
35 35
36 36
37 37 int SystemDictionary::_number_of_modifications = 0;
38 38
39 39 oop SystemDictionary::_system_loader_lock_obj = NULL;
40 40
41 41 klassOop SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
42 42 = { NULL /*, NULL...*/ };
43 43
44 44 klassOop SystemDictionary::_box_klasses[T_VOID+1] = { NULL /*, NULL...*/ };
45 45
46 46 oop SystemDictionary::_java_system_loader = NULL;
47 47
48 48 bool SystemDictionary::_has_loadClassInternal = false;
49 49 bool SystemDictionary::_has_checkPackageAccess = false;
50 50
51 51 // lazily initialized klass variables
52 52 volatile klassOop SystemDictionary::_abstract_ownable_synchronizer_klass = NULL;
53 53
54 54
55 55 // ----------------------------------------------------------------------------
56 56 // Java-level SystemLoader
57 57
58 58 oop SystemDictionary::java_system_loader() {
59 59 return _java_system_loader;
60 60 }
61 61
62 62 void SystemDictionary::compute_java_system_loader(TRAPS) {
63 63 KlassHandle system_klass(THREAD, WK_KLASS(classloader_klass));
64 64 JavaValue result(T_OBJECT);
65 65 JavaCalls::call_static(&result,
66 66 KlassHandle(THREAD, WK_KLASS(classloader_klass)),
67 67 vmSymbolHandles::getSystemClassLoader_name(),
68 68 vmSymbolHandles::void_classloader_signature(),
69 69 CHECK);
70 70
71 71 _java_system_loader = (oop)result.get_jobject();
72 72 }
73 73
74 74
75 75 // ----------------------------------------------------------------------------
76 76 // debugging
77 77
78 78 #ifdef ASSERT
79 79
80 80 // return true if class_name contains no '.' (internal format is '/')
81 81 bool SystemDictionary::is_internal_format(symbolHandle class_name) {
82 82 if (class_name.not_null()) {
83 83 ResourceMark rm;
84 84 char* name = class_name->as_C_string();
85 85 return strchr(name, '.') == NULL;
86 86 } else {
87 87 return true;
88 88 }
89 89 }
90 90
91 91 #endif
92 92
93 93 // ----------------------------------------------------------------------------
94 94 // Parallel class loading check
95 95
96 96 bool SystemDictionary::is_parallelCapable(Handle class_loader) {
97 97 if (UnsyncloadClass || class_loader.is_null()) return true;
98 98 if (AlwaysLockClassLoader) return false;
99 99 return java_lang_Class::parallelCapable(class_loader());
100 100 }
101 101 // ----------------------------------------------------------------------------
102 102 // Resolving of classes
103 103
104 104 // Forwards to resolve_or_null
105 105
106 106 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, TRAPS) {
107 107 klassOop klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
108 108 if (HAS_PENDING_EXCEPTION || klass == NULL) {
109 109 KlassHandle k_h(THREAD, klass);
110 110 // can return a null klass
111 111 klass = handle_resolution_exception(class_name, class_loader, protection_domain, throw_error, k_h, THREAD);
112 112 }
113 113 return klass;
114 114 }
115 115
116 116 klassOop SystemDictionary::handle_resolution_exception(symbolHandle class_name, Handle class_loader, Handle protection_domain, bool throw_error, KlassHandle klass_h, TRAPS) {
117 117 if (HAS_PENDING_EXCEPTION) {
118 118 // If we have a pending exception we forward it to the caller, unless throw_error is true,
119 119 // in which case we have to check whether the pending exception is a ClassNotFoundException,
120 120 // and if so convert it to a NoClassDefFoundError
121 121 // And chain the original ClassNotFoundException
122 122 if (throw_error && PENDING_EXCEPTION->is_a(SystemDictionary::classNotFoundException_klass())) {
123 123 ResourceMark rm(THREAD);
124 124 assert(klass_h() == NULL, "Should not have result with exception pending");
125 125 Handle e(THREAD, PENDING_EXCEPTION);
126 126 CLEAR_PENDING_EXCEPTION;
127 127 THROW_MSG_CAUSE_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string(), e);
128 128 } else {
129 129 return NULL;
130 130 }
131 131 }
132 132 // Class not found, throw appropriate error or exception depending on value of throw_error
133 133 if (klass_h() == NULL) {
134 134 ResourceMark rm(THREAD);
135 135 if (throw_error) {
136 136 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), class_name->as_C_string());
137 137 } else {
138 138 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), class_name->as_C_string());
139 139 }
140 140 }
141 141 return (klassOop)klass_h();
142 142 }
143 143
144 144
145 145 klassOop SystemDictionary::resolve_or_fail(symbolHandle class_name,
146 146 bool throw_error, TRAPS)
147 147 {
148 148 return resolve_or_fail(class_name, Handle(), Handle(), throw_error, THREAD);
149 149 }
150 150
151 151
152 152 // Forwards to resolve_instance_class_or_null
153 153
154 154 klassOop SystemDictionary::resolve_or_null(symbolHandle class_name, Handle class_loader, Handle protection_domain, TRAPS) {
155 155 assert(!THREAD->is_Compiler_thread(), "Can not load classes with the Compiler thread");
156 156 if (FieldType::is_array(class_name())) {
157 157 return resolve_array_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
158 158 } else {
159 159 return resolve_instance_class_or_null(class_name, class_loader, protection_domain, CHECK_NULL);
160 160 }
161 161 }
162 162
163 163 klassOop SystemDictionary::resolve_or_null(symbolHandle class_name, TRAPS) {
164 164 return resolve_or_null(class_name, Handle(), Handle(), THREAD);
165 165 }
166 166
167 167 // Forwards to resolve_instance_class_or_null
168 168
169 169 klassOop SystemDictionary::resolve_array_class_or_null(symbolHandle class_name,
170 170 Handle class_loader,
171 171 Handle protection_domain,
172 172 TRAPS) {
173 173 assert(FieldType::is_array(class_name()), "must be array");
174 174 jint dimension;
175 175 symbolOop object_key;
176 176 klassOop k = NULL;
177 177 // dimension and object_key are assigned as a side-effect of this call
178 178 BasicType t = FieldType::get_array_info(class_name(),
179 179 &dimension,
180 180 &object_key,
181 181 CHECK_NULL);
182 182
183 183 if (t == T_OBJECT) {
184 184 symbolHandle h_key(THREAD, object_key);
185 185 // naked oop "k" is OK here -- we assign back into it
186 186 k = SystemDictionary::resolve_instance_class_or_null(h_key,
187 187 class_loader,
188 188 protection_domain,
189 189 CHECK_NULL);
190 190 if (k != NULL) {
191 191 k = Klass::cast(k)->array_klass(dimension, CHECK_NULL);
192 192 }
193 193 } else {
194 194 k = Universe::typeArrayKlassObj(t);
195 195 k = typeArrayKlass::cast(k)->array_klass(dimension, CHECK_NULL);
196 196 }
197 197 return k;
198 198 }
199 199
200 200
201 201 // Must be called for any super-class or super-interface resolution
202 202 // during class definition to allow class circularity checking
203 203 // super-interface callers:
204 204 // parse_interfaces - for defineClass & jvmtiRedefineClasses
205 205 // super-class callers:
206 206 // ClassFileParser - for defineClass & jvmtiRedefineClasses
207 207 // load_shared_class - while loading a class from shared archive
208 208 // resolve_instance_class_or_null:
209 209 // via: handle_parallel_super_load
210 210 // when resolving a class that has an existing placeholder with
211 211 // a saved superclass [i.e. a defineClass is currently in progress]
212 212 // if another thread is trying to resolve the class, it must do
213 213 // super-class checks on its own thread to catch class circularity
214 214 // This last call is critical in class circularity checking for cases
215 215 // where classloading is delegated to different threads and the
216 216 // classloader lock is released.
217 217 // Take the case: Base->Super->Base
218 218 // 1. If thread T1 tries to do a defineClass of class Base
219 219 // resolve_super_or_fail creates placeholder: T1, Base (super Super)
220 220 // 2. resolve_instance_class_or_null does not find SD or placeholder for Super
221 221 // so it tries to load Super
222 222 // 3. If we load the class internally, or user classloader uses same thread
223 223 // loadClassFromxxx or defineClass via parseClassFile Super ...
224 224 // 3.1 resolve_super_or_fail creates placeholder: T1, Super (super Base)
225 225 // 3.3 resolve_instance_class_or_null Base, finds placeholder for Base
226 226 // 3.4 calls resolve_super_or_fail Base
227 227 // 3.5 finds T1,Base -> throws class circularity
228 228 //OR 4. If T2 tries to resolve Super via defineClass Super ...
229 229 // 4.1 resolve_super_or_fail creates placeholder: T2, Super (super Base)
230 230 // 4.2 resolve_instance_class_or_null Base, finds placeholder for Base (super Super)
231 231 // 4.3 calls resolve_super_or_fail Super in parallel on own thread T2
232 232 // 4.4 finds T2, Super -> throws class circularity
233 233 // Must be called, even if superclass is null, since this is
234 234 // where the placeholder entry is created which claims this
235 235 // thread is loading this class/classloader.
236 236 klassOop SystemDictionary::resolve_super_or_fail(symbolHandle child_name,
237 237 symbolHandle class_name,
238 238 Handle class_loader,
239 239 Handle protection_domain,
240 240 bool is_superclass,
241 241 TRAPS) {
242 242
243 243 // Try to get one of the well-known klasses.
244 244 // They are trusted, and do not participate in circularities.
245 245 if (LinkWellKnownClasses) {
246 246 klassOop k = find_well_known_klass(class_name());
247 247 if (k != NULL) {
248 248 return k;
249 249 }
250 250 }
251 251
252 252 // Double-check, if child class is already loaded, just return super-class,interface
253 253 // Don't add a placedholder if already loaded, i.e. already in system dictionary
254 254 // Make sure there's a placeholder for the *child* before resolving.
255 255 // Used as a claim that this thread is currently loading superclass/classloader
256 256 // Used here for ClassCircularity checks and also for heap verification
257 257 // (every instanceKlass in the heap needs to be in the system dictionary
258 258 // or have a placeholder).
259 259 // Must check ClassCircularity before checking if super class is already loaded
260 260 //
261 261 // We might not already have a placeholder if this child_name was
262 262 // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
263 263 // the name of the class might not be known until the stream is actually
264 264 // parsed.
265 265 // Bugs 4643874, 4715493
266 266 // compute_hash can have a safepoint
267 267
268 268 unsigned int d_hash = dictionary()->compute_hash(child_name, class_loader);
269 269 int d_index = dictionary()->hash_to_index(d_hash);
270 270 unsigned int p_hash = placeholders()->compute_hash(child_name, class_loader);
271 271 int p_index = placeholders()->hash_to_index(p_hash);
272 272 // can't throw error holding a lock
273 273 bool child_already_loaded = false;
274 274 bool throw_circularity_error = false;
275 275 {
276 276 MutexLocker mu(SystemDictionary_lock, THREAD);
277 277 klassOop childk = find_class(d_index, d_hash, child_name, class_loader);
278 278 klassOop quicksuperk;
279 279 // to support // loading: if child done loading, just return superclass
280 280 // if class_name, & class_loader don't match:
281 281 // if initial define, SD update will give LinkageError
282 282 // if redefine: compare_class_versions will give HIERARCHY_CHANGED
283 283 // so we don't throw an exception here.
284 284 // see: nsk redefclass014 & java.lang.instrument Instrument032
285 285 if ((childk != NULL ) && (is_superclass) &&
286 286 ((quicksuperk = instanceKlass::cast(childk)->super()) != NULL) &&
287 287
288 288 ((Klass::cast(quicksuperk)->name() == class_name()) &&
289 289 (Klass::cast(quicksuperk)->class_loader() == class_loader()))) {
290 290 return quicksuperk;
291 291 } else {
292 292 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
293 293 if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
294 294 throw_circularity_error = true;
295 295 }
296 296 }
297 297 if (!throw_circularity_error) {
298 298 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, child_name, class_loader, PlaceholderTable::LOAD_SUPER, class_name, THREAD);
299 299 }
300 300 }
301 301 if (throw_circularity_error) {
302 302 ResourceMark rm(THREAD);
303 303 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), child_name->as_C_string());
304 304 }
305 305
306 306 // java.lang.Object should have been found above
307 307 assert(class_name() != NULL, "null super class for resolving");
308 308 // Resolve the super class or interface, check results on return
309 309 klassOop superk = NULL;
310 310 superk = SystemDictionary::resolve_or_null(class_name,
311 311 class_loader,
312 312 protection_domain,
313 313 THREAD);
314 314
315 315 KlassHandle superk_h(THREAD, superk);
316 316
317 317 // Note: clean up of placeholders currently in callers of
318 318 // resolve_super_or_fail - either at update_dictionary time
319 319 // or on error
320 320 {
321 321 MutexLocker mu(SystemDictionary_lock, THREAD);
322 322 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, class_loader);
323 323 if (probe != NULL) {
324 324 probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER);
325 325 }
326 326 }
327 327 if (HAS_PENDING_EXCEPTION || superk_h() == NULL) {
328 328 // can null superk
329 329 superk_h = KlassHandle(THREAD, handle_resolution_exception(class_name, class_loader, protection_domain, true, superk_h, THREAD));
330 330 }
331 331
332 332 return superk_h();
333 333 }
334 334
335 335 void SystemDictionary::validate_protection_domain(instanceKlassHandle klass,
336 336 Handle class_loader,
337 337 Handle protection_domain,
338 338 TRAPS) {
339 339 if(!has_checkPackageAccess()) return;
340 340
341 341 // Now we have to call back to java to check if the initating class has access
342 342 JavaValue result(T_VOID);
343 343 if (TraceProtectionDomainVerification) {
344 344 // Print out trace information
345 345 tty->print_cr("Checking package access");
346 346 tty->print(" - class loader: "); class_loader()->print_value_on(tty); tty->cr();
347 347 tty->print(" - protection domain: "); protection_domain()->print_value_on(tty); tty->cr();
348 348 tty->print(" - loading: "); klass()->print_value_on(tty); tty->cr();
349 349 }
350 350
351 351 assert(class_loader() != NULL, "should not have non-null protection domain for null classloader");
352 352
353 353 KlassHandle system_loader(THREAD, SystemDictionary::classloader_klass());
354 354 JavaCalls::call_special(&result,
355 355 class_loader,
356 356 system_loader,
357 357 vmSymbolHandles::checkPackageAccess_name(),
358 358 vmSymbolHandles::class_protectiondomain_signature(),
359 359 Handle(THREAD, klass->java_mirror()),
360 360 protection_domain,
361 361 THREAD);
362 362
363 363 if (TraceProtectionDomainVerification) {
364 364 if (HAS_PENDING_EXCEPTION) {
365 365 tty->print_cr(" -> DENIED !!!!!!!!!!!!!!!!!!!!!");
366 366 } else {
367 367 tty->print_cr(" -> granted");
368 368 }
369 369 tty->cr();
370 370 }
371 371
372 372 if (HAS_PENDING_EXCEPTION) return;
373 373
374 374 // If no exception has been thrown, we have validated the protection domain
375 375 // Insert the protection domain of the initiating class into the set.
376 376 {
377 377 // We recalculate the entry here -- we've called out to java since
378 378 // the last time it was calculated.
379 379 symbolHandle kn(THREAD, klass->name());
380 380 unsigned int d_hash = dictionary()->compute_hash(kn, class_loader);
381 381 int d_index = dictionary()->hash_to_index(d_hash);
382 382
383 383 MutexLocker mu(SystemDictionary_lock, THREAD);
384 384 {
385 385 // Note that we have an entry, and entries can be deleted only during GC,
386 386 // so we cannot allow GC to occur while we're holding this entry.
387 387
388 388 // We're using a No_Safepoint_Verifier to catch any place where we
389 389 // might potentially do a GC at all.
390 390 // SystemDictionary::do_unloading() asserts that classes are only
391 391 // unloaded at a safepoint.
392 392 No_Safepoint_Verifier nosafepoint;
393 393 dictionary()->add_protection_domain(d_index, d_hash, klass, class_loader,
394 394 protection_domain, THREAD);
395 395 }
396 396 }
397 397 }
398 398
399 399 // We only get here if this thread finds that another thread
400 400 // has already claimed the placeholder token for the current operation,
401 401 // but that other thread either never owned or gave up the
402 402 // object lock
403 403 // Waits on SystemDictionary_lock to indicate placeholder table updated
404 404 // On return, caller must recheck placeholder table state
405 405 //
406 406 // We only get here if
407 407 // 1) custom classLoader, i.e. not bootstrap classloader
408 408 // 2) UnsyncloadClass not set
409 409 // 3) custom classLoader has broken the class loader objectLock
410 410 // so another thread got here in parallel
411 411 //
412 412 // lockObject must be held.
413 413 // Complicated dance due to lock ordering:
414 414 // Must first release the classloader object lock to
415 415 // allow initial definer to complete the class definition
416 416 // and to avoid deadlock
417 417 // Reclaim classloader lock object with same original recursion count
418 418 // Must release SystemDictionary_lock after notify, since
419 419 // class loader lock must be claimed before SystemDictionary_lock
420 420 // to prevent deadlocks
421 421 //
422 422 // The notify allows applications that did an untimed wait() on
423 423 // the classloader object lock to not hang.
424 424 void SystemDictionary::double_lock_wait(Handle lockObject, TRAPS) {
425 425 assert_lock_strong(SystemDictionary_lock);
426 426
427 427 bool calledholdinglock
428 428 = ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, lockObject);
429 429 assert(calledholdinglock,"must hold lock for notify");
430 430 assert((!(lockObject() == _system_loader_lock_obj) && !is_parallelCapable(lockObject)), "unexpected double_lock_wait");
431 431 ObjectSynchronizer::notifyall(lockObject, THREAD);
432 432 intptr_t recursions = ObjectSynchronizer::complete_exit(lockObject, THREAD);
433 433 SystemDictionary_lock->wait();
434 434 SystemDictionary_lock->unlock();
435 435 ObjectSynchronizer::reenter(lockObject, recursions, THREAD);
436 436 SystemDictionary_lock->lock();
437 437 }
438 438
439 439 // If the class in is in the placeholder table, class loading is in progress
440 440 // For cases where the application changes threads to load classes, it
441 441 // is critical to ClassCircularity detection that we try loading
442 442 // the superclass on the same thread internally, so we do parallel
443 443 // super class loading here.
444 444 // This also is critical in cases where the original thread gets stalled
445 445 // even in non-circularity situations.
446 446 // Note: only one thread can define the class, but multiple can resolve
447 447 // Note: must call resolve_super_or_fail even if null super -
448 448 // to force placeholder entry creation for this class for circularity detection
449 449 // Caller must check for pending exception
450 450 // Returns non-null klassOop if other thread has completed load
451 451 // and we are done,
452 452 // If return null klassOop and no pending exception, the caller must load the class
453 453 instanceKlassHandle SystemDictionary::handle_parallel_super_load(
454 454 symbolHandle name, symbolHandle superclassname, Handle class_loader,
455 455 Handle protection_domain, Handle lockObject, TRAPS) {
456 456
457 457 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
458 458 unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
459 459 int d_index = dictionary()->hash_to_index(d_hash);
460 460 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
461 461 int p_index = placeholders()->hash_to_index(p_hash);
462 462
463 463 // superk is not used, resolve_super called for circularity check only
464 464 // This code is reached in two situations. One if this thread
465 465 // is loading the same class twice (e.g. ClassCircularity, or
466 466 // java.lang.instrument).
467 467 // The second is if another thread started the resolve_super first
468 468 // and has not yet finished.
469 469 // In both cases the original caller will clean up the placeholder
470 470 // entry on error.
471 471 klassOop superk = SystemDictionary::resolve_super_or_fail(name,
472 472 superclassname,
473 473 class_loader,
474 474 protection_domain,
475 475 true,
476 476 CHECK_(nh));
477 477 // We don't redefine the class, so we just need to clean up if there
478 478 // was not an error (don't want to modify any system dictionary
479 479 // data structures).
480 480 {
481 481 MutexLocker mu(SystemDictionary_lock, THREAD);
482 482 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
483 483 SystemDictionary_lock->notify_all();
484 484 }
485 485
486 486 // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
487 487 // Serial class loaders and bootstrap classloader do wait for superclass loads
488 488 if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
489 489 MutexLocker mu(SystemDictionary_lock, THREAD);
490 490 // Check if classloading completed while we were loading superclass or waiting
491 491 klassOop check = find_class(d_index, d_hash, name, class_loader);
492 492 if (check != NULL) {
493 493 // Klass is already loaded, so just return it
494 494 return(instanceKlassHandle(THREAD, check));
495 495 } else {
496 496 return nh;
497 497 }
498 498 }
499 499
500 500 // must loop to both handle other placeholder updates
501 501 // and spurious notifications
502 502 bool super_load_in_progress = true;
503 503 PlaceholderEntry* placeholder;
504 504 while (super_load_in_progress) {
505 505 MutexLocker mu(SystemDictionary_lock, THREAD);
506 506 // Check if classloading completed while we were loading superclass or waiting
507 507 klassOop check = find_class(d_index, d_hash, name, class_loader);
508 508 if (check != NULL) {
509 509 // Klass is already loaded, so just return it
510 510 return(instanceKlassHandle(THREAD, check));
511 511 } else {
512 512 placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
513 513 if (placeholder && placeholder->super_load_in_progress() ){
514 514 // Before UnsyncloadClass:
515 515 // We only get here if the application has released the
516 516 // classloader lock when another thread was in the middle of loading a
517 517 // superclass/superinterface for this class, and now
518 518 // this thread is also trying to load this class.
519 519 // To minimize surprises, the first thread that started to
520 520 // load a class should be the one to complete the loading
521 521 // with the classfile it initially expected.
522 522 // This logic has the current thread wait once it has done
523 523 // all the superclass/superinterface loading it can, until
524 524 // the original thread completes the class loading or fails
525 525 // If it completes we will use the resulting instanceKlass
526 526 // which we will find below in the systemDictionary.
527 527 // We also get here for parallel bootstrap classloader
528 528 if (class_loader.is_null()) {
529 529 SystemDictionary_lock->wait();
530 530 } else {
531 531 double_lock_wait(lockObject, THREAD);
532 532 }
533 533 } else {
534 534 // If not in SD and not in PH, other thread's load must have failed
535 535 super_load_in_progress = false;
536 536 }
537 537 }
538 538 }
539 539 return (nh);
540 540 }
541 541
542 542
543 543 klassOop SystemDictionary::resolve_instance_class_or_null(symbolHandle class_name, Handle class_loader, Handle protection_domain, TRAPS) {
544 544 assert(class_name.not_null() && !FieldType::is_array(class_name()), "invalid class name");
545 545 // First check to see if we should remove wrapping L and ;
546 546 symbolHandle name;
547 547 if (FieldType::is_obj(class_name())) {
548 548 ResourceMark rm(THREAD);
549 549 // Ignore wrapping L and ;.
550 550 name = oopFactory::new_symbol_handle(class_name()->as_C_string() + 1, class_name()->utf8_length() - 2, CHECK_NULL);
551 551 } else {
552 552 name = class_name;
553 553 }
554 554
555 555 // UseNewReflection
556 556 // Fix for 4474172; see evaluation for more details
557 557 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
558 558
559 559 // Do lookup to see if class already exist and the protection domain
560 560 // has the right access
561 561 unsigned int d_hash = dictionary()->compute_hash(name, class_loader);
562 562 int d_index = dictionary()->hash_to_index(d_hash);
563 563 klassOop probe = dictionary()->find(d_index, d_hash, name, class_loader,
564 564 protection_domain, THREAD);
565 565 if (probe != NULL) return probe;
566 566
567 567
568 568 // Non-bootstrap class loaders will call out to class loader and
569 569 // define via jvm/jni_DefineClass which will acquire the
570 570 // class loader object lock to protect against multiple threads
571 571 // defining the class in parallel by accident.
572 572 // This lock must be acquired here so the waiter will find
573 573 // any successful result in the SystemDictionary and not attempt
574 574 // the define
575 575 // ParallelCapable Classloaders and the bootstrap classloader,
576 576 // or all classloaders with UnsyncloadClass do not acquire lock here
577 577 bool DoObjectLock = true;
578 578 if (is_parallelCapable(class_loader)) {
579 579 DoObjectLock = false;
580 580 }
581 581
582 582 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
583 583 int p_index = placeholders()->hash_to_index(p_hash);
584 584
585 585 // Class is not in SystemDictionary so we have to do loading.
586 586 // Make sure we are synchronized on the class loader before we proceed
587 587 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
588 588 check_loader_lock_contention(lockObject, THREAD);
589 589 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
590 590
591 591 // Check again (after locking) if class already exist in SystemDictionary
592 592 bool class_has_been_loaded = false;
593 593 bool super_load_in_progress = false;
594 594 bool havesupername = false;
595 595 instanceKlassHandle k;
596 596 PlaceholderEntry* placeholder;
597 597 symbolHandle superclassname;
598 598
599 599 {
600 600 MutexLocker mu(SystemDictionary_lock, THREAD);
601 601 klassOop check = find_class(d_index, d_hash, name, class_loader);
602 602 if (check != NULL) {
603 603 // Klass is already loaded, so just return it
604 604 class_has_been_loaded = true;
605 605 k = instanceKlassHandle(THREAD, check);
606 606 } else {
607 607 placeholder = placeholders()->get_entry(p_index, p_hash, name, class_loader);
608 608 if (placeholder && placeholder->super_load_in_progress()) {
609 609 super_load_in_progress = true;
610 610 if (placeholder->havesupername() == true) {
611 611 superclassname = symbolHandle(THREAD, placeholder->supername());
612 612 havesupername = true;
613 613 }
614 614 }
615 615 }
616 616 }
617 617
618 618 // If the class in is in the placeholder table, class loading is in progress
619 619 if (super_load_in_progress && havesupername==true) {
620 620 k = SystemDictionary::handle_parallel_super_load(name, superclassname,
621 621 class_loader, protection_domain, lockObject, THREAD);
622 622 if (HAS_PENDING_EXCEPTION) {
623 623 return NULL;
624 624 }
625 625 if (!k.is_null()) {
626 626 class_has_been_loaded = true;
627 627 }
628 628 }
629 629
630 630 if (!class_has_been_loaded) {
631 631
632 632 // add placeholder entry to record loading instance class
633 633 // Five cases:
634 634 // All cases need to prevent modifying bootclasssearchpath
635 635 // in parallel with a classload of same classname
636 636 // Redefineclasses uses existence of the placeholder for the duration
637 637 // of the class load to prevent concurrent redefinition of not completely
638 638 // defined classes.
639 639 // case 1. traditional classloaders that rely on the classloader object lock
640 640 // - no other need for LOAD_INSTANCE
641 641 // case 2. traditional classloaders that break the classloader object lock
642 642 // as a deadlock workaround. Detection of this case requires that
643 643 // this check is done while holding the classloader object lock,
644 644 // and that lock is still held when calling classloader's loadClass.
645 645 // For these classloaders, we ensure that the first requestor
646 646 // completes the load and other requestors wait for completion.
647 647 // case 3. UnsyncloadClass - don't use objectLocker
648 648 // With this flag, we allow parallel classloading of a
649 649 // class/classloader pair
650 650 // case4. Bootstrap classloader - don't own objectLocker
651 651 // This classloader supports parallelism at the classloader level,
652 652 // but only allows a single load of a class/classloader pair.
653 653 // No performance benefit and no deadlock issues.
654 654 // case 5. parallelCapable user level classloaders - without objectLocker
655 655 // Allow parallel classloading of a class/classloader pair
656 656 symbolHandle nullsymbolHandle;
657 657 bool throw_circularity_error = false;
658 658 {
659 659 MutexLocker mu(SystemDictionary_lock, THREAD);
660 660 if (class_loader.is_null() || !is_parallelCapable(class_loader)) {
661 661 PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
662 662 if (oldprobe) {
663 663 // only need check_seen_thread once, not on each loop
664 664 // 6341374 java/lang/Instrument with -Xcomp
665 665 if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
666 666 throw_circularity_error = true;
667 667 } else {
668 668 // case 1: traditional: should never see load_in_progress.
669 669 while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
670 670
671 671 // case 4: bootstrap classloader: prevent futile classloading,
672 672 // wait on first requestor
673 673 if (class_loader.is_null()) {
674 674 SystemDictionary_lock->wait();
675 675 } else {
676 676 // case 2: traditional with broken classloader lock. wait on first
677 677 // requestor.
678 678 double_lock_wait(lockObject, THREAD);
679 679 }
680 680 // Check if classloading completed while we were waiting
681 681 klassOop check = find_class(d_index, d_hash, name, class_loader);
682 682 if (check != NULL) {
683 683 // Klass is already loaded, so just return it
684 684 k = instanceKlassHandle(THREAD, check);
685 685 class_has_been_loaded = true;
686 686 }
687 687 // check if other thread failed to load and cleaned up
688 688 oldprobe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
689 689 }
690 690 }
691 691 }
692 692 }
693 693 // All cases: add LOAD_INSTANCE
694 694 // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
695 695 // LOAD_INSTANCE in parallel
696 696 // add placeholder entry even if error - callers will remove on error
697 697 if (!throw_circularity_error && !class_has_been_loaded) {
698 698 PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, class_loader, PlaceholderTable::LOAD_INSTANCE, nullsymbolHandle, THREAD);
699 699 // For class loaders that do not acquire the classloader object lock,
700 700 // if they did not catch another thread holding LOAD_INSTANCE,
701 701 // need a check analogous to the acquire ObjectLocker/find_class
702 702 // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
703 703 // one final check if the load has already completed
704 704 // class loaders holding the ObjectLock shouldn't find the class here
705 705 klassOop check = find_class(d_index, d_hash, name, class_loader);
706 706 if (check != NULL) {
707 707 // Klass is already loaded, so just return it
708 708 k = instanceKlassHandle(THREAD, check);
709 709 class_has_been_loaded = true;
710 710 newprobe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
711 711 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
712 712 SystemDictionary_lock->notify_all();
713 713 }
714 714 }
715 715 }
716 716 // must throw error outside of owning lock
717 717 if (throw_circularity_error) {
718 718 ResourceMark rm(THREAD);
719 719 THROW_MSG_0(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
720 720 }
721 721
722 722 if (!class_has_been_loaded) {
723 723
724 724 // Do actual loading
725 725 k = load_instance_class(name, class_loader, THREAD);
726 726
727 727 // For UnsyncloadClass and AllowParallelDefineClass only:
728 728 // If they got a linkageError, check if a parallel class load succeeded.
729 729 // If it did, then for bytecode resolution the specification requires
730 730 // that we return the same result we did for the other thread, i.e. the
731 731 // successfully loaded instanceKlass
732 732 // Should not get here for classloaders that support parallelism
733 733 // with the new cleaner mechanism
734 734 // Bootstrap goes through here to allow for an extra guarantee check
735 735 if (UnsyncloadClass || (class_loader.is_null())) {
736 736 if (k.is_null() && HAS_PENDING_EXCEPTION
737 737 && PENDING_EXCEPTION->is_a(SystemDictionary::linkageError_klass())) {
738 738 MutexLocker mu(SystemDictionary_lock, THREAD);
739 739 klassOop check = find_class(d_index, d_hash, name, class_loader);
740 740 if (check != NULL) {
741 741 // Klass is already loaded, so just use it
742 742 k = instanceKlassHandle(THREAD, check);
743 743 CLEAR_PENDING_EXCEPTION;
744 744 guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
745 745 }
746 746 }
747 747 }
748 748
749 749 // clean up placeholder entries for success or error
750 750 // This cleans up LOAD_INSTANCE entries
751 751 // It also cleans up LOAD_SUPER entries on errors from
752 752 // calling load_instance_class
753 753 {
754 754 MutexLocker mu(SystemDictionary_lock, THREAD);
755 755 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name, class_loader);
756 756 if (probe != NULL) {
757 757 probe->remove_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE);
758 758 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
759 759 SystemDictionary_lock->notify_all();
760 760 }
761 761 }
762 762
763 763 // If everything was OK (no exceptions, no null return value), and
764 764 // class_loader is NOT the defining loader, do a little more bookkeeping.
765 765 if (!HAS_PENDING_EXCEPTION && !k.is_null() &&
766 766 k->class_loader() != class_loader()) {
767 767
768 768 check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
769 769
770 770 // Need to check for a PENDING_EXCEPTION again; check_constraints
771 771 // can throw and doesn't use the CHECK macro.
772 772 if (!HAS_PENDING_EXCEPTION) {
773 773 { // Grabbing the Compile_lock prevents systemDictionary updates
774 774 // during compilations.
775 775 MutexLocker mu(Compile_lock, THREAD);
776 776 update_dictionary(d_index, d_hash, p_index, p_hash,
777 777 k, class_loader, THREAD);
778 778 }
779 779 if (JvmtiExport::should_post_class_load()) {
780 780 Thread *thread = THREAD;
781 781 assert(thread->is_Java_thread(), "thread->is_Java_thread()");
782 782 JvmtiExport::post_class_load((JavaThread *) thread, k());
783 783 }
784 784 }
785 785 }
786 786 if (HAS_PENDING_EXCEPTION || k.is_null()) {
787 787 // On error, clean up placeholders
788 788 {
789 789 MutexLocker mu(SystemDictionary_lock, THREAD);
790 790 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
791 791 SystemDictionary_lock->notify_all();
792 792 }
793 793 return NULL;
794 794 }
795 795 }
796 796 }
797 797
798 798 #ifdef ASSERT
799 799 {
800 800 Handle loader (THREAD, k->class_loader());
801 801 MutexLocker mu(SystemDictionary_lock, THREAD);
802 802 oop kk = find_class_or_placeholder(name, loader);
803 803 assert(kk == k(), "should be present in dictionary");
804 804 }
805 805 #endif
806 806
807 807 // return if the protection domain in NULL
808 808 if (protection_domain() == NULL) return k();
809 809
810 810 // Check the protection domain has the right access
811 811 {
812 812 MutexLocker mu(SystemDictionary_lock, THREAD);
813 813 // Note that we have an entry, and entries can be deleted only during GC,
814 814 // so we cannot allow GC to occur while we're holding this entry.
815 815 // We're using a No_Safepoint_Verifier to catch any place where we
816 816 // might potentially do a GC at all.
817 817 // SystemDictionary::do_unloading() asserts that classes are only
818 818 // unloaded at a safepoint.
819 819 No_Safepoint_Verifier nosafepoint;
820 820 if (dictionary()->is_valid_protection_domain(d_index, d_hash, name,
821 821 class_loader,
822 822 protection_domain)) {
823 823 return k();
824 824 }
825 825 }
826 826
827 827 // Verify protection domain. If it fails an exception is thrown
828 828 validate_protection_domain(k, class_loader, protection_domain, CHECK_(klassOop(NULL)));
829 829
830 830 return k();
831 831 }
832 832
833 833
834 834 // This routine does not lock the system dictionary.
835 835 //
836 836 // Since readers don't hold a lock, we must make sure that system
837 837 // dictionary entries are only removed at a safepoint (when only one
838 838 // thread is running), and are added to in a safe way (all links must
839 839 // be updated in an MT-safe manner).
840 840 //
841 841 // Callers should be aware that an entry could be added just after
842 842 // _dictionary->bucket(index) is read here, so the caller will not see
843 843 // the new entry.
844 844
845 845 klassOop SystemDictionary::find(symbolHandle class_name,
846 846 Handle class_loader,
847 847 Handle protection_domain,
848 848 TRAPS) {
849 849
850 850 // UseNewReflection
851 851 // The result of this call should be consistent with the result
852 852 // of the call to resolve_instance_class_or_null().
853 853 // See evaluation 6790209 and 4474172 for more details.
854 854 class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
855 855
856 856 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
857 857 int d_index = dictionary()->hash_to_index(d_hash);
858 858
859 859 {
860 860 // Note that we have an entry, and entries can be deleted only during GC,
861 861 // so we cannot allow GC to occur while we're holding this entry.
862 862 // We're using a No_Safepoint_Verifier to catch any place where we
863 863 // might potentially do a GC at all.
864 864 // SystemDictionary::do_unloading() asserts that classes are only
865 865 // unloaded at a safepoint.
866 866 No_Safepoint_Verifier nosafepoint;
867 867 return dictionary()->find(d_index, d_hash, class_name, class_loader,
868 868 protection_domain, THREAD);
869 869 }
870 870 }
871 871
872 872
873 873 // Look for a loaded instance or array klass by name. Do not do any loading.
874 874 // return NULL in case of error.
875 875 klassOop SystemDictionary::find_instance_or_array_klass(symbolHandle class_name,
876 876 Handle class_loader,
877 877 Handle protection_domain,
878 878 TRAPS) {
879 879 klassOop k = NULL;
880 880 assert(class_name() != NULL, "class name must be non NULL");
881 881
882 882 // Try to get one of the well-known klasses.
883 883 if (LinkWellKnownClasses) {
884 884 k = find_well_known_klass(class_name());
885 885 if (k != NULL) {
886 886 return k;
887 887 }
888 888 }
889 889
890 890 if (FieldType::is_array(class_name())) {
891 891 // The name refers to an array. Parse the name.
892 892 jint dimension;
893 893 symbolOop object_key;
894 894
895 895 // dimension and object_key are assigned as a side-effect of this call
896 896 BasicType t = FieldType::get_array_info(class_name(), &dimension,
897 897 &object_key, CHECK_(NULL));
898 898 if (t != T_OBJECT) {
899 899 k = Universe::typeArrayKlassObj(t);
900 900 } else {
901 901 symbolHandle h_key(THREAD, object_key);
902 902 k = SystemDictionary::find(h_key, class_loader, protection_domain, THREAD);
903 903 }
904 904 if (k != NULL) {
905 905 k = Klass::cast(k)->array_klass_or_null(dimension);
906 906 }
907 907 } else {
908 908 k = find(class_name, class_loader, protection_domain, THREAD);
909 909 }
910 910 return k;
911 911 }
912 912
913 913 // Quick range check for names of well-known classes:
914 914 static symbolOop wk_klass_name_limits[2] = {NULL, NULL};
915 915
916 916 #ifndef PRODUCT
917 917 static int find_wkk_calls, find_wkk_probes, find_wkk_wins;
918 918 // counts for "hello world": 3983, 1616, 1075
919 919 // => 60% hit after limit guard, 25% total win rate
920 920 #endif
921 921
922 922 klassOop SystemDictionary::find_well_known_klass(symbolOop class_name) {
923 923 // A bounds-check on class_name will quickly get a negative result.
924 924 NOT_PRODUCT(find_wkk_calls++);
925 925 if (class_name >= wk_klass_name_limits[0] &&
926 926 class_name <= wk_klass_name_limits[1]) {
927 927 NOT_PRODUCT(find_wkk_probes++);
928 928 vmSymbols::SID sid = vmSymbols::find_sid(class_name);
929 929 if (sid != vmSymbols::NO_SID) {
930 930 klassOop k = NULL;
931 931 switch (sid) {
932 932 #define WK_KLASS_CASE(name, symbol, ignore_option) \
933 933 case vmSymbols::VM_SYMBOL_ENUM_NAME(symbol): \
934 934 k = WK_KLASS(name); break;
935 935 WK_KLASSES_DO(WK_KLASS_CASE)
936 936 #undef WK_KLASS_CASE
937 937 }
938 938 NOT_PRODUCT(if (k != NULL) find_wkk_wins++);
939 939 return k;
940 940 }
941 941 }
942 942 return NULL;
943 943 }
944 944
945 945 // Note: this method is much like resolve_from_stream, but
946 946 // updates no supplemental data structures.
947 947 // TODO consolidate the two methods with a helper routine?
948 948 klassOop SystemDictionary::parse_stream(symbolHandle class_name,
949 949 Handle class_loader,
950 950 Handle protection_domain,
951 951 ClassFileStream* st,
952 952 KlassHandle host_klass,
953 953 GrowableArray<Handle>* cp_patches,
954 954 TRAPS) {
955 955 symbolHandle parsed_name;
956 956
957 957 // Parse the stream. Note that we do this even though this klass might
958 958 // already be present in the SystemDictionary, otherwise we would not
959 959 // throw potential ClassFormatErrors.
960 960 //
961 961 // Note: "name" is updated.
962 962 // Further note: a placeholder will be added for this class when
963 963 // super classes are loaded (resolve_super_or_fail). We expect this
964 964 // to be called for all classes but java.lang.Object; and we preload
965 965 // java.lang.Object through resolve_or_fail, not this path.
966 966
967 967 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
968 968 class_loader,
969 969 protection_domain,
970 970 host_klass,
971 971 cp_patches,
972 972 parsed_name,
973 973 true,
974 974 THREAD);
975 975
976 976
977 977 // We don't redefine the class, so we just need to clean up whether there
978 978 // was an error or not (don't want to modify any system dictionary
979 979 // data structures).
980 980 // Parsed name could be null if we threw an error before we got far
981 981 // enough along to parse it -- in that case, there is nothing to clean up.
982 982 if (!parsed_name.is_null()) {
983 983 unsigned int p_hash = placeholders()->compute_hash(parsed_name,
984 984 class_loader);
985 985 int p_index = placeholders()->hash_to_index(p_hash);
986 986 {
987 987 MutexLocker mu(SystemDictionary_lock, THREAD);
988 988 placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
989 989 SystemDictionary_lock->notify_all();
990 990 }
991 991 }
992 992
993 993 if (host_klass.not_null() && k.not_null()) {
994 994 assert(AnonymousClasses, "");
995 995 // If it's anonymous, initialize it now, since nobody else will.
996 996 k->set_host_klass(host_klass());
997 997
998 998 {
999 999 MutexLocker mu_r(Compile_lock, THREAD);
1000 1000
1001 1001 // Add to class hierarchy, initialize vtables, and do possible
1002 1002 // deoptimizations.
1003 1003 add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1004 1004
1005 1005 // But, do not add to system dictionary.
1006 1006 }
1007 1007
1008 1008 k->eager_initialize(THREAD);
1009 1009
1010 1010 // notify jvmti
1011 1011 if (JvmtiExport::should_post_class_load()) {
1012 1012 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1013 1013 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1014 1014 }
1015 1015 }
1016 1016
1017 1017 return k();
1018 1018 }
1019 1019
1020 1020 // Add a klass to the system from a stream (called by jni_DefineClass and
1021 1021 // JVM_DefineClass).
1022 1022 // Note: class_name can be NULL. In that case we do not know the name of
1023 1023 // the class until we have parsed the stream.
1024 1024
1025 1025 klassOop SystemDictionary::resolve_from_stream(symbolHandle class_name,
1026 1026 Handle class_loader,
1027 1027 Handle protection_domain,
1028 1028 ClassFileStream* st,
1029 1029 bool verify,
1030 1030 TRAPS) {
1031 1031
1032 1032 // Classloaders that support parallelism, e.g. bootstrap classloader,
1033 1033 // or all classloaders with UnsyncloadClass do not acquire lock here
1034 1034 bool DoObjectLock = true;
1035 1035 if (is_parallelCapable(class_loader)) {
1036 1036 DoObjectLock = false;
1037 1037 }
1038 1038
1039 1039 // Make sure we are synchronized on the class loader before we proceed
1040 1040 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1041 1041 check_loader_lock_contention(lockObject, THREAD);
1042 1042 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1043 1043
1044 1044 symbolHandle parsed_name;
1045 1045
1046 1046 // Parse the stream. Note that we do this even though this klass might
1047 1047 // already be present in the SystemDictionary, otherwise we would not
1048 1048 // throw potential ClassFormatErrors.
1049 1049 //
1050 1050 // Note: "name" is updated.
1051 1051 // Further note: a placeholder will be added for this class when
1052 1052 // super classes are loaded (resolve_super_or_fail). We expect this
1053 1053 // to be called for all classes but java.lang.Object; and we preload
1054 1054 // java.lang.Object through resolve_or_fail, not this path.
1055 1055
1056 1056 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1057 1057 class_loader,
1058 1058 protection_domain,
1059 1059 parsed_name,
1060 1060 verify,
1061 1061 THREAD);
1062 1062
1063 1063 const char* pkg = "java/";
1064 1064 if (!HAS_PENDING_EXCEPTION &&
1065 1065 !class_loader.is_null() &&
1066 1066 !parsed_name.is_null() &&
1067 1067 !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
1068 1068 // It is illegal to define classes in the "java." package from
1069 1069 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1070 1070 ResourceMark rm(THREAD);
1071 1071 char* name = parsed_name->as_C_string();
1072 1072 char* index = strrchr(name, '/');
1073 1073 *index = '\0'; // chop to just the package name
1074 1074 while ((index = strchr(name, '/')) != NULL) {
1075 1075 *index = '.'; // replace '/' with '.' in package name
1076 1076 }
1077 1077 const char* fmt = "Prohibited package name: %s";
1078 1078 size_t len = strlen(fmt) + strlen(name);
1079 1079 char* message = NEW_RESOURCE_ARRAY(char, len);
1080 1080 jio_snprintf(message, len, fmt, name);
1081 1081 Exceptions::_throw_msg(THREAD_AND_LOCATION,
1082 1082 vmSymbols::java_lang_SecurityException(), message);
1083 1083 }
1084 1084
1085 1085 if (!HAS_PENDING_EXCEPTION) {
1086 1086 assert(!parsed_name.is_null(), "Sanity");
1087 1087 assert(class_name.is_null() || class_name() == parsed_name(),
1088 1088 "name mismatch");
1089 1089 // Verification prevents us from creating names with dots in them, this
1090 1090 // asserts that that's the case.
1091 1091 assert(is_internal_format(parsed_name),
1092 1092 "external class name format used internally");
1093 1093
1094 1094 // Add class just loaded
1095 1095 // If a class loader supports parallel classloading handle parallel define requests
1096 1096 // find_or_define_instance_class may return a different instanceKlass
1097 1097 if (is_parallelCapable(class_loader)) {
1098 1098 k = find_or_define_instance_class(class_name, class_loader, k, THREAD);
1099 1099 } else {
1100 1100 define_instance_class(k, THREAD);
1101 1101 }
1102 1102 }
1103 1103
1104 1104 // If parsing the class file or define_instance_class failed, we
1105 1105 // need to remove the placeholder added on our behalf. But we
1106 1106 // must make sure parsed_name is valid first (it won't be if we had
1107 1107 // a format error before the class was parsed far enough to
1108 1108 // find the name).
1109 1109 if (HAS_PENDING_EXCEPTION && !parsed_name.is_null()) {
1110 1110 unsigned int p_hash = placeholders()->compute_hash(parsed_name,
1111 1111 class_loader);
1112 1112 int p_index = placeholders()->hash_to_index(p_hash);
1113 1113 {
1114 1114 MutexLocker mu(SystemDictionary_lock, THREAD);
1115 1115 placeholders()->find_and_remove(p_index, p_hash, parsed_name, class_loader, THREAD);
1116 1116 SystemDictionary_lock->notify_all();
1117 1117 }
1118 1118 return NULL;
1119 1119 }
1120 1120
1121 1121 // Make sure that we didn't leave a place holder in the
1122 1122 // SystemDictionary; this is only done on success
1123 1123 debug_only( {
1124 1124 if (!HAS_PENDING_EXCEPTION) {
1125 1125 assert(!parsed_name.is_null(), "parsed_name is still null?");
1126 1126 symbolHandle h_name (THREAD, k->name());
1127 1127 Handle h_loader (THREAD, k->class_loader());
1128 1128
1129 1129 MutexLocker mu(SystemDictionary_lock, THREAD);
1130 1130
1131 1131 oop check = find_class_or_placeholder(parsed_name, class_loader);
1132 1132 assert(check == k(), "should be present in the dictionary");
1133 1133
1134 1134 oop check2 = find_class_or_placeholder(h_name, h_loader);
1135 1135 assert(check == check2, "name inconsistancy in SystemDictionary");
1136 1136 }
1137 1137 } );
1138 1138
1139 1139 return k();
1140 1140 }
1141 1141
1142 1142
1143 1143 void SystemDictionary::set_shared_dictionary(HashtableBucket* t, int length,
1144 1144 int number_of_entries) {
1145 1145 assert(length == _nof_buckets * sizeof(HashtableBucket),
1146 1146 "bad shared dictionary size.");
1147 1147 _shared_dictionary = new Dictionary(_nof_buckets, t, number_of_entries);
1148 1148 }
1149 1149
1150 1150
1151 1151 // If there is a shared dictionary, then find the entry for the
1152 1152 // given shared system class, if any.
1153 1153
1154 1154 klassOop SystemDictionary::find_shared_class(symbolHandle class_name) {
1155 1155 if (shared_dictionary() != NULL) {
1156 1156 unsigned int d_hash = dictionary()->compute_hash(class_name, Handle());
1157 1157 int d_index = dictionary()->hash_to_index(d_hash);
1158 1158 return shared_dictionary()->find_shared_class(d_index, d_hash, class_name);
1159 1159 } else {
1160 1160 return NULL;
1161 1161 }
1162 1162 }
1163 1163
1164 1164
1165 1165 // Load a class from the shared spaces (found through the shared system
1166 1166 // dictionary). Force the superclass and all interfaces to be loaded.
1167 1167 // Update the class definition to include sibling classes and no
1168 1168 // subclasses (yet). [Classes in the shared space are not part of the
1169 1169 // object hierarchy until loaded.]
1170 1170
1171 1171 instanceKlassHandle SystemDictionary::load_shared_class(
1172 1172 symbolHandle class_name, Handle class_loader, TRAPS) {
1173 1173 instanceKlassHandle ik (THREAD, find_shared_class(class_name));
1174 1174 return load_shared_class(ik, class_loader, THREAD);
1175 1175 }
1176 1176
1177 1177 // Note well! Changes to this method may affect oop access order
1178 1178 // in the shared archive. Please take care to not make changes that
1179 1179 // adversely affect cold start time by changing the oop access order
1180 1180 // that is specified in dump.cpp MarkAndMoveOrderedReadOnly and
1181 1181 // MarkAndMoveOrderedReadWrite closures.
1182 1182 instanceKlassHandle SystemDictionary::load_shared_class(
1183 1183 instanceKlassHandle ik, Handle class_loader, TRAPS) {
1184 1184 assert(class_loader.is_null(), "non-null classloader for shared class?");
1185 1185 if (ik.not_null()) {
1186 1186 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
1187 1187 symbolHandle class_name(THREAD, ik->name());
1188 1188
1189 1189 // Found the class, now load the superclass and interfaces. If they
1190 1190 // are shared, add them to the main system dictionary and reset
1191 1191 // their hierarchy references (supers, subs, and interfaces).
1192 1192
1193 1193 if (ik->super() != NULL) {
1194 1194 symbolHandle cn(THREAD, ik->super()->klass_part()->name());
1195 1195 resolve_super_or_fail(class_name, cn,
1196 1196 class_loader, Handle(), true, CHECK_(nh));
1197 1197 }
1198 1198
1199 1199 objArrayHandle interfaces (THREAD, ik->local_interfaces());
1200 1200 int num_interfaces = interfaces->length();
1201 1201 for (int index = 0; index < num_interfaces; index++) {
1202 1202 klassOop k = klassOop(interfaces->obj_at(index));
1203 1203
1204 1204 // Note: can not use instanceKlass::cast here because
1205 1205 // interfaces' instanceKlass's C++ vtbls haven't been
1206 1206 // reinitialized yet (they will be once the interface classes
1207 1207 // are loaded)
1208 1208 symbolHandle name (THREAD, k->klass_part()->name());
1209 1209 resolve_super_or_fail(class_name, name, class_loader, Handle(), false, CHECK_(nh));
1210 1210 }
1211 1211
1212 1212 // Adjust methods to recover missing data. They need addresses for
1213 1213 // interpreter entry points and their default native method address
1214 1214 // must be reset.
1215 1215
1216 1216 // Updating methods must be done under a lock so multiple
1217 1217 // threads don't update these in parallel
1218 1218 // Shared classes are all currently loaded by the bootstrap
1219 1219 // classloader, so this will never cause a deadlock on
1220 1220 // a custom class loader lock.
1221 1221
1222 1222 {
1223 1223 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1224 1224 check_loader_lock_contention(lockObject, THREAD);
1225 1225 ObjectLocker ol(lockObject, THREAD, true);
1226 1226
1227 1227 objArrayHandle methods (THREAD, ik->methods());
1228 1228 int num_methods = methods->length();
1229 1229 for (int index2 = 0; index2 < num_methods; ++index2) {
1230 1230 methodHandle m(THREAD, methodOop(methods->obj_at(index2)));
1231 1231 m()->link_method(m, CHECK_(nh));
1232 1232 }
1233 1233 }
1234 1234
1235 1235 if (TraceClassLoading) {
1236 1236 ResourceMark rm;
1237 1237 tty->print("[Loaded %s", ik->external_name());
1238 1238 tty->print(" from shared objects file");
1239 1239 tty->print_cr("]");
1240 1240 }
1241 1241 // notify a class loaded from shared object
1242 1242 ClassLoadingService::notify_class_loaded(instanceKlass::cast(ik()),
1243 1243 true /* shared class */);
1244 1244 }
1245 1245 return ik;
1246 1246 }
1247 1247
1248 1248 #ifdef KERNEL
1249 1249 // Some classes on the bootstrap class path haven't been installed on the
1250 1250 // system yet. Call the DownloadManager method to make them appear in the
1251 1251 // bootstrap class path and try again to load the named class.
1252 1252 // Note that with delegation class loaders all classes in another loader will
1253 1253 // first try to call this so it'd better be fast!!
1254 1254 static instanceKlassHandle download_and_retry_class_load(
1255 1255 symbolHandle class_name,
1256 1256 TRAPS) {
1257 1257
1258 1258 klassOop dlm = SystemDictionary::sun_jkernel_DownloadManager_klass();
1259 1259 instanceKlassHandle nk;
1260 1260
1261 1261 // If download manager class isn't loaded just return.
1262 1262 if (dlm == NULL) return nk;
1263 1263
1264 1264 { HandleMark hm(THREAD);
1265 1265 ResourceMark rm(THREAD);
1266 1266 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nk));
1267 1267 Handle class_string = java_lang_String::externalize_classname(s, CHECK_(nk));
1268 1268
1269 1269 // return value
1270 1270 JavaValue result(T_OBJECT);
1271 1271
1272 1272 // Call the DownloadManager. We assume that it has a lock because
1273 1273 // multiple classes could be not found and downloaded at the same time.
1274 1274 // class sun.misc.DownloadManager;
1275 1275 // public static String getBootClassPathEntryForClass(String className);
1276 1276 JavaCalls::call_static(&result,
1277 1277 KlassHandle(THREAD, dlm),
1278 1278 vmSymbolHandles::getBootClassPathEntryForClass_name(),
1279 1279 vmSymbolHandles::string_string_signature(),
1280 1280 class_string,
1281 1281 CHECK_(nk));
1282 1282
1283 1283 // Get result.string and add to bootclasspath
1284 1284 assert(result.get_type() == T_OBJECT, "just checking");
1285 1285 oop obj = (oop) result.get_jobject();
1286 1286 if (obj == NULL) { return nk; }
1287 1287
1288 1288 Handle h_obj(THREAD, obj);
1289 1289 char* new_class_name = java_lang_String::as_platform_dependent_str(h_obj,
1290 1290 CHECK_(nk));
1291 1291
1292 1292 // lock the loader
1293 1293 // we use this lock because JVMTI does.
1294 1294 Handle loader_lock(THREAD, SystemDictionary::system_loader_lock());
1295 1295
1296 1296 ObjectLocker ol(loader_lock, THREAD);
1297 1297 // add the file to the bootclasspath
1298 1298 ClassLoader::update_class_path_entry_list(new_class_name, true);
1299 1299 } // end HandleMark
1300 1300
1301 1301 if (TraceClassLoading) {
1302 1302 ClassLoader::print_bootclasspath();
1303 1303 }
1304 1304 return ClassLoader::load_classfile(class_name, CHECK_(nk));
1305 1305 }
1306 1306 #endif // KERNEL
1307 1307
1308 1308
1309 1309 instanceKlassHandle SystemDictionary::load_instance_class(symbolHandle class_name, Handle class_loader, TRAPS) {
1310 1310 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
1311 1311 if (class_loader.is_null()) {
1312 1312
1313 1313 // Search the shared system dictionary for classes preloaded into the
1314 1314 // shared spaces.
1315 1315 instanceKlassHandle k;
1316 1316 {
1317 1317 PerfTraceTime vmtimer(ClassLoader::perf_shared_classload_time());
1318 1318 k = load_shared_class(class_name, class_loader, THREAD);
1319 1319 }
1320 1320
1321 1321 if (k.is_null()) {
1322 1322 // Use VM class loader
1323 1323 PerfTraceTime vmtimer(ClassLoader::perf_sys_classload_time());
1324 1324 k = ClassLoader::load_classfile(class_name, CHECK_(nh));
1325 1325 }
1326 1326
1327 1327 #ifdef KERNEL
1328 1328 // If the VM class loader has failed to load the class, call the
1329 1329 // DownloadManager class to make it magically appear on the classpath
1330 1330 // and try again. This is only configured with the Kernel VM.
1331 1331 if (k.is_null()) {
1332 1332 k = download_and_retry_class_load(class_name, CHECK_(nh));
1333 1333 }
1334 1334 #endif // KERNEL
1335 1335
1336 1336 // find_or_define_instance_class may return a different instanceKlass
1337 1337 if (!k.is_null()) {
1338 1338 k = find_or_define_instance_class(class_name, class_loader, k, CHECK_(nh));
1339 1339 }
1340 1340 return k;
1341 1341 } else {
1342 1342 // Use user specified class loader to load class. Call loadClass operation on class_loader.
1343 1343 ResourceMark rm(THREAD);
1344 1344
1345 1345 assert(THREAD->is_Java_thread(), "must be a JavaThread");
1346 1346 JavaThread* jt = (JavaThread*) THREAD;
1347 1347
1348 1348 PerfClassTraceTime vmtimer(ClassLoader::perf_app_classload_time(),
1349 1349 ClassLoader::perf_app_classload_selftime(),
1350 1350 ClassLoader::perf_app_classload_count(),
1351 1351 jt->get_thread_stat()->perf_recursion_counts_addr(),
1352 1352 jt->get_thread_stat()->perf_timers_addr(),
1353 1353 PerfClassTraceTime::CLASS_LOAD);
1354 1354
1355 1355 Handle s = java_lang_String::create_from_symbol(class_name, CHECK_(nh));
1356 1356 // Translate to external class name format, i.e., convert '/' chars to '.'
1357 1357 Handle string = java_lang_String::externalize_classname(s, CHECK_(nh));
1358 1358
1359 1359 JavaValue result(T_OBJECT);
1360 1360
1361 1361 KlassHandle spec_klass (THREAD, SystemDictionary::classloader_klass());
1362 1362
1363 1363 // Call public unsynchronized loadClass(String) directly for all class loaders
1364 1364 // for parallelCapable class loaders. JDK >=7, loadClass(String, boolean) will
1365 1365 // acquire a class-name based lock rather than the class loader object lock.
1366 1366 // JDK < 7 already acquire the class loader lock in loadClass(String, boolean),
1367 1367 // so the call to loadClassInternal() was not required.
1368 1368 //
1369 1369 // UnsyncloadClass flag means both call loadClass(String) and do
1370 1370 // not acquire the class loader lock even for class loaders that are
1371 1371 // not parallelCapable. This was a risky transitional
1372 1372 // flag for diagnostic purposes only. It is risky to call
1373 1373 // custom class loaders without synchronization.
1374 1374 // WARNING If a custom class loader does NOT synchronizer findClass, or callers of
1375 1375 // findClass, the UnsyncloadClass flag risks unexpected timing bugs in the field.
1376 1376 // Do NOT assume this will be supported in future releases.
1377 1377 //
1378 1378 // Added MustCallLoadClassInternal in case we discover in the field
1379 1379 // a customer that counts on this call
1380 1380 if (MustCallLoadClassInternal && has_loadClassInternal()) {
1381 1381 JavaCalls::call_special(&result,
1382 1382 class_loader,
1383 1383 spec_klass,
1384 1384 vmSymbolHandles::loadClassInternal_name(),
1385 1385 vmSymbolHandles::string_class_signature(),
1386 1386 string,
1387 1387 CHECK_(nh));
1388 1388 } else {
1389 1389 JavaCalls::call_virtual(&result,
1390 1390 class_loader,
1391 1391 spec_klass,
1392 1392 vmSymbolHandles::loadClass_name(),
1393 1393 vmSymbolHandles::string_class_signature(),
1394 1394 string,
1395 1395 CHECK_(nh));
1396 1396 }
1397 1397
1398 1398 assert(result.get_type() == T_OBJECT, "just checking");
1399 1399 oop obj = (oop) result.get_jobject();
1400 1400
1401 1401 // Primitive classes return null since forName() can not be
1402 1402 // used to obtain any of the Class objects representing primitives or void
1403 1403 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1404 1404 instanceKlassHandle k =
1405 1405 instanceKlassHandle(THREAD, java_lang_Class::as_klassOop(obj));
1406 1406 // For user defined Java class loaders, check that the name returned is
1407 1407 // the same as that requested. This check is done for the bootstrap
1408 1408 // loader when parsing the class file.
1409 1409 if (class_name() == k->name()) {
1410 1410 return k;
1411 1411 }
1412 1412 }
1413 1413 // Class is not found or has the wrong name, return NULL
1414 1414 return nh;
1415 1415 }
1416 1416 }
1417 1417
1418 1418 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1419 1419
1420 1420 Handle class_loader_h(THREAD, k->class_loader());
1421 1421
1422 1422 // for bootstrap and other parallel classloaders don't acquire lock,
1423 1423 // use placeholder token
1424 1424 // If a parallelCapable class loader calls define_instance_class instead of
1425 1425 // find_or_define_instance_class to get here, we have a timing
1426 1426 // hole with systemDictionary updates and check_constraints
1427 1427 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1428 1428 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1429 1429 compute_loader_lock_object(class_loader_h, THREAD)),
1430 1430 "define called without lock");
1431 1431 }
1432 1432
1433 1433 // Check class-loading constraints. Throw exception if violation is detected.
1434 1434 // Grabs and releases SystemDictionary_lock
1435 1435 // The check_constraints/find_class call and update_dictionary sequence
1436 1436 // must be "atomic" for a specific class/classloader pair so we never
1437 1437 // define two different instanceKlasses for that class/classloader pair.
1438 1438 // Existing classloaders will call define_instance_class with the
1439 1439 // classloader lock held
1440 1440 // Parallel classloaders will call find_or_define_instance_class
1441 1441 // which will require a token to perform the define class
1442 1442 symbolHandle name_h(THREAD, k->name());
1443 1443 unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader_h);
1444 1444 int d_index = dictionary()->hash_to_index(d_hash);
1445 1445 check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
1446 1446
1447 1447 // Register class just loaded with class loader (placed in Vector)
1448 1448 // Note we do this before updating the dictionary, as this can
1449 1449 // fail with an OutOfMemoryError (if it does, we will *not* put this
1450 1450 // class in the dictionary and will not update the class hierarchy).
1451 1451 if (k->class_loader() != NULL) {
1452 1452 methodHandle m(THREAD, Universe::loader_addClass_method());
1453 1453 JavaValue result(T_VOID);
1454 1454 JavaCallArguments args(class_loader_h);
1455 1455 args.push_oop(Handle(THREAD, k->java_mirror()));
1456 1456 JavaCalls::call(&result, m, &args, CHECK);
1457 1457 }
1458 1458
1459 1459 // Add the new class. We need recompile lock during update of CHA.
1460 1460 {
1461 1461 unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader_h);
1462 1462 int p_index = placeholders()->hash_to_index(p_hash);
1463 1463
1464 1464 MutexLocker mu_r(Compile_lock, THREAD);
1465 1465
1466 1466 // Add to class hierarchy, initialize vtables, and do possible
1467 1467 // deoptimizations.
1468 1468 add_to_hierarchy(k, CHECK); // No exception, but can block
1469 1469
1470 1470 // Add to systemDictionary - so other classes can see it.
1471 1471 // Grabs and releases SystemDictionary_lock
1472 1472 update_dictionary(d_index, d_hash, p_index, p_hash,
1473 1473 k, class_loader_h, THREAD);
1474 1474 }
1475 1475 k->eager_initialize(THREAD);
1476 1476
1477 1477 // notify jvmti
1478 1478 if (JvmtiExport::should_post_class_load()) {
1479 1479 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1480 1480 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1481 1481
1482 1482 }
1483 1483 }
1484 1484
1485 1485 // Support parallel classloading
1486 1486 // Initial implementation for bootstrap classloader
1487 1487 // For custom class loaders that support parallel classloading,
1488 1488 // With AllowParallelDefine flag==true, in case they do not synchronize around
1489 1489 // FindLoadedClass/DefineClass, calls, we check for parallel
1490 1490 // loading for them, wait if a defineClass is in progress
1491 1491 // and return the initial requestor's results
1492 1492 // With AllowParallelDefine flag==false, call through to define_instance_class
1493 1493 // which will throw LinkageError: duplicate class definition.
1494 1494 // For better performance, the class loaders should synchronize
1495 1495 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1496 1496 // potentially waste time reading and parsing the bytestream.
1497 1497 // Note: VM callers should ensure consistency of k/class_name,class_loader
1498 1498 instanceKlassHandle SystemDictionary::find_or_define_instance_class(symbolHandle class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1499 1499
1500 1500 instanceKlassHandle nh = instanceKlassHandle(); // null Handle
1501 1501 symbolHandle name_h(THREAD, k->name()); // passed in class_name may be null
1502 1502
1503 1503 unsigned int d_hash = dictionary()->compute_hash(name_h, class_loader);
1504 1504 int d_index = dictionary()->hash_to_index(d_hash);
1505 1505
1506 1506 // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
1507 1507 unsigned int p_hash = placeholders()->compute_hash(name_h, class_loader);
1508 1508 int p_index = placeholders()->hash_to_index(p_hash);
1509 1509 PlaceholderEntry* probe;
1510 1510
1511 1511 {
1512 1512 MutexLocker mu(SystemDictionary_lock, THREAD);
1513 1513 // First check if class already defined
1514 1514 klassOop check = find_class(d_index, d_hash, name_h, class_loader);
1515 1515 if (check != NULL) {
1516 1516 return(instanceKlassHandle(THREAD, check));
1517 1517 }
1518 1518
1519 1519 // Acquire define token for this class/classloader
1520 1520 symbolHandle nullsymbolHandle;
1521 1521 probe = placeholders()->find_and_add(p_index, p_hash, name_h, class_loader, PlaceholderTable::DEFINE_CLASS, nullsymbolHandle, THREAD);
1522 1522 // Wait if another thread defining in parallel
1523 1523 // All threads wait - even those that will throw duplicate class: otherwise
1524 1524 // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
1525 1525 // if other thread has not finished updating dictionary
1526 1526 while (probe->definer() != NULL) {
1527 1527 SystemDictionary_lock->wait();
1528 1528 }
1529 1529 // Only special cases allow parallel defines and can use other thread's results
1530 1530 // Other cases fall through, and may run into duplicate defines
1531 1531 // caught by finding an entry in the SystemDictionary
1532 1532 if ((UnsyncloadClass || AllowParallelDefineClass) && (probe->instanceKlass() != NULL)) {
1533 1533 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
1534 1534 placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
1535 1535 SystemDictionary_lock->notify_all();
1536 1536 #ifdef ASSERT
1537 1537 klassOop check = find_class(d_index, d_hash, name_h, class_loader);
1538 1538 assert(check != NULL, "definer missed recording success");
1539 1539 #endif
1540 1540 return(instanceKlassHandle(THREAD, probe->instanceKlass()));
1541 1541 } else {
1542 1542 // This thread will define the class (even if earlier thread tried and had an error)
1543 1543 probe->set_definer(THREAD);
1544 1544 }
1545 1545 }
1546 1546
1547 1547 define_instance_class(k, THREAD);
1548 1548
1549 1549 Handle linkage_exception = Handle(); // null handle
1550 1550
1551 1551 // definer must notify any waiting threads
1552 1552 {
1553 1553 MutexLocker mu(SystemDictionary_lock, THREAD);
1554 1554 PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, class_loader);
1555 1555 assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
1556 1556 if (probe != NULL) {
1557 1557 if (HAS_PENDING_EXCEPTION) {
1558 1558 linkage_exception = Handle(THREAD,PENDING_EXCEPTION);
1559 1559 CLEAR_PENDING_EXCEPTION;
1560 1560 } else {
1561 1561 probe->set_instanceKlass(k());
1562 1562 }
1563 1563 probe->set_definer(NULL);
1564 1564 probe->remove_seen_thread(THREAD, PlaceholderTable::DEFINE_CLASS);
1565 1565 placeholders()->find_and_remove(p_index, p_hash, name_h, class_loader, THREAD);
1566 1566 SystemDictionary_lock->notify_all();
1567 1567 }
1568 1568 }
1569 1569
1570 1570 // Can't throw exception while holding lock due to rank ordering
1571 1571 if (linkage_exception() != NULL) {
1572 1572 THROW_OOP_(linkage_exception(), nh); // throws exception and returns
1573 1573 }
1574 1574
1575 1575 return k;
1576 1576 }
1577 1577 Handle SystemDictionary::compute_loader_lock_object(Handle class_loader, TRAPS) {
1578 1578 // If class_loader is NULL we synchronize on _system_loader_lock_obj
1579 1579 if (class_loader.is_null()) {
1580 1580 return Handle(THREAD, _system_loader_lock_obj);
1581 1581 } else {
1582 1582 return class_loader;
1583 1583 }
1584 1584 }
1585 1585
1586 1586 // This method is added to check how often we have to wait to grab loader
1587 1587 // lock. The results are being recorded in the performance counters defined in
1588 1588 // ClassLoader::_sync_systemLoaderLockContentionRate and
1589 1589 // ClassLoader::_sync_nonSystemLoaderLockConteionRate.
1590 1590 void SystemDictionary::check_loader_lock_contention(Handle loader_lock, TRAPS) {
1591 1591 if (!UsePerfData) {
1592 1592 return;
1593 1593 }
1594 1594
1595 1595 assert(!loader_lock.is_null(), "NULL lock object");
1596 1596
1597 1597 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
1598 1598 == ObjectSynchronizer::owner_other) {
1599 1599 // contention will likely happen, so increment the corresponding
1600 1600 // contention counter.
1601 1601 if (loader_lock() == _system_loader_lock_obj) {
1602 1602 ClassLoader::sync_systemLoaderLockContentionRate()->inc();
1603 1603 } else {
1604 1604 ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
1605 1605 }
1606 1606 }
1607 1607 }
1608 1608
1609 1609 // ----------------------------------------------------------------------------
1610 1610 // Lookup
1611 1611
1612 1612 klassOop SystemDictionary::find_class(int index, unsigned int hash,
1613 1613 symbolHandle class_name,
1614 1614 Handle class_loader) {
1615 1615 assert_locked_or_safepoint(SystemDictionary_lock);
1616 1616 assert (index == dictionary()->index_for(class_name, class_loader),
1617 1617 "incorrect index?");
1618 1618
1619 1619 klassOop k = dictionary()->find_class(index, hash, class_name, class_loader);
1620 1620 return k;
1621 1621 }
1622 1622
1623 1623
1624 1624 // Basic find on classes in the midst of being loaded
1625 1625 symbolOop SystemDictionary::find_placeholder(int index, unsigned int hash,
1626 1626 symbolHandle class_name,
1627 1627 Handle class_loader) {
1628 1628 assert_locked_or_safepoint(SystemDictionary_lock);
1629 1629
1630 1630 return placeholders()->find_entry(index, hash, class_name, class_loader);
1631 1631 }
1632 1632
1633 1633
1634 1634 // Used for assertions and verification only
1635 1635 oop SystemDictionary::find_class_or_placeholder(symbolHandle class_name,
1636 1636 Handle class_loader) {
1637 1637 #ifndef ASSERT
1638 1638 guarantee(VerifyBeforeGC ||
1639 1639 VerifyDuringGC ||
1640 1640 VerifyBeforeExit ||
1641 1641 VerifyAfterGC, "too expensive");
1642 1642 #endif
1643 1643 assert_locked_or_safepoint(SystemDictionary_lock);
1644 1644 symbolOop class_name_ = class_name();
1645 1645 oop class_loader_ = class_loader();
1646 1646
1647 1647 // First look in the loaded class array
1648 1648 unsigned int d_hash = dictionary()->compute_hash(class_name, class_loader);
1649 1649 int d_index = dictionary()->hash_to_index(d_hash);
1650 1650 oop lookup = find_class(d_index, d_hash, class_name, class_loader);
1651 1651
1652 1652 if (lookup == NULL) {
1653 1653 // Next try the placeholders
1654 1654 unsigned int p_hash = placeholders()->compute_hash(class_name,class_loader);
1655 1655 int p_index = placeholders()->hash_to_index(p_hash);
1656 1656 lookup = find_placeholder(p_index, p_hash, class_name, class_loader);
1657 1657 }
1658 1658
1659 1659 return lookup;
1660 1660 }
1661 1661
1662 1662
1663 1663 // Get the next class in the diictionary.
1664 1664 klassOop SystemDictionary::try_get_next_class() {
1665 1665 return dictionary()->try_get_next_class();
1666 1666 }
1667 1667
1668 1668
1669 1669 // ----------------------------------------------------------------------------
1670 1670 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
1671 1671 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
1672 1672 // before a new class is used.
1673 1673
1674 1674 void SystemDictionary::add_to_hierarchy(instanceKlassHandle k, TRAPS) {
1675 1675 assert(k.not_null(), "just checking");
1676 1676 // Link into hierachy. Make sure the vtables are initialized before linking into
1677 1677 k->append_to_sibling_list(); // add to superklass/sibling list
1678 1678 k->process_interfaces(THREAD); // handle all "implements" declarations
1679 1679 k->set_init_state(instanceKlass::loaded);
1680 1680 // Now flush all code that depended on old class hierarchy.
1681 1681 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1682 1682 // Also, first reinitialize vtable because it may have gotten out of synch
1683 1683 // while the new class wasn't connected to the class hierarchy.
1684 1684 Universe::flush_dependents_on(k);
1685 1685 }
1686 1686
1687 1687
1688 1688 // ----------------------------------------------------------------------------
1689 1689 // GC support
1690 1690
1691 1691 // Following roots during mark-sweep is separated in two phases.
1692 1692 //
1693 1693 // The first phase follows preloaded classes and all other system
1694 1694 // classes, since these will never get unloaded anyway.
1695 1695 //
1696 1696 // The second phase removes (unloads) unreachable classes from the
1697 1697 // system dictionary and follows the remaining classes' contents.
1698 1698
1699 1699 void SystemDictionary::always_strong_oops_do(OopClosure* blk) {
1700 1700 // Follow preloaded classes/mirrors and system loader object
1701 1701 blk->do_oop(&_java_system_loader);
1702 1702 preloaded_oops_do(blk);
1703 1703 always_strong_classes_do(blk);
1704 1704 }
1705 1705
1706 1706
1707 1707 void SystemDictionary::always_strong_classes_do(OopClosure* blk) {
1708 1708 // Follow all system classes and temporary placeholders in dictionary
1709 1709 dictionary()->always_strong_classes_do(blk);
1710 1710
1711 1711 // Placeholders. These are *always* strong roots, as they
1712 1712 // represent classes we're actively loading.
1713 1713 placeholders_do(blk);
1714 1714
1715 1715 // Visit extra methods
1716 1716 if (invoke_method_table() != NULL)
1717 1717 invoke_method_table()->oops_do(blk);
1718 1718
1719 1719 // Loader constraints. We must keep the symbolOop used in the name alive.
1720 1720 constraints()->always_strong_classes_do(blk);
1721 1721
1722 1722 // Resolution errors keep the symbolOop for the error alive
1723 1723 resolution_errors()->always_strong_classes_do(blk);
1724 1724 }
1725 1725
1726 1726
1727 1727 void SystemDictionary::placeholders_do(OopClosure* blk) {
1728 1728 placeholders()->oops_do(blk);
1729 1729 }
1730 1730
1731 1731
1732 1732 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
1733 1733 bool result = dictionary()->do_unloading(is_alive);
1734 1734 constraints()->purge_loader_constraints(is_alive);
1735 1735 resolution_errors()->purge_resolution_errors(is_alive);
1736 1736 return result;
1737 1737 }
1738 1738
1739 1739
1740 1740 // The mirrors are scanned by shared_oops_do() which is
1741 1741 // not called by oops_do(). In order to process oops in
1742 1742 // a necessary order, shared_oops_do() is call by
1743 1743 // Universe::oops_do().
1744 1744 void SystemDictionary::oops_do(OopClosure* f) {
1745 1745 // Adjust preloaded classes and system loader object
1746 1746 f->do_oop(&_java_system_loader);
1747 1747 preloaded_oops_do(f);
1748 1748
1749 1749 lazily_loaded_oops_do(f);
1750 1750
1751 1751 // Adjust dictionary
1752 1752 dictionary()->oops_do(f);
1753 1753
1754 1754 // Visit extra methods
1755 1755 if (invoke_method_table() != NULL)
1756 1756 invoke_method_table()->oops_do(f);
1757 1757
1758 1758 // Partially loaded classes
1759 1759 placeholders()->oops_do(f);
1760 1760
1761 1761 // Adjust constraint table
1762 1762 constraints()->oops_do(f);
1763 1763
1764 1764 // Adjust resolution error table
1765 1765 resolution_errors()->oops_do(f);
1766 1766 }
1767 1767
1768 1768
1769 1769 void SystemDictionary::preloaded_oops_do(OopClosure* f) {
1770 1770 f->do_oop((oop*) &wk_klass_name_limits[0]);
1771 1771 f->do_oop((oop*) &wk_klass_name_limits[1]);
1772 1772
1773 1773 for (int k = (int)FIRST_WKID; k < (int)WKID_LIMIT; k++) {
1774 1774 f->do_oop((oop*) &_well_known_klasses[k]);
1775 1775 }
1776 1776
1777 1777 {
1778 1778 for (int i = 0; i < T_VOID+1; i++) {
1779 1779 if (_box_klasses[i] != NULL) {
1780 1780 assert(i >= T_BOOLEAN, "checking");
1781 1781 f->do_oop((oop*) &_box_klasses[i]);
1782 1782 }
1783 1783 }
1784 1784 }
1785 1785
1786 1786 // The basic type mirrors would have already been processed in
1787 1787 // Universe::oops_do(), via a call to shared_oops_do(), so should
1788 1788 // not be processed again.
1789 1789
1790 1790 f->do_oop((oop*) &_system_loader_lock_obj);
1791 1791 FilteredFieldsMap::klasses_oops_do(f);
1792 1792 }
1793 1793
1794 1794 void SystemDictionary::lazily_loaded_oops_do(OopClosure* f) {
1795 1795 f->do_oop((oop*) &_abstract_ownable_synchronizer_klass);
1796 1796 }
1797 1797
1798 1798 // Just the classes from defining class loaders
1799 1799 // Don't iterate over placeholders
1800 1800 void SystemDictionary::classes_do(void f(klassOop)) {
1801 1801 dictionary()->classes_do(f);
1802 1802 }
1803 1803
1804 1804 // Added for initialize_itable_for_klass
1805 1805 // Just the classes from defining class loaders
1806 1806 // Don't iterate over placeholders
1807 1807 void SystemDictionary::classes_do(void f(klassOop, TRAPS), TRAPS) {
1808 1808 dictionary()->classes_do(f, CHECK);
1809 1809 }
1810 1810
1811 1811 // All classes, and their class loaders
1812 1812 // Don't iterate over placeholders
1813 1813 void SystemDictionary::classes_do(void f(klassOop, oop)) {
1814 1814 dictionary()->classes_do(f);
1815 1815 }
1816 1816
1817 1817 // All classes, and their class loaders
1818 1818 // (added for helpers that use HandleMarks and ResourceMarks)
1819 1819 // Don't iterate over placeholders
1820 1820 void SystemDictionary::classes_do(void f(klassOop, oop, TRAPS), TRAPS) {
1821 1821 dictionary()->classes_do(f, CHECK);
1822 1822 }
1823 1823
1824 1824 void SystemDictionary::placeholders_do(void f(symbolOop, oop)) {
1825 1825 placeholders()->entries_do(f);
1826 1826 }
1827 1827
1828 1828 void SystemDictionary::methods_do(void f(methodOop)) {
1829 1829 dictionary()->methods_do(f);
1830 1830 if (invoke_method_table() != NULL)
1831 1831 invoke_method_table()->methods_do(f);
1832 1832 }
1833 1833
1834 1834 // ----------------------------------------------------------------------------
1835 1835 // Lazily load klasses
1836 1836
1837 1837 void SystemDictionary::load_abstract_ownable_synchronizer_klass(TRAPS) {
1838 1838 assert(JDK_Version::is_gte_jdk16x_version(), "Must be JDK 1.6 or later");
1839 1839
1840 1840 // if multiple threads calling this function, only one thread will load
1841 1841 // the class. The other threads will find the loaded version once the
1842 1842 // class is loaded.
1843 1843 klassOop aos = _abstract_ownable_synchronizer_klass;
1844 1844 if (aos == NULL) {
1845 1845 klassOop k = resolve_or_fail(vmSymbolHandles::java_util_concurrent_locks_AbstractOwnableSynchronizer(), true, CHECK);
1846 1846 // Force a fence to prevent any read before the write completes
1847 1847 OrderAccess::fence();
1848 1848 _abstract_ownable_synchronizer_klass = k;
1849 1849 }
1850 1850 }
1851 1851
1852 1852 // ----------------------------------------------------------------------------
1853 1853 // Initialization
1854 1854
1855 1855 void SystemDictionary::initialize(TRAPS) {
1856 1856 // Allocate arrays
1857 1857 assert(dictionary() == NULL,
1858 1858 "SystemDictionary should only be initialized once");
1859 1859 _dictionary = new Dictionary(_nof_buckets);
1860 1860 _placeholders = new PlaceholderTable(_nof_buckets);
1861 1861 _number_of_modifications = 0;
1862 1862 _loader_constraints = new LoaderConstraintTable(_loader_constraint_size);
1863 1863 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
1864 1864 // _invoke_method_table is allocated lazily in find_method_handle_invoke()
1865 1865
1866 1866 // Allocate private object used as system class loader lock
1867 1867 _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
1868 1868 // Initialize basic classes
1869 1869 initialize_preloaded_classes(CHECK);
1870 1870 }
1871 1871
1872 1872 // Compact table of directions on the initialization of klasses:
1873 1873 static const short wk_init_info[] = {
1874 1874 #define WK_KLASS_INIT_INFO(name, symbol, option) \
1875 1875 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
1876 1876 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
1877 1877 | (int)SystemDictionary::option ),
1878 1878 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
1879 1879 #undef WK_KLASS_INIT_INFO
1880 1880 0
1881 1881 };
1882 1882
1883 1883 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
1884 1884 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1885 1885 int info = wk_init_info[id - FIRST_WKID];
1886 1886 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1887 1887 symbolHandle symbol = vmSymbolHandles::symbol_handle_at((vmSymbols::SID)sid);
1888 1888 klassOop* klassp = &_well_known_klasses[id];
1889 1889 bool must_load = (init_opt < SystemDictionary::Opt);
1890 1890 bool try_load = true;
1891 1891 if (init_opt == SystemDictionary::Opt_Kernel) {
1892 1892 #ifndef KERNEL
1893 1893 try_load = false;
1894 1894 #endif //KERNEL
1895 1895 }
1896 1896 if ((*klassp) == NULL && try_load) {
1897 1897 if (must_load) {
1898 1898 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
1899 1899 } else {
1900 1900 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
1901 1901 }
1902 1902 }
1903 1903 return ((*klassp) != NULL);
1904 1904 }
1905 1905
1906 1906 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
1907 1907 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
1908 1908 for (int id = (int)start_id; id < (int)limit_id; id++) {
1909 1909 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1910 1910 int info = wk_init_info[id - FIRST_WKID];
1911 1911 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1912 1912 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
1913 1913
1914 1914 initialize_wk_klass((WKID)id, opt, CHECK);
1915 1915
1916 1916 // Update limits, so find_well_known_klass can be very fast:
1917 1917 symbolOop s = vmSymbols::symbol_at((vmSymbols::SID)sid);
1918 1918 if (wk_klass_name_limits[1] == NULL) {
1919 1919 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s;
1920 1920 } else if (wk_klass_name_limits[1] < s) {
1921 1921 wk_klass_name_limits[1] = s;
1922 1922 } else if (wk_klass_name_limits[0] > s) {
1923 1923 wk_klass_name_limits[0] = s;
1924 1924 }
1925 1925 }
1926 1926
1927 1927 // move the starting value forward to the limit:
1928 1928 start_id = limit_id;
1929 1929 }
1930 1930
1931 1931
1932 1932 void SystemDictionary::initialize_preloaded_classes(TRAPS) {
1933 1933 assert(WK_KLASS(object_klass) == NULL, "preloaded classes should only be initialized once");
1934 1934 // Preload commonly used klasses
1935 1935 WKID scan = FIRST_WKID;
1936 1936 // first do Object, String, Class
1937 1937 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(class_klass), scan, CHECK);
1938 1938
1939 1939 debug_only(instanceKlass::verify_class_klass_nonstatic_oop_maps(WK_KLASS(class_klass)));
1940 1940
1941 1941 // Fixup mirrors for classes loaded before java.lang.Class.
1942 1942 // These calls iterate over the objects currently in the perm gen
1943 1943 // so calling them at this point is matters (not before when there
1944 1944 // are fewer objects and not later after there are more objects
1945 1945 // in the perm gen.
1946 1946 Universe::initialize_basic_type_mirrors(CHECK);
1947 1947 Universe::fixup_mirrors(CHECK);
1948 1948
1949 1949 // do a bunch more:
1950 1950 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(reference_klass), scan, CHECK);
1951 1951
1952 1952 // Preload ref klasses and set reference types
1953 1953 instanceKlass::cast(WK_KLASS(reference_klass))->set_reference_type(REF_OTHER);
1954 1954 instanceRefKlass::update_nonstatic_oop_maps(WK_KLASS(reference_klass));
1955 1955
1956 1956 initialize_wk_klasses_through(WK_KLASS_ENUM_NAME(phantom_reference_klass), scan, CHECK);
1957 1957 instanceKlass::cast(WK_KLASS(soft_reference_klass))->set_reference_type(REF_SOFT);
1958 1958 instanceKlass::cast(WK_KLASS(weak_reference_klass))->set_reference_type(REF_WEAK);
1959 1959 instanceKlass::cast(WK_KLASS(final_reference_klass))->set_reference_type(REF_FINAL);
1960 1960 instanceKlass::cast(WK_KLASS(phantom_reference_klass))->set_reference_type(REF_PHANTOM);
1961 1961
1962 1962 WKID meth_group_start = WK_KLASS_ENUM_NAME(MethodHandle_klass);
↓ open down ↓ |
1962 lines elided |
↑ open up ↑ |
1963 1963 WKID meth_group_end = WK_KLASS_ENUM_NAME(WrongMethodTypeException_klass);
1964 1964 initialize_wk_klasses_until(meth_group_start, scan, CHECK);
1965 1965 if (EnableMethodHandles) {
1966 1966 initialize_wk_klasses_through(meth_group_end, scan, CHECK);
1967 1967 }
1968 1968 if (_well_known_klasses[meth_group_start] == NULL) {
1969 1969 // Skip the rest of the method handle classes, if MethodHandle is not loaded.
1970 1970 scan = WKID(meth_group_end+1);
1971 1971 }
1972 1972 WKID indy_group_start = WK_KLASS_ENUM_NAME(Linkage_klass);
1973 - WKID indy_group_end = WK_KLASS_ENUM_NAME(Dynamic_klass);
1973 + WKID indy_group_end = WK_KLASS_ENUM_NAME(InvokeDynamic_klass);
1974 1974 initialize_wk_klasses_until(indy_group_start, scan, CHECK);
1975 1975 if (EnableInvokeDynamic) {
1976 1976 initialize_wk_klasses_through(indy_group_end, scan, CHECK);
1977 1977 }
1978 1978 if (_well_known_klasses[indy_group_start] == NULL) {
1979 1979 // Skip the rest of the dynamic typing classes, if Linkage is not loaded.
1980 1980 scan = WKID(indy_group_end+1);
1981 1981 }
1982 1982
1983 1983 initialize_wk_klasses_until(WKID_LIMIT, scan, CHECK);
1984 1984
1985 1985 _box_klasses[T_BOOLEAN] = WK_KLASS(boolean_klass);
1986 1986 _box_klasses[T_CHAR] = WK_KLASS(char_klass);
1987 1987 _box_klasses[T_FLOAT] = WK_KLASS(float_klass);
1988 1988 _box_klasses[T_DOUBLE] = WK_KLASS(double_klass);
1989 1989 _box_klasses[T_BYTE] = WK_KLASS(byte_klass);
1990 1990 _box_klasses[T_SHORT] = WK_KLASS(short_klass);
1991 1991 _box_klasses[T_INT] = WK_KLASS(int_klass);
1992 1992 _box_klasses[T_LONG] = WK_KLASS(long_klass);
1993 1993 //_box_klasses[T_OBJECT] = WK_KLASS(object_klass);
1994 1994 //_box_klasses[T_ARRAY] = WK_KLASS(object_klass);
1995 1995
1996 1996 #ifdef KERNEL
1997 1997 if (sun_jkernel_DownloadManager_klass() == NULL) {
1998 1998 warning("Cannot find sun/jkernel/DownloadManager");
1999 1999 }
2000 2000 #endif // KERNEL
2001 2001
2002 2002 { // Compute whether we should use loadClass or loadClassInternal when loading classes.
2003 2003 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::loadClassInternal_name(), vmSymbols::string_class_signature());
2004 2004 _has_loadClassInternal = (method != NULL);
2005 2005 }
2006 2006 { // Compute whether we should use checkPackageAccess or NOT
2007 2007 methodOop method = instanceKlass::cast(classloader_klass())->find_method(vmSymbols::checkPackageAccess_name(), vmSymbols::class_protectiondomain_signature());
2008 2008 _has_checkPackageAccess = (method != NULL);
2009 2009 }
2010 2010 }
2011 2011
2012 2012 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
2013 2013 // If so, returns the basic type it holds. If not, returns T_OBJECT.
2014 2014 BasicType SystemDictionary::box_klass_type(klassOop k) {
2015 2015 assert(k != NULL, "");
2016 2016 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
2017 2017 if (_box_klasses[i] == k)
2018 2018 return (BasicType)i;
2019 2019 }
2020 2020 return T_OBJECT;
2021 2021 }
2022 2022
2023 2023 KlassHandle SystemDictionaryHandles::box_klass(BasicType t) {
2024 2024 if (t >= T_BOOLEAN && t <= T_VOID)
2025 2025 return KlassHandle(&SystemDictionary::_box_klasses[t], true);
2026 2026 else
2027 2027 return KlassHandle();
2028 2028 }
2029 2029
2030 2030 // Constraints on class loaders. The details of the algorithm can be
2031 2031 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
2032 2032 // Virtual Machine" by Sheng Liang and Gilad Bracha. The basic idea is
2033 2033 // that the system dictionary needs to maintain a set of contraints that
2034 2034 // must be satisfied by all classes in the dictionary.
2035 2035 // if defining is true, then LinkageError if already in systemDictionary
2036 2036 // if initiating loader, then ok if instanceKlass matches existing entry
2037 2037
2038 2038 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
2039 2039 instanceKlassHandle k,
2040 2040 Handle class_loader, bool defining,
2041 2041 TRAPS) {
2042 2042 const char *linkage_error = NULL;
2043 2043 {
2044 2044 symbolHandle name (THREAD, k->name());
2045 2045 MutexLocker mu(SystemDictionary_lock, THREAD);
2046 2046
2047 2047 klassOop check = find_class(d_index, d_hash, name, class_loader);
2048 2048 if (check != (klassOop)NULL) {
2049 2049 // if different instanceKlass - duplicate class definition,
2050 2050 // else - ok, class loaded by a different thread in parallel,
2051 2051 // we should only have found it if it was done loading and ok to use
2052 2052 // system dictionary only holds instance classes, placeholders
2053 2053 // also holds array classes
2054 2054
2055 2055 assert(check->klass_part()->oop_is_instance(), "noninstance in systemdictionary");
2056 2056 if ((defining == true) || (k() != check)) {
2057 2057 linkage_error = "loader (instance of %s): attempted duplicate class "
2058 2058 "definition for name: \"%s\"";
2059 2059 } else {
2060 2060 return;
2061 2061 }
2062 2062 }
2063 2063
2064 2064 #ifdef ASSERT
2065 2065 unsigned int p_hash = placeholders()->compute_hash(name, class_loader);
2066 2066 int p_index = placeholders()->hash_to_index(p_hash);
2067 2067 symbolOop ph_check = find_placeholder(p_index, p_hash, name, class_loader);
2068 2068 assert(ph_check == NULL || ph_check == name(), "invalid symbol");
2069 2069 #endif
2070 2070
2071 2071 if (linkage_error == NULL) {
2072 2072 if (constraints()->check_or_update(k, class_loader, name) == false) {
2073 2073 linkage_error = "loader constraint violation: loader (instance of %s)"
2074 2074 " previously initiated loading for a different type with name \"%s\"";
2075 2075 }
2076 2076 }
2077 2077 }
2078 2078
2079 2079 // Throw error now if needed (cannot throw while holding
2080 2080 // SystemDictionary_lock because of rank ordering)
2081 2081
2082 2082 if (linkage_error) {
2083 2083 ResourceMark rm(THREAD);
2084 2084 const char* class_loader_name = loader_name(class_loader());
2085 2085 char* type_name = k->name()->as_C_string();
2086 2086 size_t buflen = strlen(linkage_error) + strlen(class_loader_name) +
2087 2087 strlen(type_name);
2088 2088 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
2089 2089 jio_snprintf(buf, buflen, linkage_error, class_loader_name, type_name);
2090 2090 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
2091 2091 }
2092 2092 }
2093 2093
2094 2094
2095 2095 // Update system dictionary - done after check_constraint and add_to_hierachy
2096 2096 // have been called.
2097 2097 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
2098 2098 int p_index, unsigned int p_hash,
2099 2099 instanceKlassHandle k,
2100 2100 Handle class_loader,
2101 2101 TRAPS) {
2102 2102 // Compile_lock prevents systemDictionary updates during compilations
2103 2103 assert_locked_or_safepoint(Compile_lock);
2104 2104 symbolHandle name (THREAD, k->name());
2105 2105
2106 2106 {
2107 2107 MutexLocker mu1(SystemDictionary_lock, THREAD);
2108 2108
2109 2109 // See whether biased locking is enabled and if so set it for this
2110 2110 // klass.
2111 2111 // Note that this must be done past the last potential blocking
2112 2112 // point / safepoint. We enable biased locking lazily using a
2113 2113 // VM_Operation to iterate the SystemDictionary and installing the
2114 2114 // biasable mark word into each instanceKlass's prototype header.
2115 2115 // To avoid race conditions where we accidentally miss enabling the
2116 2116 // optimization for one class in the process of being added to the
2117 2117 // dictionary, we must not safepoint after the test of
2118 2118 // BiasedLocking::enabled().
2119 2119 if (UseBiasedLocking && BiasedLocking::enabled()) {
2120 2120 // Set biased locking bit for all loaded classes; it will be
2121 2121 // cleared if revocation occurs too often for this type
2122 2122 // NOTE that we must only do this when the class is initally
2123 2123 // defined, not each time it is referenced from a new class loader
2124 2124 if (k->class_loader() == class_loader()) {
2125 2125 k->set_prototype_header(markOopDesc::biased_locking_prototype());
2126 2126 }
2127 2127 }
2128 2128
2129 2129 // Check for a placeholder. If there, remove it and make a
2130 2130 // new system dictionary entry.
2131 2131 placeholders()->find_and_remove(p_index, p_hash, name, class_loader, THREAD);
2132 2132 klassOop sd_check = find_class(d_index, d_hash, name, class_loader);
2133 2133 if (sd_check == NULL) {
2134 2134 dictionary()->add_klass(name, class_loader, k);
2135 2135 notice_modification();
2136 2136 }
2137 2137 #ifdef ASSERT
2138 2138 sd_check = find_class(d_index, d_hash, name, class_loader);
2139 2139 assert (sd_check != NULL, "should have entry in system dictionary");
2140 2140 // Changed to allow PH to remain to complete class circularity checking
2141 2141 // while only one thread can define a class at one time, multiple
2142 2142 // classes can resolve the superclass for a class at one time,
2143 2143 // and the placeholder is used to track that
2144 2144 // symbolOop ph_check = find_placeholder(p_index, p_hash, name, class_loader);
2145 2145 // assert (ph_check == NULL, "should not have a placeholder entry");
2146 2146 #endif
2147 2147 SystemDictionary_lock->notify_all();
2148 2148 }
2149 2149 }
2150 2150
2151 2151
2152 2152 klassOop SystemDictionary::find_constrained_instance_or_array_klass(
2153 2153 symbolHandle class_name, Handle class_loader, TRAPS) {
2154 2154
2155 2155 // First see if it has been loaded directly.
2156 2156 // Force the protection domain to be null. (This removes protection checks.)
2157 2157 Handle no_protection_domain;
2158 2158 klassOop klass = find_instance_or_array_klass(class_name, class_loader,
2159 2159 no_protection_domain, CHECK_NULL);
2160 2160 if (klass != NULL)
2161 2161 return klass;
2162 2162
2163 2163 // Now look to see if it has been loaded elsewhere, and is subject to
2164 2164 // a loader constraint that would require this loader to return the
2165 2165 // klass that is already loaded.
2166 2166 if (FieldType::is_array(class_name())) {
2167 2167 // Array classes are hard because their klassOops are not kept in the
2168 2168 // constraint table. The array klass may be constrained, but the elem class
2169 2169 // may not be.
2170 2170 jint dimension;
2171 2171 symbolOop object_key;
2172 2172 BasicType t = FieldType::get_array_info(class_name(), &dimension,
2173 2173 &object_key, CHECK_(NULL));
2174 2174 if (t != T_OBJECT) {
2175 2175 klass = Universe::typeArrayKlassObj(t);
2176 2176 } else {
2177 2177 symbolHandle elem_name(THREAD, object_key);
2178 2178 MutexLocker mu(SystemDictionary_lock, THREAD);
2179 2179 klass = constraints()->find_constrained_elem_klass(class_name, elem_name, class_loader, THREAD);
2180 2180 }
2181 2181 if (klass != NULL) {
2182 2182 klass = Klass::cast(klass)->array_klass_or_null(dimension);
2183 2183 }
2184 2184 } else {
2185 2185 MutexLocker mu(SystemDictionary_lock, THREAD);
2186 2186 // Non-array classes are easy: simply check the constraint table.
2187 2187 klass = constraints()->find_constrained_klass(class_name, class_loader);
2188 2188 }
2189 2189
2190 2190 return klass;
2191 2191 }
2192 2192
2193 2193
2194 2194 bool SystemDictionary::add_loader_constraint(symbolHandle class_name,
2195 2195 Handle class_loader1,
2196 2196 Handle class_loader2,
2197 2197 Thread* THREAD) {
2198 2198 unsigned int d_hash1 = dictionary()->compute_hash(class_name, class_loader1);
2199 2199 int d_index1 = dictionary()->hash_to_index(d_hash1);
2200 2200
2201 2201 unsigned int d_hash2 = dictionary()->compute_hash(class_name, class_loader2);
2202 2202 int d_index2 = dictionary()->hash_to_index(d_hash2);
2203 2203
2204 2204 {
2205 2205 MutexLocker mu_s(SystemDictionary_lock, THREAD);
2206 2206
2207 2207 // Better never do a GC while we're holding these oops
2208 2208 No_Safepoint_Verifier nosafepoint;
2209 2209
2210 2210 klassOop klass1 = find_class(d_index1, d_hash1, class_name, class_loader1);
2211 2211 klassOop klass2 = find_class(d_index2, d_hash2, class_name, class_loader2);
2212 2212 return constraints()->add_entry(class_name, klass1, class_loader1,
2213 2213 klass2, class_loader2);
2214 2214 }
2215 2215 }
2216 2216
2217 2217 // Add entry to resolution error table to record the error when the first
2218 2218 // attempt to resolve a reference to a class has failed.
2219 2219 void SystemDictionary::add_resolution_error(constantPoolHandle pool, int which, symbolHandle error) {
2220 2220 unsigned int hash = resolution_errors()->compute_hash(pool, which);
2221 2221 int index = resolution_errors()->hash_to_index(hash);
2222 2222 {
2223 2223 MutexLocker ml(SystemDictionary_lock, Thread::current());
2224 2224 resolution_errors()->add_entry(index, hash, pool, which, error);
2225 2225 }
2226 2226 }
2227 2227
2228 2228 // Lookup resolution error table. Returns error if found, otherwise NULL.
2229 2229 symbolOop SystemDictionary::find_resolution_error(constantPoolHandle pool, int which) {
2230 2230 unsigned int hash = resolution_errors()->compute_hash(pool, which);
2231 2231 int index = resolution_errors()->hash_to_index(hash);
2232 2232 {
2233 2233 MutexLocker ml(SystemDictionary_lock, Thread::current());
2234 2234 ResolutionErrorEntry* entry = resolution_errors()->find_entry(index, hash, pool, which);
2235 2235 return (entry != NULL) ? entry->error() : (symbolOop)NULL;
2236 2236 }
2237 2237 }
2238 2238
2239 2239
2240 2240 // Signature constraints ensure that callers and callees agree about
2241 2241 // the meaning of type names in their signatures. This routine is the
2242 2242 // intake for constraints. It collects them from several places:
2243 2243 //
2244 2244 // * LinkResolver::resolve_method (if check_access is true) requires
2245 2245 // that the resolving class (the caller) and the defining class of
2246 2246 // the resolved method (the callee) agree on each type in the
2247 2247 // method's signature.
2248 2248 //
2249 2249 // * LinkResolver::resolve_interface_method performs exactly the same
2250 2250 // checks.
2251 2251 //
2252 2252 // * LinkResolver::resolve_field requires that the constant pool
2253 2253 // attempting to link to a field agree with the field's defining
2254 2254 // class about the type of the field signature.
2255 2255 //
2256 2256 // * klassVtable::initialize_vtable requires that, when a class
2257 2257 // overrides a vtable entry allocated by a superclass, that the
2258 2258 // overriding method (i.e., the callee) agree with the superclass
2259 2259 // on each type in the method's signature.
2260 2260 //
2261 2261 // * klassItable::initialize_itable requires that, when a class fills
2262 2262 // in its itables, for each non-abstract method installed in an
2263 2263 // itable, the method (i.e., the callee) agree with the interface
2264 2264 // on each type in the method's signature.
2265 2265 //
2266 2266 // All those methods have a boolean (check_access, checkconstraints)
2267 2267 // which turns off the checks. This is used from specialized contexts
2268 2268 // such as bootstrapping, dumping, and debugging.
2269 2269 //
2270 2270 // No direct constraint is placed between the class and its
2271 2271 // supertypes. Constraints are only placed along linked relations
2272 2272 // between callers and callees. When a method overrides or implements
2273 2273 // an abstract method in a supertype (superclass or interface), the
2274 2274 // constraints are placed as if the supertype were the caller to the
2275 2275 // overriding method. (This works well, since callers to the
2276 2276 // supertype have already established agreement between themselves and
2277 2277 // the supertype.) As a result of all this, a class can disagree with
2278 2278 // its supertype about the meaning of a type name, as long as that
2279 2279 // class neither calls a relevant method of the supertype, nor is
2280 2280 // called (perhaps via an override) from the supertype.
2281 2281 //
2282 2282 //
2283 2283 // SystemDictionary::check_signature_loaders(sig, l1, l2)
2284 2284 //
2285 2285 // Make sure all class components (including arrays) in the given
2286 2286 // signature will be resolved to the same class in both loaders.
2287 2287 // Returns the name of the type that failed a loader constraint check, or
2288 2288 // NULL if no constraint failed. The returned C string needs cleaning up
2289 2289 // with a ResourceMark in the caller. No exception except OOME is thrown.
2290 2290 char* SystemDictionary::check_signature_loaders(symbolHandle signature,
2291 2291 Handle loader1, Handle loader2,
2292 2292 bool is_method, TRAPS) {
2293 2293 // Nothing to do if loaders are the same.
2294 2294 if (loader1() == loader2()) {
2295 2295 return NULL;
2296 2296 }
2297 2297
2298 2298 SignatureStream sig_strm(signature, is_method);
2299 2299 while (!sig_strm.is_done()) {
2300 2300 if (sig_strm.is_object()) {
2301 2301 symbolOop s = sig_strm.as_symbol(CHECK_NULL);
2302 2302 symbolHandle sig (THREAD, s);
2303 2303 if (!add_loader_constraint(sig, loader1, loader2, THREAD)) {
2304 2304 return sig()->as_C_string();
2305 2305 }
2306 2306 }
2307 2307 sig_strm.next();
2308 2308 }
2309 2309 return NULL;
2310 2310 }
2311 2311
2312 2312
2313 2313 methodOop SystemDictionary::find_method_handle_invoke(symbolHandle signature,
2314 2314 Handle class_loader,
2315 2315 Handle protection_domain,
2316 2316 TRAPS) {
2317 2317 if (!EnableMethodHandles) return NULL;
2318 2318 assert(class_loader.is_null() && protection_domain.is_null(),
↓ open down ↓ |
335 lines elided |
↑ open up ↑ |
2319 2319 "cannot load specialized versions of MethodHandle.invoke");
2320 2320 if (invoke_method_table() == NULL) {
2321 2321 // create this side table lazily
2322 2322 _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
2323 2323 }
2324 2324 unsigned int hash = invoke_method_table()->compute_hash(signature);
2325 2325 int index = invoke_method_table()->hash_to_index(hash);
2326 2326 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature);
2327 2327 if (spe == NULL || spe->property_oop() == NULL) {
2328 2328 // Must create lots of stuff here, but outside of the SystemDictionary lock.
2329 + if (THREAD->is_Compiler_thread())
2330 + return NULL; // do not attempt from within compiler
2329 2331 Handle mt = compute_method_handle_type(signature(),
2330 2332 class_loader, protection_domain,
2331 2333 CHECK_NULL);
2332 2334 KlassHandle mh_klass = SystemDictionaryHandles::MethodHandle_klass();
2333 2335 methodHandle m = methodOopDesc::make_invoke_method(mh_klass, signature,
2334 2336 mt, CHECK_NULL);
2335 2337 // Now grab the lock. We might have to throw away the new method,
2336 2338 // if a racing thread has managed to install one at the same time.
2337 2339 {
2338 2340 MutexLocker ml(SystemDictionary_lock, Thread::current());
2339 2341 spe = invoke_method_table()->find_entry(index, hash, signature);
2340 2342 if (spe == NULL)
2341 2343 spe = invoke_method_table()->add_entry(index, hash, signature);
2342 2344 if (spe->property_oop() == NULL)
2343 2345 spe->set_property_oop(m());
2344 2346 }
2345 2347 }
2346 2348 methodOop m = (methodOop) spe->property_oop();
2347 2349 assert(m->is_method(), "");
2348 2350 return m;
2349 2351 }
2350 2352
2351 2353 // Ask Java code to find or construct a java.dyn.MethodType for the given
2352 2354 // signature, as interpreted relative to the given class loader.
2353 2355 // Because of class loader constraints, all method handle usage must be
2354 2356 // consistent with this loader.
2355 2357 Handle SystemDictionary::compute_method_handle_type(symbolHandle signature,
2356 2358 Handle class_loader,
2357 2359 Handle protection_domain,
2358 2360 TRAPS) {
2359 2361 Handle empty;
2360 2362 int npts = ArgumentCount(signature()).size();
2361 2363 objArrayHandle pts = oopFactory::new_objArray(SystemDictionary::class_klass(), npts, CHECK_(empty));
2362 2364 int arg = 0;
2363 2365 Handle rt; // the return type from the signature
2364 2366 for (SignatureStream ss(signature()); !ss.is_done(); ss.next()) {
2365 2367 oop mirror;
2366 2368 if (!ss.is_object()) {
2367 2369 mirror = Universe::java_mirror(ss.type());
2368 2370 } else {
2369 2371 symbolOop name_oop = ss.as_symbol(CHECK_(empty));
2370 2372 symbolHandle name(THREAD, name_oop);
2371 2373 klassOop klass = resolve_or_fail(name,
2372 2374 class_loader, protection_domain,
2373 2375 true, CHECK_(empty));
2374 2376 mirror = Klass::cast(klass)->java_mirror();
2375 2377 }
2376 2378 if (ss.at_return_type())
2377 2379 rt = Handle(THREAD, mirror);
2378 2380 else
2379 2381 pts->obj_at_put(arg++, mirror);
2380 2382 }
2381 2383 assert(arg == npts, "");
2382 2384
2383 2385 // call MethodType java.dyn.MethodType::makeImpl(Class rt, Class[] pts, false, true)
2384 2386 bool varargs = false, trusted = true;
2385 2387 JavaCallArguments args(Handle(THREAD, rt()));
2386 2388 args.push_oop(pts());
2387 2389 args.push_int(false);
2388 2390 args.push_int(trusted);
2389 2391 JavaValue result(T_OBJECT);
2390 2392 JavaCalls::call_static(&result,
2391 2393 SystemDictionary::MethodType_klass(),
2392 2394 vmSymbols::makeImpl_name(), vmSymbols::makeImpl_signature(),
2393 2395 &args, CHECK_(empty));
2394 2396 return Handle(THREAD, (oop) result.get_jobject());
2395 2397 }
2396 2398
2397 2399
2398 2400 // Ask Java code to find or construct a java.dyn.CallSite for the given
2399 2401 // name and signature, as interpreted relative to the given class loader.
2400 2402 Handle SystemDictionary::make_dynamic_call_site(KlassHandle caller,
2401 2403 int caller_method_idnum,
2402 2404 int caller_bci,
2403 2405 symbolHandle name,
2404 2406 methodHandle mh_invdyn,
2405 2407 TRAPS) {
2406 2408 Handle empty;
2407 2409 // call java.dyn.CallSite::makeSite(caller, name, mtype, cmid, cbci)
2408 2410 oop name_str_oop = StringTable::intern(name(), CHECK_(empty)); // not a handle!
2409 2411 JavaCallArguments args(Handle(THREAD, caller->java_mirror()));
2410 2412 args.push_oop(name_str_oop);
2411 2413 args.push_oop(mh_invdyn->method_handle_type());
2412 2414 args.push_int(caller_method_idnum);
2413 2415 args.push_int(caller_bci);
2414 2416 JavaValue result(T_OBJECT);
2415 2417 JavaCalls::call_static(&result,
2416 2418 SystemDictionary::CallSite_klass(),
2417 2419 vmSymbols::makeSite_name(), vmSymbols::makeSite_signature(),
2418 2420 &args, CHECK_(empty));
2419 2421 oop call_site_oop = (oop) result.get_jobject();
2420 2422 assert(call_site_oop->is_oop()
2421 2423 /*&& java_dyn_CallSite::is_instance(call_site_oop)*/, "must be sane");
2422 2424 java_dyn_CallSite::set_vmmethod(call_site_oop, mh_invdyn());
2423 2425 if (TraceMethodHandles) {
2424 2426 #ifndef PRODUCT
2425 2427 tty->print_cr("Linked invokedynamic bci=%d site="INTPTR_FORMAT":", caller_bci, call_site_oop);
2426 2428 call_site_oop->print();
2427 2429 tty->cr();
2428 2430 #endif // PRODUCT
2429 2431 }
2430 2432 return call_site_oop;
2431 2433 }
2432 2434
2433 2435 Handle SystemDictionary::find_bootstrap_method(KlassHandle caller,
2434 2436 KlassHandle search_bootstrap_klass,
2435 2437 TRAPS) {
2436 2438 Handle empty;
2437 2439 if (!caller->oop_is_instance()) return empty;
2438 2440
2439 2441 instanceKlassHandle ik(THREAD, caller());
2440 2442
2441 2443 oop boot_method_oop = ik->bootstrap_method();
2442 2444 if (boot_method_oop != NULL) {
2443 2445 if (TraceMethodHandles) {
2444 2446 tty->print_cr("bootstrap method for "PTR_FORMAT" cached as "PTR_FORMAT":", ik(), boot_method_oop);
2445 2447 }
2446 2448 NOT_PRODUCT(if (!boot_method_oop->is_oop()) { tty->print_cr("*** boot MH of "PTR_FORMAT" = "PTR_FORMAT, ik(), boot_method_oop); ik()->print(); });
2447 2449 assert(boot_method_oop->is_oop()
2448 2450 && java_dyn_MethodHandle::is_instance(boot_method_oop), "must be sane");
2449 2451 return Handle(THREAD, boot_method_oop);
2450 2452 }
2451 2453 boot_method_oop = NULL; // GC safety
2452 2454
2453 2455 // call java.dyn.Linkage::findBootstrapMethod(caller, sbk)
2454 2456 JavaCallArguments args(Handle(THREAD, ik->java_mirror()));
2455 2457 if (search_bootstrap_klass.is_null())
2456 2458 args.push_oop(Handle());
2457 2459 else
2458 2460 args.push_oop(search_bootstrap_klass->java_mirror());
2459 2461 JavaValue result(T_OBJECT);
2460 2462 JavaCalls::call_static(&result,
2461 2463 SystemDictionary::Linkage_klass(),
2462 2464 vmSymbols::findBootstrapMethod_name(),
2463 2465 vmSymbols::findBootstrapMethod_signature(),
2464 2466 &args, CHECK_(empty));
2465 2467 boot_method_oop = (oop) result.get_jobject();
2466 2468
2467 2469 if (boot_method_oop != NULL) {
2468 2470 if (TraceMethodHandles) {
2469 2471 #ifndef PRODUCT
2470 2472 tty->print_cr("--------");
2471 2473 tty->print_cr("bootstrap method for "PTR_FORMAT" computed as "PTR_FORMAT":", ik(), boot_method_oop);
2472 2474 ik()->print();
2473 2475 boot_method_oop->print();
2474 2476 tty->print_cr("========");
2475 2477 #endif //PRODUCT
2476 2478 }
2477 2479 assert(boot_method_oop->is_oop()
2478 2480 && java_dyn_MethodHandle::is_instance(boot_method_oop), "must be sane");
2479 2481 // probably no race conditions, but let's be careful:
2480 2482 if (Atomic::cmpxchg_ptr(boot_method_oop, ik->adr_bootstrap_method(), NULL) == NULL)
2481 2483 ik->set_bootstrap_method(boot_method_oop);
2482 2484 else
2483 2485 boot_method_oop = ik->bootstrap_method();
2484 2486 } else {
2485 2487 if (TraceMethodHandles) {
2486 2488 #ifndef PRODUCT
2487 2489 tty->print_cr("--------");
2488 2490 tty->print_cr("bootstrap method for "PTR_FORMAT" computed as NULL:", ik());
2489 2491 ik()->print();
2490 2492 tty->print_cr("========");
2491 2493 #endif //PRODUCT
2492 2494 }
2493 2495 boot_method_oop = ik->bootstrap_method();
2494 2496 }
2495 2497
2496 2498 return Handle(THREAD, boot_method_oop);
2497 2499 }
2498 2500
2499 2501 // Since the identity hash code for symbols changes when the symbols are
2500 2502 // moved from the regular perm gen (hash in the mark word) to the shared
2501 2503 // spaces (hash is the address), the classes loaded into the dictionary
2502 2504 // may be in the wrong buckets.
2503 2505
2504 2506 void SystemDictionary::reorder_dictionary() {
2505 2507 dictionary()->reorder_dictionary();
2506 2508 }
2507 2509
2508 2510
2509 2511 void SystemDictionary::copy_buckets(char** top, char* end) {
2510 2512 dictionary()->copy_buckets(top, end);
2511 2513 }
2512 2514
2513 2515
2514 2516 void SystemDictionary::copy_table(char** top, char* end) {
2515 2517 dictionary()->copy_table(top, end);
2516 2518 }
2517 2519
2518 2520
2519 2521 void SystemDictionary::reverse() {
2520 2522 dictionary()->reverse();
2521 2523 }
2522 2524
2523 2525 int SystemDictionary::number_of_classes() {
2524 2526 return dictionary()->number_of_entries();
2525 2527 }
2526 2528
2527 2529
2528 2530 // ----------------------------------------------------------------------------
2529 2531 #ifndef PRODUCT
2530 2532
2531 2533 void SystemDictionary::print() {
2532 2534 dictionary()->print();
2533 2535
2534 2536 // Placeholders
2535 2537 GCMutexLocker mu(SystemDictionary_lock);
2536 2538 placeholders()->print();
2537 2539
2538 2540 // loader constraints - print under SD_lock
2539 2541 constraints()->print();
2540 2542 }
2541 2543
2542 2544 #endif
2543 2545
2544 2546 void SystemDictionary::verify() {
2545 2547 guarantee(dictionary() != NULL, "Verify of system dictionary failed");
2546 2548 guarantee(constraints() != NULL,
2547 2549 "Verify of loader constraints failed");
2548 2550 guarantee(dictionary()->number_of_entries() >= 0 &&
2549 2551 placeholders()->number_of_entries() >= 0,
2550 2552 "Verify of system dictionary failed");
2551 2553
2552 2554 // Verify dictionary
2553 2555 dictionary()->verify();
2554 2556
2555 2557 GCMutexLocker mu(SystemDictionary_lock);
2556 2558 placeholders()->verify();
2557 2559
2558 2560 // Verify constraint table
2559 2561 guarantee(constraints() != NULL, "Verify of loader constraints failed");
2560 2562 constraints()->verify(dictionary());
2561 2563 }
2562 2564
2563 2565
2564 2566 void SystemDictionary::verify_obj_klass_present(Handle obj,
2565 2567 symbolHandle class_name,
2566 2568 Handle class_loader) {
2567 2569 GCMutexLocker mu(SystemDictionary_lock);
2568 2570 oop probe = find_class_or_placeholder(class_name, class_loader);
2569 2571 if (probe == NULL) {
2570 2572 probe = SystemDictionary::find_shared_class(class_name);
2571 2573 }
2572 2574 guarantee(probe != NULL &&
2573 2575 (!probe->is_klass() || probe == obj()),
2574 2576 "Loaded klasses should be in SystemDictionary");
2575 2577 }
2576 2578
2577 2579 #ifndef PRODUCT
2578 2580
2579 2581 // statistics code
2580 2582 class ClassStatistics: AllStatic {
2581 2583 private:
2582 2584 static int nclasses; // number of classes
2583 2585 static int nmethods; // number of methods
2584 2586 static int nmethoddata; // number of methodData
2585 2587 static int class_size; // size of class objects in words
2586 2588 static int method_size; // size of method objects in words
2587 2589 static int debug_size; // size of debug info in methods
2588 2590 static int methoddata_size; // size of methodData objects in words
2589 2591
2590 2592 static void do_class(klassOop k) {
2591 2593 nclasses++;
2592 2594 class_size += k->size();
2593 2595 if (k->klass_part()->oop_is_instance()) {
2594 2596 instanceKlass* ik = (instanceKlass*)k->klass_part();
2595 2597 class_size += ik->methods()->size();
2596 2598 class_size += ik->constants()->size();
2597 2599 class_size += ik->local_interfaces()->size();
2598 2600 class_size += ik->transitive_interfaces()->size();
2599 2601 // We do not have to count implementors, since we only store one!
2600 2602 class_size += ik->fields()->size();
2601 2603 }
2602 2604 }
2603 2605
2604 2606 static void do_method(methodOop m) {
2605 2607 nmethods++;
2606 2608 method_size += m->size();
2607 2609 // class loader uses same objArray for empty vectors, so don't count these
2608 2610 if (m->exception_table()->length() != 0) method_size += m->exception_table()->size();
2609 2611 if (m->has_stackmap_table()) {
2610 2612 method_size += m->stackmap_data()->size();
2611 2613 }
2612 2614
2613 2615 methodDataOop mdo = m->method_data();
2614 2616 if (mdo != NULL) {
2615 2617 nmethoddata++;
2616 2618 methoddata_size += mdo->size();
2617 2619 }
2618 2620 }
2619 2621
2620 2622 public:
2621 2623 static void print() {
2622 2624 SystemDictionary::classes_do(do_class);
2623 2625 SystemDictionary::methods_do(do_method);
2624 2626 tty->print_cr("Class statistics:");
2625 2627 tty->print_cr("%d classes (%d bytes)", nclasses, class_size * oopSize);
2626 2628 tty->print_cr("%d methods (%d bytes = %d base + %d debug info)", nmethods,
2627 2629 (method_size + debug_size) * oopSize, method_size * oopSize, debug_size * oopSize);
2628 2630 tty->print_cr("%d methoddata (%d bytes)", nmethoddata, methoddata_size * oopSize);
2629 2631 }
2630 2632 };
2631 2633
2632 2634
2633 2635 int ClassStatistics::nclasses = 0;
2634 2636 int ClassStatistics::nmethods = 0;
2635 2637 int ClassStatistics::nmethoddata = 0;
2636 2638 int ClassStatistics::class_size = 0;
2637 2639 int ClassStatistics::method_size = 0;
2638 2640 int ClassStatistics::debug_size = 0;
2639 2641 int ClassStatistics::methoddata_size = 0;
2640 2642
2641 2643 void SystemDictionary::print_class_statistics() {
2642 2644 ResourceMark rm;
2643 2645 ClassStatistics::print();
2644 2646 }
2645 2647
2646 2648
2647 2649 class MethodStatistics: AllStatic {
2648 2650 public:
2649 2651 enum {
2650 2652 max_parameter_size = 10
2651 2653 };
2652 2654 private:
2653 2655
2654 2656 static int _number_of_methods;
2655 2657 static int _number_of_final_methods;
2656 2658 static int _number_of_static_methods;
2657 2659 static int _number_of_native_methods;
2658 2660 static int _number_of_synchronized_methods;
2659 2661 static int _number_of_profiled_methods;
2660 2662 static int _number_of_bytecodes;
2661 2663 static int _parameter_size_profile[max_parameter_size];
2662 2664 static int _bytecodes_profile[Bytecodes::number_of_java_codes];
2663 2665
2664 2666 static void initialize() {
2665 2667 _number_of_methods = 0;
2666 2668 _number_of_final_methods = 0;
2667 2669 _number_of_static_methods = 0;
2668 2670 _number_of_native_methods = 0;
2669 2671 _number_of_synchronized_methods = 0;
2670 2672 _number_of_profiled_methods = 0;
2671 2673 _number_of_bytecodes = 0;
2672 2674 for (int i = 0; i < max_parameter_size ; i++) _parameter_size_profile[i] = 0;
2673 2675 for (int j = 0; j < Bytecodes::number_of_java_codes; j++) _bytecodes_profile [j] = 0;
2674 2676 };
2675 2677
2676 2678 static void do_method(methodOop m) {
2677 2679 _number_of_methods++;
2678 2680 // collect flag info
2679 2681 if (m->is_final() ) _number_of_final_methods++;
2680 2682 if (m->is_static() ) _number_of_static_methods++;
2681 2683 if (m->is_native() ) _number_of_native_methods++;
2682 2684 if (m->is_synchronized()) _number_of_synchronized_methods++;
2683 2685 if (m->method_data() != NULL) _number_of_profiled_methods++;
2684 2686 // collect parameter size info (add one for receiver, if any)
2685 2687 _parameter_size_profile[MIN2(m->size_of_parameters() + (m->is_static() ? 0 : 1), max_parameter_size - 1)]++;
2686 2688 // collect bytecodes info
2687 2689 {
2688 2690 Thread *thread = Thread::current();
2689 2691 HandleMark hm(thread);
2690 2692 BytecodeStream s(methodHandle(thread, m));
2691 2693 Bytecodes::Code c;
2692 2694 while ((c = s.next()) >= 0) {
2693 2695 _number_of_bytecodes++;
2694 2696 _bytecodes_profile[c]++;
2695 2697 }
2696 2698 }
2697 2699 }
2698 2700
2699 2701 public:
2700 2702 static void print() {
2701 2703 initialize();
2702 2704 SystemDictionary::methods_do(do_method);
2703 2705 // generate output
2704 2706 tty->cr();
2705 2707 tty->print_cr("Method statistics (static):");
2706 2708 // flag distribution
2707 2709 tty->cr();
2708 2710 tty->print_cr("%6d final methods %6.1f%%", _number_of_final_methods , _number_of_final_methods * 100.0F / _number_of_methods);
2709 2711 tty->print_cr("%6d static methods %6.1f%%", _number_of_static_methods , _number_of_static_methods * 100.0F / _number_of_methods);
2710 2712 tty->print_cr("%6d native methods %6.1f%%", _number_of_native_methods , _number_of_native_methods * 100.0F / _number_of_methods);
2711 2713 tty->print_cr("%6d synchronized methods %6.1f%%", _number_of_synchronized_methods, _number_of_synchronized_methods * 100.0F / _number_of_methods);
2712 2714 tty->print_cr("%6d profiled methods %6.1f%%", _number_of_profiled_methods, _number_of_profiled_methods * 100.0F / _number_of_methods);
2713 2715 // parameter size profile
2714 2716 tty->cr();
2715 2717 { int tot = 0;
2716 2718 int avg = 0;
2717 2719 for (int i = 0; i < max_parameter_size; i++) {
2718 2720 int n = _parameter_size_profile[i];
2719 2721 tot += n;
2720 2722 avg += n*i;
2721 2723 tty->print_cr("parameter size = %1d: %6d methods %5.1f%%", i, n, n * 100.0F / _number_of_methods);
2722 2724 }
2723 2725 assert(tot == _number_of_methods, "should be the same");
2724 2726 tty->print_cr(" %6d methods 100.0%%", _number_of_methods);
2725 2727 tty->print_cr("(average parameter size = %3.1f including receiver, if any)", (float)avg / _number_of_methods);
2726 2728 }
2727 2729 // bytecodes profile
2728 2730 tty->cr();
2729 2731 { int tot = 0;
2730 2732 for (int i = 0; i < Bytecodes::number_of_java_codes; i++) {
2731 2733 if (Bytecodes::is_defined(i)) {
2732 2734 Bytecodes::Code c = Bytecodes::cast(i);
2733 2735 int n = _bytecodes_profile[c];
2734 2736 tot += n;
2735 2737 tty->print_cr("%9d %7.3f%% %s", n, n * 100.0F / _number_of_bytecodes, Bytecodes::name(c));
2736 2738 }
2737 2739 }
2738 2740 assert(tot == _number_of_bytecodes, "should be the same");
2739 2741 tty->print_cr("%9d 100.000%%", _number_of_bytecodes);
2740 2742 }
2741 2743 tty->cr();
2742 2744 }
2743 2745 };
2744 2746
2745 2747 int MethodStatistics::_number_of_methods;
2746 2748 int MethodStatistics::_number_of_final_methods;
2747 2749 int MethodStatistics::_number_of_static_methods;
2748 2750 int MethodStatistics::_number_of_native_methods;
2749 2751 int MethodStatistics::_number_of_synchronized_methods;
2750 2752 int MethodStatistics::_number_of_profiled_methods;
2751 2753 int MethodStatistics::_number_of_bytecodes;
2752 2754 int MethodStatistics::_parameter_size_profile[MethodStatistics::max_parameter_size];
2753 2755 int MethodStatistics::_bytecodes_profile[Bytecodes::number_of_java_codes];
2754 2756
2755 2757
2756 2758 void SystemDictionary::print_method_statistics() {
2757 2759 MethodStatistics::print();
2758 2760 }
2759 2761
2760 2762 #endif // PRODUCT
↓ open down ↓ |
422 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX