Skip to content

Commit 3561b4e

Browse files
author
Brent Christian
committed
8233091: Backout JDK-8212117: Class.forName loads a class but not linked if class is not initialized
Reviewed-by: alanb, dholmes, mchung
1 parent 691e75e commit 3561b4e

13 files changed

Lines changed: 14 additions & 340 deletions

File tree

make/hotspot/symbols/symbols-unix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ JVM_IsSupportedJNIVersion
148148
JVM_IsThreadAlive
149149
JVM_IsVMGeneratedMethodIx
150150
JVM_LatestUserDefinedLoader
151-
JVM_LinkClass
152151
JVM_LoadLibrary
153152
JVM_MaxMemory
154153
JVM_MaxObjectInspectionAge

src/hotspot/share/include/jvm.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,6 @@ JVM_GetCallerClass(JNIEnv *env);
346346
JNIEXPORT jclass JNICALL
347347
JVM_FindPrimitiveClass(JNIEnv *env, const char *utf);
348348

349-
/*
350-
* Link the 'arg' class
351-
*/
352-
JNIEXPORT void JNICALL
353-
JVM_LinkClass(JNIEnv *env, jclass classClass, jclass arg);
354349

355350
/*
356351
* Find a class from a boot class loader. Returns NULL if class not found.

src/hotspot/share/prims/jni.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name))
419419
}
420420

421421
TempNewSymbol sym = SymbolTable::new_symbol(name);
422-
result = find_class_from_class_loader(env, sym, true, true, loader,
422+
result = find_class_from_class_loader(env, sym, true, loader,
423423
protection_domain, true, thread);
424424

425425
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
@@ -3208,7 +3208,7 @@ static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
32083208
Handle protection_domain; // null protection domain
32093209

32103210
TempNewSymbol sym = SymbolTable::new_symbol(name);
3211-
jclass result = find_class_from_class_loader(env, sym, true, true, loader, protection_domain, true, CHECK_NULL);
3211+
jclass result = find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
32123212

32133213
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
32143214
trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));

src/hotspot/share/prims/jvm.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -742,17 +742,6 @@ JVM_END
742742
// Misc. class handling ///////////////////////////////////////////////////////////
743743

744744

745-
JVM_ENTRY(void, JVM_LinkClass(JNIEnv* env, jclass classClass, jclass arg))
746-
JVMWrapper("JVM_LinkClass");
747-
748-
oop r = JNIHandles::resolve(arg);
749-
Klass* klass = java_lang_Class::as_Klass(r);
750-
751-
if (!ClassForNameDeferLinking && klass->is_instance_klass()) {
752-
InstanceKlass::cast(klass)->link_class(CHECK);
753-
}
754-
JVM_END
755-
756745
JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env))
757746
JVMWrapper("JVM_GetCallerClass");
758747

@@ -863,10 +852,9 @@ JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
863852

864853
Handle h_loader(THREAD, loader_oop);
865854
Handle h_prot(THREAD, protection_domain);
866-
867-
jboolean link = !ClassForNameDeferLinking;
868-
jclass result = find_class_from_class_loader(env, h_name, init, link, h_loader,
855+
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
869856
h_prot, false, THREAD);
857+
870858
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
871859
trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
872860
}
@@ -903,7 +891,7 @@ JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
903891
}
904892
Handle h_loader(THREAD, class_loader);
905893
Handle h_prot (THREAD, protection_domain);
906-
jclass result = find_class_from_class_loader(env, h_name, init, false, h_loader,
894+
jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
907895
h_prot, true, thread);
908896

909897
if (log_is_enabled(Debug, class, resolve) && result != NULL) {
@@ -3410,12 +3398,9 @@ JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
34103398

34113399
// Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
34123400

3413-
jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, jboolean link,
3401+
jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
34143402
Handle loader, Handle protection_domain,
34153403
jboolean throwError, TRAPS) {
3416-
// Initialization also implies linking - check for coherent args
3417-
assert((init && link) || !init, "incorrect use of init/link arguments");
3418-
34193404
// Security Note:
34203405
// The Java level wrapper will perform the necessary security check allowing
34213406
// us to pass the NULL as the initiating class loader. The VM is responsible for
@@ -3424,11 +3409,9 @@ jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, jb
34243409
// if there is no security manager in 3-arg Class.forName().
34253410
Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
34263411

3427-
// Check if we should initialize the class (which implies linking), or just link it
3412+
// Check if we should initialize the class
34283413
if (init && klass->is_instance_klass()) {
34293414
klass->initialize(CHECK_NULL);
3430-
} else if (link && klass->is_instance_klass()) {
3431-
InstanceKlass::cast(klass)->link_class(CHECK_NULL);
34323415
}
34333416
return (jclass) JNIHandles::make_local(env, klass->java_mirror());
34343417
}

src/hotspot/share/prims/jvm_misc.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
// Useful entry points shared by JNI and JVM interface.
3232
// We do not allow real JNI or JVM entry point to call each other.
3333

34-
jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, jboolean link,
35-
Handle loader, Handle protection_domain, jboolean throwError, TRAPS);
34+
jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS);
3635

3736
void trace_class_resolution(Klass* to_class);
3837

src/hotspot/share/runtime/globals.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,9 +2223,6 @@ const size_t minimumSymbolTableSize = 1024;
22232223
"Maximum total size of NIO direct-buffer allocations") \
22242224
range(0, max_jlong) \
22252225
\
2226-
product(bool, ClassForNameDeferLinking, false, \
2227-
"Revert to not linking in Class.forName()") \
2228-
\
22292226
/* Flags used for temporary code during development */ \
22302227
\
22312228
diagnostic(bool, UseNewCode, false, \

src/java.base/share/classes/java/lang/Class.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,6 @@ public static Class<?> forName(String className)
392392
*
393393
* @see java.lang.Class#forName(String)
394394
* @see java.lang.ClassLoader
395-
*
396-
* @jls 12.2 Loading of Classes and Interfaces
397-
* @jls 12.3 Linking of Classes and Interfaces
398-
* @jls 12.4 Initialization of Classes and Interfaces
399395
* @since 1.2
400396
*/
401397
@CallerSensitive
@@ -442,10 +438,6 @@ private static native Class<?> forName0(String name, boolean initialize,
442438
* <p> This method does not check whether the requested class is
443439
* accessible to its caller. </p>
444440
*
445-
* <p> Note that this method throws errors related to loading and linking as
446-
* specified in Sections 12.2 and 12.3 of <em>The Java Language
447-
* Specification</em>.
448-
*
449441
* @apiNote
450442
* This method returns {@code null} on failure rather than
451443
* throwing a {@link ClassNotFoundException}, as is done by
@@ -473,8 +465,6 @@ private static native Class<?> forName0(String name, boolean initialize,
473465
* in a module.</li>
474466
* </ul>
475467
*
476-
* @jls 12.2 Loading of Classes and Interfaces
477-
* @jls 12.3 Linking of Classes and Interfaces
478468
* @since 9
479469
* @spec JPMS
480470
*/
@@ -498,21 +488,13 @@ public static Class<?> forName(Module module, String name) {
498488
cl = module.getClassLoader();
499489
}
500490

501-
Class<?> ret;
502491
if (cl != null) {
503-
ret = cl.loadClass(module, name);
492+
return cl.loadClass(module, name);
504493
} else {
505-
ret = BootLoader.loadClass(module, name);
494+
return BootLoader.loadClass(module, name);
506495
}
507-
if (ret != null) {
508-
// The loaded class should also be linked
509-
linkClass(ret);
510-
}
511-
return ret;
512496
}
513497

514-
private static native void linkClass(Class<?> c);
515-
516498
/**
517499
* Creates a new instance of the class represented by this {@code Class}
518500
* object. The class is instantiated as if by a {@code new}

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,17 +1933,12 @@ public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuc
19331933
}
19341934

19351935
/**
1936-
* Looks up a class by name from the lookup context defined by this {@code Lookup} object.
1937-
* This method attempts to locate, load, and link the class, and then determines whether
1938-
* the class is accessible to this {@code Lookup} object. The static
1936+
* Looks up a class by name from the lookup context defined by this {@code Lookup} object. The static
19391937
* initializer of the class is not run.
19401938
* <p>
19411939
* The lookup context here is determined by the {@linkplain #lookupClass() lookup class}, its class
1942-
* loader, and the {@linkplain #lookupModes() lookup modes}.
1943-
* <p>
1944-
* Note that this method throws errors related to loading and linking as
1945-
* specified in Sections 12.2 and 12.3 of <em>The Java Language
1946-
* Specification</em>.
1940+
* loader, and the {@linkplain #lookupModes() lookup modes}. In particular, the method first attempts to
1941+
* load the requested class, and then determines whether the class is accessible to this lookup object.
19471942
*
19481943
* @param targetName the fully qualified name of the class to be looked up.
19491944
* @return the requested class.
@@ -1955,9 +1950,6 @@ public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuc
19551950
* modes.
19561951
* @throws SecurityException if a security manager is present and it
19571952
* <a href="MethodHandles.Lookup.html#secmgr">refuses access</a>
1958-
*
1959-
* @jls 12.2 Loading of Classes and Interfaces
1960-
* @jls 12.3 Linking of Classes and Interfaces
19611953
* @since 9
19621954
*/
19631955
public Class<?> findClass(String targetName) throws ClassNotFoundException, IllegalAccessException {

src/java.base/share/classes/sun/launcher/LauncherHelper.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import java.nio.file.DirectoryStream;
6363
import java.nio.file.Files;
6464
import java.nio.file.Path;
65-
import java.security.AccessControlException;
6665
import java.text.Normalizer;
6766
import java.text.MessageFormat;
6867
import java.util.ArrayList;
@@ -725,9 +724,6 @@ private static Class<?> loadModuleMainClass(String what) {
725724
} catch (LinkageError le) {
726725
abort(null, "java.launcher.module.error3", mainClass, m.getName(),
727726
le.getClass().getName() + ": " + le.getLocalizedMessage());
728-
} catch (AccessControlException ace) {
729-
abort(ace, "java.launcher.module.error5", mainClass, m.getName(),
730-
ace.getClass().getName(), ace.getLocalizedMessage());
731727
}
732728
if (c == null) {
733729
abort(null, "java.launcher.module.error2", mainClass, mainModule);
@@ -784,9 +780,6 @@ private static Class<?> loadMainClass(int mode, String what) {
784780
} catch (LinkageError le) {
785781
abort(le, "java.launcher.cls.error6", cn,
786782
le.getClass().getName() + ": " + le.getLocalizedMessage());
787-
} catch (AccessControlException ace) {
788-
abort(ace, "java.launcher.cls.error7", cn,
789-
ace.getClass().getName(), ace.getLocalizedMessage());
790783
}
791784
return mainClass;
792785
}

src/java.base/share/native/libjava/Class.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ static JNINativeMethod methods[] = {
7373
{"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations},
7474
{"getNestHost0", "()" CLS, (void *)&JVM_GetNestHost},
7575
{"getNestMembers0", "()[" CLS, (void *)&JVM_GetNestMembers},
76-
{"linkClass", "(" CLS ")V", (void *)&JVM_LinkClass},
7776
};
7877

7978
#undef OBJ

0 commit comments

Comments
 (0)