Skip to content
Permalink
Browse files
8259021: SharedSecrets should avoid double racy reads from non-volati…
…le fields

Reviewed-by: shade, redestad, rriggs, mchung, rrich, alanb
  • Loading branch information
Peter Levart committed Jan 5, 2021
1 parent d5aa49d commit 85bac8c415070992a9807599fe1ec07e4ed0c562
Showing with 93 additions and 45 deletions.
  1. +93 −45 src/java.base/share/classes/jdk/internal/access/SharedSecrets.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -83,21 +83,25 @@ public static void setJavaUtilCollectionAccess(JavaUtilCollectionAccess juca) {
}

public static JavaUtilCollectionAccess getJavaUtilCollectionAccess() {
if (javaUtilCollectionAccess == null) {
var access = javaUtilCollectionAccess;
if (access == null) {
try {
Class.forName("java.util.ImmutableCollections$Access", true, null);
access = javaUtilCollectionAccess;
} catch (ClassNotFoundException e) {};
}
return javaUtilCollectionAccess;
return access;
}

public static JavaUtilJarAccess javaUtilJarAccess() {
if (javaUtilJarAccess == null) {
var access = javaUtilJarAccess;
if (access == null) {
// Ensure JarFile is initialized; we know that this class
// provides the shared secret
ensureClassInitialized(JarFile.class);
access = javaUtilJarAccess;
}
return javaUtilJarAccess;
return access;
}

public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
@@ -117,23 +121,27 @@ public static void setJavaLangInvokeAccess(JavaLangInvokeAccess jlia) {
}

public static JavaLangInvokeAccess getJavaLangInvokeAccess() {
if (javaLangInvokeAccess == null) {
var access = javaLangInvokeAccess;
if (access == null) {
try {
Class.forName("java.lang.invoke.MethodHandleImpl", true, null);
access = javaLangInvokeAccess;
} catch (ClassNotFoundException e) {};
}
return javaLangInvokeAccess;
return access;
}

public static void setJavaLangModuleAccess(JavaLangModuleAccess jlrma) {
javaLangModuleAccess = jlrma;
}

public static JavaLangModuleAccess getJavaLangModuleAccess() {
if (javaLangModuleAccess == null) {
var access = javaLangModuleAccess;
if (access == null) {
ensureClassInitialized(ModuleDescriptor.class);
access = javaLangModuleAccess;
}
return javaLangModuleAccess;
return access;
}

public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
@@ -157,102 +165,127 @@ public static void setJavaNetUriAccess(JavaNetUriAccess jnua) {
}

public static JavaNetUriAccess getJavaNetUriAccess() {
if (javaNetUriAccess == null)
var access = javaNetUriAccess;
if (access == null) {
ensureClassInitialized(java.net.URI.class);
return javaNetUriAccess;
access = javaNetUriAccess;
}
return access;
}

public static void setJavaNetURLAccess(JavaNetURLAccess jnua) {
javaNetURLAccess = jnua;
}

public static JavaNetURLAccess getJavaNetURLAccess() {
if (javaNetURLAccess == null)
var access = javaNetURLAccess;
if (access == null) {
ensureClassInitialized(java.net.URL.class);
return javaNetURLAccess;
access = javaNetURLAccess;
}
return access;
}

public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
javaNetInetAddressAccess = jna;
}

public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
if (javaNetInetAddressAccess == null)
var access = javaNetInetAddressAccess;
if (access == null) {
ensureClassInitialized(java.net.InetAddress.class);
return javaNetInetAddressAccess;
access = javaNetInetAddressAccess;
}
return access;
}

public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
javaNetHttpCookieAccess = a;
}

public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
if (javaNetHttpCookieAccess == null)
var access = javaNetHttpCookieAccess;
if (access == null) {
ensureClassInitialized(java.net.HttpCookie.class);
return javaNetHttpCookieAccess;
access = javaNetHttpCookieAccess;
}
return access;
}

public static void setJavaNioAccess(JavaNioAccess jna) {
javaNioAccess = jna;
}

public static JavaNioAccess getJavaNioAccess() {
if (javaNioAccess == null) {
var access = javaNioAccess;
if (access == null) {
// Ensure java.nio.Buffer is initialized, which provides the
// shared secret.
ensureClassInitialized(java.nio.Buffer.class);
access = javaNioAccess;
}
return javaNioAccess;
return access;
}

public static void setJavaIOAccess(JavaIOAccess jia) {
javaIOAccess = jia;
}

public static JavaIOAccess getJavaIOAccess() {
if (javaIOAccess == null) {
var access = javaIOAccess;
if (access == null) {
ensureClassInitialized(Console.class);
access = javaIOAccess;
}
return javaIOAccess;
return access;
}

public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
javaIOFileDescriptorAccess = jiofda;
}

public static JavaIOFilePermissionAccess getJavaIOFilePermissionAccess() {
if (javaIOFilePermissionAccess == null)
var access = javaIOFilePermissionAccess;
if (access == null) {
ensureClassInitialized(FilePermission.class);

return javaIOFilePermissionAccess;
access = javaIOFilePermissionAccess;
}
return access;
}

public static void setJavaIOFilePermissionAccess(JavaIOFilePermissionAccess jiofpa) {
javaIOFilePermissionAccess = jiofpa;
}

public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
if (javaIOFileDescriptorAccess == null)
var access = javaIOFileDescriptorAccess;
if (access == null) {
ensureClassInitialized(FileDescriptor.class);

return javaIOFileDescriptorAccess;
access = javaIOFileDescriptorAccess;
}
return access;
}

