1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
977
978 ClassLoaderData* loader_data;
979 if (host_klass.not_null()) {
980 // Create a new CLD for anonymous class, that uses the same class loader
981 // as the host_klass
982 assert(EnableInvokeDynamic, "");
983 guarantee(host_klass->class_loader() == class_loader(), "should be the same");
984 guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
985 loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
986 loader_data->record_dependency(host_klass(), CHECK_NULL);
987 } else {
988 loader_data = ClassLoaderData::class_loader_data(class_loader());
989 }
990
991 // Parse the stream. Note that we do this even though this klass might
992 // already be present in the SystemDictionary, otherwise we would not
993 // throw potential ClassFormatErrors.
994 //
995 // Note: "name" is updated.
996
997 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
998 loader_data,
999 protection_domain,
1000 host_klass,
1001 cp_patches,
1002 parsed_name,
1003 true,
1004 THREAD);
1005
1006
1007 if (host_klass.not_null() && k.not_null()) {
1008 assert(EnableInvokeDynamic, "");
1009 // If it's anonymous, initialize it now, since nobody else will.
1010
1011 {
1012 MutexLocker mu_r(Compile_lock, THREAD);
1013
1014 // Add to class hierarchy, initialize vtables, and do possible
1015 // deoptimizations.
1016 add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1017
1018 // But, do not add to system dictionary.
1019
1020 // compiled code dependencies need to be validated anyway
1021 notice_modification();
1022 }
1023
1024 // Rewrite and patch constant pool here.
1025 k->link_class(CHECK_NULL);
1059 bool DoObjectLock = true;
1060 if (is_parallelCapable(class_loader)) {
1061 DoObjectLock = false;
1062 }
1063
1064 ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1065
1066 // Make sure we are synchronized on the class loader before we proceed
1067 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1068 check_loader_lock_contention(lockObject, THREAD);
1069 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1070
1071 TempNewSymbol parsed_name = NULL;
1072
1073 // Parse the stream. Note that we do this even though this klass might
1074 // already be present in the SystemDictionary, otherwise we would not
1075 // throw potential ClassFormatErrors.
1076 //
1077 // Note: "name" is updated.
1078
1079 instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1080 loader_data,
1081 protection_domain,
1082 parsed_name,
1083 verify,
1084 THREAD);
1085
1086 const char* pkg = "java/";
1087 size_t pkglen = strlen(pkg);
1088 if (!HAS_PENDING_EXCEPTION &&
1089 !class_loader.is_null() &&
1090 parsed_name != NULL &&
1091 parsed_name->utf8_length() >= (int)pkglen &&
1092 !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1093 // It is illegal to define classes in the "java." package from
1094 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1095 ResourceMark rm(THREAD);
1096 char* name = parsed_name->as_C_string();
1097 char* index = strrchr(name, '/');
1098 assert(index != NULL, "must be");
1099 *index = '\0'; // chop to just the package name
1100 while ((index = strchr(name, '/')) != NULL) {
1101 *index = '.'; // replace '/' with '.' in package name
1102 }
1103 const char* fmt = "Prohibited package name: %s";
1104 size_t len = strlen(fmt) + strlen(name);
1359 assert(result.get_type() == T_OBJECT, "just checking");
1360 oop obj = (oop) result.get_jobject();
1361
1362 // Primitive classes return null since forName() can not be
1363 // used to obtain any of the Class objects representing primitives or void
1364 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1365 instanceKlassHandle k =
1366 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1367 // For user defined Java class loaders, check that the name returned is
1368 // the same as that requested. This check is done for the bootstrap
1369 // loader when parsing the class file.
1370 if (class_name == k->name()) {
1371 return k;
1372 }
1373 }
1374 // Class is not found or has the wrong name, return NULL
1375 return nh;
1376 }
1377 }
1378
1379 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1380
1381 ClassLoaderData* loader_data = k->class_loader_data();
1382 Handle class_loader_h(THREAD, loader_data->class_loader());
1383
1384 for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1385
1386 // for bootstrap and other parallel classloaders don't acquire lock,
1387 // use placeholder token
1388 // If a parallelCapable class loader calls define_instance_class instead of
1389 // find_or_define_instance_class to get here, we have a timing
1390 // hole with systemDictionary updates and check_constraints
1391 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1392 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1393 compute_loader_lock_object(class_loader_h, THREAD)),
1394 "define called without lock");
1395 }
1396
1397 // Check class-loading constraints. Throw exception if violation is detected.
1398 // Grabs and releases SystemDictionary_lock
1429 MutexLocker mu_r(Compile_lock, THREAD);
1430
1431 // Add to class hierarchy, initialize vtables, and do possible
1432 // deoptimizations.
1433 add_to_hierarchy(k, CHECK); // No exception, but can block
1434
1435 // Add to systemDictionary - so other classes can see it.
1436 // Grabs and releases SystemDictionary_lock
1437 update_dictionary(d_index, d_hash, p_index, p_hash,
1438 k, class_loader_h, THREAD);
1439 }
1440 k->eager_initialize(THREAD);
1441
1442 // notify jvmti
1443 if (JvmtiExport::should_post_class_load()) {
1444 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1445 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1446
1447 }
1448
1449 }
1450
1451 // Support parallel classloading
1452 // All parallel class loaders, including bootstrap classloader
1453 // lock a placeholder entry for this class/class_loader pair
1454 // to allow parallel defines of different classes for this class loader
1455 // With AllowParallelDefine flag==true, in case they do not synchronize around
1456 // FindLoadedClass/DefineClass, calls, we check for parallel
1457 // loading for them, wait if a defineClass is in progress
1458 // and return the initial requestor's results
1459 // This flag does not apply to the bootstrap classloader.
1460 // With AllowParallelDefine flag==false, call through to define_instance_class
1461 // which will throw LinkageError: duplicate class definition.
1462 // False is the requested default.
1463 // For better performance, the class loaders should synchronize
1464 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1465 // potentially waste time reading and parsing the bytestream.
1466 // Note: VM callers should ensure consistency of k/class_name,class_loader
1467 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1468
2666 guarantee(dictionary()->number_of_entries() >= 0 &&
2667 placeholders()->number_of_entries() >= 0,
2668 "Verify of system dictionary failed");
2669
2670 // Verify dictionary
2671 dictionary()->verify();
2672
2673 GCMutexLocker mu(SystemDictionary_lock);
2674 placeholders()->verify();
2675
2676 // Verify constraint table
2677 guarantee(constraints() != NULL, "Verify of loader constraints failed");
2678 constraints()->verify(dictionary(), placeholders());
2679 }
2680
2681 // utility function for class load event
2682 void SystemDictionary::post_class_load_event(const Ticks& start_time,
2683 instanceKlassHandle k,
2684 Handle initiating_loader) {
2685 #if INCLUDE_TRACE
2686 EventClassLoad event(UNTIMED);
2687 if (event.should_commit()) {
2688 event.set_starttime(start_time);
2689 event.set_loadedClass(k());
2690 oop defining_class_loader = k->class_loader();
2691 event.set_definingClassLoader(defining_class_loader != NULL ?
2692 defining_class_loader->klass() : (Klass*)NULL);
2693 oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
2694 event.set_initiatingClassLoader(class_loader != NULL ?
2695 class_loader->klass() : (Klass*)NULL);
2696 event.commit();
2697 }
2698 #endif // INCLUDE_TRACE
2699 }
2700
2701 #ifndef PRODUCT
2702
2703 // statistics code
2704 class ClassStatistics: AllStatic {
2705 private:
2706 static int nclasses; // number of classes
2707 static int nmethods; // number of methods
2708 static int nmethoddata; // number of methodData
2709 static int class_size; // size of class objects in words
2710 static int method_size; // size of method objects in words
2711 static int debug_size; // size of debug info in methods
2712 static int methoddata_size; // size of methodData objects in words
2713
2714 static void do_class(Klass* k) {
2715 nclasses++;
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
977
978 ClassLoaderData* loader_data;
979 if (host_klass.not_null()) {
980 // Create a new CLD for anonymous class, that uses the same class loader
981 // as the host_klass
982 assert(EnableInvokeDynamic, "");
983 guarantee(host_klass->class_loader() == class_loader(), "should be the same");
984 guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
985 loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
986 loader_data->record_dependency(host_klass(), CHECK_NULL);
987 } else {
988 loader_data = ClassLoaderData::class_loader_data(class_loader());
989 }
990
991 // Parse the stream. Note that we do this even though this klass might
992 // already be present in the SystemDictionary, otherwise we would not
993 // throw potential ClassFormatErrors.
994 //
995 // Note: "name" is updated.
996
997 ClassFileParser parser(st);
998 instanceKlassHandle k = parser.parseClassFile(class_name,
999 loader_data,
1000 protection_domain,
1001 host_klass,
1002 cp_patches,
1003 parsed_name,
1004 true,
1005 THREAD);
1006 TRACE_KLASS_CREATION(k, parser, THREAD);
1007
1008 if (host_klass.not_null() && k.not_null()) {
1009 assert(EnableInvokeDynamic, "");
1010 // If it's anonymous, initialize it now, since nobody else will.
1011
1012 {
1013 MutexLocker mu_r(Compile_lock, THREAD);
1014
1015 // Add to class hierarchy, initialize vtables, and do possible
1016 // deoptimizations.
1017 add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1018
1019 // But, do not add to system dictionary.
1020
1021 // compiled code dependencies need to be validated anyway
1022 notice_modification();
1023 }
1024
1025 // Rewrite and patch constant pool here.
1026 k->link_class(CHECK_NULL);
1060 bool DoObjectLock = true;
1061 if (is_parallelCapable(class_loader)) {
1062 DoObjectLock = false;
1063 }
1064
1065 ClassLoaderData* loader_data = register_loader(class_loader, CHECK_NULL);
1066
1067 // Make sure we are synchronized on the class loader before we proceed
1068 Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
1069 check_loader_lock_contention(lockObject, THREAD);
1070 ObjectLocker ol(lockObject, THREAD, DoObjectLock);
1071
1072 TempNewSymbol parsed_name = NULL;
1073
1074 // Parse the stream. Note that we do this even though this klass might
1075 // already be present in the SystemDictionary, otherwise we would not
1076 // throw potential ClassFormatErrors.
1077 //
1078 // Note: "name" is updated.
1079
1080 ClassFileParser parser(st);
1081 instanceKlassHandle k = parser.parseClassFile(class_name,
1082 loader_data,
1083 protection_domain,
1084 parsed_name,
1085 verify,
1086 THREAD);
1087 TRACE_KLASS_CREATION(k, parser, THREAD);
1088
1089 const char* pkg = "java/";
1090 size_t pkglen = strlen(pkg);
1091 if (!HAS_PENDING_EXCEPTION &&
1092 !class_loader.is_null() &&
1093 parsed_name != NULL &&
1094 parsed_name->utf8_length() >= (int)pkglen &&
1095 !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
1096 // It is illegal to define classes in the "java." package from
1097 // JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
1098 ResourceMark rm(THREAD);
1099 char* name = parsed_name->as_C_string();
1100 char* index = strrchr(name, '/');
1101 assert(index != NULL, "must be");
1102 *index = '\0'; // chop to just the package name
1103 while ((index = strchr(name, '/')) != NULL) {
1104 *index = '.'; // replace '/' with '.' in package name
1105 }
1106 const char* fmt = "Prohibited package name: %s";
1107 size_t len = strlen(fmt) + strlen(name);
1362 assert(result.get_type() == T_OBJECT, "just checking");
1363 oop obj = (oop) result.get_jobject();
1364
1365 // Primitive classes return null since forName() can not be
1366 // used to obtain any of the Class objects representing primitives or void
1367 if ((obj != NULL) && !(java_lang_Class::is_primitive(obj))) {
1368 instanceKlassHandle k =
1369 instanceKlassHandle(THREAD, java_lang_Class::as_Klass(obj));
1370 // For user defined Java class loaders, check that the name returned is
1371 // the same as that requested. This check is done for the bootstrap
1372 // loader when parsing the class file.
1373 if (class_name == k->name()) {
1374 return k;
1375 }
1376 }
1377 // Class is not found or has the wrong name, return NULL
1378 return nh;
1379 }
1380 }
1381
1382 static void class_define_event(InstanceKlass* k,
1383 const ClassLoaderData* def_cld) {
1384 #if INCLUDE_TRACE
1385 EventClassDefine event;
1386 if (event.should_commit()) {
1387 ResourceMark m;
1388 event.set_definedClass(k);
1389 event.set_definingClassLoader(def_cld);
1390 event.commit();
1391 }
1392 #endif // INCLUDE_TRACE
1393 }
1394
1395 void SystemDictionary::define_instance_class(instanceKlassHandle k, TRAPS) {
1396
1397 ClassLoaderData* loader_data = k->class_loader_data();
1398 Handle class_loader_h(THREAD, loader_data->class_loader());
1399
1400 for (uintx it = 0; it < GCExpandToAllocateDelayMillis; it++){}
1401
1402 // for bootstrap and other parallel classloaders don't acquire lock,
1403 // use placeholder token
1404 // If a parallelCapable class loader calls define_instance_class instead of
1405 // find_or_define_instance_class to get here, we have a timing
1406 // hole with systemDictionary updates and check_constraints
1407 if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1408 assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1409 compute_loader_lock_object(class_loader_h, THREAD)),
1410 "define called without lock");
1411 }
1412
1413 // Check class-loading constraints. Throw exception if violation is detected.
1414 // Grabs and releases SystemDictionary_lock
1445 MutexLocker mu_r(Compile_lock, THREAD);
1446
1447 // Add to class hierarchy, initialize vtables, and do possible
1448 // deoptimizations.
1449 add_to_hierarchy(k, CHECK); // No exception, but can block
1450
1451 // Add to systemDictionary - so other classes can see it.
1452 // Grabs and releases SystemDictionary_lock
1453 update_dictionary(d_index, d_hash, p_index, p_hash,
1454 k, class_loader_h, THREAD);
1455 }
1456 k->eager_initialize(THREAD);
1457
1458 // notify jvmti
1459 if (JvmtiExport::should_post_class_load()) {
1460 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1461 JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1462
1463 }
1464
1465 class_define_event(k(), loader_data);
1466 }
1467
1468 // Support parallel classloading
1469 // All parallel class loaders, including bootstrap classloader
1470 // lock a placeholder entry for this class/class_loader pair
1471 // to allow parallel defines of different classes for this class loader
1472 // With AllowParallelDefine flag==true, in case they do not synchronize around
1473 // FindLoadedClass/DefineClass, calls, we check for parallel
1474 // loading for them, wait if a defineClass is in progress
1475 // and return the initial requestor's results
1476 // This flag does not apply to the bootstrap classloader.
1477 // With AllowParallelDefine flag==false, call through to define_instance_class
1478 // which will throw LinkageError: duplicate class definition.
1479 // False is the requested default.
1480 // For better performance, the class loaders should synchronize
1481 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1482 // potentially waste time reading and parsing the bytestream.
1483 // Note: VM callers should ensure consistency of k/class_name,class_loader
1484 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1485
2683 guarantee(dictionary()->number_of_entries() >= 0 &&
2684 placeholders()->number_of_entries() >= 0,
2685 "Verify of system dictionary failed");
2686
2687 // Verify dictionary
2688 dictionary()->verify();
2689
2690 GCMutexLocker mu(SystemDictionary_lock);
2691 placeholders()->verify();
2692
2693 // Verify constraint table
2694 guarantee(constraints() != NULL, "Verify of loader constraints failed");
2695 constraints()->verify(dictionary(), placeholders());
2696 }
2697
2698 // utility function for class load event
2699 void SystemDictionary::post_class_load_event(const Ticks& start_time,
2700 instanceKlassHandle k,
2701 Handle initiating_loader) {
2702 #if INCLUDE_TRACE
2703 EventClassLoad event;
2704 if (event.should_commit()) {
2705 event.set_loadedClass(k());
2706 event.set_definingClassLoader(k->class_loader_data());
2707 event.set_initiatingClassLoader(ClassLoaderData::class_loader_data_or_null(initiating_loader()));
2708 event.commit();
2709 }
2710 #endif // INCLUDE_TRACE
2711 }
2712
2713 #ifndef PRODUCT
2714
2715 // statistics code
2716 class ClassStatistics: AllStatic {
2717 private:
2718 static int nclasses; // number of classes
2719 static int nmethods; // number of methods
2720 static int nmethoddata; // number of methodData
2721 static int class_size; // size of class objects in words
2722 static int method_size; // size of method objects in words
2723 static int debug_size; // size of debug info in methods
2724 static int methoddata_size; // size of methodData objects in words
2725
2726 static void do_class(Klass* k) {
2727 nclasses++;
|