public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
javaSecurityAccess = jsa;
}

public static JavaSecurityAccess getJavaSecurityAccess() {
if (javaSecurityAccess == null) {
var access = javaSecurityAccess;
if (access == null) {
ensureClassInitialized(ProtectionDomain.class);
access = javaSecurityAccess;
}
return javaSecurityAccess;
return access;
}

public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
if (javaUtilZipFileAccess == null)
var access = javaUtilZipFileAccess;
if (access == null) {
ensureClassInitialized(java.util.zip.ZipFile.class);
return javaUtilZipFileAccess;
access = javaUtilZipFileAccess;
}
return access;
}

public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
@@ -288,42 +321,51 @@ public static void setJavaBeansAccess(JavaBeansAccess access) {
}

public static JavaUtilResourceBundleAccess getJavaUtilResourceBundleAccess() {
if (javaUtilResourceBundleAccess == null)
var access = javaUtilResourceBundleAccess;
if (access == null) {
ensureClassInitialized(ResourceBundle.class);
return javaUtilResourceBundleAccess;
access = javaUtilResourceBundleAccess;
}
return access;
}

public static void setJavaUtilResourceBundleAccess(JavaUtilResourceBundleAccess access) {
javaUtilResourceBundleAccess = access;
}

public static JavaObjectInputStreamReadString getJavaObjectInputStreamReadString() {
if (javaObjectInputStreamReadString == null) {
var access = javaObjectInputStreamReadString;
if (access == null) {
ensureClassInitialized(ObjectInputStream.class);
access = javaObjectInputStreamReadString;
}
return javaObjectInputStreamReadString;
return access;
}

public static void setJavaObjectInputStreamReadString(JavaObjectInputStreamReadString access) {
javaObjectInputStreamReadString = access;
}

public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
if (javaObjectInputStreamAccess == null) {
var access = javaObjectInputStreamAccess;
if (access == null) {
ensureClassInitialized(ObjectInputStream.class);
access = javaObjectInputStreamAccess;
}
return javaObjectInputStreamAccess;
return access;
}

public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
javaObjectInputStreamAccess = access;
}

public static JavaObjectInputFilterAccess getJavaObjectInputFilterAccess() {
if (javaObjectInputFilterAccess == null) {
var access = javaObjectInputFilterAccess;
if (access == null) {
ensureClassInitialized(ObjectInputFilter.Config.class);
access = javaObjectInputFilterAccess;
}
return javaObjectInputFilterAccess;
return access;
}

public static void setJavaObjectInputFilterAccess(JavaObjectInputFilterAccess access) {
@@ -335,32 +377,38 @@ public static void setJavaIORandomAccessFileAccess(JavaIORandomAccessFileAccess
}

public static JavaIORandomAccessFileAccess getJavaIORandomAccessFileAccess() {
if (javaIORandomAccessFileAccess == null) {
var access = javaIORandomAccessFileAccess;
if (access == null) {
ensureClassInitialized(RandomAccessFile.class);
access = javaIORandomAccessFileAccess;
}
return javaIORandomAccessFileAccess;
return access;
}

public static void setJavaSecuritySignatureAccess(JavaSecuritySignatureAccess jssa) {
javaSecuritySignatureAccess = jssa;
}

public static JavaSecuritySignatureAccess getJavaSecuritySignatureAccess() {
if (javaSecuritySignatureAccess == null) {
var access = javaSecuritySignatureAccess;
if (access == null) {
ensureClassInitialized(Signature.class);
access = javaSecuritySignatureAccess;
}
return javaSecuritySignatureAccess;
return access;
}

public static void setJavaxCryptoSealedObjectAccess(JavaxCryptoSealedObjectAccess jcsoa) {
javaxCryptoSealedObjectAccess = jcsoa;
}

public static JavaxCryptoSealedObjectAccess getJavaxCryptoSealedObjectAccess() {
if (javaxCryptoSealedObjectAccess == null) {
var access = javaxCryptoSealedObjectAccess;
if (access == null) {
ensureClassInitialized(SealedObject.class);
access = javaxCryptoSealedObjectAccess;
}
return javaxCryptoSealedObjectAccess;
return access;
}

private static void ensureClassInitialized(Class<?> c) {

1 comment on commit 85bac8c

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 85bac8c Jan 5, 2021

Please sign in to comment.